tr: Remove unnecessary test.
[libfirm] / ir / tr / type.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Representation of types.
23  * @author  Goetz Lindenmaier, Michael Beck
24  * @brief
25  *
26  *  Implementation of the datastructure to hold
27  *  type information.
28  *
29  *  This module supplies a datastructure to represent all types
30  *  known in the compiled program.  This includes types specified
31  *  in the program as well as types defined by the language.  In the
32  *  view of the intermediate representation there is no difference
33  *  between these types.
34  *
35  *  There exist several kinds of types, arranged by the structure of
36  *  the type.  A type is described by a set of attributes.  Some of
37  *  these attributes are common to all types, others depend on the
38  *  kind of the type.
39  *
40  *  Types are different from the modes defined in irmode:  Types are
41  *  on the level of the programming language, modes at the level of
42  *  the target processor.
43  */
44 #include "config.h"
45
46 #include <string.h>
47 #include <stdlib.h>
48 #include <stddef.h>
49 #include <stdbool.h>
50
51 #include "type_t.h"
52
53 #include "xmalloc.h"
54 #include "irprog_t.h"
55 #include "ircons.h"
56 #include "tpop_t.h"
57 #include "tv_t.h"
58 #include "irhooks.h"
59 #include "util.h"
60 #include "entity_t.h"
61 #include "error.h"
62 #include "dbginfo.h"
63 #include "irprog_t.h"
64
65 #include "array.h"
66
67 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db);
68
69 ir_type *get_none_type(void)
70 {
71         return irp->none_type;
72 }
73
74 ir_type *get_code_type(void)
75 {
76         return irp->code_type;
77 }
78
79 ir_type *get_unknown_type(void)
80 {
81         return irp->unknown_type;
82 }
83
84 void ir_init_type(ir_prog *irp)
85 {
86         /* construct none and unknown type. */
87         irp->none_type = new_type(tpop_none, mode_BAD, NULL);
88         set_type_size_bytes(irp->none_type, 0);
89         set_type_state (irp->none_type, layout_fixed);
90
91         irp->code_type = new_type(tpop_code, mode_ANY, NULL);
92         set_type_state(irp->code_type, layout_fixed);
93
94         irp->unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
95         set_type_size_bytes(irp->unknown_type, 0);
96         set_type_state (irp->unknown_type, layout_fixed);
97 }
98
99 void ir_finish_type(ir_prog *irp)
100 {
101         /** nothing todo. (The none, code, unknown types are in the global type list
102          * and freed there */
103         (void)irp;
104 }
105
106 ir_visited_t firm_type_visited;
107
108 void (set_master_type_visited)(ir_visited_t val)
109 {
110         _set_master_type_visited(val);
111 }
112
113 ir_visited_t (get_master_type_visited)(void)
114 {
115         return _get_master_type_visited();
116 }
117
118 void (inc_master_type_visited)(void)
119 {
120         _inc_master_type_visited();
121 }
122
123 /**
124  *   Creates a new type representation:
125  *
126  *   @param type_op  the kind of this type.  May not be type_id.
127  *   @param mode     the mode to be used for this type, may be NULL
128  *   @param db       debug info
129  *
130  *   @return A new type of the given type.  The remaining private attributes are
131  *           not initialized.  The type is in state layout_undefined.
132  */
133 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db)
134 {
135         ir_type *res;
136         size_t node_size;
137
138         node_size = offsetof(ir_type, attr) +  type_op->attr_size;
139         res = (ir_type*)xmalloc(node_size);
140         memset(res, 0, node_size);
141
142         res->kind       = k_type;
143         res->type_op    = type_op;
144         res->mode       = mode;
145         res->visibility = ir_visibility_external;
146         res->flags      = tf_none;
147         res->size       = 0;
148         res->align      = 0;
149         res->visit      = 0;
150         res->link       = NULL;
151         res->dbi        = db;
152 #ifdef DEBUG_libfirm
153         res->nr         = get_irp_new_node_nr();
154 #endif /* defined DEBUG_libfirm */
155
156         add_irp_type(res);   /* Remember the new type global. */
157
158         return res;
159 }
160
161 void free_type_entities(ir_type *tp)
162 {
163         const tp_op *op = get_type_tpop(tp);
164         if (op->ops.free_entities != NULL)
165                 op->ops.free_entities(tp);
166 }
167
168 static void free_type_attrs(ir_type *tp)
169 {
170         const tp_op *tpop = get_type_tpop(tp);
171
172         if (tpop->ops.free_attrs)
173                 tpop->ops.free_attrs(tp);
174 }
175
176 void free_type(ir_type *tp)
177 {
178         const tp_op *op = get_type_tpop(tp);
179
180         free_type_entities(tp);
181         /* Remove from list of all types */
182         remove_irp_type(tp);
183         /* Free the attributes of the type. */
184         free_type_attrs(tp);
185         /* Free entities automatically allocated with the ir_type */
186         if (op->ops.free_auto_entities)
187                 op->ops.free_auto_entities(tp);
188         /* And now the type itself... */
189 #ifdef DEBUG_libfirm
190         tp->kind = k_BAD;
191 #endif
192         free(tp);
193 }
194
195 void *(get_type_link)(const ir_type *tp)
196 {
197         return _get_type_link(tp);
198 }
199
200 void (set_type_link)(ir_type *tp, void *l)
201 {
202         _set_type_link(tp, l);
203 }
204
205 const tp_op *(get_type_tpop)(const ir_type *tp)
206 {
207         return _get_type_tpop(tp);
208 }
209
210 ident *(get_type_tpop_nameid)(const ir_type *tp)
211 {
212         return _get_type_tpop_nameid(tp);
213 }
214
215 const char* get_type_tpop_name(const ir_type *tp)
216 {
217         assert(tp && tp->kind == k_type);
218         return get_id_str(tp->type_op->name);
219 }
220
221 tp_opcode (get_type_tpop_code)(const ir_type *tp)
222 {
223         return _get_type_tpop_code(tp);
224 }
225
226 ir_mode *(get_type_mode)(const ir_type *tp)
227 {
228         return _get_type_mode(tp);
229 }
230
231 void set_type_mode(ir_type *tp, ir_mode *mode)
232 {
233         const tp_op *tpop = get_type_tpop(tp);
234
235         if (tpop->ops.set_type_mode)
236                 tpop->ops.set_type_mode(tp, mode);
237         else
238                 assert(0 && "setting a mode is NOT allowed for this type");
239 }
240
241 long get_type_nr(const ir_type *tp)
242 {
243         assert(tp);
244 #ifdef DEBUG_libfirm
245         return tp->nr;
246 #else
247         return (long)PTR_TO_INT(tp);
248 #endif
249 }
250
251 unsigned (get_type_size_bytes)(const ir_type *tp)
252 {
253         return _get_type_size_bytes(tp);
254 }
255
256 ir_visibility get_type_visibility(const ir_type *tp)
257 {
258         assert(is_type(tp));
259         return tp->visibility;
260 }
261
262 void set_type_visibility(ir_type *tp, ir_visibility v)
263 {
264         assert(is_type(tp));
265         tp->visibility = v;
266 }
267
268 void set_type_size_bytes(ir_type *tp, unsigned size)
269 {
270         const tp_op *tpop = get_type_tpop(tp);
271         tpop->ops.set_type_size(tp, size);
272 }
273
274 unsigned get_type_alignment_bytes(ir_type *tp)
275 {
276         unsigned align = 1;
277
278         if (tp->align > 0)
279                 return tp->align;
280
281         /* alignment NOT set calculate it "on demand" */
282         if (tp->mode)
283                 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
284         else if (is_Array_type(tp))
285                 align = get_type_alignment_bytes(get_array_element_type(tp));
286         else if (is_compound_type(tp)) {
287                 size_t i, n = get_compound_n_members(tp);
288
289                 align = 0;
290                 for (i = 0; i < n; ++i) {
291                         ir_type  *t = get_entity_type(get_compound_member(tp, i));
292                         unsigned a  = get_type_alignment_bytes(t);
293
294                         if (a > align)
295                                 align = a;
296                 }
297         } else if (is_Method_type(tp)) {
298                 align = 0;
299         }
300
301         /* write back */
302         tp->align = align;
303
304         return align;
305 }
306
307 void set_type_alignment_bytes(ir_type *tp, unsigned align)
308 {
309         assert(tp && tp->kind == k_type);
310         /* Methods don't have an alignment. */
311         if (tp->type_op != type_method) {
312                 tp->align = align;
313         }
314 }
315
316 const char *get_type_state_name(ir_type_state s)
317 {
318 #define X(a)    case a: return #a
319         switch (s) {
320                 X(layout_undefined);
321                 X(layout_fixed);
322         }
323         return "<unknown>";
324 #undef X
325 }
326
327 ir_type_state (get_type_state)(const ir_type *tp)
328 {
329         return _get_type_state(tp);
330 }
331
332 void set_type_state(ir_type *tp, ir_type_state state)
333 {
334         assert(tp && tp->kind == k_type);
335
336         if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
337                 (tp->type_op == type_method))
338                 return;
339
340         /* Just a correctness check: */
341         if (state == layout_fixed) {
342                 size_t i;
343                 switch (get_type_tpop_code(tp)) {
344                 case tpo_class:
345                         if (tp != get_glob_type()) {
346                                 size_t n_mem = get_class_n_members(tp);
347                                 for (i = 0; i < n_mem; i++) {
348                                         ir_entity *entity = get_class_member(tp, i);
349                                         if (is_Method_type(get_entity_type(entity)))
350                                                 continue;
351                                         assert(get_entity_offset(entity) > -1);
352                                 }
353                         }
354                         break;
355                 case tpo_struct:
356                         for (i = 0; i < get_struct_n_members(tp); i++) {
357                                 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
358                         }
359                         break;
360                 case tpo_union:
361                         break;
362                 case tpo_array:
363                         break;
364                 case tpo_enumeration: {
365 #ifndef NDEBUG
366                         size_t n_enums = get_enumeration_n_enums(tp);
367                         assert(get_type_mode(tp) != NULL);
368                         for (i = 0; i < n_enums; ++i) {
369                                 ir_enum_const *ec = get_enumeration_const(tp, i);
370                                 ir_tarval     *tv = get_enumeration_value(ec);
371                                 assert(tv != NULL && tv != tarval_bad);
372                         }
373 #endif
374                         break;
375                 }
376                 default: break;
377                 }
378         }
379         if (state == layout_fixed)
380                 tp->flags |= tf_layout_fixed;
381         else
382                 tp->flags &= ~tf_layout_fixed;
383 }
384
385 ir_visited_t (get_type_visited)(const ir_type *tp)
386 {
387         return _get_type_visited(tp);
388 }
389
390 void (set_type_visited)(ir_type *tp, ir_visited_t num)
391 {
392         _set_type_visited(tp, num);
393 }
394
395 void (mark_type_visited)(ir_type *tp)
396 {
397         _mark_type_visited(tp);
398 }
399
400 int (type_visited)(const ir_type *tp)
401 {
402         return _type_visited(tp);
403 }
404
405 int (type_not_visited)(const ir_type *tp)
406 {
407         return _type_not_visited(tp);
408 }
409
410 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
411 {
412         return _get_type_dbg_info(tp);
413 }
414
415 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
416 {
417         _set_type_dbg_info(tp, db);
418 }
419
420 int (is_type)(const void *thing)
421 {
422         return _is_type(thing);
423 }
424
425 int equal_type(ir_type *typ1, ir_type *typ2)
426 {
427         ir_entity **m;
428         ir_type **t;
429         size_t i;
430         size_t j;
431
432         if (typ1 == typ2) return 1;
433
434         if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
435             typ1->name != typ2->name ||
436             (get_type_mode(typ1) != get_type_mode(typ2)) ||
437             (get_type_state(typ1) != get_type_state(typ2)))
438                 return 0;
439         if ((get_type_state(typ1) == layout_fixed) &&
440                 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
441                 return 0;
442
443         switch (get_type_tpop_code(typ1)) {
444         case tpo_class:
445                 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
446                 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
447                 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
448                 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
449                 /** Compare the members **/
450                 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
451                 /* First sort the members of typ2 */
452                 for (i = 0; i < get_class_n_members(typ1); i++) {
453                         ir_entity *e1 = get_class_member(typ1, i);
454                         for (j = 0; j < get_class_n_members(typ2); j++) {
455                                 ir_entity *e2 = get_class_member(typ2, j);
456                                 if (get_entity_name(e1) == get_entity_name(e2))
457                                         m[i] = e2;
458                         }
459                 }
460                 for (i = 0; i < get_class_n_members(typ1); i++) {
461                         if (!m[i] || get_class_member(typ1, i) != m[i])
462                                 return 0;
463                 }
464                 /** Compare the supertypes **/
465                 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
466                 /* First sort the supertypes of typ2 */
467                 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
468                         ir_type *t1 = get_class_supertype(typ1, i);
469                         for (j = 0; j < get_class_n_supertypes(typ2); j++) {
470                                 ir_type *t2 = get_class_supertype(typ2, j);
471                                 if (t2->name == t1->name)
472                                         t[i] = t2;
473                         }
474                 }
475                 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
476                         if (!t[i]  ||  /* Found no counterpart */
477                                 get_class_supertype(typ1, i) != t[i])
478                                 return 0;
479                 }
480                 break;
481
482         case tpo_struct:
483                 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
484                 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
485                 /* First sort the members of lt */
486                 for (i = 0; i < get_struct_n_members(typ1); i++) {
487                         ir_entity *e1 = get_struct_member(typ1, i);
488                         for (j = 0; j < get_struct_n_members(typ2); j++) {
489                                 ir_entity *e2 = get_struct_member(typ2, j);
490                                 if (get_entity_name(e1) == get_entity_name(e2))
491                                         m[i] = e2;
492                         }
493                 }
494                 for (i = 0; i < get_struct_n_members(typ1); i++) {
495                         if (!m[i] || get_struct_member(typ1, i) != m[i])
496                                 return 0;
497                 }
498                 break;
499
500         case tpo_method: {
501                 size_t n_param1;
502                 size_t n_param2;
503
504                 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
505                 if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return 0;
506                 if (get_method_calling_convention(typ1) !=
507                     get_method_calling_convention(typ2)) return 0;
508
509                 n_param1 = get_method_n_params(typ1);
510                 n_param2 = get_method_n_params(typ2);
511
512                 if (n_param1 != n_param2) return 0;
513
514                 for (i = 0; i < n_param1; i++) {
515                         if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
516                                 return 0;
517                 }
518                 for (i = 0; i < get_method_n_ress(typ1); i++) {
519                         if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
520                                 return 0;
521                 }
522         } break;
523
524         case tpo_union:
525                 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
526                 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
527                 /* First sort the members of lt */
528                 for (i = 0; i < get_union_n_members(typ1); i++) {
529                         ir_entity *e1 = get_union_member(typ1, i);
530                         for (j = 0; j < get_union_n_members(typ2); j++) {
531                                 ir_entity *e2 = get_union_member(typ2, j);
532                                 if (get_entity_name(e1) == get_entity_name(e2))
533                                         m[i] = e2;
534                         }
535                 }
536                 for (i = 0; i < get_union_n_members(typ1); i++) {
537                         if (!m[i] || get_union_member(typ1, i) != m[i])
538                                 return 0;
539                 }
540                 break;
541
542         case tpo_array:
543                 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
544                         return 0;
545                 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
546                         return 0;
547                 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
548                         if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
549                                 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
550                                 return 0;
551                         if (get_array_order(typ1, i) != get_array_order(typ2, i))
552                                 assert(0 && "type compare with different dimension orders not implemented");
553                 }
554                 break;
555
556         case tpo_enumeration:
557                 assert(0 && "enumerations not implemented");
558                 break;
559
560         case tpo_pointer:
561                 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
562                         return 0;
563                 break;
564
565         case tpo_primitive:
566                 break;
567
568         default: break;
569         }
570         return 1;
571 }
572
573 int smaller_type(ir_type *st, ir_type *lt)
574 {
575         ir_entity **m;
576         size_t i;
577         size_t j;
578         size_t n_st_members;
579
580         if (st == lt) return 1;
581
582         if (get_type_tpop_code(st) != get_type_tpop_code(lt))
583                 return 0;
584
585         switch (get_type_tpop_code(st)) {
586         case tpo_class:
587                 return is_SubClass_of(st, lt);
588
589         case tpo_struct:
590                 n_st_members = get_struct_n_members(st);
591                 if (n_st_members != get_struct_n_members(lt))
592                         return 0;
593
594                 m = ALLOCANZ(ir_entity*, n_st_members);
595                 /* First sort the members of lt */
596                 for (i = 0; i < n_st_members; ++i) {
597                         ir_entity *se = get_struct_member(st, i);
598                         size_t n = get_struct_n_members(lt);
599                         for (j = 0; j < n; ++j) {
600                                 ir_entity *le = get_struct_member(lt, j);
601                                 if (get_entity_name(le) == get_entity_name(se))
602                                         m[i] = le;
603                         }
604                 }
605                 for (i = 0; i < n_st_members; i++) {
606                         if (!m[i]  ||  /* Found no counterpart */
607                             !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
608                                 return 0;
609                 }
610                 break;
611
612         case tpo_method: {
613                 size_t n_param1, n_param2;
614
615                 /** FIXME: is this still 1? */
616                 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
617                 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
618                 if (get_method_calling_convention(st) !=
619                     get_method_calling_convention(lt)) return 0;
620
621                 n_param1 = get_method_n_params(st);
622                 n_param2 = get_method_n_params(lt);
623
624                 if (n_param1 != n_param2) return 0;
625
626                 for (i = 0; i < get_method_n_params(st); i++) {
627                         if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
628                                 return 0;
629                 }
630                 for (i = 0; i < get_method_n_ress(st); i++) {
631                         if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
632                                 return 0;
633                 }
634         } break;
635
636         case tpo_union:
637                 n_st_members = get_union_n_members(st);
638                 if (n_st_members != get_union_n_members(lt)) return 0;
639                 m = ALLOCANZ(ir_entity*, n_st_members);
640                 /* First sort the members of lt */
641                 for (i = 0; i < n_st_members; ++i) {
642                         ir_entity *se = get_union_member(st, i);
643                         size_t n = get_union_n_members(lt);
644                         for (j = 0; j < n; ++j) {
645                                 ir_entity *le = get_union_member(lt, j);
646                                 if (get_entity_name(le) == get_entity_name(se))
647                                         m[i] = le;
648                         }
649                 }
650                 for (i = 0; i < n_st_members; ++i) {
651                         if (!m[i]  ||  /* Found no counterpart */
652                                 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
653                                 return 0;
654                 }
655                 break;
656
657         case tpo_array: {
658                 ir_type *set, *let;  /* small/large elt. ir_type */
659                 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
660                         return 0;
661                 set = get_array_element_type(st);
662                 let = get_array_element_type(lt);
663                 if (set != let) {
664                         /* If the element types are different, set must be convertible
665                            to let, and they must have the same size so that address
666                            computations work out.  To have a size the layout must
667                            be fixed. */
668                         if ((get_type_state(set) != layout_fixed) ||
669                             (get_type_state(let) != layout_fixed))
670                                 return 0;
671                         if (!smaller_type(set, let) ||
672                             get_type_size_bytes(set) != get_type_size_bytes(let))
673                                 return 0;
674                 }
675                 for (i = 0; i < get_array_n_dimensions(st); i++) {
676                         if (get_array_lower_bound(lt, i))
677                                 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
678                                         return 0;
679                                 if (get_array_upper_bound(lt, i))
680                                         if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
681                                                 return 0;
682                 }
683         } break;
684
685         case tpo_enumeration:
686                 assert(0 && "enumerations not implemented");
687                 break;
688
689         case tpo_pointer:
690                 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
691                         return 0;
692                 break;
693
694         case tpo_primitive:
695                 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
696                         return 0;
697                 break;
698
699         default: break;
700         }
701         return 1;
702 }
703
704
705 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
706 {
707         ir_type *res;
708
709         res = new_type(type_class, NULL, db);
710         res->name = name;
711
712         res->attr.ca.members     = NEW_ARR_F (ir_entity *, 0);
713         res->attr.ca.subtypes    = NEW_ARR_F (ir_type *, 0);
714         res->attr.ca.supertypes  = NEW_ARR_F (ir_type *, 0);
715         res->attr.ca.peculiarity = peculiarity_existent;
716         res->attr.ca.vtable_size = 0;
717         res->attr.ca.clss_flags  = cf_none;
718         res->attr.ca.dfn         = 0;
719         hook_new_type(res);
720         return res;
721 }
722
723 ir_type *new_type_class(ident *name)
724 {
725         return new_d_type_class(name, NULL);
726 }
727
728 void free_class_entities(ir_type *clss)
729 {
730         size_t i;
731         assert(clss && (clss->type_op == type_class));
732         /* we must iterate backward here */
733         for (i = get_class_n_members(clss); i > 0;)
734                 free_entity(get_class_member(clss, --i));
735 }
736
737 void free_class_attrs(ir_type *clss)
738 {
739         assert(clss && (clss->type_op == type_class));
740         DEL_ARR_F(clss->attr.ca.members);
741         DEL_ARR_F(clss->attr.ca.subtypes);
742         DEL_ARR_F(clss->attr.ca.supertypes);
743 }
744
745 ident *get_class_ident(const ir_type *clss)
746 {
747         assert(clss->type_op == type_class);
748         return clss->name;
749 }
750
751 const char *get_class_name(const ir_type *clss)
752 {
753         if (get_class_ident(clss) == NULL)
754                 return NULL;
755         return get_id_str(get_class_ident(clss));
756 }
757
758 static void add_class_member(ir_type *clss, ir_entity *member)
759 {
760         assert(clss && (clss->type_op == type_class));
761         assert(clss != get_entity_type(member) && "recursive type");
762         ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
763 }
764
765 size_t (get_class_n_members)(const ir_type *clss)
766 {
767         return _get_class_n_members(clss);
768 }
769
770 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
771 {
772         size_t i, n;
773         assert(clss && (clss->type_op == type_class));
774         for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
775                 if (get_class_member(clss, i) == mem)
776                         return i;
777         }
778         return INVALID_MEMBER_INDEX;
779 }
780
781 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
782 {
783         return _get_class_member(clss, pos);
784 }
785
786 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
787 {
788         size_t i, n_mem;
789         assert(clss && (clss->type_op == type_class));
790         n_mem = get_class_n_members(clss);
791         for (i = 0; i < n_mem; ++i) {
792                 ir_entity *mem = get_class_member(clss, i);
793                 if (get_entity_ident(mem) == name)
794                         return mem;
795         }
796         return NULL;
797 }
798
799 static void remove_class_member(ir_type *clss, ir_entity *member)
800 {
801         size_t i;
802         assert(clss && (clss->type_op == type_class));
803         for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
804                 if (clss->attr.ca.members[i] == member) {
805                         for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
806                                 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
807                         ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
808                         break;
809                 }
810         }
811 }
812
813 void add_class_subtype(ir_type *clss, ir_type *subtype)
814 {
815         size_t i;
816         assert(clss->type_op == type_class);
817         ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
818         for (i = 0; i < get_class_n_supertypes(subtype); i++) {
819                 if (get_class_supertype(subtype, i) == clss)
820                         /* Class already registered */
821                         return;
822         }
823         ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
824 }
825
826 size_t get_class_n_subtypes(const ir_type *clss)
827 {
828         assert(clss->type_op == type_class);
829         return ARR_LEN (clss->attr.ca.subtypes);
830 }
831
832 ir_type *get_class_subtype(ir_type *clss, size_t pos)
833 {
834         assert(clss->type_op == type_class);
835         assert(pos < get_class_n_subtypes(clss));
836         return clss->attr.ca.subtypes[pos];
837 }
838
839 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
840 {
841         size_t i, n_subtypes = get_class_n_subtypes(clss);
842         assert(is_Class_type(subclass));
843         for (i = 0; i < n_subtypes; ++i) {
844                 if (get_class_subtype(clss, i) == subclass)
845                         return i;
846         }
847         return (size_t)-1;
848 }
849
850 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
851 {
852         assert(clss->type_op == type_class);
853         assert(pos < get_class_n_subtypes(clss));
854         clss->attr.ca.subtypes[pos] = subtype;
855 }
856
857 void remove_class_subtype(ir_type *clss, ir_type *subtype)
858 {
859         size_t i;
860         assert(clss && (clss->type_op == type_class));
861         for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
862                 if (clss->attr.ca.subtypes[i] == subtype) {
863                         for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
864                                 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
865                         ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
866                         break;
867                 }
868         }
869 }
870
871 void add_class_supertype(ir_type *clss, ir_type *supertype)
872 {
873         size_t i;
874         size_t n;
875         assert(clss && (clss->type_op == type_class));
876         assert(supertype && (supertype -> type_op == type_class));
877         ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
878         for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
879                 if (get_class_subtype(supertype, i) == clss)
880                         /* Class already registered */
881                         return;
882         }
883         ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
884 }
885
886 size_t get_class_n_supertypes(const ir_type *clss)
887 {
888         assert(clss->type_op == type_class);
889         return ARR_LEN(clss->attr.ca.supertypes);
890 }
891
892 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
893 {
894         size_t i, n_supertypes = get_class_n_supertypes(clss);
895         assert(super_clss && (super_clss->type_op == type_class));
896         for (i = 0; i < n_supertypes; i++) {
897                 if (get_class_supertype(clss, i) == super_clss)
898                         return i;
899         }
900         return (size_t)-1;
901 }
902
903 ir_type *get_class_supertype(ir_type *clss, size_t pos)
904 {
905         assert(clss->type_op == type_class);
906         assert(pos < get_class_n_supertypes(clss));
907         return clss->attr.ca.supertypes[pos];
908 }
909
910 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
911 {
912         assert(clss->type_op == type_class);
913         assert(pos < get_class_n_supertypes(clss));
914         clss->attr.ca.supertypes[pos] = supertype;
915 }
916
917 void remove_class_supertype(ir_type *clss, ir_type *supertype)
918 {
919         size_t i;
920         assert(clss && (clss->type_op == type_class));
921         for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
922                 if (clss->attr.ca.supertypes[i] == supertype) {
923                         for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
924                                 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
925                         ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
926                         break;
927                 }
928         }
929 }
930
931 ir_peculiarity get_class_peculiarity(const ir_type *clss)
932 {
933         assert(clss && (clss->type_op == type_class));
934         return clss->attr.ca.peculiarity;
935 }
936
937 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
938 {
939         assert(clss && (clss->type_op == type_class));
940         assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
941         clss->attr.ca.peculiarity = pec;
942 }
943
944 unsigned (get_class_vtable_size)(const ir_type *clss)
945 {
946         return _get_class_vtable_size(clss);
947 }
948
949 void (set_class_vtable_size)(ir_type *clss, unsigned size)
950 {
951         _set_class_vtable_size(clss, size);
952 }
953
954 int (is_class_final)(const ir_type *clss)
955 {
956         return _is_class_final(clss);
957 }
958
959 void (set_class_final)(ir_type *clss, int flag)
960 {
961         _set_class_final(clss, flag);
962 }
963
964 int (is_class_interface)(const ir_type *clss)
965 {
966         return _is_class_interface(clss);
967 }
968
969 void (set_class_interface)(ir_type *clss, int flag)
970 {
971         _set_class_interface(clss, flag);
972 }
973
974 int (is_class_abstract)(const ir_type *clss)
975 {
976          return _is_class_abstract(clss);
977 }
978
979 void (set_class_abstract)(ir_type *clss, int final)
980 {
981         _set_class_abstract(clss, final);
982 }
983
984 void set_class_dfn(ir_type *clss, int dfn)
985 {
986         clss->attr.ca.dfn = dfn;
987 }
988
989 int get_class_dfn(const ir_type *clss)
990 {
991         return (clss->attr.ca.dfn);
992 }
993
994 int (is_Class_type)(const ir_type *clss)
995 {
996         return _is_class_type(clss);
997 }
998
999 void set_class_mode(ir_type *tp, ir_mode *mode)
1000 {
1001         /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1002         assert(get_type_state(tp) == layout_fixed &&
1003                tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1004         tp->mode = mode;
1005 }
1006
1007 void set_class_size(ir_type *tp, unsigned size)
1008 {
1009         tp->size = size;
1010 }
1011
1012
1013 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1014 {
1015         ir_type *res = new_type(type_struct, NULL, db);
1016         res->name = name;
1017
1018         res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1019         hook_new_type(res);
1020         return res;
1021 }
1022
1023 ir_type *new_type_struct(ident *name)
1024 {
1025         return new_d_type_struct (name, NULL);
1026 }
1027
1028 void free_struct_entities(ir_type *strct)
1029 {
1030         size_t i;
1031         assert(strct && (strct->type_op == type_struct));
1032         /* we must iterate backward here */
1033         for (i = get_struct_n_members(strct); i > 0;)
1034                 free_entity(get_struct_member(strct, --i));
1035 }
1036
1037 void free_struct_attrs(ir_type *strct)
1038 {
1039         assert(strct && (strct->type_op == type_struct));
1040         DEL_ARR_F(strct->attr.sa.members);
1041 }
1042
1043 ident *get_struct_ident(const ir_type *strct)
1044 {
1045         assert(strct->type_op == type_struct);
1046         return strct->name;
1047 }
1048
1049 const char *get_struct_name(const ir_type *strct)
1050 {
1051         if (get_struct_ident(strct) == NULL)
1052                 return NULL;
1053         return get_id_str(get_struct_ident(strct));
1054 }
1055
1056 size_t get_struct_n_members(const ir_type *strct)
1057 {
1058         assert(strct->type_op == type_struct);
1059         return ARR_LEN(strct->attr.sa.members);
1060 }
1061
1062 static void add_struct_member(ir_type *strct, ir_entity *member)
1063 {
1064         assert(strct && (strct->type_op == type_struct));
1065         assert(get_type_tpop(get_entity_type(member)) != type_method);
1066         assert(strct != get_entity_type(member) && "recursive type");
1067         ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1068 }
1069
1070 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1071 {
1072         assert(strct && (strct->type_op == type_struct));
1073         assert(pos < get_struct_n_members(strct));
1074         return strct->attr.sa.members[pos];
1075 }
1076
1077 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1078 {
1079         size_t i, n;
1080         assert(strct && (strct->type_op == type_struct));
1081         for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1082                 if (get_struct_member(strct, i) == mem)
1083                         return i;
1084         }
1085         return (size_t)-1;
1086 }
1087
1088 static void remove_struct_member(ir_type *strct, ir_entity *member)
1089 {
1090         size_t i;
1091         assert(strct && (strct->type_op == type_struct));
1092         for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1093                 if (strct->attr.sa.members[i] == member) {
1094                         for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1095                                 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1096                         ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1097                         break;
1098                 }
1099         }
1100 }
1101
1102 int (is_Struct_type)(const ir_type *strct)
1103 {
1104         return _is_struct_type(strct);
1105 }
1106
1107 void set_struct_mode(ir_type *tp, ir_mode *mode)
1108 {
1109         /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1110         assert(get_type_state(tp) == layout_fixed &&
1111                tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1112         tp->mode = mode;
1113 }
1114
1115 void set_struct_size(ir_type *tp, unsigned size)
1116 {
1117         tp->size = size;
1118 }
1119
1120 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1121 {
1122         ir_type *res;
1123
1124         assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1125         res = new_type(type_method, mode_P_code, db);
1126         res->flags               |= tf_layout_fixed;
1127         res->size                 = get_mode_size_bytes(mode_P_code);
1128         res->attr.ma.n_params     = n_param;
1129         res->attr.ma.params       = XMALLOCNZ(tp_ent_pair, n_param);
1130         res->attr.ma.n_res        = n_res;
1131         res->attr.ma.res_type     = XMALLOCNZ(tp_ent_pair, n_res);
1132         res->attr.ma.variadicity  = variadicity_non_variadic;
1133         res->attr.ma.properties   = mtp_no_property;
1134         hook_new_type(res);
1135         return res;
1136 }
1137
1138 ir_type *new_type_method(size_t n_param, size_t n_res)
1139 {
1140         return new_d_type_method(n_param, n_res, NULL);
1141 }
1142
1143 ir_type *clone_type_method(ir_type *tp)
1144 {
1145         ir_type  *res;
1146         ir_mode  *mode;
1147         size_t    n_params;
1148         size_t    n_res;
1149         type_dbg_info *db;
1150
1151         assert(is_Method_type(tp));
1152
1153         mode     = tp->mode;
1154         n_params = tp->attr.ma.n_params;
1155         n_res    = tp->attr.ma.n_res;
1156         db       = tp->dbi;
1157
1158         res = new_type(type_method, mode, db);
1159
1160         res->flags                    = tp->flags;
1161         res->higher_type              = tp->higher_type;
1162         res->size                     = tp->size;
1163         res->attr.ma.n_params         = n_params;
1164         res->attr.ma.params           = XMALLOCN(tp_ent_pair, n_params);
1165         memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1166         res->attr.ma.n_res            = n_res;
1167         res->attr.ma.res_type         = XMALLOCN(tp_ent_pair, n_res);
1168         memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1169         res->attr.ma.variadicity      = tp->attr.ma.variadicity;
1170         res->attr.ma.properties       = tp->attr.ma.properties;
1171         res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1172         hook_new_type(res);
1173         return res;
1174 }
1175
1176 void free_method_entities(ir_type *method)
1177 {
1178         (void) method;
1179         assert(method && (method->type_op == type_method));
1180 }
1181
1182 void free_method_attrs(ir_type *method)
1183 {
1184         assert(method && (method->type_op == type_method));
1185         free(method->attr.ma.params);
1186         free(method->attr.ma.res_type);
1187 }
1188
1189 size_t (get_method_n_params)(const ir_type *method)
1190 {
1191         return _get_method_n_params(method);
1192 }
1193
1194 ir_type *get_method_param_type(const ir_type *method, size_t pos)
1195 {
1196         ir_type *res;
1197         assert(method->type_op == type_method);
1198         assert(pos < get_method_n_params(method));
1199         res = method->attr.ma.params[pos].tp;
1200         assert(res != NULL && "empty method param type");
1201         return res;
1202 }
1203
1204 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1205 {
1206         assert(method->type_op == type_method);
1207         assert(pos < get_method_n_params(method));
1208         method->attr.ma.params[pos].tp = tp;
1209 }
1210
1211 size_t (get_method_n_ress)(const ir_type *method)
1212 {
1213         return _get_method_n_ress(method);
1214 }
1215
1216 ir_type *get_method_res_type(const ir_type *method, size_t pos)
1217 {
1218         ir_type *res;
1219         assert(method->type_op == type_method);
1220         assert(pos < get_method_n_ress(method));
1221         res = method->attr.ma.res_type[pos].tp;
1222         assert(res != NULL && "empty method return type");
1223         return res;
1224 }
1225
1226 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1227 {
1228         assert(method->type_op == type_method);
1229         assert(pos < get_method_n_ress(method));
1230         /* set the result ir_type */
1231         method->attr.ma.res_type[pos].tp = tp;
1232         /* If information constructed set pass-by-value representation. */
1233 }
1234
1235 const char *get_variadicity_name(ir_variadicity vari)
1236 {
1237 #define X(a)    case a: return #a
1238         switch (vari) {
1239         X(variadicity_non_variadic);
1240         X(variadicity_variadic);
1241         default:
1242                 return "BAD VALUE";
1243         }
1244 #undef X
1245 }
1246
1247 ir_variadicity get_method_variadicity(const ir_type *method)
1248 {
1249         assert(method && (method->type_op == type_method));
1250         return method->attr.ma.variadicity;
1251 }
1252
1253 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1254 {
1255         assert(method && (method->type_op == type_method));
1256         method->attr.ma.variadicity = vari;
1257 }
1258
1259 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1260 {
1261         return _get_method_additional_properties(method);
1262 }
1263
1264 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1265 {
1266         _set_method_additional_properties(method, mask);
1267 }
1268
1269 void (add_method_additional_properties)(ir_type *method,
1270                                         mtp_additional_properties flag)
1271 {
1272         _add_method_additional_properties(method, flag);
1273 }
1274
1275 unsigned (get_method_calling_convention)(const ir_type *method)
1276 {
1277         return _get_method_calling_convention(method);
1278 }
1279
1280 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1281 {
1282         _set_method_calling_convention(method, cc_mask);
1283 }
1284
1285 unsigned get_method_n_regparams(ir_type *method)
1286 {
1287         unsigned cc = get_method_calling_convention(method);
1288         assert(IS_FASTCALL(cc));
1289
1290         return cc & ~cc_bits;
1291 }
1292
1293 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1294 {
1295         unsigned cc = get_method_calling_convention(method);
1296         assert(IS_FASTCALL(cc));
1297
1298         set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1299 }
1300
1301 int (is_Method_type)(const ir_type *method)
1302 {
1303         return _is_method_type(method);
1304 }
1305
1306
1307 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1308 {
1309         ir_type *res = new_type(type_union, NULL, db);
1310         res->name = name;
1311
1312         res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1313         hook_new_type(res);
1314         return res;
1315 }
1316
1317 ir_type *new_type_union(ident *name)
1318 {
1319         return new_d_type_union(name, NULL);
1320 }
1321
1322 void free_union_entities(ir_type *uni)
1323 {
1324         size_t i;
1325         assert(uni && (uni->type_op == type_union));
1326         /* we must iterate backward here */
1327         for (i = get_union_n_members(uni); i > 0;)
1328                 free_entity(get_union_member(uni, --i));
1329 }
1330
1331 void free_union_attrs(ir_type *uni)
1332 {
1333         assert(uni && (uni->type_op == type_union));
1334         DEL_ARR_F(uni->attr.ua.members);
1335 }
1336
1337 ident *get_union_ident(const ir_type *uni)
1338 {
1339         assert(uni->type_op == type_union);
1340         return uni->name;
1341 }
1342
1343 const char *get_union_name(const ir_type *uni)
1344 {
1345         if (get_union_ident(uni) == NULL)
1346                 return NULL;
1347         return get_id_str(get_union_ident(uni));
1348 }
1349
1350 size_t get_union_n_members(const ir_type *uni)
1351 {
1352         assert(uni->type_op == type_union);
1353         return ARR_LEN(uni->attr.ua.members);
1354 }
1355
1356 static void add_union_member(ir_type *uni, ir_entity *member)
1357 {
1358         assert(uni->type_op == type_union);
1359         assert(uni != get_entity_type(member) && "recursive type");
1360         ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1361 }
1362
1363 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1364 {
1365         assert(uni->type_op == type_union);
1366         assert(pos < get_union_n_members(uni));
1367         return uni->attr.ua.members[pos];
1368 }
1369
1370 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1371 {
1372         size_t i, n;
1373         assert(uni && (uni->type_op == type_union));
1374         for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1375                 if (get_union_member(uni, i) == mem)
1376                         return i;
1377         }
1378         return (size_t)-1;
1379 }
1380
1381 static void remove_union_member(ir_type *uni, ir_entity *member)
1382 {
1383         size_t i;
1384         assert(uni && (uni->type_op == type_union));
1385         for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1386                 if (uni->attr.ua.members[i] == member) {
1387                         for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1388                                 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1389                         ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1390                         break;
1391                 }
1392         }
1393 }
1394
1395 int (is_Union_type)(const ir_type *uni)
1396 {
1397         return _is_union_type(uni);
1398 }
1399
1400 void set_union_size(ir_type *tp, unsigned size)
1401 {
1402         tp->size = size;
1403 }
1404
1405
1406 ir_type *new_type_segment(ident *const name, type_flags const flags)
1407 {
1408         ir_type *const seg = new_type_class(name);
1409         seg->flags |= tf_segment | flags;
1410         set_class_final(seg, true);
1411         return seg;
1412 }
1413
1414
1415 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1416                           type_dbg_info *db)
1417 {
1418         ir_type *res;
1419         size_t i;
1420         ir_node *unk;
1421         ir_graph *irg = get_const_code_irg();
1422
1423         assert(!is_Method_type(element_type));
1424
1425         res = new_type(type_array, NULL, db);
1426         res->attr.aa.n_dimensions = n_dimensions;
1427         res->attr.aa.lower_bound  = XMALLOCNZ(ir_node*, n_dimensions);
1428         res->attr.aa.upper_bound  = XMALLOCNZ(ir_node*, n_dimensions);
1429         res->attr.aa.order        = XMALLOCNZ(size_t,   n_dimensions);
1430
1431         unk = new_r_Unknown(irg, mode_Iu);
1432         for (i = 0; i < n_dimensions; i++) {
1433                 res->attr.aa.lower_bound[i] =
1434                 res->attr.aa.upper_bound[i] = unk;
1435                 res->attr.aa.order[i]       = i;
1436         }
1437
1438         ident *const id = new_id_from_chars("elem_ent", 8);
1439         res->attr.aa.element_type = element_type;
1440         res->attr.aa.element_ent  = new_entity(res, id, element_type);
1441
1442         hook_new_type(res);
1443         return res;
1444 }
1445
1446 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1447 {
1448         return new_d_type_array(n_dimensions, element_type, NULL);
1449 }
1450
1451 void free_array_automatic_entities(ir_type *array)
1452 {
1453         assert(array->type_op == type_array);
1454         free_entity(get_array_element_entity(array));
1455 }
1456
1457 void free_array_entities(ir_type *array)
1458 {
1459         (void) array;
1460         assert(array->type_op == type_array);
1461 }
1462
1463 void free_array_attrs(ir_type *array)
1464 {
1465         assert(array->type_op == type_array);
1466         free(array->attr.aa.lower_bound);
1467         free(array->attr.aa.upper_bound);
1468         free(array->attr.aa.order);
1469 }
1470
1471 size_t get_array_n_dimensions(const ir_type *array)
1472 {
1473         assert(array->type_op == type_array);
1474         return array->attr.aa.n_dimensions;
1475 }
1476
1477 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1478                       ir_node *upper_bound)
1479 {
1480         assert(array->type_op == type_array);
1481         assert(lower_bound && "lower_bound node may not be NULL.");
1482         assert(upper_bound && "upper_bound node may not be NULL.");
1483         assert(dimension < array->attr.aa.n_dimensions);
1484         array->attr.aa.lower_bound[dimension] = lower_bound;
1485         array->attr.aa.upper_bound[dimension] = upper_bound;
1486 }
1487
1488 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1489                           int upper_bound)
1490 {
1491         ir_graph *irg = get_const_code_irg();
1492         set_array_bounds(array, dimension,
1493                   new_r_Const_long(irg, mode_Iu, lower_bound),
1494                   new_r_Const_long(irg, mode_Iu, upper_bound));
1495 }
1496
1497 void set_array_lower_bound(ir_type *array, size_t dimension,
1498                            ir_node *lower_bound)
1499 {
1500         assert(array->type_op == type_array);
1501         assert(lower_bound && "lower_bound node may not be NULL.");
1502         array->attr.aa.lower_bound[dimension] = lower_bound;
1503 }
1504
1505 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1506 {
1507         ir_graph *irg = get_const_code_irg();
1508         set_array_lower_bound(array, dimension,
1509              new_r_Const_long(irg, mode_Iu, lower_bound));
1510 }
1511
1512 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1513 {
1514   assert(array->type_op == type_array);
1515   assert(upper_bound && "upper_bound node may not be NULL.");
1516   array->attr.aa.upper_bound[dimension] = upper_bound;
1517 }
1518
1519 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1520 {
1521         ir_graph *irg = get_const_code_irg();
1522         set_array_upper_bound(array, dimension,
1523                               new_r_Const_long(irg, mode_Iu, upper_bound));
1524 }
1525
1526 int has_array_lower_bound(const ir_type *array, size_t dimension)
1527 {
1528         assert(array->type_op == type_array);
1529         return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1530 }
1531
1532 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1533 {
1534         assert(array->type_op == type_array);
1535         return array->attr.aa.lower_bound[dimension];
1536 }
1537
1538 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1539 {
1540         ir_node *node;
1541         assert(array->type_op == type_array);
1542         node = array->attr.aa.lower_bound[dimension];
1543         return get_tarval_long(get_Const_tarval(node));
1544 }
1545
1546 int has_array_upper_bound(const ir_type *array, size_t dimension)
1547 {
1548         assert(array->type_op == type_array);
1549         return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1550 }
1551
1552 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1553 {
1554         assert(array->type_op == type_array);
1555         return array->attr.aa.upper_bound[dimension];
1556 }
1557
1558 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1559 {
1560         ir_node *node;
1561         assert(array->type_op == type_array);
1562         node = array->attr.aa.upper_bound[dimension];
1563         return get_tarval_long(get_Const_tarval(node));
1564 }
1565
1566 void set_array_order(ir_type *array, size_t dimension, size_t order)
1567 {
1568         assert(array->type_op == type_array);
1569         array->attr.aa.order[dimension] = order;
1570 }
1571
1572 size_t get_array_order(const ir_type *array, size_t dimension)
1573 {
1574         assert(array->type_op == type_array);
1575         return array->attr.aa.order[dimension];
1576 }
1577
1578 size_t find_array_dimension(const ir_type *array, size_t order)
1579 {
1580         size_t dim;
1581
1582         assert(array->type_op == type_array);
1583
1584         for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1585                 if (array->attr.aa.order[dim] == order)
1586                         return dim;
1587         }
1588         return (size_t)-1;
1589 }
1590
1591 void set_array_element_type(ir_type *array, ir_type *tp)
1592 {
1593         assert(array->type_op == type_array);
1594         assert(!is_Method_type(tp));
1595         array->attr.aa.element_type = tp;
1596 }
1597
1598 ir_type *get_array_element_type(const ir_type *array)
1599 {
1600         assert(array->type_op == type_array);
1601         return array->attr.aa.element_type;
1602 }
1603
1604 void set_array_element_entity(ir_type *array, ir_entity *ent)
1605 {
1606         assert(array->type_op == type_array);
1607         assert((get_entity_type(ent)->type_op != type_method));
1608         array->attr.aa.element_ent = ent;
1609         array->attr.aa.element_type = get_entity_type(ent);
1610 }
1611
1612 ir_entity *get_array_element_entity(const ir_type *array)
1613 {
1614         assert(array->type_op == type_array);
1615         return array->attr.aa.element_ent;
1616 }
1617
1618 int is_array_variable_size(const ir_type *array)
1619 {
1620         assert(array->type_op == type_array);
1621         return (array->flags & tf_variable_size) != 0;
1622 }
1623
1624 void set_array_variable_size(ir_type *array, int flag)
1625 {
1626         assert(array->type_op == type_array);
1627         array->flags = (array->flags & ~tf_variable_size)
1628                        | (flag != 0 ? tf_variable_size : 0);
1629 }
1630
1631 int (is_Array_type)(const ir_type *array)
1632 {
1633         return _is_array_type(array);
1634 }
1635
1636 void set_array_size(ir_type *tp, unsigned size)
1637 {
1638         /* FIXME: Here we should make some checks with the element type size */
1639         tp->size = size;
1640 }
1641
1642
1643 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1644 {
1645         ir_type *res;
1646
1647         res = new_type(type_enumeration, NULL, db);
1648         res->name = name;
1649         res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1650         hook_new_type(res);
1651         return res;
1652 }
1653
1654 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1655 {
1656         return new_d_type_enumeration(name, n_enums, NULL);
1657 }
1658
1659 void free_enumeration_entities(ir_type *enumeration)
1660 {
1661         (void) enumeration;
1662         assert(enumeration->type_op == type_enumeration);
1663 }
1664
1665 void free_enumeration_attrs(ir_type *enumeration)
1666 {
1667         assert(enumeration->type_op == type_enumeration);
1668         DEL_ARR_F(enumeration->attr.ea.enumer);
1669 }
1670
1671 ident *get_enumeration_ident(const ir_type *enumeration)
1672 {
1673         assert(enumeration->type_op == type_enumeration);
1674         return enumeration->name;
1675 }
1676
1677 const char *get_enumeration_name(const ir_type *enumeration)
1678 {
1679         if (get_enumeration_ident(enumeration) == NULL)
1680                 return NULL;
1681         return get_id_str(get_enumeration_ident(enumeration));
1682 }
1683
1684 size_t get_enumeration_n_enums(const ir_type *enumeration)
1685 {
1686         assert(enumeration->type_op == type_enumeration);
1687         return ARR_LEN(enumeration->attr.ea.enumer);
1688 }
1689
1690 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1691                            ir_tarval *con)
1692 {
1693         assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1694         enumeration->attr.ea.enumer[pos].nameid = nameid;
1695         enumeration->attr.ea.enumer[pos].value  = con;
1696         enumeration->attr.ea.enumer[pos].owner  = enumeration;
1697 }
1698
1699 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1700 {
1701         assert(enumeration->type_op == type_enumeration);
1702         assert(pos < get_enumeration_n_enums(enumeration));
1703         return &enumeration->attr.ea.enumer[pos];
1704 }
1705
1706 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1707 {
1708         return enum_cnst->owner;
1709 }
1710
1711 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1712 {
1713         enum_cnst->value = con;
1714 }
1715
1716 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1717 {
1718         return enum_cnst->value;
1719 }
1720
1721 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1722 {
1723         enum_cnst->nameid = id;
1724 }
1725
1726 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1727 {
1728         return enum_cnst->nameid;
1729 }
1730
1731 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1732 {
1733         return get_id_str(enum_cnst->nameid);
1734 }
1735
1736 int (is_Enumeration_type)(const ir_type *enumeration)
1737 {
1738         return _is_enumeration_type(enumeration);
1739 }
1740
1741 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1742 {
1743         assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1744         /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1745         assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1746
1747         tp->size = get_mode_size_bytes(mode);
1748         tp->mode = mode;
1749 }
1750
1751
1752
1753 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1754 {
1755         ir_type *res;
1756         ir_mode *mode;
1757
1758         if (is_Method_type(points_to) || is_code_type(points_to)) {
1759                 mode = mode_P_code;
1760         } else {
1761                 mode = mode_P_data;
1762         }
1763
1764         res = new_type(type_pointer, mode, db);
1765         res->attr.pa.points_to = points_to;
1766         assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1767         res->size = get_mode_size_bytes(res->mode);
1768         res->flags |= tf_layout_fixed;
1769         hook_new_type(res);
1770         return res;
1771 }
1772
1773 ir_type *new_type_pointer(ir_type *points_to)
1774 {
1775         return new_d_type_pointer(points_to, NULL);
1776 }
1777
1778 void free_pointer_entities(ir_type *pointer)
1779 {
1780         (void) pointer;
1781         assert(pointer && (pointer->type_op == type_pointer));
1782 }
1783
1784 void free_pointer_attrs(ir_type *pointer)
1785 {
1786         (void) pointer;
1787         assert(pointer && (pointer->type_op == type_pointer));
1788 }
1789
1790 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1791 {
1792         assert(pointer && (pointer->type_op == type_pointer));
1793         pointer->attr.pa.points_to = tp;
1794 }
1795
1796 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1797 {
1798         assert(pointer && (pointer->type_op == type_pointer));
1799         return pointer->attr.pa.points_to;
1800 }
1801
1802 int (is_Pointer_type)(const ir_type *pointer)
1803 {
1804         return _is_pointer_type(pointer);
1805 }
1806
1807 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1808 {
1809         assert(mode_is_reference(mode) && "Modes of pointers must be references");
1810         /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1811         assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1812
1813         tp->size = get_mode_size_bytes(mode);
1814         tp->mode = mode;
1815 }
1816
1817 ir_type *find_pointer_type_to_type(ir_type *tp)
1818 {
1819         size_t i, n = get_irp_n_types();
1820         for (i = 0; i < n; ++i) {
1821                 ir_type *found = get_irp_type(i);
1822                 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1823                         return (found);
1824         }
1825         return get_unknown_type();
1826 }
1827
1828
1829 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1830 {
1831         ir_type *res = new_type(type_primitive, mode, db);
1832         res->size  = get_mode_size_bytes(mode);
1833         res->flags |= tf_layout_fixed;
1834         res->attr.ba.base_type = NULL;
1835         hook_new_type(res);
1836         return res;
1837 }
1838
1839 ir_type *new_type_primitive(ir_mode *mode)
1840 {
1841         return new_d_type_primitive(mode, NULL);
1842 }
1843
1844 int (is_Primitive_type)(const ir_type *primitive)
1845 {
1846         return _is_primitive_type(primitive);
1847 }
1848
1849 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1850 {
1851         /* Modes of primitives must be data */
1852         assert(mode_is_data(mode));
1853
1854         /* For primitive size depends on the mode. */
1855         tp->size = get_mode_size_bytes(mode);
1856         tp->mode = mode;
1857 }
1858
1859 ir_type *get_primitive_base_type(const ir_type *tp)
1860 {
1861         assert(is_Primitive_type(tp));
1862         return tp->attr.ba.base_type;
1863 }
1864
1865 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
1866 {
1867         assert(is_Primitive_type(tp));
1868         tp->attr.ba.base_type = base_tp;
1869 }
1870
1871
1872
1873 int (is_atomic_type)(const ir_type *tp)
1874 {
1875         return _is_atomic_type(tp);
1876 }
1877
1878 size_t get_compound_n_members(const ir_type *tp)
1879 {
1880         const tp_op *op  = get_type_tpop(tp);
1881         return op->ops.get_n_members(tp);
1882 }
1883
1884 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
1885 {
1886         const tp_op *op = get_type_tpop(tp);
1887         return op->ops.get_member(tp, pos);
1888 }
1889
1890 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
1891 {
1892         const tp_op *op = get_type_tpop(tp);
1893         return op->ops.get_member_index(tp, member);
1894 }
1895
1896 void set_compound_variable_size(ir_type *tp, int variable_size_flag)
1897 {
1898         assert(is_compound_type(tp));
1899         tp->flags = (tp->flags & ~tf_variable_size)
1900                     | (variable_size_flag != 0 ? tf_variable_size : 0);
1901 }
1902
1903 int is_compound_variable_size(const ir_type *tp)
1904 {
1905         assert(is_compound_type(tp));
1906         return (tp->flags & tf_variable_size) != 0;
1907 }
1908
1909 int is_compound_type(const ir_type *tp)
1910 {
1911         assert(tp->kind == k_type);
1912         return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1913 }
1914
1915 ident *get_compound_ident(const ir_type *tp)
1916 {
1917         assert(is_compound_type(tp));
1918         return tp->name;
1919 }
1920
1921 const char *get_compound_name(const ir_type *tp)
1922 {
1923         if (get_compound_ident(tp) == NULL)
1924                 return NULL;
1925         return get_id_str(get_compound_ident(tp));
1926 }
1927
1928 void remove_compound_member(ir_type *compound, ir_entity *entity)
1929 {
1930         switch (get_type_tpop_code(compound)) {
1931         case tpo_class:  remove_class_member(compound, entity);  break;
1932         case tpo_struct: remove_struct_member(compound, entity); break;
1933         case tpo_union:  remove_union_member(compound, entity);  break;
1934         default:
1935                 panic("argument for remove_compound_member not a compound type");
1936         }
1937 }
1938
1939 void add_compound_member(ir_type *compound, ir_entity *entity)
1940 {
1941         switch (get_type_tpop_code(compound)) {
1942         case tpo_class:  add_class_member(compound, entity);  break;
1943         case tpo_struct: add_struct_member(compound, entity); break;
1944         case tpo_union:  add_union_member(compound, entity);  break;
1945         default:
1946                 panic("argument for add_compound_member not a compound type");
1947         }
1948 }
1949
1950 int is_code_type(const ir_type *tp)
1951 {
1952         assert(tp->kind == k_type);
1953         return tp->type_op == tpop_code;
1954 }
1955
1956 int is_unknown_type(const ir_type *tp)
1957 {
1958         assert(tp->kind == k_type);
1959         return tp->type_op == tpop_unknown;
1960 }
1961
1962 int is_none_type(const ir_type *tp)
1963 {
1964         assert(tp->kind == k_type);
1965         return tp->type_op == tpop_none;
1966 }
1967
1968 int is_frame_type(const ir_type *tp)
1969 {
1970         return tp->flags & tf_frame_type;
1971 }
1972
1973 ir_type *new_type_frame(void)
1974 {
1975         ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
1976
1977         res->flags |= tf_frame_type;
1978
1979         /* It is not possible to derive from the frame type. Set the final flag. */
1980         set_class_final(res, 1);
1981
1982         return res;
1983 }
1984
1985 ir_type *clone_frame_type(ir_type *type)
1986 {
1987         ir_type *res;
1988         size_t  i, n;
1989
1990         assert(is_frame_type(type));
1991         /* the entity link resource should be allocated if this function is called */
1992         assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
1993
1994         res = new_type_frame();
1995         for (i = 0, n = get_class_n_members(type); i < n; ++i) {
1996                 ir_entity *ent  = get_class_member(type, i);
1997                 ir_entity *nent = copy_entity_own(ent, res);
1998                 set_entity_link(ent, nent);
1999                 set_entity_link(nent, ent);
2000         }
2001         return res;
2002 }
2003
2004 void set_default_size(ir_type *tp, unsigned size)
2005 {
2006         tp->size = size;
2007 }
2008
2009 void default_layout_compound_type(ir_type *type)
2010 {
2011         size_t   i;
2012         size_t   n         = get_compound_n_members(type);
2013         int      size      = 0;
2014         unsigned align_all = 1;
2015         bool     var_size  = is_compound_variable_size(type);
2016
2017         for (i = 0; i < n; ++i) {
2018                 ir_entity *entity      = get_compound_member(type, i);
2019                 ir_type   *entity_type = get_entity_type(entity);
2020                 unsigned   align;
2021                 unsigned   misalign;
2022                 unsigned   entity_size;
2023
2024                 if (is_Method_type(entity_type))
2025                         continue;
2026
2027                 if (i+1 < n || !var_size) {
2028                         assert(get_type_state(entity_type) == layout_fixed);
2029                         entity_size = get_type_size_bytes(entity_type);
2030                 } else {
2031                         entity_size = 0;
2032                 }
2033
2034                 align     = get_type_alignment_bytes(entity_type);
2035                 align_all = align > align_all ? align : align_all;
2036                 misalign  = (align ? size % align : 0);
2037                 size     += (misalign ? align - misalign : 0);
2038
2039                 set_entity_offset(entity, size);
2040                 if (!is_Union_type(type)) {
2041                         size += entity_size;
2042                 }
2043         }
2044         if (align_all > 0 && size % align_all) {
2045                 size += align_all - (size % align_all);
2046         }
2047         if (align_all > get_type_alignment_bytes(type)) {
2048                 set_type_alignment_bytes(type, align_all);
2049         }
2050         set_type_size_bytes(type, size);
2051         set_type_state(type, layout_fixed);
2052 }
2053
2054 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2055                             int at_start)
2056 {
2057         ir_entity *area;
2058         ir_type *tp;
2059         ident *name;
2060         char buf[32];
2061         int offset;
2062         unsigned frame_size  = get_type_size_bytes(frame_type);
2063         unsigned frame_align = get_type_alignment_bytes(frame_type);
2064         static unsigned area_cnt = 0;
2065
2066         assert(is_frame_type(frame_type));
2067         assert(get_type_state(frame_type) == layout_fixed);
2068         assert(get_type_alignment_bytes(frame_type) > 0);
2069         set_type_state(frame_type, layout_undefined);
2070
2071         if (irp->byte_type == NULL)
2072                 irp->byte_type = new_type_primitive(mode_Bu);
2073
2074         snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2075         name = new_id_from_str(buf);
2076
2077         tp = new_type_array(1, irp->byte_type);
2078         set_array_bounds_int(tp, 0, 0, size);
2079         set_type_alignment_bytes(tp, alignment);
2080         set_type_size_bytes(tp, size);
2081
2082         if (at_start) {
2083                 size_t i, n;
2084                 unsigned delta = (size + frame_align - 1) & ~(frame_align - 1);
2085                 /* fix all offsets so far */
2086                 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2087                         ir_entity *ent = get_class_member(frame_type, i);
2088
2089                         set_entity_offset(ent, get_entity_offset(ent) + delta);
2090                 }
2091                 /* calculate offset and new type size */
2092                 offset = 0;
2093                 frame_size += delta;
2094         } else {
2095                 /* calculate offset and new type size */
2096                 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2097                 frame_size = offset + size;
2098         }
2099
2100         area = new_entity(frame_type, name, tp);
2101         set_entity_offset(area, offset);
2102         set_type_size_bytes(frame_type, frame_size);
2103         if (alignment > frame_align) {
2104                 set_type_alignment_bytes(frame_type, alignment);
2105         }
2106
2107         /* mark this entity as compiler generated */
2108         set_entity_compiler_generated(area, 1);
2109
2110         set_type_state(frame_type, layout_fixed);
2111         return area;
2112 }
2113
2114 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2115 {
2116         ident *id;
2117         int p;
2118         type_dbg_info *tdbgi = get_type_dbg_info(type);
2119         if (tdbgi != NULL) {
2120                 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2121                 return;
2122         }
2123
2124         /* we have to construct some name... */
2125         switch (get_type_tpop_code(type)) {
2126         case tpo_uninitialized:
2127                 break;
2128         case tpo_code:
2129                 snprintf(buffer, buffer_size, "code");
2130                 return;
2131
2132         case tpo_class:
2133                 id = get_class_ident(type);
2134                 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2135                 return;
2136
2137         case tpo_struct:
2138                 id = get_struct_ident(type);
2139                 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2140                 return;
2141
2142         case tpo_union:
2143                 id = get_union_ident(type);
2144                 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2145                 return;
2146
2147         case tpo_enumeration:
2148                 id = get_enumeration_ident(type);
2149                 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2150                 return;
2151
2152         case tpo_unknown:
2153                 snprintf(buffer, buffer_size, "unknown type");
2154                 return;
2155
2156         case tpo_pointer:
2157                 p = snprintf(buffer, buffer_size, "pointer to ");
2158                 buffer      += p;
2159                 buffer_size -= p;
2160                 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2161                 return;
2162
2163         case tpo_array:
2164                 p = snprintf(buffer, buffer_size, "array of ");
2165                 buffer      += p;
2166                 buffer_size -= p;
2167                 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2168                 return;
2169
2170         case tpo_primitive:
2171                 id = get_mode_ident(get_type_mode(type));
2172                 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2173                 return;
2174
2175         case tpo_none:
2176                 snprintf(buffer, buffer_size, "none");
2177                 return;
2178         case tpo_method:
2179                 /* TODO: we should print argument and return types here... */
2180                 snprintf(buffer, buffer_size, "method type");
2181                 return;
2182         }
2183         snprintf(buffer, buffer_size, "invalid type");
2184 }