summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-15 16:35:10 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-15 16:35:10 -0700
commite0389f5a1f32cb611e5a595a5974ee1d5c15f43d (patch)
tree7bee1ed3a1f235ac97de9f8d9893f2994015c5c3 /source
parent04d43cd71f081f1b8d2f0fd803a47cb6342e4fcd (diff)
Replace `DeclRef` approach
For context: a `DeclRef` is supposed to capture both a pointer to a particualr declaration, and also any information needed to specialize that declaration for a context (e.g., generic parameter substitutions). The existing approach had a hiearchy of specialized decl-ref types that mirrored the AST hierarchy, but that led to a lot of boilerplate where you had to recapitulate the exact same hierarchy. The new appraoch basically treats `DeclRef<T>` as a sort of "smart pointer" in that it wraps a pointer to a `T` (the declaration), plus a side field for the specialization info, and then allows it to be cast as needed to other types (where the pointer cast would be allowed), while carrying along the side info. To enable this, all the things that used to be member functions of declaration-reference types are now free functions that take a `DeclRef<T>` for some specific `T` as a parameter.
Diffstat (limited to 'source')
-rw-r--r--source/slang/check.cpp306
-rw-r--r--source/slang/emit.cpp40
-rw-r--r--source/slang/lookup.cpp31
-rw-r--r--source/slang/lookup.h7
-rw-r--r--source/slang/parameter-binding.cpp18
-rw-r--r--source/slang/parser.cpp6
-rw-r--r--source/slang/reflection.cpp14
-rw-r--r--source/slang/syntax.cpp55
-rw-r--r--source/slang/syntax.h338
-rw-r--r--source/slang/type-layout.cpp8
-rw-r--r--source/slang/type-layout.h4
11 files changed, 389 insertions, 438 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp
index 8fcd905c8..7d101d070 100644
--- a/source/slang/check.cpp
+++ b/source/slang/check.cpp
@@ -110,9 +110,9 @@ namespace Slang
}
RefPtr<ExpressionSyntaxNode> ConstructDeclRefExpr(
- DeclRef declRef,
- RefPtr<ExpressionSyntaxNode> baseExpr,
- RefPtr<ExpressionSyntaxNode> originalExpr)
+ DeclRef<Decl> declRef,
+ RefPtr<ExpressionSyntaxNode> baseExpr,
+ RefPtr<ExpressionSyntaxNode> originalExpr)
{
if (baseExpr)
{
@@ -302,11 +302,11 @@ namespace Slang
// The arguments should already be checked against
// the declaration.
RefPtr<ExpressionType> InstantiateGenericType(
- GenericDeclRef genericDeclRef,
+ DeclRef<GenericDecl> genericDeclRef,
List<RefPtr<ExpressionSyntaxNode>> const& args)
{
RefPtr<Substitutions> subst = new Substitutions();
- subst->genericDecl = genericDeclRef.GetDecl();
+ subst->genericDecl = genericDeclRef.getDecl();
subst->outer = genericDeclRef.substitutions;
for (auto argExpr : args)
@@ -314,8 +314,8 @@ namespace Slang
subst->args.Add(ExtractGenericArgVal(argExpr));
}
- DeclRef innerDeclRef;
- innerDeclRef.decl = genericDeclRef.GetInner();
+ DeclRef<Decl> innerDeclRef;
+ innerDeclRef.decl = GetInner(genericDeclRef);
innerDeclRef.substitutions = subst;
return DeclRefType::Create(innerDeclRef);
@@ -391,7 +391,7 @@ namespace Slang
auto genericDeclRef = genericDeclRefType->GetDeclRef();
EnsureDecl(genericDeclRef.decl);
List<RefPtr<ExpressionSyntaxNode>> args;
- for (RefPtr<Decl> member : genericDeclRef.GetDecl()->Members)
+ for (RefPtr<Decl> member : genericDeclRef.getDecl()->Members)
{
if (auto typeParam = member.As<GenericTypeParamDecl>())
{
@@ -685,7 +685,7 @@ namespace Slang
if(auto toDeclRefType = toType->As<DeclRefType>())
{
auto toTypeDeclRef = toDeclRefType->declRef;
- if(auto toStructDeclRef = toTypeDeclRef.As<StructDeclRef>())
+ if(auto toStructDeclRef = toTypeDeclRef.As<StructSyntaxNode>())
{
// Trying to initialize a `struct` type given an initializer list.
// We will go through the fields in order and try to match them
@@ -693,7 +693,7 @@ namespace Slang
int argIndex = 0;
- for(auto fieldDeclRef : toStructDeclRef.GetMembersOfType<FieldDeclRef>())
+ for(auto fieldDeclRef : getMembersOfType<StructField>(toStructDeclRef))
{
if(argIndex >= argCount)
{
@@ -708,7 +708,7 @@ namespace Slang
ConversionCost argCost;
bool argResult = TryCoerceImpl(
- fieldDeclRef.GetType(),
+ GetType(fieldDeclRef),
outToExpr ? &coercedArg : nullptr,
arg->Type,
arg,
@@ -908,7 +908,7 @@ namespace Slang
if (auto toDeclRefType = toType->As<DeclRefType>())
{
auto toTypeDeclRef = toDeclRefType->declRef;
- if (auto interfaceDeclRef = toTypeDeclRef.As<InterfaceDeclRef>())
+ if (auto interfaceDeclRef = toTypeDeclRef.As<InterfaceDecl>())
{
// Trying to convert to an interface type.
//
@@ -1085,7 +1085,7 @@ namespace Slang
if(auto declRefType = base.type->As<DeclRefType>())
{
- if(auto interfaceDeclRef = declRefType->declRef.As<InterfaceDeclRef>())
+ if(auto interfaceDeclRef = declRefType->declRef.As<InterfaceDecl>())
{
return;
}
@@ -1255,7 +1255,7 @@ namespace Slang
}
// HACK(tfoley): Visiting all generic declarations here,
// because otherwise they won't get visited.
- for (auto & g : program->GetMembersOfType<GenericDecl>())
+ for (auto & g : program->getMembersOfType<GenericDecl>())
{
VisitGenericDecl(g.Ptr());
}
@@ -1912,7 +1912,7 @@ namespace Slang
if (!funcDeclRefExpr) return nullptr;
auto funcDeclRef = funcDeclRefExpr->declRef;
- auto intrinsicMod = funcDeclRef.GetDecl()->FindModifier<IntrinsicOpModifier>();
+ auto intrinsicMod = funcDeclRef.getDecl()->FindModifier<IntrinsicOpModifier>();
if (!intrinsicMod) return nullptr;
// Let's not constant-fold operations with more than a certain number of arguments, for simplicity
@@ -2024,7 +2024,7 @@ namespace Slang
{
auto declRef = declRefExpr->declRef;
- if (auto genericValParamRef = declRef.As<GenericValueParamDeclRef>())
+ if (auto genericValParamRef = declRef.As<GenericValueParamDecl>())
{
// TODO(tfoley): handle the case of non-`int` value parameters...
return new GenericParamIntVal(genericValParamRef);
@@ -2032,9 +2032,9 @@ namespace Slang
// We may also need to check for references to variables that
// are defined in a way that can be used as a constant expression:
- if(auto varRef = declRef.As<VarDeclBaseRef>())
+ if(auto varRef = declRef.As<VarDeclBase>())
{
- auto varDecl = varRef.GetDecl();
+ auto varDecl = varRef.getDecl();
switch(sourceLanguage)
{
@@ -2046,7 +2046,7 @@ namespace Slang
if(auto constAttr = varDecl->FindModifier<ConstModifier>())
{
// HLSL `static const` can be used as a constant expression
- if(auto initExpr = varRef.getInitExpr())
+ if(auto initExpr = getInitExpr(varRef))
{
return TryConstantFoldExpr(initExpr.Ptr());
}
@@ -2077,7 +2077,7 @@ namespace Slang
// TODO(tfoley): this is cloned from the case above that handles generic value parameters
return new GenericParamIntVal(varRef);
}
- else if(auto initExpr = varRef.getInitExpr())
+ else if(auto initExpr = getInitExpr(varRef))
{
// This is an ordinary constant, and not a specialization constant, so we
// can try to fold its value right now.
@@ -2180,7 +2180,7 @@ namespace Slang
substitutions->args.Add(elementType);
substitutions->args.Add(elementCount);
- auto declRef = DeclRef(vectorTypeDecl.Ptr(), substitutions);
+ auto declRef = DeclRef<Decl>(vectorTypeDecl.Ptr(), substitutions);
return DeclRefType::Create(declRef)->As<VectorExpressionType>();
}
@@ -2257,10 +2257,10 @@ namespace Slang
if (auto declRefType = baseType->AsDeclRefType())
{
- if (auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDeclRef>())
+ if (auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDecl>())
{
// Checking of the type must be complete before we can reference its members safely
- EnsureDecl(aggTypeDeclRef.GetDecl(), DeclCheckState::Checked);
+ EnsureDecl(aggTypeDeclRef.getDecl(), DeclCheckState::Checked);
// Note(tfoley): The name used for lookup here is a bit magical, since
// it must match what the parser installed in subscript declarations.
@@ -2330,7 +2330,7 @@ namespace Slang
RefPtr<ExpressionType> resultType;
if (auto funcDeclRef = funcType->declRef)
{
- EnsureDecl(funcDeclRef.GetDecl());
+ EnsureDecl(funcDeclRef.getDecl());
params = funcDeclRef->GetParameters().ToArray();
resultType = funcDecl->ReturnType;
@@ -2403,9 +2403,9 @@ namespace Slang
if (auto targetDeclRefType = decl->targetType->As<DeclRefType>())
{
// Attach our extension to that type as a candidate...
- if (auto aggTypeDeclRef = targetDeclRefType->declRef.As<AggTypeDeclRef>())
+ if (auto aggTypeDeclRef = targetDeclRefType->declRef.As<AggTypeDecl>())
{
- auto aggTypeDecl = aggTypeDeclRef.GetDecl();
+ auto aggTypeDecl = aggTypeDeclRef.getDecl();
decl->nextCandidateExtension = aggTypeDecl->candidateExtensions;
aggTypeDecl->candidateExtensions = decl;
}
@@ -2512,18 +2512,18 @@ namespace Slang
bool DoesTypeConformToInterface(
RefPtr<ExpressionType> type,
- InterfaceDeclRef interfaceDeclRef)
+ DeclRef<InterfaceDecl> interfaceDeclRef)
{
// for now look up a conformance member...
if(auto declRefType = type->As<DeclRefType>())
{
- if( auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDeclRef>() )
+ if( auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDecl>() )
{
- for( auto inheritanceDeclRef : aggTypeDeclRef.GetMembersOfType<InheritanceDeclRef>())
+ for( auto inheritanceDeclRef : getMembersOfType<InheritanceDecl>(aggTypeDeclRef))
{
- EnsureDecl(inheritanceDeclRef.GetDecl());
+ EnsureDecl(inheritanceDeclRef.getDecl());
- auto inheritedDeclRefType = inheritanceDeclRef.getBaseType()->As<DeclRefType>();
+ auto inheritedDeclRefType = getBaseType(inheritanceDeclRef)->As<DeclRefType>();
if (!inheritedDeclRefType)
continue;
@@ -2539,7 +2539,7 @@ namespace Slang
RefPtr<ExpressionType> TryJoinTypeWithInterface(
RefPtr<ExpressionType> type,
- InterfaceDeclRef interfaceDeclRef)
+ DeclRef<InterfaceDecl> interfaceDeclRef)
{
// The most basic test here should be: does the type declare conformance to the trait.
if(DoesTypeConformToInterface(type, interfaceDeclRef))
@@ -2625,7 +2625,7 @@ namespace Slang
// HACK: trying to work trait types in here...
if(auto leftDeclRefType = left->As<DeclRefType>())
{
- if( auto leftInterfaceRef = leftDeclRefType->declRef.As<InterfaceDeclRef>() )
+ if( auto leftInterfaceRef = leftDeclRefType->declRef.As<InterfaceDecl>() )
{
//
return TryJoinTypeWithInterface(right, leftInterfaceRef);
@@ -2633,7 +2633,7 @@ namespace Slang
}
if(auto rightDeclRefType = right->As<DeclRefType>())
{
- if( auto rightInterfaceRef = rightDeclRefType->declRef.As<InterfaceDeclRef>() )
+ if( auto rightInterfaceRef = rightDeclRefType->declRef.As<InterfaceDecl>() )
{
//
return TryJoinTypeWithInterface(left, rightInterfaceRef);
@@ -2655,14 +2655,14 @@ namespace Slang
// we solved for along the way.
RefPtr<Substitutions> TrySolveConstraintSystem(
ConstraintSystem* system,
- GenericDeclRef genericDeclRef)
+ DeclRef<GenericDecl> genericDeclRef)
{
// For now the "solver" is going to be ridiculously simplistic.
// The generic itself will have some constraints, so we need to try and solve those too
- for( auto constraintDeclRef : genericDeclRef.GetMembersOfType<GenericTypeConstraintDeclRef>() )
+ for( auto constraintDeclRef : getMembersOfType<GenericTypeConstraintDecl>(genericDeclRef) )
{
- if(!TryUnifyTypes(*system, constraintDeclRef.GetSub(), constraintDeclRef.GetSup()))
+ if(!TryUnifyTypes(*system, GetSub(constraintDeclRef), GetSup(constraintDeclRef)))
return nullptr;
}
@@ -2670,14 +2670,14 @@ namespace Slang
// each we will try to find a way to satisfy all
// the constraints for that parameter
List<RefPtr<Val>> args;
- for (auto m : genericDeclRef.GetMembers())
+ for (auto m : getMembers(genericDeclRef))
{
- if (auto typeParam = m.As<GenericTypeParamDeclRef>())
+ if (auto typeParam = m.As<GenericTypeParamDecl>())
{
RefPtr<ExpressionType> type = nullptr;
for (auto& c : system->constraints)
{
- if (c.decl != typeParam.GetDecl())
+ if (c.decl != typeParam.getDecl())
continue;
auto cType = c.val.As<ExpressionType>();
@@ -2708,7 +2708,7 @@ namespace Slang
}
args.Add(type);
}
- else if (auto valParam = m.As<GenericValueParamDeclRef>())
+ else if (auto valParam = m.As<GenericValueParamDecl>())
{
// TODO(tfoley): maybe support more than integers some day?
// TODO(tfoley): figure out how this needs to interact with
@@ -2716,7 +2716,7 @@ namespace Slang
RefPtr<IntVal> val = nullptr;
for (auto& c : system->constraints)
{
- if (c.decl != valParam.GetDecl())
+ if (c.decl != valParam.getDecl())
continue;
auto cVal = c.val.As<IntVal>();
@@ -2764,7 +2764,7 @@ namespace Slang
// Consruct a reference to the extension with our constraint variables
// as the
RefPtr<Substitutions> solvedSubst = new Substitutions();
- solvedSubst->genericDecl = genericDeclRef.GetDecl();
+ solvedSubst->genericDecl = genericDeclRef.getDecl();
solvedSubst->outer = genericDeclRef.substitutions;
solvedSubst->args = args;
@@ -2780,7 +2780,7 @@ namespace Slang
RefPtr<ExpressionType> type = nullptr;
for (auto& c : system->constraints)
{
- if (c.decl != typeVar->declRef.GetDecl())
+ if (c.decl != typeVar->declRef.getDecl())
continue;
auto cType = c.val.As<ExpressionType>();
@@ -2815,7 +2815,7 @@ namespace Slang
RefPtr<IntVal> val = nullptr;
for (auto& c : system->constraints)
{
- if (c.decl != valueVar->declRef.GetDecl())
+ if (c.decl != valueVar->declRef.getDecl())
continue;
auto cVal = c.val.As<IntVal>();
@@ -2944,7 +2944,7 @@ namespace Slang
};
// count the number of parameters required/allowed for a callable
- ParamCounts CountParameters(FilteredMemberRefList<ParamDeclRef> params)
+ ParamCounts CountParameters(FilteredMemberRefList<ParameterSyntaxNode> params)
{
ParamCounts counts = { 0, 0 };
for (auto param : params)
@@ -2961,7 +2961,7 @@ namespace Slang
// 2. We are not handling the possibility of multiple declarations for
// a single function, where we'd need to merge default parameters across
// all the declarations.
- if (!param.GetDecl()->Expr)
+ if (!param.getDecl()->Expr)
{
counts.required++;
}
@@ -2970,10 +2970,10 @@ namespace Slang
}
// count the number of parameters required/allowed for a generic
- ParamCounts CountParameters(GenericDeclRef genericRef)
+ ParamCounts CountParameters(DeclRef<GenericDecl> genericRef)
{
ParamCounts counts = { 0, 0 };
- for (auto m : genericRef.GetDecl()->Members)
+ for (auto m : genericRef.getDecl()->Members)
{
if (auto typeParam = m.As<GenericTypeParamDecl>())
{
@@ -3004,11 +3004,11 @@ namespace Slang
switch (candidate.flavor)
{
case OverloadCandidate::Flavor::Func:
- paramCounts = CountParameters(candidate.item.declRef.As<CallableDeclRef>().GetParameters());
+ paramCounts = CountParameters(GetParameters(candidate.item.declRef.As<CallableDecl>()));
break;
case OverloadCandidate::Flavor::Generic:
- paramCounts = CountParameters(candidate.item.declRef.As<GenericDeclRef>());
+ paramCounts = CountParameters(candidate.item.declRef.As<GenericDecl>());
break;
default:
@@ -3084,12 +3084,12 @@ namespace Slang
{
auto& args = context.appExpr->Arguments;
- auto genericDeclRef = candidate.item.declRef.As<GenericDeclRef>();
+ auto genericDeclRef = candidate.item.declRef.As<GenericDecl>();
int aa = 0;
- for (auto memberRef : genericDeclRef.GetMembers())
+ for (auto memberRef : getMembers(genericDeclRef))
{
- if (auto typeParamRef = memberRef.As<GenericTypeParamDeclRef>())
+ if (auto typeParamRef = memberRef.As<GenericTypeParamDecl>())
{
auto arg = args[aa++];
@@ -3105,14 +3105,14 @@ namespace Slang
TypeExp typeExp = CoerceToProperType(TypeExp(arg));
}
}
- else if (auto valParamRef = memberRef.As<GenericValueParamDeclRef>())
+ else if (auto valParamRef = memberRef.As<GenericValueParamDecl>())
{
auto arg = args[aa++];
if (context.mode == OverloadResolveContext::Mode::JustTrying)
{
ConversionCost cost = kConversionCost_None;
- if (!CanCoerce(valParamRef.GetType(), arg->Type, &cost))
+ if (!CanCoerce(GetType(valParamRef), arg->Type, &cost))
{
return false;
}
@@ -3120,7 +3120,7 @@ namespace Slang
}
else
{
- arg = Coerce(valParamRef.GetType(), arg);
+ arg = Coerce(GetType(valParamRef), arg);
auto val = ExtractGenericArgInteger(arg);
}
}
@@ -3140,11 +3140,11 @@ namespace Slang
auto& args = context.appExpr->Arguments;
int argCount = args.Count();
- List<ParamDeclRef> params;
+ List<DeclRef<ParameterSyntaxNode>> params;
switch (candidate.flavor)
{
case OverloadCandidate::Flavor::Func:
- params = candidate.item.declRef.As<CallableDeclRef>().GetParameters().ToArray();
+ params = GetParameters(candidate.item.declRef.As<CallableDecl>()).ToArray();
break;
case OverloadCandidate::Flavor::Generic:
@@ -3167,7 +3167,7 @@ namespace Slang
if (context.mode == OverloadResolveContext::Mode::JustTrying)
{
ConversionCost cost = kConversionCost_None;
- if (!CanCoerce(param.GetType(), arg->Type, &cost))
+ if (!CanCoerce(GetType(param), arg->Type, &cost))
{
return false;
}
@@ -3175,7 +3175,7 @@ namespace Slang
}
else
{
- arg = Coerce(param.GetType(), arg);
+ arg = Coerce(GetType(param), arg);
}
}
return true;
@@ -3224,7 +3224,7 @@ namespace Slang
assert(!"unexpected");
return CreateErrorExpr(appExpr.Ptr());
}
- auto baseGenericRef = baseDeclRefExpr->declRef.As<GenericDeclRef>();
+ auto baseGenericRef = baseDeclRefExpr->declRef.As<GenericDecl>();
if (!baseGenericRef)
{
assert(!"unexpected");
@@ -3232,7 +3232,7 @@ namespace Slang
}
RefPtr<Substitutions> subst = new Substitutions();
- subst->genericDecl = baseGenericRef.GetDecl();
+ subst->genericDecl = baseGenericRef.getDecl();
subst->outer = baseGenericRef.substitutions;
for (auto arg : appExpr->Arguments)
@@ -3240,7 +3240,7 @@ namespace Slang
subst->args.Add(ExtractGenericArgVal(arg));
}
- DeclRef innerDeclRef(baseGenericRef.GetInner(), subst);
+ DeclRef<Decl> innerDeclRef(GetInner(baseGenericRef), subst);
return ConstructDeclRefExpr(
innerDeclRef,
@@ -3298,9 +3298,9 @@ namespace Slang
context.appExpr->Type = candidate.resultType;
// A call may yield an l-value, and we should take a look at the candidate to be sure
- if(auto subscriptDeclRef = candidate.item.declRef.As<SubscriptDeclRef>())
+ if(auto subscriptDeclRef = candidate.item.declRef.As<SubscriptDecl>())
{
- for(auto setter : subscriptDeclRef.GetDecl()->GetMembersOfType<SetterDecl>())
+ for(auto setter : subscriptDeclRef.getDecl()->getMembersOfType<SetterDecl>())
{
context.appExpr->Type.IsLeftValue = true;
}
@@ -3442,15 +3442,15 @@ namespace Slang
void AddFuncOverloadCandidate(
LookupResultItem item,
- CallableDeclRef funcDeclRef,
+ DeclRef<CallableDecl> funcDeclRef,
OverloadResolveContext& context)
{
- EnsureDecl(funcDeclRef.GetDecl());
+ EnsureDecl(funcDeclRef.getDecl());
OverloadCandidate candidate;
candidate.flavor = OverloadCandidate::Flavor::Func;
candidate.item = item;
- candidate.resultType = funcDeclRef.GetResultType();
+ candidate.resultType = GetResultType(funcDeclRef);
AddOverloadCandidate(context, candidate);
}
@@ -3480,10 +3480,10 @@ namespace Slang
void AddCtorOverloadCandidate(
LookupResultItem typeItem,
RefPtr<ExpressionType> type,
- ConstructorDeclRef ctorDeclRef,
+ DeclRef<ConstructorDecl> ctorDeclRef,
OverloadResolveContext& context)
{
- EnsureDecl(ctorDeclRef.GetDecl());
+ EnsureDecl(ctorDeclRef.getDecl());
// `typeItem` refers to the type being constructed (the thing
// that was applied as a function) so we need to construct
@@ -3622,12 +3622,12 @@ namespace Slang
bool TryUnifyIntParam(
ConstraintSystem& constraints,
- VarDeclBaseRef const& varRef,
+ DeclRef<VarDeclBase> const& varRef,
RefPtr<IntVal> val)
{
- if(auto genericValueParamRef = varRef.As<GenericValueParamDeclRef>())
+ if(auto genericValueParamRef = varRef.As<GenericValueParamDecl>())
{
- return TryUnifyIntParam(constraints, genericValueParamRef.GetDecl(), val);
+ return TryUnifyIntParam(constraints, genericValueParamRef.getDecl(), val);
}
else
{
@@ -3644,18 +3644,18 @@ namespace Slang
{
auto fstDeclRef = fstDeclRefType->declRef;
- if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(fstDeclRef.GetDecl()))
+ if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(fstDeclRef.getDecl()))
return TryUnifyTypeParam(constraints, typeParamDecl, snd);
if (auto sndDeclRefType = snd->As<DeclRefType>())
{
auto sndDeclRef = sndDeclRefType->declRef;
- if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(sndDeclRef.GetDecl()))
+ if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(sndDeclRef.getDecl()))
return TryUnifyTypeParam(constraints, typeParamDecl, fst);
// can't be unified if they refer to differnt declarations.
- if (fstDeclRef.GetDecl() != sndDeclRef.GetDecl()) return false;
+ if (fstDeclRef.getDecl() != sndDeclRef.getDecl()) return false;
// next we need to unify the substitutions applied
// to each decalration reference.
@@ -3697,7 +3697,7 @@ namespace Slang
{
auto fstDeclRef = fstDeclRefType->declRef;
- if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(fstDeclRef.GetDecl()))
+ if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(fstDeclRef.getDecl()))
return TryUnifyTypeParam(constraints, typeParamDecl, snd);
}
@@ -3705,7 +3705,7 @@ namespace Slang
{
auto sndDeclRef = sndDeclRefType->declRef;
- if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(sndDeclRef.GetDecl()))
+ if (auto typeParamDecl = dynamic_cast<GenericTypeParamDecl*>(sndDeclRef.getDecl()))
return TryUnifyTypeParam(constraints, typeParamDecl, fst);
}
@@ -3746,7 +3746,7 @@ namespace Slang
}
// Is the candidate extension declaration actually applicable to the given type
- ExtensionDeclRef ApplyExtensionToType(
+ DeclRef<ExtensionDecl> ApplyExtensionToType(
ExtensionDecl* extDecl,
RefPtr<ExpressionType> type)
{
@@ -3755,21 +3755,21 @@ namespace Slang
ConstraintSystem constraints;
if (!TryUnifyTypes(constraints, extDecl->targetType, type))
- return DeclRef().As<ExtensionDeclRef>();
+ return DeclRef<Decl>().As<ExtensionDecl>();
- auto constraintSubst = TrySolveConstraintSystem(&constraints, DeclRef(extGenericDecl, nullptr).As<GenericDeclRef>());
+ auto constraintSubst = TrySolveConstraintSystem(&constraints, DeclRef<Decl>(extGenericDecl, nullptr).As<GenericDecl>());
if (!constraintSubst)
{
- return DeclRef().As<ExtensionDeclRef>();
+ return DeclRef<Decl>().As<ExtensionDecl>();
}
// Consruct a reference to the extension with our constraint variables
// set as they were found by solving the constraint system.
- ExtensionDeclRef extDeclRef = DeclRef(extDecl, constraintSubst).As<ExtensionDeclRef>();
+ DeclRef<ExtensionDecl> extDeclRef = DeclRef<Decl>(extDecl, constraintSubst).As<ExtensionDecl>();
// We expect/require that the result of unification is such that
// the target types are now equal
- assert(extDeclRef.GetTargetType()->Equals(type));
+ assert(GetTargetType(extDeclRef)->Equals(type));
return extDeclRef;
}
@@ -3778,27 +3778,27 @@ namespace Slang
// The easy case is when the extension isn't generic:
// either it applies to the type or not.
if (!type->Equals(extDecl->targetType))
- return DeclRef().As<ExtensionDeclRef>();
- return DeclRef(extDecl, nullptr).As<ExtensionDeclRef>();
+ return DeclRef<Decl>().As<ExtensionDecl>();
+ return DeclRef<Decl>(extDecl, nullptr).As<ExtensionDecl>();
}
}
bool TryUnifyArgAndParamTypes(
ConstraintSystem& system,
RefPtr<ExpressionSyntaxNode> argExpr,
- ParamDeclRef paramDeclRef)
+ DeclRef<ParameterSyntaxNode> paramDeclRef)
{
// TODO(tfoley): potentially need a bit more
// nuance in case where argument might be
// an overload group...
- return TryUnifyTypes(system, argExpr->Type, paramDeclRef.GetType());
+ return TryUnifyTypes(system, argExpr->Type, GetType(paramDeclRef));
}
// Take a generic declaration and try to specialize its parameters
// so that the resulting inner declaration can be applicable in
// a particular context...
- DeclRef SpecializeGenericForOverload(
- GenericDeclRef genericDeclRef,
+ DeclRef<Decl> SpecializeGenericForOverload(
+ DeclRef<GenericDecl> genericDeclRef,
OverloadResolveContext& context)
{
ConstraintSystem constraints;
@@ -3806,15 +3806,15 @@ namespace Slang
// Construct a reference to the inner declaration that has any generic
// parameter substitutions in place already, but *not* any substutions
// for the generic declaration we are currently trying to infer.
- auto innerDecl = genericDeclRef.GetInner();
- DeclRef unspecializedInnerRef = DeclRef(innerDecl, genericDeclRef.substitutions);
+ auto innerDecl = GetInner(genericDeclRef);
+ DeclRef<Decl> unspecializedInnerRef = DeclRef<Decl>(innerDecl, genericDeclRef.substitutions);
// Check what type of declaration we are dealing with, and then try
// to match it up with the arguments accordingly...
- if (auto funcDeclRef = unspecializedInnerRef.As<CallableDeclRef>())
+ if (auto funcDeclRef = unspecializedInnerRef.As<CallableDecl>())
{
auto& args = context.appExpr->Arguments;
- auto params = funcDeclRef.GetParameters().ToArray();
+ auto params = GetParameters(funcDeclRef).ToArray();
int argCount = args.Count();
int paramCount = params.Count();
@@ -3823,14 +3823,14 @@ namespace Slang
// TODO(tfoley): need more nuance here
if (argCount != paramCount)
{
- return DeclRef(nullptr, nullptr);
+ return DeclRef<Decl>(nullptr, nullptr);
}
for (int aa = 0; aa < argCount; ++aa)
{
#if 0
if (!TryUnifyArgAndParamTypes(constraints, args[aa], params[aa]))
- return DeclRef(nullptr, nullptr);
+ return DeclRef<Decl>(nullptr, nullptr);
#else
// The question here is whether failure to "unify" an argument
// and parameter should lead to immediate failure.
@@ -3857,41 +3857,41 @@ namespace Slang
else
{
// TODO(tfoley): any other cases needed here?
- return DeclRef(nullptr, nullptr);
+ return DeclRef<Decl>(nullptr, nullptr);
}
auto constraintSubst = TrySolveConstraintSystem(&constraints, genericDeclRef);
if (!constraintSubst)
{
// constraint solving failed
- return DeclRef(nullptr, nullptr);
+ return DeclRef<Decl>(nullptr, nullptr);
}
// We can now construct a reference to the inner declaration using
// the solution to our constraints.
- return DeclRef(innerDecl, constraintSubst);
+ return DeclRef<Decl>(innerDecl, constraintSubst);
}
void AddAggTypeOverloadCandidates(
LookupResultItem typeItem,
RefPtr<ExpressionType> type,
- AggTypeDeclRef aggTypeDeclRef,
+ DeclRef<AggTypeDecl> aggTypeDeclRef,
OverloadResolveContext& context)
{
- for (auto ctorDeclRef : aggTypeDeclRef.GetMembersOfType<ConstructorDeclRef>())
+ for (auto ctorDeclRef : getMembersOfType<ConstructorDecl>(aggTypeDeclRef))
{
// now work through this candidate...
AddCtorOverloadCandidate(typeItem, type, ctorDeclRef, context);
}
// Now walk through any extensions we can find for this types
- for (auto ext = aggTypeDeclRef.GetCandidateExtensions(); ext; ext = ext->nextCandidateExtension)
+ for (auto ext = GetCandidateExtensions(aggTypeDeclRef); ext; ext = ext->nextCandidateExtension)
{
auto extDeclRef = ApplyExtensionToType(ext, type);
if (!extDeclRef)
continue;
- for (auto ctorDeclRef : extDeclRef.GetMembersOfType<ConstructorDeclRef>())
+ for (auto ctorDeclRef : getMembersOfType<ConstructorDecl>(extDeclRef))
{
// TODO(tfoley): `typeItem` here should really reference the extension...
@@ -3900,15 +3900,15 @@ namespace Slang
}
// Also check for generic constructors
- for (auto genericDeclRef : extDeclRef.GetMembersOfType<GenericDeclRef>())
+ for (auto genericDeclRef : getMembersOfType<GenericDecl>(extDeclRef))
{
- if (auto ctorDecl = genericDeclRef.GetDecl()->inner.As<ConstructorDecl>())
+ if (auto ctorDecl = genericDeclRef.getDecl()->inner.As<ConstructorDecl>())
{
- DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context);
+ DeclRef<Decl> innerRef = SpecializeGenericForOverload(genericDeclRef, context);
if (!innerRef)
continue;
- ConstructorDeclRef innerCtorRef = innerRef.As<ConstructorDeclRef>();
+ DeclRef<ConstructorDecl> innerCtorRef = innerRef.As<ConstructorDecl>();
AddCtorOverloadCandidate(typeItem, type, innerCtorRef, context);
@@ -3924,7 +3924,7 @@ namespace Slang
{
if (auto declRefType = type->As<DeclRefType>())
{
- if (auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDeclRef>())
+ if (auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDecl>())
{
AddAggTypeOverloadCandidates(LookupResultItem(aggTypeDeclRef), type, aggTypeDeclRef, context);
}
@@ -3937,19 +3937,19 @@ namespace Slang
{
auto declRef = item.declRef;
- if (auto funcDeclRef = item.declRef.As<CallableDeclRef>())
+ if (auto funcDeclRef = item.declRef.As<CallableDecl>())
{
AddFuncOverloadCandidate(item, funcDeclRef, context);
}
- else if (auto aggTypeDeclRef = item.declRef.As<AggTypeDeclRef>())
+ else if (auto aggTypeDeclRef = item.declRef.As<AggTypeDecl>())
{
auto type = DeclRefType::Create(aggTypeDeclRef);
AddAggTypeOverloadCandidates(item, type, aggTypeDeclRef, context);
}
- else if (auto genericDeclRef = item.declRef.As<GenericDeclRef>())
+ else if (auto genericDeclRef = item.declRef.As<GenericDecl>())
{
// Try to infer generic arguments, based on the context
- DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context);
+ DeclRef<Decl> innerRef = SpecializeGenericForOverload(genericDeclRef, context);
if (innerRef)
{
@@ -3975,9 +3975,9 @@ namespace Slang
AddOverloadCandidateInner(context, candidate);
}
}
- else if( auto typeDefDeclRef = item.declRef.As<TypeDefDeclRef>() )
+ else if( auto typeDefDeclRef = item.declRef.As<TypeDefDecl>() )
{
- AddTypeOverloadCandidates(typeDefDeclRef.GetType(), context);
+ AddTypeOverloadCandidates(GetType(typeDefDeclRef), context);
}
else
{
@@ -4033,21 +4033,21 @@ namespace Slang
sb << val->ToString();
}
- void formatDeclPath(StringBuilder& sb, DeclRef declRef)
+ void formatDeclPath(StringBuilder& sb, DeclRef<Decl> declRef)
{
// Find the parent declaration
auto parentDeclRef = declRef.GetParent();
// If the immediate parent is a generic, then we probably
// want the declaration above that...
- auto parentGenericDeclRef = parentDeclRef.As<GenericDeclRef>();
+ auto parentGenericDeclRef = parentDeclRef.As<GenericDecl>();
if(parentGenericDeclRef)
{
parentDeclRef = parentGenericDeclRef.GetParent();
}
// Depending on what the parent is, we may want to format things specially
- if(auto aggTypeDeclRef = parentDeclRef.As<AggTypeDeclRef>())
+ if(auto aggTypeDeclRef = parentDeclRef.As<AggTypeDecl>())
{
formatDeclPath(sb, aggTypeDeclRef);
sb << ".";
@@ -4060,7 +4060,7 @@ namespace Slang
if( parentGenericDeclRef )
{
assert(declRef.substitutions);
- assert(declRef.substitutions->genericDecl == parentGenericDeclRef.GetDecl());
+ assert(declRef.substitutions->genericDecl == parentGenericDeclRef.getDecl());
sb << "<";
bool first = true;
@@ -4074,20 +4074,20 @@ namespace Slang
}
}
- void formatDeclParams(StringBuilder& sb, DeclRef declRef)
+ void formatDeclParams(StringBuilder& sb, DeclRef<Decl> declRef)
{
- if (auto funcDeclRef = declRef.As<CallableDeclRef>())
+ if (auto funcDeclRef = declRef.As<CallableDecl>())
{
// This is something callable, so we need to also print parameter types for overloading
sb << "(";
bool first = true;
- for (auto paramDeclRef : funcDeclRef.GetParameters())
+ for (auto paramDeclRef : GetParameters(funcDeclRef))
{
if (!first) sb << ", ";
- formatType(sb, paramDeclRef.GetType());
+ formatType(sb, GetType(paramDeclRef));
first = false;
@@ -4095,25 +4095,25 @@ namespace Slang
sb << ")";
}
- else if(auto genericDeclRef = declRef.As<GenericDeclRef>())
+ else if(auto genericDeclRef = declRef.As<GenericDecl>())
{
sb << "<";
bool first = true;
- for (auto paramDeclRef : genericDeclRef.GetMembers())
+ for (auto paramDeclRef : getMembers(genericDeclRef))
{
- if(auto genericTypeParam = paramDeclRef.As<GenericTypeParamDeclRef>())
+ if(auto genericTypeParam = paramDeclRef.As<GenericTypeParamDecl>())
{
if (!first) sb << ", ";
first = false;
sb << genericTypeParam.GetName();
}
- else if(auto genericValParam = paramDeclRef.As<GenericValueParamDeclRef>())
+ else if(auto genericValParam = paramDeclRef.As<GenericValueParamDecl>())
{
if (!first) sb << ", ";
first = false;
- formatType(sb, genericValParam.GetType());
+ formatType(sb, GetType(genericValParam));
sb << " ";
sb << genericValParam.GetName();
}
@@ -4122,20 +4122,20 @@ namespace Slang
}
sb << ">";
- formatDeclParams(sb, DeclRef(genericDeclRef.GetInner(), genericDeclRef.substitutions));
+ formatDeclParams(sb, DeclRef<Decl>(GetInner(genericDeclRef), genericDeclRef.substitutions));
}
else
{
}
}
- void formatDeclSignature(StringBuilder& sb, DeclRef declRef)
+ void formatDeclSignature(StringBuilder& sb, DeclRef<Decl> declRef)
{
formatDeclPath(sb, declRef);
formatDeclParams(sb, declRef);
}
- String getDeclSignatureString(DeclRef declRef)
+ String getDeclSignatureString(DeclRef<Decl> declRef)
{
StringBuilder sb;
formatDeclSignature(sb, declRef);
@@ -4283,9 +4283,9 @@ namespace Slang
LookupResultItem baseItem,
OverloadResolveContext& context)
{
- if (auto genericDeclRef = baseItem.declRef.As<GenericDeclRef>())
+ if (auto genericDeclRef = baseItem.declRef.As<GenericDecl>())
{
- EnsureDecl(genericDeclRef.GetDecl());
+ EnsureDecl(genericDeclRef.getDecl());
OverloadCandidate candidate;
candidate.flavor = OverloadCandidate::Flavor::Generic;
@@ -4414,11 +4414,11 @@ namespace Slang
{
auto declRef = baseDeclRefExpr->declRef;
- if (auto genericDeclRef = declRef.As<GenericDeclRef>())
+ if (auto genericDeclRef = declRef.As<GenericDecl>())
{
int argCount = typeNode->Args.Count();
int argIndex = 0;
- for (RefPtr<Decl> member : genericDeclRef.GetDecl()->Members)
+ for (RefPtr<Decl> member : genericDeclRef.getDecl()->Members)
{
if (auto typeParam = member.As<GenericTypeParamDecl>())
{
@@ -4500,7 +4500,7 @@ namespace Slang
{
List<RefPtr<ParameterSyntaxNode>> paramsStorage;
List<RefPtr<ParameterSyntaxNode>> * params = nullptr;
- if (auto func = funcType->declRef.GetDecl())
+ if (auto func = funcType->declRef.getDecl())
{
paramsStorage = func->GetParameters().ToArray();
params = &paramsStorage;
@@ -4622,7 +4622,7 @@ namespace Slang
#endif
// Get the type to use when referencing a declaration
- QualType GetTypeForDeclRef(DeclRef declRef)
+ QualType GetTypeForDeclRef(DeclRef<Decl> declRef)
{
return getTypeForDeclRef(
this,
@@ -4792,10 +4792,10 @@ namespace Slang
}
else if (auto declRefType = baseType->AsDeclRefType())
{
- if (auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDeclRef>())
+ if (auto aggTypeDeclRef = declRefType->declRef.As<AggTypeDecl>())
{
// Checking of the type must be complete before we can reference its members safely
- EnsureDecl(aggTypeDeclRef.GetDecl(), DeclCheckState::Checked);
+ EnsureDecl(aggTypeDeclRef.getDecl(), DeclCheckState::Checked);
LookupResult lookupResult = LookUpLocal(expr->MemberName, aggTypeDeclRef);
@@ -4809,7 +4809,7 @@ namespace Slang
expr->BaseExpression,
expr);
#if 0
- DeclRef memberDeclRef(lookupResult.decl, aggTypeDeclRef.substitutions);
+ DeclRef<Decl> memberDeclRef(lookupResult.decl, aggTypeDeclRef.substitutions);
return ConstructDeclRefExpr(memberDeclRef, expr->BaseExpression, expr);
#endif
@@ -4831,11 +4831,11 @@ namespace Slang
if (!memberDecl)
{
- memberDecl = m.GetDecl();
+ memberDecl = m.getDecl();
}
else
{
- secondDecl = m.GetDecl();
+ secondDecl = m.getDecl();
break;
}
}
@@ -4852,7 +4852,7 @@ namespace Slang
if (!secondDecl)
{
// TODO: need to
- DeclRef memberDeclRef(memberDecl, aggTypeDeclRef.substitutions);
+ DeclRef<Decl> memberDeclRef(memberDecl, aggTypeDeclRef.substitutions);
expr->declRef = memberDeclRef;
expr->Type = GetTypeForDeclRef(memberDeclRef);
@@ -4976,48 +4976,48 @@ namespace Slang
QualType getTypeForDeclRef(
SemanticsVisitor* sema,
DiagnosticSink* sink,
- DeclRef declRef,
+ DeclRef<Decl> declRef,
RefPtr<ExpressionType>* outTypeResult)
{
if( sema )
{
- sema->EnsureDecl(declRef.GetDecl());
+ sema->EnsureDecl(declRef.getDecl());
}
// We need to insert an appropriate type for the expression, based on
// what we found.
- if (auto varDeclRef = declRef.As<VarDeclBaseRef>())
+ if (auto varDeclRef = declRef.As<VarDeclBase>())
{
QualType qualType;
- qualType.type = varDeclRef.GetType();
+ qualType.type = GetType(varDeclRef);
qualType.IsLeftValue = true; // TODO(tfoley): allow explicit `const` or `let` variables
return qualType;
}
- else if (auto typeAliasDeclRef = declRef.As<TypeDefDeclRef>())
+ else if (auto typeAliasDeclRef = declRef.As<TypeDefDecl>())
{
auto type = new NamedExpressionType(typeAliasDeclRef);
*outTypeResult = type;
return new TypeType(type);
}
- else if (auto aggTypeDeclRef = declRef.As<AggTypeDeclRef>())
+ else if (auto aggTypeDeclRef = declRef.As<AggTypeDecl>())
{
auto type = DeclRefType::Create(aggTypeDeclRef);
*outTypeResult = type;
return new TypeType(type);
}
- else if (auto simpleTypeDeclRef = declRef.As<SimpleTypeDeclRef>())
+ else if (auto simpleTypeDeclRef = declRef.As<SimpleTypeDecl>())
{
auto type = DeclRefType::Create(simpleTypeDeclRef);
*outTypeResult = type;
return new TypeType(type);
}
- else if (auto genericDeclRef = declRef.As<GenericDeclRef>())
+ else if (auto genericDeclRef = declRef.As<GenericDecl>())
{
auto type = new GenericDeclRefType(genericDeclRef);
*outTypeResult = type;
return new TypeType(type);
}
- else if (auto funcDeclRef = declRef.As<CallableDeclRef>())
+ else if (auto funcDeclRef = declRef.As<CallableDecl>())
{
auto type = new FuncType();
type->declRef = funcDeclRef;
@@ -5032,7 +5032,7 @@ namespace Slang
}
QualType getTypeForDeclRef(
- DeclRef declRef)
+ DeclRef<Decl> declRef)
{
RefPtr<ExpressionType> typeResult;
return getTypeForDeclRef(nullptr, nullptr, declRef, &typeResult);
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp
index 69bdaebf6..a2a1e9e21 100644
--- a/source/slang/emit.cpp
+++ b/source/slang/emit.cpp
@@ -43,7 +43,7 @@ static void EmitType(EmitContext* context, RefPtr<ExpressionType> type, String c
static void EmitType(EmitContext* context, RefPtr<ExpressionType> type);
static void EmitExpr(EmitContext* context, RefPtr<ExpressionSyntaxNode> expr);
static void EmitStmt(EmitContext* context, RefPtr<StatementSyntaxNode> stmt);
-static void EmitDeclRef(EmitContext* context, DeclRef declRef);
+static void EmitDeclRef(EmitContext* context, DeclRef<Decl> declRef);
// Low-level emit logic
@@ -457,7 +457,7 @@ static void emitCallExpr(
if (auto funcDeclRefExpr = funcExpr.As<DeclRefExpr>())
{
auto funcDeclRef = funcDeclRefExpr->declRef;
- auto funcDecl = funcDeclRef.GetDecl();
+ auto funcDecl = funcDeclRef.getDecl();
if (auto intrinsicOpModifier = funcDecl->FindModifier<IntrinsicOpModifier>())
{
switch (intrinsicOpModifier->op)
@@ -617,7 +617,7 @@ static void emitCallExpr(
// We might be calling an intrinsic subscript operation,
// and should desugar it accordingly
- if(auto subscriptDeclRef = funcDeclRef.As<SubscriptDeclRef>())
+ if(auto subscriptDeclRef = funcDeclRef.As<SubscriptDecl>())
{
// We expect any subscript operation to be invoked as a member,
// so the function expression had better be in the correct form.
@@ -647,7 +647,7 @@ static void emitCallExpr(
if (auto funcDeclRefExpr = funcExpr.As<DeclRefExpr>())
{
auto declRef = funcDeclRefExpr->declRef;
- if (auto ctorDeclRef = declRef.As<ConstructorDeclRef>())
+ if (auto ctorDeclRef = declRef.As<ConstructorDecl>())
{
// We really want to emit a reference to the type begin constructed
EmitType(context, callExpr->Type);
@@ -1604,7 +1604,7 @@ static void EmitVal(EmitContext* context, RefPtr<Val> val)
}
}
-static void EmitDeclRef(EmitContext* context, DeclRef declRef)
+static void EmitDeclRef(EmitContext* context, DeclRef<Decl> declRef)
{
// TODO: need to qualify a declaration name based on parent scopes/declarations
@@ -1614,10 +1614,10 @@ static void EmitDeclRef(EmitContext* context, DeclRef declRef)
// If the declaration is nested directly in a generic, then
// we need to output the generic arguments here
auto parentDeclRef = declRef.GetParent();
- if (auto genericDeclRef = parentDeclRef.As<GenericDeclRef>())
+ if (auto genericDeclRef = parentDeclRef.As<GenericDecl>())
{
// Only do this for declarations of appropriate flavors
- if(auto funcDeclRef = declRef.As<FuncDeclBaseRef>())
+ if(auto funcDeclRef = declRef.As<FunctionDeclBase>())
{
// Don't emit generic arguments for functions, because HLSL doesn't allow them
return;
@@ -1869,16 +1869,16 @@ static void EmitStructDecl(EmitContext* context, RefPtr<StructSyntaxNode> decl)
}
// Shared emit logic for variable declarations (used for parameters, locals, globals, fields)
-static void EmitVarDeclCommon(EmitContext* context, VarDeclBaseRef declRef)
+static void EmitVarDeclCommon(EmitContext* context, DeclRef<VarDeclBase> declRef)
{
- EmitModifiers(context, declRef.GetDecl());
+ EmitModifiers(context, declRef.getDecl());
- EmitType(context, declRef.GetType(), declRef.GetName());
+ EmitType(context, GetType(declRef), declRef.GetName());
- EmitSemantics(context, declRef.GetDecl());
+ EmitSemantics(context, declRef.getDecl());
// TODO(tfoley): technically have to apply substitution here too...
- if (auto initExpr = declRef.GetDecl()->Expr)
+ if (auto initExpr = declRef.getDecl()->Expr)
{
Emit(context, " = ");
EmitExpr(context, initExpr);
@@ -1888,7 +1888,7 @@ static void EmitVarDeclCommon(EmitContext* context, VarDeclBaseRef declRef)
// Shared emit logic for variable declarations (used for parameters, locals, globals, fields)
static void EmitVarDeclCommon(EmitContext* context, RefPtr<VarDeclBase> decl)
{
- EmitVarDeclCommon(context, DeclRef(decl.Ptr(), nullptr).As<VarDeclBaseRef>());
+ EmitVarDeclCommon(context, DeclRef<Decl>(decl.Ptr(), nullptr).As<VarDeclBase>());
}
// Emit a single `regsiter` semantic, as appropriate for a given resource-type-specific layout info
@@ -2033,14 +2033,14 @@ static void emitHLSLParameterBlockDecl(
emitHLSLRegisterSemantic(context, *info);
Emit(context, "\n{\n");
- if (auto structRef = declRefType->declRef.As<StructDeclRef>())
+ if (auto structRef = declRefType->declRef.As<StructSyntaxNode>())
{
- for (auto field : structRef.GetMembersOfType<FieldDeclRef>())
+ for (auto field : getMembersOfType<StructField>(structRef))
{
EmitVarDeclCommon(context, field);
RefPtr<VarLayout> fieldLayout;
- structTypeLayout->mapVarToLayout.TryGetValue(field.GetDecl(), fieldLayout);
+ structTypeLayout->mapVarToLayout.TryGetValue(field.getDecl(), fieldLayout);
assert(fieldLayout);
// Emit explicit layout annotations for every field
@@ -2197,12 +2197,12 @@ static void emitGLSLParameterBlockDecl(
}
Emit(context, "\n{\n");
- if (auto structRef = declRefType->declRef.As<StructDeclRef>())
+ if (auto structRef = declRefType->declRef.As<StructSyntaxNode>())
{
- for (auto field : structRef.GetMembersOfType<FieldDeclRef>())
+ for (auto field : getMembersOfType<StructField>(structRef))
{
RefPtr<VarLayout> fieldLayout;
- structTypeLayout->mapVarToLayout.TryGetValue(field.GetDecl(), fieldLayout);
+ structTypeLayout->mapVarToLayout.TryGetValue(field.getDecl(), fieldLayout);
assert(fieldLayout);
// TODO(tfoley): We may want to emit *some* of these,
@@ -2292,7 +2292,7 @@ static void EmitFuncDecl(EmitContext* context, RefPtr<FunctionSyntaxNode> decl)
Emit(context, "(");
bool first = true;
- for (auto paramDecl : decl->GetMembersOfType<ParameterSyntaxNode>())
+ for (auto paramDecl : decl->getMembersOfType<ParameterSyntaxNode>())
{
if (!first) Emit(context, ", ");
EmitParamDecl(context, paramDecl);
diff --git a/source/slang/lookup.cpp b/source/slang/lookup.cpp
index 89d07e223..e9c2177de 100644
--- a/source/slang/lookup.cpp
+++ b/source/slang/lookup.cpp
@@ -9,13 +9,13 @@ namespace Slang {
struct BreadcrumbInfo
{
LookupResultItem::Breadcrumb::Kind kind;
- DeclRef declRef;
+ DeclRef<Decl> declRef;
BreadcrumbInfo* prev = nullptr;
};
void DoLocalLookupImpl(
String const& name,
- ContainerDeclRef containerDeclRef,
+ DeclRef<ContainerDecl> containerDeclRef,
LookupRequest const& request,
LookupResult& result,
BreadcrumbInfo* inBreadcrumbs);
@@ -113,7 +113,7 @@ LookupResult refineLookup(LookupResult const& inResult, LookupMask mask)
LookupResult result;
for (auto item : inResult.items)
{
- if (!DeclPassesLookupMask(item.declRef.GetDecl(), mask))
+ if (!DeclPassesLookupMask(item.declRef.getDecl(), mask))
continue;
AddToLookupResult(result, item);
@@ -122,7 +122,7 @@ LookupResult refineLookup(LookupResult const& inResult, LookupMask mask)
}
LookupResultItem CreateLookupResultItem(
- DeclRef declRef,
+ DeclRef<Decl> declRef,
BreadcrumbInfo* breadcrumbInfos)
{
LookupResultItem item;
@@ -167,7 +167,7 @@ void DoMemberLookupImpl(
if (auto baseDeclRefType = baseType->As<DeclRefType>())
{
- if (auto baseAggTypeDeclRef = baseDeclRefType->declRef.As<AggTypeDeclRef>())
+ if (auto baseAggTypeDeclRef = baseDeclRefType->declRef.As<AggTypeDecl>())
{
DoLocalLookupImpl(name, baseAggTypeDeclRef, request, ioResult, breadcrumbs);
}
@@ -178,7 +178,7 @@ void DoMemberLookupImpl(
void DoMemberLookupImpl(
String const& name,
- DeclRef baseDeclRef,
+ DeclRef<Decl> baseDeclRef,
LookupRequest const& request,
LookupResult& ioResult,
BreadcrumbInfo* breadcrumbs)
@@ -190,12 +190,12 @@ void DoMemberLookupImpl(
// Look for members of the given name in the given container for declarations
void DoLocalLookupImpl(
String const& name,
- ContainerDeclRef containerDeclRef,
+ DeclRef<ContainerDecl> containerDeclRef,
LookupRequest const& request,
LookupResult& result,
BreadcrumbInfo* inBreadcrumbs)
{
- ContainerDecl* containerDecl = containerDeclRef.GetDecl();
+ ContainerDecl* containerDecl = containerDeclRef.getDecl();
// Ensure that the lookup dictionary in the container is up to date
if (!containerDecl->memberDictionaryIsValid)
@@ -217,7 +217,7 @@ void DoLocalLookupImpl(
continue;
// The declaration passed the test, so add it!
- AddToLookupResult(result, CreateLookupResultItem(DeclRef(m, containerDeclRef.substitutions), inBreadcrumbs));
+ AddToLookupResult(result, CreateLookupResultItem(DeclRef<Decl>(m, containerDeclRef.substitutions), inBreadcrumbs));
}
@@ -228,7 +228,7 @@ void DoLocalLookupImpl(
{
// The reference to the transparent member should use whatever
// substitutions we used in referring to its outer container
- DeclRef transparentMemberDeclRef(transparentInfo.decl, containerDeclRef.substitutions);
+ DeclRef<Decl> transparentMemberDeclRef(transparentInfo.decl, containerDeclRef.substitutions);
// We need to leave a breadcrumb so that we know that the result
// of lookup involves a member lookup step here
@@ -261,7 +261,7 @@ void DoLookupImpl(
if(!link->containerDecl)
continue;
- ContainerDeclRef containerRef = DeclRef(link->containerDecl, nullptr).As<ContainerDeclRef>();
+ DeclRef<ContainerDecl> containerRef = DeclRef<Decl>(link->containerDecl, nullptr).As<ContainerDecl>();
DoLocalLookupImpl(name, containerRef, request, result, nullptr);
}
@@ -292,7 +292,7 @@ LookupResult LookUp(String const& name, RefPtr<Scope> scope)
// perform lookup within the context of a particular container declaration,
// and do *not* look further up the chain
-LookupResult LookUpLocal(String const& name, ContainerDeclRef containerDeclRef)
+LookupResult LookUpLocal(String const& name, DeclRef<ContainerDecl> containerDeclRef)
{
LookupRequest request;
LookupResult result;
@@ -300,11 +300,4 @@ LookupResult LookUpLocal(String const& name, ContainerDeclRef containerDeclRef)
return result;
}
-LookupResult LookUpLocal(String const& name, ContainerDecl* containerDecl)
-{
- ContainerDeclRef containerRef = DeclRef(containerDecl, nullptr).As<ContainerDeclRef>();
- return LookUpLocal(name, containerRef);
-}
-
-
}
diff --git a/source/slang/lookup.h b/source/slang/lookup.h
index bdf392aec..eb1888878 100644
--- a/source/slang/lookup.h
+++ b/source/slang/lookup.h
@@ -19,8 +19,7 @@ LookupResult LookUp(String const& name, RefPtr<Scope> scope);
// perform lookup within the context of a particular container declaration,
// and do *not* look further up the chain
-LookupResult LookUpLocal(String const& name, ContainerDeclRef containerDeclRef);
-LookupResult LookUpLocal(String const& name, ContainerDecl* containerDecl);
+LookupResult LookUpLocal(String const& name, DeclRef<ContainerDecl> containerDeclRef);
// TODO: this belongs somewhere else
@@ -28,11 +27,11 @@ class SemanticsVisitor;
QualType getTypeForDeclRef(
SemanticsVisitor* sema,
DiagnosticSink* sink,
- DeclRef declRef,
+ DeclRef<Decl> declRef,
RefPtr<ExpressionType>* outTypeResult);
QualType getTypeForDeclRef(
- DeclRef declRef);
+ DeclRef<Decl> declRef);
}
diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp
index 1a1fa4335..0756eb68c 100644
--- a/source/slang/parameter-binding.cpp
+++ b/source/slang/parameter-binding.cpp
@@ -262,10 +262,10 @@ static bool findLayoutArg(
template<typename T>
static bool findLayoutArg(
- DeclRef declRef,
+ DeclRef<Decl> declRef,
int* outVal)
{
- return findLayoutArg<T>(declRef.GetDecl(), outVal);
+ return findLayoutArg<T>(declRef.getDecl(), outVal);
}
//
@@ -437,7 +437,7 @@ static void collectGlobalScopeParameter(
// Now create a variable layout that we can use
RefPtr<VarLayout> varLayout = new VarLayout();
varLayout->typeLayout = typeLayout;
- varLayout->varDecl = DeclRef(varDecl.Ptr(), nullptr).As<VarDeclBaseRef>();
+ varLayout->varDecl = DeclRef<Decl>(varDecl.Ptr(), nullptr).As<VarDeclBase>();
// This declaration may represent the same logical parameter
// as a declaration that came from a different translation unit.
@@ -528,7 +528,7 @@ static void addExplicitParameterBindings_HLSL(
// here is where we want to extract and apply them...
// Look for HLSL `register` or `packoffset` semantics.
- for (auto semantic : varDecl.GetDecl()->GetModifiersOfType<HLSLLayoutSemantic>())
+ for (auto semantic : varDecl.getDecl()->GetModifiersOfType<HLSLLayoutSemantic>())
{
// Need to extract the information encoded in the semantic
LayoutSemanticInfo semanticInfo = ExtractLayoutSemanticInfo(context, semantic);
@@ -916,15 +916,15 @@ static void processEntryPointParameter(
{
auto declRef = declRefType->declRef;
- if (auto structDeclRef = declRef.As<StructDeclRef>())
+ if (auto structDeclRef = declRef.As<StructSyntaxNode>())
{
// Need to recursively walk the fields of the structure now...
- for( auto field : structDeclRef.GetFields() )
+ for( auto field : GetFields(structDeclRef) )
{
processEntryPointParameterWithPossibleSemantic(
context,
- field.GetDecl(),
- field.GetType(),
+ field.getDecl(),
+ GetType(field),
state);
}
}
@@ -1223,7 +1223,7 @@ void GenerateParameterBindings(
for( auto& varLayout : parameterInfo->varLayouts )
{
- globalScopeStructLayout->mapVarToLayout.Add(varLayout->varDecl.GetDecl(), varLayout);
+ globalScopeStructLayout->mapVarToLayout.Add(varLayout->varDecl.getDecl(), varLayout);
}
}
globalScopeRules->EndStructLayout(&structLayoutInfo);
diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp
index df2986959..29b02f816 100644
--- a/source/slang/parser.cpp
+++ b/source/slang/parser.cpp
@@ -754,7 +754,7 @@ namespace Slang
if( lookupResult.isValid() && !lookupResult.isOverloaded() )
{
auto& item = lookupResult.item;
- auto decl = item.declRef.GetDecl();
+ auto decl = item.declRef.getDecl();
if( auto modifierDecl = dynamic_cast<ModifierDecl*>(decl) )
{
@@ -1910,7 +1910,7 @@ namespace Slang
auto paramConstraint = new GenericTypeConstraintDecl();
parser->FillPosition(paramConstraint);
- auto paramType = DeclRefType::Create(DeclRef(paramDecl, nullptr));
+ auto paramType = DeclRefType::Create(DeclRef<Decl>(paramDecl, nullptr));
auto paramTypeExpr = new SharedTypeExpr();
paramTypeExpr->Position = paramDecl->Position;
@@ -2365,7 +2365,7 @@ parser->ReadToken(TokenType::Comma);
if(!lookupResult.isValid() || lookupResult.isOverloaded())
return false;
- auto decl = lookupResult.item.declRef.GetDecl();
+ auto decl = lookupResult.item.declRef.getDecl();
if( auto typeDecl = dynamic_cast<AggTypeDecl*>(decl) )
{
return true;
diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp
index 220e5bcdf..89fd94093 100644
--- a/source/slang/reflection.cpp
+++ b/source/slang/reflection.cpp
@@ -135,7 +135,7 @@ SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* inType)
else if( auto declRefType = type->As<DeclRefType>() )
{
auto declRef = declRefType->declRef;
- if( auto structDeclRef = declRef.As<StructDeclRef>() )
+ if( auto structDeclRef = declRef.As<StructSyntaxNode>() )
{
return SLANG_TYPE_KIND_STRUCT;
}
@@ -155,9 +155,9 @@ SLANG_API unsigned int spReflectionType_GetFieldCount(SlangReflectionType* inTyp
if(auto declRefType = dynamic_cast<DeclRefType*>(type))
{
auto declRef = declRefType->declRef;
- if( auto structDeclRef = declRef.As<StructDeclRef>())
+ if( auto structDeclRef = declRef.As<StructSyntaxNode>())
{
- return structDeclRef.GetFields().Count();
+ return GetFields(structDeclRef).Count();
}
}
@@ -174,10 +174,10 @@ SLANG_API SlangReflectionVariable* spReflectionType_GetFieldByIndex(SlangReflect
if(auto declRefType = dynamic_cast<DeclRefType*>(type))
{
auto declRef = declRefType->declRef;
- if( auto structDeclRef = declRef.As<StructDeclRef>())
+ if( auto structDeclRef = declRef.As<StructSyntaxNode>())
{
- auto fieldDeclRef = structDeclRef.GetFields().ToArray()[index];
- return (SlangReflectionVariable*) fieldDeclRef.GetDecl();
+ auto fieldDeclRef = GetFields(structDeclRef).ToArray()[index];
+ return (SlangReflectionVariable*) fieldDeclRef.getDecl();
}
}
@@ -573,7 +573,7 @@ SLANG_API SlangReflectionVariable* spReflectionVariableLayout_GetVariable(SlangR
auto varLayout = convert(inVarLayout);
if(!varLayout) return nullptr;
- return convert(varLayout->varDecl.GetDecl());
+ return convert(varLayout->varDecl.getDecl());
}
SLANG_API SlangReflectionTypeLayout* spReflectionVariableLayout_GetTypeLayout(SlangReflectionVariableLayout* inVarLayout)
diff --git a/source/slang/syntax.cpp b/source/slang/syntax.cpp
index 19f3939ce..d37f782f6 100644
--- a/source/slang/syntax.cpp
+++ b/source/slang/syntax.cpp
@@ -291,20 +291,11 @@ namespace Slang
{
auto declRefType = AsDeclRefType();
if (!declRefType) return false;
- auto structDeclRef = declRefType->declRef.As<StructDeclRef>();
+ auto structDeclRef = declRefType->declRef.As<StructSyntaxNode>();
if (!structDeclRef) return false;
return true;
}
- bool ExpressionType::IsClass()
- {
- auto declRefType = AsDeclRefType();
- if (!declRefType) return false;
- auto classDeclRef = declRefType->declRef.As<ClassDeclRef>();
- if (!classDeclRef) return false;
- return true;
- }
-
#if 0
RefPtr<ExpressionType> ExpressionType::Bool;
RefPtr<ExpressionType> ExpressionType::UInt;
@@ -405,7 +396,7 @@ namespace Slang
// the case we especially care about is when this type references a declaration
// of a generic parameter, since that is what we might be substituting...
- if (auto genericTypeParamDecl = dynamic_cast<GenericTypeParamDecl*>(declRef.GetDecl()))
+ if (auto genericTypeParamDecl = dynamic_cast<GenericTypeParamDecl*>(declRef.getDecl()))
{
// search for a substitution that might apply to us
for (auto s = subst; s; s = s->outer.Ptr())
@@ -443,7 +434,7 @@ namespace Slang
int diff = 0;
- DeclRef substDeclRef = declRef.SubstituteImpl(subst, &diff);
+ DeclRef<Decl> substDeclRef = declRef.SubstituteImpl(subst, &diff);
if (!diff)
return this;
@@ -471,15 +462,15 @@ namespace Slang
// TODO: need to figure out how to unify this with the logic
// in the generic case...
- DeclRefType* DeclRefType::Create(DeclRef declRef)
+ DeclRefType* DeclRefType::Create(DeclRef<Decl> declRef)
{
- if (auto builtinMod = declRef.GetDecl()->FindModifier<BuiltinTypeModifier>())
+ if (auto builtinMod = declRef.getDecl()->FindModifier<BuiltinTypeModifier>())
{
auto type = new BasicExpressionType(builtinMod->tag);
type->declRef = declRef;
return type;
}
- else if (auto magicMod = declRef.GetDecl()->FindModifier<MagicTypeModifier>())
+ else if (auto magicMod = declRef.getDecl()->FindModifier<MagicTypeModifier>())
{
Substitutions* subst = declRef.substitutions.Ptr();
@@ -679,7 +670,7 @@ namespace Slang
ExpressionType* NamedExpressionType::CreateCanonicalType()
{
- return declRef.GetType()->GetCanonicalType();
+ return GetType(declRef)->GetCanonicalType();
}
int NamedExpressionType::GetHashCode()
@@ -754,7 +745,7 @@ namespace Slang
String GenericDeclRefType::ToString()
{
// TODO: what is appropriate here?
- return "<GenericDeclRef>";
+ return "<DeclRef<GenericDecl>>";
}
bool GenericDeclRefType::EqualsImpl(ExpressionType * type)
@@ -1034,13 +1025,13 @@ namespace Slang
// the generic decl associated with the substitution list must be
// the generic decl that declared this parameter
auto genericDecl = s->genericDecl;
- if (genericDecl != declRef.GetDecl()->ParentDecl)
+ if (genericDecl != declRef.getDecl()->ParentDecl)
continue;
int index = 0;
for (auto m : genericDecl->Members)
{
- if (m.Ptr() == declRef.GetDecl())
+ if (m.Ptr() == declRef.getDecl())
{
// We've found it, so return the corresponding specialization argument
(*ioDiff)++;
@@ -1144,9 +1135,9 @@ namespace Slang
}
- // DeclRef
+ // DeclRefBase
- RefPtr<ExpressionType> DeclRef::Substitute(RefPtr<ExpressionType> type) const
+ RefPtr<ExpressionType> DeclRefBase::Substitute(RefPtr<ExpressionType> type) const
{
// No substitutions? Easy.
if (!substitutions)
@@ -1158,7 +1149,7 @@ namespace Slang
return type->Substitute(substitutions.Ptr()).As<ExpressionType>();
}
- DeclRef DeclRef::Substitute(DeclRef declRef) const
+ DeclRefBase DeclRefBase::Substitute(DeclRefBase declRef) const
{
if(!substitutions)
return declRef;
@@ -1167,7 +1158,7 @@ namespace Slang
return declRef.SubstituteImpl(substitutions.Ptr(), &diff);
}
- RefPtr<ExpressionSyntaxNode> DeclRef::Substitute(RefPtr<ExpressionSyntaxNode> expr) const
+ RefPtr<ExpressionSyntaxNode> DeclRefBase::Substitute(RefPtr<ExpressionSyntaxNode> expr) const
{
// No substitutions? Easy.
if (!substitutions)
@@ -1179,7 +1170,7 @@ namespace Slang
}
- DeclRef DeclRef::SubstituteImpl(Substitutions* subst, int* ioDiff)
+ DeclRefBase DeclRefBase::SubstituteImpl(Substitutions* subst, int* ioDiff)
{
if (!substitutions) return *this;
@@ -1191,7 +1182,7 @@ namespace Slang
*ioDiff += diff;
- DeclRef substDeclRef;
+ DeclRefBase substDeclRef;
substDeclRef.decl = decl;
substDeclRef.substitutions = substSubst;
return substDeclRef;
@@ -1199,7 +1190,7 @@ namespace Slang
// Check if this is an equivalent declaration reference to another
- bool DeclRef::Equals(DeclRef const& declRef) const
+ bool DeclRefBase::Equals(DeclRefBase const& declRef) const
{
if (decl != declRef.decl)
return false;
@@ -1211,30 +1202,30 @@ namespace Slang
}
// Convenience accessors for common properties of declarations
- String const& DeclRef::GetName() const
+ String const& DeclRefBase::GetName() const
{
return decl->Name.Content;
}
- DeclRef DeclRef::GetParent() const
+ DeclRefBase DeclRefBase::GetParent() const
{
auto parentDecl = decl->ParentDecl;
if (auto parentGeneric = dynamic_cast<GenericDecl*>(parentDecl))
{
// We need to strip away one layer of specialization
assert(substitutions);
- return DeclRef(parentGeneric, substitutions->outer);
+ return DeclRefBase(parentGeneric, substitutions->outer);
}
else
{
// If the parent isn't a generic, then it must
// use the same specializations as this declaration
- return DeclRef(parentDecl, substitutions);
+ return DeclRefBase(parentDecl, substitutions);
}
}
- int DeclRef::GetHashCode() const
+ int DeclRefBase::GetHashCode() const
{
auto rs = PointerHash<1>::GetHashCode(decl);
if (substitutions)
@@ -1365,7 +1356,7 @@ namespace Slang
RefPtr<Decl> decl,
RefPtr<BuiltinTypeModifier> modifier)
{
- auto type = DeclRefType::Create(DeclRef(decl.Ptr(), nullptr));
+ auto type = DeclRefType::Create(DeclRef<Decl>(decl.Ptr(), nullptr));
ExpressionType::sBuiltinTypes[(int)modifier->tag] = type;
}
diff --git a/source/slang/syntax.h b/source/slang/syntax.h
index 96eb3d530..9f93c305c 100644
--- a/source/slang/syntax.h
+++ b/source/slang/syntax.h
@@ -700,73 +700,123 @@ namespace Slang
// A reference to a declaration, which may include
// substitutions for generic parameters.
- struct DeclRef
+ struct DeclRefBase
{
typedef Decl DeclType;
// The underlying declaration
Decl* decl = nullptr;
- Decl* GetDecl() const { return decl; }
+ Decl* getDecl() const { return decl; }
// Optionally, a chain of substititions to perform
RefPtr<Substitutions> substitutions;
- DeclRef()
+ DeclRefBase()
{}
- DeclRef(Decl* decl, RefPtr<Substitutions> substitutions)
+ DeclRefBase(Decl* decl, RefPtr<Substitutions> substitutions)
: decl(decl)
, substitutions(substitutions)
{}
// Apply substitutions to a type or ddeclaration
RefPtr<ExpressionType> Substitute(RefPtr<ExpressionType> type) const;
- DeclRef Substitute(DeclRef declRef) const;
+
+ DeclRefBase Substitute(DeclRefBase declRef) const;
// Apply substitutions to an expression
RefPtr<ExpressionSyntaxNode> Substitute(RefPtr<ExpressionSyntaxNode> expr) const;
// Apply substitutions to this declaration reference
- DeclRef SubstituteImpl(Substitutions* subst, int* ioDiff);
+ DeclRefBase SubstituteImpl(Substitutions* subst, int* ioDiff);
// Check if this is an equivalent declaration reference to another
- bool Equals(DeclRef const& declRef) const;
- bool operator == (const DeclRef& other) const
+ bool Equals(DeclRefBase const& declRef) const;
+ bool operator == (const DeclRefBase& other) const
{
return Equals(other);
}
// Convenience accessors for common properties of declarations
String const& GetName() const;
- DeclRef GetParent() const;
+ DeclRefBase GetParent() const;
+
+ int GetHashCode() const;
+ };
+
+ template<typename T>
+ struct DeclRef : DeclRefBase
+ {
+ typedef T DeclType;
+
+ DeclRef()
+ {}
+
+ DeclRef(T* decl, RefPtr<Substitutions> substitutions)
+ : DeclRefBase(decl, substitutions)
+ {}
+
+ template <typename U>
+ DeclRef(DeclRef<U> const& other,
+ typename EnableIf<IsConvertible<T*, U*>::Value, void>::type* = 0)
+ : DeclRefBase(other.decl, other.substitutions)
+ {
+ }
// "dynamic cast" to a more specific declaration reference type
template<typename T>
- T As() const
+ DeclRef<T> As() const
{
- T result;
- result.decl = dynamic_cast<T::DeclType*>(decl);
+ DeclRef<T> result;
+ result.decl = dynamic_cast<T*>(decl);
result.substitutions = substitutions;
return result;
}
- // Implicit conversion mostly so we can use a `DeclRef`
- // in a conditional context
- operator Decl*() const
+ T* getDecl() const
{
- return decl;
+ return (T*)decl;
}
- int GetHashCode() const;
- };
+ operator T*() const
+ {
+ return getDecl();
+ }
- // Helper macro for defining `DeclRef` subtypes
- #define SLANG_DECLARE_DECL_REF(D) \
- typedef D DeclType; \
- D* GetDecl() const { return (D*) decl; } \
- /* */
+ //
+ static DeclRef<T> unsafeInit(DeclRefBase const& declRef)
+ {
+ return DeclRef<T>((T*) declRef.decl, declRef.substitutions);
+ }
+ RefPtr<ExpressionType> Substitute(RefPtr<ExpressionType> type) const
+ {
+ return DeclRefBase::Substitute(type);
+ }
+ RefPtr<ExpressionSyntaxNode> Substitute(RefPtr<ExpressionSyntaxNode> expr) const
+ {
+ return DeclRefBase::Substitute(expr);
+ }
+ // Apply substitutions to a type or ddeclaration
+ template<typename U>
+ DeclRef<U> Substitute(DeclRef<U> declRef) const
+ {
+ return DeclRef<U>::unsafeInit(DeclRefBase::Substitute(declRef));
+ }
+
+ // Apply substitutions to this declaration reference
+ DeclRef<T> SubstituteImpl(Substitutions* subst, int* ioDiff)
+ {
+ return DeclRef<T>::unsafeInit(DeclRefBase::SubstituteImpl(subst, ioDiff));
+ }
+
+ DeclRef<ContainerDecl> GetParent() const
+ {
+ return DeclRef<ContainerDecl>::unsafeInit(DeclRefBase::GetParent());
+ }
+
+ };
// The type of a reference to an overloaded name
class OverloadGroupType : public ExpressionType
@@ -809,17 +859,17 @@ namespace Slang
class DeclRefType : public ExpressionType
{
public:
- DeclRef declRef;
+ DeclRef<Decl> declRef;
virtual String ToString() override;
virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff) override;
- static DeclRefType* Create(DeclRef declRef);
+ static DeclRefType* Create(DeclRef<Decl> declRef);
protected:
DeclRefType()
{}
- DeclRefType(DeclRef declRef)
+ DeclRefType(DeclRef<Decl> declRef)
: declRef(declRef)
{}
virtual int GetHashCode() override;
@@ -1246,7 +1296,7 @@ namespace Slang
List<RefPtr<Decl>> Members;
template<typename T>
- FilteredMemberList<T> GetMembersOfType()
+ FilteredMemberList<T> getMembersOfType()
{
return FilteredMemberList<T>(Members);
}
@@ -1286,9 +1336,9 @@ namespace Slang
return count;
}
- List<T> ToArray() const
+ List<DeclRef<T>> ToArray() const
{
- List<T> result;
+ List<DeclRef<T>> result;
for (auto d : *this)
result.Add(d);
return result;
@@ -1320,9 +1370,9 @@ namespace Slang
ptr = list->Adjust(ptr + 1, end);
}
- T operator*()
+ DeclRef<T> operator*()
{
- return DeclRef(ptr->Ptr(), list->substitutions).As<T>();
+ return DeclRef<T>((T*) ptr->Ptr(), list->substitutions);
}
};
@@ -1333,7 +1383,7 @@ namespace Slang
{
while (ptr != end)
{
- DeclRef declRef(ptr->Ptr(), substitutions);
+ DeclRef<Decl> declRef(ptr->Ptr(), substitutions);
if (declRef.As<T>())
return ptr;
ptr++;
@@ -1342,22 +1392,16 @@ namespace Slang
}
};
- struct ContainerDeclRef : DeclRef
+ inline FilteredMemberRefList<Decl> getMembers(DeclRef<ContainerDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(ContainerDecl);
-
- FilteredMemberRefList<DeclRef> GetMembers() const
- {
- return FilteredMemberRefList<DeclRef>(GetDecl()->Members, substitutions);
- }
-
- template<typename T>
- FilteredMemberRefList<T> GetMembersOfType() const
- {
- return FilteredMemberRefList<T>(GetDecl()->Members, substitutions);
- }
+ return FilteredMemberRefList<Decl>(declRef.getDecl()->Members, declRef.substitutions);
+ }
- };
+ template<typename T>
+ inline FilteredMemberRefList<T> getMembersOfType(DeclRef<ContainerDecl> const& declRef)
+ {
+ return FilteredMemberRefList<T>(declRef.getDecl()->Members, declRef.substitutions);
+ }
//
// Type Expressions
@@ -1417,14 +1461,15 @@ namespace Slang
RefPtr<ExpressionSyntaxNode> Expr;
};
- struct VarDeclBaseRef : DeclRef
+ inline RefPtr<ExpressionType> GetType(DeclRef<VarDeclBase> const& declRef)
{
- SLANG_DECLARE_DECL_REF(VarDeclBase);
-
- RefPtr<ExpressionType> GetType() const { return Substitute(GetDecl()->Type); }
+ return declRef.Substitute(declRef.getDecl()->Type);
+ }
- RefPtr<ExpressionSyntaxNode> getInitExpr() const { return Substitute(GetDecl()->Expr); }
- };
+ inline RefPtr<ExpressionSyntaxNode> getInitExpr(DeclRef<VarDeclBase> const& declRef)
+ {
+ return declRef.Substitute(declRef.getDecl()->Expr);
+ }
// A field of a `struct` type
class StructField : public VarDeclBase
@@ -1435,11 +1480,6 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct FieldDeclRef : VarDeclBaseRef
- {
- SLANG_DECLARE_DECL_REF(StructField)
- };
-
// An `AggTypeDeclBase` captures the shared functionality
// between true aggregate type declarations and extension
// declarations:
@@ -1453,11 +1493,6 @@ namespace Slang
public:
};
- struct AggTypeDeclBaseRef : ContainerDeclRef
- {
- SLANG_DECLARE_DECL_REF(AggTypeDeclBase);
- };
-
// An extension to apply to an existing type
class ExtensionDecl : public AggTypeDeclBase
{
@@ -1471,12 +1506,11 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct ExtensionDeclRef : AggTypeDeclBaseRef
- {
- SLANG_DECLARE_DECL_REF(ExtensionDecl);
- RefPtr<ExpressionType> GetTargetType() const { return Substitute(GetDecl()->targetType); }
- };
+ inline RefPtr<ExpressionType> GetTargetType(DeclRef<ExtensionDecl> const& declRef)
+ {
+ return declRef.Substitute(declRef.getDecl()->targetType);
+ }
// Declaration of a type that represents some sort of aggregate
class AggTypeDecl : public AggTypeDeclBase
@@ -1487,7 +1521,7 @@ namespace Slang
ExtensionDecl* candidateExtensions = nullptr;
FilteredMemberList<StructField> GetFields()
{
- return GetMembersOfType<StructField>();
+ return getMembersOfType<StructField>();
}
StructField* FindField(String name)
{
@@ -1511,12 +1545,10 @@ namespace Slang
}
};
- struct AggTypeDeclRef : public AggTypeDeclBaseRef
+ inline ExtensionDecl* GetCandidateExtensions(DeclRef<AggTypeDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(AggTypeDecl);
-
- ExtensionDecl* GetCandidateExtensions() const { return GetDecl()->candidateExtensions; }
- };
+ return declRef.getDecl()->candidateExtensions;
+ }
class StructSyntaxNode : public AggTypeDecl
{
@@ -1524,12 +1556,10 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct StructDeclRef : public AggTypeDeclRef
+ inline FilteredMemberRefList<StructField> GetFields(DeclRef<StructSyntaxNode> const& declRef)
{
- SLANG_DECLARE_DECL_REF(StructSyntaxNode);
-
- FilteredMemberRefList<FieldDeclRef> GetFields() const { return GetMembersOfType<FieldDeclRef>(); }
- };
+ return getMembersOfType<StructField>(declRef);
+ }
class ClassSyntaxNode : public AggTypeDecl
{
@@ -1537,13 +1567,6 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct ClassDeclRef : public AggTypeDeclRef
- {
- SLANG_DECLARE_DECL_REF(ClassSyntaxNode);
-
- FilteredMemberRefList<FieldDeclRef> GetFields() const { return GetMembersOfType<FieldDeclRef>(); }
- };
-
// An interface which other types can conform to
class InterfaceDecl : public AggTypeDecl
{
@@ -1551,12 +1574,6 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct InterfaceDeclRef : public AggTypeDeclRef
- {
- SLANG_DECLARE_DECL_REF(InterfaceDecl);
- };
-
-
// A kind of pseudo-member that represents an explicit
// or implicit inheritance relationship.
//
@@ -1569,12 +1586,10 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct InheritanceDeclRef : public DeclRef
+ inline RefPtr<ExpressionType> getBaseType(DeclRef<InheritanceDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(InheritanceDecl);
-
- RefPtr<ExpressionType> getBaseType() { return Substitute(GetDecl()->base.type); }
- };
+ return declRef.Substitute(declRef.getDecl()->base.type);
+ }
// TODO: may eventually need sub-classes for explicit/direct vs. implicit/indirect inheritance
@@ -1584,11 +1599,6 @@ namespace Slang
{
};
- struct SimpleTypeDeclRef : DeclRef
- {
- SLANG_DECLARE_DECL_REF(SimpleTypeDecl)
- };
-
// A `typedef` declaration
class TypeDefDecl : public SimpleTypeDecl
{
@@ -1598,22 +1608,20 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct TypeDefDeclRef : SimpleTypeDeclRef
+ inline RefPtr<ExpressionType> GetType(DeclRef<TypeDefDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(TypeDefDecl);
-
- RefPtr<ExpressionType> GetType() const { return Substitute(GetDecl()->Type); }
- };
+ return declRef.Substitute(declRef.getDecl()->Type);
+ }
// A type alias of some kind (e.g., via `typedef`)
class NamedExpressionType : public ExpressionType
{
public:
- NamedExpressionType(TypeDefDeclRef declRef)
+ NamedExpressionType(DeclRef<TypeDefDecl> declRef)
: declRef(declRef)
{}
- TypeDefDeclRef declRef;
+ DeclRef<TypeDefDecl> declRef;
virtual String ToString() override;
@@ -1664,36 +1672,26 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct ParamDeclRef : VarDeclBaseRef
- {
- SLANG_DECLARE_DECL_REF(ParameterSyntaxNode);
- };
-
// Base class for things that have parameter lists and can thus be applied to arguments ("called")
class CallableDecl : public ContainerDecl
{
public:
FilteredMemberList<ParameterSyntaxNode> GetParameters()
{
- return GetMembersOfType<ParameterSyntaxNode>();
+ return getMembersOfType<ParameterSyntaxNode>();
}
TypeExp ReturnType;
};
- struct CallableDeclRef : ContainerDeclRef
+ inline RefPtr<ExpressionType> GetResultType(DeclRef<CallableDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(CallableDecl);
-
- RefPtr<ExpressionType> GetResultType() const
- {
- return Substitute(GetDecl()->ReturnType.type.Ptr());
- }
+ return declRef.Substitute(declRef.getDecl()->ReturnType.type.Ptr());
+ }
- FilteredMemberRefList<ParamDeclRef> GetParameters()
- {
- return GetMembersOfType<ParamDeclRef>();
- }
- };
+ inline FilteredMemberRefList<ParameterSyntaxNode> GetParameters(DeclRef<CallableDecl> const& declRef)
+ {
+ return getMembersOfType<ParameterSyntaxNode>(declRef);
+ }
// Base class for callable things that may also have a body that is evaluated to produce their result
class FunctionDeclBase : public CallableDecl
@@ -1702,11 +1700,6 @@ namespace Slang
RefPtr<StatementSyntaxNode> Body;
};
- struct FuncDeclBaseRef : CallableDeclRef
- {
- SLANG_DECLARE_DECL_REF(FunctionDeclBase);
- };
-
// Function types are currently used for references to symbols that name
// either ordinary functions, or "component functions."
// We do not directly store a representation of the type, and instead
@@ -1714,7 +1707,7 @@ namespace Slang
class FuncType : public ExpressionType
{
public:
- CallableDeclRef declRef;
+ DeclRef<CallableDecl> declRef;
virtual String ToString() override;
protected:
@@ -1730,11 +1723,6 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct ConstructorDeclRef : FuncDeclBaseRef
- {
- SLANG_DECLARE_DECL_REF(ConstructorDecl);
- };
-
// A subscript operation used to index instances of a type
class SubscriptDecl : public CallableDecl
{
@@ -1742,11 +1730,6 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct SubscriptDeclRef : CallableDeclRef
- {
- SLANG_DECLARE_DECL_REF(SubscriptDecl);
- };
-
// An "accessor" for a subscript or property
class AccessorDecl : public FunctionDeclBase
{
@@ -1776,12 +1759,6 @@ namespace Slang
}
};
- struct FuncDeclRef : FuncDeclBaseRef
- {
- SLANG_DECLARE_DECL_REF(FunctionSyntaxNode);
- };
-
-
struct Scope : public RefObject
{
// The parent of this scope (where lookup should go if nothing is found locally)
@@ -1806,7 +1783,7 @@ namespace Slang
RefPtr<Scope> scope;
// The declaration of the symbol being referenced
- DeclRef declRef;
+ DeclRef<Decl> declRef;
};
class VarExpressionSyntaxNode : public DeclRefExpr
@@ -1848,10 +1825,10 @@ namespace Slang
};
Kind kind;
- DeclRef declRef;
+ DeclRef<Decl> declRef;
RefPtr<Breadcrumb> next;
- Breadcrumb(Kind kind, DeclRef declRef, RefPtr<Breadcrumb> next)
+ Breadcrumb(Kind kind, DeclRef<Decl> declRef, RefPtr<Breadcrumb> next)
: kind(kind)
, declRef(declRef)
, next(next)
@@ -1859,7 +1836,7 @@ namespace Slang
};
// A properly-specialized reference to the declaration that was found.
- DeclRef declRef;
+ DeclRef<Decl> declRef;
// Any breadcrumbs needed in order to turn that declaration
// reference into a well-formed expression.
@@ -1870,10 +1847,10 @@ namespace Slang
RefPtr<Breadcrumb> breadcrumbs;
LookupResultItem() = default;
- explicit LookupResultItem(DeclRef declRef)
+ explicit LookupResultItem(DeclRef<Decl> declRef)
: declRef(declRef)
{}
- LookupResultItem(DeclRef declRef, RefPtr<Breadcrumb> breadcrumbs)
+ LookupResultItem(DeclRef<Decl> declRef, RefPtr<Breadcrumb> breadcrumbs)
: declRef(declRef)
, breadcrumbs(breadcrumbs)
{}
@@ -1894,7 +1871,7 @@ namespace Slang
List<LookupResultItem> items;
// Was at least one result found?
- bool isValid() const { return item.declRef.GetDecl() != nullptr; }
+ bool isValid() const { return item.declRef.getDecl() != nullptr; }
bool isOverloaded() const { return items.Count() > 1; }
};
@@ -2076,20 +2053,20 @@ namespace Slang
// Access members of specific types
FilteredMemberList<FunctionSyntaxNode> GetFunctions()
{
- return GetMembersOfType<FunctionSyntaxNode>();
+ return getMembersOfType<FunctionSyntaxNode>();
}
FilteredMemberList<ClassSyntaxNode> GetClasses()
{
- return GetMembersOfType<ClassSyntaxNode>();
+ return getMembersOfType<ClassSyntaxNode>();
}
FilteredMemberList<StructSyntaxNode> GetStructs()
{
- return GetMembersOfType<StructSyntaxNode>();
+ return getMembersOfType<StructSyntaxNode>();
}
FilteredMemberList<TypeDefDecl> GetTypeDefs()
{
- return GetMembersOfType<TypeDefDecl>();
+ return getMembersOfType<TypeDefDecl>();
}
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
@@ -2372,23 +2349,23 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct GenericDeclRef : ContainerDeclRef
+ inline Decl* GetInner(DeclRef<GenericDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(GenericDecl);
-
- Decl* GetInner() const { return GetDecl()->inner.Ptr(); }
- };
+ // TODO: Should really return a `DeclRef<Decl>` for the inner
+ // declaration, and not just a raw pointer
+ return declRef.getDecl()->inner.Ptr();
+ }
// The "type" of an expression that names a generic declaration.
class GenericDeclRefType : public ExpressionType
{
public:
- GenericDeclRefType(GenericDeclRef declRef)
+ GenericDeclRefType(DeclRef<GenericDecl> declRef)
: declRef(declRef)
{}
- GenericDeclRef declRef;
- GenericDeclRef const& GetDeclRef() const { return declRef; }
+ DeclRef<GenericDecl> declRef;
+ DeclRef<GenericDecl> const& GetDeclRef() const { return declRef; }
virtual String ToString() override;
@@ -2413,11 +2390,6 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct GenericTypeParamDeclRef : SimpleTypeDeclRef
- {
- SLANG_DECLARE_DECL_REF(GenericTypeParamDecl);
- };
-
// A constraint placed as part of a generic declaration
class GenericTypeConstraintDecl : public Decl
{
@@ -2432,14 +2404,15 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct GenericTypeConstraintDeclRef : DeclRef
+ inline RefPtr<ExpressionType> GetSub(DeclRef<GenericTypeConstraintDecl> const& declRef)
{
- SLANG_DECLARE_DECL_REF(GenericTypeConstraintDecl);
-
- RefPtr<ExpressionType> GetSub() { return Substitute(GetDecl()->sub); }
- RefPtr<ExpressionType> GetSup() { return Substitute(GetDecl()->sup); }
- };
+ return declRef.Substitute(declRef.getDecl()->sub);
+ }
+ inline RefPtr<ExpressionType> GetSup(DeclRef<GenericTypeConstraintDecl> const& declRef)
+ {
+ return declRef.Substitute(declRef.getDecl()->sup);
+ }
class GenericValueParamDecl : public VarDeclBase
{
@@ -2447,18 +2420,13 @@ namespace Slang
virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
};
- struct GenericValueParamDeclRef : VarDeclBaseRef
- {
- SLANG_DECLARE_DECL_REF(GenericValueParamDecl);
- };
-
// The logical "value" of a rererence to a generic value parameter
class GenericParamIntVal : public IntVal
{
public:
- VarDeclBaseRef declRef;
+ DeclRef<VarDeclBase> declRef;
- GenericParamIntVal(VarDeclBaseRef declRef)
+ GenericParamIntVal(DeclRef<VarDeclBase> declRef)
: declRef(declRef)
{}
diff --git a/source/slang/type-layout.cpp b/source/slang/type-layout.cpp
index 8b390b718..3427d6a6f 100644
--- a/source/slang/type-layout.cpp
+++ b/source/slang/type-layout.cpp
@@ -1009,7 +1009,7 @@ SimpleLayoutInfo GetLayoutImpl(
{
auto declRef = declRefType->declRef;
- if (auto structDeclRef = declRef.As<StructDeclRef>())
+ if (auto structDeclRef = declRef.As<StructSyntaxNode>())
{
RefPtr<StructTypeLayout> typeLayout;
if (outTypeLayout)
@@ -1022,11 +1022,11 @@ SimpleLayoutInfo GetLayoutImpl(
UniformLayoutInfo info = rules->BeginStructLayout();
- for (auto field : structDeclRef.GetFields())
+ for (auto field : GetFields(structDeclRef))
{
RefPtr<TypeLayout> fieldTypeLayout;
UniformLayoutInfo fieldInfo = GetLayoutImpl(
- field.GetType().Ptr(),
+ GetType(field).Ptr(),
rules,
outTypeLayout ? &fieldTypeLayout : nullptr).getUniformLayout();
@@ -1053,7 +1053,7 @@ SimpleLayoutInfo GetLayoutImpl(
fieldLayout->varDecl = field;
fieldLayout->typeLayout = fieldTypeLayout;
typeLayout->fields.Add(fieldLayout);
- typeLayout->mapVarToLayout.Add(field.GetDecl(), fieldLayout);
+ typeLayout->mapVarToLayout.Add(field.getDecl(), fieldLayout);
// Set up uniform offset information, if there is any uniform data in the field
if( fieldTypeLayout->FindResourceInfo(LayoutResourceKind::Uniform) )
diff --git a/source/slang/type-layout.h b/source/slang/type-layout.h
index df937c1d4..07867fddb 100644
--- a/source/slang/type-layout.h
+++ b/source/slang/type-layout.h
@@ -219,8 +219,8 @@ class VarLayout : public RefObject
{
public:
// The variable we are laying out
- VarDeclBaseRef varDecl;
- VarDeclBase* getVariable() { return varDecl.GetDecl(); }
+ DeclRef<VarDeclBase> varDecl;
+ VarDeclBase* getVariable() { return varDecl.getDecl(); }
String const& getName() { return getVariable()->getName(); }