From 6e591ada0eb652c320bba4bd8a46cd579946df01 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 7 Nov 2017 19:09:40 -0500 Subject: Support generic interface methods (#251) * improve diagnostic messages and prevent fatal errors from crashing the compiler. * fix top level exception catching. * spelling fix * change wording of invalidSwizzleExpr diagnostic * add speculative GenericsApp expr parsing * add new test case of cascading generics call. * Fixing bugs in compiling cascaded generic function calls. Add implementation of DeclaredSubTypeWitness::SubstituteImpl() This is not needed by the type checker, but needed by IR specialization. When input source contains cascading generic function call, the arguments to `specialize` instruction is currently represented as a substitution. The arg values of this subsittution can be a `DeclaredSubTypeWitness` when a generic function uses one of its generic parameter to specialize another generic function. When the top level generics function is being specialized, this substitution argument, which is a `DeclaredSubTypeWitness`, needs to be substituted with the witness that used to specialize the top level function in the specialized specialize instruction as well. * add a test case for cascading generic function call. * parser bug fix * fixes #255 * add test case for issue #255 * Generate missing `specialize` instruction when calling a generic method from an interface constraint. When calling a generic method via an interface, we should be generating the following ir: ... f = lookup_interface_method(...) f_s = specailize(f, declRef) ... This commit fixes this `emitFuncRef` function to emit the needed `specialize` instruction. * fixes #260 This fix follows the second apporach in the disucssion. It generated mangled name for specialized functions by appending new substitution type names to the original mangled name. * Disabling removing and re-inserting specailized functions in getSpecalizeFunc() I am not sure why it is needed, it seems HLSL and GLSL backends are generating forward declarations anyways, so the order of functions in IRModule shouldn't matter. * cleanup and complete test cases. * fix warnings --- source/slang/syntax.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'source/slang/syntax.cpp') diff --git a/source/slang/syntax.cpp b/source/slang/syntax.cpp index 9025c545a..4e1778e6e 100644 --- a/source/slang/syntax.cpp +++ b/source/slang/syntax.cpp @@ -1527,6 +1527,61 @@ void Type::accept(IValVisitor* visitor, void* extra) && declRef.Equals(otherWitness->declRef); } + RefPtr DeclaredSubtypeWitness::SubstituteImpl(Substitutions* subst, int * ioDiff) + { + DeclRef genParamDeclRef; + if (auto subDeclRefType = this->sub.As()) + { + genParamDeclRef = subDeclRefType->declRef.As(); + } + if (!genParamDeclRef) + return this; + auto genParamDecl = genParamDeclRef.getDecl(); + // search for a substitution that might apply to us + for (auto s = subst; s; s = s->outer.Ptr()) + { + if (auto genericSubst = dynamic_cast(s)) + { + // the generic decl associated with the substitution list must be + // the generic decl that declared this parameter + auto genericDecl = genericSubst->genericDecl; + if (genericDecl != genParamDecl->ParentDecl) + continue; + bool found = false; + int index = 0; + for (auto m : genericDecl->Members) + { + if (m.Ptr() == genParamDecl) + { + // We've found it, so return the corresponding specialization argument + (*ioDiff)++; + found = true; + break; + } + else if (auto typeParam = m.As()) + { + index++; + } + else if (auto valParam = m.As()) + { + index++; + } + else + { + } + } + if (found) + { + auto ordinaryParamCount = genericDecl->getMembersOfType().Count() + + genericDecl->getMembersOfType().Count(); + SLANG_ASSERT(ordinaryParamCount + index < genericSubst->args.Count()); + return genericSubst->args[ordinaryParamCount + index]; + } + } + } + return this; + } + String DeclaredSubtypeWitness::ToString() { StringBuilder sb; @@ -1655,4 +1710,16 @@ void Type::accept(IValVisitor* visitor, void* extra) return false; } + RefPtr getGenericSubstitution(RefPtr subst) + { + auto p = subst.Ptr(); + while (p) + { + if (auto genSubst = dynamic_cast(p)) + return genSubst; + p = p->outer.Ptr(); + } + return nullptr; + } + } -- cgit v1.2.3