Added flag "peculiarity" to entity.h, type.h.
[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
266   return res;
267 }
268 inline void free_class_attrs(type *clss) {
269   assert(clss && (clss->type_op == type_class));
270   DEL_ARR_F(clss->attr.ca.members);
271   DEL_ARR_F(clss->attr.ca.subtypes);
272   DEL_ARR_F(clss->attr.ca.supertypes);
273 }
274 /* manipulate private fields of class type  */
275 void    add_class_member   (type *clss, entity *member) {
276   assert(clss && (clss->type_op == type_class));
277   ARR_APP1 (entity *, clss->attr.ca.members, member);
278 }
279 int     get_class_n_member (type *clss) {
280   assert(clss && (clss->type_op == type_class));
281   return (ARR_LEN (clss->attr.ca.members))-1;
282 }
283 entity *get_class_member   (type *clss, int pos) {
284   assert(clss && (clss->type_op == type_class));
285   assert(pos >= 0 && pos < get_class_n_member(clss));
286   return clss->attr.ca.members[pos+1];
287 }
288 void    set_class_member   (type *clss, entity *member, int pos) {
289   assert(clss && (clss->type_op == type_class));
290   assert(pos >= 0 && pos < get_class_n_member(clss));
291   clss->attr.ca.members[pos+1] = member;
292 }
293 void    set_class_members  (type *clss, entity **members, int arity) {
294   int i;
295   assert(clss && (clss->type_op == type_class));
296   DEL_ARR_F(clss->attr.ca.members);
297   clss->attr.ca.members    = NEW_ARR_F (entity *, 1);
298   for (i = 0; i < arity; i++) {
299     set_entity_owner(members[i], clss);
300     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
301   }
302 }
303 void    remove_class_member(type *clss, entity *member) {
304   int i;
305   assert(clss && (clss->type_op == type_class));
306   for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
307     if (clss->attr.ca.members[i+1] == member) {
308       for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
309         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
310       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
311       break;
312     }
313 }
314
315 void    add_class_subtype   (type *clss, type *subtype) {
316   int i;
317   assert(clss && (clss->type_op == type_class));
318   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
319   for (i = 0; i < get_class_n_supertype(subtype); i++)
320     if (get_class_supertype(subtype, i) == clss)
321       /* Class already registered */
322       return;
323   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
324 }
325 int     get_class_n_subtype (type *clss) {
326   assert(clss && (clss->type_op == type_class));
327   return (ARR_LEN (clss->attr.ca.subtypes))-1;
328 }
329 type   *get_class_subtype   (type *clss, int pos) {
330   assert(clss && (clss->type_op == type_class));
331   assert(pos >= 0 && pos < get_class_n_subtype(clss));
332   return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
333 }
334 void    set_class_subtype   (type *clss, type *subtype, int pos) {
335   assert(clss && (clss->type_op == type_class));
336   assert(pos >= 0 && pos < get_class_n_subtype(clss));
337   clss->attr.ca.subtypes[pos+1] = subtype;
338 }
339 void    remove_class_subtype(type *clss, type *subtype) {
340   int i;
341   assert(clss && (clss->type_op == type_class));
342   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
343     if (clss->attr.ca.subtypes[i+1] == subtype) {
344       for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
345         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
346       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
347       break;
348     }
349 }
350
351 void    add_class_supertype   (type *clss, type *supertype) {
352   int i;
353   assert(clss && (clss->type_op == type_class));
354   assert(supertype && (supertype -> type_op == type_class));
355   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
356   for (i = 0; i < get_class_n_subtype(supertype); i++)
357     if (get_class_subtype(supertype, i) == clss)
358       /* Class already registered */
359       return;
360   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
361 }
362 int     get_class_n_supertype (type *clss) {
363   assert(clss && (clss->type_op == type_class));
364   return (ARR_LEN (clss->attr.ca.supertypes))-1;
365 }
366 type   *get_class_supertype   (type *clss, int pos) {
367   assert(clss && (clss->type_op == type_class));
368   assert(pos >= 0 && pos < get_class_n_supertype(clss));
369   return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
370 }
371 void    set_class_supertype   (type *clss, type *supertype, int pos) {
372   assert(clss && (clss->type_op == type_class));
373   assert(pos >= 0 && pos < get_class_n_supertype(clss));
374   clss->attr.ca.supertypes[pos+1] = supertype;
375 }
376 void    remove_class_supertype(type *clss, type *supertype) {
377   int i;
378   assert(clss && (clss->type_op == type_class));
379   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
380     if (clss->attr.ca.supertypes[i+1] == supertype) {
381       for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
382         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
383       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
384       break;
385     }
386 }
387
388 inline peculiarity get_class_peculiarity (type *clss) {
389   assert(clss && (clss->type_op == type_class));
390   return clss->attr.ca.peculiarity;
391 }
392 inline void        set_class_peculiarity (type *clss, peculiarity pec) {
393   assert(clss && (clss->type_op == type_class));
394   clss->attr.ca.peculiarity = pec;
395 }
396
397 /* typecheck */
398 bool    is_class_type(type *clss) {
399   assert(clss);
400   if (clss->type_op == type_class) return 1; else return 0;
401 }
402
403 /*******************************************************************/
404 /** TYPE_STRUCT                                                   **/
405 /*******************************************************************/
406
407 /* create a new type struct */
408 type   *new_type_struct (ident *name) {
409   type *res;
410   res = new_type(type_struct, NULL, name);
411   res->attr.sa.members = NEW_ARR_F (entity *, 1);
412   return res;
413 }
414 inline void free_struct_attrs (type *strct) {
415   assert(strct && (strct->type_op == type_struct));
416   DEL_ARR_F(strct->attr.sa.members);
417 }
418 /* manipulate private fields of struct */
419 void    add_struct_member   (type *strct, entity *member) {
420   assert(strct && (strct->type_op == type_struct));
421   assert(get_type_tpop(get_entity_type(member)) != type_method);
422     /*    @@@ lowerfirm geht nicht durch */
423   ARR_APP1 (entity *, strct->attr.sa.members, member);
424 }
425 int     get_struct_n_member (type *strct) {
426   assert(strct && (strct->type_op == type_struct));
427   return (ARR_LEN (strct->attr.sa.members))-1;
428 }
429 entity *get_struct_member   (type *strct, int pos) {
430   assert(strct && (strct->type_op == type_struct));
431   assert(pos >= 0 && pos < get_struct_n_member(strct));
432   return strct->attr.sa.members[pos+1];
433 }
434 void    set_struct_member   (type *strct, int pos, entity *member) {
435   assert(strct && (strct->type_op == type_struct));
436   assert(pos >= 0 && pos < get_struct_n_member(strct));
437   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
438   strct->attr.sa.members[pos+1] = member;
439 }
440 void    remove_struct_member(type *strct, entity *member) {
441   int i;
442   assert(strct && (strct->type_op == type_struct));
443   for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
444     if (strct->attr.sa.members[i+1] == member) {
445       for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
446         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
447       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
448       break;
449     }
450 }
451 /* typecheck */
452 bool    is_struct_type(type *strct) {
453   assert(strct);
454   if (strct->type_op == type_struct) return 1; else return 0;
455 }
456
457 /*******************************************************************/
458 /** TYPE_METHOD                                                   **/
459 /*******************************************************************/
460
461 /* Create a new method type.
462    N_param is the number of parameters, n_res the number of results.  */
463 type *new_type_method (ident *name, int n_param, int n_res) {
464   type *res;
465   res = new_type(type_method, mode_p, name);
466   res->state = layout_fixed;
467   res->size = get_mode_size(mode_p);
468   res->attr.ma.n_params   = n_param;
469   res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
470   res->attr.ma.n_res      = n_res;
471   res->attr.ma.res_type   = (type **) xmalloc (sizeof (type *) * n_res);
472   return res;
473 }
474 inline void free_method_attrs(type *method) {
475   assert(method && (method->type_op == type_method));
476   free(method->attr.ma.param_type);
477   free(method->attr.ma.res_type);
478 }
479 /* manipulate private fields of method. */
480 int   get_method_n_params  (type *method) {
481   assert(method && (method->type_op == type_method));
482   return method->attr.ma.n_params;
483 }
484 type *get_method_param_type(type *method, int pos) {
485   assert(method && (method->type_op == type_method));
486   assert(pos >= 0 && pos < get_method_n_params(method));
487   return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
488 }
489 void  set_method_param_type(type *method, int pos, type* type) {
490   assert(method && (method->type_op == type_method));
491   assert(pos >= 0 && pos < get_method_n_params(method));
492   method->attr.ma.param_type[pos] = type;
493 }
494
495 int   get_method_n_res   (type *method) {
496   assert(method && (method->type_op == type_method));
497   return method->attr.ma.n_res;
498 }
499 type *get_method_res_type(type *method, int pos) {
500   assert(method && (method->type_op == type_method));
501   assert(pos >= 0 && pos < get_method_n_res(method));
502   return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
503 }
504 void  set_method_res_type(type *method, int pos, type* type) {
505   assert(method && (method->type_op == type_method));
506   assert(pos >= 0 && pos < get_method_n_res(method));
507   method->attr.ma.res_type[pos] = type;
508 }
509
510 /* typecheck */
511 bool  is_method_type     (type *method) {
512   assert(method);
513   if (method->type_op == type_method) return 1; else return 0;
514 }
515 /*****/
516
517 /*******************************************************************/
518 /** TYPE_UNION                                                    **/
519 /*******************************************************************/
520
521 /* create a new type uni */
522 type  *new_type_uni (ident *name) {
523   type *res;
524   res = new_type(type_union, NULL, name);
525   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
526     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
527   res->attr.ua.members = NEW_ARR_F (entity *, 1);
528   return res;
529 }
530 inline void free_union_attrs (type *uni) {
531   assert(uni && (uni->type_op == type_union));
532   DEL_ARR_F(uni->attr.ua.members);
533 }
534 /* manipulate private fields of union */
535 #if 0
536 int    get_union_n_types      (type *uni) {
537   assert(uni && (uni->type_op == type_union));
538   return uni->attr.ua.n_types;
539 }
540 type  *get_union_unioned_type (type *uni, int pos) {
541   assert(uni && (uni->type_op == type_union));
542   assert(pos >= 0 && pos < get_union_n_types(uni));
543   return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
544 }
545 void   set_union_unioned_type (type *uni, int pos, type *type) {
546   assert(uni && (uni->type_op == type_union));
547   assert(pos >= 0 && pos < get_union_n_types(uni));
548   uni->attr.ua.unioned_type[pos] = type;
549 }
550 ident *get_union_delim_nameid (type *uni, int pos) {
551   assert(uni && (uni->type_op == type_union));
552   assert(pos >= 0 && pos < get_union_n_types(uni));
553   return uni->attr.ua.delim_names[pos];
554 }
555 const char *get_union_delim_name (type *uni, int pos) {
556   assert(uni && (uni->type_op == type_union));
557   assert(pos >= 0 && pos < get_union_n_types(uni));
558   return id_to_str(uni->attr.ua.delim_names[pos]);
559 }
560 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
561   assert(uni && (uni->type_op == type_union));
562   assert(pos >= 0 && pos < get_union_n_types(uni));
563   uni->attr.ua.delim_names[pos] = id;
564 }
565 #endif
566 int    get_union_n_members      (type *uni) {
567   assert(uni && (uni->type_op == type_union));
568   return (ARR_LEN (uni->attr.ua.members))-1;
569 }
570 void    add_union_member   (type *uni, entity *member) {
571   assert(uni && (uni->type_op == type_union));
572   ARR_APP1 (entity *, uni->attr.ua.members, member);
573 }
574 entity  *get_union_member (type *uni, int pos) {
575   assert(uni && (uni->type_op == type_union));
576   assert(pos >= 0 && pos < get_union_n_members(uni));
577   return uni->attr.ua.members[pos+1];
578 }
579 void   set_union_member (type *uni, int pos, entity *member) {
580   assert(uni && (uni->type_op == type_union));
581   assert(pos >= 0 && pos < get_union_n_members(uni));
582   uni->attr.ua.members[pos+1] = member;
583 }
584 void   remove_union_member(type *uni, entity *member) {
585   int i;
586   assert(uni && (uni->type_op == type_union));
587   for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
588     if (uni->attr.ua.members[i+1] == member) {
589       for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
590         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
591       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
592       break;
593     }
594 }
595
596 /* typecheck */
597 bool   is_union_type         (type *uni) {
598   assert(uni);
599   if (uni->type_op == type_union) return 1; else return 0;
600 }
601
602 /*******************************************************************/
603 /** TYPE_ARRAY                                                    **/
604 /*******************************************************************/
605
606
607 /* create a new type array -- set dimension sizes independently */
608 type *new_type_array         (ident *name, int n_dimensions,
609                               type *element_type) {
610   type *res;
611   int i;
612   assert((element_type->type_op != type_method));
613   assert(get_type_tpop(element_type) != type_method);
614   res = new_type(type_array, NULL, name);
615   res->attr.aa.n_dimensions = n_dimensions;
616   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
617   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
618   res->attr.aa.order  = (int *) xmalloc (sizeof (int) * n_dimensions);
619
620   for (i = 0; i < n_dimensions; i++) {
621     res->attr.aa.lower_bound[i]  = NULL;
622     res->attr.aa.upper_bound[i]  = NULL;
623     res->attr.aa.order[i] = i;
624   }
625   res->attr.aa.element_type = element_type;
626   new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
627   return res;
628 }
629 inline void free_array_attrs (type *array) {
630   assert(array && (array->type_op == type_array));
631   free(array->attr.aa.lower_bound);
632   free(array->attr.aa.upper_bound);
633 }
634
635 /* manipulate private fields of array type */
636 int   get_array_n_dimensions (type *array) {
637   assert(array && (array->type_op == type_array));
638   return array->attr.aa.n_dimensions;
639 }
640 void  set_array_bounds_int (type *array, int dimension, int lower_bound,
641                             int upper_bound) {
642   ir_graph *rem;
643   assert(array && (array->type_op == type_array));
644   rem = current_ir_graph;
645   current_ir_graph = get_const_code_irg();
646   array->attr.aa.lower_bound[dimension] =
647     new_Const(mode_I, tarval_from_long (mode_I, lower_bound));
648   array->attr.aa.upper_bound[dimension] =
649     new_Const(mode_I, tarval_from_long (mode_I, upper_bound));
650   current_ir_graph = rem;
651 }
652
653 void  set_array_bounds (type *array, int dimension, ir_node * lower_bound,
654                         ir_node * upper_bound) {
655   assert(array && (array->type_op == type_array));
656   array->attr.aa.lower_bound[dimension] = lower_bound;
657   array->attr.aa.upper_bound[dimension] = upper_bound;
658 }
659 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
660   ir_graph *rem;
661   assert(array && (array->type_op == type_array));
662   rem = current_ir_graph;
663   current_ir_graph = get_const_code_irg();
664   array->attr.aa.lower_bound[dimension] =
665     new_Const(mode_I, tarval_from_long (mode_I, lower_bound));
666   current_ir_graph = rem;
667 }
668 void  set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
669   assert(array && (array->type_op == type_array));
670   array->attr.aa.lower_bound[dimension] = lower_bound;
671 }
672 void  set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
673   assert(array && (array->type_op == type_array));
674   array->attr.aa.upper_bound[dimension] = upper_bound;
675 }
676 ir_node * get_array_lower_bound  (type *array, int dimension) {
677   assert(array && (array->type_op == type_array));
678   return array->attr.aa.lower_bound[dimension];
679 }
680 ir_node * get_array_upper_bound  (type *array, int dimension) {
681   assert(array && (array->type_op == type_array));
682   return array->attr.aa.upper_bound[dimension];
683 }
684 void set_array_order (type *array, int dimension, int order) {
685   assert(array && (array->type_op == type_array));
686   array->attr.aa.order[dimension] = order;
687 }
688 int  get_array_order (type *array, int dimension) {
689   assert(array && (array->type_op == type_array));
690   return array->attr.aa.order[dimension];
691 }
692 void  set_array_element_type (type *array, type *type) {
693   assert(array && (array->type_op == type_array));
694   array->attr.aa.element_type = type;
695 }
696 type *get_array_element_type (type *array) {
697   assert(array && (array->type_op == type_array));
698   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
699 }
700 void  set_array_element_entity (type *array, entity *ent) {
701   assert(array && (array->type_op == type_array));
702   assert((get_entity_type(ent)->type_op != type_method));
703   array->attr.aa.element_ent = ent;
704   array->attr.aa.element_type = get_entity_type(ent);
705 }
706 entity *get_array_element_entity (type *array) {
707   assert(array && (array->type_op == type_array));
708   return array->attr.aa.element_ent;
709 }
710
711 /* typecheck */
712 bool   is_array_type         (type *array) {
713   assert(array);
714   if (array->type_op == type_array) return 1; else return 0;
715 }
716
717 /*******************************************************************/
718 /** TYPE_ENUMERATION                                              **/
719 /*******************************************************************/
720
721 /* create a new type enumeration -- set the enumerators independently */
722 type   *new_type_enumeration    (ident *name, int n_enums) {
723   type *res;
724   int i;
725   res = new_type(type_enumeration, NULL, name);
726   res->attr.ea.n_enums     = n_enums;
727   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
728   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
729   for (i = 0; i < n_enums; i++) {
730     res->attr.ea.enumer[i] = NULL;
731     res->attr.ea.enum_nameid  = NULL;
732   }
733   return res;
734 }
735
736 inline void free_enumeration_attrs(type *enumeration) {
737   assert(enumeration && (enumeration->type_op == type_enumeration));
738   free(enumeration->attr.ea.enumer);
739   free(enumeration->attr.ea.enum_nameid);
740 }
741
742 /* manipulate fields of enumeration type. */
743 int     get_enumeration_n_enums (type *enumeration) {
744   assert(enumeration && (enumeration->type_op == type_enumeration));
745   return enumeration->attr.ea.n_enums;
746 }
747 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
748   assert(enumeration && (enumeration->type_op == type_enumeration));
749   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
750   enumeration->attr.ea.enumer[pos] = con;
751 }
752 tarval *get_enumeration_enum    (type *enumeration, int pos) {
753   assert(enumeration && (enumeration->type_op == type_enumeration));
754   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
755   return enumeration->attr.ea.enumer[pos];
756 }
757 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
758   assert(enumeration && (enumeration->type_op == type_enumeration));
759   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
760   enumeration->attr.ea.enum_nameid[pos] = id;
761 }
762 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
763   assert(enumeration && (enumeration->type_op == type_enumeration));
764   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
765   return enumeration->attr.ea.enum_nameid[pos];
766 }
767 const char *get_enumeration_name(type *enumeration, int pos) {
768   assert(enumeration && (enumeration->type_op == type_enumeration));
769   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
770   return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
771 }
772
773 /* typecheck */
774 bool    is_enumeration_type     (type *enumeration) {
775   assert(enumeration);
776   if (enumeration->type_op == type_enumeration) return 1; else return 0;
777 }
778
779 /*******************************************************************/
780 /** TYPE_POINTER                                                  **/
781 /*******************************************************************/
782
783 /* Create a new type pointer */
784 type *new_type_pointer           (ident *name, type *points_to) {
785   type *res;
786   res = new_type(type_pointer, mode_p, name);
787   res->attr.pa.points_to = points_to;
788   res->size = get_mode_size(res->mode);
789   res->state = layout_fixed;
790   return res;
791 }
792 inline void free_pointer_attrs (type *pointer) {
793   assert(pointer && (pointer->type_op == type_pointer));
794 }
795 /* manipulate fields of type_pointer */
796 void  set_pointer_points_to_type (type *pointer, type *type) {
797   assert(pointer && (pointer->type_op == type_pointer));
798   pointer->attr.pa.points_to = type;
799 }
800 type *get_pointer_points_to_type (type *pointer) {
801   assert(pointer && (pointer->type_op == type_pointer));
802   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
803 }
804
805 /* typecheck */
806 bool  is_pointer_type            (type *pointer) {
807   assert(pointer);
808   if (pointer->type_op == type_pointer) return 1; else return 0;
809 }
810
811
812 /*******************************************************************/
813 /** TYPE_PRIMITIVE                                                **/
814 /*******************************************************************/
815
816 /* create a new type primitive */
817 type *new_type_primitive (ident *name, ir_mode *mode) {
818   type *res;
819   /* @@@ assert( mode_is_data(mode) && (!mode == mode_p)); */
820   res = new_type(type_primitive, mode, name);
821   res->size = get_mode_size(mode);
822   res->state = layout_fixed;
823   return res;
824 }
825 inline void free_primitive_attrs (type *primitive) {
826   assert(primitive && (primitive->type_op == type_primitive));
827 }
828
829 /* typecheck */
830 bool  is_primitive_type  (type *primitive) {
831   assert(primitive && primitive->kind == k_type);
832   if (primitive->type_op == type_primitive) return 1; else return 0;
833 }
834
835 /*******************************************************************/
836 /** common functionality                                          **/
837 /*******************************************************************/
838
839
840 inline int is_atomic_type(type *tp) {
841   assert(tp && tp->kind == k_type);
842   return (is_primitive_type(tp) || is_pointer_type(tp) ||
843           is_enumeration_type(tp));
844 }
845 inline int is_compound_type(type *tp) {
846   assert(tp && tp->kind == k_type);
847   return (is_class_type(tp) || is_struct_type(tp) ||
848           is_array_type(tp) || is_union_type(tp));
849 }