added functions set_type_link and get_type_link
[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 void*       get_type_link(type *tp);
96 void        set_type_link(type *tp, void *l);
97 tp_op*      get_type_tpop(type *tp);
98 ident*      get_type_tpop_nameid(type *tp);
99 const char* get_type_tpop_name(type *tp);
100 tp_opcode   get_type_tpop_code(type *tp);
101
102 ir_mode*    get_type_mode(type *tp);
103 void        set_type_mode(type *tp, ir_mode* m);
104
105 ident*      get_type_nameid(type *tp);
106 void        set_type_nameid(type *tp, ident* id);
107 const char* get_type_name(type *tp);
108
109 int         get_type_size(type *tp);
110 /* For primitives and pointer types the size is always fixed.
111    This call is legal but has no effect. */
112 void        set_type_size(type *tp, int size);
113
114 typedef enum {
115   layout_undefined,    /* The layout of this type is not defined.
116                           Address computation to access fields is not
117                           possible, fields must be accessed by Sel nodes.
118                           This is the default value except for pointer and
119                           primitive types. */
120   layout_fixed         /* The layout is fixed, all component/member entities
121                           have an offset assigned.  Size of the type is known.
122                           Arrays can be accessed by explicit address
123                           computation. Default for pointer and primitive types.
124                        */
125 } type_state;
126
127 type_state  get_type_state(type *tp);
128 /* For primitives and pointer types the layout is always fixed.
129    This call is legal but has no effect. */
130 void        set_type_state(type *tp, type_state state);
131
132 unsigned long get_type_visited(type *tp);
133 void        set_type_visited(type *tp, unsigned long num);
134 /* Sets visited field in type to type_visited. */
135 void        mark_type_visited(type *tp);
136 /*****/
137
138 /****v* type/visited
139  *
140  * NAME
141  *   type_visited -  visited flag to traverse the type information
142  * PURPOSE
143  *   Increase this flag by one before traversing the type information.
144  *   Mark type nodes as visited by set_type_visited(type, type_visited).
145  *   Check whether node was already visited by comparing get_type_visited(type)
146  *   and type_visited.
147  *   Or use the function to walk all types.
148  * SEE ALSO
149  *   typewalk
150  * SOURCE
151  */
152 extern unsigned long type_visited;
153 /*****/
154
155 /****f* type/is_type
156  *
157  * NAME
158  *   is_type - Checks whether a pointer points to a type.
159  * SYNOPSIS
160  *   bool is_type            (void *thing);
161  * INPUTS
162  *   thing - a pointer
163  * RESULT
164  *   true if the thing is a type, else false
165  ***
166  */
167 int is_type            (void *thing);
168
169 /****** type/class
170  * NAME
171  *  Representation of a class type.
172  * NOTE
173  *  If the type opcode is set to type_class the type represents class
174  *  types.  A list of fields and methods is associated with a class.
175  *  Further a class can inherit from and bequest to other classes.
176  *  @@@ value class???
177  * ATTRIBUTES
178  *  The following attributes are private to this type kind.
179  *  member     All entities belonging to this class.  This are methode entities
180  *             which have type_method or fields that can have any of the
181  *             following type kinds: type_class, type_struct, type_union,
182  *             type_array, type_enumeration, type_pointer, type_primitive.
183  *
184  *  subtypes   A list of direct subclasses.
185  *
186  *  supertypes A list of direct superclasses.
187  *
188  *  These are dynamic lists that can be grown with an "add_" function,
189  *  but not shrinked.
190  * SOURCE
191  */
192 /* create a new class type */
193 type   *new_type_class (ident *name);
194
195 /** manipulate private fields of class type  **/
196 /* Adds the entity as member of the class.  */
197 void    add_class_member   (type *clss, entity *member);
198 /* Returns the number of members of this class. */
199 int     get_class_n_member (type *clss);
200 /* Returns the member at position pos, 0 <= pos < n_member */
201 entity *get_class_member   (type *clss, int pos);
202 /* Overwrites the member at position pos, 0 <= pos < n_member with
203    the passed entity. */
204 void    set_class_member   (type *clss, entity *member, int pos);
205 /* Finds member in the list of members and overwrites it with NULL */
206 void    remove_class_member(type *clss, entity *member);
207
208
209 /* Adds subtype as subtype to clss and also
210    clss as supertype to subtype */
211 void    add_class_subtype   (type *clss, type *subtype);
212 /* Returns the number of subtypes */
213 int     get_class_n_subtype (type *clss);
214 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
215 type   *get_class_subtype   (type *clss, int pos);
216 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype.  Does not
217    set the corresponding supertype relation for subtype: this might
218    be a different position! */
219 void    set_class_subtype   (type *clss, type *subtype, int pos);
220 /* Finds subtype in the list of subtypes and overwrites it with NULL */
221 void    remove_class_subtype(type *clss, type *subtype);
222
223
224 /* Adds supertype as supertype to class and also
225    class as subtype to supertype. */
226 void    add_class_supertype   (type *clss, type *supertype);
227 /* Returns the number of supertypes */
228 int     get_class_n_supertype (type *clss);
229 /* Gets the supertype at position pos,  0 <= pos < n_supertype. */
230 type   *get_class_supertype   (type *clss, int pos);
231 /* Sets the supertype at postition pos, 0 <= pos < n_subtype.  Does not
232    set the corresponding subtype relation for supertype: this might
233    be a different position! */
234 void    set_class_supertype   (type *clss, type *supertype, int pos);
235 /* Finds supertype in the list of supertypes and overwrites it with NULL */
236 void    remove_class_supertype(type *clss, type *supertype);
237
238 /* typecheck */
239 bool    is_class_type(type *clss);
240 /*****/
241
242 /****** type/struct
243  * NAME
244  *  Representation of a struct type.
245  * NOTE
246  *  Type_strct represents aggregate types that consist of a list
247  *  of fields.
248  * ATTRIBUTES
249  *  member   All entities belonging to this class.  This are the fields
250  *           that can have any of the following types:  type_class,
251  *           type_struct, type_union, type_array, type_enumeration,
252  *           type_pointer, type_primitive.
253  *           This is a dynamic list that can be grown with an "add_" function,
254  *           but not shrinked.
255  *           This is a dynamic list that can be grown with an "add_" function,
256  *           but not shrinked.
257  * SOURCE
258  */
259 /* create a new type struct */
260 type   *new_type_struct (ident *name);
261
262 /* manipulate private fields of struct */
263 void    add_struct_member   (type *strct, entity *member);
264 int     get_struct_n_member (type *strct);
265 entity *get_struct_member   (type *strct, int pos);
266 void    set_struct_member   (type *strct, int pos, entity *member);
267 /* Finds member in the list of memberss and overwrites it with NULL */
268 void    remove_struct_member (type *strct, entity *member);
269
270 /* typecheck */
271 bool    is_struct_type(type *strct);
272 /*****/
273
274 /****** type/method
275  * NAME
276  *  Representation of a method type.
277  * NOTE
278  *  A method type represents a method, function or procedure type.
279  *  It contains a list of the parameter and result types, as these
280  *  are part of the type description.  These lists should not
281  *  be changed by a optimization, as a change creates a new method
282  *  type.  Therefore optimizations should allocated new method types.
283  *  The set_ routines are only for construction by a frontend.
284  * ATTRIBUTES
285  *  n_params    Number of parameters to the procedure.
286  *              A procedure in FIRM has only call by value parameters.
287  *
288  *  param_type  A list with the types of parameters.  This list is ordered.
289  *              The nth type in this list corresponds to the nth element
290  *              in the parameter tuple that is a result of the start node.
291  *              (See ircons.h for more information.)
292  *
293  *  n_res       The number of results of the method.  In general, procedures
294  *              have zero results, functions one.
295  *
296  *  res_type    A list with the types of parameters.  This list is ordered.
297  *              The nth type in this list corresponds to the nth input to
298  *              Return nodes.  (See ircons.h for more information.)
299  * SOURCE
300  */
301
302 /* Create a new method type.
303    N_param is the number of parameters, n_res the number of results.
304    The arrays for the parameter and result types are not initialized by
305    the constructor. */
306 type *new_type_method (ident *name, int n_param, int n_res);
307
308 /* manipulate private fields of method. */
309 int   get_method_n_params  (type *method);
310 type *get_method_param_type(type *method, int pos);
311 void  set_method_param_type(type *method, int pos, type* type);
312
313 int   get_method_n_res   (type *method);
314 type *get_method_res_type(type *method, int pos);
315 void  set_method_res_type(type *method, int pos, type* type);
316
317 /* typecheck */
318 bool  is_method_type     (type *method);
319 /*****/
320
321 /****** type/union
322  * NAME
323  *   Representation of a union type.
324  * NOTE
325  *   The union type represents union types.
326  * ATTRIBUTES
327  *   n_types        Number of unioned types.
328  *   members        Entities for unioned types.  Fixed length array.
329  *                  This is a dynamic list that can be grown with an "add_" function,
330  *                  but not shrinked.
331  * SOURCE
332  */
333 /* create a new type union  */
334 type   *new_type_union (ident *name);
335
336 /* manipulate private fields of struct */
337 int     get_union_n_members      (type *uni);
338 void    add_union_member (type *uni, entity *member);
339 entity *get_union_member (type *uni, int pos);
340 void    set_union_member (type *uni, int pos, entity *member);
341 void    remove_union_member (type *uni, entity *member);
342
343 /* typecheck */
344 bool    is_union_type          (type *uni);
345 /*****/
346
347 #if 0
348 /* We don't need these if the union has entities, which it now
349    does. The entities are necessary for the analysis algorithms. */
350 type  *get_union_unioned_type (type *uni, int pos);
351 void   set_union_unioned_type (type *uni, int pos, type *type);
352
353 ident *get_union_delim_nameid (type *uni, int pos);
354 const char *get_union_delim_name (type *uni, int pos);
355 void   set_union_delim_nameid (type *uni, int pos, ident *id);
356 #endif
357
358 /****** type/array
359  * NAME
360  *   Representation of an array type.
361  * NOTE
362  *   The array type represents rectangular multi dimensional arrays.
363  * ATTRIBUTES
364  *   n_dimensions     Number of array dimensions.
365  *   *lower_bound     Lower bounds of dimensions.  Usually all 0.
366  *   *upper_bound     Upper bounds or dimensions.
367  *   *element_type    The type of the array elements.
368  *   *element_ent     An entity for the array elements to be used for
369  *                    element selection with Sel.
370  *                    @@@ Do we need several entities?  One might want
371  *                    to select a dimension and not a single element in
372  *                    case of multidim arrays.
373  * SOURCE
374  */
375 /* create a new type array --
376    Set dimension sizes after call to constructor with set_* routines.
377    Entity for array elements is built automatically. */
378 type *new_type_array         (ident *name, int n_dimensions,
379                               type *element_type);
380
381 /* manipulate private fields of array type */
382 int   get_array_n_dimensions (type *array);
383 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
384                                                           ir_node *upper_bound);
385 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
386 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
387 ir_node * get_array_lower_bound  (type *array, int dimension);
388 ir_node * get_array_upper_bound  (type *array, int dimension);
389
390 void  set_array_element_type (type *array, type *type);
391 type *get_array_element_type (type *array);
392
393 void  set_array_element_entity (type *array, entity *ent);
394 entity *get_array_element_entity (type *array);
395
396 /* typecheck */
397 bool   is_array_type         (type *array);
398 /*****/
399
400 /****** type/enumeration
401  * NAME
402  *  Representation of an enumeration type.
403  * NOTE
404  *  Enumeration types need not necessarily be represented explicitly
405  *  by Firm types, as the frontend can lower them to integer constants as
406  *  well.  For debugging purposes or similar tasks this information is useful.
407  * ATTRIBUTES
408  *   *enum           The target values representing the constants used to
409  *                   represent individual enumerations.
410  *   *enum_nameid    Idents containing the source program name of the enumeration
411  *                   constants
412  *
413 *****
414 */
415 /* create a new type enumeration -- set the enumerators independently */
416 type   *new_type_enumeration    (ident *name, int n_enums);
417
418 /* manipulate fields of enumeration type. */
419 int     get_enumeration_n_enums (type *enumeration);
420
421 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
422 tarval *get_enumeration_enum    (type *enumeration, int pos);
423
424 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
425 ident  *get_enumeration_nameid  (type *enumeration, int pos);
426 const char *get_enumeration_name(type *enumeration, int pos);
427
428 /* typecheck */
429 bool    is_enumeration_type     (type *enumeration);
430 /*****/
431
432 /****** type/pointer
433  * NAME
434  *   Representation of a pointer type.
435  * NOTE
436  *   Pointer types.
437  * ATTRIBUTES
438  *   points_to       The type of the entity this pointer points to.
439  * SOURCE
440  */
441 /* Create a new type pointer */
442 type *new_type_pointer           (ident *name, type *points_to);
443
444 /* manipulate fields of type_pointer */
445 void  set_pointer_points_to_type (type *pointer, type *type);
446 type *get_pointer_points_to_type (type *pointer);
447
448 /* typecheck */
449 bool  is_pointer_type            (type *pointer);
450 /*****/
451
452 /****** type/primitive
453  * NAME
454  *   Representation of a primitive type.
455  * NOTE
456  *   Primitive types are types that represent indivisible data values that
457  *   map directly to modes.  They don't have a private attribute.  The
458  *   important information they carry is held in the common mode field.
459  * SOURCE
460 */
461 /* create a new type primitive */
462 type *new_type_primitive (ident *name, ir_mode *mode);
463
464 /* typecheck */
465 bool  is_primitive_type  (type *primitive);
466 /*****/
467
468 # endif /* _TYPE_H_ */