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