Skip to content

[cxx-interop] Use a synthesized C++ method when invoking a base metho… #68846

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 4 commits into from
Oct 17, 2023
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsClangImporter.def
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ ERROR(move_only_requires_move_only,none,
"use of noncopyable C++ type '%0' requires -enable-experimental-move-only",
(StringRef))

ERROR(failed_base_method_call_synthesis,none,
"failed to synthesize call to the base method %0 of type %0",
(ValueDecl *, ValueDecl *))

NOTE(unsupported_builtin_type, none, "built-in type '%0' not supported", (StringRef))
NOTE(record_field_not_imported, none, "field %0 unavailable (cannot import)", (const clang::NamedDecl*))
NOTE(invoked_func_not_imported, none, "function %0 unavailable (cannot import)", (const clang::NamedDecl*))
Expand Down
515 changes: 454 additions & 61 deletions lib/ClangImporter/ClangImporter.cpp

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3621,6 +3621,10 @@ class CXXMethodConventions : public CFunctionTypeConventions {
// possible to make it easy for LLVM to optimize away the thunk.
return ResultConvention::Indirect;
}
if (TheDecl->hasAttr<clang::CFReturnsRetainedAttr>() &&
resultTL.getLoweredType().isForeignReferenceType()) {
return ResultConvention::Owned;
}
return CFunctionTypeConventions::getResult(resultTL);
}
static bool classof(const Conventions *C) {
Expand Down
34 changes: 34 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,37 @@ struct ClassTemplate {
};

struct DerivedFromClassTemplate : ClassTemplate<int> {};

int &getCopyCounter() {
static int copyCounter = 0;
return copyCounter;
}

class CopyTrackedBaseClass {
public:
CopyTrackedBaseClass(int x) : x(x) {}
CopyTrackedBaseClass(const CopyTrackedBaseClass &other) : x(other.x) {
++getCopyCounter();
}

int x;
};

class CopyTrackedDerivedClass: public CopyTrackedBaseClass {
public:
CopyTrackedDerivedClass(int x) : CopyTrackedBaseClass(x) {}
};

class NonEmptyBase {
public:
int getY() const {
return y;
}
private:
int y = 11;
};

class CopyTrackedDerivedDerivedClass: public NonEmptyBase, public CopyTrackedDerivedClass {
public:
CopyTrackedDerivedDerivedClass(int x) : CopyTrackedDerivedClass(x) {}
};
45 changes: 45 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,48 @@ struct DerivedFromEmptyBaseClass : EmptyBaseClass {
int a = 42;
int b = 42;
};

int &getCopyCounter() {
static int copyCounter = 0;
return copyCounter;
}

class CopyTrackedBaseClass {
public:
CopyTrackedBaseClass(int x) : x(x) {}
CopyTrackedBaseClass(const CopyTrackedBaseClass &other) : x(other.x) {
++getCopyCounter();
}

int getX() const {
return x;
}
int getXMut() {
return x;
}
private:
int x;
};

class CopyTrackedDerivedClass: public CopyTrackedBaseClass {
public:
CopyTrackedDerivedClass(int x) : CopyTrackedBaseClass(x) {}

int getDerivedX() const {
return getX();
}
};

class NonEmptyBase {
public:
int getY() const {
return y;
}
private:
int y = 11;
};

class CopyTrackedDerivedDerivedClass: public NonEmptyBase, public CopyTrackedDerivedClass {
public:
CopyTrackedDerivedDerivedClass(int x) : CopyTrackedDerivedClass(x) {}
};
4 changes: 4 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module SubTypes {
header "sub-types.h"
}

module Subscripts {
header "subscripts.h"
}

module TypeAliases {
header "type-aliases.h"
}
Expand Down
70 changes: 70 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/subscripts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
int &getCopyCounter() {
static int copyCounter = 0;
return copyCounter;
}

class CopyTrackedBaseClass {
public:
CopyTrackedBaseClass(int x) : x(x) {}
CopyTrackedBaseClass(const CopyTrackedBaseClass &other) : x(other.x) {
++getCopyCounter();
}

int operator [](int y) const {
return y + x;
}
private:
int x;
};

class CopyTrackedDerivedClass: public CopyTrackedBaseClass {
public:
CopyTrackedDerivedClass(int x) : CopyTrackedBaseClass(x) {}
};

class NonEmptyBase {
public:
int getY() const {
return y;
}
private:
int y = 11;
};

class CopyTrackedDerivedDerivedClass: public NonEmptyBase, public CopyTrackedDerivedClass {
public:
CopyTrackedDerivedDerivedClass(int x) : CopyTrackedDerivedClass(x) {}
};

class SubscriptReturnsRef {
public:
const int &operator [](int y) const {
return x[y];
}
int &operator [](int y) {
return x[y];
}

private:
int x[10] = {0};
};

class DerivedSubscriptReturnsRef: public SubscriptReturnsRef {
public:
inline DerivedSubscriptReturnsRef() : SubscriptReturnsRef() {}
};

class NonConstSubscriptReturnsRef {
public:
int &operator [](int y) {
return x[y];
}

private:
int x[10] = {0};
};

class DerivedNonConstSubscriptReturnsRef: public NonConstSubscriptReturnsRef {
public:
inline DerivedNonConstSubscriptReturnsRef() : NonConstSubscriptReturnsRef() {}
};
14 changes: 14 additions & 0 deletions test/Interop/Cxx/class/inheritance/fields-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-emit-irgen -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s

import Fields

func testGetX() -> CInt {
let derivedDerived = CopyTrackedDerivedDerivedClass(42)
return derivedDerived.x
}

let _ = testGetX()

// CHECK: define {{.*}}linkonce_odr{{.*}} i32 @{{.*}}__synthesizedBaseCall___synthesizedBaseGetterAccessor{{.*}}(ptr {{.*}} %[[THIS_PTR:.*]])
// CHECK: %[[ADD_PTR:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i64 4
// CHECK: call noundef i32 @{{.*}}__synthesizedBaseGetterAccessor{{.*}}(ptr {{.*}} %[[ADD_PTR]])
15 changes: 15 additions & 0 deletions test/Interop/Cxx/class/inheritance/fields-silgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-emit-sil -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none | %FileCheck %s

import Fields

func testGetX() -> CInt {
let derived = CopyTrackedDerivedClass(42)
return derived.x
}

let _ = testGetX()

// CHECK: sil shared [transparent] @$sSo23CopyTrackedDerivedClassV1xs5Int32Vvg : $@convention(method) (@in_guaranteed CopyTrackedDerivedClass) -> Int32
// CHECK: {{.*}}(%[[SELF_VAL:.*]] : $*CopyTrackedDerivedClass):
// CHECK: function_ref @{{.*}}__synthesizedBaseGetterAccessor_{{.*}} : $@convention(cxx_method) (@in_guaranteed CopyTrackedDerivedClass) -> Int32
// CHECK-NEXT: apply %{{.*}}(%[[SELF_VAL]])
27 changes: 27 additions & 0 deletions test/Interop/Cxx/class/inheritance/fields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,31 @@ FieldsTestSuite.test("Same field from derived") {
expectEqual(derived.a, 42)
}

extension CopyTrackedBaseClass {
var swiftProp: CInt {
return x
}
}

FieldsTestSuite.test("Get field without copying base in the getter accessor") {
let base = CopyTrackedBaseClass(0)
var copyCounter = getCopyCounter().pointee
expectEqual(base.swiftProp, 0)
// Measure copy counter of a regular
// property access for a C++ type to compare
// it to see if any additional copies are
// needed to access the property from the base class.
let expectedCopyCountDiff = getCopyCounter().pointee - copyCounter

let derived = CopyTrackedDerivedClass(234)
copyCounter = getCopyCounter().pointee
expectEqual(derived.x, 234)
expectEqual(copyCounter, getCopyCounter().pointee - expectedCopyCountDiff)

let derivedDerived = CopyTrackedDerivedDerivedClass(-11)
copyCounter = getCopyCounter().pointee
expectEqual(derivedDerived.x, -11)
expectEqual(copyCounter, getCopyCounter().pointee - expectedCopyCountDiff)
}

runAllTests()
17 changes: 17 additions & 0 deletions test/Interop/Cxx/class/inheritance/functions-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-emit-irgen -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s

import Functions

func testGetX() -> CInt {
let derivedDerived = CopyTrackedDerivedDerivedClass(42)
return derivedDerived.getX()
}

let _ = testGetX()

// CHECK: define {{.*}} swiftcc i32 @"$sSo018CopyTrackedDerivedC5ClassV4getXs5Int32VyF"(ptr noalias swiftself dereferenceable(8) %[[SELF_PTR:.*]])
// CHECK: = call i32 @[[SYNTH_METHOD:.*]](ptr %[[SELF_PTR]])

// CHECK: define {{.*}}linkonce_odr{{.*}} i32 @[[SYNTH_METHOD]](ptr {{.*}} %[[THIS_PTR:.*]])
// CHECK: %[[ADD_PTR:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i64 4
// CHECK: call noundef i32 @{{.*}}(ptr {{.*}} %[[ADD_PTR]])
15 changes: 15 additions & 0 deletions test/Interop/Cxx/class/inheritance/functions-silgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-emit-sil -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none | %FileCheck %s

import Functions

func testGetX() -> CInt {
let derived = CopyTrackedDerivedClass(42)
return derived.getX()
}

let _ = testGetX()

// CHECK: sil shared @$sSo23CopyTrackedDerivedClassV4getXs5Int32VyF : $@convention(method) (@in_guaranteed CopyTrackedDerivedClass) -> Int32
// CHECK: {{.*}}(%[[SELF_VAL:.*]] : $*CopyTrackedDerivedClass):
// CHECK: function_ref @{{.*}}__synthesizedBaseCall_{{.*}} : $@convention(cxx_method) (@in_guaranteed CopyTrackedDerivedClass) -> Int32
// CHECK-NEXT: apply %{{.*}}(%[[SELF_VAL]])
22 changes: 22 additions & 0 deletions test/Interop/Cxx/class/inheritance/functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,26 @@ FunctionsTestSuite.test("non-empty derived from empty class") {
expectEqual(derived.b, 42)
}

FunctionsTestSuite.test("base member calls do not require copying") {
let derived = CopyTrackedDerivedClass(42)
var copyCounter = getCopyCounter().pointee
expectEqual(derived.getX(), 42)
expectEqual(copyCounter, getCopyCounter().pointee)
expectEqual(derived.getDerivedX(), 42)
expectEqual(copyCounter, getCopyCounter().pointee)

let derivedDerived = CopyTrackedDerivedDerivedClass(-5)
copyCounter = getCopyCounter().pointee
expectEqual(derivedDerived.getX(), -5)
expectEqual(derivedDerived.getY(), 11)
expectEqual(copyCounter, getCopyCounter().pointee)
}

FunctionsTestSuite.test("mutating base member calls do not require copying") {
var derived = CopyTrackedDerivedClass(42)
var copyCounter = getCopyCounter().pointee
expectEqual(derived.getXMut(), 42)
expectEqual(copyCounter, getCopyCounter().pointee)
}

runAllTests()
14 changes: 14 additions & 0 deletions test/Interop/Cxx/class/inheritance/subscript-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-emit-irgen -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s

import Subscripts

func testGetX() -> CInt {
let derivedDerived = CopyTrackedDerivedDerivedClass(42)
return derivedDerived[0]
}

let _ = testGetX()

// CHECK: define {{.*}}linkonce_odr{{.*}} i32 @{{.*}}__synthesizedBaseCall___synthesizedBaseCall_operatorSubscript{{.*}}(ptr {{.*}} %[[THIS_PTR:.*]], i32 {{.*}})
// CHECK: %[[ADD_PTR:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i64 4
// CHECK: call noundef i32 @{{.*}}__synthesizedBaseCall_operatorSubscript{{.*}}(ptr {{.*}} %[[ADD_PTR]], i32 {{.*}})
15 changes: 15 additions & 0 deletions test/Interop/Cxx/class/inheritance/subscript-silgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-emit-sil -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none | %FileCheck %s

import Subscripts

func testGetX() -> CInt {
let derived = CopyTrackedDerivedClass(42)
return derived[0]
}

let _ = testGetX()

// CHECK: sil shared [transparent] @$sSo23CopyTrackedDerivedClassVys5Int32VADcig : $@convention(method) (Int32, @in_guaranteed CopyTrackedDerivedClass) -> Int32
// CHECK: {{.*}}(%[[INT_VAL:.*]] : $Int32, %[[SELF_VAL:.*]] : $*CopyTrackedDerivedClass):
// CHECK: function_ref @{{.*}}__synthesizedBaseCall_operatorSubscript{{.*}} : $@convention(cxx_method) (Int32, @in_guaranteed CopyTrackedDerivedClass) -> Int32
// CHECK-NEXT: apply %{{.*}}(%[[INT_VAL]], %[[SELF_VAL]])
25 changes: 25 additions & 0 deletions test/Interop/Cxx/class/inheritance/subscripts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
//
// REQUIRES: executable_test

import CxxShim
import StdlibUnittest
import Subscripts

var FieldsTestSuite = TestSuite("Getting and setting subscripts in base classes")

FieldsTestSuite.test("Subscript from derived returning ref") {
var derived = DerivedSubscriptReturnsRef()
expectEqual(derived[0], 0)
derived[0] = 42
expectEqual(derived[0], 42)
}

FieldsTestSuite.test("Non-const subscript from derived returning ref") {
var derived = DerivedNonConstSubscriptReturnsRef()
expectEqual(derived[1], 0)
derived[1] = -11
expectEqual(derived[1], -11)
}

runAllTests()
Loading