Initial import of c parser
[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 #define CLONE_ARR_F(arr)                        \
61   NEW_ARR_F (__typeof__(arr[0]), ARR_LEN ((arr)))
62
63 /**
64  * Duplicates an array and returns the new flexible one.
65  *
66  * @param type     The element type of the new array.
67  * @param arr      An array from which the elements will be duplicated
68  *
69  * This macro creates a flexible array of a given type at runtime.
70  * The size of the array can be changed later.
71  *
72  * @return A pointer to the flexible array (can be used as a pointer to the
73  *         first element of this array).
74  */
75 #define DUP_ARR_F(arr)                                                  \
76   memcpy (CLONE_ARR_F (__typeof__(arr[0]), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN((arr)))
77
78 /**
79  * Delete a flexible array.
80  *
81  * @param arr    The flexible array.
82  */
83 #define DEL_ARR_F(arr) (_del_arr_f ((arr)))
84
85 /**
86  * Creates a dynamic array on an obstack.
87  *
88  * @param type     The element type of the new array.
89  * @param obstack  A struct obstack * were the data will be allocated
90  * @param nelts    A size_t expression evaluating to the number of elements
91  *
92  * This macro creates a dynamic array of a given type at runtime.
93  * The size of the array cannot be changed later.
94  *
95  * @return A pointer to the dynamic array (can be used as a pointer to the
96  *         first element of this array).
97  */
98 #define NEW_ARR_D(type, obstack, nelts)                                 \
99   (  nelts                                                              \
100    ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts))    \
101    : (type *)arr_mt_descr.v.elts)
102
103 /**
104  * Creates a new dynamic array with the same number of elements as a
105  * given one.
106  *
107  * @param type     The element type of the new array.
108  * @param obstack  An struct obstack * were the data will be allocated
109  * @param arr      An array from which the number of elements will be taken
110  *
111  * This macro creates a dynamic array of a given type at runtime.
112  * The size of the array cannot be changed later.
113  *
114  * @return A pointer to the dynamic array (can be used as a pointer to the
115  *         first element of this array).
116  */
117 #define CLONE_ARR_D(obstack, arr)               \
118   NEW_ARR_D (__typeof__(arr[0]), (obstack), ARR_LEN ((arr)))
119
120 /**
121  * Duplicates an array and returns the new dynamic one.
122  *
123  * @param type     The element type of the new array.
124  * @param obstack  An struct obstack * were the data will be allocated
125  * @param arr      An array from which the elements will be duplicated
126  *
127  * This macro creates a dynamic array of a given type at runtime.
128  * The size of the array cannot be changed later.
129  *
130  * @return A pointer to the dynamic array (can be used as a pointer to the
131  *         first element of this array).
132  */
133 #define DUP_ARR_D(obstack, arr)                                                 \
134   memcpy (CLONE_ARR_D (__typeof__(arr[0]), (obstack), (arr)), (arr), sizeof(__typeof__(arr[0])) * ARR_LEN ((arr)))
135
136 /**
137  * Create an automatic array which will be deleted at return from function.
138  * Beware, the data will be allocated on the function stack!
139  *
140  * @param type     The element type of the new array.
141  * @param var      A lvalue of type (type *) which will hold the new array.
142  * @param n        number of elements in this array.
143  *
144  * This macro creates a dynamic array on the functions stack of a given type at runtime.
145  * The size of the array cannot be changed later.
146  */
147 #define NEW_ARR_A(type, var, n)                                                                 \
148   do {                                                                                          \
149     int _nelts = (n);                                                                           \
150     assert (_nelts >= 0);                                                                       \
151     (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts;    \
152     _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type));                           \
153     (void)(_ARR_DESCR ((var))->nelts = _nelts);                                                 \
154   } while (0)
155
156 /**
157  * Creates a new automatic array with the same number of elements as a
158  * given one.
159  *
160  * @param var      A lvalue of type (type *) which will hold the new array.
161  * @param arr      An array from which the elements will be duplicated
162  *
163  * This macro creates a dynamic array of a given type at runtime.
164  * The size of the array cannot be changed later.
165  *
166  * @return A pointer to the dynamic array (can be used as a pointer to the
167  *         first element of this array).
168  */
169 #define CLONE_ARR_A(var, arr)           \
170   NEW_ARR_A (__typeof__(arr[0]), (var), ARR_LEN ((arr)))
171
172 /**
173  * Duplicates an array and returns a new automatic one.
174  *
175  * @param type     The element type of the new array.
176  * @param var      A lvalue of type (type *) which will hold the new array.
177  * @param arr      An array from with the number of elements will be taken
178  *
179  * This macro creates a dynamic array of a given type at runtime.
180  * The size of the array cannot be changed later.
181  *
182  * @return A pointer to the dynamic array (can be used as a pointer to the
183  *         first element of this array).
184  */
185 #define DUP_ARR_A(var, arr)                                     \
186   do { CLONE_ARR_A(__typeof__(arr[0]), (var), (arr));                                   \
187        memcpy ((var), (arr), sizeof (__typeof__(arr[0])) * ARR_LEN ((arr))); }  \
188   while (0)
189
190 /**
191  * Declare an initialized (zero'ed) array of fixed size.
192  * This macro should be used at file scope only.
193  *
194  * @param type     The element type of the new array.
195  * @param var      A lvalue of type (type *) which will hold the new array.
196  * @param _nelts   Number of elements in this new array.
197  */
198 #define DECL_ARR_S(type, var, _nelts)                                   \
199   ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var;                     \
200   type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)),   \
201                _##var.nelts = _nelts,                                   \
202                _##var.v.elts)
203
204 /**
205  * Returns the length of an array
206  *
207  * @param arr  a flexible, dynamic, automatic or static array.
208  */
209 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
210
211 /**
212  * Resize a flexible array, allocate more data if needed but do NOT
213  * reduce.
214  *
215  * @param arr      The array, which must be an lvalue.
216  * @param n        The new size of the array.
217  *
218  * @remark  This macro may change arr, so update all references!
219  */
220 #define ARR_RESIZE(arr, n)                                      \
221   ((arr) = _arr_resize ((arr), (n), sizeof(__typeof__(arr[0]))))
222
223 /**
224  * Resize a flexible array, always reallocate data.
225  *
226  * @param arr      The array, which must be an lvalue.
227  * @param n        The new size of the array.
228  *
229  * @remark  This macro may change arr, so update all references!
230  */
231 #define ARR_SETLEN(arr, n)                                      \
232   ((arr) = _arr_setlen ((arr), (n), sizeof(__typeof__(arr[0])) * (n)))
233
234 /** Set a length smaller than the current length of the array.  Do not
235  *  resize. len must be <= ARR_LEN(arr). */
236 #define ARR_SHRINKLEN(arr,len)                                          \
237    (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len),             \
238     _ARR_DESCR((arr))->nelts = len)
239
240 /**
241  * Resize a flexible array by growing it by delta elements.
242  *
243  * @param arr      The array, which must be an lvalue.
244  * @param delta    The delta number of elements.
245  *
246  * @remark  This macro may change arr, so update all references!
247  */
248 #define ARR_EXTEND(arr, delta)                  \
249   ARR_RESIZE ((arr), ARR_LEN ((arr)) + (delta))
250
251 /**
252  * Resize a flexible array to hold n elements only if it is currently shorter
253  * than n.
254  *
255  * @param arr      The array, which must be an lvalue.
256  * @param n        The new size of the array.
257  *
258  * @remark  This macro may change arr, so update all references!
259  */
260 #define ARR_EXTO(arr, n)                                                \
261   ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE ((arr), (n)+1) : (arr))
262
263 /**
264  * Append one element to a flexible array.
265  *
266  * @param arr      The array, which must be an lvalue.
267  * @param elt      The new element, must be of type (type).
268  */
269 #define ARR_APP1(arr, elt)                                      \
270   (ARR_EXTEND ((arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
271
272
273 #ifdef NDEBUG
274 # define ARR_VRFY(arr) ((void)0)
275 # define ARR_IDX_VRFY(arr, idx) ((void)0)
276 #else
277 # define ARR_VRFY(arr)                                                                  \
278     assert (   (   (_ARR_DESCR((arr))->magic == ARR_D_MAGIC)                            \
279                 || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC)                            \
280                 || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC))                           \
281             && (   (_ARR_DESCR((arr))->magic != ARR_F_MAGIC)                            \
282                 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts))        \
283             && (_ARR_DESCR((arr))->nelts >= 0))
284 # define ARR_IDX_VRFY(arr, idx)                         \
285     assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
286 #endif
287
288
289 /* Private !!!
290    Don't try this at home, kids, we're trained professionals ;->
291    ... or at the IPD, either. */
292 #ifdef NDEBUG
293 # define _ARR_DBGINF_DECL
294 # define _ARR_SET_DBGINF(descr, co, es)
295 #else
296 # define _ARR_DBGINF_DECL int magic; size_t eltsize;
297 # define _ARR_SET_DBGINF(descr, co, es)                                 \
298     ( (descr)->magic = (co), (descr)->eltsize = (es) )
299 #endif
300
301 /**
302  * Construct an array header.
303  */
304 #define ARR_STRUCT(type, _nelts)                                                \
305   struct {                                                                      \
306     _ARR_DBGINF_DECL                                                            \
307     union {                                                                     \
308       struct obstack *obstack;  /* dynamic: allocated on this obstack */        \
309       int allocated;                    /* flexible: #slots allocated */        \
310     } u;                                                                        \
311     int nelts;                                                                  \
312     union {                                                                     \
313       type elts[(_nelts)];                                                      \
314       aligned_type align[1];                                                    \
315     } v;                                                                        \
316   }
317
318 /**
319  * The array descriptor header type.
320  */
321 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
322
323 extern _arr_descr arr_mt_descr;
324
325 void *_new_arr_f (int, size_t);
326 void _del_arr_f (void *);
327 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
328 void *_arr_resize (void *, int, size_t);
329 void *_arr_setlen (void *, int, size_t);
330
331 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
332 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))
333
334 #endif