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.
37 # include "firm_common.h"
44 /* to resolve recursion between entity.h and type.h */
45 #ifndef _ENTITY_TYPEDEF_
46 #define _ENTITY_TYPEDEF_
47 typedef struct entity entity;
50 #ifndef _IR_NODE_TYPEDEF_
51 #define _IR_NODE_TYPEDEF_
52 typedef struct ir_node ir_node;
58 * type - An abstract data type to represent types.
60 * This is the abstract data type with which any type known in the
61 * compiled program can be represented. This includes types specified
62 * in the program as well as types defined by the language. In the
63 * view of the intermediate representation there is no difference
64 * between these types.
66 * There exist several kinds of types, arranged by the structure of
67 * the type. These are distinguished by a type opcode.
68 * A type is described by a set of attributes. Some of these attributes
69 * are common to all types, others depend on the kind of the type.
71 * The following describes the common attributes. They can only be
72 * accessed by the functions given below.
75 * The common fields are:
77 * firm_kind A firm_kind tag containing k_type. This is useful
78 * for dynamically checking whether a node is a type node.
79 * type_op A tp_op specifying the kind of the type.
80 * mode The mode to be used to represent the type on a machine.
81 * @@@ maybe not global field??
82 * name An identifier specifying the name of the type. To be
83 * set by the frontend.
84 * size The size of the type, i.e. an entity of this type will
85 * occupy size bytes in memory. In several cases this is
86 * determined when fixing the layout of this type (class,
87 * struct, union, array, enumeration).
88 * state The state of the type. The state represents whether the
89 * layout of the type is undefined or fixed (values: layout_undefined
90 * or layout_fixed). Compound types can have an undefined
91 * layout. The layout of the basic types primitive and pointer
92 * is always layout_fixed. If the layout of
93 * compound types is fixed all entities must have an offset
94 * and the size of the type must be set.
95 * A fixed layout for enumeration types means that each enumeration
96 * is associated with an implementation value.
97 * visit A counter for walks of the type information.
98 * link A void* to associate some additional information with the type.
100 * These fields can only be accessed via access functions.
102 * Depending on the value of type_op, i.e., depending on the kind of the
103 * type the adt contains further attributes. These are documented below.
105 * class, struct, method, union, array, enumeration, pointer, primitive
108 #ifndef _TYPE_TYPEDEF_
109 #define _TYPE_TYPEDEF_
110 typedef struct type type;
113 # include "type_or_entity.h"
115 /* Frees the memory used by the type. Does not free the entities
116 belonging to the type, except for the array element entity. */
117 void free_type(type *tp);
119 tp_op* get_type_tpop(type *tp);
120 ident* get_type_tpop_nameid(type *tp);
121 const char* get_type_tpop_name(type *tp);
122 tp_opcode get_type_tpop_code(type *tp);
124 ident* get_type_ident(type *tp);
125 void set_type_ident(type *tp, ident* id);
126 const char* get_type_name(type *tp);
129 layout_undefined, /* The layout of this type is not defined.
130 Address computation to access fields is not
131 possible, fields must be accessed by Sel
132 nodes. This is the default value except for
133 pointer, primitive and method types. */
134 layout_fixed /* The layout is fixed, all component/member entities
135 have an offset assigned. Size of the type is known.
136 Arrays can be accessed by explicit address
137 computation. Default for pointer, primitive ane method
140 type_state get_type_state(type *tp);
141 /* For primitives, pointer and method types the layout is always fixed.
142 This call is legal but has no effect. */
143 void set_type_state(type *tp, type_state state);
145 /* Returns NULL for all non atomic types. */
146 ir_mode* get_type_mode(type *tp);
147 /* Only has an effect on primitive and enumeration types */
148 void set_type_mode(type *tp, ir_mode* m);
150 int get_type_size(type *tp);
151 /* For primitive, enumeration, pointer and method types the size
152 is always fixed. This call is legal but has no effect. */
153 void set_type_size(type *tp, int size);
156 unsigned long get_type_visited(type *tp);
157 void set_type_visited(type *tp, unsigned long num);
158 /* Sets visited field in type to type_visited. */
159 void mark_type_visited(type *tp);
161 void* get_type_link(type *tp);
162 void set_type_link(type *tp, void *l);
168 * type_visited - visited flag to traverse the type information
170 * Increase this flag by one before traversing the type information.
171 * Mark type nodes as visited by set_type_visited(type, type_visited).
172 * Check whether node was already visited by comparing get_type_visited(type)
174 * Or use the function to walk all types.
179 extern unsigned long type_visited;
180 void set_master_type_visited(unsigned long val);
181 unsigned long get_master_type_visited();
182 void inc_master_type_visited();
188 * is_type - Checks whether a pointer points to a type.
190 * bool is_type (void *thing);
194 * true if the thing is a type, else false
197 int is_type (void *thing);
199 /****f* type/equal_types
202 * equal_type - Checks whether two types are structural equal.
204 * bool equal_types (type *typ1, type *typ2);
208 * true if the types are equal, else false.
210 * - they are the same type kind
211 * - they have the same name
212 * - they have the same mode (if applicable)
213 * - they have the same type_state and, ev., the same size
214 * - they are class types and have
215 * - the same members (see same_entity in entity.h)
216 * - the same supertypes -- the C-pointers are compared --> no recursive call.
217 * - the same number of subtypes. Subtypes are not compared,
218 * as this could cause a cyclic test.
219 * - the same peculiarity
220 * - they are structure types and have the same members
221 * - they are method types and have
222 * - the same parameter types
223 * - the same result types
224 * - they are union types and have the same members
225 * - they are array types and have
226 * - the same number of dimensions
227 * - the same dimension bounds
228 * - the same dimension order
229 * - the same element type
230 * - they are enumeration types and have the same enumerator names
231 * - they are pointer types and have the identical points_to type
232 * (i.e., the same C-struct to represent the type, type_id is skipped.
233 * This is to avoid endless recursions; with pointer types circlic
234 * type graphs are possible.)
238 bool equal_type(type *tpy1, type *typ2);
240 /****f* type/smaller_type
243 * smaller_type - Checks whether two types are structural comparable.
245 * bool smaller_type (type *st, type *lt);
249 * true if type st is smaller than type lt, i.e. whenever
250 * lt is expected a st can be used.
252 * - they are the same type kind
253 * - mode(st) < mode (lt) (if applicable)
254 * - they are class types and st is (transitive) subtype of lt,
255 * - they are structure types and
256 * - the members of st have exactly one counterpart in lt with the same name,
257 * - the counterpart has a bigger type.
258 * - they are method types and have
259 * - the same number of parameter and result types,
260 * - the parameter types of st are smaller than those of lt,
261 * - the result types of st are smaller than those of lt
262 * - they are union types and have the members of st have exactly one
263 * counterpart in lt and the type is smaller
264 * - they are array types and have
265 * - the same number of dimensions
266 * - all bounds of lt are bound of st
267 * - the same dimension order
268 * - the same element type
270 * - the element type of st is smaller than that of lt
271 * - the element types have the same size and fixed layout.
272 * - they are enumeration types and have the same enumerator names
273 * - they are pointer types and have the points_to type of st is
274 * smaller than the points_to type of lt.
277 bool smaller_type (type *st, type *lt);
281 * Representation of a class type.
283 * If the type opcode is set to type_class the type represents class
284 * types. A list of fields and methods is associated with a class.
285 * Further a class can inherit from and bequest to other classes.
288 * The following attributes are private to this type kind.
289 * member All entities belonging to this class. This are methode entities
290 * which have type_method or fields that can have any of the
291 * following type kinds: type_class, type_struct, type_union,
292 * type_array, type_enumeration, type_pointer, type_primitive.
294 * subtypes A list of direct subclasses.
296 * supertypes A list of direct superclasses.
298 * These are dynamic lists that can be grown with an "add_" function,
301 * peculiarity The peculiarity of this class. If the class is of peculiarity
302 * "description" it only is a description of requirememts to a class,
303 * as, e.g., a Java interface. The class will never be allocated.
304 * Peculiatity inherited is only possible for entities. An entity
305 * is of peculiarity inherited if the compiler generated the entity
306 * to explicitly resolve inheritance. An inherited method entity has
308 * Values: description, existent, inherited. Default: existent.
312 /* create a new class type */
313 type *new_type_class (ident *name);
314 type *new_d_type_class (ident *name, dbg_info *db);
316 /** manipulate private fields of class type **/
317 /* Adds the entity as member of the class. */
318 void add_class_member (type *clss, entity *member);
319 /* Returns the number of members of this class. */
320 int get_class_n_members (type *clss);
321 /* Returns the member at position pos, 0 <= pos < n_member */
322 entity *get_class_member (type *clss, int pos);
323 /* Overwrites the member at position pos, 0 <= pos < n_member with
324 the passed entity. */
325 void set_class_member (type *clss, entity *member, int pos);
326 /* Replaces complete member list in class type by the list passed. Copies the
327 list passed. This function is necessary to reduce the number of members.
328 members is an array of entities, num the size of this array. Sets all
329 owners of the members passed to clss. */
330 void set_class_members (type *clss, entity *members[], int arity);
331 /* Finds member in the list of members and removes it.
332 Shrinks the member list, so iterate from the end!!!
333 Does not deallocate the entity. */
334 void remove_class_member(type *clss, entity *member);
337 /* Adds subtype as subtype to clss.
338 Checks whether clss is a supertype of subtype. If not
339 adds also clss as supertype to subtype. */
340 void add_class_subtype (type *clss, type *subtype);
341 /* Returns the number of subtypes */
342 int get_class_n_subtypes (type *clss);
343 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
344 type *get_class_subtype (type *clss, int pos);
345 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype. Does not
346 set the corresponding supertype relation for subtype: this might
347 be a different position! */
348 void set_class_subtype (type *clss, type *subtype, int pos);
349 /* Finds subtype in the list of subtypes and removes it */
350 void remove_class_subtype(type *clss, type *subtype);
353 /* Adds supertype as supertype to class.
354 Checks whether clss is a subtype of supertype. If not
355 adds also clss as subtype to supertype. */
356 void add_class_supertype (type *clss, type *supertype);
357 /* Returns the number of supertypes */
358 int get_class_n_supertypes (type *clss);
359 /* Gets the supertype at position pos, 0 <= pos < n_supertype. */
360 type *get_class_supertype (type *clss, int pos);
361 /* Sets the supertype at postition pos, 0 <= pos < n_subtype. Does not
362 set the corresponding subtype relation for supertype: this might
363 be a different position! */
364 void set_class_supertype (type *clss, type *supertype, int pos);
365 /* Finds supertype in the list of supertypes and removes it */
366 void remove_class_supertype(type *clss, type *supertype);
368 /* This enumeration flags the peculiarity of entities and types. */
369 typedef enum peculiarity {
370 description, /* Represents only a description. The entity/type is never
371 allocated, no code/data exists for this entity/type. */
372 inherited, /* Describes explicitly that other entities are
373 inherited to the owner of this entity.
374 Overwrites must refer to at least one other
375 entity. If this is a method entity there exists
376 no irg for this entity, only for one of the
378 existent /* The entity/type (can) exist. */
381 /* The peculiarity of the class. The enumeration peculiarity is defined
383 INLINE peculiarity get_class_peculiarity (type *clss);
384 INLINE void set_class_peculiarity (type *clss, peculiarity pec);
386 /* Set and get a class' dfn --
387 @@@ This is an undocumented field, subject to change! */
388 void set_class_dfn (type *clss, int dfn);
389 int get_class_dfn (type *clss);
392 bool is_class_type(type *clss);
393 /* Returns true if low is subclass of high. */
394 bool is_subclass_of(type *low, type *high);
399 * Representation of a struct type.
401 * Type_strct represents aggregate types that consist of a list
404 * member All entities belonging to this class. This are the fields
405 * that can have any of the following types: type_class,
406 * type_struct, type_union, type_array, type_enumeration,
407 * type_pointer, type_primitive.
408 * This is a dynamic list that can be grown with an "add_" function,
410 * This is a dynamic list that can be grown with an "add_" function,
414 /* create a new type struct */
415 type *new_type_struct (ident *name);
416 type *new_d_type_struct (ident *name, dbg_info* db);
418 /* manipulate private fields of struct */
419 void add_struct_member (type *strct, entity *member);
420 int get_struct_n_members (type *strct);
421 entity *get_struct_member (type *strct, int pos);
422 void set_struct_member (type *strct, int pos, entity *member);
423 /* Finds member in the list of memberss and removees it */
424 void remove_struct_member (type *strct, entity *member);
427 bool is_struct_type(type *strct);
432 * Representation of a method type.
434 * A method type represents a method, function or procedure type.
435 * It contains a list of the parameter and result types, as these
436 * are part of the type description. These lists should not
437 * be changed by a optimization, as a change creates a new method
438 * type. Therefore optimizations should allocated new method types.
439 * The set_ routines are only for construction by a frontend.
441 * n_params Number of parameters to the procedure.
442 * A procedure in FIRM has only call by value parameters.
444 * param_type A list with the types of parameters. This list is ordered.
445 * The nth type in this list corresponds to the nth element
446 * in the parameter tuple that is a result of the start node.
447 * (See ircons.h for more information.)
449 * n_res The number of results of the method. In general, procedures
450 * have zero results, functions one.
452 * res_type A list with the types of parameters. This list is ordered.
453 * The nth type in this list corresponds to the nth input to
454 * Return nodes. (See ircons.h for more information.)
458 /* Create a new method type.
459 N_param is the number of parameters, n_res the number of results.
460 The arrays for the parameter and result types are not initialized by
462 type *new_type_method (ident *name, int n_param, int n_res);
463 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
465 /* manipulate private fields of method. */
466 int get_method_n_params (type *method);
467 type *get_method_param_type(type *method, int pos);
468 void set_method_param_type(type *method, int pos, type* tp);
470 int get_method_n_ress (type *method);
471 type *get_method_res_type(type *method, int pos);
472 void set_method_res_type(type *method, int pos, type* tp);
475 bool is_method_type (type *method);
480 * Representation of a union type.
482 * The union type represents union types.
484 * n_types Number of unioned types.
485 * members Entities for unioned types. Fixed length array.
486 * This is a dynamic list that can be grown with an "add_" function,
490 /* create a new type union */
491 type *new_type_union (ident *name);
492 type *new_d_type_union (ident *name, dbg_info* db);
494 /* manipulate private fields of struct */
495 int get_union_n_members (type *uni);
496 void add_union_member (type *uni, entity *member);
497 entity *get_union_member (type *uni, int pos);
498 void set_union_member (type *uni, int pos, entity *member);
499 /* Finds member in the list of members and removes it. */
500 void remove_union_member (type *uni, entity *member);
503 bool is_union_type (type *uni);
508 * Representation of an array type.
510 * The array type represents rectangular multi dimensional arrays.
511 * The constants representing the bounds must be allocated to
512 * get_const_code_irg() by setting current_ir_graph accordingly.
514 * n_dimensions Number of array dimensions.
515 * *lower_bound Lower bounds of dimensions. Usually all 0.
516 * *upper_bound Upper bounds or dimensions.
517 * *element_type The type of the array elements.
518 * *element_ent An entity for the array elements to be used for
519 * element selection with Sel.
520 * @@@ Do we need several entities? One might want
521 * to select a dimension and not a single element in
522 * case of multidim arrays.
525 /* create a new type array --
526 Sets n_dimension to dimension and all dimension entries to NULL.
527 Initializes order to the order of the dimensions.
528 The entity for array elements is built automatically.
529 Set dimension sizes after call to constructor with set_* routines. */
530 type *new_type_array (ident *name, int n_dimensions,
532 type *new_d_type_array (ident *name, int n_dimensions,
533 type *element_type, dbg_info* db);
535 /* manipulate private fields of array type */
536 int get_array_n_dimensions (type *array);
537 /* Allocates Const nodes of mode_I for the array dimensions */
538 void set_array_bounds_int (type *array, int dimension, int lower_bound,
540 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
541 ir_node *upper_bound);
542 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
543 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
544 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
545 void set_array_upper_bound_int (type *array, int dimension, int lower_bound);
546 ir_node * get_array_lower_bound (type *array, int dimension);
547 ir_node * get_array_upper_bound (type *array, int dimension);
549 void set_array_order (type *array, int dimension, int order);
550 int get_array_order (type *array, int dimension);
552 void set_array_element_type (type *array, type *tp);
553 type *get_array_element_type (type *array);
555 void set_array_element_entity (type *array, entity *ent);
556 entity *get_array_element_entity (type *array);
559 bool is_array_type (type *array);
562 /****** type/enumeration
564 * Representation of an enumeration type.
566 * Enumeration types need not necessarily be represented explicitly
567 * by Firm types, as the frontend can lower them to integer constants as
568 * well. For debugging purposes or similar tasks this information is useful.
570 * *enum The target values representing the constants used to
571 * represent individual enumerations.
572 * *enum_nameid Idents containing the source program name of the enumeration
577 /* create a new type enumeration -- set the enumerators independently */
578 type *new_type_enumeration (ident *name, int n_enums);
579 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
581 /* manipulate fields of enumeration type. */
582 int get_enumeration_n_enums (type *enumeration);
584 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
585 tarval *get_enumeration_enum (type *enumeration, int pos);
587 void set_enumeration_nameid (type *enumeration, int pos, ident *id);
588 ident *get_enumeration_nameid (type *enumeration, int pos);
589 const char *get_enumeration_name(type *enumeration, int pos);
592 bool is_enumeration_type (type *enumeration);
597 * Representation of a pointer type.
601 * points_to The type of the entity this pointer points to.
604 /* Create a new type pointer */
605 type *new_type_pointer (ident *name, type *points_to);
606 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db);
608 /* manipulate fields of type_pointer */
609 void set_pointer_points_to_type (type *pointer, type *tp);
610 type *get_pointer_points_to_type (type *pointer);
613 bool is_pointer_type (type *pointer);
616 /****** type/primitive
618 * Representation of a primitive type.
620 * Primitive types are types that represent indivisible data values that
621 * map directly to modes. They don't have a private attribute. The
622 * important information they carry is held in the common mode field.
625 /* create a new type primitive */
626 type *new_type_primitive (ident *name, ir_mode *mode);
627 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
630 bool is_primitive_type (type *primitive);
635 /****f* type/is_atomic_type
638 * is_atomic_type - Checks whether a type is atomic.
640 * int is_atomic_type(type *tp);
644 * true if type is primitive, pointer or enumeration
647 int is_atomic_type(type *tp);
649 /****f* type/is_compound_type
652 * is_compound_type - Checks whether a type is compound.
654 * int is_compound_type(type *tp)
658 * true if the type is class, structure, union or array type.
661 int is_compound_type(type *tp);
663 # endif /* _TYPE_H_ */