xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjCXX/arc-overloading.mm (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -fblocks -Wno-objc-root-class %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc// Simple ownership conversions + diagnostics.
4*f4a2713aSLionel Sambucint &f0(id __strong const *); // expected-note{{candidate function not viable: 1st argument ('__weak id *') has __weak ownership, but parameter has __strong ownership}}
5*f4a2713aSLionel Sambuc
6*f4a2713aSLionel Sambucvoid test_f0() {
7*f4a2713aSLionel Sambuc  id __strong *sip;
8*f4a2713aSLionel Sambuc  id __strong const *csip;
9*f4a2713aSLionel Sambuc  id __weak *wip;
10*f4a2713aSLionel Sambuc  id __autoreleasing *aip;
11*f4a2713aSLionel Sambuc  id __unsafe_unretained *uip;
12*f4a2713aSLionel Sambuc
13*f4a2713aSLionel Sambuc  int &ir1 = f0(sip);
14*f4a2713aSLionel Sambuc  int &ir2 = f0(csip);
15*f4a2713aSLionel Sambuc  int &ir3 = f0(aip);
16*f4a2713aSLionel Sambuc  int &ir4 = f0(uip);
17*f4a2713aSLionel Sambuc  f0(wip); // expected-error{{no matching function for call to 'f0'}}
18*f4a2713aSLionel Sambuc}
19*f4a2713aSLionel Sambuc
20*f4a2713aSLionel Sambuc// Simple overloading
21*f4a2713aSLionel Sambucint &f1(id __strong const *);
22*f4a2713aSLionel Sambucfloat &f1(id __weak const *);
23*f4a2713aSLionel Sambuc
24*f4a2713aSLionel Sambucvoid test_f1() {
25*f4a2713aSLionel Sambuc  id __strong *sip;
26*f4a2713aSLionel Sambuc  id __strong const *csip;
27*f4a2713aSLionel Sambuc  id __weak *wip;
28*f4a2713aSLionel Sambuc  id __autoreleasing *aip;
29*f4a2713aSLionel Sambuc  id __unsafe_unretained *uip;
30*f4a2713aSLionel Sambuc
31*f4a2713aSLionel Sambuc  int &ir1 = f1(sip);
32*f4a2713aSLionel Sambuc  int &ir2 = f1(csip);
33*f4a2713aSLionel Sambuc  float &fr1 = f1(wip);
34*f4a2713aSLionel Sambuc  int &ir3 = f1(aip);
35*f4a2713aSLionel Sambuc  int &ir4 = f1(uip);
36*f4a2713aSLionel Sambuc}
37*f4a2713aSLionel Sambuc
38*f4a2713aSLionel Sambuc// Simple overloading
39*f4a2713aSLionel Sambucint &f2(id __strong const *); // expected-note{{candidate function}}
40*f4a2713aSLionel Sambucfloat &f2(id __autoreleasing const *); // expected-note{{candidate function}}
41*f4a2713aSLionel Sambuc
42*f4a2713aSLionel Sambucvoid test_f2() {
43*f4a2713aSLionel Sambuc  id __strong *sip;
44*f4a2713aSLionel Sambuc  id __strong const *csip;
45*f4a2713aSLionel Sambuc  id __weak *wip;
46*f4a2713aSLionel Sambuc  id __autoreleasing *aip;
47*f4a2713aSLionel Sambuc  id __unsafe_unretained *uip;
48*f4a2713aSLionel Sambuc
49*f4a2713aSLionel Sambuc  // Prefer non-ownership conversions to ownership conversions.
50*f4a2713aSLionel Sambuc  int &ir1 = f2(sip);
51*f4a2713aSLionel Sambuc  int &ir2 = f2(csip);
52*f4a2713aSLionel Sambuc  float &fr1 = f2(aip);
53*f4a2713aSLionel Sambuc
54*f4a2713aSLionel Sambuc  f2(uip); // expected-error{{call to 'f2' is ambiguous}}
55*f4a2713aSLionel Sambuc}
56*f4a2713aSLionel Sambuc
57*f4a2713aSLionel Sambuc// Writeback conversion
58*f4a2713aSLionel Sambucint &f3(id __autoreleasing *); // expected-note{{candidate function not viable: 1st argument ('__unsafe_unretained id *') has __unsafe_unretained ownership, but parameter has __autoreleasing ownership}}
59*f4a2713aSLionel Sambuc
60*f4a2713aSLionel Sambucvoid test_f3() {
61*f4a2713aSLionel Sambuc  id __strong sip;
62*f4a2713aSLionel Sambuc  id __weak wip;
63*f4a2713aSLionel Sambuc  id __autoreleasing aip;
64*f4a2713aSLionel Sambuc  id __unsafe_unretained uip;
65*f4a2713aSLionel Sambuc
66*f4a2713aSLionel Sambuc  int &ir1 = f3(&sip);
67*f4a2713aSLionel Sambuc  int &ir2 = f3(&wip);
68*f4a2713aSLionel Sambuc  int &ir3 = f3(&aip);
69*f4a2713aSLionel Sambuc  f3(&uip); // expected-error{{no matching function for call to 'f3'}}
70*f4a2713aSLionel Sambuc}
71*f4a2713aSLionel Sambuc
72*f4a2713aSLionel Sambuc// Writeback conversion vs. no conversion
73*f4a2713aSLionel Sambucint &f4(id __autoreleasing *);
74*f4a2713aSLionel Sambucfloat &f4(id __strong *);
75*f4a2713aSLionel Sambuc
76*f4a2713aSLionel Sambucvoid test_f4() {
77*f4a2713aSLionel Sambuc  id __strong sip;
78*f4a2713aSLionel Sambuc  id __weak wip;
79*f4a2713aSLionel Sambuc  id __autoreleasing aip;
80*f4a2713aSLionel Sambuc  extern __weak id weak_global_ptr;
81*f4a2713aSLionel Sambuc
82*f4a2713aSLionel Sambuc  float &fr1 = f4(&sip);
83*f4a2713aSLionel Sambuc  int &ir1 = f4(&wip);
84*f4a2713aSLionel Sambuc  int &ir2 = f4(&aip);
85*f4a2713aSLionel Sambuc  int &ir3 = f4(&weak_global_ptr); // expected-error{{passing address of non-local object to __autoreleasing parameter for write-back}}
86*f4a2713aSLionel Sambuc}
87*f4a2713aSLionel Sambuc
88*f4a2713aSLionel Sambuc// Writeback conversion vs. other conversion.
89*f4a2713aSLionel Sambucint &f5(id __autoreleasing *);
90*f4a2713aSLionel Sambucfloat &f5(id const __unsafe_unretained *);
91*f4a2713aSLionel Sambuc
92*f4a2713aSLionel Sambucvoid test_f5() {
93*f4a2713aSLionel Sambuc  id __strong sip;
94*f4a2713aSLionel Sambuc  id __weak wip;
95*f4a2713aSLionel Sambuc  id __autoreleasing aip;
96*f4a2713aSLionel Sambuc
97*f4a2713aSLionel Sambuc  int &ir1 = f5(&wip);
98*f4a2713aSLionel Sambuc  float &fr1 = f5(&sip);
99*f4a2713aSLionel Sambuc  int &ir2 = f5(&aip);
100*f4a2713aSLionel Sambuc}
101*f4a2713aSLionel Sambuc
102*f4a2713aSLionel Sambuc@interface A
103*f4a2713aSLionel Sambuc@end
104*f4a2713aSLionel Sambuc
105*f4a2713aSLionel Sambucint &f6(id __autoreleasing *);
106*f4a2713aSLionel Sambucfloat &f6(id const __unsafe_unretained *);
107*f4a2713aSLionel Sambuc
108*f4a2713aSLionel Sambucvoid test_f6() {
109*f4a2713aSLionel Sambuc  A* __strong sip;
110*f4a2713aSLionel Sambuc  A* __weak wip;
111*f4a2713aSLionel Sambuc  A* __autoreleasing aip;
112*f4a2713aSLionel Sambuc
113*f4a2713aSLionel Sambuc  int &ir1 = f6(&wip);
114*f4a2713aSLionel Sambuc  float &fr1 = f6(&sip);
115*f4a2713aSLionel Sambuc  int &ir2 = f6(&aip);
116*f4a2713aSLionel Sambuc}
117*f4a2713aSLionel Sambuc
118*f4a2713aSLionel Sambuc// Reference binding
119*f4a2713aSLionel Sambucvoid f7(__strong id&); // expected-note{{candidate function not viable: 1st argument ('__weak id') has __weak ownership, but parameter has __strong ownership}} \
120*f4a2713aSLionel Sambuc // expected-note{{candidate function not viable: 1st argument ('__autoreleasing id') has __autoreleasing ownership, but parameter has __strong ownership}} \
121*f4a2713aSLionel Sambuc // expected-note{{candidate function not viable: 1st argument ('__unsafe_unretained id') has __unsafe_unretained ownership, but parameter has __strong ownership}}
122*f4a2713aSLionel Sambuc
123*f4a2713aSLionel Sambucvoid test_f7() {
124*f4a2713aSLionel Sambuc  __strong id strong_id;
125*f4a2713aSLionel Sambuc  __weak id weak_id;
126*f4a2713aSLionel Sambuc  __autoreleasing id autoreleasing_id;
127*f4a2713aSLionel Sambuc  __unsafe_unretained id unsafe_id;
128*f4a2713aSLionel Sambuc  f7(strong_id);
129*f4a2713aSLionel Sambuc  f7(weak_id); // expected-error{{no matching function for call to 'f7'}}
130*f4a2713aSLionel Sambuc  f7(autoreleasing_id); // expected-error{{no matching function for call to 'f7'}}
131*f4a2713aSLionel Sambuc  f7(unsafe_id); // expected-error{{no matching function for call to 'f7'}}
132*f4a2713aSLionel Sambuc}
133*f4a2713aSLionel Sambuc
134*f4a2713aSLionel Sambucvoid f8(const __strong id&);
135*f4a2713aSLionel Sambuc
136*f4a2713aSLionel Sambucvoid test_f8() {
137*f4a2713aSLionel Sambuc  __strong id strong_id;
138*f4a2713aSLionel Sambuc  __weak id weak_id;
139*f4a2713aSLionel Sambuc  __autoreleasing id autoreleasing_id;
140*f4a2713aSLionel Sambuc  __unsafe_unretained id unsafe_id;
141*f4a2713aSLionel Sambuc
142*f4a2713aSLionel Sambuc  f8(strong_id);
143*f4a2713aSLionel Sambuc  f8(weak_id);
144*f4a2713aSLionel Sambuc  f8(autoreleasing_id);
145*f4a2713aSLionel Sambuc  f8(unsafe_id);
146*f4a2713aSLionel Sambuc}
147*f4a2713aSLionel Sambuc
148*f4a2713aSLionel Sambucint &f9(__strong id&);
149*f4a2713aSLionel Sambucfloat &f9(const __autoreleasing id&);
150*f4a2713aSLionel Sambuc
151*f4a2713aSLionel Sambucvoid test_f9() {
152*f4a2713aSLionel Sambuc  __strong id strong_id;
153*f4a2713aSLionel Sambuc  __weak id weak_id;
154*f4a2713aSLionel Sambuc  __autoreleasing id autoreleasing_id;
155*f4a2713aSLionel Sambuc  __unsafe_unretained id unsafe_id;
156*f4a2713aSLionel Sambuc
157*f4a2713aSLionel Sambuc  int &ir1 = f9(strong_id);
158*f4a2713aSLionel Sambuc  float &fr1 = f9(autoreleasing_id);
159*f4a2713aSLionel Sambuc  float &fr2 = f9(unsafe_id);
160*f4a2713aSLionel Sambuc  float &fr2a = f9(weak_id);
161*f4a2713aSLionel Sambuc
162*f4a2713aSLionel Sambuc  __strong A *strong_a;
163*f4a2713aSLionel Sambuc  __weak A *weak_a;
164*f4a2713aSLionel Sambuc  __autoreleasing A *autoreleasing_a;
165*f4a2713aSLionel Sambuc  __unsafe_unretained A *unsafe_unretained_a;
166*f4a2713aSLionel Sambuc  float &fr3 = f9(strong_a);
167*f4a2713aSLionel Sambuc  float &fr4 = f9(autoreleasing_a);
168*f4a2713aSLionel Sambuc  float &fr5 = f9(unsafe_unretained_a);
169*f4a2713aSLionel Sambuc  float &fr6 = f9(weak_a);
170*f4a2713aSLionel Sambuc
171*f4a2713aSLionel Sambuc  const __autoreleasing id& ar1 = strong_a;
172*f4a2713aSLionel Sambuc  const __autoreleasing id& ar2 = autoreleasing_a;
173*f4a2713aSLionel Sambuc  const __autoreleasing id& ar3 = unsafe_unretained_a;
174*f4a2713aSLionel Sambuc  const __autoreleasing id& ar4 = weak_a;
175*f4a2713aSLionel Sambuc}
176*f4a2713aSLionel Sambuc
177*f4a2713aSLionel Sambuc// rdar://9790531
178*f4a2713aSLionel Sambucvoid f9790531(void *inClientData); // expected-note {{candidate function not viable: cannot implicitly convert argument of type 'MixerEQGraphTestDelegate *const __strong' to 'void *' for 1st argument under ARC}}
179*f4a2713aSLionel Sambucvoid f9790531_1(struct S*inClientData); // expected-note {{candidate function not viable}}
180*f4a2713aSLionel Sambucvoid f9790531_2(char * inClientData); // expected-note {{candidate function not viable}}
181*f4a2713aSLionel Sambuc
182*f4a2713aSLionel Sambuc@class UIApplication;
183*f4a2713aSLionel Sambuc
184*f4a2713aSLionel Sambuc@interface MixerEQGraphTestDelegate
185*f4a2713aSLionel Sambuc- (void)applicationDidFinishLaunching;
186*f4a2713aSLionel Sambuc@end
187*f4a2713aSLionel Sambuc
188*f4a2713aSLionel Sambuc@implementation MixerEQGraphTestDelegate
189*f4a2713aSLionel Sambuc- (void)applicationDidFinishLaunching {
190*f4a2713aSLionel Sambuc    f9790531(self); // expected-error {{no matching function for call to 'f9790531'}}
191*f4a2713aSLionel Sambuc    f9790531_1(self); // expected-error {{no matching function for call to 'f9790531_1'}}
192*f4a2713aSLionel Sambuc    f9790531_2(self); // expected-error {{no matching function for call to 'f9790531_2'}}
193*f4a2713aSLionel Sambuc}
194*f4a2713aSLionel Sambuc@end
195*f4a2713aSLionel Sambuc
196*f4a2713aSLionel Sambucclass rdar10142572 {
197*f4a2713aSLionel Sambuc  id f() __attribute__((ns_returns_retained));
198*f4a2713aSLionel Sambuc  id g(); // expected-note{{previous declaration}}
199*f4a2713aSLionel Sambuc};
200*f4a2713aSLionel Sambuc
201*f4a2713aSLionel Sambucid rdar10142572::f() { return 0; } // okay: merged down
202*f4a2713aSLionel Sambucid __attribute__((ns_returns_retained)) rdar10142572::g() { return 0; } // expected-error{{function declared with the ns_returns_retained attribute was previously declared without the ns_returns_retained attribute}}
203