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