summaryrefslogtreecommitdiff
path: root/source/slang
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2018-01-29 16:32:52 -0800
committerGitHub <noreply@github.com>2018-01-29 16:32:52 -0800
commitb6bc0837ba6e7fb42280bf6289f4dab633c88392 (patch)
tree1a802fe0f95eaed5d844a55acb666a4791442051 /source/slang
parent06f0effb848c6b938e03da8a61e44b3d032380e8 (diff)
Remove #import directive (#389)
Fixes #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.
Diffstat (limited to 'source/slang')
-rw-r--r--source/slang/compiler.h4
-rw-r--r--source/slang/parser.cpp25
-rw-r--r--source/slang/preprocessor.cpp116
-rw-r--r--source/slang/slang.cpp41
-rw-r--r--source/slang/token-defs.h2
5 files changed, 1 insertions, 187 deletions
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index 960e67ffe..fab1e19c8 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -364,10 +364,6 @@ namespace Slang
String const& source,
SourceLoc const& loc);
- void handlePoundImport(
- String const& path,
- TokenList const& tokens);
-
void loadParsedModule(
RefPtr<TranslationUnitRequest> const& translationUnit,
Name* name,
diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp
index 531606f8d..538e5d576 100644
--- a/source/slang/parser.cpp
+++ b/source/slang/parser.cpp
@@ -835,24 +835,6 @@ namespace Slang
return decl;
}
- static RefPtr<Decl> parsePoundImportDecl(
- Parser* parser)
- {
- parser->haveSeenAnyImportDecls = true;
-
- Token importToken = parser->ReadToken(TokenType::PoundImport);
-
- NameLoc nameAndLoc;
- nameAndLoc.name = getName(parser, importToken.Content);
- nameAndLoc.loc = importToken.loc;
-
- auto decl = new ImportDecl();
- decl->moduleNameAndLoc = nameAndLoc;
- decl->scope = parser->currentScope;
-
- return decl;
- }
-
static NameLoc ParseDeclName(
Parser* parser)
{
@@ -2592,13 +2574,6 @@ namespace Slang
}
break;
- // The preprocessor will generate a custom token to represent
- // the site of a `#import` directive, so that we can catch
- // it downstream in the parser, here.
- case TokenType::PoundImport:
- decl = parsePoundImportDecl(parser);
- break;
-
// If nothing else matched, we try to parse an "ordinary" declarator-based declaration
default:
decl = ParseDeclaratorDecl(parser, containerDecl);
diff --git a/source/slang/preprocessor.cpp b/source/slang/preprocessor.cpp
index 533a81521..46dbd8fe9 100644
--- a/source/slang/preprocessor.cpp
+++ b/source/slang/preprocessor.cpp
@@ -1562,9 +1562,6 @@ static void expectEndOfDirective(PreprocessorDirectiveContext* context)
AdvanceRawToken(context->preprocessor);
}
-// Handle a `#import` directive
-static void HandleImportDirective(PreprocessorDirectiveContext* context);
-
// Handle a `#include` directive
static void HandleIncludeDirective(PreprocessorDirectiveContext* context)
{
@@ -1928,7 +1925,6 @@ static const PreprocessorDirective kDirectives[] =
{ "elif", &HandleElifDirective, ProcessWhenSkipping },
{ "endif", &HandleEndIfDirective, ProcessWhenSkipping },
- { "import", &HandleImportDirective, 0 },
{ "include", &HandleIncludeDirective, 0 },
{ "define", &HandleDefineDirective, 0 },
{ "undef", &HandleUndefDirective, 0 },
@@ -2188,116 +2184,4 @@ TokenList preprocessSource(
return tokens;
}
-//
-
-// Handle a `#import` directive
-static void HandleImportDirective(PreprocessorDirectiveContext* context)
-{
- Token pathToken;
- if(!Expect(context, TokenType::StringLiteral, Diagnostics::expectedTokenInPreprocessorDirective, &pathToken))
- return;
-
- String path = getFileNameTokenValue(pathToken);
-
- auto directiveLoc = GetDirectiveLoc(context);
- auto expandedDirectiveLoc = context->preprocessor->translationUnit->compileRequest->getSourceManager()->expandSourceLoc(directiveLoc);
- String pathIncludedFrom = expandedDirectiveLoc.getSpellingPath();
- String foundPath;
- String foundSource;
-
- IncludeHandler* includeHandler = context->preprocessor->includeHandler;
- if (!includeHandler)
- {
- GetSink(context)->diagnose(pathToken.loc, Diagnostics::importFailed, path);
- GetSink(context)->diagnose(pathToken.loc, Diagnostics::noIncludeHandlerSpecified);
- return;
- }
- auto includeResult = includeHandler->TryToFindIncludeFile(path, pathIncludedFrom, &foundPath, &foundSource);
-
- switch (includeResult)
- {
- case IncludeResult::NotFound:
- case IncludeResult::Error:
- GetSink(context)->diagnose(pathToken.loc, Diagnostics::importFailed, path);
- return;
-
- case IncludeResult::Found:
- break;
- }
-
- // Do all checking related to the end of this directive before we push a new stream,
- // just to avoid complications where that check would need to deal with
- // a switch of input stream
- expectEndOfDirective(context);
-
- // TODO: may want to have some kind of canonicalization step here
- String moduleKey = foundPath;
-
- // Import code from the chosen file, if needed. We only
- // need to import on the first `#import` directive, and
- // after that we ignore additional `#import`s for the same file.
- {
- auto translationUnit = context->preprocessor->translationUnit;
- auto request = translationUnit->compileRequest;
-
-
- // Have we already loaded a module matching this name?
- if (request->mapPathToLoadedModule.TryGetValue(moduleKey))
- {
- // The module has already been loaded, so we don't need to
- // actually tokenize the code here. But note that we *do*
- // go on to insert tokens for an `import` operation into
- // the stream, so it is up to downstream code to avoid
- // re-importing the same thing twice.
- }
- else
- {
- // We are going to preprocess the file using the *same* preprocessor
- // state that is already active. The main alternative would be
- // to construct a fresh preprocessor and use that. The current
- // choice is made so that macros defined in the imported file
- // will be made visible to the importer, rather than disappear
- // when a sub-preprocessor gets finalized.
- auto preprocessor = context->preprocessor;
-
- // We need to save/restore the input stream, so that we can
- // re-use the preprocessor
- PreprocessorInputStream* savedStream = preprocessor->inputStream;
-
- // Create an input stream for reading from the imported file
- SourceFile* sourceFile = context->preprocessor->getCompileRequest()->getSourceManager()->allocateSourceFile(foundPath, foundSource);
- PreprocessorInputStream* subInputStream = CreateInputStreamForSource(preprocessor, sourceFile);
-
- // Now preprocess that stream
- preprocessor->inputStream = subInputStream;
- TokenList subTokens = ReadAllTokens(preprocessor);
-
- // Restore the previous input stream
- preprocessor->inputStream = savedStream;
-
- // Now we need to do something with those tokens we read
- request->handlePoundImport(
- moduleKey,
- subTokens);
- }
- }
-
- // Now create a dummy token stream to represent the import request,
- // so that it can be manifest in the user's program
-
- Token token;
- token.type = TokenType::PoundImport;
- token.loc = GetDirectiveLoc(context);
- token.flags = 0;
- token.Content = foundPath;
-
- SimpleTokenInputStream* inputStream = createSimpleInputStream(
- context->preprocessor,
- token);
-
- PushInputStream(context->preprocessor, inputStream);
-}
-
-
-
}
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp
index 773b1dd09..688593529 100644
--- a/source/slang/slang.cpp
+++ b/source/slang/slang.cpp
@@ -559,45 +559,6 @@ RefPtr<ModuleDecl> CompileRequest::loadModule(
return translationUnit->SyntaxNode;
}
-void CompileRequest::handlePoundImport(
- String const& path,
- TokenList const& tokens)
-{
- RefPtr<TranslationUnitRequest> translationUnit = new TranslationUnitRequest();
- translationUnit->compileRequest = this;
-
- translationUnit->compileFlags = this->compileFlags & (SLANG_COMPILE_FLAG_USE_IR);
-
- // Imported code is always native Slang code
- RefPtr<Scope> languageScope = mSession->slangLanguageScope;
-
- RefPtr<ModuleDecl> translationUnitSyntax = new ModuleDecl();
- translationUnit->SyntaxNode = translationUnitSyntax;
-
- parseSourceFile(
- translationUnit.Ptr(),
- tokens,
- &mSink,
- languageScope);
-
- // TODO: handle errors
-
- // TODO: It is a bit broken here that we use the module path,
- // as the "name" when registering things, but this saves
- // us the trouble of trying to special-case things when
- // checking an `import` down the road.
- //
- // Ideally we'd construct a suitable name by effectively
- // running the name->path logic in reverse (e.g., replacing
- // `-` with `_` and `/` with `.`).
- Name* name = getNamePool()->getName(path);
-
- loadParsedModule(
- translationUnit,
- name,
- path);
-}
-
RefPtr<ModuleDecl> CompileRequest::findOrImportModule(
Name* name,
SourceLoc const& loc)
@@ -664,7 +625,7 @@ RefPtr<ModuleDecl> CompileRequest::findOrImportModule(
break;
}
- // Maybe this was loaded previously via `#import`
+ // Maybe this was loaded previously at a different relative name?
if (mapPathToLoadedModule.TryGetValue(foundPath, loadedModule))
return loadedModule->moduleDecl;
diff --git a/source/slang/token-defs.h b/source/slang/token-defs.h
index 08f67f996..9edc1450d 100644
--- a/source/slang/token-defs.h
+++ b/source/slang/token-defs.h
@@ -30,8 +30,6 @@ TOKEN(NewLine, "newline")
TOKEN(LineComment, "line comment")
TOKEN(BlockComment, "block comment")
-TOKEN(PoundImport, "'#import'")
-
#define PUNCTUATION(id, text) \
TOKEN(id, "'" text "'")