inlinening of functions
[libfirm] / ir / tr / type.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type.c
4  * Purpose:     Representation of types.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  *
15  *   file type.c - implementation of the datastructure to hold
16  *   type information.
17  *  (C) 2001 by Universitaet Karlsruhe
18  *  Goetz Lindenmaier
19  *
20  *  This module supplies a datastructure to represent all types
21  *  known in the compiled program.  This includes types specified
22  *  in the program as well as types defined by the language.  In the
23  *  view of the intermediate representation there is no difference
24  *  between these types.
25  *
26  *  There exist several kinds of types, arranged by the structure of
27  *  the type.  A type is described by a set of attributes.  Some of
28  *  these attributes are common to all types, others depend on the
29  *  kind of the type.
30  *
31  *  Types are different from the modes defined in irmode:  Types are
32  *  on the level of the programming language, modes at the level of
33  *  the target processor.
34  *
35  * @see  type_t.h type tpop
36  */
37
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42
43 # include <stdlib.h>
44 # include <stddef.h>
45 # include <string.h>
46 # include "type_t.h"
47 # include "tpop_t.h"
48 # include "irprog_t.h"
49 # include "typegmod.h"
50 # include "array.h"
51 # include "irprog.h"
52 # include "mangle.h"
53 # include "tv.h"
54 # include "ircons.h"
55
56 /*******************************************************************/
57 /** TYPE                                                          **/
58 /*******************************************************************/
59
60 type *none_type;    type *get_none_type(void)    { return none_type;    }
61 type *unknown_type; type *get_unknown_type(void) { return unknown_type; }
62
63
64 #ifdef DEBUG_libfirm
65 /** Returns a new, unique number to number nodes or the like. */
66 int get_irp_new_node_nr(void);
67 #endif
68
69 /* Suffixes added to types used for pass-by-value representations. */
70 static ident *value_params_suffix = NULL;
71 static ident *value_ress_suffix = NULL;
72
73 void init_type(void) {
74   value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
75   value_ress_suffix   = new_id_from_str(VALUE_RESS_SUFFIX);
76
77   /* construct none and unknown type. */
78   none_type    = new_type(tpop_none,    mode_BAD, new_id_from_str("type_none"));
79   set_type_size_bits(none_type, 0);
80   set_type_state (none_type, layout_fixed);
81   remove_irp_type(none_type);
82   unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"));
83   set_type_size_bits(unknown_type, 0);
84   set_type_state (unknown_type, layout_fixed);
85   remove_irp_type(unknown_type);
86 }
87
88 unsigned long type_visited;
89
90 void (set_master_type_visited)(unsigned long val) { __set_master_type_visited(val); }
91 unsigned long (get_master_type_visited)(void)     { return __get_master_type_visited(); }
92 void (inc_master_type_visited)(void)              { __inc_master_type_visited(); }
93
94
95 type *
96 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
97   type *res;
98   int node_size ;
99
100   assert(type_op != type_id);
101   assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
102
103   node_size = offsetof(type, attr) +  type_op->attr_size;
104   res = (type *) xmalloc (node_size);
105   add_irp_type(res);   /* Remember the new type global. */
106
107   res->kind    = k_type;
108   res->type_op = type_op;
109   res->mode    = mode;
110   res->name    = name;
111   res->state   = layout_undefined;
112   res->size    = -1;
113   res->visit   = 0;
114   res -> link  = NULL;
115 #ifdef DEBUG_libfirm
116   res->nr      = get_irp_new_node_nr();
117 #endif
118
119   return res;
120 }
121
122 void        free_type(type *tp) {
123   if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
124     return;
125   /* Remove from list of all types */
126   remove_irp_type(tp);
127   /* Free the attributes of the type. */
128   free_type_attrs(tp);
129   /* Free entities automatically allocated with the type */
130   if (is_array_type(tp))
131     free_entity(get_array_element_entity(tp));
132   /* And now the type itself... */
133   tp->kind = k_BAD;
134   free(tp);
135 }
136
137 void free_type_entities(type *tp) {
138   switch(get_type_tpop_code(tp)) {
139   case tpo_class:       { free_class_entities(tp);       } break;
140   case tpo_struct:      { free_struct_entities(tp);      } break;
141   case tpo_method:      { free_method_entities(tp);      } break;
142   case tpo_union:       { free_union_entities(tp);       } break;
143   case tpo_array:       { free_array_entities(tp);       } break;
144   case tpo_enumeration: { free_enumeration_entities(tp); } break;
145   case tpo_pointer:     { free_pointer_entities(tp);     } break;
146   case tpo_primitive:   { free_primitive_entities(tp);   } break;
147   default: break;
148   }
149 }
150
151 void free_type_attrs(type *tp) {
152   switch(get_type_tpop_code(tp)) {
153   case tpo_class:       { free_class_attrs(tp);       } break;
154   case tpo_struct:      { free_struct_attrs(tp);      } break;
155   case tpo_method:      { free_method_attrs(tp);      } break;
156   case tpo_union:       { free_union_attrs(tp);       } break;
157   case tpo_array:       { free_array_attrs(tp);       } break;
158   case tpo_enumeration: { free_enumeration_attrs(tp); } break;
159   case tpo_pointer:     { free_pointer_attrs(tp);     } break;
160   case tpo_primitive:   { free_primitive_attrs(tp);   } break;
161   default: break;
162   }
163 }
164
165 /* set/get the link field */
166 void *(get_type_link)(type *tp)
167 {
168   return __get_type_link(tp);
169 }
170
171 void (set_type_link)(type *tp, void *l)
172 {
173   __set_type_link(tp, l);
174 }
175
176 tp_op *(get_type_tpop)(type *tp) {
177   return __get_type_tpop(tp);
178 }
179
180 ident *(get_type_tpop_nameid)(type *tp) {
181   return __get_type_tpop_nameid(tp);
182 }
183
184 const char* get_type_tpop_name(type *tp) {
185   assert(tp && tp->kind == k_type);
186   return get_id_str(tp->type_op->name);
187 }
188
189 tp_opcode (get_type_tpop_code)(type *tp) {
190   return __get_type_tpop_code(tp);
191 }
192
193 ir_mode *(get_type_mode)(type *tp) {
194   return __get_type_mode(tp);
195 }
196
197 void        set_type_mode(type *tp, ir_mode* m) {
198   assert(tp && tp->kind == k_type);
199
200   assert(((tp->type_op != type_primitive)   || mode_is_data(m))     &&
201          /* Modes of primitives must be data */
202          ((tp->type_op != type_enumeration) || mode_is_int(m))      &&
203          /* Modes of enumerations must be integers */
204          ((tp->type_op != type_pointer)     || mode_is_reference(m))   );
205          /* Modes of pointers must be references. */
206
207   switch (get_type_tpop_code(tp)) {
208   case tpo_primitive:
209     /* For primitive size depends on the mode. */
210     tp->size = get_mode_size_bits(m);
211     tp->mode = m;
212     break;
213   case tpo_enumeration:
214   case tpo_pointer:
215     /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
216     assert((get_mode_size_bits(m) & 7) == 0 && "unorthodox modes not implemented");
217     tp->size = get_mode_size_bits(m);
218     tp->mode = m;
219     break;
220   case tpo_struct:
221   case tpo_class:
222     /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
223     assert(get_type_state(tp) == layout_fixed &&
224            tp->size == get_mode_size_bits(m) &&
225            "mode don't match struct/class layout");
226     tp->mode = m;
227     break;
228   default:
229     assert(0 && "setting a mode is NOT allowed for this type");
230   }
231 }
232
233 ident *(get_type_ident)(type *tp) {
234   return __get_type_ident(tp);
235 }
236
237 void (set_type_ident)(type *tp, ident* id) {
238   __set_type_ident(tp, id);
239 }
240
241 /* Outputs a unique number for this node */
242 long (get_type_nr)(type *tp) {
243   return __get_type_nr(tp);
244 }
245
246 const char* get_type_name(type *tp) {
247   assert(tp && tp->kind == k_type);
248   return (get_id_str(tp->name));
249 }
250
251 int (get_type_size_bytes)(type *tp) {
252   return __get_type_size_bytes(tp);
253 }
254
255 int (get_type_size_bits)(type *tp) {
256   return __get_type_size_bits(tp);
257 }
258
259 void
260 set_type_size_bits(type *tp, int size) {
261   assert(tp && tp->kind == k_type);
262   /* For pointer enumeration and primitive size depends on the mode.
263      Methods don't have a size. */
264   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
265       (tp->type_op != type_enumeration) && (tp->type_op != type_method)) {
266     if (tp->type_op == type_primitive)
267       tp->size = size;
268     else {
269       /* argh: we must allow to set negative values as "invalid size" */
270       tp->size = (size >= 0) ? (size + 7) & ~7 : size;
271       assert(tp->size == size && "setting a bit size is NOT allowed for this type");
272     }
273   }
274 }
275
276 void
277 set_type_size_bytes(type *tp, int size) {
278   set_type_size_bits(tp, 8*size);
279 }
280
281 type_state (get_type_state)(type *tp) {
282   return __get_type_state(tp);
283 }
284
285 void
286 set_type_state(type *tp, type_state state) {
287   assert(tp && tp->kind == k_type);
288
289   if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
290       (tp->type_op == type_method))
291     return;
292
293   /* Just a correctness check: */
294   if (state == layout_fixed) {
295     int i;
296     switch (get_type_tpop_code(tp)) {
297     case tpo_class:
298       {
299         assert(get_type_size_bits(tp) > -1);
300         if (tp != get_glob_type()) {
301           int n_mem = get_class_n_members(tp);
302           for (i = 0; i < n_mem; i++) {
303             if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
304               { DDMT(tp); DDME(get_class_member(tp, i)); }
305             assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
306             /* TR ??
307             assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
308                    (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
309                    */
310           }
311         }
312       } break;
313     case tpo_struct:
314       {
315         assert(get_type_size_bits(tp) > -1);
316         for (i = 0; i < get_struct_n_members(tp); i++) {
317           assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
318           assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
319         }
320       } break;
321     case tpo_union:
322       { /* ?? */
323       } break;
324     case tpo_array:
325       { /* ??
326          Check order?
327          Assure that only innermost dimension is dynamic? */
328       } break;
329     case tpo_enumeration:
330       {
331         assert(get_type_mode != NULL);
332         for (i = 0; i < get_enumeration_n_enums(tp); i++)
333           assert(get_enumeration_enum(tp, i) != NULL);
334       } break;
335     default: break;
336     } /* switch (tp) */
337   }
338   tp->state = state;
339 }
340
341 unsigned long (get_type_visited)(type *tp) {
342   return __get_type_visited(tp);
343 }
344
345 void (set_type_visited)(type *tp, unsigned long num) {
346   __set_type_visited(tp, num);
347 }
348
349 /* Sets visited field in type to type_visited. */
350 void (mark_type_visited)(type *tp) {
351   __mark_type_visited(tp);
352 }
353
354 /* @@@ name clash with master flag
355 int (type_visited)(type *tp) {
356   return __type_visited(tp);
357 }*/
358
359 int (type_not_visited)(type *tp) {
360   return __type_not_visited(tp);
361 }
362
363 int (is_type)(void *thing) {
364   return __is_type(thing);
365 }
366
367 bool equal_type(type *typ1, type *typ2) {
368   entity **m;
369   type **t;
370   int i, j;
371
372   if (typ1 == typ2) return true;
373
374   if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
375       (get_type_ident(typ1) != get_type_ident(typ2)) ||
376       (get_type_mode(typ1) != get_type_mode(typ2)) ||
377       (get_type_state(typ1) != get_type_state(typ2)))
378     return false;
379   if ((get_type_state(typ1) == layout_fixed) &&
380       (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
381     return false;
382
383   switch(get_type_tpop_code(typ1)) {
384   case tpo_class:       {
385     if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
386     if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
387     if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
388     if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
389     /** Compare the members **/
390     m = alloca(sizeof(entity *) * get_class_n_members(typ1));
391     memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
392     /* First sort the members of typ2 */
393     for (i = 0; i < get_class_n_members(typ1); i++) {
394       entity *e1 = get_class_member(typ1, i);
395       for (j = 0; j < get_class_n_members(typ2); j++) {
396         entity *e2 = get_class_member(typ2, j);
397         if (get_entity_name(e1) == get_entity_name(e2))
398           m[i] = e2;
399       }
400     }
401     for (i = 0; i < get_class_n_members(typ1); i++) {
402       if (!m[i]  ||  /* Found no counterpart */
403           !equal_entity(get_class_member(typ1, i), m[i]))
404         return false;
405     }
406     /** Compare the supertypes **/
407     t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
408     memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
409     /* First sort the supertypes of typ2 */
410     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
411       type *t1 = get_class_supertype(typ1, i);
412       for (j = 0; j < get_class_n_supertypes(typ2); j++) {
413         type *t2 = get_class_supertype(typ2, j);
414         if (get_type_ident(t2) == get_type_ident(t1))
415           t[i] = t2;
416       }
417     }
418     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
419       if (!t[i]  ||  /* Found no counterpart */
420           get_class_supertype(typ1, i) != t[i])
421         return false;
422     }
423   } break;
424   case tpo_struct:      {
425     if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
426     m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
427     memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
428     /* First sort the members of lt */
429     for (i = 0; i < get_struct_n_members(typ1); i++) {
430       entity *e1 = get_struct_member(typ1, i);
431       for (j = 0; j < get_struct_n_members(typ2); j++) {
432         entity *e2 = get_struct_member(typ2, j);
433         if (get_entity_name(e1) == get_entity_name(e2))
434           m[i] = e2;
435       }
436     }
437     for (i = 0; i < get_struct_n_members(typ1); i++) {
438       if (!m[i]  ||  /* Found no counterpart */
439           !equal_entity(get_struct_member(typ1, i), m[i]))
440         return false;
441     }
442   } break;
443   case tpo_method:      {
444     int n_param1, n_param2;
445
446     if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
447     if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return false;
448
449     if (get_method_variadicity(typ1) == variadicity_non_variadic) {
450       n_param1 = get_method_n_params(typ1);
451       n_param2 = get_method_n_params(typ2);
452     }
453     else {
454       n_param1 = get_method_first_variadic_param_index(typ1);
455       n_param2 = get_method_first_variadic_param_index(typ2);
456     }
457
458     if (n_param1 != n_param2) return false;
459
460     for (i = 0; i < n_param1; i++) {
461       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
462         return false;
463     }
464     for (i = 0; i < get_method_n_ress(typ1); i++) {
465       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
466         return false;
467     }
468   } break;
469   case tpo_union:       {
470     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
471     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
472     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
473     /* First sort the members of lt */
474     for (i = 0; i < get_union_n_members(typ1); i++) {
475       entity *e1 = get_union_member(typ1, i);
476       for (j = 0; j < get_union_n_members(typ2); j++) {
477         entity *e2 = get_union_member(typ2, j);
478         if (get_entity_name(e1) == get_entity_name(e2))
479           m[i] = e2;
480       }
481     }
482     for (i = 0; i < get_union_n_members(typ1); i++) {
483       if (!m[i]  ||  /* Found no counterpart */
484           !equal_entity(get_union_member(typ1, i), m[i]))
485         return false;
486     }
487   } break;
488   case tpo_array:       {
489     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
490       return false;
491     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
492       return false;
493     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
494       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
495           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
496         return false;
497       if (get_array_order(typ1, i) != get_array_order(typ2, i))
498         assert(0 && "type compare with different dimension orders not implemented");
499     }
500   } break;
501   case tpo_enumeration: {
502     assert(0 && "enumerations not implemented");
503   } break;
504   case tpo_pointer:     {
505     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
506       return false;
507   } break;
508   case tpo_primitive:   {
509   } break;
510   default: break;
511   }
512   return true;
513 }
514
515 bool smaller_type (type *st, type *lt) {
516   entity **m;
517   int i, j;
518
519   if (st == lt) return true;
520
521   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
522     return false;
523
524   switch(get_type_tpop_code(st)) {
525   case tpo_class:       {
526     return is_subclass_of(st, lt);
527   } break;
528   case tpo_struct:      {
529     if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
530     m = alloca(sizeof(entity *) * get_struct_n_members(st));
531     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
532     /* First sort the members of lt */
533     for (i = 0; i < get_struct_n_members(st); i++) {
534       entity *se = get_struct_member(st, i);
535       for (j = 0; j < get_struct_n_members(lt); j++) {
536         entity *le = get_struct_member(lt, j);
537         if (get_entity_name(le) == get_entity_name(se))
538           m[i] = le;
539       }
540     }
541     for (i = 0; i < get_struct_n_members(st); i++) {
542       if (!m[i]  ||  /* Found no counterpart */
543           !smaller_type(get_entity_type(get_struct_member(st, i)),
544                         get_entity_type(m[i])))
545         return false;
546     }
547   } break;
548   case tpo_method:      {
549     /** FIXME: is this still true? */
550     if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
551     if (get_method_n_params(st) != get_method_n_params(lt)) return false;
552     if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
553     for (i = 0; i < get_method_n_params(st); i++) {
554       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
555         return false;
556     }
557     for (i = 0; i < get_method_n_ress(st); i++) {
558       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
559         return false;
560     }
561   } break;
562   case tpo_union:       {
563     if (get_union_n_members(st) != get_union_n_members(lt)) return false;
564     m = alloca(sizeof(entity *) * get_union_n_members(st));
565     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
566     /* First sort the members of lt */
567     for (i = 0; i < get_union_n_members(st); i++) {
568       entity *se = get_union_member(st, i);
569       for (j = 0; j < get_union_n_members(lt); j++) {
570         entity *le = get_union_member(lt, j);
571         if (get_entity_name(le) == get_entity_name(se))
572           m[i] = le;
573       }
574     }
575     for (i = 0; i < get_union_n_members(st); i++) {
576       if (!m[i]  ||  /* Found no counterpart */
577           !smaller_type(get_entity_type(get_union_member(st, i)),
578                         get_entity_type(m[i])))
579         return false;
580     }
581   } break;
582   case tpo_array:       {
583     type *set, *let;  /* small/large elt. type */
584     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
585       return false;
586     set = get_array_element_type(st);
587     let = get_array_element_type(lt);
588     if (set != let) {
589       /* If the elt types are different, set must be convertible
590          to let, and they must have the same size so that address
591          computations work out.  To have a size the layout must
592          be fixed. */
593       if ((get_type_state(set) != layout_fixed) ||
594           (get_type_state(let) != layout_fixed))
595         return false;
596       if (!smaller_type(set, let) ||
597           get_type_size_bits(set) != get_type_size_bits(let))
598         return false;
599     }
600     for(i = 0; i < get_array_n_dimensions(st); i++) {
601       if (get_array_lower_bound(lt, i))
602         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
603           return false;
604       if (get_array_upper_bound(lt, i))
605         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
606           return false;
607     }
608   } break;
609   case tpo_enumeration: {
610     assert(0 && "enumerations not implemented");
611   } break;
612   case tpo_pointer:     {
613     if (!smaller_type(get_pointer_points_to_type(st),
614                       get_pointer_points_to_type(lt)))
615       return false;
616   } break;
617   case tpo_primitive:   {
618     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
619       return false;
620   } break;
621   default: break;
622   }
623   return true;
624 }
625
626 /*-----------------------------------------------------------------*/
627 /* TYPE_CLASS                                                      */
628 /*-----------------------------------------------------------------*/
629
630 /* create a new class type */
631 type   *new_type_class (ident *name) {
632   type *res;
633
634   res = new_type(type_class, NULL, name);
635
636   res->attr.ca.members     = NEW_ARR_F (entity *, 1);
637   res->attr.ca.subtypes    = NEW_ARR_F (type *, 1);
638   res->attr.ca.supertypes  = NEW_ARR_F (type *, 1);
639   res->attr.ca.peculiarity = peculiarity_existent;
640   res->attr.ca.dfn         = 0;
641
642   return res;
643 }
644 type   *new_d_type_class (ident *name, dbg_info* db) {
645   type *res = new_type_class (name);
646   set_type_dbg_info(res, db);
647   return res;
648 }
649
650 void free_class_entities(type *clss) {
651   int i;
652   assert(clss && (clss->type_op == type_class));
653   for (i = get_class_n_members(clss)-1; i >= 0; --i)
654     free_entity(get_class_member(clss, i));
655 }
656
657 void free_class_attrs(type *clss) {
658   assert(clss && (clss->type_op == type_class));
659   DEL_ARR_F(clss->attr.ca.members);
660   DEL_ARR_F(clss->attr.ca.subtypes);
661   DEL_ARR_F(clss->attr.ca.supertypes);
662 }
663
664 /* manipulate private fields of class type  */
665 void    add_class_member   (type *clss, entity *member) {
666   assert(clss && (clss->type_op == type_class));
667   ARR_APP1 (entity *, clss->attr.ca.members, member);
668 }
669
670 int     (get_class_n_members) (type *clss) {
671   return __get_class_n_members(clss);
672 }
673
674 int     get_class_member_index(type *clss, entity *mem) {
675   int i;
676   assert(clss && (clss->type_op == type_class));
677   for (i = 0; i < get_class_n_members(clss); i++)
678     if (get_class_member(clss, i) == mem)
679       return i;
680   return -1;
681 }
682
683 entity *(get_class_member)   (type *clss, int pos) {
684   return __get_class_member(clss, pos);
685 }
686
687 entity *get_class_member_by_name(type *clss, ident *name) {
688   int i, n_mem;
689   assert(clss && (clss->type_op == type_class));
690   n_mem = get_class_n_members(clss);
691   for (i = 0; i < n_mem; ++i) {
692     entity *mem = get_class_member(clss, i);
693     if (get_entity_ident(mem) == name) return mem;
694   }
695   return NULL;
696 }
697
698 void    set_class_member   (type *clss, entity *member, int pos) {
699   assert(clss && (clss->type_op == type_class));
700   assert(pos >= 0 && pos < get_class_n_members(clss));
701   clss->attr.ca.members[pos+1] = member;
702 }
703 void    set_class_members  (type *clss, entity **members, int arity) {
704   int i;
705   assert(clss && (clss->type_op == type_class));
706   DEL_ARR_F(clss->attr.ca.members);
707   clss->attr.ca.members    = NEW_ARR_F (entity *, 1);
708   for (i = 0; i < arity; i++) {
709     set_entity_owner(members[i], clss);
710     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
711   }
712 }
713 void    remove_class_member(type *clss, entity *member) {
714   int i;
715   assert(clss && (clss->type_op == type_class));
716   for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
717     if (clss->attr.ca.members[i] == member) {
718       for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
719         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
720       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
721       break;
722     }
723   }
724 }
725
726 void    add_class_subtype   (type *clss, type *subtype) {
727   int i;
728   assert(clss && (clss->type_op == type_class));
729   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
730   for (i = 0; i < get_class_n_supertypes(subtype); i++)
731     if (get_class_supertype(subtype, i) == clss)
732       /* Class already registered */
733       return;
734   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
735 }
736 int     get_class_n_subtypes (type *clss) {
737   assert(clss && (clss->type_op == type_class));
738   return (ARR_LEN (clss->attr.ca.subtypes))-1;
739 }
740 type   *get_class_subtype   (type *clss, int pos) {
741   assert(clss && (clss->type_op == type_class));
742   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
743   return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
744 }
745 void    set_class_subtype   (type *clss, type *subtype, int pos) {
746   assert(clss && (clss->type_op == type_class));
747   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
748   clss->attr.ca.subtypes[pos+1] = subtype;
749 }
750 void    remove_class_subtype(type *clss, type *subtype) {
751   int i;
752   assert(clss && (clss->type_op == type_class));
753   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
754     if (clss->attr.ca.subtypes[i] == subtype) {
755       for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
756         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
757       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
758       break;
759     }
760 }
761
762 void    add_class_supertype   (type *clss, type *supertype) {
763   int i;
764   assert(clss && (clss->type_op == type_class));
765   assert(supertype && (supertype -> type_op == type_class));
766   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
767   for (i = 0; i < get_class_n_subtypes(supertype); i++)
768     if (get_class_subtype(supertype, i) == clss)
769       /* Class already registered */
770       return;
771   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
772 }
773 int     get_class_n_supertypes (type *clss) {
774   assert(clss && (clss->type_op == type_class));
775   return (ARR_LEN (clss->attr.ca.supertypes))-1;
776 }
777 int get_class_supertype_index(type *clss, type *super_clss) {
778   int i;
779   assert(clss && (clss->type_op == type_class));
780   assert(super_clss && (super_clss->type_op == type_class));
781   for (i = 0; i < get_class_n_supertypes(clss); i++)
782     if (get_class_supertype(clss, i) == super_clss)
783       return i;
784   return -1;
785 }
786 type   *get_class_supertype   (type *clss, int pos) {
787   assert(clss && (clss->type_op == type_class));
788   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
789   return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
790 }
791 void    set_class_supertype   (type *clss, type *supertype, int pos) {
792   assert(clss && (clss->type_op == type_class));
793   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
794   clss->attr.ca.supertypes[pos+1] = supertype;
795 }
796 void    remove_class_supertype(type *clss, type *supertype) {
797   int i;
798   assert(clss && (clss->type_op == type_class));
799   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
800     if (clss->attr.ca.supertypes[i] == supertype) {
801       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
802         clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
803       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
804       break;
805     }
806 }
807
808 char *get_peculiarity_string(peculiarity p) {
809   if (p == peculiarity_description)
810     return "peculiarity_description";
811   if (p == peculiarity_inherited)
812     return "peculiarity_inherited";
813   return "peculiarity_existent";
814 }
815
816 peculiarity get_class_peculiarity (type *clss) {
817   assert(clss && (clss->type_op == type_class));
818   return clss->attr.ca.peculiarity;
819 }
820 void        set_class_peculiarity (type *clss, peculiarity pec) {
821   assert(clss && (clss->type_op == type_class));
822   assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
823   clss->attr.ca.peculiarity = pec;
824 }
825
826 void set_class_dfn (type *clss, int dfn)
827 {
828   clss->attr.ca.dfn        = dfn;
829 }
830
831 int get_class_dfn (type *clss)
832 {
833   return (clss->attr.ca.dfn);
834 }
835
836 /* typecheck */
837 int (is_class_type)(type *clss) {
838   return __is_class_type(clss);
839 }
840
841 bool is_subclass_of(type *low, type *high) {
842   int i;
843   assert(is_class_type(low) && is_class_type(high));
844   if (low == high) return true;
845   /* depth first search from high downwards. */
846   for (i = 0; i < get_class_n_subtypes(high); i++) {
847     if (low == get_class_subtype(high, i))
848       return true;
849     if (is_subclass_of(low, get_class_subtype(high, i)))
850       return true;
851   }
852   return false;
853 }
854
855 /*----------------------------------------------------------------**/
856 /* TYPE_STRUCT                                                     */
857 /*----------------------------------------------------------------**/
858
859 /* create a new type struct */
860 type   *new_type_struct (ident *name) {
861   type *res;
862   res = new_type(type_struct, NULL, name);
863   res->attr.sa.members = NEW_ARR_F (entity *, 1);
864   return res;
865 }
866 type   *new_d_type_struct (ident *name, dbg_info* db) {
867   type *res = new_type_struct (name);
868   set_type_dbg_info(res, db);
869   return res;
870 }
871 void free_struct_entities (type *strct) {
872   int i;
873   assert(strct && (strct->type_op == type_struct));
874   for (i = get_struct_n_members(strct)-1; i >= 0; --i)
875     free_entity(get_struct_member(strct, i));
876 }
877 void free_struct_attrs (type *strct) {
878   assert(strct && (strct->type_op == type_struct));
879   DEL_ARR_F(strct->attr.sa.members);
880 }
881
882 /* manipulate private fields of struct */
883 int     get_struct_n_members (type *strct) {
884   assert(strct && (strct->type_op == type_struct));
885   return (ARR_LEN (strct->attr.sa.members))-1;
886 }
887
888 void    add_struct_member   (type *strct, entity *member) {
889   assert(strct && (strct->type_op == type_struct));
890   assert(get_type_tpop(get_entity_type(member)) != type_method);
891     /*    @@@ lowerfirm geht nicht durch */
892   ARR_APP1 (entity *, strct->attr.sa.members, member);
893 }
894
895 entity *get_struct_member   (type *strct, int pos) {
896   assert(strct && (strct->type_op == type_struct));
897   assert(pos >= 0 && pos < get_struct_n_members(strct));
898   return strct->attr.sa.members[pos+1];
899 }
900
901 int     get_struct_member_index(type *strct, entity *mem) {
902   int i;
903   assert(strct && (strct->type_op == type_struct));
904   for (i = 0; i < get_struct_n_members(strct); i++)
905     if (get_struct_member(strct, i) == mem)
906       return i;
907   return -1;
908 }
909
910 void    set_struct_member   (type *strct, int pos, entity *member) {
911   assert(strct && (strct->type_op == type_struct));
912   assert(pos >= 0 && pos < get_struct_n_members(strct));
913   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
914   strct->attr.sa.members[pos+1] = member;
915 }
916 void    remove_struct_member(type *strct, entity *member) {
917   int i;
918   assert(strct && (strct->type_op == type_struct));
919   for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
920     if (strct->attr.sa.members[i] == member) {
921       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
922         strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
923       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
924       break;
925     }
926 }
927
928 /* typecheck */
929 int (is_struct_type)(type *strct) {
930   return __is_struct_type(strct);
931 }
932
933 /*******************************************************************/
934 /** TYPE_METHOD                                                   **/
935 /*******************************************************************/
936
937 /**
938  * Lazy construction of value argument / result representation.
939  * Constructs a struct type and its member.  The types of the members
940  * are passed in the argument list.
941  *
942  * @param name    name of the type constructed
943  * @param len     number of fields
944  * @param tps     array of field types with length len
945  */
946 static INLINE type *
947 build_value_type(ident *name, int len, type **tps) {
948   int i;
949   type *res = new_type_struct(name);
950   /* Remove type from type list.  Must be treated differently than other types. */
951   remove_irp_type_from_list(res);
952   for (i = 0; i < len; i++) {
953     type *elt_type = res;   /* use res as default if corresponding type is not yet set. */
954     if (tps[i]) elt_type = tps[i];
955     new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
956   }
957   return res;
958 }
959
960 /* Create a new method type.
961    N_param is the number of parameters, n_res the number of results.  */
962 type *new_type_method (ident *name, int n_param, int n_res) {
963   type *res;
964
965   assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
966   res = new_type(type_method, mode_P_mach, name);
967   res->state                        = layout_fixed;
968   res->size                         = get_mode_size_bits(mode_P_mach);
969   res->attr.ma.n_params             = n_param;
970   res->attr.ma.param_type           = (type **) xmalloc (sizeof (type *) * n_param);
971   res->attr.ma.value_params         = NULL;
972   res->attr.ma.n_res                = n_res;
973   res->attr.ma.res_type             = (type **) xmalloc (sizeof (type *) * n_res);
974   res->attr.ma.value_ress           = NULL;
975   res->attr.ma.variadicity          = variadicity_non_variadic;
976   res->attr.ma.first_variadic_param = -1;
977
978   return res;
979 }
980
981 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
982   type *res = new_type_method (name, n_param, n_res);
983   set_type_dbg_info(res, db);
984   return res;
985 }
986
987 void free_method_entities(type *method) {
988   assert(method && (method->type_op == type_method));
989 }
990
991 /* Attention: also frees entities in value parameter subtypes! */
992 void free_method_attrs(type *method) {
993   assert(method && (method->type_op == type_method));
994   free(method->attr.ma.param_type);
995   free(method->attr.ma.res_type);
996   if (method->attr.ma.value_params) {
997     free_type_entities(method->attr.ma.value_params);
998     free_type(method->attr.ma.value_params);
999   }
1000   if (method->attr.ma.value_ress) {
1001     free_type_entities(method->attr.ma.value_ress);
1002     free_type(method->attr.ma.value_ress);
1003   }
1004 }
1005
1006 /* manipulate private fields of method. */
1007 int   get_method_n_params  (type *method) {
1008   assert(method && (method->type_op == type_method));
1009   return method->attr.ma.n_params;
1010 }
1011
1012 type *get_method_param_type(type *method, int pos) {
1013   type *res;
1014   assert(method && (method->type_op == type_method));
1015   assert(pos >= 0 && pos < get_method_n_params(method));
1016   res = method->attr.ma.param_type[pos];
1017   assert(res != NULL && "empty method param type");
1018   return method->attr.ma.param_type[pos] = skip_tid(res);
1019 }
1020
1021 void  set_method_param_type(type *method, int pos, type* tp) {
1022   assert(method && (method->type_op == type_method));
1023   assert(pos >= 0 && pos < get_method_n_params(method));
1024   method->attr.ma.param_type[pos] = tp;
1025   /* If information constructed set pass-by-value representation. */
1026   if (method->attr.ma.value_params) {
1027     assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1028     set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1029   }
1030 }
1031
1032 /* Returns an entity that represents the copied value argument.  Only necessary
1033    for compounds passed by value. */
1034 entity *get_method_value_param_ent(type *method, int pos) {
1035   assert(method && (method->type_op == type_method));
1036   assert(pos >= 0 && pos < get_method_n_params(method));
1037   if (!method->attr.ma.value_params)
1038     method->attr.ma.value_params
1039       = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1040                          get_method_n_params(method), method->attr.ma.param_type);
1041   assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos))
1042           != method->attr.ma.value_params)
1043          && "param type not yet set");
1044   return get_struct_member(method->attr.ma.value_params, pos);
1045 }
1046
1047 /*
1048  * Returns a type that represents the copied value arguments.
1049  */
1050 type *get_method_value_param_type(type *method)
1051 {
1052   assert(method && (method->type_op == type_method));
1053   return method->attr.ma.value_params;
1054 }
1055
1056 int   get_method_n_ress   (type *method) {
1057   assert(method && (method->type_op == type_method));
1058   return method->attr.ma.n_res;
1059 }
1060
1061 type *get_method_res_type(type *method, int pos) {
1062   type *res;
1063   assert(method && (method->type_op == type_method));
1064   assert(pos >= 0 && pos < get_method_n_ress(method));
1065   res = method->attr.ma.res_type[pos];
1066   assert(res != NULL && "empty method return type");
1067   return method->attr.ma.res_type[pos] = skip_tid(res);
1068 }
1069
1070 void  set_method_res_type(type *method, int pos, type* tp) {
1071   assert(method && (method->type_op == type_method));
1072   assert(pos >= 0 && pos < get_method_n_ress(method));
1073   /* set the result type */
1074   method->attr.ma.res_type[pos] = tp;
1075   /* If information constructed set pass-by-value representation. */
1076   if (method->attr.ma.value_ress) {
1077     assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1078     set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1079   }
1080 }
1081
1082 /* Returns an entity that represents the copied value result.  Only necessary
1083    for compounds passed by value. */
1084 entity *get_method_value_res_ent(type *method, int pos) {
1085   assert(method && (method->type_op == type_method));
1086   assert(pos >= 0 && pos < get_method_n_ress(method));
1087   if (!method->attr.ma.value_ress)
1088     method->attr.ma.value_ress
1089       = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1090                          get_method_n_ress(method), method->attr.ma.res_type);
1091   assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
1092          && "result type not yet set");
1093   return get_struct_member(method->attr.ma.value_ress, pos);
1094 }
1095
1096 /*
1097  * Returns a type that represents the copied value results.
1098  */
1099 type *get_method_value_res_type(type *method) {
1100   assert(method && (method->type_op == type_method));
1101   return method->attr.ma.value_ress;
1102 }
1103
1104 /* Returns the null-terminated name of this variadicity. */
1105 const char *get_variadicity_name(variadicity vari)
1106 {
1107 #define X(a)    case a: return #a
1108   switch (vari) {
1109     X(variadicity_non_variadic);
1110     X(variadicity_variadic);
1111     default:
1112       return "BAD VALUE";
1113   }
1114 #undef X
1115 }
1116
1117 variadicity get_method_variadicity(type *method)
1118 {
1119   assert(method && (method->type_op == type_method));
1120   return method->attr.ma.variadicity;
1121 }
1122
1123 void set_method_variadicity(type *method, variadicity vari)
1124 {
1125   assert(method && (method->type_op == type_method));
1126   method->attr.ma.variadicity = vari;
1127 }
1128
1129 /*
1130  * Returns the first variadic parameter index of a type.
1131  * If this index was NOT set, the index of the last parameter
1132  * of the method type plus one is returned for variadic functions.
1133  * Non-variadic function types always return -1 here.
1134  */
1135 int get_method_first_variadic_param_index(type *method)
1136 {
1137   assert(method && (method->type_op == type_method));
1138
1139   if (method->attr.ma.variadicity == variadicity_non_variadic)
1140     return -1;
1141
1142   if (method->attr.ma.first_variadic_param == -1)
1143     return get_method_n_params(method);
1144   return method->attr.ma.first_variadic_param;
1145 }
1146
1147 /*
1148  * Sets the first variadic parameter index. This allows to specify
1149  * a complete call type (containing the type of all parameters)
1150  * but still have the knowledge, which parameter must be passed as
1151  * variadic one.
1152  */
1153 void set_method_first_variadic_param_index(type *method, int index)
1154 {
1155   assert(method && (method->type_op == type_method));
1156   assert(index >= 0 && index <= get_method_n_params(method));
1157
1158   method->attr.ma.first_variadic_param = index;
1159 }
1160
1161 /* typecheck */
1162 int (is_method_type)(type *method) {
1163   return __is_method_type(method);
1164 }
1165
1166 /*-----------------------------------------------------------------*/
1167 /* TYPE_UNION                                                      */
1168 /*-----------------------------------------------------------------*/
1169
1170 /* create a new type uni */
1171 type  *new_type_union (ident *name) {
1172   type *res;
1173   res = new_type(type_union, NULL, name);
1174   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
1175     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
1176   res->attr.ua.members = NEW_ARR_F (entity *, 1);
1177   return res;
1178 }
1179 type  *new_d_type_union (ident *name, dbg_info* db) {
1180   type *res = new_type_union (name);
1181   set_type_dbg_info(res, db);
1182   return res;
1183 }
1184 void free_union_entities (type *uni) {
1185   int i;
1186   assert(uni && (uni->type_op == type_union));
1187   for (i = get_union_n_members(uni)-1; i >= 0; --i)
1188     free_entity(get_union_member(uni, i));
1189 }
1190 void free_union_attrs (type *uni) {
1191   assert(uni && (uni->type_op == type_union));
1192   DEL_ARR_F(uni->attr.ua.members);
1193 }
1194 /* manipulate private fields of union */
1195 #if 0
1196 int    get_union_n_types      (type *uni) {
1197   assert(uni && (uni->type_op == type_union));
1198   return uni->attr.ua.n_types;
1199 }
1200 type  *get_union_unioned_type (type *uni, int pos) {
1201   assert(uni && (uni->type_op == type_union));
1202   assert(pos >= 0 && pos < get_union_n_types(uni));
1203   return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
1204 }
1205 void   set_union_unioned_type (type *uni, int pos, type *tp) {
1206   assert(uni && (uni->type_op == type_union));
1207   assert(pos >= 0 && pos < get_union_n_types(uni));
1208   uni->attr.ua.unioned_type[pos] = tp;
1209 }
1210 ident *get_union_delim_nameid (type *uni, int pos) {
1211   assert(uni && (uni->type_op == type_union));
1212   assert(pos >= 0 && pos < get_union_n_types(uni));
1213   return uni->attr.ua.delim_names[pos];
1214 }
1215 const char *get_union_delim_name (type *uni, int pos) {
1216   assert(uni && (uni->type_op == type_union));
1217   assert(pos >= 0 && pos < get_union_n_types(uni));
1218   return get_id_str(uni->attr.ua.delim_names[pos]);
1219 }
1220 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
1221   assert(uni && (uni->type_op == type_union));
1222   assert(pos >= 0 && pos < get_union_n_types(uni));
1223   uni->attr.ua.delim_names[pos] = id;
1224 }
1225 #endif
1226 int    get_union_n_members      (type *uni) {
1227   assert(uni && (uni->type_op == type_union));
1228   return (ARR_LEN (uni->attr.ua.members))-1;
1229 }
1230 void    add_union_member   (type *uni, entity *member) {
1231   assert(uni && (uni->type_op == type_union));
1232   ARR_APP1 (entity *, uni->attr.ua.members, member);
1233 }
1234 entity  *get_union_member (type *uni, int pos) {
1235   assert(uni && (uni->type_op == type_union));
1236   assert(pos >= 0 && pos < get_union_n_members(uni));
1237   return uni->attr.ua.members[pos+1];
1238 }
1239 void   set_union_member (type *uni, int pos, entity *member) {
1240   assert(uni && (uni->type_op == type_union));
1241   assert(pos >= 0 && pos < get_union_n_members(uni));
1242   uni->attr.ua.members[pos+1] = member;
1243 }
1244 void   remove_union_member(type *uni, entity *member) {
1245   int i;
1246   assert(uni && (uni->type_op == type_union));
1247   for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
1248     if (uni->attr.ua.members[i] == member) {
1249       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1250         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1251       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1252       break;
1253     }
1254 }
1255
1256 /* typecheck */
1257 int (is_union_type)(type *uni) {
1258   return __is_union_type(uni);
1259 }
1260
1261 /*-----------------------------------------------------------------*/
1262 /* TYPE_ARRAY                                                      */
1263 /*-----------------------------------------------------------------*/
1264
1265
1266 /* create a new type array -- set dimension sizes independently */
1267 type *new_type_array         (ident *name, int n_dimensions,
1268                               type *element_type) {
1269   type *res;
1270   int i;
1271   ir_graph *rem = current_ir_graph;
1272   assert(!is_method_type(element_type));
1273
1274   res = new_type(type_array, NULL, name);
1275   res->attr.aa.n_dimensions = n_dimensions;
1276   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1277   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1278   res->attr.aa.order  = (int *) xmalloc (sizeof (int) * n_dimensions);
1279
1280   current_ir_graph = get_const_code_irg();
1281   for (i = 0; i < n_dimensions; i++) {
1282     res->attr.aa.lower_bound[i]  = new_Unknown(mode_Iu);
1283     res->attr.aa.upper_bound[i]  = new_Unknown(mode_Iu);
1284     res->attr.aa.order[i] = i;
1285   }
1286   current_ir_graph = rem;
1287
1288   res->attr.aa.element_type = element_type;
1289   new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
1290
1291   return res;
1292 }
1293
1294 type *new_d_type_array (ident *name, int n_dimensions,
1295                         type *element_type, dbg_info* db) {
1296   type *res = new_type_array (name, n_dimensions, element_type);
1297   set_type_dbg_info(res, db);
1298   return res;
1299 }
1300
1301 void free_array_entities (type *array) {
1302   assert(array && (array->type_op == type_array));
1303 }
1304
1305 void free_array_attrs (type *array) {
1306   assert(array && (array->type_op == type_array));
1307   free(array->attr.aa.lower_bound);
1308   free(array->attr.aa.upper_bound);
1309 }
1310
1311 /* manipulate private fields of array type */
1312 int   get_array_n_dimensions (type *array) {
1313   assert(array && (array->type_op == type_array));
1314   return array->attr.aa.n_dimensions;
1315 }
1316
1317 void
1318 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1319                   ir_node * upper_bound) {
1320   assert(array && (array->type_op == type_array));
1321   assert(lower_bound && "lower_bound node may not be NULL.");
1322   assert(upper_bound && "upper_bound node may not be NULL.");
1323   assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1324   array->attr.aa.lower_bound[dimension] = lower_bound;
1325   array->attr.aa.upper_bound[dimension] = upper_bound;
1326 }
1327 void
1328 set_array_bounds_int (type *array, int dimension, int lower_bound,
1329                       int upper_bound) {
1330   ir_graph *rem = current_ir_graph;
1331   current_ir_graph = get_const_code_irg();
1332   set_array_bounds (array, dimension,
1333                     new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1334                     new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1335   current_ir_graph = rem;
1336 }
1337 void
1338 set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
1339   assert(array && (array->type_op == type_array));
1340   assert(lower_bound && "lower_bound node may not be NULL.");
1341   array->attr.aa.lower_bound[dimension] = lower_bound;
1342 }
1343 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1344   ir_graph *rem = current_ir_graph;
1345   current_ir_graph = get_const_code_irg();
1346   set_array_lower_bound  (array, dimension,
1347                           new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1348   current_ir_graph = rem;
1349 }
1350 void
1351 set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
1352   assert(array && (array->type_op == type_array));
1353   assert(upper_bound && "upper_bound node may not be NULL.");
1354   array->attr.aa.upper_bound[dimension] = upper_bound;
1355 }
1356 void  set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1357   ir_graph *rem = current_ir_graph;
1358   current_ir_graph = get_const_code_irg();
1359   set_array_upper_bound  (array, dimension,
1360                           new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1361   current_ir_graph = rem;
1362 }
1363 int       has_array_lower_bound  (type *array, int dimension) {
1364   assert(array && (array->type_op == type_array));
1365   return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1366 }
1367 ir_node * get_array_lower_bound  (type *array, int dimension) {
1368   assert(array && (array->type_op == type_array));
1369   return array->attr.aa.lower_bound[dimension];
1370 }
1371 int       has_array_upper_bound  (type *array, int dimension) {
1372   assert(array && (array->type_op == type_array));
1373   return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1374 }
1375 ir_node * get_array_upper_bound  (type *array, int dimension) {
1376   assert(array && (array->type_op == type_array));
1377   return array->attr.aa.upper_bound[dimension];
1378 }
1379
1380 void set_array_order (type *array, int dimension, int order) {
1381   assert(array && (array->type_op == type_array));
1382   array->attr.aa.order[dimension] = order;
1383 }
1384 int  get_array_order (type *array, int dimension) {
1385   assert(array && (array->type_op == type_array));
1386   return array->attr.aa.order[dimension];
1387 }
1388
1389 void  set_array_element_type (type *array, type *tp) {
1390   assert(array && (array->type_op == type_array));
1391   assert(!is_method_type(tp));
1392   array->attr.aa.element_type = tp;
1393 }
1394 type *get_array_element_type (type *array) {
1395   assert(array && (array->type_op == type_array));
1396   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1397 }
1398
1399 void  set_array_element_entity (type *array, entity *ent) {
1400   assert(array && (array->type_op == type_array));
1401   assert((get_entity_type(ent)->type_op != type_method));
1402   array->attr.aa.element_ent = ent;
1403   array->attr.aa.element_type = get_entity_type(ent);
1404 }
1405 entity *get_array_element_entity (type *array) {
1406   assert(array && (array->type_op == type_array));
1407   return array->attr.aa.element_ent;
1408 }
1409
1410 /* typecheck */
1411 int (is_array_type)(type *array) {
1412   return __is_array_type(array);
1413 }
1414
1415 /*-----------------------------------------------------------------*/
1416 /* TYPE_ENUMERATION                                                */
1417 /*-----------------------------------------------------------------*/
1418
1419 /* create a new type enumeration -- set the enumerators independently */
1420 type   *new_type_enumeration    (ident *name, int n_enums) {
1421   type *res;
1422   res = new_type(type_enumeration, NULL, name);
1423   res->attr.ea.n_enums     = n_enums;
1424   res->attr.ea.enumer      = (tarval **)xmalloc(sizeof(res->attr.ea.enumer[0]) * n_enums);
1425   res->attr.ea.enum_nameid = (ident  **)xmalloc(sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1426   memset(res->attr.ea.enumer,      0, sizeof(res->attr.ea.enumer[0])      * n_enums);
1427   memset(res->attr.ea.enum_nameid, 0, sizeof(res->attr.ea.enum_nameid[0]) * n_enums);
1428   return res;
1429 }
1430 type   *new_d_type_enumeration    (ident *name, int n_enums, dbg_info* db) {
1431   type *res = new_type_enumeration (name, n_enums);
1432   set_type_dbg_info(res, db);
1433   return res;
1434 }
1435
1436 void free_enumeration_entities(type *enumeration) {
1437   assert(enumeration && (enumeration->type_op == type_enumeration));
1438 }
1439 void free_enumeration_attrs(type *enumeration) {
1440   assert(enumeration && (enumeration->type_op == type_enumeration));
1441   free(enumeration->attr.ea.enumer);
1442   free(enumeration->attr.ea.enum_nameid);
1443 }
1444
1445 /* manipulate fields of enumeration type. */
1446 int     get_enumeration_n_enums (type *enumeration) {
1447   assert(enumeration && (enumeration->type_op == type_enumeration));
1448   return enumeration->attr.ea.n_enums;
1449 }
1450 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
1451   assert(enumeration && (enumeration->type_op == type_enumeration));
1452   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1453   enumeration->attr.ea.enumer[pos] = con;
1454 }
1455 tarval *get_enumeration_enum    (type *enumeration, int pos) {
1456   assert(enumeration && (enumeration->type_op == type_enumeration));
1457   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1458   return enumeration->attr.ea.enumer[pos];
1459 }
1460 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
1461   assert(enumeration && (enumeration->type_op == type_enumeration));
1462   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1463   enumeration->attr.ea.enum_nameid[pos] = id;
1464 }
1465 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
1466   assert(enumeration && (enumeration->type_op == type_enumeration));
1467   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1468   return enumeration->attr.ea.enum_nameid[pos];
1469 }
1470 const char *get_enumeration_name(type *enumeration, int pos) {
1471   assert(enumeration && (enumeration->type_op == type_enumeration));
1472   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1473   return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1474 }
1475
1476 /* typecheck */
1477 int (is_enumeration_type)(type *enumeration) {
1478   return __is_enumeration_type(enumeration);
1479 }
1480
1481 /*-----------------------------------------------------------------*/
1482 /* TYPE_POINTER                                                    */
1483 /*-----------------------------------------------------------------*/
1484
1485 /* Create a new type pointer */
1486 type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1487   type *res;
1488   assert(mode_is_reference(ptr_mode));
1489   res = new_type(type_pointer, ptr_mode, name);
1490   res->attr.pa.points_to = points_to;
1491   assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1492   res->size = get_mode_size_bits(res->mode);
1493   res->state = layout_fixed;
1494   return res;
1495 }
1496 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1497   type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1498   set_type_dbg_info(res, db);
1499   return res;
1500 }
1501 void free_pointer_entities (type *pointer) {
1502   assert(pointer && (pointer->type_op == type_pointer));
1503 }
1504 void free_pointer_attrs (type *pointer) {
1505   assert(pointer && (pointer->type_op == type_pointer));
1506 }
1507 /* manipulate fields of type_pointer */
1508 void  set_pointer_points_to_type (type *pointer, type *tp) {
1509   assert(pointer && (pointer->type_op == type_pointer));
1510   pointer->attr.pa.points_to = tp;
1511 }
1512 type *get_pointer_points_to_type (type *pointer) {
1513   assert(pointer && (pointer->type_op == type_pointer));
1514   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1515 }
1516
1517 /* typecheck */
1518 int (is_pointer_type)(type *pointer) {
1519   return __is_pointer_type(pointer);
1520 }
1521
1522 /* Returns the first pointer type that has as points_to tp.
1523  *  Not efficient: O(#types).
1524  *  If not found returns unknown_type. */
1525 type *find_pointer_type_to_type (type *tp) {
1526   int i;
1527   for (i = 0; i < get_irp_n_types(); ++i) {
1528     type *found = get_irp_type(i);
1529     if (is_pointer_type(found) && get_pointer_points_to_type(found) == tp)
1530       return (found);
1531   }
1532   return unknown_type;
1533 }
1534
1535
1536
1537 /*-----------------------------------------------------------------*/
1538 /* TYPE_PRIMITIVE                                                  */
1539 /*-----------------------------------------------------------------*/
1540
1541 /* create a new type primitive */
1542 type *new_type_primitive (ident *name, ir_mode *mode) {
1543   type *res;
1544   /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1545   res = new_type(type_primitive, mode, name);
1546   res->size  = get_mode_size_bits(mode);
1547   res->state = layout_fixed;
1548   return res;
1549 }
1550 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1551   type *res = new_type_primitive (name, mode);
1552   set_type_dbg_info(res, db);
1553   return res;
1554 }
1555 void free_primitive_entities (type *primitive) {
1556   assert(primitive && (primitive->type_op == type_primitive));
1557 }
1558 void free_primitive_attrs (type *primitive) {
1559   assert(primitive && (primitive->type_op == type_primitive));
1560 }
1561
1562 /* typecheck */
1563 int (is_primitive_type)(type *primitive) {
1564   return __is_primitive_type(primitive);
1565 }
1566
1567 /*-----------------------------------------------------------------*/
1568 /* common functionality                                            */
1569 /*-----------------------------------------------------------------*/
1570
1571
1572 int (is_atomic_type)(type *tp) {
1573   return __is_atomic_type(tp);
1574 }
1575
1576 /*
1577  * Gets the number of elements in a firm compound type.
1578  */
1579 int get_compound_n_members(type *tp)
1580 {
1581   int res = 0;
1582
1583   if (is_struct_type(tp))
1584     res = get_struct_n_members(tp);
1585   else if (is_class_type(tp))
1586     res = get_class_n_members(tp);
1587   else if (is_union_type(tp))
1588     res = get_union_n_members(tp);
1589   else
1590     assert(0 && "need struct, union or class for member count");
1591
1592   return res;
1593 }
1594
1595 /*
1596  * Gets the member of a firm compound type at position pos.
1597  */
1598 entity *get_compound_member(type *tp, int pos)
1599 {
1600   entity *res;
1601
1602   if (is_struct_type(tp))
1603     res = get_struct_member(tp, pos);
1604   else if (is_class_type(tp))
1605     res = get_class_member(tp, pos);
1606   else if (is_union_type(tp))
1607     res = get_union_member(tp, pos);
1608   else
1609   {
1610     assert(0 && "need struct, union or class to get a member");
1611     res=NULL;
1612   }
1613
1614   return res;
1615 }
1616
1617
1618 int is_compound_type(type *tp) {
1619   assert(tp && tp->kind == k_type);
1620   return (is_class_type(tp) || is_struct_type(tp) ||
1621           is_array_type(tp) || is_union_type(tp));
1622 }
1623
1624
1625 #ifdef DEBUG_libfirm
1626 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
1627
1628 void dump_type (type *tp) {
1629   int i;
1630
1631   printf("%s type %s (%ld)", get_tpop_name(get_type_tpop(tp)), get_type_name(tp), get_type_nr(tp));
1632
1633   switch (get_type_tpop_code(tp)) {
1634
1635   case tpo_class:
1636     printf("\n  members: ");
1637     for (i = 0; i < get_class_n_members(tp); ++i) {
1638       entity *mem = get_class_member(tp, i);
1639       printf("\n    (%3d) %s:\t %s",
1640              get_entity_offset_bits(mem), get_type_name(get_entity_type(mem)), get_entity_name(mem));
1641     }
1642     printf("\n  supertypes: ");
1643     for (i = 0; i < get_class_n_supertypes(tp); ++i) {
1644       type *stp = get_class_supertype(tp, i);
1645       printf("\n    %s", get_type_name(stp));
1646     }
1647     printf("\n  subtypes: ");
1648     for (i = 0; i < get_class_n_subtypes(tp); ++i) {
1649       type *stp = get_class_subtype(tp, i);
1650       printf("\n    %s", get_type_name(stp));
1651     }
1652
1653     printf("\n  peculiarity: %s", get_peculiarity_string(get_class_peculiarity(tp)));
1654     break;
1655
1656   case tpo_union:
1657   case tpo_struct:
1658     printf("\n  members: ");
1659     for (i = 0; i < get_compound_n_members(tp); ++i) {
1660       entity *mem = get_compound_member(tp, i);
1661       printf("\n    (%3d) %s:\t %s",
1662              get_entity_offset_bits(mem), get_type_name(get_entity_type(mem)), get_entity_name(mem));
1663     }
1664     break;
1665
1666   case tpo_pointer: {
1667     type *tt = get_pointer_points_to_type(tp);
1668
1669     printf("\n  points to %s (%ld)", get_type_name(tt), get_type_nr(tt));
1670   } break;
1671
1672   default:
1673     printf(": details not implemented\n");
1674   }
1675   printf("\n\n");
1676 }
1677 #else  /* DEBUG_libfirm */
1678 void dump_type (type *tp) {}
1679 #endif /* DEBUG_libfirm */