Skip to content

Commit b6bc083

Browse files
author
Tim Foley
authored
Remove #import directive (shader-slang#389)
Fixes shader-slang#380 The `#import` directive was a stopgap measure to allow a macro-heavy shader library to incrementally adopt `import`, but it has turned out to cause as many problems as it fixes (not least because users have never been able to form a good mental model around which kind of import to do when). This change yanks support for the feature.
1 parent 06f0eff commit b6bc083

File tree

7 files changed

+1
-355
lines changed

7 files changed

+1
-355
lines changed

source/slang/compiler.h

-4
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,6 @@ namespace Slang
364364
String const& source,
365365
SourceLoc const& loc);
366366

367-
void handlePoundImport(
368-
String const& path,
369-
TokenList const& tokens);
370-
371367
void loadParsedModule(
372368
RefPtr<TranslationUnitRequest> const& translationUnit,
373369
Name* name,

source/slang/parser.cpp

-25
Original file line numberDiff line numberDiff line change
@@ -835,24 +835,6 @@ namespace Slang
835835
return decl;
836836
}
837837

838-
static RefPtr<Decl> parsePoundImportDecl(
839-
Parser* parser)
840-
{
841-
parser->haveSeenAnyImportDecls = true;
842-
843-
Token importToken = parser->ReadToken(TokenType::PoundImport);
844-
845-
NameLoc nameAndLoc;
846-
nameAndLoc.name = getName(parser, importToken.Content);
847-
nameAndLoc.loc = importToken.loc;
848-
849-
auto decl = new ImportDecl();
850-
decl->moduleNameAndLoc = nameAndLoc;
851-
decl->scope = parser->currentScope;
852-
853-
return decl;
854-
}
855-
856838
static NameLoc ParseDeclName(
857839
Parser* parser)
858840
{
@@ -2592,13 +2574,6 @@ namespace Slang
25922574
}
25932575
break;
25942576

2595-
// The preprocessor will generate a custom token to represent
2596-
// the site of a `#import` directive, so that we can catch
2597-
// it downstream in the parser, here.
2598-
case TokenType::PoundImport:
2599-
decl = parsePoundImportDecl(parser);
2600-
break;
2601-
26022577
// If nothing else matched, we try to parse an "ordinary" declarator-based declaration
26032578
default:
26042579
decl = ParseDeclaratorDecl(parser, containerDecl);

source/slang/preprocessor.cpp

-116
Original file line numberDiff line numberDiff line change
@@ -1562,9 +1562,6 @@ static void expectEndOfDirective(PreprocessorDirectiveContext* context)
15621562
AdvanceRawToken(context->preprocessor);
15631563
}
15641564

1565-
// Handle a `#import` directive
1566-
static void HandleImportDirective(PreprocessorDirectiveContext* context);
1567-
15681565
// Handle a `#include` directive
15691566
static void HandleIncludeDirective(PreprocessorDirectiveContext* context)
15701567
{
@@ -1928,7 +1925,6 @@ static const PreprocessorDirective kDirectives[] =
19281925
{ "elif", &HandleElifDirective, ProcessWhenSkipping },
19291926
{ "endif", &HandleEndIfDirective, ProcessWhenSkipping },
19301927

1931-
{ "import", &HandleImportDirective, 0 },
19321928
{ "include", &HandleIncludeDirective, 0 },
19331929
{ "define", &HandleDefineDirective, 0 },
19341930
{ "undef", &HandleUndefDirective, 0 },
@@ -2188,116 +2184,4 @@ TokenList preprocessSource(
21882184
return tokens;
21892185
}
21902186

2191-
//
2192-
2193-
// Handle a `#import` directive
2194-
static void HandleImportDirective(PreprocessorDirectiveContext* context)
2195-
{
2196-
Token pathToken;
2197-
if(!Expect(context, TokenType::StringLiteral, Diagnostics::expectedTokenInPreprocessorDirective, &pathToken))
2198-
return;
2199-
2200-
String path = getFileNameTokenValue(pathToken);
2201-
2202-
auto directiveLoc = GetDirectiveLoc(context);
2203-
auto expandedDirectiveLoc = context->preprocessor->translationUnit->compileRequest->getSourceManager()->expandSourceLoc(directiveLoc);
2204-
String pathIncludedFrom = expandedDirectiveLoc.getSpellingPath();
2205-
String foundPath;
2206-
String foundSource;
2207-
2208-
IncludeHandler* includeHandler = context->preprocessor->includeHandler;
2209-
if (!includeHandler)
2210-
{
2211-
GetSink(context)->diagnose(pathToken.loc, Diagnostics::importFailed, path);
2212-
GetSink(context)->diagnose(pathToken.loc, Diagnostics::noIncludeHandlerSpecified);
2213-
return;
2214-
}
2215-
auto includeResult = includeHandler->TryToFindIncludeFile(path, pathIncludedFrom, &foundPath, &foundSource);
2216-
2217-
switch (includeResult)
2218-
{
2219-
case IncludeResult::NotFound:
2220-
case IncludeResult::Error:
2221-
GetSink(context)->diagnose(pathToken.loc, Diagnostics::importFailed, path);
2222-
return;
2223-
2224-
case IncludeResult::Found:
2225-
break;
2226-
}
2227-
2228-
// Do all checking related to the end of this directive before we push a new stream,
2229-
// just to avoid complications where that check would need to deal with
2230-
// a switch of input stream
2231-
expectEndOfDirective(context);
2232-
2233-
// TODO: may want to have some kind of canonicalization step here
2234-
String moduleKey = foundPath;
2235-
2236-
// Import code from the chosen file, if needed. We only
2237-
// need to import on the first `#import` directive, and
2238-
// after that we ignore additional `#import`s for the same file.
2239-
{
2240-
auto translationUnit = context->preprocessor->translationUnit;
2241-
auto request = translationUnit->compileRequest;
2242-
2243-
2244-
// Have we already loaded a module matching this name?
2245-
if (request->mapPathToLoadedModule.TryGetValue(moduleKey))
2246-
{
2247-
// The module has already been loaded, so we don't need to
2248-
// actually tokenize the code here. But note that we *do*
2249-
// go on to insert tokens for an `import` operation into
2250-
// the stream, so it is up to downstream code to avoid
2251-
// re-importing the same thing twice.
2252-
}
2253-
else
2254-
{
2255-
// We are going to preprocess the file using the *same* preprocessor
2256-
// state that is already active. The main alternative would be
2257-
// to construct a fresh preprocessor and use that. The current
2258-
// choice is made so that macros defined in the imported file
2259-
// will be made visible to the importer, rather than disappear
2260-
// when a sub-preprocessor gets finalized.
2261-
auto preprocessor = context->preprocessor;
2262-
2263-
// We need to save/restore the input stream, so that we can
2264-
// re-use the preprocessor
2265-
PreprocessorInputStream* savedStream = preprocessor->inputStream;
2266-
2267-
// Create an input stream for reading from the imported file
2268-
SourceFile* sourceFile = context->preprocessor->getCompileRequest()->getSourceManager()->allocateSourceFile(foundPath, foundSource);
2269-
PreprocessorInputStream* subInputStream = CreateInputStreamForSource(preprocessor, sourceFile);
2270-
2271-
// Now preprocess that stream
2272-
preprocessor->inputStream = subInputStream;
2273-
TokenList subTokens = ReadAllTokens(preprocessor);
2274-
2275-
// Restore the previous input stream
2276-
preprocessor->inputStream = savedStream;
2277-
2278-
// Now we need to do something with those tokens we read
2279-
request->handlePoundImport(
2280-
moduleKey,
2281-
subTokens);
2282-
}
2283-
}
2284-
2285-
// Now create a dummy token stream to represent the import request,
2286-
// so that it can be manifest in the user's program
2287-
2288-
Token token;
2289-
token.type = TokenType::PoundImport;
2290-
token.loc = GetDirectiveLoc(context);
2291-
token.flags = 0;
2292-
token.Content = foundPath;
2293-
2294-
SimpleTokenInputStream* inputStream = createSimpleInputStream(
2295-
context->preprocessor,
2296-
token);
2297-
2298-
PushInputStream(context->preprocessor, inputStream);
2299-
}
2300-
2301-
2302-
23032187
}

source/slang/slang.cpp

+1-40
Original file line numberDiff line numberDiff line change
@@ -559,45 +559,6 @@ RefPtr<ModuleDecl> CompileRequest::loadModule(
559559
return translationUnit->SyntaxNode;
560560
}
561561

562-
void CompileRequest::handlePoundImport(
563-
String const& path,
564-
TokenList const& tokens)
565-
{
566-
RefPtr<TranslationUnitRequest> translationUnit = new TranslationUnitRequest();
567-
translationUnit->compileRequest = this;
568-
569-
translationUnit->compileFlags = this->compileFlags & (SLANG_COMPILE_FLAG_USE_IR);
570-
571-
// Imported code is always native Slang code
572-
RefPtr<Scope> languageScope = mSession->slangLanguageScope;
573-
574-
RefPtr<ModuleDecl> translationUnitSyntax = new ModuleDecl();
575-
translationUnit->SyntaxNode = translationUnitSyntax;
576-
577-
parseSourceFile(
578-
translationUnit.Ptr(),
579-
tokens,
580-
&mSink,
581-
languageScope);
582-
583-
// TODO: handle errors
584-
585-
// TODO: It is a bit broken here that we use the module path,
586-
// as the "name" when registering things, but this saves
587-
// us the trouble of trying to special-case things when
588-
// checking an `import` down the road.
589-
//
590-
// Ideally we'd construct a suitable name by effectively
591-
// running the name->path logic in reverse (e.g., replacing
592-
// `-` with `_` and `/` with `.`).
593-
Name* name = getNamePool()->getName(path);
594-
595-
loadParsedModule(
596-
translationUnit,
597-
name,
598-
path);
599-
}
600-
601562
RefPtr<ModuleDecl> CompileRequest::findOrImportModule(
602563
Name* name,
603564
SourceLoc const& loc)
@@ -664,7 +625,7 @@ RefPtr<ModuleDecl> CompileRequest::findOrImportModule(
664625
break;
665626
}
666627

667-
// Maybe this was loaded previously via `#import`
628+
// Maybe this was loaded previously at a different relative name?
668629
if (mapPathToLoadedModule.TryGetValue(foundPath, loadedModule))
669630
return loadedModule->moduleDecl;
670631

source/slang/token-defs.h

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ TOKEN(NewLine, "newline")
3030
TOKEN(LineComment, "line comment")
3131
TOKEN(BlockComment, "block comment")
3232

33-
TOKEN(PoundImport, "'#import'")
34-
3533
#define PUNCTUATION(id, text) \
3634
TOKEN(id, "'" text "'")
3735

tests/render/pound-import.hlsl

-147
This file was deleted.

0 commit comments

Comments
 (0)