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