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