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