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