From df6eeb93c1718334779ae328db277cdf7a9d7b04 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 12 Jan 2018 14:15:56 -0800 Subject: Refactor substitution representation in DeclRefBase (#363) This commit changes the type of `DeclRefBase::substitutions` from `RefPtr` to `SubstitutionSet`, which is a new type defined as following: ``` struct SubstitutionSet { RefPtr genericSubstitutions; RefPtr thisTypeSubstitution; RefPtr globalGenParamSubstitutions; } ``` This change get rid of most helper functions to retreive the substitution of a certain type, as well as surgery operations to insert a `ThisTypeSubstitution` or `GlobalGenericTypeSubstittuion` at top or bottom of the substitution chain. It also simplies type comparison when certain type of substitution should not be considered as part of type definition. --- source/slang/ast-legalize.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'source/slang/ast-legalize.cpp') diff --git a/source/slang/ast-legalize.cpp b/source/slang/ast-legalize.cpp index c6f42d4da..996a66eac 100644 --- a/source/slang/ast-legalize.cpp +++ b/source/slang/ast-legalize.cpp @@ -2629,11 +2629,12 @@ struct LoweringVisitor SLANG_UNEXPECTED("unhandled value kind"); } - RefPtr translateSubstitutions( - Substitutions* inSubstitutions) + SubstitutionSet translateSubstitutions( + SubstitutionSet inSubstitutions) { - if (!inSubstitutions) return nullptr; - if (auto genSubst = dynamic_cast(inSubstitutions)) + if (!inSubstitutions) return SubstitutionSet(); + SubstitutionSet rs; + if (auto genSubst = inSubstitutions.genericSubstitutions) { RefPtr result = new GenericSubstitution(); result->genericDecl = translateDeclRef(genSubst->genericDecl)->As(); @@ -2641,16 +2642,16 @@ struct LoweringVisitor { result->args.Add(translateVal(arg)); } - return result; + rs.genericSubstitutions = result; } - else if (auto thisSubst = dynamic_cast(inSubstitutions)) + if (auto thisSubst = inSubstitutions.thisTypeSubstitution) { RefPtr result = new ThisTypeSubstitution(); if (result->sourceType) result->sourceType = translateVal(result->sourceType); - return result; + rs.thisTypeSubstitution = result; } - return nullptr; + return rs; } static Decl* getModifiedDecl(Decl* decl) @@ -2673,7 +2674,7 @@ struct LoweringVisitor RefPtr translateDeclRef( Decl* decl) { - return translateDeclRefImpl(DeclRef(decl, nullptr)); + return translateDeclRefImpl(DeclRef(decl, SubstitutionSet())); } LegalExpr translateSimpleLegalValToLegalExpr(IRValue* irVal) @@ -4943,11 +4944,16 @@ struct FindIRDeclUsedByASTVisitor // If this is a specialized declaration reference, then any // of the arguments also need to be walked. - for(auto subst = declRef.substitutions; subst; subst = subst->outer) + for(auto subst = declRef.substitutions.genericSubstitutions; subst; subst = subst->outer) { walkSubst(subst); } - + for (auto subst = declRef.substitutions.globalGenParamSubstitutions; subst; subst = subst->outer) + { + walkSubst(subst); + } + if (declRef.substitutions.thisTypeSubstitution) + walkSubst(declRef.substitutions.thisTypeSubstitution); // If any parent of the declaration was in the stdlib, or // is registered as a builtin, then skip it. for (auto pp = decl; pp; pp = pp->ParentDecl) -- cgit v1.2.3