| 1 |
/* Test basic Objective-C foreach syntax. This tests iterations, with |
| 2 |
the basic syntax 'for (object in array) statements' |
| 3 |
*/ |
| 4 |
/* { dg-do run } */ |
| 5 |
/* { dg-skip-if "No NeXT fast enum. pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */ |
| 6 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 7 |
/* { dg-options "-mno-constant-cfstrings" { target *-*-darwin* } } */ |
| 8 |
/* { dg-additional-sources "../objc-obj-c++-shared/nsconstantstring-class-impl.m" } */ |
| 9 |
/* { dg-additional-options "-Wno-objc-root-class" } */ |
| 10 |
|
| 11 |
#include "../objc-obj-c++-shared/TestsuiteObject.m" |
| 12 |
#ifndef __NEXT_RUNTIME__ |
| 13 |
#include <objc/NXConstStr.h> |
| 14 |
#else |
| 15 |
#include "../objc-obj-c++-shared/nsconstantstring-class.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
extern int printf (const char *, ...); |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
/* |
| 22 |
struct __objcFastEnumerationState |
| 23 |
{ |
| 24 |
unsigned long state; |
| 25 |
id *itemsPtr; |
| 26 |
unsigned long *mutationsPtr; |
| 27 |
unsigned long extra[5]; |
| 28 |
}; |
| 29 |
*/ |
| 30 |
|
| 31 |
/* A mini-array implementation that can be used to test fast |
| 32 |
enumeration. You create the array with some objects; you can |
| 33 |
mutate the array, and you can fast-enumerate it. |
| 34 |
*/ |
| 35 |
@interface MyArray : TestsuiteObject |
| 36 |
{ |
| 37 |
unsigned int length; |
| 38 |
id *objects; |
| 39 |
unsigned long mutated; |
| 40 |
} |
| 41 |
- (id) initWithLength: (unsigned int)l objects: (id *)o; |
| 42 |
- (void) mutate; |
| 43 |
- (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state |
| 44 |
objects:(id *)stackbuf |
| 45 |
count:(unsigned long)len; |
| 46 |
@end |
| 47 |
|
| 48 |
@implementation MyArray : TestsuiteObject |
| 49 |
- (id) initWithLength: (unsigned int)l |
| 50 |
objects: (id *)o |
| 51 |
{ |
| 52 |
length = l; |
| 53 |
objects = o; |
| 54 |
mutated = 0; |
| 55 |
return self; |
| 56 |
} |
| 57 |
- (void) mutate |
| 58 |
{ |
| 59 |
mutated = 1; |
| 60 |
} |
| 61 |
- (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state |
| 62 |
objects: (id*)stackbuf |
| 63 |
count: (unsigned long)len |
| 64 |
{ |
| 65 |
unsigned long i, batch_size; |
| 66 |
|
| 67 |
/* We keep how many objects we served in the state->state counter. So the next batch |
| 68 |
will contain up to length - state->state objects. */ |
| 69 |
batch_size = length - state->state; |
| 70 |
|
| 71 |
/* Make obvious adjustments. */ |
| 72 |
if (batch_size < 0) |
| 73 |
batch_size = 0; |
| 74 |
|
| 75 |
if (batch_size > len) |
| 76 |
batch_size = len; |
| 77 |
|
| 78 |
/* Copy the objects. */ |
| 79 |
for (i = 0; i < batch_size; i++) |
| 80 |
stackbuf[i] = objects[i]; |
| 81 |
|
| 82 |
state->state += batch_size; |
| 83 |
state->itemsPtr = stackbuf; |
| 84 |
state->mutationsPtr = &mutated; |
| 85 |
|
| 86 |
return batch_size; |
| 87 |
} |
| 88 |
@end |
| 89 |
|
| 90 |
void |
| 91 |
foo (MyArray *array, int x) |
| 92 |
{ |
| 93 |
TestsuiteObject *object; |
| 94 |
int i, j, k; |
| 95 |
|
| 96 |
label1: |
| 97 |
for (i = 0; i < 2; ++i) |
| 98 |
{ |
| 99 |
if (i == 1) |
| 100 |
{ |
| 101 |
if (x != 5) |
| 102 |
abort (); |
| 103 |
return; |
| 104 |
} |
| 105 |
k = 0; |
| 106 |
label2: |
| 107 |
for (object in array) |
| 108 |
{ |
| 109 |
if (k == 1) |
| 110 |
{ |
| 111 |
if (x != 3) |
| 112 |
abort (); |
| 113 |
return; |
| 114 |
} |
| 115 |
++k; |
| 116 |
label3: |
| 117 |
for (j = 0; j < 2; ++j) |
| 118 |
{ |
| 119 |
if (j == 1) |
| 120 |
{ |
| 121 |
if (x != 1) |
| 122 |
abort (); |
| 123 |
return; |
| 124 |
} |
| 125 |
label4: |
| 126 |
switch (x) |
| 127 |
{ |
| 128 |
case 0: |
| 129 |
break label4; |
| 130 |
case 1: |
| 131 |
continue label3; |
| 132 |
case 2: |
| 133 |
break label3; |
| 134 |
case 3: |
| 135 |
continue label2; |
| 136 |
case 4: |
| 137 |
break label2; |
| 138 |
case 5: |
| 139 |
continue label1; |
| 140 |
default: |
| 141 |
break label1; |
| 142 |
} |
| 143 |
if (x != 0) |
| 144 |
abort (); |
| 145 |
return; |
| 146 |
} |
| 147 |
if (x != 2) |
| 148 |
abort (); |
| 149 |
return; |
| 150 |
} |
| 151 |
if (x != 4) |
| 152 |
abort (); |
| 153 |
return; |
| 154 |
} |
| 155 |
if (x <= 5) |
| 156 |
abort (); |
| 157 |
} |
| 158 |
|
| 159 |
int |
| 160 |
main () |
| 161 |
{ |
| 162 |
MyArray *array; |
| 163 |
id objects[2] = { @"object1", @"object2" }; |
| 164 |
int i; |
| 165 |
|
| 166 |
array = [[MyArray alloc] initWithLength: 2 |
| 167 |
objects: objects]; |
| 168 |
for (i = 0; i < 6; ++i) |
| 169 |
foo (array, i); |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |