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