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
37 # include "typegmod_t.h"
44 /*******************************************************************/
46 /*******************************************************************/
49 /** Returns a new, unique number to number nodes or the like. */
50 int get_irp_new_node_nr(void);
53 unsigned long type_visited;
54 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
55 INLINE unsigned long get_master_type_visited() { return type_visited; }
56 INLINE void inc_master_type_visited() { type_visited++; }
58 void free_type(type *tp) {
59 /* Remove from list of all types */
61 /* Free the attributes of the type. */
63 /* Free entities automatically allocated with the type */
64 if (is_array_type(tp))
65 free_entity(get_array_element_entity(tp));
66 /* And now the type itself... */
71 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
75 assert(type_op != type_id);
76 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
78 node_size = offsetof (type, attr) + type_op->attr_size;
79 res = (type *) xmalloc (node_size);
80 add_irp_type(res); /* Remember the new type global. */
83 res->type_op = type_op;
86 res->state = layout_undefined;
91 res->nr = get_irp_new_node_nr();
97 void free_type_attrs(type *tp) {
98 switch(get_type_tpop_code(tp)) {
99 case tpo_class: { free_class_attrs(tp); } break;
100 case tpo_struct: { free_struct_attrs(tp); } break;
101 case tpo_method: { free_method_attrs(tp); } break;
102 case tpo_union: { free_union_attrs(tp); } break;
103 case tpo_array: { free_array_attrs(tp); } break;
104 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
105 case tpo_pointer: { free_pointer_attrs(tp); } break;
106 case tpo_primitive: { free_primitive_attrs(tp); } break;
111 /* set/get the link field */
112 void *get_type_link(type *tp)
114 assert(tp && tp->kind == k_type);
118 void set_type_link(type *tp, void *l)
120 assert(tp && tp->kind == k_type);
124 tp_op* get_type_tpop(type *tp) {
125 assert(tp && tp->kind == k_type);
129 ident* get_type_tpop_nameid(type *tp) {
130 assert(tp && tp->kind == k_type);
131 return tp->type_op->name;
134 const char* get_type_tpop_name(type *tp) {
135 assert(tp && tp->kind == k_type);
136 return id_to_str(tp->type_op->name);
139 tp_opcode get_type_tpop_code(type *tp) {
140 assert(tp && tp->kind == k_type);
141 return tp->type_op->code;
144 ir_mode* get_type_mode(type *tp) {
145 assert(tp && tp->kind == k_type);
149 void set_type_mode(type *tp, ir_mode* m) {
150 assert(tp && tp->kind == k_type);
152 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
153 /* Modes of primitives must be data */
154 ((tp->type_op != type_enumeration) || mode_is_int(m)));
155 /* Modes of enumerations must be integers */
157 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
158 /* For pointer, primitive and enumeration size depends on the mode. */
159 assert((get_mode_size_bytes(m) != -1) && "unorthodox modes not implemented");
160 tp->size = get_mode_size_bytes(m);
165 ident* get_type_ident(type *tp) {
166 assert(tp && tp->kind == k_type);
170 void set_type_ident(type *tp, ident* id) {
171 assert(tp && tp->kind == k_type);
175 /* Outputs a unique number for this node */
177 get_type_nr(type *tp) {
186 const char* get_type_name(type *tp) {
187 assert(tp && tp->kind == k_type);
188 return (id_to_str(tp->name));
191 int get_type_size(type *tp) {
192 assert(tp && tp->kind == k_type);
197 set_type_size(type *tp, int size) {
198 assert(tp && tp->kind == k_type);
199 /* For pointer enumeration and primitive size depends on the mode.
200 Methods don't have a size. */
201 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
202 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
207 get_type_state(type *tp) {
208 assert(tp && tp->kind == k_type);
213 set_type_state(type *tp, type_state state) {
214 assert(tp && tp->kind == k_type);
216 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
217 (tp->type_op == type_method))
220 /* Just a correctness check: */
221 if (state == layout_fixed) {
223 switch (get_type_tpop_code(tp)) {
226 assert(get_type_size(tp) > -1);
227 if (tp != get_glob_type())
228 for (i = 0; i < get_class_n_members(tp); i++) {
229 assert(get_entity_offset(get_class_member(tp, i)) > -1);
230 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
231 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
236 assert(get_type_size(tp) > -1);
237 for (i = 0; i < get_struct_n_members(tp); i++) {
238 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
239 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
248 Assure that only innermost dimension is dynamic? */
250 case tpo_enumeration:
252 assert(get_type_mode != NULL);
253 for (i = 0; i < get_enumeration_n_enums(tp); i++)
254 assert(get_enumeration_enum(tp, i) != NULL);
262 unsigned long get_type_visited(type *tp) {
263 assert(tp && tp->kind == k_type);
267 void set_type_visited(type *tp, unsigned long num) {
268 assert(tp && tp->kind == k_type);
271 /* Sets visited field in type to type_visited. */
272 void mark_type_visited(type *tp) {
273 assert(tp && tp->kind == k_type);
274 assert(tp->visit < type_visited);
275 tp->visit = type_visited;
277 /* @@@ name clash with master flag
278 bool type_visited(type *tp) {
279 assert(tp && tp->kind == k_type);
280 return tp->visit >= type_visited;
282 bool type_not_visited(type *tp) {
283 assert(tp && tp->kind == k_type);
284 return tp->visit < type_visited;
288 int is_type (void *thing) {
290 if (get_kind(thing) == k_type)
297 bool equal_type(type *typ1, type *typ2) {
302 if (typ1 == typ2) return true;
304 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
305 (get_type_ident(typ1) != get_type_ident(typ2)) ||
306 (get_type_mode(typ1) != get_type_mode(typ2)) ||
307 (get_type_state(typ1) != get_type_state(typ2)))
309 if ((get_type_state(typ1) == layout_fixed) &&
310 (get_type_size(typ1) != get_type_size(typ2)))
313 switch(get_type_tpop_code(typ1)) {
315 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
316 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
317 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
318 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
319 /** Compare the members **/
320 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
321 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
322 /* First sort the members of typ2 */
323 for (i = 0; i < get_class_n_members(typ1); i++) {
324 entity *e1 = get_class_member(typ1, i);
325 for (j = 0; j < get_class_n_members(typ2); j++) {
326 entity *e2 = get_class_member(typ2, j);
327 if (get_entity_name(e1) == get_entity_name(e2))
331 for (i = 0; i < get_class_n_members(typ1); i++) {
332 if (!m[i] || /* Found no counterpart */
333 !equal_entity(get_class_member(typ1, i), m[i]))
336 /** Compare the supertypes **/
337 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
338 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
339 /* First sort the supertypes of typ2 */
340 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
341 type *t1 = get_class_supertype(typ1, i);
342 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
343 type *t2 = get_class_supertype(typ2, j);
344 if (get_type_ident(t2) == get_type_ident(t1))
348 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
349 if (!t[i] || /* Found no counterpart */
350 get_class_supertype(typ1, i) != t[i])
355 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
356 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
357 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
358 /* First sort the members of lt */
359 for (i = 0; i < get_struct_n_members(typ1); i++) {
360 entity *e1 = get_struct_member(typ1, i);
361 for (j = 0; j < get_struct_n_members(typ2); j++) {
362 entity *e2 = get_struct_member(typ2, j);
363 if (get_entity_name(e1) == get_entity_name(e2))
367 for (i = 0; i < get_struct_n_members(typ1); i++) {
368 if (!m[i] || /* Found no counterpart */
369 !equal_entity(get_struct_member(typ1, i), m[i]))
374 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
375 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
376 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
377 for (i = 0; i < get_method_n_params(typ1); i++) {
378 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
381 for (i = 0; i < get_method_n_ress(typ1); i++) {
382 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
387 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
388 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
389 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
390 /* First sort the members of lt */
391 for (i = 0; i < get_union_n_members(typ1); i++) {
392 entity *e1 = get_union_member(typ1, i);
393 for (j = 0; j < get_union_n_members(typ2); j++) {
394 entity *e2 = get_union_member(typ2, j);
395 if (get_entity_name(e1) == get_entity_name(e2))
399 for (i = 0; i < get_union_n_members(typ1); i++) {
400 if (!m[i] || /* Found no counterpart */
401 !equal_entity(get_union_member(typ1, i), m[i]))
406 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
408 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
410 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
411 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
412 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
414 if (get_array_order(typ1, i) != get_array_order(typ2, i))
415 assert(0 && "type compare with different dimension orders not implemented");
418 case tpo_enumeration: {
419 assert(0 && "enumerations not implemented");
422 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
425 case tpo_primitive: {
432 bool smaller_type (type *st, type *lt) {
436 if (st == lt) return true;
438 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
441 switch(get_type_tpop_code(st)) {
443 return is_subclass_of(st, lt);
446 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
447 m = alloca(sizeof(entity *) * get_struct_n_members(st));
448 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
449 /* First sort the members of lt */
450 for (i = 0; i < get_struct_n_members(st); i++) {
451 entity *se = get_struct_member(st, i);
452 for (j = 0; j < get_struct_n_members(lt); j++) {
453 entity *le = get_struct_member(lt, j);
454 if (get_entity_name(le) == get_entity_name(se))
458 for (i = 0; i < get_struct_n_members(st); i++) {
459 if (!m[i] || /* Found no counterpart */
460 !smaller_type(get_entity_type(get_struct_member(st, i)),
461 get_entity_type(m[i])))
466 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
467 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
468 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
469 for (i = 0; i < get_method_n_params(st); i++) {
470 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
473 for (i = 0; i < get_method_n_ress(st); i++) {
474 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
479 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
480 m = alloca(sizeof(entity *) * get_union_n_members(st));
481 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
482 /* First sort the members of lt */
483 for (i = 0; i < get_union_n_members(st); i++) {
484 entity *se = get_union_member(st, i);
485 for (j = 0; j < get_union_n_members(lt); j++) {
486 entity *le = get_union_member(lt, j);
487 if (get_entity_name(le) == get_entity_name(se))
491 for (i = 0; i < get_union_n_members(st); i++) {
492 if (!m[i] || /* Found no counterpart */
493 !smaller_type(get_entity_type(get_union_member(st, i)),
494 get_entity_type(m[i])))
499 type *set, *let; /* small/large elt. type */
500 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
502 set = get_array_element_type(st);
503 let = get_array_element_type(lt);
505 /* If the elt types are different, set must be convertible
506 to let, and they must have the same size so that address
507 computations work out. To have a size the layout must
509 if ((get_type_state(set) != layout_fixed) ||
510 (get_type_state(let) != layout_fixed))
512 if (!smaller_type(set, let) ||
513 get_type_size(set) != get_type_size(let))
516 for(i = 0; i < get_array_n_dimensions(st); i++) {
517 if (get_array_lower_bound(lt, i))
518 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
520 if (get_array_upper_bound(lt, i))
521 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
525 case tpo_enumeration: {
526 assert(0 && "enumerations not implemented");
529 if (!smaller_type(get_pointer_points_to_type(st),
530 get_pointer_points_to_type(lt)))
533 case tpo_primitive: {
534 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
542 /*******************************************************************/
544 /*******************************************************************/
546 /* create a new class type */
547 INLINE type *new_type_class (ident *name) {
550 res = new_type(type_class, NULL, name);
552 res->attr.ca.members = NEW_ARR_F (entity *, 1);
553 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
554 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
555 res->attr.ca.peculiarity = existent;
556 res->attr.ca.dfn = 0;
560 type *new_d_type_class (ident *name, dbg_info* db) {
561 type *res = new_type_class (name);
562 set_type_dbg_info(res, db);
565 INLINE void free_class_attrs(type *clss) {
566 assert(clss && (clss->type_op == type_class));
567 DEL_ARR_F(clss->attr.ca.members);
568 DEL_ARR_F(clss->attr.ca.subtypes);
569 DEL_ARR_F(clss->attr.ca.supertypes);
572 /* manipulate private fields of class type */
573 void add_class_member (type *clss, entity *member) {
574 assert(clss && (clss->type_op == type_class));
575 ARR_APP1 (entity *, clss->attr.ca.members, member);
577 int get_class_n_members (type *clss) {
578 assert(clss && (clss->type_op == type_class));
579 return (ARR_LEN (clss->attr.ca.members))-1;
581 int get_class_member_index(type *clss, entity *mem) {
583 assert(clss && (clss->type_op == type_class));
584 for (i = 0; i < get_class_n_members(clss); i++)
585 if (get_class_member(clss, i) == mem)
589 entity *get_class_member (type *clss, int pos) {
590 assert(clss && (clss->type_op == type_class));
591 assert(pos >= 0 && pos < get_class_n_members(clss));
592 return clss->attr.ca.members[pos+1];
594 void set_class_member (type *clss, entity *member, int pos) {
595 assert(clss && (clss->type_op == type_class));
596 assert(pos >= 0 && pos < get_class_n_members(clss));
597 clss->attr.ca.members[pos+1] = member;
599 void set_class_members (type *clss, entity **members, int arity) {
601 assert(clss && (clss->type_op == type_class));
602 DEL_ARR_F(clss->attr.ca.members);
603 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
604 for (i = 0; i < arity; i++) {
605 set_entity_owner(members[i], clss);
606 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
609 void remove_class_member(type *clss, entity *member) {
611 assert(clss && (clss->type_op == type_class));
612 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
613 if (clss->attr.ca.members[i] == member) {
614 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
615 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
616 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
622 void add_class_subtype (type *clss, type *subtype) {
624 assert(clss && (clss->type_op == type_class));
625 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
626 for (i = 0; i < get_class_n_supertypes(subtype); i++)
627 if (get_class_supertype(subtype, i) == clss)
628 /* Class already registered */
630 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
632 int get_class_n_subtypes (type *clss) {
633 assert(clss && (clss->type_op == type_class));
634 return (ARR_LEN (clss->attr.ca.subtypes))-1;
636 type *get_class_subtype (type *clss, int pos) {
637 assert(clss && (clss->type_op == type_class));
638 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
639 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
641 void set_class_subtype (type *clss, type *subtype, int pos) {
642 assert(clss && (clss->type_op == type_class));
643 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
644 clss->attr.ca.subtypes[pos+1] = subtype;
646 void remove_class_subtype(type *clss, type *subtype) {
648 assert(clss && (clss->type_op == type_class));
649 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
650 if (clss->attr.ca.subtypes[i] == subtype) {
651 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
652 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
653 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
658 void add_class_supertype (type *clss, type *supertype) {
660 assert(clss && (clss->type_op == type_class));
661 assert(supertype && (supertype -> type_op == type_class));
662 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
663 for (i = 0; i < get_class_n_subtypes(supertype); i++)
664 if (get_class_subtype(supertype, i) == clss)
665 /* Class already registered */
667 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
669 int get_class_n_supertypes (type *clss) {
670 assert(clss && (clss->type_op == type_class));
671 return (ARR_LEN (clss->attr.ca.supertypes))-1;
673 int get_class_supertype_index(type *clss, type *super_clss) {
675 assert(clss && (clss->type_op == type_class));
676 assert(super_clss && (super_clss->type_op == type_class));
677 for (i = 0; i < get_class_n_supertypes(clss); i++)
678 if (get_class_supertype(clss, i) == super_clss)
682 type *get_class_supertype (type *clss, int pos) {
683 assert(clss && (clss->type_op == type_class));
684 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
685 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
687 void set_class_supertype (type *clss, type *supertype, int pos) {
688 assert(clss && (clss->type_op == type_class));
689 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
690 clss->attr.ca.supertypes[pos+1] = supertype;
692 void remove_class_supertype(type *clss, type *supertype) {
694 assert(clss && (clss->type_op == type_class));
695 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
696 if (clss->attr.ca.supertypes[i] == supertype) {
697 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
698 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
699 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
704 INLINE peculiarity get_class_peculiarity (type *clss) {
705 assert(clss && (clss->type_op == type_class));
706 return clss->attr.ca.peculiarity;
708 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
709 assert(clss && (clss->type_op == type_class));
710 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
711 clss->attr.ca.peculiarity = pec;
714 void set_class_dfn (type *clss, int dfn)
716 clss->attr.ca.dfn = dfn;
719 int get_class_dfn (type *clss)
721 return (clss->attr.ca.dfn);
725 bool is_class_type(type *clss) {
727 if (clss->type_op == type_class) return 1; else return 0;
730 bool is_subclass_of(type *low, type *high) {
732 assert(is_class_type(low) && is_class_type(high));
733 if (low == high) return true;
734 /* depth first search from high downwards. */
735 for (i = 0; i < get_class_n_subtypes(high); i++) {
736 if (low == get_class_subtype(high, i))
738 if (is_subclass_of(low, get_class_subtype(high, i)))
744 /*******************************************************************/
746 /*******************************************************************/
748 /* create a new type struct */
749 INLINE type *new_type_struct (ident *name) {
751 res = new_type(type_struct, NULL, name);
752 res->attr.sa.members = NEW_ARR_F (entity *, 1);
755 type *new_d_type_struct (ident *name, dbg_info* db) {
756 type *res = new_type_struct (name);
757 set_type_dbg_info(res, db);
760 INLINE void free_struct_attrs (type *strct) {
761 assert(strct && (strct->type_op == type_struct));
762 DEL_ARR_F(strct->attr.sa.members);
765 /* manipulate private fields of struct */
766 int get_struct_n_members (type *strct) {
767 assert(strct && (strct->type_op == type_struct));
768 return (ARR_LEN (strct->attr.sa.members))-1;
770 void add_struct_member (type *strct, entity *member) {
771 assert(strct && (strct->type_op == type_struct));
772 assert(get_type_tpop(get_entity_type(member)) != type_method);
773 /* @@@ lowerfirm geht nicht durch */
774 ARR_APP1 (entity *, strct->attr.sa.members, member);
776 entity *get_struct_member (type *strct, int pos) {
777 assert(strct && (strct->type_op == type_struct));
778 assert(pos >= 0 && pos < get_struct_n_members(strct));
779 return strct->attr.sa.members[pos+1];
781 void set_struct_member (type *strct, int pos, entity *member) {
782 assert(strct && (strct->type_op == type_struct));
783 assert(pos >= 0 && pos < get_struct_n_members(strct));
784 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
785 strct->attr.sa.members[pos+1] = member;
787 void remove_struct_member(type *strct, entity *member) {
789 assert(strct && (strct->type_op == type_struct));
790 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
791 if (strct->attr.sa.members[i] == member) {
792 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
793 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
794 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
800 bool is_struct_type(type *strct) {
802 if (strct->type_op == type_struct) return 1; else return 0;
805 /*******************************************************************/
807 /*******************************************************************/
809 /* Create a new method type.
810 N_param is the number of parameters, n_res the number of results. */
811 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
813 res = new_type(type_method, mode_P, name);
814 res->state = layout_fixed;
815 assert((get_mode_size_bytes(mode_P) != -1) && "unorthodox modes not implemented");
816 res->size = get_mode_size_bytes(mode_P);
817 res->attr.ma.n_params = n_param;
818 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
819 res->attr.ma.n_res = n_res;
820 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
821 res->attr.ma.variadicity = non_variadic;
825 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
826 type *res = new_type_method (name, n_param, n_res);
827 set_type_dbg_info(res, db);
830 INLINE void free_method_attrs(type *method) {
831 assert(method && (method->type_op == type_method));
832 free(method->attr.ma.param_type);
833 free(method->attr.ma.res_type);
835 /* manipulate private fields of method. */
836 int get_method_n_params (type *method) {
837 assert(method && (method->type_op == type_method));
838 return method->attr.ma.n_params;
840 type *get_method_param_type(type *method, int pos) {
841 assert(method && (method->type_op == type_method));
842 assert(pos >= 0 && pos < get_method_n_params(method));
843 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
845 void set_method_param_type(type *method, int pos, type* tp) {
846 assert(method && (method->type_op == type_method));
847 assert(pos >= 0 && pos < get_method_n_params(method));
848 method->attr.ma.param_type[pos] = tp;
851 int get_method_n_ress (type *method) {
852 assert(method && (method->type_op == type_method));
853 return method->attr.ma.n_res;
855 type *get_method_res_type(type *method, int pos) {
856 assert(method && (method->type_op == type_method));
857 assert(pos >= 0 && pos < get_method_n_ress(method));
858 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
860 void set_method_res_type(type *method, int pos, type* tp) {
861 assert(method && (method->type_op == type_method));
862 assert(pos >= 0 && pos < get_method_n_ress(method));
863 method->attr.ma.res_type[pos] = tp;
866 variadicity get_method_variadicity(type *method)
868 assert(method && (method->type_op == type_method));
869 return method->attr.ma.variadicity;
872 void set_method_variadicity(type *method, variadicity vari)
874 assert(method && (method->type_op == type_method));
875 method->attr.ma.variadicity = vari;
879 bool is_method_type (type *method) {
881 if (method->type_op == type_method) return 1; else return 0;
884 /*******************************************************************/
886 /*******************************************************************/
888 /* create a new type uni */
889 INLINE type *new_type_union (ident *name) {
891 res = new_type(type_union, NULL, name);
892 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
893 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
894 res->attr.ua.members = NEW_ARR_F (entity *, 1);
897 type *new_d_type_union (ident *name, dbg_info* db) {
898 type *res = new_type_union (name);
899 set_type_dbg_info(res, db);
902 INLINE void free_union_attrs (type *uni) {
903 assert(uni && (uni->type_op == type_union));
904 DEL_ARR_F(uni->attr.ua.members);
906 /* manipulate private fields of union */
908 int get_union_n_types (type *uni) {
909 assert(uni && (uni->type_op == type_union));
910 return uni->attr.ua.n_types;
912 type *get_union_unioned_type (type *uni, int pos) {
913 assert(uni && (uni->type_op == type_union));
914 assert(pos >= 0 && pos < get_union_n_types(uni));
915 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
917 void set_union_unioned_type (type *uni, int pos, type *tp) {
918 assert(uni && (uni->type_op == type_union));
919 assert(pos >= 0 && pos < get_union_n_types(uni));
920 uni->attr.ua.unioned_type[pos] = tp;
922 ident *get_union_delim_nameid (type *uni, int pos) {
923 assert(uni && (uni->type_op == type_union));
924 assert(pos >= 0 && pos < get_union_n_types(uni));
925 return uni->attr.ua.delim_names[pos];
927 const char *get_union_delim_name (type *uni, int pos) {
928 assert(uni && (uni->type_op == type_union));
929 assert(pos >= 0 && pos < get_union_n_types(uni));
930 return id_to_str(uni->attr.ua.delim_names[pos]);
932 void set_union_delim_nameid (type *uni, int pos, ident *id) {
933 assert(uni && (uni->type_op == type_union));
934 assert(pos >= 0 && pos < get_union_n_types(uni));
935 uni->attr.ua.delim_names[pos] = id;
938 int get_union_n_members (type *uni) {
939 assert(uni && (uni->type_op == type_union));
940 return (ARR_LEN (uni->attr.ua.members))-1;
942 void add_union_member (type *uni, entity *member) {
943 assert(uni && (uni->type_op == type_union));
944 ARR_APP1 (entity *, uni->attr.ua.members, member);
946 entity *get_union_member (type *uni, int pos) {
947 assert(uni && (uni->type_op == type_union));
948 assert(pos >= 0 && pos < get_union_n_members(uni));
949 return uni->attr.ua.members[pos+1];
951 void set_union_member (type *uni, int pos, entity *member) {
952 assert(uni && (uni->type_op == type_union));
953 assert(pos >= 0 && pos < get_union_n_members(uni));
954 uni->attr.ua.members[pos+1] = member;
956 void remove_union_member(type *uni, entity *member) {
958 assert(uni && (uni->type_op == type_union));
959 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
960 if (uni->attr.ua.members[i] == member) {
961 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
962 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
963 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
969 bool is_union_type (type *uni) {
971 if (uni->type_op == type_union) return 1; else return 0;
974 /*******************************************************************/
976 /*******************************************************************/
979 /* create a new type array -- set dimension sizes independently */
980 INLINE type *new_type_array (ident *name, int n_dimensions,
981 type *element_type) {
984 assert(!is_method_type(element_type));
985 res = new_type(type_array, NULL, name);
986 res->attr.aa.n_dimensions = n_dimensions;
987 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
988 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
989 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
991 for (i = 0; i < n_dimensions; i++) {
992 res->attr.aa.lower_bound[i] = NULL;
993 res->attr.aa.upper_bound[i] = NULL;
994 res->attr.aa.order[i] = i;
996 res->attr.aa.element_type = element_type;
997 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
1001 type *new_d_type_array (ident *name, int n_dimensions,
1002 type *element_type, dbg_info* db) {
1003 type *res = new_type_array (name, n_dimensions, element_type);
1004 set_type_dbg_info(res, db);
1008 INLINE void free_array_attrs (type *array) {
1009 assert(array && (array->type_op == type_array));
1010 free(array->attr.aa.lower_bound);
1011 free(array->attr.aa.upper_bound);
1014 /* manipulate private fields of array type */
1015 int get_array_n_dimensions (type *array) {
1016 assert(array && (array->type_op == type_array));
1017 return array->attr.aa.n_dimensions;
1021 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1022 ir_node * upper_bound) {
1023 assert(array && (array->type_op == type_array));
1024 array->attr.aa.lower_bound[dimension] = lower_bound;
1025 array->attr.aa.upper_bound[dimension] = upper_bound;
1028 set_array_bounds_int (type *array, int dimension, int lower_bound,
1030 ir_graph *rem = current_ir_graph;
1031 current_ir_graph = get_const_code_irg();
1032 set_array_bounds (array, dimension,
1033 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1034 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1035 current_ir_graph = rem;
1038 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1039 assert(array && (array->type_op == type_array));
1040 array->attr.aa.lower_bound[dimension] = lower_bound;
1042 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1043 ir_graph *rem = current_ir_graph;
1044 current_ir_graph = get_const_code_irg();
1045 set_array_lower_bound (array, dimension,
1046 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1047 current_ir_graph = rem;
1050 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1051 assert(array && (array->type_op == type_array));
1052 array->attr.aa.upper_bound[dimension] = upper_bound;
1054 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1055 ir_graph *rem = current_ir_graph;
1056 current_ir_graph = get_const_code_irg();
1057 set_array_upper_bound (array, dimension,
1058 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1059 current_ir_graph = rem;
1061 ir_node * get_array_lower_bound (type *array, int dimension) {
1062 assert(array && (array->type_op == type_array));
1063 return array->attr.aa.lower_bound[dimension];
1065 ir_node * get_array_upper_bound (type *array, int dimension) {
1066 assert(array && (array->type_op == type_array));
1067 return array->attr.aa.upper_bound[dimension];
1070 void set_array_order (type *array, int dimension, int order) {
1071 assert(array && (array->type_op == type_array));
1072 array->attr.aa.order[dimension] = order;
1074 int get_array_order (type *array, int dimension) {
1075 assert(array && (array->type_op == type_array));
1076 return array->attr.aa.order[dimension];
1079 void set_array_element_type (type *array, type *tp) {
1080 assert(array && (array->type_op == type_array));
1081 assert(!is_method_type(tp));
1082 array->attr.aa.element_type = tp;
1084 type *get_array_element_type (type *array) {
1085 assert(array && (array->type_op == type_array));
1086 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1089 void set_array_element_entity (type *array, entity *ent) {
1090 assert(array && (array->type_op == type_array));
1091 assert((get_entity_type(ent)->type_op != type_method));
1092 array->attr.aa.element_ent = ent;
1093 array->attr.aa.element_type = get_entity_type(ent);
1095 entity *get_array_element_entity (type *array) {
1096 assert(array && (array->type_op == type_array));
1097 return array->attr.aa.element_ent;
1101 bool is_array_type (type *array) {
1103 if (array->type_op == type_array) return 1; else return 0;
1106 /*******************************************************************/
1107 /** TYPE_ENUMERATION **/
1108 /*******************************************************************/
1110 /* create a new type enumeration -- set the enumerators independently */
1111 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1114 res = new_type(type_enumeration, NULL, name);
1115 res->attr.ea.n_enums = n_enums;
1116 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1117 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1118 for (i = 0; i < n_enums; i++) {
1119 res->attr.ea.enumer[i] = NULL;
1120 res->attr.ea.enum_nameid = NULL;
1124 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1125 type *res = new_type_enumeration (name, n_enums);
1126 set_type_dbg_info(res, db);
1130 INLINE void free_enumeration_attrs(type *enumeration) {
1131 assert(enumeration && (enumeration->type_op == type_enumeration));
1132 free(enumeration->attr.ea.enumer);
1133 free(enumeration->attr.ea.enum_nameid);
1136 /* manipulate fields of enumeration type. */
1137 int get_enumeration_n_enums (type *enumeration) {
1138 assert(enumeration && (enumeration->type_op == type_enumeration));
1139 return enumeration->attr.ea.n_enums;
1141 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1142 assert(enumeration && (enumeration->type_op == type_enumeration));
1143 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1144 enumeration->attr.ea.enumer[pos] = con;
1146 tarval *get_enumeration_enum (type *enumeration, int pos) {
1147 assert(enumeration && (enumeration->type_op == type_enumeration));
1148 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1149 return enumeration->attr.ea.enumer[pos];
1151 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1152 assert(enumeration && (enumeration->type_op == type_enumeration));
1153 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1154 enumeration->attr.ea.enum_nameid[pos] = id;
1156 ident *get_enumeration_nameid (type *enumeration, int pos) {
1157 assert(enumeration && (enumeration->type_op == type_enumeration));
1158 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1159 return enumeration->attr.ea.enum_nameid[pos];
1161 const char *get_enumeration_name(type *enumeration, int pos) {
1162 assert(enumeration && (enumeration->type_op == type_enumeration));
1163 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1164 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1168 bool is_enumeration_type (type *enumeration) {
1169 assert(enumeration);
1170 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1173 /*******************************************************************/
1174 /** TYPE_POINTER **/
1175 /*******************************************************************/
1177 /* Create a new type pointer */
1178 INLINE type *new_type_pointer (ident *name, type *points_to) {
1180 res = new_type(type_pointer, mode_P, name);
1181 res->attr.pa.points_to = points_to;
1182 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1183 res->size = get_mode_size_bytes(res->mode);
1184 res->state = layout_fixed;
1187 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1188 type *res = new_type_pointer (name, points_to);
1189 set_type_dbg_info(res, db);
1192 INLINE void free_pointer_attrs (type *pointer) {
1193 assert(pointer && (pointer->type_op == type_pointer));
1195 /* manipulate fields of type_pointer */
1196 void set_pointer_points_to_type (type *pointer, type *tp) {
1197 assert(pointer && (pointer->type_op == type_pointer));
1198 pointer->attr.pa.points_to = tp;
1200 type *get_pointer_points_to_type (type *pointer) {
1201 assert(pointer && (pointer->type_op == type_pointer));
1202 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1206 bool is_pointer_type (type *pointer) {
1208 if (pointer->type_op == type_pointer) return 1; else return 0;
1212 /*******************************************************************/
1213 /** TYPE_PRIMITIVE **/
1214 /*******************************************************************/
1216 /* create a new type primitive */
1217 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1219 /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1220 res = new_type(type_primitive, mode, name);
1221 assert((get_mode_size_bytes(mode) != -1) && "unorthodox modes not implemented");
1222 res->size = get_mode_size_bytes(mode);
1223 res->state = layout_fixed;
1226 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1227 type *res = new_type_primitive (name, mode);
1228 set_type_dbg_info(res, db);
1231 INLINE void free_primitive_attrs (type *primitive) {
1232 assert(primitive && (primitive->type_op == type_primitive));
1236 bool is_primitive_type (type *primitive) {
1237 assert(primitive && primitive->kind == k_type);
1238 if (primitive->type_op == type_primitive) return 1; else return 0;
1241 /*******************************************************************/
1242 /** common functionality **/
1243 /*******************************************************************/
1246 INLINE int is_atomic_type(type *tp) {
1247 assert(tp && tp->kind == k_type);
1248 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1249 is_enumeration_type(tp));
1253 * Gets the number of elements in a firm compound type.
1255 int get_compound_n_members(type *tp)
1259 if (is_struct_type(tp))
1260 res = get_struct_n_members(tp);
1261 else if (is_class_type(tp))
1262 res = get_class_n_members(tp);
1263 else if (is_union_type(tp))
1264 res = get_union_n_members(tp);
1266 assert(0 && "need struct, union or class for member count");
1272 * Gets the member of a firm compound type at position pos.
1274 entity *get_compound_member(type *tp, int pos)
1278 if (is_struct_type(tp))
1279 res = get_struct_member(tp, pos);
1280 else if (is_class_type(tp))
1281 res = get_class_member(tp, pos);
1282 else if (is_union_type(tp))
1283 res = get_union_member(tp, pos);
1285 assert(0 && "need struct, union or class to get a member");
1291 INLINE int is_compound_type(type *tp) {
1292 assert(tp && tp->kind == k_type);
1293 return (is_class_type(tp) || is_struct_type(tp) ||
1294 is_array_type(tp) || is_union_type(tp));