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