From afa9f4b2786c92e72a563f316e074f62770630cb Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 13 Oct 2022 13:42:05 -0700 Subject: Fix missing implementations in ConjunctionSubtypeWitness. (#2449) * Fix missing implementations in ConjunctionSubtypeWitness. * Fix. Co-authored-by: Yong He --- source/slang/slang-ast-val.cpp | 102 +++++++++++++++++++++++++++++++++++++ source/slang/slang-ast-val.h | 14 ++++- source/slang/slang-lookup.cpp | 4 +- source/slang/slang-lower-to-ir.cpp | 2 +- 4 files changed, 117 insertions(+), 5 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ast-val.cpp b/source/slang/slang-ast-val.cpp index b5291509f..377dee350 100644 --- a/source/slang/slang-ast-val.cpp +++ b/source/slang/slang-ast-val.cpp @@ -558,6 +558,108 @@ Val* TaggedUnionSubtypeWitness::_substituteImplOverride(ASTBuilder* astBuilder, return substWitness; } +bool ConjunctionSubtypeWitness::_equalsValOverride(Val* val) +{ + if (auto other = as(val)) + { + return other->leftWitness && other->leftWitness->equalsVal(leftWitness) && + other->rightWitness && other->rightWitness->equalsVal(rightWitness); + } + return false; +} + +void ConjunctionSubtypeWitness::_toTextOverride(StringBuilder& out) +{ + out << "ConjunctionSubtypeWitness("; + if (leftWitness) out << leftWitness; + out << ","; + if (rightWitness) out << rightWitness; + out << ")"; +} + +HashCode ConjunctionSubtypeWitness::_getHashCodeOverride() +{ + HashCode result = 0; + if (leftWitness) result = leftWitness->getHashCode(); + if (rightWitness) result = combineHash(result, rightWitness->getHashCode()); + return result; +} + +Val* ConjunctionSubtypeWitness::_substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff) +{ + int diff = 0; + Val* left = nullptr; + Val* right = nullptr; + + auto substSub = as(sub->substituteImpl(astBuilder, subst, &diff)); + auto substSup = as(sup->substituteImpl(astBuilder, subst, &diff)); + + if (leftWitness) + left = leftWitness->substituteImpl(astBuilder, subst, &diff); + if (rightWitness) + right = rightWitness->substituteImpl(astBuilder, subst, &diff); + + *ioDiff += diff; + + if (diff) + { + auto result = astBuilder->create(); + result->leftWitness = left; + result->rightWitness = right; + result->sub = substSub; + result->sup = substSup; + return result; + } + return this; +} + +bool ExtractFromConjunctionSubtypeWitness::_equalsValOverride(Val* val) +{ + if (auto other = as(val)) + { + return other->conjunctionWitness && other->conjunctionWitness->equalsVal(conjunctionWitness) && + other->indexInConjunction == indexInConjunction; + } + return false; +} + +void ExtractFromConjunctionSubtypeWitness::_toTextOverride(StringBuilder& out) +{ + out << "ExtractFromConjunctionSubtypeWitness("; + if (conjunctionWitness) + out << conjunctionWitness; + out << "," << indexInConjunction; + out << ")"; +} + +HashCode ExtractFromConjunctionSubtypeWitness::_getHashCodeOverride() +{ + return combineHash(indexInConjunction, conjunctionWitness ? conjunctionWitness->getHashCode() : 0); +} + +Val* ExtractFromConjunctionSubtypeWitness::_substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff) +{ + int diff = 0; + Val* newConjunctionWitness = nullptr; + + auto substSub = as(sub->substituteImpl(astBuilder, subst, &diff)); + auto substSup = as(sup->substituteImpl(astBuilder, subst, &diff)); + + if (this->conjunctionWitness) + newConjunctionWitness = conjunctionWitness->substituteImpl(astBuilder, subst, &diff); + *ioDiff += diff; + + if (diff) + { + auto result = astBuilder->create(); + result->conjunctionWitness = newConjunctionWitness; + result->sub = substSub; + result->sup = substSup; + return result; + } + return this; +} + // ModifierVal bool ModifierVal::_equalsValOverride(Val* val) diff --git a/source/slang/slang-ast-val.h b/source/slang/slang-ast-val.h index 0f7bdec8e..a67d62e3b 100644 --- a/source/slang/slang-ast-val.h +++ b/source/slang/slang-ast-val.h @@ -410,6 +410,11 @@ class ConjunctionSubtypeWitness : public SubtypeWitness /// Witness that `sub : sup->right` Val* rightWitness; + + bool _equalsValOverride(Val* val); + void _toTextOverride(StringBuilder& out); + HashCode _getHashCodeOverride(); + Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff); }; /// A witness that `T : X` because `T : X & Y` or `T : Y & X` @@ -418,14 +423,19 @@ class ExtractFromConjunctionSubtypeWitness : public SubtypeWitness SLANG_AST_CLASS(ExtractFromConjunctionSubtypeWitness) /// Witness that `T : L & R` for some `R` - Val* conunctionWitness; + Val* conjunctionWitness; /// The zero-based index of the super-type we care about in the conjunction /// - /// If `conunctionWitness` is `T : X & Y` then this index should be zero if + /// If `conjunctionWitness` is `T : X & Y` then this index should be zero if /// we want to represent `T : X` and one if we want `T : Y`. /// int indexInConjunction; + + bool _equalsValOverride(Val* val); + void _toTextOverride(StringBuilder& out); + HashCode _getHashCodeOverride(); + Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff); }; /// A value that represents a modifier attached to some other value diff --git a/source/slang/slang-lookup.cpp b/source/slang/slang-lookup.cpp index a8818fd5c..cddf3d7ce 100644 --- a/source/slang/slang-lookup.cpp +++ b/source/slang/slang-lookup.cpp @@ -712,7 +712,7 @@ static void _lookUpMembersInSuperTypeImpl( // The evidence for the subtype relationship will be a witness // proving that `leafType : leftType & rightType`: // - leafIsLeftWitness->conunctionWitness = leafIsSuperWitness; + leafIsLeftWitness->conjunctionWitness = leafIsSuperWitness; // // ... along with the index of the desired super-type in // that conjunction. The index of `leftType` in `leftType & rightType` @@ -725,7 +725,7 @@ static void _lookUpMembersInSuperTypeImpl( // the conjunction. // auto leafIsRightWitness = astBuilder->create(); - leafIsRightWitness->conunctionWitness = leafIsSuperWitness; + leafIsRightWitness->conjunctionWitness = leafIsSuperWitness; leafIsRightWitness->indexInConjunction = 1; leafIsRightWitness->sub = leafType; leafIsRightWitness->sup = rightType; diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index e2ca9a711..bd724aa9d 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -1671,7 +1671,7 @@ struct ValLoweringVisitor : ValVisitorconunctionWitness); + auto conjunctionWitness = lowerSimpleVal(context, val->conjunctionWitness); auto conjunctionTupleType = as(conjunctionWitness->getDataType()); SLANG_ASSERT(conjunctionTupleType); -- cgit v1.2.3