d7b21272e6ea45537328de0411577be7bc5ea7da
[libfirm] / ir / tr / type.c
1 /****h* libfirm/type.c
2  *
3  * NAME
4  *   file type.c - implementation of the datastructure to hold
5  *   type information.
6  * COPYRIGHT
7  *  (C) 2001 by Universitaet Karlsruhe
8  * AUTHORS
9  *  Martin Trapp, Christian Schaefer, Goetz Lindenmaier
10  *
11  * NOTES
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.
17  *
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
21  *  kind of the type.
22  *
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.
26  *
27  * SEE ALSO
28  *   type_t.h type tpop
29  *****
30  */
31 # include <stdlib.h>
32 # include <stddef.h>
33 # include "type_t.h"
34 # include "tpop_t.h"
35 # include "array.h"
36
37 /*******************************************************************/
38 /** TYPE                                                          **/
39 /*******************************************************************/
40
41 unsigned long type_visited;
42
43 inline type *
44 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
45   type *res;
46
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. */
50
51   res->kind = k_type;
52   res->type_op = type_op;
53   res->mode = mode;
54   res->name = name;
55   res->state = layout_undefined;
56   res->size = -1;
57   res->visit = 0;
58
59   return res;
60 }
61
62 tp_op*      get_type_tpop(type *tp) {
63   assert(tp);
64   return tp->type_op;
65 }
66
67 ident*      get_type_tpop_nameid(type *tp) {
68   assert(tp);
69   return tp->type_op->name;
70 }
71 const char* get_type_tpop_name(type *tp) {
72   assert(tp);
73   return id_to_str(tp->type_op->name);
74 }
75 tp_opcode    get_type_tpop_code(type *tp) {
76   assert(tp);
77   return tp->type_op->code;
78 }
79 ir_mode*    get_type_mode(type *tp) {
80   assert(tp);
81   return tp->mode;
82 }
83 void        set_type_mode(type *tp, ir_mode* m) {
84   assert(tp);
85   tp->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);
89 }
90 ident*      get_type_nameid(type *tp) {
91   assert(tp);
92   return tp->name;
93 }
94 void        set_type_nameid(type *tp, ident* id) {
95   assert(tp);
96   tp->name = id;
97 }
98 const char* get_type_name(type *tp) {
99   assert(tp);
100   return id_to_str(tp->name);
101 }
102 int         get_type_size(type *tp) {
103   assert(tp);
104   return tp->size;
105 }
106
107 void
108 set_type_size(type *tp, int size) {
109   assert(tp);
110   /* For pointer and primitive size depends on the mode. */
111   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
112     tp->size = size;
113 }
114
115 type_state
116 get_type_state(type *tp) {
117   assert(tp);
118   return tp->state;
119 }
120
121 void
122 set_type_state(type *tp, type_state state) {
123   assert(tp);
124   /* For pointer and primitive always fixed. */
125   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
126     tp->state = state;
127 }
128
129 unsigned long get_type_visited(type *tp) {
130   assert(tp);
131   return tp->visit;
132 }
133
134 void        set_type_visited(type *tp, unsigned long num) {
135   assert(tp);
136   tp->visit = num;
137 }
138 /* Sets visited field in type to type_visited. */
139 void        mark_type_visited(type *tp) {
140   assert(tp);
141   assert(tp->visit < type_visited);
142   tp->visit = type_visited;
143 }
144
145 int is_type            (void *thing) {
146   assert(thing);
147   if (get_kind(thing) == k_type)
148     return 1;
149   else
150     return 0;
151 }
152
153 /*******************************************************************/
154 /** TYPE_CLASS                                                    **/
155 /*******************************************************************/
156
157 /* create a new class type */
158 type   *new_type_class (ident *name) {
159   type *res;
160
161   res = new_type(type_class, NULL, name);
162
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);
166
167   return res;
168 }
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);
173 }
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;
177 }
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];
181 }
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;
185 }
186 void    remove_class_member(type *clss, entity *member) {
187   int i;
188   assert(clss && (clss->type_op == type_class));
189   for (i = 1; i < (ARR_LEN (clss->attr.ca.members))-1; i++)
190     if (clss->attr.ca.members[i+1] == member) {
191       clss->attr.ca.members[i+1] = NULL;
192       break;
193     }
194 }
195
196 void    add_class_subtype   (type *clss, type *subtype) {
197   assert(clss && (clss->type_op == type_class));
198   ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
199   ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
200 }
201 int     get_class_n_subtype (type *clss) {
202   assert(clss && (clss->type_op == type_class));
203   return (ARR_LEN (clss->attr.ca.subtypes))-1;
204 }
205 type   *get_class_subtype   (type *clss, int pos) {
206   assert(clss && (clss->type_op == type_class));
207   return clss->attr.ca.subtypes[pos+1];
208 }
209 void    set_class_subtype   (type *clss, type *subtype, int pos) {
210   assert(clss && (clss->type_op == type_class));
211   clss->attr.ca.subtypes[pos+1] = subtype;
212 }
213 void    remove_class_subtype(type *clss, type *subtype) {
214   int i;
215   assert(clss && (clss->type_op == type_class));
216   for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
217     if (clss->attr.ca.subtypes[i+1] == subtype) {
218       clss->attr.ca.subtypes[i+1] = NULL;
219       break;
220     }
221 }
222
223 void    add_class_supertype   (type *clss, type *supertype) {
224   assert(clss && (clss->type_op == type_class));
225   ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
226   ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
227 }
228 int     get_class_n_supertype (type *clss) {
229   assert(clss && (clss->type_op == type_class));
230   return (ARR_LEN (clss->attr.ca.supertypes))-1;
231 }
232 type   *get_class_supertype   (type *clss, int pos) {
233   assert(clss && (clss->type_op == type_class));
234   return clss->attr.ca.supertypes[pos+1];
235 }
236 void    set_class_supertype   (type *clss, type *supertype, int pos) {
237   assert(clss && (clss->type_op == type_class));
238   clss->attr.ca.supertypes[pos+1] = supertype;
239 }
240 void    remove_class_supertype(type *clss, type *supertype) {
241   int i;
242   assert(clss && (clss->type_op == type_class));
243   for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
244     if (clss->attr.ca.supertypes[i+1] == supertype) {
245       clss->attr.ca.supertypes[i+1] = NULL;
246       break;
247     }
248 }
249 /* typecheck */
250 bool    is_class_type(type *clss) {
251   assert(clss);
252   if (clss->type_op == type_class) return 1; else return 0;
253 }
254
255 /*******************************************************************/
256 /** TYPE_STRUCT                                                   **/
257 /*******************************************************************/
258
259 /* create a new type struct */
260 type   *new_type_struct (ident *name) {
261   type *res;
262   res = new_type(type_struct, NULL, name);
263   res->attr.sa.members = NEW_ARR_F (entity *, 1);
264   return res;
265 }
266 /* manipulate private fields of struct */
267 void    add_struct_member   (type *strct, entity *member) {
268   assert(strct && (strct->type_op == type_struct));
269   ARR_APP1 (entity *, strct->attr.sa.members, member);
270 }
271 int     get_struct_n_member (type *strct) {
272   assert(strct && (strct->type_op == type_struct));
273   return (ARR_LEN (strct->attr.sa.members))-1;
274 }
275 entity *get_struct_member   (type *strct, int pos) {
276   assert(strct && (strct->type_op == type_struct));
277   return strct->attr.sa.members[pos+1];
278 }
279 void    set_struct_member   (type *strct, int pos, entity *member) {
280   assert(strct && (strct->type_op == type_struct));
281   strct->attr.sa.members[pos+1] = member;
282 }
283 void    remove_struct_member(type *strct, entity *member) {
284   int i;
285   assert(strct && (strct->type_op == type_struct));
286   for (i = 1; i < (ARR_LEN (strct->attr.ca.members))-1; i++)
287     if (strct->attr.ca.members[i+1] == member) {
288       strct->attr.ca.members[i+1] = NULL;
289       break;
290     }
291 }
292 /* typecheck */
293 bool    is_struct_type(type *strct) {
294   assert(strct);
295   if (strct->type_op == type_struct) return 1; else return 0;
296 }
297
298 /*******************************************************************/
299 /** TYPE_METHOD                                                   **/
300 /*******************************************************************/
301
302 /* Create a new method type.
303    N_param is the number of parameters, n_res the number of results.  */
304 type *new_type_method (ident *name, int n_param, int n_res) {
305   type *res;
306   res = new_type(type_method, NULL, name);
307   res->attr.ma.n_params   = n_param;
308   res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
309   res->attr.ma.n_res      = n_res;
310   res->attr.ma.res_type   = (type **) xmalloc (sizeof (type *) * n_res);
311   return res;
312 }
313
314 /* manipulate private fields of method. */
315 int   get_method_n_params  (type *method) {
316   assert(method && (method->type_op == type_method));
317   return method->attr.ma.n_params;
318 }
319 type *get_method_param_type(type *method, int pos) {
320   assert(method && (method->type_op == type_method));
321   return method->attr.ma.param_type[pos];
322 }
323 void  set_method_param_type(type *method, int pos, type* type) {
324   assert(method && (method->type_op == type_method));
325   method->attr.ma.param_type[pos] = type;
326 }
327
328 int   get_method_n_res   (type *method) {
329   assert(method && (method->type_op == type_method));
330   return method->attr.ma.n_res;
331 }
332 type *get_method_res_type(type *method, int pos) {
333   assert(method && (method->type_op == type_method));
334   return method->attr.ma.res_type[pos];
335 }
336 void  set_method_res_type(type *method, int pos, type* type) {
337   assert(method && (method->type_op == type_method));
338   method->attr.ma.res_type[pos] = type;
339 }
340
341 /* typecheck */
342 bool  is_method_type     (type *method) {
343   assert(method);
344   if (method->type_op == type_method) return 1; else return 0;
345 }
346 /*****/
347
348 /*******************************************************************/
349 /** TYPE_UNION                                                    **/
350 /*******************************************************************/
351
352 /* create a new type uni */
353 type  *new_type_uni (ident *name) {
354   type *res;
355   res = new_type(type_union, NULL, name);
356   /*res->attr.ua.unioned_type = (type **)  xmalloc (sizeof (type *)  * n_types);
357     res->attr.ua.delim_names  = (ident **) xmalloc (sizeof (ident *) * n_types); */
358   res->attr.ua.members = NEW_ARR_F (entity *, 1);
359   return res;
360 }
361 /* manipulate private fields of struct */
362 #if 0
363 int    get_union_n_types      (type *uni) {
364   assert(uni && (uni->type_op == type_union));
365   return uni->attr.ua.n_types;
366 }
367 type  *get_union_unioned_type (type *uni, int pos) {
368   assert(uni && (uni->type_op == type_union));
369   return uni->attr.ua.unioned_type[pos];
370 }
371 void   set_union_unioned_type (type *uni, int pos, type *type) {
372   assert(uni && (uni->type_op == type_union));
373   uni->attr.ua.unioned_type[pos] = type;
374 }
375 ident *get_union_delim_nameid (type *uni, int pos) {
376   assert(uni && (uni->type_op == type_union));
377   return uni->attr.ua.delim_names[pos];
378 }
379 const char *get_union_delim_name (type *uni, int pos) {
380   assert(uni && (uni->type_op == type_union));
381   return id_to_str(uni->attr.ua.delim_names[pos]);
382 }
383 void   set_union_delim_nameid (type *uni, int pos, ident *id) {
384   assert(uni && (uni->type_op == type_union));
385   uni->attr.ua.delim_names[pos] = id;
386 }
387 #endif
388 int    get_union_n_members      (type *uni) {
389   assert(uni && (uni->type_op == type_union));
390   return (ARR_LEN (uni->attr.ua.members))-1;
391 }
392 void    add_union_member   (type *uni, entity *member) {
393   assert(uni && (uni->type_op == type_union));
394   ARR_APP1 (entity *, uni->attr.ua.members, member);
395 }
396 entity  *get_union_member (type *uni, int pos) {
397   assert(uni && (uni->type_op == type_union));
398   return uni->attr.ua.members[pos+1];
399 }
400 void   set_union_member (type *uni, int pos, entity *member) {
401   assert(uni && (uni->type_op == type_union));
402   uni->attr.ua.members[pos+1] = member;
403 }
404 void   remove_union_member(type *uni, entity *member) {
405   int i;
406   assert(uni && (uni->type_op == type_union));
407   for (i = 1; i < (ARR_LEN (uni->attr.ca.members))-1; i++)
408     if (uni->attr.ca.members[i+1] == member) {
409       uni->attr.ca.members[i+1] = NULL;
410       break;
411     }
412 }
413
414 /* typecheck */
415 bool   is_union_type         (type *uni) {
416   assert(uni);
417   if (uni->type_op == type_union) return 1; else return 0;
418 }
419
420 /*******************************************************************/
421 /** TYPE_ARRAY                                                    **/
422 /*******************************************************************/
423
424
425 /* create a new type array -- set dimension sizes independently */
426 type *new_type_array         (ident *name, int n_dimensions,
427                               type *element_type) {
428   type *res;
429   res = new_type(type_array, NULL, name);
430   res->attr.aa.n_dimensions = n_dimensions;
431   res->attr.aa.lower_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
432   res->attr.aa.upper_bound  = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
433   res->attr.aa.element_type = element_type;
434   new_entity(res, name, element_type);
435   return res;
436 }
437
438 /* manipulate private fields of array type */
439 int   get_array_n_dimensions (type *array) {
440   assert(array && (array->type_op == type_array));
441   return array->attr.aa.n_dimensions;
442 }
443 void  set_array_bounds       (type *array, int dimension, ir_node * lower_bound,
444                                                           ir_node * upper_bound) {
445   assert(array && (array->type_op == type_array));
446   array->attr.aa.lower_bound[dimension] = lower_bound;
447   array->attr.aa.upper_bound[dimension] = upper_bound;
448 }
449 void  set_array_lower_bound  (type *array, int dimension, ir_node * lower_bound) {
450   assert(array && (array->type_op == type_array));
451   array->attr.aa.lower_bound[dimension] = lower_bound;
452 }
453 void  set_array_upper_bound  (type *array, int dimension, ir_node * upper_bound) {
454   assert(array && (array->type_op == type_array));
455   array->attr.aa.upper_bound[dimension] = upper_bound;
456 }
457 ir_node * get_array_lower_bound  (type *array, int dimension) {
458   assert(array && (array->type_op == type_array));
459   return array->attr.aa.lower_bound[dimension];
460 }
461 ir_node * get_array_upper_bound  (type *array, int dimension) {
462   assert(array && (array->type_op == type_array));
463   return array->attr.aa.upper_bound[dimension];
464 }
465 void  set_array_element_type (type *array, type *type) {
466   assert(array && (array->type_op == type_array));
467   array->attr.aa.element_type = type;
468 }
469 type *get_array_element_type (type *array) {
470   assert(array && (array->type_op == type_array));
471   return array->attr.aa.element_type;
472 }
473 void  set_array_element_entity (type *array, entity *ent) {
474   assert(array && (array->type_op == type_array));
475   array->attr.aa.element_ent = ent;
476 }
477 entity *get_array_element_entity (type *array) {
478   assert(array && (array->type_op == type_array));
479   return array->attr.aa.element_ent;
480 }
481
482 /* typecheck */
483 bool   is_array_type         (type *array) {
484   assert(array);
485   if (array->type_op == type_array) return 1; else return 0;
486 }
487
488 /*******************************************************************/
489 /** TYPE_ENUMERATION                                              **/
490 /*******************************************************************/
491
492 /* create a new type enumeration -- set the enumerators independently */
493 type   *new_type_enumeration    (ident *name, int n_enums) {
494   type *res;
495   res = new_type(type_enumeration, NULL, name);
496   res->attr.ea.n_enums     = n_enums;
497   res->attr.ea.enumer      = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
498   res->attr.ea.enum_nameid = (ident  **) xmalloc (sizeof (ident  *) * n_enums);
499   return res;
500 }
501
502 /* manipulate fields of enumeration type. */
503 int     get_enumeration_n_enums (type *enumeration) {
504   assert(enumeration && (enumeration->type_op == type_enumeration));
505   return enumeration->attr.ea.n_enums;
506 }
507 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con) {
508   assert(enumeration && (enumeration->type_op == type_enumeration));
509   enumeration->attr.ea.enumer[pos] = con;
510 }
511 tarval *get_enumeration_enum    (type *enumeration, int pos) {
512   assert(enumeration && (enumeration->type_op == type_enumeration));
513   return enumeration->attr.ea.enumer[pos];
514 }
515 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id) {
516   assert(enumeration && (enumeration->type_op == type_enumeration));
517   enumeration->attr.ea.enum_nameid[pos] = id;
518 }
519 ident  *get_enumeration_nameid  (type *enumeration, int pos) {
520   assert(enumeration && (enumeration->type_op == type_enumeration));
521   return enumeration->attr.ea.enum_nameid[pos];
522 }
523 const char *get_enumeration_name(type *enumeration, int pos) {
524   assert(enumeration && (enumeration->type_op == type_enumeration));
525   return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
526 }
527
528 /* typecheck */
529 bool    is_enumeration_type     (type *enumeration) {
530   assert(enumeration);
531   if (enumeration->type_op == type_enumeration) return 1; else return 0;
532 }
533
534 /*******************************************************************/
535 /** TYPE_POINTER                                                  **/
536 /*******************************************************************/
537
538 /* Create a new type pointer */
539 type *new_type_pointer           (ident *name, type *points_to) {
540   type *res;
541   res = new_type(type_pointer, mode_p, name);
542   res->attr.pa.points_to = points_to;
543   res->size = get_mode_size(res->mode);
544   res->state = layout_fixed;
545   return res;
546 }
547 /* manipulate fields of type_pointer */
548 void  set_pointer_points_to_type (type *pointer, type *type) {
549   assert(pointer && (pointer->type_op == type_pointer));
550   pointer->attr.pa.points_to = type;
551 }
552 type *get_pointer_points_to_type (type *pointer) {
553   assert(pointer && (pointer->type_op == type_pointer));
554   return pointer->attr.pa.points_to;
555 }
556
557 /* typecheck */
558 bool  is_pointer_type            (type *pointer) {
559   assert(pointer);
560   if (pointer->type_op == type_pointer) return 1; else return 0;
561 }
562
563
564 /*******************************************************************/
565 /** TYPE_PRIMITIVE                                                **/
566 /*******************************************************************/
567
568 /* create a new type primitive */
569 type *new_type_primitive (ident *name, ir_mode *mode) {
570   type *res;
571   res = new_type(type_primitive, mode, name);
572   res->size = get_mode_size(mode);
573   res->state = layout_fixed;
574   return res;
575 }
576
577 /* typecheck */
578 bool  is_primitive_type  (type *primitive) {
579   assert(primitive);
580   if (primitive->type_op == type_primitive) return 1; else return 0;
581 }