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;
66 tp_op* get_type_tpop(type *tp) {
71 ident* get_type_tpop_nameid(type *tp) {
73 return tp->type_op->name;
75 const char* get_type_tpop_name(type *tp) {
77 return id_to_str(tp->type_op->name);
79 tp_opcode get_type_tpop_code(type *tp) {
81 return tp->type_op->code;
83 ir_mode* get_type_mode(type *tp) {
87 void set_type_mode(type *tp, ir_mode* m) {
90 /* For pointer and primitive size depends on the mode. */
91 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
92 tp->size == get_mode_size(m);
94 ident* get_type_nameid(type *tp) {
98 void set_type_nameid(type *tp, ident* id) {
102 const char* get_type_name(type *tp) {
104 return id_to_str(tp->name);
106 int get_type_size(type *tp) {
112 set_type_size(type *tp, int size) {
114 /* For pointer and primitive size depends on the mode. */
115 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
120 get_type_state(type *tp) {
126 set_type_state(type *tp, type_state state) {
128 /* For pointer and primitive always fixed. */
129 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
133 unsigned long get_type_visited(type *tp) {
138 void set_type_visited(type *tp, unsigned long num) {
142 /* Sets visited field in type to type_visited. */
143 void mark_type_visited(type *tp) {
145 assert(tp->visit < type_visited);
146 tp->visit = type_visited;
149 int is_type (void *thing) {
151 if (get_kind(thing) == k_type)
157 /*******************************************************************/
159 /*******************************************************************/
161 /* create a new class type */
162 type *new_type_class (ident *name) {
165 res = new_type(type_class, NULL, name);
167 res->attr.ca.members = NEW_ARR_F (entity *, 1);
168 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
169 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
173 /* manipulate private fields of class type */
174 void add_class_member (type *clss, entity *member) {
175 assert(clss && (clss->type_op == type_class));
176 ARR_APP1 (entity *, clss->attr.ca.members, member);
178 int get_class_n_member (type *clss) {
179 assert(clss && (clss->type_op == type_class));
180 return (ARR_LEN (clss->attr.ca.members))-1;
182 entity *get_class_member (type *clss, int pos) {
183 assert(clss && (clss->type_op == type_class));
184 return clss->attr.ca.members[pos+1];
186 void set_class_member (type *clss, entity *member, int pos) {
187 assert(clss && (clss->type_op == type_class));
188 clss->attr.ca.members[pos+1] = member;
190 void remove_class_member(type *clss, entity *member) {
192 assert(clss && (clss->type_op == type_class));
193 for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
194 if (clss->attr.ca.members[i+1] == member) {
195 for(i++; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
196 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
197 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
202 void add_class_subtype (type *clss, type *subtype) {
203 assert(clss && (clss->type_op == type_class));
204 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
205 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
207 int get_class_n_subtype (type *clss) {
208 assert(clss && (clss->type_op == type_class));
209 return (ARR_LEN (clss->attr.ca.subtypes))-1;
211 type *get_class_subtype (type *clss, int pos) {
212 assert(clss && (clss->type_op == type_class));
213 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
215 void set_class_subtype (type *clss, type *subtype, int pos) {
216 assert(clss && (clss->type_op == type_class));
217 clss->attr.ca.subtypes[pos+1] = subtype;
219 void remove_class_subtype(type *clss, type *subtype) {
221 assert(clss && (clss->type_op == type_class));
222 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
223 if (clss->attr.ca.subtypes[i+1] == subtype) {
224 for(i++; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
225 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
226 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
231 void add_class_supertype (type *clss, type *supertype) {
232 assert(clss && (clss->type_op == type_class));
233 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
234 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
236 int get_class_n_supertype (type *clss) {
237 assert(clss && (clss->type_op == type_class));
238 return (ARR_LEN (clss->attr.ca.supertypes))-1;
240 type *get_class_supertype (type *clss, int pos) {
241 assert(clss && (clss->type_op == type_class));
242 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
244 void set_class_supertype (type *clss, type *supertype, int pos) {
245 assert(clss && (clss->type_op == type_class));
246 clss->attr.ca.supertypes[pos+1] = supertype;
248 void remove_class_supertype(type *clss, type *supertype) {
250 assert(clss && (clss->type_op == type_class));
251 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
252 if (clss->attr.ca.supertypes[i+1] == supertype) {
253 for(i++; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
254 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
255 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
260 bool is_class_type(type *clss) {
262 if (clss->type_op == type_class) return 1; else return 0;
265 /*******************************************************************/
267 /*******************************************************************/
269 /* create a new type struct */
270 type *new_type_struct (ident *name) {
272 res = new_type(type_struct, NULL, name);
273 res->attr.sa.members = NEW_ARR_F (entity *, 1);
276 /* manipulate private fields of struct */
277 void add_struct_member (type *strct, entity *member) {
278 assert(strct && (strct->type_op == type_struct));
279 ARR_APP1 (entity *, strct->attr.sa.members, member);
281 int get_struct_n_member (type *strct) {
282 assert(strct && (strct->type_op == type_struct));
283 return (ARR_LEN (strct->attr.sa.members))-1;
285 entity *get_struct_member (type *strct, int pos) {
286 assert(strct && (strct->type_op == type_struct));
287 return strct->attr.sa.members[pos+1];
289 void set_struct_member (type *strct, int pos, entity *member) {
290 assert(strct && (strct->type_op == type_struct));
291 strct->attr.sa.members[pos+1] = member;
293 void remove_struct_member(type *strct, entity *member) {
295 assert(strct && (strct->type_op == type_struct));
296 for (i = 1; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
297 if (strct->attr.sa.members[i+1] == member) {
298 for(i++; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
299 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
300 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
305 bool is_struct_type(type *strct) {
307 if (strct->type_op == type_struct) return 1; else return 0;
310 /*******************************************************************/
312 /*******************************************************************/
314 /* Create a new method type.
315 N_param is the number of parameters, n_res the number of results. */
316 type *new_type_method (ident *name, int n_param, int n_res) {
318 res = new_type(type_method, NULL, name);
319 res->attr.ma.n_params = n_param;
320 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
321 res->attr.ma.n_res = n_res;
322 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
326 /* manipulate private fields of method. */
327 int get_method_n_params (type *method) {
328 assert(method && (method->type_op == type_method));
329 return method->attr.ma.n_params;
331 type *get_method_param_type(type *method, int pos) {
332 assert(method && (method->type_op == type_method));
333 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
335 void set_method_param_type(type *method, int pos, type* type) {
336 assert(method && (method->type_op == type_method));
337 method->attr.ma.param_type[pos] = type;
340 int get_method_n_res (type *method) {
341 assert(method && (method->type_op == type_method));
342 return method->attr.ma.n_res;
344 type *get_method_res_type(type *method, int pos) {
345 assert(method && (method->type_op == type_method));
346 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
348 void set_method_res_type(type *method, int pos, type* type) {
349 assert(method && (method->type_op == type_method));
350 method->attr.ma.res_type[pos] = type;
354 bool is_method_type (type *method) {
356 if (method->type_op == type_method) return 1; else return 0;
360 /*******************************************************************/
362 /*******************************************************************/
364 /* create a new type uni */
365 type *new_type_uni (ident *name) {
367 res = new_type(type_union, NULL, name);
368 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
369 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
370 res->attr.ua.members = NEW_ARR_F (entity *, 1);
373 /* manipulate private fields of struct */
375 int get_union_n_types (type *uni) {
376 assert(uni && (uni->type_op == type_union));
377 return uni->attr.ua.n_types;
379 type *get_union_unioned_type (type *uni, int pos) {
380 assert(uni && (uni->type_op == type_union));
381 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
383 void set_union_unioned_type (type *uni, int pos, type *type) {
384 assert(uni && (uni->type_op == type_union));
385 uni->attr.ua.unioned_type[pos] = type;
387 ident *get_union_delim_nameid (type *uni, int pos) {
388 assert(uni && (uni->type_op == type_union));
389 return uni->attr.ua.delim_names[pos];
391 const char *get_union_delim_name (type *uni, int pos) {
392 assert(uni && (uni->type_op == type_union));
393 return id_to_str(uni->attr.ua.delim_names[pos]);
395 void set_union_delim_nameid (type *uni, int pos, ident *id) {
396 assert(uni && (uni->type_op == type_union));
397 uni->attr.ua.delim_names[pos] = id;
400 int get_union_n_members (type *uni) {
401 assert(uni && (uni->type_op == type_union));
402 return (ARR_LEN (uni->attr.ua.members))-1;
404 void add_union_member (type *uni, entity *member) {
405 assert(uni && (uni->type_op == type_union));
406 ARR_APP1 (entity *, uni->attr.ua.members, member);
408 entity *get_union_member (type *uni, int pos) {
409 assert(uni && (uni->type_op == type_union));
410 return uni->attr.ua.members[pos+1];
412 void set_union_member (type *uni, int pos, entity *member) {
413 assert(uni && (uni->type_op == type_union));
414 uni->attr.ua.members[pos+1] = member;
416 void remove_union_member(type *uni, entity *member) {
418 assert(uni && (uni->type_op == type_union));
419 for (i = 1; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
420 if (uni->attr.ua.members[i+1] == member) {
421 for(i++; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
422 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
423 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
429 bool is_union_type (type *uni) {
431 if (uni->type_op == type_union) return 1; else return 0;
434 /*******************************************************************/
436 /*******************************************************************/
439 /* create a new type array -- set dimension sizes independently */
440 type *new_type_array (ident *name, int n_dimensions,
441 type *element_type) {
443 res = new_type(type_array, NULL, name);
444 res->attr.aa.n_dimensions = n_dimensions;
445 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
446 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
447 res->attr.aa.element_type = element_type;
448 new_entity(res, name, element_type);
452 /* manipulate private fields of array type */
453 int get_array_n_dimensions (type *array) {
454 assert(array && (array->type_op == type_array));
455 return array->attr.aa.n_dimensions;
457 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
458 ir_node * upper_bound) {
459 assert(array && (array->type_op == type_array));
460 array->attr.aa.lower_bound[dimension] = lower_bound;
461 array->attr.aa.upper_bound[dimension] = upper_bound;
463 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
464 assert(array && (array->type_op == type_array));
465 array->attr.aa.lower_bound[dimension] = lower_bound;
467 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
468 assert(array && (array->type_op == type_array));
469 array->attr.aa.upper_bound[dimension] = upper_bound;
471 ir_node * get_array_lower_bound (type *array, int dimension) {
472 assert(array && (array->type_op == type_array));
473 return array->attr.aa.lower_bound[dimension];
475 ir_node * get_array_upper_bound (type *array, int dimension) {
476 assert(array && (array->type_op == type_array));
477 return array->attr.aa.upper_bound[dimension];
479 void set_array_element_type (type *array, type *type) {
480 assert(array && (array->type_op == type_array));
481 array->attr.aa.element_type = type;
483 type *get_array_element_type (type *array) {
484 assert(array && (array->type_op == type_array));
485 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
487 void set_array_element_entity (type *array, entity *ent) {
488 assert(array && (array->type_op == type_array));
489 array->attr.aa.element_ent = ent;
491 entity *get_array_element_entity (type *array) {
492 assert(array && (array->type_op == type_array));
493 return array->attr.aa.element_ent;
497 bool is_array_type (type *array) {
499 if (array->type_op == type_array) return 1; else return 0;
502 /*******************************************************************/
503 /** TYPE_ENUMERATION **/
504 /*******************************************************************/
506 /* create a new type enumeration -- set the enumerators independently */
507 type *new_type_enumeration (ident *name, int n_enums) {
509 res = new_type(type_enumeration, NULL, name);
510 res->attr.ea.n_enums = n_enums;
511 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
512 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
516 /* manipulate fields of enumeration type. */
517 int get_enumeration_n_enums (type *enumeration) {
518 assert(enumeration && (enumeration->type_op == type_enumeration));
519 return enumeration->attr.ea.n_enums;
521 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
522 assert(enumeration && (enumeration->type_op == type_enumeration));
523 enumeration->attr.ea.enumer[pos] = con;
525 tarval *get_enumeration_enum (type *enumeration, int pos) {
526 assert(enumeration && (enumeration->type_op == type_enumeration));
527 return enumeration->attr.ea.enumer[pos];
529 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
530 assert(enumeration && (enumeration->type_op == type_enumeration));
531 enumeration->attr.ea.enum_nameid[pos] = id;
533 ident *get_enumeration_nameid (type *enumeration, int pos) {
534 assert(enumeration && (enumeration->type_op == type_enumeration));
535 return enumeration->attr.ea.enum_nameid[pos];
537 const char *get_enumeration_name(type *enumeration, int pos) {
538 assert(enumeration && (enumeration->type_op == type_enumeration));
539 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
543 bool is_enumeration_type (type *enumeration) {
545 if (enumeration->type_op == type_enumeration) return 1; else return 0;
548 /*******************************************************************/
550 /*******************************************************************/
552 /* Create a new type pointer */
553 type *new_type_pointer (ident *name, type *points_to) {
555 res = new_type(type_pointer, mode_p, name);
556 res->attr.pa.points_to = points_to;
557 res->size = get_mode_size(res->mode);
558 res->state = layout_fixed;
561 /* manipulate fields of type_pointer */
562 void set_pointer_points_to_type (type *pointer, type *type) {
563 assert(pointer && (pointer->type_op == type_pointer));
564 pointer->attr.pa.points_to = type;
566 type *get_pointer_points_to_type (type *pointer) {
567 assert(pointer && (pointer->type_op == type_pointer));
568 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
572 bool is_pointer_type (type *pointer) {
574 if (pointer->type_op == type_pointer) return 1; else return 0;
578 /*******************************************************************/
579 /** TYPE_PRIMITIVE **/
580 /*******************************************************************/
582 /* create a new type primitive */
583 type *new_type_primitive (ident *name, ir_mode *mode) {
585 res = new_type(type_primitive, mode, name);
586 res->size = get_mode_size(mode);
587 res->state = layout_fixed;
592 bool is_primitive_type (type *primitive) {
594 if (primitive->type_op == type_primitive) return 1; else return 0;