| 1 |
/* Test instance variable scope. */ |
| 2 |
/* Author: Dimitris Papavasiliou <[email protected]>. */ |
| 3 |
/* { dg-do run } */ |
| 4 |
/* { dg-additional-options "-Wno-shadow-ivar -fno-local-ivars -Wno-objc-root-class" } */ |
| 5 |
|
| 6 |
#include "../objc-obj-c++-shared/runtime.h" |
| 7 |
#include <objc/objc.h> |
| 8 |
|
| 9 |
extern void abort(void); |
| 10 |
|
| 11 |
int someivar = 1; |
| 12 |
|
| 13 |
/* The testsuite object depends on local variable scope so we need to |
| 14 |
implement our own minimal base object here. */ |
| 15 |
|
| 16 |
@interface MyClass |
| 17 |
{ |
| 18 |
Class isa; |
| 19 |
int someivar; |
| 20 |
} |
| 21 |
|
| 22 |
+ (id) initialize; |
| 23 |
+ (id) alloc; |
| 24 |
- (id) init; |
| 25 |
- (int) getGlobal; |
| 26 |
- (int) getInstance; |
| 27 |
- (int) getHidden; |
| 28 |
@end |
| 29 |
|
| 30 |
@implementation MyClass |
| 31 |
+ (id) initialize |
| 32 |
{ |
| 33 |
return self; |
| 34 |
} |
| 35 |
|
| 36 |
+ (id) alloc |
| 37 |
{ |
| 38 |
return class_createInstance (self, 0); |
| 39 |
} |
| 40 |
|
| 41 |
- (id) init |
| 42 |
{ |
| 43 |
self->someivar = 2; |
| 44 |
|
| 45 |
return self; |
| 46 |
} |
| 47 |
|
| 48 |
- (int) getGlobal |
| 49 |
{ |
| 50 |
return someivar; |
| 51 |
} |
| 52 |
|
| 53 |
- (int) getInstance |
| 54 |
{ |
| 55 |
return self->someivar; |
| 56 |
} |
| 57 |
|
| 58 |
- (int) getHidden |
| 59 |
{ |
| 60 |
int someivar = 3; |
| 61 |
|
| 62 |
return someivar; |
| 63 |
} |
| 64 |
@end |
| 65 |
|
| 66 |
int main(void) |
| 67 |
{ |
| 68 |
id object; |
| 69 |
|
| 70 |
object = [[MyClass alloc] init]; |
| 71 |
|
| 72 |
/* Check for aliasing between instance variable and global |
| 73 |
variable. */ |
| 74 |
|
| 75 |
if ([object getGlobal] != 1) { |
| 76 |
abort(); |
| 77 |
} |
| 78 |
|
| 79 |
if ([object getInstance] != 2) { |
| 80 |
abort(); |
| 81 |
} |
| 82 |
|
| 83 |
/* Check whether the local variable hides the instance variable. */ |
| 84 |
|
| 85 |
if ([object getHidden] != 3) { |
| 86 |
abort(); |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |