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/lower-to-ir.cpp | 72 ++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 32 deletions(-) (limited to 'source/slang/lower-to-ir.cpp') diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index e08498fc0..4af36f718 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -497,7 +497,7 @@ LoweredValInfo emitWitnessTableRef( { // if we are referring to an actual type, don't include substitution // and generate specialize instruction - srcDeclRef.substitutions = nullptr; + srcDeclRef.substitutions = SubstitutionSet(); } witnessTableVal = context->irBuilder->emitFindWitnessTable(srcDeclRef, inheritanceDeclRef.getDecl()->base.type); return maybeEmitSpecializeInst(context, LoweredValInfo::simple(witnessTableVal), declRefType->declRef); @@ -1058,15 +1058,12 @@ struct ValLoweringVisitor : ValVisitor* ioArgs, DeclRefBase declRef) { - auto subs = declRef.substitutions; + auto subs = declRef.substitutions.genericSubstitutions; while(subs) { - if (auto genSubst = subs.As()) + for (auto aa : subs->args) { - for (auto aa : genSubst->args) - { - (*ioArgs).Add(getSimpleVal(context, lowerVal(context, aa))); - } + (*ioArgs).Add(getSimpleVal(context, lowerVal(context, aa))); } subs = subs->outer; } @@ -3837,41 +3834,52 @@ RefPtr lowerSubstitutionArg( // Given a set of substitutions, make sure that we have // lowered the arguments being used into a form that // is suitable for use in the IR. -RefPtr lowerSubstitutions( +RefPtr lowerGenericSubstitutions( IRGenContext* context, - Substitutions* subst) + GenericSubstitution* genSubst) { - if(!subst) + if(!genSubst) return nullptr; - RefPtr result; - if (auto genSubst = dynamic_cast(subst)) - { - RefPtr newSubst = new GenericSubstitution(); - newSubst->genericDecl = genSubst->genericDecl; - - for (auto arg : genSubst->args) - { - auto newArg = lowerSubstitutionArg(context, arg); - newSubst->args.Add(newArg); - } + RefPtr result; + RefPtr newSubst = new GenericSubstitution(); + newSubst->genericDecl = genSubst->genericDecl; - result = newSubst; - } - else if (auto thisSubst = dynamic_cast(subst)) + for (auto arg : genSubst->args) { - RefPtr newSubst = new ThisTypeSubstitution(); - newSubst->sourceType = lowerSubstitutionArg(context, thisSubst->sourceType); - result = newSubst; + auto newArg = lowerSubstitutionArg(context, arg); + newSubst->args.Add(newArg); } - if (subst->outer) + + result = newSubst; + if (genSubst->outer) { - result->outer = lowerSubstitutions( + result->outer = lowerGenericSubstitutions( context, - subst->outer); + genSubst->outer); } return result; } +RefPtr lowerThisTypeSubstitution( + IRGenContext* context, + ThisTypeSubstitution* thisSubst) +{ + if (!thisSubst) + return nullptr; + RefPtr newSubst = new ThisTypeSubstitution(); + newSubst->sourceType = lowerSubstitutionArg(context, thisSubst->sourceType); + return newSubst; +} + +SubstitutionSet lowerSubstitutions(IRGenContext* context, SubstitutionSet subst) +{ + SubstitutionSet rs; + rs.genericSubstitutions = lowerGenericSubstitutions(context, subst.genericSubstitutions); + rs.thisTypeSubstitution = lowerThisTypeSubstitution(context, subst.thisTypeSubstitution); + rs.globalGenParamSubstitutions = subst.globalGenParamSubstitutions; + return rs; +} + LoweredValInfo emitDeclRef( IRGenContext* context, DeclRef declRef) @@ -3889,7 +3897,7 @@ LoweredValInfo maybeEmitSpecializeInst(IRGenContext* context, { // If this declaration reference doesn't involve any specializations, // then we are done at this point. - if (!hasGenericSubstitutions(declRef.substitutions)) + if (!declRef.substitutions.genericSubstitutions) return loweredDecl; // There's no reason to specialize something that maps to a NULL pointer. @@ -3902,7 +3910,7 @@ LoweredValInfo maybeEmitSpecializeInst(IRGenContext* context, // need to walk through those and replace things in // cases where the `Val`s used for substitution should // lower to something other than their original form. - RefPtr newSubst = lowerSubstitutions(context, declRef.substitutions); + auto newSubst = lowerSubstitutions(context, declRef.substitutions); declRef.substitutions = newSubst; -- cgit v1.2.3