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) {
86 ident* get_type_nameid(type *tp) {
90 void set_type_nameid(type *tp, ident* id) {
94 const char* get_type_name(type *tp) {
96 return id_to_str(tp->name);
98 int get_type_size(type *tp) {
102 void set_type_size(type *tp, int size) {
106 unsigned long get_type_visited(type *tp) {
110 void set_type_visited(type *tp, unsigned long num) {
114 /* Sets visited field in type to type_visited. */
115 void mark_type_visited(type *tp) {
117 assert(tp->visit < type_visited);
118 tp->visit = type_visited;
121 int is_type (void *thing) {
123 if (get_kind(thing) == k_type)
129 /*******************************************************************/
131 /*******************************************************************/
133 /* create a new class type */
134 type *new_type_class (ident *name) {
137 res = new_type(type_class, NULL, name);
139 res->attr.ca.members = NEW_ARR_F (entity *, 1);
140 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
141 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
145 /* manipulate private fields of class type */
146 void add_class_member (type *clss, entity *member) {
147 assert(clss && (clss->type_op == type_class));
148 ARR_APP1 (entity *, clss->attr.ca.members, member);
150 int get_class_n_member (type *clss) {
151 assert(clss && (clss->type_op == type_class));
152 return (ARR_LEN (clss->attr.ca.members))-1;
154 entity *get_class_member (type *clss, int pos) {
155 assert(clss && (clss->type_op == type_class));
156 return clss->attr.ca.members[pos+1];
158 void set_class_member (type *clss, entity *member, int pos) {
159 assert(clss && (clss->type_op == type_class));
160 clss->attr.ca.members[pos+1] = member;
163 void add_class_subtype (type *clss, type *subtype) {
164 assert(clss && (clss->type_op == type_class));
165 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
167 int get_class_n_subtype (type *clss) {
168 assert(clss && (clss->type_op == type_class));
169 return (ARR_LEN (clss->attr.ca.subtypes))-1;
171 type *get_class_subtype (type *clss, int pos) {
172 assert(clss && (clss->type_op == type_class));
173 return clss->attr.ca.subtypes[pos+1];
175 void set_class_subtype (type *clss, type *subtype, int pos) {
176 assert(clss && (clss->type_op == type_class));
177 clss->attr.ca.subtypes[pos+1] = subtype;
180 void add_class_supertype (type *clss, type *supertype) {
181 assert(clss && (clss->type_op == type_class));
182 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
184 int get_class_n_supertype (type *clss) {
185 assert(clss && (clss->type_op == type_class));
186 return (ARR_LEN (clss->attr.ca.supertypes))-1;
188 type *get_class_supertype (type *clss, int pos) {
189 assert(clss && (clss->type_op == type_class));
190 return clss->attr.ca.supertypes[pos+1];
192 void set_class_supertype (type *clss, type *supertype, int pos) {
193 assert(clss && (clss->type_op == type_class));
194 clss->attr.ca.supertypes[pos+1] = supertype;
197 bool is_class_type(type *clss) {
199 if (clss->type_op == type_class) return 1; else return 0;
202 /*******************************************************************/
204 /*******************************************************************/
206 /* create a new type struct */
207 type *new_type_struct (ident *name) {
209 res = new_type(type_struct, NULL, name);
210 res->attr.sa.members = NEW_ARR_F (entity *, 1);
213 /* manipulate private fields of struct */
214 void add_struct_member (type *strct, entity *member) {
215 assert(strct && (strct->type_op == type_struct));
216 ARR_APP1 (entity *, strct->attr.sa.members, member);
218 int get_struct_n_member (type *strct) {
219 assert(strct && (strct->type_op == type_struct));
220 return (ARR_LEN (strct->attr.sa.members))-1;
222 entity *get_struct_member (type *strct, int pos) {
223 assert(strct && (strct->type_op == type_struct));
224 return strct->attr.sa.members[pos+1];
226 void set_struct_member (type *strct, int pos, entity *member) {
227 assert(strct && (strct->type_op == type_struct));
228 strct->attr.sa.members[pos+1] = member;
231 bool is_struct_type(type *strct) {
233 if (strct->type_op == type_struct) return 1; else return 0;
236 /*******************************************************************/
238 /*******************************************************************/
240 /* Create a new method type.
241 N_param is the number of parameters, n_res the number of results. */
242 type *new_type_method (ident *name, int n_param, int n_res) {
244 res = new_type(type_method, NULL, name);
245 res->attr.ma.n_params = n_param;
246 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
247 res->attr.ma.n_res = n_res;
248 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
252 /* manipulate private fields of method. */
253 int get_method_n_params (type *method) {
254 assert(method && (method->type_op == type_method));
255 return method->attr.ma.n_params;
257 type *get_method_param_type(type *method, int pos) {
258 assert(method && (method->type_op == type_method));
259 return method->attr.ma.param_type[pos];
261 void set_method_param_type(type *method, int pos, type* type) {
262 assert(method && (method->type_op == type_method));
263 method->attr.ma.param_type[pos] = type;
266 int get_method_n_res (type *method) {
267 assert(method && (method->type_op == type_method));
268 return method->attr.ma.n_res;
270 type *get_method_res_type(type *method, int pos) {
271 assert(method && (method->type_op == type_method));
272 return method->attr.ma.res_type[pos];
274 void set_method_res_type(type *method, int pos, type* type) {
275 assert(method && (method->type_op == type_method));
276 method->attr.ma.res_type[pos] = type;
279 bool is_method_type (type *method) {
281 if (method->type_op == type_method) return 1; else return 0;
285 /*******************************************************************/
287 /*******************************************************************/
289 /* create a new type uni */
290 type *new_type_uni (ident *name, int n_types) {
292 res = new_type(type_union, NULL, name);
293 res->attr.ua.n_types = n_types;
294 res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
295 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types);
298 /* manipulate private fields of struct */
299 int get_union_n_types (type *uni) {
300 assert(uni && (uni->type_op == type_union));
301 return uni->attr.ua.n_types;
303 type *get_union_unioned_type (type *uni, int pos) {
304 assert(uni && (uni->type_op == type_union));
305 return uni->attr.ua.unioned_type[pos];
307 void set_union_unioned_type (type *uni, int pos, type *type) {
308 assert(uni && (uni->type_op == type_union));
309 uni->attr.ua.unioned_type[pos] = type;
311 ident *get_union_delim_nameid (type *uni, int pos) {
312 assert(uni && (uni->type_op == type_union));
313 return uni->attr.ua.delim_names[pos];
315 const char *get_union_delim_name (type *uni, int pos) {
316 assert(uni && (uni->type_op == type_union));
317 return id_to_str(uni->attr.ua.delim_names[pos]);
319 void set_union_delim_nameid (type *uni, int pos, ident *id) {
320 assert(uni && (uni->type_op == type_union));
321 uni->attr.ua.delim_names[pos] = id;
325 bool is_union_type (type *uni) {
327 if (uni->type_op == type_union) return 1; else return 0;
330 /*******************************************************************/
332 /*******************************************************************/
335 /* create a new type array -- set dimension sizes independently */
336 type *new_type_array (ident *name, int n_dimensions) {
338 res = new_type(type_array, NULL, name);
339 res->attr.aa.n_dimensions = n_dimensions;
340 res->attr.aa.lower_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
341 res->attr.aa.upper_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
344 /* manipulate private fields of array type */
345 int get_array_n_dimensions (type *array) {
346 assert(array && (array->type_op == type_array));
347 return array->attr.aa.n_dimensions;
349 void set_array_bounds (type *array, int dimension, int lower_bound,
351 assert(array && (array->type_op == type_array));
352 array->attr.aa.lower_bound[dimension] = lower_bound;
353 array->attr.aa.upper_bound[dimension] = upper_bound;
355 void set_array_lower_bound (type *array, int dimension, int lower_bound) {
356 assert(array && (array->type_op == type_array));
357 array->attr.aa.lower_bound[dimension] = lower_bound;
359 void set_array_upper_bound (type *array, int dimension, int upper_bound) {
360 assert(array && (array->type_op == type_array));
361 array->attr.aa.upper_bound[dimension] = upper_bound;
363 int get_array_lower_bound (type *array, int dimension) {
364 assert(array && (array->type_op == type_array));
365 return array->attr.aa.lower_bound[dimension];
367 int get_array_upper_bound (type *array, int dimension) {
368 assert(array && (array->type_op == type_array));
369 return array->attr.aa.upper_bound[dimension];
371 void set_array_element_type (type *array, type *type) {
372 assert(array && (array->type_op == type_array));
373 array->attr.aa.element_type = type;
375 type *get_array_element_type (type *array) {
376 assert(array && (array->type_op == type_array));
377 return array->attr.aa.element_type;
380 bool is_array_type (type *array) {
382 if (array->type_op == type_array) return 1; else return 0;
385 /*******************************************************************/
386 /** TYPE_ENUMERATION **/
387 /*******************************************************************/
389 /* create a new type enumeration -- set the enumerators independently */
390 type *new_type_enumeration (ident *name, int n_enums) {
392 res = new_type(type_enumeration, NULL, name);
393 res->attr.ea.n_enums = n_enums;
394 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
395 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
399 /* manipulate fields of enumeration type. */
400 int get_enumeration_n_enums (type *enumeration) {
401 assert(enumeration && (enumeration->type_op == type_enumeration));
402 return enumeration->attr.ea.n_enums;
404 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
405 assert(enumeration && (enumeration->type_op == type_enumeration));
406 enumeration->attr.ea.enumer[pos] = con;
408 tarval *get_enumeration_enum (type *enumeration, int pos) {
409 assert(enumeration && (enumeration->type_op == type_enumeration));
410 return enumeration->attr.ea.enumer[pos];
412 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
413 assert(enumeration && (enumeration->type_op == type_enumeration));
414 enumeration->attr.ea.enum_nameid[pos] = id;
416 ident *get_enumeration_nameid (type *enumeration, int pos) {
417 assert(enumeration && (enumeration->type_op == type_enumeration));
418 return enumeration->attr.ea.enum_nameid[pos];
420 const char *get_enumeration_name(type *enumeration, int pos) {
421 assert(enumeration && (enumeration->type_op == type_enumeration));
422 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
426 bool is_enumeration_type (type *enumeration) {
428 if (enumeration->type_op == type_enumeration) return 1; else return 0;
431 /*******************************************************************/
433 /*******************************************************************/
435 /* Create a new type pointer */
436 type *new_type_pointer (ident *name, type *points_to) {
438 res = new_type(type_pointer, mode_p, name);
439 res->attr.pa.points_to = points_to;
442 /* manipulate fields of type_pointer */
443 void set_pointer_points_to_type (type *pointer, type *type) {
444 assert(pointer && (pointer->type_op == type_pointer));
445 pointer->attr.pa.points_to = type;
447 type *get_pointer_points_to_type (type *pointer) {
448 assert(pointer && (pointer->type_op == type_pointer));
449 return pointer->attr.pa.points_to;
453 bool is_pointer_type (type *pointer) {
455 if (pointer->type_op == type_pointer) return 1; else return 0;
459 /*******************************************************************/
460 /** TYPE_PRIMITIVE **/
461 /*******************************************************************/
463 /* create a new type primitive */
464 type *new_type_primitive (ident *name, ir_mode *mode) {
466 res = new_type(type_primitive, mode_p, name);
471 bool is_primitive_type (type *primitive) {
473 if (primitive->type_op == type_primitive) return 1; else return 0;