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