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