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