Skip to content

Commit 926653d

Browse files
committed
[cxx-interop][SwiftToCxx] Print operator keyword when mapping Swift operator to C++
Previously we emitted this: ``` SWIFT_INLINE_THUNK bool ==(const IntBox& lhs, const IntBox& rhs) ``` which is not valid in C++. Now we'll emit this: ``` SWIFT_INLINE_THUNK bool operator==(const IntBox& lhs, const IntBox& rhs) ``` rdar://114772296
1 parent ba06693 commit 926653d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/AST/SwiftNameTranslation.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ isVisibleToObjC(const ValueDecl *VD, AccessLevel minRequiredAccess,
156156
StringRef
157157
swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
158158
CustomNamesOnly_t customNamesOnly) {
159+
ASTContext& ctx = VD->getASTContext();
160+
159161
if (const auto *Expose = VD->getAttrs().getAttribute<ExposeAttr>()) {
160162
if (!Expose->Name.empty())
161163
return Expose->Name;
@@ -167,6 +169,11 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
167169
if (isa<ConstructorDecl>(VD))
168170
return "init";
169171

172+
if (VD->isOperator()) {
173+
std::string name = ("operator" + VD->getBaseIdentifier().str()).str();
174+
return ctx.getIdentifier(name).str();
175+
}
176+
170177
if (auto *mod = dyn_cast<ModuleDecl>(VD)) {
171178
if (mod->isStdlibModule())
172179
return "swift";
@@ -188,7 +195,7 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
188195
os << char(std::toupper(paramNameStr[0]));
189196
os << paramNameStr.drop_front(1);
190197
}
191-
auto r = VD->getASTContext().getIdentifier(os.str());
198+
auto r = ctx.getIdentifier(os.str());
192199
return r.str();
193200
}
194201

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Operators -clang-header-expose-decls=all-public -emit-clang-header-path %t/operators.h
3+
// RUN: %FileCheck %s < %t/operators.h
4+
5+
// TODO: %check-interop-cxx-header-in-clang(%t/operators.h)
6+
// unfortunately the header still triggers an error:
7+
// error: no member named '_impl_IntBox' in namespace 'Operators::_impl'
8+
9+
// CHECK-LABEL: namespace Operators SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Operators") {
10+
11+
// CHECK-LABEL: namespace _impl {
12+
13+
// CHECK: SWIFT_EXTERN bool $s9Operators2eeoiySbAA6IntBoxV_ADtF(struct swift_interop_passStub_Operators_uint32_t_0_4 lhs, struct swift_interop_passStub_Operators_uint32_t_0_4 rhs) SWIFT_NOEXCEPT SWIFT_CALL; // ==(_:_:)
14+
15+
// CHECK: }
16+
17+
public struct IntBox { var x: CInt }
18+
19+
public func -(lhs: IntBox, rhs: IntBox) -> CInt {
20+
return lhs.x - rhs.x
21+
}
22+
23+
// CHECK: SWIFT_INLINE_THUNK int operator-(const IntBox& lhs, const IntBox& rhs) noexcept SWIFT_SYMBOL("s:9Operators1soiys5Int32VAA6IntBoxV_AFtF") SWIFT_WARN_UNUSED_RESULT {
24+
// CHECK: return _impl::$s9Operators1soiys5Int32VAA6IntBoxV_AFtF(_impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(lhs)), _impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(rhs)));
25+
// CHECK: }
26+
27+
public func ==(lhs: IntBox, rhs: IntBox) -> Bool {
28+
return lhs.x == rhs.x
29+
}
30+
31+
// CHECK: SWIFT_INLINE_THUNK bool operator==(const IntBox& lhs, const IntBox& rhs) noexcept SWIFT_SYMBOL("s:9Operators2eeoiySbAA6IntBoxV_ADtF") SWIFT_WARN_UNUSED_RESULT {
32+
// CHECK: return _impl::$s9Operators2eeoiySbAA6IntBoxV_ADtF(_impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(lhs)), _impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(rhs)));
33+
// CHECK: }

0 commit comments

Comments
 (0)