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