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