xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjCXX/overload.mm (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class %s
2*f4a2713aSLionel Sambuc@interface Foo
3*f4a2713aSLionel Sambuc@end
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc@implementation Foo
6*f4a2713aSLionel Sambuc
7*f4a2713aSLionel Sambucvoid func(id);
8*f4a2713aSLionel Sambuc
9*f4a2713aSLionel Sambuc+ zone {
10*f4a2713aSLionel Sambuc func(self);
11*f4a2713aSLionel Sambuc return self;
12*f4a2713aSLionel Sambuc}
13*f4a2713aSLionel Sambuc@end
14*f4a2713aSLionel Sambuc
15*f4a2713aSLionel Sambuc@protocol P0
16*f4a2713aSLionel Sambuc@end
17*f4a2713aSLionel Sambuc
18*f4a2713aSLionel Sambuc@protocol P1
19*f4a2713aSLionel Sambuc@end
20*f4a2713aSLionel Sambuc
21*f4a2713aSLionel Sambuc@interface A <P0>
22*f4a2713aSLionel Sambuc@end
23*f4a2713aSLionel Sambuc
24*f4a2713aSLionel Sambuc@interface B : A
25*f4a2713aSLionel Sambuc@end
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc@interface C <P1>
28*f4a2713aSLionel Sambuc@end
29*f4a2713aSLionel Sambuc
30*f4a2713aSLionel Sambucint& f(A*); // expected-note {{candidate}}
31*f4a2713aSLionel Sambucfloat& f(B*); // expected-note {{candidate}}
32*f4a2713aSLionel Sambucvoid g(A*);
33*f4a2713aSLionel Sambuc
34*f4a2713aSLionel Sambucint& h(A*);
35*f4a2713aSLionel Sambucfloat& h(id);
36*f4a2713aSLionel Sambuc
37*f4a2713aSLionel Sambucvoid test0(A* a, B* b, id val) {
38*f4a2713aSLionel Sambuc  int& i1 = f(a);
39*f4a2713aSLionel Sambuc  float& f1 = f(b);
40*f4a2713aSLionel Sambuc
41*f4a2713aSLionel Sambuc  // GCC succeeds here, which is clearly ridiculous.
42*f4a2713aSLionel Sambuc  float& f2 = f(val); // expected-error {{ambiguous}}
43*f4a2713aSLionel Sambuc
44*f4a2713aSLionel Sambuc  g(a);
45*f4a2713aSLionel Sambuc  g(b);
46*f4a2713aSLionel Sambuc  g(val);
47*f4a2713aSLionel Sambuc  int& i2 = h(a);
48*f4a2713aSLionel Sambuc  float& f3 = h(val);
49*f4a2713aSLionel Sambuc
50*f4a2713aSLionel Sambuc  int& i3 = h(b);
51*f4a2713aSLionel Sambuc}
52*f4a2713aSLionel Sambuc
53*f4a2713aSLionel Sambucvoid test1(A* a) {
54*f4a2713aSLionel Sambuc  B* b = a; // expected-warning{{incompatible pointer types initializing 'B *' with an expression of type 'A *'}}
55*f4a2713aSLionel Sambuc  B *c; c = a; // expected-warning{{incompatible pointer types assigning to 'B *' from 'A *'}}
56*f4a2713aSLionel Sambuc}
57*f4a2713aSLionel Sambuc
58*f4a2713aSLionel Sambucvoid test2(A** ap) {
59*f4a2713aSLionel Sambuc  B** bp = ap; // expected-warning{{incompatible pointer types initializing 'B **' with an expression of type 'A **'}}
60*f4a2713aSLionel Sambuc  bp = ap; // expected-warning{{incompatible pointer types assigning to 'B **' from 'A **'}}
61*f4a2713aSLionel Sambuc}
62*f4a2713aSLionel Sambuc
63*f4a2713aSLionel Sambucint& cv(A*);
64*f4a2713aSLionel Sambucfloat& cv(const A*);
65*f4a2713aSLionel Sambuc
66*f4a2713aSLionel Sambucint& cv2(void*);
67*f4a2713aSLionel Sambucfloat& cv2(const void*);
68*f4a2713aSLionel Sambuc
69*f4a2713aSLionel Sambucvoid cv_test(A* a, B* b, const A* ac, const B* bc) {
70*f4a2713aSLionel Sambuc  int &i1 = cv(a);
71*f4a2713aSLionel Sambuc  int &i2 = cv(b);
72*f4a2713aSLionel Sambuc  float &f1 = cv(ac);
73*f4a2713aSLionel Sambuc  float &f2 = cv(bc);
74*f4a2713aSLionel Sambuc  int& i3 = cv2(a);
75*f4a2713aSLionel Sambuc  float& f3 = cv2(ac);
76*f4a2713aSLionel Sambuc}
77*f4a2713aSLionel Sambuc
78*f4a2713aSLionel Sambucint& qualid(id<P0>);
79*f4a2713aSLionel Sambucfloat& qualid(id<P1>);
80*f4a2713aSLionel Sambuc
81*f4a2713aSLionel Sambucvoid qualid_test(A *a, B *b, C *c) {
82*f4a2713aSLionel Sambuc  int& i1 = qualid(a);
83*f4a2713aSLionel Sambuc  int& i2 = qualid(b);
84*f4a2713aSLionel Sambuc
85*f4a2713aSLionel Sambuc  float& f1 = qualid(c);
86*f4a2713aSLionel Sambuc
87*f4a2713aSLionel Sambuc  id<P0> p1 = 0;
88*f4a2713aSLionel Sambuc  p1 = 0;
89*f4a2713aSLionel Sambuc}
90*f4a2713aSLionel Sambuc
91*f4a2713aSLionel Sambuc
92*f4a2713aSLionel Sambuc@class NSException;
93*f4a2713aSLionel Sambuctypedef struct {
94*f4a2713aSLionel Sambuc    void (*throw_exc)(id);
95*f4a2713aSLionel Sambuc}
96*f4a2713aSLionel Sambucobjc_exception_functions_t;
97*f4a2713aSLionel Sambuc
98*f4a2713aSLionel Sambucvoid (*_NSExceptionRaiser(void))(NSException *) {
99*f4a2713aSLionel Sambuc    objc_exception_functions_t exc_funcs;
100*f4a2713aSLionel Sambuc    return exc_funcs.throw_exc; // expected-warning{{incompatible pointer types returning 'void (*)(id)' from a function with result type 'void (*)(NSException *)'}}
101*f4a2713aSLionel Sambuc}
102*f4a2713aSLionel Sambuc
103*f4a2713aSLionel Sambucnamespace test5 {
104*f4a2713aSLionel Sambuc  void foo(bool);
105*f4a2713aSLionel Sambuc  void foo(void *);
106*f4a2713aSLionel Sambuc
107*f4a2713aSLionel Sambuc  void test(id p) {
108*f4a2713aSLionel Sambuc    foo(p);
109*f4a2713aSLionel Sambuc  }
110*f4a2713aSLionel Sambuc}
111*f4a2713aSLionel Sambuc
112*f4a2713aSLionel Sambuc// rdar://problem/8592139
113*f4a2713aSLionel Sambucnamespace test6 {
114*f4a2713aSLionel Sambuc  void foo(id); // expected-note{{candidate function}}
115*f4a2713aSLionel Sambuc  void foo(A*) __attribute__((unavailable)); // expected-note {{explicitly made unavailable}}
116*f4a2713aSLionel Sambuc
117*f4a2713aSLionel Sambuc  void test(B *b) {
118*f4a2713aSLionel Sambuc    foo(b); // expected-error {{call to unavailable function 'foo'}}
119*f4a2713aSLionel Sambuc  }
120*f4a2713aSLionel Sambuc}
121*f4a2713aSLionel Sambuc
122*f4a2713aSLionel Sambucnamespace rdar8714395 {
123*f4a2713aSLionel Sambuc  int &f(const void*);
124*f4a2713aSLionel Sambuc  float &f(const Foo*);
125*f4a2713aSLionel Sambuc
126*f4a2713aSLionel Sambuc  int &f2(const void*);
127*f4a2713aSLionel Sambuc  float &f2(Foo const* const *);
128*f4a2713aSLionel Sambuc
129*f4a2713aSLionel Sambuc  int &f3(const void*);
130*f4a2713aSLionel Sambuc  float &f3(Foo const**);
131*f4a2713aSLionel Sambuc
132*f4a2713aSLionel Sambuc  void g(Foo *p) {
133*f4a2713aSLionel Sambuc    float &fr = f(p);
134*f4a2713aSLionel Sambuc    float &fr2 = f2(&p);
135*f4a2713aSLionel Sambuc    int &ir = f3(&p);
136*f4a2713aSLionel Sambuc  }
137*f4a2713aSLionel Sambuc
138*f4a2713aSLionel Sambuc
139*f4a2713aSLionel Sambuc}
140*f4a2713aSLionel Sambuc
141*f4a2713aSLionel Sambucnamespace rdar8734046 {
142*f4a2713aSLionel Sambuc  void f1(id);
143*f4a2713aSLionel Sambuc  void f2(id<P0>);
144*f4a2713aSLionel Sambuc  void g(const A *a) {
145*f4a2713aSLionel Sambuc    f1(a);
146*f4a2713aSLionel Sambuc    f2(a);
147*f4a2713aSLionel Sambuc  }
148*f4a2713aSLionel Sambuc}
149*f4a2713aSLionel Sambuc
150*f4a2713aSLionel Sambucnamespace PR9735 {
151*f4a2713aSLionel Sambuc  int &f3(const A*);
152*f4a2713aSLionel Sambuc  float &f3(const void*);
153*f4a2713aSLionel Sambuc
154*f4a2713aSLionel Sambuc  void test_f(B* b, const B* bc) {
155*f4a2713aSLionel Sambuc    int &ir1 = f3(b);
156*f4a2713aSLionel Sambuc    int &ir2 = f3(bc);
157*f4a2713aSLionel Sambuc  }
158*f4a2713aSLionel Sambuc}
159*f4a2713aSLionel Sambuc
160*f4a2713aSLionel Sambuc@interface D : B
161*f4a2713aSLionel Sambuc@end
162*f4a2713aSLionel Sambuc
163*f4a2713aSLionel Sambucnamespace rdar9327203 {
164*f4a2713aSLionel Sambuc  int &f(void* const&, int);
165*f4a2713aSLionel Sambuc  float &f(void* const&, long);
166*f4a2713aSLionel Sambuc
167*f4a2713aSLionel Sambuc  void g(id x) {
168*f4a2713aSLionel Sambuc    int &fr = (f)(x, 0);
169*f4a2713aSLionel Sambuc  }
170*f4a2713aSLionel Sambuc}
171*f4a2713aSLionel Sambuc
172*f4a2713aSLionel Sambucnamespace class_id {
173*f4a2713aSLionel Sambuc  // it's okay to overload Class with id.
174*f4a2713aSLionel Sambuc  void f(Class) { }
175*f4a2713aSLionel Sambuc  void f(id) { }
176*f4a2713aSLionel Sambuc}
177