Code review to compile with -ansi
[libfirm] / ir / tr / type.c
1 /****h* libfirm/type.c
2  *
3  * NAME
4  *   file type.c - implementation of the datastructure to hold
5  *   type information.
6  * COPYRIGHT
7  *  (C) 2001 by Universitaet Karlsruhe
8  * AUTHORS
9  *  Martin Trapp, Christian Schaefer, Goetz Lindenmaier
10  *
11  * NOTES
12  *  This module supplies a datastructure to represent all types
13  *  known in the compiled program.  This includes types specified
14  *  in the program as well as types defined by the language.  In the
15  *  view of the intermediate representation there is no difference
16  *  between these types.
17  *
18  *  There exist several kinds of types, arranged by the structure of
19  *  the type.  A type is described by a set of attributes.  Some of
20  *  these attributes are common to all types, others depend on the
21  *  kind of the type.
22  *
23  *  Types are different from the modes defined in irmode:  Types are
24  *  on the level of the programming language, modes at the level of
25  *  the target processor.
26  *
27  * SEE ALSO
28  *   type_t.h type tpop
29  *****
30  */
31
32 /* $Id$ */
33
34 # include <stdlib.h>
35 # include <stddef.h>
36 # include "type_t.h"
37 # include "tpop_t.h"
38 # include "typegmod_t.h"
39 # include "array.h"
40 # include "irprog.h"
41 # include "mangle.h"
42 # include "tv.h"
43 # include "ircons.h"
44
45 /*******************************************************************/
46 /** TYPE                                                          **/
47 /*******************************************************************/
48
49 unsigned long type_visited;
50
51 INLINE type *
52 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
53   type *res;
54   int node_size ;
55
56   assert(type_op != type_id);
57
58   node_size = offsetof (type, attr) +  type_op->attr_size;
59   res = (type *) xmalloc (node_size);
60   add_irp_type(res);   /* Remember the new type global. */
61
62   res->kind = k_type;
63   res->type_op = type_op;
64   res->mode = mode;
65   res->name = name;
66   res->state = layout_undefined;
67   res->size = -1;
68   res->visit = 0;
69   res -> link = NULL;
70
71   return res;
72 }
73
74 void free_type_attrs(type *tp) {
75   switch(get_type_tpop_code(tp)) {
76   case tpo_class:       { free_class_attrs(tp);       } break;
77   case tpo_struct:      { free_struct_attrs(tp);      } break;
78   case tpo_method:      { free_method_attrs(tp);      } break;
79   case tpo_union:       { free_union_attrs(tp);       } break;
80   case tpo_array:       { free_array_attrs(tp);       } break;
81   case tpo_enumeration: { free_enumeration_attrs(tp); } break;
82   case tpo_pointer:     { free_pointer_attrs(tp);     } break;
83   case tpo_primitive:   { free_primitive_attrs(tp);   } break;
84   default: break;
85   }
86 }
87
88 /* set/get the link field */
89 void *get_type_link(type *tp)
90 {
91   assert(tp && tp->kind == k_type);
92   return(tp -> link);
93 }
94
95 void set_type_link(type *tp, void *l)
96 {
97   assert(tp && tp->kind == k_type);
98   tp -> link = l;
99 }
100
101 tp_op*      get_type_tpop(type *tp) {
102   assert(tp && tp->kind == k_type);
103   return tp->type_op;
104 }
105
106 ident*      get_type_tpop_nameid(type *tp) {
107   assert(tp && tp->kind == k_type);
108   return tp->type_op->name;
109 }
110
111 const char* get_type_tpop_name(type *tp) {
112   assert(tp && tp->kind == k_type);
113   return id_to_str(tp->type_op->name);
114 }
115
116 tp_opcode    get_type_tpop_code(type *tp) {
117   assert(tp && tp->kind == k_type);
118   return tp->type_op->code;
119 }
120
121 ir_mode*    get_type_mode(type *tp) {
122   assert(tp && tp->kind == k_type);
123   return tp->mode;
124 }
125
126 void        set_type_mode(type *tp, ir_mode* m) {
127   assert(tp && tp->kind == k_type);
128
129   assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
130          /* Modes of primitives must be data */
131          ((tp->type_op != type_enumeration) || mode_is_int(m)));
132          /* Modes of enumerations must be integers */
133
134   if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
135     /* For pointer, primitive and enumeration size depends on the mode. */
136     tp->size = get_mode_size(m);
137     tp->mode = m;
138   }
139 }
140
141 ident*      get_type_ident(type *tp) {
142   assert(tp && tp->kind == k_type);
143   return tp->name;
144 }
145
146 void        set_type_ident(type *tp, ident* id) {
147   assert(tp && tp->kind == k_type);
148   tp->name = id;
149 }
150
151 const char* get_type_name(type *tp) {
152   assert(tp && tp->kind == k_type);
153   return (id_to_str(tp->name));
154 }
155
156 int         get_type_size(type *tp) {
157   assert(tp && tp->kind == k_type);
158   return tp->size;
159 }
160
161 void
162 set_type_size(type *tp, int size) {
163   assert(tp && tp->kind == k_type);
164   /* For pointer enumeration and primitive size depends on the mode.
165      Methods don't have a size. */
166   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
167       (tp->type_op != type_enumeration) && (tp->type_op != type_method))
168     tp->size = size;
169 }
170
171 type_state
172 get_type_state(type *tp) {
173   assert(tp && tp->kind == k_type);
174   return tp->state;
175 }
176
177 void
178 set_type_state(type *tp, type_state state) {
179   assert(tp && tp->kind == k_type);
180
181   if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
182       (tp->type_op == type_method))
183     return;
184
185   /* Just a correctness check: */
186   if (state == layout_fixed) {
187     int i;
188     switch (get_type_tpop_code(tp)) {
189     case tpo_class:
190       {
191         assert(get_type_size(tp) > -1);
192         if (tp != get_glob_type())
193           for (i = 0; i < get_class_n_member(tp); i++) {
194             assert(get_entity_offset(get_class_member(tp, i)) > -1);
195             assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
196                    (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
197             /*    @@@ lowerfirm geht nicht durch */
198           }
199       } break;
200     case tpo_struct:
201       {
202         /* assert(get_type_size(tp) > -1);    @@@ lowerfirm geht nicht durch */
203         for (i = 0; i < get_struct_n_member(tp); i++) {
204           assert(get_entity_offset(get_struct_member(tp, i)) > -1);
205           assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
206         }
207       } break;
208     case tpo_union:
209       { /* ?? */
210       } break;
211     case tpo_array:
212       { /* ??
213          Check order?
214          Assure that only innermost dimension is dynamic? */
215       } break;
216     case tpo_enumeration:
217       {
218         assert(get_type_mode != NULL);
219         for (i = 0; i < get_enumeration_n_enums(tp); i++)
220           assert(get_enumeration_enum(tp, i) != NULL);
221       } break;
222     default: break;
223     } /* switch (tp) */
224   }
225   tp->state = state;
226 }
227
228 unsigned long get_type_visited(type *tp) {
229   assert(tp && tp->kind == k_type);
230   return tp->visit;
231 }
232
233 void        set_type_visited(type *tp, unsigned long num) {
234   assert(tp && tp->kind == k_type);
235   tp->visit = num;
236 }
237 /* Sets visited field in type to type_visited. */
238 void        mark_type_visited(type *tp) {
239   assert(tp && tp->kind == k_type);
240   assert(tp->visit < type_visited);
241   tp->visit = type_visited;
242 }
243
244 int is_type            (void *thing) {
245   assert(thing);
246   if (get_kind(thing) == k_type)
247     return 1;
248   else
249     return 0;
250 }
251
252 /*******************************************************************/
253 /** TYPE_CLASS                                                    **/
254 /*******************************************************************/
255
256 /* create a new class type */
257 type   *new_type_class (ident *name) {
258   type *res;
259
260   res = new_type(type_class, NULL, name);
261
262   res->attr.ca.members    = NEW_ARR_F (entity *, 1);
263   res->attr.ca.subtypes   = NEW_ARR_F (type *, 1);
264   res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
265   res->attr.ca.peculiarity = existent;
266   res->attr.ca.dfn        = 0;
267
268   return res;
269 }
270 INLINE void free_class_attrs(type *clss) {
271   assert(clss && (clss->type_op == type_class));
272   DEL_ARR_F(clss->attr.ca.members);
273   DEL_ARR_F(clss->attr.ca.subtypes);
274   DEL_ARR_F(clss->attr.ca.supertypes);
275 }
276 /* manipulate private fields of class type  */
277 void    add_class_member   (type *clss, entity *member) {
278   assert(clss && (clss->type_op == type_class));
279   ARR_APP1 (entity *, clss->attr.ca.members, member);
280 }
281 int     get_class_n_member (type *clss) {
282   assert(clss && (clss->type_op == type_class));
283   return (ARR_LEN (clss->attr.ca.members))-1;
284 }
285 entity *get_class_member   (type *clss, int pos) {
286   assert(clss && (clss->type_op == type_class));
287   assert(pos >= 0 && pos < get_class_n_member(clss));
288   return clss->attr.ca.members[pos+1];
289 }
290 void    set_class_member   (type *clss, entity *member, int pos) {
291   assert(clss && (clss->type_op == type_class));
292   assert(pos >= 0 && pos < get_class_n_member(clss));
293   clss->attr.ca.members[pos+1] = member;
294 }
295 void    set_class_members  (type *clss, entity **members, int arity) {
296   int i;
297   assert(clss && (clss->type_op == type_class));
298   DEL_ARR_F(clss->attr.ca.members);
299   clss->attr.ca.members    = NEW_ARR_F (entity *, 1);
300   for (i = 0; i < arity; i++) {
301     set_entity_owner(members[i], clss);
302     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
303   }
304 }
305 void    remove_class_member(type *clss, entity *member) {
306   int i;
307   assert(clss && (clss->type_op == type_class));
308   for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
309     if (clss->attr.ca.members[i+1] == member) {
310       for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
311         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
312       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
313       break;
314     }
315 }
316
317 void    add_class_subtype   (type *clss, type *subtype) {
318   int i;
319   assert(clss && (clss->type_op == type_class));
320   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
321   for (i = 0; i < get_class_n_supertype(subtype); i++)
322     if (get_class_supertype(subtype, i) == clss)
323       /* Class already registered */
324       return;
325   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
326 }
327 int     get_class_n_subtype (type *clss) {
328   assert(clss && (clss->type_op == type_class));
329   return (ARR_LEN (clss->attr.ca.subtypes))-1;
330 }
331 type   *get_class_subtype   (type *clss, int pos) {
332   assert(clss && (clss->type_op == type_class));
333   assert(pos >= 0 && pos < get_class_n_subtype(clss));
334   return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
335 }
336 void    set_class_subtype   (type *clss, type *subtype, int pos) {
337   assert(clss && (clss->type_op == type_class));
338   assert(pos >= 0 && pos < get_class_n_subtype(clss));
339   clss->attr.ca.subtypes[pos+1] = subtype;
340 }
341 void    remove_class_subtype(type *clss, type *subtype) {
342   int i;
343   assert(clss && (clss->type_op == type_class));
344   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
345     if (clss->attr.ca.subtypes[i+1] == subtype) {
346       for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
347         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
348       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
349       break;
350     }
351 }
352
353 void    add_class_supertype   (type *clss, type *supertype) {
354   int i;
355   assert(clss && (clss->type_op == type_class));
356   assert(supertype && (supertype -> type_op == type_class));
357   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
358   for (i = 0; i < get_class_n_subtype(supertype); i++)
359     if (get_class_subtype(supertype, i) == clss)
360       /* Class already registered */
361       return;
362   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
363 }
364 int     get_class_n_supertype (type *clss) {
365   assert(clss && (clss->type_op == type_class));
366   return (ARR_LEN (clss->attr.ca.supertypes))-1;
367 }
368 type   *get_class_supertype   (type *clss, int pos) {
369   assert(clss && (clss->type_op == type_class));
370   assert(pos >= 0 && pos < get_class_n_supertype(clss));
371   return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
372 }
373 void    set_class_supertype   (type *clss, type *supertype, int pos) {
374   assert(clss && (clss->type_op == type_class));
375   assert(pos >= 0 && pos < get_class_n_supertype(clss));
376   clss->attr.ca.supertypes[pos+1] = supertype;
377 }
378 void    remove_class_supertype(type *clss, type *supertype) {
379   int i;
380   assert(clss && (clss->type_op == type_class));
381   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
382     if (clss->attr.ca.supertypes[i+1] == supertype) {
383       for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
384         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
385       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
386       break;
387     }
388 }
389
390 INLINE peculiarity get_class_peculiarity (type *clss) {
391   assert(clss && (clss->type_op == type_class));
392   return clss->attr.ca.peculiarity;
393 }
394 INLINE void        set_class_peculiarity (type *clss, peculiarity pec) {
395   assert(clss && (clss->type_op == type_class));
396   clss->attr.ca.peculiarity = pec;
397 }
398
399 void set_class_dfn (type *clss, int dfn)
400 {
401   clss->attr.ca.dfn        = dfn;
402 }
403
404 int get_class_dfn (type *clss)
405 {
406   return (clss->attr.ca.dfn);
407 }
408
409 /* typecheck */
410 bool    is_class_type(type *clss) {
411   assert(clss);
412   if (clss->type_op == type_class) return 1; else return 0;
413 }
414
415 /*******************************************************************/
416 /** TYPE_STRUCT                                                   **/
417 /*******************************************************************/
418
419 /* create a new type struct */
420 type   *new_type_struct (ident *name) {
421   type *res;
422   res = new_type(type_struct, NULL, name);
423   res->attr.sa.members = NEW_ARR_F (entity *, 1);
424   return res;
425 }
426 INLINE void free_struct_attrs (type *strct) {
427   assert(strct && (strct->type_op == type_struct));
428   DEL_ARR_F(strct->attr.sa.members);
429 }
430 /* manipulate private fields of struct */
431 void    add_struct_member   (type *strct, entity *member) {
432   assert(strct && (strct->type_op == type_struct));
433   assert(get_type_tpop(get_entity_type(member)) != type_method);
434     /*    @@@ lowerfirm geht nicht durch */
435   ARR_APP1 (entity *, strct->attr.sa.members, member);
436 }
437 int     get_struct_n_member (type *strct) {
438   assert(strct && (strct->type_op == type_struct));
439   return (ARR_LEN (strct->attr.sa.members))-1;
440 }
441 entity *get_struct_member   (type *strct, int pos) {
442   assert(strct && (strct->type_op == type_struct));
443   assert(pos >= 0 && pos < get_struct_n_member(strct));
444   return strct->attr.sa.members[pos+1];
445 }
446 void    set_struct_member   (type *strct, int pos, entity *member) {
447   assert(strct && (strct->type_op == type_struct));
448   assert(pos >= 0 && pos < get_struct_n_member(strct));
449   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
450   strct->attr.sa.members[pos+1] = member;
451 }
452 void    remove_struct_member(type *strct, entity *member) {
453   int i;
454   assert(strct && (strct->type_op == type_struct));
455   for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
456     if (strct->attr.sa.members[i+1] == member) {
457       for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
458         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
459       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
460       break;
461     }
462 }
463 /* typecheck */
464 bool    is_struct_type(type *strct) {
465   assert(strct);
466   if (strct->type_op == type_struct) return 1; else return 0;
467 }
468
469 /*******************************************************************/
470 /** TYPE_METHOD                                                   **/
471 /*******************************************************************/
472
473 /* Create a new method type.
474    N_param is the number of parameters, n_res the number of results.  */
475 type *new_type_method (ident *name, int n_param, int n_res) {
476   type *res;
477   res = new_type(type_method, mode_p, name);
478   res->state = layout_fixed;
479   res->size = get_mode_size(mode_p);
480   res->attr.ma.n_params   = n_param;
481   res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
482   res->attr.ma.n_res      = n_res;
483   res->attr.ma.res_type   = (type **) xmalloc (sizeof (type *) * n_res);
484   return res;
485 }
486 INLINE void free_method_attrs(type *method) {
487   assert(method && (method->type_op == type_method));
488   free(method->attr.ma.param_type);
489   free(method->attr.ma.res_type);
490 }
491 /* manipulate private fields of method. */
492 int   get_method_n_params  (type *method) {
493   assert(method && (method->type_op == type_method));
494   return method->attr.ma.n_params;
495 }
496 type *get_method_param_type(type *method, int pos) {
497   assert(method && (method->type_op == type_method));
498   assert(pos >= 0 && pos < get_method_n_params(method));
499   return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
500 }
501 void  set_method_param_type(type *method, int pos, type* type) {
502   assert(method && (method->type_op == type_method));
503   assert(pos >= 0 && pos < get_method_n_params(method));
504   method->attr.ma.param_type[pos] = type;
505 }
506
507 int   get_method_n_res   (type *method) {
508   assert(method && (method->type_op == type_method));
509   return method->attr.ma.n_res;
510 }
511 type *get_method_res_type(type *method, int pos) {
512   assert(method && (method->type_op == type_method));
513   assert(pos >= 0 && pos < get_method_n_res(method));
514   return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
515 }
516 void  set_method_res_type(type *method, int pos, type* type) {
517   assert(method && (method->type_op == type_method));
518   assert(pos >= 0 && pos < get_method_n_res(method));
519   method->attr.ma.res_type[pos] = type;
520 }
521
522 /* typecheck */
523 bool  is_method_type     (type *method) {
524   assert(method);
525   if (method->type_op == type_method) return 1; else return 0;
526 }
527 /*****/
528
529 /*******************************************************************/
530 /** TYPE_UNION                                                    **/
531 /*******************************************************************/
532
533 /* create a new type uni */
534 type  *new_type_uni (ident *name) {
535   type *res;
536   res = new_type(type_union, NULL, name);
537   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
538     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
539   res->attr.ua.members = NEW_ARR_F (entity *, 1);
540   return res;
541 }
542 INLINE void free_union_attrs (type *uni) {
543   assert(uni && (uni->type_op == type_union));
544   DEL_ARR_F(uni->attr.ua.members);
545 }
546 /* manipulate private fields of union */
547 #if 0
548 int    get_union_n_types      (type *uni) {
549   assert(uni && (uni->type_op == type_union));
550   return uni->attr.ua.n_types;
551 }
552 type  *get_union_unioned_type (type *uni, int pos) {
553   assert(uni && (uni->type_op == type_union));
554   assert(pos >= 0 && pos < get_union_n_types(uni));
555   return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
556 }
557 void   set_union_unioned_type (type *uni, int pos, type *type) {
558   assert(uni && (uni->type_op == type_union));
559   assert(pos >= 0 && pos < get_union_n_types(uni));
560   uni->attr.ua.unioned_type[pos] = type;
561 }
562 ident *get_union_delim_nameid (type *uni, int pos) {
563   assert(uni && (uni->type_op == type_union));
564   assert(pos >= 0 && pos < get_union_n_types(uni));
565   return uni->attr.ua.delim_names[pos];
566 }
567 const char *get_union_delim_name (type *uni, int pos) {
568   assert(uni && (uni->type_op == type_union));
569   assert(pos >= 0 && pos < get_union_n_types(uni));
570   return id_to_str(uni->attr.ua.delim_names[pos]);
571 }
572 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
573   assert(uni && (uni->type_op == type_union));
574   assert(pos >= 0 && pos < get_union_n_types(uni));
575   uni->attr.ua.delim_names[pos] = id;
576 }
577 #endif
578 int    get_union_n_members      (type *uni) {
579   assert(uni && (uni->type_op == type_union));
580   return (ARR_LEN (uni->attr.ua.members))-1;
581 }
582 void    add_union_member   (type *uni, entity *member) {
583   assert(uni && (uni->type_op == type_union));
584   ARR_APP1 (entity *, uni->attr.ua.members, member);
585 }
586 entity  *get_union_member (type *uni, int pos) {
587   assert(uni && (uni->type_op == type_union));
588   assert(pos >= 0 && pos < get_union_n_members(uni));
589   return uni->attr.ua.members[pos+1];
590 }
591 void   set_union_member (type *uni, int pos, entity *member) {
592   assert(uni && (uni->type_op == type_union));
593   assert(pos >= 0 && pos < get_union_n_members(uni));
594   uni->attr.ua.members[pos+1] = member;
595 }
596 void   remove_union_member(type *uni, entity *member) {
597   int i;
598   assert(uni && (uni->type_op == type_union));
599   for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
600     if (uni->attr.ua.members[i+1] == member) {
601       for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
602         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
603       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
604       break;
605     }
606 }
607
608 /* typecheck */
609 bool   is_union_type         (type *uni) {
610   assert(uni);
611   if (uni->type_op == type_union) return 1; else return 0;
612 }
613
614 /*******************************************************************/
615 /** TYPE_ARRAY                                                    **/
616 /*******************************************************************/
617
618
619 /* create a new type array -- set dimension sizes independently */
620 type *new_type_array         (ident *name, int n_dimensions,
621                                                           type *element_type) {
622   type *res;
623   int i;
624   assert((element_type->type_op != type_method));
625   assert(get_type_tpop(element_type) != type_method);
626   res = new_type(type_array, NULL, name);
627   res->attr.aa.n_dimensions = n_dimensions;
628   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
629   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
630   res->attr.aa.order  = (int *) xmalloc (sizeof (int) * n_dimensions);
631
632   for (i = 0; i < n_dimensions; i++) {
633     res->attr.aa.lower_bound[i]  = NULL;
634     res->attr.aa.upper_bound[i]  = NULL;
635     res->attr.aa.order[i] = i;
636   }
637   res->attr.aa.element_type = element_type;
638   new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
639   return res;
640 }
641
642 INLINE void free_array_attrs (type *array) {
643   assert(array && (array->type_op == type_array));
644   free(array->attr.aa.lower_bound);
645   free(array->attr.aa.upper_bound);
646 }
647
648 /* manipulate private fields of array type */
649 int   get_array_n_dimensions (type *array) {
650   assert(array && (array->type_op == type_array));
651   return array->attr.aa.n_dimensions;
652 }
653 void  set_array_bounds_int (type *array, int dimension, int lower_bound,
654                             int upper_bound) {
655   ir_graph *rem;
656   assert(array && (array->type_op == type_array));
657   rem = current_ir_graph;
658   current_ir_graph = get_const_code_irg();
659   array->attr.aa.lower_bound[dimension] =
660     new_Const(mode_I, tarval_from_long (mode_I, lower_bound));
661   array->attr.aa.upper_bound[dimension] =
662     new_Const(mode_I, tarval_from_long (mode_I, upper_bound));
663   current_ir_graph = rem;
664 }
665
666 void  set_array_bounds (type *array, int dimension, ir_node * lower_bound,
667                         ir_node * upper_bound) {
668   assert(array && (array->type_op == type_array));
669   array->attr.aa.lower_bound[dimension] = lower_bound;
670   array->attr.aa.upper_bound[dimension] = upper_bound;
671 }
672 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
673   ir_graph *rem;
674   assert(array && (array->type_op == type_array));
675   rem = current_ir_graph;
676   current_ir_graph = get_const_code_irg();
677   array->attr.aa.lower_bound[dimension] =
678     new_Const(mode_I, tarval_from_long (mode_I, lower_bound));
679   current_ir_graph = rem;
680 }
681 void  set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
682   assert(array && (array->type_op == type_array));
683   array->attr.aa.lower_bound[dimension] = lower_bound;
684 }
685 void  set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
686   assert(array && (array->type_op == type_array));
687   array->attr.aa.upper_bound[dimension] = upper_bound;
688 }
689 ir_node * get_array_lower_bound  (type *array, int dimension) {
690   assert(array && (array->type_op == type_array));
691   return array->attr.aa.lower_bound[dimension];
692 }
693 ir_node * get_array_upper_bound  (type *array, int dimension) {
694   assert(array && (array->type_op == type_array));
695   return array->attr.aa.upper_bound[dimension];
696 }
697 void set_array_order (type *array, int dimension, int order) {
698   assert(array && (array->type_op == type_array));
699   array->attr.aa.order[dimension] = order;
700 }
701 int  get_array_order (type *array, int dimension) {
702   assert(array && (array->type_op == type_array));
703   return array->attr.aa.order[dimension];
704 }
705 void  set_array_element_type (type *array, type *type) {
706   assert(array && (array->type_op == type_array));
707   array->attr.aa.element_type = type;
708 }
709 type *get_array_element_type (type *array) {
710   assert(array && (array->type_op == type_array));
711   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
712 }
713 void  set_array_element_entity (type *array, entity *ent) {
714   assert(array && (array->type_op == type_array));
715   assert((get_entity_type(ent)->type_op != type_method));
716   array->attr.aa.element_ent = ent;
717   array->attr.aa.element_type = get_entity_type(ent);
718 }
719 entity *get_array_element_entity (type *array) {
720   assert(array && (array->type_op == type_array));
721   return array->attr.aa.element_ent;
722 }
723
724 /* typecheck */
725 bool   is_array_type         (type *array) {
726   assert(array);
727   if (array->type_op == type_array) return 1; else return 0;
728 }
729
730 /*******************************************************************/
731 /** TYPE_ENUMERATION                                              **/
732 /*******************************************************************/
733
734 /* create a new type enumeration -- set the enumerators independently */
735 type   *new_type_enumeration    (ident *name, int n_enums) {
736   type *res;
737   int i;
738   res = new_type(type_enumeration, NULL, name);
739   res->attr.ea.n_enums     = n_enums;
740   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
741   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
742   for (i = 0; i < n_enums; i++) {
743     res->attr.ea.enumer[i] = NULL;
744     res->attr.ea.enum_nameid  = NULL;
745   }
746   return res;
747 }
748
749 INLINE void free_enumeration_attrs(type *enumeration) {
750   assert(enumeration && (enumeration->type_op == type_enumeration));
751   free(enumeration->attr.ea.enumer);
752   free(enumeration->attr.ea.enum_nameid);
753 }
754
755 /* manipulate fields of enumeration type. */
756 int     get_enumeration_n_enums (type *enumeration) {
757   assert(enumeration && (enumeration->type_op == type_enumeration));
758   return enumeration->attr.ea.n_enums;
759 }
760 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
761   assert(enumeration && (enumeration->type_op == type_enumeration));
762   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
763   enumeration->attr.ea.enumer[pos] = con;
764 }
765 tarval *get_enumeration_enum    (type *enumeration, int pos) {
766   assert(enumeration && (enumeration->type_op == type_enumeration));
767   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
768   return enumeration->attr.ea.enumer[pos];
769 }
770 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
771   assert(enumeration && (enumeration->type_op == type_enumeration));
772   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
773   enumeration->attr.ea.enum_nameid[pos] = id;
774 }
775 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
776   assert(enumeration && (enumeration->type_op == type_enumeration));
777   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
778   return enumeration->attr.ea.enum_nameid[pos];
779 }
780 const char *get_enumeration_name(type *enumeration, int pos) {
781   assert(enumeration && (enumeration->type_op == type_enumeration));
782   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
783   return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
784 }
785
786 /* typecheck */
787 bool    is_enumeration_type     (type *enumeration) {
788   assert(enumeration);
789   if (enumeration->type_op == type_enumeration) return 1; else return 0;
790 }
791
792 /*******************************************************************/
793 /** TYPE_POINTER                                                  **/
794 /*******************************************************************/
795
796 /* Create a new type pointer */
797 type *new_type_pointer           (ident *name, type *points_to) {
798   type *res;
799   res = new_type(type_pointer, mode_p, name);
800   res->attr.pa.points_to = points_to;
801   res->size = get_mode_size(res->mode);
802   res->state = layout_fixed;
803   return res;
804 }
805 INLINE void free_pointer_attrs (type *pointer) {
806   assert(pointer && (pointer->type_op == type_pointer));
807 }
808 /* manipulate fields of type_pointer */
809 void  set_pointer_points_to_type (type *pointer, type *type) {
810   assert(pointer && (pointer->type_op == type_pointer));
811   pointer->attr.pa.points_to = type;
812 }
813 type *get_pointer_points_to_type (type *pointer) {
814   assert(pointer && (pointer->type_op == type_pointer));
815   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
816 }
817
818 /* typecheck */
819 bool  is_pointer_type            (type *pointer) {
820   assert(pointer);
821   if (pointer->type_op == type_pointer) return 1; else return 0;
822 }
823
824
825 /*******************************************************************/
826 /** TYPE_PRIMITIVE                                                **/
827 /*******************************************************************/
828
829 /* create a new type primitive */
830 type *new_type_primitive (ident *name, ir_mode *mode) {
831   type *res;
832   /* @@@ assert( mode_is_data(mode) && (!mode == mode_p)); */
833   res = new_type(type_primitive, mode, name);
834   res->size = get_mode_size(mode);
835   res->state = layout_fixed;
836   return res;
837 }
838 INLINE void free_primitive_attrs (type *primitive) {
839   assert(primitive && (primitive->type_op == type_primitive));
840 }
841
842 /* typecheck */
843 bool  is_primitive_type  (type *primitive) {
844   assert(primitive && primitive->kind == k_type);
845   if (primitive->type_op == type_primitive) return 1; else return 0;
846 }
847
848 /*******************************************************************/
849 /** common functionality                                          **/
850 /*******************************************************************/
851
852
853 INLINE int is_atomic_type(type *tp) {
854   assert(tp && tp->kind == k_type);
855   return (is_primitive_type(tp) || is_pointer_type(tp) ||
856           is_enumeration_type(tp));
857 }
858 INLINE int is_compound_type(type *tp) {
859   assert(tp && tp->kind == k_type);
860   return (is_class_type(tp) || is_struct_type(tp) ||
861           is_array_type(tp) || is_union_type(tp));
862 }