Skip to content

[cxx-interop] Fix calling methods with dependent types. #41440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ static ValueDecl *rewriteIntegerTypes(SubstitutionMap subst, ValueDecl *oldDecl,
return newDecl;
}

static Expr *createSelfExpr(FuncDecl *fnDecl) {
ASTContext &ctx = fnDecl->getASTContext();

auto selfDecl = fnDecl->getImplicitSelfDecl();
auto selfRefExpr = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
/*implicit*/ true);

if (!fnDecl->isMutating()) {
selfRefExpr->setType(selfDecl->getInterfaceType());
return selfRefExpr;
}
selfRefExpr->setType(LValueType::get(selfDecl->getInterfaceType()));

auto inoutSelfExpr = new (ctx) InOutExpr(
SourceLoc(), selfRefExpr,
fnDecl->mapTypeIntoContext(selfDecl->getValueInterfaceType()),
/*isImplicit*/ true);
inoutSelfExpr->setType(InOutType::get(selfDecl->getInterfaceType()));
return inoutSelfExpr;
}

// Synthesize a thunk body for the function created in
// "addThunkForDependentTypes". This will just cast all params and forward them
// along to the specialized function. It will also cast the result before
Expand Down Expand Up @@ -211,10 +232,27 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
paramIndex++;
}

auto *specializedFuncDeclRef = new (ctx) DeclRefExpr(ConcreteDeclRef(specializedFuncDecl),
Expr *specializedFuncDeclRef = new (ctx) DeclRefExpr(ConcreteDeclRef(specializedFuncDecl),
DeclNameLoc(), true);
specializedFuncDeclRef->setType(specializedFuncDecl->getInterfaceType());

if (specializedFuncDecl->isInstanceMember()) {
auto selfExpr = createSelfExpr(thunkDecl);
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfExpr);
memberCall->setThrows(false);
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
specializedFuncDeclRef = memberCall;
specializedFuncDeclRef->setType(resultType);
} else if (specializedFuncDecl->isStatic()) {
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext()->getAsDecl())->getDeclaredInterfaceType();
auto selfTypeExpr = TypeExpr::createImplicit(selfType, ctx);
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfTypeExpr);
memberCall->setThrows(false);
specializedFuncDeclRef = memberCall;
specializedFuncDeclRef->setType(resultType);
}

auto argList = ArgumentList::createImplicit(ctx, forwardingParams);
auto *specializedFuncCallExpr = CallExpr::createImplicit(ctx, specializedFuncDeclRef, argList);
specializedFuncCallExpr->setType(specializedFuncDecl->getResultInterfaceType());
Expand Down Expand Up @@ -279,6 +317,7 @@ static ValueDecl *addThunkForDependentTypes(FuncDecl *oldDecl,
newFnDecl->copyFormalAccessFrom(newDecl);
newFnDecl->setBodySynthesizer(synthesizeDependentTypeThunkParamForwarding, newDecl);
newFnDecl->setSelfAccessKind(newDecl->getSelfAccessKind());
if (newDecl->isStatic()) newFnDecl->setStatic();
newFnDecl->getAttrs().add(
new (newDecl->getASTContext()) TransparentAttr(/*IsImplicit=*/true));
return newFnDecl;
Expand Down Expand Up @@ -308,10 +347,27 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
forwardingParams.push_back(Argument(SourceLoc(), Identifier(), paramRefExpr));
}

auto *specializedFuncDeclRef = new (ctx) DeclRefExpr(ConcreteDeclRef(specializedFuncDecl),
Expr *specializedFuncDeclRef = new (ctx) DeclRefExpr(ConcreteDeclRef(specializedFuncDecl),
DeclNameLoc(), true);
specializedFuncDeclRef->setType(specializedFuncDecl->getInterfaceType());

if (specializedFuncDecl->isInstanceMember()) {
auto selfExpr = createSelfExpr(thunkDecl);
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfExpr);
memberCall->setThrows(false);
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
specializedFuncDeclRef = memberCall;
specializedFuncDeclRef->setType(resultType);
} else if (specializedFuncDecl->isStatic()) {
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext()->getAsDecl())->getDeclaredInterfaceType();
auto selfTypeExpr = TypeExpr::createImplicit(selfType, ctx);
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfTypeExpr);
memberCall->setThrows(false);
specializedFuncDeclRef = memberCall;
specializedFuncDeclRef->setType(resultType);
}

auto argList = ArgumentList::createImplicit(ctx, forwardingParams);
auto *specializedFuncCallExpr = CallExpr::createImplicit(ctx, specializedFuncDeclRef, argList);
specializedFuncCallExpr->setType(thunkDecl->getResultInterfaceType());
Expand Down Expand Up @@ -379,6 +435,7 @@ static ValueDecl *generateThunkForExtraMetatypes(SubstitutionMap subst,
thunk->copyFormalAccessFrom(newDecl);
thunk->setBodySynthesizer(synthesizeForwardingThunkBody, newDecl);
thunk->setSelfAccessKind(newDecl->getSelfAccessKind());
if (newDecl->isStatic()) thunk->setStatic();
thunk->getAttrs().add(
new (newDecl->getASTContext()) TransparentAttr(/*IsImplicit=*/true));

Expand Down
12 changes: 12 additions & 0 deletions test/Interop/Cxx/templates/Inputs/dependent-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ struct M {
using U = T;

T getValue() const { return value; }

template<class U>
M<U> memberDependentReturnType(U a) const { return {a}; }

template<class U>
M<U> memberDependentReturnTypeMutable(U a) { return {a}; }

template<class U>
static M<U> memberDependentReturnTypeStatic(U a) { return {a}; }

template<class U>
U memberDependentParamType(M<U> a) const { return a.value; }
};

template<class T, class U>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_TEMPLATE_TYPE_PARAMETER_NOT_IN_SIGNATURE_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_TEMPLATE_TYPE_PARAMETER_NOT_IN_SIGNATURE_H

struct Struct {
template <typename T>
void templateTypeParamNotUsedInSignature() const {}

template <typename T>
T templateTypeParamUsedInReturnType(int x) const { return x; }

template <typename T>
void templateTypeParamNotUsedInSignatureMutable() {}

template <typename T>
static void templateTypeParamNotUsedInSignatureStatic() {}
};

template <typename T>
void templateTypeParamNotUsedInSignature() {}

Expand Down
26 changes: 26 additions & 0 deletions test/Interop/Cxx/templates/dependent-types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,30 @@ DependentTypesTestSuite.test("Multiple dependent arguments (not inferred).") {
expectEqual(m.getValue(), 42)
}

// We still have some problems calling methods on Windows: SR-13129 and rdar://88391102
#if !os(Windows)
DependentTypesTestSuite.test("Function template methods") {
let m = M<Int>(value: 42)
let m2 = m.memberDependentReturnType(CInt(32)) as! M<CInt>
let val: Int = m2.memberDependentParamType(m)

expectEqual(m2.getValue(), 32)
expectEqual(val, 42)
}

DependentTypesTestSuite.test("Function template methods (mutable)") {
var m = M<CInt>(value: 2)
let m2 = m.memberDependentReturnTypeMutable(42) as! M<Int>
expectEqual(m2.getValue(), 42)
}

DependentTypesTestSuite.test("Function template methods (static)") {
let m = M<CInt>.memberDependentReturnTypeStatic(42) as! M<Int>
expectEqual(m.getValue(), 42)

let m2 = M<Int>.memberDependentReturnTypeStatic(CInt(32)) as! M<CInt>
expectEqual(m2.getValue(), 32)
}
#endif // Windows

runAllTests()
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@ TemplateNotInSignatureTestSuite.test("Pointer types") {
expectEqual(x, 1)
}

TemplateNotInSignatureTestSuite.test("Member function templates") {
let s = Struct()
s.templateTypeParamNotUsedInSignature(T: Int.self)
let x: Int = templateTypeParamUsedInReturnType(42)
expectEqual(x, 42)
}

TemplateNotInSignatureTestSuite.test("Member function templates (mutable)") {
var s = Struct()
s.templateTypeParamNotUsedInSignatureMutable(T: Int.self)
}

TemplateNotInSignatureTestSuite.test("Member function templates (static)") {
Struct.templateTypeParamNotUsedInSignatureStatic(T: Int.self)
}

runAllTests()