Add interface and abstract flags to class types.
[libfirm] / ir / tr / type_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type_t.h
4  * Purpose:     Representation of types -- private header.
5  * Author:      Goetz Lindenmaier
6  * Modified by: Michael Beck
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifndef _FIRM_TR_TYPE_T_H_
14 #define _FIRM_TR_TYPE_T_H_
15
16 #include "firm_config.h"
17 #include "type.h"
18 #include "tpop_t.h"
19 #include "irgraph.h"
20
21 #include "array.h"
22
23 /**
24  * @file type_t.h
25  * This file contains the datatypes hidden in type.h.
26  *
27  * @author Goetz Lindenmaier
28  * @see  type.h tpop_t.h tpop.h
29  */
30
31 /** Class flags. */
32 enum class_flags {
33   cf_none            = 0,  /**< No flags. */
34   cf_final_class     = 1,  /**< Set if a class is an final class */
35   cf_interface_class = 2,  /**< Set if a class is an "interface" */
36   cf_absctract_class = 4,  /**< Set if a class is "abstract" */
37 };
38
39 /** Class attributes. */
40 typedef struct {
41   entity  **members;        /**< Array containing the fields and methods of this class. */
42   ir_type **subtypes;       /**< Array containing the direct subtypes. */
43   ir_type **supertypes;     /**< Array containing the direct supertypes */
44   peculiarity peculiarity;  /**< The peculiarity of this class. */
45   entity  *type_info;       /**< An entity representing this class, used for type info. */
46   int     dfn;              /**< A number that can be used for 'instanceof' operator. */
47   unsigned vtable_size;     /**< The size of the vtable for this class. */
48   unsigned clss_flags;      /**< Additional class flags. */
49 } cls_attr;
50
51 /** struct attributes */
52 typedef struct {
53   entity **members;    /**< Fields of this struct. No method entities allowed. */
54 } stc_attr;
55
56 /** A (type, entity) pair */
57 typedef struct {
58   ir_type *tp;         /**< A type. */
59   entity  *ent;        /**< An entity. */
60 } tp_ent_pair;
61
62 /** method attributes */
63 typedef struct {
64   int n_params;                   /**< Number of parameters. */
65   tp_ent_pair *param_type;        /**< Array of parameter type/value entities pairs. */
66   ir_type *value_params;          /**< A type whose entities represent copied value arguments. */
67   int n_res;                      /**< Number of results. */
68   tp_ent_pair *res_type;          /**< Array of result type/value entity pairs. */
69   ir_type *value_ress;            /**< A type whose entities represent copied value results. */
70   variadicity variadicity;        /**< The variadicity of the method. */
71   int first_variadic_param;       /**< The index of the first variadic parameter or -1 if non-variadic .*/
72   unsigned additional_properties; /**< Set of additional method properties. */
73   unsigned irg_calling_conv;      /**< A set of calling convention flags. */
74 } mtd_attr;
75
76 /** union attributes */
77 typedef struct {
78   entity **members;    /**< Fields of this union. No method entities allowed. */
79 } uni_attr;
80
81 /** array attributes */
82 typedef struct {
83   int   n_dimensions;     /**< Number of array dimensions.  */
84   ir_node **lower_bound;  /**< Lower bounds of dimensions.  Usually all 0. */
85   ir_node **upper_bound;  /**< Upper bounds or dimensions. */
86   int *order;             /**< Ordering of dimensions. */
87   ir_type *element_type;  /**< The type of the array elements. */
88   entity *element_ent;    /**< Entity for the array elements, to be used for
89                                element selection with Sel. */
90 } arr_attr;
91
92 /** enum attributes */
93 typedef struct {
94   int      n_enums;    /**< Number of enumerators. */
95   tarval **enumer;     /**< Contains all constants that represent a member
96                           of the enum -- enumerators. */
97   ident  **enum_nameid;/**< Contains the names of the enum fields as specified by
98                           the source program. */
99 } enm_attr;
100
101 /** pointer attributes */
102 typedef struct {
103   ir_type *points_to;  /**< The type of the entity the pointer points to. */
104 } ptr_attr;
105
106 /*
107 typedef struct {   * No private attr yet! *
108 } pri_attr; */
109
110
111 /*
112 typedef struct {        * No private attr, must be smaller than others! *
113 } id_attr;
114 */
115
116 /** General type attributes. */
117 typedef union {
118   cls_attr ca;      /**< Attributes of a class type */
119   stc_attr sa;      /**< Attributes of a struct type */
120   mtd_attr ma;      /**< Attributes of a method type */
121   uni_attr ua;      /**< Attributes of an union type */
122   arr_attr aa;      /**< Attributes of an array type */
123   enm_attr ea;      /**< Attributes of an enumeration type */
124   ptr_attr pa;      /**< Attributes of a pointer type */
125 } tp_attr;
126
127 /** Additional type flags. */
128 enum type_flags {
129   tf_none            = 0,  /**< No flags. */
130   tf_frame_type      = 1,  /**< Set if this is a frame type. */
131   tf_lowered_type    = 2,  /**< Set if this is a lowered type. */
132   tf_layout_fixed    = 4   /**< Set if the layout of a type is fixed */
133 };
134
135 /** the structure of a type */
136 struct ir_type {
137   firm_kind kind;          /**< the firm kind, must be k_type */
138   const tp_op *type_op;    /**< the type operation of the type */
139   ident *name;             /**< The name of the type */
140   visibility visibility;   /**< Visibility of entities of this type. */
141   unsigned flags;          /**< Type flags, a bitmask of enum type_flags. */
142   int size;                /**< Size of an entity of this type. This is determined
143                                 when fixing the layout of this class.  Size must be
144                                 given in bits. */
145   int align;               /**< Alignment of an entity of this type. This should be
146                                 set according to the source language needs. If not set it's
147                                 calculated automatically by get_type_alignment().
148                                 Alignment must be given in bits. */
149   ir_mode *mode;           /**< The mode for atomic types */
150   unsigned long visit;     /**< visited counter for walks of the type information */
151   void *link;              /**< holds temporary data - like in irnode_t.h */
152   struct dbg_info *dbi;    /**< A pointer to information for debug support. */
153   ir_type *assoc_type;     /**< The associated lowered/unlowered type */
154
155   /* ------------- fields for analyses ---------------*/
156
157 #ifdef DEBUG_libfirm
158   long nr;                 /**< An unique node number for each node to make output
159                                 readable. */
160 #endif
161   tp_attr attr;            /**< Type kind specific fields. This must be the last
162                                 entry in this struct!  Varying size! */
163 };
164
165 /**
166  *   Creates a new type representation:
167  *
168  *   @param type_op  the kind of this type.  May not be type_id.
169  *   @param mode     the mode to be used for this type, may be NULL
170  *   @param name     an ident for the name of this type.
171  *   @param db       debug info
172  *
173  *   @return A new type of the given type.  The remaining private attributes are not
174  *           initialized.  The type is in state layout_undefined.
175  */
176 ir_type *
177 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
178 void free_type_attrs       (ir_type *tp);
179
180 void free_class_entities      (ir_type *clss);
181 void free_struct_entities     (ir_type *strct);
182 void free_method_entities     (ir_type *method);
183 void free_union_entities      (ir_type *uni);
184 void free_array_entities      (ir_type *array);
185 void free_enumeration_entities(ir_type *enumeration);
186 void free_pointer_entities    (ir_type *pointer);
187
188 void free_array_automatic_entities(ir_type *array);
189
190 void free_class_attrs      (ir_type *clss);
191 void free_struct_attrs     (ir_type *strct);
192 void free_method_attrs     (ir_type *method);
193 void free_union_attrs      (ir_type *uni);
194 void free_array_attrs      (ir_type *array);
195 void free_enumeration_attrs(ir_type *enumeration);
196 void free_pointer_attrs    (ir_type *pointer);
197
198 void set_class_mode(ir_type *tp, ir_mode *mode);
199 void set_struct_mode(ir_type *tp, ir_mode *mode);
200 void set_pointer_mode(ir_type *tp, ir_mode *mode);
201 void set_primitive_mode(ir_type *tp, ir_mode *mode);
202 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
203
204 void set_class_size_bits(ir_type *tp, int bits);
205 void set_struct_size_bits(ir_type *tp, int bits);
206 void set_union_size_bits(ir_type *tp, int bits);
207 void set_array_size_bits(ir_type *tp, int size);
208 void set_default_size_bits(ir_type *tp, int size);
209
210 /**
211  * Initialize the type module.
212  *
213  * @param builtin_db       debug info for built-in objects
214  * @param default_cc_mask  default calling conventions for methods
215  */
216 void firm_init_type(dbg_info *builtin_db, unsigned default_cc_mask);
217
218
219 /* ------------------- *
220  *  inline functions   *
221  * ------------------- */
222
223 extern unsigned long firm_type_visited;
224
225 static INLINE void _set_master_type_visited(unsigned long val) { firm_type_visited = val; }
226 static INLINE unsigned long _get_master_type_visited(void)     { return firm_type_visited; }
227 static INLINE void _inc_master_type_visited(void)              { ++firm_type_visited; }
228
229 static INLINE void *
230 _get_type_link(const ir_type *tp) {
231   assert(tp && tp->kind == k_type);
232   return(tp -> link);
233 }
234
235 static INLINE void
236 _set_type_link(ir_type *tp, void *l) {
237   assert(tp && tp->kind == k_type);
238   tp -> link = l;
239 }
240
241 static INLINE const tp_op*
242 _get_type_tpop(const ir_type *tp) {
243   assert(tp && tp->kind == k_type);
244   return tp->type_op;
245 }
246
247 static INLINE ident*
248 _get_type_tpop_nameid(const ir_type *tp) {
249   assert(tp && tp->kind == k_type);
250   return get_tpop_ident(tp->type_op);
251 }
252
253 static INLINE tp_opcode
254 _get_type_tpop_code(const ir_type *tp) {
255   assert(tp && tp->kind == k_type);
256   return get_tpop_code(tp->type_op);
257 }
258
259 static INLINE ir_mode *
260 _get_type_mode(const ir_type *tp) {
261   assert(tp && tp->kind == k_type);
262   return tp->mode;
263 }
264
265 static INLINE ident *
266 _get_type_ident(const ir_type *tp) {
267   assert(tp && tp->kind == k_type);
268   return tp->name;
269 }
270
271 static INLINE void
272 _set_type_ident(ir_type *tp, ident* id) {
273   assert(tp && tp->kind == k_type);
274   tp->name = id;
275 }
276
277 static INLINE int
278 _get_type_size_bits(const ir_type *tp) {
279   assert(tp && tp->kind == k_type);
280   return tp->size;
281 }
282
283 static INLINE int
284 _get_type_size_bytes(const ir_type *tp) {
285   int size = _get_type_size_bits(tp);
286   if (size < 0)
287     return -1;
288   if ((size & 7) != 0) {
289     assert(0 && "cannot take byte size of this type");
290     return -1;
291   }
292   return size >> 3;
293 }
294
295 static INLINE type_state
296 _get_type_state(const ir_type *tp) {
297   assert(tp && tp->kind == k_type);
298   return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
299 }
300
301 static INLINE unsigned long
302 _get_type_visited(const ir_type *tp) {
303   assert(tp && tp->kind == k_type);
304   return tp->visit;
305 }
306
307 static INLINE void
308 _set_type_visited(ir_type *tp, unsigned long num) {
309   assert(tp && tp->kind == k_type);
310   tp->visit = num;
311 }
312
313 static INLINE void
314 _mark_type_visited(ir_type *tp) {
315   assert(tp && tp->kind == k_type);
316   assert(tp->visit < firm_type_visited);
317   tp->visit = firm_type_visited;
318 }
319
320 static INLINE int
321 _type_visited(const ir_type *tp) {
322   assert(tp && tp->kind == k_type);
323   return tp->visit >= firm_type_visited;
324 }
325
326 static INLINE int
327 _type_not_visited(const ir_type *tp) {
328   assert(tp && tp->kind == k_type);
329   return tp->visit  < firm_type_visited;
330 }
331
332 static INLINE int
333 _is_type(const void *thing) {
334   return (get_kind(thing) == k_type);
335 }
336
337 static INLINE int
338 _is_class_type(const ir_type *clss) {
339   assert(clss);
340   return (clss->type_op == type_class);
341 }
342
343 static INLINE int
344 _get_class_n_members (const ir_type *clss) {
345   assert(clss && (clss->type_op == type_class));
346   return (ARR_LEN (clss->attr.ca.members));
347 }
348
349 static INLINE entity *
350 _get_class_member   (const ir_type *clss, int pos) {
351   assert(clss && (clss->type_op == type_class));
352   assert(pos >= 0 && pos < _get_class_n_members(clss));
353   return clss->attr.ca.members[pos];
354 }
355
356 static INLINE unsigned
357 _get_class_vtable_size(const ir_type *clss) {
358   assert(clss && (clss->type_op == type_class));
359   return clss->attr.ca.vtable_size;
360 }
361
362 static INLINE void
363 _set_class_vtable_size(ir_type *clss, unsigned vtable_size) {
364   assert(clss && (clss->type_op == type_class));
365   clss->attr.ca.vtable_size = vtable_size;
366 }
367
368 static INLINE int
369 _is_class_final(const ir_type *clss) {
370   assert(clss && (clss->type_op == type_class));
371   return clss->attr.ca.clss_flags & cf_final_class;
372 }
373
374 static INLINE void
375 _set_class_final(ir_type *clss, int final) {
376   assert(clss && (clss->type_op == type_class));
377   if (final)
378     clss->attr.ca.clss_flags |= cf_final_class;
379   else
380     clss->attr.ca.clss_flags &= ~cf_final_class;
381 }
382
383 static INLINE int
384 _is_class_interface(const ir_type *clss) {
385   assert(clss && (clss->type_op == type_class));
386   return clss->attr.ca.clss_flags & cf_interface_class;
387 }
388
389 static INLINE void
390 _set_class_interface(ir_type *clss, int final) {
391   assert(clss && (clss->type_op == type_class));
392   if (final)
393     clss->attr.ca.clss_flags |= cf_interface_class;
394   else
395     clss->attr.ca.clss_flags &= ~cf_interface_class;
396 }
397
398 static INLINE int
399 _is_class_abstract(const ir_type *clss) {
400   assert(clss && (clss->type_op == type_class));
401   return clss->attr.ca.clss_flags & cf_absctract_class;
402 }
403
404 static INLINE void
405 _set_class_abstract(ir_type *clss, int final) {
406   assert(clss && (clss->type_op == type_class));
407   if (final)
408     clss->attr.ca.clss_flags |= cf_absctract_class;
409   else
410     clss->attr.ca.clss_flags &= ~cf_absctract_class;
411 }
412
413 static INLINE int
414 _is_struct_type(const ir_type *strct) {
415   assert(strct);
416   return (strct->type_op == type_struct);
417 }
418
419 static INLINE int
420 _is_method_type(const ir_type *method) {
421   assert(method);
422   return (method->type_op == type_method);
423 }
424
425 static INLINE int
426 _is_union_type(const ir_type *uni) {
427   assert(uni);
428   return (uni->type_op == type_union);
429 }
430
431 static INLINE int
432 _is_array_type(const ir_type *array) {
433   assert(array);
434   return (array->type_op == type_array);
435 }
436
437 static INLINE int
438 _is_enumeration_type(const ir_type *enumeration) {
439   assert(enumeration);
440   return (enumeration->type_op == type_enumeration);
441 }
442
443 static INLINE int
444 _is_pointer_type(const ir_type *pointer) {
445   assert(pointer);
446   return (pointer->type_op == type_pointer);
447 }
448
449 /** Returns true if a type is a primitive type. */
450 static INLINE int
451 _is_primitive_type(const ir_type *primitive) {
452   assert(primitive && primitive->kind == k_type);
453   return (primitive->type_op == type_primitive);
454 }
455
456 static INLINE int
457 _is_atomic_type(const ir_type *tp) {
458   assert(tp && tp->kind == k_type);
459   return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
460       _is_enumeration_type(tp));
461 }
462
463 static INLINE int
464 _get_method_n_params(const ir_type *method) {
465   assert(method && (method->type_op == type_method));
466   return method->attr.ma.n_params;
467 }
468
469 static INLINE int
470 _get_method_n_ress(const ir_type *method) {
471   assert(method && (method->type_op == type_method));
472   return method->attr.ma.n_res;
473 }
474
475 static INLINE unsigned
476 _get_method_additional_properties(const ir_type *method) {
477   assert(method && (method->type_op == type_method));
478   return method->attr.ma.additional_properties;
479 }
480
481 static INLINE void
482 _set_method_additional_properties(ir_type *method, unsigned mask) {
483   assert(method && (method->type_op == type_method));
484
485   /* do not allow to set the mtp_property_inherited flag or
486    * the automatic inheritance of flags will not work */
487   method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
488 }
489
490 static INLINE void
491 _set_method_additional_property(ir_type *method, mtp_additional_property flag) {
492   assert(method && (method->type_op == type_method));
493
494   /* do not allow to set the mtp_property_inherited flag or
495    * the automatic inheritance of flags will not work */
496   method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
497 }
498
499 static INLINE unsigned
500 _get_method_calling_convention(const ir_type *method) {
501   assert(method && (method->type_op == type_method));
502   return method->attr.ma.irg_calling_conv;
503 }
504
505 static INLINE void
506 _set_method_calling_convention(ir_type *method, unsigned cc_mask) {
507   assert(method && (method->type_op == type_method));
508   method->attr.ma.irg_calling_conv = cc_mask;
509 }
510
511 #define set_master_type_visited(val)      _set_master_type_visited(val)
512 #define get_master_type_visited()         _get_master_type_visited()
513 #define inc_master_type_visited()         _inc_master_type_visited()
514 #define get_type_link(tp)                 _get_type_link(tp)
515 #define set_type_link(tp, l)              _set_type_link(tp, l)
516 #define get_type_tpop(tp)                 _get_type_tpop(tp)
517 #define get_type_tpop_nameid(tp)          _get_type_tpop_nameid(tp)
518 #define get_type_tpop_code(tp)            _get_type_tpop_code(tp)
519 #define get_type_mode(tp)                 _get_type_mode(tp)
520 #define get_type_ident(tp)                _get_type_ident(tp)
521 #define set_type_ident(tp, id)            _set_type_ident(tp, id)
522 #define get_type_size_bits(tp)            _get_type_size_bits(tp)
523 #define get_type_size_bytes(tp)           _get_type_size_bytes(tp)
524 #define get_type_state(tp)                _get_type_state(tp)
525 #define get_type_visited(tp)              _get_type_visited(tp)
526 #define set_type_visited(tp, num)         _set_type_visited(tp, num)
527 #define mark_type_visited(tp)             _mark_type_visited(tp)
528 #define type_visited(tp)                  _type_visited(tp)
529 #define type_not_visited(tp)              _type_not_visited(tp)
530 #define is_type(thing)                    _is_type(thing)
531 #define is_Class_type(clss)               _is_class_type(clss)
532 #define get_class_n_members(clss)         _get_class_n_members(clss)
533 #define get_class_member(clss, pos)       _get_class_member(clss, pos)
534 #define get_class_vtable_size(clss)       _get_class_vtable_size(clss)
535 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
536 #define is_class_final(clss)              _is_class_final(clss)
537 #define set_class_final(clss, flag)       _set_class_final(clss, flag)
538 #define is_class_interface(clss)          _is_class_interface(clss)
539 #define set_class_interface(clss, flag)   _set_class_interface(clss, flag)
540 #define is_class_abstract(clss)           _is_class_abstract(clss)
541 #define set_class_abstract(clss, flag)    _set_class_abstract(clss, flag)
542 #define is_Struct_type(strct)             _is_struct_type(strct)
543 #define is_Method_type(method)            _is_method_type(method)
544 #define is_Union_type(uni)                _is_union_type(uni)
545 #define is_Array_type(array)              _is_array_type(array)
546 #define is_Enumeration_type(enumeration)  _is_enumeration_type(enumeration)
547 #define is_Pointer_type(pointer)          _is_pointer_type(pointer)
548 #define is_Primitive_type(primitive)      _is_primitive_type(primitive)
549 #define is_atomic_type(tp)                _is_atomic_type(tp)
550 #define get_method_n_params(method)       _get_method_n_params(method)
551 #define get_method_n_ress(method)         _get_method_n_ress(method)
552 #define get_method_additional_properties(method)        _get_method_additional_properties(method)
553 #define set_method_additional_properties(method, mask)  _set_method_additional_properties(method, mask)
554 #define set_method_additional_property(method, flag)    _set_method_additional_property(method, flag)
555 #define get_method_calling_convention(method)           _get_method_calling_convention(method)
556 #define set_method_calling_convention(method, cc_mask)  _set_method_calling_convention(method, cc_mask)
557
558 #endif /* _FIRM_TR_TYPE_T_H_ */