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