From 3d4eaf3c9b13e32c4e4d7737f17805503cddcb0b Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 15 Jan 2018 18:15:49 -0500 Subject: Support transitive interfaces This commit is a bunch of quick hacks to get transitive interfaces to work. The idea is for each concrete type we create one giant witness table that contains entries for all the transitively reachable interface requirements, and then create one copy of that witness table for each interface it implements. `DoLocalLookupImpl` now also looks up in inherited interface decles when looking up for a symbol in an interface decl. When visiting `InheritanceDecl` in `lower-to-ir`, create copies of the giant witness table for each transitively inherited interface, so that these witness tables can be found later when the IR is specialized. Re-enable the `copy all witness tables` hack in `specializeIRForEntryPoint` to ensure those transitive witness tables are copied over. --- source/slang/lower-to-ir.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (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 498783f4b..c8e010f7d 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -2781,6 +2781,30 @@ struct DeclLoweringVisitor : DeclVisitor return LoweredValInfo(); } + void walkInheritanceHierarchyAndCreateWitnessTableCopies(IRWitnessTable* witnessTable, Type* subType, InheritanceDecl* inheritanceDecl) + { + auto baseDeclRef = inheritanceDecl->base.type.As(); + if (auto baseInterfaceDeclRef = baseDeclRef->declRef.As()) + { + for (auto subInheritanceDeclRef : getMembersOfType(baseInterfaceDeclRef)) + { + auto cpyMangledName = getMangledNameForConformanceWitness(subType, subInheritanceDeclRef.getDecl()->getSup().type); + if (!witnessTablesDictionary.ContainsKey(cpyMangledName)) + { + auto cpyTable = context->irBuilder->createWitnessTable(); + cpyTable->mangledName = cpyMangledName; + context->irBuilder->createWitnessTableEntry(witnessTable, + context->irBuilder->getDeclRefVal(subInheritanceDeclRef), cpyTable); + cpyTable->entries = witnessTable->entries; + witnessTablesDictionary.Add(cpyMangledName, cpyTable); + walkInheritanceHierarchyAndCreateWitnessTableCopies(witnessTable, subType, subInheritanceDeclRef.getDecl()); + } + } + } + } + + Dictionary witnessTablesDictionary; + LoweredValInfo visitInheritanceDecl(InheritanceDecl* inheritanceDecl) { // Construct a type for the parent declaration. @@ -2817,6 +2841,8 @@ struct DeclLoweringVisitor : DeclVisitor // conformance of the type to its super-type. auto witnessTable = context->irBuilder->createWitnessTable(); witnessTable->mangledName = mangledName; + + witnessTablesDictionary.Add(mangledName, witnessTable); if (parentDecl->ParentDecl) witnessTable->genericDecl = dynamic_cast(parentDecl->ParentDecl); @@ -2850,6 +2876,7 @@ struct DeclLoweringVisitor : DeclVisitor } witnessTable->moveToEnd(); + walkInheritanceHierarchyAndCreateWitnessTableCopies(witnessTable, type, inheritanceDecl); // A direct reference to this inheritance relationship (e.g., // as a subtype witness) will take the form of a reference to -- cgit v1.2.3