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