Bugfix
[libfirm] / ir / tr / type.c
1 /****h* libfirm/type.c
2  *
3  * NAME
4  *   file type.c - implementation of the datastructure to hold
5  *   type information.
6  * COPYRIGHT
7  *  (C) 2001 by Universitaet Karlsruhe
8  * AUTHORS
9  *  Martin Trapp, Christian Schaefer, Goetz Lindenmaier
10  *
11  * NOTES
12  *  This module supplies a datastructure to represent all types
13  *  known in the compiled program.  This includes types specified
14  *  in the program as well as types defined by the language.  In the
15  *  view of the intermediate representation there is no difference
16  *  between these types.
17  *
18  *  There exist several kinds of types, arranged by the structure of
19  *  the type.  A type is described by a set of attributes.  Some of
20  *  these attributes are common to all types, others depend on the
21  *  kind of the type.
22  *
23  *  Types are different from the modes defined in irmode:  Types are
24  *  on the level of the programming language, modes at the level of
25  *  the target processor.
26  *
27  * SEE ALSO
28  *   type_t.h type tpop
29  *****
30  */
31
32 /* $Id$ */
33
34 # include <stdlib.h>
35 # include <stddef.h>
36 # include <string.h>
37 # include "type_t.h"
38 # include "tpop_t.h"
39 # include "typegmod_t.h"
40 # include "array.h"
41 # include "irprog.h"
42 # include "mangle.h"
43 # include "tv.h"
44 # include "ircons.h"
45
46 /*******************************************************************/
47 /** TYPE                                                          **/
48 /*******************************************************************/
49
50 unsigned long type_visited;
51 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
52 INLINE unsigned long get_master_type_visited() { return type_visited; }
53 INLINE void inc_master_type_visited() { type_visited++; }
54
55 void        free_type(type *tp) {
56   /* Remove from list of all types */
57   remove_irp_type(tp);
58   /* Free the attributes of the type. */
59   free_type_attrs(tp);
60   /* Free entities automatically allocated with the type */
61   if (is_array_type(tp))
62     free_entity(get_array_element_entity(tp));
63   /* And now the type itself... */
64   free(tp);
65 }
66
67 INLINE type *
68 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
69   type *res;
70   int node_size ;
71
72   assert(type_op != type_id);
73
74   node_size = offsetof (type, attr) +  type_op->attr_size;
75   res = (type *) xmalloc (node_size);
76   add_irp_type(res);   /* Remember the new type global. */
77
78   res->kind = k_type;
79   res->type_op = type_op;
80   res->mode = mode;
81   res->name = name;
82   res->state = layout_undefined;
83   res->size = -1;
84   res->visit = 0;
85   res -> link = NULL;
86
87   return res;
88 }
89
90 void free_type_attrs(type *tp) {
91   switch(get_type_tpop_code(tp)) {
92   case tpo_class:       { free_class_attrs(tp);       } break;
93   case tpo_struct:      { free_struct_attrs(tp);      } break;
94   case tpo_method:      { free_method_attrs(tp);      } break;
95   case tpo_union:       { free_union_attrs(tp);       } break;
96   case tpo_array:       { free_array_attrs(tp);       } break;
97   case tpo_enumeration: { free_enumeration_attrs(tp); } break;
98   case tpo_pointer:     { free_pointer_attrs(tp);     } break;
99   case tpo_primitive:   { free_primitive_attrs(tp);   } break;
100   default: break;
101   }
102 }
103
104 /* set/get the link field */
105 void *get_type_link(type *tp)
106 {
107   assert(tp && tp->kind == k_type);
108   return(tp -> link);
109 }
110
111 void set_type_link(type *tp, void *l)
112 {
113   assert(tp && tp->kind == k_type);
114   tp -> link = l;
115 }
116
117 tp_op*      get_type_tpop(type *tp) {
118   assert(tp && tp->kind == k_type);
119   return tp->type_op;
120 }
121
122 ident*      get_type_tpop_nameid(type *tp) {
123   assert(tp && tp->kind == k_type);
124   return tp->type_op->name;
125 }
126
127 const char* get_type_tpop_name(type *tp) {
128   assert(tp && tp->kind == k_type);
129   return id_to_str(tp->type_op->name);
130 }
131
132 tp_opcode    get_type_tpop_code(type *tp) {
133   assert(tp && tp->kind == k_type);
134   return tp->type_op->code;
135 }
136
137 ir_mode*    get_type_mode(type *tp) {
138   assert(tp && tp->kind == k_type);
139   return tp->mode;
140 }
141
142 void        set_type_mode(type *tp, ir_mode* m) {
143   assert(tp && tp->kind == k_type);
144
145   assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
146          /* Modes of primitives must be data */
147          ((tp->type_op != type_enumeration) || mode_is_int(m)));
148          /* Modes of enumerations must be integers */
149
150   if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
151     /* For pointer, primitive and enumeration size depends on the mode. */
152     tp->size = get_mode_size(m);
153     tp->mode = m;
154   }
155 }
156
157 ident*      get_type_ident(type *tp) {
158   assert(tp && tp->kind == k_type);
159   return tp->name;
160 }
161
162 void        set_type_ident(type *tp, ident* id) {
163   assert(tp && tp->kind == k_type);
164   tp->name = id;
165 }
166
167 const char* get_type_name(type *tp) {
168   assert(tp && tp->kind == k_type);
169   return (id_to_str(tp->name));
170 }
171
172 int         get_type_size(type *tp) {
173   assert(tp && tp->kind == k_type);
174   return tp->size;
175 }
176
177 void
178 set_type_size(type *tp, int size) {
179   assert(tp && tp->kind == k_type);
180   /* For pointer enumeration and primitive size depends on the mode.
181      Methods don't have a size. */
182   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
183       (tp->type_op != type_enumeration) && (tp->type_op != type_method))
184     tp->size = size;
185 }
186
187 type_state
188 get_type_state(type *tp) {
189   assert(tp && tp->kind == k_type);
190   return tp->state;
191 }
192
193 void
194 set_type_state(type *tp, type_state state) {
195   assert(tp && tp->kind == k_type);
196
197   if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
198       (tp->type_op == type_method))
199     return;
200
201   /* Just a correctness check: */
202   if (state == layout_fixed) {
203     int i;
204     switch (get_type_tpop_code(tp)) {
205     case tpo_class:
206       {
207         assert(get_type_size(tp) > -1);
208         if (tp != get_glob_type())
209           for (i = 0; i < get_class_n_members(tp); i++) {
210             assert(get_entity_offset(get_class_member(tp, i)) > -1);
211             assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
212                    (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
213             /*    @@@ lowerfirm geht nicht durch */
214           }
215       } break;
216     case tpo_struct:
217       {
218         /* assert(get_type_size(tp) > -1);    @@@ lowerfirm geht nicht durch */
219         for (i = 0; i < get_struct_n_members(tp); i++) {
220           assert(get_entity_offset(get_struct_member(tp, i)) > -1);
221           assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
222         }
223       } break;
224     case tpo_union:
225       { /* ?? */
226       } break;
227     case tpo_array:
228       { /* ??
229          Check order?
230          Assure that only innermost dimension is dynamic? */
231       } break;
232     case tpo_enumeration:
233       {
234         assert(get_type_mode != NULL);
235         for (i = 0; i < get_enumeration_n_enums(tp); i++)
236           assert(get_enumeration_enum(tp, i) != NULL);
237       } break;
238     default: break;
239     } /* switch (tp) */
240   }
241   tp->state = state;
242 }
243
244 unsigned long get_type_visited(type *tp) {
245   assert(tp && tp->kind == k_type);
246   return tp->visit;
247 }
248
249 void        set_type_visited(type *tp, unsigned long num) {
250   assert(tp && tp->kind == k_type);
251   tp->visit = num;
252 }
253 /* Sets visited field in type to type_visited. */
254 void        mark_type_visited(type *tp) {
255   assert(tp && tp->kind == k_type);
256   assert(tp->visit < type_visited);
257   tp->visit = type_visited;
258 }
259
260 int is_type            (void *thing) {
261   assert(thing);
262   if (get_kind(thing) == k_type)
263     return 1;
264   else
265     return 0;
266 }
267
268
269 bool equal_type(type *typ1, type *typ2) {
270   entity **m;
271   type **t;
272   int i, j;
273
274   if (typ1 == typ2) return true;
275
276   if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
277       (get_type_name(typ1) != get_type_name(typ2)) ||
278       (get_type_mode(typ1) != get_type_mode(typ2)) ||
279       (get_type_state(typ1) != get_type_state(typ2)))
280     return false;
281   if ((get_type_state(typ1) == layout_fixed) &&
282       (get_type_size(typ1) != get_type_size(typ2)))
283     return false;
284
285   switch(get_type_tpop_code(typ1)) {
286   case tpo_class:       {
287     if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
288     if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
289     if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
290     if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
291     /** Compare the members **/
292     m = alloca(sizeof(entity *) * get_class_n_members(typ1));
293     memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
294     /* First sort the members of typ2 */
295     for (i = 0; i < get_class_n_members(typ1); i++) {
296       entity *e1 = get_class_member(typ1, i);
297       for (j = 0; j < get_class_n_members(typ2); j++) {
298         entity *e2 = get_class_member(typ2, j);
299         if (get_entity_name(e1) == get_entity_name(e2))
300           m[i] = e2;
301       }
302     }
303     for (i = 0; i < get_class_n_members(typ1); i++) {
304       if (!m[i]  ||  /* Found no counterpart */
305           !equal_entity(get_class_member(typ1, i), m[i]))
306         return false;
307     }
308     /** Compare the supertypes **/
309     t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
310     memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
311     /* First sort the supertypes of typ2 */
312     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
313       type *t1 = get_class_supertype(typ1, i);
314       for (j = 0; j < get_class_n_supertypes(typ2); j++) {
315         type *t2 = get_class_supertype(typ2, j);
316         if (get_type_name(t2) == get_type_name(t1))
317           t[i] = t2;
318       }
319     }
320     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
321       if (!t[i]  ||  /* Found no counterpart */
322           get_class_supertype(typ1, i) != t[i])
323         return false;
324     }
325   } break;
326   case tpo_struct:      {
327     if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
328     m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
329     memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
330     /* First sort the members of lt */
331     for (i = 0; i < get_struct_n_members(typ1); i++) {
332       entity *e1 = get_struct_member(typ1, i);
333       for (j = 0; j < get_struct_n_members(typ2); j++) {
334         entity *e2 = get_struct_member(typ2, j);
335         if (get_entity_name(e1) == get_entity_name(e2))
336           m[i] = e2;
337       }
338     }
339     for (i = 0; i < get_struct_n_members(typ1); i++) {
340       if (!m[i]  ||  /* Found no counterpart */
341           !equal_entity(get_struct_member(typ1, i), m[i]))
342         return false;
343     }
344   } break;
345   case tpo_method:      {
346     if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
347     if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
348     for (i = 0; i < get_method_n_params(typ1); i++) {
349       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
350         return false;
351     }
352     for (i = 0; i < get_method_n_ress(typ1); i++) {
353       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
354         return false;
355     }
356   } break;
357   case tpo_union:       {
358     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
359     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
360     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
361     /* First sort the members of lt */
362     for (i = 0; i < get_union_n_members(typ1); i++) {
363       entity *e1 = get_union_member(typ1, i);
364       for (j = 0; j < get_union_n_members(typ2); j++) {
365         entity *e2 = get_union_member(typ2, j);
366         if (get_entity_name(e1) == get_entity_name(e2))
367           m[i] = e2;
368       }
369     }
370     for (i = 0; i < get_union_n_members(typ1); i++) {
371       if (!m[i]  ||  /* Found no counterpart */
372           !equal_entity(get_union_member(typ1, i), m[i]))
373         return false;
374     }
375   } break;
376   case tpo_array:       {
377     type *set, *let;  /* small/large elt. type */
378     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
379       return false;
380     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
381       return false;
382     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
383       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
384           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
385         return false;
386       if (get_array_order(typ1, i) != get_array_order(typ2, i))
387         assert(0 && "type compare with different dimension orders not implemented");
388     }
389   } break;
390   case tpo_enumeration: {
391     assert(0 && "enumerations not implemented");
392   } break;
393   case tpo_pointer:     {
394     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
395       return false;
396   } break;
397   case tpo_primitive:   {
398   } break;
399   default: break;
400   }
401   return true;
402 }
403
404 bool smaller_type (type *st, type *lt) {
405   entity **m;
406   int i, j;
407
408   if (st == lt) return true;
409
410   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
411     return false;
412
413   switch(get_type_tpop_code(st)) {
414   case tpo_class:       {
415     return is_subclass_of(st, lt);
416   } break;
417   case tpo_struct:      {
418     if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
419     m = alloca(sizeof(entity *) * get_struct_n_members(st));
420     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
421     /* First sort the members of lt */
422     for (i = 0; i < get_struct_n_members(st); i++) {
423       entity *se = get_struct_member(st, i);
424       for (j = 0; j < get_struct_n_members(lt); j++) {
425         entity *le = get_struct_member(lt, j);
426         if (get_entity_name(le) == get_entity_name(se))
427           m[i] = le;
428       }
429     }
430     for (i = 0; i < get_struct_n_members(st); i++) {
431       if (!m[i]  ||  /* Found no counterpart */
432           !smaller_type(get_entity_type(get_struct_member(st, i)),
433                         get_entity_type(m[i])))
434         return false;
435     }
436   } break;
437   case tpo_method:      {
438     if (get_method_n_params(st) != get_method_n_params(lt)) return false;
439     if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
440     for (i = 0; i < get_method_n_params(st); i++) {
441       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
442         return false;
443     }
444     for (i = 0; i < get_method_n_ress(st); i++) {
445       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
446         return false;
447     }
448   } break;
449   case tpo_union:       {
450     if (get_union_n_members(st) != get_union_n_members(lt)) return false;
451     m = alloca(sizeof(entity *) * get_union_n_members(st));
452     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
453     /* First sort the members of lt */
454     for (i = 0; i < get_union_n_members(st); i++) {
455       entity *se = get_union_member(st, i);
456       for (j = 0; j < get_union_n_members(lt); j++) {
457         entity *le = get_union_member(lt, j);
458         if (get_entity_name(le) == get_entity_name(se))
459           m[i] = le;
460       }
461     }
462     for (i = 0; i < get_union_n_members(st); i++) {
463       if (!m[i]  ||  /* Found no counterpart */
464           !smaller_type(get_entity_type(get_union_member(st, i)),
465                         get_entity_type(m[i])))
466         return false;
467     }
468   } break;
469   case tpo_array:       {
470     type *set, *let;  /* small/large elt. type */
471     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
472       return false;
473     set = get_array_element_type(st);
474     let = get_array_element_type(lt);
475     if (set != let) {
476       /* If the elt types are different, set must be convertible
477          to let, and they must have the same size so that address
478          computations work out.  To have a size the layout must
479          be fixed. */
480       if ((get_type_state(set) != layout_fixed) ||
481           (get_type_state(let) != layout_fixed))
482         return false;
483       if (!smaller_type(set, let) ||
484           get_type_size(set) != get_type_size(let))
485         return false;
486     }
487     for(i = 0; i < get_array_n_dimensions(st); i++) {
488       if (get_array_lower_bound(lt, i))
489         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
490           return false;
491       if (get_array_upper_bound(lt, i))
492         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
493           return false;
494     }
495   } break;
496   case tpo_enumeration: {
497     assert(0 && "enumerations not implemented");
498   } break;
499   case tpo_pointer:     {
500     if (!smaller_type(get_pointer_points_to_type(st),
501                       get_pointer_points_to_type(lt)))
502       return false;
503   } break;
504   case tpo_primitive:   {
505     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
506       return false;
507   } break;
508   default: break;
509   }
510   return true;
511 }
512
513 /*******************************************************************/
514 /** TYPE_CLASS                                                    **/
515 /*******************************************************************/
516
517 /* create a new class type */
518 INLINE type   *new_type_class (ident *name) {
519   type *res;
520
521   res = new_type(type_class, NULL, name);
522
523   res->attr.ca.members    = NEW_ARR_F (entity *, 1);
524   res->attr.ca.subtypes   = NEW_ARR_F (type *, 1);
525   res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
526   res->attr.ca.peculiarity = existent;
527   res->attr.ca.dfn        = 0;
528
529   return res;
530 }
531 type   *new_d_type_class (ident *name, dbg_info* db) {
532   type *res = new_type_class (name);
533   set_type_dbg_info(res, db);
534   return res;
535 }
536 INLINE void free_class_attrs(type *clss) {
537   assert(clss && (clss->type_op == type_class));
538   DEL_ARR_F(clss->attr.ca.members);
539   DEL_ARR_F(clss->attr.ca.subtypes);
540   DEL_ARR_F(clss->attr.ca.supertypes);
541 }
542
543 /* manipulate private fields of class type  */
544 void    add_class_member   (type *clss, entity *member) {
545   assert(clss && (clss->type_op == type_class));
546   ARR_APP1 (entity *, clss->attr.ca.members, member);
547 }
548 int     get_class_n_members (type *clss) {
549   assert(clss && (clss->type_op == type_class));
550   return (ARR_LEN (clss->attr.ca.members))-1;
551 }
552 entity *get_class_member   (type *clss, int pos) {
553   assert(clss && (clss->type_op == type_class));
554   assert(pos >= 0 && pos < get_class_n_members(clss));
555   return clss->attr.ca.members[pos+1];
556 }
557 void    set_class_member   (type *clss, entity *member, int pos) {
558   assert(clss && (clss->type_op == type_class));
559   assert(pos >= 0 && pos < get_class_n_members(clss));
560   clss->attr.ca.members[pos+1] = member;
561 }
562 void    set_class_members  (type *clss, entity **members, int arity) {
563   int i;
564   assert(clss && (clss->type_op == type_class));
565   DEL_ARR_F(clss->attr.ca.members);
566   clss->attr.ca.members    = NEW_ARR_F (entity *, 1);
567   for (i = 0; i < arity; i++) {
568     set_entity_owner(members[i], clss);
569     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
570   }
571 }
572 void    remove_class_member(type *clss, entity *member) {
573   int i;
574   assert(clss && (clss->type_op == type_class));
575   for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
576     if (clss->attr.ca.members[i] == member) {
577       for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
578         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
579       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
580       break;
581     }
582   }
583 }
584
585 void    add_class_subtype   (type *clss, type *subtype) {
586   int i;
587   assert(clss && (clss->type_op == type_class));
588   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
589   for (i = 0; i < get_class_n_supertypes(subtype); i++)
590     if (get_class_supertype(subtype, i) == clss)
591       /* Class already registered */
592       return;
593   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
594 }
595 int     get_class_n_subtypes (type *clss) {
596   assert(clss && (clss->type_op == type_class));
597   return (ARR_LEN (clss->attr.ca.subtypes))-1;
598 }
599 type   *get_class_subtype   (type *clss, int pos) {
600   assert(clss && (clss->type_op == type_class));
601   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
602   return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
603 }
604 void    set_class_subtype   (type *clss, type *subtype, int pos) {
605   assert(clss && (clss->type_op == type_class));
606   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
607   clss->attr.ca.subtypes[pos+1] = subtype;
608 }
609 void    remove_class_subtype(type *clss, type *subtype) {
610   int i;
611   assert(clss && (clss->type_op == type_class));
612   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
613     if (clss->attr.ca.subtypes[i] == subtype) {
614       for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
615         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
616       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
617       break;
618     }
619 }
620
621 void    add_class_supertype   (type *clss, type *supertype) {
622   int i;
623   assert(clss && (clss->type_op == type_class));
624   assert(supertype && (supertype -> type_op == type_class));
625   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
626   for (i = 0; i < get_class_n_subtypes(supertype); i++)
627     if (get_class_subtype(supertype, i) == clss)
628       /* Class already registered */
629       return;
630   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
631 }
632 int     get_class_n_supertypes (type *clss) {
633   assert(clss && (clss->type_op == type_class));
634   return (ARR_LEN (clss->attr.ca.supertypes))-1;
635 }
636 type   *get_class_supertype   (type *clss, int pos) {
637   assert(clss && (clss->type_op == type_class));
638   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
639   return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
640 }
641 void    set_class_supertype   (type *clss, type *supertype, int pos) {
642   assert(clss && (clss->type_op == type_class));
643   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
644   clss->attr.ca.supertypes[pos+1] = supertype;
645 }
646 void    remove_class_supertype(type *clss, type *supertype) {
647   int i;
648   assert(clss && (clss->type_op == type_class));
649   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
650     if (clss->attr.ca.supertypes[i] == supertype) {
651       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
652         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
653       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
654       break;
655     }
656 }
657
658 INLINE peculiarity get_class_peculiarity (type *clss) {
659   assert(clss && (clss->type_op == type_class));
660   return clss->attr.ca.peculiarity;
661 }
662 INLINE void        set_class_peculiarity (type *clss, peculiarity pec) {
663   assert(clss && (clss->type_op == type_class));
664   assert(pec != inherited);  /* There is no inheritance of types in libFirm. */
665   clss->attr.ca.peculiarity = pec;
666 }
667
668 void set_class_dfn (type *clss, int dfn)
669 {
670   clss->attr.ca.dfn        = dfn;
671 }
672
673 int get_class_dfn (type *clss)
674 {
675   return (clss->attr.ca.dfn);
676 }
677
678 /* typecheck */
679 bool    is_class_type(type *clss) {
680   assert(clss);
681   if (clss->type_op == type_class) return 1; else return 0;
682 }
683
684 bool is_subclass_of(type *low, type *high) {
685   int i;
686   assert(is_class_type(low) && is_class_type(high));
687   if (low == high) return true;
688   /* depth first search from high downwards. */
689   for (i = 0; i < get_class_n_subtypes(high); i++) {
690     if (low == get_class_subtype(high, i))
691       return true;
692     if (is_subclass_of(low, get_class_subtype(high, i)))
693       return true;
694   }
695   return false;
696 }
697
698 /*******************************************************************/
699 /** TYPE_STRUCT                                                   **/
700 /*******************************************************************/
701
702 /* create a new type struct */
703 INLINE type   *new_type_struct (ident *name) {
704   type *res;
705   res = new_type(type_struct, NULL, name);
706   res->attr.sa.members = NEW_ARR_F (entity *, 1);
707   return res;
708 }
709 type   *new_d_type_struct (ident *name, dbg_info* db) {
710   type *res = new_type_struct (name);
711   set_type_dbg_info(res, db);
712   return res;
713 }
714 INLINE void free_struct_attrs (type *strct) {
715   assert(strct && (strct->type_op == type_struct));
716   DEL_ARR_F(strct->attr.sa.members);
717 }
718
719 /* manipulate private fields of struct */
720 int     get_struct_n_members (type *strct) {
721   assert(strct && (strct->type_op == type_struct));
722   return (ARR_LEN (strct->attr.sa.members))-1;
723 }
724 void    add_struct_member   (type *strct, entity *member) {
725   assert(strct && (strct->type_op == type_struct));
726   assert(get_type_tpop(get_entity_type(member)) != type_method);
727     /*    @@@ lowerfirm geht nicht durch */
728   ARR_APP1 (entity *, strct->attr.sa.members, member);
729 }
730 entity *get_struct_member   (type *strct, int pos) {
731   assert(strct && (strct->type_op == type_struct));
732   assert(pos >= 0 && pos < get_struct_n_members(strct));
733   return strct->attr.sa.members[pos+1];
734 }
735 void    set_struct_member   (type *strct, int pos, entity *member) {
736   assert(strct && (strct->type_op == type_struct));
737   assert(pos >= 0 && pos < get_struct_n_members(strct));
738   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
739   strct->attr.sa.members[pos+1] = member;
740 }
741 void    remove_struct_member(type *strct, entity *member) {
742   int i;
743   assert(strct && (strct->type_op == type_struct));
744   for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
745     if (strct->attr.sa.members[i] == member) {
746       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
747         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
748       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
749       break;
750     }
751 }
752
753 /* typecheck */
754 bool    is_struct_type(type *strct) {
755   assert(strct);
756   if (strct->type_op == type_struct) return 1; else return 0;
757 }
758
759 /*******************************************************************/
760 /** TYPE_METHOD                                                   **/
761 /*******************************************************************/
762
763 /* Create a new method type.
764    N_param is the number of parameters, n_res the number of results.  */
765 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
766   type *res;
767   res = new_type(type_method, mode_p, name);
768   res->state = layout_fixed;
769   res->size = get_mode_size(mode_p);
770   res->attr.ma.n_params   = n_param;
771   res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
772   res->attr.ma.n_res      = n_res;
773   res->attr.ma.res_type   = (type **) xmalloc (sizeof (type *) * n_res);
774   return res;
775 }
776 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
777   type *res = new_type_method (name, n_param, n_res);
778   set_type_dbg_info(res, db);
779   return res;
780 }
781 INLINE void free_method_attrs(type *method) {
782   assert(method && (method->type_op == type_method));
783   free(method->attr.ma.param_type);
784   free(method->attr.ma.res_type);
785 }
786 /* manipulate private fields of method. */
787 int   get_method_n_params  (type *method) {
788   assert(method && (method->type_op == type_method));
789   return method->attr.ma.n_params;
790 }
791 type *get_method_param_type(type *method, int pos) {
792   assert(method && (method->type_op == type_method));
793   assert(pos >= 0 && pos < get_method_n_params(method));
794   return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
795 }
796 void  set_method_param_type(type *method, int pos, type* type) {
797   assert(method && (method->type_op == type_method));
798   assert(pos >= 0 && pos < get_method_n_params(method));
799   method->attr.ma.param_type[pos] = type;
800 }
801
802 int   get_method_n_ress   (type *method) {
803   assert(method && (method->type_op == type_method));
804   return method->attr.ma.n_res;
805 }
806 type *get_method_res_type(type *method, int pos) {
807   assert(method && (method->type_op == type_method));
808   assert(pos >= 0 && pos < get_method_n_ress(method));
809   return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
810 }
811 void  set_method_res_type(type *method, int pos, type* type) {
812   assert(method && (method->type_op == type_method));
813   assert(pos >= 0 && pos < get_method_n_ress(method));
814   method->attr.ma.res_type[pos] = type;
815 }
816
817 /* typecheck */
818 bool  is_method_type     (type *method) {
819   assert(method);
820   if (method->type_op == type_method) return 1; else return 0;
821 }
822 /*****/
823
824 /*******************************************************************/
825 /** TYPE_UNION                                                    **/
826 /*******************************************************************/
827
828 /* create a new type uni */
829 INLINE type  *new_type_union (ident *name) {
830   type *res;
831   res = new_type(type_union, NULL, name);
832   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
833     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
834   res->attr.ua.members = NEW_ARR_F (entity *, 1);
835   return res;
836 }
837 type  *new_d_type_union (ident *name, dbg_info* db) {
838   type *res = new_type_uni (name);
839   set_type_dbg_info(res, db);
840   return res;
841 }
842 INLINE void free_union_attrs (type *uni) {
843   assert(uni && (uni->type_op == type_union));
844   DEL_ARR_F(uni->attr.ua.members);
845 }
846 /* manipulate private fields of union */
847 #if 0
848 int    get_union_n_types      (type *uni) {
849   assert(uni && (uni->type_op == type_union));
850   return uni->attr.ua.n_types;
851 }
852 type  *get_union_unioned_type (type *uni, int pos) {
853   assert(uni && (uni->type_op == type_union));
854   assert(pos >= 0 && pos < get_union_n_types(uni));
855   return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
856 }
857 void   set_union_unioned_type (type *uni, int pos, type *type) {
858   assert(uni && (uni->type_op == type_union));
859   assert(pos >= 0 && pos < get_union_n_types(uni));
860   uni->attr.ua.unioned_type[pos] = type;
861 }
862 ident *get_union_delim_nameid (type *uni, int pos) {
863   assert(uni && (uni->type_op == type_union));
864   assert(pos >= 0 && pos < get_union_n_types(uni));
865   return uni->attr.ua.delim_names[pos];
866 }
867 const char *get_union_delim_name (type *uni, int pos) {
868   assert(uni && (uni->type_op == type_union));
869   assert(pos >= 0 && pos < get_union_n_types(uni));
870   return id_to_str(uni->attr.ua.delim_names[pos]);
871 }
872 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
873   assert(uni && (uni->type_op == type_union));
874   assert(pos >= 0 && pos < get_union_n_types(uni));
875   uni->attr.ua.delim_names[pos] = id;
876 }
877 #endif
878 int    get_union_n_members      (type *uni) {
879   assert(uni && (uni->type_op == type_union));
880   return (ARR_LEN (uni->attr.ua.members))-1;
881 }
882 void    add_union_member   (type *uni, entity *member) {
883   assert(uni && (uni->type_op == type_union));
884   ARR_APP1 (entity *, uni->attr.ua.members, member);
885 }
886 entity  *get_union_member (type *uni, int pos) {
887   assert(uni && (uni->type_op == type_union));
888   assert(pos >= 0 && pos < get_union_n_members(uni));
889   return uni->attr.ua.members[pos+1];
890 }
891 void   set_union_member (type *uni, int pos, entity *member) {
892   assert(uni && (uni->type_op == type_union));
893   assert(pos >= 0 && pos < get_union_n_members(uni));
894   uni->attr.ua.members[pos+1] = member;
895 }
896 void   remove_union_member(type *uni, entity *member) {
897   int i;
898   assert(uni && (uni->type_op == type_union));
899   for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
900     if (uni->attr.ua.members[i] == member) {
901       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
902         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
903       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
904       break;
905     }
906 }
907
908 /* typecheck */
909 bool   is_union_type         (type *uni) {
910   assert(uni);
911   if (uni->type_op == type_union) return 1; else return 0;
912 }
913
914 /*******************************************************************/
915 /** TYPE_ARRAY                                                    **/
916 /*******************************************************************/
917
918
919 /* create a new type array -- set dimension sizes independently */
920 INLINE type *new_type_array         (ident *name, int n_dimensions,
921                               type *element_type) {
922   type *res;
923   int i;
924   assert(!is_method_type(element_type));
925   res = new_type(type_array, NULL, name);
926   res->attr.aa.n_dimensions = n_dimensions;
927   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
928   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
929   res->attr.aa.order  = (int *) xmalloc (sizeof (int) * n_dimensions);
930
931   for (i = 0; i < n_dimensions; i++) {
932     res->attr.aa.lower_bound[i]  = NULL;
933     res->attr.aa.upper_bound[i]  = NULL;
934     res->attr.aa.order[i] = i;
935   }
936   res->attr.aa.element_type = element_type;
937   new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
938   return res;
939 }
940 type *new_d_type_array (ident *name, int n_dimensions,
941                         type *element_type, dbg_info* db) {
942   type *res = new_type_array (name, n_dimensions, element_type);
943   set_type_dbg_info(res, db);
944   return res;
945 }
946
947 INLINE void free_array_attrs (type *array) {
948   assert(array && (array->type_op == type_array));
949   free(array->attr.aa.lower_bound);
950   free(array->attr.aa.upper_bound);
951 }
952
953 /* manipulate private fields of array type */
954 int   get_array_n_dimensions (type *array) {
955   assert(array && (array->type_op == type_array));
956   return array->attr.aa.n_dimensions;
957 }
958
959 INLINE void
960 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
961                   ir_node * upper_bound) {
962   assert(array && (array->type_op == type_array));
963   array->attr.aa.lower_bound[dimension] = lower_bound;
964   array->attr.aa.upper_bound[dimension] = upper_bound;
965 }
966 void
967 set_array_bounds_int (type *array, int dimension, int lower_bound,
968                       int upper_bound) {
969   ir_graph *rem = current_ir_graph;
970   current_ir_graph = get_const_code_irg();
971   set_array_bounds (array, dimension,
972                     new_Const(mode_I, tarval_from_long (mode_I, lower_bound)),
973                     new_Const(mode_I, tarval_from_long (mode_I, upper_bound)));
974   current_ir_graph = rem;
975 }
976 INLINE void
977 set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
978   assert(array && (array->type_op == type_array));
979   array->attr.aa.lower_bound[dimension] = lower_bound;
980 }
981 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
982   ir_graph *rem = current_ir_graph;
983   current_ir_graph = get_const_code_irg();
984   set_array_lower_bound  (array, dimension,
985                           new_Const(mode_I, tarval_from_long (mode_I, lower_bound)));
986   current_ir_graph = rem;
987 }
988 INLINE void
989 set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
990   assert(array && (array->type_op == type_array));
991   array->attr.aa.upper_bound[dimension] = upper_bound;
992 }
993 void  set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
994   ir_graph *rem = current_ir_graph;
995   current_ir_graph = get_const_code_irg();
996   set_array_upper_bound  (array, dimension,
997                           new_Const(mode_I, tarval_from_long (mode_I, upper_bound)));
998   current_ir_graph = rem;
999 }
1000 ir_node * get_array_lower_bound  (type *array, int dimension) {
1001   assert(array && (array->type_op == type_array));
1002   return array->attr.aa.lower_bound[dimension];
1003 }
1004 ir_node * get_array_upper_bound  (type *array, int dimension) {
1005   assert(array && (array->type_op == type_array));
1006   return array->attr.aa.upper_bound[dimension];
1007 }
1008
1009 void set_array_order (type *array, int dimension, int order) {
1010   assert(array && (array->type_op == type_array));
1011   array->attr.aa.order[dimension] = order;
1012 }
1013 int  get_array_order (type *array, int dimension) {
1014   assert(array && (array->type_op == type_array));
1015   return array->attr.aa.order[dimension];
1016 }
1017
1018 void  set_array_element_type (type *array, type *type) {
1019   assert(array && (array->type_op == type_array));
1020   assert(!is_method_type(type));
1021   array->attr.aa.element_type = type;
1022 }
1023 type *get_array_element_type (type *array) {
1024   assert(array && (array->type_op == type_array));
1025   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1026 }
1027
1028 void  set_array_element_entity (type *array, entity *ent) {
1029   assert(array && (array->type_op == type_array));
1030   assert((get_entity_type(ent)->type_op != type_method));
1031   array->attr.aa.element_ent = ent;
1032   array->attr.aa.element_type = get_entity_type(ent);
1033 }
1034 entity *get_array_element_entity (type *array) {
1035   assert(array && (array->type_op == type_array));
1036   return array->attr.aa.element_ent;
1037 }
1038
1039 /* typecheck */
1040 bool   is_array_type         (type *array) {
1041   assert(array);
1042   if (array->type_op == type_array) return 1; else return 0;
1043 }
1044
1045 /*******************************************************************/
1046 /** TYPE_ENUMERATION                                              **/
1047 /*******************************************************************/
1048
1049 /* create a new type enumeration -- set the enumerators independently */
1050 INLINE type   *new_type_enumeration    (ident *name, int n_enums) {
1051   type *res;
1052   int i;
1053   res = new_type(type_enumeration, NULL, name);
1054   res->attr.ea.n_enums     = n_enums;
1055   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1056   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
1057   for (i = 0; i < n_enums; i++) {
1058     res->attr.ea.enumer[i] = NULL;
1059     res->attr.ea.enum_nameid  = NULL;
1060   }
1061   return res;
1062 }
1063 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db) {
1064   type *res = new_type_enumeration (name, n_enums);
1065   set_type_dbg_info(res, db);
1066   return res;
1067 }
1068
1069 INLINE void free_enumeration_attrs(type *enumeration) {
1070   assert(enumeration && (enumeration->type_op == type_enumeration));
1071   free(enumeration->attr.ea.enumer);
1072   free(enumeration->attr.ea.enum_nameid);
1073 }
1074
1075 /* manipulate fields of enumeration type. */
1076 int     get_enumeration_n_enums (type *enumeration) {
1077   assert(enumeration && (enumeration->type_op == type_enumeration));
1078   return enumeration->attr.ea.n_enums;
1079 }
1080 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
1081   assert(enumeration && (enumeration->type_op == type_enumeration));
1082   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1083   enumeration->attr.ea.enumer[pos] = con;
1084 }
1085 tarval *get_enumeration_enum    (type *enumeration, int pos) {
1086   assert(enumeration && (enumeration->type_op == type_enumeration));
1087   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1088   return enumeration->attr.ea.enumer[pos];
1089 }
1090 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
1091   assert(enumeration && (enumeration->type_op == type_enumeration));
1092   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1093   enumeration->attr.ea.enum_nameid[pos] = id;
1094 }
1095 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
1096   assert(enumeration && (enumeration->type_op == type_enumeration));
1097   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1098   return enumeration->attr.ea.enum_nameid[pos];
1099 }
1100 const char *get_enumeration_name(type *enumeration, int pos) {
1101   assert(enumeration && (enumeration->type_op == type_enumeration));
1102   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1103   return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1104 }
1105
1106 /* typecheck */
1107 bool    is_enumeration_type     (type *enumeration) {
1108   assert(enumeration);
1109   if (enumeration->type_op == type_enumeration) return 1; else return 0;
1110 }
1111
1112 /*******************************************************************/
1113 /** TYPE_POINTER                                                  **/
1114 /*******************************************************************/
1115
1116 /* Create a new type pointer */
1117 INLINE type *new_type_pointer           (ident *name, type *points_to) {
1118   type *res;
1119   res = new_type(type_pointer, mode_p, name);
1120   res->attr.pa.points_to = points_to;
1121   res->size = get_mode_size(res->mode);
1122   res->state = layout_fixed;
1123   return res;
1124 }
1125 type *new_d_type_pointer           (ident *name, type *points_to, dbg_info* db) {
1126   type *res = new_type_pointer (name, points_to);
1127   set_type_dbg_info(res, db);
1128   return res;
1129 }
1130 INLINE void free_pointer_attrs (type *pointer) {
1131   assert(pointer && (pointer->type_op == type_pointer));
1132 }
1133 /* manipulate fields of type_pointer */
1134 void  set_pointer_points_to_type (type *pointer, type *type) {
1135   assert(pointer && (pointer->type_op == type_pointer));
1136   pointer->attr.pa.points_to = type;
1137 }
1138 type *get_pointer_points_to_type (type *pointer) {
1139   assert(pointer && (pointer->type_op == type_pointer));
1140   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1141 }
1142
1143 /* typecheck */
1144 bool  is_pointer_type            (type *pointer) {
1145   assert(pointer);
1146   if (pointer->type_op == type_pointer) return 1; else return 0;
1147 }
1148
1149
1150 /*******************************************************************/
1151 /** TYPE_PRIMITIVE                                                **/
1152 /*******************************************************************/
1153
1154 /* create a new type primitive */
1155 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1156   type *res;
1157   /* @@@ assert( mode_is_data(mode) && (!mode == mode_p)); */
1158   res = new_type(type_primitive, mode, name);
1159   res->size = get_mode_size(mode);
1160   res->state = layout_fixed;
1161   return res;
1162 }
1163 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1164   type *res = new_type_primitive (name, mode);
1165   set_type_dbg_info(res, db);
1166   return res;
1167 }
1168 INLINE void free_primitive_attrs (type *primitive) {
1169   assert(primitive && (primitive->type_op == type_primitive));
1170 }
1171
1172 /* typecheck */
1173 bool  is_primitive_type  (type *primitive) {
1174   assert(primitive && primitive->kind == k_type);
1175   if (primitive->type_op == type_primitive) return 1; else return 0;
1176 }
1177
1178 /*******************************************************************/
1179 /** common functionality                                          **/
1180 /*******************************************************************/
1181
1182
1183 INLINE int is_atomic_type(type *tp) {
1184   assert(tp && tp->kind == k_type);
1185   return (is_primitive_type(tp) || is_pointer_type(tp) ||
1186           is_enumeration_type(tp));
1187 }
1188 INLINE int is_compound_type(type *tp) {
1189   assert(tp && tp->kind == k_type);
1190   return (is_class_type(tp) || is_struct_type(tp) ||
1191           is_array_type(tp) || is_union_type(tp));
1192 }