*** empty log message ***
[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 "typegmod_t.h"
36 # include "array.h"
37
38 /*******************************************************************/
39 /** TYPE                                                          **/
40 /*******************************************************************/
41
42 unsigned long type_visited;
43
44 inline type *
45 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
46   type *res;
47   int node_size ;
48
49   assert(type_op != type_id);
50
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. */
54
55   res->kind = k_type;
56   res->type_op = type_op;
57   res->mode = mode;
58   res->name = name;
59   res->state = layout_undefined;
60   res->size = -1;
61   res->visit = 0;
62
63   return res;
64 }
65
66 tp_op*      get_type_tpop(type *tp) {
67   assert(tp);
68   return tp->type_op;
69 }
70
71 ident*      get_type_tpop_nameid(type *tp) {
72   assert(tp);
73   return tp->type_op->name;
74 }
75 const char* get_type_tpop_name(type *tp) {
76   assert(tp);
77   return id_to_str(tp->type_op->name);
78 }
79 tp_opcode    get_type_tpop_code(type *tp) {
80   assert(tp);
81   return tp->type_op->code;
82 }
83 ir_mode*    get_type_mode(type *tp) {
84   assert(tp);
85   return tp->mode;
86 }
87 void        set_type_mode(type *tp, ir_mode* m) {
88   assert(tp);
89   tp->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);
93 }
94 ident*      get_type_nameid(type *tp) {
95   assert(tp);
96   return tp->name;
97 }
98 void        set_type_nameid(type *tp, ident* id) {
99   assert(tp);
100   tp->name = id;
101 }
102 const char* get_type_name(type *tp) {
103   assert(tp);
104   return id_to_str(tp->name);
105 }
106 int         get_type_size(type *tp) {
107   assert(tp);
108   return tp->size;
109 }
110
111 void
112 set_type_size(type *tp, int size) {
113   assert(tp);
114   /* For pointer and primitive size depends on the mode. */
115   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
116     tp->size = size;
117 }
118
119 type_state
120 get_type_state(type *tp) {
121   assert(tp);
122   return tp->state;
123 }
124
125 void
126 set_type_state(type *tp, type_state state) {
127   assert(tp);
128   /* For pointer and primitive always fixed. */
129   if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive))
130     tp->state = state;
131 }
132
133 unsigned long get_type_visited(type *tp) {
134   assert(tp);
135   return tp->visit;
136 }
137
138 void        set_type_visited(type *tp, unsigned long num) {
139   assert(tp);
140   tp->visit = num;
141 }
142 /* Sets visited field in type to type_visited. */
143 void        mark_type_visited(type *tp) {
144   assert(tp);
145   assert(tp->visit < type_visited);
146   tp->visit = type_visited;
147 }
148
149 int is_type            (void *thing) {
150   assert(thing);
151   if (get_kind(thing) == k_type)
152     return 1;
153   else
154     return 0;
155 }
156
157 /*******************************************************************/
158 /** TYPE_CLASS                                                    **/
159 /*******************************************************************/
160
161 /* create a new class type */
162 type   *new_type_class (ident *name) {
163   type *res;
164
165   res = new_type(type_class, NULL, name);
166
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);
170
171   return res;
172 }
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);
177 }
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;
181 }
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];
185 }
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;
189 }
190 void    remove_class_member(type *clss, entity *member) {
191   int i;
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);
198       break;
199     }
200 }
201
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);
206 }
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;
210 }
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]);
214 }
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;
218 }
219 void    remove_class_subtype(type *clss, type *subtype) {
220   int i;
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);
227       break;
228     }
229 }
230
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);
235 }
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;
239 }
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]);
243 }
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;
247 }
248 void    remove_class_supertype(type *clss, type *supertype) {
249   int i;
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);
256       break;
257     }
258 }
259 /* typecheck */
260 bool    is_class_type(type *clss) {
261   assert(clss);
262   if (clss->type_op == type_class) return 1; else return 0;
263 }
264
265 /*******************************************************************/
266 /** TYPE_STRUCT                                                   **/
267 /*******************************************************************/
268
269 /* create a new type struct */
270 type   *new_type_struct (ident *name) {
271   type *res;
272   res = new_type(type_struct, NULL, name);
273   res->attr.sa.members = NEW_ARR_F (entity *, 1);
274   return res;
275 }
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);
280 }
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;
284 }
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];
288 }
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;
292 }
293 void    remove_struct_member(type *strct, entity *member) {
294   int i;
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);
301       break;
302     }
303 }
304 /* typecheck */
305 bool    is_struct_type(type *strct) {
306   assert(strct);
307   if (strct->type_op == type_struct) return 1; else return 0;
308 }
309
310 /*******************************************************************/
311 /** TYPE_METHOD                                                   **/
312 /*******************************************************************/
313
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) {
317   type *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);
323   return res;
324 }
325
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;
330 }
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]);
334 }
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;
338 }
339
340 int   get_method_n_res   (type *method) {
341   assert(method && (method->type_op == type_method));
342   return method->attr.ma.n_res;
343 }
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]);
347 }
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;
351 }
352
353 /* typecheck */
354 bool  is_method_type     (type *method) {
355   assert(method);
356   if (method->type_op == type_method) return 1; else return 0;
357 }
358 /*****/
359
360 /*******************************************************************/
361 /** TYPE_UNION                                                    **/
362 /*******************************************************************/
363
364 /* create a new type uni */
365 type  *new_type_uni (ident *name) {
366   type *res;
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);
371   return res;
372 }
373 /* manipulate private fields of struct */
374 #if 0
375 int    get_union_n_types      (type *uni) {
376   assert(uni && (uni->type_op == type_union));
377   return uni->attr.ua.n_types;
378 }
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]);
382 }
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;
386 }
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];
390 }
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]);
394 }
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;
398 }
399 #endif
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;
403 }
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);
407 }
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];
411 }
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;
415 }
416 void   remove_union_member(type *uni, entity *member) {
417   int i;
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);
424       break;
425     }
426 }
427
428 /* typecheck */
429 bool   is_union_type         (type *uni) {
430   assert(uni);
431   if (uni->type_op == type_union) return 1; else return 0;
432 }
433
434 /*******************************************************************/
435 /** TYPE_ARRAY                                                    **/
436 /*******************************************************************/
437
438
439 /* create a new type array -- set dimension sizes independently */
440 type *new_type_array         (ident *name, int n_dimensions,
441                               type *element_type) {
442   type *res;
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);
449   return res;
450 }
451
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;
456 }
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;
462 }
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;
466 }
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;
470 }
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];
474 }
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];
478 }
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;
482 }
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);
486 }
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;
490 }
491 entity *get_array_element_entity (type *array) {
492   assert(array && (array->type_op == type_array));
493   return array->attr.aa.element_ent;
494 }
495
496 /* typecheck */
497 bool   is_array_type         (type *array) {
498   assert(array);
499   if (array->type_op == type_array) return 1; else return 0;
500 }
501
502 /*******************************************************************/
503 /** TYPE_ENUMERATION                                              **/
504 /*******************************************************************/
505
506 /* create a new type enumeration -- set the enumerators independently */
507 type   *new_type_enumeration    (ident *name, int n_enums) {
508   type *res;
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);
513   return res;
514 }
515
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;
520 }
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;
524 }
525 tarval *get_enumeration_enum    (type *enumeration, int pos) {
526   assert(enumeration && (enumeration->type_op == type_enumeration));
527   return enumeration->attr.ea.enumer[pos];
528 }
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;
532 }
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];
536 }
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]);
540 }
541
542 /* typecheck */
543 bool    is_enumeration_type     (type *enumeration) {
544   assert(enumeration);
545   if (enumeration->type_op == type_enumeration) return 1; else return 0;
546 }
547
548 /*******************************************************************/
549 /** TYPE_POINTER                                                  **/
550 /*******************************************************************/
551
552 /* Create a new type pointer */
553 type *new_type_pointer           (ident *name, type *points_to) {
554   type *res;
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;
559   return res;
560 }
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;
565 }
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);
569 }
570
571 /* typecheck */
572 bool  is_pointer_type            (type *pointer) {
573   assert(pointer);
574   if (pointer->type_op == type_pointer) return 1; else return 0;
575 }
576
577
578 /*******************************************************************/
579 /** TYPE_PRIMITIVE                                                **/
580 /*******************************************************************/
581
582 /* create a new type primitive */
583 type *new_type_primitive (ident *name, ir_mode *mode) {
584   type *res;
585   res = new_type(type_primitive, mode, name);
586   res->size = get_mode_size(mode);
587   res->state = layout_fixed;
588   return res;
589 }
590
591 /* typecheck */
592 bool  is_primitive_type  (type *primitive) {
593   assert(primitive);
594   if (primitive->type_op == type_primitive) return 1; else return 0;
595 }