4 * Project: libFIRM <br>
5 * File name: ir/tr/type.h <br>
6 * Purpose: Representation of types. <br>
7 * Author: Goetz Lindenmaier <br>
10 * Copyright: (c) 2001-2003 Universität Karlsruhe <br>
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. <br>
15 * Datastructure to hold type information.
17 * This module supplies a datastructure to represent all types
18 * known in the compiled program. This includes types specified
19 * in the program as well as types defined by the language. In the
20 * view of the intermediate representation there is no difference
21 * between these types. Finally it specifies some auxiliary types.
23 * There exist several kinds of types, arranged by the structure of
24 * the type. A type is described by a set of attributes. Some of
25 * these attributes are common to all types, others depend on the
28 * Types are different from the modes defined in irmode: Types are
29 * on the level of the programming language, modes at the level of
30 * the target processor.
38 # include "firm_types.h"
40 # include "firm_common.h"
44 # include "tr_inheritance.h"
47 * An abstract data type to represent types.
49 * This is the abstract data type with which any type known in the
50 * compiled program can be represented. This includes types specified
51 * in the program as well as types defined by the language. In the
52 * view of the intermediate representation there is no difference
53 * between these types.
55 * There exist several kinds of types, arranged by the structure of
56 * the type. These are distinguished by a type opcode.
57 * A type is described by a set of attributes. Some of these attributes
58 * are common to all types, others depend on the kind of the type.
60 * The following describes the common attributes. They can only be
61 * accessed by the functions given below.
63 * The common fields are:
65 * - firm_kind: A firm_kind tag containing k_type. This is useful
66 * for dynamically checking whether a node is a type node.
67 * - type_op: A tp_op specifying the kind of the type.
68 * - mode: The mode to be used to represent the type on a machine.
69 * - name: An identifier specifying the name of the type. To be
70 * set by the frontend.
71 * - size: The size of the type, i.e. an entity of this type will
72 * occupy size bits in memory. In several cases this is
73 * determined when fixing the layout of this type (class,
74 * struct, union, array, enumeration).
75 * - alignment The alignment of the type, i.e. an entity of this type will
76 * be allocated an an address in memory with this alignment.
77 * In several cases this is determined when fixing the layout
78 * of this type (class, struct, union, array)
79 * - state: The state of the type. The state represents whether the
80 * layout of the type is undefined or fixed (values: layout_undefined
81 * or layout_fixed). Compound types can have an undefined
82 * layout. The layout of the basic types primitive and pointer
83 * is always layout_fixed. If the layout of
84 * compound types is fixed all entities must have an offset
85 * and the size of the type must be set.
86 * A fixed layout for enumeration types means that each enumeration
87 * is associated with an implementation value.
88 * - visit: A counter for walks of the type information.
89 * - link: A void* to associate some additional information with the type.
91 * These fields can only be accessed via access functions.
93 * Depending on the value of @c type_op, i.e., depending on the kind of the
94 * type the adt contains further attributes. These are documented below.
98 * @link class_type class @endlink, @link struct_type struct @endlink,
99 * @link method_type method @endlink, @link union_type union @endlink,
100 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
101 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
104 * mode maybe not global field??
106 #ifndef _TYPE_TYPEDEF_
107 #define _TYPE_TYPEDEF_
108 typedef struct type type;
111 # include "type_or_entity.h"
113 /** frees all entities associated with a type.
114 Does not free array entity.
115 Warning: make sure these entities are not referenced anywhere else.
117 void free_type_entities(type *tp);
119 /** Frees the memory used by the type.
121 * Removes the type from the type list. Does not free the entities
122 * belonging to the type, except for the array element entity. Does
123 * not free if tp is "none" or "unknown". Frees entities in value
124 * param subtypes of method types!!! Make sure these are not
125 * referenced any more. Further make sure there is no pointer type
126 * that refers to this type. */
127 void free_type(type *tp);
129 const tp_op*get_type_tpop(const type *tp);
130 ident* get_type_tpop_nameid(const type *tp);
131 const char* get_type_tpop_name(const type *tp);
132 tp_opcode get_type_tpop_code(const type *tp);
134 ident* get_type_ident(const type *tp);
135 void set_type_ident(type *tp, ident* id);
136 const char* get_type_name(const type *tp);
138 /** This enumeration flags the visibility of entities and types.
140 * This is necessary for partial compilation.
141 * We rely on the ordering of the flags.
144 visibility_local, /**< The entity is only visible locally. This is the default for
146 The type is only visible locally. All instances are allocated
147 locally, and no pointer to entities of this type are passed
148 out of this compilation unit. */
149 visibility_external_visible, /**< The entity is visible to other external program parts, but
150 it is defined here. It may not be optimized away. The entity must
152 For types: entities of this type can be accessed externally. No
153 instances of this type are allocated externally. */
154 visibility_external_allocated /**< The entity is defined and allocated externally. This compilation
155 must not allocate memory for this entity. The entity must
156 be static_allocated. This can also be an external defined
158 For types: entities of this type are allocated and accessed from
159 external code. Default for types. */
162 /** The visibility of a type.
164 * The visibility of a type indicates, whether entities of this type
165 * are accessed or allocated in external code.
167 * An entity of a type is allocated in external code, if the external
168 * code declares a variable of this type, or dynamically allocates
169 * an entity of this type. If the external code declares a (compound)
170 * type, that contains entities of this type, the visibility also
171 * must be external_allocated.
173 * The visibility must be higher than that of all entities, if the
174 * type is a compound. Here it is questionable, what happens with
175 * static entities. If these are accessed external by direct reference,
176 * (a static call to a method, that is also in the dispatch table)
177 * it should not affect the visibility of the type.
180 * @@@ Do we need a visibility for types?
181 * I change the layout of types radically when doing type splitting.
182 * I need to know, which fields of classes are accessed in the RTS,
183 * e.g., [_length. I may not move [_length to the split part.
184 * The layout though, is a property of the type.
186 * One could also think of changing the mode of a type ...
188 * But, we could also output macros to access the fields, e.g.,
189 * ACCESS_[_length (X) X->length // conventional
190 * ACCESS_[_length (X) X->_split_ref->length // with type splitting
192 * For now I implement this function, that returns the visibility
193 * based on the visibility of the entities of a compound ...
195 * This function returns visibility_external_visible if one or more
196 * entities of a compound type have visibility_external_visible.
197 * Entities of types are never visibility_external_allocated (right?).
198 * Else returns visibility_local.
200 visibility get_type_visibility (const type *tp);
201 void set_type_visibility (type *tp, visibility v);
205 /** The state of the type layout. */
207 layout_undefined, /**< The layout of this type is not defined.
208 Address computation to access fields is not
209 possible, fields must be accessed by Sel
210 nodes. This is the default value except for
211 pointer, primitive and method types. */
212 layout_fixed /**< The layout is fixed, all component/member entities
213 have an offset assigned. Size of the type is known.
214 Arrays can be accessed by explicit address
215 computation. Default for pointer, primitive and method
219 /** Returns a human readable string for the enum entry. */
220 const char *get_type_state_name(type_state s);
222 /** Returns the type layout state of a type. */
223 type_state get_type_state(const type *tp);
225 /** Sets the type layout state of a type.
227 * For primitives, pointer and method types the layout is always fixed.
228 * This call is legal but has no effect.
230 void set_type_state(type *tp, type_state state);
232 /** Returns the mode of a type.
234 * Returns NULL for all non atomic types.
236 ir_mode* get_type_mode(const type *tp);
238 /** Sets the mode of a type.
240 * Only has an effect on primitive, enumeration and pointer types.
242 void set_type_mode(type *tp, ir_mode* m);
244 /** Returns the size of a type in bytes, returns -1 if the size is NOT
245 * a byte size, i.e. not dividable by 8. */
246 int get_type_size_bytes(const type *tp);
248 /** Returns the size of a type in bits. */
249 int get_type_size_bits(const type *tp);
251 /** Sets the size of a type in bytes.
253 * For primitive, enumeration, pointer and method types the size
254 * is always fixed. This call is legal but has no effect.
256 void set_type_size_bytes(type *tp, int size);
258 /** Sets the size of a type in bits.
260 * For primitive, enumeration, pointer and method types the size
261 * is always fixed. This call is legal but has no effect.
263 void set_type_size_bits(type *tp, int size);
265 /** Returns the alignment of a type in bytes.
267 * Returns -1 if the alignment is NOT
268 * a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
269 int get_type_alignment_bytes(type *tp);
271 /** Returns the alignment of a type in bits.
273 * If the alignment of a type is
274 * not set, it is calculated here according to the following rules:
275 * -#.) if a type has a mode, the alignment is the mode size.
276 * -#.) compound types have the alignment of there biggest member.
277 * -#.) array types have the alignment of there element type.
278 * -#.) method types return 0 here.
279 * -#.) all other types return 8 here (i.e. aligned at byte).
281 int get_type_alignment_bits(type *tp);
283 /** Sets the alignment of a type in bytes. */
284 void set_type_alignment_bytes(type *tp, int size);
286 /** Sets the alignment of a type in bits.
288 * For method types the alignment is always fixed.
289 * This call is legal but has no effect.
291 void set_type_alignment_bits(type *tp, int size);
293 unsigned long get_type_visited(const type *tp);
294 void set_type_visited(type *tp, unsigned long num);
295 /* Sets visited field in type to type_visited. */
296 void mark_type_visited(type *tp);
297 int type_visited(const type *tp);
298 int type_not_visited(const type *tp);
300 /** Returns the associated link field of a type. */
301 void* get_type_link(const type *tp);
302 /** Sets the associated link field of a type. */
303 void set_type_link(type *tp, void *l);
306 * Visited flag to traverse the type information.
308 * Increase this flag by one before traversing the type information
309 * using inc_master_type_visited().
310 * Mark type nodes as visited by mark_type_visited(type).
311 * Check whether node was already visited by type_visited(type)
312 * and type_not_visited(type).
313 * Or use the function to walk all types.
317 void set_master_type_visited(unsigned long val);
318 unsigned long get_master_type_visited(void);
319 void inc_master_type_visited(void);
322 * Checks whether a pointer points to a type.
324 * @param thing an arbitrary pointer
327 * true if the thing is a type, else false
329 int is_type (const void *thing);
332 * Checks whether two types are structurally equal.
334 * @param typ1 the first type
335 * @param typ2 the second type
338 * true if the types are equal, else false.
340 * Types are equal if :
341 * - they are the same type kind
342 * - they have the same name
343 * - they have the same mode (if applicable)
344 * - they have the same type_state and, ev., the same size
345 * - they are class types and have:
346 * - the same members (see same_entity in entity.h)
347 * - the same supertypes -- the C-pointers are compared --> no recursive call.
348 * - the same number of subtypes. Subtypes are not compared,
349 * as this could cause a cyclic test.
350 * - the same peculiarity
351 * - they are structure types and have the same members
352 * - they are method types and have
353 * - the same parameter types
354 * - the same result types
355 * - they are union types and have the same members
356 * - they are array types and have
357 * - the same number of dimensions
358 * - the same dimension bounds
359 * - the same dimension order
360 * - the same element type
361 * - they are enumeration types and have the same enumerator names
362 * - they are pointer types and have the identical points_to type
363 * (i.e., the same C-struct to represent the type, type_id is skipped.
364 * This is to avoid endless recursions; with pointer types cyclic
365 * type graphs are possible.)
367 int equal_type(type *typ1, type *typ2);
370 * Checks whether two types are structural comparable.
372 * @param st pointer type
373 * @param lt pointer type
376 * true if type st is smaller than type lt, i.e. whenever
377 * lt is expected a st can be used.
379 * - they are the same type kind
380 * - mode(st) < mode (lt) (if applicable)
381 * - they are class types and st is (transitive) subtype of lt,
382 * - they are structure types and
383 * - the members of st have exactly one counterpart in lt with the same name,
384 * - the counterpart has a bigger type.
385 * - they are method types and have
386 * - the same number of parameter and result types,
387 * - the parameter types of st are smaller than those of lt,
388 * - the result types of st are smaller than those of lt
389 * - they are union types and have the members of st have exactly one
390 * @return counterpart in lt and the type is smaller
391 * - they are array types and have
392 * - the same number of dimensions
393 * - all bounds of lt are bound of st
394 * - the same dimension order
395 * - the same element type
397 * - the element type of st is smaller than that of lt
398 * - the element types have the same size and fixed layout.
399 * - they are enumeration types and have the same enumerator names
400 * - they are pointer types and have the points_to type of st is
401 * @return smaller than the points_to type of lt.
404 int smaller_type (type *st, type *lt);
407 * @page class_type Representation of a class type
409 * If the type opcode is set to type_class the type represents class
410 * types. A list of fields and methods is associated with a class.
411 * Further a class can inherit from and bequest to other classes.
413 * The following attributes are private to this type kind:
414 * - member: All entities belonging to this class. This are method entities
415 * which have type_method or fields that can have any of the
416 * following type kinds: type_class, type_struct, type_union,
417 * type_array, type_enumeration, type_pointer, type_primitive.
419 * The following two are dynamic lists that can be grown with an "add_" function,
422 * - subtypes: A list of direct subclasses.
424 * - supertypes: A list of direct superclasses.
426 * - peculiarity: The peculiarity of this class. If the class is of peculiarity
427 * "description" it only is a description of requirements to a class,
428 * as, e.g., a Java interface. The class will never be allocated.
429 * Peculiarity inherited is only possible for entities. An entity
430 * is of peculiarity inherited if the compiler generated the entity
431 * to explicitly resolve inheritance. An inherited method entity has
433 * Values: description, existent, inherited. Default: existent.
437 /** Creates a new class type. */
438 type *new_type_class (ident *name);
440 /** Creates a new class type with debug information. */
441 type *new_d_type_class (ident *name, dbg_info *db);
443 /* --- manipulate private fields of class type --- */
445 /** Adds the entity as member of the class. */
446 void add_class_member (type *clss, entity *member);
448 /** Returns the number of members of this class. */
449 int get_class_n_members (const type *clss);
451 /** Returns the member at position pos, 0 <= pos < n_member */
452 entity *get_class_member (const type *clss, int pos);
454 /** Returns index of mem in clss, -1 if not contained. */
455 int get_class_member_index(type *clss, entity *mem);
457 /** Finds the member with name 'name'. If several members with the same
458 * name returns one of them. Returns NULL if no member found. */
459 entity *get_class_member_by_name(type *clss, ident *name);
461 /** Overwrites the member at position pos, 0 <= pos < n_member with
462 * the passed entity. */
463 void set_class_member (type *clss, entity *member, int pos);
465 /** Replaces complete member list in class type by the list passed.
467 * Copies the list passed. This function is necessary to reduce the number of members.
468 * members is an array of entities, num the size of this array. Sets all
469 * owners of the members passed to clss. */
470 void set_class_members (type *clss, entity *members[], int arity);
472 /** Finds member in the list of members and removes it.
474 * Shrinks the member list, so iterate from the end!!!
475 * Does not deallocate the entity. */
476 void remove_class_member(type *clss, entity *member);
479 /** Adds subtype as subtype to clss.
481 * Checks whether clss is a supertype of subtype. If not
482 * adds also clss as supertype to subtype. */
483 void add_class_subtype (type *clss, type *subtype);
485 /** Returns the number of subtypes */
486 int get_class_n_subtypes (const type *clss);
488 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
489 type *get_class_subtype (type *clss, int pos);
491 /** Returns the index to access subclass as subtype of class.
493 * If subclass is no direct subtype of class returns -1.
495 int get_class_subtype_index(type *clss, const type *subclass);
497 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
499 * Does not set the corresponding supertype relation for subtype: this might
500 * be a different position! */
501 void set_class_subtype (type *clss, type *subtype, int pos);
503 /** Finds subtype in the list of subtypes and removes it */
504 void remove_class_subtype(type *clss, type *subtype);
506 /* Convenience macros */
507 #define add_class_derived_type(clss, drvtype) add_class_subtype(clss, drvtype)
508 #define get_class_n_derived_types(clss) get_class_n_subtypes(clss)
509 #define get_class_derived_type(clss, pos) get_class_subtype(clss, pos)
510 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
511 #define set_class_derived_type(clss, drvtype, pos) set_class_subtype(clss, drvtype, pos)
512 #define remove_class_derived_type(clss, drvtype) remove_class_subtype(clss, drvtype)
514 /** Adds supertype as supertype to class.
516 * Checks whether clss is a subtype of supertype. If not
517 * adds also clss as subtype to supertype. */
518 void add_class_supertype (type *clss, type *supertype);
520 /** Returns the number of supertypes */
521 int get_class_n_supertypes (const type *clss);
523 /** Returns the index to access superclass as supertype of class.
525 * If superclass is no direct supertype of class returns -1.
527 int get_class_supertype_index(type *clss, type *super_clss);
529 /** Gets the supertype at position pos, 0 <= pos < n_supertype. */
530 type *get_class_supertype (type *clss, int pos);
532 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
534 * Does not set the corresponding subtype relation for supertype: this might
535 * be at a different position! */
536 void set_class_supertype (type *clss, type *supertype, int pos);
538 /** Finds supertype in the list of supertypes and removes it */
539 void remove_class_supertype(type *clss, type *supertype);
541 /** Convenience macro */
542 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
543 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
544 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
545 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
546 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
547 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
549 /** Convenience macro */
550 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
551 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
552 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
553 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
554 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
555 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
557 /** This enumeration flags the peculiarity of entities and types. */
558 typedef enum peculiarity {
559 peculiarity_description, /**< Represents only a description. The entity/type is never
560 allocated, no code/data exists for this entity/type.
561 @@@ eventually rename to descriptive (adjective as the others!)*/
562 peculiarity_inherited, /**< Describes explicitly that other entities are
563 inherited to the owner of this entity.
564 Overwrites must refer to at least one other
565 entity. If this is a method entity there exists
566 no irg for this entity, only for one of the
569 peculiarity_existent /**< The entity/type (can) exist.
570 @@@ eventually rename to 'real' i.e., 'echt'
571 This serves better as opposition to description _and_ inherited.*/
573 const char *get_peculiarity_string(peculiarity p);
575 /** Returns the peculiarity of the class. */
576 peculiarity get_class_peculiarity (const type *clss);
577 /** Sets the peculiarity of the class. */
578 void set_class_peculiarity (type *clss, peculiarity pec);
580 /* Set and get a class' dfn --
581 @todo This is an undocumented field, subject to change! */
582 void set_class_dfn (type *clss, int dfn);
583 int get_class_dfn (const type *clss);
585 /** Returns true if a type is a class type. */
586 int is_Class_type(const type *clss);
589 * @page struct_type Representation of a struct type
591 * Type_strct represents aggregate types that consist of a list
593 * The following attributes are private to this type kind:
594 * - member: All entities belonging to this class. This are the fields
595 * that can have any of the following types: type_class,
596 * type_struct, type_union, type_array, type_enumeration,
597 * type_pointer, type_primitive.
598 * This is a dynamic list that can be grown with an "add_" function,
600 * This is a dynamic list that can be grown with an "add_" function,
603 /** Creates a new type struct */
604 type *new_type_struct (ident *name);
605 /** Creates a new type struct with debug information. */
606 type *new_d_type_struct (ident *name, dbg_info* db);
608 /* --- manipulate private fields of struct --- */
610 /** Adds the entity as member of the struct. */
611 void add_struct_member (type *strct, entity *member);
613 /** Returns the number of members of this struct. */
614 int get_struct_n_members (const type *strct);
616 /** Returns the member at position pos, 0 <= pos < n_member */
617 entity *get_struct_member (const type *strct, int pos);
619 /** Returns index of member in strct, -1 if not contained. */
620 int get_struct_member_index(type *strct, entity *member);
622 /** Overwrites the member at position pos, 0 <= pos < n_member with
623 the passed entity. */
624 void set_struct_member (type *strct, int pos, entity *member);
626 /** Finds member in the list of members and removes it. */
627 void remove_struct_member (type *strct, entity *member);
629 /** Returns true if a type is a struct type. */
630 int is_Struct_type(const type *strct);
633 * @page method_type Representation of a method type
635 * A method type represents a method, function or procedure type.
636 * It contains a list of the parameter and result types, as these
637 * are part of the type description. These lists should not
638 * be changed by a optimization, as a change creates a new method
639 * type. Therefore optimizations should allocated new method types.
640 * The set_ routines are only for construction by a frontend.
642 * - n_params: Number of parameters to the procedure.
643 * A procedure in FIRM has only call by value parameters.
645 * - param_type: A list with the types of parameters. This list is ordered.
646 * The nth type in this list corresponds to the nth element
647 * in the parameter tuple that is a result of the start node.
648 * (See ircons.h for more information.)
651 * A list of entities (whose owner is a struct private to the
652 * method type) that represent parameters passed by value.
654 * - n_res: The number of results of the method. In general, procedures
655 * have zero results, functions one.
657 * - res_type: A list with the types of parameters. This list is ordered.
658 * The nth type in this list corresponds to the nth input to
659 * Return nodes. (See ircons.h for more information.)
662 * A list of entities (whose owner is a struct private to the
663 * method type) that represent results passed by value.
666 /* These macros define the suffixes for the types and entities used
667 to represent value parameters / results. */
668 #define VALUE_PARAMS_SUFFIX "val_param"
669 #define VALUE_RESS_SUFFIX "val_res"
671 /** Create a new method type.
673 * @param name the name (ident) of this type
674 * @param n_param the number of parameters
675 * @param n_res the number of results
677 * The arrays for the parameter and result types are not initialized by
680 type *new_type_method (ident *name, int n_param, int n_res);
682 /** Create a new method type with debug information.
684 * @param name the name (ident) of this type
685 * @param n_param the number of parameters
686 * @param n_res the number of results
687 * @param db user defined debug information
689 * The arrays for the parameter and result types are not initialized by
692 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
694 /* -- manipulate private fields of method. -- */
696 /** Returns the number of parameters of this method. */
697 int get_method_n_params (const type *method);
699 /** Returns the type of the parameter at position pos of a method. */
700 type *get_method_param_type(type *method, int pos);
701 /** Sets the type of the parameter at position pos of a method.
702 Also changes the type in the pass-by-value representation by just
703 changing the type of the corresponding entity if the representation is constructed. */
704 void set_method_param_type(type *method, int pos, type* tp);
705 /** Returns an entity that represents the copied value argument. Only necessary
706 for compounds passed by value. This information is constructed only on demand. */
707 entity *get_method_value_param_ent(type *method, int pos);
709 * Returns a type that represents the copied value arguments.
711 type *get_method_value_param_type(const type *method);
713 /** Returns the number of results of a method type. */
714 int get_method_n_ress (const type *method);
715 /** Returns the return type of a method type at position pos. */
716 type *get_method_res_type(type *method, int pos);
717 /** Sets the type of the result at position pos of a method.
718 Also changes the type in the pass-by-value representation by just
719 changing the type of the corresponding entity if the representation is constructed. */
720 void set_method_res_type(type *method, int pos, type* tp);
721 /** Returns an entity that represents the copied value result. Only necessary
722 for compounds passed by value. This information is constructed only on demand. */
723 entity *get_method_value_res_ent(type *method, int pos);
726 * Returns a type that represents the copied value results.
728 type *get_method_value_res_type(const type *method);
731 * This enum flags the variadicity of methods (methods with a
732 * variable amount of arguments (e.g. C's printf). Default is
735 typedef enum variadicity {
736 variadicity_non_variadic, /**< non variadic */
737 variadicity_variadic /**< variadic */
740 /** Returns the null-terminated name of this variadicity. */
741 const char *get_variadicity_name(variadicity vari);
743 /** Returns the variadicity of a method. */
744 variadicity get_method_variadicity(const type *method);
746 /** Sets the variadicity of a method. */
747 void set_method_variadicity(type *method, variadicity vari);
750 * Returns the first variadic parameter index of a type.
751 * If this index was NOT set, the index of the last parameter
752 * of the method type plus one is returned for variadic functions.
753 * Non-variadic function types always return -1 here.
755 int get_method_first_variadic_param_index(const type *method);
758 * Sets the first variadic parameter index. This allows to specify
759 * a complete call type (containing the type of all parameters)
760 * but still have the knowledge, which parameter must be passed as
763 void set_method_first_variadic_param_index(type *method, int index);
766 * additional method type properties:
767 * Tell about special properties of a method type. Some
768 * of these may be discovered by analyses.
771 mtp_no_property = 0x00000000, /**< no additional properties, default */
772 mtp_property_const = 0x00000001, /**< This method did not access memory and calculates
773 its return values solely from its parameters.
774 GCC: __attribute__((const)). */
775 mtp_property_pure = 0x00000002, /**< This method did NOT write to memory and calculates
776 its return values solely form its parameters and
777 the memory they points to (or global vars).
778 GCC: __attribute__((pure)). */
779 mtp_property_noreturn = 0x00000004, /**< This method did not return due to an aborting system
781 GCC: __attribute__((noreturn)). */
782 mtp_property_nothrow = 0x00000008, /**< This method cannot throw an exception.
783 GCC: __attribute__((nothrow)). */
784 mtp_property_naked = 0x00000010, /**< This method is naked.
785 GCC: __attribute__((naked)). */
786 mtp_property_malloc = 0x00000020, /**< This method returns newly allocate memory.
787 GCC: __attribute__((malloc)). */
788 mtp_property_inherited = 0x40000000 /**< used only in irg's, means property is inherited
790 } mtp_additional_property;
792 /** Returns the mask of the additional graph properties. */
793 unsigned get_method_additional_properties(const type *method);
795 /** Sets the mask of the additional graph properties. */
796 void set_method_additional_properties(type *method, unsigned property_mask);
798 /** Sets one additional graph property. */
799 void set_method_additional_property(type *method, mtp_additional_property flag);
802 * calling conventions: the lower 8 bits are flags, the upper 24
803 * are the number of arguments transmitted in registers.
806 cc_reg_param = 0x00000001, /**< Transmit parameters in registers, else the stack is used.
807 This flag may be set as default on some architectures. */
808 cc_last_on_top = 0x00000002, /**< The last non-register parameter is transmitted on top of
809 the stack. If this flag is not set, the first
810 non-register parameter is used (cdecl/stdcall convention) */
811 cc_callee_clear_stk = 0x00000004, /**< The callee clears the stack. This forbids variadic
812 function calls (stdcall). */
813 cc_this_call = 0x00000008, /**< The first parameter is a this pointer and is transmitted
816 /* for easier access */
817 cc_cdecl_set = 0, /**< cdecl calling convention */
818 cc_stdcall_set = cc_callee_clear_stk, /**< stdcall calling convention */
819 cc_fastcall_set = cc_reg_param|cc_callee_clear_stk, /**< fastcall calling convention */
820 cc_defmask = cc_reg_param|cc_last_on_top|cc_callee_clear_stk
821 } calling_convention;
823 /** return the default calling convention for method types */
824 unsigned get_default_cc_mask(void);
827 * check for the CDECL calling convention
829 #define IS_CDECL(cc_mask) (((cc_mask) & cc_defmask) == cc_cdecl_set)
832 * check for the STDCALL calling convention
834 #define IS_STDCALL(cc_mask) (((cc_mask) & cc_defmask) == cc_stdcall_set)
837 * check for the FASTCALL calling convention
839 #define IS_FASTCALL(cc_mask) (((cc_mask) & cc_defmask) == cc_fastcall_set)
842 * add the CDECL convention bits
844 #define SET_CDECL(cc_mask) (((cc_mask) & ~cc_defmask) | cc_cdecl_set)
847 * add the STDCALL convention bits
849 #define SET_STDCALL(cc_mask) (((cc_mask) & ~cc_defmask) | cc_stdcall_set)
852 * add the FASTCALL convention bits
854 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_defmask) | cc_fastcall_set)
856 /** Returns the calling convention of an entities graph. */
857 unsigned get_method_calling_convention(const type *method);
859 /** Sets the calling convention of an entities graph. */
860 void set_method_calling_convention(type *method, unsigned cc_mask);
862 /** Returns the number of register parameters in a fastcall. 0 means default. */
863 unsigned get_method_fastcall_n_regs(type *method);
865 /** Sets the number of register parameters in a fastcall. 0 means default. */
866 void set_method_fastcall_n_regs(type *method, unsigned n_regs);
868 /** Returns true if a type is a method type. */
869 int is_Method_type (const type *method);
872 * @page union_type Representation of a union (variant) type.
874 * The union type represents union types.
875 * - n_types: Number of unioned types.
876 * - members: Entities for unioned types. Fixed length array.
877 * This is a dynamic list that can be grown with an "add_" function,
880 /** Creates a new type union. */
881 type *new_type_union (ident *name);
883 /** Creates a new type union with debug information. */
884 type *new_d_type_union (ident *name, dbg_info* db);
886 /* --- manipulate private fields of struct --- */
888 /** Returns the number of unioned types of this union */
889 int get_union_n_members (const type *uni);
891 /** Adds a new entity to a union type */
892 void add_union_member (type *uni, entity *member);
894 /** Returns the entity at position pos of a union */
895 entity *get_union_member (const type *uni, int pos);
897 /** Overwrites a entity at position pos in a union type. */
898 void set_union_member (type *uni, int pos, entity *member);
900 /** Finds member in the list of members and removes it. */
901 void remove_union_member (type *uni, entity *member);
903 /** Returns true if a type is a union type. */
904 int is_Union_type (const type *uni);
907 * @page array_type Representation of an array type
909 * The array type represents rectangular multi dimensional arrays.
910 * The constants representing the bounds must be allocated to
911 * get_const_code_irg() by setting current_ir_graph accordingly.
913 * - n_dimensions: Number of array dimensions.
914 * - *lower_bound: Lower bounds of dimensions. Usually all 0.
915 * - *upper_bound: Upper bounds or dimensions.
916 * - *element_type: The type of the array elements.
917 * - *element_ent: An entity for the array elements to be used for
918 * element selection with Sel.
920 * Do we need several entities? One might want
921 * to select a dimension and not a single element in case of multi
922 * dimensional arrays.
925 /** Create a new type array.
927 * Sets n_dimension to dimension and all dimension entries to NULL.
928 * Initializes order to the order of the dimensions.
929 * The entity for array elements is built automatically.
930 * Set dimension sizes after call to constructor with set_* routines.
932 type *new_type_array (ident *name, int n_dimensions,
935 /** Create a new type array with debug information.
937 * Sets n_dimension to dimension and all dimension entries to NULL.
938 * Initializes order to the order of the dimensions.
939 * The entity for array elements is built automatically.
940 * Set dimension sizes after call to constructor with set_* routines.
941 * A legal array type must have at least one dimension set.
943 type *new_d_type_array (ident *name, int n_dimensions,
944 type *element_type, dbg_info* db);
946 /* --- manipulate private fields of array type --- */
948 /** Returns the number of array dimensions of this type. */
949 int get_array_n_dimensions (const type *array);
952 * Allocates Const nodes of mode_I for one array dimension.
953 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
955 void set_array_bounds_int (type *array, int dimension, int lower_bound,
958 * Sets the bounds for one array dimension.
959 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
961 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
962 ir_node *upper_bound);
963 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
964 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
966 /** Allocates Const nodes of mode_I for the lower bound of an array
967 dimension, i.e. [lower,upper[ */
968 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
970 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
971 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
973 /** Allocates Const nodes of mode_I for the upper bound of an array
974 dimension, i.e. [lower,upper[. */
975 void set_array_upper_bound_int (type *array, int dimension, int upper_bound);
977 /** Returns true if lower bound != Unknown. */
978 int has_array_lower_bound (const type *array, int dimension);
979 /** Returns the lower bound of an array. */
980 ir_node * get_array_lower_bound (const type *array, int dimension);
981 /** Works only if bound is Const node with tarval that can be converted to long. */
982 long get_array_lower_bound_int (const type *array, int dimension);
983 /** returns true if lower bound != Unknown */
984 int has_array_upper_bound (const type *array, int dimension);
985 /** Returns the upper bound of an array. */
986 ir_node * get_array_upper_bound (const type *array, int dimension);
987 /** Works only if bound is Const node with tarval that can be converted to long. */
988 long get_array_upper_bound_int (const type *array, int dimension);
990 /** Sets an array dimension to a specific order. */
991 void set_array_order (type *array, int dimension, int order);
993 /** Returns the order of an array dimension. */
994 int get_array_order (const type *array, int dimension);
996 /** Find the array dimension that is placed at order ord. */
997 int find_array_dimension(const type *array, int order);
999 /** Sets the array element type. */
1000 void set_array_element_type (type *array, type *tp);
1002 /** Gets the array element type. */
1003 type *get_array_element_type (type *array);
1005 /** Sets the array element entity. */
1006 void set_array_element_entity (type *array, entity *ent);
1008 /** Get the array element entity. */
1009 entity *get_array_element_entity (const type *array);
1011 /** Returns true if a type is an array type. */
1012 int is_Array_type(const type *array);
1015 * @page enumeration_type Representation of an enumeration type
1017 * Enumeration types need not necessarily be represented explicitly
1018 * by Firm types, as the frontend can lower them to integer constants as
1019 * well. For debugging purposes or similar tasks this information is useful.
1021 * - *enum: The target values representing the constants used to
1022 * represent individual enumerations.
1023 * - *enum_nameid: Idents containing the source program name of the enumeration
1026 /** Create a new type enumeration -- set the enumerators independently. */
1027 type *new_type_enumeration (ident *name, int n_enums);
1029 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1030 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
1032 /* --- manipulate fields of enumeration type. --- */
1034 /** Returns the number of enumeration values of this enumeration */
1035 int get_enumeration_n_enums (const type *enumeration);
1037 /** Sets the enumeration value at a given position. */
1038 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
1040 /** Returns the enumeration value at a given position. */
1041 tarval *get_enumeration_enum (const type *enumeration, int pos);
1043 /** Assign an ident to an enumeration value at a given position. */
1044 void set_enumeration_nameid (type *enumeration, int pos, ident *id);
1046 /** Returns the assigned ident of an enumeration value at a given position. */
1047 ident *get_enumeration_nameid (const type *enumeration, int pos);
1049 /** Returns the assigned name of an enumeration value at a given position. */
1050 const char *get_enumeration_name(const type *enumeration, int pos);
1052 /** Returns true if a type is a enumeration type. */
1053 int is_Enumeration_type (const type *enumeration);
1056 * @page pointer_type Representation of a pointer type
1058 * The mode of the pointer type must be a reference mode.
1061 * - points_to: The type of the entity this pointer points to.
1064 /** Creates a new type pointer. */
1065 type *new_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode);
1067 /** Creates a new type pointer with debug information. */
1068 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db);
1070 /* --- manipulate fields of type_pointer --- */
1072 /** Sets the type to which a pointer points to. */
1073 void set_pointer_points_to_type (type *pointer, type *tp);
1075 /** Returns the type to which a pointer points to. */
1076 type *get_pointer_points_to_type (type *pointer);
1078 /** Returns true if a type is a pointer type. */
1079 int is_Pointer_type (const type *pointer);
1081 /** Returns the first pointer type that has as points_to tp.
1082 * Not efficient: O(#types).
1083 * If not found returns unknown_type. */
1084 type *find_pointer_type_to_type (type *tp);
1087 * @page primitive_type Representation of a primitive type
1089 * Primitive types are types that represent indivisible data values that
1090 * map directly to modes. They don't have a private attribute. The
1091 * important information they carry is held in the common mode field.
1093 /** Creates a new primitive type. */
1094 type *new_type_primitive (ident *name, ir_mode *mode);
1096 /** Creates a new primitive type with debug information. */
1097 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
1099 /** Returns true if a type is a primitive type. */
1100 int is_Primitive_type (const type *primitive);
1106 * This type is an auxiliary type dedicated to support type analyses.
1108 * The none type represents that there is no type. The type can be used to
1109 * initialize fields of type* that actually can not contain a type or that
1110 * are initialized for an analysis. There exists exactly one type none.
1111 * This type is not on the type list in ir_prog. It is
1112 * allocated when initializing the type module.
1114 * The following values are set:
1116 * - name: "type_none"
1117 * - state: layout_fixed
1120 /** A variable that contains the only none type. */
1121 extern type *firm_none_type;
1122 /** Returns the none type */
1123 type *get_none_type(void);
1126 * @page unknown_type
1128 * This type is an auxiliary type dedicated to support type analyses.
1130 * The unknown type represents that there could be a type, but it is not
1131 * known. This type can be used to initialize fields before an analysis (not known
1132 * yet) or to represent the top of a lattice (could not be determined). There exists
1133 * exactly one type unknown. This type is not on the type list in ir_prog. It is
1134 * allocated when initializing the type module.
1136 * The following values are set:
1138 * - name: "type_unknown"
1139 * - state: layout_fixed
1142 /** A variable that contains the only unknown type. */
1143 extern type *firm_unknown_type;
1144 /** Returns the unknown type */
1145 type *get_unknown_type(void);
1149 * Checks whether a type is atomic.
1150 * @param tp any type
1151 * @return true if type is primitive, pointer or enumeration
1153 int is_atomic_type(const type *tp);
1155 /* --- Support for compound types --- */
1158 * Gets the number of elements in a firm compound type.
1160 * This is just a comfortability function, because structs and
1161 * classes can often be treated be the same code, but they have
1162 * different access functions to their members.
1164 * @param tp The type (must be struct, union or class).
1166 * @return Number of members in the compound type.
1168 int get_compound_n_members(const type *tp);
1171 * Gets the member of a firm compound type at position pos.
1173 * @param tp The type (must be struct, union or class).
1174 * @param pos The number of the member.
1176 * @return The member entity at position pos.
1178 * @see get_compound_n_members() for justification of existence.
1180 entity *get_compound_member(const type *tp, int pos);
1183 * Checks whether a type is compound.
1185 * @param tp - any type
1187 * @return true if the type is class, structure, union or array type.
1189 int is_compound_type(const type *tp);
1192 * Checks, whether a type is a frame type
1194 int is_frame_type(const type *tp);
1197 * Makes a new frame type. Frame types are class types,
1198 * so all class access functions work.
1199 * Frame types are not in the global list of types.
1201 type *new_type_frame(ident *name);
1203 /*-----------------------------------------------------------------*/
1205 /*-----------------------------------------------------------------*/
1208 * Outputs a unique number for this type if libfirm is compiled for
1209 * debugging, (configure with --enable-debug) else returns the address
1210 * of the type cast to long.
1212 long get_type_nr(const type *tp);
1215 # endif /* _TYPE_H_ */