renamed old IrgVrfy() into new IrgVerify()
[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:
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 _TYPE_T_H_
14 # define _TYPE_T_H_
15
16 # include "firm_config.h"
17 # include "type.h"
18 # include "tpop_t.h"
19
20 # include "array.h"
21
22 /**
23  * @file type_t.h
24  * This file contains the datatypes hidden in type.h.
25  *
26  * @author Goetz Lindenmaier
27  * @see  type.h tpop_t.h tpop.h
28  */
29
30 /** class attributes */
31 typedef struct {
32   entity **members;    /**< fields and methods of this class */
33   type   **subtypes;   /**< direct subtypes */
34   type   **supertypes; /**< direct supertypes */
35   peculiarity peculiarity;
36   int dfn;             /**< number used for 'instanceof' operator */
37 } cls_attr;
38
39 /** struct attributes */
40 typedef struct {
41   entity **members;    /**< fields of this struct. No method entities allowed. */
42 } stc_attr;
43
44 /** method attributes */
45 typedef struct {
46   int n_params;              /**< number of parameters */
47   type **param_type;         /**< code generation needs this information. */
48   type *value_params;        /**< A type whose entities represent copied value arguments. */
49   int n_res;                 /**< number of results */
50   type **res_type;           /**< array with result types */
51   type *value_ress;          /**< A type whose entities represent copied value results. */
52   variadicity variadicity;   /**< variadicity of the method. */
53   int first_variadic_param;  /**< index of the first variadic param or -1 if non-variadic .*/
54 } mtd_attr;
55
56 /** union attributes */
57 typedef struct {
58   entity **members;    /**< fields of this union. No method entities allowed. */
59 } uni_attr;
60
61 /** array attributes */
62 typedef struct {
63   int   n_dimensions;  /**< Number of array dimensions.  */
64   ir_node **lower_bound;   /**< Lower bounds of dimensions.  Usually all 0. */
65   ir_node **upper_bound;   /**< Upper bounds or dimensions. */
66   int *order;              /**< Ordering of dimensions. */
67   type *element_type;  /**< The type of the array elements. */
68   entity *element_ent; /**< Entity for the array elements, to be used for
69               element selection with Sel. */
70 } arr_attr;
71
72 /** enum attributes */
73 typedef struct {
74   int      n_enums;    /**< Number of enumerators. */
75   tarval **enumer;     /**< Contains all constants that represent a member
76                           of the enum -- enumerators. */
77   ident  **enum_nameid;/**< Contains the names of the enum fields as specified by
78                           the source program */
79 } enm_attr;
80
81 /** pointer attributes */
82 typedef struct {
83   type *points_to;     /**< The type of the entity the pointer points to. */
84 } ptr_attr;
85
86 /*
87 typedef struct {   * No private attr yet! *
88 } pri_attr; */
89
90
91 /*
92 typedef struct {        * No private attr, must be smaller than others! *
93 } id_attr;
94 */
95
96 /** General type attributes. */
97 typedef union {
98   cls_attr ca;      /**< attributes of a class type */
99   stc_attr sa;      /**< attributes of a struct type */
100   mtd_attr ma;      /**< attributes of a method type */
101   uni_attr ua;      /**< attributes of an union type */
102   arr_attr aa;      /**< attributes of an array type */
103   enm_attr ea;      /**< attributes of an enumeration type */
104   ptr_attr pa;      /**< attributes of a pointer type */
105 } tp_attr;
106
107 /** the structure of a type */
108 struct type {
109   firm_kind kind;          /**< the firm kind, must be k_type */
110   const tp_op *type_op;    /**< the type operation of the type */
111   ident *name;             /**< The name of the type */
112   visibility visibility;   /**< Visibility of entities of this type. */
113   char frame_type;         /**< True if this is a frame type, false else */
114   type_state state;        /**< Represents the types state: layout undefined or
115                                 fixed. */
116   int size;                /**< Size of an entity of this type. This is determined
117                                 when fixing the layout of this class.  Size must be
118                                 given in bits. */
119   int align;               /**< Alignment of an entity of this type. This should be
120                                 set according to the source language needs. If not set it's
121                                 calculated automatically by get_type_alignment().
122                                 Alignment must be given in bits. */
123   ir_mode *mode;           /**< The mode for atomic types */
124   unsigned long visit;     /**< visited counter for walks of the type information */
125   void *link;              /**< holds temporary data - like in irnode_t.h */
126   struct dbg_info *dbi;    /**< A pointer to information for debug support. */
127
128   /* ------------- fields for analyses ---------------*/
129
130 #ifdef DEBUG_libfirm
131   int nr;             /**< a unique node number for each node to make output
132                            readable. */
133 #endif
134   tp_attr attr;            /* type kind specific fields. This must be the last
135                   entry in this struct!  Varying size! */
136 };
137
138 /**
139  *   Creates a new type representation:
140  *
141  *   @param type_op  the kind of this type.  May not be type_id.
142  *   @param mode     the mode to be used for this type, may be NULL
143  *   @param name     an ident for the name of this type.
144  *   @param db       debug info
145  *
146  *   @return A new type of the given type.  The remaining private attributes are not
147  *           initialized.  The type is in state layout_undefined.
148  */
149 INLINE type *
150 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
151 void free_type_attrs       (type *tp);
152
153 INLINE void free_class_entities      (type *clss);
154 INLINE void free_struct_entities     (type *strct);
155 INLINE void free_method_entities     (type *method);
156 INLINE void free_union_entities      (type *uni);
157 INLINE void free_array_entities      (type *array);
158 INLINE void free_enumeration_entities(type *enumeration);
159 INLINE void free_pointer_entities    (type *pointer);
160 INLINE void free_primitive_entities  (type *primitive);
161
162 INLINE void free_class_attrs      (type *clss);
163 INLINE void free_struct_attrs     (type *strct);
164 INLINE void free_method_attrs     (type *method);
165 INLINE void free_union_attrs      (type *uni);
166 INLINE void free_array_attrs      (type *array);
167 INLINE void free_enumeration_attrs(type *enumeration);
168 INLINE void free_pointer_attrs    (type *pointer);
169 INLINE void free_primitive_attrs  (type *primitive);
170
171
172 /**
173  * Initialize the type module.
174  *
175  * @param buildin_db  debug info for builtin objects
176  */
177 void firm_init_type(dbg_info *builtin_db);
178
179
180 /* ------------------- *
181  *  inline functions   *
182  * ------------------- */
183
184 extern unsigned long firm_type_visited;
185
186 static INLINE void _set_master_type_visited(unsigned long val) { firm_type_visited = val; }
187 static INLINE unsigned long _get_master_type_visited(void)     { return firm_type_visited; }
188 static INLINE void _inc_master_type_visited(void)              { ++firm_type_visited; }
189
190 static INLINE void *
191 _get_type_link(const type *tp) {
192   assert(tp && tp->kind == k_type);
193   return(tp -> link);
194 }
195
196 static INLINE void
197 _set_type_link(type *tp, void *l) {
198   assert(tp && tp->kind == k_type);
199   tp -> link = l;
200 }
201
202 static INLINE const tp_op*
203 _get_type_tpop(const type *tp) {
204   assert(tp && tp->kind == k_type);
205   return tp->type_op;
206 }
207
208 static INLINE ident*
209 _get_type_tpop_nameid(const type *tp) {
210   assert(tp && tp->kind == k_type);
211   return get_tpop_ident(tp->type_op);
212 }
213
214 static INLINE tp_opcode
215 _get_type_tpop_code(const type *tp) {
216   assert(tp && tp->kind == k_type);
217   return get_tpop_code(tp->type_op);
218 }
219
220 static INLINE ir_mode *
221 _get_type_mode(const type *tp) {
222   assert(tp && tp->kind == k_type);
223   return tp->mode;
224 }
225
226 static INLINE ident *
227 _get_type_ident(const type *tp) {
228   assert(tp && tp->kind == k_type);
229   return tp->name;
230 }
231
232 static INLINE void
233 _set_type_ident(type *tp, ident* id) {
234   assert(tp && tp->kind == k_type);
235   tp->name = id;
236 }
237
238 static INLINE int
239 _get_type_size_bits(const type *tp) {
240   assert(tp && tp->kind == k_type);
241   return tp->size;
242 }
243
244 static INLINE int
245 _get_type_size_bytes(const type *tp) {
246   int size = _get_type_size_bits(tp);
247   if (size < 0)
248     return -1;
249   if ((size & 7) != 0) {
250     assert(0 && "cannot take byte size of this type");
251     return -1;
252   }
253   return size >> 3;
254 }
255
256 static INLINE type_state
257 _get_type_state(const type *tp) {
258   assert(tp && tp->kind == k_type);
259   return tp->state;
260 }
261
262 static INLINE unsigned long
263 _get_type_visited(const type *tp) {
264   assert(tp && tp->kind == k_type);
265   return tp->visit;
266 }
267
268 static INLINE void
269 _set_type_visited(type *tp, unsigned long num) {
270   assert(tp && tp->kind == k_type);
271   tp->visit = num;
272 }
273
274 static INLINE void
275 _mark_type_visited(type *tp) {
276   assert(tp && tp->kind == k_type);
277   assert(tp->visit < firm_type_visited);
278   tp->visit = firm_type_visited;
279 }
280
281 static INLINE int
282 _type_visited(const type *tp) {
283   assert(tp && tp->kind == k_type);
284   return tp->visit >= firm_type_visited;
285 }
286
287 static INLINE int
288 _type_not_visited(const type *tp) {
289   assert(tp && tp->kind == k_type);
290   return tp->visit  < firm_type_visited;
291 }
292
293 static INLINE int
294 _is_type(const void *thing) {
295   return (get_kind(thing) == k_type);
296 }
297
298 static INLINE int
299 _is_class_type(const type *clss) {
300   assert(clss);
301   return (clss->type_op == type_class);
302 }
303
304 static INLINE int
305 _get_class_n_members (const type *clss) {
306   assert(clss && (clss->type_op == type_class));
307   return (ARR_LEN (clss->attr.ca.members));
308 }
309
310 static INLINE entity *
311 _get_class_member   (const type *clss, int pos) {
312   assert(clss && (clss->type_op == type_class));
313   assert(pos >= 0 && pos < _get_class_n_members(clss));
314   return clss->attr.ca.members[pos];
315 }
316
317 static INLINE int
318 _is_struct_type(const type *strct) {
319   assert(strct);
320   return (strct->type_op == type_struct);
321 }
322
323 static INLINE int
324 _is_method_type(const type *method) {
325   assert(method);
326   return (method->type_op == type_method);
327 }
328
329 static INLINE int
330 _is_union_type(const type *uni) {
331   assert(uni);
332   return (uni->type_op == type_union);
333 }
334
335 static INLINE int
336 _is_array_type(const type *array) {
337   assert(array);
338   return (array->type_op == type_array);
339 }
340
341 static INLINE int
342 _is_enumeration_type(const type *enumeration) {
343   assert(enumeration);
344   return (enumeration->type_op == type_enumeration);
345 }
346
347 static INLINE int
348 _is_pointer_type(const type *pointer) {
349   assert(pointer);
350   return (pointer->type_op == type_pointer);
351 }
352
353 /** Returns true if a type is a primitive type. */
354 static INLINE int
355 _is_primitive_type(const type *primitive) {
356   assert(primitive && primitive->kind == k_type);
357   return (primitive->type_op == type_primitive);
358 }
359
360 static INLINE int
361 _is_atomic_type(const type *tp) {
362   assert(tp && tp->kind == k_type);
363   return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
364       _is_enumeration_type(tp));
365 }
366
367
368 #define set_master_type_visited(val)      _set_master_type_visited(val)
369 #define get_master_type_visited()         _get_master_type_visited()
370 #define inc_master_type_visited()         _inc_master_type_visited()
371 #define get_type_link(tp)                 _get_type_link(tp)
372 #define set_type_link(tp, l)              _set_type_link(tp, l)
373 #define get_type_tpop(tp)                 _get_type_tpop(tp)
374 #define get_type_tpop_nameid(tp)          _get_type_tpop_nameid(tp)
375 #define get_type_tpop_code(tp)            _get_type_tpop_code(tp)
376 #define get_type_mode(tp)                 _get_type_mode(tp)
377 #define get_type_ident(tp)                _get_type_ident(tp)
378 #define set_type_ident(tp, id)            _set_type_ident(tp, id)
379 #define get_type_size(tp)                 _get_type_size(tp)
380 #define get_type_state(tp)                _get_type_state(tp)
381 #define get_type_visited(tp)              _get_type_visited(tp)
382 #define set_type_visited(tp, num)         _set_type_visited(tp, num)
383 #define mark_type_visited(tp)             _mark_type_visited(tp)
384 #define type_visited(tp)                  _type_visited(tp)
385 #define type_not_visited(tp)              _type_not_visited(tp)
386 #define is_type(thing)                    _is_type(thing)
387 #define is_Class_type(clss)               _is_class_type(clss)
388 #define get_class_n_members(clss)         _get_class_n_members(clss)
389 #define get_class_member(clss, pos)       _get_class_member(clss, pos)
390 #define is_Struct_type(strct)             _is_struct_type(strct)
391 #define is_Method_type(method)            _is_method_type(method)
392 #define is_Union_type(uni)                _is_union_type(uni)
393 #define is_Array_type(array)              _is_array_type(array)
394 #define is_Enumeration_type(enumeration)  _is_enumeration_type(enumeration)
395 #define is_Pointer_type(pointer)          _is_pointer_type(pointer)
396 #define is_Primitive_type(primitive)      _is_primitive_type(primitive)
397 #define is_atomic_type(tp)                _is_atomic_type(tp)
398
399 # endif /* _TYPE_T_H_ */