removed a bug in Cond_kind access routines
[libfirm] / ir / tr / type.h
1 /****h* libfirm/type
2  *
3  * NAME
4  *   file type.h - datastructure to hold type information.
5  * COPYRIGHT
6  *  (C) 2001 by Universitaet Karlsruhe
7  * AUTHORS
8  *  Goetz Lindenmaier
9  *
10  * NOTES
11  *  This module supplies a datastructure to represent all types
12  *  known in the compiled program.  This includes types specified
13  *  in the program as well as types defined by the language.  In the
14  *  view of the intermediate representation there is no difference
15  *  between these types.
16  *
17  *  There exist several kinds of types, arranged by the structure of
18  *  the type.  A type is described by a set of attributes.  Some of
19  *  these attributes are common to all types, others depend on the
20  *  kind of the type.
21  *
22  *  Types are different from the modes defined in irmode:  Types are
23  *  on the level of the programming language, modes at the level of
24  *  the target processor.
25  *
26  * SEE ALSO
27  *   tpop.h
28  *****
29  */
30 # ifndef _TYPE_H_
31 # define _TYPE_H_
32
33 # include "tpop.h"
34 # include "common.h"
35 # include "ident.h"
36 # include "irmode.h"
37 # include "bool.h"
38
39 #ifndef _ENTITY_TYPEDEF_
40 #define _ENTITY_TYPEDEF_
41 /* to resolve recursion between entity.h and type.h */
42 typedef struct entity entity;
43 #endif
44
45 #ifndef _IR_NODE_TYPEDEF_
46 #define _IR_NODE_TYPEDEF_
47 typedef struct ir_node ir_node;
48 #endif
49
50 /****s* type/type
51  *
52  * NAME
53  *   type - An abstract data type to represent types.
54  * NOTE
55  *  This is the abstract data type with which any type known in the
56  *  compiled program can be represented.  This includes types specified
57  *  in the program as well as types defined by the language.  In the
58  *  view of the intermediate representation there is no difference
59  *  between these types.
60  *
61  *  There exist several kinds of types, arranged by the structure of
62  *  the type.  These are distinguished by a type opcode.
63  *  A type is described by a set of attributes.  Some of these attributes
64  *  are common to all types, others depend on the kind of the type.
65  *
66  *  The following describes the common attributes.  They can only be
67  *  accessed by the functions given below.
68  *
69  * ATTRIBUTES
70  *  The common fields are:
71  *
72  *  firm_kind    A firm_kind tag containing k_type.  This is useful
73  *               for dynamically checking whether a node is a type node.
74  *  type_op      A tp_op specifying the kind of the type.
75  *  mode         The mode to be used to represent the type on a machine.
76  *               @@@ maybe not global field??
77  *  name         An identifier specifying the name of the type.  To be
78  *               set by the frontend.
79  *  size         The size of the type, i.e. an entity of this type will
80  *               occupy size bytes in memory.  In several cases this is
81  *               determined when fixing the layout of this type (class,
82  *               struct, union, array, enumeration).
83  *  visit        A counter for walks of the type information.
84  *
85  *  These fields can only be accessed via access functions.
86  *
87  *  Depending on the value of type_op, i.e., depending on the kind of the
88  *  type the adt contains further attributes.  These are documented below.
89  * SEE ALSO
90  *   class, struct, method, union, array, enumeration, pointer, primitive
91  * SOURCE
92  */
93 typedef struct type type;
94
95 tp_op*      get_type_tpop(type *tp);
96 ident*      get_type_tpop_nameid(type *tp);
97 const char* get_type_tpop_name(type *tp);
98 tp_opcode   get_type_tpop_code(type *tp);
99
100 ir_mode*    get_type_mode(type *tp);
101 void        set_type_mode(type *tp, ir_mode* m);
102
103 ident*      get_type_nameid(type *tp);
104 void        set_type_nameid(type *tp, ident* id);
105 const char* get_type_name(type *tp);
106
107 int         get_type_size(type *tp);
108 /* For primitives and pointer types the size is always fixed.
109    This call is legal but has no effect. */
110 void        set_type_size(type *tp, int size);
111
112 typedef enum {
113   layout_undefined,    /* The layout of this type is not defined.
114                           Address computation to access fields is not
115                           possible, fields must be accessed by Sel nodes.
116                           This is the default value except for pointer and
117                           primitive types. */
118   layout_fixed         /* The layout is fixed, all component/member entities
119                           have an offset assigned.  Size of the type is known.
120                           Arrays can be accessed by explicit address
121                           computation. Default for pointer and primitive types.
122                        */
123 } type_state;
124
125 type_state  get_type_state(type *tp);
126 /* For primitives and pointer types the layout is always fixed.
127    This call is legal but has no effect. */
128 void        set_type_state(type *tp, type_state state);
129
130 unsigned long get_type_visited(type *tp);
131 void        set_type_visited(type *tp, unsigned long num);
132 /* Sets visited field in type to type_visited. */
133 void        mark_type_visited(type *tp);
134 /*****/
135
136 /****v* type/visited
137  *
138  * NAME
139  *   type_visited -  visited flag to traverse the type information
140  * PURPOSE
141  *   Increase this flag by one before traversing the type information.
142  *   Mark type nodes as visited by set_type_visited(type, type_visited).
143  *   Check whether node was already visited by comparing get_type_visited(type)
144  *   and type_visited.
145  *   Or use the function to walk all types.
146  * SEE ALSO
147  *   typewalk
148  * SOURCE
149  */
150 extern unsigned long type_visited;
151 /*****/
152
153 /****f* type/is_type
154  *
155  * NAME
156  *   is_type - Checks whether a pointer points to a type.
157  * SYNOPSIS
158  *   bool is_type            (void *thing);
159  * INPUTS
160  *   thing - a pointer
161  * RESULT
162  *   true if the thing is a type, else false
163  ***
164  */
165 int is_type            (void *thing);
166
167 /****** type/class
168  * NAME
169  *  Representation of a class type.
170  * NOTE
171  *  If the type opcode is set to type_class the type represents class
172  *  types.  A list of fields and methods is associated with a class.
173  *  Further a class can inherit from and bequest to other classes.
174  *  @@@ value class???
175  * ATTRIBUTES
176  *  The following attributes are private to this type kind.
177  *  member     All entities belonging to this class.  This are methode entities
178  *             which have type_method or fields that can have any of the
179  *             following type kinds: type_class, type_struct, type_union,
180  *             type_array, type_enumeration, type_pointer, type_primitive.
181  *
182  *  subtypes   A list of direct subclasses.
183  *
184  *  supertypes A list of direct superclasses.
185  *
186  *  These are dynamic lists that can be grown with an "add_" function,
187  *  but not shrinked.
188  * SOURCE
189  */
190 /* create a new class type */
191 type   *new_type_class (ident *name);
192
193 /* manipulate private fields of class type  */
194 void    add_class_member   (type *clss, entity *member);
195 int     get_class_n_member (type *clss);
196 entity *get_class_member   (type *clss, int pos);
197 void    set_class_member   (type *clss, entity *member, int pos);
198
199 void    add_class_subtype   (type *clss, type *subtype);
200 int     get_class_n_subtype (type *clss);
201 type   *get_class_subtype   (type *clss, int pos);
202 void    set_class_subtype   (type *clss, type *subtype, int pos);
203
204 void    add_class_supertype   (type *clss, type *supertype);
205 int     get_class_n_supertype (type *clss);
206 type   *get_class_supertype   (type *clss, int pos);
207 void    set_class_supertype   (type *clss, type *supertype, int pos);
208
209 /* typecheck */
210 bool    is_class_type(type *clss);
211 /*****/
212
213 /****** type/struct
214  * NAME
215  *  Representation of a struct type.
216  * NOTE
217  *  Type_strct represents aggregate types that consist of a list
218  *  of fields.
219  * ATTRIBUTES
220  *  member   All entities belonging to this class.  This are the fields
221  *           that can have any of the following types:  type_class,
222  *           type_struct, type_union, type_array, type_enumeration,
223  *           type_pointer, type_primitive.
224  *           This is a dynamic list that can be grown with an "add_" function,
225  *           but not shrinked.
226  *           This is a dynamic list that can be grown with an "add_" function,
227  *           but not shrinked.
228  * SOURCE
229  */
230 /* create a new type struct */
231 type   *new_type_struct (ident *name);
232
233 /* manipulate private fields of struct */
234 void    add_struct_member   (type *strct, entity *member);
235 int     get_struct_n_member (type *strct);
236 entity *get_struct_member   (type *strct, int pos);
237 void    set_struct_member   (type *strct, int pos, entity *member);
238
239 /* typecheck */
240 bool    is_struct_type(type *strct);
241 /*****/
242
243 /****** type/method
244  * NAME
245  *  Representation of a method type.
246  * NOTE
247  *  A method type represents a method, function or procedure type.
248  *  It contains a list of the parameter and result types, as these
249  *  are part of the type description.  These lists should not
250  *  be changed by a optimization, as a change creates a new method
251  *  type.  Therefore optimizations should allocated new method types.
252  *  The set_ routines are only for construction by a frontend.
253  * ATTRIBUTES
254  *  n_params    Number of parameters to the procedure.
255  *              A procedure in FIRM has only call by value parameters.
256  *
257  *  param_type  A list with the types of parameters.  This list is ordered.
258  *              The nth type in this list corresponds to the nth element
259  *              in the parameter tuple that is a result of the start node.
260  *              (See ircons.h for more information.)
261  *
262  *  n_res       The number of results of the method.  In general, procedures
263  *              have zero results, functions one.
264  *
265  *  res_type    A list with the types of parameters.  This list is ordered.
266  *              The nth type in this list corresponds to the nth input to
267  *              Return nodes.  (See ircons.h for more information.)
268  * SOURCE
269  */
270
271 /* Create a new method type.
272    N_param is the number of parameters, n_res the number of results.
273    The arrays for the parameter and result types are not initialized by
274    the constructor. */
275 type *new_type_method (ident *name, int n_param, int n_res);
276
277 /* manipulate private fields of method. */
278 int   get_method_n_params  (type *method);
279 type *get_method_param_type(type *method, int pos);
280 void  set_method_param_type(type *method, int pos, type* type);
281
282 int   get_method_n_res   (type *method);
283 type *get_method_res_type(type *method, int pos);
284 void  set_method_res_type(type *method, int pos, type* type);
285
286 /* typecheck */
287 bool  is_method_type     (type *method);
288 /*****/
289
290 /****** type/union
291  * NAME
292  *   Representation of a union type.
293  * NOTE
294  *   The union type represents union types.
295  * ATTRIBUTES
296  *   n_types        Number of unioned types.
297  *   members        Entities for unioned types.  Fixed length array.
298  *                  This is a dynamic list that can be grown with an "add_" function,
299  *                  but not shrinked.
300  * SOURCE
301  */
302 /* create a new type union  */
303 type   *new_type_union (ident *name);
304
305 /* manipulate private fields of struct */
306 int     get_union_n_members      (type *uni);
307 void    add_union_member (type *uni, entity *member);
308 entity *get_union_member (type *uni, int pos);
309 void    set_union_member (type *uni, int pos, entity *member);
310
311 /* typecheck */
312 bool    is_union_type          (type *uni);
313 /*****/
314
315 #if 0
316 /* We don't need these if the union has entities, which it now
317    does. The entities are necessary for the analysis algorithms. */
318 type  *get_union_unioned_type (type *uni, int pos);
319 void   set_union_unioned_type (type *uni, int pos, type *type);
320
321 ident *get_union_delim_nameid (type *uni, int pos);
322 const char *get_union_delim_name (type *uni, int pos);
323 void   set_union_delim_nameid (type *uni, int pos, ident *id);
324 #endif
325
326 /****** type/array
327  * NAME
328  *   Representation of an array type.
329  * NOTE
330  *   The array type represents rectangular multi dimensional arrays.
331  * ATTRIBUTES
332  *   n_dimensions     Number of array dimensions.
333  *   *lower_bound     Lower bounds of dimensions.  Usually all 0.
334  *   *upper_bound     Upper bounds or dimensions.
335  *   *element_type    The type of the array elements.
336  *   *element_ent     An entity for the array elements to be used for
337  *                    element selection with Sel.
338  *                    @@@ Do we need several entities?  One might want
339  *                    to select a dimension and not a single element in
340  *                    case of multidim arrays.
341  * SOURCE
342  */
343 /* create a new type array --
344    Set dimension sizes after call to constructor with set_* routines.
345    Entity for array elements is built automatically. */
346 type *new_type_array         (ident *name, int n_dimensions,
347                               type *element_type);
348
349 /* manipulate private fields of array type */
350 int   get_array_n_dimensions (type *array);
351 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
352                                                           ir_node *upper_bound);
353 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
354 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
355 ir_node * get_array_lower_bound  (type *array, int dimension);
356 ir_node * get_array_upper_bound  (type *array, int dimension);
357
358 void  set_array_element_type (type *array, type *type);
359 type *get_array_element_type (type *array);
360
361 void  set_array_element_entity (type *array, entity *ent);
362 entity *get_array_element_entity (type *array);
363
364 /* typecheck */
365 bool   is_array_type         (type *array);
366 /*****/
367
368 /****** type/enumeration
369  * NAME
370  *  Representation of an enumeration type.
371  * NOTE
372  *  Enumeration types need not necessarily be represented explicitly
373  *  by Firm types, as the frontend can lower them to integer constants as
374  *  well.  For debugging purposes or similar tasks this information is useful.
375  * ATTRIBUTES
376  *   *enum           The target values representing the constants used to
377  *                   represent individual enumerations.
378  *   *enum_nameid    Idents containing the source program name of the enumeration
379  *                   constants
380  *
381 *****
382 */
383 /* create a new type enumeration -- set the enumerators independently */
384 type   *new_type_enumeration    (ident *name, int n_enums);
385
386 /* manipulate fields of enumeration type. */
387 int     get_enumeration_n_enums (type *enumeration);
388
389 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
390 tarval *get_enumeration_enum    (type *enumeration, int pos);
391
392 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
393 ident  *get_enumeration_nameid  (type *enumeration, int pos);
394 const char *get_enumeration_name(type *enumeration, int pos);
395
396 /* typecheck */
397 bool    is_enumeration_type     (type *enumeration);
398 /*****/
399
400 /****** type/pointer
401  * NAME
402  *   Representation of a pointer type.
403  * NOTE
404  *   Pointer types.
405  * ATTRIBUTES
406  *   points_to       The type of the entity this pointer points to.
407  * SOURCE
408  */
409 /* Create a new type pointer */
410 type *new_type_pointer           (ident *name, type *points_to);
411
412 /* manipulate fields of type_pointer */
413 void  set_pointer_points_to_type (type *pointer, type *type);
414 type *get_pointer_points_to_type (type *pointer);
415
416 /* typecheck */
417 bool  is_pointer_type            (type *pointer);
418 /*****/
419
420 /****** type/primitive
421  * NAME
422  *   Representation of a primitive type.
423  * NOTE
424  *   Primitive types are types that represent indivisible data values that
425  *   map directly to modes.  They don't have a private attribute.  The
426  *   important information they carry is held in the common mode field.
427  * SOURCE
428 */
429 /* create a new type primitive */
430 type *new_type_primitive (ident *name, ir_mode *mode);
431
432 /* typecheck */
433 bool  is_primitive_type  (type *primitive);
434 /*****/
435
436 # endif /* _TYPE_H_ */