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