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.
37 /*******************************************************************/
39 /*******************************************************************/
41 unsigned long type_visited;
44 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
47 int node_size = offsetof (type, attr) + type_op->attr_size;
48 res = (type *) xmalloc (node_size);
49 add_irp_type(res); /* Remember the new type global. */
52 res->type_op = type_op;
55 res->state = layout_undefined;
62 tp_op* get_type_tpop(type *tp) {
67 ident* get_type_tpop_nameid(type *tp) {
69 return tp->type_op->name;
71 const char* get_type_tpop_name(type *tp) {
73 return id_to_str(tp->type_op->name);
75 tp_opcode get_type_tpop_code(type *tp) {
77 return tp->type_op->code;
79 ir_mode* get_type_mode(type *tp) {
83 void set_type_mode(type *tp, ir_mode* m) {
86 /* For pointer and primitive size depends on the mode. */
87 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
88 tp->size == get_mode_size(m);
90 ident* get_type_nameid(type *tp) {
94 void set_type_nameid(type *tp, ident* id) {
98 const char* get_type_name(type *tp) {
100 return id_to_str(tp->name);
102 int get_type_size(type *tp) {
108 set_type_size(type *tp, int size) {
110 /* For pointer and primitive size depends on the mode. */
111 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
116 get_type_state(type *tp) {
122 set_type_state(type *tp, type_state state) {
124 /* For pointer and primitive always fixed. */
125 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
129 unsigned long get_type_visited(type *tp) {
134 void set_type_visited(type *tp, unsigned long num) {
138 /* Sets visited field in type to type_visited. */
139 void mark_type_visited(type *tp) {
141 assert(tp->visit < type_visited);
142 tp->visit = type_visited;
145 int is_type (void *thing) {
147 if (get_kind(thing) == k_type)
153 /*******************************************************************/
155 /*******************************************************************/
157 /* create a new class type */
158 type *new_type_class (ident *name) {
161 res = new_type(type_class, NULL, name);
163 res->attr.ca.members = NEW_ARR_F (entity *, 1);
164 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
165 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
169 /* manipulate private fields of class type */
170 void add_class_member (type *clss, entity *member) {
171 assert(clss && (clss->type_op == type_class));
172 ARR_APP1 (entity *, clss->attr.ca.members, member);
174 int get_class_n_member (type *clss) {
175 assert(clss && (clss->type_op == type_class));
176 return (ARR_LEN (clss->attr.ca.members))-1;
178 entity *get_class_member (type *clss, int pos) {
179 assert(clss && (clss->type_op == type_class));
180 return clss->attr.ca.members[pos+1];
182 void set_class_member (type *clss, entity *member, int pos) {
183 assert(clss && (clss->type_op == type_class));
184 clss->attr.ca.members[pos+1] = member;
187 void add_class_subtype (type *clss, type *subtype) {
188 assert(clss && (clss->type_op == type_class));
189 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
191 int get_class_n_subtype (type *clss) {
192 assert(clss && (clss->type_op == type_class));
193 return (ARR_LEN (clss->attr.ca.subtypes))-1;
195 type *get_class_subtype (type *clss, int pos) {
196 assert(clss && (clss->type_op == type_class));
197 return clss->attr.ca.subtypes[pos+1];
199 void set_class_subtype (type *clss, type *subtype, int pos) {
200 assert(clss && (clss->type_op == type_class));
201 clss->attr.ca.subtypes[pos+1] = subtype;
204 void add_class_supertype (type *clss, type *supertype) {
205 assert(clss && (clss->type_op == type_class));
206 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
208 int get_class_n_supertype (type *clss) {
209 assert(clss && (clss->type_op == type_class));
210 return (ARR_LEN (clss->attr.ca.supertypes))-1;
212 type *get_class_supertype (type *clss, int pos) {
213 assert(clss && (clss->type_op == type_class));
214 return clss->attr.ca.supertypes[pos+1];
216 void set_class_supertype (type *clss, type *supertype, int pos) {
217 assert(clss && (clss->type_op == type_class));
218 clss->attr.ca.supertypes[pos+1] = supertype;
221 bool is_class_type(type *clss) {
223 if (clss->type_op == type_class) return 1; else return 0;
226 /*******************************************************************/
228 /*******************************************************************/
230 /* create a new type struct */
231 type *new_type_struct (ident *name) {
233 res = new_type(type_struct, NULL, name);
234 res->attr.sa.members = NEW_ARR_F (entity *, 1);
237 /* manipulate private fields of struct */
238 void add_struct_member (type *strct, entity *member) {
239 assert(strct && (strct->type_op == type_struct));
240 ARR_APP1 (entity *, strct->attr.sa.members, member);
242 int get_struct_n_member (type *strct) {
243 assert(strct && (strct->type_op == type_struct));
244 return (ARR_LEN (strct->attr.sa.members))-1;
246 entity *get_struct_member (type *strct, int pos) {
247 assert(strct && (strct->type_op == type_struct));
248 return strct->attr.sa.members[pos+1];
250 void set_struct_member (type *strct, int pos, entity *member) {
251 assert(strct && (strct->type_op == type_struct));
252 strct->attr.sa.members[pos+1] = member;
255 bool is_struct_type(type *strct) {
257 if (strct->type_op == type_struct) return 1; else return 0;
260 /*******************************************************************/
262 /*******************************************************************/
264 /* Create a new method type.
265 N_param is the number of parameters, n_res the number of results. */
266 type *new_type_method (ident *name, int n_param, int n_res) {
268 res = new_type(type_method, NULL, name);
269 res->attr.ma.n_params = n_param;
270 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
271 res->attr.ma.n_res = n_res;
272 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
276 /* manipulate private fields of method. */
277 int get_method_n_params (type *method) {
278 assert(method && (method->type_op == type_method));
279 return method->attr.ma.n_params;
281 type *get_method_param_type(type *method, int pos) {
282 assert(method && (method->type_op == type_method));
283 return method->attr.ma.param_type[pos];
285 void set_method_param_type(type *method, int pos, type* type) {
286 assert(method && (method->type_op == type_method));
287 method->attr.ma.param_type[pos] = type;
290 int get_method_n_res (type *method) {
291 assert(method && (method->type_op == type_method));
292 return method->attr.ma.n_res;
294 type *get_method_res_type(type *method, int pos) {
295 assert(method && (method->type_op == type_method));
296 return method->attr.ma.res_type[pos];
298 void set_method_res_type(type *method, int pos, type* type) {
299 assert(method && (method->type_op == type_method));
300 method->attr.ma.res_type[pos] = type;
304 bool is_method_type (type *method) {
306 if (method->type_op == type_method) return 1; else return 0;
310 /*******************************************************************/
312 /*******************************************************************/
314 /* create a new type uni */
315 type *new_type_uni (ident *name) {
317 res = new_type(type_union, NULL, name);
318 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
319 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
320 res->attr.ua.members = NEW_ARR_F (entity *, 1);
323 /* manipulate private fields of struct */
325 int get_union_n_types (type *uni) {
326 assert(uni && (uni->type_op == type_union));
327 return uni->attr.ua.n_types;
329 type *get_union_unioned_type (type *uni, int pos) {
330 assert(uni && (uni->type_op == type_union));
331 return uni->attr.ua.unioned_type[pos];
333 void set_union_unioned_type (type *uni, int pos, type *type) {
334 assert(uni && (uni->type_op == type_union));
335 uni->attr.ua.unioned_type[pos] = type;
337 ident *get_union_delim_nameid (type *uni, int pos) {
338 assert(uni && (uni->type_op == type_union));
339 return uni->attr.ua.delim_names[pos];
341 const char *get_union_delim_name (type *uni, int pos) {
342 assert(uni && (uni->type_op == type_union));
343 return id_to_str(uni->attr.ua.delim_names[pos]);
345 void set_union_delim_nameid (type *uni, int pos, ident *id) {
346 assert(uni && (uni->type_op == type_union));
347 uni->attr.ua.delim_names[pos] = id;
350 int get_union_n_members (type *uni) {
351 assert(uni && (uni->type_op == type_union));
352 return (ARR_LEN (uni->attr.ua.members))-1;
354 void add_union_member (type *uni, entity *member) {
355 assert(uni && (uni->type_op == type_union));
356 ARR_APP1 (entity *, uni->attr.ua.members, member);
358 entity *get_union_member (type *uni, int pos) {
359 assert(uni && (uni->type_op == type_union));
360 return uni->attr.ua.members[pos+1];
362 void set_union_member (type *uni, int pos, entity *member) {
363 assert(uni && (uni->type_op == type_union));
364 uni->attr.ua.members[pos+1] = member;
368 bool is_union_type (type *uni) {
370 if (uni->type_op == type_union) return 1; else return 0;
373 /*******************************************************************/
375 /*******************************************************************/
378 /* create a new type array -- set dimension sizes independently */
379 type *new_type_array (ident *name, int n_dimensions,
380 type *element_type) {
382 res = new_type(type_array, NULL, name);
383 res->attr.aa.n_dimensions = n_dimensions;
384 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
385 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
386 res->attr.aa.element_type = element_type;
387 new_entity(res, name, element_type);
391 /* manipulate private fields of array type */
392 int get_array_n_dimensions (type *array) {
393 assert(array && (array->type_op == type_array));
394 return array->attr.aa.n_dimensions;
396 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
397 ir_node * upper_bound) {
398 assert(array && (array->type_op == type_array));
399 array->attr.aa.lower_bound[dimension] = lower_bound;
400 array->attr.aa.upper_bound[dimension] = upper_bound;
402 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
403 assert(array && (array->type_op == type_array));
404 array->attr.aa.lower_bound[dimension] = lower_bound;
406 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
407 assert(array && (array->type_op == type_array));
408 array->attr.aa.upper_bound[dimension] = upper_bound;
410 ir_node * get_array_lower_bound (type *array, int dimension) {
411 assert(array && (array->type_op == type_array));
412 return array->attr.aa.lower_bound[dimension];
414 ir_node * get_array_upper_bound (type *array, int dimension) {
415 assert(array && (array->type_op == type_array));
416 return array->attr.aa.upper_bound[dimension];
418 void set_array_element_type (type *array, type *type) {
419 assert(array && (array->type_op == type_array));
420 array->attr.aa.element_type = type;
422 type *get_array_element_type (type *array) {
423 assert(array && (array->type_op == type_array));
424 return array->attr.aa.element_type;
426 void set_array_element_entity (type *array, entity *ent) {
427 assert(array && (array->type_op == type_array));
428 array->attr.aa.element_ent = ent;
430 entity *get_array_element_entity (type *array) {
431 assert(array && (array->type_op == type_array));
432 return array->attr.aa.element_ent;
436 bool is_array_type (type *array) {
438 if (array->type_op == type_array) return 1; else return 0;
441 /*******************************************************************/
442 /** TYPE_ENUMERATION **/
443 /*******************************************************************/
445 /* create a new type enumeration -- set the enumerators independently */
446 type *new_type_enumeration (ident *name, int n_enums) {
448 res = new_type(type_enumeration, NULL, name);
449 res->attr.ea.n_enums = n_enums;
450 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
451 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
455 /* manipulate fields of enumeration type. */
456 int get_enumeration_n_enums (type *enumeration) {
457 assert(enumeration && (enumeration->type_op == type_enumeration));
458 return enumeration->attr.ea.n_enums;
460 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
461 assert(enumeration && (enumeration->type_op == type_enumeration));
462 enumeration->attr.ea.enumer[pos] = con;
464 tarval *get_enumeration_enum (type *enumeration, int pos) {
465 assert(enumeration && (enumeration->type_op == type_enumeration));
466 return enumeration->attr.ea.enumer[pos];
468 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
469 assert(enumeration && (enumeration->type_op == type_enumeration));
470 enumeration->attr.ea.enum_nameid[pos] = id;
472 ident *get_enumeration_nameid (type *enumeration, int pos) {
473 assert(enumeration && (enumeration->type_op == type_enumeration));
474 return enumeration->attr.ea.enum_nameid[pos];
476 const char *get_enumeration_name(type *enumeration, int pos) {
477 assert(enumeration && (enumeration->type_op == type_enumeration));
478 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
482 bool is_enumeration_type (type *enumeration) {
484 if (enumeration->type_op == type_enumeration) return 1; else return 0;
487 /*******************************************************************/
489 /*******************************************************************/
491 /* Create a new type pointer */
492 type *new_type_pointer (ident *name, type *points_to) {
494 res = new_type(type_pointer, mode_p, name);
495 res->attr.pa.points_to = points_to;
496 res->size = get_mode_size(res->mode);
497 res->state = layout_fixed;
500 /* manipulate fields of type_pointer */
501 void set_pointer_points_to_type (type *pointer, type *type) {
502 assert(pointer && (pointer->type_op == type_pointer));
503 pointer->attr.pa.points_to = type;
505 type *get_pointer_points_to_type (type *pointer) {
506 assert(pointer && (pointer->type_op == type_pointer));
507 return pointer->attr.pa.points_to;
511 bool is_pointer_type (type *pointer) {
513 if (pointer->type_op == type_pointer) return 1; else return 0;
517 /*******************************************************************/
518 /** TYPE_PRIMITIVE **/
519 /*******************************************************************/
521 /* create a new type primitive */
522 type *new_type_primitive (ident *name, ir_mode *mode) {
524 res = new_type(type_primitive, mode, name);
525 res->size = get_mode_size(mode);
526 res->state = layout_fixed;
531 bool is_primitive_type (type *primitive) {
533 if (primitive->type_op == type_primitive) return 1; else return 0;