2 * (C) 2001 by Universitaet Karlsruhe
9 * Datastructure to hold type information.
11 * @author Goetz Lindenmaier
13 * This module supplies a datastructure to represent all types
14 * known in the compiled program. This includes types specified
15 * in the program as well as types defined by the language. In the
16 * view of the intermediate representation there is no difference
17 * between these types.
19 * There exist several kinds of types, arranged by the structure of
20 * the type. A type is described by a set of attributes. Some of
21 * these attributes are common to all types, others depend on the
24 * Types are different from the modes defined in irmode: Types are
25 * on the level of the programming language, modes at the level of
26 * 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;
56 * An abstract data type to represent types.
58 * This is the abstract data type with which any type known in the
59 * compiled program can be represented. This includes types specified
60 * in the program as well as types defined by the language. In the
61 * view of the intermediate representation there is no difference
62 * between these types.
64 * There exist several kinds of types, arranged by the structure of
65 * the type. These are distinguished by a type opcode.
66 * A type is described by a set of attributes. Some of these attributes
67 * are common to all types, others depend on the kind of the type.
69 * The following describes the common attributes. They can only be
70 * accessed by the functions given below.
72 * The common fields are:
74 * - firm_kind A firm_kind tag containing k_type. This is useful
75 * for dynamically checking whether a node is a type node.
76 * - type_op A tp_op specifying the kind of the type.
77 * - mode The mode to be used to represent the type on a machine.
78 * @@@ maybe not global field??
79 * - name An identifier specifying the name of the type. To be
80 * set by the frontend.
81 * - size The size of the type, i.e. an entity of this type will
82 * occupy size bytes in memory. In several cases this is
83 * determined when fixing the layout of this type (class,
84 * struct, union, array, enumeration).
85 * - state The state of the type. The state represents whether the
86 * layout of the type is undefined or fixed (values: layout_undefined
87 * or layout_fixed). Compound types can have an undefined
88 * layout. The layout of the basic types primitive and pointer
89 * is always layout_fixed. If the layout of
90 * compound types is fixed all entities must have an offset
91 * and the size of the type must be set.
92 * A fixed layout for enumeration types means that each enumeration
93 * is associated with an implementation value.
94 * - visit A counter for walks of the type information.
95 * - link A void* to associate some additional information with the type.
97 * These fields can only be accessed via access functions.
99 * Depending on the value of type_op, i.e., depending on the kind of the
100 * type the adt contains further attributes. These are documented below.
101 * @see class, struct, method, union, array, enumeration, pointer, primitive
103 #ifndef _TYPE_TYPEDEF_
104 #define _TYPE_TYPEDEF_
105 typedef struct type type;
108 # include "type_or_entity.h"
110 /** Frees the memory used by the type. Does not free the entities
111 belonging to the type, except for the array element entity. */
112 void free_type(type *tp);
114 tp_op* get_type_tpop(type *tp);
115 ident* get_type_tpop_nameid(type *tp);
116 const char* get_type_tpop_name(type *tp);
117 tp_opcode get_type_tpop_code(type *tp);
119 ident* get_type_ident(type *tp);
120 void set_type_ident(type *tp, ident* id);
121 const char* get_type_name(type *tp);
124 layout_undefined, /**< The layout of this type is not defined.
125 Address computation to access fields is not
126 possible, fields must be accessed by Sel
127 nodes. This is the default value except for
128 pointer, primitive and method types. */
129 layout_fixed /**< The layout is fixed, all component/member entities
130 have an offset assigned. Size of the type is known.
131 Arrays can be accessed by explicit address
132 computation. Default for pointer, primitive ane method
135 type_state get_type_state(type *tp);
136 /* For primitives, pointer and method types the layout is always fixed.
137 This call is legal but has no effect. */
138 void set_type_state(type *tp, type_state state);
140 /* Returns NULL for all non atomic types. */
141 ir_mode* get_type_mode(type *tp);
142 /* Only has an effect on primitive and enumeration types */
143 void set_type_mode(type *tp, ir_mode* m);
145 int get_type_size(type *tp);
146 /* For primitive, enumeration, pointer and method types the size
147 is always fixed. This call is legal but has no effect. */
148 void set_type_size(type *tp, int size);
151 unsigned long get_type_visited(type *tp);
152 void set_type_visited(type *tp, unsigned long num);
153 /* Sets visited field in type to type_visited. */
154 void mark_type_visited(type *tp);
155 /* @@@ name clash!! bool type_visited(type *tp); */
156 bool type_not_visited(type *tp);
158 void* get_type_link(type *tp);
159 void set_type_link(type *tp, void *l);
163 * visited flag to traverse the type information
164 * Increase this flag by one before traversing the type information.
165 * Mark type nodes as visited by set_type_visited(type, type_visited).
166 * Check whether node was already visited by comparing get_type_visited(type)
168 * Or use the function to walk all types.
171 extern unsigned long type_visited;
172 void set_master_type_visited(unsigned long val);
173 unsigned long get_master_type_visited(void);
174 void inc_master_type_visited(void);
178 * Checks whether a pointer points to a type.
180 * @param thing an arbitrary pointer
183 * true if the thing is a type, else false
186 int is_type (void *thing);
189 * Checks whether two types are structural equal.
191 * @param st pointer type
192 * @param lt pointer type
195 * true if the types are equal, else false.
197 * - they are the same type kind
198 * - they have the same name
199 * - they have the same mode (if applicable)
200 * - they have the same type_state and, ev., the same size
201 * - they are class types and have
202 * - the same members (see same_entity in entity.h)
203 * - the same supertypes -- the C-pointers are compared --> no recursive call.
204 * - the same number of subtypes. Subtypes are not compared,
205 * as this could cause a cyclic test.
206 * - the same peculiarity
207 * - they are structure types and have the same members
208 * - they are method types and have
209 * - the same parameter types
210 * - the same result types
211 * - they are union types and have the same members
212 * - they are array types and have
213 * - the same number of dimensions
214 * - the same dimension bounds
215 * - the same dimension order
216 * - the same element type
217 * - they are enumeration types and have the same enumerator names
218 * - they are pointer types and have the identical points_to type
219 * (i.e., the same C-struct to represent the type, type_id is skipped.
220 * This is to avoid endless recursions; with pointer types circlic
221 * type graphs are possible.)
223 bool equal_type(type *tpy1, type *typ2);
226 * Checks whether two types are structural comparable.
228 * @param st pointer type
229 * @param lt pointer type
232 * true if type st is smaller than type lt, i.e. whenever
233 * lt is expected a st can be used.
235 * - they are the same type kind
236 * - mode(st) < mode (lt) (if applicable)
237 * - they are class types and st is (transitive) subtype of lt,
238 * - they are structure types and
239 * - the members of st have exactly one counterpart in lt with the same name,
240 * - the counterpart has a bigger type.
241 * - they are method types and have
242 * - the same number of parameter and result types,
243 * - the parameter types of st are smaller than those of lt,
244 * - the result types of st are smaller than those of lt
245 * - they are union types and have the members of st have exactly one
246 * @return counterpart in lt and the type is smaller
247 * - they are array types and have
248 * - the same number of dimensions
249 * - all bounds of lt are bound of st
250 * - the same dimension order
251 * - the same element type
253 * - the element type of st is smaller than that of lt
254 * - the element types have the same size and fixed layout.
255 * - they are enumeration types and have the same enumerator names
256 * - they are pointer types and have the points_to type of st is
257 * @return smaller than the points_to type of lt.
260 bool smaller_type (type *st, type *lt);
263 * Representation of a class type.
264 * If the type opcode is set to type_class the type represents class
265 * types. A list of fields and methods is associated with a class.
266 * Further a class can inherit from and bequest to other classes.
268 * @param The following attributes are private to this type kind.
269 * @param member All entities belonging to this class. This are methode entities
270 * which have type_method or fields that can have any of the
271 * following type kinds: type_class, type_struct, type_union,
272 * type_array, type_enumeration, type_pointer, type_primitive.
274 * @param subtypes A list of direct subclasses.
276 * @param supertypes A list of direct superclasses.
278 * @param These are dynamic lists that can be grown with an "add_" function,
279 * @param but not shrinked.
281 * @param peculiarity The peculiarity of this class. If the class is of peculiarity
282 * "description" it only is a description of requirememts to a class,
283 * as, e.g., a Java interface. The class will never be allocated.
284 * Peculiatity inherited is only possible for entities. An entity
285 * is of peculiarity inherited if the compiler generated the entity
286 * to explicitly resolve inheritance. An inherited method entity has
288 * Values: description, existent, inherited. Default: existent.
291 /* create a new class type */
292 type *new_type_class (ident *name);
293 type *new_d_type_class (ident *name, dbg_info *db);
295 /** manipulate private fields of class type **/
296 /* Adds the entity as member of the class. */
297 void add_class_member (type *clss, entity *member);
298 /* Returns the number of members of this class. */
299 int get_class_n_members (type *clss);
300 /* Returns the member at position pos, 0 <= pos < n_member */
301 entity *get_class_member (type *clss, int pos);
302 /** Returns index of mem in clss, -1 if not contained. */
303 int get_class_member_index(type *clss, entity *mem);
304 /* Overwrites the member at position pos, 0 <= pos < n_member with
305 the passed entity. */
306 void set_class_member (type *clss, entity *member, int pos);
307 /* Replaces complete member list in class type by the list passed. Copies the
308 list passed. This function is necessary to reduce the number of members.
309 members is an array of entities, num the size of this array. Sets all
310 owners of the members passed to clss. */
311 void set_class_members (type *clss, entity *members[], int arity);
312 /* Finds member in the list of members and removes it.
313 Shrinks the member list, so iterate from the end!!!
314 Does not deallocate the entity. */
315 void remove_class_member(type *clss, entity *member);
318 /* Adds subtype as subtype to clss.
319 Checks whether clss is a supertype of subtype. If not
320 adds also clss as supertype to subtype. */
321 void add_class_subtype (type *clss, type *subtype);
322 /* Returns the number of subtypes */
323 int get_class_n_subtypes (type *clss);
324 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
325 type *get_class_subtype (type *clss, int pos);
326 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype. Does not
327 set the corresponding supertype relation for subtype: this might
328 be a different position! */
329 void set_class_subtype (type *clss, type *subtype, int pos);
330 /* Finds subtype in the list of subtypes and removes it */
331 void remove_class_subtype(type *clss, type *subtype);
334 /* Adds supertype as supertype to class.
335 Checks whether clss is a subtype of supertype. If not
336 adds also clss as subtype to supertype. */
337 void add_class_supertype (type *clss, type *supertype);
338 /* Returns the number of supertypes */
339 int get_class_n_supertypes (type *clss);
340 int get_class_supertype_index(type *clss, type *super_clss);
341 /* Gets the supertype at position pos, 0 <= pos < n_supertype. */
342 type *get_class_supertype (type *clss, int pos);
343 /* Sets the supertype at postition pos, 0 <= pos < n_subtype. Does not
344 set the corresponding subtype relation for supertype: this might
345 be a different position! */
346 void set_class_supertype (type *clss, type *supertype, int pos);
347 /* Finds supertype in the list of supertypes and removes it */
348 void remove_class_supertype(type *clss, type *supertype);
350 /** This enumeration flags the peculiarity of entities and types. */
351 typedef enum peculiarity {
352 description, /**< Represents only a description. The entity/type is never
353 allocated, no code/data exists for this entity/type. */
354 inherited, /**< Describes explicitly that other entities are
355 inherited to the owner of this entity.
356 Overwrites must refer to at least one other
357 entity. If this is a method entity there exists
358 no irg for this entity, only for one of the
360 existent /**< The entity/type (can) exist. */
363 /* The peculiarity of the class. The enumeration peculiarity is defined
365 INLINE peculiarity get_class_peculiarity (type *clss);
366 INLINE void set_class_peculiarity (type *clss, peculiarity pec);
368 /* Set and get a class' dfn --
369 @todo This is an undocumented field, subject to change! */
370 void set_class_dfn (type *clss, int dfn);
371 int get_class_dfn (type *clss);
374 bool is_class_type(type *clss);
375 /* Returns true if low is subclass of high. */
376 bool is_subclass_of(type *low, type *high);
379 * Representation of a struct type.
380 * Type_strct represents aggregate types that consist of a list
382 * @param member All entities belonging to this class. This are the fields
383 * that can have any of the following types: type_class,
384 * type_struct, type_union, type_array, type_enumeration,
385 * type_pointer, type_primitive.
386 * This is a dynamic list that can be grown with an "add_" function,
388 * This is a dynamic list that can be grown with an "add_" function,
391 /* create a new type struct */
392 type *new_type_struct (ident *name);
393 type *new_d_type_struct (ident *name, dbg_info* db);
395 /* manipulate private fields of struct */
396 void add_struct_member (type *strct, entity *member);
397 int get_struct_n_members (type *strct);
398 entity *get_struct_member (type *strct, int pos);
399 void set_struct_member (type *strct, int pos, entity *member);
400 /* Finds member in the list of memberss and removees it */
401 void remove_struct_member (type *strct, entity *member);
404 bool is_struct_type(type *strct);
407 * Representation of a method type.
408 * A method type represents a method, function or procedure type.
409 * It contains a list of the parameter and result types, as these
410 * are part of the type description. These lists should not
411 * be changed by a optimization, as a change creates a new method
412 * type. Therefore optimizations should allocated new method types.
413 * The set_ routines are only for construction by a frontend.
414 * @param n_params Number of parameters to the procedure.
415 * A procedure in FIRM has only call by value parameters.
417 * @param param_type A list with the types of parameters. This list is ordered.
418 * The nth type in this list corresponds to the nth element
419 * in the parameter tuple that is a result of the start node.
420 * (See ircons.h for more information.)
422 * @param n_res The number of results of the method. In general, procedures
423 * have zero results, functions one.
425 * @param res_type A list with the types of parameters. This list is ordered.
426 * The nth type in this list corresponds to the nth input to
427 * Return nodes. (See ircons.h for more information.)
430 /** Create a new method type.
432 @param n_param the number of parameters
433 @param n_res the number of results
435 The arrays for the parameter and result types are not initialized by
437 type *new_type_method (ident *name, int n_param, int n_res);
438 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
440 /** manipulate private fields of method. */
441 int get_method_n_params (type *method);
442 type *get_method_param_type(type *method, int pos);
443 void set_method_param_type(type *method, int pos, type* tp);
445 int get_method_n_ress (type *method);
446 type *get_method_res_type(type *method, int pos);
447 void set_method_res_type(type *method, int pos, type* tp);
450 * this enum flags the variadicity of methods (methods with a
451 * variable amount of arguments (e.g. C's printf). Default is
454 typedef enum variadicity {
459 variadicity get_method_variadicity(type *method);
460 void set_method_variadicity(type *method, variadicity vari);
463 bool is_method_type (type *method);
466 * Representation of a union type.
467 * The union type represents union types.
468 * @param n_types Number of unioned types.
469 * @param members Entities for unioned types. Fixed length array.
470 * This is a dynamic list that can be grown with an "add_" function,
473 /* create a new type union */
474 type *new_type_union (ident *name);
475 type *new_d_type_union (ident *name, dbg_info* db);
477 /* manipulate private fields of struct */
478 int get_union_n_members (type *uni);
479 void add_union_member (type *uni, entity *member);
480 entity *get_union_member (type *uni, int pos);
481 void set_union_member (type *uni, int pos, entity *member);
482 /* Finds member in the list of members and removes it. */
483 void remove_union_member (type *uni, entity *member);
486 bool is_union_type (type *uni);
489 * Representation of an array type.
490 * The array type represents rectangular multi dimensional arrays.
491 * The constants representing the bounds must be allocated to
492 * get_const_code_irg() by setting current_ir_graph accordingly.
493 * @param n_dimensions Number of array dimensions.
494 * @param *lower_bound Lower bounds of dimensions. Usually all 0.
495 * @param *upper_bound Upper bounds or dimensions.
496 * @param *element_type The type of the array elements.
497 * @param *element_ent An entity for the array elements to be used for
498 * element selection with Sel.
500 * Do we need several entities? One might want
501 * to select a dimension and not a single element in case of multidim arrays.
503 /* create a new type array --
504 Sets n_dimension to dimension and all dimension entries to NULL.
505 Initializes order to the order of the dimensions.
506 The entity for array elements is built automatically.
507 Set dimension sizes after call to constructor with set_* routines. */
508 type *new_type_array (ident *name, int n_dimensions,
510 type *new_d_type_array (ident *name, int n_dimensions,
511 type *element_type, dbg_info* db);
513 /* manipulate private fields of array type */
514 int get_array_n_dimensions (type *array);
515 /* Allocates Const nodes of mode_I for the array dimensions */
516 void set_array_bounds_int (type *array, int dimension, int lower_bound,
518 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
519 ir_node *upper_bound);
520 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
521 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
522 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
523 void set_array_upper_bound_int (type *array, int dimension, int lower_bound);
524 ir_node * get_array_lower_bound (type *array, int dimension);
525 ir_node * get_array_upper_bound (type *array, int dimension);
527 void set_array_order (type *array, int dimension, int order);
528 int get_array_order (type *array, int dimension);
530 void set_array_element_type (type *array, type *tp);
531 type *get_array_element_type (type *array);
533 void set_array_element_entity (type *array, entity *ent);
534 entity *get_array_element_entity (type *array);
537 bool is_array_type (type *array);
540 * Representation of an enumeration type.
541 * Enumeration types need not necessarily be represented explicitly
542 * by Firm types, as the frontend can lower them to integer constants as
543 * well. For debugging purposes or similar tasks this information is useful.
544 * @param *enum The target values representing the constants used to
545 * represent individual enumerations.
546 * @param *enum_nameid Idents containing the source program name of the enumeration
550 /* create a new type enumeration -- set the enumerators independently */
551 type *new_type_enumeration (ident *name, int n_enums);
552 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
554 /* manipulate fields of enumeration type. */
555 int get_enumeration_n_enums (type *enumeration);
557 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
558 tarval *get_enumeration_enum (type *enumeration, int pos);
560 void set_enumeration_nameid (type *enumeration, int pos, ident *id);
561 ident *get_enumeration_nameid (type *enumeration, int pos);
562 const char *get_enumeration_name(type *enumeration, int pos);
565 bool is_enumeration_type (type *enumeration);
568 * Representation of a pointer type.
570 * @param points_to The type of the entity this pointer points to.
572 /* Create a new type pointer */
573 type *new_type_pointer (ident *name, type *points_to);
574 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db);
576 /* manipulate fields of type_pointer */
577 void set_pointer_points_to_type (type *pointer, type *tp);
578 type *get_pointer_points_to_type (type *pointer);
581 bool is_pointer_type (type *pointer);
584 * Representation of a primitive type.
585 * Primitive types are types that represent indivisible data values that
586 * map directly to modes. They don't have a private attribute. The
587 * important information they carry is held in the common mode field.
589 /* create a new type primitive */
590 type *new_type_primitive (ident *name, ir_mode *mode);
591 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
594 bool is_primitive_type (type *primitive);
599 * Checks whether a type is atomic.
600 * @param tp - any type
601 * @return true if type is primitive, pointer or enumeration
603 int is_atomic_type(type *tp);
606 * Checks whether a type is compound.
607 * @param tp - any type
608 * @return true if the type is class, structure, union or array type.
610 int is_compound_type(type *tp);
613 /** Outputs a unique number for this type if libfirm is compiled for
614 debugging, (configure with --enable-debug) else returns 0. */
615 INLINE long get_type_nr(type *tp);
617 # endif /* _TYPE_H_ */