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