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