replaced variable args macros by functions to make it c89 compatible
[libfirm] / ir / tr / type.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type.c
4  * Purpose:     Representation of types.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  *
15  *   file type.c - implementation of the datastructure to hold
16  *   type information.
17  *  (C) 2001 by Universitaet Karlsruhe
18  *  Goetz Lindenmaier
19  *
20  *  This module supplies a datastructure to represent all types
21  *  known in the compiled program.  This includes types specified
22  *  in the program as well as types defined by the language.  In the
23  *  view of the intermediate representation there is no difference
24  *  between these types.
25  *
26  *  There exist several kinds of types, arranged by the structure of
27  *  the type.  A type is described by a set of attributes.  Some of
28  *  these attributes are common to all types, others depend on the
29  *  kind of the type.
30  *
31  *  Types are different from the modes defined in irmode:  Types are
32  *  on the level of the programming language, modes at the level of
33  *  the target processor.
34  *
35  * @see  type_t.h type tpop
36  */
37
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42
43 # include <stdlib.h>
44 # include <stddef.h>
45 # include <string.h>
46 # include "type_t.h"
47 # include "tpop_t.h"
48 # include "irprog_t.h"
49 # include "typegmod.h"
50 # include "array.h"
51 # include "irprog.h"
52 # include "mangle.h"
53 # include "tv.h"
54 # include "ircons.h"
55
56 /*******************************************************************/
57 /** TYPE                                                          **/
58 /*******************************************************************/
59
60 type *none_type;    type *get_none_type(void)    { return none_type;    }
61 type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
62
63
64 #ifdef DEBUG_libfirm
65 /** Returns a new, unique number to number nodes or the like. */
66 int get_irp_new_node_nr(void);
67 #endif
68
69 /* Suffixes added to types used for pass-by-value representations. */
70 static ident *value_params_suffix = NULL;
71 static ident *value_ress_suffix = NULL;
72
73 void init_type(void) {
74   value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
75   value_ress_suffix   = new_id_from_str(VALUE_RESS_SUFFIX);
76
77   /* construct none and unknown type. */
78   none_type    = new_type(tpop_none,    mode_BAD, new_id_from_str("type_none"));
79   set_type_size  (none_type, 0);
80   set_type_state (none_type, layout_fixed);
81   remove_irp_type(none_type);
82   unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
83   set_type_size  (unknown_type, 0);
84   set_type_state (unknown_type, layout_fixed);
85   remove_irp_type(unknown_type);
86 }
87
88 unsigned long type_visited;
89 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
90 INLINE unsigned long get_master_type_visited() { return type_visited; }
91 INLINE void inc_master_type_visited() { type_visited++; }
92
93 void        free_type(type *tp) {
94   if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
95     return;
96   /* Remove from list of all types */
97   remove_irp_type(tp);
98   /* Free the attributes of the type. */
99   free_type_attrs(tp);
100   /* Free entities automatically allocated with the type */
101   if (is_array_type(tp))
102     free_entity(get_array_element_entity(tp));
103   /* And now the type itself... */
104   tp->kind = k_BAD;
105   free(tp);
106 }
107
108 INLINE type *
109 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
110   type *res;
111   int node_size ;
112
113   assert(type_op != type_id);
114   assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
115
116   node_size = offsetof (type, attr) +  type_op->attr_size;
117   res = (type *) xmalloc (node_size);
118   add_irp_type(res);   /* Remember the new type global. */
119
120   res->kind = k_type;
121   res->type_op = type_op;
122   res->mode = mode;
123   res->name = name;
124   res->state = layout_undefined;
125   res->size = -1;
126   res->visit = 0;
127   res -> link = NULL;
128 #ifdef DEBUG_libfirm
129   res->nr = get_irp_new_node_nr();
130 #endif
131
132   return res;
133 }
134
135 void free_type_attrs(type *tp) {
136   switch(get_type_tpop_code(tp)) {
137   case tpo_class:       { free_class_attrs(tp);       } break;
138   case tpo_struct:      { free_struct_attrs(tp);      } break;
139   case tpo_method:      { free_method_attrs(tp);      } break;
140   case tpo_union:       { free_union_attrs(tp);       } break;
141   case tpo_array:       { free_array_attrs(tp);       } break;
142   case tpo_enumeration: { free_enumeration_attrs(tp); } break;
143   case tpo_pointer:     { free_pointer_attrs(tp);     } break;
144   case tpo_primitive:   { free_primitive_attrs(tp);   } break;
145   default: break;
146   }
147 }
148
149 /* set/get the link field */
150 void *get_type_link(type *tp)
151 {
152   assert(tp && tp->kind == k_type);
153   return(tp -> link);
154 }
155
156 void set_type_link(type *tp, void *l)
157 {
158   assert(tp && tp->kind == k_type);
159   tp -> link = l;
160 }
161
162 tp_op*      get_type_tpop(type *tp) {
163   assert(tp && tp->kind == k_type);
164   return tp->type_op;
165 }
166
167 ident*      get_type_tpop_nameid(type *tp) {
168   assert(tp && tp->kind == k_type);
169   return tp->type_op->name;
170 }
171
172 const char* get_type_tpop_name(type *tp) {
173   assert(tp && tp->kind == k_type);
174   return get_id_str(tp->type_op->name);
175 }
176
177 tp_opcode    get_type_tpop_code(type *tp) {
178   assert(tp && tp->kind == k_type);
179   return tp->type_op->code;
180 }
181
182 ir_mode*    get_type_mode(type *tp) {
183   assert(tp && tp->kind == k_type);
184   return tp->mode;
185 }
186
187 void        set_type_mode(type *tp, ir_mode* m) {
188   assert(tp && tp->kind == k_type);
189
190   assert(((tp->type_op != type_primitive)   || mode_is_data(m))     &&
191          /* Modes of primitives must be data */
192          ((tp->type_op != type_enumeration) || mode_is_int(m))      &&
193          /* Modes of enumerations must be integers */
194          ((tp->type_op != type_pointer)     || mode_is_reference(m))   );
195          /* Modes of pointers must be references. */
196
197   if ((tp->type_op == type_primitive)   ||
198       (tp->type_op == type_enumeration) ||
199       (tp->type_op == type_pointer)       ) {
200     /* For pointer, primitive and enumeration size depends on the mode. */
201     assert((get_mode_size_bytes(m) != -1) && "unorthodox modes not implemented");
202     tp->size = get_mode_size_bytes(m);
203     tp->mode = m;
204   }
205 }
206
207 ident*      get_type_ident(type *tp) {
208   assert(tp && tp->kind == k_type);
209   return tp->name;
210 }
211
212 void        set_type_ident(type *tp, ident* id) {
213   assert(tp && tp->kind == k_type);
214   tp->name = id;
215 }
216
217 /* Outputs a unique number for this node */
218 INLINE long
219 get_type_nr(type *tp) {
220   assert(tp);
221 #ifdef DEBUG_libfirm
222   return tp->nr;
223 #else
224   return (long)tp;
225 #endif
226 }
227
228 const char* get_type_name(type *tp) {
229   assert(tp && tp->kind == k_type);
230   return (get_id_str(tp->name));
231 }
232
233 int         get_type_size(type *tp) {
234   assert(tp && tp->kind == k_type);
235   return tp->size;
236 }
237
238 void
239 set_type_size(type *tp, int size) {
240   assert(tp && tp->kind == k_type);
241   /* For pointer enumeration and primitive size depends on the mode.
242      Methods don't have a size. */
243   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
244       (tp->type_op != type_enumeration) && (tp->type_op != type_method))
245     tp->size = size;
246 }
247
248 type_state
249 get_type_state(type *tp) {
250   assert(tp && tp->kind == k_type);
251   return tp->state;
252 }
253
254 void
255 set_type_state(type *tp, type_state state) {
256   assert(tp && tp->kind == k_type);
257
258   if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
259       (tp->type_op == type_method))
260     return;
261
262   /* Just a correctness check: */
263   if (state == layout_fixed) {
264     int i;
265     switch (get_type_tpop_code(tp)) {
266     case tpo_class:
267       {
268         assert(get_type_size(tp) > -1);
269         if (tp != get_glob_type())
270           for (i = 0; i < get_class_n_members(tp); i++) {
271             if (get_entity_offset(get_class_member(tp, i)) <= -1)
272               { DDMT(tp); DDME(get_class_member(tp, i)); }
273             assert(get_entity_offset(get_class_member(tp, i)) > -1);
274             assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
275                    (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
276           }
277       } break;
278     case tpo_struct:
279       {
280         assert(get_type_size(tp) > -1);
281         for (i = 0; i < get_struct_n_members(tp); i++) {
282           assert(get_entity_offset(get_struct_member(tp, i)) > -1);
283           assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
284         }
285       } break;
286     case tpo_union:
287       { /* ?? */
288       } break;
289     case tpo_array:
290       { /* ??
291          Check order?
292          Assure that only innermost dimension is dynamic? */
293       } break;
294     case tpo_enumeration:
295       {
296         assert(get_type_mode != NULL);
297         for (i = 0; i < get_enumeration_n_enums(tp); i++)
298           assert(get_enumeration_enum(tp, i) != NULL);
299       } break;
300     default: break;
301     } /* switch (tp) */
302   }
303   tp->state = state;
304 }
305
306 unsigned long get_type_visited(type *tp) {
307   assert(tp && tp->kind == k_type);
308   return tp->visit;
309 }
310
311 void        set_type_visited(type *tp, unsigned long num) {
312   assert(tp && tp->kind == k_type);
313   tp->visit = num;
314 }
315 /* Sets visited field in type to type_visited. */
316 void        mark_type_visited(type *tp) {
317   assert(tp && tp->kind == k_type);
318   assert(tp->visit < type_visited);
319   tp->visit = type_visited;
320 }
321 /* @@@ name clash with master flag
322 bool          type_visited(type *tp) {
323   assert(tp && tp->kind == k_type);
324   return tp->visit >= type_visited;
325   }*/
326 bool          type_not_visited(type *tp) {
327   assert(tp && tp->kind == k_type);
328   return tp->visit  < type_visited;
329 }
330
331
332 int is_type            (void *thing) {
333   assert(thing);
334   if (get_kind(thing) == k_type)
335     return 1;
336   else
337     return 0;
338 }
339
340
341 bool equal_type(type *typ1, type *typ2) {
342   entity **m;
343   type **t;
344   int i, j;
345
346   if (typ1 == typ2) return true;
347
348   if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
349       (get_type_ident(typ1) != get_type_ident(typ2)) ||
350       (get_type_mode(typ1) != get_type_mode(typ2)) ||
351       (get_type_state(typ1) != get_type_state(typ2)))
352     return false;
353   if ((get_type_state(typ1) == layout_fixed) &&
354       (get_type_size(typ1) != get_type_size(typ2)))
355     return false;
356
357   switch(get_type_tpop_code(typ1)) {
358   case tpo_class:       {
359     if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
360     if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
361     if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
362     if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
363     /** Compare the members **/
364     m = alloca(sizeof(entity *) * get_class_n_members(typ1));
365     memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
366     /* First sort the members of typ2 */
367     for (i = 0; i < get_class_n_members(typ1); i++) {
368       entity *e1 = get_class_member(typ1, i);
369       for (j = 0; j < get_class_n_members(typ2); j++) {
370         entity *e2 = get_class_member(typ2, j);
371         if (get_entity_name(e1) == get_entity_name(e2))
372           m[i] = e2;
373       }
374     }
375     for (i = 0; i < get_class_n_members(typ1); i++) {
376       if (!m[i]  ||  /* Found no counterpart */
377           !equal_entity(get_class_member(typ1, i), m[i]))
378         return false;
379     }
380     /** Compare the supertypes **/
381     t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
382     memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
383     /* First sort the supertypes of typ2 */
384     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
385       type *t1 = get_class_supertype(typ1, i);
386       for (j = 0; j < get_class_n_supertypes(typ2); j++) {
387         type *t2 = get_class_supertype(typ2, j);
388         if (get_type_ident(t2) == get_type_ident(t1))
389           t[i] = t2;
390       }
391     }
392     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
393       if (!t[i]  ||  /* Found no counterpart */
394           get_class_supertype(typ1, i) != t[i])
395         return false;
396     }
397   } break;
398   case tpo_struct:      {
399     if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
400     m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
401     memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
402     /* First sort the members of lt */
403     for (i = 0; i < get_struct_n_members(typ1); i++) {
404       entity *e1 = get_struct_member(typ1, i);
405       for (j = 0; j < get_struct_n_members(typ2); j++) {
406         entity *e2 = get_struct_member(typ2, j);
407         if (get_entity_name(e1) == get_entity_name(e2))
408           m[i] = e2;
409       }
410     }
411     for (i = 0; i < get_struct_n_members(typ1); i++) {
412       if (!m[i]  ||  /* Found no counterpart */
413           !equal_entity(get_struct_member(typ1, i), m[i]))
414         return false;
415     }
416   } break;
417   case tpo_method:      {
418     if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
419     if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
420     if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
421     for (i = 0; i < get_method_n_params(typ1); i++) {
422       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
423         return false;
424     }
425     for (i = 0; i < get_method_n_ress(typ1); i++) {
426       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
427         return false;
428     }
429   } break;
430   case tpo_union:       {
431     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
432     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
433     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
434     /* First sort the members of lt */
435     for (i = 0; i < get_union_n_members(typ1); i++) {
436       entity *e1 = get_union_member(typ1, i);
437       for (j = 0; j < get_union_n_members(typ2); j++) {
438         entity *e2 = get_union_member(typ2, j);
439         if (get_entity_name(e1) == get_entity_name(e2))
440           m[i] = e2;
441       }
442     }
443     for (i = 0; i < get_union_n_members(typ1); i++) {
444       if (!m[i]  ||  /* Found no counterpart */
445           !equal_entity(get_union_member(typ1, i), m[i]))
446         return false;
447     }
448   } break;
449   case tpo_array:       {
450     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
451       return false;
452     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
453       return false;
454     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
455       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
456           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
457         return false;
458       if (get_array_order(typ1, i) != get_array_order(typ2, i))
459         assert(0 && "type compare with different dimension orders not implemented");
460     }
461   } break;
462   case tpo_enumeration: {
463     assert(0 && "enumerations not implemented");
464   } break;
465   case tpo_pointer:     {
466     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
467       return false;
468   } break;
469   case tpo_primitive:   {
470   } break;
471   default: break;
472   }
473   return true;
474 }
475
476 bool smaller_type (type *st, type *lt) {
477   entity **m;
478   int i, j;
479
480   if (st == lt) return true;
481
482   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
483     return false;
484
485   switch(get_type_tpop_code(st)) {
486   case tpo_class:       {
487     return is_subclass_of(st, lt);
488   } break;
489   case tpo_struct:      {
490     if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
491     m = alloca(sizeof(entity *) * get_struct_n_members(st));
492     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
493     /* First sort the members of lt */
494     for (i = 0; i < get_struct_n_members(st); i++) {
495       entity *se = get_struct_member(st, i);
496       for (j = 0; j < get_struct_n_members(lt); j++) {
497         entity *le = get_struct_member(lt, j);
498         if (get_entity_name(le) == get_entity_name(se))
499           m[i] = le;
500       }
501     }
502     for (i = 0; i < get_struct_n_members(st); i++) {
503       if (!m[i]  ||  /* Found no counterpart */
504           !smaller_type(get_entity_type(get_struct_member(st, i)),
505                         get_entity_type(m[i])))
506         return false;
507     }
508   } break;
509   case tpo_method:      {
510     if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
511     if (get_method_n_params(st) != get_method_n_params(lt)) return false;
512     if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
513     for (i = 0; i < get_method_n_params(st); i++) {
514       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
515         return false;
516     }
517     for (i = 0; i < get_method_n_ress(st); i++) {
518       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
519         return false;
520     }
521   } break;
522   case tpo_union:       {
523     if (get_union_n_members(st) != get_union_n_members(lt)) return false;
524     m = alloca(sizeof(entity *) * get_union_n_members(st));
525     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
526     /* First sort the members of lt */
527     for (i = 0; i < get_union_n_members(st); i++) {
528       entity *se = get_union_member(st, i);
529       for (j = 0; j < get_union_n_members(lt); j++) {
530         entity *le = get_union_member(lt, j);
531         if (get_entity_name(le) == get_entity_name(se))
532           m[i] = le;
533       }
534     }
535     for (i = 0; i < get_union_n_members(st); i++) {
536       if (!m[i]  ||  /* Found no counterpart */
537           !smaller_type(get_entity_type(get_union_member(st, i)),
538                         get_entity_type(m[i])))
539         return false;
540     }
541   } break;
542   case tpo_array:       {
543     type *set, *let;  /* small/large elt. type */
544     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
545       return false;
546     set = get_array_element_type(st);
547     let = get_array_element_type(lt);
548     if (set != let) {
549       /* If the elt types are different, set must be convertible
550          to let, and they must have the same size so that address
551          computations work out.  To have a size the layout must
552          be fixed. */
553       if ((get_type_state(set) != layout_fixed) ||
554           (get_type_state(let) != layout_fixed))
555         return false;
556       if (!smaller_type(set, let) ||
557           get_type_size(set) != get_type_size(let))
558         return false;
559     }
560     for(i = 0; i < get_array_n_dimensions(st); i++) {
561       if (get_array_lower_bound(lt, i))
562         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
563           return false;
564       if (get_array_upper_bound(lt, i))
565         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
566           return false;
567     }
568   } break;
569   case tpo_enumeration: {
570     assert(0 && "enumerations not implemented");
571   } break;
572   case tpo_pointer:     {
573     if (!smaller_type(get_pointer_points_to_type(st),
574                       get_pointer_points_to_type(lt)))
575       return false;
576   } break;
577   case tpo_primitive:   {
578     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
579       return false;
580   } break;
581   default: break;
582   }
583   return true;
584 }
585
586 /*******************************************************************/
587 /** TYPE_CLASS                                                    **/
588 /*******************************************************************/
589
590 /* create a new class type */
591 INLINE type   *new_type_class (ident *name) {
592   type *res;
593
594   res = new_type(type_class, NULL, name);
595
596   res->attr.ca.members     = NEW_ARR_F (entity *, 1);
597   res->attr.ca.subtypes    = NEW_ARR_F (type *, 1);
598   res->attr.ca.supertypes  = NEW_ARR_F (type *, 1);
599   res->attr.ca.peculiarity = peculiarity_existent;
600   res->attr.ca.dfn         = 0;
601
602   return res;
603 }
604 type   *new_d_type_class (ident *name, dbg_info* db) {
605   type *res = new_type_class (name);
606   set_type_dbg_info(res, db);
607   return res;
608 }
609 INLINE void free_class_attrs(type *clss) {
610   assert(clss && (clss->type_op == type_class));
611   DEL_ARR_F(clss->attr.ca.members);
612   DEL_ARR_F(clss->attr.ca.subtypes);
613   DEL_ARR_F(clss->attr.ca.supertypes);
614 }
615
616 /* manipulate private fields of class type  */
617 void    add_class_member   (type *clss, entity *member) {
618   assert(clss && (clss->type_op == type_class));
619   ARR_APP1 (entity *, clss->attr.ca.members, member);
620 }
621 int     get_class_n_members (type *clss) {
622   assert(clss && (clss->type_op == type_class));
623   return (ARR_LEN (clss->attr.ca.members))-1;
624 }
625 int     get_class_member_index(type *clss, entity *mem) {
626   int i;
627   assert(clss && (clss->type_op == type_class));
628   for (i = 0; i < get_class_n_members(clss); i++)
629     if (get_class_member(clss, i) == mem)
630       return i;
631   return -1;
632 }
633 entity *get_class_member   (type *clss, int pos) {
634   assert(clss && (clss->type_op == type_class));
635   assert(pos >= 0 && pos < get_class_n_members(clss));
636   return clss->attr.ca.members[pos+1];
637 }
638 void    set_class_member   (type *clss, entity *member, int pos) {
639   assert(clss && (clss->type_op == type_class));
640   assert(pos >= 0 && pos < get_class_n_members(clss));
641   clss->attr.ca.members[pos+1] = member;
642 }
643 void    set_class_members  (type *clss, entity **members, int arity) {
644   int i;
645   assert(clss && (clss->type_op == type_class));
646   DEL_ARR_F(clss->attr.ca.members);
647   clss->attr.ca.members    = NEW_ARR_F (entity *, 1);
648   for (i = 0; i < arity; i++) {
649     set_entity_owner(members[i], clss);
650     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
651   }
652 }
653 void    remove_class_member(type *clss, entity *member) {
654   int i;
655   assert(clss && (clss->type_op == type_class));
656   for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
657     if (clss->attr.ca.members[i] == member) {
658       for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
659         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
660       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
661       break;
662     }
663   }
664 }
665
666 void    add_class_subtype   (type *clss, type *subtype) {
667   int i;
668   assert(clss && (clss->type_op == type_class));
669   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
670   for (i = 0; i < get_class_n_supertypes(subtype); i++)
671     if (get_class_supertype(subtype, i) == clss)
672       /* Class already registered */
673       return;
674   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
675 }
676 int     get_class_n_subtypes (type *clss) {
677   assert(clss && (clss->type_op == type_class));
678   return (ARR_LEN (clss->attr.ca.subtypes))-1;
679 }
680 type   *get_class_subtype   (type *clss, int pos) {
681   assert(clss && (clss->type_op == type_class));
682   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
683   return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
684 }
685 void    set_class_subtype   (type *clss, type *subtype, int pos) {
686   assert(clss && (clss->type_op == type_class));
687   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
688   clss->attr.ca.subtypes[pos+1] = subtype;
689 }
690 void    remove_class_subtype(type *clss, type *subtype) {
691   int i;
692   assert(clss && (clss->type_op == type_class));
693   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
694     if (clss->attr.ca.subtypes[i] == subtype) {
695       for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
696         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
697       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
698       break;
699     }
700 }
701
702 void    add_class_supertype   (type *clss, type *supertype) {
703   int i;
704   assert(clss && (clss->type_op == type_class));
705   assert(supertype && (supertype -> type_op == type_class));
706   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
707   for (i = 0; i < get_class_n_subtypes(supertype); i++)
708     if (get_class_subtype(supertype, i) == clss)
709       /* Class already registered */
710       return;
711   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
712 }
713 int     get_class_n_supertypes (type *clss) {
714   assert(clss && (clss->type_op == type_class));
715   return (ARR_LEN (clss->attr.ca.supertypes))-1;
716 }
717 int get_class_supertype_index(type *clss, type *super_clss) {
718   int i;
719   assert(clss && (clss->type_op == type_class));
720   assert(super_clss && (super_clss->type_op == type_class));
721   for (i = 0; i < get_class_n_supertypes(clss); i++)
722     if (get_class_supertype(clss, i) == super_clss)
723       return i;
724   return -1;
725 }
726 type   *get_class_supertype   (type *clss, int pos) {
727   assert(clss && (clss->type_op == type_class));
728   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
729   return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
730 }
731 void    set_class_supertype   (type *clss, type *supertype, int pos) {
732   assert(clss && (clss->type_op == type_class));
733   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
734   clss->attr.ca.supertypes[pos+1] = supertype;
735 }
736 void    remove_class_supertype(type *clss, type *supertype) {
737   int i;
738   assert(clss && (clss->type_op == type_class));
739   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
740     if (clss->attr.ca.supertypes[i] == supertype) {
741       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
742         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
743       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
744       break;
745     }
746 }
747
748 char *get_peculiarity_string(peculiarity p) {
749   if (p == peculiarity_description)
750     return "peculiarity_description";
751   if (p == peculiarity_inherited)
752     return "peculiarity_inherited";
753   return "peculiarity_existent";
754 }
755
756 INLINE peculiarity get_class_peculiarity (type *clss) {
757   assert(clss && (clss->type_op == type_class));
758   return clss->attr.ca.peculiarity;
759 }
760 INLINE void        set_class_peculiarity (type *clss, peculiarity pec) {
761   assert(clss && (clss->type_op == type_class));
762   assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
763   clss->attr.ca.peculiarity = pec;
764 }
765
766 void set_class_dfn (type *clss, int dfn)
767 {
768   clss->attr.ca.dfn        = dfn;
769 }
770
771 int get_class_dfn (type *clss)
772 {
773   return (clss->attr.ca.dfn);
774 }
775
776 /* typecheck */
777 bool    is_class_type(type *clss) {
778   assert(clss);
779   if (clss->type_op == type_class) return 1; else return 0;
780 }
781
782 bool is_subclass_of(type *low, type *high) {
783   int i;
784   assert(is_class_type(low) && is_class_type(high));
785   if (low == high) return true;
786   /* depth first search from high downwards. */
787   for (i = 0; i < get_class_n_subtypes(high); i++) {
788     if (low == get_class_subtype(high, i))
789       return true;
790     if (is_subclass_of(low, get_class_subtype(high, i)))
791       return true;
792   }
793   return false;
794 }
795
796 /*******************************************************************/
797 /** TYPE_STRUCT                                                   **/
798 /*******************************************************************/
799
800 /* create a new type struct */
801 INLINE type   *new_type_struct (ident *name) {
802   type *res;
803   res = new_type(type_struct, NULL, name);
804   res->attr.sa.members = NEW_ARR_F (entity *, 1);
805   return res;
806 }
807 type   *new_d_type_struct (ident *name, dbg_info* db) {
808   type *res = new_type_struct (name);
809   set_type_dbg_info(res, db);
810   return res;
811 }
812 INLINE void free_struct_attrs (type *strct) {
813   assert(strct && (strct->type_op == type_struct));
814   DEL_ARR_F(strct->attr.sa.members);
815 }
816
817 /* manipulate private fields of struct */
818 int     get_struct_n_members (type *strct) {
819   assert(strct && (strct->type_op == type_struct));
820   return (ARR_LEN (strct->attr.sa.members))-1;
821 }
822 void    add_struct_member   (type *strct, entity *member) {
823   assert(strct && (strct->type_op == type_struct));
824   assert(get_type_tpop(get_entity_type(member)) != type_method);
825     /*    @@@ lowerfirm geht nicht durch */
826   ARR_APP1 (entity *, strct->attr.sa.members, member);
827 }
828 entity *get_struct_member   (type *strct, int pos) {
829   assert(strct && (strct->type_op == type_struct));
830   assert(pos >= 0 && pos < get_struct_n_members(strct));
831   return strct->attr.sa.members[pos+1];
832 }
833 void    set_struct_member   (type *strct, int pos, entity *member) {
834   assert(strct && (strct->type_op == type_struct));
835   assert(pos >= 0 && pos < get_struct_n_members(strct));
836   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
837   strct->attr.sa.members[pos+1] = member;
838 }
839 void    remove_struct_member(type *strct, entity *member) {
840   int i;
841   assert(strct && (strct->type_op == type_struct));
842   for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
843     if (strct->attr.sa.members[i] == member) {
844       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
845         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
846       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
847       break;
848     }
849 }
850
851 /* typecheck */
852 bool    is_struct_type(type *strct) {
853   assert(strct);
854   if (strct->type_op == type_struct) return 1; else return 0;
855 }
856
857 /*******************************************************************/
858 /** TYPE_METHOD                                                   **/
859 /*******************************************************************/
860
861 /* Lazy construction of value argument / result representation. */
862 static INLINE type *
863 build_value_type(ident *name, int len, type **tps) {
864   int i;
865   type *res = new_type_struct(name);
866   /* Remove type from type list.  Must be treated differently than other types. */
867   remove_irp_type_from_list(res);
868   for (i = 0; i < len; i++) {
869     type *elt_type = res;   /* use res as default if corresponding type is not yet set. */
870     if (tps[i]) elt_type = tps[i];
871     new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
872   }
873   return res;
874 }
875
876 /* Create a new method type.
877    N_param is the number of parameters, n_res the number of results.  */
878 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
879   type *res;
880   res = new_type(type_method, mode_P_mach, name);
881   res->state = layout_fixed;
882   assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
883   res->size = get_mode_size_bytes(mode_P_mach);
884   res->attr.ma.n_params     = n_param;
885   res->attr.ma.param_type   = (type **) xmalloc (sizeof (type *) * n_param);
886   res->attr.ma.value_params = NULL;
887   res->attr.ma.n_res        = n_res;
888   res->attr.ma.res_type     = (type **) xmalloc (sizeof (type *) * n_res);
889   res->attr.ma.value_ress   = NULL;
890   res->attr.ma.variadicity  = variadicity_non_variadic;
891
892   return res;
893 }
894
895 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
896   type *res = new_type_method (name, n_param, n_res);
897   set_type_dbg_info(res, db);
898   return res;
899 }
900
901 INLINE void free_method_attrs(type *method) {
902   assert(method && (method->type_op == type_method));
903   free(method->attr.ma.param_type);
904   free(method->attr.ma.res_type);
905 }
906
907 /* manipulate private fields of method. */
908 int   get_method_n_params  (type *method) {
909   assert(method && (method->type_op == type_method));
910   return method->attr.ma.n_params;
911 }
912 type *get_method_param_type(type *method, int pos) {
913   assert(method && (method->type_op == type_method));
914   assert(pos >= 0 && pos < get_method_n_params(method));
915   return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
916 }
917 void  set_method_param_type(type *method, int pos, type* tp) {
918   assert(method && (method->type_op == type_method));
919   assert(pos >= 0 && pos < get_method_n_params(method));
920   method->attr.ma.param_type[pos] = tp;
921   /* If information constructed set pass-by-value representation. */
922   if (method->attr.ma.value_params) {
923     assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
924     set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
925   }
926 }
927 /* Returns an entity that represents the copied value argument.  Only necessary
928    for compounds passed by value. */
929 entity *get_method_value_param_ent(type *method, int pos) {
930   assert(method && (method->type_op == type_method));
931   assert(pos >= 0 && pos < get_method_n_params(method));
932   if (!method->attr.ma.value_params)
933     method->attr.ma.value_params
934       = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
935                          get_method_n_params(method), method->attr.ma.param_type);
936   assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
937           != method->attr.ma.value_params)
938          && "param type not yet set");
939   return get_struct_member(method->attr.ma.value_params, pos);
940 }
941
942 type *get_method_value_res_type(type *method) {
943   assert(method && (method->type_op == type_method));
944   return method->attr.ma.value_params;
945 }
946
947
948 int   get_method_n_ress   (type *method) {
949   assert(method && (method->type_op == type_method));
950   return method->attr.ma.n_res;
951 }
952 type *get_method_res_type(type *method, int pos) {
953   assert(method && (method->type_op == type_method));
954   assert(pos >= 0 && pos < get_method_n_ress(method));
955   return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
956 }
957 void  set_method_res_type(type *method, int pos, type* tp) {
958   assert(method && (method->type_op == type_method));
959   assert(pos >= 0 && pos < get_method_n_ress(method));
960   /* set the result type */
961   method->attr.ma.res_type[pos] = tp;
962   /* If information constructed set pass-by-value representation. */
963   if (method->attr.ma.value_ress) {
964     assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
965     set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
966   }
967 }
968 /* Returns an entity that represents the copied value result.  Only necessary
969    for compounds passed by value. */
970 entity *get_method_value_res_ent(type *method, int pos) {
971   assert(method && (method->type_op == type_method));
972   assert(pos >= 0 && pos < get_method_n_ress(method));
973   if (!method->attr.ma.value_ress)
974     method->attr.ma.value_ress
975       = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
976                          get_method_n_ress(method), method->attr.ma.res_type);
977   assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
978          && "result type not yet set");
979   return get_struct_member(method->attr.ma.value_ress, pos);
980 }
981
982 /* Returns the null-terminated name of this variadicity. */
983 const char *get_variadicity_name(variadicity vari)
984 {
985 #define X(a)    case a: return #a
986   switch (vari) {
987     X(variadicity_non_variadic);
988     X(variadicity_variadic);
989     default:
990       return "BAD VALUE";
991   }
992 #undef X
993 }
994
995 variadicity get_method_variadicity(type *method)
996 {
997   assert(method && (method->type_op == type_method));
998   return method->attr.ma.variadicity;
999 }
1000
1001 void set_method_variadicity(type *method, variadicity vari)
1002 {
1003   assert(method && (method->type_op == type_method));
1004   method->attr.ma.variadicity = vari;
1005 }
1006
1007 /* typecheck */
1008 bool  is_method_type     (type *method) {
1009   assert(method);
1010   if (method->type_op == type_method) return 1; else return 0;
1011 }
1012
1013 /*******************************************************************/
1014 /** TYPE_UNION                                                    **/
1015 /*******************************************************************/
1016
1017 /* create a new type uni */
1018 INLINE type  *new_type_union (ident *name) {
1019   type *res;
1020   res = new_type(type_union, NULL, name);
1021   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
1022     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
1023   res->attr.ua.members = NEW_ARR_F (entity *, 1);
1024   return res;
1025 }
1026 type  *new_d_type_union (ident *name, dbg_info* db) {
1027   type *res = new_type_union (name);
1028   set_type_dbg_info(res, db);
1029   return res;
1030 }
1031 INLINE void free_union_attrs (type *uni) {
1032   assert(uni && (uni->type_op == type_union));
1033   DEL_ARR_F(uni->attr.ua.members);
1034 }
1035 /* manipulate private fields of union */
1036 #if 0
1037 int    get_union_n_types      (type *uni) {
1038   assert(uni && (uni->type_op == type_union));
1039   return uni->attr.ua.n_types;
1040 }
1041 type  *get_union_unioned_type (type *uni, int pos) {
1042   assert(uni && (uni->type_op == type_union));
1043   assert(pos >= 0 && pos < get_union_n_types(uni));
1044   return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1045 }
1046 void   set_union_unioned_type (type *uni, int pos, type *tp) {
1047   assert(uni && (uni->type_op == type_union));
1048   assert(pos >= 0 && pos < get_union_n_types(uni));
1049   uni->attr.ua.unioned_type[pos] = tp;
1050 }
1051 ident *get_union_delim_nameid (type *uni, int pos) {
1052   assert(uni && (uni->type_op == type_union));
1053   assert(pos >= 0 && pos < get_union_n_types(uni));
1054   return uni->attr.ua.delim_names[pos];
1055 }
1056 const char *get_union_delim_name (type *uni, int pos) {
1057   assert(uni && (uni->type_op == type_union));
1058   assert(pos >= 0 && pos < get_union_n_types(uni));
1059   return get_id_str(uni->attr.ua.delim_names[pos]);
1060 }
1061 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
1062   assert(uni && (uni->type_op == type_union));
1063   assert(pos >= 0 && pos < get_union_n_types(uni));
1064   uni->attr.ua.delim_names[pos] = id;
1065 }
1066 #endif
1067 int    get_union_n_members      (type *uni) {
1068   assert(uni && (uni->type_op == type_union));
1069   return (ARR_LEN (uni->attr.ua.members))-1;
1070 }
1071 void    add_union_member   (type *uni, entity *member) {
1072   assert(uni && (uni->type_op == type_union));
1073   ARR_APP1 (entity *, uni->attr.ua.members, member);
1074 }
1075 entity  *get_union_member (type *uni, int pos) {
1076   assert(uni && (uni->type_op == type_union));
1077   assert(pos >= 0 && pos < get_union_n_members(uni));
1078   return uni->attr.ua.members[pos+1];
1079 }
1080 void   set_union_member (type *uni, int pos, entity *member) {
1081   assert(uni && (uni->type_op == type_union));
1082   assert(pos >= 0 && pos < get_union_n_members(uni));
1083   uni->attr.ua.members[pos+1] = member;
1084 }
1085 void   remove_union_member(type *uni, entity *member) {
1086   int i;
1087   assert(uni && (uni->type_op == type_union));
1088   for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
1089     if (uni->attr.ua.members[i] == member) {
1090       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1091         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1092       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1093       break;
1094     }
1095 }
1096
1097 /* typecheck */
1098 bool   is_union_type         (type *uni) {
1099   assert(uni);
1100   if (uni->type_op == type_union) return 1; else return 0;
1101 }
1102
1103 /*******************************************************************/
1104 /** TYPE_ARRAY                                                    **/
1105 /*******************************************************************/
1106
1107
1108 /* create a new type array -- set dimension sizes independently */
1109 INLINE type *new_type_array         (ident *name, int n_dimensions,
1110                               type *element_type) {
1111   type *res;
1112   int i;
1113   ir_graph *rem = current_ir_graph;
1114   assert(!is_method_type(element_type));
1115
1116   res = new_type(type_array, NULL, name);
1117   res->attr.aa.n_dimensions = n_dimensions;
1118   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1119   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1120   res->attr.aa.order  = (int *) xmalloc (sizeof (int) * n_dimensions);
1121
1122   current_ir_graph = get_const_code_irg();
1123   for (i = 0; i < n_dimensions; i++) {
1124     res->attr.aa.lower_bound[i]  = new_Unknown();
1125     res->attr.aa.upper_bound[i]  = new_Unknown();
1126     res->attr.aa.order[i] = i;
1127   }
1128   current_ir_graph = rem;
1129
1130   res->attr.aa.element_type = element_type;
1131   new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
1132
1133   return res;
1134 }
1135 type *new_d_type_array (ident *name, int n_dimensions,
1136                         type *element_type, dbg_info* db) {
1137   type *res = new_type_array (name, n_dimensions, element_type);
1138   set_type_dbg_info(res, db);
1139   return res;
1140 }
1141
1142 INLINE void free_array_attrs (type *array) {
1143   assert(array && (array->type_op == type_array));
1144   free(array->attr.aa.lower_bound);
1145   free(array->attr.aa.upper_bound);
1146 }
1147
1148 /* manipulate private fields of array type */
1149 int   get_array_n_dimensions (type *array) {
1150   assert(array && (array->type_op == type_array));
1151   return array->attr.aa.n_dimensions;
1152 }
1153
1154 INLINE void
1155 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1156                   ir_node * upper_bound) {
1157   assert(array && (array->type_op == type_array));
1158   assert(lower_bound && "lower_bound node may not be NULL.");
1159   assert(upper_bound && "upper_bound node may not be NULL.");
1160   assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1161   array->attr.aa.lower_bound[dimension] = lower_bound;
1162   array->attr.aa.upper_bound[dimension] = upper_bound;
1163 }
1164 void
1165 set_array_bounds_int (type *array, int dimension, int lower_bound,
1166                       int upper_bound) {
1167   ir_graph *rem = current_ir_graph;
1168   current_ir_graph = get_const_code_irg();
1169   set_array_bounds (array, dimension,
1170                     new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1171                     new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1172   current_ir_graph = rem;
1173 }
1174 INLINE void
1175 set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
1176   assert(array && (array->type_op == type_array));
1177   assert(lower_bound && "lower_bound node may not be NULL.");
1178   array->attr.aa.lower_bound[dimension] = lower_bound;
1179 }
1180 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1181   ir_graph *rem = current_ir_graph;
1182   current_ir_graph = get_const_code_irg();
1183   set_array_lower_bound  (array, dimension,
1184                           new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1185   current_ir_graph = rem;
1186 }
1187 INLINE void
1188 set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
1189   assert(array && (array->type_op == type_array));
1190   assert(upper_bound && "upper_bound node may not be NULL.");
1191   array->attr.aa.upper_bound[dimension] = upper_bound;
1192 }
1193 void  set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1194   ir_graph *rem = current_ir_graph;
1195   current_ir_graph = get_const_code_irg();
1196   set_array_upper_bound  (array, dimension,
1197                           new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1198   current_ir_graph = rem;
1199 }
1200 int       has_array_lower_bound  (type *array, int dimension) {
1201   assert(array && (array->type_op == type_array));
1202   return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1203 }
1204 ir_node * get_array_lower_bound  (type *array, int dimension) {
1205   assert(array && (array->type_op == type_array));
1206   return array->attr.aa.lower_bound[dimension];
1207 }
1208 int       has_array_upper_bound  (type *array, int dimension) {
1209   assert(array && (array->type_op == type_array));
1210   return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1211 }
1212 ir_node * get_array_upper_bound  (type *array, int dimension) {
1213   assert(array && (array->type_op == type_array));
1214   return array->attr.aa.upper_bound[dimension];
1215 }
1216
1217 void set_array_order (type *array, int dimension, int order) {
1218   assert(array && (array->type_op == type_array));
1219   array->attr.aa.order[dimension] = order;
1220 }
1221 int  get_array_order (type *array, int dimension) {
1222   assert(array && (array->type_op == type_array));
1223   return array->attr.aa.order[dimension];
1224 }
1225
1226 void  set_array_element_type (type *array, type *tp) {
1227   assert(array && (array->type_op == type_array));
1228   assert(!is_method_type(tp));
1229   array->attr.aa.element_type = tp;
1230 }
1231 type *get_array_element_type (type *array) {
1232   assert(array && (array->type_op == type_array));
1233   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1234 }
1235
1236 void  set_array_element_entity (type *array, entity *ent) {
1237   assert(array && (array->type_op == type_array));
1238   assert((get_entity_type(ent)->type_op != type_method));
1239   array->attr.aa.element_ent = ent;
1240   array->attr.aa.element_type = get_entity_type(ent);
1241 }
1242 entity *get_array_element_entity (type *array) {
1243   assert(array && (array->type_op == type_array));
1244   return array->attr.aa.element_ent;
1245 }
1246
1247 /* typecheck */
1248 bool   is_array_type         (type *array) {
1249   assert(array);
1250   if (array->type_op == type_array) return 1; else return 0;
1251 }
1252
1253 /*******************************************************************/
1254 /** TYPE_ENUMERATION                                              **/
1255 /*******************************************************************/
1256
1257 /* create a new type enumeration -- set the enumerators independently */
1258 INLINE type   *new_type_enumeration    (ident *name, int n_enums) {
1259   type *res;
1260   int i;
1261   res = new_type(type_enumeration, NULL, name);
1262   res->attr.ea.n_enums     = n_enums;
1263   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1264   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
1265   for (i = 0; i < n_enums; i++) {
1266     res->attr.ea.enumer[i] = NULL;
1267     res->attr.ea.enum_nameid  = NULL;
1268   }
1269   return res;
1270 }
1271 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db) {
1272   type *res = new_type_enumeration (name, n_enums);
1273   set_type_dbg_info(res, db);
1274   return res;
1275 }
1276
1277 INLINE void free_enumeration_attrs(type *enumeration) {
1278   assert(enumeration && (enumeration->type_op == type_enumeration));
1279   free(enumeration->attr.ea.enumer);
1280   free(enumeration->attr.ea.enum_nameid);
1281 }
1282
1283 /* manipulate fields of enumeration type. */
1284 int     get_enumeration_n_enums (type *enumeration) {
1285   assert(enumeration && (enumeration->type_op == type_enumeration));
1286   return enumeration->attr.ea.n_enums;
1287 }
1288 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
1289   assert(enumeration && (enumeration->type_op == type_enumeration));
1290   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1291   enumeration->attr.ea.enumer[pos] = con;
1292 }
1293 tarval *get_enumeration_enum    (type *enumeration, int pos) {
1294   assert(enumeration && (enumeration->type_op == type_enumeration));
1295   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1296   return enumeration->attr.ea.enumer[pos];
1297 }
1298 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
1299   assert(enumeration && (enumeration->type_op == type_enumeration));
1300   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1301   enumeration->attr.ea.enum_nameid[pos] = id;
1302 }
1303 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
1304   assert(enumeration && (enumeration->type_op == type_enumeration));
1305   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1306   return enumeration->attr.ea.enum_nameid[pos];
1307 }
1308 const char *get_enumeration_name(type *enumeration, int pos) {
1309   assert(enumeration && (enumeration->type_op == type_enumeration));
1310   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1311   return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1312 }
1313
1314 /* typecheck */
1315 bool    is_enumeration_type     (type *enumeration) {
1316   assert(enumeration);
1317   if (enumeration->type_op == type_enumeration) return 1; else return 0;
1318 }
1319
1320 /*******************************************************************/
1321 /** TYPE_POINTER                                                  **/
1322 /*******************************************************************/
1323
1324 /* Create a new type pointer */
1325 INLINE type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1326   type *res;
1327   assert(mode_is_reference(ptr_mode));
1328   res = new_type(type_pointer, ptr_mode, name);
1329   res->attr.pa.points_to = points_to;
1330   assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1331   res->size = get_mode_size_bytes(res->mode);
1332   res->state = layout_fixed;
1333   return res;
1334 }
1335 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1336   type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1337   set_type_dbg_info(res, db);
1338   return res;
1339 }
1340 INLINE void free_pointer_attrs (type *pointer) {
1341   assert(pointer && (pointer->type_op == type_pointer));
1342 }
1343 /* manipulate fields of type_pointer */
1344 void  set_pointer_points_to_type (type *pointer, type *tp) {
1345   assert(pointer && (pointer->type_op == type_pointer));
1346   pointer->attr.pa.points_to = tp;
1347 }
1348 type *get_pointer_points_to_type (type *pointer) {
1349   assert(pointer && (pointer->type_op == type_pointer));
1350   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1351 }
1352
1353 /* typecheck */
1354 bool  is_pointer_type            (type *pointer) {
1355   assert(pointer);
1356   if (pointer->type_op == type_pointer) return 1; else return 0;
1357 }
1358
1359 /* Returns the first pointer type that has as points_to tp.
1360  *  Not efficient: O(#types).
1361  *  If not found returns unknown_type. */
1362 type *find_pointer_type_to_type (type *tp) {
1363   int i;
1364   for (i = 0; i < get_irp_n_types(); ++i) {
1365     type *found = get_irp_type(i);
1366     if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
1367       return (found);
1368   }
1369   return unknown_type;
1370 }
1371
1372
1373
1374 /*******************************************************************/
1375 /** TYPE_PRIMITIVE                                                **/
1376 /*******************************************************************/
1377
1378 /* create a new type primitive */
1379 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1380   type *res;
1381   /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1382   res = new_type(type_primitive, mode, name);
1383   assert((get_mode_size_bytes(mode) != -1) && "unorthodox modes not implemented");
1384   res->size = get_mode_size_bytes(mode);
1385   res->state = layout_fixed;
1386   return res;
1387 }
1388 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1389   type *res = new_type_primitive (name, mode);
1390   set_type_dbg_info(res, db);
1391   return res;
1392 }
1393 INLINE void free_primitive_attrs (type *primitive) {
1394   assert(primitive && (primitive->type_op == type_primitive));
1395 }
1396
1397 /* typecheck */
1398 bool  is_primitive_type  (type *primitive) {
1399   assert(primitive && primitive->kind == k_type);
1400   if (primitive->type_op == type_primitive) return 1; else return 0;
1401 }
1402
1403 /*******************************************************************/
1404 /** common functionality                                          **/
1405 /*******************************************************************/
1406
1407
1408 INLINE int is_atomic_type(type *tp) {
1409   assert(tp && tp->kind == k_type);
1410   return (is_primitive_type(tp) || is_pointer_type(tp) ||
1411           is_enumeration_type(tp));
1412 }
1413
1414 /*
1415  * Gets the number of elements in a firm compound type.
1416  */
1417 int get_compound_n_members(type *tp)
1418 {
1419   int res = 0;
1420
1421   if (is_struct_type(tp))
1422     res = get_struct_n_members(tp);
1423   else if (is_class_type(tp))
1424     res = get_class_n_members(tp);
1425   else if (is_union_type(tp))
1426     res = get_union_n_members(tp);
1427   else
1428     assert(0 && "need struct, union or class for member count");
1429
1430   return res;
1431 }
1432
1433 /*
1434  * Gets the member of a firm compound type at position pos.
1435  */
1436 entity *get_compound_member(type *tp, int pos)
1437 {
1438   entity *res;
1439
1440   if (is_struct_type(tp))
1441     res = get_struct_member(tp, pos);
1442   else if (is_class_type(tp))
1443     res = get_class_member(tp, pos);
1444   else if (is_union_type(tp))
1445     res = get_union_member(tp, pos);
1446   else
1447   {
1448     assert(0 && "need struct, union or class to get a member");
1449     res=NULL;
1450   }
1451
1452   return res;
1453 }
1454
1455
1456 INLINE int is_compound_type(type *tp) {
1457   assert(tp && tp->kind == k_type);
1458   return (is_class_type(tp) || is_struct_type(tp) ||
1459           is_array_type(tp) || is_union_type(tp));
1460 }