summaryrefslogtreecommitdiff
path: root/source/slang/slang.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-07-10 14:09:18 -0700
committerGitHub <noreply@github.com>2024-07-10 14:09:18 -0700
commitb89421cb3b803165455020f5b70d582b6aec6e76 (patch)
tree461327ffbb55e7cea0ca73ae11cfa18425c904a8 /source/slang/slang.cpp
parent45ef0ce906c93c16495755fec2e597573e8631c4 (diff)
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.
Diffstat (limited to 'source/slang/slang.cpp')
-rw-r--r--source/slang/slang.cpp50
1 files changed, 49 insertions, 1 deletions
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<Decl> ComponentType::findDeclFromString(
+ String const& name,
+ DiagnosticSink* sink)
+{
+ // If we've looked up this type name before,
+ // then we can re-use it.
+ //
+ DeclRef<Decl> 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<DeclRefExpr>(checkedExpr))
+ {
+ result = declRefExpr->declRef;
+ }
+
+ m_decls[name] = result;
+ return result;
+}
+
static void collectExportedConstantInContainer(
Dictionary<String, IntVal*>& dict,
ASTBuilder* builder,
@@ -4075,7 +4124,6 @@ RefPtr<EntryPoint> Module::findEntryPointByName(UnownedStringSlice const& name)
return nullptr;
}
-
RefPtr<EntryPoint> Module::findAndCheckEntryPoint(
UnownedStringSlice const& name,
SlangStage stage,