From b89421cb3b803165455020f5b70d582b6aec6e76 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 10 Jul 2024 14:09:18 -0700 Subject: Add reflection API for functions. (#4587) * Add reflection API for functions. This change adds `SlangFunctionReflection` type in the reflection API that provides methods for querying function result type, parameters and user-defined attributes. `ProgramLayout::findFunctionByName` can now find a function with the given name and returns a `FunctionReflection`. `IEntryPoint` now has a `getFunctionReflection` method that returns an `FunctionReflection` for the entrypoint. * More modifiers; make reflection API consistent. --- source/slang/slang.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index adfa031f5..d601dcd0e 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -2229,6 +2229,55 @@ Type* ComponentType::getTypeFromString( return type; } +DeclRef ComponentType::findDeclFromString( + String const& name, + DiagnosticSink* sink) +{ + // If we've looked up this type name before, + // then we can re-use it. + // + DeclRef result; + if (m_decls.tryGetValue(name, result)) + return result; + + + // TODO(JS): For now just used the linkages ASTBuilder to keep on scope + // + // The parseTermString uses the linkage ASTBuilder for it's parsing. + // + // It might be possible to just create a temporary ASTBuilder - the worry though is + // that the parsing sets a member variable in AST node to one of these scopes, and then + // it become a dangling pointer. So for now we go with the linkages. + auto astBuilder = getLinkage()->getASTBuilder(); + + // Otherwise, we need to start looking in + // the modules that were directly or + // indirectly referenced. + // + Scope* scope = _getOrCreateScopeForLegacyLookup(astBuilder); + + auto linkage = getLinkage(); + + SLANG_AST_BUILDER_RAII(linkage->getASTBuilder()); + + Expr* expr = linkage->parseTermString(name, scope); + + SharedSemanticsContext sharedSemanticsContext( + linkage, + nullptr, + sink); + SemanticsVisitor visitor(&sharedSemanticsContext); + + auto checkedExpr = visitor.CheckExpr(expr); + if (auto declRefExpr = as(checkedExpr)) + { + result = declRefExpr->declRef; + } + + m_decls[name] = result; + return result; +} + static void collectExportedConstantInContainer( Dictionary& dict, ASTBuilder* builder, @@ -4075,7 +4124,6 @@ RefPtr Module::findEntryPointByName(UnownedStringSlice const& name) return nullptr; } - RefPtr Module::findAndCheckEntryPoint( UnownedStringSlice const& name, SlangStage stage, -- cgit v1.2.3