added doxygen autodocumentation
[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_n_params(typ1) != get_method_n_params(typ2)) return false;
341     if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
342     for (i = 0; i < get_method_n_params(typ1); i++) {
343       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
344         return false;
345     }
346     for (i = 0; i < get_method_n_ress(typ1); i++) {
347       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
348         return false;
349     }
350   } break;
351   case tpo_union:       {
352     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
353     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
354     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
355     /* First sort the members of lt */
356     for (i = 0; i < get_union_n_members(typ1); i++) {
357       entity *e1 = get_union_member(typ1, i);
358       for (j = 0; j < get_union_n_members(typ2); j++) {
359         entity *e2 = get_union_member(typ2, j);
360         if (get_entity_name(e1) == get_entity_name(e2))
361           m[i] = e2;
362       }
363     }
364     for (i = 0; i < get_union_n_members(typ1); i++) {
365       if (!m[i]  ||  /* Found no counterpart */
366           !equal_entity(get_union_member(typ1, i), m[i]))
367         return false;
368     }
369   } break;
370   case tpo_array:       {
371     type *set, *let;  /* small/large elt. type */
372     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
373       return false;
374     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
375       return false;
376     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
377       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
378           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
379         return false;
380       if (get_array_order(typ1, i) != get_array_order(typ2, i))
381         assert(0 && "type compare with different dimension orders not implemented");
382     }
383   } break;
384   case tpo_enumeration: {
385     assert(0 && "enumerations not implemented");
386   } break;
387   case tpo_pointer:     {
388     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
389       return false;
390   } break;
391   case tpo_primitive:   {
392   } break;
393   default: break;
394   }
395   return true;
396 }
397
398 bool smaller_type (type *st, type *lt) {
399   entity **m;
400   int i, j;
401
402   if (st == lt) return true;
403
404   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
405     return false;
406
407   switch(get_type_tpop_code(st)) {
408   case tpo_class:       {
409     return is_subclass_of(st, lt);
410   } break;
411   case tpo_struct:      {
412     if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
413     m = alloca(sizeof(entity *) * get_struct_n_members(st));
414     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
415     /* First sort the members of lt */
416     for (i = 0; i < get_struct_n_members(st); i++) {
417       entity *se = get_struct_member(st, i);
418       for (j = 0; j < get_struct_n_members(lt); j++) {
419         entity *le = get_struct_member(lt, j);
420         if (get_entity_name(le) == get_entity_name(se))
421           m[i] = le;
422       }
423     }
424     for (i = 0; i < get_struct_n_members(st); i++) {
425       if (!m[i]  ||  /* Found no counterpart */
426           !smaller_type(get_entity_type(get_struct_member(st, i)),
427                         get_entity_type(m[i])))
428         return false;
429     }
430   } break;
431   case tpo_method:      {
432     if (get_method_n_params(st) != get_method_n_params(lt)) return false;
433     if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
434     for (i = 0; i < get_method_n_params(st); i++) {
435       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
436         return false;
437     }
438     for (i = 0; i < get_method_n_ress(st); i++) {
439       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
440         return false;
441     }
442   } break;
443   case tpo_union:       {
444     if (get_union_n_members(st) != get_union_n_members(lt)) return false;
445     m = alloca(sizeof(entity *) * get_union_n_members(st));
446     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
447     /* First sort the members of lt */
448     for (i = 0; i < get_union_n_members(st); i++) {
449       entity *se = get_union_member(st, i);
450       for (j = 0; j < get_union_n_members(lt); j++) {
451         entity *le = get_union_member(lt, j);
452         if (get_entity_name(le) == get_entity_name(se))
453           m[i] = le;
454       }
455     }
456     for (i = 0; i < get_union_n_members(st); i++) {
457       if (!m[i]  ||  /* Found no counterpart */
458           !smaller_type(get_entity_type(get_union_member(st, i)),
459                         get_entity_type(m[i])))
460         return false;
461     }
462   } break;
463   case tpo_array:       {
464     type *set, *let;  /* small/large elt. type */
465     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
466       return false;
467     set = get_array_element_type(st);
468     let = get_array_element_type(lt);
469     if (set != let) {
470       /* If the elt types are different, set must be convertible
471          to let, and they must have the same size so that address
472          computations work out.  To have a size the layout must
473          be fixed. */
474       if ((get_type_state(set) != layout_fixed) ||
475           (get_type_state(let) != layout_fixed))
476         return false;
477       if (!smaller_type(set, let) ||
478           get_type_size(set) != get_type_size(let))
479         return false;
480     }
481     for(i = 0; i < get_array_n_dimensions(st); i++) {
482       if (get_array_lower_bound(lt, i))
483         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
484           return false;
485       if (get_array_upper_bound(lt, i))
486         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
487           return false;
488     }
489   } break;
490   case tpo_enumeration: {
491     assert(0 && "enumerations not implemented");
492   } break;
493   case tpo_pointer:     {
494     if (!smaller_type(get_pointer_points_to_type(st),
495                       get_pointer_points_to_type(lt)))
496       return false;
497   } break;
498   case tpo_primitive:   {
499     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
500       return false;
501   } break;
502   default: break;
503   }
504   return true;
505 }
506
507 /*******************************************************************/
508 /** TYPE_CLASS                                                    **/
509 /*******************************************************************/
510
511 /* create a new class type */
512 INLINE type   *new_type_class (ident *name) {
513   type *res;
514
515   res = new_type(type_class, NULL, name);
516
517   res->attr.ca.members    = NEW_ARR_F (entity *, 1);
518   res->attr.ca.subtypes   = NEW_ARR_F (type *, 1);
519   res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
520   res->attr.ca.peculiarity = existent;
521   res->attr.ca.dfn        = 0;
522
523   return res;
524 }
525 type   *new_d_type_class (ident *name, dbg_info* db) {
526   type *res = new_type_class (name);
527   set_type_dbg_info(res, db);
528   return res;
529 }
530 INLINE void free_class_attrs(type *clss) {
531   assert(clss && (clss->type_op == type_class));
532   DEL_ARR_F(clss->attr.ca.members);
533   DEL_ARR_F(clss->attr.ca.subtypes);
534   DEL_ARR_F(clss->attr.ca.supertypes);
535 }
536
537 /* manipulate private fields of class type  */
538 void    add_class_member   (type *clss, entity *member) {
539   assert(clss && (clss->type_op == type_class));
540   ARR_APP1 (entity *, clss->attr.ca.members, member);
541 }
542 int     get_class_n_members (type *clss) {
543   assert(clss && (clss->type_op == type_class));
544   return (ARR_LEN (clss->attr.ca.members))-1;
545 }
546 entity *get_class_member   (type *clss, int pos) {
547   assert(clss && (clss->type_op == type_class));
548   assert(pos >= 0 && pos < get_class_n_members(clss));
549   return clss->attr.ca.members[pos+1];
550 }
551 void    set_class_member   (type *clss, entity *member, int pos) {
552   assert(clss && (clss->type_op == type_class));
553   assert(pos >= 0 && pos < get_class_n_members(clss));
554   clss->attr.ca.members[pos+1] = member;
555 }
556 void    set_class_members  (type *clss, entity **members, int arity) {
557   int i;
558   assert(clss && (clss->type_op == type_class));
559   DEL_ARR_F(clss->attr.ca.members);
560   clss->attr.ca.members    = NEW_ARR_F (entity *, 1);
561   for (i = 0; i < arity; i++) {
562     set_entity_owner(members[i], clss);
563     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
564   }
565 }
566 void    remove_class_member(type *clss, entity *member) {
567   int i;
568   assert(clss && (clss->type_op == type_class));
569   for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
570     if (clss->attr.ca.members[i] == member) {
571       for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
572         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
573       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
574       break;
575     }
576   }
577 }
578
579 void    add_class_subtype   (type *clss, type *subtype) {
580   int i;
581   assert(clss && (clss->type_op == type_class));
582   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
583   for (i = 0; i < get_class_n_supertypes(subtype); i++)
584     if (get_class_supertype(subtype, i) == clss)
585       /* Class already registered */
586       return;
587   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
588 }
589 int     get_class_n_subtypes (type *clss) {
590   assert(clss && (clss->type_op == type_class));
591   return (ARR_LEN (clss->attr.ca.subtypes))-1;
592 }
593 type   *get_class_subtype   (type *clss, int pos) {
594   assert(clss && (clss->type_op == type_class));
595   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
596   return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
597 }
598 void    set_class_subtype   (type *clss, type *subtype, int pos) {
599   assert(clss && (clss->type_op == type_class));
600   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
601   clss->attr.ca.subtypes[pos+1] = subtype;
602 }
603 void    remove_class_subtype(type *clss, type *subtype) {
604   int i;
605   assert(clss && (clss->type_op == type_class));
606   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
607     if (clss->attr.ca.subtypes[i] == subtype) {
608       for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
609         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
610       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
611       break;
612     }
613 }
614
615 void    add_class_supertype   (type *clss, type *supertype) {
616   int i;
617   assert(clss && (clss->type_op == type_class));
618   assert(supertype && (supertype -> type_op == type_class));
619   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
620   for (i = 0; i < get_class_n_subtypes(supertype); i++)
621     if (get_class_subtype(supertype, i) == clss)
622       /* Class already registered */
623       return;
624   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
625 }
626 int     get_class_n_supertypes (type *clss) {
627   assert(clss && (clss->type_op == type_class));
628   return (ARR_LEN (clss->attr.ca.supertypes))-1;
629 }
630 type   *get_class_supertype   (type *clss, int pos) {
631   assert(clss && (clss->type_op == type_class));
632   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
633   return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
634 }
635 void    set_class_supertype   (type *clss, type *supertype, int pos) {
636   assert(clss && (clss->type_op == type_class));
637   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
638   clss->attr.ca.supertypes[pos+1] = supertype;
639 }
640 void    remove_class_supertype(type *clss, type *supertype) {
641   int i;
642   assert(clss && (clss->type_op == type_class));
643   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
644     if (clss->attr.ca.supertypes[i] == supertype) {
645       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
646         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
647       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
648       break;
649     }
650 }
651
652 INLINE peculiarity get_class_peculiarity (type *clss) {
653   assert(clss && (clss->type_op == type_class));
654   return clss->attr.ca.peculiarity;
655 }
656 INLINE void        set_class_peculiarity (type *clss, peculiarity pec) {
657   assert(clss && (clss->type_op == type_class));
658   assert(pec != inherited);  /* There is no inheritance of types in libFirm. */
659   clss->attr.ca.peculiarity = pec;
660 }
661
662 void set_class_dfn (type *clss, int dfn)
663 {
664   clss->attr.ca.dfn        = dfn;
665 }
666
667 int get_class_dfn (type *clss)
668 {
669   return (clss->attr.ca.dfn);
670 }
671
672 /* typecheck */
673 bool    is_class_type(type *clss) {
674   assert(clss);
675   if (clss->type_op == type_class) return 1; else return 0;
676 }
677
678 bool is_subclass_of(type *low, type *high) {
679   int i;
680   assert(is_class_type(low) && is_class_type(high));
681   if (low == high) return true;
682   /* depth first search from high downwards. */
683   for (i = 0; i < get_class_n_subtypes(high); i++) {
684     if (low == get_class_subtype(high, i))
685       return true;
686     if (is_subclass_of(low, get_class_subtype(high, i)))
687       return true;
688   }
689   return false;
690 }
691
692 /*******************************************************************/
693 /** TYPE_STRUCT                                                   **/
694 /*******************************************************************/
695
696 /* create a new type struct */
697 INLINE type   *new_type_struct (ident *name) {
698   type *res;
699   res = new_type(type_struct, NULL, name);
700   res->attr.sa.members = NEW_ARR_F (entity *, 1);
701   return res;
702 }
703 type   *new_d_type_struct (ident *name, dbg_info* db) {
704   type *res = new_type_struct (name);
705   set_type_dbg_info(res, db);
706   return res;
707 }
708 INLINE void free_struct_attrs (type *strct) {
709   assert(strct && (strct->type_op == type_struct));
710   DEL_ARR_F(strct->attr.sa.members);
711 }
712
713 /* manipulate private fields of struct */
714 int     get_struct_n_members (type *strct) {
715   assert(strct && (strct->type_op == type_struct));
716   return (ARR_LEN (strct->attr.sa.members))-1;
717 }
718 void    add_struct_member   (type *strct, entity *member) {
719   assert(strct && (strct->type_op == type_struct));
720   assert(get_type_tpop(get_entity_type(member)) != type_method);
721     /*    @@@ lowerfirm geht nicht durch */
722   ARR_APP1 (entity *, strct->attr.sa.members, member);
723 }
724 entity *get_struct_member   (type *strct, int pos) {
725   assert(strct && (strct->type_op == type_struct));
726   assert(pos >= 0 && pos < get_struct_n_members(strct));
727   return strct->attr.sa.members[pos+1];
728 }
729 void    set_struct_member   (type *strct, int pos, entity *member) {
730   assert(strct && (strct->type_op == type_struct));
731   assert(pos >= 0 && pos < get_struct_n_members(strct));
732   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
733   strct->attr.sa.members[pos+1] = member;
734 }
735 void    remove_struct_member(type *strct, entity *member) {
736   int i;
737   assert(strct && (strct->type_op == type_struct));
738   for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
739     if (strct->attr.sa.members[i] == member) {
740       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
741         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
742       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
743       break;
744     }
745 }
746
747 /* typecheck */
748 bool    is_struct_type(type *strct) {
749   assert(strct);
750   if (strct->type_op == type_struct) return 1; else return 0;
751 }
752
753 /*******************************************************************/
754 /** TYPE_METHOD                                                   **/
755 /*******************************************************************/
756
757 /* Create a new method type.
758    N_param is the number of parameters, n_res the number of results.  */
759 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
760   type *res;
761   res = new_type(type_method, mode_P, name);
762   res->state = layout_fixed;
763   res->size = get_mode_size(mode_P);
764   res->attr.ma.n_params   = n_param;
765   res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
766   res->attr.ma.n_res      = n_res;
767   res->attr.ma.res_type   = (type **) xmalloc (sizeof (type *) * n_res);
768   return res;
769 }
770 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
771   type *res = new_type_method (name, n_param, n_res);
772   set_type_dbg_info(res, db);
773   return res;
774 }
775 INLINE void free_method_attrs(type *method) {
776   assert(method && (method->type_op == type_method));
777   free(method->attr.ma.param_type);
778   free(method->attr.ma.res_type);
779 }
780 /* manipulate private fields of method. */
781 int   get_method_n_params  (type *method) {
782   assert(method && (method->type_op == type_method));
783   return method->attr.ma.n_params;
784 }
785 type *get_method_param_type(type *method, int pos) {
786   assert(method && (method->type_op == type_method));
787   assert(pos >= 0 && pos < get_method_n_params(method));
788   return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
789 }
790 void  set_method_param_type(type *method, int pos, type* tp) {
791   assert(method && (method->type_op == type_method));
792   assert(pos >= 0 && pos < get_method_n_params(method));
793   method->attr.ma.param_type[pos] = tp;
794 }
795
796 int   get_method_n_ress   (type *method) {
797   assert(method && (method->type_op == type_method));
798   return method->attr.ma.n_res;
799 }
800 type *get_method_res_type(type *method, int pos) {
801   assert(method && (method->type_op == type_method));
802   assert(pos >= 0 && pos < get_method_n_ress(method));
803   return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
804 }
805 void  set_method_res_type(type *method, int pos, type* tp) {
806   assert(method && (method->type_op == type_method));
807   assert(pos >= 0 && pos < get_method_n_ress(method));
808   method->attr.ma.res_type[pos] = tp;
809 }
810
811 /* typecheck */
812 bool  is_method_type     (type *method) {
813   assert(method);
814   if (method->type_op == type_method) return 1; else return 0;
815 }
816
817 /*******************************************************************/
818 /** TYPE_UNION                                                    **/
819 /*******************************************************************/
820
821 /* create a new type uni */
822 INLINE type  *new_type_union (ident *name) {
823   type *res;
824   res = new_type(type_union, NULL, name);
825   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
826     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
827   res->attr.ua.members = NEW_ARR_F (entity *, 1);
828   return res;
829 }
830 type  *new_d_type_union (ident *name, dbg_info* db) {
831   type *res = new_type_union (name);
832   set_type_dbg_info(res, db);
833   return res;
834 }
835 INLINE void free_union_attrs (type *uni) {
836   assert(uni && (uni->type_op == type_union));
837   DEL_ARR_F(uni->attr.ua.members);
838 }
839 /* manipulate private fields of union */
840 #if 0
841 int    get_union_n_types      (type *uni) {
842   assert(uni && (uni->type_op == type_union));
843   return uni->attr.ua.n_types;
844 }
845 type  *get_union_unioned_type (type *uni, int pos) {
846   assert(uni && (uni->type_op == type_union));
847   assert(pos >= 0 && pos < get_union_n_types(uni));
848   return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
849 }
850 void   set_union_unioned_type (type *uni, int pos, type *tp) {
851   assert(uni && (uni->type_op == type_union));
852   assert(pos >= 0 && pos < get_union_n_types(uni));
853   uni->attr.ua.unioned_type[pos] = tp;
854 }
855 ident *get_union_delim_nameid (type *uni, int pos) {
856   assert(uni && (uni->type_op == type_union));
857   assert(pos >= 0 && pos < get_union_n_types(uni));
858   return uni->attr.ua.delim_names[pos];
859 }
860 const char *get_union_delim_name (type *uni, int pos) {
861   assert(uni && (uni->type_op == type_union));
862   assert(pos >= 0 && pos < get_union_n_types(uni));
863   return id_to_str(uni->attr.ua.delim_names[pos]);
864 }
865 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
866   assert(uni && (uni->type_op == type_union));
867   assert(pos >= 0 && pos < get_union_n_types(uni));
868   uni->attr.ua.delim_names[pos] = id;
869 }
870 #endif
871 int    get_union_n_members      (type *uni) {
872   assert(uni && (uni->type_op == type_union));
873   return (ARR_LEN (uni->attr.ua.members))-1;
874 }
875 void    add_union_member   (type *uni, entity *member) {
876   assert(uni && (uni->type_op == type_union));
877   ARR_APP1 (entity *, uni->attr.ua.members, member);
878 }
879 entity  *get_union_member (type *uni, int pos) {
880   assert(uni && (uni->type_op == type_union));
881   assert(pos >= 0 && pos < get_union_n_members(uni));
882   return uni->attr.ua.members[pos+1];
883 }
884 void   set_union_member (type *uni, int pos, entity *member) {
885   assert(uni && (uni->type_op == type_union));
886   assert(pos >= 0 && pos < get_union_n_members(uni));
887   uni->attr.ua.members[pos+1] = member;
888 }
889 void   remove_union_member(type *uni, entity *member) {
890   int i;
891   assert(uni && (uni->type_op == type_union));
892   for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
893     if (uni->attr.ua.members[i] == member) {
894       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
895         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
896       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
897       break;
898     }
899 }
900
901 /* typecheck */
902 bool   is_union_type         (type *uni) {
903   assert(uni);
904   if (uni->type_op == type_union) return 1; else return 0;
905 }
906
907 /*******************************************************************/
908 /** TYPE_ARRAY                                                    **/
909 /*******************************************************************/
910
911
912 /* create a new type array -- set dimension sizes independently */
913 INLINE type *new_type_array         (ident *name, int n_dimensions,
914                               type *element_type) {
915   type *res;
916   int i;
917   assert(!is_method_type(element_type));
918   res = new_type(type_array, NULL, name);
919   res->attr.aa.n_dimensions = n_dimensions;
920   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
921   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
922   res->attr.aa.order  = (int *) xmalloc (sizeof (int) * n_dimensions);
923
924   for (i = 0; i < n_dimensions; i++) {
925     res->attr.aa.lower_bound[i]  = NULL;
926     res->attr.aa.upper_bound[i]  = NULL;
927     res->attr.aa.order[i] = i;
928   }
929   res->attr.aa.element_type = element_type;
930   new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
931   return res;
932 }
933 type *new_d_type_array (ident *name, int n_dimensions,
934                         type *element_type, dbg_info* db) {
935   type *res = new_type_array (name, n_dimensions, element_type);
936   set_type_dbg_info(res, db);
937   return res;
938 }
939
940 INLINE void free_array_attrs (type *array) {
941   assert(array && (array->type_op == type_array));
942   free(array->attr.aa.lower_bound);
943   free(array->attr.aa.upper_bound);
944 }
945
946 /* manipulate private fields of array type */
947 int   get_array_n_dimensions (type *array) {
948   assert(array && (array->type_op == type_array));
949   return array->attr.aa.n_dimensions;
950 }
951
952 INLINE void
953 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
954                   ir_node * upper_bound) {
955   assert(array && (array->type_op == type_array));
956   array->attr.aa.lower_bound[dimension] = lower_bound;
957   array->attr.aa.upper_bound[dimension] = upper_bound;
958 }
959 void
960 set_array_bounds_int (type *array, int dimension, int lower_bound,
961                       int upper_bound) {
962   ir_graph *rem = current_ir_graph;
963   current_ir_graph = get_const_code_irg();
964   set_array_bounds (array, dimension,
965                     new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)),
966                     new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
967   current_ir_graph = rem;
968 }
969 INLINE void
970 set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
971   assert(array && (array->type_op == type_array));
972   array->attr.aa.lower_bound[dimension] = lower_bound;
973 }
974 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
975   ir_graph *rem = current_ir_graph;
976   current_ir_graph = get_const_code_irg();
977   set_array_lower_bound  (array, dimension,
978                           new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)));
979   current_ir_graph = rem;
980 }
981 INLINE void
982 set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
983   assert(array && (array->type_op == type_array));
984   array->attr.aa.upper_bound[dimension] = upper_bound;
985 }
986 void  set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
987   ir_graph *rem = current_ir_graph;
988   current_ir_graph = get_const_code_irg();
989   set_array_upper_bound  (array, dimension,
990                           new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
991   current_ir_graph = rem;
992 }
993 ir_node * get_array_lower_bound  (type *array, int dimension) {
994   assert(array && (array->type_op == type_array));
995   return array->attr.aa.lower_bound[dimension];
996 }
997 ir_node * get_array_upper_bound  (type *array, int dimension) {
998   assert(array && (array->type_op == type_array));
999   return array->attr.aa.upper_bound[dimension];
1000 }
1001
1002 void set_array_order (type *array, int dimension, int order) {
1003   assert(array && (array->type_op == type_array));
1004   array->attr.aa.order[dimension] = order;
1005 }
1006 int  get_array_order (type *array, int dimension) {
1007   assert(array && (array->type_op == type_array));
1008   return array->attr.aa.order[dimension];
1009 }
1010
1011 void  set_array_element_type (type *array, type *tp) {
1012   assert(array && (array->type_op == type_array));
1013   assert(!is_method_type(tp));
1014   array->attr.aa.element_type = tp;
1015 }
1016 type *get_array_element_type (type *array) {
1017   assert(array && (array->type_op == type_array));
1018   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1019 }
1020
1021 void  set_array_element_entity (type *array, entity *ent) {
1022   assert(array && (array->type_op == type_array));
1023   assert((get_entity_type(ent)->type_op != type_method));
1024   array->attr.aa.element_ent = ent;
1025   array->attr.aa.element_type = get_entity_type(ent);
1026 }
1027 entity *get_array_element_entity (type *array) {
1028   assert(array && (array->type_op == type_array));
1029   return array->attr.aa.element_ent;
1030 }
1031
1032 /* typecheck */
1033 bool   is_array_type         (type *array) {
1034   assert(array);
1035   if (array->type_op == type_array) return 1; else return 0;
1036 }
1037
1038 /*******************************************************************/
1039 /** TYPE_ENUMERATION                                              **/
1040 /*******************************************************************/
1041
1042 /* create a new type enumeration -- set the enumerators independently */
1043 INLINE type   *new_type_enumeration    (ident *name, int n_enums) {
1044   type *res;
1045   int i;
1046   res = new_type(type_enumeration, NULL, name);
1047   res->attr.ea.n_enums     = n_enums;
1048   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1049   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
1050   for (i = 0; i < n_enums; i++) {
1051     res->attr.ea.enumer[i] = NULL;
1052     res->attr.ea.enum_nameid  = NULL;
1053   }
1054   return res;
1055 }
1056 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db) {
1057   type *res = new_type_enumeration (name, n_enums);
1058   set_type_dbg_info(res, db);
1059   return res;
1060 }
1061
1062 INLINE void free_enumeration_attrs(type *enumeration) {
1063   assert(enumeration && (enumeration->type_op == type_enumeration));
1064   free(enumeration->attr.ea.enumer);
1065   free(enumeration->attr.ea.enum_nameid);
1066 }
1067
1068 /* manipulate fields of enumeration type. */
1069 int     get_enumeration_n_enums (type *enumeration) {
1070   assert(enumeration && (enumeration->type_op == type_enumeration));
1071   return enumeration->attr.ea.n_enums;
1072 }
1073 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
1074   assert(enumeration && (enumeration->type_op == type_enumeration));
1075   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1076   enumeration->attr.ea.enumer[pos] = con;
1077 }
1078 tarval *get_enumeration_enum    (type *enumeration, int pos) {
1079   assert(enumeration && (enumeration->type_op == type_enumeration));
1080   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1081   return enumeration->attr.ea.enumer[pos];
1082 }
1083 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
1084   assert(enumeration && (enumeration->type_op == type_enumeration));
1085   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1086   enumeration->attr.ea.enum_nameid[pos] = id;
1087 }
1088 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
1089   assert(enumeration && (enumeration->type_op == type_enumeration));
1090   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1091   return enumeration->attr.ea.enum_nameid[pos];
1092 }
1093 const char *get_enumeration_name(type *enumeration, int pos) {
1094   assert(enumeration && (enumeration->type_op == type_enumeration));
1095   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1096   return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1097 }
1098
1099 /* typecheck */
1100 bool    is_enumeration_type     (type *enumeration) {
1101   assert(enumeration);
1102   if (enumeration->type_op == type_enumeration) return 1; else return 0;
1103 }
1104
1105 /*******************************************************************/
1106 /** TYPE_POINTER                                                  **/
1107 /*******************************************************************/
1108
1109 /* Create a new type pointer */
1110 INLINE type *new_type_pointer           (ident *name, type *points_to) {
1111   type *res;
1112   res = new_type(type_pointer, mode_P, name);
1113   res->attr.pa.points_to = points_to;
1114   res->size = get_mode_size(res->mode);
1115   res->state = layout_fixed;
1116   return res;
1117 }
1118 type *new_d_type_pointer           (ident *name, type *points_to, dbg_info* db) {
1119   type *res = new_type_pointer (name, points_to);
1120   set_type_dbg_info(res, db);
1121   return res;
1122 }
1123 INLINE void free_pointer_attrs (type *pointer) {
1124   assert(pointer && (pointer->type_op == type_pointer));
1125 }
1126 /* manipulate fields of type_pointer */
1127 void  set_pointer_points_to_type (type *pointer, type *tp) {
1128   assert(pointer && (pointer->type_op == type_pointer));
1129   pointer->attr.pa.points_to = tp;
1130 }
1131 type *get_pointer_points_to_type (type *pointer) {
1132   assert(pointer && (pointer->type_op == type_pointer));
1133   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1134 }
1135
1136 /* typecheck */
1137 bool  is_pointer_type            (type *pointer) {
1138   assert(pointer);
1139   if (pointer->type_op == type_pointer) return 1; else return 0;
1140 }
1141
1142
1143 /*******************************************************************/
1144 /** TYPE_PRIMITIVE                                                **/
1145 /*******************************************************************/
1146
1147 /* create a new type primitive */
1148 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1149   type *res;
1150   /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1151   res = new_type(type_primitive, mode, name);
1152   res->size = get_mode_size(mode);
1153   res->state = layout_fixed;
1154   return res;
1155 }
1156 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1157   type *res = new_type_primitive (name, mode);
1158   set_type_dbg_info(res, db);
1159   return res;
1160 }
1161 INLINE void free_primitive_attrs (type *primitive) {
1162   assert(primitive && (primitive->type_op == type_primitive));
1163 }
1164
1165 /* typecheck */
1166 bool  is_primitive_type  (type *primitive) {
1167   assert(primitive && primitive->kind == k_type);
1168   if (primitive->type_op == type_primitive) return 1; else return 0;
1169 }
1170
1171 /*******************************************************************/
1172 /** common functionality                                          **/
1173 /*******************************************************************/
1174
1175
1176 INLINE int is_atomic_type(type *tp) {
1177   assert(tp && tp->kind == k_type);
1178   return (is_primitive_type(tp) || is_pointer_type(tp) ||
1179           is_enumeration_type(tp));
1180 }
1181 INLINE int is_compound_type(type *tp) {
1182   assert(tp && tp->kind == k_type);
1183   return (is_class_type(tp) || is_struct_type(tp) ||
1184           is_array_type(tp) || is_union_type(tp));
1185 }