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