3 * file type.c - implementation of the datastructure to hold
5 * (C) 2001 by Universitaet Karlsruhe
6 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
8 * This module supplies a datastructure to represent all types
9 * known in the compiled program. This includes types specified
10 * in the program as well as types defined by the language. In the
11 * view of the intermediate representation there is no difference
12 * between these types.
14 * There exist several kinds of types, arranged by the structure of
15 * the type. A type is described by a set of attributes. Some of
16 * these attributes are common to all types, others depend on the
19 * Types are different from the modes defined in irmode: Types are
20 * on the level of the programming language, modes at the level of
21 * the target processor.
23 * @see type_t.h type tpop
33 # include "typegmod_t.h"
40 /*******************************************************************/
42 /*******************************************************************/
44 unsigned long type_visited;
45 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
46 INLINE unsigned long get_master_type_visited() { return type_visited; }
47 INLINE void inc_master_type_visited() { type_visited++; }
49 void free_type(type *tp) {
50 /* Remove from list of all types */
52 /* Free the attributes of the type. */
54 /* Free entities automatically allocated with the type */
55 if (is_array_type(tp))
56 free_entity(get_array_element_entity(tp));
57 /* And now the type itself... */
62 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
66 assert(type_op != type_id);
68 node_size = offsetof (type, attr) + type_op->attr_size;
69 res = (type *) xmalloc (node_size);
70 add_irp_type(res); /* Remember the new type global. */
73 res->type_op = type_op;
76 res->state = layout_undefined;
84 void free_type_attrs(type *tp) {
85 switch(get_type_tpop_code(tp)) {
86 case tpo_class: { free_class_attrs(tp); } break;
87 case tpo_struct: { free_struct_attrs(tp); } break;
88 case tpo_method: { free_method_attrs(tp); } break;
89 case tpo_union: { free_union_attrs(tp); } break;
90 case tpo_array: { free_array_attrs(tp); } break;
91 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
92 case tpo_pointer: { free_pointer_attrs(tp); } break;
93 case tpo_primitive: { free_primitive_attrs(tp); } break;
98 /* set/get the link field */
99 void *get_type_link(type *tp)
101 assert(tp && tp->kind == k_type);
105 void set_type_link(type *tp, void *l)
107 assert(tp && tp->kind == k_type);
111 tp_op* get_type_tpop(type *tp) {
112 assert(tp && tp->kind == k_type);
116 ident* get_type_tpop_nameid(type *tp) {
117 assert(tp && tp->kind == k_type);
118 return tp->type_op->name;
121 const char* get_type_tpop_name(type *tp) {
122 assert(tp && tp->kind == k_type);
123 return id_to_str(tp->type_op->name);
126 tp_opcode get_type_tpop_code(type *tp) {
127 assert(tp && tp->kind == k_type);
128 return tp->type_op->code;
131 ir_mode* get_type_mode(type *tp) {
132 assert(tp && tp->kind == k_type);
136 void set_type_mode(type *tp, ir_mode* m) {
137 assert(tp && tp->kind == k_type);
139 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
140 /* Modes of primitives must be data */
141 ((tp->type_op != type_enumeration) || mode_is_int(m)));
142 /* Modes of enumerations must be integers */
144 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
145 /* For pointer, primitive and enumeration size depends on the mode. */
146 tp->size = get_mode_size(m);
151 ident* get_type_ident(type *tp) {
152 assert(tp && tp->kind == k_type);
156 void set_type_ident(type *tp, ident* id) {
157 assert(tp && tp->kind == k_type);
161 const char* get_type_name(type *tp) {
162 assert(tp && tp->kind == k_type);
163 return (id_to_str(tp->name));
166 int get_type_size(type *tp) {
167 assert(tp && tp->kind == k_type);
172 set_type_size(type *tp, int size) {
173 assert(tp && tp->kind == k_type);
174 /* For pointer enumeration and primitive size depends on the mode.
175 Methods don't have a size. */
176 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
177 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
182 get_type_state(type *tp) {
183 assert(tp && tp->kind == k_type);
188 set_type_state(type *tp, type_state state) {
189 assert(tp && tp->kind == k_type);
191 if ((tp->type_op == type_pointer) && (tp->type_op == type_primitive) &&
192 (tp->type_op == type_method))
195 /* Just a correctness check: */
196 if (state == layout_fixed) {
198 switch (get_type_tpop_code(tp)) {
201 assert(get_type_size(tp) > -1);
202 if (tp != get_glob_type())
203 for (i = 0; i < get_class_n_members(tp); i++) {
204 assert(get_entity_offset(get_class_member(tp, i)) > -1);
205 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
206 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
207 /* @@@ lowerfirm geht nicht durch */
212 /* assert(get_type_size(tp) > -1); @@@ lowerfirm geht nicht durch */
213 for (i = 0; i < get_struct_n_members(tp); i++) {
214 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
215 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
224 Assure that only innermost dimension is dynamic? */
226 case tpo_enumeration:
228 assert(get_type_mode != NULL);
229 for (i = 0; i < get_enumeration_n_enums(tp); i++)
230 assert(get_enumeration_enum(tp, i) != NULL);
238 unsigned long get_type_visited(type *tp) {
239 assert(tp && tp->kind == k_type);
243 void set_type_visited(type *tp, unsigned long num) {
244 assert(tp && tp->kind == k_type);
247 /* Sets visited field in type to type_visited. */
248 void mark_type_visited(type *tp) {
249 assert(tp && tp->kind == k_type);
250 assert(tp->visit < type_visited);
251 tp->visit = type_visited;
254 int is_type (void *thing) {
256 if (get_kind(thing) == k_type)
263 bool equal_type(type *typ1, type *typ2) {
268 if (typ1 == typ2) return true;
270 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
271 (get_type_name(typ1) != get_type_name(typ2)) ||
272 (get_type_mode(typ1) != get_type_mode(typ2)) ||
273 (get_type_state(typ1) != get_type_state(typ2)))
275 if ((get_type_state(typ1) == layout_fixed) &&
276 (get_type_size(typ1) != get_type_size(typ2)))
279 switch(get_type_tpop_code(typ1)) {
281 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
282 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
283 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
284 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
285 /** Compare the members **/
286 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
287 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
288 /* First sort the members of typ2 */
289 for (i = 0; i < get_class_n_members(typ1); i++) {
290 entity *e1 = get_class_member(typ1, i);
291 for (j = 0; j < get_class_n_members(typ2); j++) {
292 entity *e2 = get_class_member(typ2, j);
293 if (get_entity_name(e1) == get_entity_name(e2))
297 for (i = 0; i < get_class_n_members(typ1); i++) {
298 if (!m[i] || /* Found no counterpart */
299 !equal_entity(get_class_member(typ1, i), m[i]))
302 /** Compare the supertypes **/
303 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
304 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
305 /* First sort the supertypes of typ2 */
306 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
307 type *t1 = get_class_supertype(typ1, i);
308 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
309 type *t2 = get_class_supertype(typ2, j);
310 if (get_type_name(t2) == get_type_name(t1))
314 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
315 if (!t[i] || /* Found no counterpart */
316 get_class_supertype(typ1, i) != t[i])
321 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
322 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
323 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
324 /* First sort the members of lt */
325 for (i = 0; i < get_struct_n_members(typ1); i++) {
326 entity *e1 = get_struct_member(typ1, i);
327 for (j = 0; j < get_struct_n_members(typ2); j++) {
328 entity *e2 = get_struct_member(typ2, j);
329 if (get_entity_name(e1) == get_entity_name(e2))
333 for (i = 0; i < get_struct_n_members(typ1); i++) {
334 if (!m[i] || /* Found no counterpart */
335 !equal_entity(get_struct_member(typ1, i), m[i]))
340 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
341 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
342 for (i = 0; i < get_method_n_params(typ1); i++) {
343 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
346 for (i = 0; i < get_method_n_ress(typ1); i++) {
347 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
352 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
353 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
354 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
355 /* First sort the members of lt */
356 for (i = 0; i < get_union_n_members(typ1); i++) {
357 entity *e1 = get_union_member(typ1, i);
358 for (j = 0; j < get_union_n_members(typ2); j++) {
359 entity *e2 = get_union_member(typ2, j);
360 if (get_entity_name(e1) == get_entity_name(e2))
364 for (i = 0; i < get_union_n_members(typ1); i++) {
365 if (!m[i] || /* Found no counterpart */
366 !equal_entity(get_union_member(typ1, i), m[i]))
371 type *set, *let; /* small/large elt. type */
372 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
374 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
376 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
377 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
378 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
380 if (get_array_order(typ1, i) != get_array_order(typ2, i))
381 assert(0 && "type compare with different dimension orders not implemented");
384 case tpo_enumeration: {
385 assert(0 && "enumerations not implemented");
388 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
391 case tpo_primitive: {
398 bool smaller_type (type *st, type *lt) {
402 if (st == lt) return true;
404 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
407 switch(get_type_tpop_code(st)) {
409 return is_subclass_of(st, lt);
412 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
413 m = alloca(sizeof(entity *) * get_struct_n_members(st));
414 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
415 /* First sort the members of lt */
416 for (i = 0; i < get_struct_n_members(st); i++) {
417 entity *se = get_struct_member(st, i);
418 for (j = 0; j < get_struct_n_members(lt); j++) {
419 entity *le = get_struct_member(lt, j);
420 if (get_entity_name(le) == get_entity_name(se))
424 for (i = 0; i < get_struct_n_members(st); i++) {
425 if (!m[i] || /* Found no counterpart */
426 !smaller_type(get_entity_type(get_struct_member(st, i)),
427 get_entity_type(m[i])))
432 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
433 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
434 for (i = 0; i < get_method_n_params(st); i++) {
435 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
438 for (i = 0; i < get_method_n_ress(st); i++) {
439 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
444 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
445 m = alloca(sizeof(entity *) * get_union_n_members(st));
446 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
447 /* First sort the members of lt */
448 for (i = 0; i < get_union_n_members(st); i++) {
449 entity *se = get_union_member(st, i);
450 for (j = 0; j < get_union_n_members(lt); j++) {
451 entity *le = get_union_member(lt, j);
452 if (get_entity_name(le) == get_entity_name(se))
456 for (i = 0; i < get_union_n_members(st); i++) {
457 if (!m[i] || /* Found no counterpart */
458 !smaller_type(get_entity_type(get_union_member(st, i)),
459 get_entity_type(m[i])))
464 type *set, *let; /* small/large elt. type */
465 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
467 set = get_array_element_type(st);
468 let = get_array_element_type(lt);
470 /* If the elt types are different, set must be convertible
471 to let, and they must have the same size so that address
472 computations work out. To have a size the layout must
474 if ((get_type_state(set) != layout_fixed) ||
475 (get_type_state(let) != layout_fixed))
477 if (!smaller_type(set, let) ||
478 get_type_size(set) != get_type_size(let))
481 for(i = 0; i < get_array_n_dimensions(st); i++) {
482 if (get_array_lower_bound(lt, i))
483 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
485 if (get_array_upper_bound(lt, i))
486 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
490 case tpo_enumeration: {
491 assert(0 && "enumerations not implemented");
494 if (!smaller_type(get_pointer_points_to_type(st),
495 get_pointer_points_to_type(lt)))
498 case tpo_primitive: {
499 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
507 /*******************************************************************/
509 /*******************************************************************/
511 /* create a new class type */
512 INLINE type *new_type_class (ident *name) {
515 res = new_type(type_class, NULL, name);
517 res->attr.ca.members = NEW_ARR_F (entity *, 1);
518 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
519 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
520 res->attr.ca.peculiarity = existent;
521 res->attr.ca.dfn = 0;
525 type *new_d_type_class (ident *name, dbg_info* db) {
526 type *res = new_type_class (name);
527 set_type_dbg_info(res, db);
530 INLINE void free_class_attrs(type *clss) {
531 assert(clss && (clss->type_op == type_class));
532 DEL_ARR_F(clss->attr.ca.members);
533 DEL_ARR_F(clss->attr.ca.subtypes);
534 DEL_ARR_F(clss->attr.ca.supertypes);
537 /* manipulate private fields of class type */
538 void add_class_member (type *clss, entity *member) {
539 assert(clss && (clss->type_op == type_class));
540 ARR_APP1 (entity *, clss->attr.ca.members, member);
542 int get_class_n_members (type *clss) {
543 assert(clss && (clss->type_op == type_class));
544 return (ARR_LEN (clss->attr.ca.members))-1;
546 entity *get_class_member (type *clss, int pos) {
547 assert(clss && (clss->type_op == type_class));
548 assert(pos >= 0 && pos < get_class_n_members(clss));
549 return clss->attr.ca.members[pos+1];
551 void set_class_member (type *clss, entity *member, int pos) {
552 assert(clss && (clss->type_op == type_class));
553 assert(pos >= 0 && pos < get_class_n_members(clss));
554 clss->attr.ca.members[pos+1] = member;
556 void set_class_members (type *clss, entity **members, int arity) {
558 assert(clss && (clss->type_op == type_class));
559 DEL_ARR_F(clss->attr.ca.members);
560 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
561 for (i = 0; i < arity; i++) {
562 set_entity_owner(members[i], clss);
563 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
566 void remove_class_member(type *clss, entity *member) {
568 assert(clss && (clss->type_op == type_class));
569 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
570 if (clss->attr.ca.members[i] == member) {
571 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
572 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
573 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
579 void add_class_subtype (type *clss, type *subtype) {
581 assert(clss && (clss->type_op == type_class));
582 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
583 for (i = 0; i < get_class_n_supertypes(subtype); i++)
584 if (get_class_supertype(subtype, i) == clss)
585 /* Class already registered */
587 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
589 int get_class_n_subtypes (type *clss) {
590 assert(clss && (clss->type_op == type_class));
591 return (ARR_LEN (clss->attr.ca.subtypes))-1;
593 type *get_class_subtype (type *clss, int pos) {
594 assert(clss && (clss->type_op == type_class));
595 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
596 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
598 void set_class_subtype (type *clss, type *subtype, int pos) {
599 assert(clss && (clss->type_op == type_class));
600 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
601 clss->attr.ca.subtypes[pos+1] = subtype;
603 void remove_class_subtype(type *clss, type *subtype) {
605 assert(clss && (clss->type_op == type_class));
606 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
607 if (clss->attr.ca.subtypes[i] == subtype) {
608 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
609 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
610 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
615 void add_class_supertype (type *clss, type *supertype) {
617 assert(clss && (clss->type_op == type_class));
618 assert(supertype && (supertype -> type_op == type_class));
619 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
620 for (i = 0; i < get_class_n_subtypes(supertype); i++)
621 if (get_class_subtype(supertype, i) == clss)
622 /* Class already registered */
624 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
626 int get_class_n_supertypes (type *clss) {
627 assert(clss && (clss->type_op == type_class));
628 return (ARR_LEN (clss->attr.ca.supertypes))-1;
630 type *get_class_supertype (type *clss, int pos) {
631 assert(clss && (clss->type_op == type_class));
632 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
633 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
635 void set_class_supertype (type *clss, type *supertype, int pos) {
636 assert(clss && (clss->type_op == type_class));
637 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
638 clss->attr.ca.supertypes[pos+1] = supertype;
640 void remove_class_supertype(type *clss, type *supertype) {
642 assert(clss && (clss->type_op == type_class));
643 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
644 if (clss->attr.ca.supertypes[i] == supertype) {
645 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
646 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
647 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
652 INLINE peculiarity get_class_peculiarity (type *clss) {
653 assert(clss && (clss->type_op == type_class));
654 return clss->attr.ca.peculiarity;
656 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
657 assert(clss && (clss->type_op == type_class));
658 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
659 clss->attr.ca.peculiarity = pec;
662 void set_class_dfn (type *clss, int dfn)
664 clss->attr.ca.dfn = dfn;
667 int get_class_dfn (type *clss)
669 return (clss->attr.ca.dfn);
673 bool is_class_type(type *clss) {
675 if (clss->type_op == type_class) return 1; else return 0;
678 bool is_subclass_of(type *low, type *high) {
680 assert(is_class_type(low) && is_class_type(high));
681 if (low == high) return true;
682 /* depth first search from high downwards. */
683 for (i = 0; i < get_class_n_subtypes(high); i++) {
684 if (low == get_class_subtype(high, i))
686 if (is_subclass_of(low, get_class_subtype(high, i)))
692 /*******************************************************************/
694 /*******************************************************************/
696 /* create a new type struct */
697 INLINE type *new_type_struct (ident *name) {
699 res = new_type(type_struct, NULL, name);
700 res->attr.sa.members = NEW_ARR_F (entity *, 1);
703 type *new_d_type_struct (ident *name, dbg_info* db) {
704 type *res = new_type_struct (name);
705 set_type_dbg_info(res, db);
708 INLINE void free_struct_attrs (type *strct) {
709 assert(strct && (strct->type_op == type_struct));
710 DEL_ARR_F(strct->attr.sa.members);
713 /* manipulate private fields of struct */
714 int get_struct_n_members (type *strct) {
715 assert(strct && (strct->type_op == type_struct));
716 return (ARR_LEN (strct->attr.sa.members))-1;
718 void add_struct_member (type *strct, entity *member) {
719 assert(strct && (strct->type_op == type_struct));
720 assert(get_type_tpop(get_entity_type(member)) != type_method);
721 /* @@@ lowerfirm geht nicht durch */
722 ARR_APP1 (entity *, strct->attr.sa.members, member);
724 entity *get_struct_member (type *strct, int pos) {
725 assert(strct && (strct->type_op == type_struct));
726 assert(pos >= 0 && pos < get_struct_n_members(strct));
727 return strct->attr.sa.members[pos+1];
729 void set_struct_member (type *strct, int pos, entity *member) {
730 assert(strct && (strct->type_op == type_struct));
731 assert(pos >= 0 && pos < get_struct_n_members(strct));
732 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
733 strct->attr.sa.members[pos+1] = member;
735 void remove_struct_member(type *strct, entity *member) {
737 assert(strct && (strct->type_op == type_struct));
738 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
739 if (strct->attr.sa.members[i] == member) {
740 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
741 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
742 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
748 bool is_struct_type(type *strct) {
750 if (strct->type_op == type_struct) return 1; else return 0;
753 /*******************************************************************/
755 /*******************************************************************/
757 /* Create a new method type.
758 N_param is the number of parameters, n_res the number of results. */
759 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
761 res = new_type(type_method, mode_P, name);
762 res->state = layout_fixed;
763 res->size = get_mode_size(mode_P);
764 res->attr.ma.n_params = n_param;
765 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
766 res->attr.ma.n_res = n_res;
767 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
770 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
771 type *res = new_type_method (name, n_param, n_res);
772 set_type_dbg_info(res, db);
775 INLINE void free_method_attrs(type *method) {
776 assert(method && (method->type_op == type_method));
777 free(method->attr.ma.param_type);
778 free(method->attr.ma.res_type);
780 /* manipulate private fields of method. */
781 int get_method_n_params (type *method) {
782 assert(method && (method->type_op == type_method));
783 return method->attr.ma.n_params;
785 type *get_method_param_type(type *method, int pos) {
786 assert(method && (method->type_op == type_method));
787 assert(pos >= 0 && pos < get_method_n_params(method));
788 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
790 void set_method_param_type(type *method, int pos, type* tp) {
791 assert(method && (method->type_op == type_method));
792 assert(pos >= 0 && pos < get_method_n_params(method));
793 method->attr.ma.param_type[pos] = tp;
796 int get_method_n_ress (type *method) {
797 assert(method && (method->type_op == type_method));
798 return method->attr.ma.n_res;
800 type *get_method_res_type(type *method, int pos) {
801 assert(method && (method->type_op == type_method));
802 assert(pos >= 0 && pos < get_method_n_ress(method));
803 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
805 void set_method_res_type(type *method, int pos, type* tp) {
806 assert(method && (method->type_op == type_method));
807 assert(pos >= 0 && pos < get_method_n_ress(method));
808 method->attr.ma.res_type[pos] = tp;
812 bool is_method_type (type *method) {
814 if (method->type_op == type_method) return 1; else return 0;
817 /*******************************************************************/
819 /*******************************************************************/
821 /* create a new type uni */
822 INLINE type *new_type_union (ident *name) {
824 res = new_type(type_union, NULL, name);
825 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
826 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
827 res->attr.ua.members = NEW_ARR_F (entity *, 1);
830 type *new_d_type_union (ident *name, dbg_info* db) {
831 type *res = new_type_union (name);
832 set_type_dbg_info(res, db);
835 INLINE void free_union_attrs (type *uni) {
836 assert(uni && (uni->type_op == type_union));
837 DEL_ARR_F(uni->attr.ua.members);
839 /* manipulate private fields of union */
841 int get_union_n_types (type *uni) {
842 assert(uni && (uni->type_op == type_union));
843 return uni->attr.ua.n_types;
845 type *get_union_unioned_type (type *uni, int pos) {
846 assert(uni && (uni->type_op == type_union));
847 assert(pos >= 0 && pos < get_union_n_types(uni));
848 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
850 void set_union_unioned_type (type *uni, int pos, type *tp) {
851 assert(uni && (uni->type_op == type_union));
852 assert(pos >= 0 && pos < get_union_n_types(uni));
853 uni->attr.ua.unioned_type[pos] = tp;
855 ident *get_union_delim_nameid (type *uni, int pos) {
856 assert(uni && (uni->type_op == type_union));
857 assert(pos >= 0 && pos < get_union_n_types(uni));
858 return uni->attr.ua.delim_names[pos];
860 const char *get_union_delim_name (type *uni, int pos) {
861 assert(uni && (uni->type_op == type_union));
862 assert(pos >= 0 && pos < get_union_n_types(uni));
863 return id_to_str(uni->attr.ua.delim_names[pos]);
865 void set_union_delim_nameid (type *uni, int pos, ident *id) {
866 assert(uni && (uni->type_op == type_union));
867 assert(pos >= 0 && pos < get_union_n_types(uni));
868 uni->attr.ua.delim_names[pos] = id;
871 int get_union_n_members (type *uni) {
872 assert(uni && (uni->type_op == type_union));
873 return (ARR_LEN (uni->attr.ua.members))-1;
875 void add_union_member (type *uni, entity *member) {
876 assert(uni && (uni->type_op == type_union));
877 ARR_APP1 (entity *, uni->attr.ua.members, member);
879 entity *get_union_member (type *uni, int pos) {
880 assert(uni && (uni->type_op == type_union));
881 assert(pos >= 0 && pos < get_union_n_members(uni));
882 return uni->attr.ua.members[pos+1];
884 void set_union_member (type *uni, int pos, entity *member) {
885 assert(uni && (uni->type_op == type_union));
886 assert(pos >= 0 && pos < get_union_n_members(uni));
887 uni->attr.ua.members[pos+1] = member;
889 void remove_union_member(type *uni, entity *member) {
891 assert(uni && (uni->type_op == type_union));
892 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
893 if (uni->attr.ua.members[i] == member) {
894 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
895 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
896 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
902 bool is_union_type (type *uni) {
904 if (uni->type_op == type_union) return 1; else return 0;
907 /*******************************************************************/
909 /*******************************************************************/
912 /* create a new type array -- set dimension sizes independently */
913 INLINE type *new_type_array (ident *name, int n_dimensions,
914 type *element_type) {
917 assert(!is_method_type(element_type));
918 res = new_type(type_array, NULL, name);
919 res->attr.aa.n_dimensions = n_dimensions;
920 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
921 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
922 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
924 for (i = 0; i < n_dimensions; i++) {
925 res->attr.aa.lower_bound[i] = NULL;
926 res->attr.aa.upper_bound[i] = NULL;
927 res->attr.aa.order[i] = i;
929 res->attr.aa.element_type = element_type;
930 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
933 type *new_d_type_array (ident *name, int n_dimensions,
934 type *element_type, dbg_info* db) {
935 type *res = new_type_array (name, n_dimensions, element_type);
936 set_type_dbg_info(res, db);
940 INLINE void free_array_attrs (type *array) {
941 assert(array && (array->type_op == type_array));
942 free(array->attr.aa.lower_bound);
943 free(array->attr.aa.upper_bound);
946 /* manipulate private fields of array type */
947 int get_array_n_dimensions (type *array) {
948 assert(array && (array->type_op == type_array));
949 return array->attr.aa.n_dimensions;
953 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
954 ir_node * upper_bound) {
955 assert(array && (array->type_op == type_array));
956 array->attr.aa.lower_bound[dimension] = lower_bound;
957 array->attr.aa.upper_bound[dimension] = upper_bound;
960 set_array_bounds_int (type *array, int dimension, int lower_bound,
962 ir_graph *rem = current_ir_graph;
963 current_ir_graph = get_const_code_irg();
964 set_array_bounds (array, dimension,
965 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)),
966 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
967 current_ir_graph = rem;
970 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
971 assert(array && (array->type_op == type_array));
972 array->attr.aa.lower_bound[dimension] = lower_bound;
974 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
975 ir_graph *rem = current_ir_graph;
976 current_ir_graph = get_const_code_irg();
977 set_array_lower_bound (array, dimension,
978 new_Const(mode_Iu, tarval_from_long (mode_Iu, lower_bound)));
979 current_ir_graph = rem;
982 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
983 assert(array && (array->type_op == type_array));
984 array->attr.aa.upper_bound[dimension] = upper_bound;
986 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
987 ir_graph *rem = current_ir_graph;
988 current_ir_graph = get_const_code_irg();
989 set_array_upper_bound (array, dimension,
990 new_Const(mode_Iu, tarval_from_long (mode_Iu, upper_bound)));
991 current_ir_graph = rem;
993 ir_node * get_array_lower_bound (type *array, int dimension) {
994 assert(array && (array->type_op == type_array));
995 return array->attr.aa.lower_bound[dimension];
997 ir_node * get_array_upper_bound (type *array, int dimension) {
998 assert(array && (array->type_op == type_array));
999 return array->attr.aa.upper_bound[dimension];
1002 void set_array_order (type *array, int dimension, int order) {
1003 assert(array && (array->type_op == type_array));
1004 array->attr.aa.order[dimension] = order;
1006 int get_array_order (type *array, int dimension) {
1007 assert(array && (array->type_op == type_array));
1008 return array->attr.aa.order[dimension];
1011 void set_array_element_type (type *array, type *tp) {
1012 assert(array && (array->type_op == type_array));
1013 assert(!is_method_type(tp));
1014 array->attr.aa.element_type = tp;
1016 type *get_array_element_type (type *array) {
1017 assert(array && (array->type_op == type_array));
1018 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1021 void set_array_element_entity (type *array, entity *ent) {
1022 assert(array && (array->type_op == type_array));
1023 assert((get_entity_type(ent)->type_op != type_method));
1024 array->attr.aa.element_ent = ent;
1025 array->attr.aa.element_type = get_entity_type(ent);
1027 entity *get_array_element_entity (type *array) {
1028 assert(array && (array->type_op == type_array));
1029 return array->attr.aa.element_ent;
1033 bool is_array_type (type *array) {
1035 if (array->type_op == type_array) return 1; else return 0;
1038 /*******************************************************************/
1039 /** TYPE_ENUMERATION **/
1040 /*******************************************************************/
1042 /* create a new type enumeration -- set the enumerators independently */
1043 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1046 res = new_type(type_enumeration, NULL, name);
1047 res->attr.ea.n_enums = n_enums;
1048 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1049 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1050 for (i = 0; i < n_enums; i++) {
1051 res->attr.ea.enumer[i] = NULL;
1052 res->attr.ea.enum_nameid = NULL;
1056 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1057 type *res = new_type_enumeration (name, n_enums);
1058 set_type_dbg_info(res, db);
1062 INLINE void free_enumeration_attrs(type *enumeration) {
1063 assert(enumeration && (enumeration->type_op == type_enumeration));
1064 free(enumeration->attr.ea.enumer);
1065 free(enumeration->attr.ea.enum_nameid);
1068 /* manipulate fields of enumeration type. */
1069 int get_enumeration_n_enums (type *enumeration) {
1070 assert(enumeration && (enumeration->type_op == type_enumeration));
1071 return enumeration->attr.ea.n_enums;
1073 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1074 assert(enumeration && (enumeration->type_op == type_enumeration));
1075 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1076 enumeration->attr.ea.enumer[pos] = con;
1078 tarval *get_enumeration_enum (type *enumeration, int pos) {
1079 assert(enumeration && (enumeration->type_op == type_enumeration));
1080 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1081 return enumeration->attr.ea.enumer[pos];
1083 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1084 assert(enumeration && (enumeration->type_op == type_enumeration));
1085 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1086 enumeration->attr.ea.enum_nameid[pos] = id;
1088 ident *get_enumeration_nameid (type *enumeration, int pos) {
1089 assert(enumeration && (enumeration->type_op == type_enumeration));
1090 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1091 return enumeration->attr.ea.enum_nameid[pos];
1093 const char *get_enumeration_name(type *enumeration, int pos) {
1094 assert(enumeration && (enumeration->type_op == type_enumeration));
1095 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1096 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1100 bool is_enumeration_type (type *enumeration) {
1101 assert(enumeration);
1102 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1105 /*******************************************************************/
1106 /** TYPE_POINTER **/
1107 /*******************************************************************/
1109 /* Create a new type pointer */
1110 INLINE type *new_type_pointer (ident *name, type *points_to) {
1112 res = new_type(type_pointer, mode_P, name);
1113 res->attr.pa.points_to = points_to;
1114 res->size = get_mode_size(res->mode);
1115 res->state = layout_fixed;
1118 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1119 type *res = new_type_pointer (name, points_to);
1120 set_type_dbg_info(res, db);
1123 INLINE void free_pointer_attrs (type *pointer) {
1124 assert(pointer && (pointer->type_op == type_pointer));
1126 /* manipulate fields of type_pointer */
1127 void set_pointer_points_to_type (type *pointer, type *tp) {
1128 assert(pointer && (pointer->type_op == type_pointer));
1129 pointer->attr.pa.points_to = tp;
1131 type *get_pointer_points_to_type (type *pointer) {
1132 assert(pointer && (pointer->type_op == type_pointer));
1133 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1137 bool is_pointer_type (type *pointer) {
1139 if (pointer->type_op == type_pointer) return 1; else return 0;
1143 /*******************************************************************/
1144 /** TYPE_PRIMITIVE **/
1145 /*******************************************************************/
1147 /* create a new type primitive */
1148 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1150 /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1151 res = new_type(type_primitive, mode, name);
1152 res->size = get_mode_size(mode);
1153 res->state = layout_fixed;
1156 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1157 type *res = new_type_primitive (name, mode);
1158 set_type_dbg_info(res, db);
1161 INLINE void free_primitive_attrs (type *primitive) {
1162 assert(primitive && (primitive->type_op == type_primitive));
1166 bool is_primitive_type (type *primitive) {
1167 assert(primitive && primitive->kind == k_type);
1168 if (primitive->type_op == type_primitive) return 1; else return 0;
1171 /*******************************************************************/
1172 /** common functionality **/
1173 /*******************************************************************/
1176 INLINE int is_atomic_type(type *tp) {
1177 assert(tp && tp->kind == k_type);
1178 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1179 is_enumeration_type(tp));
1181 INLINE int is_compound_type(type *tp) {
1182 assert(tp && tp->kind == k_type);
1183 return (is_class_type(tp) || is_struct_type(tp) ||
1184 is_array_type(tp) || is_union_type(tp));