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