diff options
| -rw-r--r-- | source/slang/compiler.h | 4 | ||||
| -rw-r--r-- | source/slang/parser.cpp | 25 | ||||
| -rw-r--r-- | source/slang/preprocessor.cpp | 116 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 41 | ||||
| -rw-r--r-- | source/slang/token-defs.h | 2 | ||||
| -rw-r--r-- | tests/render/pound-import.hlsl | 147 | ||||
| -rw-r--r-- | tests/render/pound-import.slang.h | 21 |
7 files changed, 1 insertions, 355 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 "'") diff --git a/tests/render/pound-import.hlsl b/tests/render/pound-import.hlsl deleted file mode 100644 index 07b195966..000000000 --- a/tests/render/pound-import.hlsl +++ /dev/null @@ -1,147 +0,0 @@ -//TEST(smoke,render):COMPARE_HLSL_GLSL_RENDER: - -// This is a basic test case for cross-compilation behavior. -// -// We will define distinct HLSL and GLSL entry points, -// but the two will share a dependency on a file of -// pure Spire code that provides the actual shading logic. - - -// Pull in Spire code depdendency using extended syntax: -#import "pound-import.slang.h" - -#if defined(__HLSL__) - -cbuffer Uniforms -{ - float4x4 modelViewProjection; -}; - -struct AssembledVertex -{ - float3 position; - float3 color; -}; - -struct CoarseVertex -{ - float3 color; -}; - -struct Fragment -{ - float4 color; -}; - -// Vertex Shader - -struct VertexStageInput -{ - AssembledVertex assembledVertex : A; -}; - -struct VertexStageOutput -{ - CoarseVertex coarseVertex : CoarseVertex; - float4 sv_position : SV_Position; -}; - -VertexStageOutput vertexMain(VertexStageInput input) -{ - VertexStageOutput output; - - float3 position = input.assembledVertex.position; - float3 color = input.assembledVertex.color; - - output.coarseVertex.color = color; - output.sv_position = mul(modelViewProjection, float4(position, 1.0)); - - return output; - -} - -// Fragment Shader - -struct FragmentStageInput -{ - CoarseVertex coarseVertex : CoarseVertex; -}; - -struct FragmentStageOutput -{ - Fragment fragment : SV_Target; -}; - -FragmentStageOutput fragmentMain(FragmentStageInput input) -{ - FragmentStageOutput output; - - float3 color = input.coarseVertex.color; - - color = transformColor(color); - - output.fragment.color = float4(color, 1.0); - - return output; -} - -#elif defined(__GLSL__) - -#version 420 - -uniform Uniforms -{ - mat4x4 modelViewProjection; -}; - -#define ASSEMBLED_VERTEX(QUAL) \ - /* */ - -#define V2F(QUAL) \ - layout(location = 0) QUAL vec3 coarse_color; \ - /* */ - -// Vertex Shader - -#ifdef __GLSL_VERTEX__ - -layout(location = 0) -in vec3 assembled_position; - -layout(location = 1) -in vec3 assembled_color; - -V2F(out) - -void main() -{ - vec3 position = assembled_position; - vec3 color = assembled_color; - - coarse_color = color; -// gl_Position = modelViewProjection * vec4(position, 1.0); - gl_Position = vec4(position, 1.0) * modelViewProjection; -} - -#endif - -#ifdef __GLSL_FRAGMENT__ - -V2F(in) - -layout(location = 0) -out vec4 fragment_color; - -void main() -{ - vec3 color = coarse_color; - - color = transformColor(color); - - fragment_color = vec4(color, 1.0); -} - - -#endif - -#endif diff --git a/tests/render/pound-import.slang.h b/tests/render/pound-import.slang.h deleted file mode 100644 index d53005688..000000000 --- a/tests/render/pound-import.slang.h +++ /dev/null @@ -1,21 +0,0 @@ -//TEST_IGNORE_FILE: - -// This file implements the "library" code -// that both the HLSL and GLSL shaders share. -// -// This code is written in Slang (more or less -// just HLSL), and will be translated as needed -// for each of the targets. - -float3 transformColor(float3 color) -{ - float3 result; - - result.x = sin(20.0 * (color.x + color.y)); - result.y = saturate(cos(color.z * 30.0)); - result.z = sin(color.x * color.y * color.z * 100.0); - - result = 0.5 * (result + 1); - - return result; -}
\ No newline at end of file |
