7ff863a6707e896cc761c8911309fbba8ab64279
[libfirm] / ir / tr / type.h
1 /****h* libfirm/type6 2002/03/19 13:08:33
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
31 /* $Id$ */
32
33 # ifndef _TYPE_H_
34 # define _TYPE_H_
35
36 # include "tpop.h"
37 # include "common.h"
38 # include "ident.h"
39 # include "irmode.h"
40 # include "bool.h"
41
42
43 #ifndef _ENTITY_TYPEDEF_
44 #define _ENTITY_TYPEDEF_
45 /* to resolve recursion between entity.h and type.h */
46 typedef struct entity entity;
47 #endif
48
49 #ifndef _IR_NODE_TYPEDEF_
50 #define _IR_NODE_TYPEDEF_
51 typedef struct ir_node ir_node;
52 #endif
53
54 /****s* type/type
55  *
56  * NAME
57  *   type - An abstract data type to represent types.
58  * NOTE
59  *  This is the abstract data type with which any type known in the
60  *  compiled program can be represented.  This includes types specified
61  *  in the program as well as types defined by the language.  In the
62  *  view of the intermediate representation there is no difference
63  *  between these types.
64  *
65  *  There exist several kinds of types, arranged by the structure of
66  *  the type.  These are distinguished by a type opcode.
67  *  A type is described by a set of attributes.  Some of these attributes
68  *  are common to all types, others depend on the kind of the type.
69  *
70  *  The following describes the common attributes.  They can only be
71  *  accessed by the functions given below.
72  *
73  * ATTRIBUTES
74  *  The common fields are:
75  *
76  *  firm_kind    A firm_kind tag containing k_type.  This is useful
77  *               for dynamically checking whether a node is a type node.
78  *  type_op      A tp_op specifying the kind of the type.
79  *  mode         The mode to be used to represent the type on a machine.
80  *               @@@ maybe not global field??
81  *  name         An identifier specifying the name of the type.  To be
82  *               set by the frontend.
83  *  size         The size of the type, i.e. an entity of this type will
84  *               occupy size bytes in memory.  In several cases this is
85  *               determined when fixing the layout of this type (class,
86  *               struct, union, array, enumeration).
87  *  state        The state of the type.  The state represents whether the
88  *               layout of the type is undefined or fixed (values: layout_undefined
89  *               or layout_fixed).  Compound types can have an undefined
90  *               layout.  The layout of the basic types primitive and pointer
91  *               is always layout_fixed.  If the layout of
92  *               compound types is fixed all entities must have an offset
93  *               and the size of the type must be set.
94  *               A fixed layout for enumeration types means that each enumeration
95  *               is associated with an implementation value.
96  *  visit        A counter for walks of the type information.
97  *  link         A void* to associate some additional information with the type.
98  *
99  *  These fields can only be accessed via access functions.
100  *
101  *  Depending on the value of type_op, i.e., depending on the kind of the
102  *  type the adt contains further attributes.  These are documented below.
103  * SEE ALSO
104  *   class, struct, method, union, array, enumeration, pointer, primitive
105  * SOURCE
106  */
107 typedef struct type type;
108
109 # include "type_or_entity.h"
110
111 /* Frees the memory used by the type.   Does not free the entities
112    belonging to the type, except for the array element entity.  */
113 void        free_type(type *tp);
114
115 tp_op*      get_type_tpop(type *tp);
116 ident*      get_type_tpop_nameid(type *tp);
117 const char* get_type_tpop_name(type *tp);
118 tp_opcode   get_type_tpop_code(type *tp);
119
120 ident*      get_type_ident(type *tp);
121 void        set_type_ident(type *tp, ident* id);
122 const char* get_type_name(type *tp);
123
124 typedef enum {
125   layout_undefined,    /* The layout of this type is not defined.
126                           Address computation to access fields is not
127                           possible, fields must be accessed by Sel
128                           nodes.  This is the default value except for
129                           pointer, primitive and method types. */
130   layout_fixed         /* The layout is fixed, all component/member entities
131                           have an offset assigned.  Size of the type is known.
132                           Arrays can be accessed by explicit address
133                           computation. Default for pointer, primitive ane method
134                           types.  */
135 } type_state;
136 type_state  get_type_state(type *tp);
137 /* For primitives, pointer and method types the layout is always fixed.
138    This call is legal but has no effect. */
139 void        set_type_state(type *tp, type_state state);
140
141 /* Returns NULL for all non atomic types. */
142 ir_mode*    get_type_mode(type *tp);
143 /* Only has an effect on primitive and enumeration types */
144 void        set_type_mode(type *tp, ir_mode* m);
145
146 int         get_type_size(type *tp);
147 /* For primitive, enumeration, pointer and method types the size
148    is always fixed. This call is legal but has no effect. */
149 void        set_type_size(type *tp, int size);
150
151
152 unsigned long get_type_visited(type *tp);
153 void        set_type_visited(type *tp, unsigned long num);
154 /* Sets visited field in type to type_visited. */
155 void        mark_type_visited(type *tp);
156
157 void*       get_type_link(type *tp);
158 void        set_type_link(type *tp, void *l);
159 /*****/
160
161 /****v* type/visited
162  *
163  * NAME
164  *   type_visited -  visited flag to traverse the type information
165  * PURPOSE
166  *   Increase this flag by one before traversing the type information.
167  *   Mark type nodes as visited by set_type_visited(type, type_visited).
168  *   Check whether node was already visited by comparing get_type_visited(type)
169  *   and type_visited.
170  *   Or use the function to walk all types.
171  * SEE ALSO
172  *   typewalk
173  * SOURCE
174  */
175 extern unsigned long type_visited;
176 /*****/
177
178 /****f* type/is_type
179  *
180  * NAME
181  *   is_type - Checks whether a pointer points to a type.
182  * SYNOPSIS
183  *   bool is_type            (void *thing);
184  * INPUTS
185  *   thing - a pointer
186  * RESULT
187  *   true if the thing is a type, else false
188  ***
189  */
190 int is_type            (void *thing);
191
192 /****** type/class
193  * NAME
194  *  Representation of a class type.
195  * NOTE
196  *  If the type opcode is set to type_class the type represents class
197  *  types.  A list of fields and methods is associated with a class.
198  *  Further a class can inherit from and bequest to other classes.
199  *  @@@ value class???
200  * ATTRIBUTES
201  *  The following attributes are private to this type kind.
202  *  member     All entities belonging to this class.  This are methode entities
203  *             which have type_method or fields that can have any of the
204  *             following type kinds: type_class, type_struct, type_union,
205  *             type_array, type_enumeration, type_pointer, type_primitive.
206  *
207  *  subtypes   A list of direct subclasses.
208  *
209  *  supertypes A list of direct superclasses.
210  *
211  *  These are dynamic lists that can be grown with an "add_" function,
212  *  but not shrinked.
213  *
214  *  peculiarity The peculiarity of this class.  If the class is of peculiarity
215  *             "description" it only is a description of requirememts to a class,
216  *             as, e.g., a Java interface.  The class will never be allocated.
217  *             Peculiatity inherited is only possible for entities.  An entity
218  *             is of peculiarity inherited if the compiler generated the entity
219  *             to explicitly resolve inheritance.  An inherited method entity has
220  *             no value for irg.
221  *             Values: description, existent, inherited.  Default: existent.
222  *
223  * SOURCE
224  */
225 /* create a new class type */
226 type   *new_type_class (ident *name);
227
228 /** manipulate private fields of class type  **/
229 /* Adds the entity as member of the class.  */
230 void    add_class_member   (type *clss, entity *member);
231 /* Returns the number of members of this class. */
232 int     get_class_n_members (type *clss);
233 /* Returns the member at position pos, 0 <= pos < n_member */
234 entity *get_class_member   (type *clss, int pos);
235 /* Overwrites the member at position pos, 0 <= pos < n_member with
236    the passed entity. */
237 void    set_class_member   (type *clss, entity *member, int pos);
238 /* Replaces complete member list in class type by the list passed.  Copies the
239    list passed. This function is necessary to reduce the number of members.
240    members is an array of entities, num the size of this array.  Sets all
241    owners of the members passed to clss. */
242 void    set_class_members  (type *clss, entity **members, int arity);
243 /* Finds member in the list of members and overwrites it with NULL
244  @@@ Doesn't work properly. */
245 void    remove_class_member(type *clss, entity *member);
246
247
248 /* Adds subtype as subtype to clss.
249    Checks whether clss is a supertype of subtype.  If not
250    adds also clss as supertype to subtype.  */
251 void    add_class_subtype   (type *clss, type *subtype);
252 /* Returns the number of subtypes */
253 int     get_class_n_subtypes (type *clss);
254 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
255 type   *get_class_subtype   (type *clss, int pos);
256 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype.  Does not
257    set the corresponding supertype relation for subtype: this might
258    be a different position! */
259 void    set_class_subtype   (type *clss, type *subtype, int pos);
260 /* Finds subtype in the list of subtypes and overwrites it with NULL
261  @@@ Doesn't work properly. */
262 void    remove_class_subtype(type *clss, type *subtype);
263
264
265 /* Adds supertype as supertype to class.
266    Checks whether clss is a subtype of supertype.  If not
267    adds also clss as subtype to supertype.  */
268 void    add_class_supertype   (type *clss, type *supertype);
269 /* Returns the number of supertypes */
270 int     get_class_n_supertypes (type *clss);
271 /* Gets the supertype at position pos,  0 <= pos < n_supertype. */
272 type   *get_class_supertype   (type *clss, int pos);
273 /* Sets the supertype at postition pos, 0 <= pos < n_subtype.  Does not
274    set the corresponding subtype relation for supertype: this might
275    be a different position! */
276 void    set_class_supertype   (type *clss, type *supertype, int pos);
277 /* Finds supertype in the list of supertypes and overwrites it with NULL
278  @@@ Doesn't work properly. */
279 void    remove_class_supertype(type *clss, type *supertype);
280
281 /* This enumeration flags the peculiarity of entities and types. */
282 typedef enum peculiarity {
283   description,     /* Represents only a description.  The entity/type is never
284                       allocated, no code/data exists for this entity/type. */
285   inherited,       /* Describes explicitly that other entities are inherited
286                           to the owner of this entity.  Overwrites must refer to
287                           at least one other entity.  If this is a method entity
288                           there exists no irg for this entity, only for one of
289                           the overwritten ones. */
290   existent         /* The entity/type (can) exist. */
291 } peculiarity;
292
293 /* The peculiarity of the class.  The enumeration peculiarity is defined
294    in entity.h */
295 INLINE peculiarity get_class_peculiarity (type *clss);
296 INLINE void        set_class_peculiarity (type *clss, peculiarity pec);
297
298 /* Set and get a class' dfn --
299    @@@ This is an undocumented field, subject to change! */
300 void set_class_dfn (type*, int);
301 int  get_class_dfn (type*);
302
303 /* typecheck */
304 bool    is_class_type(type *clss);
305 /*****/
306
307 /****** type/struct
308  * NAME
309  *  Representation of a struct type.
310  * NOTE
311  *  Type_strct represents aggregate types that consist of a list
312  *  of fields.
313  * ATTRIBUTES
314  *  member   All entities belonging to this class.  This are the fields
315  *           that can have any of the following types:  type_class,
316  *           type_struct, type_union, type_array, type_enumeration,
317  *           type_pointer, type_primitive.
318  *           This is a dynamic list that can be grown with an "add_" function,
319  *           but not shrinked.
320  *           This is a dynamic list that can be grown with an "add_" function,
321  *           but not shrinked.
322  * SOURCE
323  */
324 /* create a new type struct */
325 type   *new_type_struct (ident *name);
326
327 /* manipulate private fields of struct */
328 void    add_struct_member   (type *strct, entity *member);
329 int     get_struct_n_members (type *strct);
330 entity *get_struct_member   (type *strct, int pos);
331 void    set_struct_member   (type *strct, int pos, entity *member);
332 /* Finds member in the list of memberss and overwrites it with NULL
333  @@@ Doesn't work properly. */
334 void    remove_struct_member (type *strct, entity *member);
335
336 /* typecheck */
337 bool    is_struct_type(type *strct);
338 /*****/
339
340 /****** type/method
341  * NAME
342  *  Representation of a method type.
343  * NOTE
344  *  A method type represents a method, function or procedure type.
345  *  It contains a list of the parameter and result types, as these
346  *  are part of the type description.  These lists should not
347  *  be changed by a optimization, as a change creates a new method
348  *  type.  Therefore optimizations should allocated new method types.
349  *  The set_ routines are only for construction by a frontend.
350  * ATTRIBUTES
351  *  n_params    Number of parameters to the procedure.
352  *              A procedure in FIRM has only call by value parameters.
353  *
354  *  param_type  A list with the types of parameters.  This list is ordered.
355  *              The nth type in this list corresponds to the nth element
356  *              in the parameter tuple that is a result of the start node.
357  *              (See ircons.h for more information.)
358  *
359  *  n_res       The number of results of the method.  In general, procedures
360  *              have zero results, functions one.
361  *
362  *  res_type    A list with the types of parameters.  This list is ordered.
363  *              The nth type in this list corresponds to the nth input to
364  *              Return nodes.  (See ircons.h for more information.)
365  * SOURCE
366  */
367
368 /* Create a new method type.
369    N_param is the number of parameters, n_res the number of results.
370    The arrays for the parameter and result types are not initialized by
371    the constructor. */
372 type *new_type_method (ident *name, int n_param, int n_res);
373
374 /* manipulate private fields of method. */
375 int   get_method_n_params  (type *method);
376 type *get_method_param_type(type *method, int pos);
377 void  set_method_param_type(type *method, int pos, type* type);
378
379 int   get_method_n_ress   (type *method);
380 type *get_method_res_type(type *method, int pos);
381 void  set_method_res_type(type *method, int pos, type* type);
382
383 /* typecheck */
384 bool  is_method_type     (type *method);
385 /*****/
386
387 /****** type/union
388  * NAME
389  *   Representation of a union type.
390  * NOTE
391  *   The union type represents union types.
392  * ATTRIBUTES
393  *   n_types        Number of unioned types.
394  *   members        Entities for unioned types.  Fixed length array.
395  *                  This is a dynamic list that can be grown with an "add_" function,
396  *                  but not shrinked.
397  * SOURCE
398  */
399 /* create a new type union  */
400 type   *new_type_union (ident *name);
401
402 /* manipulate private fields of struct */
403 int     get_union_n_members      (type *uni);
404 void    add_union_member (type *uni, entity *member);
405 entity *get_union_member (type *uni, int pos);
406 void    set_union_member (type *uni, int pos, entity *member);
407 /* Finds member in the list of members and overwrites it with NULL
408    @@@ Doesn't work properly. */
409 void    remove_union_member (type *uni, entity *member);
410
411 /* typecheck */
412 bool    is_union_type          (type *uni);
413 /*****/
414
415 #if 0
416 /* We don't need these if the union has entities, which it now
417    does. The entities are necessary for the analysis algorithms. */
418 type  *get_union_unioned_type (type *uni, int pos);
419 void   set_union_unioned_type (type *uni, int pos, type *type);
420
421 ident *get_union_delim_nameid (type *uni, int pos);
422 const char *get_union_delim_name (type *uni, int pos);
423 void   set_union_delim_nameid (type *uni, int pos, ident *id);
424 #endif
425
426 /****** type/array
427  * NAME
428  *   Representation of an array type.
429  * NOTE
430  *   The array type represents rectangular multi dimensional arrays.
431  *   The constants representing the bounds must be allocated to
432  *   get_const_code_irg() by setting current_ir_graph accordingly.
433  * ATTRIBUTES
434  *   n_dimensions     Number of array dimensions.
435  *   *lower_bound     Lower bounds of dimensions.  Usually all 0.
436  *   *upper_bound     Upper bounds or dimensions.
437  *   *element_type    The type of the array elements.
438  *   *element_ent     An entity for the array elements to be used for
439  *                    element selection with Sel.
440  *                    @@@ Do we need several entities?  One might want
441  *                    to select a dimension and not a single element in
442  *                    case of multidim arrays.
443  * SOURCE
444  */
445 /* create a new type array --
446    Sets n_dimension to dimension and all dimension entries to NULL.
447    Initializes order to the order of the dimensions.
448    The entity for array elements is built automatically.
449    Set dimension sizes after call to constructor with set_* routines. */
450 type *new_type_array         (ident *name, int n_dimensions,
451                               type *element_type);
452
453 /* manipulate private fields of array type */
454 int   get_array_n_dimensions (type *array);
455 /* Allocates Const nodes of mode_I for the array dimensions */
456 void  set_array_bounds_int   (type *array, int dimension, int lower_bound,
457                                                           int upper_bound);
458 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
459                                                           ir_node *upper_bound);
460 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
461 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound);
462 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
463 void  set_array_upper_bound_int (type *array, int dimension, int lower_bound);
464 ir_node * get_array_lower_bound  (type *array, int dimension);
465 ir_node * get_array_upper_bound  (type *array, int dimension);
466
467 void set_array_order (type *array, int dimension, int order);
468 int  get_array_order (type *array, int dimension);
469
470 void  set_array_element_type (type *array, type *type);
471 type *get_array_element_type (type *array);
472
473 void  set_array_element_entity (type *array, entity *ent);
474 entity *get_array_element_entity (type *array);
475
476 /* typecheck */
477 bool   is_array_type         (type *array);
478 /*****/
479
480 /****** type/enumeration
481  * NAME
482  *  Representation of an enumeration type.
483  * NOTE
484  *  Enumeration types need not necessarily be represented explicitly
485  *  by Firm types, as the frontend can lower them to integer constants as
486  *  well.  For debugging purposes or similar tasks this information is useful.
487  * ATTRIBUTES
488  *   *enum           The target values representing the constants used to
489  *                   represent individual enumerations.
490  *   *enum_nameid    Idents containing the source program name of the enumeration
491  *                   constants
492  *
493 *****
494 */
495 /* create a new type enumeration -- set the enumerators independently */
496 type   *new_type_enumeration    (ident *name, int n_enums);
497
498 /* manipulate fields of enumeration type. */
499 int     get_enumeration_n_enums (type *enumeration);
500
501 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
502 tarval *get_enumeration_enum    (type *enumeration, int pos);
503
504 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
505 ident  *get_enumeration_nameid  (type *enumeration, int pos);
506 const char *get_enumeration_name(type *enumeration, int pos);
507
508 /* typecheck */
509 bool    is_enumeration_type     (type *enumeration);
510 /*****/
511
512 /****** type/pointer
513  * NAME
514  *   Representation of a pointer type.
515  * NOTE
516  *   Pointer types.
517  * ATTRIBUTES
518  *   points_to       The type of the entity this pointer points to.
519  * SOURCE
520  */
521 /* Create a new type pointer */
522 type *new_type_pointer           (ident *name, type *points_to);
523
524 /* manipulate fields of type_pointer */
525 void  set_pointer_points_to_type (type *pointer, type *type);
526 type *get_pointer_points_to_type (type *pointer);
527
528 /* typecheck */
529 bool  is_pointer_type            (type *pointer);
530 /*****/
531
532 /****** type/primitive
533  * NAME
534  *   Representation of a primitive type.
535  * NOTE
536  *   Primitive types are types that represent indivisible data values that
537  *   map directly to modes.  They don't have a private attribute.  The
538  *   important information they carry is held in the common mode field.
539  * SOURCE
540 */
541 /* create a new type primitive */
542 type *new_type_primitive (ident *name, ir_mode *mode);
543
544 /* typecheck */
545 bool  is_primitive_type  (type *primitive);
546 /*****/
547
548
549
550 /****f* type/is_atomic_type
551  *
552  * NAME
553  *   is_atomic_type - Checks whether a type is atomic.
554  * SYNOPSIS
555  *   int is_atomic_type(type *tp);
556  * INPUTS
557  *   tp - any type
558  * RESULT
559  *   true if type is primitive, pointer or enumeration
560  ***
561  */
562 int is_atomic_type(type *tp);
563
564 /****f* type/is_compound_type
565  *
566  * NAME
567  *   is_compound_type - Checks whether a type is compound.
568  * SYNOPSIS
569  *   int is_compound_type(type *tp)
570  * INPUTS
571  *   tp - any type
572  * RESULT
573  *   true if the type is class, structure, union or array type.
574  ***
575  */
576 int is_compound_type(type *tp);
577
578 # endif /* _TYPE_H_ */