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