made error output for missing labels gcc like
[cparser] / adt / array.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/array.h
4  * Purpose:     Declarations for Array.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file array.h   Dynamic and flexible arrays for C.
15  */
16
17 #ifndef _ARRAY_H
18 #define _ARRAY_H
19
20 #include <assert.h>
21 #include <stddef.h>
22 #include <obstack.h>
23
24 #include "fourcc.h"
25 #include "align.h"
26 #include "xmalloc.h"
27
28 #define ARR_D_MAGIC     FOURCC('A','R','R','D')
29 #define ARR_A_MAGIC     FOURCC('A','R','R','A')
30 #define ARR_F_MAGIC     FOURCC('A','R','R','F')
31
32 /**
33  * Creates a flexible array.
34  *
35  * @param type     The element type of the new array.
36  * @param nelts    a size_t expression evaluating to the number of elements
37  *
38  * This macro creates a flexible array of a given type at runtime.
39  * The size of the array can be changed later.
40  *
41  * @return A pointer to the flexible array (can be used as a pointer to the
42  *         first element of this array).
43  */
44 #define NEW_ARR_F(type, nelts)                                          \
45   ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
46
47 /**
48  * Creates a new flxible array with the same number of elements as a
49  * given one.
50  *
51  * @param type     The element type of the new array.
52  * @param arr      An array from which the number of elements will be taken
53  *
54  * This macro creates a flexible array of a given type at runtime.
55  * The size of the array can be changed later.
56  *
57  * @return A pointer to the flexible array (can be used as a pointer to the
58  *         first element of this array).
59  */
60 #ifdef __GNUC__
61 #define CLONE_ARR_F(type, arr)                  \
62   NEW_ARR_F (__typeof__(arr[0]), ARR_LEN ((arr)))
63 #else
64 #define CLONE_ARR_F(type, arr)                  \
65         NEW_ARR_F (type, ARR_LEN ((arr)))
66 #endif
67
68 /**
69  * Duplicates an array and returns the new flexible one.
70  *
71  * @param type     The element type of the new array.
72  * @param arr      An array from which the elements will be duplicated
73  *
74  * This macro creates a flexible array of a given type at runtime.
75  * The size of the array can be changed later.
76  *
77  * @return A pointer to the flexible array (can be used as a pointer to the
78  *         first element of this array).
79  */
80 #ifdef __GNUC__
81 #define DUP_ARR_F(type, arr)                                                    \
82   memcpy (CLONE_ARR_F (__typeof__(arr[0]), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr)))
83 #else
84 #define DUP_ARR_F(type, arr)                                                    \
85         memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
86 #endif
87
88 /**
89  * Delete a flexible array.
90  *
91  * @param arr    The flexible array.
92  */
93 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
94
95 /**
96  * Creates a dynamic array on an obstack.
97  *
98  * @param type     The element type of the new array.
99  * @param obstack  A struct obstack * were the data will be allocated
100  * @param nelts    A size_t expression evaluating to the number of elements
101  *
102  * This macro creates a dynamic array of a given type at runtime.
103  * The size of the array cannot be changed later.
104  *
105  * @return A pointer to the dynamic array (can be used as a pointer to the
106  *         first element of this array).
107  */
108 #define NEW_ARR_D(type, obstack, nelts)                                 \
109   (  nelts                                                              \
110    ? (type *)_new_arr_d((obstack), (nelts), sizeof(type) * (nelts))     \
111    : (type *)arr_mt_descr.v.elts)
112
113 /**
114  * Creates a new dynamic array with the same number of elements as a
115  * given one.
116  *
117  * @param type     The element type of the new array.
118  * @param obstack  An struct obstack * were the data will be allocated
119  * @param arr      An array from which the number of elements will be taken
120  *
121  * This macro creates a dynamic array of a given type at runtime.
122  * The size of the array cannot be changed later.
123  *
124  * @return A pointer to the dynamic array (can be used as a pointer to the
125  *         first element of this array).
126  */
127 #ifdef __GNUC__
128 #define CLONE_ARR_D(type, obstack, arr)         \
129   NEW_ARR_D(__typeof__(arr[0]), (obstack), ARR_LEN((arr)))
130 #else
131 #define CLONE_ARR_D(type, obstack, arr)         \
132         NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
133 #endif
134
135 /**
136  * Duplicates an array and returns the new dynamic one.
137  *
138  * @param type     The element type of the new array.
139  * @param obstack  An struct obstack * were the data will be allocated
140  * @param arr      An array from which the elements will be duplicated
141  *
142  * This macro creates a dynamic array of a given type at runtime.
143  * The size of the array cannot be changed later.
144  *
145  * @return A pointer to the dynamic array (can be used as a pointer to the
146  *         first element of this array).
147  */
148 #ifdef __GNUC__
149 #define DUP_ARR_D(type, obstack, arr)                                                   \
150   memcpy(CLONE_ARR_D(__typeof__(arr[0]), (obstack), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr)))
151 #else
152 #define DUP_ARR_D(type, obstack, arr)                                                   \
153         memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
154 #endif
155
156 /**
157  * Create an automatic array which will be deleted at return from function.
158  * Beware, the data will be allocated on the function stack!
159  *
160  * @param type     The element type of the new array.
161  * @param var      A lvalue of type (type *) which will hold the new array.
162  * @param n        number of elements in this array.
163  *
164  * This macro creates a dynamic array on the functions stack of a given type at runtime.
165  * The size of the array cannot be changed later.
166  */
167 #define NEW_ARR_A(type, var, n)                                                                 \
168   do {                                                                                          \
169     int _nelts = (n);                                                                           \
170     assert (_nelts >= 0);                                                                       \
171     (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts;    \
172     _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type));                           \
173     (void)(_ARR_DESCR ((var))->nelts = _nelts);                                                 \
174   } while (0)
175
176 /**
177  * Creates a new automatic array with the same number of elements as a
178  * given one.
179  *
180  * @param type     The element type of the array.
181  * @param var      A lvalue of type (type *) which will hold the new array.
182  * @param arr      An array from which the elements will be duplicated
183  *
184  * This macro creates a dynamic array of a given type at runtime.
185  * The size of the array cannot be changed later.
186  *
187  * @return A pointer to the dynamic array (can be used as a pointer to the
188  *         first element of this array).
189  */
190 #ifdef __GNUC__
191 #define CLONE_ARR_A(type, var, arr)             \
192   NEW_ARR_A(__typeof__(arr[0]), (var), ARR_LEN((arr)))
193 #else
194 #define CLONE_ARR_A(type, var, arr)             \
195         NEW_ARR_A(type, (var), ARR_LEN((arr)))
196 #endif
197
198 /**
199  * Duplicates an array and returns a new automatic one.
200  *
201  * @param type     The element type of the new array.
202  * @param var      A lvalue of type (type *) which will hold the new array.
203  * @param arr      An array from with the number of elements will be taken
204  *
205  * This macro creates a dynamic array of a given type at runtime.
206  * The size of the array cannot be changed later.
207  *
208  * @return A pointer to the dynamic array (can be used as a pointer to the
209  *         first element of this array).
210  */
211 #ifdef __GNUC__
212 #define DUP_ARR_A(type, var, arr)                                       \
213   do { CLONE_ARR_A(__typeof__(arr[0]), (var), (arr));                                   \
214        memcpy((var), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr))); }     \
215   while (0)
216 #else
217 #define DUP_ARR_A(type, var, arr)                                       \
218   do { CLONE_ARR_A(type, (var), (arr));                                 \
219     memcpy((var), (arr), sizeof(type) * ARR_LEN((arr))); }      \
220   while (0)
221 #endif
222
223 /**
224  * Declare an initialized (zero'ed) array of fixed size.
225  * This macro should be used at file scope only.
226  *
227  * @param type     The element type of the new array.
228  * @param var      A lvalue of type (type *) which will hold the new array.
229  * @param _nelts   Number of elements in this new array.
230  */
231 #define DECL_ARR_S(type, var, _nelts)                                   \
232   ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var;                     \
233   type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)),   \
234                _##var.nelts = _nelts,                                   \
235                _##var.v.elts)
236
237 /**
238  * Returns the length of an array
239  *
240  * @param arr  a flexible, dynamic, automatic or static array.
241  */
242 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
243
244 /**
245  * Resize a flexible array, allocate more data if needed but do NOT
246  * reduce.
247  *
248  * @param type     The element type of the array.
249  * @param arr      The array, which must be an lvalue.
250  * @param n        The new size of the array.
251  *
252  * @remark  This macro may change arr, so update all references!
253  */
254 #ifdef __GNUC__
255 #define ARR_RESIZE(type, arr, n)                                        \
256   ((arr) = _arr_resize((arr), (n), sizeof(__typeof__(arr[0]))))
257 #else
258 #define ARR_RESIZE(type, arr, n)                                        \
259         ((arr) = _arr_resize((arr), (n), sizeof(type)))
260 #endif
261
262 /**
263  * Resize a flexible array, always reallocate data.
264  *
265  * @param type     The element type of the array.
266  * @param arr      The array, which must be an lvalue.
267  * @param n        The new size of the array.
268  *
269  * @remark  This macro may change arr, so update all references!
270  */
271 #ifdef __GNUC__
272 #define ARR_SETLEN(type, arr, n)                                        \
273   ((arr) = _arr_setlen ((arr), (n), sizeof(__typeof__(arr[0])) * (n)))
274 #else
275 #define ARR_SETLEN(type, arr, n)                                        \
276   ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
277 #endif
278
279 /** Set a length smaller than the current length of the array.  Do not
280  *  resize. len must be <= ARR_LEN(arr). */
281 #define ARR_SHRINKLEN(arr,len)                                          \
282    (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len),             \
283     _ARR_DESCR((arr))->nelts = len)
284
285 /**
286  * Resize a flexible array by growing it by delta elements.
287  *
288  * @param type     The element type of the array.
289  * @param arr      The array, which must be an lvalue.
290  * @param delta    The delta number of elements.
291  *
292  * @remark  This macro may change arr, so update all references!
293  */
294 #define ARR_EXTEND(type, arr, delta)                    \
295   ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
296
297 /**
298  * Resize a flexible array to hold n elements only if it is currently shorter
299  * than n.
300  *
301  * @param type     The element type of the array.
302  * @param arr      The array, which must be an lvalue.
303  * @param n        The new size of the array.
304  *
305  * @remark  This macro may change arr, so update all references!
306  */
307 #define ARR_EXTO(type, arr, n)                                          \
308   ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
309
310 /**
311  * Append one element to a flexible array.
312  *
313  * @param type     The element type of the array.
314  * @param arr      The array, which must be an lvalue.
315  * @param elt      The new element, must be of type (type).
316  */
317 #define ARR_APP1(type, arr, elt)                                        \
318   (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
319
320
321 #ifdef NDEBUG
322 # define ARR_VRFY(arr) ((void)0)
323 # define ARR_IDX_VRFY(arr, idx) ((void)0)
324 #else
325 # define ARR_VRFY(arr)                                                                  \
326     assert (   (   (_ARR_DESCR((arr))->magic == ARR_D_MAGIC)                            \
327                 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC)                            \
328                 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC))                           \
329             && (   (_ARR_DESCR((arr))->magic != ARR_F_MAGIC)                            \
330                 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts))        \
331             && (_ARR_DESCR((arr))->nelts >= 0))
332 # define ARR_IDX_VRFY(arr, idx)                         \
333     assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
334 #endif
335
336
337 /* Private !!!
338    Don't try this at home, kids, we're trained professionals ;->
339    ... or at the IPD, either. */
340 #ifdef NDEBUG
341 # define _ARR_DBGINF_DECL
342 # define _ARR_SET_DBGINF(descr, co, es)
343 #else
344 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
345 # define _ARR_SET_DBGINF(descr, co, es)                                 \
346     ( (descr)->magic = (co), (descr)->eltsize = (es) )
347 #endif
348
349 /**
350  * Construct an array header.
351  */
352 #define ARR_STRUCT(type, _nelts)                                                \
353   struct {                                                                      \
354     _ARR_DBGINF_DECL                                                            \
355     union {                                                                     \
356       struct obstack *obstack;  /* dynamic: allocated on this obstack */        \
357       int allocated;                    /* flexible: #slots allocated */        \
358     } u;                                                                        \
359     int nelts;                                                                  \
360     union {                                                                     \
361       type elts[(_nelts)];                                                      \
362       aligned_type align[1];                                                    \
363     } v;                                                                        \
364   }
365
366 /**
367  * The array descriptor header type.
368  */
369 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
370
371 extern _arr_descr arr_mt_descr;
372
373 void *_new_arr_f (int, size_t);
374 void _del_arr_f (void *);
375 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
376 void *_arr_resize (void *, int, size_t);
377 void *_arr_setlen (void *, int, size_t);
378
379 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
380 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))
381
382 #endif