From 9f89c3b2de80c49222c4a6b48e845894c4256a5d Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 3 Jan 2018 12:46:23 -0800 Subject: Add API for querying TypeLayout from a Type Added two API functions: 1. `spReflection_FindTypeByName`, which returns a DeclRefType to the struct type with the given name. The function finds from all loaded modules in a `CompileRequest` for a decl with the given name, construct a `Type` object and cache it in `CompileRequest::types` dictionary. The subsequent calls to `spReflection_FindTypeByName` with the same name will simply returned the cached Type objects. 2. `spReflection_GetTypeLayout`, which returns a `TypeLayout` for a given `Type`. This function creates and caches the `TypeLayout` in the `TargetRequest` object that is used to create the `ProgramLayout`. --- source/slang/reflection.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'source/slang/reflection.cpp') diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp index 5270df8b4..0558ff773 100644 --- a/source/slang/reflection.cpp +++ b/source/slang/reflection.cpp @@ -428,6 +428,47 @@ SLANG_API char const* spReflectionType_GetName(SlangReflectionType* inType) return nullptr; } +SLANG_API SlangReflectionType * spReflection_FindTypeByName(SlangReflection * reflection, char const * name) +{ + auto context = convert(reflection); + auto compileRequest = context->targetRequest->compileRequest; + + RefPtr result; + if (compileRequest->types.TryGetValue(name, result)) + return (SlangReflectionType*)result.Ptr(); + + Decl* resultDecl = nullptr; + for (auto module : compileRequest->loadedModulesList) + { + auto nameObj = compileRequest->getNamePool()->getName(name); + if (module->moduleDecl->memberDictionary.TryGetValue(nameObj, resultDecl)) + break; + } + if (resultDecl) + { + RefPtr declRefType = new DeclRefType(); + declRefType->declRef.decl = resultDecl; + compileRequest->types[name] = declRefType; + return (SlangReflectionType*)declRefType.Ptr(); + } + return nullptr; +} + +SLANG_API SlangReflectionTypeLayout* spReflection_GetTypeLayout( + SlangReflection* reflection, + SlangReflectionType* inType, + SlangLayoutRules /*rules*/) +{ + auto context = convert(reflection); + auto type = convert(inType); + auto layoutContext = getInitialLayoutContextForTarget(context->targetRequest); + RefPtr result; + if (context->targetRequest->typeLayouts.TryGetValue(type, result)) + return (SlangReflectionTypeLayout*)result.Ptr(); + result = CreateTypeLayout(layoutContext, type); + context->targetRequest->typeLayouts[type] = result; + return (SlangReflectionTypeLayout*)result.Ptr(); +} SLANG_API SlangReflectionType* spReflectionType_GetResourceResultType(SlangReflectionType* inType) { -- cgit v1.2.3 From 771da268461e9ad3addf94a452a4c13ee291918e Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 3 Jan 2018 12:53:16 -0800 Subject: spReflection_FindTypeByName: add lookup in translationUnits. --- source/slang/reflection.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'source/slang/reflection.cpp') diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp index 0558ff773..59b50891c 100644 --- a/source/slang/reflection.cpp +++ b/source/slang/reflection.cpp @@ -438,12 +438,17 @@ SLANG_API SlangReflectionType * spReflection_FindTypeByName(SlangReflection * re return (SlangReflectionType*)result.Ptr(); Decl* resultDecl = nullptr; + auto nameObj = compileRequest->getNamePool()->getName(name); for (auto module : compileRequest->loadedModulesList) { - auto nameObj = compileRequest->getNamePool()->getName(name); if (module->moduleDecl->memberDictionary.TryGetValue(nameObj, resultDecl)) break; } + for (auto transUnit : compileRequest->translationUnits) + { + if (transUnit->SyntaxNode->memberDictionary.TryGetValue(nameObj, resultDecl)) + break; + } if (resultDecl) { RefPtr declRefType = new DeclRefType(); -- cgit v1.2.3 From 550405d2de2ca617046e73fe5ec7e5e1765a5c97 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 3 Jan 2018 17:15:03 -0800 Subject: Fix type lookup of global type arguments Global type argument lookup should be done in both loaded modules and current trnaslation units. This is the same as the logic of spReflection_FindTypeByName, so it is extracted into `CompileRequest::lookupGlobalDecl(Name*)` method and reused in places. --- source/slang/check.cpp | 3 ++- source/slang/compiler.h | 2 ++ source/slang/reflection.cpp | 12 +----------- source/slang/slang.cpp | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 12 deletions(-) (limited to 'source/slang/reflection.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index dc5efbd73..46ed9da15 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -6777,7 +6777,8 @@ namespace Slang // Lookup generic parameter types in global scope for (auto name : entryPoint->genericParameterTypeNames) { - if (!translationUnitSyntax->memberDictionary.TryGetValue(name, firstDeclWithName)) + 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. diff --git a/source/slang/compiler.h b/source/slang/compiler.h index 2302c77c0..1fca4751c 100644 --- a/source/slang/compiler.h +++ b/source/slang/compiler.h @@ -373,6 +373,8 @@ namespace Slang Name* name, SourceLoc const& loc); + Decl* lookupGlobalDecl(Name* name); + SourceManager* getSourceManager() { return sourceManager; diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp index 59b50891c..c9de75d6e 100644 --- a/source/slang/reflection.cpp +++ b/source/slang/reflection.cpp @@ -437,18 +437,8 @@ SLANG_API SlangReflectionType * spReflection_FindTypeByName(SlangReflection * re if (compileRequest->types.TryGetValue(name, result)) return (SlangReflectionType*)result.Ptr(); - Decl* resultDecl = nullptr; auto nameObj = compileRequest->getNamePool()->getName(name); - for (auto module : compileRequest->loadedModulesList) - { - if (module->moduleDecl->memberDictionary.TryGetValue(nameObj, resultDecl)) - break; - } - for (auto transUnit : compileRequest->translationUnits) - { - if (transUnit->SyntaxNode->memberDictionary.TryGetValue(nameObj, resultDecl)) - break; - } + Decl* resultDecl = compileRequest->lookupGlobalDecl(nameObj); if (resultDecl) { RefPtr declRefType = new DeclRefType(); diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 3156e5008..0b3e0ceb7 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -590,6 +590,22 @@ RefPtr CompileRequest::findOrImportModule( loc); } +Decl * CompileRequest::lookupGlobalDecl(Name * name) +{ + Decl* resultDecl = nullptr; + for (auto module : loadedModulesList) + { + if (module->moduleDecl->memberDictionary.TryGetValue(name, resultDecl)) + break; + } + for (auto transUnit : translationUnits) + { + if (transUnit->SyntaxNode->memberDictionary.TryGetValue(name, resultDecl)) + break; + } + return resultDecl; +} + RefPtr findOrImportModule( CompileRequest* request, Name* name, -- cgit v1.2.3