create_abstraction() now returns an error code if the file could not
[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 /* @@@ name clash with master flag
499 int (type_visited)(const type *tp) {
500   return _type_visited(tp);
501 }*/
502
503 int (type_not_visited)(const type *tp) {
504   return _type_not_visited(tp);
505 }
506
507 int (is_type)(const void *thing) {
508   return _is_type(thing);
509 }
510
511 /* Checks whether two types are structural equal.*/
512 int equal_type(type *typ1, type *typ2) {
513   entity **m;
514   type **t;
515   int i, j;
516
517   if (typ1 == typ2) return 1;
518
519   if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
520       (get_type_ident(typ1) != get_type_ident(typ2)) ||
521       (get_type_mode(typ1) != get_type_mode(typ2)) ||
522       (get_type_state(typ1) != get_type_state(typ2)))
523     return 0;
524   if ((get_type_state(typ1) == layout_fixed) &&
525       (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
526     return 0;
527
528   switch(get_type_tpop_code(typ1)) {
529   case tpo_class:       {
530     if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
531     if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
532     if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
533     if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
534     /** Compare the members **/
535     m = alloca(sizeof(entity *) * get_class_n_members(typ1));
536     memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
537     /* First sort the members of typ2 */
538     for (i = 0; i < get_class_n_members(typ1); i++) {
539       entity *e1 = get_class_member(typ1, i);
540       for (j = 0; j < get_class_n_members(typ2); j++) {
541         entity *e2 = get_class_member(typ2, j);
542         if (get_entity_name(e1) == get_entity_name(e2))
543           m[i] = e2;
544       }
545     }
546     for (i = 0; i < get_class_n_members(typ1); i++) {
547       if (!m[i]  ||  /* Found no counterpart */
548           !equal_entity(get_class_member(typ1, i), m[i]))
549         return 0;
550     }
551     /** Compare the supertypes **/
552     t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
553     memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
554     /* First sort the supertypes of typ2 */
555     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
556       type *t1 = get_class_supertype(typ1, i);
557       for (j = 0; j < get_class_n_supertypes(typ2); j++) {
558         type *t2 = get_class_supertype(typ2, j);
559         if (get_type_ident(t2) == get_type_ident(t1))
560           t[i] = t2;
561       }
562     }
563     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
564       if (!t[i]  ||  /* Found no counterpart */
565           get_class_supertype(typ1, i) != t[i])
566         return 0;
567     }
568   } break;
569   case tpo_struct:      {
570     if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
571     m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
572     memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
573     /* First sort the members of lt */
574     for (i = 0; i < get_struct_n_members(typ1); i++) {
575       entity *e1 = get_struct_member(typ1, i);
576       for (j = 0; j < get_struct_n_members(typ2); j++) {
577         entity *e2 = get_struct_member(typ2, j);
578         if (get_entity_name(e1) == get_entity_name(e2))
579           m[i] = e2;
580       }
581     }
582     for (i = 0; i < get_struct_n_members(typ1); i++) {
583       if (!m[i]  ||  /* Found no counterpart */
584           !equal_entity(get_struct_member(typ1, i), m[i]))
585         return 0;
586     }
587   } break;
588   case tpo_method:      {
589     int n_param1, n_param2;
590
591     if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
592     if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return 0;
593
594     if (get_method_variadicity(typ1) == variadicity_non_variadic) {
595       n_param1 = get_method_n_params(typ1);
596       n_param2 = get_method_n_params(typ2);
597     }
598     else {
599       n_param1 = get_method_first_variadic_param_index(typ1);
600       n_param2 = get_method_first_variadic_param_index(typ2);
601     }
602
603     if (n_param1 != n_param2) return 0;
604
605     for (i = 0; i < n_param1; i++) {
606       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
607     return 0;
608     }
609     for (i = 0; i < get_method_n_ress(typ1); i++) {
610       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
611         return 0;
612     }
613   } break;
614   case tpo_union:       {
615     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
616     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
617     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
618     /* First sort the members of lt */
619     for (i = 0; i < get_union_n_members(typ1); i++) {
620       entity *e1 = get_union_member(typ1, i);
621       for (j = 0; j < get_union_n_members(typ2); j++) {
622         entity *e2 = get_union_member(typ2, j);
623         if (get_entity_name(e1) == get_entity_name(e2))
624           m[i] = e2;
625       }
626     }
627     for (i = 0; i < get_union_n_members(typ1); i++) {
628       if (!m[i]  ||  /* Found no counterpart */
629           !equal_entity(get_union_member(typ1, i), m[i]))
630         return 0;
631     }
632   } break;
633   case tpo_array:       {
634     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
635       return 0;
636     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
637       return 0;
638     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
639       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
640           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
641         return 0;
642       if (get_array_order(typ1, i) != get_array_order(typ2, i))
643         assert(0 && "type compare with different dimension orders not implemented");
644     }
645   } break;
646   case tpo_enumeration: {
647     assert(0 && "enumerations not implemented");
648   } break;
649   case tpo_pointer:     {
650     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
651       return 0;
652   } break;
653   case tpo_primitive:   {
654   } break;
655   default: break;
656   }
657   return 1;
658 }
659
660 /* Checks whether two types are structural comparable. */
661 int smaller_type (type *st, type *lt) {
662   entity **m;
663   int i, j;
664
665   if (st == lt) return 1;
666
667   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
668     return 0;
669
670   switch(get_type_tpop_code(st)) {
671   case tpo_class:       {
672     return is_subclass_of(st, lt);
673   } break;
674   case tpo_struct:      {
675     if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
676     m = alloca(sizeof(entity *) * get_struct_n_members(st));
677     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
678     /* First sort the members of lt */
679     for (i = 0; i < get_struct_n_members(st); i++) {
680       entity *se = get_struct_member(st, i);
681       for (j = 0; j < get_struct_n_members(lt); j++) {
682     entity *le = get_struct_member(lt, j);
683     if (get_entity_name(le) == get_entity_name(se))
684       m[i] = le;
685       }
686     }
687     for (i = 0; i < get_struct_n_members(st); i++) {
688       if (!m[i]  ||  /* Found no counterpart */
689           !smaller_type(get_entity_type(get_struct_member(st, i)),
690                 get_entity_type(m[i])))
691         return 0;
692     }
693   } break;
694   case tpo_method:      {
695     /** FIXME: is this still 1? */
696     if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
697     if (get_method_n_params(st) != get_method_n_params(lt)) return 0;
698     if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
699     for (i = 0; i < get_method_n_params(st); i++) {
700       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
701         return 0;
702     }
703     for (i = 0; i < get_method_n_ress(st); i++) {
704       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
705         return 0;
706     }
707   } break;
708   case tpo_union:       {
709     if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
710     m = alloca(sizeof(entity *) * get_union_n_members(st));
711     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
712     /* First sort the members of lt */
713     for (i = 0; i < get_union_n_members(st); i++) {
714       entity *se = get_union_member(st, i);
715       for (j = 0; j < get_union_n_members(lt); j++) {
716         entity *le = get_union_member(lt, j);
717         if (get_entity_name(le) == get_entity_name(se))
718           m[i] = le;
719           }
720     }
721     for (i = 0; i < get_union_n_members(st); i++) {
722       if (!m[i]  ||  /* Found no counterpart */
723           !smaller_type(get_entity_type(get_union_member(st, i)),
724                 get_entity_type(m[i])))
725         return 0;
726     }
727   } break;
728   case tpo_array:       {
729     type *set, *let;  /* small/large elt. type */
730     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
731       return 0;
732     set = get_array_element_type(st);
733     let = get_array_element_type(lt);
734     if (set != let) {
735       /* If the elt types are different, set must be convertible
736          to let, and they must have the same size so that address
737          computations work out.  To have a size the layout must
738          be fixed. */
739       if ((get_type_state(set) != layout_fixed) ||
740           (get_type_state(let) != layout_fixed))
741         return 0;
742       if (!smaller_type(set, let) ||
743           get_type_size_bits(set) != get_type_size_bits(let))
744         return 0;
745     }
746     for(i = 0; i < get_array_n_dimensions(st); i++) {
747       if (get_array_lower_bound(lt, i))
748         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
749           return 0;
750       if (get_array_upper_bound(lt, i))
751         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
752           return 0;
753     }
754   } break;
755   case tpo_enumeration: {
756     assert(0 && "enumerations not implemented");
757   } break;
758   case tpo_pointer:     {
759     if (!smaller_type(get_pointer_points_to_type(st),
760               get_pointer_points_to_type(lt)))
761       return 0;
762   } break;
763   case tpo_primitive:   {
764     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
765       return 0;
766   } break;
767   default: break;
768   }
769   return 1;
770 }
771
772 /*-----------------------------------------------------------------*/
773 /* TYPE_CLASS                                                      */
774 /*-----------------------------------------------------------------*/
775
776 /* create a new class type */
777 type   *new_d_type_class (ident *name, dbg_info *db) {
778   type *res;
779
780   res = new_type(type_class, NULL, name, db);
781
782   res->attr.ca.members     = NEW_ARR_F (entity *, 0);
783   res->attr.ca.subtypes    = NEW_ARR_F (type *, 0);
784   res->attr.ca.supertypes  = NEW_ARR_F (type *, 0);
785   res->attr.ca.peculiarity = peculiarity_existent;
786   res->attr.ca.dfn         = 0;
787
788   return res;
789 }
790 type   *new_type_class (ident *name) {
791   return new_d_type_class (name, NULL);
792 }
793
794 void free_class_entities(type *clss) {
795   int i;
796   assert(clss && (clss->type_op == type_class));
797   for (i = get_class_n_members(clss)-1; i >= 0; --i)
798     free_entity(get_class_member(clss, i));
799 }
800
801 void free_class_attrs(type *clss) {
802   assert(clss && (clss->type_op == type_class));
803   DEL_ARR_F(clss->attr.ca.members);
804   DEL_ARR_F(clss->attr.ca.subtypes);
805   DEL_ARR_F(clss->attr.ca.supertypes);
806 }
807
808 /* manipulate private fields of class type  */
809 void    add_class_member   (type *clss, entity *member) {
810   assert(clss && (clss->type_op == type_class));
811   assert(clss != get_entity_type(member) && "recursive type");
812   ARR_APP1 (entity *, clss->attr.ca.members, member);
813 }
814
815 int     (get_class_n_members) (const type *clss) {
816   return _get_class_n_members(clss);
817 }
818
819 int     get_class_member_index(type *clss, entity *mem) {
820   int i;
821   assert(clss && (clss->type_op == type_class));
822   for (i = 0; i < get_class_n_members(clss); i++)
823     if (get_class_member(clss, i) == mem)
824       return i;
825   return -1;
826 }
827
828 entity *(get_class_member)   (const type *clss, int pos) {
829   return _get_class_member(clss, pos);
830 }
831
832 entity *get_class_member_by_name(type *clss, ident *name) {
833   int i, n_mem;
834   assert(clss && (clss->type_op == type_class));
835   n_mem = get_class_n_members(clss);
836   for (i = 0; i < n_mem; ++i) {
837     entity *mem = get_class_member(clss, i);
838     if (get_entity_ident(mem) == name) return mem;
839   }
840   return NULL;
841 }
842
843 void    set_class_member   (type *clss, entity *member, int pos) {
844   assert(clss && (clss->type_op == type_class));
845   assert(pos >= 0 && pos < get_class_n_members(clss));
846   clss->attr.ca.members[pos] = member;
847 }
848 void    set_class_members  (type *clss, entity **members, int arity) {
849   int i;
850   assert(clss && (clss->type_op == type_class));
851   DEL_ARR_F(clss->attr.ca.members);
852   clss->attr.ca.members    = NEW_ARR_F (entity *, 0);
853   for (i = 0; i < arity; i++) {
854     set_entity_owner(members[i], clss);
855     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
856   }
857 }
858 void    remove_class_member(type *clss, entity *member) {
859   int i;
860   assert(clss && (clss->type_op == type_class));
861   for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
862     if (clss->attr.ca.members[i] == member) {
863       for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
864     clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
865       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
866       break;
867     }
868   }
869 }
870
871 void    add_class_subtype   (type *clss, type *subtype) {
872   int i;
873   assert(clss && (clss->type_op == type_class));
874   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
875   for (i = 0; i < get_class_n_supertypes(subtype); i++)
876     if (get_class_supertype(subtype, i) == clss)
877       /* Class already registered */
878       return;
879   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
880 }
881 int     get_class_n_subtypes (const type *clss) {
882   assert(clss && (clss->type_op == type_class));
883   return (ARR_LEN (clss->attr.ca.subtypes));
884 }
885 type   *get_class_subtype   (type *clss, int pos) {
886   assert(clss && (clss->type_op == type_class));
887   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
888   return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
889 }
890 int     get_class_subtype_index(type *clss, const type *subclass) {
891   int i, n_subtypes = get_class_n_subtypes(clss);
892   assert(is_Class_type(subclass));
893   for (i = 0; i < n_subtypes; ++i) {
894     if (get_class_subtype(clss, i) == subclass) return i;
895   }
896   return -1;
897 }
898 void    set_class_subtype   (type *clss, type *subtype, int pos) {
899   assert(clss && (clss->type_op == type_class));
900   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
901   clss->attr.ca.subtypes[pos] = subtype;
902 }
903 void    remove_class_subtype(type *clss, type *subtype) {
904   int i;
905   assert(clss && (clss->type_op == type_class));
906   for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
907     if (clss->attr.ca.subtypes[i] == subtype) {
908       for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
909     clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
910       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
911       break;
912     }
913 }
914
915 void    add_class_supertype   (type *clss, type *supertype) {
916   int i;
917   assert(clss && (clss->type_op == type_class));
918   assert(supertype && (supertype -> type_op == type_class));
919   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
920   for (i = 0; i < get_class_n_subtypes(supertype); i++)
921     if (get_class_subtype(supertype, i) == clss)
922       /* Class already registered */
923       return;
924   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
925 }
926 int     get_class_n_supertypes (const type *clss) {
927   assert(clss && (clss->type_op == type_class));
928   return (ARR_LEN (clss->attr.ca.supertypes));
929 }
930 int get_class_supertype_index(type *clss, type *super_clss) {
931   int i, n_supertypes = get_class_n_supertypes(clss);
932   assert(super_clss && (super_clss->type_op == type_class));
933   for (i = 0; i < n_supertypes; i++)
934     if (get_class_supertype(clss, i) == super_clss)
935       return i;
936   return -1;
937 }
938 type   *get_class_supertype   (type *clss, int pos) {
939   assert(clss && (clss->type_op == type_class));
940   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
941   return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
942 }
943 void    set_class_supertype   (type *clss, type *supertype, int pos) {
944   assert(clss && (clss->type_op == type_class));
945   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
946   clss->attr.ca.supertypes[pos] = supertype;
947 }
948 void    remove_class_supertype(type *clss, type *supertype) {
949   int i;
950   assert(clss && (clss->type_op == type_class));
951   for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
952     if (clss->attr.ca.supertypes[i] == supertype) {
953       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
954     clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
955       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
956       break;
957     }
958 }
959
960 const char *get_peculiarity_string(peculiarity p) {
961 #define X(a)    case a: return #a
962   switch (p) {
963     X(peculiarity_description);
964     X(peculiarity_inherited);
965     X(peculiarity_existent);
966   }
967 #undef X
968   return "invalid peculiarity";
969 }
970
971 peculiarity get_class_peculiarity (const type *clss) {
972   assert(clss && (clss->type_op == type_class));
973   return clss->attr.ca.peculiarity;
974 }
975
976 void        set_class_peculiarity (type *clss, peculiarity pec) {
977   assert(clss && (clss->type_op == type_class));
978   assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
979   clss->attr.ca.peculiarity = pec;
980 }
981
982 void set_class_dfn (type *clss, int dfn)
983 {
984   clss->attr.ca.dfn        = dfn;
985 }
986
987 int get_class_dfn (const type *clss)
988 {
989   return (clss->attr.ca.dfn);
990 }
991
992 /* typecheck */
993 int (is_Class_type)(const type *clss) {
994   return _is_class_type(clss);
995 }
996
997 /*----------------------------------------------------------------**/
998 /* TYPE_STRUCT                                                     */
999 /*----------------------------------------------------------------**/
1000
1001 /* create a new type struct */
1002 type   *new_d_type_struct(ident *name, dbg_info *db) {
1003   type *res = new_type(type_struct, NULL, name, db);
1004
1005   res->attr.sa.members = NEW_ARR_F(entity *, 0);
1006   return res;
1007 }
1008 type   *new_type_struct (ident *name) {
1009   return new_d_type_struct (name, NULL);
1010 }
1011
1012 void free_struct_entities (type *strct) {
1013   int i;
1014   assert(strct && (strct->type_op == type_struct));
1015   for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1016     free_entity(get_struct_member(strct, i));
1017 }
1018 void free_struct_attrs (type *strct) {
1019   assert(strct && (strct->type_op == type_struct));
1020   DEL_ARR_F(strct->attr.sa.members);
1021 }
1022
1023 /* manipulate private fields of struct */
1024 int     get_struct_n_members (const type *strct) {
1025   assert(strct && (strct->type_op == type_struct));
1026   return (ARR_LEN (strct->attr.sa.members));
1027 }
1028
1029 void    add_struct_member   (type *strct, entity *member) {
1030   assert(strct && (strct->type_op == type_struct));
1031   assert(get_type_tpop(get_entity_type(member)) != type_method);
1032     /*    @@@ lowerfirm geht nicht durch */
1033   assert(strct != get_entity_type(member) && "recursive type");
1034   ARR_APP1 (entity *, strct->attr.sa.members, member);
1035 }
1036
1037 entity *get_struct_member   (const type *strct, int pos) {
1038   assert(strct && (strct->type_op == type_struct));
1039   assert(pos >= 0 && pos < get_struct_n_members(strct));
1040   return strct->attr.sa.members[pos];
1041 }
1042
1043 int     get_struct_member_index(type *strct, entity *mem) {
1044   int i;
1045   assert(strct && (strct->type_op == type_struct));
1046   for (i = 0; i < get_struct_n_members(strct); i++)
1047     if (get_struct_member(strct, i) == mem)
1048       return i;
1049   return -1;
1050 }
1051
1052 void    set_struct_member   (type *strct, int pos, entity *member) {
1053   assert(strct && (strct->type_op == type_struct));
1054   assert(pos >= 0 && pos < get_struct_n_members(strct));
1055   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1056   strct->attr.sa.members[pos] = member;
1057 }
1058 void    remove_struct_member(type *strct, entity *member) {
1059   int i;
1060   assert(strct && (strct->type_op == type_struct));
1061   for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1062     if (strct->attr.sa.members[i] == member) {
1063       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1064     strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1065       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1066       break;
1067     }
1068 }
1069
1070 /* typecheck */
1071 int (is_Struct_type)(const type *strct) {
1072   return _is_struct_type(strct);
1073 }
1074
1075 /*******************************************************************/
1076 /** TYPE_METHOD                                                   **/
1077 /*******************************************************************/
1078
1079 /**
1080  * Lazy construction of value argument / result representation.
1081  * Constructs a struct type and its member.  The types of the members
1082  * are passed in the argument list.
1083  *
1084  * @param name    name of the type constructed
1085  * @param len     number of fields
1086  * @param tps     array of field types with length len
1087  */
1088 static INLINE type *
1089 build_value_type(ident *name, int len, type **tps) {
1090   int i;
1091   type *res = new_type_struct(name);
1092   /* Remove type from type list.  Must be treated differently than other types. */
1093   remove_irp_type(res);
1094   for (i = 0; i < len; i++) {
1095     type *elt_type = res;   /* use res as default if corresponding type is not yet set. */
1096     if (tps[i]) elt_type = tps[i];
1097     new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1098   }
1099   return res;
1100 }
1101
1102 /* Create a new method type.
1103    N_param is the number of parameters, n_res the number of results.  */
1104 type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1105   type *res;
1106
1107   assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1108   res = new_type(type_method, mode_P_code, name, db);
1109   res->state                        = layout_fixed;
1110   res->size                         = get_mode_size_bits(mode_P_code);
1111   res->attr.ma.n_params             = n_param;
1112   res->attr.ma.param_type           = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1113   res->attr.ma.value_params         = NULL;
1114   res->attr.ma.n_res                = n_res;
1115   res->attr.ma.res_type             = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1116   res->attr.ma.value_ress           = NULL;
1117   res->attr.ma.variadicity          = variadicity_non_variadic;
1118   res->attr.ma.first_variadic_param = -1;
1119
1120   return res;
1121 }
1122
1123 type *new_type_method(ident *name, int n_param, int n_res) {
1124   return new_d_type_method(name, n_param, n_res, NULL);
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_d_type_union(ident *name, dbg_info *db) {
1312   type *res = new_type(type_union, NULL, name, db);
1313
1314   res->attr.ua.members = NEW_ARR_F(entity *, 0);
1315   return res;
1316 }
1317 type  *new_type_union(ident *name) {
1318   return new_d_type_union(name, NULL);
1319 }
1320 void free_union_entities(type *uni) {
1321   int i;
1322   assert(uni && (uni->type_op == type_union));
1323   for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1324     free_entity(get_union_member(uni, i));
1325 }
1326 void free_union_attrs (type *uni) {
1327   assert(uni && (uni->type_op == type_union));
1328   DEL_ARR_F(uni->attr.ua.members);
1329 }
1330 /* manipulate private fields of union */
1331 int    get_union_n_members      (const type *uni) {
1332   assert(uni && (uni->type_op == type_union));
1333   return (ARR_LEN (uni->attr.ua.members));
1334 }
1335 void    add_union_member   (type *uni, entity *member) {
1336   assert(uni && (uni->type_op == type_union));
1337   assert(uni != get_entity_type(member) && "recursive type");
1338   ARR_APP1 (entity *, uni->attr.ua.members, member);
1339 }
1340 entity  *get_union_member (const type *uni, int pos) {
1341   assert(uni && (uni->type_op == type_union));
1342   assert(pos >= 0 && pos < get_union_n_members(uni));
1343   return uni->attr.ua.members[pos];
1344 }
1345 void   set_union_member (type *uni, int pos, entity *member) {
1346   assert(uni && (uni->type_op == type_union));
1347   assert(pos >= 0 && pos < get_union_n_members(uni));
1348   uni->attr.ua.members[pos] = member;
1349 }
1350 void   remove_union_member(type *uni, entity *member) {
1351   int i;
1352   assert(uni && (uni->type_op == type_union));
1353   for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1354     if (uni->attr.ua.members[i] == member) {
1355       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1356         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1357       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1358       break;
1359     }
1360 }
1361
1362 /* typecheck */
1363 int (is_Union_type)(const type *uni) {
1364   return _is_union_type(uni);
1365 }
1366
1367 /*-----------------------------------------------------------------*/
1368 /* TYPE_ARRAY                                                      */
1369 /*-----------------------------------------------------------------*/
1370
1371
1372 /* create a new type array -- set dimension sizes independently */
1373 type *new_d_type_array(ident *name, int n_dimensions, type *element_type, dbg_info *db) {
1374   type *res;
1375   int i;
1376   ir_node *unk;
1377   ir_graph *rem = current_ir_graph;
1378
1379   assert(!is_Method_type(element_type));
1380
1381   res = new_type(type_array, NULL, name, db);
1382   res->attr.aa.n_dimensions = n_dimensions;
1383   res->attr.aa.lower_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1384   res->attr.aa.upper_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1385   res->attr.aa.order        = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1386
1387   current_ir_graph = get_const_code_irg();
1388   unk = new_Unknown( mode_Iu);
1389   for (i = 0; i < n_dimensions; i++) {
1390     res->attr.aa.lower_bound[i] =
1391     res->attr.aa.upper_bound[i] = unk;
1392     res->attr.aa.order[i]       = i;
1393   }
1394   current_ir_graph = rem;
1395
1396   res->attr.aa.element_type = element_type;
1397   new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1398
1399   return res;
1400 }
1401
1402 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1403   return new_d_type_array(name, n_dimensions, element_type, NULL);
1404 }
1405
1406 void free_array_entities (type *array) {
1407   assert(array && (array->type_op == type_array));
1408 }
1409
1410 void free_array_attrs (type *array) {
1411   assert(array && (array->type_op == type_array));
1412   free(array->attr.aa.lower_bound);
1413   free(array->attr.aa.upper_bound);
1414 }
1415
1416 /* manipulate private fields of array type */
1417 int   get_array_n_dimensions (const type *array) {
1418   assert(array && (array->type_op == type_array));
1419   return array->attr.aa.n_dimensions;
1420 }
1421
1422 void
1423 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1424           ir_node * upper_bound) {
1425   assert(array && (array->type_op == type_array));
1426   assert(lower_bound && "lower_bound node may not be NULL.");
1427   assert(upper_bound && "upper_bound node may not be NULL.");
1428   assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1429   array->attr.aa.lower_bound[dimension] = lower_bound;
1430   array->attr.aa.upper_bound[dimension] = upper_bound;
1431 }
1432 void
1433 set_array_bounds_int (type *array, int dimension, int lower_bound,
1434               int upper_bound) {
1435   ir_graph *rem = current_ir_graph;
1436   current_ir_graph = get_const_code_irg();
1437   set_array_bounds (array, dimension,
1438             new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1439             new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1440   current_ir_graph = rem;
1441 }
1442 void
1443 set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
1444   assert(array && (array->type_op == type_array));
1445   assert(lower_bound && "lower_bound node may not be NULL.");
1446   array->attr.aa.lower_bound[dimension] = lower_bound;
1447 }
1448 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1449   ir_graph *rem = current_ir_graph;
1450   current_ir_graph = get_const_code_irg();
1451   set_array_lower_bound  (array, dimension,
1452               new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1453   current_ir_graph = rem;
1454 }
1455 void
1456 set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
1457   assert(array && (array->type_op == type_array));
1458   assert(upper_bound && "upper_bound node may not be NULL.");
1459   array->attr.aa.upper_bound[dimension] = upper_bound;
1460 }
1461 void  set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1462   ir_graph *rem = current_ir_graph;
1463   current_ir_graph = get_const_code_irg();
1464   set_array_upper_bound  (array, dimension,
1465               new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1466   current_ir_graph = rem;
1467 }
1468 int      has_array_lower_bound  (const type *array, int dimension) {
1469   assert(array && (array->type_op == type_array));
1470   return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1471 }
1472 ir_node *get_array_lower_bound  (const type *array, int dimension) {
1473   assert(array && (array->type_op == type_array));
1474   return array->attr.aa.lower_bound[dimension];
1475 }
1476 long     get_array_lower_bound_int  (const type *array, int dimension) {
1477   ir_node *node;
1478   assert(array && (array->type_op == type_array));
1479   node = array->attr.aa.lower_bound[dimension];
1480   assert(get_irn_op(node) == op_Const);
1481   return get_tarval_long(get_Const_tarval(node));
1482 }
1483 int       has_array_upper_bound  (const type *array, int dimension) {
1484   assert(array && (array->type_op == type_array));
1485   return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1486 }
1487 ir_node * get_array_upper_bound  (const type *array, int dimension) {
1488   assert(array && (array->type_op == type_array));
1489   return array->attr.aa.upper_bound[dimension];
1490 }
1491 long     get_array_upper_bound_int  (const type *array, int dimension) {
1492   ir_node *node;
1493   assert(array && (array->type_op == type_array));
1494   node = array->attr.aa.upper_bound[dimension];
1495   assert(get_irn_op(node) == op_Const);
1496   return get_tarval_long(get_Const_tarval(node));
1497 }
1498
1499 void set_array_order (type *array, int dimension, int order) {
1500   assert(array && (array->type_op == type_array));
1501   array->attr.aa.order[dimension] = order;
1502 }
1503
1504 int  get_array_order (const type *array, int dimension) {
1505   assert(array && (array->type_op == type_array));
1506   return array->attr.aa.order[dimension];
1507 }
1508
1509 int find_array_dimension(const type *array, int order) {
1510   int dim;
1511
1512   assert(array && (array->type_op == type_array));
1513
1514   for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1515     if (array->attr.aa.order[dim] == order)
1516       return dim;
1517   }
1518   return -1;
1519 }
1520
1521 void  set_array_element_type (type *array, type *tp) {
1522   assert(array && (array->type_op == type_array));
1523   assert(!is_Method_type(tp));
1524   array->attr.aa.element_type = tp;
1525 }
1526 type *get_array_element_type (type *array) {
1527   assert(array && (array->type_op == type_array));
1528   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1529 }
1530
1531 void  set_array_element_entity (type *array, entity *ent) {
1532   assert(array && (array->type_op == type_array));
1533   assert((get_entity_type(ent)->type_op != type_method));
1534   array->attr.aa.element_ent = ent;
1535   array->attr.aa.element_type = get_entity_type(ent);
1536 }
1537 entity *get_array_element_entity (const type *array) {
1538   assert(array && (array->type_op == type_array));
1539   return array->attr.aa.element_ent;
1540 }
1541
1542 /* typecheck */
1543 int (is_Array_type)(const type *array) {
1544   return _is_array_type(array);
1545 }
1546
1547 /*-----------------------------------------------------------------*/
1548 /* TYPE_ENUMERATION                                                */
1549 /*-----------------------------------------------------------------*/
1550
1551 /* create a new type enumeration -- set the enumerators independently */
1552 type   *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1553   type *res = new_type(type_enumeration, NULL, name, db);
1554
1555   res->attr.ea.n_enums     = n_enums;
1556   res->attr.ea.enumer      = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1557   res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1558   return res;
1559 }
1560 type   *new_type_enumeration(ident *name, int n_enums) {
1561   return new_d_type_enumeration(name, n_enums, NULL);
1562 }
1563
1564 void free_enumeration_entities(type *enumeration) {
1565   assert(enumeration && (enumeration->type_op == type_enumeration));
1566 }
1567 void free_enumeration_attrs(type *enumeration) {
1568   assert(enumeration && (enumeration->type_op == type_enumeration));
1569   free(enumeration->attr.ea.enumer);
1570   free(enumeration->attr.ea.enum_nameid);
1571 }
1572
1573 /* manipulate fields of enumeration type. */
1574 int     get_enumeration_n_enums (const type *enumeration) {
1575   assert(enumeration && (enumeration->type_op == type_enumeration));
1576   return enumeration->attr.ea.n_enums;
1577 }
1578 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
1579   assert(enumeration && (enumeration->type_op == type_enumeration));
1580   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1581   enumeration->attr.ea.enumer[pos] = con;
1582 }
1583 tarval *get_enumeration_enum    (const type *enumeration, int pos) {
1584   assert(enumeration && (enumeration->type_op == type_enumeration));
1585   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1586   return enumeration->attr.ea.enumer[pos];
1587 }
1588 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
1589   assert(enumeration && (enumeration->type_op == type_enumeration));
1590   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1591   enumeration->attr.ea.enum_nameid[pos] = id;
1592 }
1593 ident  *get_enumeration_nameid  (const type *enumeration, int pos) {
1594   assert(enumeration && (enumeration->type_op == type_enumeration));
1595   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1596   return enumeration->attr.ea.enum_nameid[pos];
1597 }
1598 const char *get_enumeration_name(const type *enumeration, int pos) {
1599   assert(enumeration && (enumeration->type_op == type_enumeration));
1600   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1601   return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1602 }
1603
1604 /* typecheck */
1605 int (is_Enumeration_type)(const type *enumeration) {
1606   return _is_enumeration_type(enumeration);
1607 }
1608
1609 /*-----------------------------------------------------------------*/
1610 /* TYPE_POINTER                                                    */
1611 /*-----------------------------------------------------------------*/
1612
1613 /* Create a new type pointer */
1614 type *new_d_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1615   type *res;
1616
1617   assert(mode_is_reference(ptr_mode));
1618   res = new_type(type_pointer, ptr_mode, name, db);
1619   res->attr.pa.points_to = points_to;
1620   assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1621   res->size = get_mode_size_bits(res->mode);
1622   res->state = layout_fixed;
1623   return res;
1624 }
1625 type *new_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode) {
1626   return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1627 }
1628 void free_pointer_entities (type *pointer) {
1629   assert(pointer && (pointer->type_op == type_pointer));
1630 }
1631 void free_pointer_attrs (type *pointer) {
1632   assert(pointer && (pointer->type_op == type_pointer));
1633 }
1634 /* manipulate fields of type_pointer */
1635 void  set_pointer_points_to_type (type *pointer, type *tp) {
1636   assert(pointer && (pointer->type_op == type_pointer));
1637   pointer->attr.pa.points_to = tp;
1638 }
1639 type *get_pointer_points_to_type (type *pointer) {
1640   assert(pointer && (pointer->type_op == type_pointer));
1641   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1642 }
1643
1644 /* typecheck */
1645 int (is_Pointer_type)(const type *pointer) {
1646   return _is_pointer_type(pointer);
1647 }
1648
1649 /* Returns the first pointer type that has as points_to tp.
1650  *  Not efficient: O(#types).
1651  *  If not found returns firm_unknown_type. */
1652 type *find_pointer_type_to_type (type *tp) {
1653   int i;
1654   for (i = 0; i < get_irp_n_types(); ++i) {
1655     type *found = get_irp_type(i);
1656     if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1657       return (found);
1658   }
1659   return firm_unknown_type;
1660 }
1661
1662
1663
1664 /*-----------------------------------------------------------------*/
1665 /* TYPE_PRIMITIVE                                                  */
1666 /*-----------------------------------------------------------------*/
1667
1668 /* create a new type primitive */
1669 type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1670   type *res;
1671   /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1672   res = new_type(type_primitive, mode, name, db);
1673   res->size  = get_mode_size_bits(mode);
1674   res->state = layout_fixed;
1675   return res;
1676 }
1677 type *new_type_primitive(ident *name, ir_mode *mode) {
1678   return new_d_type_primitive(name, mode, NULL);
1679 }
1680 void free_primitive_entities (type *primitive) {
1681   assert(primitive && (primitive->type_op == type_primitive));
1682 }
1683 void free_primitive_attrs (type *primitive) {
1684   assert(primitive && (primitive->type_op == type_primitive));
1685 }
1686
1687 /* typecheck */
1688 int (is_Primitive_type)(const type *primitive) {
1689   return _is_primitive_type(primitive);
1690 }
1691
1692 /*-----------------------------------------------------------------*/
1693 /* common functionality                                            */
1694 /*-----------------------------------------------------------------*/
1695
1696
1697 int (is_atomic_type)(const type *tp) {
1698   return _is_atomic_type(tp);
1699 }
1700
1701 /*
1702  * Gets the number of elements in a firm compound type.
1703  */
1704 int get_compound_n_members(const type *tp)
1705 {
1706   int res = 0;
1707
1708   if (is_Struct_type(tp))
1709     res = get_struct_n_members(tp);
1710   else if (is_Class_type(tp))
1711     res = get_class_n_members(tp);
1712   else if (is_Union_type(tp))
1713     res = get_union_n_members(tp);
1714   else
1715     assert(0 && "need struct, union or class for member count");
1716
1717   return res;
1718 }
1719
1720 /*
1721  * Gets the member of a firm compound type at position pos.
1722  */
1723 entity *get_compound_member(const type *tp, int pos)
1724 {
1725   entity *res;
1726
1727   if (is_Struct_type(tp))
1728     res = get_struct_member(tp, pos);
1729   else if (is_Class_type(tp))
1730     res = get_class_member(tp, pos);
1731   else if (is_Union_type(tp))
1732     res = get_union_member(tp, pos);
1733   else
1734   {
1735     assert(0 && "need struct, union or class to get a member");
1736     res = NULL;
1737   }
1738
1739   return res;
1740 }
1741
1742 int is_compound_type(const type *tp) {
1743   assert(tp && tp->kind == k_type);
1744   return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1745 }
1746
1747 /* Checks, whether a type is a frame type */
1748 int is_frame_type(const type *tp) {
1749   return tp->frame_type;
1750 }
1751
1752 /* Makes a new frame type. */
1753 type *new_type_frame(ident *name)
1754 {
1755   type *res = new_type_class(name);
1756
1757   res->frame_type = 1;
1758
1759   /* Remove type from type list.  Must be treated differently than other types. */
1760   remove_irp_type(res);
1761
1762   return res;
1763 }