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