Skip to content

Commit eef1428

Browse files
committed
PrintAsObjC: Support for blocks with __owned parameters
1 parent 6d5ff20 commit eef1428

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,18 @@ class DeclAndTypePrinter::Implementation
19341934
if (!FT->getParams().empty()) {
19351935
interleave(FT->getParams(),
19361936
[this](const AnyFunctionType::Param &param) {
1937-
print(param.getOldType(), OTK_None, param.getLabel(),
1937+
switch (param.getValueOwnership()) {
1938+
case ValueOwnership::Default:
1939+
case ValueOwnership::Shared:
1940+
break;
1941+
case ValueOwnership::Owned:
1942+
os << "SWIFT_RELEASES_ARGUMENT ";
1943+
break;
1944+
case ValueOwnership::InOut:
1945+
llvm_unreachable("bad specifier");
1946+
}
1947+
1948+
print(param.getParameterType(), OTK_None, param.getLabel(),
19381949
IsFunctionParam);
19391950
},
19401951
[this] { os << ", "; });

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx) {
112112
"#else\n"
113113
"# define SWIFT_NOESCAPE\n"
114114
"#endif\n"
115+
"#if __has_attribute(ns_consumed)\n"
116+
"# define SWIFT_RELEASES_ARGUMENT __attribute__((ns_consumed))\n"
117+
"#else\n"
118+
"# define SWIFT_RELEASES_ARGUMENT\n"
119+
"#endif\n"
115120
"#if __has_attribute(warn_unused_result)\n"
116121
"# define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n"
117122
"#else\n"

test/PrintAsObjC/blocks.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
113113
return input
114114
}
115115

116+
// CHECK-NEXT: - (void)blockWithConsumingArgument:(void (^ _Nonnull)(SWIFT_RELEASES_ARGUMENT NSObject * _Nonnull))block;
117+
@objc func blockWithConsumingArgument(_ block: @escaping (__owned NSObject) -> ()) {}
118+
116119
// CHECK-NEXT: @property (nonatomic, copy) NSInteger (^ _Nullable savedBlock)(NSInteger);
117120
@objc var savedBlock: ((Int) -> Int)?
118121

0 commit comments

Comments
 (0)