Skip to content

Commit 1811125

Browse files
committed
[cxx-interop] Import operator bool() as an underscored function
1 parent 9888ffd commit 1811125

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ static bool isErrorOutParameter(const clang::ParmVarDecl *param,
155155

156156
static bool isBoolType(clang::ASTContext &ctx, clang::QualType type) {
157157
do {
158+
if (type->isBooleanType())
159+
return true;
160+
158161
// Check whether we have a typedef for "BOOL" or "Boolean".
159162
if (auto typedefType = dyn_cast<clang::TypedefType>(type.getTypePtr())) {
160163
auto typedefDecl = typedefType->getDecl();
@@ -1845,7 +1848,20 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18451848
break;
18461849
}
18471850

1848-
case clang::DeclarationName::CXXConversionFunctionName:
1851+
case clang::DeclarationName::CXXConversionFunctionName: {
1852+
auto conversionDecl = dyn_cast<clang::CXXConversionDecl>(D);
1853+
if (!conversionDecl)
1854+
return ImportedName();
1855+
auto toType = conversionDecl->getConversionType();
1856+
// Only import `operator bool()` for now.
1857+
if (isBoolType(clangSema.Context, toType)) {
1858+
isFunction = true;
1859+
baseName = "__convertToBool";
1860+
addEmptyArgNamesForClangFunction(conversionDecl, argumentNames);
1861+
break;
1862+
}
1863+
return ImportedName();
1864+
}
18491865
case clang::DeclarationName::CXXDestructorName:
18501866
case clang::DeclarationName::CXXLiteralOperatorName:
18511867
case clang::DeclarationName::CXXUsingDirective:

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct LoadableIntWrapper {
2222
return value + x * y;
2323
}
2424

25+
operator int() const { return value; }
26+
2527
LoadableIntWrapper &operator++() {
2628
value++;
2729
return *this;
@@ -48,6 +50,7 @@ struct LoadableBoolWrapper {
4850
LoadableBoolWrapper operator!() {
4951
return LoadableBoolWrapper{.value = !value};
5052
}
53+
operator bool() const { return value; }
5154
};
5255

5356
template<class T>

test/Interop/Cxx/operators/member-inline-module-interface.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// CHECK: struct LoadableBoolWrapper {
1313
// CHECK: prefix static func ! (lhs: inout LoadableBoolWrapper) -> LoadableBoolWrapper
14+
// CHECK: func __convertToBool() -> Bool
1415
// CHECK: }
1516

1617
// CHECK: struct AddressOnlyIntWrapper {

0 commit comments

Comments
 (0)