From d33e6b7475a87d5a62101afc81813e9c9e458a70 Mon Sep 17 00:00:00 2001 From: Yong He Date: Sun, 14 Jan 2018 16:22:11 -0500 Subject: allow extension of a concrete type to implement additional interface Also support the scenario that the extension declares conformance to interface I, and a method M in I is already supported by the base implementation. --- source/slang/check.cpp | 39 +++++++++++++++++++-------------- source/slang/lower-to-ir.cpp | 25 +++++++++++++++------ source/slang/syntax.h | 52 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 29 deletions(-) (limited to 'source') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 6c484b493..6b8331060 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -1794,7 +1794,7 @@ namespace Slang // `requiredMemberDeclRef` is a required member of // the interface. RefPtr findWitnessForInterfaceRequirement( - DeclRef typeDeclRef, + DeclRef typeDeclRef, InheritanceDecl* inheritanceDecl, DeclRef interfaceDeclRef, DeclRef requiredMemberDeclRef, @@ -1833,11 +1833,9 @@ namespace Slang // Make sure that by-name lookup is possible. buildMemberDictionary(typeDeclRef.getDecl()); - - Decl* firstMemberOfName = nullptr; - typeDeclRef.getDecl()->memberDictionary.TryGetValue(name, firstMemberOfName); - - if (!firstMemberOfName) + auto lookupResult = lookUpLocal(getSession(), this, name, typeDeclRef); + + if (!lookupResult.isValid()) { getSink()->diagnose(inheritanceDecl, Diagnostics::typeDoesntImplementInterfaceRequirement, typeDeclRef, requiredMemberDeclRef); return nullptr; @@ -1845,10 +1843,10 @@ namespace Slang // Iterate over the members and look for one that matches // the expected signature for the requirement. - for (auto memberDecl = firstMemberOfName; memberDecl; memberDecl = memberDecl->nextInContainerWithSameName) + for (auto member : lookupResult) { - if (doesMemberSatisfyRequirement(DeclRef(memberDecl, typeDeclRef.substitutions), requiredMemberDeclRef, requirementWitness)) - return memberDecl; + if (doesMemberSatisfyRequirement(member.declRef, requiredMemberDeclRef, requirementWitness)) + return member.declRef.getDecl(); } // No suitable member found, although there were candidates. @@ -1867,7 +1865,7 @@ namespace Slang // (via the given `inheritanceDecl`) actually provides // members to satisfy all the requirements in the interface. bool checkInterfaceConformance( - DeclRef typeDeclRef, + DeclRef typeDeclRef, InheritanceDecl* inheritanceDecl, DeclRef interfaceDeclRef) { @@ -1925,7 +1923,7 @@ namespace Slang } bool checkConformanceToType( - DeclRef typeDeclRef, + DeclRef typeDeclRef, InheritanceDecl* inheritanceDecl, Type* baseType) { @@ -1953,7 +1951,7 @@ namespace Slang // `inheritanceDecl` actually does what it needs to // for that inheritance to be valid. bool checkConformance( - DeclRef typeDecl, + DeclRef typeDecl, InheritanceDecl* inheritanceDecl) { // Look at the type being inherited from, and validate @@ -1963,10 +1961,10 @@ namespace Slang } bool checkConformance( - AggTypeDecl* typeDecl, + AggTypeDeclBase* typeDecl, InheritanceDecl* inheritanceDecl) { - return checkConformance(DeclRef(typeDecl, SubstitutionSet()), inheritanceDecl); + return checkConformance(DeclRef(typeDecl, SubstitutionSet()), inheritanceDecl); } void visitAggTypeDecl(AggTypeDecl* decl) @@ -3479,10 +3477,11 @@ namespace Slang // TODO: need to check that the target type names a declaration... + DeclRef aggTypeDeclRef; if (auto targetDeclRefType = decl->targetType->As()) { // Attach our extension to that type as a candidate... - if (auto aggTypeDeclRef = targetDeclRefType->declRef.As()) + if (aggTypeDeclRef = targetDeclRefType->declRef.As()) { auto aggTypeDecl = aggTypeDeclRef.getDecl(); decl->nextCandidateExtension = aggTypeDecl->candidateExtensions; @@ -3516,6 +3515,14 @@ namespace Slang EnsureDecl(m); } + if (aggTypeDeclRef) + { + for (auto inheritanceDecl : decl->getMembersOfType()) + { + checkConformance(aggTypeDeclRef.getDecl(), inheritanceDecl); + } + } + decl->SetCheckState(DeclCheckState::Checked); } @@ -3802,7 +3809,7 @@ namespace Slang if( auto aggTypeDeclRef = declRef.As() ) { - for( auto inheritanceDeclRef : getMembersOfType(aggTypeDeclRef)) + for( auto inheritanceDeclRef : getMembersOfTypeWithExt(aggTypeDeclRef)) { EnsureDecl(inheritanceDeclRef.getDecl()); diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 61ca53278..498783f4b 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -2788,19 +2788,30 @@ struct DeclLoweringVisitor : DeclVisitor // TODO: if this inheritance declaration is under an extension, // then we should construct the type that is being extended, // and not a reference to the extension itself. - auto parentDecl = inheritanceDecl->ParentDecl; - RefPtr type = DeclRefType::Create( - context->getSession(), - makeDeclRef(parentDecl)); + auto parentDecl = inheritanceDecl->ParentDecl; + RefPtr type; + if (auto extParentDecl = dynamic_cast(parentDecl)) + { + type = extParentDecl->targetType.type; + if (auto declRefType = type.As()) + { + if (auto aggTypeDecl = declRefType->declRef.As()) + parentDecl = aggTypeDecl.getDecl(); + } + } + else + { + type = DeclRefType::Create( + context->getSession(), + makeDeclRef(parentDecl)); + } // What is the super-type that we have declared we inherit from? RefPtr superType = inheritanceDecl->base.type; // Construct the mangled name for the witness table, which depends // on the type that is conforming, and the type that it conforms to. - String mangledName = getMangledNameForConformanceWitness( - makeDeclRef(parentDecl), - superType); + String mangledName = getMangledNameForConformanceWitness(type, superType); // Build an IR level witness table, which will represent the // conformance of the type to its super-type. diff --git a/source/slang/syntax.h b/source/slang/syntax.h index 375eb5f1c..ab26b1f6d 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -978,6 +978,30 @@ namespace Slang { return items.Count() > 1 ? items[0].declRef.GetName() : item.declRef.GetName(); } + LookupResultItem* begin() + { + if (isValid()) + { + if (isOverloaded()) + return items.begin(); + else + return &item; + } + else + return nullptr; + } + LookupResultItem* end() + { + if (isValid()) + { + if (isOverloaded()) + return items.end(); + else + return &item + 1; + } + else + return nullptr; + } }; struct SemanticsVisitor; @@ -1085,6 +1109,27 @@ namespace Slang return FilteredMemberRefList(declRef.getDecl()->Members, declRef.substitutions); } + inline ExtensionDecl* GetCandidateExtensions(DeclRef const& declRef) + { + return declRef.getDecl()->candidateExtensions; + } + + template + inline FilteredMemberRefList getMembersOfTypeWithExt(DeclRef const& declRef) + { + auto rs = getMembersOfType(declRef); + if (auto aggDeclRef = declRef.As()) + { + for (auto ext = GetCandidateExtensions(aggDeclRef); ext; ext = ext->nextCandidateExtension) + { + auto extMembers = getMembersOfType(DeclRef(ext, declRef.substitutions)); + const_cast>&>(rs.decls).AddRange(extMembers.decls); + } + } + return rs; + } + + inline RefPtr GetType(DeclRef const& declRef) { return declRef.Substitute(declRef.getDecl()->type.Ptr()); @@ -1099,12 +1144,7 @@ namespace Slang { return declRef.Substitute(declRef.getDecl()->targetType.Ptr()); } - - inline ExtensionDecl* GetCandidateExtensions(DeclRef const& declRef) - { - return declRef.getDecl()->candidateExtensions; - } - + inline FilteredMemberRefList GetFields(DeclRef const& declRef) { return getMembersOfType(declRef); -- cgit v1.2.3 From ae8ef43d2c807da536331eaeec022e35aa4299c1 Mon Sep 17 00:00:00 2001 From: Yong He Date: Sun, 14 Jan 2018 16:52:02 -0500 Subject: Fixup field lookup from a member function defined in an extension This fixes item 2 in #361 Modifies existing extension-multi-interface.slang test case to cover the additional scenario. --- source/slang/lookup.cpp | 14 +++++++++++++- tests/compute/extension-multi-interface.slang | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/slang/lookup.cpp b/source/slang/lookup.cpp index 376d9bd6d..19503d63f 100644 --- a/source/slang/lookup.cpp +++ b/source/slang/lookup.cpp @@ -345,7 +345,19 @@ void DoLookupImpl( // Now perform "local" lookup in the context of the container, // as if we were looking up a member directly. - // + + // if we are currently in an extension decl, perform local lookup + // in the target decl we are extending + if (auto extDeclRef = containerDeclRef.As()) + { + if (auto targetDeclRef = extDeclRef.getDecl()->targetType->AsDeclRefType()) + { + if (auto aggDeclRef = targetDeclRef->declRef.As()) + { + containerDeclRef = extDeclRef.Substitute(aggDeclRef); + } + } + } DoLocalLookupImpl( session, name, containerDeclRef, request, result, breadcrumbs); diff --git a/tests/compute/extension-multi-interface.slang b/tests/compute/extension-multi-interface.slang index 6cc88f87c..c5136fb3c 100644 --- a/tests/compute/extension-multi-interface.slang +++ b/tests/compute/extension-multi-interface.slang @@ -21,6 +21,7 @@ interface IAddAndSub struct Simple : IAdd { + float base; float addf(float u, float v) { return u+v; @@ -31,7 +32,7 @@ __extension Simple : ISub, IAddAndSub { float subf(float u, float v) { - return u-v; + return base+u-v; } }; @@ -44,6 +45,7 @@ float testAddSub(T t) void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) { Simple s; + s.base = 0.0; float outVal = testAddSub(s); outputBuffer[dispatchThreadID.x] = outVal; } \ No newline at end of file -- cgit v1.2.3 From 436f0e7198735d118daa956d0a3bb698571c3cd7 Mon Sep 17 00:00:00 2001 From: Yong He Date: Sun, 14 Jan 2018 18:20:46 -0500 Subject: temporary workaround to fix test case failures. --- source/slang/check.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'source') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 6b8331060..3103a7908 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -1076,8 +1076,12 @@ namespace Slang { // The user is asking for us to actually perform the conversion, // so we need to generate an appropriate expression here. - - throw "foo bar baz"; + + // YONGH: I am confused why we are not hitting this case before + //throw "foo bar baz"; + // YONGH: temporary work around, may need to create the actual + // invocation expr to the constructor call + *outToExpr = fromExpr; } return true; @@ -1848,7 +1852,7 @@ namespace Slang if (doesMemberSatisfyRequirement(member.declRef, requiredMemberDeclRef, requirementWitness)) return member.declRef.getDecl(); } - + // No suitable member found, although there were candidates. // // TODO: Eventually we might want something akin to the current -- cgit v1.2.3