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