Skip to content

Commit 9e755c2

Browse files
committed
Merge pull request #1519 from kballard/objc-blocks-include-param-names
[Swift 2.2][PrintAsObjC] Print argument names for function/block types
2 parents e19dc7c + 51c4e53 commit 9e755c2

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,12 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
614614

615615
if (!clangTy.isNull() && isNSUInteger(clangTy)) {
616616
os << "NSUInteger " << objCName;
617+
if (hasReservedName)
618+
os << "_";
617619
} else {
618-
print(ty, OTK_None, objCName.str());
620+
print(ty, OTK_None, objCName);
619621
}
620622

621-
if (hasReservedName)
622-
os << "_";
623-
624623
os << ";\n";
625624
}
626625

@@ -1123,8 +1122,10 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
11231122
if (tupleTy->getNumElements() == 0) {
11241123
os << "void";
11251124
} else {
1126-
interleave(tupleTy->getElementTypes(),
1127-
[this](Type ty) { print(ty, OTK_None); },
1125+
interleave(tupleTy->getElements(),
1126+
[this](TupleTypeElt elt) {
1127+
print(elt.getType(), OTK_None, elt.getName());
1128+
},
11281129
[this] { os << ", "; });
11291130
}
11301131
} else {
@@ -1175,15 +1176,19 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
11751176
/// visitPart().
11761177
public:
11771178
void print(Type ty, Optional<OptionalTypeKind> optionalKind,
1178-
StringRef name = "") {
1179+
Identifier name = Identifier()) {
11791180
PrettyStackTraceType trace(M.getASTContext(), "printing", ty);
11801181

11811182
decltype(openFunctionTypes) savedFunctionTypes;
11821183
savedFunctionTypes.swap(openFunctionTypes);
11831184

11841185
visitPart(ty, optionalKind);
1185-
if (!name.empty())
1186+
if (!name.empty()) {
11861187
os << ' ' << name;
1188+
if (isClangKeyword(name)) {
1189+
os << '_';
1190+
}
1191+
}
11871192
while (!openFunctionTypes.empty()) {
11881193
const FunctionType *openFunctionTy = openFunctionTypes.pop_back_val();
11891194
finishFunctionType(openFunctionTy);

test/PrintAsObjC/blocks.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import ObjectiveC
1313

14-
typealias MyTuple = (a: Int, b: AnyObject?)
14+
typealias MyTuple = (Int, AnyObject?)
15+
typealias MyNamedTuple = (a: Int, b: AnyObject?)
1516
typealias MyInt = Int
1617

1718
// CHECK-LABEL: @interface Callbacks
@@ -26,11 +27,21 @@ typealias MyInt = Int
2627
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull, NSObject * _Nonnull))returnsBlockWithTwoInputs;
2728
// CHECK-NEXT: - (void)blockWithTypealias:(NSInteger (^ _Nonnull)(NSInteger, id _Nullable))input;
2829
// CHECK-NEXT: - (void)blockWithSimpleTypealias:(NSInteger (^ _Nonnull)(NSInteger))input;
30+
// CHECK-NEXT: - (void)namedArguments:(void (^ _Nonnull)(float f1, float f2, double d1, double d2))input;
31+
// CHECK-NEXT: - (void)blockTakesNamedBlock:(void (^ _Nonnull)(void (^ _Nonnull block)(void)))input;
32+
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull object))returnsBlockWithNamedInput;
33+
// CHECK-NEXT: - (void)blockWithTypealiasWithNames:(NSInteger (^ _Nonnull)(NSInteger a, id _Nullable b))input;
34+
// CHECK-NEXT: - (void)blockWithKeyword:(NSInteger (^ _Nonnull)(NSInteger class_))_Nullable_;
2935
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointers:(NSInteger (* _Nonnull)(NSInteger))input;
3036
// CHECK-NEXT: - (void)functionPointerTakesAndReturnsFunctionPointer:(NSInteger (* _Nonnull (^ _Nonnull (* _Nonnull)(NSInteger))(NSInteger))(NSInteger))input;
37+
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger result))functionPointersWithName:(NSInteger (* _Nonnull)(NSInteger value))input;
3138
// CHECK-NEXT: @property (nonatomic, copy) NSInteger (^ _Nullable savedBlock)(NSInteger);
39+
// CHECK-NEXT: @property (nonatomic, copy) NSInteger (^ _Nullable savedBlockWithName)(NSInteger x);
3240
// CHECK-NEXT: @property (nonatomic) NSInteger (* _Nonnull savedFunctionPointer)(NSInteger);
3341
// CHECK-NEXT: @property (nonatomic) NSInteger (* _Nullable savedFunctionPointer2)(NSInteger);
42+
// CHECK-NEXT: @property (nonatomic) NSInteger (* _Nonnull savedFunctionPointerWithName)(NSInteger x);
43+
// CHECK-NEXT: @property (nonatomic, copy, getter=this, setter=setThis:) NSInteger (^ _Nonnull this_)(NSInteger block);
44+
// CHECK-NEXT: @property (nonatomic, getter=class, setter=setClass:) NSInteger (* _Nonnull class_)(NSInteger function);
3445
// CHECK-NEXT: init
3546
// CHECK-NEXT: @end
3647
@objc class Callbacks {
@@ -62,6 +73,16 @@ typealias MyInt = Int
6273
func blockWithTypealias(input: MyTuple -> MyInt) {}
6374
func blockWithSimpleTypealias(input: MyInt -> MyInt) {}
6475

76+
func namedArguments(input: (f1: Float, f2: Float, d1: Double, d2: Double) -> ()) {}
77+
func blockTakesNamedBlock(input: (block: () -> ()) -> ()) {}
78+
func returnsBlockWithNamedInput() -> ((object: NSObject) -> ())? {
79+
return nil
80+
}
81+
82+
func blockWithTypealiasWithNames(input: MyNamedTuple -> MyInt) {}
83+
84+
func blockWithKeyword(_Nullable: (`class`: Int) -> Int) {}
85+
6586
func functionPointers(input: @convention(c) Int -> Int)
6687
-> @convention(c) Int -> Int {
6788
return input
@@ -73,7 +94,18 @@ typealias MyInt = Int
7394
) {
7495
}
7596

97+
func functionPointersWithName(input: @convention(c) (value: Int) -> Int)
98+
-> @convention(c) (result: Int) -> Int {
99+
return input
100+
}
101+
76102
var savedBlock: (Int -> Int)?
103+
var savedBlockWithName: ((x: Int) -> Int)?
77104
var savedFunctionPointer: @convention(c) Int -> Int = { $0 }
78105
var savedFunctionPointer2: (@convention(c) Int -> Int)? = { $0 }
106+
var savedFunctionPointerWithName: @convention(c) (x: Int) -> Int = { $0 }
107+
108+
// The following uses a clang keyword as the name.
109+
var this: (block: Int) -> Int = { $0 }
110+
var `class`: @convention(c) (function: Int) -> Int = { $0 }
79111
}

0 commit comments

Comments
 (0)