Skip to content

Commit 2f2ae8c

Browse files
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
1 parent 366c9b4 commit 2f2ae8c

File tree

6 files changed

+689
-33
lines changed

6 files changed

+689
-33
lines changed

include/slang.h

+143-4
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ extern "C"
21212121
typedef struct SlangReflectionTypeParameter SlangReflectionTypeParameter;
21222122
typedef struct SlangReflectionUserAttribute SlangReflectionUserAttribute;
21232123
typedef struct SlangReflectionFunction SlangReflectionFunction;
2124+
typedef struct SlangReflectionGeneric SlangReflectionGeneric;
21242125

21252126
/*
21262127
Type aliases to maintain backward compatibility.
@@ -2469,6 +2470,7 @@ extern "C"
24692470

24702471
SLANG_API char const* spReflectionType_GetName(SlangReflectionType* type);
24712472
SLANG_API SlangResult spReflectionType_GetFullName(SlangReflectionType* type, ISlangBlob** outNameBlob);
2473+
SLANG_API SlangReflectionGeneric* spReflectionType_GetGenericContainer(SlangReflectionType* type);
24722474

24732475
// Type Layout Reflection
24742476

@@ -2553,6 +2555,7 @@ extern "C"
25532555
SLANG_API SlangReflectionUserAttribute* spReflectionVariable_GetUserAttribute(SlangReflectionVariable* var, unsigned int index);
25542556
SLANG_API SlangReflectionUserAttribute* spReflectionVariable_FindUserAttributeByName(SlangReflectionVariable* var, SlangSession * globalSession, char const* name);
25552557
SLANG_API bool spReflectionVariable_HasDefaultValue(SlangReflectionVariable* inVar);
2558+
SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer(SlangReflectionVariable* var);
25562559

25572560
// Variable Layout Reflection
25582561

@@ -2578,6 +2581,7 @@ extern "C"
25782581
SLANG_API unsigned int spReflectionFunction_GetParameterCount(SlangReflectionFunction* func);
25792582
SLANG_API SlangReflectionVariable* spReflectionFunction_GetParameter(SlangReflectionFunction* func, unsigned index);
25802583
SLANG_API SlangReflectionType* spReflectionFunction_GetResultType(SlangReflectionFunction* func);
2584+
SLANG_API SlangReflectionGeneric* spReflectionFunction_GetGenericContainer(SlangReflectionFunction* func);
25812585

25822586
// Abstract Decl Reflection
25832587

@@ -2586,8 +2590,26 @@ extern "C"
25862590
SLANG_API SlangDeclKind spReflectionDecl_getKind(SlangReflectionDecl* decl);
25872591
SLANG_API SlangReflectionFunction* spReflectionDecl_castToFunction(SlangReflectionDecl* decl);
25882592
SLANG_API SlangReflectionVariable* spReflectionDecl_castToVariable(SlangReflectionDecl* decl);
2589-
SLANG_API SlangReflectionType* spReflection_getTypeFromDecl(SlangSession* session, SlangReflectionDecl* decl);
2590-
2593+
SLANG_API SlangReflectionGeneric* spReflectionDecl_castToGeneric(SlangReflectionDecl* decl);
2594+
SLANG_API SlangReflectionType* spReflection_getTypeFromDecl(SlangReflectionDecl* decl);
2595+
SLANG_API SlangReflectionDecl* spReflectionDecl_getParent(SlangReflectionDecl* decl);
2596+
2597+
// Generic Reflection
2598+
2599+
SLANG_API SlangReflectionDecl* spReflectionGeneric_asDecl(SlangReflectionGeneric* generic);
2600+
SLANG_API char const* spReflectionGeneric_GetName(SlangReflectionGeneric* generic);
2601+
SLANG_API unsigned int spReflectionGeneric_GetTypeParameterCount(SlangReflectionGeneric* generic);
2602+
SLANG_API SlangReflectionVariable* spReflectionGeneric_GetTypeParameter(SlangReflectionGeneric* generic, unsigned index);
2603+
SLANG_API unsigned int spReflectionGeneric_GetValueParameterCount(SlangReflectionGeneric* generic);
2604+
SLANG_API SlangReflectionVariable* spReflectionGeneric_GetValueParameter(SlangReflectionGeneric* generic, unsigned index);
2605+
SLANG_API unsigned int spReflectionGeneric_GetTypeParameterConstraintCount(SlangReflectionGeneric* generic, SlangReflectionVariable* typeParam);
2606+
SLANG_API SlangReflectionType* spReflectionGeneric_GetTypeParameterConstraintType(SlangReflectionGeneric* generic, SlangReflectionVariable* typeParam, unsigned index);
2607+
SLANG_API SlangDeclKind spReflectionGeneric_GetInnerKind(SlangReflectionGeneric* generic);
2608+
SLANG_API SlangReflectionDecl* spReflectionGeneric_GetInnerDecl(SlangReflectionGeneric* generic);
2609+
SLANG_API SlangReflectionGeneric* spReflectionGeneric_GetOuterGenericContainer(SlangReflectionGeneric* generic);
2610+
SLANG_API SlangReflectionType* spReflectionGeneric_GetConcreteType(SlangReflectionGeneric* generic, SlangReflectionVariable* typeParam);
2611+
SLANG_API int64_t spReflectionGeneric_GetConcreteIntVal(SlangReflectionGeneric* generic, SlangReflectionVariable* valueParam);
2612+
25912613

25922614
/** Get the stage that a variable belongs to (if any).
25932615
@@ -2678,6 +2700,8 @@ extern "C"
26782700
SLANG_API SlangReflectionTypeLayout* spReflection_GetTypeLayout(SlangReflection* reflection, SlangReflectionType* reflectionType, SlangLayoutRules rules);
26792701

26802702
SLANG_API SlangReflectionFunction* spReflection_FindFunctionByName(SlangReflection* reflection, char const* name);
2703+
SLANG_API SlangReflectionFunction* spReflection_FindFunctionByNameInType(SlangReflection* reflection, SlangReflectionType* reflType, char const* name);
2704+
SLANG_API SlangReflectionVariable* spReflection_FindVarByNameInType(SlangReflection* reflection, SlangReflectionType* reflType, char const* name);
26812705

26822706
SLANG_API SlangUInt spReflection_getEntryPointCount(SlangReflection* reflection);
26832707
SLANG_API SlangReflectionEntryPoint* spReflection_getEntryPointByIndex(SlangReflection* reflection, SlangUInt index);
@@ -2735,6 +2759,8 @@ namespace slang
27352759
struct TypeReflection;
27362760
struct VariableLayoutReflection;
27372761
struct VariableReflection;
2762+
struct FunctionReflection;
2763+
struct GenericReflection;
27382764

27392765
struct UserAttribute
27402766
{
@@ -2913,6 +2939,11 @@ namespace slang
29132939
{
29142940
return (UserAttribute*)spReflectionType_FindUserAttributeByName((SlangReflectionType*)this, name);
29152941
}
2942+
2943+
SlangReflectionGeneric* getGenericContainer()
2944+
{
2945+
return (SlangReflectionGeneric*) spReflectionType_GetGenericContainer((SlangReflectionType*) this);
2946+
}
29162947
};
29172948

29182949
enum ParameterCategory : SlangParameterCategoryIntegral
@@ -3360,10 +3391,12 @@ namespace slang
33603391
{
33613392
return spReflectionVariable_GetUserAttributeCount((SlangReflectionVariable*)this);
33623393
}
3394+
33633395
UserAttribute* getUserAttributeByIndex(unsigned int index)
33643396
{
33653397
return (UserAttribute*)spReflectionVariable_GetUserAttribute((SlangReflectionVariable*)this, index);
33663398
}
3399+
33673400
UserAttribute* findUserAttributeByName(SlangSession* globalSession, char const* name)
33683401
{
33693402
return (UserAttribute*)spReflectionVariable_FindUserAttributeByName((SlangReflectionVariable*)this, globalSession, name);
@@ -3373,6 +3406,11 @@ namespace slang
33733406
{
33743407
return spReflectionVariable_HasDefaultValue((SlangReflectionVariable*)this);
33753408
}
3409+
3410+
GenericReflection* getGenericContainer()
3411+
{
3412+
return (GenericReflection*)spReflectionVariable_GetGenericContainer((SlangReflectionVariable*)this);
3413+
}
33763414
};
33773415

33783416
struct VariableLayoutReflection
@@ -3498,6 +3536,81 @@ namespace slang
34983536
{
34993537
return (Modifier*)spReflectionFunction_FindModifier((SlangReflectionFunction*)this, (SlangModifierID)id);
35003538
}
3539+
3540+
GenericReflection* getGenericContainer()
3541+
{
3542+
return (GenericReflection*)spReflectionFunction_GetGenericContainer((SlangReflectionFunction*)this);
3543+
}
3544+
};
3545+
3546+
struct GenericReflection
3547+
{
3548+
3549+
DeclReflection* asDecl()
3550+
{
3551+
return (DeclReflection*)spReflectionGeneric_asDecl((SlangReflectionGeneric*)this);
3552+
}
3553+
3554+
char const* getName()
3555+
{
3556+
return spReflectionGeneric_GetName((SlangReflectionGeneric*)this);
3557+
}
3558+
3559+
unsigned int getTypeParameterCount()
3560+
{
3561+
return spReflectionGeneric_GetTypeParameterCount((SlangReflectionGeneric*)this);
3562+
}
3563+
3564+
VariableReflection* getTypeParameter(unsigned index)
3565+
{
3566+
return (VariableReflection*)spReflectionGeneric_GetTypeParameter((SlangReflectionGeneric*)this, index);
3567+
}
3568+
3569+
unsigned int getValueParameterCount()
3570+
{
3571+
return spReflectionGeneric_GetValueParameterCount((SlangReflectionGeneric*)this);
3572+
}
3573+
3574+
VariableReflection* getValueParameter(unsigned index)
3575+
{
3576+
return (VariableReflection*)spReflectionGeneric_GetValueParameter((SlangReflectionGeneric*)this, index);
3577+
}
3578+
3579+
unsigned int getTypeParameterConstraintCount(VariableReflection* typeParam)
3580+
{
3581+
return spReflectionGeneric_GetTypeParameterConstraintCount((SlangReflectionGeneric*)this, (SlangReflectionVariable*)typeParam);
3582+
}
3583+
3584+
TypeReflection* getTypeParameterConstraintType(VariableReflection* typeParam, unsigned index)
3585+
{
3586+
return (TypeReflection*)spReflectionGeneric_GetTypeParameterConstraintType((SlangReflectionGeneric*)this, (SlangReflectionVariable*)typeParam, index);
3587+
}
3588+
3589+
DeclReflection* getInnerDecl()
3590+
{
3591+
return (DeclReflection*)spReflectionGeneric_GetInnerDecl((SlangReflectionGeneric*)this);
3592+
}
3593+
3594+
SlangDeclKind getInnerKind()
3595+
{
3596+
return spReflectionGeneric_GetInnerKind((SlangReflectionGeneric*)this);
3597+
}
3598+
3599+
GenericReflection* getOuterGenericContainer()
3600+
{
3601+
return (GenericReflection*)spReflectionGeneric_GetOuterGenericContainer((SlangReflectionGeneric*)this);
3602+
}
3603+
3604+
TypeReflection* getConcreteType(VariableReflection* typeParam)
3605+
{
3606+
return (TypeReflection*)spReflectionGeneric_GetConcreteType((SlangReflectionGeneric*)this, (SlangReflectionVariable*)typeParam);
3607+
}
3608+
3609+
int64_t getConcreteIntVal(VariableReflection* valueParam)
3610+
{
3611+
return spReflectionGeneric_GetConcreteIntVal((SlangReflectionGeneric*)this, (SlangReflectionVariable*)valueParam);
3612+
}
3613+
35013614
};
35023615

35033616
struct EntryPointReflection
@@ -3672,6 +3785,22 @@ namespace slang
36723785
name);
36733786
}
36743787

3788+
FunctionReflection* findFunctionByNameInType(TypeReflection* type, const char* name)
3789+
{
3790+
return (FunctionReflection*)spReflection_FindFunctionByNameInType(
3791+
(SlangReflection*) this,
3792+
(SlangReflectionType*) type,
3793+
name);
3794+
}
3795+
3796+
VariableReflection* findVarByNameInType(TypeReflection* type, const char* name)
3797+
{
3798+
return (VariableReflection*)spReflection_FindVarByNameInType(
3799+
(SlangReflection*) this,
3800+
(SlangReflectionType*) type,
3801+
name);
3802+
}
3803+
36753804
TypeLayoutReflection* getTypeLayout(
36763805
TypeReflection* type,
36773806
LayoutRules rules = LayoutRules::Default)
@@ -3749,9 +3878,9 @@ namespace slang
37493878
return (DeclReflection*)spReflectionDecl_getChild((SlangReflectionDecl*)this, index);
37503879
}
37513880

3752-
TypeReflection* getType(SlangSession* session)
3881+
TypeReflection* getType()
37533882
{
3754-
return (TypeReflection*)spReflection_getTypeFromDecl(session, (SlangReflectionDecl*)this);
3883+
return (TypeReflection*)spReflection_getTypeFromDecl((SlangReflectionDecl*)this);
37553884
}
37563885

37573886
VariableReflection* asVariable()
@@ -3764,6 +3893,16 @@ namespace slang
37643893
return (FunctionReflection*)spReflectionDecl_castToFunction((SlangReflectionDecl*)this);
37653894
}
37663895

3896+
GenericReflection* asGeneric()
3897+
{
3898+
return (GenericReflection*)spReflectionDecl_castToGeneric((SlangReflectionDecl*)this);
3899+
}
3900+
3901+
DeclReflection* getParent()
3902+
{
3903+
return (DeclReflection*)spReflectionDecl_getParent((SlangReflectionDecl*)this);
3904+
}
3905+
37673906
template <Kind K>
37683907
struct FilteredList
37693908
{

source/slang/slang-compiler.h

+6
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ namespace Slang
420420
DeclRef<Decl> findDeclFromString(
421421
String const& name,
422422
DiagnosticSink* sink);
423+
424+
DeclRef<Decl> findDeclFromStringInType(
425+
Type* type,
426+
String const& name,
427+
LookupMask mask,
428+
DiagnosticSink* sink);
423429

424430
Dictionary<String, IntVal*>& getMangledNameToIntValMap();
425431
ConstantIntVal* tryFoldIntVal(IntVal* intVal);

source/slang/slang-parser.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,7 @@ namespace Slang
22032203
case TokenType::OpEql:
22042204
case TokenType::OpNeq:
22052205
case TokenType::OpGreater:
2206+
case TokenType::EndOfFile:
22062207
{
22072208
return parseGenericApp(parser, base);
22082209
}

0 commit comments

Comments
 (0)