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