Skip to content

[cxx-interop] Support reference types in function templates. #34536

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
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
16 changes: 14 additions & 2 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1814,8 +1814,20 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
// Import the parameter type into Swift.
Type swiftParamTy;
bool isParamTypeImplicitlyUnwrapped = false;
if (auto *templateParamType =
dyn_cast<clang::TemplateTypeParmType>(paramTy)) {

auto referenceType = dyn_cast<clang::ReferenceType>(paramTy);
if (referenceType &&
isa<clang::TemplateTypeParmType>(referenceType->getPointeeType())) {
auto pointeeType = referenceType->getPointeeType();
auto templateParamType = cast<clang::TemplateTypeParmType>(pointeeType);
PointerTypeKind pointerKind = pointeeType.getQualifiers().hasConst()
? PTK_UnsafePointer
: PTK_UnsafeMutablePointer;
auto genericType =
findGenericTypeInGenericDecls(templateParamType, genericParams);
swiftParamTy = genericType->wrapInPointer(pointerKind);
} else if (auto *templateParamType =
dyn_cast<clang::TemplateTypeParmType>(paramTy)) {
swiftParamTy =
findGenericTypeInGenericDecls(templateParamType, genericParams);
} else {
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/templates/Inputs/function-templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ decltype(auto) testAuto(T arg) {
template <class T> struct Dep { using TT = T; };

template <class T> void useDependentType(typename Dep<T>::TT) {}

template <class T> void lvalueReference(T &ref) { ref = 42; }

template <class T> void constLvalueReference(const T &) {}

template <class T> void forwardingReference(T &&) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
// CHECK: func passThroughConst<T>(_ value: T) -> T
// CHECK: func returns_template<R, T, U>(_ a: T, _ b: U) -> R
// CHECK: func cannot_infer_template<T>()
// CHECK: func lvalueReference<T>(_ ref: UnsafeMutablePointer<T>)
// CHECK: func constLvalueReference<T>(_: UnsafePointer<T>)
// CHECK: func forwardingReference<T>(_: UnsafeMutablePointer<T>)
6 changes: 6 additions & 0 deletions test/Interop/Cxx/templates/function-template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ FunctionTemplateTestSuite.test("add<T, U> where T, U == Int") {
expectEqual(65, result)
}

FunctionTemplateTestSuite.test("lvalueReference<T> where T == Int") {
var value = 0
lvalueReference(&value)
expectEqual(value, 42)
}

// TODO: currently "Any" is imported as an Objective-C "id".
// This doesn't work without the Objective-C runtime.
#if _runtime(_ObjC)
Expand Down