Skip to content

Commit 2028931

Browse files
authored
Merge pull request #60068 from apple/egorzhdan/cxx-operator-exclaim-prefix
[cxx-interop] Mark `operator!` as `prefix func`
2 parents 5a33fc9 + 1be0086 commit 2028931

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,9 +1881,13 @@ synthesizeOperatorMethodBody(AbstractFunctionDecl *afd, void *context) {
18811881
FuncDecl *
18821882
SwiftDeclSynthesizer::makeOperator(FuncDecl *operatorMethod,
18831883
clang::CXXMethodDecl *clangOperator) {
1884+
clang::OverloadedOperatorKind opKind = clangOperator->getOverloadedOperator();
1885+
1886+
assert(opKind != clang::OverloadedOperatorKind::OO_None &&
1887+
"expected a C++ operator");
1888+
18841889
auto &ctx = ImporterImpl.SwiftContext;
1885-
auto opName =
1886-
clang::getOperatorSpelling(clangOperator->getOverloadedOperator());
1890+
auto opName = clang::getOperatorSpelling(opKind);
18871891
auto paramList = operatorMethod->getParameters();
18881892
auto genericParamList = operatorMethod->getGenericParams();
18891893

@@ -1935,6 +1939,11 @@ SwiftDeclSynthesizer::makeOperator(FuncDecl *operatorMethod,
19351939
topLevelStaticFuncDecl->setBodySynthesizer(synthesizeOperatorMethodBody,
19361940
operatorMethod);
19371941

1942+
// If this is a unary prefix operator (e.g. `!`), add a `prefix` attribute.
1943+
if (clangOperator->param_empty()) {
1944+
topLevelStaticFuncDecl->getAttrs().add(new (ctx) PrefixAttr(SourceLoc()));
1945+
}
1946+
19381947
return topLevelStaticFuncDecl;
19391948
}
19401949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// CHECK: }
1010

1111
// CHECK: struct LoadableBoolWrapper {
12-
// CHECK: static func ! (lhs: inout LoadableBoolWrapper) -> LoadableBoolWrapper
12+
// CHECK: prefix static func ! (lhs: inout LoadableBoolWrapper) -> LoadableBoolWrapper
1313
// CHECK: }
1414

1515
// CHECK: struct AddressOnlyIntWrapper {

test/Interop/Cxx/operators/member-inline-silgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public func exclaim(_ wrapper: inout LoadableBoolWrapper) -> LoadableBoolWrapper
1616
// CHECK: bb0([[SELF:%.*]] : $*LoadableBoolWrapper):
1717
// CHECK: [[METATYPE:%.*]] = metatype $@thin LoadableBoolWrapper.Type
1818
// CHECK: [[SELFACCESS:%.*]] = begin_access [modify] [static] [[SELF]] : $*LoadableBoolWrapper
19-
// CHECK: [[OP:%.*]] = function_ref @$sSo19LoadableBoolWrapperV1noiyA2BzFZ : $@convention(method) (@inout LoadableBoolWrapper, @thin LoadableBoolWrapper.Type) -> LoadableBoolWrapper
19+
// CHECK: [[OP:%.*]] = function_ref @$sSo19LoadableBoolWrapperV1nopyA2BzFZ : $@convention(method) (@inout LoadableBoolWrapper, @thin LoadableBoolWrapper.Type) -> LoadableBoolWrapper
2020
// CHECK: apply [[OP]]([[SELFACCESS]], [[METATYPE]]) : $@convention(method) (@inout LoadableBoolWrapper, @thin LoadableBoolWrapper.Type) -> LoadableBoolWrapper
2121
// CHECK: end_access [[SELFACCESS]]
2222

0 commit comments

Comments
 (0)