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