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/syntax.h | 65 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 15 deletions(-) (limited to 'source/slang/syntax.h') diff --git a/source/slang/syntax.h b/source/slang/syntax.h index a1f4ba801..375eb5f1c 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -412,7 +412,27 @@ namespace Slang return SyntaxClass::getClass(); } - + struct SubstitutionSet + { + RefPtr genericSubstitutions; + RefPtr thisTypeSubstitution; + RefPtr globalGenParamSubstitutions; + operator bool() const + { + return genericSubstitutions || thisTypeSubstitution || globalGenParamSubstitutions; + } + SubstitutionSet() {} + SubstitutionSet(RefPtr genSubst, RefPtr inThisTypeSubst, + RefPtr globalSubst) + { + genericSubstitutions = genSubst; + thisTypeSubstitution = inThisTypeSubst; + globalGenParamSubstitutions = globalSubst; + } + bool Equals(SubstitutionSet substSet) const; + SubstitutionSet substituteImpl(SubstitutionSet subst, int * ioDiff); + int GetHashCode() const; + }; // A reference to a declaration, which may include // substitutions for generic parameters. struct DeclRefBase @@ -424,14 +444,25 @@ namespace Slang Decl* getDecl() const { return decl; } // Optionally, a chain of substititions to perform - RefPtr substitutions; + SubstitutionSet substitutions; DeclRefBase() {} + + DeclRefBase(Decl* decl) + :decl(decl) + {} - DeclRefBase(Decl* decl, RefPtr substitutions) - : decl(decl) - , substitutions(substitutions) + DeclRefBase(Decl* decl, SubstitutionSet subst) + :decl(decl), + substitutions(subst) + {} + + DeclRefBase(Decl* decl, RefPtr genSubstitutions, + RefPtr thisTypeSubst = nullptr, + RefPtr globalSubst = nullptr) + : decl(decl), + substitutions(genSubstitutions, thisTypeSubst, globalSubst) {} // Apply substitutions to a type or ddeclaration @@ -443,7 +474,7 @@ namespace Slang RefPtr Substitute(RefPtr expr) const; // Apply substitutions to this declaration reference - DeclRefBase SubstituteImpl(Substitutions* subst, int* ioDiff); + DeclRefBase SubstituteImpl(SubstitutionSet subst, int* ioDiff); // Check if this is an equivalent declaration reference to another @@ -470,9 +501,13 @@ namespace Slang DeclRef() {} + + DeclRef(T* decl, SubstitutionSet subst) + : DeclRefBase(decl, subst) + {} - DeclRef(T* decl, RefPtr substitutions) - : DeclRefBase(decl, substitutions) + DeclRef(T* decl, RefPtr genSubst) + : DeclRefBase(decl, SubstitutionSet(genSubst, nullptr, nullptr)) {} template @@ -525,7 +560,7 @@ namespace Slang } // Apply substitutions to this declaration reference - DeclRef SubstituteImpl(Substitutions* subst, int* ioDiff) + DeclRef SubstituteImpl(SubstitutionSet subst, int* ioDiff) { return DeclRef::unsafeInit(DeclRefBase::SubstituteImpl(subst, ioDiff)); } @@ -641,11 +676,11 @@ namespace Slang struct FilteredMemberRefList { List> const& decls; - RefPtr substitutions; + SubstitutionSet substitutions; FilteredMemberRefList( List> const& decls, - RefPtr substitutions) + SubstitutionSet substitutions) : decls(decls) , substitutions(substitutions) {} @@ -1153,12 +1188,12 @@ namespace Slang } // TODO: where should this live? - RefPtr createDefaultSubstitutions( + SubstitutionSet createDefaultSubstitutions( Session* session, Decl* decl, - Substitutions* parentSubst); + SubstitutionSet parentSubst); - RefPtr createDefaultSubstitutions( + SubstitutionSet createDefaultSubstitutions( Session* session, Decl* decl); @@ -1184,7 +1219,7 @@ namespace Slang // substitution lists of `DeclRef` etc. to replace the call of // `declRef.substitutions->SubstituteImpl()`, because the head to the linked list is known as a // member of that class there. - RefPtr substituteSubstitutions(RefPtr oldSubst, Substitutions * subst, int * ioDiff); + SubstitutionSet substituteSubstitutions(SubstitutionSet oldSubst, SubstitutionSet subst, int * ioDiff); } // namespace Slang #endif \ No newline at end of file -- cgit v1.2.3