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