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