5ceb3934afe13f69c4e1a15aff9af9b8615827b0
[libfirm] / ir / tr / type.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type.c
4  * Purpose:     Representation of types.
5  * Author:      Goetz Lindenmaier
6  * Modified by: Michael Beck
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  *
15  *  @file type.c
16  *
17  *  Implementation of the datastructure to hold
18  *  type information.
19  *
20  *  (C) 2001-2006 by Universitaet Karlsruhe
21  *  Goetz Lindenmaier, Michael Beck
22  *
23  *  This module supplies a datastructure to represent all types
24  *  known in the compiled program.  This includes types specified
25  *  in the program as well as types defined by the language.  In the
26  *  view of the intermediate representation there is no difference
27  *  between these types.
28  *
29  *  There exist several kinds of types, arranged by the structure of
30  *  the type.  A type is described by a set of attributes.  Some of
31  *  these attributes are common to all types, others depend on the
32  *  kind of the type.
33  *
34  *  Types are different from the modes defined in irmode:  Types are
35  *  on the level of the programming language, modes at the level of
36  *  the target processor.
37  *
38  * @see  type_t.h type tpop
39  */
40
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #ifdef HAVE_ALLOCA_H
46 #include <alloca.h>
47 #endif
48 #ifdef HAVE_MALLOC_H
49 #include <malloc.h>
50 #endif
51 #ifdef HAVE_STRING_H
52 # include <string.h>
53 #endif
54 #ifdef HAVE_STDLIB_H
55 # include <stdlib.h>
56 #endif
57
58 # include <stddef.h>
59
60 # include "type_t.h"
61
62 # include "xmalloc.h"
63 # include "irprog_t.h"
64 # include "ircons.h"
65 # include "tpop_t.h"
66 # include "typegmod.h"
67 # include "mangle.h"
68 # include "tv_t.h"
69 # include "irhooks.h"
70
71 # include "array.h"
72
73 /*-----------------------------------------------------------------*/
74 /** TYPE                                                          **/
75 /*-----------------------------------------------------------------*/
76
77 ir_type *firm_none_type;    ir_type *get_none_type(void)    { return firm_none_type;    }
78 ir_type *firm_unknown_type; ir_type *get_unknown_type(void) { return firm_unknown_type; }
79
80
81 #ifdef DEBUG_libfirm
82 /* Returns a new, unique number to number nodes or the like. */
83 int get_irp_new_node_nr(void);
84 #endif
85
86 /* Suffixes added to types used for pass-by-value representations. */
87 static ident *value_params_suffix = NULL;
88 static ident *value_ress_suffix = NULL;
89
90 /** The default calling convention for method types. */
91 static unsigned default_cc_mask;
92
93 /* return the default calling convention for method types */
94 unsigned get_default_cc_mask(void) {
95   return default_cc_mask;
96 }
97
98 /* Initialize the type module. */
99 void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask)
100 {
101   default_cc_mask     = def_cc_mask;
102   value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
103   value_ress_suffix   = new_id_from_str(VALUE_RESS_SUFFIX);
104
105   /* construct none and unknown type. */
106   firm_none_type    = new_type(tpop_none,    mode_BAD, new_id_from_str("type_none"), builtin_db);
107   set_type_size_bits(firm_none_type, 0);
108   set_type_state (firm_none_type, layout_fixed);
109   remove_irp_type(firm_none_type);
110
111   firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
112   set_type_size_bits(firm_unknown_type, 0);
113   set_type_state (firm_unknown_type, layout_fixed);
114   remove_irp_type(firm_unknown_type);
115 }
116
117 /** the global type visited flag */
118 unsigned long firm_type_visited;
119
120 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
121 unsigned long (get_master_type_visited)(void)     { return _get_master_type_visited(); }
122 void (inc_master_type_visited)(void)              { _inc_master_type_visited(); }
123
124 /*
125  * Creates a new type representation.
126  */
127 ir_type *
128 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
129   ir_type *res;
130   int node_size;
131
132   assert(type_op != type_id);
133   assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
134
135   node_size = offsetof(ir_type, attr) +  type_op->attr_size;
136   res = xmalloc(node_size);
137   memset(res, 0, node_size);
138
139   res->kind       = k_type;
140   res->type_op    = type_op;
141   res->mode       = mode;
142   res->name       = name;
143   res->visibility = visibility_external_allocated;
144   res->flags      = tf_none;
145   res->size       = -1;
146   res->align      = -1;
147   res->visit      = 0;
148   res->link       = NULL;
149   res->dbi        = db;
150   res->assoc_type = NULL;
151 #ifdef DEBUG_libfirm
152   res->nr         = get_irp_new_node_nr();
153 #endif /* defined DEBUG_libfirm */
154
155   add_irp_type(res);   /* Remember the new type global. */
156
157   return res;
158 }
159
160 void        free_type(ir_type *tp) {
161   const tp_op *op = get_type_tpop(tp);
162
163   if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
164     return;
165   /* Remove from list of all types */
166   remove_irp_type(tp);
167   /* Free the attributes of the type. */
168   free_type_attrs(tp);
169   /* Free entities automatically allocated with the ir_type */
170   if (op->ops.free_auto_entities)
171     op->ops.free_auto_entities(tp);
172   /* And now the type itself... */
173   tp->kind = k_BAD;
174   free(tp);
175 }
176
177 void free_type_entities(ir_type *tp) {
178   const tp_op *tpop = get_type_tpop(tp);
179
180   if (tpop->ops.free_entities)
181     tpop->ops.free_entities(tp);
182 }
183
184 void free_type_attrs(ir_type *tp) {
185   const tp_op *tpop = get_type_tpop(tp);
186
187   if (tpop->ops.free_attrs)
188     tpop->ops.free_attrs(tp);
189 }
190
191 /* set/get the link field */
192 void *(get_type_link)(const ir_type *tp) {
193   return _get_type_link(tp);
194 }
195
196 void (set_type_link)(ir_type *tp, void *l) {
197   _set_type_link(tp, l);
198 }
199
200 const tp_op *(get_type_tpop)(const ir_type *tp) {
201   return _get_type_tpop(tp);
202 }
203
204 ident *(get_type_tpop_nameid)(const ir_type *tp) {
205   return _get_type_tpop_nameid(tp);
206 }
207
208 const char* get_type_tpop_name(const ir_type *tp) {
209   assert(tp && tp->kind == k_type);
210   return get_id_str(tp->type_op->name);
211 }
212
213 tp_opcode (get_type_tpop_code)(const ir_type *tp) {
214   return _get_type_tpop_code(tp);
215 }
216
217 ir_mode *(get_type_mode)(const ir_type *tp) {
218   return _get_type_mode(tp);
219 }
220
221 void        set_type_mode(ir_type *tp, ir_mode *mode) {
222   const tp_op *tpop = get_type_tpop(tp);
223
224   if (tpop->ops.set_type_mode)
225     tpop->ops.set_type_mode(tp, mode);
226   else
227     assert(0 && "setting a mode is NOT allowed for this type");
228 }
229
230 ident *(get_type_ident)(const ir_type *tp) {
231   return _get_type_ident(tp);
232 }
233
234 void (set_type_ident)(ir_type *tp, ident* id) {
235   _set_type_ident(tp, id);
236 }
237
238 /* Outputs a unique number for this node */
239 long get_type_nr(const ir_type *tp) {
240   assert(tp);
241 #ifdef DEBUG_libfirm
242   return tp->nr;
243 #else
244   return (long)tp;
245 #endif
246 }
247
248 const char* get_type_name(const ir_type *tp) {
249   assert(tp && tp->kind == k_type);
250   return (get_id_str(tp->name));
251 }
252
253 int (get_type_size_bytes)(const ir_type *tp) {
254   return _get_type_size_bytes(tp);
255 }
256
257 int (get_type_size_bits)(const ir_type *tp) {
258   return _get_type_size_bits(tp);
259 }
260
261
262 visibility get_type_visibility (const ir_type *tp) {
263 #if 0
264   visibility res =  visibility_local;
265   if (is_compound_type(tp)) {
266
267     if (is_Array_type(tp)) {
268       entity *mem = get_array_element_entity(tp);
269       if (get_entity_visibility(mem) != visibility_local)
270         res = visibility_external_visible;
271     } else {
272       int i, n_mems = get_compound_n_members(tp);
273       for (i = 0; i < n_mems; ++i) {
274         entity *mem = get_compound_member(tp, i);
275         if (get_entity_visibility(mem) != visibility_local)
276           res = visibility_external_visible;
277       }
278     }
279   }
280   return res;
281 #endif
282   assert(is_type(tp));
283   return tp->visibility;
284 }
285
286 void       set_type_visibility (ir_type *tp, visibility v) {
287   assert(is_type(tp));
288 #if 0
289   /* check for correctness */
290   if (v != visibility_external_allocated) {
291     visibility res =  visibility_local;
292     if (is_compound_type(tp)) {
293       if (is_Array_type(tp)) {
294         entity *mem = get_array_element_entity(tp);
295         if (get_entity_visibility(mem) >  res)
296                 res = get_entity_visibility(mem);
297       } else {
298         int i, n_mems = get_compound_n_members(tp);
299         for (i = 0; i < n_mems; ++i) {
300                 entity *mem = get_compound_member(tp, i);
301                 if (get_entity_visibility(mem) > res)
302                   res = get_entity_visibility(mem);
303         }
304       }
305     }
306     assert(res < v);
307   }
308 #endif
309   tp->visibility = v;
310 }
311
312 void
313 set_type_size_bits(ir_type *tp, int size) {
314   const tp_op *tpop = get_type_tpop(tp);
315
316   if (tpop->ops.set_type_size)
317     tpop->ops.set_type_size(tp, size);
318   else
319     assert(0 && "Cannot set size for this type");
320 }
321
322 void
323 set_type_size_bytes(ir_type *tp, int size) {
324   set_type_size_bits(tp, 8*size);
325 }
326
327 int get_type_alignment_bytes(ir_type *tp) {
328   int align = get_type_alignment_bits(tp);
329
330   return align < 0 ? align : (align + 7) >> 3;
331 }
332
333 int get_type_alignment_bits(ir_type *tp) {
334   int align = 8;
335
336   if (tp->align > 0)
337     return tp->align;
338
339   /* alignment NOT set calculate it "on demand" */
340   if (tp->mode)
341     align = get_mode_size_bits(tp->mode);
342   else if (is_Array_type(tp))
343     align = get_type_alignment_bits(get_array_element_type(tp));
344   else if (is_compound_type(tp)) {
345     int i, n = get_compound_n_members(tp);
346
347     align = 0;
348     for (i = 0; i < n; ++i) {
349       ir_type *t = get_entity_type(get_compound_member(tp, i));
350       int   a = get_type_alignment_bits(t);
351
352       if (a > align)
353         align = a;
354     }
355   }
356   else if (is_Method_type(tp))
357     align = 0;
358
359   /* write back */
360   tp->align = align;
361
362   return align;
363 }
364
365 void
366 set_type_alignment_bits(ir_type *tp, int align) {
367   assert(tp && tp->kind == k_type);
368   /* Methods don't have an alignment. */
369   if (tp->type_op != type_method) {
370     tp->align = align;
371   }
372 }
373
374 void
375 set_type_alignment_bytes(ir_type *tp, int align) {
376   set_type_alignment_bits(tp, 8*align);
377 }
378
379 /* Returns a human readable string for the enum entry. */
380 const char *get_type_state_name(type_state s) {
381 #define X(a)    case a: return #a;
382   switch (s) {
383     X(layout_undefined);
384     X(layout_fixed);
385   }
386   return "<unknown>";
387 #undef X
388 }
389
390
391 type_state (get_type_state)(const ir_type *tp) {
392   return _get_type_state(tp);
393 }
394
395 void
396 set_type_state(ir_type *tp, type_state state) {
397   assert(tp && tp->kind == k_type);
398
399   if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
400       (tp->type_op == type_method))
401     return;
402
403   /* Just a correctness check: */
404   if (state == layout_fixed) {
405     int i;
406     switch (get_type_tpop_code(tp)) {
407     case tpo_class:
408       {
409     assert(get_type_size_bits(tp) > -1);
410     if (tp != get_glob_type()) {
411       int n_mem = get_class_n_members(tp);
412       for (i = 0; i < n_mem; i++) {
413         if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
414           { DDMT(tp); DDME(get_class_member(tp, i)); }
415         assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
416             /* TR ??
417         assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
418            (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
419                    */
420       }
421     }
422       } break;
423     case tpo_struct:
424       {
425         assert(get_type_size_bits(tp) > -1);
426         for (i = 0; i < get_struct_n_members(tp); i++) {
427           assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
428           assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
429         }
430       } break;
431     case tpo_union:
432       { /* ?? */
433       } break;
434     case tpo_array:
435       { /* ??
436          Check order?
437          Assure that only innermost dimension is dynamic? */
438       } break;
439     case tpo_enumeration:
440       {
441         assert(get_type_mode != NULL);
442         for (i = 0; i < get_enumeration_n_enums(tp); i++)
443           assert(get_enumeration_enum(tp, i) != NULL);
444       } break;
445     default: break;
446     } /* switch (tp) */
447   }
448   if (state = layout_fixed)
449     tp->flags |= tf_layout_fixed;
450   else
451     tp->flags &= tf_layout_fixed;
452 }
453
454 unsigned long (get_type_visited)(const ir_type *tp) {
455   return _get_type_visited(tp);
456 }
457
458 void (set_type_visited)(ir_type *tp, unsigned long num) {
459   _set_type_visited(tp, num);
460 }
461
462 /* Sets visited field in type to type_visited. */
463 void (mark_type_visited)(ir_type *tp) {
464   _mark_type_visited(tp);
465 }
466
467 int (type_visited)(const ir_type *tp) {
468   return _type_visited(tp);
469 }
470
471 int (type_not_visited)(const ir_type *tp) {
472   return _type_not_visited(tp);
473 }
474
475 int (is_type)(const void *thing) {
476   return _is_type(thing);
477 }
478
479 /* Checks whether two types are structural equal.*/
480 int equal_type(ir_type *typ1, ir_type *typ2) {
481   entity **m;
482   ir_type **t;
483   int i, j;
484
485   if (typ1 == typ2) return 1;
486
487   if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
488       (get_type_ident(typ1) != get_type_ident(typ2)) ||
489       (get_type_mode(typ1) != get_type_mode(typ2)) ||
490       (get_type_state(typ1) != get_type_state(typ2)))
491     return 0;
492   if ((get_type_state(typ1) == layout_fixed) &&
493       (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
494     return 0;
495
496   switch (get_type_tpop_code(typ1)) {
497   case tpo_class:       {
498     if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
499     if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
500     if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
501     if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
502     /** Compare the members **/
503     m = alloca(sizeof(entity *) * get_class_n_members(typ1));
504     memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
505     /* First sort the members of typ2 */
506     for (i = 0; i < get_class_n_members(typ1); i++) {
507       entity *e1 = get_class_member(typ1, i);
508       for (j = 0; j < get_class_n_members(typ2); j++) {
509         entity *e2 = get_class_member(typ2, j);
510         if (get_entity_name(e1) == get_entity_name(e2))
511           m[i] = e2;
512       }
513     }
514     for (i = 0; i < get_class_n_members(typ1); i++) {
515       if (!m[i]  ||  /* Found no counterpart */
516           !equal_entity(get_class_member(typ1, i), m[i]))
517         return 0;
518     }
519     /** Compare the supertypes **/
520     t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
521     memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
522     /* First sort the supertypes of typ2 */
523     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
524       ir_type *t1 = get_class_supertype(typ1, i);
525       for (j = 0; j < get_class_n_supertypes(typ2); j++) {
526         ir_type *t2 = get_class_supertype(typ2, j);
527         if (get_type_ident(t2) == get_type_ident(t1))
528           t[i] = t2;
529       }
530     }
531     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
532       if (!t[i]  ||  /* Found no counterpart */
533           get_class_supertype(typ1, i) != t[i])
534         return 0;
535     }
536   } break;
537   case tpo_struct:      {
538     if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
539     m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
540     memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
541     /* First sort the members of lt */
542     for (i = 0; i < get_struct_n_members(typ1); i++) {
543       entity *e1 = get_struct_member(typ1, i);
544       for (j = 0; j < get_struct_n_members(typ2); j++) {
545         entity *e2 = get_struct_member(typ2, j);
546         if (get_entity_name(e1) == get_entity_name(e2))
547           m[i] = e2;
548       }
549     }
550     for (i = 0; i < get_struct_n_members(typ1); i++) {
551       if (!m[i]  ||  /* Found no counterpart */
552           !equal_entity(get_struct_member(typ1, i), m[i]))
553         return 0;
554     }
555   } break;
556   case tpo_method:      {
557     int n_param1, n_param2;
558
559     if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
560     if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return 0;
561     if (get_method_calling_convention(typ1) !=
562         get_method_calling_convention(typ2)) return 0;
563
564     if (get_method_variadicity(typ1) == variadicity_non_variadic) {
565       n_param1 = get_method_n_params(typ1);
566       n_param2 = get_method_n_params(typ2);
567     }
568     else {
569       n_param1 = get_method_first_variadic_param_index(typ1);
570       n_param2 = get_method_first_variadic_param_index(typ2);
571     }
572
573     if (n_param1 != n_param2) return 0;
574
575     for (i = 0; i < n_param1; i++) {
576       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
577         return 0;
578     }
579     for (i = 0; i < get_method_n_ress(typ1); i++) {
580       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
581         return 0;
582     }
583   } break;
584   case tpo_union:       {
585     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
586     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
587     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
588     /* First sort the members of lt */
589     for (i = 0; i < get_union_n_members(typ1); i++) {
590       entity *e1 = get_union_member(typ1, i);
591       for (j = 0; j < get_union_n_members(typ2); j++) {
592         entity *e2 = get_union_member(typ2, j);
593         if (get_entity_name(e1) == get_entity_name(e2))
594           m[i] = e2;
595       }
596     }
597     for (i = 0; i < get_union_n_members(typ1); i++) {
598       if (!m[i]  ||  /* Found no counterpart */
599           !equal_entity(get_union_member(typ1, i), m[i]))
600         return 0;
601     }
602   } break;
603   case tpo_array:       {
604     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
605       return 0;
606     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
607       return 0;
608     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
609       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
610           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
611         return 0;
612       if (get_array_order(typ1, i) != get_array_order(typ2, i))
613         assert(0 && "type compare with different dimension orders not implemented");
614     }
615   } break;
616   case tpo_enumeration: {
617     assert(0 && "enumerations not implemented");
618   } break;
619   case tpo_pointer:     {
620     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
621       return 0;
622   } break;
623   case tpo_primitive:   {
624   } break;
625   default: break;
626   }
627   return 1;
628 }
629
630 /* Checks whether two types are structural comparable. */
631 int smaller_type (ir_type *st, ir_type *lt) {
632   entity **m;
633   int i, j;
634
635   if (st == lt) return 1;
636
637   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
638     return 0;
639
640   switch(get_type_tpop_code(st)) {
641   case tpo_class:       {
642     return is_SubClass_of(st, lt);
643   } break;
644   case tpo_struct:      {
645     if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
646     m = alloca(sizeof(entity *) * get_struct_n_members(st));
647     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
648     /* First sort the members of lt */
649     for (i = 0; i < get_struct_n_members(st); i++) {
650       entity *se = get_struct_member(st, i);
651       for (j = 0; j < get_struct_n_members(lt); j++) {
652         entity *le = get_struct_member(lt, j);
653         if (get_entity_name(le) == get_entity_name(se))
654           m[i] = le;
655       }
656     }
657     for (i = 0; i < get_struct_n_members(st); i++) {
658       if (!m[i]  ||  /* Found no counterpart */
659           !smaller_type(get_entity_type(get_struct_member(st, i)),
660                 get_entity_type(m[i])))
661         return 0;
662     }
663   } break;
664   case tpo_method:      {
665     int n_param1, n_param2;
666
667     /** FIXME: is this still 1? */
668     if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
669     if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
670     if (get_method_calling_convention(st) !=
671       get_method_calling_convention(lt)) return 0;
672
673     if (get_method_variadicity(st) == variadicity_non_variadic) {
674       n_param1 = get_method_n_params(st);
675       n_param2 = get_method_n_params(lt);
676     }
677     else {
678       n_param1 = get_method_first_variadic_param_index(st);
679       n_param2 = get_method_first_variadic_param_index(lt);
680     }
681
682     if (n_param1 != n_param2) return 0;
683
684     for (i = 0; i < get_method_n_params(st); i++) {
685       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
686         return 0;
687     }
688     for (i = 0; i < get_method_n_ress(st); i++) {
689       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
690         return 0;
691     }
692   } break;
693   case tpo_union:       {
694     if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
695     m = alloca(sizeof(entity *) * get_union_n_members(st));
696     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
697     /* First sort the members of lt */
698     for (i = 0; i < get_union_n_members(st); i++) {
699       entity *se = get_union_member(st, i);
700       for (j = 0; j < get_union_n_members(lt); j++) {
701         entity *le = get_union_member(lt, j);
702         if (get_entity_name(le) == get_entity_name(se))
703           m[i] = le;
704           }
705     }
706     for (i = 0; i < get_union_n_members(st); i++) {
707       if (!m[i]  ||  /* Found no counterpart */
708           !smaller_type(get_entity_type(get_union_member(st, i)),
709                 get_entity_type(m[i])))
710         return 0;
711     }
712   } break;
713   case tpo_array:       {
714     ir_type *set, *let;  /* small/large elt. ir_type */
715     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
716       return 0;
717     set = get_array_element_type(st);
718     let = get_array_element_type(lt);
719     if (set != let) {
720       /* If the element types are different, set must be convertible
721          to let, and they must have the same size so that address
722          computations work out.  To have a size the layout must
723          be fixed. */
724       if ((get_type_state(set) != layout_fixed) ||
725           (get_type_state(let) != layout_fixed))
726         return 0;
727       if (!smaller_type(set, let) ||
728           get_type_size_bits(set) != get_type_size_bits(let))
729         return 0;
730     }
731     for(i = 0; i < get_array_n_dimensions(st); i++) {
732       if (get_array_lower_bound(lt, i))
733         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
734           return 0;
735       if (get_array_upper_bound(lt, i))
736         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
737           return 0;
738     }
739   } break;
740   case tpo_enumeration: {
741     assert(0 && "enumerations not implemented");
742   } break;
743   case tpo_pointer:     {
744     if (!smaller_type(get_pointer_points_to_type(st),
745               get_pointer_points_to_type(lt)))
746       return 0;
747   } break;
748   case tpo_primitive:   {
749     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
750       return 0;
751   } break;
752   default: break;
753   }
754   return 1;
755 }
756
757 /*-----------------------------------------------------------------*/
758 /* TYPE_CLASS                                                      */
759 /*-----------------------------------------------------------------*/
760
761 /* create a new class ir_type */
762 ir_type *new_d_type_class (ident *name, dbg_info *db) {
763   ir_type *res;
764
765   res = new_type(type_class, NULL, name, db);
766
767   res->attr.ca.members     = NEW_ARR_F (entity *, 0);
768   res->attr.ca.subtypes    = NEW_ARR_F (ir_type *, 0);
769   res->attr.ca.supertypes  = NEW_ARR_F (ir_type *, 0);
770   res->attr.ca.peculiarity = peculiarity_existent;
771   res->attr.ca.type_info   = NULL;
772   res->attr.ca.final       = 0;
773   res->attr.ca.dfn         = 0;
774   hook_new_type(res);
775   return res;
776 }
777
778 ir_type *new_type_class (ident *name) {
779   return new_d_type_class (name, NULL);
780 }
781
782 /* free all entities of a class */
783 void free_class_entities(ir_type *clss) {
784   int i;
785   assert(clss && (clss->type_op == type_class));
786   for (i = get_class_n_members(clss) - 1; i >= 0; --i)
787     free_entity(get_class_member(clss, i));
788   /* do NOT free the type info here. It belongs to another class */
789 }
790
791 void free_class_attrs(ir_type *clss) {
792   assert(clss && (clss->type_op == type_class));
793   DEL_ARR_F(clss->attr.ca.members);
794   DEL_ARR_F(clss->attr.ca.subtypes);
795   DEL_ARR_F(clss->attr.ca.supertypes);
796 }
797
798 /* manipulate private fields of class type  */
799 void    add_class_member   (ir_type *clss, entity *member) {
800   assert(clss && (clss->type_op == type_class));
801   assert(clss != get_entity_type(member) && "recursive type");
802   ARR_APP1 (entity *, clss->attr.ca.members, member);
803 }
804
805 int     (get_class_n_members) (const ir_type *clss) {
806   return _get_class_n_members(clss);
807 }
808
809 int     get_class_member_index(const ir_type *clss, entity *mem) {
810   int i, n;
811   assert(clss && (clss->type_op == type_class));
812   for (i = 0, n = get_class_n_members(clss); i < n; ++i)
813     if (get_class_member(clss, i) == mem)
814       return i;
815   return -1;
816 }
817
818 entity *(get_class_member)   (const ir_type *clss, int pos) {
819   return _get_class_member(clss, pos);
820 }
821
822 entity *get_class_member_by_name(ir_type *clss, ident *name) {
823   int i, n_mem;
824   assert(clss && (clss->type_op == type_class));
825   n_mem = get_class_n_members(clss);
826   for (i = 0; i < n_mem; ++i) {
827     entity *mem = get_class_member(clss, i);
828     if (get_entity_ident(mem) == name) return mem;
829   }
830   return NULL;
831 }
832
833 void    set_class_member   (ir_type *clss, entity *member, int pos) {
834   assert(clss && (clss->type_op == type_class));
835   assert(pos >= 0 && pos < get_class_n_members(clss));
836   clss->attr.ca.members[pos] = member;
837 }
838 void    set_class_members  (ir_type *clss, entity **members, int arity) {
839   int i;
840   assert(clss && (clss->type_op == type_class));
841   DEL_ARR_F(clss->attr.ca.members);
842   clss->attr.ca.members    = NEW_ARR_F (entity *, 0);
843   for (i = 0; i < arity; i++) {
844     set_entity_owner(members[i], clss);
845     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
846   }
847 }
848 void    remove_class_member(ir_type *clss, entity *member) {
849   int i;
850   assert(clss && (clss->type_op == type_class));
851   for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
852     if (clss->attr.ca.members[i] == member) {
853       for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
854         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
855       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
856       break;
857     }
858   }
859 }
860
861 void    add_class_subtype   (ir_type *clss, ir_type *subtype) {
862   int i;
863   assert(clss && (clss->type_op == type_class));
864   ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
865   for (i = 0; i < get_class_n_supertypes(subtype); i++)
866     if (get_class_supertype(subtype, i) == clss)
867       /* Class already registered */
868       return;
869   ARR_APP1 (ir_type *, subtype->attr.ca.supertypes, clss);
870 }
871 int     get_class_n_subtypes (const ir_type *clss) {
872   assert(clss && (clss->type_op == type_class));
873   return (ARR_LEN (clss->attr.ca.subtypes));
874 }
875 ir_type *get_class_subtype   (ir_type *clss, int pos) {
876   assert(clss && (clss->type_op == type_class));
877   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
878   return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
879 }
880 int     get_class_subtype_index(ir_type *clss, const ir_type *subclass) {
881   int i, n_subtypes = get_class_n_subtypes(clss);
882   assert(is_Class_type(subclass));
883   for (i = 0; i < n_subtypes; ++i) {
884     if (get_class_subtype(clss, i) == subclass) return i;
885   }
886   return -1;
887 }
888 void    set_class_subtype   (ir_type *clss, ir_type *subtype, int pos) {
889   assert(clss && (clss->type_op == type_class));
890   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
891   clss->attr.ca.subtypes[pos] = subtype;
892 }
893 void    remove_class_subtype(ir_type *clss, ir_type *subtype) {
894   int i;
895   assert(clss && (clss->type_op == type_class));
896   for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
897     if (clss->attr.ca.subtypes[i] == subtype) {
898       for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
899         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
900       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
901       break;
902     }
903 }
904
905 void    add_class_supertype   (ir_type *clss, ir_type *supertype) {
906   int i;
907   assert(clss && (clss->type_op == type_class));
908   assert(supertype && (supertype -> type_op == type_class));
909   ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
910   for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
911     if (get_class_subtype(supertype, i) == clss)
912       /* Class already registered */
913       return;
914   ARR_APP1 (ir_type *, supertype->attr.ca.subtypes, clss);
915 }
916 int     get_class_n_supertypes (const ir_type *clss) {
917   assert(clss && (clss->type_op == type_class));
918   return (ARR_LEN (clss->attr.ca.supertypes));
919 }
920 int get_class_supertype_index(ir_type *clss, ir_type *super_clss) {
921   int i, n_supertypes = get_class_n_supertypes(clss);
922   assert(super_clss && (super_clss->type_op == type_class));
923   for (i = 0; i < n_supertypes; i++)
924     if (get_class_supertype(clss, i) == super_clss)
925       return i;
926   return -1;
927 }
928 ir_type *get_class_supertype   (ir_type *clss, int pos) {
929   assert(clss && (clss->type_op == type_class));
930   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
931   return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
932 }
933 void    set_class_supertype   (ir_type *clss, ir_type *supertype, int pos) {
934   assert(clss && (clss->type_op == type_class));
935   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
936   clss->attr.ca.supertypes[pos] = supertype;
937 }
938 void    remove_class_supertype(ir_type *clss, ir_type *supertype) {
939   int i;
940   assert(clss && (clss->type_op == type_class));
941   for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
942     if (clss->attr.ca.supertypes[i] == supertype) {
943       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
944     clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
945       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
946       break;
947     }
948 }
949 entity *get_class_type_info(const ir_type *clss) {
950   return clss->attr.ca.type_info;
951 }
952 void set_class_type_info(ir_type *clss, entity *ent) {
953   clss->attr.ca.type_info = ent;
954 }
955
956 const char *get_peculiarity_string(peculiarity p) {
957 #define X(a)    case a: return #a
958   switch (p) {
959     X(peculiarity_description);
960     X(peculiarity_inherited);
961     X(peculiarity_existent);
962   }
963 #undef X
964   return "invalid peculiarity";
965 }
966
967 peculiarity get_class_peculiarity (const ir_type *clss) {
968   assert(clss && (clss->type_op == type_class));
969   return clss->attr.ca.peculiarity;
970 }
971
972 void        set_class_peculiarity (ir_type *clss, peculiarity pec) {
973   assert(clss && (clss->type_op == type_class));
974   assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
975   clss->attr.ca.peculiarity = pec;
976 }
977
978 int (is_class_final)(const ir_type *clss) {
979   return _is_class_final(clss);
980 }
981
982 void (set_class_final)(ir_type *clss, int final) {
983   _set_class_final(clss, final);
984 }
985
986 void set_class_dfn (ir_type *clss, int dfn) {
987   clss->attr.ca.dfn = dfn;
988 }
989
990 int get_class_dfn (const ir_type *clss) {
991   return (clss->attr.ca.dfn);
992 }
993
994 /* typecheck */
995 int (is_Class_type)(const ir_type *clss) {
996   return _is_class_type(clss);
997 }
998
999 void set_class_mode(ir_type *tp, ir_mode *mode) {
1000   /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1001   assert(get_type_state(tp) == layout_fixed &&
1002     tp->size == get_mode_size_bits(mode) && "mode don't match class layout");
1003   tp->mode = mode;
1004 }
1005
1006 void set_class_size_bits(ir_type *tp, int size) {
1007   /* argh: we must allow to set negative values as "invalid size" */
1008   tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1009   assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1010 }
1011
1012 /*----------------------------------------------------------------**/
1013 /* TYPE_STRUCT                                                     */
1014 /*----------------------------------------------------------------**/
1015
1016 /* create a new type struct */
1017 ir_type *new_d_type_struct(ident *name, dbg_info *db) {
1018   ir_type *res = new_type(type_struct, NULL, name, db);
1019
1020   res->attr.sa.members = NEW_ARR_F(entity *, 0);
1021   hook_new_type(res);
1022   return res;
1023 }
1024
1025 ir_type *new_type_struct (ident *name) {
1026   return new_d_type_struct (name, NULL);
1027 }
1028
1029 void free_struct_entities (ir_type *strct) {
1030   int i;
1031   assert(strct && (strct->type_op == type_struct));
1032   for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1033     free_entity(get_struct_member(strct, i));
1034 }
1035 void free_struct_attrs (ir_type *strct) {
1036   assert(strct && (strct->type_op == type_struct));
1037   DEL_ARR_F(strct->attr.sa.members);
1038 }
1039
1040 /* manipulate private fields of struct */
1041 int     get_struct_n_members (const ir_type *strct) {
1042   assert(strct && (strct->type_op == type_struct));
1043   return (ARR_LEN (strct->attr.sa.members));
1044 }
1045
1046 void    add_struct_member   (ir_type *strct, entity *member) {
1047   assert(strct && (strct->type_op == type_struct));
1048   assert(get_type_tpop(get_entity_type(member)) != type_method);
1049     /*    @@@ lowerfirm geht nicht durch */
1050   assert(strct != get_entity_type(member) && "recursive type");
1051   ARR_APP1 (entity *, strct->attr.sa.members, member);
1052 }
1053
1054 entity *get_struct_member   (const ir_type *strct, int pos) {
1055   assert(strct && (strct->type_op == type_struct));
1056   assert(pos >= 0 && pos < get_struct_n_members(strct));
1057   return strct->attr.sa.members[pos];
1058 }
1059
1060 int     get_struct_member_index(const ir_type *strct, entity *mem) {
1061   int i, n;
1062   assert(strct && (strct->type_op == type_struct));
1063   for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1064     if (get_struct_member(strct, i) == mem)
1065       return i;
1066   return -1;
1067 }
1068
1069 void    set_struct_member   (ir_type *strct, int pos, entity *member) {
1070   assert(strct && (strct->type_op == type_struct));
1071   assert(pos >= 0 && pos < get_struct_n_members(strct));
1072   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1073   strct->attr.sa.members[pos] = member;
1074 }
1075 void    remove_struct_member(ir_type *strct, entity *member) {
1076   int i;
1077   assert(strct && (strct->type_op == type_struct));
1078   for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1079     if (strct->attr.sa.members[i] == member) {
1080       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1081     strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1082       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1083       break;
1084     }
1085 }
1086
1087 /* typecheck */
1088 int (is_Struct_type)(const ir_type *strct) {
1089   return _is_struct_type(strct);
1090 }
1091
1092 void set_struct_mode(ir_type *tp, ir_mode *mode) {
1093   /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1094   assert(get_type_state(tp) == layout_fixed &&
1095     tp->size == get_mode_size_bits(mode) && "mode don't match struct layout");
1096   tp->mode = mode;
1097 }
1098
1099 void set_struct_size_bits(ir_type *tp, int size) {
1100   /* argh: we must allow to set negative values as "invalid size" */
1101   tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1102   assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1103 }
1104
1105 /*******************************************************************/
1106 /** TYPE_METHOD                                                   **/
1107 /*******************************************************************/
1108
1109 /**
1110  * Lazy construction of value argument / result representation.
1111  * Constructs a struct type and its member.  The types of the members
1112  * are passed in the argument list.
1113  *
1114  * @param name    name of the type constructed
1115  * @param len     number of fields
1116  * @param tps     array of field types with length len
1117  */
1118 static INLINE ir_type *
1119 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1120   int i;
1121   ir_type *res = new_type_struct(name);
1122   /* Remove type from type list.  Must be treated differently than other types. */
1123   remove_irp_type(res);
1124   for (i = 0; i < len; i++) {
1125     /* use res as default if corresponding type is not yet set. */
1126     ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1127
1128     tps[i].ent = new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1129   }
1130   return res;
1131 }
1132
1133 /* Create a new method type.
1134    N_param is the number of parameters, n_res the number of results.  */
1135 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1136   ir_type *res;
1137
1138   assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1139   res = new_type(type_method, mode_P_code, name, db);
1140   res->flags                       |= tf_layout_fixed;
1141   res->size                         = get_mode_size_bits(mode_P_code);
1142   res->attr.ma.n_params             = n_param;
1143   res->attr.ma.param_type           = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1144   res->attr.ma.value_params         = NULL;
1145   res->attr.ma.n_res                = n_res;
1146   res->attr.ma.res_type             = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1147   res->attr.ma.value_ress           = NULL;
1148   res->attr.ma.variadicity          = variadicity_non_variadic;
1149   res->attr.ma.first_variadic_param = -1;
1150   res->attr.ma.additional_properties = mtp_no_property;
1151   res->attr.ma.irg_calling_conv     = default_cc_mask;
1152   hook_new_type(res);
1153   return res;
1154 }
1155
1156 ir_type *new_type_method(ident *name, int n_param, int n_res) {
1157   return new_d_type_method(name, n_param, n_res, NULL);
1158 }
1159
1160 void free_method_entities(ir_type *method) {
1161   assert(method && (method->type_op == type_method));
1162 }
1163
1164 /* Attention: also frees entities in value parameter subtypes! */
1165 void free_method_attrs(ir_type *method) {
1166   assert(method && (method->type_op == type_method));
1167   free(method->attr.ma.param_type);
1168   free(method->attr.ma.res_type);
1169   if (method->attr.ma.value_params) {
1170     free_type_entities(method->attr.ma.value_params);
1171     free_type(method->attr.ma.value_params);
1172   }
1173   if (method->attr.ma.value_ress) {
1174     free_type_entities(method->attr.ma.value_ress);
1175     free_type(method->attr.ma.value_ress);
1176   }
1177 }
1178
1179 /* manipulate private fields of method. */
1180 int (get_method_n_params)(const ir_type *method) {
1181   return _get_method_n_params(method);
1182 }
1183
1184 ir_type *get_method_param_type(ir_type *method, int pos) {
1185   ir_type *res;
1186   assert(method && (method->type_op == type_method));
1187   assert(pos >= 0 && pos < get_method_n_params(method));
1188   res = method->attr.ma.param_type[pos].tp;
1189   assert(res != NULL && "empty method param type");
1190   return method->attr.ma.param_type[pos].tp = skip_tid(res);
1191 }
1192
1193 void  set_method_param_type(ir_type *method, int pos, ir_type *tp) {
1194   assert(method && (method->type_op == type_method));
1195   assert(pos >= 0 && pos < get_method_n_params(method));
1196   method->attr.ma.param_type[pos].tp = tp;
1197   /* If information constructed set pass-by-value representation. */
1198   if (method->attr.ma.value_params) {
1199     assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1200     set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1201   }
1202 }
1203
1204 /* Returns an entity that represents the copied value argument.  Only necessary
1205    for compounds passed by value. */
1206 entity *get_method_value_param_ent(ir_type *method, int pos) {
1207   assert(method && (method->type_op == type_method));
1208   assert(pos >= 0 && pos < get_method_n_params(method));
1209
1210   if (!method->attr.ma.value_params) {
1211     /* parameter value type not created yet, build */
1212     method->attr.ma.value_params
1213       = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1214              get_method_n_params(method), method->attr.ma.param_type);
1215   }
1216   /*
1217    * build_value_type() sets the method->attr.ma.value_params type as default if
1218    * no type is set!
1219    */
1220   assert((get_entity_type(method->attr.ma.param_type[pos].ent) != method->attr.ma.value_params)
1221      && "param type not yet set");
1222   return method->attr.ma.param_type[pos].ent;
1223 }
1224
1225 /*
1226  * Returns a type that represents the copied value arguments.
1227  */
1228 ir_type *get_method_value_param_type(const ir_type *method)
1229 {
1230   assert(method && (method->type_op == type_method));
1231   return method->attr.ma.value_params;
1232 }
1233
1234 int (get_method_n_ress)(const ir_type *method) {
1235   return _get_method_n_ress(method);
1236 }
1237
1238 ir_type *get_method_res_type(ir_type *method, int pos) {
1239   ir_type *res;
1240   assert(method && (method->type_op == type_method));
1241   assert(pos >= 0 && pos < get_method_n_ress(method));
1242   res = method->attr.ma.res_type[pos].tp;
1243   assert(res != NULL && "empty method return type");
1244   return method->attr.ma.res_type[pos].tp = skip_tid(res);
1245 }
1246
1247 void  set_method_res_type(ir_type *method, int pos, ir_type *tp) {
1248   assert(method && (method->type_op == type_method));
1249   assert(pos >= 0 && pos < get_method_n_ress(method));
1250   /* set the result ir_type */
1251   method->attr.ma.res_type[pos].tp = tp;
1252   /* If information constructed set pass-by-value representation. */
1253   if (method->attr.ma.value_ress) {
1254     assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1255     set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1256   }
1257 }
1258
1259 /* Returns an entity that represents the copied value result.  Only necessary
1260    for compounds passed by value. */
1261 entity *get_method_value_res_ent(ir_type *method, int pos) {
1262   assert(method && (method->type_op == type_method));
1263   assert(pos >= 0 && pos < get_method_n_ress(method));
1264
1265   if (!method->attr.ma.value_ress) {
1266     /* result value type not created yet, build */
1267     method->attr.ma.value_ress
1268       = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1269              get_method_n_ress(method), method->attr.ma.res_type);
1270   }
1271   /*
1272    * build_value_type() sets the method->attr.ma.value_ress type as default if
1273    * no type is set!
1274    */
1275   assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1276      && "result type not yet set");
1277
1278   return method->attr.ma.res_type[pos].ent;
1279 }
1280
1281 /*
1282  * Returns a type that represents the copied value results.
1283  */
1284 ir_type *get_method_value_res_type(const ir_type *method) {
1285   assert(method && (method->type_op == type_method));
1286   return method->attr.ma.value_ress;
1287 }
1288
1289 /* Returns the null-terminated name of this variadicity. */
1290 const char *get_variadicity_name(variadicity vari)
1291 {
1292 #define X(a)    case a: return #a
1293   switch (vari) {
1294     X(variadicity_non_variadic);
1295     X(variadicity_variadic);
1296     default:
1297       return "BAD VALUE";
1298   }
1299 #undef X
1300 }
1301
1302 variadicity get_method_variadicity(const ir_type *method)
1303 {
1304   assert(method && (method->type_op == type_method));
1305   return method->attr.ma.variadicity;
1306 }
1307
1308 void set_method_variadicity(ir_type *method, variadicity vari)
1309 {
1310   assert(method && (method->type_op == type_method));
1311   method->attr.ma.variadicity = vari;
1312 }
1313
1314 /*
1315  * Returns the first variadic parameter index of a type.
1316  * If this index was NOT set, the index of the last parameter
1317  * of the method type plus one is returned for variadic functions.
1318  * Non-variadic function types always return -1 here.
1319  */
1320 int get_method_first_variadic_param_index(const ir_type *method)
1321 {
1322   assert(method && (method->type_op == type_method));
1323
1324   if (method->attr.ma.variadicity == variadicity_non_variadic)
1325     return -1;
1326
1327   if (method->attr.ma.first_variadic_param == -1)
1328     return get_method_n_params(method);
1329   return method->attr.ma.first_variadic_param;
1330 }
1331
1332 /*
1333  * Sets the first variadic parameter index. This allows to specify
1334  * a complete call type (containing the type of all parameters)
1335  * but still have the knowledge, which parameter must be passed as
1336  * variadic one.
1337  */
1338 void set_method_first_variadic_param_index(ir_type *method, int index)
1339 {
1340   assert(method && (method->type_op == type_method));
1341   assert(index >= 0 && index <= get_method_n_params(method));
1342
1343   method->attr.ma.first_variadic_param = index;
1344 }
1345
1346 unsigned (get_method_additional_properties)(const ir_type *method) {
1347   return _get_method_additional_properties(method);
1348 }
1349
1350 void (set_method_additional_properties)(ir_type *method, unsigned mask) {
1351   _set_method_additional_properties(method, mask);
1352 }
1353
1354 void (set_method_additional_property)(ir_type *method, mtp_additional_property flag) {
1355   _set_method_additional_property(method, flag);
1356 }
1357
1358 /* Returns the calling convention of an entities graph. */
1359 unsigned (get_method_calling_convention)(const ir_type *method) {
1360   return _get_method_calling_convention(method);
1361 }
1362
1363 /* Sets the calling convention of an entities graph. */
1364 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask) {
1365   _set_method_calling_convention(method, cc_mask);
1366 }
1367
1368 /* Returns the number of registers parameters, 0 means default. */
1369 unsigned get_method_n_regparams(ir_type *method) {
1370   unsigned cc = get_method_calling_convention(method);
1371   assert(IS_FASTCALL(cc));
1372
1373   return cc & ~cc_bits;
1374 }
1375
1376 /* Sets the number of registers parameters, 0 means default. */
1377 void set_method_n_regparams(ir_type *method, unsigned n_regs) {
1378   unsigned cc = get_method_calling_convention(method);
1379   assert(IS_FASTCALL(cc));
1380
1381   set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1382 }
1383
1384 /* typecheck */
1385 int (is_Method_type)(const ir_type *method) {
1386   return _is_method_type(method);
1387 }
1388
1389 /*-----------------------------------------------------------------*/
1390 /* TYPE_UNION                                                      */
1391 /*-----------------------------------------------------------------*/
1392
1393 /* create a new type uni */
1394 ir_type *new_d_type_union(ident *name, dbg_info *db) {
1395   ir_type *res = new_type(type_union, NULL, name, db);
1396
1397   res->attr.ua.members = NEW_ARR_F(entity *, 0);
1398   hook_new_type(res);
1399   return res;
1400 }
1401
1402 ir_type *new_type_union(ident *name) {
1403   return new_d_type_union(name, NULL);
1404 }
1405
1406 void free_union_entities(ir_type *uni) {
1407   int i;
1408   assert(uni && (uni->type_op == type_union));
1409   for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1410     free_entity(get_union_member(uni, i));
1411 }
1412
1413 void free_union_attrs (ir_type *uni) {
1414   assert(uni && (uni->type_op == type_union));
1415   DEL_ARR_F(uni->attr.ua.members);
1416 }
1417
1418 /* manipulate private fields of union */
1419 int    get_union_n_members      (const ir_type *uni) {
1420   assert(uni && (uni->type_op == type_union));
1421   return (ARR_LEN (uni->attr.ua.members));
1422 }
1423 void    add_union_member   (ir_type *uni, entity *member) {
1424   assert(uni && (uni->type_op == type_union));
1425   assert(uni != get_entity_type(member) && "recursive type");
1426   ARR_APP1 (entity *, uni->attr.ua.members, member);
1427 }
1428 entity  *get_union_member (const ir_type *uni, int pos) {
1429   assert(uni && (uni->type_op == type_union));
1430   assert(pos >= 0 && pos < get_union_n_members(uni));
1431   return uni->attr.ua.members[pos];
1432 }
1433 int     get_union_member_index(const ir_type *uni, entity *mem) {
1434   int i, n;
1435   assert(uni && (uni->type_op == type_union));
1436   for (i = 0, n = get_union_n_members(uni); i < n; ++i)
1437     if (get_union_member(uni, i) == mem)
1438       return i;
1439   return -1;
1440 }
1441 void   set_union_member (ir_type *uni, int pos, entity *member) {
1442   assert(uni && (uni->type_op == type_union));
1443   assert(pos >= 0 && pos < get_union_n_members(uni));
1444   uni->attr.ua.members[pos] = member;
1445 }
1446 void   remove_union_member(ir_type *uni, entity *member) {
1447   int i;
1448   assert(uni && (uni->type_op == type_union));
1449   for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1450     if (uni->attr.ua.members[i] == member) {
1451       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1452         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1453       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1454       break;
1455     }
1456 }
1457
1458 /* typecheck */
1459 int (is_Union_type)(const ir_type *uni) {
1460   return _is_union_type(uni);
1461 }
1462
1463 void set_union_size_bits(ir_type *tp, int size) {
1464   /* argh: we must allow to set negative values as "invalid size" */
1465   tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1466   assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1467 }
1468
1469 /*-----------------------------------------------------------------*/
1470 /* TYPE_ARRAY                                                      */
1471 /*-----------------------------------------------------------------*/
1472
1473
1474 /* create a new type array -- set dimension sizes independently */
1475 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type, dbg_info *db) {
1476   ir_type *res;
1477   int i;
1478   ir_node *unk;
1479   ir_graph *rem = current_ir_graph;
1480
1481   assert(!is_Method_type(element_type));
1482
1483   res = new_type(type_array, NULL, name, db);
1484   res->attr.aa.n_dimensions = n_dimensions;
1485   res->attr.aa.lower_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1486   res->attr.aa.upper_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1487   res->attr.aa.order        = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1488
1489   current_ir_graph = get_const_code_irg();
1490   unk = new_Unknown( mode_Iu);
1491   for (i = 0; i < n_dimensions; i++) {
1492     res->attr.aa.lower_bound[i] =
1493     res->attr.aa.upper_bound[i] = unk;
1494     res->attr.aa.order[i]       = i;
1495   }
1496   current_ir_graph = rem;
1497
1498   res->attr.aa.element_type = element_type;
1499   new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1500   hook_new_type(res);
1501   return res;
1502 }
1503
1504 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type) {
1505   return new_d_type_array(name, n_dimensions, element_type, NULL);
1506 }
1507
1508 void free_array_automatic_entities(ir_type *array) {
1509   assert(array && (array->type_op == type_array));
1510   free_entity(get_array_element_entity(array));
1511 }
1512
1513 void free_array_entities (ir_type *array) {
1514   assert(array && (array->type_op == type_array));
1515 }
1516
1517 void free_array_attrs (ir_type *array) {
1518   assert(array && (array->type_op == type_array));
1519   free(array->attr.aa.lower_bound);
1520   free(array->attr.aa.upper_bound);
1521 }
1522
1523 /* manipulate private fields of array ir_type */
1524 int   get_array_n_dimensions (const ir_type *array) {
1525   assert(array && (array->type_op == type_array));
1526   return array->attr.aa.n_dimensions;
1527 }
1528
1529 void
1530 set_array_bounds (ir_type *array, int dimension, ir_node * lower_bound,
1531           ir_node * upper_bound) {
1532   assert(array && (array->type_op == type_array));
1533   assert(lower_bound && "lower_bound node may not be NULL.");
1534   assert(upper_bound && "upper_bound node may not be NULL.");
1535   assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1536   array->attr.aa.lower_bound[dimension] = lower_bound;
1537   array->attr.aa.upper_bound[dimension] = upper_bound;
1538 }
1539 void
1540 set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
1541               int upper_bound) {
1542   ir_graph *rem = current_ir_graph;
1543   current_ir_graph = get_const_code_irg();
1544   set_array_bounds (array, dimension,
1545             new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1546             new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1547   current_ir_graph = rem;
1548 }
1549 void
1550 set_array_lower_bound  (ir_type *array, int dimension, ir_node * lower_bound) {
1551   assert(array && (array->type_op == type_array));
1552   assert(lower_bound && "lower_bound node may not be NULL.");
1553   array->attr.aa.lower_bound[dimension] = lower_bound;
1554 }
1555 void  set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound) {
1556   ir_graph *rem = current_ir_graph;
1557   current_ir_graph = get_const_code_irg();
1558   set_array_lower_bound  (array, dimension,
1559               new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1560   current_ir_graph = rem;
1561 }
1562 void
1563 set_array_upper_bound  (ir_type *array, int dimension, ir_node * upper_bound) {
1564   assert(array && (array->type_op == type_array));
1565   assert(upper_bound && "upper_bound node may not be NULL.");
1566   array->attr.aa.upper_bound[dimension] = upper_bound;
1567 }
1568 void  set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound) {
1569   ir_graph *rem = current_ir_graph;
1570   current_ir_graph = get_const_code_irg();
1571   set_array_upper_bound  (array, dimension,
1572               new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1573   current_ir_graph = rem;
1574 }
1575 int      has_array_lower_bound  (const ir_type *array, int dimension) {
1576   assert(array && (array->type_op == type_array));
1577   return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1578 }
1579 ir_node *get_array_lower_bound  (const ir_type *array, int dimension) {
1580   assert(array && (array->type_op == type_array));
1581   return array->attr.aa.lower_bound[dimension];
1582 }
1583 long     get_array_lower_bound_int  (const ir_type *array, int dimension) {
1584   ir_node *node;
1585   assert(array && (array->type_op == type_array));
1586   node = array->attr.aa.lower_bound[dimension];
1587   assert(get_irn_op(node) == op_Const);
1588   return get_tarval_long(get_Const_tarval(node));
1589 }
1590 int       has_array_upper_bound  (const ir_type *array, int dimension) {
1591   assert(array && (array->type_op == type_array));
1592   return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1593 }
1594 ir_node * get_array_upper_bound  (const ir_type *array, int dimension) {
1595   assert(array && (array->type_op == type_array));
1596   return array->attr.aa.upper_bound[dimension];
1597 }
1598 long     get_array_upper_bound_int  (const ir_type *array, int dimension) {
1599   ir_node *node;
1600   assert(array && (array->type_op == type_array));
1601   node = array->attr.aa.upper_bound[dimension];
1602   assert(get_irn_op(node) == op_Const);
1603   return get_tarval_long(get_Const_tarval(node));
1604 }
1605
1606 void set_array_order (ir_type *array, int dimension, int order) {
1607   assert(array && (array->type_op == type_array));
1608   array->attr.aa.order[dimension] = order;
1609 }
1610
1611 int  get_array_order (const ir_type *array, int dimension) {
1612   assert(array && (array->type_op == type_array));
1613   return array->attr.aa.order[dimension];
1614 }
1615
1616 int find_array_dimension(const ir_type *array, int order) {
1617   int dim;
1618
1619   assert(array && (array->type_op == type_array));
1620
1621   for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1622     if (array->attr.aa.order[dim] == order)
1623       return dim;
1624   }
1625   return -1;
1626 }
1627
1628 void  set_array_element_type (ir_type *array, ir_type *tp) {
1629   assert(array && (array->type_op == type_array));
1630   assert(!is_Method_type(tp));
1631   array->attr.aa.element_type = tp;
1632 }
1633 ir_type *get_array_element_type (ir_type *array) {
1634   assert(array && (array->type_op == type_array));
1635   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1636 }
1637
1638 void  set_array_element_entity (ir_type *array, entity *ent) {
1639   assert(array && (array->type_op == type_array));
1640   assert((get_entity_type(ent)->type_op != type_method));
1641   array->attr.aa.element_ent = ent;
1642   array->attr.aa.element_type = get_entity_type(ent);
1643 }
1644 entity *get_array_element_entity (const ir_type *array) {
1645   assert(array && (array->type_op == type_array));
1646   return array->attr.aa.element_ent;
1647 }
1648
1649 /* typecheck */
1650 int (is_Array_type)(const ir_type *array) {
1651   return _is_array_type(array);
1652 }
1653
1654 void set_array_size_bits(ir_type *tp, int size) {
1655   /* FIXME: Here we should make some checks with the element type size */
1656   tp->size = size;
1657 }
1658 /*-----------------------------------------------------------------*/
1659 /* TYPE_ENUMERATION                                                */
1660 /*-----------------------------------------------------------------*/
1661
1662 /* create a new type enumeration -- set the enumerators independently */
1663 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1664   ir_type *res = new_type(type_enumeration, NULL, name, db);
1665
1666   res->attr.ea.n_enums     = n_enums;
1667   res->attr.ea.enumer      = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1668   res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1669   hook_new_type(res);
1670   return res;
1671 }
1672
1673 ir_type *new_type_enumeration(ident *name, int n_enums) {
1674   return new_d_type_enumeration(name, n_enums, NULL);
1675 }
1676
1677 void free_enumeration_entities(ir_type *enumeration) {
1678   assert(enumeration && (enumeration->type_op == type_enumeration));
1679 }
1680 void free_enumeration_attrs(ir_type *enumeration) {
1681   assert(enumeration && (enumeration->type_op == type_enumeration));
1682   free(enumeration->attr.ea.enumer);
1683   free(enumeration->attr.ea.enum_nameid);
1684 }
1685
1686 /* manipulate fields of enumeration type. */
1687 int     get_enumeration_n_enums (const ir_type *enumeration) {
1688   assert(enumeration && (enumeration->type_op == type_enumeration));
1689   return enumeration->attr.ea.n_enums;
1690 }
1691 void    set_enumeration_enum    (ir_type *enumeration, int pos, tarval *con) {
1692   assert(enumeration && (enumeration->type_op == type_enumeration));
1693   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1694   enumeration->attr.ea.enumer[pos] = con;
1695 }
1696 tarval *get_enumeration_enum    (const ir_type *enumeration, int pos) {
1697   assert(enumeration && (enumeration->type_op == type_enumeration));
1698   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1699   return enumeration->attr.ea.enumer[pos];
1700 }
1701 void    set_enumeration_nameid  (ir_type *enumeration, int pos, ident *id) {
1702   assert(enumeration && (enumeration->type_op == type_enumeration));
1703   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1704   enumeration->attr.ea.enum_nameid[pos] = id;
1705 }
1706 ident  *get_enumeration_nameid  (const ir_type *enumeration, int pos) {
1707   assert(enumeration && (enumeration->type_op == type_enumeration));
1708   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1709   return enumeration->attr.ea.enum_nameid[pos];
1710 }
1711 const char *get_enumeration_name(const ir_type *enumeration, int pos) {
1712   assert(enumeration && (enumeration->type_op == type_enumeration));
1713   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1714   return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1715 }
1716
1717 /* typecheck */
1718 int (is_Enumeration_type)(const ir_type *enumeration) {
1719   return _is_enumeration_type(enumeration);
1720 }
1721
1722 void set_enumeration_mode(ir_type *tp, ir_mode *mode) {
1723   assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1724   /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1725   assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1726
1727   tp->size = get_mode_size_bits(mode);
1728   tp->mode = mode;
1729 }
1730
1731 /*-----------------------------------------------------------------*/
1732 /* TYPE_POINTER                                                    */
1733 /*-----------------------------------------------------------------*/
1734
1735 /* Create a new type pointer */
1736 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1737   ir_type *res;
1738
1739   assert(mode_is_reference(ptr_mode));
1740   res = new_type(type_pointer, ptr_mode, name, db);
1741   res->attr.pa.points_to = points_to;
1742   assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1743   res->size = get_mode_size_bits(res->mode);
1744   res->flags |= tf_layout_fixed;
1745   hook_new_type(res);
1746   return res;
1747 }
1748
1749 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode) {
1750   return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1751 }
1752
1753 void free_pointer_entities (ir_type *pointer) {
1754   assert(pointer && (pointer->type_op == type_pointer));
1755 }
1756
1757 void free_pointer_attrs (ir_type *pointer) {
1758   assert(pointer && (pointer->type_op == type_pointer));
1759 }
1760
1761 /* manipulate fields of type_pointer */
1762 void  set_pointer_points_to_type (ir_type *pointer, ir_type *tp) {
1763   assert(pointer && (pointer->type_op == type_pointer));
1764   pointer->attr.pa.points_to = tp;
1765 }
1766
1767 ir_type *get_pointer_points_to_type (ir_type *pointer) {
1768   assert(pointer && (pointer->type_op == type_pointer));
1769   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1770 }
1771
1772 /* typecheck */
1773 int (is_Pointer_type)(const ir_type *pointer) {
1774   return _is_pointer_type(pointer);
1775 }
1776
1777 void set_pointer_mode(ir_type *tp, ir_mode *mode) {
1778   assert(mode_is_reference(mode) && "Modes of pointers must be references");
1779   /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1780   assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1781
1782   tp->size = get_mode_size_bits(mode);
1783   tp->mode = mode;
1784 }
1785
1786 /* Returns the first pointer type that has as points_to tp.
1787  *  Not efficient: O(#types).
1788  *  If not found returns firm_unknown_type. */
1789 ir_type *find_pointer_type_to_type (ir_type *tp) {
1790   int i;
1791   for (i = 0; i < get_irp_n_types(); ++i) {
1792     ir_type *found = get_irp_type(i);
1793     if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1794       return (found);
1795   }
1796   return firm_unknown_type;
1797 }
1798
1799
1800 /*-----------------------------------------------------------------*/
1801 /* TYPE_PRIMITIVE                                                  */
1802 /*-----------------------------------------------------------------*/
1803
1804 /* create a new type primitive */
1805 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1806   ir_type *res;
1807   /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1808   res = new_type(type_primitive, mode, name, db);
1809   res->size  = get_mode_size_bits(mode);
1810   res->flags |= tf_layout_fixed;
1811   hook_new_type(res);
1812   return res;
1813 }
1814
1815 ir_type *new_type_primitive(ident *name, ir_mode *mode) {
1816   return new_d_type_primitive(name, mode, NULL);
1817 }
1818
1819 /* typecheck */
1820 int (is_Primitive_type)(const ir_type *primitive) {
1821   return _is_primitive_type(primitive);
1822 }
1823
1824 void set_primitive_mode(ir_type *tp, ir_mode *mode) {
1825   /* Modes of primitives must be data */
1826   assert(mode_is_data(mode));
1827
1828   /* For primitive size depends on the mode. */
1829   tp->size = get_mode_size_bits(mode);
1830   tp->mode = mode;
1831 }
1832
1833
1834 /*-----------------------------------------------------------------*/
1835 /* common functionality                                            */
1836 /*-----------------------------------------------------------------*/
1837
1838
1839 int (is_atomic_type)(const ir_type *tp) {
1840   return _is_atomic_type(tp);
1841 }
1842
1843 /*
1844  * Gets the number of elements in a firm compound type.
1845  */
1846 int get_compound_n_members(const ir_type *tp)
1847 {
1848   const tp_op *op = get_type_tpop(tp);
1849   int res = 0;
1850
1851   if (op->ops.get_n_members)
1852     res = op->ops.get_n_members(tp);
1853   else
1854     assert(0 && "no member count for this type");
1855
1856   return res;
1857 }
1858
1859 /*
1860  * Gets the member of a firm compound type at position pos.
1861  */
1862 entity *get_compound_member(const ir_type *tp, int pos)
1863 {
1864   const tp_op *op = get_type_tpop(tp);
1865   entity *res = NULL;
1866
1867   if (op->ops.get_member)
1868     res = op->ops.get_member(tp, pos);
1869   else
1870     assert(0 && "no members in this type");
1871
1872   return res;
1873 }
1874
1875 /* Returns index of member in tp, -1 if not contained. */
1876 int get_compound_member_index(const ir_type *tp, entity *member)
1877 {
1878   const tp_op *op = get_type_tpop(tp);
1879   int index = -1;
1880
1881   if (op->ops.get_member_index)
1882     index = op->ops.get_member_index(tp, member);
1883   else
1884     assert(0 && "no members in this type");
1885
1886   return index;
1887 }
1888
1889 int is_compound_type(const ir_type *tp) {
1890   assert(tp && tp->kind == k_type);
1891   return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1892 }
1893
1894 /* Checks, whether a type is a frame ir_type */
1895 int is_frame_type(const ir_type *tp) {
1896   return tp->flags & tf_frame_type;
1897 }
1898
1899 /* Checks, whether a type is a lowered ir_type */
1900 int is_lowered_type(const ir_type *tp) {
1901   return tp->flags & tf_lowered_type;
1902 }
1903
1904 /* Makes a new frame type. */
1905 ir_type *new_type_frame(ident *name)
1906 {
1907   ir_type *res = new_type_class(name);
1908
1909   res->flags |= tf_frame_type;
1910
1911   /* Remove type from type list.  Must be treated differently than other types. */
1912   remove_irp_type(res);
1913
1914   return res;
1915 }
1916
1917 /* Sets a lowered type for a type. This sets both associations. */
1918 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
1919   assert(is_type(tp) && is_type(lowered_type));
1920   lowered_type->flags |= tf_lowered_type;
1921   tp->assoc_type = lowered_type;
1922   lowered_type->assoc_type = tp;
1923 }
1924
1925 /*
1926  * Gets the lowered/unlowered type of a type or NULL if this type
1927  * has no lowered/unlowered one.
1928  */
1929 ir_type *get_associated_type(const ir_type *tp) {
1930   return tp->assoc_type;
1931 }
1932
1933 /* set the type size for the unknown and none ir_type */
1934 void set_default_size_bits(ir_type *tp, int size) {
1935   tp->size = size;
1936 }