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