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