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