Merge reload-branch up to revision 101000
[official-gcc.git] / gcc / testsuite / obj-c++.dg / class-protocol-1.mm
blobb8200d0c0b30c4c9f5b0fcf5f25a798610a05a7b
2 /* Check Class <protocol> types */
3 /* Author: David Ayers <d.ayers@inode.at> */
4 /* { dg-do compile } */
6 #include <objc/objc.h>
7 #include <objc/objc-api.h>
9 @protocol MyProto1
10 +(void)doItClass1;
11 -(void)doItInstance1;
12 @end
14 @protocol MyProto2
15 +(void)doItClass2;
16 -(void)doItInstance2;
17 @end
19 @interface MyClass1 <MyProto1>
21   Class isa;
23 @end
24 @implementation MyClass1
25 +(void)doItClass1{}
26 -(void)doItInstance1{}
27 @end
29 @interface MyClass2 : MyClass1 <MyProto2>
30 @end
31 @implementation MyClass2
32 +(void)doItClass2{}
33 -(void)doItInstance2{}
34 @end
36 @interface MyClass3
38   Class isa;
40 @end
41 @interface MyClass4 : MyClass3 <MyProto1>
42 @end
44 /*----------------------------------------*/
46 Class cls = 0;
47 Class <MyProto1> clsP1 = 0;
48 Class <MyProto2> clsP2 = 0;
50 void
51 testSimple(void)
53   [cls doItClass1];
54   [cls doItInstance1];
55   [cls doItClass2];
56   [cls doItInstance2];
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" } */
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" }  */
68   [MyClass1 doItClass1];
69   [MyClass1 doItInstance1];
70   [MyClass1 doItClass2];    /* { dg-warning "may not respond to" } */
71   [MyClass1 doItInstance2]; /* { dg-warning "may not respond to" } */
73   [MyClass2 doItClass1];
74   [MyClass2 doItInstance1];
75   [MyClass2 doItClass2];
76   [MyClass2 doItInstance2]; /* { dg-warning "may not respond to" } */
78   [MyClass3 doItClass1];    /* { dg-warning "may not respond to" } */
79   [MyClass3 doItInstance1]; /* { dg-warning "may not respond to" } */
81   [MyClass4 doItClass1];
82   [MyClass4 doItInstance1]; /* { dg-warning "may not respond to" } */
85 /*----------------------------------------*/
86 /* Protocols declared by categories */
88 @protocol MyProto3
89 +(void)doItClass3;
90 -(void)doItInstance3;
91 @end
92 @protocol MyProto4
93 +(void)doItClass4;
94 -(void)doItInstance4;
95 @end
97 @interface MyClass1 (Category1) <MyProto3>
98 @end
99 @interface MyClass2 (Category2) <MyProto4>
100 @end
102 void
103 testCategory(void)
105   [cls doItClass3];
106   [cls doItInstance3];
107   [cls doItClass4];
108   [cls doItInstance4];
110   [MyClass1 doItClass3];
111   [MyClass1 doItInstance3];
112   [MyClass1 doItClass4];    /* { dg-warning "may not respond" } */
113   [MyClass1 doItInstance4]; /* { dg-warning "may not respond" } */
115   [MyClass2 doItClass3];
116   [MyClass2 doItInstance3];
117   [MyClass2 doItClass4];
118   [MyClass2 doItInstance4]; /* { dg-warning "may not respond" } */
122 /*----------------------------------------*/
123 /* Inherited protocols declared by categories */
125 @protocol MyProto5 <MyProto1>
126 +(void)doItClass5;
127 -(void)doItInstance5;
128 @end
130 @protocol MyProto6 <MyProto2>
131 +(void)doItClass6;
132 -(void)doItInstance6;
133 @end
135 @interface MyClass1 (Category3) <MyProto5>
136 @end
137 @interface MyClass2 (Category4) <MyProto6>
138 @end
140 Class <MyProto5> clsP5 = 0;
141 Class <MyProto6> clsP6 = 0;
143 void
144 testCategoryInherited(void)
146   [cls doItClass5];
147   [cls doItInstance5];
148   [cls doItClass6];
149   [cls doItInstance6];
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" } */
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" }  */
162   [MyClass1 doItClass5];
163   [MyClass1 doItInstance5];
164   [MyClass1 doItClass6];    /* { dg-warning "may not respond" } */
165   [MyClass1 doItInstance6]; /* { dg-warning "may not respond" } */
167   [MyClass2 doItClass5];
168   [MyClass2 doItInstance5];
169   [MyClass2 doItClass6];
170   [MyClass2 doItInstance6]; /* { dg-warning "may not respond" } */
174 /*----------------------------------------*/
175 /* Forward declared root protocols */
177 @protocol FwProto;
179 @interface MyClass1 (Forward) <FwProto>
180 @end
182 Class <FwProto> clsP7 = 0;
184 void
185 testForwardeDeclared1(void)
187   [cls doItClass7];         /* { dg-warning "no .\\+doItClass7. method found" } */
188   [cls doItInstance7];      /* { dg-warning "no .\\+doItInstance7. method found" } */
190   [clsP7 doItClass7];       /* { dg-warning "not found in protocol" } */
191   /* { dg-warning "no .\\+doItClass7. method found" "" { target *-*-* } 190 } */
192   [clsP7 doItInstance7];    /* { dg-warning "not found in protocol" } */
193   /* { dg-warning "no .\\+doItInstance7. method found" "" { target *-*-* } 192 } */
195   [MyClass1 doItClass7];    /* { dg-warning "may not respond" } */
196   [MyClass1 doItInstance7]; /* { dg-warning "may not respond" } */
198   [MyClass2 doItClass7];    /* { dg-warning "may not respond" } */
199   [MyClass2 doItInstance7]; /* { dg-warning "may not respond" } */
203 @protocol FwProto
204 +(void)doItClass7;
205 -(void)doItInstance7;
206 @end
208 void
209 testForwardeDeclared2(void)
211   [cls doItClass7];
212   [cls doItInstance7];
214   [clsP7 doItClass7];    
215   [clsP7 doItInstance7]; /* { dg-warning "instead of" }  */
217   [MyClass1 doItClass7];
218   [MyClass1 doItInstance7];
220   [MyClass2 doItClass7];
221   [MyClass2 doItInstance7];
224 /*----------------------------------------*/
225 /* Inherited non root protocols */
227 @protocol MyProto8
228 +(void)doItClass8;
229 -(void)doItInstance8;
230 @end
232 @protocol MyProto9 <MyProto8>
233 +(void)doItClass9;
234 -(void)doItInstance9;
235 @end
237 @interface MyClass1 (InheritedNonRoot) <MyProto9>
238 @end
240 Class <MyProto8> clsP8 = 0;
241 Class <MyProto9> clsP9 = 0;
243 void
244 testInheritedNonRoot(void)
246   [cls doItClass8];
247   [cls doItInstance8];
248   [cls doItClass9];
249   [cls doItInstance9];
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" } */
256   [clsP9 doItClass8];
257   [clsP9 doItInstance8]; /* { dg-warning "instead of" }  */
258   [clsP9 doItClass9];
259   [clsP9 doItInstance9]; /* { dg-warning "instead of" }  */
261   [MyClass1 doItClass8];
262   [MyClass1 doItInstance8];
263   [MyClass1 doItClass9];
264   [MyClass1 doItInstance9];
266   [MyClass2 doItClass8];
267   [MyClass2 doItInstance8];
268   [MyClass2 doItClass9];
269   [MyClass2 doItInstance9];
270   
273 /*----------------------------------------*/
274 /* Prototype mismatch  */
276 @protocol MyOtherProto1
277 +(id)doItClass1;
278 -(id)doItInstance1;
279 @end
280 @interface MyOtherClass1 <MyOtherProto1>
281 @end
283 Class <MyOtherProto1> oclsP1;
285 void
286 testPrototypeMismatch(void)
288   id tmp1 = [oclsP1 doItClass1];
289   id tmp2 = [oclsP1 doItInstance1]; /* { dg-warning "instead of" }  */
291   [clsP1 doItClass1];
292   [clsP1 doItInstance1]; /* { dg-warning "instead of" }  */
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;
302 MyClass1 *mc1 = nil;
304 void
305 testComptypes(void)
307   { /* id <protocol>, id <protocol>  */
308     objP1 == objP2;  /* { dg-warning "lacks a cast" } */
309     objP2 == objP1;  /* { dg-warning "lacks a cast" } */
311     objP1 == objP5;
312     objP5 == objP1;
313   }
314   { /* id <protocol>, SomeClass *  */
315     mc1 == objP1;
316     objP1 == mc1;
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-warning "between pointer" } */
331     objP1 == num; /* { dg-warning "between pointer" } */
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" } */
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-warning "between pointer" } */
357     clsP1 == num; /* { dg-warning "between pointer" } */
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   }
367   { /* id <protocol>, id <protocol>  */
368     objP1 = objP2; /* { dg-warning "does not conform" } */
369     objP2 = objP1; /* { dg-warning "does not conform" } */
371     objP1 = objP5;
372     objP5 = objP1; /* { dg-warning "does not conform" } */
373   }
374   { /* id <protocol>, SomeClass *  */
375     mc1 = objP1;
376     objP1 = mc1;
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" } */
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" } */
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" } */
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   }
432 int main ()
434   testSimple();
435   testCategory();
436   testCategoryInherited();
437   return(0);
440 /* { dg-warning "Messages without a matching method signature" "" { target *-*-* } 0 } */
441 /* { dg-warning "will be assumed to return .id. and accept" "" { target *-*-* } 0 } */
442 /* { dg-warning ".\.\.\.. as arguments" "" { target *-*-* } 0 } */