4 * file type.c - implementation of the datastructure to hold
7 * (C) 2001 by Universitaet Karlsruhe
9 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
12 * This module supplies a datastructure to represent all types
13 * known in the compiled program. This includes types specified
14 * in the program as well as types defined by the language. In the
15 * view of the intermediate representation there is no difference
16 * between these types.
18 * There exist several kinds of types, arranged by the structure of
19 * the type. A type is described by a set of attributes. Some of
20 * these attributes are common to all types, others depend on the
23 * Types are different from the modes defined in irmode: Types are
24 * on the level of the programming language, modes at the level of
25 * the target processor.
39 # include "typegmod_t.h"
46 /*******************************************************************/
48 /*******************************************************************/
50 unsigned long type_visited;
51 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
52 INLINE unsigned long get_master_type_visited() { return type_visited; }
53 INLINE void inc_master_type_visited() { type_visited++; }
55 void free_type(type *tp) {
56 /* Remove from list of all types */
58 /* Free the attributes of the type. */
60 /* Free entities automatically allocated with the type */
61 if (is_array_type(tp))
62 free_entity(get_array_element_entity(tp));
63 /* And now the type itself... */
68 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
72 assert(type_op != type_id);
74 node_size = offsetof (type, attr) + type_op->attr_size;
75 res = (type *) xmalloc (node_size);
76 add_irp_type(res); /* Remember the new type global. */
79 res->type_op = type_op;
82 res->state = layout_undefined;
90 void free_type_attrs(type *tp) {
91 switch(get_type_tpop_code(tp)) {
92 case tpo_class: { free_class_attrs(tp); } break;
93 case tpo_struct: { free_struct_attrs(tp); } break;
94 case tpo_method: { free_method_attrs(tp); } break;
95 case tpo_union: { free_union_attrs(tp); } break;
96 case tpo_array: { free_array_attrs(tp); } break;
97 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
98 case tpo_pointer: { free_pointer_attrs(tp); } break;
99 case tpo_primitive: { free_primitive_attrs(tp); } break;
104 /* set/get the link field */
105 void *get_type_link(type *tp)
107 assert(tp && tp->kind == k_type);
111 void set_type_link(type *tp, void *l)
113 assert(tp && tp->kind == k_type);
117 tp_op* get_type_tpop(type *tp) {
118 assert(tp && tp->kind == k_type);
122 ident* get_type_tpop_nameid(type *tp) {
123 assert(tp && tp->kind == k_type);
124 return tp->type_op->name;
127 const char* get_type_tpop_name(type *tp) {
128 assert(tp && tp->kind == k_type);
129 return id_to_str(tp->type_op->name);
132 tp_opcode get_type_tpop_code(type *tp) {
133 assert(tp && tp->kind == k_type);
134 return tp->type_op->code;
137 ir_mode* get_type_mode(type *tp) {
138 assert(tp && tp->kind == k_type);
142 void set_type_mode(type *tp, ir_mode* m) {
143 assert(tp && tp->kind == k_type);
145 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
146 /* Modes of primitives must be data */
147 ((tp->type_op != type_enumeration) || mode_is_int(m)));
148 /* Modes of enumerations must be integers */
150 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
151 /* For pointer, primitive and enumeration size depends on the mode. */
152 tp->size = get_mode_size(m);
157 ident* get_type_ident(type *tp) {
158 assert(tp && tp->kind == k_type);
162 void set_type_ident(type *tp, ident* id) {
163 assert(tp && tp->kind == k_type);
167 const char* get_type_name(type *tp) {
168 assert(tp && tp->kind == k_type);
169 return (id_to_str(tp->name));
172 int get_type_size(type *tp) {
173 assert(tp && tp->kind == k_type);
178 set_type_size(type *tp, int size) {
179 assert(tp && tp->kind == k_type);
180 /* For pointer enumeration and primitive size depends on the mode.
181 Methods don't have a size. */
182 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
183 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
188 get_type_state(type *tp) {
189 assert(tp && tp->kind == k_type);
194 set_type_state(type *tp, type_state state) {
195 assert(tp && tp->kind == k_type);
197 if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
198 (tp->type_op == type_method))
201 /* Just a correctness check: */
202 if (state == layout_fixed) {
204 switch (get_type_tpop_code(tp)) {
207 assert(get_type_size(tp) > -1);
208 if (tp != get_glob_type())
209 for (i = 0; i < get_class_n_members(tp); i++) {
210 assert(get_entity_offset(get_class_member(tp, i)) > -1);
211 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
212 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
213 /* @@@ lowerfirm geht nicht durch */
218 /* assert(get_type_size(tp) > -1); @@@ lowerfirm geht nicht durch */
219 for (i = 0; i < get_struct_n_members(tp); i++) {
220 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
221 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
230 Assure that only innermost dimension is dynamic? */
232 case tpo_enumeration:
234 assert(get_type_mode != NULL);
235 for (i = 0; i < get_enumeration_n_enums(tp); i++)
236 assert(get_enumeration_enum(tp, i) != NULL);
244 unsigned long get_type_visited(type *tp) {
245 assert(tp && tp->kind == k_type);
249 void set_type_visited(type *tp, unsigned long num) {
250 assert(tp && tp->kind == k_type);
253 /* Sets visited field in type to type_visited. */
254 void mark_type_visited(type *tp) {
255 assert(tp && tp->kind == k_type);
256 assert(tp->visit < type_visited);
257 tp->visit = type_visited;
260 int is_type (void *thing) {
262 if (get_kind(thing) == k_type)
269 bool equal_type(type *typ1, type *typ2) {
274 if (typ1 == typ2) return true;
276 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
277 (get_type_name(typ1) != get_type_name(typ2)) ||
278 (get_type_mode(typ1) != get_type_mode(typ2)) ||
279 (get_type_state(typ1) != get_type_state(typ2)))
281 if ((get_type_state(typ1) == layout_fixed) &&
282 (get_type_size(typ1) != get_type_size(typ2)))
285 switch(get_type_tpop_code(typ1)) {
287 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
288 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
289 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
290 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
291 /** Compare the members **/
292 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
293 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
294 /* First sort the members of typ2 */
295 for (i = 0; i < get_class_n_members(typ1); i++) {
296 entity *e1 = get_class_member(typ1, i);
297 for (j = 0; j < get_class_n_members(typ2); j++) {
298 entity *e2 = get_class_member(typ2, j);
299 if (get_entity_name(e1) == get_entity_name(e2))
303 for (i = 0; i < get_class_n_members(typ1); i++) {
304 if (!m[i] || /* Found no counterpart */
305 !equal_entity(get_class_member(typ1, i), m[i]))
308 /** Compare the supertypes **/
309 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
310 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
311 /* First sort the supertypes of typ2 */
312 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
313 type *t1 = get_class_supertype(typ1, i);
314 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
315 type *t2 = get_class_supertype(typ2, j);
316 if (get_type_name(t2) == get_type_name(t1))
320 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
321 if (!t[i] || /* Found no counterpart */
322 get_class_supertype(typ1, i) != t[i])
327 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
328 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
329 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
330 /* First sort the members of lt */
331 for (i = 0; i < get_struct_n_members(typ1); i++) {
332 entity *e1 = get_struct_member(typ1, i);
333 for (j = 0; j < get_struct_n_members(typ2); j++) {
334 entity *e2 = get_struct_member(typ2, j);
335 if (get_entity_name(e1) == get_entity_name(e2))
339 for (i = 0; i < get_struct_n_members(typ1); i++) {
340 if (!m[i] || /* Found no counterpart */
341 !equal_entity(get_struct_member(typ1, i), m[i]))
346 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
347 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
348 for (i = 0; i < get_method_n_params(typ1); i++) {
349 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
352 for (i = 0; i < get_method_n_ress(typ1); i++) {
353 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
358 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
359 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
360 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
361 /* First sort the members of lt */
362 for (i = 0; i < get_union_n_members(typ1); i++) {
363 entity *e1 = get_union_member(typ1, i);
364 for (j = 0; j < get_union_n_members(typ2); j++) {
365 entity *e2 = get_union_member(typ2, j);
366 if (get_entity_name(e1) == get_entity_name(e2))
370 for (i = 0; i < get_union_n_members(typ1); i++) {
371 if (!m[i] || /* Found no counterpart */
372 !equal_entity(get_union_member(typ1, i), m[i]))
377 type *set, *let; /* small/large elt. type */
378 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
380 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
382 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
383 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
384 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
386 if (get_array_order(typ1, i) != get_array_order(typ2, i))
387 assert(0 && "type compare with different dimension orders not implemented");
390 case tpo_enumeration: {
391 assert(0 && "enumerations not implemented");
394 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
397 case tpo_primitive: {
404 bool smaller_type (type *st, type *lt) {
408 if (st == lt) return true;
410 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
413 switch(get_type_tpop_code(st)) {
415 return is_subclass_of(st, lt);
418 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
419 m = alloca(sizeof(entity *) * get_struct_n_members(st));
420 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
421 /* First sort the members of lt */
422 for (i = 0; i < get_struct_n_members(st); i++) {
423 entity *se = get_struct_member(st, i);
424 for (j = 0; j < get_struct_n_members(lt); j++) {
425 entity *le = get_struct_member(lt, j);
426 if (get_entity_name(le) == get_entity_name(se))
430 for (i = 0; i < get_struct_n_members(st); i++) {
431 if (!m[i] || /* Found no counterpart */
432 !smaller_type(get_entity_type(get_struct_member(st, i)),
433 get_entity_type(m[i])))
438 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
439 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
440 for (i = 0; i < get_method_n_params(st); i++) {
441 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
444 for (i = 0; i < get_method_n_ress(st); i++) {
445 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
450 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
451 m = alloca(sizeof(entity *) * get_union_n_members(st));
452 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
453 /* First sort the members of lt */
454 for (i = 0; i < get_union_n_members(st); i++) {
455 entity *se = get_union_member(st, i);
456 for (j = 0; j < get_union_n_members(lt); j++) {
457 entity *le = get_union_member(lt, j);
458 if (get_entity_name(le) == get_entity_name(se))
462 for (i = 0; i < get_union_n_members(st); i++) {
463 if (!m[i] || /* Found no counterpart */
464 !smaller_type(get_entity_type(get_union_member(st, i)),
465 get_entity_type(m[i])))
470 type *set, *let; /* small/large elt. type */
471 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
473 set = get_array_element_type(st);
474 let = get_array_element_type(lt);
476 /* If the elt types are different, set must be convertible
477 to let, and they must have the same size so that address
478 computations work out. To have a size the layout must
480 if ((get_type_state(set) != layout_fixed) ||
481 (get_type_state(let) != layout_fixed))
483 if (!smaller_type(set, let) ||
484 get_type_size(set) != get_type_size(let))
487 for(i = 0; i < get_array_n_dimensions(st); i++) {
488 if (get_array_lower_bound(lt, i))
489 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
491 if (get_array_upper_bound(lt, i))
492 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
496 case tpo_enumeration: {
497 assert(0 && "enumerations not implemented");
500 if (!smaller_type(get_pointer_points_to_type(st),
501 get_pointer_points_to_type(lt)))
504 case tpo_primitive: {
505 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
513 /*******************************************************************/
515 /*******************************************************************/
517 /* create a new class type */
518 INLINE type *new_type_class (ident *name) {
521 res = new_type(type_class, NULL, name);
523 res->attr.ca.members = NEW_ARR_F (entity *, 1);
524 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
525 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
526 res->attr.ca.peculiarity = existent;
527 res->attr.ca.dfn = 0;
531 type *new_d_type_class (ident *name, dbg_info* db) {
532 type *res = new_type_class (name);
533 set_type_dbg_info(res, db);
536 INLINE void free_class_attrs(type *clss) {
537 assert(clss && (clss->type_op == type_class));
538 DEL_ARR_F(clss->attr.ca.members);
539 DEL_ARR_F(clss->attr.ca.subtypes);
540 DEL_ARR_F(clss->attr.ca.supertypes);
543 /* manipulate private fields of class type */
544 void add_class_member (type *clss, entity *member) {
545 assert(clss && (clss->type_op == type_class));
546 ARR_APP1 (entity *, clss->attr.ca.members, member);
548 int get_class_n_members (type *clss) {
549 assert(clss && (clss->type_op == type_class));
550 return (ARR_LEN (clss->attr.ca.members))-1;
552 entity *get_class_member (type *clss, int pos) {
553 assert(clss && (clss->type_op == type_class));
554 assert(pos >= 0 && pos < get_class_n_members(clss));
555 return clss->attr.ca.members[pos+1];
557 void set_class_member (type *clss, entity *member, int pos) {
558 assert(clss && (clss->type_op == type_class));
559 assert(pos >= 0 && pos < get_class_n_members(clss));
560 clss->attr.ca.members[pos+1] = member;
562 void set_class_members (type *clss, entity **members, int arity) {
564 assert(clss && (clss->type_op == type_class));
565 DEL_ARR_F(clss->attr.ca.members);
566 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
567 for (i = 0; i < arity; i++) {
568 set_entity_owner(members[i], clss);
569 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
572 void remove_class_member(type *clss, entity *member) {
574 assert(clss && (clss->type_op == type_class));
575 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
576 if (clss->attr.ca.members[i] == member) {
577 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
578 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
579 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
585 void add_class_subtype (type *clss, type *subtype) {
587 assert(clss && (clss->type_op == type_class));
588 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
589 for (i = 0; i < get_class_n_supertypes(subtype); i++)
590 if (get_class_supertype(subtype, i) == clss)
591 /* Class already registered */
593 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
595 int get_class_n_subtypes (type *clss) {
596 assert(clss && (clss->type_op == type_class));
597 return (ARR_LEN (clss->attr.ca.subtypes))-1;
599 type *get_class_subtype (type *clss, int pos) {
600 assert(clss && (clss->type_op == type_class));
601 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
602 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
604 void set_class_subtype (type *clss, type *subtype, int pos) {
605 assert(clss && (clss->type_op == type_class));
606 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
607 clss->attr.ca.subtypes[pos+1] = subtype;
609 void remove_class_subtype(type *clss, type *subtype) {
611 assert(clss && (clss->type_op == type_class));
612 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
613 if (clss->attr.ca.subtypes[i] == subtype) {
614 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
615 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
616 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
621 void add_class_supertype (type *clss, type *supertype) {
623 assert(clss && (clss->type_op == type_class));
624 assert(supertype && (supertype -> type_op == type_class));
625 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
626 for (i = 0; i < get_class_n_subtypes(supertype); i++)
627 if (get_class_subtype(supertype, i) == clss)
628 /* Class already registered */
630 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
632 int get_class_n_supertypes (type *clss) {
633 assert(clss && (clss->type_op == type_class));
634 return (ARR_LEN (clss->attr.ca.supertypes))-1;
636 type *get_class_supertype (type *clss, int pos) {
637 assert(clss && (clss->type_op == type_class));
638 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
639 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
641 void set_class_supertype (type *clss, type *supertype, int pos) {
642 assert(clss && (clss->type_op == type_class));
643 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
644 clss->attr.ca.supertypes[pos+1] = supertype;
646 void remove_class_supertype(type *clss, type *supertype) {
648 assert(clss && (clss->type_op == type_class));
649 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
650 if (clss->attr.ca.supertypes[i] == supertype) {
651 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
652 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
653 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
658 INLINE peculiarity get_class_peculiarity (type *clss) {
659 assert(clss && (clss->type_op == type_class));
660 return clss->attr.ca.peculiarity;
662 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
663 assert(clss && (clss->type_op == type_class));
664 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
665 clss->attr.ca.peculiarity = pec;
668 void set_class_dfn (type *clss, int dfn)
670 clss->attr.ca.dfn = dfn;
673 int get_class_dfn (type *clss)
675 return (clss->attr.ca.dfn);
679 bool is_class_type(type *clss) {
681 if (clss->type_op == type_class) return 1; else return 0;
684 bool is_subclass_of(type *low, type *high) {
686 assert(is_class_type(low) && is_class_type(high));
687 if (low == high) return true;
688 /* depth first search from high downwards. */
689 for (i = 0; i < get_class_n_subtypes(high); i++) {
690 if (low == get_class_subtype(high, i))
692 if (is_subclass_of(low, get_class_subtype(high, i)))
698 /*******************************************************************/
700 /*******************************************************************/
702 /* create a new type struct */
703 INLINE type *new_type_struct (ident *name) {
705 res = new_type(type_struct, NULL, name);
706 res->attr.sa.members = NEW_ARR_F (entity *, 1);
709 type *new_d_type_struct (ident *name, dbg_info* db) {
710 type *res = new_type_struct (name);
711 set_type_dbg_info(res, db);
714 INLINE void free_struct_attrs (type *strct) {
715 assert(strct && (strct->type_op == type_struct));
716 DEL_ARR_F(strct->attr.sa.members);
719 /* manipulate private fields of struct */
720 int get_struct_n_members (type *strct) {
721 assert(strct && (strct->type_op == type_struct));
722 return (ARR_LEN (strct->attr.sa.members))-1;
724 void add_struct_member (type *strct, entity *member) {
725 assert(strct && (strct->type_op == type_struct));
726 assert(get_type_tpop(get_entity_type(member)) != type_method);
727 /* @@@ lowerfirm geht nicht durch */
728 ARR_APP1 (entity *, strct->attr.sa.members, member);
730 entity *get_struct_member (type *strct, int pos) {
731 assert(strct && (strct->type_op == type_struct));
732 assert(pos >= 0 && pos < get_struct_n_members(strct));
733 return strct->attr.sa.members[pos+1];
735 void set_struct_member (type *strct, int pos, entity *member) {
736 assert(strct && (strct->type_op == type_struct));
737 assert(pos >= 0 && pos < get_struct_n_members(strct));
738 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
739 strct->attr.sa.members[pos+1] = member;
741 void remove_struct_member(type *strct, entity *member) {
743 assert(strct && (strct->type_op == type_struct));
744 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
745 if (strct->attr.sa.members[i] == member) {
746 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
747 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
748 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
754 bool is_struct_type(type *strct) {
756 if (strct->type_op == type_struct) return 1; else return 0;
759 /*******************************************************************/
761 /*******************************************************************/
763 /* Create a new method type.
764 N_param is the number of parameters, n_res the number of results. */
765 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
767 res = new_type(type_method, mode_p, name);
768 res->state = layout_fixed;
769 res->size = get_mode_size(mode_p);
770 res->attr.ma.n_params = n_param;
771 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
772 res->attr.ma.n_res = n_res;
773 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
776 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
777 type *res = new_type_method (name, n_param, n_res);
778 set_type_dbg_info(res, db);
781 INLINE void free_method_attrs(type *method) {
782 assert(method && (method->type_op == type_method));
783 free(method->attr.ma.param_type);
784 free(method->attr.ma.res_type);
786 /* manipulate private fields of method. */
787 int get_method_n_params (type *method) {
788 assert(method && (method->type_op == type_method));
789 return method->attr.ma.n_params;
791 type *get_method_param_type(type *method, int pos) {
792 assert(method && (method->type_op == type_method));
793 assert(pos >= 0 && pos < get_method_n_params(method));
794 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
796 void set_method_param_type(type *method, int pos, type* type) {
797 assert(method && (method->type_op == type_method));
798 assert(pos >= 0 && pos < get_method_n_params(method));
799 method->attr.ma.param_type[pos] = type;
802 int get_method_n_ress (type *method) {
803 assert(method && (method->type_op == type_method));
804 return method->attr.ma.n_res;
806 type *get_method_res_type(type *method, int pos) {
807 assert(method && (method->type_op == type_method));
808 assert(pos >= 0 && pos < get_method_n_ress(method));
809 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
811 void set_method_res_type(type *method, int pos, type* type) {
812 assert(method && (method->type_op == type_method));
813 assert(pos >= 0 && pos < get_method_n_ress(method));
814 method->attr.ma.res_type[pos] = type;
818 bool is_method_type (type *method) {
820 if (method->type_op == type_method) return 1; else return 0;
824 /*******************************************************************/
826 /*******************************************************************/
828 /* create a new type uni */
829 INLINE type *new_type_union (ident *name) {
831 res = new_type(type_union, NULL, name);
832 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
833 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
834 res->attr.ua.members = NEW_ARR_F (entity *, 1);
837 type *new_d_type_union (ident *name, dbg_info* db) {
838 type *res = new_type_uni (name);
839 set_type_dbg_info(res, db);
842 INLINE void free_union_attrs (type *uni) {
843 assert(uni && (uni->type_op == type_union));
844 DEL_ARR_F(uni->attr.ua.members);
846 /* manipulate private fields of union */
848 int get_union_n_types (type *uni) {
849 assert(uni && (uni->type_op == type_union));
850 return uni->attr.ua.n_types;
852 type *get_union_unioned_type (type *uni, int pos) {
853 assert(uni && (uni->type_op == type_union));
854 assert(pos >= 0 && pos < get_union_n_types(uni));
855 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
857 void set_union_unioned_type (type *uni, int pos, type *type) {
858 assert(uni && (uni->type_op == type_union));
859 assert(pos >= 0 && pos < get_union_n_types(uni));
860 uni->attr.ua.unioned_type[pos] = type;
862 ident *get_union_delim_nameid (type *uni, int pos) {
863 assert(uni && (uni->type_op == type_union));
864 assert(pos >= 0 && pos < get_union_n_types(uni));
865 return uni->attr.ua.delim_names[pos];
867 const char *get_union_delim_name (type *uni, int pos) {
868 assert(uni && (uni->type_op == type_union));
869 assert(pos >= 0 && pos < get_union_n_types(uni));
870 return id_to_str(uni->attr.ua.delim_names[pos]);
872 void set_union_delim_nameid (type *uni, int pos, ident *id) {
873 assert(uni && (uni->type_op == type_union));
874 assert(pos >= 0 && pos < get_union_n_types(uni));
875 uni->attr.ua.delim_names[pos] = id;
878 int get_union_n_members (type *uni) {
879 assert(uni && (uni->type_op == type_union));
880 return (ARR_LEN (uni->attr.ua.members))-1;
882 void add_union_member (type *uni, entity *member) {
883 assert(uni && (uni->type_op == type_union));
884 ARR_APP1 (entity *, uni->attr.ua.members, member);
886 entity *get_union_member (type *uni, int pos) {
887 assert(uni && (uni->type_op == type_union));
888 assert(pos >= 0 && pos < get_union_n_members(uni));
889 return uni->attr.ua.members[pos+1];
891 void set_union_member (type *uni, int pos, entity *member) {
892 assert(uni && (uni->type_op == type_union));
893 assert(pos >= 0 && pos < get_union_n_members(uni));
894 uni->attr.ua.members[pos+1] = member;
896 void remove_union_member(type *uni, entity *member) {
898 assert(uni && (uni->type_op == type_union));
899 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
900 if (uni->attr.ua.members[i] == member) {
901 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
902 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
903 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
909 bool is_union_type (type *uni) {
911 if (uni->type_op == type_union) return 1; else return 0;
914 /*******************************************************************/
916 /*******************************************************************/
919 /* create a new type array -- set dimension sizes independently */
920 INLINE type *new_type_array (ident *name, int n_dimensions,
921 type *element_type) {
924 assert(!is_method_type(element_type));
925 res = new_type(type_array, NULL, name);
926 res->attr.aa.n_dimensions = n_dimensions;
927 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
928 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
929 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
931 for (i = 0; i < n_dimensions; i++) {
932 res->attr.aa.lower_bound[i] = NULL;
933 res->attr.aa.upper_bound[i] = NULL;
934 res->attr.aa.order[i] = i;
936 res->attr.aa.element_type = element_type;
937 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
940 type *new_d_type_array (ident *name, int n_dimensions,
941 type *element_type, dbg_info* db) {
942 type *res = new_type_array (name, n_dimensions, element_type);
943 set_type_dbg_info(res, db);
947 INLINE void free_array_attrs (type *array) {
948 assert(array && (array->type_op == type_array));
949 free(array->attr.aa.lower_bound);
950 free(array->attr.aa.upper_bound);
953 /* manipulate private fields of array type */
954 int get_array_n_dimensions (type *array) {
955 assert(array && (array->type_op == type_array));
956 return array->attr.aa.n_dimensions;
960 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
961 ir_node * upper_bound) {
962 assert(array && (array->type_op == type_array));
963 array->attr.aa.lower_bound[dimension] = lower_bound;
964 array->attr.aa.upper_bound[dimension] = upper_bound;
967 set_array_bounds_int (type *array, int dimension, int lower_bound,
969 ir_graph *rem = current_ir_graph;
970 current_ir_graph = get_const_code_irg();
971 set_array_bounds (array, dimension,
972 new_Const(mode_I, tarval_from_long (mode_I, lower_bound)),
973 new_Const(mode_I, tarval_from_long (mode_I, upper_bound)));
974 current_ir_graph = rem;
977 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
978 assert(array && (array->type_op == type_array));
979 array->attr.aa.lower_bound[dimension] = lower_bound;
981 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
982 ir_graph *rem = current_ir_graph;
983 current_ir_graph = get_const_code_irg();
984 set_array_lower_bound (array, dimension,
985 new_Const(mode_I, tarval_from_long (mode_I, lower_bound)));
986 current_ir_graph = rem;
989 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
990 assert(array && (array->type_op == type_array));
991 array->attr.aa.upper_bound[dimension] = upper_bound;
993 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
994 ir_graph *rem = current_ir_graph;
995 current_ir_graph = get_const_code_irg();
996 set_array_upper_bound (array, dimension,
997 new_Const(mode_I, tarval_from_long (mode_I, upper_bound)));
998 current_ir_graph = rem;
1000 ir_node * get_array_lower_bound (type *array, int dimension) {
1001 assert(array && (array->type_op == type_array));
1002 return array->attr.aa.lower_bound[dimension];
1004 ir_node * get_array_upper_bound (type *array, int dimension) {
1005 assert(array && (array->type_op == type_array));
1006 return array->attr.aa.upper_bound[dimension];
1009 void set_array_order (type *array, int dimension, int order) {
1010 assert(array && (array->type_op == type_array));
1011 array->attr.aa.order[dimension] = order;
1013 int get_array_order (type *array, int dimension) {
1014 assert(array && (array->type_op == type_array));
1015 return array->attr.aa.order[dimension];
1018 void set_array_element_type (type *array, type *type) {
1019 assert(array && (array->type_op == type_array));
1020 assert(!is_method_type(type));
1021 array->attr.aa.element_type = type;
1023 type *get_array_element_type (type *array) {
1024 assert(array && (array->type_op == type_array));
1025 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1028 void set_array_element_entity (type *array, entity *ent) {
1029 assert(array && (array->type_op == type_array));
1030 assert((get_entity_type(ent)->type_op != type_method));
1031 array->attr.aa.element_ent = ent;
1032 array->attr.aa.element_type = get_entity_type(ent);
1034 entity *get_array_element_entity (type *array) {
1035 assert(array && (array->type_op == type_array));
1036 return array->attr.aa.element_ent;
1040 bool is_array_type (type *array) {
1042 if (array->type_op == type_array) return 1; else return 0;
1045 /*******************************************************************/
1046 /** TYPE_ENUMERATION **/
1047 /*******************************************************************/
1049 /* create a new type enumeration -- set the enumerators independently */
1050 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1053 res = new_type(type_enumeration, NULL, name);
1054 res->attr.ea.n_enums = n_enums;
1055 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1056 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1057 for (i = 0; i < n_enums; i++) {
1058 res->attr.ea.enumer[i] = NULL;
1059 res->attr.ea.enum_nameid = NULL;
1063 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1064 type *res = new_type_enumeration (name, n_enums);
1065 set_type_dbg_info(res, db);
1069 INLINE void free_enumeration_attrs(type *enumeration) {
1070 assert(enumeration && (enumeration->type_op == type_enumeration));
1071 free(enumeration->attr.ea.enumer);
1072 free(enumeration->attr.ea.enum_nameid);
1075 /* manipulate fields of enumeration type. */
1076 int get_enumeration_n_enums (type *enumeration) {
1077 assert(enumeration && (enumeration->type_op == type_enumeration));
1078 return enumeration->attr.ea.n_enums;
1080 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1081 assert(enumeration && (enumeration->type_op == type_enumeration));
1082 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1083 enumeration->attr.ea.enumer[pos] = con;
1085 tarval *get_enumeration_enum (type *enumeration, int pos) {
1086 assert(enumeration && (enumeration->type_op == type_enumeration));
1087 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1088 return enumeration->attr.ea.enumer[pos];
1090 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1091 assert(enumeration && (enumeration->type_op == type_enumeration));
1092 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1093 enumeration->attr.ea.enum_nameid[pos] = id;
1095 ident *get_enumeration_nameid (type *enumeration, int pos) {
1096 assert(enumeration && (enumeration->type_op == type_enumeration));
1097 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1098 return enumeration->attr.ea.enum_nameid[pos];
1100 const char *get_enumeration_name(type *enumeration, int pos) {
1101 assert(enumeration && (enumeration->type_op == type_enumeration));
1102 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1103 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1107 bool is_enumeration_type (type *enumeration) {
1108 assert(enumeration);
1109 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1112 /*******************************************************************/
1113 /** TYPE_POINTER **/
1114 /*******************************************************************/
1116 /* Create a new type pointer */
1117 INLINE type *new_type_pointer (ident *name, type *points_to) {
1119 res = new_type(type_pointer, mode_p, name);
1120 res->attr.pa.points_to = points_to;
1121 res->size = get_mode_size(res->mode);
1122 res->state = layout_fixed;
1125 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1126 type *res = new_type_pointer (name, points_to);
1127 set_type_dbg_info(res, db);
1130 INLINE void free_pointer_attrs (type *pointer) {
1131 assert(pointer && (pointer->type_op == type_pointer));
1133 /* manipulate fields of type_pointer */
1134 void set_pointer_points_to_type (type *pointer, type *type) {
1135 assert(pointer && (pointer->type_op == type_pointer));
1136 pointer->attr.pa.points_to = type;
1138 type *get_pointer_points_to_type (type *pointer) {
1139 assert(pointer && (pointer->type_op == type_pointer));
1140 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1144 bool is_pointer_type (type *pointer) {
1146 if (pointer->type_op == type_pointer) return 1; else return 0;
1150 /*******************************************************************/
1151 /** TYPE_PRIMITIVE **/
1152 /*******************************************************************/
1154 /* create a new type primitive */
1155 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1157 /* @@@ assert( mode_is_data(mode) && (!mode == mode_p)); */
1158 res = new_type(type_primitive, mode, name);
1159 res->size = get_mode_size(mode);
1160 res->state = layout_fixed;
1163 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1164 type *res = new_type_primitive (name, mode);
1165 set_type_dbg_info(res, db);
1168 INLINE void free_primitive_attrs (type *primitive) {
1169 assert(primitive && (primitive->type_op == type_primitive));
1173 bool is_primitive_type (type *primitive) {
1174 assert(primitive && primitive->kind == k_type);
1175 if (primitive->type_op == type_primitive) return 1; else return 0;
1178 /*******************************************************************/
1179 /** common functionality **/
1180 /*******************************************************************/
1183 INLINE int is_atomic_type(type *tp) {
1184 assert(tp && tp->kind == k_type);
1185 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1186 is_enumeration_type(tp));
1188 INLINE int is_compound_type(type *tp) {
1189 assert(tp && tp->kind == k_type);
1190 return (is_class_type(tp) || is_struct_type(tp) ||
1191 is_array_type(tp) || is_union_type(tp));