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.
38 # include "typegmod_t.h"
45 /*******************************************************************/
47 /*******************************************************************/
49 unsigned long type_visited;
52 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
56 assert(type_op != type_id);
58 node_size = offsetof (type, attr) + type_op->attr_size;
59 res = (type *) xmalloc (node_size);
60 add_irp_type(res); /* Remember the new type global. */
63 res->type_op = type_op;
66 res->state = layout_undefined;
74 void free_type_attrs(type *tp) {
75 switch(get_type_tpop_code(tp)) {
76 case tpo_class: { free_class_attrs(tp); } break;
77 case tpo_struct: { free_struct_attrs(tp); } break;
78 case tpo_method: { free_method_attrs(tp); } break;
79 case tpo_union: { free_union_attrs(tp); } break;
80 case tpo_array: { free_array_attrs(tp); } break;
81 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
82 case tpo_pointer: { free_pointer_attrs(tp); } break;
83 case tpo_primitive: { free_primitive_attrs(tp); } break;
88 /* set/get the link field */
89 void *get_type_link(type *tp)
95 void set_type_link(type *tp, void *l)
101 tp_op* get_type_tpop(type *tp) {
106 ident* get_type_tpop_nameid(type *tp) {
108 return tp->type_op->name;
111 const char* get_type_tpop_name(type *tp) {
113 return id_to_str(tp->type_op->name);
116 tp_opcode get_type_tpop_code(type *tp) {
118 return tp->type_op->code;
121 ir_mode* get_type_mode(type *tp) {
126 void set_type_mode(type *tp, ir_mode* m) {
129 /* For pointer and primitive size depends on the mode. */
130 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
131 tp->size == get_mode_size(m);
134 ident* get_type_ident(type *tp) {
139 void set_type_ident(type *tp, ident* id) {
144 const char* get_type_name(type *tp) {
146 return id_to_str(tp->name);
149 int get_type_size(type *tp) {
155 set_type_size(type *tp, int size) {
157 /* For pointer and primitive size depends on the mode. */
158 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
163 get_type_state(type *tp) {
169 set_type_state(type *tp, type_state state) {
171 /* For pointer and primitive always fixed. */
172 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive)) {
173 /* @@@ assert that the layout really is fixed!!! */
178 unsigned long get_type_visited(type *tp) {
183 void set_type_visited(type *tp, unsigned long num) {
187 /* Sets visited field in type to type_visited. */
188 void mark_type_visited(type *tp) {
190 assert(tp->visit < type_visited);
191 tp->visit = type_visited;
194 int is_type (void *thing) {
196 if (get_kind(thing) == k_type)
202 /*******************************************************************/
204 /*******************************************************************/
206 /* create a new class type */
207 type *new_type_class (ident *name) {
210 res = new_type(type_class, NULL, name);
212 res->attr.ca.members = NEW_ARR_F (entity *, 1);
213 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
214 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
218 inline void free_class_attrs(type *clss) {
219 assert(clss && (clss->type_op == type_class));
220 DEL_ARR_F(clss->attr.ca.members);
221 DEL_ARR_F(clss->attr.ca.subtypes);
222 DEL_ARR_F(clss->attr.ca.supertypes);
224 /* manipulate private fields of class type */
225 void add_class_member (type *clss, entity *member) {
226 assert(clss && (clss->type_op == type_class));
227 ARR_APP1 (entity *, clss->attr.ca.members, member);
229 int get_class_n_member (type *clss) {
230 assert(clss && (clss->type_op == type_class));
231 return (ARR_LEN (clss->attr.ca.members))-1;
233 entity *get_class_member (type *clss, int pos) {
234 assert(clss && (clss->type_op == type_class));
235 return clss->attr.ca.members[pos+1];
237 void set_class_member (type *clss, entity *member, int pos) {
238 assert(clss && (clss->type_op == type_class));
239 clss->attr.ca.members[pos+1] = member;
241 void remove_class_member(type *clss, entity *member) {
243 assert(clss && (clss->type_op == type_class));
244 for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
245 if (clss->attr.ca.members[i+1] == member) {
246 for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
247 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
248 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
253 void add_class_subtype (type *clss, type *subtype) {
255 assert(clss && (clss->type_op == type_class));
256 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
257 for (i = 0; i < get_class_n_supertype(subtype); i++)
258 if (get_class_supertype(subtype, i) == clss)
259 /* Class already registered */
261 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
263 int get_class_n_subtype (type *clss) {
264 assert(clss && (clss->type_op == type_class));
265 return (ARR_LEN (clss->attr.ca.subtypes))-1;
267 type *get_class_subtype (type *clss, int pos) {
268 assert(clss && (clss->type_op == type_class));
269 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
271 void set_class_subtype (type *clss, type *subtype, int pos) {
272 assert(clss && (clss->type_op == type_class));
273 clss->attr.ca.subtypes[pos+1] = subtype;
275 void remove_class_subtype(type *clss, type *subtype) {
277 assert(clss && (clss->type_op == type_class));
278 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
279 if (clss->attr.ca.subtypes[i+1] == subtype) {
280 for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
281 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
282 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
287 void add_class_supertype (type *clss, type *supertype) {
289 assert(clss && (clss->type_op == type_class));
290 assert(supertype && (supertype -> type_op == type_class));
291 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
292 for (i = 0; i < get_class_n_subtype(supertype); i++)
293 if (get_class_subtype(supertype, i) == clss)
294 /* Class already registered */
296 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
298 int get_class_n_supertype (type *clss) {
299 assert(clss && (clss->type_op == type_class));
300 return (ARR_LEN (clss->attr.ca.supertypes))-1;
302 type *get_class_supertype (type *clss, int pos) {
303 assert(clss && (clss->type_op == type_class));
304 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
306 void set_class_supertype (type *clss, type *supertype, int pos) {
307 assert(clss && (clss->type_op == type_class));
308 clss->attr.ca.supertypes[pos+1] = supertype;
310 void remove_class_supertype(type *clss, type *supertype) {
312 assert(clss && (clss->type_op == type_class));
313 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
314 if (clss->attr.ca.supertypes[i+1] == supertype) {
315 for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
316 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
317 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
322 bool is_class_type(type *clss) {
324 if (clss->type_op == type_class) return 1; else return 0;
327 /*******************************************************************/
329 /*******************************************************************/
331 /* create a new type struct */
332 type *new_type_struct (ident *name) {
334 res = new_type(type_struct, NULL, name);
335 res->attr.sa.members = NEW_ARR_F (entity *, 1);
338 inline void free_struct_attrs (type *strct) {
339 assert(strct && (strct->type_op == type_struct));
340 DEL_ARR_F(strct->attr.sa.members);
342 /* manipulate private fields of struct */
343 void add_struct_member (type *strct, entity *member) {
344 assert(strct && (strct->type_op == type_struct));
345 ARR_APP1 (entity *, strct->attr.sa.members, member);
347 int get_struct_n_member (type *strct) {
348 assert(strct && (strct->type_op == type_struct));
349 return (ARR_LEN (strct->attr.sa.members))-1;
351 entity *get_struct_member (type *strct, int pos) {
352 assert(strct && (strct->type_op == type_struct));
353 return strct->attr.sa.members[pos+1];
355 void set_struct_member (type *strct, int pos, entity *member) {
356 assert(strct && (strct->type_op == type_struct));
357 strct->attr.sa.members[pos+1] = member;
359 void remove_struct_member(type *strct, entity *member) {
361 assert(strct && (strct->type_op == type_struct));
362 for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
363 if (strct->attr.sa.members[i+1] == member) {
364 for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
365 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
366 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
371 bool is_struct_type(type *strct) {
373 if (strct->type_op == type_struct) return 1; else return 0;
376 /*******************************************************************/
378 /*******************************************************************/
380 /* Create a new method type.
381 N_param is the number of parameters, n_res the number of results. */
382 type *new_type_method (ident *name, int n_param, int n_res) {
384 res = new_type(type_method, NULL, name);
385 res->attr.ma.n_params = n_param;
386 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
387 res->attr.ma.n_res = n_res;
388 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
391 inline void free_method_attrs(type *method) {
392 assert(method && (method->type_op == type_method));
393 free(method->attr.ma.param_type);
394 free(method->attr.ma.res_type);
396 /* manipulate private fields of method. */
397 int get_method_n_params (type *method) {
398 assert(method && (method->type_op == type_method));
399 return method->attr.ma.n_params;
401 type *get_method_param_type(type *method, int pos) {
402 assert(method && (method->type_op == type_method));
403 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
405 void set_method_param_type(type *method, int pos, type* type) {
406 assert(method && (method->type_op == type_method));
407 method->attr.ma.param_type[pos] = type;
410 int get_method_n_res (type *method) {
411 assert(method && (method->type_op == type_method));
412 return method->attr.ma.n_res;
414 type *get_method_res_type(type *method, int pos) {
415 assert(method && (method->type_op == type_method));
416 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
418 void set_method_res_type(type *method, int pos, type* type) {
419 assert(method && (method->type_op == type_method));
420 method->attr.ma.res_type[pos] = type;
424 bool is_method_type (type *method) {
426 if (method->type_op == type_method) return 1; else return 0;
430 /*******************************************************************/
432 /*******************************************************************/
434 /* create a new type uni */
435 type *new_type_uni (ident *name) {
437 res = new_type(type_union, NULL, name);
438 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
439 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
440 res->attr.ua.members = NEW_ARR_F (entity *, 1);
443 inline void free_union_attrs (type *uni) {
444 assert(uni && (uni->type_op == type_union));
445 DEL_ARR_F(uni->attr.ua.members);
447 /* manipulate private fields of struct */
449 int get_union_n_types (type *uni) {
450 assert(uni && (uni->type_op == type_union));
451 return uni->attr.ua.n_types;
453 type *get_union_unioned_type (type *uni, int pos) {
454 assert(uni && (uni->type_op == type_union));
455 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
457 void set_union_unioned_type (type *uni, int pos, type *type) {
458 assert(uni && (uni->type_op == type_union));
459 uni->attr.ua.unioned_type[pos] = type;
461 ident *get_union_delim_nameid (type *uni, int pos) {
462 assert(uni && (uni->type_op == type_union));
463 return uni->attr.ua.delim_names[pos];
465 const char *get_union_delim_name (type *uni, int pos) {
466 assert(uni && (uni->type_op == type_union));
467 return id_to_str(uni->attr.ua.delim_names[pos]);
469 void set_union_delim_nameid (type *uni, int pos, ident *id) {
470 assert(uni && (uni->type_op == type_union));
471 uni->attr.ua.delim_names[pos] = id;
474 int get_union_n_members (type *uni) {
475 assert(uni && (uni->type_op == type_union));
476 return (ARR_LEN (uni->attr.ua.members))-1;
478 void add_union_member (type *uni, entity *member) {
479 assert(uni && (uni->type_op == type_union));
480 ARR_APP1 (entity *, uni->attr.ua.members, member);
482 entity *get_union_member (type *uni, int pos) {
483 assert(uni && (uni->type_op == type_union));
484 return uni->attr.ua.members[pos+1];
486 void set_union_member (type *uni, int pos, entity *member) {
487 assert(uni && (uni->type_op == type_union));
488 uni->attr.ua.members[pos+1] = member;
490 void remove_union_member(type *uni, entity *member) {
492 assert(uni && (uni->type_op == type_union));
493 for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
494 if (uni->attr.ua.members[i+1] == member) {
495 for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
496 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
497 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
503 bool is_union_type (type *uni) {
505 if (uni->type_op == type_union) return 1; else return 0;
508 /*******************************************************************/
510 /*******************************************************************/
513 /* create a new type array -- set dimension sizes independently */
514 type *new_type_array (ident *name, int n_dimensions,
515 type *element_type) {
517 res = new_type(type_array, NULL, name);
518 res->attr.aa.n_dimensions = n_dimensions;
519 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
520 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
521 res->attr.aa.element_type = element_type;
522 new_entity(res, mangle(name, id_from_str("elem_ent", 8)), element_type);
525 inline void free_array_attrs (type *array) {
526 assert(array && (array->type_op == type_array));
527 free(array->attr.aa.lower_bound);
528 free(array->attr.aa.upper_bound);
531 /* manipulate private fields of array type */
532 int get_array_n_dimensions (type *array) {
533 assert(array && (array->type_op == type_array));
534 return array->attr.aa.n_dimensions;
536 void set_array_bounds_int (type *array, int dimension, int lower_bound,
539 assert(array && (array->type_op == type_array));
540 rem = current_ir_graph;
541 current_ir_graph = get_const_code_irg();
542 array->attr.aa.lower_bound[dimension] =
543 new_Const(mode_I, tarval_from_long (mode_I, lower_bound));
544 array->attr.aa.upper_bound[dimension] =
545 new_Const(mode_I, tarval_from_long (mode_I, upper_bound));
546 current_ir_graph = rem;
549 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
550 ir_node * upper_bound) {
551 assert(array && (array->type_op == type_array));
552 array->attr.aa.lower_bound[dimension] = lower_bound;
553 array->attr.aa.upper_bound[dimension] = upper_bound;
555 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
556 assert(array && (array->type_op == type_array));
557 array->attr.aa.lower_bound[dimension] = lower_bound;
559 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
560 assert(array && (array->type_op == type_array));
561 array->attr.aa.upper_bound[dimension] = upper_bound;
563 ir_node * get_array_lower_bound (type *array, int dimension) {
564 assert(array && (array->type_op == type_array));
565 return array->attr.aa.lower_bound[dimension];
567 ir_node * get_array_upper_bound (type *array, int dimension) {
568 assert(array && (array->type_op == type_array));
569 return array->attr.aa.upper_bound[dimension];
571 void set_array_element_type (type *array, type *type) {
572 assert(array && (array->type_op == type_array));
573 array->attr.aa.element_type = type;
575 type *get_array_element_type (type *array) {
576 assert(array && (array->type_op == type_array));
577 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
579 void set_array_element_entity (type *array, entity *ent) {
580 assert(array && (array->type_op == type_array));
581 array->attr.aa.element_ent = ent;
583 entity *get_array_element_entity (type *array) {
584 assert(array && (array->type_op == type_array));
585 return array->attr.aa.element_ent;
589 bool is_array_type (type *array) {
591 if (array->type_op == type_array) return 1; else return 0;
594 /*******************************************************************/
595 /** TYPE_ENUMERATION **/
596 /*******************************************************************/
598 /* create a new type enumeration -- set the enumerators independently */
599 type *new_type_enumeration (ident *name, int n_enums) {
601 res = new_type(type_enumeration, NULL, name);
602 res->attr.ea.n_enums = n_enums;
603 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
604 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
607 inline void free_enumeration_attrs(type *enumeration) {
608 assert(enumeration && (enumeration->type_op == type_enumeration));
609 free(enumeration->attr.ea.enumer);
610 free(enumeration->attr.ea.enum_nameid);
613 /* manipulate fields of enumeration type. */
614 int get_enumeration_n_enums (type *enumeration) {
615 assert(enumeration && (enumeration->type_op == type_enumeration));
616 return enumeration->attr.ea.n_enums;
618 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
619 assert(enumeration && (enumeration->type_op == type_enumeration));
620 enumeration->attr.ea.enumer[pos] = con;
622 tarval *get_enumeration_enum (type *enumeration, int pos) {
623 assert(enumeration && (enumeration->type_op == type_enumeration));
624 return enumeration->attr.ea.enumer[pos];
626 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
627 assert(enumeration && (enumeration->type_op == type_enumeration));
628 enumeration->attr.ea.enum_nameid[pos] = id;
630 ident *get_enumeration_nameid (type *enumeration, int pos) {
631 assert(enumeration && (enumeration->type_op == type_enumeration));
632 return enumeration->attr.ea.enum_nameid[pos];
634 const char *get_enumeration_name(type *enumeration, int pos) {
635 assert(enumeration && (enumeration->type_op == type_enumeration));
636 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
640 bool is_enumeration_type (type *enumeration) {
642 if (enumeration->type_op == type_enumeration) return 1; else return 0;
645 /*******************************************************************/
647 /*******************************************************************/
649 /* Create a new type pointer */
650 type *new_type_pointer (ident *name, type *points_to) {
652 res = new_type(type_pointer, mode_p, name);
653 res->attr.pa.points_to = points_to;
654 res->size = get_mode_size(res->mode);
655 res->state = layout_fixed;
658 inline void free_pointer_attrs (type *pointer) {
659 assert(pointer && (pointer->type_op == type_pointer));
661 /* manipulate fields of type_pointer */
662 void set_pointer_points_to_type (type *pointer, type *type) {
663 assert(pointer && (pointer->type_op == type_pointer));
664 pointer->attr.pa.points_to = type;
666 type *get_pointer_points_to_type (type *pointer) {
667 assert(pointer && (pointer->type_op == type_pointer));
668 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
672 bool is_pointer_type (type *pointer) {
674 if (pointer->type_op == type_pointer) return 1; else return 0;
678 /*******************************************************************/
679 /** TYPE_PRIMITIVE **/
680 /*******************************************************************/
682 /* create a new type primitive */
683 type *new_type_primitive (ident *name, ir_mode *mode) {
685 res = new_type(type_primitive, mode, name);
686 res->size = get_mode_size(mode);
687 res->state = layout_fixed;
690 inline void free_primitive_attrs (type *primitive) {
691 assert(primitive && (primitive->type_op == type_primitive));
695 bool is_primitive_type (type *primitive) {
697 if (primitive->type_op == type_primitive) return 1; else return 0;
700 int is_atomic_type(type *tp) {
701 return (is_primitive_type(tp) || is_pointer_type(tp) ||
702 is_enumeration_type(tp));
704 int is_compound_type(type *tp) {
705 return (is_class_type(tp) || is_struct_type(tp) ||
706 is_array_type(tp) || is_union_type(tp));