diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2017-08-07 15:44:00 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-08-07 15:44:00 -0700 |
| commit | 9ad2b40b79907c847451891ce2716fcbcdd2e916 (patch) | |
| tree | e492a82b13334955c1c56f6e3f9d25e8165de82c | |
| parent | ca8eea98c89c632dd7b5a6a8b84d379d1e9e59cf (diff) | |
| parent | 7b54f43fb1b123f451460edb0add218a0428fe95 (diff) | |
Merge pull request #153 from tfoleyNV/remove-globals
Remove uses of global variables
| -rw-r--r-- | source/slang/check.cpp | 172 | ||||
| -rw-r--r-- | source/slang/compiler.cpp | 17 | ||||
| -rw-r--r-- | source/slang/compiler.h | 63 | ||||
| -rw-r--r-- | source/slang/emit.cpp | 13 | ||||
| -rw-r--r-- | source/slang/lookup.cpp | 64 | ||||
| -rw-r--r-- | source/slang/lookup.h | 8 | ||||
| -rw-r--r-- | source/slang/lower.cpp | 87 | ||||
| -rw-r--r-- | source/slang/parameter-binding.cpp | 2 | ||||
| -rw-r--r-- | source/slang/parser.cpp | 31 | ||||
| -rw-r--r-- | source/slang/slang-stdlib.cpp | 32 | ||||
| -rw-r--r-- | source/slang/slang-stdlib.h | 15 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 75 | ||||
| -rw-r--r-- | source/slang/slang.vcxproj | 1 | ||||
| -rw-r--r-- | source/slang/slang.vcxproj.filters | 1 | ||||
| -rw-r--r-- | source/slang/syntax-base-defs.h | 28 | ||||
| -rw-r--r-- | source/slang/syntax.cpp | 255 | ||||
| -rw-r--r-- | source/slang/syntax.h | 38 | ||||
| -rw-r--r-- | source/slang/type-defs.h | 31 |
18 files changed, 559 insertions, 374 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp index ccc0fb087..4c73894c5 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -88,6 +88,10 @@ namespace Slang CompileRequest* getCompileRequest() { return request; } TranslationUnitRequest* getTranslationUnit() { return translationUnit; } + Session* getSession() + { + return getCompileRequest()->mSession; + } public: // Translate Types @@ -107,7 +111,7 @@ namespace Slang { return typeType->type; } - return ExpressionType::Error; + return getSession()->getErrorType(); } RefPtr<ExpressionType> TranslateTypeNode(const RefPtr<ExpressionSyntaxNode> & node) { @@ -214,7 +218,8 @@ namespace Slang { auto overloadedExpr = new OverloadedExpr(); overloadedExpr->Position = originalExpr->Position; - overloadedExpr->Type = QualType(ExpressionType::Overloaded); + overloadedExpr->Type = QualType( + getSession()->getOverloadedType()); overloadedExpr->base = baseExpr; overloadedExpr->lookupResult2 = lookupResult; return overloadedExpr; @@ -253,9 +258,9 @@ namespace Slang getSink()->diagnose(item.declRef, Diagnostics::overloadCandidate, declString); } } + // TODO(tfoley): should we construct a new ErrorExpr here? - overloadedExpr->Type = QualType(ExpressionType::Error); - return overloadedExpr; + return CreateErrorExpr(overloadedExpr); } // otherwise, we had a single decl and it was valid, hooray! @@ -273,7 +278,7 @@ namespace Slang { return expr; } - else if (expr->Type.type->Equals(ExpressionType::Error)) + else if (auto errorType = expr->Type.type->As<ErrorType>()) { return expr; } @@ -292,7 +297,7 @@ namespace Slang { return typeType->type; } - return ExpressionType::Error; + return getSession()->getErrorType(); } RefPtr<ExpressionType> ExtractGenericArgType(RefPtr<ExpressionSyntaxNode> exp) @@ -317,7 +322,7 @@ namespace Slang { return typeType->type; } - else if (exp->Type->Equals(ExpressionType::Error)) + else if (auto errorType = exp->Type->As<ErrorType>()) { return exp->Type.type; } @@ -332,7 +337,7 @@ namespace Slang // The arguments should already be checked against // the declaration. RefPtr<ExpressionType> InstantiateGenericType( - DeclRef<GenericDecl> genericDeclRef, + DeclRef<GenericDecl> genericDeclRef, List<RefPtr<ExpressionSyntaxNode>> const& args) { RefPtr<Substitutions> subst = new Substitutions(); @@ -348,7 +353,9 @@ namespace Slang innerDeclRef.decl = GetInner(genericDeclRef); innerDeclRef.substitutions = subst; - return DeclRefType::Create(innerDeclRef); + return DeclRefType::Create( + getSession(), + innerDeclRef); } // Make sure a declaration has been checked, so we can refer to it. @@ -432,7 +439,7 @@ namespace Slang { getSink()->diagnose(typeExp.exp.Ptr(), Diagnostics::unimplemented, "can't fill in default for generic type parameter"); } - *outProperType = ExpressionType::Error; + *outProperType = getSession()->getErrorType(); } return false; } @@ -451,7 +458,7 @@ namespace Slang { getSink()->diagnose(typeExp.exp.Ptr(), Diagnostics::unimplemented, "can't fill in default for generic type parameter"); } - *outProperType = ExpressionType::Error; + *outProperType = getSession()->getErrorType(); } return false; } @@ -525,7 +532,7 @@ namespace Slang { getSink()->diagnose(result.exp.Ptr(), Diagnostics::invalidTypeVoid); } - result.type = ExpressionType::Error; + result.type = getSession()->getErrorType(); return result; } } @@ -546,7 +553,7 @@ namespace Slang RefPtr<ExpressionSyntaxNode> CreateErrorExpr(ExpressionSyntaxNode* expr) { - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); return expr; } @@ -554,7 +561,7 @@ namespace Slang { // TODO: we may want other cases here... - if (expr->Type->Equals(ExpressionType::Error)) + if (auto errorType = expr->Type->As<ErrorType>()) return true; return false; @@ -1042,7 +1049,9 @@ namespace Slang // clobber the type on `fromExpr`, and an invariant here is that coercion // really shouldn't *change* the expression that is passed in, but should // introduce new AST nodes to coerce its value to a different type... - return CreateImplicitCastExpr(ExpressionType::Error, fromExpr); + return CreateImplicitCastExpr( + getSession()->getErrorType(), + fromExpr); } return expr; } @@ -1327,11 +1336,11 @@ namespace Slang if (auto builtinMod = inner->FindModifier<BuiltinTypeModifier>()) { - RegisterBuiltinDecl(decl, builtinMod); + registerBuiltinDecl(getSession(), decl, builtinMod); } if (auto magicMod = inner->FindModifier<MagicTypeModifier>()) { - RegisterMagicDecl(decl, magicMod); + registerMagicDecl(getSession(), decl, magicMod); } } @@ -1588,7 +1597,7 @@ namespace Slang // TODO: This needs to bottleneck through the common variable checks para->Type = CheckUsableType(para->Type); - if (para->Type.Equals(ExpressionType::GetVoid())) + if (para->Type.Equals(getSession()->getVoidType())) { if (!isRewriteMode()) { @@ -1707,7 +1716,7 @@ namespace Slang { RefPtr<ExpressionSyntaxNode> e = expr; e = CheckTerm(e); - e = Coerce(ExpressionType::GetBool(), e); + e = Coerce(getSession()->getBoolType(), e); return e; } @@ -1749,7 +1758,7 @@ namespace Slang { PushOuterStmt(stmt); - stmt->varDecl->Type.type = ExpressionType::GetInt(); + stmt->varDecl->Type.type = getSession()->getIntType(); addModifier(stmt->varDecl, new ConstModifier()); RefPtr<IntVal> rangeBeginVal; @@ -1851,7 +1860,7 @@ namespace Slang { if (!stmt->Expression) { - if (function && !function->ReturnType.Equals(ExpressionType::GetVoid())) + if (function && !function->ReturnType.Equals(getSession()->getVoidType())) { if (!isRewriteMode()) { @@ -1862,7 +1871,7 @@ namespace Slang else { stmt->Expression = CheckTerm(stmt->Expression); - if (!stmt->Expression->Type->Equals(ExpressionType::Error.Ptr())) + if (!stmt->Expression->Type->Equals(getSession()->getErrorType())) { if (function) { @@ -1922,12 +1931,9 @@ namespace Slang // Create a new array type based on the size we found, // and install it into our type. - auto newArrayType = new ArrayExpressionType(); - newArrayType->BaseType = arrayType->BaseType; - newArrayType->ArrayLength = elementCount; - - // Okay we are good to go! - varDecl->Type.type = newArrayType; + varDecl->Type.type = getArrayType( + arrayType->BaseType, + elementCount); } void ValidateArraySizeForVariable(Variable* varDecl) @@ -1973,7 +1979,7 @@ namespace Slang } #endif varDecl->Type = typeExp; - if (varDecl->Type.Equals(ExpressionType::GetVoid())) + if (varDecl->Type.Equals(getSession()->getVoidType())) { if (!isRewriteMode()) { @@ -2034,16 +2040,16 @@ namespace Slang switch (expr->ConstType) { case ConstantExpressionSyntaxNode::ConstantType::Int: - expr->Type = ExpressionType::GetInt(); + expr->Type = getSession()->getIntType(); break; case ConstantExpressionSyntaxNode::ConstantType::Bool: - expr->Type = ExpressionType::GetBool(); + expr->Type = getSession()->getBoolType(); break; case ConstantExpressionSyntaxNode::ConstantType::Float: - expr->Type = ExpressionType::GetFloat(); + expr->Type = getSession()->getFloatType(); break; default: - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); throw "Invalid constant type."; break; } @@ -2273,7 +2279,7 @@ namespace Slang // or NULL if the expression isn't recognized as a constant. RefPtr<IntVal> TryCheckIntegerConstantExpression(ExpressionSyntaxNode* exp) { - if (!exp->Type.type->Equals(ExpressionType::GetInt())) + if (!exp->Type.type->Equals(getSession()->getIntType())) { return nullptr; } @@ -2288,7 +2294,7 @@ namespace Slang RefPtr<IntVal> CheckIntegerConstantExpression(ExpressionSyntaxNode* inExpr) { // First coerce the expression to the expected type - auto expr = Coerce(ExpressionType::GetInt(),inExpr); + auto expr = Coerce(getSession()->getIntType(),inExpr); auto result = TryCheckIntegerConstantExpression(expr.Ptr()); if (!result) { @@ -2309,8 +2315,8 @@ namespace Slang auto baseExpr = subscriptExpr->BaseExpression; auto indexExpr = subscriptExpr->IndexExpression; - if (!indexExpr->Type->Equals(ExpressionType::GetInt()) && - !indexExpr->Type->Equals(ExpressionType::GetUInt())) + if (!indexExpr->Type->Equals(getSession()->getIntType()) && + !indexExpr->Type->Equals(getSession()->getUIntType())) { if (!isRewriteMode()) { @@ -2341,7 +2347,9 @@ namespace Slang RefPtr<ExpressionType> elementType, RefPtr<IntVal> elementCount) { - auto vectorGenericDecl = findMagicDecl("Vector").As<GenericDecl>(); + auto session = getSession(); + auto vectorGenericDecl = findMagicDecl( + session, "Vector").As<GenericDecl>(); auto vectorTypeDecl = vectorGenericDecl->inner; auto substitutions = new Substitutions(); @@ -2351,7 +2359,9 @@ namespace Slang auto declRef = DeclRef<Decl>(vectorTypeDecl.Ptr(), substitutions); - return DeclRefType::Create(declRef)->As<VectorExpressionType>(); + return DeclRefType::Create( + session, + declRef)->As<VectorExpressionType>(); } RefPtr<ExpressionSyntaxNode> visitIndexExpressionSyntaxNode(IndexExpressionSyntaxNode* subscriptExpr) @@ -2388,12 +2398,12 @@ namespace Slang } auto elementType = CoerceToUsableType(TypeExp(baseExpr, baseTypeType->type)); - auto arrayType = new ArrayExpressionType(); - arrayType->BaseType = elementType.Ptr(); - arrayType->ArrayLength = elementCount; + auto arrayType = getArrayType( + elementType, + elementCount); typeResult = arrayType; - subscriptExpr->Type = new TypeType(arrayType); + subscriptExpr->Type = QualType(getTypeType(arrayType)); return subscriptExpr; } else if (auto baseArrayType = baseType->As<ArrayExpressionType>()) @@ -2433,7 +2443,9 @@ namespace Slang // Note(tfoley): The name used for lookup here is a bit magical, since // it must match what the parser installed in subscript declarations. - LookupResult lookupResult = LookUpLocal(this, "operator[]", aggTypeDeclRef); + LookupResult lookupResult = LookUpLocal( + getSession(), + this, "operator[]", aggTypeDeclRef); if (!lookupResult.isValid()) { goto fail; @@ -2553,7 +2565,7 @@ namespace Slang } } } - else if (decl->targetType->Equals(ExpressionType::Error)) + else if (decl->targetType->Equals(getSession()->getErrorType())) { // there was an error, so ignore } @@ -3332,7 +3344,7 @@ namespace Slang } context.mode = OverloadResolveContext::Mode::ForReal; - context.appExpr->Type = QualType(ExpressionType::Error); + context.appExpr->Type = QualType(getSession()->getErrorType()); if (!TryCheckOverloadCandidateArity(context, candidate)) goto error; @@ -4002,7 +4014,9 @@ namespace Slang } else if (auto aggTypeDeclRef = item.declRef.As<AggTypeDecl>()) { - auto type = DeclRefType::Create(aggTypeDeclRef); + auto type = DeclRefType::Create( + getSession(), + aggTypeDeclRef); AddAggTypeOverloadCandidates(item, type, aggTypeDeclRef, context); } else if (auto genericDeclRef = item.declRef.As<GenericDecl>()) @@ -4359,7 +4373,7 @@ namespace Slang { getSink()->diagnose(expr->FunctionExpr, Diagnostics::expectedFunction); } - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); return expr; } } @@ -4577,9 +4591,11 @@ namespace Slang if (expr->declRef) return expr; - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); - auto lookupResult = LookUp(this, expr->name, expr->scope); + auto lookupResult = LookUp( + getSession(), + this, expr->name, expr->scope); if (lookupResult.isValid()) { return createLookupResultExpr( @@ -4603,7 +4619,7 @@ namespace Slang expr->TargetType = targetType; // The way to perform casting depends on the types involved - if (expr->Expression->Type->Equals(ExpressionType::Error.Ptr())) + if (expr->Expression->Type->Equals(getSession()->getErrorType())) { // If the expression being casted has an error type, then just silently succeed expr->Type = targetType.Ptr(); @@ -4636,7 +4652,7 @@ namespace Slang { getSink()->diagnose(expr, Diagnostics::invalidTypeCast, expr->Expression->Type, targetType->ToString()); } - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); return expr; } @@ -4644,6 +4660,7 @@ namespace Slang QualType GetTypeForDeclRef(DeclRef<Decl> declRef) { return getTypeForDeclRef( + getSession(), this, getSink(), declRef, @@ -4868,7 +4885,9 @@ namespace Slang // Checking of the type must be complete before we can reference its members safely EnsureDecl(aggTypeDeclRef.getDecl(), DeclCheckState::Checked); - LookupResult lookupResult = LookUpLocal(this, expr->name, aggTypeDeclRef); + LookupResult lookupResult = LookUpLocal( + getSession(), + this, expr->name, aggTypeDeclRef); if (!lookupResult.isValid()) { goto fail; @@ -4891,7 +4910,9 @@ namespace Slang // Checking of the type must be complete before we can reference its members safely EnsureDecl(aggTypeDeclRef.getDecl(), DeclCheckState::Checked); - LookupResult lookupResult = LookUpLocal(this, expr->name, aggTypeDeclRef); + LookupResult lookupResult = LookUpLocal( + getSession(), + this, expr->name, aggTypeDeclRef); if (!lookupResult.isValid()) { goto fail; @@ -4909,16 +4930,16 @@ namespace Slang { getSink()->diagnose(expr, Diagnostics::noMemberOfNameInType, expr->name, baseType); } - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); return expr; } // All remaining cases assume we have a `BasicType` else if (!baseType->AsBasicType()) - expr->Type = QualType(ExpressionType::Error); + expr->Type = QualType(getSession()->getErrorType()); else - expr->Type = QualType(ExpressionType::Error); - if (!baseType->Equals(ExpressionType::Error.Ptr()) && - expr->Type->Equals(ExpressionType::Error.Ptr())) + expr->Type = QualType(getSession()->getErrorType()); + if (!baseType->Equals(getSession()->getErrorType()) && + expr->Type->Equals(getSession()->getErrorType())) { if (!isRewriteMode()) { @@ -4943,7 +4964,7 @@ namespace Slang arg = CheckTerm(arg); } - expr->Type = ExpressionType::getInitializerListType(); + expr->Type = getSession()->getInitializerListType(); return expr; } @@ -5028,9 +5049,10 @@ namespace Slang // Get the type to use when referencing a declaration QualType getTypeForDeclRef( + Session* session, SemanticsVisitor* sema, DiagnosticSink* sink, - DeclRef<Decl> declRef, + DeclRef<Decl> declRef, RefPtr<ExpressionType>* outTypeResult) { if( sema ) @@ -5049,47 +5071,47 @@ namespace Slang } else if (auto typeAliasDeclRef = declRef.As<TypeDefDecl>()) { - auto type = new NamedExpressionType(typeAliasDeclRef); + auto type = getNamedType(session, typeAliasDeclRef); *outTypeResult = type; - return new TypeType(type); + return QualType(getTypeType(type)); } else if (auto aggTypeDeclRef = declRef.As<AggTypeDecl>()) { - auto type = DeclRefType::Create(aggTypeDeclRef); + auto type = DeclRefType::Create(session, aggTypeDeclRef); *outTypeResult = type; - return new TypeType(type); + return QualType(getTypeType(type)); } else if (auto simpleTypeDeclRef = declRef.As<SimpleTypeDecl>()) { - auto type = DeclRefType::Create(simpleTypeDeclRef); + auto type = DeclRefType::Create(session, simpleTypeDeclRef); *outTypeResult = type; - return new TypeType(type); + return QualType(getTypeType(type)); } else if (auto genericDeclRef = declRef.As<GenericDecl>()) { - auto type = new GenericDeclRefType(genericDeclRef); + auto type = getGenericDeclRefType(session, genericDeclRef); *outTypeResult = type; - return new TypeType(type); + return QualType(getTypeType(type)); } else if (auto funcDeclRef = declRef.As<CallableDecl>()) { - auto type = new FuncType(); - type->declRef = funcDeclRef; - return type; + auto type = getFuncType(session, funcDeclRef); + return QualType(type); } if( sink ) { sink->diagnose(declRef, Diagnostics::unimplemented, "cannot form reference to this kind of declaration"); } - return QualType(ExpressionType::Error); + return QualType(session->getErrorType()); } QualType getTypeForDeclRef( - DeclRef<Decl> declRef) + Session* session, + DeclRef<Decl> declRef) { RefPtr<ExpressionType> typeResult; - return getTypeForDeclRef(nullptr, nullptr, declRef, &typeResult); + return getTypeForDeclRef(session, nullptr, nullptr, declRef, &typeResult); } DeclRef<ExtensionDecl> ApplyExtensionToType( diff --git a/source/slang/compiler.cpp b/source/slang/compiler.cpp index 6bb8abe7e..60f111d83 100644 --- a/source/slang/compiler.cpp +++ b/source/slang/compiler.cpp @@ -8,7 +8,6 @@ #include "parser.h" #include "preprocessor.h" #include "syntax-visitors.h" -#include "slang-stdlib.h" #include "reflection.h" #include "emit.h" @@ -883,8 +882,20 @@ namespace Slang char const* ext, bool isBinary) { - static int counter = 0; - int id = counter++; + // Try to generate a unique ID for the file to dump, + // even in cases where there might be multiple threads + // doing compilation. + // + // This is primarily a debugging aid, so we don't + // really need/want to do anything too elaborate + + static uint32_t counter = 0; +#ifdef WIN32 + uint32_t id = InterlockedIncrement(&counter); +#else + // TODO: actually implement the case for other platforms + uint32_t id = counter++; +#endif String path; path.append("slang-dump-"); diff --git a/source/slang/compiler.h b/source/slang/compiler.h index 180fb027a..8fcbd444d 100644 --- a/source/slang/compiler.h +++ b/source/slang/compiler.h @@ -309,6 +309,69 @@ namespace Slang CompileRequest* compileRequest, char const* text, CodeGenTarget target); + + // + + class Session + { + public: + // + + RefPtr<Scope> coreLanguageScope; + RefPtr<Scope> hlslLanguageScope; + RefPtr<Scope> slangLanguageScope; + RefPtr<Scope> glslLanguageScope; + + List<RefPtr<ProgramSyntaxNode>> loadedModuleCode; + + + // + + // Generated code for stdlib, etc. + String stdlibPath; + String coreLibraryCode; + String slangLibraryCode; + String hlslLibraryCode; + String glslLibraryCode; + + String getStdlibPath(); + String getCoreLibraryCode(); + String getHLSLLibraryCode(); + String getGLSLLibraryCode(); + + // Basic types that we don't want to re-create all the time + RefPtr<ExpressionType> errorType; + RefPtr<ExpressionType> initializerListType; + RefPtr<ExpressionType> overloadedType; + + Dictionary<int, RefPtr<ExpressionType>> builtinTypes; + Dictionary<String, Decl*> magicDecls; + List<RefPtr<ExpressionType>> canonicalTypes; + + void initializeTypes(); + + ExpressionType* getBoolType(); + ExpressionType* getFloatType(); + ExpressionType* getDoubleType(); + ExpressionType* getIntType(); + ExpressionType* getUIntType(); + ExpressionType* getVoidType(); + ExpressionType* getBuiltinType(BaseType flavor); + + ExpressionType* getInitializerListType(); + ExpressionType* getOverloadedType(); + ExpressionType* getErrorType(); + + // + + Session(); + + void addBuiltinSource( + RefPtr<Scope> const& scope, + String const& path, + String const& source); + }; + } #endif
\ No newline at end of file diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 5a59674f5..d2f932ebf 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -375,6 +375,11 @@ struct EmitVisitor : context(context) {} + Session* getSession() + { + return context->shared->entryPoint->compileRequest->mSession; + } + // Low-level emit logic void emitRawTextSpan(char const* textBegin, char const* textEnd) @@ -2308,9 +2313,9 @@ struct EmitVisitor emitTokenWithLocation(litExpr->token); break; } - if(type->Equals(ExpressionType::GetInt())) + if(type->Equals(getSession()->getIntType())) {} - else if(type->Equals(ExpressionType::GetUInt())) + else if(type->Equals(getSession()->getUIntType())) { suffix = "u"; } @@ -2330,9 +2335,9 @@ struct EmitVisitor emitTokenWithLocation(litExpr->token); break; } - if(type->Equals(ExpressionType::GetFloat())) + if(type->Equals(getSession()->getFloatType())) {} - else if(type->Equals(ExpressionType::getDoubleType())) + else if(type->Equals(getSession()->getDoubleType())) { suffix = "l"; } diff --git a/source/slang/lookup.cpp b/source/slang/lookup.cpp index f5e472ec5..b0e9ce971 100644 --- a/source/slang/lookup.cpp +++ b/source/slang/lookup.cpp @@ -22,8 +22,9 @@ struct BreadcrumbInfo }; void DoLocalLookupImpl( + Session* session, String const& name, - DeclRef<ContainerDecl> containerDeclRef, + DeclRef<ContainerDecl> containerDeclRef, LookupRequest const& request, LookupResult& result, BreadcrumbInfo* inBreadcrumbs); @@ -151,6 +152,7 @@ LookupResultItem CreateLookupResultItem( } void DoMemberLookupImpl( + Session* session, String const& name, RefPtr<ExpressionType> baseType, LookupRequest const& request, @@ -168,7 +170,9 @@ void DoMemberLookupImpl( derefBreacrumb.prev = breadcrumbs; // Recursively perform lookup on the result of deref - return DoMemberLookupImpl(name, pointerLikeType->elementType, request, ioResult, &derefBreacrumb); + return DoMemberLookupImpl( + session, + name, pointerLikeType->elementType, request, ioResult, &derefBreacrumb); } // Default case: no dereference needed @@ -177,7 +181,9 @@ void DoMemberLookupImpl( { if (auto baseAggTypeDeclRef = baseDeclRefType->declRef.As<AggTypeDecl>()) { - DoLocalLookupImpl(name, baseAggTypeDeclRef, request, ioResult, breadcrumbs); + DoLocalLookupImpl( + session, + name, baseAggTypeDeclRef, request, ioResult, breadcrumbs); } } @@ -185,18 +191,24 @@ void DoMemberLookupImpl( } void DoMemberLookupImpl( - String const& name, - DeclRef<Decl> baseDeclRef, + Session* session, + String const& name, + DeclRef<Decl> baseDeclRef, LookupRequest const& request, LookupResult& ioResult, BreadcrumbInfo* breadcrumbs) { - auto baseType = getTypeForDeclRef(baseDeclRef); - return DoMemberLookupImpl(name, baseType, request, ioResult, breadcrumbs); + auto baseType = getTypeForDeclRef( + session, + baseDeclRef); + return DoMemberLookupImpl( + session, + name, baseType, request, ioResult, breadcrumbs); } // Look for members of the given name in the given container for declarations void DoLocalLookupImpl( + Session* session, String const& name, DeclRef<ContainerDecl> containerDeclRef, LookupRequest const& request, @@ -246,13 +258,21 @@ void DoLocalLookupImpl( memberRefBreadcrumb.declRef = transparentMemberDeclRef; memberRefBreadcrumb.prev = inBreadcrumbs; - DoMemberLookupImpl(name, transparentMemberDeclRef, request, result, &memberRefBreadcrumb); + DoMemberLookupImpl( + session, + name, + transparentMemberDeclRef, + request, + result, + &memberRefBreadcrumb); } // Consider lookup via extension if( auto aggTypeDeclRef = containerDeclRef.As<AggTypeDecl>() ) { - RefPtr<ExpressionType> type = DeclRefType::Create(aggTypeDeclRef); + RefPtr<ExpressionType> type = DeclRefType::Create( + session, + aggTypeDeclRef); for (auto ext = GetCandidateExtensions(aggTypeDeclRef); ext; ext = ext->nextCandidateExtension) { @@ -264,12 +284,15 @@ void DoLocalLookupImpl( // the constructed result can somehow indicate that a member // was found through an extension. - DoLocalLookupImpl(name, extDeclRef, request, result, inBreadcrumbs); + DoLocalLookupImpl( + session, + name, extDeclRef, request, result, inBreadcrumbs); } } } void DoLookupImpl( + Session* session, String const& name, LookupRequest const& request, LookupResult& result) @@ -301,7 +324,9 @@ void DoLookupImpl( { if( auto genericTypeParam = pp.As<GenericTypeParamDecl>() ) { - subst->args.Add(DeclRefType::Create(DeclRef<GenericTypeParamDecl>(genericTypeParam.Ptr(), nullptr))); + subst->args.Add(DeclRefType::Create( + session, + DeclRef<GenericTypeParamDecl>(genericTypeParam.Ptr(), nullptr))); } else if( auto genericValParam = pp.As<GenericValueParamDecl>() ) { @@ -311,7 +336,9 @@ void DoLookupImpl( } DeclRef<ContainerDecl> containerRef = DeclRef<Decl>(containerDecl, subst).As<ContainerDecl>(); - DoLocalLookupImpl(name, containerRef, request, result, nullptr); + DoLocalLookupImpl( + session, + name, containerRef, request, result, nullptr); } if (result.isValid()) @@ -325,14 +352,18 @@ void DoLookupImpl( // If we run out of scopes, then we are done. } -LookupResult DoLookup(String const& name, LookupRequest const& request) +LookupResult DoLookup( + Session* session, + String const& name, + LookupRequest const& request) { LookupResult result; - DoLookupImpl(name, request, result); + DoLookupImpl(session, name, request, result); return result; } LookupResult LookUp( + Session* session, SemanticsVisitor* semantics, String const& name, RefPtr<Scope> scope) @@ -340,12 +371,13 @@ LookupResult LookUp( LookupRequest request; request.semantics = semantics; request.scope = scope; - return DoLookup(name, request); + return DoLookup(session, name, request); } // perform lookup within the context of a particular container declaration, // and do *not* look further up the chain LookupResult LookUpLocal( + Session* session, SemanticsVisitor* semantics, String const& name, DeclRef<ContainerDecl> containerDeclRef) @@ -354,7 +386,7 @@ LookupResult LookUpLocal( request.semantics = semantics; LookupResult result; - DoLocalLookupImpl(name, containerDeclRef, request, result, nullptr); + DoLocalLookupImpl(session, name, containerDeclRef, request, result, nullptr); return result; } diff --git a/source/slang/lookup.h b/source/slang/lookup.h index 60e39bfcf..35f378948 100644 --- a/source/slang/lookup.h +++ b/source/slang/lookup.h @@ -18,6 +18,7 @@ void buildMemberDictionary(ContainerDecl* decl); // Look up a name in the given scope, proceeding up through // parent scopes as needed. LookupResult LookUp( + Session* session, SemanticsVisitor* semantics, String const& name, RefPtr<Scope> scope); @@ -25,6 +26,7 @@ LookupResult LookUp( // perform lookup within the context of a particular container declaration, // and do *not* look further up the chain LookupResult LookUpLocal( + Session* session, SemanticsVisitor* semantics, String const& name, DeclRef<ContainerDecl> containerDeclRef); @@ -32,13 +34,15 @@ LookupResult LookUpLocal( // TODO: this belongs somewhere else QualType getTypeForDeclRef( + Session* session, SemanticsVisitor* sema, DiagnosticSink* sink, - DeclRef<Decl> declRef, + DeclRef<Decl> declRef, RefPtr<ExpressionType>* outTypeResult); QualType getTypeForDeclRef( - DeclRef<Decl> declRef); + Session* session, + DeclRef<Decl> declRef); } diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index ba6b48509..3449c647e 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -321,6 +321,11 @@ struct LoweringVisitor // then this will point to that variable. RefPtr<Variable> resultVariable; + Session* getSession() + { + return shared->compileRequest->mSession; + } + CodeGenTarget getTarget() { return shared->target; } bool isReservedWord(String const& name) @@ -539,20 +544,25 @@ struct LoweringVisitor RefPtr<ExpressionType> visitGenericDeclRefType(GenericDeclRefType* type) { - return new GenericDeclRefType(translateDeclRef(DeclRef<Decl>(type->declRef)).As<GenericDecl>()); + return getGenericDeclRefType( + type->getSession(), + translateDeclRef(DeclRef<Decl>(type->declRef)).As<GenericDecl>()); } RefPtr<ExpressionType> visitFuncType(FuncType* type) { - RefPtr<FuncType> loweredType = new FuncType(); - loweredType->declRef = translateDeclRef(DeclRef<Decl>(type->declRef)).As<CallableDecl>(); + RefPtr<FuncType> loweredType = getFuncType( + getSession(), + translateDeclRef(DeclRef<Decl>(type->declRef)).As<CallableDecl>()); return loweredType; } RefPtr<ExpressionType> visitDeclRefType(DeclRefType* type) { auto loweredDeclRef = translateDeclRef(type->declRef); - return DeclRefType::Create(loweredDeclRef); + return DeclRefType::Create( + type->getSession(), + loweredDeclRef); } RefPtr<ExpressionType> visitNamedExpressionType(NamedExpressionType* type) @@ -563,19 +573,21 @@ struct LoweringVisitor return lowerType(GetType(type->declRef)); } - return new NamedExpressionType(translateDeclRef(DeclRef<Decl>(type->declRef)).As<TypeDefDecl>()); + return getNamedType( + type->getSession(), + translateDeclRef(DeclRef<Decl>(type->declRef)).As<TypeDefDecl>()); } RefPtr<ExpressionType> visitTypeType(TypeType* type) { - return new TypeType(lowerType(type->type)); + return getTypeType(lowerType(type->type)); } RefPtr<ExpressionType> visitArrayExpressionType(ArrayExpressionType* type) { - RefPtr<ArrayExpressionType> loweredType = new ArrayExpressionType(); - loweredType->BaseType = lowerType(type->BaseType); - loweredType->ArrayLength = lowerVal(type->ArrayLength).As<IntVal>(); + RefPtr<ArrayExpressionType> loweredType = Slang::getArrayType( + lowerType(type->BaseType), + lowerVal(type->ArrayLength).As<IntVal>()); return loweredType; } @@ -2815,9 +2827,9 @@ return loweredExpr; RefPtr<ExpressionType> fieldVarType = fieldType; for (auto aa = info.arraySpecs; aa; aa = aa->next) { - RefPtr<ArrayExpressionType> arrayType = new ArrayExpressionType(); - arrayType->BaseType = fieldVarType; - arrayType->ArrayLength = aa->elementCount; + RefPtr<ArrayExpressionType> arrayType = Slang::getArrayType( + fieldVarType, + aa->elementCount); fieldVarType = arrayType; } @@ -3378,29 +3390,32 @@ return loweredExpr; RefPtr<ExpressionType> getFloatType() { - return ExpressionType::GetFloat(); + return getSession()->getFloatType(); } RefPtr<ExpressionType> getIntType() { - return ExpressionType::GetInt(); + return getSession()->getIntType(); } RefPtr<ExpressionType> getUIntType() { - return ExpressionType::GetUInt(); + return getSession()->getUIntType(); } RefPtr<ExpressionType> getBoolType() { - return ExpressionType::GetBool(); + return getSession()->getBoolType(); } RefPtr<VectorExpressionType> getVectorType( RefPtr<ExpressionType> elementType, RefPtr<IntVal> elementCount) { - auto vectorGenericDecl = findMagicDecl("Vector").As<GenericDecl>(); + auto session = getSession(); + auto vectorGenericDecl = findMagicDecl( + session, + "Vector").As<GenericDecl>(); auto vectorTypeDecl = vectorGenericDecl->inner; auto substitutions = new Substitutions(); @@ -3410,7 +3425,9 @@ return loweredExpr; auto declRef = DeclRef<Decl>(vectorTypeDecl.Ptr(), substitutions); - return DeclRefType::Create(declRef)->As<VectorExpressionType>(); + return DeclRefType::Create( + session, + declRef)->As<VectorExpressionType>(); } RefPtr<IntVal> getConstantIntVal(IntegerLiteralValue value) @@ -3430,18 +3447,7 @@ return loweredExpr; RefPtr<ArrayExpressionType> getUnsizedArrayType( RefPtr<ExpressionType> elementType) { - RefPtr<ArrayExpressionType> arrayType = new ArrayExpressionType(); - arrayType->BaseType = elementType; - return arrayType; - } - - RefPtr<ArrayExpressionType> getArrayType( - RefPtr<ExpressionType> elementType, - RefPtr<IntVal> elementCount) - { - RefPtr<ArrayExpressionType> arrayType = new ArrayExpressionType(); - arrayType->BaseType = elementType; - arrayType->ArrayLength = elementCount; + RefPtr<ArrayExpressionType> arrayType = Slang::getArrayType(elementType); return arrayType; } @@ -3449,7 +3455,7 @@ return loweredExpr; RefPtr<ExpressionType> elementType, IntegerLiteralValue elementCount) { - return getArrayType(elementType, getConstantIntVal(elementCount)); + return Slang::getArrayType(elementType, getConstantIntVal(elementCount)); } RefPtr<ExpressionSyntaxNode> lowerSimpleShaderParameterToGLSLGlobal( @@ -3461,9 +3467,9 @@ return loweredExpr; for (auto aa = info.arraySpecs; aa; aa = aa->next) { - RefPtr<ArrayExpressionType> arrayType = new ArrayExpressionType(); - arrayType->BaseType = type; - arrayType->ArrayLength = aa->elementCount; + RefPtr<ArrayExpressionType> arrayType = Slang::getArrayType( + type, + aa->elementCount); type = arrayType; } @@ -3923,7 +3929,7 @@ return loweredExpr; // Now we will generate a `void main() { ... }` function to call the lowered code. RefPtr<FunctionSyntaxNode> mainDecl = new FunctionSyntaxNode(); - mainDecl->ReturnType.type = ExpressionType::GetVoid(); + mainDecl->ReturnType.type = getSession()->getVoidType(); mainDecl->Name.Content = "main"; // If the user's entry point was called `main` then rename it here @@ -3988,7 +3994,7 @@ return loweredExpr; // Generate a local variable for the result, if any RefPtr<Variable> resultVarDecl; - if (!loweredEntryPointFunc->ReturnType->Equals(ExpressionType::GetVoid())) + if (!loweredEntryPointFunc->ReturnType->Equals(getSession()->getVoidType())) { resultVarDecl = new Variable(); resultVarDecl->Position = loweredEntryPointFunc->Position; @@ -4003,8 +4009,9 @@ return loweredExpr; // Now generate a call to the entry-point function, using the local variables auto entryPointDeclRef = makeDeclRef(loweredEntryPointFunc.Ptr()); - RefPtr<FuncType> entryPointType = new FuncType(); - entryPointType->declRef = entryPointDeclRef; + auto entryPointType = getFuncType( + getSession(), + entryPointDeclRef); RefPtr<VarExpressionSyntaxNode> entryPointRef = new VarExpressionSyntaxNode(); entryPointRef->name = loweredEntryPointFunc->getName(); @@ -4106,7 +4113,7 @@ return loweredExpr; // global-scope declaration. auto loweredReturnType = lowerType(entryPointDecl->ReturnType); RefPtr<Variable> resultGlobal; - if (!loweredReturnType->Equals(ExpressionType::GetVoid())) + if (!loweredReturnType->Equals(getSession()->getVoidType())) { resultGlobal = new Variable(); // TODO: need a scheme for generating unique names @@ -4117,7 +4124,7 @@ return loweredExpr; } loweredDecl->Name.Content = "main"; - loweredDecl->ReturnType.type = ExpressionType::GetVoid(); + loweredDecl->ReturnType.type = getSession()->getVoidType(); // We will emit the body statement in a context where // a `return` statmenet will generate writes to the diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp index 6128261c7..9b1036d02 100644 --- a/source/slang/parameter-binding.cpp +++ b/source/slang/parameter-binding.cpp @@ -1689,7 +1689,7 @@ void generateParameterBindings( RefPtr<Variable> var = new Variable(); var->Name.Content = "SLANG_hack_samplerForTexelFetch"; - var->Type.type = new SamplerStateType(); + var->Type.type = getSamplerStateType(request->mSession); auto typeLayout = new TypeLayout(); typeLayout->type = var->Type.type; diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 801628e30..c36b2f477 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -80,6 +80,12 @@ namespace Slang , fileName(_fileName) , outerScope(outerScope) {} + + Session* getSession() + { + return translationUnit->compileRequest->mSession; + } + RefPtr<ProgramSyntaxNode> Parse(); Token ReadToken(); @@ -820,6 +826,7 @@ namespace Slang if( parser->LookAheadToken(TokenType::Identifier) ) { LookupResult lookupResult = LookUp( + parser->getSession(), nullptr, // No semantics visitor available yet! parser->tokenReader.PeekToken().Content, parser->currentScope); @@ -2070,12 +2077,14 @@ namespace Slang auto paramConstraint = new GenericTypeConstraintDecl(); parser->FillPosition(paramConstraint); - auto paramType = DeclRefType::Create(DeclRef<Decl>(paramDecl, nullptr)); + auto paramType = DeclRefType::Create( + parser->getSession(), + DeclRef<Decl>(paramDecl, nullptr)); auto paramTypeExpr = new SharedTypeExpr(); paramTypeExpr->Position = paramDecl->Position; paramTypeExpr->base.type = paramType; - paramTypeExpr->Type = new TypeType(paramType); + paramTypeExpr->Type = QualType(getTypeType(paramType)); paramConstraint->sub = TypeExp(paramTypeExpr); paramConstraint->sup = parser->ParseTypeExp(); @@ -2518,6 +2527,7 @@ namespace Slang static bool isGenericName(Parser* parser, String const& name) { auto lookupResult = LookUp( + parser->getSession(), nullptr, // no semantics visitor available yet name, parser->currentScope); @@ -2539,6 +2549,7 @@ namespace Slang static bool isTypeName(Parser* parser, String const& name) { auto lookupResult = LookUp( + parser->getSession(), nullptr, // no semantics visitor available yet name, parser->currentScope); @@ -3389,24 +3400,24 @@ namespace Slang if(unknownCount) { parser->sink->diagnose(token, Diagnostics::invalidIntegerLiteralSuffix, suffix); - suffixType = ExpressionType::GetError(); + suffixType = parser->getSession()->getErrorType(); } // `u` or `ul` suffix -> `uint` else if(uCount == 1 && (lCount <= 1)) { - suffixType = ExpressionType::GetUInt(); + suffixType = parser->getSession()->getUIntType(); } // `l` suffix on integer -> `int` (== `long`) else if(lCount == 1 && !uCount) { - suffixType = ExpressionType::GetInt(); + suffixType = parser->getSession()->getIntType(); } // TODO: probably need `ll` and `ull` // TODO: are there other suffixes we need to handle? else { parser->sink->diagnose(token, Diagnostics::invalidIntegerLiteralSuffix, suffix); - suffixType = ExpressionType::GetError(); + suffixType = parser->getSession()->getErrorType(); } } @@ -3459,23 +3470,23 @@ namespace Slang if(unknownCount) { parser->sink->diagnose(token, Diagnostics::invalidFloatingPOintLiteralSuffix, suffix); - suffixType = ExpressionType::GetError(); + suffixType = parser->getSession()->getErrorType(); } // `f` suffix -> `float` if(fCount == 1 && !lCount) { - suffixType = ExpressionType::GetFloat(); + suffixType = parser->getSession()->getFloatType(); } // `l` or `lf` suffix on float -> `double` else if(lCount == 1 && (fCount <= 1)) { - suffixType = ExpressionType::getDoubleType(); + suffixType = parser->getSession()->getDoubleType(); } // TODO: are there other suffixes we need to handle? else { parser->sink->diagnose(token, Diagnostics::invalidFloatingPOintLiteralSuffix, suffix); - suffixType = ExpressionType::GetError(); + suffixType = parser->getSession()->getErrorType(); } } diff --git a/source/slang/slang-stdlib.cpp b/source/slang/slang-stdlib.cpp index 2a0d2abb2..71e8a6621 100644 --- a/source/slang/slang-stdlib.cpp +++ b/source/slang/slang-stdlib.cpp @@ -1,6 +1,6 @@ // slang-stdlib.cpp -#include "slang-stdlib.h" +#include "compiler.h" #include "syntax.h" #define STRINGIZE(x) STRINGIZE2(x) @@ -1042,9 +1042,7 @@ typedef Texture2D texture2D; namespace Slang { - static String stdlibPath; - - String getStdlibPath() + String Session::getStdlibPath() { if(stdlibPath.Length() != 0) return stdlibPath; @@ -1068,12 +1066,6 @@ namespace Slang return stdlibPath; } - // Cached code for the various libraries - String coreLibraryCode; - String slangLibraryCode; - String hlslLibraryCode; - String glslLibraryCode; - enum { SINT_MASK = 1 << 0, @@ -1156,7 +1148,7 @@ namespace Slang }; - String getCoreLibraryCode() + String Session::getCoreLibraryCode() { if (coreLibraryCode.Length() > 0) return coreLibraryCode; @@ -1974,7 +1966,7 @@ namespace Slang return coreLibraryCode; } - String getHLSLLibraryCode() + String Session::getHLSLLibraryCode() { if (hlslLibraryCode.Length() > 0) return hlslLibraryCode; @@ -2073,7 +2065,7 @@ namespace Slang // GLSL-specific library code - String getGLSLLibraryCode() + String Session::getGLSLLibraryCode() { if(glslLibraryCode.Length() != 0) return glslLibraryCode; @@ -2283,18 +2275,4 @@ namespace Slang glslLibraryCode = sb.ProduceString(); return glslLibraryCode; } - - - - // - - void finalizeShaderLibrary() - { - stdlibPath = String(); - - coreLibraryCode = String(); - hlslLibraryCode = String(); - glslLibraryCode = String(); - } - } diff --git a/source/slang/slang-stdlib.h b/source/slang/slang-stdlib.h deleted file mode 100644 index d21b1e7f1..000000000 --- a/source/slang/slang-stdlib.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef SHADER_COMPILER_STD_LIB_H -#define SHADER_COMPILER_STD_LIB_H - -#include "../core/basic.h" - -namespace Slang -{ - String getCoreLibraryCode(); - String getHLSLLibraryCode(); - String getGLSLLibraryCode(); - - void finalizeShaderLibrary(); -} - -#endif
\ No newline at end of file diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index bb83eb8af..03bb6a09b 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1,7 +1,6 @@ #include "../../slang.h" #include "../core/slang-io.h" -#include "../slang/slang-stdlib.h" #include "parameter-binding.h" #include "../slang/parser.h" #include "../slang/preprocessor.h" @@ -19,65 +18,31 @@ namespace Slang { -class Session +Session::Session() { -public: - bool useCache = false; - String cacheDir; + // Initialize representations of some very basic types: + initializeTypes(); - RefPtr<Scope> coreLanguageScope; - RefPtr<Scope> hlslLanguageScope; - RefPtr<Scope> slangLanguageScope; - RefPtr<Scope> glslLanguageScope; - - List<RefPtr<ProgramSyntaxNode>> loadedModuleCode; - - - Session(bool /*pUseCache*/, String /*pCacheDir*/) - { - // Initialize global state - // TODO: move this into the session instead - BasicExpressionType::Init(); - - // Create scopes for various language builtins. - // - // TODO: load these on-demand to avoid parsing - // stdlib code for languages the user won't use. - - coreLanguageScope = new Scope(); - - hlslLanguageScope = new Scope(); - hlslLanguageScope->nextSibling = coreLanguageScope; - - slangLanguageScope = new Scope(); - slangLanguageScope->nextSibling = hlslLanguageScope; - - glslLanguageScope = new Scope(); - glslLanguageScope->nextSibling = coreLanguageScope; - - addBuiltinSource(coreLanguageScope, "core", getCoreLibraryCode()); - addBuiltinSource(hlslLanguageScope, "hlsl", getHLSLLibraryCode()); - addBuiltinSource(glslLanguageScope, "glsl", getGLSLLibraryCode()); - } + // Create scopes for various language builtins. + // + // TODO: load these on-demand to avoid parsing + // stdlib code for languages the user won't use. - ~Session() - { - // We need to clean up the strings for the standard library - // code that we might have allocated and loaded into static - // variables (TODO: don't use `static` variables for this stuff) + coreLanguageScope = new Scope(); - finalizeShaderLibrary(); + hlslLanguageScope = new Scope(); + hlslLanguageScope->nextSibling = coreLanguageScope; - // Ditto for our type represnetation stuff + slangLanguageScope = new Scope(); + slangLanguageScope->nextSibling = hlslLanguageScope; - ExpressionType::Finalize(); - } + glslLanguageScope = new Scope(); + glslLanguageScope->nextSibling = coreLanguageScope; - void addBuiltinSource( - RefPtr<Scope> const& scope, - String const& path, - String const& source); -}; + addBuiltinSource(coreLanguageScope, "core", getCoreLibraryCode()); + addBuiltinSource(hlslLanguageScope, "hlsl", getHLSLLibraryCode()); + addBuiltinSource(glslLanguageScope, "glsl", getGLSLLibraryCode()); +} struct IncludeHandlerImpl : IncludeHandler { @@ -614,9 +579,9 @@ void Session::addBuiltinSource( #define SESSION(x) reinterpret_cast<Slang::Session *>(x) #define REQ(x) reinterpret_cast<Slang::CompileRequest*>(x) -SLANG_API SlangSession* spCreateSession(const char * cacheDir) +SLANG_API SlangSession* spCreateSession(const char*) { - return reinterpret_cast<SlangSession *>(new Slang::Session((cacheDir ? true : false), cacheDir)); + return reinterpret_cast<SlangSession *>(new Slang::Session()); } SLANG_API void spDestroySession( diff --git a/source/slang/slang.vcxproj b/source/slang/slang.vcxproj index f67e7f5bb..4a1e75bc3 100644 --- a/source/slang/slang.vcxproj +++ b/source/slang/slang.vcxproj @@ -184,7 +184,6 @@ <ClInclude Include="profile-defs.h" /> <ClInclude Include="profile.h" /> <ClInclude Include="reflection.h" /> - <ClInclude Include="slang-stdlib.h" /> <ClInclude Include="source-loc.h" /> <ClInclude Include="stmt-defs.h" /> <ClInclude Include="syntax-base-defs.h" /> diff --git a/source/slang/slang.vcxproj.filters b/source/slang/slang.vcxproj.filters index a276ae95b..c43cd9dbb 100644 --- a/source/slang/slang.vcxproj.filters +++ b/source/slang/slang.vcxproj.filters @@ -17,7 +17,6 @@ <ClInclude Include="profile.h" /> <ClInclude Include="profile-defs.h" /> <ClInclude Include="reflection.h" /> - <ClInclude Include="slang-stdlib.h" /> <ClInclude Include="source-loc.h" /> <ClInclude Include="syntax.h" /> <ClInclude Include="syntax-visitors.h" /> diff --git a/source/slang/syntax-base-defs.h b/source/slang/syntax-base-defs.h index 03d4b749a..eda554988 100644 --- a/source/slang/syntax-base-defs.h +++ b/source/slang/syntax-base-defs.h @@ -62,29 +62,9 @@ ABSTRACT_SYNTAX_CLASS(ExpressionType, Val) RAW( public: - static RefPtr<ExpressionType> Error; - static RefPtr<ExpressionType> initializerListType; - static RefPtr<ExpressionType> Overloaded; + Session* getSession() { return this->session; } + void setSession(Session* s) { this->session = s; } - static Dictionary<int, RefPtr<ExpressionType>> sBuiltinTypes; - static Dictionary<String, Decl*> sMagicDecls; - - // Note: just exists to make sure we can clean up - // canonical types we create along the way - static List<RefPtr<ExpressionType>> sCanonicalTypes; - - - - static ExpressionType* GetBool(); - static ExpressionType* GetFloat(); - static ExpressionType* getDoubleType(); - static ExpressionType* GetInt(); - static ExpressionType* GetUInt(); - static ExpressionType* GetVoid(); - static ExpressionType* getInitializerListType(); - static ExpressionType* GetError(); - -public: virtual String ToString() = 0; bool Equals(ExpressionType * type); @@ -115,8 +95,6 @@ public: bool IsSampler() { return As<SamplerStateType>() != nullptr; } bool IsStruct(); bool IsClass(); - static void Init(); - static void Finalize(); ExpressionType* GetCanonicalType(); virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff) override; @@ -127,6 +105,8 @@ protected: virtual ExpressionType* CreateCanonicalType() = 0; ExpressionType* canonicalType = nullptr; + + Session* session = nullptr; ) END_SYNTAX_CLASS() diff --git a/source/slang/syntax.cpp b/source/slang/syntax.cpp index f3c940038..e9eeeaadc 100644 --- a/source/slang/syntax.cpp +++ b/source/slang/syntax.cpp @@ -1,5 +1,6 @@ #include "syntax.h" +#include "compiler.h" #include "visitor.h" #include <typeinfo> @@ -165,38 +166,69 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) return true; } -#if 0 - RefPtr<ExpressionType> ExpressionType::Bool; - RefPtr<ExpressionType> ExpressionType::UInt; - RefPtr<ExpressionType> ExpressionType::Int; - RefPtr<ExpressionType> ExpressionType::Float; - RefPtr<ExpressionType> ExpressionType::Float2; - RefPtr<ExpressionType> ExpressionType::Void; -#endif - RefPtr<ExpressionType> ExpressionType::Error; - RefPtr<ExpressionType> ExpressionType::initializerListType; - RefPtr<ExpressionType> ExpressionType::Overloaded; + void Session::initializeTypes() + { + errorType = new ErrorType(); + errorType->setSession(this); + + initializerListType = new InitializerListType(); + initializerListType->setSession(this); - Dictionary<int, RefPtr<ExpressionType>> ExpressionType::sBuiltinTypes; - Dictionary<String, Decl*> ExpressionType::sMagicDecls; - List<RefPtr<ExpressionType>> ExpressionType::sCanonicalTypes; + overloadedType = new OverloadGroupType(); + overloadedType->setSession(this); + } - void ExpressionType::Init() + ExpressionType* Session::getBoolType() { - Error = new ErrorType(); - initializerListType = new InitializerListType(); - Overloaded = new OverloadGroupType(); + return getBuiltinType(BaseType::Bool); + } + + ExpressionType* Session::getFloatType() + { + return getBuiltinType(BaseType::Float); + } + + ExpressionType* Session::getDoubleType() + { + return getBuiltinType(BaseType::Double); } - void ExpressionType::Finalize() + + ExpressionType* Session::getIntType() + { + return getBuiltinType(BaseType::Int); + } + + ExpressionType* Session::getUIntType() + { + return getBuiltinType(BaseType::UInt); + } + + ExpressionType* Session::getVoidType() + { + return getBuiltinType(BaseType::Void); + } + + ExpressionType* Session::getBuiltinType(BaseType flavor) { - Error = nullptr; - initializerListType = nullptr; - Overloaded = nullptr; - // Note(tfoley): This seems to be just about the only way to clear out a List<T> - sCanonicalTypes = List<RefPtr<ExpressionType>>(); - sBuiltinTypes = Dictionary<int, RefPtr<ExpressionType>>(); - sMagicDecls = Dictionary<String, Decl*>(); + return RefPtr<ExpressionType>(builtinTypes[(int)flavor]); } + + ExpressionType* Session::getInitializerListType() + { + return initializerListType; + } + + ExpressionType* Session::getOverloadedType() + { + return overloadedType; + } + + ExpressionType* Session::getErrorType() + { + return errorType; + } + + bool ArrayExpressionType::EqualsImpl(ExpressionType * type) { auto arrType = type->AsArrayType(); @@ -206,11 +238,11 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) } ExpressionType* ArrayExpressionType::CreateCanonicalType() { - auto canonicalBaseType = BaseType->GetCanonicalType(); - auto canonicalArrayType = new ArrayExpressionType(); - sCanonicalTypes.Add(canonicalArrayType); - canonicalArrayType->BaseType = canonicalBaseType; - canonicalArrayType->ArrayLength = ArrayLength; + auto canonicalElementType = BaseType->GetCanonicalType(); + auto canonicalArrayType = getArrayType( + canonicalElementType, + ArrayLength); + session->canonicalTypes.Add(canonicalArrayType); return canonicalArrayType; } int ArrayExpressionType::GetHashCode() @@ -308,7 +340,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) *ioDiff += diff; // Re-construct the type in case we are using a specialized sub-class - return DeclRefType::Create(substDeclRef); + return DeclRefType::Create(getSession(), substDeclRef); } static RefPtr<ExpressionType> ExtractGenericArgType(RefPtr<Val> val) @@ -327,11 +359,14 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) // TODO: need to figure out how to unify this with the logic // in the generic case... - DeclRefType* DeclRefType::Create(DeclRef<Decl> declRef) + DeclRefType* DeclRefType::Create( + Session* session, + DeclRef<Decl> declRef) { if (auto builtinMod = declRef.getDecl()->FindModifier<BuiltinTypeModifier>()) { auto type = new BasicExpressionType(builtinMod->tag); + type->setSession(session); type->declRef = declRef; return type; } @@ -342,6 +377,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) if (magicMod->name == "SamplerState") { auto type = new SamplerStateType(); + type->setSession(session); type->declRef = declRef; type->flavor = SamplerStateType::Flavor(magicMod->tag); return type; @@ -350,6 +386,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) { SLANG_ASSERT(subst && subst->args.Count() == 2); auto vecType = new VectorExpressionType(); + vecType->setSession(session); vecType->declRef = declRef; vecType->elementType = ExtractGenericArgType(subst->args[0]); vecType->elementCount = ExtractGenericArgInteger(subst->args[1]); @@ -359,6 +396,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) { SLANG_ASSERT(subst && subst->args.Count() == 3); auto matType = new MatrixExpressionType(); + matType->setSession(session); matType->declRef = declRef; return matType; } @@ -368,6 +406,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) auto textureType = new TextureType( TextureType::Flavor(magicMod->tag), ExtractGenericArgType(subst->args[0])); + textureType->setSession(session); textureType->declRef = declRef; return textureType; } @@ -377,6 +416,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) auto textureType = new TextureSamplerType( TextureType::Flavor(magicMod->tag), ExtractGenericArgType(subst->args[0])); + textureType->setSession(session); textureType->declRef = declRef; return textureType; } @@ -386,6 +426,7 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) auto textureType = new GLSLImageType( TextureType::Flavor(magicMod->tag), ExtractGenericArgType(subst->args[0])); + textureType->setSession(session); textureType->declRef = declRef; return textureType; } @@ -396,7 +437,8 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) #define CASE(n,T) \ else if(magicMod->name == #n) { \ - auto type = new T(); \ + auto type = new T(); \ + type->setSession(session); \ type->declRef = declRef; \ return type; \ } @@ -409,7 +451,8 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) #define CASE(n,T) \ else if(magicMod->name == #n) { \ SLANG_ASSERT(subst && subst->args.Count() == 1); \ - auto type = new T(); \ + auto type = new T(); \ + type->setSession(session); \ type->elementType = ExtractGenericArgType(subst->args[0]); \ type->declRef = declRef; \ return type; \ @@ -435,7 +478,8 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) // "magic" builtin types which have no generic parameters #define CASE(n,T) \ else if(magicMod->name == #n) { \ - auto type = new T(); \ + auto type = new T(); \ + type->setSession(session); \ type->declRef = declRef; \ return type; \ } @@ -455,7 +499,9 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) } else { - return new DeclRefType(declRef); + auto type = new DeclRefType(declRef); + type->setSession(session); + return type; } } @@ -602,8 +648,8 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) ExpressionType* TypeType::CreateCanonicalType() { - auto canType = new TypeType(type->GetCanonicalType()); - sCanonicalTypes.Add(canType); + auto canType = getTypeType(type->GetCanonicalType()); + session->canonicalTypes.Add(canType); return canType; } @@ -953,65 +999,30 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) // - void RegisterBuiltinDecl( + void registerBuiltinDecl( + Session* session, RefPtr<Decl> decl, RefPtr<BuiltinTypeModifier> modifier) { - auto type = DeclRefType::Create(DeclRef<Decl>(decl.Ptr(), nullptr)); - ExpressionType::sBuiltinTypes[(int)modifier->tag] = type; + auto type = DeclRefType::Create( + session, + DeclRef<Decl>(decl.Ptr(), nullptr)); + session->builtinTypes[(int)modifier->tag] = type; } - void RegisterMagicDecl( + void registerMagicDecl( + Session* session, RefPtr<Decl> decl, RefPtr<MagicTypeModifier> modifier) { - ExpressionType::sMagicDecls[modifier->name] = decl.Ptr(); + session->magicDecls[modifier->name] = decl.Ptr(); } RefPtr<Decl> findMagicDecl( - String const& name) - { - return ExpressionType::sMagicDecls[name].GetValue(); - } - - ExpressionType* ExpressionType::GetBool() - { - return sBuiltinTypes[(int)BaseType::Bool].GetValue().Ptr(); - } - - ExpressionType* ExpressionType::GetFloat() - { - return sBuiltinTypes[(int)BaseType::Float].GetValue().Ptr(); - } - - ExpressionType* ExpressionType::getDoubleType() - { - return sBuiltinTypes[(int)BaseType::Double].GetValue().Ptr(); - } - - ExpressionType* ExpressionType::GetInt() - { - return sBuiltinTypes[(int)BaseType::Int].GetValue().Ptr(); - } - - ExpressionType* ExpressionType::GetUInt() - { - return sBuiltinTypes[(int)BaseType::UInt].GetValue().Ptr(); - } - - ExpressionType* ExpressionType::GetVoid() - { - return sBuiltinTypes[(int)BaseType::Void].GetValue().Ptr(); - } - - ExpressionType* ExpressionType::getInitializerListType() - { - return initializerListType.Ptr(); - } - - ExpressionType* ExpressionType::GetError() + Session* session, + String const& name) { - return ExpressionType::Error.Ptr(); + return session->magicDecls[name].GetValue(); } // @@ -1061,4 +1072,74 @@ void ExpressionType::accept(IValVisitor* visitor, void* extra) { return this->declRef.substitutions->args[1].As<IntVal>().Ptr(); } -}
\ No newline at end of file + + // Constructors for types + + RefPtr<ArrayExpressionType> getArrayType( + ExpressionType* elementType, + IntVal* elementCount) + { + auto session = elementType->getSession(); + auto arrayType = new ArrayExpressionType(); + arrayType->setSession(session); + arrayType->BaseType = elementType; + arrayType->ArrayLength = elementCount; + return arrayType; + } + + RefPtr<ArrayExpressionType> getArrayType( + ExpressionType* elementType) + { + auto session = elementType->getSession(); + auto arrayType = new ArrayExpressionType(); + arrayType->setSession(session); + arrayType->BaseType = elementType; + return arrayType; + } + + RefPtr<NamedExpressionType> getNamedType( + Session* session, + DeclRef<TypeDefDecl> const& declRef) + { + auto namedType = new NamedExpressionType(declRef); + namedType->setSession(session); + return namedType; + } + + RefPtr<TypeType> getTypeType( + ExpressionType* type) + { + auto session = type->getSession(); + auto typeType = new TypeType(type); + typeType->setSession(session); + return typeType; + } + + RefPtr<FuncType> getFuncType( + Session* session, + DeclRef<CallableDecl> const& declRef) + { + auto funcType = new FuncType(); + funcType->setSession(session); + funcType->declRef = declRef; + return funcType; + } + + RefPtr<GenericDeclRefType> getGenericDeclRefType( + Session* session, + DeclRef<GenericDecl> const& declRef) + { + auto genericDeclRefType = new GenericDeclRefType(declRef); + genericDeclRefType->setSession(session); + return genericDeclRefType; + } + + RefPtr<SamplerStateType> getSamplerStateType( + Session* session) + { + auto samplerStateType = new SamplerStateType(); + samplerStateType->setSession(session); + return samplerStateType; + } + +} diff --git a/source/slang/syntax.h b/source/slang/syntax.h index 3f1c47fb9..56b5624b5 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -11,6 +11,7 @@ namespace Slang { + class Session; class Substitutions; class SyntaxVisitor; class FunctionSyntaxNode; @@ -1074,16 +1075,19 @@ namespace Slang // // TODO(tfoley): These should really belong to the compilation context! // - void RegisterBuiltinDecl( + void registerBuiltinDecl( + Session* session, RefPtr<Decl> decl, RefPtr<BuiltinTypeModifier> modifier); - void RegisterMagicDecl( + void registerMagicDecl( + Session* session, RefPtr<Decl> decl, RefPtr<MagicTypeModifier> modifier); // Look up a magic declaration by its name RefPtr<Decl> findMagicDecl( - String const& name); + Session* session, + String const& name); // Create an instance of a syntax class by name SyntaxNodeBase* createInstanceOfSyntaxClassByName( @@ -1171,6 +1175,34 @@ namespace Slang return declRef.getDecl()->inner.Ptr(); } + + // + + RefPtr<ArrayExpressionType> getArrayType( + ExpressionType* elementType, + IntVal* elementCount); + + RefPtr<ArrayExpressionType> getArrayType( + ExpressionType* elementType); + + RefPtr<NamedExpressionType> getNamedType( + Session* session, + DeclRef<TypeDefDecl> const& declRef); + + RefPtr<TypeType> getTypeType( + ExpressionType* type); + + RefPtr<FuncType> getFuncType( + Session* session, + DeclRef<CallableDecl> const& declRef); + + RefPtr<GenericDeclRefType> getGenericDeclRefType( + Session* session, + DeclRef<GenericDecl> const& declRef); + + RefPtr<SamplerStateType> getSamplerStateType( + Session* session); + } // namespace Slang #endif
\ No newline at end of file diff --git a/source/slang/type-defs.h b/source/slang/type-defs.h index 138b0a849..c1818eae0 100644 --- a/source/slang/type-defs.h +++ b/source/slang/type-defs.h @@ -49,11 +49,14 @@ RAW( virtual String ToString() override; virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff) override; - static DeclRefType* Create(DeclRef<Decl> declRef); + static DeclRefType* Create( + Session* session, + DeclRef<Decl> declRef); DeclRefType() {} - DeclRefType(DeclRef<Decl> declRef) + DeclRefType( + DeclRef<Decl> declRef) : declRef(declRef) {} protected: @@ -66,6 +69,7 @@ END_SYNTAX_CLASS() // Base class for types that can be used in arithmetic expressions ABSTRACT_SYNTAX_CLASS(ArithmeticExpressionType, DeclRefType) RAW( +public: virtual BasicExpressionType* GetScalarType() = 0; ) END_SYNTAX_CLASS() @@ -75,11 +79,9 @@ SYNTAX_CLASS(BasicExpressionType, ArithmeticExpressionType) FIELD(BaseType, BaseType) RAW( - BasicExpressionType() - { - BaseType = Slang::BaseType::Int; - } - BasicExpressionType(Slang::BaseType baseType) + BasicExpressionType() {} + BasicExpressionType( + Slang::BaseType baseType) { BaseType = baseType; } @@ -211,7 +213,9 @@ SYNTAX_CLASS(SamplerStateType, DeclRefType) { SamplerState, SamplerComparisonState, - };) + }; + + ) FIELD(Flavor, flavor) END_SYNTAX_CLASS() @@ -284,6 +288,7 @@ SYNTAX_CLASS(ArrayExpressionType, ExpressionType) RAW( virtual Slang::String ToString() override; + protected: virtual bool EqualsImpl(ExpressionType * type) override; virtual ExpressionType* CreateCanonicalType() override; @@ -336,6 +341,7 @@ END_SYNTAX_CLASS() // A matrix type, e.g., `matrix<T,R,C>` SYNTAX_CLASS(MatrixExpressionType, ArithmeticExpressionType) RAW( + ExpressionType* getElementType(); IntVal* getRowCount(); IntVal* getColumnCount(); @@ -355,7 +361,8 @@ SYNTAX_CLASS(NamedExpressionType, ExpressionType) RAW( NamedExpressionType() {} - NamedExpressionType(DeclRef<TypeDefDecl> declRef) + NamedExpressionType( + DeclRef<TypeDefDecl> declRef) : declRef(declRef) {} @@ -377,6 +384,9 @@ SYNTAX_CLASS(FuncType, ExpressionType) DECL_FIELD(DeclRef<CallableDecl>, declRef) RAW( + FuncType() + {} + virtual String ToString() override; protected: virtual bool EqualsImpl(ExpressionType * type) override; @@ -393,7 +403,8 @@ SYNTAX_CLASS(GenericDeclRefType, ExpressionType) RAW( GenericDeclRefType() {} - GenericDeclRefType(DeclRef<GenericDecl> declRef) + GenericDeclRefType( + DeclRef<GenericDecl> declRef) : declRef(declRef) {} |
