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;
61 tp_op* get_type_tpop(type *tp) {
66 ident* get_type_tpop_nameid(type *tp) {
68 return tp->type_op->name;
70 const char* get_type_tpop_name(type *tp) {
72 return id_to_str(tp->type_op->name);
74 tp_opcode get_type_tpop_code(type *tp) {
76 return tp->type_op->code;
78 ir_mode* get_type_mode(type *tp) {
82 void set_type_mode(type *tp, ir_mode* m) {
85 /* For pointer and primitive size depends on the mode. */
86 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive))
87 tp->size == get_mode_size(m);
89 ident* get_type_nameid(type *tp) {
93 void set_type_nameid(type *tp, ident* id) {
97 const char* get_type_name(type *tp) {
99 return id_to_str(tp->name);
101 int get_type_size(type *tp) {
105 void set_type_size(type *tp, int size) {
107 /* For pointer and primitive size depends on the mode. */
108 assert((tp->type_op != type_pointer) && (tp->type_op != type_primitive));
111 unsigned long get_type_visited(type *tp) {
115 void set_type_visited(type *tp, unsigned long num) {
119 /* Sets visited field in type to type_visited. */
120 void mark_type_visited(type *tp) {
122 assert(tp->visit < type_visited);
123 tp->visit = type_visited;
126 int is_type (void *thing) {
128 if (get_kind(thing) == k_type)
134 /*******************************************************************/
136 /*******************************************************************/
138 /* create a new class type */
139 type *new_type_class (ident *name) {
142 res = new_type(type_class, NULL, name);
144 res->attr.ca.members = NEW_ARR_F (entity *, 1);
145 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
146 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
150 /* manipulate private fields of class type */
151 void add_class_member (type *clss, entity *member) {
152 assert(clss && (clss->type_op == type_class));
153 ARR_APP1 (entity *, clss->attr.ca.members, member);
155 int get_class_n_member (type *clss) {
156 assert(clss && (clss->type_op == type_class));
157 return (ARR_LEN (clss->attr.ca.members))-1;
159 entity *get_class_member (type *clss, int pos) {
160 assert(clss && (clss->type_op == type_class));
161 return clss->attr.ca.members[pos+1];
163 void set_class_member (type *clss, entity *member, int pos) {
164 assert(clss && (clss->type_op == type_class));
165 clss->attr.ca.members[pos+1] = member;
168 void add_class_subtype (type *clss, type *subtype) {
169 assert(clss && (clss->type_op == type_class));
170 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
172 int get_class_n_subtype (type *clss) {
173 assert(clss && (clss->type_op == type_class));
174 return (ARR_LEN (clss->attr.ca.subtypes))-1;
176 type *get_class_subtype (type *clss, int pos) {
177 assert(clss && (clss->type_op == type_class));
178 return clss->attr.ca.subtypes[pos+1];
180 void set_class_subtype (type *clss, type *subtype, int pos) {
181 assert(clss && (clss->type_op == type_class));
182 clss->attr.ca.subtypes[pos+1] = subtype;
185 void add_class_supertype (type *clss, type *supertype) {
186 assert(clss && (clss->type_op == type_class));
187 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
189 int get_class_n_supertype (type *clss) {
190 assert(clss && (clss->type_op == type_class));
191 return (ARR_LEN (clss->attr.ca.supertypes))-1;
193 type *get_class_supertype (type *clss, int pos) {
194 assert(clss && (clss->type_op == type_class));
195 return clss->attr.ca.supertypes[pos+1];
197 void set_class_supertype (type *clss, type *supertype, int pos) {
198 assert(clss && (clss->type_op == type_class));
199 clss->attr.ca.supertypes[pos+1] = supertype;
202 bool is_class_type(type *clss) {
204 if (clss->type_op == type_class) return 1; else return 0;
207 /*******************************************************************/
209 /*******************************************************************/
211 /* create a new type struct */
212 type *new_type_struct (ident *name) {
214 res = new_type(type_struct, NULL, name);
215 res->attr.sa.members = NEW_ARR_F (entity *, 1);
218 /* manipulate private fields of struct */
219 void add_struct_member (type *strct, entity *member) {
220 assert(strct && (strct->type_op == type_struct));
221 ARR_APP1 (entity *, strct->attr.sa.members, member);
223 int get_struct_n_member (type *strct) {
224 assert(strct && (strct->type_op == type_struct));
225 return (ARR_LEN (strct->attr.sa.members))-1;
227 entity *get_struct_member (type *strct, int pos) {
228 assert(strct && (strct->type_op == type_struct));
229 return strct->attr.sa.members[pos+1];
231 void set_struct_member (type *strct, int pos, entity *member) {
232 assert(strct && (strct->type_op == type_struct));
233 strct->attr.sa.members[pos+1] = member;
236 bool is_struct_type(type *strct) {
238 if (strct->type_op == type_struct) return 1; else return 0;
241 /*******************************************************************/
243 /*******************************************************************/
245 /* Create a new method type.
246 N_param is the number of parameters, n_res the number of results. */
247 type *new_type_method (ident *name, int n_param, int n_res) {
249 res = new_type(type_method, NULL, name);
250 res->attr.ma.n_params = n_param;
251 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
252 res->attr.ma.n_res = n_res;
253 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
257 /* manipulate private fields of method. */
258 int get_method_n_params (type *method) {
259 assert(method && (method->type_op == type_method));
260 return method->attr.ma.n_params;
262 type *get_method_param_type(type *method, int pos) {
263 assert(method && (method->type_op == type_method));
264 return method->attr.ma.param_type[pos];
266 void set_method_param_type(type *method, int pos, type* type) {
267 assert(method && (method->type_op == type_method));
268 method->attr.ma.param_type[pos] = type;
271 int get_method_n_res (type *method) {
272 assert(method && (method->type_op == type_method));
273 return method->attr.ma.n_res;
275 type *get_method_res_type(type *method, int pos) {
276 assert(method && (method->type_op == type_method));
277 return method->attr.ma.res_type[pos];
279 void set_method_res_type(type *method, int pos, type* type) {
280 assert(method && (method->type_op == type_method));
281 method->attr.ma.res_type[pos] = type;
285 bool is_method_type (type *method) {
287 if (method->type_op == type_method) return 1; else return 0;
291 /*******************************************************************/
293 /*******************************************************************/
295 /* create a new type uni */
296 type *new_type_uni (ident *name) {
298 res = new_type(type_union, NULL, name);
299 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
300 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
301 res->attr.ua.members = NEW_ARR_F (entity *, 1);
304 /* manipulate private fields of struct */
306 int get_union_n_types (type *uni) {
307 assert(uni && (uni->type_op == type_union));
308 return uni->attr.ua.n_types;
310 type *get_union_unioned_type (type *uni, int pos) {
311 assert(uni && (uni->type_op == type_union));
312 return uni->attr.ua.unioned_type[pos];
314 void set_union_unioned_type (type *uni, int pos, type *type) {
315 assert(uni && (uni->type_op == type_union));
316 uni->attr.ua.unioned_type[pos] = type;
318 ident *get_union_delim_nameid (type *uni, int pos) {
319 assert(uni && (uni->type_op == type_union));
320 return uni->attr.ua.delim_names[pos];
322 const char *get_union_delim_name (type *uni, int pos) {
323 assert(uni && (uni->type_op == type_union));
324 return id_to_str(uni->attr.ua.delim_names[pos]);
326 void set_union_delim_nameid (type *uni, int pos, ident *id) {
327 assert(uni && (uni->type_op == type_union));
328 uni->attr.ua.delim_names[pos] = id;
331 int get_union_n_members (type *uni) {
332 assert(uni && (uni->type_op == type_union));
333 return (ARR_LEN (uni->attr.ua.members))-1;
335 void add_union_member (type *uni, entity *member) {
336 assert(uni && (uni->type_op == type_union));
337 ARR_APP1 (entity *, uni->attr.ua.members, member);
339 entity *get_union_member (type *uni, int pos) {
340 assert(uni && (uni->type_op == type_union));
341 return uni->attr.ua.members[pos+1];
343 void set_union_member (type *uni, int pos, entity *member) {
344 assert(uni && (uni->type_op == type_union));
345 uni->attr.ua.members[pos+1] = member;
349 bool is_union_type (type *uni) {
351 if (uni->type_op == type_union) return 1; else return 0;
354 /*******************************************************************/
356 /*******************************************************************/
359 /* create a new type array -- set dimension sizes independently */
360 type *new_type_array (ident *name, int n_dimensions,
361 type *element_type) {
363 res = new_type(type_array, NULL, name);
364 res->attr.aa.n_dimensions = n_dimensions;
365 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
366 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
367 res->attr.aa.element_type = element_type;
368 new_entity(res, name, element_type);
372 /* manipulate private fields of array type */
373 int get_array_n_dimensions (type *array) {
374 assert(array && (array->type_op == type_array));
375 return array->attr.aa.n_dimensions;
377 void set_array_bounds (type *array, int dimension, ir_node * lower_bound,
378 ir_node * upper_bound) {
379 assert(array && (array->type_op == type_array));
380 array->attr.aa.lower_bound[dimension] = lower_bound;
381 array->attr.aa.upper_bound[dimension] = upper_bound;
383 void set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
384 assert(array && (array->type_op == type_array));
385 array->attr.aa.lower_bound[dimension] = lower_bound;
387 void set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
388 assert(array && (array->type_op == type_array));
389 array->attr.aa.upper_bound[dimension] = upper_bound;
391 ir_node * get_array_lower_bound (type *array, int dimension) {
392 assert(array && (array->type_op == type_array));
393 return array->attr.aa.lower_bound[dimension];
395 ir_node * get_array_upper_bound (type *array, int dimension) {
396 assert(array && (array->type_op == type_array));
397 return array->attr.aa.upper_bound[dimension];
399 void set_array_element_type (type *array, type *type) {
400 assert(array && (array->type_op == type_array));
401 array->attr.aa.element_type = type;
403 type *get_array_element_type (type *array) {
404 assert(array && (array->type_op == type_array));
405 return array->attr.aa.element_type;
407 void set_array_element_entity (type *array, entity *ent) {
408 assert(array && (array->type_op == type_array));
409 array->attr.aa.element_ent = ent;
411 entity *get_array_element_entity (type *array) {
412 assert(array && (array->type_op == type_array));
413 return array->attr.aa.element_ent;
417 bool is_array_type (type *array) {
419 if (array->type_op == type_array) return 1; else return 0;
422 /*******************************************************************/
423 /** TYPE_ENUMERATION **/
424 /*******************************************************************/
426 /* create a new type enumeration -- set the enumerators independently */
427 type *new_type_enumeration (ident *name, int n_enums) {
429 res = new_type(type_enumeration, NULL, name);
430 res->attr.ea.n_enums = n_enums;
431 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
432 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
436 /* manipulate fields of enumeration type. */
437 int get_enumeration_n_enums (type *enumeration) {
438 assert(enumeration && (enumeration->type_op == type_enumeration));
439 return enumeration->attr.ea.n_enums;
441 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
442 assert(enumeration && (enumeration->type_op == type_enumeration));
443 enumeration->attr.ea.enumer[pos] = con;
445 tarval *get_enumeration_enum (type *enumeration, int pos) {
446 assert(enumeration && (enumeration->type_op == type_enumeration));
447 return enumeration->attr.ea.enumer[pos];
449 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
450 assert(enumeration && (enumeration->type_op == type_enumeration));
451 enumeration->attr.ea.enum_nameid[pos] = id;
453 ident *get_enumeration_nameid (type *enumeration, int pos) {
454 assert(enumeration && (enumeration->type_op == type_enumeration));
455 return enumeration->attr.ea.enum_nameid[pos];
457 const char *get_enumeration_name(type *enumeration, int pos) {
458 assert(enumeration && (enumeration->type_op == type_enumeration));
459 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
463 bool is_enumeration_type (type *enumeration) {
465 if (enumeration->type_op == type_enumeration) return 1; else return 0;
468 /*******************************************************************/
470 /*******************************************************************/
472 /* Create a new type pointer */
473 type *new_type_pointer (ident *name, type *points_to) {
475 res = new_type(type_pointer, mode_p, name);
476 res->attr.pa.points_to = points_to;
477 res->size = get_mode_size(res->mode);
480 /* manipulate fields of type_pointer */
481 void set_pointer_points_to_type (type *pointer, type *type) {
482 assert(pointer && (pointer->type_op == type_pointer));
483 pointer->attr.pa.points_to = type;
485 type *get_pointer_points_to_type (type *pointer) {
486 assert(pointer && (pointer->type_op == type_pointer));
487 return pointer->attr.pa.points_to;
491 bool is_pointer_type (type *pointer) {
493 if (pointer->type_op == type_pointer) return 1; else return 0;
497 /*******************************************************************/
498 /** TYPE_PRIMITIVE **/
499 /*******************************************************************/
501 /* create a new type primitive */
502 type *new_type_primitive (ident *name, ir_mode *mode) {
504 res = new_type(type_primitive, mode, name);
505 res->size = get_mode_size(mode);
510 bool is_primitive_type (type *primitive) {
512 if (primitive->type_op == type_primitive) return 1; else return 0;