From 2f2ae8c31490ab01ce0d0cc76d5d7fcf1d21efe7 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Wed, 7 Aug 2024 02:04:37 -0400 Subject: More reflection API features. (#4740) * More reflection API features. + Lookup methods and members (by string) on types + Fix issue with looking up non-static members through the scope operator '::' + `GenericReflection`: Cast a decl to generic to access unspecialized generic parameter names and constraints + `GenericReflection`: Use `getGenericContainer()` from function, variable or type to access the 'nearest' generic parent along with specialization info + `GenericReflection::getConcreteType` and `GenericReflection::getConcreteIntVal`: to get the concrete type of a param in the context of the reflection object + `GenericReflection::getOuterGenericContainer` to go up one level and get the outer generic declarations (if there are more than one enclosing generic scopes) + `DeclReflection::getParent`: go to parent declaration. + Change `VariableReflection` to be a `DeclRef` rather than a decl (allows us to return properly substituted types for methods, members, and more) * Fix Falcor issue --- source/slang/slang.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index cf8c19033..9ba02ee50 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -26,6 +26,8 @@ #include "slang-parser.h" #include "slang-preprocessor.h" #include "slang-type-layout.h" +#include "slang-lookup.h" + # #include "slang-options.h" @@ -2279,7 +2281,10 @@ DeclRef ComponentType::findDeclFromString( linkage, nullptr, sink); - SemanticsVisitor visitor(&sharedSemanticsContext); + SemanticsContext context(&sharedSemanticsContext); + context = context.allowStaticReferenceToNonStaticMember(); + + SemanticsVisitor visitor(context); auto checkedExpr = visitor.CheckExpr(expr); if (auto declRefExpr = as(checkedExpr)) @@ -2291,6 +2296,86 @@ DeclRef ComponentType::findDeclFromString( return result; } +DeclRef ComponentType::findDeclFromStringInType( + Type* type, + String const& name, + LookupMask mask, + DiagnosticSink* sink) +{ + DeclRef result; + + // Only look up in the type if it is a DeclRefType + if (!as(type)) + return DeclRef(); + + // 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); + SemanticsContext context(&sharedSemanticsContext); + context = context.allowStaticReferenceToNonStaticMember(); + + SemanticsVisitor visitor(context); + + GenericAppExpr* genericOuterExpr = nullptr; + if (as(expr)) + { + // Unwrap the generic application, and re-wrap it around the static-member expr + genericOuterExpr = as(expr); + expr = genericOuterExpr->functionExpr; + } + + if (!as(expr)) + return result; + + auto rs = astBuilder->create(); + auto typeExpr = astBuilder->create(); + auto typetype = astBuilder->getOrCreate(type); + typeExpr->type = typetype; + rs->baseExpression = typeExpr; + rs->name = as(expr)->name; + + expr = rs; + + // If we have a generic-app expression, re-wrap the static-member expr + if (genericOuterExpr) + { + genericOuterExpr->functionExpr = expr; + expr = genericOuterExpr; + } + + auto checkedTerm = visitor.CheckTerm(expr); + auto resolvedTerm = visitor.maybeResolveOverloadedExpr(checkedTerm, mask, sink); + + if (auto declRefExpr = as(resolvedTerm)) + { + result = declRefExpr->declRef; + } + + return result; +} + static void collectExportedConstantInContainer( Dictionary& dict, ASTBuilder* builder, -- cgit v1.2.3