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