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