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.
35 # include "typegmod_t.h"
38 /*******************************************************************/
40 /*******************************************************************/
42 unsigned long type_visited;
45 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
49 assert(type_op != type_id);
51 node_size = offsetof (type, attr) + type_op->attr_size;
52 res = (type *) xmalloc (node_size);
53 add_irp_type(res); /* Remember the new type global. */
56 res->type_op = type_op;
59 res->state = layout_undefined;
67 void free_type_attrs(type *tp) {
68 switch(get_type_tpop_code(tp)) {
69 case tpo_class: { free_class_attrs(tp); } break;
70 case tpo_struct: { free_struct_attrs(tp); } break;
71 case tpo_method: { free_method_attrs(tp); } break;
72 case tpo_union: { free_union_attrs(tp); } break;
73 case tpo_array: { free_array_attrs(tp); } break;
74 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
75 case tpo_pointer: { free_pointer_attrs(tp); } break;
76 case tpo_primitive: { free_primitive_attrs(tp); } break;
81 /* set/get the link field */
82 void *get_type_link(type *tp)
88 void set_type_link(type *tp, void *l)
94 tp_op* get_type_tpop(type *tp) {
99 ident* get_type_tpop_nameid(type *tp) {
101 return tp->type_op->name;
103 const char* get_type_tpop_name(type *tp) {
105 return id_to_str(tp->type_op->name);
107 tp_opcode get_type_tpop_code(type *tp) {
109 return tp->type_op->code;
111 ir_mode* get_type_mode(type *tp) {
115 void set_type_mode(type *tp, ir_mode* m) {
118 /* For pointer and primitive size depends on the mode. */
119 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
120 tp->size == get_mode_size(m);
122 ident* get_type_nameid(type *tp) {
126 void set_type_nameid(type *tp, ident* id) {
130 const char* get_type_name(type *tp) {
132 return id_to_str(tp->name);
134 int get_type_size(type *tp) {
140 set_type_size(type *tp, int size) {
142 /* For pointer and primitive size depends on the mode. */
143 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
148 get_type_state(type *tp) {
154 set_type_state(type *tp, type_state state) {
156 /* For pointer and primitive always fixed. */
157 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
161 unsigned long get_type_visited(type *tp) {
166 void set_type_visited(type *tp, unsigned long num) {
170 /* Sets visited field in type to type_visited. */
171 void mark_type_visited(type *tp) {
173 assert(tp->visit < type_visited);
174 tp->visit = type_visited;
177 int is_type (void *thing) {
179 if (get_kind(thing) == k_type)
185 /*******************************************************************/
187 /*******************************************************************/
189 /* create a new class type */
190 type *new_type_class (ident *name) {
193 res = new_type(type_class, NULL, name);
195 res->attr.ca.members = NEW_ARR_F (entity *, 1);
196 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
197 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
201 inline void free_class_attrs(type *clss) {
202 assert(clss && (clss->type_op == type_class));
203 DEL_ARR_F(clss->attr.ca.members);
204 DEL_ARR_F(clss->attr.ca.subtypes);
205 DEL_ARR_F(clss->attr.ca.supertypes);
207 /* manipulate private fields of class type */
208 void add_class_member (type *clss, entity *member) {
209 assert(clss && (clss->type_op == type_class));
210 ARR_APP1 (entity *, clss->attr.ca.members, member);
212 int get_class_n_member (type *clss) {
213 assert(clss && (clss->type_op == type_class));
214 return (ARR_LEN (clss->attr.ca.members))-1;
216 entity *get_class_member (type *clss, int pos) {
217 assert(clss && (clss->type_op == type_class));
218 return clss->attr.ca.members[pos+1];
220 void set_class_member (type *clss, entity *member, int pos) {
221 assert(clss && (clss->type_op == type_class));
222 clss->attr.ca.members[pos+1] = member;
224 void remove_class_member(type *clss, entity *member) {
226 assert(clss && (clss->type_op == type_class));
227 for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
228 if (clss->attr.ca.members[i+1] == member) {
229 for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
230 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
231 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
236 void add_class_subtype (type *clss, type *subtype) {
237 assert(clss && (clss->type_op == type_class));
238 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
239 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
241 int get_class_n_subtype (type *clss) {
242 assert(clss && (clss->type_op == type_class));
243 return (ARR_LEN (clss->attr.ca.subtypes))-1;
245 type *get_class_subtype (type *clss, int pos) {
246 assert(clss && (clss->type_op == type_class));
247 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
249 void set_class_subtype (type *clss, type *subtype, int pos) {
250 assert(clss && (clss->type_op == type_class));
251 clss->attr.ca.subtypes[pos+1] = subtype;
253 void remove_class_subtype(type *clss, type *subtype) {
255 assert(clss && (clss->type_op == type_class));
256 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
257 if (clss->attr.ca.subtypes[i+1] == subtype) {
258 for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
259 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
260 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
265 void add_class_supertype (type *clss, type *supertype) {
266 assert(clss && (clss->type_op == type_class));
267 assert(supertype && (supertype -> type_op == type_class));
268 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
269 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
271 int get_class_n_supertype (type *clss) {
272 assert(clss && (clss->type_op == type_class));
273 return (ARR_LEN (clss->attr.ca.supertypes))-1;
275 type *get_class_supertype (type *clss, int pos) {
276 assert(clss && (clss->type_op == type_class));
277 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
279 void set_class_supertype (type *clss, type *supertype, int pos) {
280 assert(clss && (clss->type_op == type_class));
281 clss->attr.ca.supertypes[pos+1] = supertype;
283 void remove_class_supertype(type *clss, type *supertype) {
285 assert(clss && (clss->type_op == type_class));
286 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
287 if (clss->attr.ca.supertypes[i+1] == supertype) {
288 for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
289 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
290 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
295 bool is_class_type(type *clss) {
297 if (clss->type_op == type_class) return 1; else return 0;
300 /*******************************************************************/
302 /*******************************************************************/
304 /* create a new type struct */
305 type *new_type_struct (ident *name) {
307 res = new_type(type_struct, NULL, name);
308 res->attr.sa.members = NEW_ARR_F (entity *, 1);
311 inline void free_struct_attrs (type *strct) {
312 assert(strct && (strct->type_op == type_struct));
313 DEL_ARR_F(strct->attr.sa.members);
315 /* manipulate private fields of struct */
316 void add_struct_member (type *strct, entity *member) {
317 assert(strct && (strct->type_op == type_struct));
318 ARR_APP1 (entity *, strct->attr.sa.members, member);
320 int get_struct_n_member (type *strct) {
321 assert(strct && (strct->type_op == type_struct));
322 return (ARR_LEN (strct->attr.sa.members))-1;
324 entity *get_struct_member (type *strct, int pos) {
325 assert(strct && (strct->type_op == type_struct));
326 return strct->attr.sa.members[pos+1];
328 void set_struct_member (type *strct, int pos, entity *member) {
329 assert(strct && (strct->type_op == type_struct));
330 strct->attr.sa.members[pos+1] = member;
332 void remove_struct_member(type *strct, entity *member) {
334 assert(strct && (strct->type_op == type_struct));
335 for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
336 if (strct->attr.sa.members[i+1] == member) {
337 for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
338 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
339 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
344 bool is_struct_type(type *strct) {
346 if (strct->type_op == type_struct) return 1; else return 0;
349 /*******************************************************************/
351 /*******************************************************************/
353 /* Create a new method type.
354 N_param is the number of parameters, n_res the number of results. */
355 type *new_type_method (ident *name, int n_param, int n_res) {
357 res = new_type(type_method, NULL, name);
358 res->attr.ma.n_params = n_param;
359 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
360 res->attr.ma.n_res = n_res;
361 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
364 inline void free_method_attrs(type *method) {
365 assert(method && (method->type_op == type_method));
366 free(method->attr.ma.param_type);
367 free(method->attr.ma.res_type);
369 /* manipulate private fields of method. */
370 int get_method_n_params (type *method) {
371 assert(method && (method->type_op == type_method));
372 return method->attr.ma.n_params;
374 type *get_method_param_type(type *method, int pos) {
375 assert(method && (method->type_op == type_method));
376 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
378 void set_method_param_type(type *method, int pos, type* type) {
379 assert(method && (method->type_op == type_method));
380 method->attr.ma.param_type[pos] = type;
383 int get_method_n_res (type *method) {
384 assert(method && (method->type_op == type_method));
385 return method->attr.ma.n_res;
387 type *get_method_res_type(type *method, int pos) {
388 assert(method && (method->type_op == type_method));
389 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
391 void set_method_res_type(type *method, int pos, type* type) {
392 assert(method && (method->type_op == type_method));
393 method->attr.ma.res_type[pos] = type;
397 bool is_method_type (type *method) {
399 if (method->type_op == type_method) return 1; else return 0;
403 /*******************************************************************/
405 /*******************************************************************/
407 /* create a new type uni */
408 type *new_type_uni (ident *name) {
410 res = new_type(type_union, NULL, name);
411 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
412 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
413 res->attr.ua.members = NEW_ARR_F (entity *, 1);
416 inline void free_union_attrs (type *uni) {
417 assert(uni && (uni->type_op == type_union));
418 DEL_ARR_F(uni->attr.ua.members);
420 /* manipulate private fields of struct */
422 int get_union_n_types (type *uni) {
423 assert(uni && (uni->type_op == type_union));
424 return uni->attr.ua.n_types;
426 type *get_union_unioned_type (type *uni, int pos) {
427 assert(uni && (uni->type_op == type_union));
428 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
430 void set_union_unioned_type (type *uni, int pos, type *type) {
431 assert(uni && (uni->type_op == type_union));
432 uni->attr.ua.unioned_type[pos] = type;
434 ident *get_union_delim_nameid (type *uni, int pos) {
435 assert(uni && (uni->type_op == type_union));
436 return uni->attr.ua.delim_names[pos];
438 const char *get_union_delim_name (type *uni, int pos) {
439 assert(uni && (uni->type_op == type_union));
440 return id_to_str(uni->attr.ua.delim_names[pos]);
442 void set_union_delim_nameid (type *uni, int pos, ident *id) {
443 assert(uni && (uni->type_op == type_union));
444 uni->attr.ua.delim_names[pos] = id;
447 int get_union_n_members (type *uni) {
448 assert(uni && (uni->type_op == type_union));
449 return (ARR_LEN (uni->attr.ua.members))-1;
451 void add_union_member (type *uni, entity *member) {
452 assert(uni && (uni->type_op == type_union));
453 ARR_APP1 (entity *, uni->attr.ua.members, member);
455 entity *get_union_member (type *uni, int pos) {
456 assert(uni && (uni->type_op == type_union));
457 return uni->attr.ua.members[pos+1];
459 void set_union_member (type *uni, int pos, entity *member) {
460 assert(uni && (uni->type_op == type_union));
461 uni->attr.ua.members[pos+1] = member;
463 void remove_union_member(type *uni, entity *member) {
465 assert(uni && (uni->type_op == type_union));
466 for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
467 if (uni->attr.ua.members[i+1] == member) {
468 for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
469 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
470 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
476 bool is_union_type (type *uni) {
478 if (uni->type_op == type_union) return 1; else return 0;
481 /*******************************************************************/
483 /*******************************************************************/
486 /* create a new type array -- set dimension sizes independently */
487 type *new_type_array (ident *name, int n_dimensions,
488 type *element_type) {
490 res = new_type(type_array, NULL, name);
491 res->attr.aa.n_dimensions = n_dimensions;
492 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
493 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
494 res->attr.aa.element_type = element_type;
495 new_entity(res, name, element_type);
498 inline void free_array_attrs (type *array) {
499 assert(array && (array->type_op == type_array));
500 free(array->attr.aa.lower_bound);
501 free(array->attr.aa.upper_bound);
504 /* manipulate private fields of array type */
505 int get_array_n_dimensions (type *array) {
506 assert(array && (array->type_op == type_array));
507 return array->attr.aa.n_dimensions;
509 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
510 ir_node * upper_bound) {
511 assert(array && (array->type_op == type_array));
512 array->attr.aa.lower_bound[dimension] = lower_bound;
513 array->attr.aa.upper_bound[dimension] = upper_bound;
515 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
516 assert(array && (array->type_op == type_array));
517 array->attr.aa.lower_bound[dimension] = lower_bound;
519 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
520 assert(array && (array->type_op == type_array));
521 array->attr.aa.upper_bound[dimension] = upper_bound;
523 ir_node * get_array_lower_bound (type *array, int dimension) {
524 assert(array && (array->type_op == type_array));
525 return array->attr.aa.lower_bound[dimension];
527 ir_node * get_array_upper_bound (type *array, int dimension) {
528 assert(array && (array->type_op == type_array));
529 return array->attr.aa.upper_bound[dimension];
531 void set_array_element_type (type *array, type *type) {
532 assert(array && (array->type_op == type_array));
533 array->attr.aa.element_type = type;
535 type *get_array_element_type (type *array) {
536 assert(array && (array->type_op == type_array));
537 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
539 void set_array_element_entity (type *array, entity *ent) {
540 assert(array && (array->type_op == type_array));
541 array->attr.aa.element_ent = ent;
543 entity *get_array_element_entity (type *array) {
544 assert(array && (array->type_op == type_array));
545 return array->attr.aa.element_ent;
549 bool is_array_type (type *array) {
551 if (array->type_op == type_array) return 1; else return 0;
554 /*******************************************************************/
555 /** TYPE_ENUMERATION **/
556 /*******************************************************************/
558 /* create a new type enumeration -- set the enumerators independently */
559 type *new_type_enumeration (ident *name, int n_enums) {
561 res = new_type(type_enumeration, NULL, name);
562 res->attr.ea.n_enums = n_enums;
563 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
564 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
567 inline void free_enumeration_attrs(type *enumeration) {
568 assert(enumeration && (enumeration->type_op == type_enumeration));
569 free(enumeration->attr.ea.enumer);
570 free(enumeration->attr.ea.enum_nameid);
573 /* manipulate fields of enumeration type. */
574 int get_enumeration_n_enums (type *enumeration) {
575 assert(enumeration && (enumeration->type_op == type_enumeration));
576 return enumeration->attr.ea.n_enums;
578 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
579 assert(enumeration && (enumeration->type_op == type_enumeration));
580 enumeration->attr.ea.enumer[pos] = con;
582 tarval *get_enumeration_enum (type *enumeration, int pos) {
583 assert(enumeration && (enumeration->type_op == type_enumeration));
584 return enumeration->attr.ea.enumer[pos];
586 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
587 assert(enumeration && (enumeration->type_op == type_enumeration));
588 enumeration->attr.ea.enum_nameid[pos] = id;
590 ident *get_enumeration_nameid (type *enumeration, int pos) {
591 assert(enumeration && (enumeration->type_op == type_enumeration));
592 return enumeration->attr.ea.enum_nameid[pos];
594 const char *get_enumeration_name(type *enumeration, int pos) {
595 assert(enumeration && (enumeration->type_op == type_enumeration));
596 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
600 bool is_enumeration_type (type *enumeration) {
602 if (enumeration->type_op == type_enumeration) return 1; else return 0;
605 /*******************************************************************/
607 /*******************************************************************/
609 /* Create a new type pointer */
610 type *new_type_pointer (ident *name, type *points_to) {
612 res = new_type(type_pointer, mode_p, name);
613 res->attr.pa.points_to = points_to;
614 res->size = get_mode_size(res->mode);
615 res->state = layout_fixed;
618 inline void free_pointer_attrs (type *pointer) {
619 assert(pointer && (pointer->type_op == type_pointer));
621 /* manipulate fields of type_pointer */
622 void set_pointer_points_to_type (type *pointer, type *type) {
623 assert(pointer && (pointer->type_op == type_pointer));
624 pointer->attr.pa.points_to = type;
626 type *get_pointer_points_to_type (type *pointer) {
627 assert(pointer && (pointer->type_op == type_pointer));
628 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
632 bool is_pointer_type (type *pointer) {
634 if (pointer->type_op == type_pointer) return 1; else return 0;
638 /*******************************************************************/
639 /** TYPE_PRIMITIVE **/
640 /*******************************************************************/
642 /* create a new type primitive */
643 type *new_type_primitive (ident *name, ir_mode *mode) {
645 res = new_type(type_primitive, mode, name);
646 res->size = get_mode_size(mode);
647 res->state = layout_fixed;
650 inline void free_primitive_attrs (type *primitive) {
651 assert(primitive && (primitive->type_op == type_primitive));
655 bool is_primitive_type (type *primitive) {
657 if (primitive->type_op == type_primitive) return 1; else return 0;