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