From e0389f5a1f32cb611e5a595a5974ee1d5c15f43d Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 15 Jun 2017 16:35:10 -0700 Subject: 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` 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` for some specific `T` as a parameter. --- source/slang/check.cpp | 306 ++++++++++++++++----------------- source/slang/emit.cpp | 40 ++--- source/slang/lookup.cpp | 31 ++-- source/slang/lookup.h | 7 +- source/slang/parameter-binding.cpp | 18 +- source/slang/parser.cpp | 6 +- source/slang/reflection.cpp | 14 +- source/slang/syntax.cpp | 55 +++--- source/slang/syntax.h | 338 +++++++++++++++++-------------------- source/slang/type-layout.cpp | 8 +- source/slang/type-layout.h | 4 +- 11 files changed, 389 insertions(+), 438 deletions(-) (limited to 'source') 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 ConstructDeclRefExpr( - DeclRef declRef, - RefPtr baseExpr, - RefPtr originalExpr) + DeclRef declRef, + RefPtr baseExpr, + RefPtr originalExpr) { if (baseExpr) { @@ -302,11 +302,11 @@ namespace Slang // The arguments should already be checked against // the declaration. RefPtr InstantiateGenericType( - GenericDeclRef genericDeclRef, + DeclRef genericDeclRef, List> const& args) { RefPtr 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 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> args; - for (RefPtr member : genericDeclRef.GetDecl()->Members) + for (RefPtr member : genericDeclRef.getDecl()->Members) { if (auto typeParam = member.As()) { @@ -685,7 +685,7 @@ namespace Slang if(auto toDeclRefType = toType->As()) { auto toTypeDeclRef = toDeclRefType->declRef; - if(auto toStructDeclRef = toTypeDeclRef.As()) + if(auto toStructDeclRef = toTypeDeclRef.As()) { // 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()) + for(auto fieldDeclRef : getMembersOfType(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()) { auto toTypeDeclRef = toDeclRefType->declRef; - if (auto interfaceDeclRef = toTypeDeclRef.As()) + if (auto interfaceDeclRef = toTypeDeclRef.As()) { // Trying to convert to an interface type. // @@ -1085,7 +1085,7 @@ namespace Slang if(auto declRefType = base.type->As()) { - if(auto interfaceDeclRef = declRefType->declRef.As()) + if(auto interfaceDeclRef = declRefType->declRef.As()) { 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()) + for (auto & g : program->getMembersOfType()) { VisitGenericDecl(g.Ptr()); } @@ -1912,7 +1912,7 @@ namespace Slang if (!funcDeclRefExpr) return nullptr; auto funcDeclRef = funcDeclRefExpr->declRef; - auto intrinsicMod = funcDeclRef.GetDecl()->FindModifier(); + auto intrinsicMod = funcDeclRef.getDecl()->FindModifier(); 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()) + if (auto genericValParamRef = declRef.As()) { // 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()) + if(auto varRef = declRef.As()) { - auto varDecl = varRef.GetDecl(); + auto varDecl = varRef.getDecl(); switch(sourceLanguage) { @@ -2046,7 +2046,7 @@ namespace Slang if(auto constAttr = varDecl->FindModifier()) { // 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(vectorTypeDecl.Ptr(), substitutions); return DeclRefType::Create(declRef)->As(); } @@ -2257,10 +2257,10 @@ namespace Slang if (auto declRefType = baseType->AsDeclRefType()) { - if (auto aggTypeDeclRef = declRefType->declRef.As()) + if (auto aggTypeDeclRef = declRefType->declRef.As()) { // 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 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()) { // Attach our extension to that type as a candidate... - if (auto aggTypeDeclRef = targetDeclRefType->declRef.As()) + if (auto aggTypeDeclRef = targetDeclRefType->declRef.As()) { - auto aggTypeDecl = aggTypeDeclRef.GetDecl(); + auto aggTypeDecl = aggTypeDeclRef.getDecl(); decl->nextCandidateExtension = aggTypeDecl->candidateExtensions; aggTypeDecl->candidateExtensions = decl; } @@ -2512,18 +2512,18 @@ namespace Slang bool DoesTypeConformToInterface( RefPtr type, - InterfaceDeclRef interfaceDeclRef) + DeclRef interfaceDeclRef) { // for now look up a conformance member... if(auto declRefType = type->As()) { - if( auto aggTypeDeclRef = declRefType->declRef.As() ) + if( auto aggTypeDeclRef = declRefType->declRef.As() ) { - for( auto inheritanceDeclRef : aggTypeDeclRef.GetMembersOfType()) + for( auto inheritanceDeclRef : getMembersOfType(aggTypeDeclRef)) { - EnsureDecl(inheritanceDeclRef.GetDecl()); + EnsureDecl(inheritanceDeclRef.getDecl()); - auto inheritedDeclRefType = inheritanceDeclRef.getBaseType()->As(); + auto inheritedDeclRefType = getBaseType(inheritanceDeclRef)->As(); if (!inheritedDeclRefType) continue; @@ -2539,7 +2539,7 @@ namespace Slang RefPtr TryJoinTypeWithInterface( RefPtr type, - InterfaceDeclRef interfaceDeclRef) + DeclRef 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()) { - if( auto leftInterfaceRef = leftDeclRefType->declRef.As() ) + if( auto leftInterfaceRef = leftDeclRefType->declRef.As() ) { // return TryJoinTypeWithInterface(right, leftInterfaceRef); @@ -2633,7 +2633,7 @@ namespace Slang } if(auto rightDeclRefType = right->As()) { - if( auto rightInterfaceRef = rightDeclRefType->declRef.As() ) + if( auto rightInterfaceRef = rightDeclRefType->declRef.As() ) { // return TryJoinTypeWithInterface(left, rightInterfaceRef); @@ -2655,14 +2655,14 @@ namespace Slang // we solved for along the way. RefPtr TrySolveConstraintSystem( ConstraintSystem* system, - GenericDeclRef genericDeclRef) + DeclRef 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() ) + for( auto constraintDeclRef : getMembersOfType(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> args; - for (auto m : genericDeclRef.GetMembers()) + for (auto m : getMembers(genericDeclRef)) { - if (auto typeParam = m.As()) + if (auto typeParam = m.As()) { RefPtr type = nullptr; for (auto& c : system->constraints) { - if (c.decl != typeParam.GetDecl()) + if (c.decl != typeParam.getDecl()) continue; auto cType = c.val.As(); @@ -2708,7 +2708,7 @@ namespace Slang } args.Add(type); } - else if (auto valParam = m.As()) + else if (auto valParam = m.As()) { // 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 val = nullptr; for (auto& c : system->constraints) { - if (c.decl != valParam.GetDecl()) + if (c.decl != valParam.getDecl()) continue; auto cVal = c.val.As(); @@ -2764,7 +2764,7 @@ namespace Slang // Consruct a reference to the extension with our constraint variables // as the RefPtr 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 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(); @@ -2815,7 +2815,7 @@ namespace Slang RefPtr 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(); @@ -2944,7 +2944,7 @@ namespace Slang }; // count the number of parameters required/allowed for a callable - ParamCounts CountParameters(FilteredMemberRefList params) + ParamCounts CountParameters(FilteredMemberRefList 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 genericRef) { ParamCounts counts = { 0, 0 }; - for (auto m : genericRef.GetDecl()->Members) + for (auto m : genericRef.getDecl()->Members) { if (auto typeParam = m.As()) { @@ -3004,11 +3004,11 @@ namespace Slang switch (candidate.flavor) { case OverloadCandidate::Flavor::Func: - paramCounts = CountParameters(candidate.item.declRef.As().GetParameters()); + paramCounts = CountParameters(GetParameters(candidate.item.declRef.As())); break; case OverloadCandidate::Flavor::Generic: - paramCounts = CountParameters(candidate.item.declRef.As()); + paramCounts = CountParameters(candidate.item.declRef.As()); break; default: @@ -3084,12 +3084,12 @@ namespace Slang { auto& args = context.appExpr->Arguments; - auto genericDeclRef = candidate.item.declRef.As(); + auto genericDeclRef = candidate.item.declRef.As(); int aa = 0; - for (auto memberRef : genericDeclRef.GetMembers()) + for (auto memberRef : getMembers(genericDeclRef)) { - if (auto typeParamRef = memberRef.As()) + if (auto typeParamRef = memberRef.As()) { auto arg = args[aa++]; @@ -3105,14 +3105,14 @@ namespace Slang TypeExp typeExp = CoerceToProperType(TypeExp(arg)); } } - else if (auto valParamRef = memberRef.As()) + else if (auto valParamRef = memberRef.As()) { 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 params; + List> params; switch (candidate.flavor) { case OverloadCandidate::Flavor::Func: - params = candidate.item.declRef.As().GetParameters().ToArray(); + params = GetParameters(candidate.item.declRef.As()).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(); + auto baseGenericRef = baseDeclRefExpr->declRef.As(); if (!baseGenericRef) { assert(!"unexpected"); @@ -3232,7 +3232,7 @@ namespace Slang } RefPtr 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 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()) + if(auto subscriptDeclRef = candidate.item.declRef.As()) { - for(auto setter : subscriptDeclRef.GetDecl()->GetMembersOfType()) + for(auto setter : subscriptDeclRef.getDecl()->getMembersOfType()) { context.appExpr->Type.IsLeftValue = true; } @@ -3442,15 +3442,15 @@ namespace Slang void AddFuncOverloadCandidate( LookupResultItem item, - CallableDeclRef funcDeclRef, + DeclRef 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 type, - ConstructorDeclRef ctorDeclRef, + DeclRef 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 const& varRef, RefPtr val) { - if(auto genericValueParamRef = varRef.As()) + if(auto genericValueParamRef = varRef.As()) { - 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(fstDeclRef.GetDecl())) + if (auto typeParamDecl = dynamic_cast(fstDeclRef.getDecl())) return TryUnifyTypeParam(constraints, typeParamDecl, snd); if (auto sndDeclRefType = snd->As()) { auto sndDeclRef = sndDeclRefType->declRef; - if (auto typeParamDecl = dynamic_cast(sndDeclRef.GetDecl())) + if (auto typeParamDecl = dynamic_cast(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(fstDeclRef.GetDecl())) + if (auto typeParamDecl = dynamic_cast(fstDeclRef.getDecl())) return TryUnifyTypeParam(constraints, typeParamDecl, snd); } @@ -3705,7 +3705,7 @@ namespace Slang { auto sndDeclRef = sndDeclRefType->declRef; - if (auto typeParamDecl = dynamic_cast(sndDeclRef.GetDecl())) + if (auto typeParamDecl = dynamic_cast(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 ApplyExtensionToType( ExtensionDecl* extDecl, RefPtr type) { @@ -3755,21 +3755,21 @@ namespace Slang ConstraintSystem constraints; if (!TryUnifyTypes(constraints, extDecl->targetType, type)) - return DeclRef().As(); + return DeclRef().As(); - auto constraintSubst = TrySolveConstraintSystem(&constraints, DeclRef(extGenericDecl, nullptr).As()); + auto constraintSubst = TrySolveConstraintSystem(&constraints, DeclRef(extGenericDecl, nullptr).As()); if (!constraintSubst) { - return DeclRef().As(); + return DeclRef().As(); } // 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(); + DeclRef extDeclRef = DeclRef(extDecl, constraintSubst).As(); // 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(); - return DeclRef(extDecl, nullptr).As(); + return DeclRef().As(); + return DeclRef(extDecl, nullptr).As(); } } bool TryUnifyArgAndParamTypes( ConstraintSystem& system, RefPtr argExpr, - ParamDeclRef paramDeclRef) + DeclRef 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 SpecializeGenericForOverload( + DeclRef 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 unspecializedInnerRef = DeclRef(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()) + if (auto funcDeclRef = unspecializedInnerRef.As()) { 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(nullptr, nullptr); } for (int aa = 0; aa < argCount; ++aa) { #if 0 if (!TryUnifyArgAndParamTypes(constraints, args[aa], params[aa])) - return DeclRef(nullptr, nullptr); + return DeclRef(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(nullptr, nullptr); } auto constraintSubst = TrySolveConstraintSystem(&constraints, genericDeclRef); if (!constraintSubst) { // constraint solving failed - return DeclRef(nullptr, nullptr); + return DeclRef(nullptr, nullptr); } // We can now construct a reference to the inner declaration using // the solution to our constraints. - return DeclRef(innerDecl, constraintSubst); + return DeclRef(innerDecl, constraintSubst); } void AddAggTypeOverloadCandidates( LookupResultItem typeItem, RefPtr type, - AggTypeDeclRef aggTypeDeclRef, + DeclRef aggTypeDeclRef, OverloadResolveContext& context) { - for (auto ctorDeclRef : aggTypeDeclRef.GetMembersOfType()) + for (auto ctorDeclRef : getMembersOfType(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()) + for (auto ctorDeclRef : getMembersOfType(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()) + for (auto genericDeclRef : getMembersOfType(extDeclRef)) { - if (auto ctorDecl = genericDeclRef.GetDecl()->inner.As()) + if (auto ctorDecl = genericDeclRef.getDecl()->inner.As()) { - DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context); + DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context); if (!innerRef) continue; - ConstructorDeclRef innerCtorRef = innerRef.As(); + DeclRef innerCtorRef = innerRef.As(); AddCtorOverloadCandidate(typeItem, type, innerCtorRef, context); @@ -3924,7 +3924,7 @@ namespace Slang { if (auto declRefType = type->As()) { - if (auto aggTypeDeclRef = declRefType->declRef.As()) + if (auto aggTypeDeclRef = declRefType->declRef.As()) { AddAggTypeOverloadCandidates(LookupResultItem(aggTypeDeclRef), type, aggTypeDeclRef, context); } @@ -3937,19 +3937,19 @@ namespace Slang { auto declRef = item.declRef; - if (auto funcDeclRef = item.declRef.As()) + if (auto funcDeclRef = item.declRef.As()) { AddFuncOverloadCandidate(item, funcDeclRef, context); } - else if (auto aggTypeDeclRef = item.declRef.As()) + else if (auto aggTypeDeclRef = item.declRef.As()) { auto type = DeclRefType::Create(aggTypeDeclRef); AddAggTypeOverloadCandidates(item, type, aggTypeDeclRef, context); } - else if (auto genericDeclRef = item.declRef.As()) + else if (auto genericDeclRef = item.declRef.As()) { // Try to infer generic arguments, based on the context - DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context); + DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context); if (innerRef) { @@ -3975,9 +3975,9 @@ namespace Slang AddOverloadCandidateInner(context, candidate); } } - else if( auto typeDefDeclRef = item.declRef.As() ) + else if( auto typeDefDeclRef = item.declRef.As() ) { - 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 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(); + auto parentGenericDeclRef = parentDeclRef.As(); if(parentGenericDeclRef) { parentDeclRef = parentGenericDeclRef.GetParent(); } // Depending on what the parent is, we may want to format things specially - if(auto aggTypeDeclRef = parentDeclRef.As()) + if(auto aggTypeDeclRef = parentDeclRef.As()) { 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 declRef) { - if (auto funcDeclRef = declRef.As()) + if (auto funcDeclRef = declRef.As()) { // 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()) + else if(auto genericDeclRef = declRef.As()) { sb << "<"; bool first = true; - for (auto paramDeclRef : genericDeclRef.GetMembers()) + for (auto paramDeclRef : getMembers(genericDeclRef)) { - if(auto genericTypeParam = paramDeclRef.As()) + if(auto genericTypeParam = paramDeclRef.As()) { if (!first) sb << ", "; first = false; sb << genericTypeParam.GetName(); } - else if(auto genericValParam = paramDeclRef.As()) + else if(auto genericValParam = paramDeclRef.As()) { 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(GetInner(genericDeclRef), genericDeclRef.substitutions)); } else { } } - void formatDeclSignature(StringBuilder& sb, DeclRef declRef) + void formatDeclSignature(StringBuilder& sb, DeclRef declRef) { formatDeclPath(sb, declRef); formatDeclParams(sb, declRef); } - String getDeclSignatureString(DeclRef declRef) + String getDeclSignatureString(DeclRef declRef) { StringBuilder sb; formatDeclSignature(sb, declRef); @@ -4283,9 +4283,9 @@ namespace Slang LookupResultItem baseItem, OverloadResolveContext& context) { - if (auto genericDeclRef = baseItem.declRef.As()) + if (auto genericDeclRef = baseItem.declRef.As()) { - 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()) + if (auto genericDeclRef = declRef.As()) { int argCount = typeNode->Args.Count(); int argIndex = 0; - for (RefPtr member : genericDeclRef.GetDecl()->Members) + for (RefPtr member : genericDeclRef.getDecl()->Members) { if (auto typeParam = member.As()) { @@ -4500,7 +4500,7 @@ namespace Slang { List> paramsStorage; List> * params = nullptr; - if (auto func = funcType->declRef.GetDecl()) + if (auto func = funcType->declRef.getDecl()) { paramsStorage = func->GetParameters().ToArray(); params = ¶msStorage; @@ -4622,7 +4622,7 @@ namespace Slang #endif // Get the type to use when referencing a declaration - QualType GetTypeForDeclRef(DeclRef declRef) + QualType GetTypeForDeclRef(DeclRef declRef) { return getTypeForDeclRef( this, @@ -4792,10 +4792,10 @@ namespace Slang } else if (auto declRefType = baseType->AsDeclRefType()) { - if (auto aggTypeDeclRef = declRefType->declRef.As()) + if (auto aggTypeDeclRef = declRefType->declRef.As()) { // 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 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 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 declRef, RefPtr* 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()) + if (auto varDeclRef = declRef.As()) { 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()) + else if (auto typeAliasDeclRef = declRef.As()) { auto type = new NamedExpressionType(typeAliasDeclRef); *outTypeResult = type; return new TypeType(type); } - else if (auto aggTypeDeclRef = declRef.As()) + else if (auto aggTypeDeclRef = declRef.As()) { auto type = DeclRefType::Create(aggTypeDeclRef); *outTypeResult = type; return new TypeType(type); } - else if (auto simpleTypeDeclRef = declRef.As()) + else if (auto simpleTypeDeclRef = declRef.As()) { auto type = DeclRefType::Create(simpleTypeDeclRef); *outTypeResult = type; return new TypeType(type); } - else if (auto genericDeclRef = declRef.As()) + else if (auto genericDeclRef = declRef.As()) { auto type = new GenericDeclRefType(genericDeclRef); *outTypeResult = type; return new TypeType(type); } - else if (auto funcDeclRef = declRef.As()) + else if (auto funcDeclRef = declRef.As()) { auto type = new FuncType(); type->declRef = funcDeclRef; @@ -5032,7 +5032,7 @@ namespace Slang } QualType getTypeForDeclRef( - DeclRef declRef) + DeclRef declRef) { RefPtr 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 type, String c static void EmitType(EmitContext* context, RefPtr type); static void EmitExpr(EmitContext* context, RefPtr expr); static void EmitStmt(EmitContext* context, RefPtr stmt); -static void EmitDeclRef(EmitContext* context, DeclRef declRef); +static void EmitDeclRef(EmitContext* context, DeclRef declRef); // Low-level emit logic @@ -457,7 +457,7 @@ static void emitCallExpr( if (auto funcDeclRefExpr = funcExpr.As()) { auto funcDeclRef = funcDeclRefExpr->declRef; - auto funcDecl = funcDeclRef.GetDecl(); + auto funcDecl = funcDeclRef.getDecl(); if (auto intrinsicOpModifier = funcDecl->FindModifier()) { 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()) + if(auto subscriptDeclRef = funcDeclRef.As()) { // 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()) { auto declRef = funcDeclRefExpr->declRef; - if (auto ctorDeclRef = declRef.As()) + if (auto ctorDeclRef = declRef.As()) { // 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) } } -static void EmitDeclRef(EmitContext* context, DeclRef declRef) +static void EmitDeclRef(EmitContext* context, DeclRef 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()) + if (auto genericDeclRef = parentDeclRef.As()) { // Only do this for declarations of appropriate flavors - if(auto funcDeclRef = declRef.As()) + if(auto funcDeclRef = declRef.As()) { // Don't emit generic arguments for functions, because HLSL doesn't allow them return; @@ -1869,16 +1869,16 @@ static void EmitStructDecl(EmitContext* context, RefPtr 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 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 decl) { - EmitVarDeclCommon(context, DeclRef(decl.Ptr(), nullptr).As()); + EmitVarDeclCommon(context, DeclRef(decl.Ptr(), nullptr).As()); } // 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()) + if (auto structRef = declRefType->declRef.As()) { - for (auto field : structRef.GetMembersOfType()) + for (auto field : getMembersOfType(structRef)) { EmitVarDeclCommon(context, field); RefPtr 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()) + if (auto structRef = declRefType->declRef.As()) { - for (auto field : structRef.GetMembersOfType()) + for (auto field : getMembersOfType(structRef)) { RefPtr 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 decl) Emit(context, "("); bool first = true; - for (auto paramDecl : decl->GetMembersOfType()) + for (auto paramDecl : decl->getMembersOfType()) { 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 declRef; BreadcrumbInfo* prev = nullptr; }; void DoLocalLookupImpl( String const& name, - ContainerDeclRef containerDeclRef, + DeclRef 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 declRef, BreadcrumbInfo* breadcrumbInfos) { LookupResultItem item; @@ -167,7 +167,7 @@ void DoMemberLookupImpl( if (auto baseDeclRefType = baseType->As()) { - if (auto baseAggTypeDeclRef = baseDeclRefType->declRef.As()) + if (auto baseAggTypeDeclRef = baseDeclRefType->declRef.As()) { DoLocalLookupImpl(name, baseAggTypeDeclRef, request, ioResult, breadcrumbs); } @@ -178,7 +178,7 @@ void DoMemberLookupImpl( void DoMemberLookupImpl( String const& name, - DeclRef baseDeclRef, + DeclRef 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 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(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 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(); + DeclRef containerRef = DeclRef(link->containerDecl, nullptr).As(); DoLocalLookupImpl(name, containerRef, request, result, nullptr); } @@ -292,7 +292,7 @@ LookupResult LookUp(String const& name, RefPtr 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 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(); - 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); // 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 containerDeclRef); // TODO: this belongs somewhere else @@ -28,11 +27,11 @@ class SemanticsVisitor; QualType getTypeForDeclRef( SemanticsVisitor* sema, DiagnosticSink* sink, - DeclRef declRef, + DeclRef declRef, RefPtr* outTypeResult); QualType getTypeForDeclRef( - DeclRef declRef); + DeclRef 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 static bool findLayoutArg( - DeclRef declRef, + DeclRef declRef, int* outVal) { - return findLayoutArg(declRef.GetDecl(), outVal); + return findLayoutArg(declRef.getDecl(), outVal); } // @@ -437,7 +437,7 @@ static void collectGlobalScopeParameter( // Now create a variable layout that we can use RefPtr varLayout = new VarLayout(); varLayout->typeLayout = typeLayout; - varLayout->varDecl = DeclRef(varDecl.Ptr(), nullptr).As(); + varLayout->varDecl = DeclRef(varDecl.Ptr(), nullptr).As(); // 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()) + for (auto semantic : varDecl.getDecl()->GetModifiersOfType()) { // 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()) + if (auto structDeclRef = declRef.As()) { // 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(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(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(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() ) { auto declRef = declRefType->declRef; - if( auto structDeclRef = declRef.As() ) + if( auto structDeclRef = declRef.As() ) { return SLANG_TYPE_KIND_STRUCT; } @@ -155,9 +155,9 @@ SLANG_API unsigned int spReflectionType_GetFieldCount(SlangReflectionType* inTyp if(auto declRefType = dynamic_cast(type)) { auto declRef = declRefType->declRef; - if( auto structDeclRef = declRef.As()) + if( auto structDeclRef = declRef.As()) { - return structDeclRef.GetFields().Count(); + return GetFields(structDeclRef).Count(); } } @@ -174,10 +174,10 @@ SLANG_API SlangReflectionVariable* spReflectionType_GetFieldByIndex(SlangReflect if(auto declRefType = dynamic_cast(type)) { auto declRef = declRefType->declRef; - if( auto structDeclRef = declRef.As()) + if( auto structDeclRef = declRef.As()) { - 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(); + auto structDeclRef = declRefType->declRef.As(); if (!structDeclRef) return false; return true; } - bool ExpressionType::IsClass() - { - auto declRefType = AsDeclRefType(); - if (!declRefType) return false; - auto classDeclRef = declRefType->declRef.As(); - if (!classDeclRef) return false; - return true; - } - #if 0 RefPtr ExpressionType::Bool; RefPtr 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(declRef.GetDecl())) + if (auto genericTypeParamDecl = dynamic_cast(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 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 declRef) { - if (auto builtinMod = declRef.GetDecl()->FindModifier()) + if (auto builtinMod = declRef.getDecl()->FindModifier()) { auto type = new BasicExpressionType(builtinMod->tag); type->declRef = declRef; return type; } - else if (auto magicMod = declRef.GetDecl()->FindModifier()) + else if (auto magicMod = declRef.getDecl()->FindModifier()) { 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 ""; + return ">"; } 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 DeclRef::Substitute(RefPtr type) const + RefPtr DeclRefBase::Substitute(RefPtr type) const { // No substitutions? Easy. if (!substitutions) @@ -1158,7 +1149,7 @@ namespace Slang return type->Substitute(substitutions.Ptr()).As(); } - 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 DeclRef::Substitute(RefPtr expr) const + RefPtr DeclRefBase::Substitute(RefPtr 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(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, RefPtr modifier) { - auto type = DeclRefType::Create(DeclRef(decl.Ptr(), nullptr)); + auto type = DeclRefType::Create(DeclRef(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; - DeclRef() + DeclRefBase() {} - DeclRef(Decl* decl, RefPtr substitutions) + DeclRefBase(Decl* decl, RefPtr substitutions) : decl(decl) , substitutions(substitutions) {} // Apply substitutions to a type or ddeclaration RefPtr Substitute(RefPtr type) const; - DeclRef Substitute(DeclRef declRef) const; + + DeclRefBase Substitute(DeclRefBase declRef) const; // Apply substitutions to an expression RefPtr Substitute(RefPtr 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 + struct DeclRef : DeclRefBase + { + typedef T DeclType; + + DeclRef() + {} + + DeclRef(T* decl, RefPtr substitutions) + : DeclRefBase(decl, substitutions) + {} + + template + DeclRef(DeclRef const& other, + typename EnableIf::Value, void>::type* = 0) + : DeclRefBase(other.decl, other.substitutions) + { + } // "dynamic cast" to a more specific declaration reference type template - T As() const + DeclRef As() const { - T result; - result.decl = dynamic_cast(decl); + DeclRef result; + result.decl = dynamic_cast(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 unsafeInit(DeclRefBase const& declRef) + { + return DeclRef((T*) declRef.decl, declRef.substitutions); + } + RefPtr Substitute(RefPtr type) const + { + return DeclRefBase::Substitute(type); + } + RefPtr Substitute(RefPtr expr) const + { + return DeclRefBase::Substitute(expr); + } + // Apply substitutions to a type or ddeclaration + template + DeclRef Substitute(DeclRef declRef) const + { + return DeclRef::unsafeInit(DeclRefBase::Substitute(declRef)); + } + + // Apply substitutions to this declaration reference + DeclRef SubstituteImpl(Substitutions* subst, int* ioDiff) + { + return DeclRef::unsafeInit(DeclRefBase::SubstituteImpl(subst, ioDiff)); + } + + DeclRef GetParent() const + { + return DeclRef::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 declRef; virtual String ToString() override; virtual RefPtr SubstituteImpl(Substitutions* subst, int* ioDiff) override; - static DeclRefType* Create(DeclRef declRef); + static DeclRefType* Create(DeclRef declRef); protected: DeclRefType() {} - DeclRefType(DeclRef declRef) + DeclRefType(DeclRef declRef) : declRef(declRef) {} virtual int GetHashCode() override; @@ -1246,7 +1296,7 @@ namespace Slang List> Members; template - FilteredMemberList GetMembersOfType() + FilteredMemberList getMembersOfType() { return FilteredMemberList(Members); } @@ -1286,9 +1336,9 @@ namespace Slang return count; } - List ToArray() const + List> ToArray() const { - List result; + List> 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 operator*() { - return DeclRef(ptr->Ptr(), list->substitutions).As(); + return DeclRef((T*) ptr->Ptr(), list->substitutions); } }; @@ -1333,7 +1383,7 @@ namespace Slang { while (ptr != end) { - DeclRef declRef(ptr->Ptr(), substitutions); + DeclRef declRef(ptr->Ptr(), substitutions); if (declRef.As()) return ptr; ptr++; @@ -1342,22 +1392,16 @@ namespace Slang } }; - struct ContainerDeclRef : DeclRef + inline FilteredMemberRefList getMembers(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(ContainerDecl); - - FilteredMemberRefList GetMembers() const - { - return FilteredMemberRefList(GetDecl()->Members, substitutions); - } - - template - FilteredMemberRefList GetMembersOfType() const - { - return FilteredMemberRefList(GetDecl()->Members, substitutions); - } + return FilteredMemberRefList(declRef.getDecl()->Members, declRef.substitutions); + } - }; + template + inline FilteredMemberRefList getMembersOfType(DeclRef const& declRef) + { + return FilteredMemberRefList(declRef.getDecl()->Members, declRef.substitutions); + } // // Type Expressions @@ -1417,14 +1461,15 @@ namespace Slang RefPtr Expr; }; - struct VarDeclBaseRef : DeclRef + inline RefPtr GetType(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(VarDeclBase); - - RefPtr GetType() const { return Substitute(GetDecl()->Type); } + return declRef.Substitute(declRef.getDecl()->Type); + } - RefPtr getInitExpr() const { return Substitute(GetDecl()->Expr); } - }; + inline RefPtr getInitExpr(DeclRef 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 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 Accept(SyntaxVisitor * visitor) override; }; - struct ExtensionDeclRef : AggTypeDeclBaseRef - { - SLANG_DECLARE_DECL_REF(ExtensionDecl); - RefPtr GetTargetType() const { return Substitute(GetDecl()->targetType); } - }; + inline RefPtr GetTargetType(DeclRef 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 GetFields() { - return GetMembersOfType(); + return getMembersOfType(); } StructField* FindField(String name) { @@ -1511,12 +1545,10 @@ namespace Slang } }; - struct AggTypeDeclRef : public AggTypeDeclBaseRef + inline ExtensionDecl* GetCandidateExtensions(DeclRef 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 Accept(SyntaxVisitor * visitor) override; }; - struct StructDeclRef : public AggTypeDeclRef + inline FilteredMemberRefList GetFields(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(StructSyntaxNode); - - FilteredMemberRefList GetFields() const { return GetMembersOfType(); } - }; + return getMembersOfType(declRef); + } class ClassSyntaxNode : public AggTypeDecl { @@ -1537,13 +1567,6 @@ namespace Slang virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; - struct ClassDeclRef : public AggTypeDeclRef - { - SLANG_DECLARE_DECL_REF(ClassSyntaxNode); - - FilteredMemberRefList GetFields() const { return GetMembersOfType(); } - }; - // An interface which other types can conform to class InterfaceDecl : public AggTypeDecl { @@ -1551,12 +1574,6 @@ namespace Slang virtual RefPtr 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 Accept(SyntaxVisitor * visitor) override; }; - struct InheritanceDeclRef : public DeclRef + inline RefPtr getBaseType(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(InheritanceDecl); - - RefPtr 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 Accept(SyntaxVisitor * visitor) override; }; - struct TypeDefDeclRef : SimpleTypeDeclRef + inline RefPtr GetType(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(TypeDefDecl); - - RefPtr 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 declRef) : declRef(declRef) {} - TypeDefDeclRef declRef; + DeclRef declRef; virtual String ToString() override; @@ -1664,36 +1672,26 @@ namespace Slang virtual RefPtr 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 GetParameters() { - return GetMembersOfType(); + return getMembersOfType(); } TypeExp ReturnType; }; - struct CallableDeclRef : ContainerDeclRef + inline RefPtr GetResultType(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(CallableDecl); - - RefPtr GetResultType() const - { - return Substitute(GetDecl()->ReturnType.type.Ptr()); - } + return declRef.Substitute(declRef.getDecl()->ReturnType.type.Ptr()); + } - FilteredMemberRefList GetParameters() - { - return GetMembersOfType(); - } - }; + inline FilteredMemberRefList GetParameters(DeclRef const& declRef) + { + return getMembersOfType(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 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 declRef; virtual String ToString() override; protected: @@ -1730,11 +1723,6 @@ namespace Slang virtual RefPtr 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 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; // The declaration of the symbol being referenced - DeclRef declRef; + DeclRef declRef; }; class VarExpressionSyntaxNode : public DeclRefExpr @@ -1848,10 +1825,10 @@ namespace Slang }; Kind kind; - DeclRef declRef; + DeclRef declRef; RefPtr next; - Breadcrumb(Kind kind, DeclRef declRef, RefPtr next) + Breadcrumb(Kind kind, DeclRef declRef, RefPtr 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 declRef; // Any breadcrumbs needed in order to turn that declaration // reference into a well-formed expression. @@ -1870,10 +1847,10 @@ namespace Slang RefPtr breadcrumbs; LookupResultItem() = default; - explicit LookupResultItem(DeclRef declRef) + explicit LookupResultItem(DeclRef declRef) : declRef(declRef) {} - LookupResultItem(DeclRef declRef, RefPtr breadcrumbs) + LookupResultItem(DeclRef declRef, RefPtr breadcrumbs) : declRef(declRef) , breadcrumbs(breadcrumbs) {} @@ -1894,7 +1871,7 @@ namespace Slang List 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 GetFunctions() { - return GetMembersOfType(); + return getMembersOfType(); } FilteredMemberList GetClasses() { - return GetMembersOfType(); + return getMembersOfType(); } FilteredMemberList GetStructs() { - return GetMembersOfType(); + return getMembersOfType(); } FilteredMemberList GetTypeDefs() { - return GetMembersOfType(); + return getMembersOfType(); } virtual RefPtr Accept(SyntaxVisitor * visitor) override; @@ -2372,23 +2349,23 @@ namespace Slang virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; - struct GenericDeclRef : ContainerDeclRef + inline Decl* GetInner(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(GenericDecl); - - Decl* GetInner() const { return GetDecl()->inner.Ptr(); } - }; + // TODO: Should really return a `DeclRef` 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 declRef) : declRef(declRef) {} - GenericDeclRef declRef; - GenericDeclRef const& GetDeclRef() const { return declRef; } + DeclRef declRef; + DeclRef const& GetDeclRef() const { return declRef; } virtual String ToString() override; @@ -2413,11 +2390,6 @@ namespace Slang virtual RefPtr 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 Accept(SyntaxVisitor * visitor) override; }; - struct GenericTypeConstraintDeclRef : DeclRef + inline RefPtr GetSub(DeclRef const& declRef) { - SLANG_DECLARE_DECL_REF(GenericTypeConstraintDecl); - - RefPtr GetSub() { return Substitute(GetDecl()->sub); } - RefPtr GetSup() { return Substitute(GetDecl()->sup); } - }; + return declRef.Substitute(declRef.getDecl()->sub); + } + inline RefPtr GetSup(DeclRef const& declRef) + { + return declRef.Substitute(declRef.getDecl()->sup); + } class GenericValueParamDecl : public VarDeclBase { @@ -2447,18 +2420,13 @@ namespace Slang virtual RefPtr 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 declRef; - GenericParamIntVal(VarDeclBaseRef declRef) + GenericParamIntVal(DeclRef 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()) + if (auto structDeclRef = declRef.As()) { RefPtr typeLayout; if (outTypeLayout) @@ -1022,11 +1022,11 @@ SimpleLayoutInfo GetLayoutImpl( UniformLayoutInfo info = rules->BeginStructLayout(); - for (auto field : structDeclRef.GetFields()) + for (auto field : GetFields(structDeclRef)) { RefPtr 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 varDecl; + VarDeclBase* getVariable() { return varDecl.getDecl(); } String const& getName() { return getVariable()->getName(); } -- cgit v1.2.3