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