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