Debian Codegraph @

gcc/testsuite/obj-c++.dg/class-protocol-1.mm

raw
1 /* Check Class <protocol> types */
2 /* Author: David Ayers <[email protected]> */
3 /* { dg-do compile } */
4 // { dg-additional-options "-Wno-objc-root-class" }
5
6 #include <objc/objc.h>
7 #include "../objc-obj-c++-shared/runtime.h"
8
9 @protocol MyProto1
10 +(void)doItClass1;
11 -(void)doItInstance1;
12 @end
13
14 @protocol MyProto2
15 +(void)doItClass2;
16 -(void)doItInstance2;
17 @end
18
19 @interface MyClass1 <MyProto1>
20 {
21 Class isa;
22 }
23 @end
24 @implementation MyClass1
25 +(void)doItClass1{}
26 -(void)doItInstance1{}
27 @end
28
29 @interface MyClass2 : MyClass1 <MyProto2>
30 @end
31 @implementation MyClass2
32 +(void)doItClass2{}
33 -(void)doItInstance2{}
34 @end
35
36 @interface MyClass3
37 {
38 Class isa;
39 }
40 @end
41 @interface MyClass4 : MyClass3 <MyProto1>
42 @end
43
44 /*----------------------------------------*/
45
46 Class cls = 0;
47 Class <MyProto1> clsP1 = 0;
48 Class <MyProto2> clsP2 = 0;
49
50 void
51 testSimple(void)
52 {
53 [cls doItClass1];
54 [cls doItInstance1];
55 [cls doItClass2];
56 [cls doItInstance2];
57
58 [clsP1 doItClass1];
59 [clsP1 doItInstance1]; /* { dg-warning "instead of" } */
60 [clsP1 doItClass2]; /* { dg-warning "not found in protocol" } */
61 [clsP1 doItInstance2]; /* { dg-warning "not found in protocol" } */
62
63 [clsP2 doItClass1]; /* { dg-warning "not found in protocol" } */
64 [clsP2 doItInstance1]; /* { dg-warning "not found in protocol" } */
65 [clsP2 doItClass2];
66 [clsP2 doItInstance2]; /* { dg-warning "instead of" } */
67
68 [MyClass1 doItClass1];
69 [MyClass1 doItInstance1];
70 [MyClass1 doItClass2]; /* { dg-warning "may not respond to" } */
71 [MyClass1 doItInstance2]; /* { dg-warning "may not respond to" } */
72
73 [MyClass2 doItClass1];
74 [MyClass2 doItInstance1];
75 [MyClass2 doItClass2];
76 [MyClass2 doItInstance2]; /* { dg-warning "may not respond to" } */
77
78 [MyClass3 doItClass1]; /* { dg-warning "may not respond to" } */
79 [MyClass3 doItInstance1]; /* { dg-warning "may not respond to" } */
80
81 [MyClass4 doItClass1];
82 [MyClass4 doItInstance1]; /* { dg-warning "may not respond to" } */
83 }
84
85 /*----------------------------------------*/
86 /* Protocols declared by categories */
87
88 @protocol MyProto3
89 +(void)doItClass3;
90 -(void)doItInstance3;
91 @end
92 @protocol MyProto4
93 +(void)doItClass4;
94 -(void)doItInstance4;
95 @end
96
97 @interface MyClass1 (Category1) <MyProto3>
98 @end
99 @interface MyClass2 (Category2) <MyProto4>
100 @end
101
102 void
103 testCategory(void)
104 {
105 [cls doItClass3];
106 [cls doItInstance3];
107 [cls doItClass4];
108 [cls doItInstance4];
109
110 [MyClass1 doItClass3];
111 [MyClass1 doItInstance3];
112 [MyClass1 doItClass4]; /* { dg-warning "may not respond" } */
113 [MyClass1 doItInstance4]; /* { dg-warning "may not respond" } */
114
115 [MyClass2 doItClass3];
116 [MyClass2 doItInstance3];
117 [MyClass2 doItClass4];
118 [MyClass2 doItInstance4]; /* { dg-warning "may not respond" } */
119
120 }
121
122 /*----------------------------------------*/
123 /* Inherited protocols declared by categories */
124
125 @protocol MyProto5 <MyProto1>
126 +(void)doItClass5;
127 -(void)doItInstance5;
128 @end
129
130 @protocol MyProto6 <MyProto2>
131 +(void)doItClass6;
132 -(void)doItInstance6;
133 @end
134
135 @interface MyClass1 (Category3) <MyProto5>
136 @end
137 @interface MyClass2 (Category4) <MyProto6>
138 @end
139
140 Class <MyProto5> clsP5 = 0;
141 Class <MyProto6> clsP6 = 0;
142
143 void
144 testCategoryInherited(void)
145 {
146 [cls doItClass5];
147 [cls doItInstance5];
148 [cls doItClass6];
149 [cls doItInstance6];
150
151 [clsP5 doItClass1];
152 [clsP5 doItInstance1]; /* { dg-warning "instead of" } */
153 [clsP5 doItClass2]; /* { dg-warning "not found in protocol" } */
154 [clsP5 doItInstance2]; /* { dg-warning "not found in protocol" } */
155
156 [clsP6 doItClass1]; /* { dg-warning "not found in protocol" } */
157 [clsP6 doItInstance1]; /* { dg-warning "not found in protocol" } */
158 [clsP6 doItClass2];
159 [clsP6 doItInstance2]; /* { dg-warning "instead of" } */
160
161
162 [MyClass1 doItClass5];
163 [MyClass1 doItInstance5];
164 [MyClass1 doItClass6]; /* { dg-warning "may not respond" } */
165 [MyClass1 doItInstance6]; /* { dg-warning "may not respond" } */
166
167 [MyClass2 doItClass5];
168 [MyClass2 doItInstance5];
169 [MyClass2 doItClass6];
170 [MyClass2 doItInstance6]; /* { dg-warning "may not respond" } */
171
172 }
173
174 /*----------------------------------------*/
175 /* Forward declared root protocols */
176
177 @protocol FwProto;
178
179 @interface MyClass1 (Forward) <FwProto> /* { dg-warning "definition of protocol .FwProto. not found" } */
180 @end
181
182 Class <FwProto> clsP7 = 0;
183
184 void
185 testForwardeDeclared1(void)
186 {
187 [cls doItClass7]; /* { dg-warning "no .\\+doItClass7. method found" } */
188 [cls doItInstance7]; /* { dg-warning "no .\\+doItInstance7. method found" } */
189
190 [clsP7 doItClass7]; /* { dg-warning "not found in protocol" } */
191 /* { dg-warning "no .\\+doItClass7. method found" "" { target *-*-* } .-1 } */
192 [clsP7 doItInstance7]; /* { dg-warning "not found in protocol" } */
193 /* { dg-warning "no .\\+doItInstance7. method found" "" { target *-*-* } .-1 } */
194
195 [MyClass1 doItClass7]; /* { dg-warning "may not respond" } */
196 [MyClass1 doItInstance7]; /* { dg-warning "may not respond" } */
197
198 [MyClass2 doItClass7]; /* { dg-warning "may not respond" } */
199 [MyClass2 doItInstance7]; /* { dg-warning "may not respond" } */
200
201 }
202
203 @protocol FwProto
204 +(void)doItClass7;
205 -(void)doItInstance7;
206 @end
207
208 void
209 testForwardeDeclared2(void)
210 {
211 [cls doItClass7];
212 [cls doItInstance7];
213
214 [clsP7 doItClass7];
215 [clsP7 doItInstance7]; /* { dg-warning "instead of" } */
216
217 [MyClass1 doItClass7];
218 [MyClass1 doItInstance7];
219
220 [MyClass2 doItClass7];
221 [MyClass2 doItInstance7];
222 }
223
224 /*----------------------------------------*/
225 /* Inherited non root protocols */
226
227 @protocol MyProto8
228 +(void)doItClass8;
229 -(void)doItInstance8;
230 @end
231
232 @protocol MyProto9 <MyProto8>
233 +(void)doItClass9;
234 -(void)doItInstance9;
235 @end
236
237 @interface MyClass1 (InheritedNonRoot) <MyProto9>
238 @end
239
240 Class <MyProto8> clsP8 = 0;
241 Class <MyProto9> clsP9 = 0;
242
243 void
244 testInheritedNonRoot(void)
245 {
246 [cls doItClass8];
247 [cls doItInstance8];
248 [cls doItClass9];
249 [cls doItInstance9];
250
251 [clsP8 doItClass8];
252 [clsP8 doItInstance8]; /* { dg-warning "instead of" } */
253 [clsP8 doItClass9]; /* { dg-warning "not found in protocol" } */
254 [clsP8 doItInstance9]; /* { dg-warning "not found in protocol" } */
255
256 [clsP9 doItClass8];
257 [clsP9 doItInstance8]; /* { dg-warning "instead of" } */
258 [clsP9 doItClass9];
259 [clsP9 doItInstance9]; /* { dg-warning "instead of" } */
260
261 [MyClass1 doItClass8];
262 [MyClass1 doItInstance8];
263 [MyClass1 doItClass9];
264 [MyClass1 doItInstance9];
265
266 [MyClass2 doItClass8];
267 [MyClass2 doItInstance8];
268 [MyClass2 doItClass9];
269 [MyClass2 doItInstance9];
270
271 }
272
273 /*----------------------------------------*/
274 /* Prototype mismatch */
275
276 @protocol MyOtherProto1
277 +(id)doItClass1;
278 -(id)doItInstance1;
279 @end
280 @interface MyOtherClass1 <MyOtherProto1>
281 @end
282
283 Class <MyOtherProto1> oclsP1;
284
285 void
286 testPrototypeMismatch(void)
287 {
288 id tmp1 = [oclsP1 doItClass1];
289 id tmp2 = [oclsP1 doItInstance1]; /* { dg-warning "instead of" } */
290
291 [clsP1 doItClass1];
292 [clsP1 doItInstance1]; /* { dg-warning "instead of" } */
293 }
294
295 id obj = nil;
296 id <MyProto1> objP1 = nil;
297 id <MyProto2> objP2 = nil;
298 id <MyProto5> objP5 = nil;
299 int num = 0;
300 void *ptr = 0;
301
302 MyClass1 *mc1 = nil;
303
304 void
305 testComptypes(void)
306 {
307 { /* id <protocol>, id <protocol> */
308 objP1 == objP2; /* { dg-warning "lacks a cast" } */
309 objP2 == objP1; /* { dg-warning "lacks a cast" } */
310
311 objP1 == objP5;
312 objP5 == objP1;
313 }
314 { /* id <protocol>, SomeClass * */
315 mc1 == objP1;
316 objP1 == mc1;
317
318 mc1 == objP2; /* { dg-warning "lacks a cast" } */
319 objP2 == mc1; /* { dg-warning "lacks a cast" } */
320 }
321 { /* id <protocol>, id */
322 obj == objP1;
323 objP1 == obj;
324 }
325 { /* id <protocol>, Class */
326 cls == objP1; /* { dg-warning "lacks a cast" } */
327 objP1 == cls; /* { dg-warning "lacks a cast" } */
328 }
329 { /* id <protocol>, non-ObjC */
330 num == objP1; /* { dg-error "between pointer" } */
331 objP1 == num; /* { dg-error "between pointer" } */
332
333 ptr == objP1;
334 objP1 == ptr;
335 }
336 { /* Class <protocol>, Class <protocol> */
337 clsP1 == clsP2; /* { dg-warning "lacks a cast" } */
338 clsP2 == clsP1; /* { dg-warning "lacks a cast" } */
339
340 clsP1 == clsP5;
341 clsP5 == clsP1;
342 }
343 { /* Class <protocol>, SomeClass * */
344 mc1 == clsP1; /* { dg-warning "lacks a cast" } */
345 clsP1 == mc1; /* { dg-warning "lacks a cast" } */
346 }
347 { /* Class <protocol>, id */
348 obj == clsP1;
349 clsP1 == obj;
350 }
351 { /* Class <protocol>, Class */
352 cls == clsP1;
353 clsP1 == cls;
354 }
355 { /* Class <protocol>, non-ObjC */
356 num == clsP1; /* { dg-error "between pointer" } */
357 clsP1 == num; /* { dg-error "between pointer" } */
358
359 ptr == clsP1;
360 clsP1 == ptr;
361 }
362 { /* Class <protocol>, id <protocol> */
363 clsP1 == objP1; /* { dg-warning "lacks a cast" } */
364 objP1 == clsP1; /* { dg-warning "lacks a cast" } */
365 }
366
367 { /* id <protocol>, id <protocol> */
368 objP1 = objP2; /* { dg-warning "does not conform" } */
369 objP2 = objP1; /* { dg-warning "does not conform" } */
370
371 objP1 = objP5;
372 objP5 = objP1; /* { dg-warning "does not conform" } */
373 }
374 { /* id <protocol>, SomeClass * */
375 mc1 = objP1;
376 objP1 = mc1;
377
378 mc1 = objP2; /* { dg-warning "does not conform" } */
379 objP2 = mc1; /* { dg-warning "does not implement" } */
380 }
381 { /* id <protocol>, id */
382 obj = objP1;
383 objP1 = obj;
384 }
385 { /* id <protocol>, Class */
386 cls = objP1; /* { dg-warning "distinct Objective\\-C type" } */
387 objP1 = cls; /* { dg-warning "distinct Objective\\-C type" } */
388 }
389 { /* id <protocol>, non-ObjC */
390 num = objP1; /* { dg-error "invalid conversion" } */
391 objP1 = num; /* { dg-error "invalid conversion" } */
392
393 ptr = objP1;
394 objP1 = ptr; /* { dg-error "invalid conversion" } */
395 }
396 { /* Class <protocol>, Class <protocol> */
397 clsP1 = clsP2; /* { dg-warning "does not conform" } */
398 clsP2 = clsP1; /* { dg-warning "does not conform" } */
399
400 clsP1 = clsP5;
401 clsP5 = clsP1; /* { dg-warning "does not conform" } */
402 }
403 { /* Class <protocol>, SomeClass * */
404 /* These combinations should always elicit a warning. */
405 mc1 = clsP1; /* { dg-warning "distinct Objective\\-C type" } */
406 clsP1 = mc1; /* { dg-warning "distinct Objective\\-C type" } */
407
408 mc1 = clsP2; /* { dg-warning "distinct Objective\\-C type" } */
409 clsP2 = mc1; /* { dg-warning "distinct Objective\\-C type" } */
410 }
411 { /* Class <protocol>, id */
412 obj = clsP1;
413 clsP1 = obj;
414 }
415 { /* Class <protocol>, Class */
416 cls = clsP1;
417 clsP1 = cls;
418 }
419 { /* Class <protocol>, non-ObjC */
420 num = clsP1; /* { dg-error "invalid conversion" } */
421 clsP1 = num; /* { dg-error "invalid conversion" } */
422
423 ptr = clsP1;
424 clsP1 = ptr; /* { dg-error "invalid conversion" } */
425 }
426 { /* Class <protocol>, id <protocol> */
427 clsP1 = objP1; /* { dg-warning "distinct Objective\\-C type" } */
428 objP1 = clsP1; /* { dg-warning "distinct Objective\\-C type" } */
429 }
430 }
431
432 int main ()
433 {
434 testSimple();
435 testCategory();
436 testCategoryInherited();
437 return(0);
438 }
439
440 /* { dg-warning "messages without a matching method signature will be assumed to return .id. and accept .\.\.\.. as arguments" "" { target *-*-* } 0 } */