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