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