From 9d515dd257498cba9144dd31f2f3219e997e03d0 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 19 Jan 2018 18:20:23 -0500 Subject: Allow arbitrary type string as type argument in spAddEntryPointEx. --- source/slang/check.cpp | 35 ++++++++++++++++++----------------- source/slang/compiler.h | 4 +++- source/slang/decl-defs.h | 4 +++- source/slang/parser.cpp | 13 +++++++++++++ source/slang/parser.h | 5 +++++ source/slang/slang.cpp | 17 ++++++++++++++++- 6 files changed, 58 insertions(+), 20 deletions(-) (limited to 'source') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index dfc09c485..7cfb317fb 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -6945,25 +6945,26 @@ namespace Slang // Lookup generic parameter types in global scope for (auto name : entryPoint->genericParameterTypeNames) - { - firstDeclWithName = entryPoint->compileRequest->lookupGlobalDecl(name); - if (!firstDeclWithName) - { - // If there doesn't appear to be any such declaration, then - // we need to diagnose it as an error, and then bail out. - sink->diagnose(translationUnitSyntax, Diagnostics::entryPointTypeParameterNotFound, name); - return; - } + { + // parse type name RefPtr type; - if (auto aggType = firstDeclWithName->As()) - { - type = DeclRefType::Create(entryPoint->compileRequest->mSession, DeclRef(aggType, nullptr)); - } - else if (auto typeDefDecl = firstDeclWithName->As()) - { - type = GetType(DeclRef(typeDefDecl, nullptr)); + for (auto & module : entryPoint->compileRequest->loadedModulesList) + { + RefPtr typeExpr = entryPoint->compileRequest->parseTypeString(entryPoint->getTranslationUnit(), + name, module->moduleDecl->scope); + DiagnosticSink nSink; + SemanticsVisitor visitor( + &nSink, + translationUnit->compileRequest, + translationUnit); + auto typeOut = visitor.CheckProperType(TypeExp(typeExpr)); + if (!nSink.errorCount) + { + type = typeOut.type; + break; + } } - else + if (!type) { sink->diagnose(firstDeclWithName, Diagnostics::entryPointTypeSymbolNotAType, name); return; diff --git a/source/slang/compiler.h b/source/slang/compiler.h index 1fca4751c..4854f4060 100644 --- a/source/slang/compiler.h +++ b/source/slang/compiler.h @@ -104,7 +104,7 @@ namespace Slang // The type names we want to substitute into the // global generic type parameters - List genericParameterTypeNames; + List genericParameterTypeNames; // The profile that the entry point will be compiled for // (this is a combination of the target state, and also @@ -318,6 +318,8 @@ namespace Slang ~CompileRequest(); + RefPtr parseTypeString(TranslationUnitRequest * translationUnit, String typeStr, RefPtr scope); + void parseTranslationUnit( TranslationUnitRequest* translationUnit); diff --git a/source/slang/decl-defs.h b/source/slang/decl-defs.h index fb35e327a..8e1985e3f 100644 --- a/source/slang/decl-defs.h +++ b/source/slang/decl-defs.h @@ -196,7 +196,9 @@ SIMPLE_SYNTAX_CLASS(Variable, VarDeclBase); // A "module" of code (essentiately, a single translation unit) // that provides a scope for some number of declarations. -SIMPLE_SYNTAX_CLASS(ModuleDecl, ContainerDecl) +SYNTAX_CLASS(ModuleDecl, ContainerDecl) + FIELD(RefPtr, scope) +END_SYNTAX_CLASS() SYNTAX_CLASS(ImportDecl, Decl) // The name of the module we are trying to import diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 7e36b0e71..531606f8d 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -2704,6 +2704,7 @@ namespace Slang PushScope(program); program->loc = tokenReader.PeekLoc(); + program->scope = currentScope; ParseDeclBody(this, program, TokenType::EndOfFile); PopScope(); @@ -3960,6 +3961,17 @@ namespace Slang return parsePrefixExpr(this); } + RefPtr parseTypeFromSourceFile(TranslationUnitRequest* translationUnit, + TokenSpan const& tokens, + DiagnosticSink* sink, + RefPtr const& outerScope) + { + Parser parser(tokens, sink, outerScope); + parser.translationUnit = translationUnit; + parser.currentScope = outerScope; + return parser.ParseType(); + } + // Parse a source file into an existing translation unit void parseSourceFile( TranslationUnitRequest* translationUnit, @@ -3971,6 +3983,7 @@ namespace Slang parser.translationUnit = translationUnit; + return parser.parseSourceFile(translationUnit->SyntaxNode.Ptr()); } diff --git a/source/slang/parser.h b/source/slang/parser.h index 60fe4b3ae..785b6e345 100644 --- a/source/slang/parser.h +++ b/source/slang/parser.h @@ -14,6 +14,11 @@ namespace Slang DiagnosticSink* sink, RefPtr const& outerScope); + RefPtr parseTypeFromSourceFile(TranslationUnitRequest* translationUnit, + TokenSpan const& tokens, + DiagnosticSink* sink, + RefPtr const& outerScope); + RefPtr populateBaseLanguageModule( Session* session, RefPtr scope); diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 2ebf024e3..d907e7614 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -125,6 +125,21 @@ CompileRequest::CompileRequest(Session* session) CompileRequest::~CompileRequest() {} + +RefPtr CompileRequest::parseTypeString(TranslationUnitRequest * translationUnit, String typeStr, RefPtr scope) +{ + Slang::SourceFile srcFile; + srcFile.content = typeStr; + DiagnosticSink sink; + auto tokens = preprocessSource( + &srcFile, + &sink, + nullptr, + Dictionary(), + translationUnit); + return parseTypeFromSourceFile(translationUnit, tokens, &sink, scope); +} + void CompileRequest::parseTranslationUnit( TranslationUnitRequest* translationUnit) { @@ -429,7 +444,7 @@ int CompileRequest::addEntryPoint( entryPoint->profile = entryPointProfile; entryPoint->translationUnitIndex = translationUnitIndex; for (auto typeName : genericTypeNames) - entryPoint->genericParameterTypeNames.Add(getNamePool()->getName(typeName)); + entryPoint->genericParameterTypeNames.Add(typeName); auto translationUnit = translationUnits[translationUnitIndex].Ptr(); translationUnit->entryPoints.Add(entryPoint); -- cgit v1.2.3