1 /****h* libfirm/type6 2002/03/19 13:08:33
4 * file type.h - datastructure to hold type information.
6 * (C) 2001 by Universitaet Karlsruhe
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.
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
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.
43 #ifndef _ENTITY_TYPEDEF_
44 #define _ENTITY_TYPEDEF_
45 /* to resolve recursion between entity.h and type.h */
46 typedef struct entity entity;
49 #ifndef _IR_NODE_TYPEDEF_
50 #define _IR_NODE_TYPEDEF_
51 typedef struct ir_node ir_node;
57 * type - An abstract data type to represent types.
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.
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.
70 * The following describes the common attributes. They can only be
71 * accessed by the functions given below.
74 * The common fields are:
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.
99 * These fields can only be accessed via access functions.
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.
104 * class, struct, method, union, array, enumeration, pointer, primitive
107 typedef struct type type;
109 # include "type_or_entity.h"
111 void* get_type_link(type *tp);
112 void set_type_link(type *tp, void *l);
113 tp_op* get_type_tpop(type *tp);
114 ident* get_type_tpop_nameid(type *tp);
115 const char* get_type_tpop_name(type *tp);
116 tp_opcode get_type_tpop_code(type *tp);
118 /* Returns NULL for all non atomic types. */
119 ir_mode* get_type_mode(type *tp);
120 /* Only has an effect on primitive and enumeration types */
121 void set_type_mode(type *tp, ir_mode* m);
123 ident* get_type_ident(type *tp);
124 void set_type_ident(type *tp, ident* id);
125 const char* get_type_name(type *tp);
127 int get_type_size(type *tp);
128 /* For primitives, enumerations, pointer and method types the size
129 is always fixed. This call is legal but has no effect. */
130 void set_type_size(type *tp, int size);
133 layout_undefined, /* The layout of this type is not defined.
134 Address computation to access fields is not
135 possible, fields must be accessed by Sel nodes.
136 This is the default value except for pointer and
138 layout_fixed /* The layout is fixed, all component/member entities
139 have an offset assigned. Size of the type is known.
140 Arrays can be accessed by explicit address
141 computation. Default for pointer and primitive types.
145 type_state get_type_state(type *tp);
146 /* For primitives, pointer and method types the layout is always fixed.
147 This call is legal but has no effect. */
148 void set_type_state(type *tp, type_state state);
150 unsigned long get_type_visited(type *tp);
151 void set_type_visited(type *tp, unsigned long num);
152 /* Sets visited field in type to type_visited. */
153 void mark_type_visited(type *tp);
159 * type_visited - visited flag to traverse the type information
161 * Increase this flag by one before traversing the type information.
162 * Mark type nodes as visited by set_type_visited(type, type_visited).
163 * Check whether node was already visited by comparing get_type_visited(type)
165 * Or use the function to walk all types.
170 extern unsigned long type_visited;
176 * is_type - Checks whether a pointer points to a type.
178 * bool is_type (void *thing);
182 * true if the thing is a type, else false
185 int is_type (void *thing);
189 * Representation of a class type.
191 * If the type opcode is set to type_class the type represents class
192 * types. A list of fields and methods is associated with a class.
193 * Further a class can inherit from and bequest to other classes.
196 * The following attributes are private to this type kind.
197 * member All entities belonging to this class. This are methode entities
198 * which have type_method or fields that can have any of the
199 * following type kinds: type_class, type_struct, type_union,
200 * type_array, type_enumeration, type_pointer, type_primitive.
202 * subtypes A list of direct subclasses.
204 * supertypes A list of direct superclasses.
206 * These are dynamic lists that can be grown with an "add_" function,
209 * peculiarity The peculiarity of this class. If the class is of peculiarity
210 * "description" it only is a description of requirememts to a class,
211 * as, e.g., a Java interface. The class will never be allocated.
212 * Peculiatity inherited is only possible for entities. An entity
213 * is of peculiarity inherited if the compiler generated the entity
214 * to explicitly resolve inheritance. An inherited method entity has
216 * Values: description, existent, inherited. Default: existent.
220 /* create a new class type */
221 type *new_type_class (ident *name);
223 /** manipulate private fields of class type **/
224 /* Adds the entity as member of the class. */
225 void add_class_member (type *clss, entity *member);
226 /* Returns the number of members of this class. */
227 int get_class_n_member (type *clss);
228 /* Returns the member at position pos, 0 <= pos < n_member */
229 entity *get_class_member (type *clss, int pos);
230 /* Overwrites the member at position pos, 0 <= pos < n_member with
231 the passed entity. */
232 void set_class_member (type *clss, entity *member, int pos);
233 /* Replaces complete member list in class type by the list passed. Copies the
234 list passed. This function is necessary to reduce the number of members.
235 members is an array of entities, num the size of this array. Sets all
236 owners of the members passed to clss. */
237 void set_class_members (type *clss, entity **members, int arity);
238 /* Finds member in the list of members and overwrites it with NULL
239 @@@ Doesn't work properly. */
240 void remove_class_member(type *clss, entity *member);
243 /* Adds subtype as subtype to clss.
244 Checks whether clss is a supertype of subtype. If not
245 adds also clss as supertype to subtype. */
246 void add_class_subtype (type *clss, type *subtype);
247 /* Returns the number of subtypes */
248 int get_class_n_subtype (type *clss);
249 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
250 type *get_class_subtype (type *clss, int pos);
251 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype. Does not
252 set the corresponding supertype relation for subtype: this might
253 be a different position! */
254 void set_class_subtype (type *clss, type *subtype, int pos);
255 /* Finds subtype in the list of subtypes and overwrites it with NULL
256 @@@ Doesn't work properly. */
257 void remove_class_subtype(type *clss, type *subtype);
260 /* Adds supertype as supertype to class.
261 Checks whether clss is a subtype of supertype. If not
262 adds also clss as subtype to supertype. */
263 void add_class_supertype (type *clss, type *supertype);
264 /* Returns the number of supertypes */
265 int get_class_n_supertype (type *clss);
266 /* Gets the supertype at position pos, 0 <= pos < n_supertype. */
267 type *get_class_supertype (type *clss, int pos);
268 /* Sets the supertype at postition pos, 0 <= pos < n_subtype. Does not
269 set the corresponding subtype relation for supertype: this might
270 be a different position! */
271 void set_class_supertype (type *clss, type *supertype, int pos);
272 /* Finds supertype in the list of supertypes and overwrites it with NULL
273 @@@ Doesn't work properly. */
274 void remove_class_supertype(type *clss, type *supertype);
276 /* This enumeration flags the peculiarity of entities and types. */
277 typedef enum peculiarity {
278 description, /* Represents only a description. The entity/type is never
279 allocated, no code/data exists for this entity/type. */
280 inherited, /* Describes explicitly that other entities are inherited
281 to the owner of this entity. Overwrites must refer to
282 at least one other entity. If this is a method entity
283 there exists no irg for this entity, only for one of
284 the overwritten ones. */
285 existent /* The entity/type (can) exist. */
288 /* The peculiarity of the class. The enumeration peculiarity is defined
290 inline peculiarity get_class_peculiarity (type *clss);
291 inline void set_class_peculiarity (type *clss, peculiarity pec);
293 /* Set and get a class' dfn */
294 void set_class_dfn (type*, int);
295 int get_class_dfn (type*);
298 bool is_class_type(type *clss);
303 * Representation of a struct type.
305 * Type_strct represents aggregate types that consist of a list
308 * member All entities belonging to this class. This are the fields
309 * that can have any of the following types: type_class,
310 * type_struct, type_union, type_array, type_enumeration,
311 * type_pointer, type_primitive.
312 * This is a dynamic list that can be grown with an "add_" function,
314 * This is a dynamic list that can be grown with an "add_" function,
318 /* create a new type struct */
319 type *new_type_struct (ident *name);
321 /* manipulate private fields of struct */
322 void add_struct_member (type *strct, entity *member);
323 int get_struct_n_member (type *strct);
324 entity *get_struct_member (type *strct, int pos);
325 void set_struct_member (type *strct, int pos, entity *member);
326 /* Finds member in the list of memberss and overwrites it with NULL
327 @@@ Doesn't work properly. */
328 void remove_struct_member (type *strct, entity *member);
331 bool is_struct_type(type *strct);
336 * Representation of a method type.
338 * A method type represents a method, function or procedure type.
339 * It contains a list of the parameter and result types, as these
340 * are part of the type description. These lists should not
341 * be changed by a optimization, as a change creates a new method
342 * type. Therefore optimizations should allocated new method types.
343 * The set_ routines are only for construction by a frontend.
345 * n_params Number of parameters to the procedure.
346 * A procedure in FIRM has only call by value parameters.
348 * param_type A list with the types of parameters. This list is ordered.
349 * The nth type in this list corresponds to the nth element
350 * in the parameter tuple that is a result of the start node.
351 * (See ircons.h for more information.)
353 * n_res The number of results of the method. In general, procedures
354 * have zero results, functions one.
356 * res_type A list with the types of parameters. This list is ordered.
357 * The nth type in this list corresponds to the nth input to
358 * Return nodes. (See ircons.h for more information.)
362 /* Create a new method type.
363 N_param is the number of parameters, n_res the number of results.
364 The arrays for the parameter and result types are not initialized by
366 type *new_type_method (ident *name, int n_param, int n_res);
368 /* manipulate private fields of method. */
369 int get_method_n_params (type *method);
370 type *get_method_param_type(type *method, int pos);
371 void set_method_param_type(type *method, int pos, type* type);
373 int get_method_n_res (type *method);
374 type *get_method_res_type(type *method, int pos);
375 void set_method_res_type(type *method, int pos, type* type);
378 bool is_method_type (type *method);
383 * Representation of a union type.
385 * The union type represents union types.
387 * n_types Number of unioned types.
388 * members Entities for unioned types. Fixed length array.
389 * This is a dynamic list that can be grown with an "add_" function,
393 /* create a new type union */
394 type *new_type_union (ident *name);
396 /* manipulate private fields of struct */
397 int get_union_n_members (type *uni);
398 void add_union_member (type *uni, entity *member);
399 entity *get_union_member (type *uni, int pos);
400 void set_union_member (type *uni, int pos, entity *member);
401 /* Finds member in the list of members and overwrites it with NULL
402 @@@ Doesn't work properly. */
403 void remove_union_member (type *uni, entity *member);
406 bool is_union_type (type *uni);
410 /* We don't need these if the union has entities, which it now
411 does. The entities are necessary for the analysis algorithms. */
412 type *get_union_unioned_type (type *uni, int pos);
413 void set_union_unioned_type (type *uni, int pos, type *type);
415 ident *get_union_delim_nameid (type *uni, int pos);
416 const char *get_union_delim_name (type *uni, int pos);
417 void set_union_delim_nameid (type *uni, int pos, ident *id);
422 * Representation of an array type.
424 * The array type represents rectangular multi dimensional arrays.
425 * The constants representing the bounds must be allocated to
426 * get_const_code_irg() by setting current_ir_graph accordingly.
428 * n_dimensions Number of array dimensions.
429 * *lower_bound Lower bounds of dimensions. Usually all 0.
430 * *upper_bound Upper bounds or dimensions.
431 * *element_type The type of the array elements.
432 * *element_ent An entity for the array elements to be used for
433 * element selection with Sel.
434 * @@@ Do we need several entities? One might want
435 * to select a dimension and not a single element in
436 * case of multidim arrays.
439 /* create a new type array --
440 Sets n_dimension to dimension and all dimension entries to NULL.
441 Initializes order to the order of the dimensions.
442 Entity for array elements is built automatically.
443 Set dimension sizes after call to constructor with set_* routines. */
444 type *new_type_array (ident *name, int n_dimensions,
447 /* manipulate private fields of array type */
448 int get_array_n_dimensions (type *array);
449 /* Allocates Const nodes of mode_I for the array dimensions */
450 void set_array_bounds_int (type *array, int dimension, int lower_bound,
452 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
453 ir_node *upper_bound);
454 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
455 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
456 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
457 ir_node * get_array_lower_bound (type *array, int dimension);
458 ir_node * get_array_upper_bound (type *array, int dimension);
460 void set_array_order (type *array, int dimension, int order);
461 int get_array_order (type *array, int dimension);
463 void set_array_element_type (type *array, type *type);
464 type *get_array_element_type (type *array);
466 void set_array_element_entity (type *array, entity *ent);
467 entity *get_array_element_entity (type *array);
470 bool is_array_type (type *array);
473 /****** type/enumeration
475 * Representation of an enumeration type.
477 * Enumeration types need not necessarily be represented explicitly
478 * by Firm types, as the frontend can lower them to integer constants as
479 * well. For debugging purposes or similar tasks this information is useful.
481 * *enum The target values representing the constants used to
482 * represent individual enumerations.
483 * *enum_nameid Idents containing the source program name of the enumeration
488 /* create a new type enumeration -- set the enumerators independently */
489 type *new_type_enumeration (ident *name, int n_enums);
491 /* manipulate fields of enumeration type. */
492 int get_enumeration_n_enums (type *enumeration);
494 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
495 tarval *get_enumeration_enum (type *enumeration, int pos);
497 void set_enumeration_nameid (type *enumeration, int pos, ident *id);
498 ident *get_enumeration_nameid (type *enumeration, int pos);
499 const char *get_enumeration_name(type *enumeration, int pos);
502 bool is_enumeration_type (type *enumeration);
507 * Representation of a pointer type.
511 * points_to The type of the entity this pointer points to.
514 /* Create a new type pointer */
515 type *new_type_pointer (ident *name, type *points_to);
517 /* manipulate fields of type_pointer */
518 void set_pointer_points_to_type (type *pointer, type *type);
519 type *get_pointer_points_to_type (type *pointer);
522 bool is_pointer_type (type *pointer);
525 /****** type/primitive
527 * Representation of a primitive type.
529 * Primitive types are types that represent indivisible data values that
530 * map directly to modes. They don't have a private attribute. The
531 * important information they carry is held in the common mode field.
534 /* create a new type primitive */
535 type *new_type_primitive (ident *name, ir_mode *mode);
538 bool is_primitive_type (type *primitive);
543 /****f* type/is_atomic_type
546 * is_atomic_type - Checks whether a type is atomic.
548 * int is_atomic_type(type *tp);
552 * true if type is primitive, pointer or enumeration
555 int is_atomic_type(type *tp);
557 /****f* type/is_compound_type
560 * is_compound_type - Checks whether a type is compound.
562 * int is_compound_type(type *tp)
566 * true if the type is class, structure, union or array type.
569 int is_compound_type(type *tp);
571 # endif /* _TYPE_H_ */