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