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