| 1 |
/* Contributed by Nicola Pero <[email protected]>, November 2010. */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 4 |
|
| 5 |
/* Test dot-syntax with super in a category. */ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <objc/objc.h> |
| 9 |
#include <objc/runtime.h> |
| 10 |
|
| 11 |
@interface MyRootClass |
| 12 |
{ |
| 13 |
Class isa; |
| 14 |
int a; |
| 15 |
} |
| 16 |
+ (id) initialize; |
| 17 |
+ (id) alloc; |
| 18 |
- (id) init; |
| 19 |
- (int) count; |
| 20 |
- (void) setCount: (int)count; |
| 21 |
@end |
| 22 |
|
| 23 |
@implementation MyRootClass |
| 24 |
+ (id) initialize { return self; } |
| 25 |
+ (id) alloc { return class_createInstance (self, 0); } |
| 26 |
- (id) init { return self; } |
| 27 |
- (int) count |
| 28 |
{ |
| 29 |
return a; |
| 30 |
} |
| 31 |
- (void) setCount: (int)count |
| 32 |
{ |
| 33 |
a = count; |
| 34 |
} |
| 35 |
@end |
| 36 |
|
| 37 |
/* First, test 'super' in the main implementation of a subclass. */ |
| 38 |
@interface MySubClass : MyRootClass |
| 39 |
- (int) superCount; |
| 40 |
- (void) setSuperCount: (int)count; |
| 41 |
@end |
| 42 |
|
| 43 |
@implementation MySubClass |
| 44 |
- (int) superCount |
| 45 |
{ |
| 46 |
return super.count; |
| 47 |
} |
| 48 |
- (void) setSuperCount: (int)count |
| 49 |
{ |
| 50 |
super.count = count; |
| 51 |
} |
| 52 |
@end |
| 53 |
|
| 54 |
/* Now, test 'super' in a category of a subclass. */ |
| 55 |
@interface MySubClass (Category) |
| 56 |
- (int) superCount2; |
| 57 |
- (void) setSuperCount2: (int)count; |
| 58 |
- (int) test: (int)x; |
| 59 |
@end |
| 60 |
|
| 61 |
@implementation MySubClass (Category) |
| 62 |
- (int) superCount2 |
| 63 |
{ |
| 64 |
return super.count; |
| 65 |
} |
| 66 |
- (void) setSuperCount2: (int)count |
| 67 |
{ |
| 68 |
super.count = count; |
| 69 |
} |
| 70 |
- (int) test: (int)x |
| 71 |
{ |
| 72 |
/* For positive x, the following will leave super.count |
| 73 |
unchanged. */ |
| 74 |
super.count++; |
| 75 |
--super.count; |
| 76 |
|
| 77 |
super.count = (x < 0 ? x : super.count); |
| 78 |
|
| 79 |
if ((x = super.count)) |
| 80 |
super.count += 1; |
| 81 |
|
| 82 |
if ((x = super.count)) |
| 83 |
super.count -= 1; |
| 84 |
|
| 85 |
/* Finally, also put a bit of self.count in the mix. */ |
| 86 |
self.count++; |
| 87 |
super.count--; |
| 88 |
|
| 89 |
return super.count; |
| 90 |
} |
| 91 |
@end |
| 92 |
|
| 93 |
int main (void) |
| 94 |
{ |
| 95 |
MySubClass *object = [[MySubClass alloc] init]; |
| 96 |
|
| 97 |
object.count = 10; |
| 98 |
if (object.count != 10) |
| 99 |
abort (); |
| 100 |
|
| 101 |
object.superCount = 11; |
| 102 |
if (object.superCount != 11) |
| 103 |
abort (); |
| 104 |
|
| 105 |
object.superCount2 = 12; |
| 106 |
if (object.superCount2 != 12) |
| 107 |
abort (); |
| 108 |
|
| 109 |
if ([object test: 45] != 12) |
| 110 |
abort (); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |