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