attribute is pointless here and should have been __attribute__
[libfirm] / include / libfirm / typerep.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19 #ifndef FIRM_TYPEREP_H
20 #define FIRM_TYPEREP_H
21
22 #include "firm_types.h"
23
24 /**
25  * @page entity       Entity representation
26  *
27  * An entity is the representation of program known objects in Firm.
28  * The primary concept of entities is to represent members of complex
29  * types, i.e., fields and methods of classes.  As not all programming
30  * language model all variables and methods as members of some class,
31  * the concept of entities is extended to cover also local and global
32  * variables, and arbitrary procedures.
33  *
34  * An entity always specifies the type of the object it represents and
35  * the type of the object it is a part of, the owner of the entity.
36  * Originally this is the type of the class of which the entity is a
37  * member.
38  * The owner of local variables is the procedure they are defined in.
39  * The owner of global variables and procedures visible in the whole
40  * program is a universally defined class type "GlobalType".  The owner
41  * of procedures defined in the scope of an other procedure is the
42  * enclosing procedure.
43  *
44  * The type ir_entity is an abstract data type to represent program entities.
45  * If contains the following attributes:
46  *
47  *   - owner:      A compound type this entity is a part of.
48  *   - type:       The type of this entity.
49  *   - name:       The string that represents this entity in the source program. *   - allocation: A flag saying whether the entity is dynamically or statically *                 allocated (values: dynamic_allocated,  static_allocated,
50  *                 automatic_allocated).
51  *   - visibility: A flag indicating the visibility of this entity (values: local,
52  *                 external_visible,  external_allocated)
53  *   - variability: A flag indicating the variability of this entity (values:
54  *                  uninitialized, initialized, part_constant, constant)
55  *   - volatility: @@@
56  *   - offset:     The offset of the entity within the compound object in bytes.  Only set
57  *                 if the owner in the state "layout_fixed".
58  *   - offset_bits_remainder:   The offset bit remainder of a bitfield entity (in a compound)
59  *                 in bits.  Only set if the owner in the state "layout_fixed".
60  *   - overwrites: A list of entities overwritten by this entity.  This list is only
61  *                 existent if the owner of this entity is a class.  The members in
62  *                 this list must be entities of super classes.
63  *   - overwrittenby: A list of entities that overwrite this entity.  This list is only
64  *                 existent if the owner of this entity is a class.  The members in
65  *                 this list must be entities of sub classes.
66  *   - link:       A void* to associate some additional information with the entity.
67  *   - irg:        If the entity is a method this is the ir graph that represents the
68  *                 code of the method.
69  *   - peculiarity: The peculiarity of the entity.  If the entity is a method this
70  *                 indicates whether the entity represents
71  *                 a real method or whether it only exists to describe an interface.
72  *                 In that case there nowhere exists code for this entity and this entity
73  *                 is never dynamically used in the code.
74  *                 Values: description, existent.  Default: existent.
75  *   - visited:    visited flag.  Master flag is type_visited.
76  *
77  * These fields can only be accessed via access functions.
78  *
79  * @see  ir_type, ir_entity
80  */
81
82 /** This enumeration flags the visibility of entities and types.
83  *
84  * This is necessary for partial compilation.
85  * We rely on the ordering of the flags.
86  */
87 typedef enum {
88   visibility_local,              /**< The entity is only visible locally.  This is the default for
89                                       entities.
90                                       The type is only visible locally.  All instances are allocated
91                                       locally, and no pointer to entities of this type are passed
92                                       out of this compilation unit. */
93   visibility_external_visible,   /**< The entity is visible to other external program parts, but
94                                       it is defined here.  It may not be optimized away.  The entity must
95                                       be static_allocated.
96                                       For types:  entities of this type can be accessed externally.  No
97                                       instances of this type are allocated externally.  */
98   visibility_external_allocated  /**< The entity is defined and allocated externally.  This compilation
99                                       must not allocate memory for this entity. The entity must
100                                       be static_allocated.  This can also be an external defined
101                                       method.
102                                       For types:  entities of this type are allocated and accessed from
103                                       external code.  Default for types.  */
104 } ir_visibility;
105
106 /** This enumeration flags the peculiarity of entities and types. */
107 typedef enum {
108   peculiarity_description,     /**< Represents only a description.  The entity/type is never
109                             allocated, no code/data exists for this entity/type.
110                         @@@ eventually rename to descriptive (adjective as the others!)*/
111   peculiarity_inherited,       /**< Describes explicitly that other entities are
112                             inherited to the owner of this entity.
113                             Overwrites must refer to at least one other
114                             entity.  If this is a method entity there exists
115                             no irg for this entity, only for one of the
116                             overwritten ones.
117                         Only for entity. */
118   peculiarity_existent         /**< The entity/type (can) exist.
119                     @@@ eventually rename to 'real' i.e., 'echt'
120                         This serves better as opposition to description _and_ inherited.*/
121 } ir_peculiarity;
122
123 /**
124  * Additional method type properties:
125  * Tell about special properties of a method type. Some
126  * of these may be discovered by analyses.
127  */
128 typedef enum {
129   mtp_no_property        = 0x00000000, /**< no additional properties, default */
130   mtp_property_const     = 0x00000001, /**< This method did not access memory and calculates
131                                          its return values solely from its parameters.
132                                          GCC: __attribute__((const)). */
133   mtp_property_pure      = 0x00000002, /**< This method did NOT write to memory and calculates
134                                          its return values solely from its parameters and
135                                          the memory they points to (or global vars).
136                                          GCC: __attribute__((pure)). */
137   mtp_property_noreturn  = 0x00000004, /**< This method did not return due to an aborting system
138                                          call.
139                                          GCC: __attribute__((noreturn)). */
140   mtp_property_nothrow   = 0x00000008, /**< This method cannot throw an exception.
141                                          GCC: __attribute__((nothrow)). */
142   mtp_property_naked     = 0x00000010, /**< This method is naked.
143                                          GCC: __attribute__((naked)). */
144   mtp_property_malloc    = 0x00000020, /**< This method returns newly allocate memory.
145                                          GCC: __attribute__((malloc)). */
146   mtp_property_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
147                                          a lowering phase will remove all calls to it. */
148   mtp_property_runtime   = 0x00000080, /**< This method represents a runtime routine. */
149   mtp_property_private   = 0x00000100, /**< All method invocations are known, the backend is free to
150                                                                                 optimize the call in any possible way. */
151   mtp_property_inherited = (1<<31)     /**< Internal. Used only in irg's, means property is
152                                          inherited from type. */
153 } mtp_additional_property;
154
155 /**
156  * Creates a new entity.
157  *
158  * Automatically inserts the entity as a member of owner.
159  * Entity is automatic_allocated and uninitialized except if the type
160  * is type_method, then it is static_allocated and constant.  The constant
161  * value is a pointer to the method.
162  * Visibility is local, offset -1, and it is not volatile.
163  */
164 ir_entity     *new_entity(ir_type *owner, ident *name, ir_type *tp);
165
166 /**
167  * Creates a new entity.
168  *
169  * Automatically inserts the entity as a member of owner.
170  * The entity is automatic allocated and uninitialized except if the type
171  * is type_method, then it is static allocated and constant.  The constant
172  * value is a pointer to the method.
173  * Visibility is local, offset -1, and it is not volatile.
174  */
175 ir_entity     *new_d_entity(ir_type *owner, ident *name, ir_type *tp, dbg_info *db);
176
177 /**
178  * Copies the entity if the new_owner is different from the
179  * owner of the old entity,  else returns the old entity.
180  *
181  * Automatically inserts the new entity as a member of owner.
182  * Resets the overwrites/overwritten_by fields.
183  * Keeps the old atomic value.
184  *   @@@ Maybe we should change this.  If peculiarity of a method
185  *       is existent, we should add a new SymConst that points to
186  *       itself and not to the origin.  Right now we have to change
187  *       the peculiarity and then set a new atomic value by hand.
188  */
189 ir_entity     *copy_entity_own(ir_entity *old, ir_type *new_owner);
190
191 /**
192  * Copies the entity if the new_name is different from the
193  * name of the old entity, else returns the old entity.
194  *
195  * Automatically inserts the new entity as a member of owner.
196  * The mangled name ld_name is set to NULL.
197  * Overwrites relation is copied from old.
198  */
199 ir_entity     *copy_entity_name(ir_entity *old, ident *new_name);
200
201 /**
202  * Frees the entity.
203  *
204  * The owner will still contain the pointer to this
205  * entity, as well as all other references!
206  */
207 void        free_entity(ir_entity *ent);
208
209 /** Returns the name of an entity. */
210 const char *get_entity_name(const ir_entity *ent);
211
212 /** Returns the ident of an entity. */
213 ident      *get_entity_ident(const ir_entity *ent);
214
215 /** Sets the ident of the entity. */
216 void        set_entity_ident(ir_entity *ent, ident *id);
217
218 /** Returns the mangled name of the entity.
219  *
220  * If the mangled name is set it returns the existing name.
221  * Else it generates a name with mangle_entity()
222  * and remembers this new name internally.
223  */
224 ident      *get_entity_ld_ident(ir_entity *ent);
225
226 /** Sets the mangled name of the entity. */
227 void        set_entity_ld_ident(ir_entity *ent, ident *ld_ident);
228
229 /** Returns the mangled name of the entity as a string. */
230 const char *get_entity_ld_name(ir_entity *ent);
231
232 /** Returns the owner of the entity. */
233 ir_type    *get_entity_owner(ir_entity *ent);
234
235 /** Sets the owner field in entity to owner.  Don't forget to add
236    ent to owner!! */
237 void        set_entity_owner(ir_entity *ent, ir_type *owner);
238
239 /** Returns the type of an entity. */
240 ir_type  *get_entity_type(ir_entity *ent);
241
242 /** Sets the type of an entity. */
243 void      set_entity_type(ir_entity *ent, ir_type *tp);
244
245 /** The allocation type. */
246 typedef enum {
247   allocation_automatic, /**< The entity is allocated during runtime, implicitly
248                              as component of a compound type.   This is the default. */
249   allocation_parameter, /**< The entity is a parameter.  It is also automatic allocated.
250                              We distinguish the allocation of parameters from the allocation
251                              of local variables as their placement depends on the calling
252                              conventions. */
253   allocation_dynamic,   /**< The entity is allocated during runtime, explicitly
254                              by an Alloc node. */
255   allocation_static     /**< The entity is allocated statically.  We can use a
256                              Const as address of the entity.  This is the default for methods. */
257 } ir_allocation;
258
259 /** Returns the allocation type of an entity. */
260 ir_allocation get_entity_allocation(const ir_entity *ent);
261
262 /** Sets the allocation type of an entity. */
263 void           set_entity_allocation(ir_entity *ent, ir_allocation al);
264
265 /** Return the name of the allocation type. */
266 const char *get_allocation_name(ir_allocation vis);
267
268 /** Returns the visibility of an entity. */
269 ir_visibility get_entity_visibility(const ir_entity *ent);
270
271 /** Sets the visibility of an entity. */
272 void       set_entity_visibility(ir_entity *ent, ir_visibility vis);
273
274 /** Return the name of the visibility */
275 const char *get_visibility_name(ir_visibility vis);
276
277 /** This enumeration flags the variability of entities. */
278 typedef enum {
279   variability_uninitialized,    /**< The content of the entity is completely unknown. Default. */
280   variability_initialized,      /**< After allocation the entity is initialized with the
281                                      value given somewhere in the entity. */
282   variability_part_constant,    /**< For entities of compound types.
283                                      The members of the entity are mixed constant,
284                                      initialized or uninitialized. */
285   variability_constant          /**< The entity is constant. */
286 } ir_variability;
287
288 /** Returns the variability of an entity. */
289 ir_variability get_entity_variability(const ir_entity *ent);
290
291 /** Sets the variability of an entity. */
292 void           set_entity_variability(ir_entity *ent, ir_variability var);
293
294 /** Return the name of the variability. */
295 const char *get_variability_name(ir_variability var);
296
297 /** This enumeration flags the volatility of entities. */
298 typedef enum {
299   volatility_non_volatile,    /**< The entity is not volatile. Default. */
300   volatility_is_volatile      /**< The entity is volatile */
301 } ir_volatility;
302
303 /** Returns the volatility of an entity. */
304 ir_volatility get_entity_volatility(const ir_entity *ent);
305
306 /** Sets the volatility of an entity. */
307 void          set_entity_volatility(ir_entity *ent, ir_volatility vol);
308
309 /** Return the name of the volatility. */
310 const char *get_volatility_name(ir_volatility var);
311
312 /** This enumeration flags the stickyness of an entity. */
313 typedef enum {
314   stickyness_unsticky,          /**< The entity can be removed from
315                                    the program, unless contraindicated
316                                    by other attributes. Default. */
317   stickyness_sticky             /**< The entity must remain in the
318                                    program in any case. */
319 } ir_stickyness;
320
321 /** Get the entity's stickyness. */
322 ir_stickyness get_entity_stickyness(const ir_entity *ent);
323
324 /** Set the entity's stickyness. */
325 void          set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness);
326
327 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
328 int       get_entity_offset(const ir_entity *ent);
329
330 /** Sets the offset of an entity (in a compound) in bytes. */
331 void      set_entity_offset(ir_entity *ent, int offset);
332
333 /** Returns the offset bit remainder of a bitfield entity (in a compound) in bits. Only set if layout = fixed. */
334 unsigned char get_entity_offset_bits_remainder(const ir_entity *ent);
335
336 /** Sets the offset bit remainder of a bitfield entity (in a compound) in bits. */
337 void      set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset);
338
339 /** Returns the stored intermediate information. */
340 void *get_entity_link(const ir_entity *ent);
341
342 /** Stores new intermediate information. */
343 void set_entity_link(ir_entity *ent, void *l);
344
345 /* -- Fields of method entities -- */
346 /** The entity knows the corresponding irg if the entity is a method.
347    This allows to get from a Call to the called irg.
348    Only entities of peculiarity "existent" can have a corresponding irg,
349    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
350 ir_graph *get_entity_irg(const ir_entity *ent);
351 void      set_entity_irg(ir_entity *ent, ir_graph *irg);
352
353 /** Gets the entity vtable number. */
354 unsigned get_entity_vtable_number(const ir_entity *ent);
355
356 /** Sets the entity vtable number. */
357 void     set_entity_vtable_number(ir_entity *ent, unsigned vtable_number);
358
359 /** Return the peculiarity of an entity. */
360 ir_peculiarity get_entity_peculiarity(const ir_entity *ent);
361
362 /** Sets the peculiarity of an entity. */
363 void           set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec);
364
365 /** Checks if an entity cannot be overridden anymore. */
366 int is_entity_final(const ir_entity *ent);
367
368 /** Sets/resets the final flag of an entity. */
369 void set_entity_final(ir_entity *ent, int final);
370
371 /** Checks if an entity is compiler generated. */
372 int is_entity_compiler_generated(const ir_entity *ent);
373
374 /** Sets/resets the compiler generated flag. */
375 void set_entity_compiler_generated(ir_entity *ent, int flag);
376
377 /**
378  * The state of the address_taken flag.
379  */
380 typedef enum {
381         ir_address_not_taken     = 0,  /**< The address is NOT taken. */
382         ir_address_taken_unknown = 1,  /**< The state of the address taken flag is unknown. */
383         ir_address_taken         = 2   /**< The address IS taken. */
384 } ir_address_taken_state;
385
386 /** Return the state of the address taken flag of an entity. */
387 ir_address_taken_state get_entity_address_taken(const ir_entity *ent);
388
389 /** Sets/resets the state of the address taken flag of an entity. */
390 void set_entity_address_taken(ir_entity *ent, ir_address_taken_state flag);
391
392 /** Return the name of the address_taken state. */
393 const char *get_address_taken_state_name(ir_address_taken_state state);
394
395 /* -- Representation of constant values of entities -- */
396 /**
397  * Returns true if the the node is representable as code on
398  * const_code_irg.
399  *
400  * @deprecated This function is not used by libFirm and stays here
401  *             only as a helper for the old Jack frontend.
402  */
403 int      is_irn_const_expression(ir_node *n);
404
405 /**
406  * Copies a Firm subgraph that complies to the restrictions for
407  * constant expressions to current_block in current_ir_graph.
408  *
409  * @param dbg  debug info for all newly created nodes
410  * @param n    the node
411  *
412  * Set current_ir_graph to get_const_code_irg() to generate a constant
413  * expression.
414  */
415 ir_node *copy_const_value(dbg_info *dbg, ir_node *n);
416
417 /* Set has no effect for existent entities of type method. */
418 ir_node *get_atomic_ent_value(ir_entity *ent);
419 void     set_atomic_ent_value(ir_entity *ent, ir_node *val);
420
421 /** Creates a new compound graph path. */
422 compound_graph_path *new_compound_graph_path(ir_type *tp, int length);
423
424 /** Returns non-zero if an object is a compound graph path */
425 int     is_compound_graph_path(const void *thing);
426
427 /** Frees a graph path object */
428 void    free_compound_graph_path (compound_graph_path *gr);
429
430 /** Returns the length of a graph path */
431 int     get_compound_graph_path_length(const compound_graph_path *gr);
432
433 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr, int pos);
434 void    set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node);
435 int     get_compound_graph_path_array_index(const compound_graph_path *gr, int pos);
436 void    set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
437
438 /** Checks whether the path up to pos is correct. If the path contains a NULL,
439  *  assumes the path is not complete and returns non-zero. */
440 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
441
442 /* A value of a compound entity is a pair of a value and the description of the
443    corresponding access path to the member of the compound.  */
444 void     add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path);
445 void     set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos);
446 /** Returns the number of constant values needed to initialize the entity.
447  *
448  *  Asserts if the entity has variability_uninitialized.
449  * */
450 int      get_compound_ent_n_values(ir_entity *ent);
451 /** Returns a constant value given the position. */
452 ir_node *get_compound_ent_value(ir_entity *ent, int pos);
453 /** Returns the access path for value at position pos. */
454 compound_graph_path *get_compound_ent_value_path(ir_entity *ent, int pos);
455 /** Returns a constant value given the access path.
456  *  The path must contain array indices for all array element entities. */
457 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path);
458
459 /** Removes all constant entries where the path ends at value_ent. Does not
460    free the memory of the paths.  (The same path might be used for several
461    constant entities. */
462 void     remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent);
463
464 /* Some languages support only trivial access paths, i.e., the member is a
465    direct, atomic member of the constant entities type. In this case the
466    corresponding entity can be accessed directly.  The following functions
467    allow direct access. */
468
469 /** Generates a Path with length 1.
470     Beware: Has a bad runtime for array elements (O(|array|) and should be
471     avoided there. Use add_compound_ent_value_w_path() instead and create
472     the path manually. */
473 void     add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member);
474
475 /** Returns the last member in the path */
476 ir_entity  *get_compound_ent_value_member(ir_entity *ent, int pos);
477
478 /** Sets the path at pos 0 */
479 void     set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos);
480
481 /** Initializes the entity ent which must be of a one dimensional
482    array type with the values given in the values array.
483    The array must have a lower and an upper bound.  Keeps the
484    order of values. Does not test whether the number of values
485    fits into the given array size.  Does not test whether the
486    values have the proper mode for the array. */
487 void set_array_entity_values(ir_entity *ent, tarval **values, int num_vals);
488
489 /**
490  * Return the offset in bits from the last byte address.
491  *
492  * This requires that the layout of all concerned types is fixed.
493  *
494  * @param ent Any entity of compound type with at least pos initialization values.
495  * @param pos The position of the value for which the offset is requested.
496  */
497 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos);
498
499 /** Return the overall offset of value at position pos in bytes.
500  *
501  * This requires that the layout of all concerned types is fixed.
502  * Asserts if bit offset is not byte aligned.
503  *
504  * @param ent Any entity of compound type with at least pos initialization values.
505  * @param pos The position of the value for which the offset is requested.
506  */
507 int  get_compound_ent_value_offset_bytes(ir_entity *ent, int pos);
508
509 /* --- Fields of entities with a class type as owner --- */
510 /* Overwrites is a field that specifies that an access to the overwritten
511    entity in the supertype must use this entity.  It's a list as with
512    multiple inheritance several entities can be overwritten.  This field
513    is mostly useful for method entities.
514    If a Sel node selects an entity that is overwritten by other entities it
515    must return a pointer to the entity of the dynamic type of the pointer
516    that is passed to it.  Lowering of the Sel node must assure this.
517    Overwrittenby is the inverse of overwrites.  Both add routines add
518    both relations, they only differ in the order of arguments. */
519 void    add_entity_overwrites   (ir_entity *ent, ir_entity *overwritten);
520 int     get_entity_n_overwrites (ir_entity *ent);
521 int     get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten);
522 ir_entity *get_entity_overwrites   (ir_entity *ent, int pos);
523 void    set_entity_overwrites   (ir_entity *ent, int pos, ir_entity *overwritten);
524 void    remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten);
525
526 void    add_entity_overwrittenby   (ir_entity *ent, ir_entity *overwrites);
527 int     get_entity_n_overwrittenby (ir_entity *ent);
528 int     get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites);
529 ir_entity *get_entity_overwrittenby   (ir_entity *ent, int pos);
530 void    set_entity_overwrittenby   (ir_entity *ent, int pos, ir_entity *overwrites);
531 void    remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites);
532
533 /**
534  *   Checks whether a pointer points to an entity.
535  *
536  *   @param thing     an arbitrary pointer
537  *
538  *   @return
539  *       true if the thing is an entity, else false
540  */
541 int is_entity (const void *thing);
542
543 /** Returns true if the type of the entity is a primitive, pointer
544  * enumeration or method type.
545  *
546  * @Note This is a different classification than from is_primitive_type().
547  */
548 int is_atomic_entity(ir_entity *ent);
549 /** Returns true if the type of the entity is a class, structure,
550    array or union type. */
551 int is_compound_entity(ir_entity *ent);
552 /** Returns true if the type of the entity is a Method type. */
553 int is_method_entity(ir_entity *ent);
554
555 /** Returns non-zero if ent1 and ent2 have are equal except for their owner.
556    Two entities are equal if
557     - they have the same type (the same C-struct)
558     - ...?
559 */
560 int equal_entity(ir_entity *ent1, ir_entity *ent2);
561
562 /** Outputs a unique number for this entity if libfirm is compiled for
563  *  debugging, (configure with --enable-debug) else returns the address
564  *  of the type cast to long.
565  */
566 long get_entity_nr(const ir_entity *ent);
567
568 /** Returns the entities visited count. */
569 unsigned long get_entity_visited(ir_entity *ent);
570
571 /** Sets the entities visited count. */
572 void        set_entity_visited(ir_entity *ent, unsigned long num);
573
574 /** Sets visited field in entity to entity_visited. */
575 void        mark_entity_visited(ir_entity *ent);
576
577 /** Returns true if this entity was visited. */
578 int        entity_visited(ir_entity *ent);
579
580 /** Returns true if this entity was not visited. */
581 int        entity_not_visited(ir_entity *ent);
582
583 /**
584  * Returns the mask of the additional entity properties.
585  * The properties are automatically inherited from the irg if available
586  * or from the method type if they were not set using
587  * set_entity_additional_properties() or
588  * set_entity_additional_property().
589  */
590 unsigned get_entity_additional_properties(ir_entity *ent);
591
592 /** Sets the mask of the additional graph properties. */
593 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask);
594
595 /** Sets one additional graph property. */
596 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag);
597
598 /** Returns the class type that this type info entity represents or NULL
599     if ent is no type info entity. */
600 ir_type *get_entity_repr_class(const ir_entity *ent);
601
602 /**
603  * @page unknown_entity  The Unknown entity
604  *
605  *  This entity is an auxiliary entity dedicated to support analyses.
606  *
607  *  The unknown entity represents that there could be an entity, but it is not
608  *  known.  This entity can be used to initialize fields before an analysis (not known
609  *  yet) or to represent the top of a lattice (could not be determined).  There exists
610  *  exactly one entity unknown. This entity has as owner and as type the unknown type. It is
611  *  allocated when initializing the entity module.
612  *
613  *  The entity can take the role of any entity, also methods.  It returns default
614  *  values in these cases.
615  *
616  *  The following values are set:
617  *
618  * - name          = "unknown_entity"
619  * - ld_name       = "unknown_entity"
620  * - owner         = unknown_type
621  * - type          = unknown_type
622  * - allocation    = allocation_automatic
623  * - visibility    = visibility_external_allocated
624  * - offset        = -1
625  * - variability   = variability_uninitialized
626  * - value         = SymConst(unknown_entity)
627  * - values        = NULL
628  * - val_paths     = NULL
629  * - peculiarity   = peculiarity_existent
630  * - volatility    = volatility_non_volatile
631  * - stickyness    = stickyness_unsticky
632  * - ld_name       = NULL
633  * - overwrites    = NULL
634  * - overwrittenby = NULL
635  * - irg           = NULL
636  * - link          = NULL
637  */
638 /* A variable that contains the only unknown entity. */
639 extern ir_entity *unknown_entity;
640
641 /** Returns the @link unknown_entity unknown entity @endlink. */
642 ir_entity *get_unknown_entity(void);
643
644 /** Encodes how a pointer parameter is accessed. */
645 typedef enum acc_bits {
646   ptr_access_none  = 0,                                 /**< no access */
647   ptr_access_read  = 1,                                 /**< read access */
648   ptr_access_write = 2,                                 /**< write access */
649   ptr_access_rw    = ptr_access_read|ptr_access_write,  /**< read AND write access */
650   ptr_access_store = 4,                                 /**< the pointer is stored */
651   ptr_access_all   = ptr_access_rw|ptr_access_store     /**< all possible access */
652 } ptr_access_kind;
653
654 #define IS_READ(a)     ((a) & ptr_access_read)
655 #define IS_WRITTEN(a)  ((a) & ptr_access_write)
656 #define IS_STORED(a)   ((a) & ptr_access_store)
657
658 /**
659  * Supported image sections.
660  * Currently only methods can be placed in different sections.
661  */
662 typedef enum {
663   section_text,           /**< The code segment. This is the default for methods. */
664   section_constructors    /**< The constructor section. */
665 } ir_img_section;
666
667 /** Returns the section of a method. */
668 ir_img_section get_method_img_section(const ir_entity *method);
669
670 /** Sets the section of a method. */
671 void set_method_img_section(ir_entity *method, ir_img_section section);
672
673
674
675
676 /**
677  * @page tyop  type operations
678  *  This module specifies the kinds of types available in firm.
679  *
680  *  They are called type opcodes. These include classes, structs, methods, unions,
681  *  arrays, enumerations, pointers and primitive types.
682  *  Special types with own opcodes are the id type, a type representing an unknown
683  *  type and a type used to specify that something has no type.
684  *
685  *  @see type.h
686  */
687
688 /**
689  *  An enum for the type kinds.
690  *  For each type kind exists a typecode to identify it.
691  */
692 typedef enum {
693         tpo_uninitialized = 0,   /* not a type opcode */
694         tpo_class,               /**< A class type. */
695         tpo_struct,              /**< A struct type. */
696         tpo_method,              /**< A method type. */
697         tpo_union,               /**< An union type. */
698         tpo_array,               /**< An array type. */
699         tpo_enumeration,         /**< An enumeration type. */
700         tpo_pointer,             /**< A pointer type. */
701         tpo_primitive,           /**< A primitive type. */
702         tpo_id,                  /**< Special Id tag used for type replacement. */
703         tpo_none,                /**< Special type for the None type. */
704         tpo_unknown,             /**< Special code for the Unknown type. */
705         tpo_max                  /* not a type opcode */
706 } tp_opcode;
707
708 /**
709  * A structure containing information about a kind of type.
710  * A structure containing information about a kind of type.  So far
711  * this is only the kind name, an enum for case-switching and some
712  * internal values.
713  *
714  * @see  get_tpop_name(), get_tpop_code(), get_tpop_ident()
715  */
716 typedef struct tp_op tp_op;
717
718
719 /**
720  * Returns the string for the type opcode.
721  *
722  * @param op  The type opcode to get the string from.
723  * @return a string.  (@todo Null terminated???)
724  */
725 const char *get_tpop_name (const tp_op *op);
726
727 /**
728  * Returns an enum for the type opcode.
729  *
730  * @param op   The type opcode to get the enum from.
731  * @return the enum.
732  */
733 tp_opcode get_tpop_code (const tp_op *op);
734
735 /**
736  * Returns the ident for the type opcode.
737  *
738  * @param op   The type opcode to get the ident from.
739  * @return The ident.
740  */
741 ident *get_tpop_ident (const tp_op *op);
742
743 /**
744  * This type opcode marks that the corresponding type is a class type.
745  *
746  * Consequently the type refers to supertypes, subtypes and entities.
747  * Entities can be any fields, but also methods.
748  * @@@ value class or not???
749  * This struct is dynamically allocated but constant for the lifetime
750  * of the library.
751  */
752 extern tp_op *type_class;
753 tp_op *get_tpop_class(void);
754
755 /**
756  * This type opcode marks that the corresponding type is a compound type
757  * as a struct in C.
758  *
759  * Consequently the type refers to a list of entities
760  * which may not be methods (but pointers to methods).
761  * This struct is dynamically allocated but constant for the lifetime
762  * of the library.
763  */
764 extern tp_op *type_struct;
765 tp_op *get_tpop_struct(void);
766
767 /**
768  * This type opcode marks that the corresponding type is a method type.
769  *
770  * Consequently it refers to a list of arguments and results.
771  * This struct is dynamically allocated but constant for the lifetime
772  * of the library.
773  */
774 extern tp_op *type_method;
775 tp_op *get_tpop_method(void);
776
777 /**
778  * This type opcode marks that the corresponding type is a union type.
779  *
780  * Consequently it refers to a list of unioned types.
781  * This struct is dynamically allocated but constant for the lifetime
782  * of the library.
783  */
784 extern tp_op *type_union;
785 tp_op *get_tpop_union(void);
786
787 /**
788  * This type opcode marks that the corresponding type is an array type.
789  *
790  * Consequently it contains a list of dimensions (lower and upper bounds)
791  * and an element type.
792  * This struct is dynamically allocated but constant for the lifetime
793  * of the library.
794  */
795 extern tp_op *type_array;
796 tp_op *get_tpop_array(void);
797
798 /**
799  * This type opcode marks that the corresponding type is an enumeration type.
800  *
801  * Consequently it contains a list of idents for the enumeration identifiers
802  * and a list of target values that are the constants used to implement
803  * the enumerators.
804  * This struct is dynamically allocated but constant for the lifetime
805  * of the library.
806  */
807 extern tp_op *type_enumeration;
808 tp_op *get_tpop_enumeration(void);
809
810 /**
811  * This type opcode marks that the corresponding type is a pointer type.
812  *
813  * It contains a reference to the type the pointer points to.
814  * This struct is dynamically allocated but constant for the lifetime
815  * of the library.
816  */
817 extern tp_op *type_pointer;
818 tp_op *get_tpop_pointer(void);
819
820 /**
821  * This type opcode marks that the corresponding type is a primitive type.
822  *
823  * Primitive types are types that are directly mapped to target machine
824  * modes.
825  * This struct is dynamically allocated but constant for the lifetime
826  * of the library.
827  */
828 extern tp_op *type_primitive;
829 tp_op *get_tpop_primitive(void);
830
831 /**
832  * This type opcode is an auxiliary opcode dedicated to support transformations
833  * of the type structure.
834  *
835  * If a type is changed to another type with another
836  * opcode the new type will be allocated with new memory.  All nodes refering
837  * to the old type need to be changed to refer to the new one.  This is simplified
838  * by turning the old type into an id type that merely forwards to the new type
839  * that now replaces the old one.
840  * type_ids should never be visible out of the type module.  All access routines
841  * should automatically check for type_id and eventually follow the forward in
842  * type_id.  Two types are exchanged by a call to exchange_types.
843  * If a type_id is visible externally report this as bug.  If it is assured that
844  * this never happens this extern variable can be moved to tpop_t.h.
845  * This struct is dynamically allocated but constant for the lifetime
846  * of the library.
847  */
848 extern tp_op *type_id;
849 tp_op *get_tpop_id(void);
850
851 /**
852  * This type opcode is an auxiliary opcode dedicated to support type analyses.
853  *
854  * Types with this opcode represents that there is no type.
855  * The type can be used to initialize fields of the type* that actually can not
856  * contain a type or that are initialized for an analysis. There exists exactly
857  * one type with this opcode.
858  */
859 extern tp_op *tpop_none;
860 tp_op *get_tpop_none(void);
861
862 /**
863  * This type opcode is an auxiliary opcode dedicated to support type analyses.
864  *
865  * Types with this opcode represents that there could be a type, but it is not
866  * known.  This type can be used to initialize fields before an analysis (not known
867  * yet) or to represent the top of a lattice (could not be determined).  There exists
868  * exactly one type with this opcode.
869  */
870 extern tp_op *tpop_unknown;
871 tp_op *get_tpop_unknown(void);
872
873 /* ----------------------------------------------------------------------- */
874 /* Classify pairs of types/entities in the inheritance relations.          */
875 /* ----------------------------------------------------------------------- */
876
877 /** Returns true if low is subclass of high.
878  *
879  *  Low is a subclass of high if low == high or if low is a subclass of
880  *  a subclass of high.  I.e, we search in all subtypes of high for low.
881  *  @@@ this can be implemented more efficient if we know the set of all
882  *  subclasses of high.  */
883 int is_SubClass_of(ir_type *low, ir_type *high);
884
885 /** Subclass check for pointers to classes.
886  *
887  *  Dereferences at both types the same amount of pointer types (as
888  *  many as possible).  If the remaining types are both class types
889  *  and subclasses, returns true, else false.  Can also be called with
890  *  two class types.  */
891 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
892
893 /** Returns true if high is superclass of low.
894  *
895  *  Low is a subclass of high if low == high or if low is a subclass of
896  *  a subclass of high.  I.e, we search in all subtypes of high for low.
897  *  @@@ this can be implemented more efficient if we know the set of all
898  *  subclasses of high.  */
899 #define is_SuperClass_of(high, low) is_SubClass_of(low, high)
900
901 /** Superclass check for pointers to classes.
902  *
903  *  Dereferences at both types the same amount of pointer types (as
904  *  many as possible).  If the remaining types are both class types
905  *  and superclasses, returns true, else false.  Can also be called with
906  *  two class types.  */
907 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
908
909 /** Returns true if high is (transitive) overwritten by low.
910  *
911  *  Returns false if high == low. */
912 int is_overwritten_by(ir_entity *high, ir_entity *low);
913
914 /** Resolve polymorphism in the inheritance relation.
915  *
916  *  Returns the dynamically referenced entity if the static entity and the
917  *  dynamic type are given.
918  *  Searches downwards in overwritten tree. */
919 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity* static_ent);
920
921 /* ----------------------------------------------------------------------- */
922 /* Resolve implicit inheritance.                                           */
923 /* ----------------------------------------------------------------------- */
924
925 /** Default name mangling for inherited entities.
926  *
927  *  Returns an ident that consists of the name of type followed by an
928  *  underscore and the name (not ld_name) of the entity. */
929 ident *default_mangle_inherited_name(ir_entity *ent, ir_type *clss);
930
931 /** Type of argument functions for inheritance resolver.
932  *
933  * @param ent     The entity in the super type that will be overwritten
934  *                by the newly generated entity, for which this name is
935  *                used.
936  * @param clss    The class type in which the new entity will be placed.
937  */
938 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
939
940 /** Resolve implicit inheritance.
941  *
942  *  Resolves the implicit inheritance supplied by firm.  Firm defines,
943  *  that each entity that is not overwritten in a subclass is
944  *  inherited to this subclass without change implicitly.  This
945  *  function generates entities that explicitly represent this
946  *  inheritance.  It generates for each entity overwriting entities in
947  *  all subclasses of the owner of the entity, if the entity is not
948  *  overwritten in that subclass.
949  *
950  *  The name of the new entity is generated with the function passed.
951  *  If the function is NULL, the default_mangle_inherited_name() is
952  *  used.
953  *
954  *  This function was moved here from firmlower 3/2005.
955  */
956 void resolve_inheritance(mangle_inherited_name_func *mfunc);
957
958
959 /* ----------------------------------------------------------------------- */
960 /* The transitive closure of the subclass/superclass and                   */
961 /* overwrites/overwrittenby relation.                                      */
962 /*                                                                         */
963 /* A walk over the ir (O(#types+#entities)) computes the transitive        */
964 /* closure.  Adding a new type/entity or changing the basic relations in   */
965 /* some other way invalidates the transitive closure, i.e., it is not      */
966 /* updated by the basic functions.                                         */
967 /*                                                                         */
968 /* The transitive edges are held in a set, not in an array as the          */
969 /* underlying relation.                                                    */
970 /*                                                                         */
971 /* Do the sets contain the node itself?  I assume NOT!                     */
972 /* ----------------------------------------------------------------------- */
973
974 /** The state of the transitive closure.
975  *
976  *  @todo: we could manage the state for each relation separately.  Invalidating
977  *  the entity relations does not mean invalidating the class relation. */
978 typedef enum {
979         inh_transitive_closure_none,       /**<  Closure is not computed, can not be accessed. */
980         inh_transitive_closure_valid,      /**<  Closure computed and valid. */
981         inh_transitive_closure_invalid,    /**<  Closure invalid, but can be accessed. */
982         inh_transitive_closure_max         /**<  Invalid value. */
983 } inh_transitive_closure_state;
984
985 void                         set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
986 void                         invalidate_irp_inh_transitive_closure_state(void);
987 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
988
989
990 /** Compute transitive closure of the subclass/superclass and
991  * overwrites/overwrittenby relation.
992  *
993  * This function walks over the ir (O(#types+#entities)) to compute the
994  * transitive closure.    */
995 void compute_inh_transitive_closure(void);
996
997 /** Free memory occupied by the transitive closure information. */
998 void free_inh_transitive_closure(void);
999
1000
1001 /* - subtype ------------------------------------------------------------- */
1002
1003 /** Iterate over all transitive subtypes. */
1004 ir_type *get_class_trans_subtype_first(ir_type *tp);
1005 ir_type *get_class_trans_subtype_next (ir_type *tp);
1006 int   is_class_trans_subtype (ir_type *tp, ir_type *subtp);
1007
1008 /* - supertype ----------------------------------------------------------- */
1009
1010 /** Iterate over all transitive supertypes. */
1011 ir_type *get_class_trans_supertype_first(ir_type *tp);
1012 ir_type *get_class_trans_supertype_next (ir_type *tp);
1013
1014 /* - overwrittenby ------------------------------------------------------- */
1015
1016 /** Iterate over all entities that transitive overwrite this entities. */
1017 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent);
1018 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent);
1019
1020 /* - overwrites ---------------------------------------------------------- */
1021
1022 /** Iterate over all transitive overwritten entities. */
1023 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent);
1024 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent);
1025
1026
1027 /* ----------------------------------------------------------------------- */
1028 /** The state of Cast operations that cast class types or pointers to class
1029  *  types.
1030  *
1031  * The state expresses, how far Cast operations conform with the class
1032  * hierarchy.
1033  *
1034  *   class A {}
1035  *   class B1 extends A {}
1036  *   class B2 extends A {}
1037  *   class C  extends B1 {}
1038  * normalized:  Cast operations conform with the inheritance relation.
1039  *   I.e., the type of the operand of a Cast is either a super= or a sub-
1040  *   type of the type casted to. Example: (A)((B2) (new C())).
1041  * transitive:  Cast operations conform with the transitive inheritance
1042  *   relation. Example: (A)(new C()).
1043  * any:  Cast operations do not conform with the transitive inheritance
1044  *   relation.  Example: (B2)(new B1())
1045  *
1046  *  @see: tropt.h
1047  */
1048 /* ----------------------------------------------------------------------- */
1049
1050 /** Flags for class cast state.
1051  *
1052  * The state in irp is always smaller or equal to the state of any
1053  * irg.
1054  *
1055  * We rely on the ordering of the enum. */
1056 typedef enum {
1057         ir_class_casts_any        = 0, /**< There are class casts that do not cast in conformance with
1058                                             the class hierarchy.  @@@ So far this does not happen in Firm. */
1059         ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
1060         ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
1061         ir_class_casts_state_max
1062 } ir_class_cast_state;
1063 char *get_class_cast_state_string(ir_class_cast_state s);
1064
1065 void                set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
1066 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
1067 void                set_irp_class_cast_state(ir_class_cast_state s);
1068 ir_class_cast_state get_irp_class_cast_state(void);
1069
1070 /** Verify the class cast state of an irg.
1071  *
1072  *  Asserts if state is to high, outputs warning if state is to low
1073  *  and firm verbosity is set.
1074  */
1075 void verify_irg_class_cast_state(ir_graph *irg);
1076
1077 /**
1078  * possible trvrfy() error codes
1079  */
1080 enum trvrfy_error_codes {
1081         no_error = 0,                      /**< no error */
1082         error_ent_not_cont,                /**< overwritten entity not in superclass */
1083         error_null_mem,                    /**< compound contains NULL member */
1084         error_const_on_wrong_irg,          /**< constant placed on wrong IRG */
1085         error_existent_entity_without_irg, /**< Method entities with pecularity_exist must have an irg */
1086         error_wrong_ent_overwrites,        /**< number of entity overwrites exceeds number of class overwrites */
1087         error_inherited_ent_without_const, /**< inherited method entity not pointing to existent entity */
1088         error_glob_ent_allocation,         /**< wrong allocation of a global entity */
1089         error_ent_const_mode,              /**< Mode of constant in entity did not match entities type. */
1090         error_ent_wrong_owner              /**< Mode of constant in entity did not match entities type. */
1091 };
1092
1093 /**
1094  * Checks a type.
1095  *
1096  * @return
1097  *  0   if no error encountered
1098  */
1099 int check_type(ir_type *tp);
1100
1101 /**
1102  * Check an entity. Currently, we check only if initialized constants
1103  * are build on the const irg graph.
1104  *
1105  * @return
1106  *  0   if no error encountered
1107  *  != 0    a trvrfy_error_codes code
1108  */
1109 int check_entity(ir_entity *ent);
1110
1111 /**
1112  * Walks the type information and performs a set of sanity checks.
1113  *
1114  * Currently, the following checks are executed:
1115  * - values of initialized entities must be allocated on the constant IRG
1116  * - class types: doesn't have NULL members
1117  * - class types: all overwrites are existent in the super type
1118  *
1119  * @return
1120  *    0 if graph is correct
1121  *    else error code.
1122  */
1123 int tr_vrfy(void);
1124
1125 /**
1126  * If NDEBUG is defined performs nothing, else calls the tr_vrfy() function.
1127  */
1128 #ifdef NDEBUG
1129 #define TR_VRFY()       0
1130 #else
1131 #define TR_VRFY()       tr_vrfy()
1132 #endif
1133
1134 /**
1135  *
1136  * @file typegmod.h
1137  *  This module supplies routines that support changing the type graph.
1138  */
1139
1140 /** Replaces one type by the other.
1141  *
1142  *  Old type is replaced by new_type.  All references to old_type
1143  *  now point to new_type.  The memory for the old type is destroyed,
1144  *  but still used.  Therefore it is not freed.
1145  *  All referenced to this memory will be lost after a certain while.
1146  *  An exception is the list of types in irp (irprog.h).
1147  *  In the future there might be a routine to recover the memory, but
1148  *  this will be at considerable runtime cost.
1149  *
1150  *  @param old_type  - The old type that shall be replaced by the new type.
1151  *  @param new_type  - The new type that will replace old_type.
1152  *
1153  */
1154 void exchange_types(ir_type *old_type, ir_type *new_type);
1155
1156 /** Skip id types until a useful type is reached.
1157  *
1158  *  @param tp - A type of arbitrary kind.
1159  *
1160  *  @return
1161  *    tp if it is not an id type.
1162  *    If tp is an id type returns the real type it stands for.
1163  */
1164 ir_type *skip_tid(ir_type *tp);
1165
1166 /**
1167  * @page type   representation of types
1168  *
1169  *  Datastructure to hold type information.
1170  *
1171  *  This module supplies a datastructure to represent all types
1172  *  known in the compiled program.  This includes types specified
1173  *  in the program as well as types defined by the language.  In the
1174  *  view of the intermediate representation there is no difference
1175  *  between these types.  Finally it specifies some auxiliary types.
1176  *
1177  *  There exist several kinds of types, arranged by the structure of
1178  *  the type.  A type is described by a set of attributes.  Some of
1179  *  these attributes are common to all types, others depend on the
1180  *  kind of the type.
1181  *
1182  *  Types are different from the modes defined in irmode:  Types are
1183  *  on the level of the programming language, modes at the level of
1184  *  the target processor.
1185  *
1186  *  @see  tpop.h
1187  */
1188
1189 #include "typerep.h"
1190
1191 /** Frees all entities associated with a type.
1192  *  Does not free the array entity.
1193  *  Warning: ensure these entities are not referenced anywhere else.
1194  */
1195 void        free_type_entities(ir_type *tp);
1196
1197 /** Frees the memory used by the type.
1198  *
1199  * Removes the type from the type list. Does not free the entities
1200  * belonging to the type, except for the array element entity.  Does
1201  * not free if tp is "none" or "unknown".  Frees entities in value
1202  * param subtypes of method types!!! Make sure these are not
1203  * referenced any more.  Further make sure there is no pointer type
1204  * that refers to this type.                           */
1205 void        free_type(ir_type *tp);
1206
1207 const tp_op*get_type_tpop(const ir_type *tp);
1208 ident*      get_type_tpop_nameid(const ir_type *tp);
1209 const char* get_type_tpop_name(const ir_type *tp);
1210 tp_opcode   get_type_tpop_code(const ir_type *tp);
1211
1212 ident*      get_type_ident(const ir_type *tp);
1213 void        set_type_ident(ir_type *tp, ident* id);
1214 const char* get_type_name(const ir_type *tp);
1215
1216 /** The visibility of a type.
1217  *
1218  *  The visibility of a type indicates, whether entities of this type
1219  *  are accessed or allocated in external code.
1220  *
1221  *  An entity of a type is allocated in external code, if the external
1222  *  code declares a variable of this type, or dynamically allocates
1223  *  an entity of this type.  If the external code declares a (compound)
1224  *  type, that contains entities of this type, the visibility also
1225  *  must be external_allocated.
1226  *
1227  *  The visibility must be higher than that of all entities, if the
1228  *  type is a compound.  Here it is questionable, what happens with
1229  *  static entities.  If these are accessed external by direct reference,
1230  *  (a static call to a method, that is also in the dispatch table)
1231  *  it should not affect the visibility of the type.
1232  *
1233  *
1234  * @@@ Do we need a visibility for types?
1235  * I change the layout of types radically when doing type splitting.
1236  * I need to know, which fields of classes are accessed in the RTS,
1237  * e.g., [_length.  I may not move [_length to the split part.
1238  * The layout though, is a property of the type.
1239  *
1240  * One could also think of changing the mode of a type ...
1241  *
1242  * But, we could also output macros to access the fields, e.g.,
1243  *  ACCESS_[_length (X)   X->length              // conventional
1244  *  ACCESS_[_length (X)   X->_split_ref->length  // with type splitting
1245  *
1246  * For now I implement this function, that returns the visibility
1247  * based on the visibility of the entities of a compound ...
1248  *
1249  * This function returns visibility_external_visible if one or more
1250  * entities of a compound type have visibility_external_visible.
1251  * Entities of types are never visibility_external_allocated (right?).
1252  * Else returns visibility_local.
1253  */
1254 ir_visibility get_type_visibility(const ir_type *tp);
1255 void          set_type_visibility(ir_type *tp, ir_visibility v);
1256
1257
1258
1259 /** The state of the type layout. */
1260 typedef enum {
1261   layout_undefined,    /**< The layout of this type is not defined.
1262                             Address computation to access fields is not
1263                             possible, fields must be accessed by Sel
1264                             nodes.  Enumeration constants might be undefined.
1265                             This is the default value except for
1266                             pointer, primitive and method types. */
1267   layout_fixed         /**< The layout is fixed, all component/member entities
1268                             have an offset assigned.  Size of the type is known.
1269                             Arrays can be accessed by explicit address
1270                             computation.  Enumeration constants must be defined.
1271                             Default for pointer, primitive and method types. */
1272 } type_state;
1273
1274 /** Returns a human readable string for the enum entry. */
1275 const char *get_type_state_name(type_state s);
1276
1277 /** Returns the type layout state of a type. */
1278 type_state  get_type_state(const ir_type *tp);
1279
1280 /** Sets the type layout state of a type.
1281  *
1282  * For primitives, pointer and method types the layout is always fixed.
1283  * This call is legal but has no effect.
1284  */
1285 void        set_type_state(ir_type *tp, type_state state);
1286
1287 /** Returns the mode of a type.
1288  *
1289  * Returns NULL for all non atomic types.
1290  */
1291 ir_mode*    get_type_mode(const ir_type *tp);
1292
1293 /** Sets the mode of a type.
1294  *
1295  * Only has an effect on primitive, enumeration and pointer types.
1296  */
1297 void        set_type_mode(ir_type *tp, ir_mode* m);
1298
1299 /** Returns the size of a type in bytes, returns -1 if the size is NOT
1300  *  a byte size, i.e. not dividable by 8. */
1301 int         get_type_size_bytes(const ir_type *tp);
1302
1303 /** Returns the size of a type in bits. */
1304 int         get_type_size_bits(const ir_type *tp);
1305
1306 /** Sets the size of a type in bytes.
1307  *
1308  * For primitive, enumeration, pointer and method types the size
1309  * is always fixed. This call is legal but has no effect.
1310  */
1311 void        set_type_size_bytes(ir_type *tp, int size);
1312
1313 /** Sets the size of a type in bits.
1314  *
1315  * For primitive, enumeration, pointer and method types the size
1316  * is always fixed. This call is legal but has no effect.
1317  */
1318 void        set_type_size_bits(ir_type *tp, int size);
1319
1320 /** Returns the alignment of a type in bytes.
1321  *
1322  *  Returns -1 if the alignment is NOT
1323  *  a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
1324 int         get_type_alignment_bytes(ir_type *tp);
1325
1326 /** Returns the alignment of a type in bits.
1327  *
1328  *  If the alignment of a type is
1329  *  not set, it is calculated here according to the following rules:
1330  *  -#.) if a type has a mode, the alignment is the mode size.
1331  *  -#.) compound types have the alignment of there biggest member.
1332  *  -#.) array types have the alignment of there element type.
1333  *  -#.) method types return 0 here.
1334  *  -#.) all other types return 8 here (i.e. aligned at byte).
1335  */
1336 int         get_type_alignment_bits(ir_type *tp);
1337
1338 /** Sets the alignment of a type in bytes. */
1339 void        set_type_alignment_bytes(ir_type *tp, int size);
1340
1341 /** Sets the alignment of a type in bits.
1342  *
1343  * For method types the alignment is always fixed.
1344  * This call is legal but has no effect.
1345  */
1346 void        set_type_alignment_bits(ir_type *tp, int size);
1347
1348 /** Returns the visited count of a type. */
1349 unsigned long get_type_visited(const ir_type *tp);
1350 /** Sets the visited count of a type to num. */
1351 void          set_type_visited(ir_type *tp, unsigned long num);
1352 /** Sets visited field in type to type_visited. */
1353 void          mark_type_visited(ir_type *tp);
1354 /** Returns non-zero if the type is already visited */
1355 int           type_visited(const ir_type *tp);
1356 /** Returns non-zero if the type is not yet visited */
1357 int           type_not_visited(const ir_type *tp);
1358
1359 /** Returns the associated link field of a type. */
1360 void*         get_type_link(const ir_type *tp);
1361 /** Sets the associated link field of a type. */
1362 void          set_type_link(ir_type *tp, void *l);
1363
1364 /**
1365  * Visited flag to traverse the type information.
1366  *
1367  * Increase this flag by one before traversing the type information
1368  * using inc_master_type_visited().
1369  * Mark type nodes as visited by mark_type_visited(ir_type).
1370  * Check whether node was already visited by type_visited(ir_type)
1371  * and type_not_visited(ir_type).
1372  * Or use the function to walk all types.
1373  *
1374  * @see  typewalk
1375  */
1376 void          set_master_type_visited(unsigned long val);
1377 unsigned long get_master_type_visited(void);
1378 void          inc_master_type_visited(void);
1379
1380 /**
1381  * Checks whether a pointer points to a type.
1382  *
1383  * @param thing     an arbitrary pointer
1384  *
1385  * @return
1386  *     true if the thing is a type, else false
1387  */
1388 int is_type(const void *thing);
1389
1390 /**
1391  *   Checks whether two types are structurally equal.
1392  *
1393  *   @param typ1  the first type
1394  *   @param typ2  the second type
1395  *
1396  *   @return
1397  *    true if the types are equal, else false.
1398  *
1399  *   Types are equal if :
1400  *    - they are the same type kind
1401  *    - they have the same name
1402  *    - they have the same mode (if applicable)
1403  *    - they have the same type_state and, ev., the same size
1404  *    - they are class types and have:
1405  *      - the same members (see same_entity in entity.h)
1406  *      - the same supertypes -- the C-pointers are compared --> no recursive call.
1407  *      - the same number of subtypes.  Subtypes are not compared,
1408  *        as this could cause a cyclic test.
1409  *      - the same peculiarity
1410  *    - they are structure types and have the same members
1411  *    - they are method types and have
1412  *      - the same parameter types
1413  *      - the same result types
1414  *    - they are union types and have the same members
1415  *    - they are array types and have
1416  *      - the same number of dimensions
1417  *      - the same dimension bounds
1418  *      - the same dimension order
1419  *      - the same element type
1420  *    - they are enumeration types and have the same enumerator names
1421  *    - they are pointer types and have the identical points_to type
1422  *      (i.e., the same C-struct to represent the type, type_id is skipped.
1423  *       This is to avoid endless recursions; with pointer types cyclic
1424  *       type graphs are possible.)
1425  */
1426 int equal_type(ir_type *typ1, ir_type *typ2);
1427
1428 /**
1429  *   Checks whether two types are structural comparable.
1430  *
1431  *   @param st pointer type
1432  *   @param lt pointer type
1433  *
1434  *   @return
1435  *    true if type st is smaller than type lt, i.e. whenever
1436  *    lt is expected a st can be used.
1437  *    This is true if
1438  *    - they are the same type kind
1439  *    - mode(st) < mode (lt)  (if applicable)
1440  *    - they are class types and st is (transitive) subtype of lt,
1441  *    - they are structure types and
1442  *       - the members of st have exactly one counterpart in lt with the same name,
1443  *       - the counterpart has a bigger type.
1444  *    - they are method types and have
1445  *      - the same number of parameter and result types,
1446  *      - the parameter types of st are smaller than those of lt,
1447  *      - the result types of st are smaller than those of lt
1448  *    - they are union types and have the members of st have exactly one
1449  *      @return counterpart in lt and the type is smaller
1450  *    - they are array types and have
1451  *      - the same number of dimensions
1452  *      - all bounds of lt are bound of st
1453  *      - the same dimension order
1454  *      - the same element type
1455  *      @return or
1456  *      - the element type of st is smaller than that of lt
1457  *      - the element types have the same size and fixed layout.
1458  *    - they are enumeration types and have the same enumerator names
1459  *    - they are pointer types and have the points_to type of st is
1460  *      @return smaller than the points_to type of lt.
1461  *
1462  */
1463 int smaller_type(ir_type *st, ir_type *lt);
1464
1465 /**
1466  *  @page class_type    Representation of a class type
1467  *
1468  *  If the type opcode is set to type_class the type represents class
1469  *  types.  A list of fields and methods is associated with a class.
1470  *  Further a class can inherit from and bequest to other classes.
1471  *
1472  *  The following attributes are private to this type kind:
1473  *  - member:     All entities belonging to this class.  This are method entities
1474  *                which have type_method or fields that can have any of the
1475  *                following type kinds: type_class, type_struct, type_union,
1476  *                type_array, type_enumeration, type_pointer, type_primitive.
1477  *
1478  *  The following two are dynamic lists that can be grown with an "add_" function,
1479  *  but not shrinked:
1480  *
1481  *  - subtypes:    A list of direct subclasses.
1482  *
1483  *  - supertypes:  A list of direct superclasses.
1484  *
1485  *  - peculiarity: The peculiarity of this class.  If the class is of peculiarity
1486  *                 "description" it only is a description of requirements to a class,
1487  *                 as, e.g., a Java interface.  The class will never be allocated.
1488  *                 Peculiarity inherited is only possible for entities.  An entity
1489  *                 is of peculiarity inherited if the compiler generated the entity
1490  *                 to explicitly resolve inheritance.  An inherited method entity has
1491  *                 no value for irg.
1492  *                 Values: description, existent, inherited.  Default: existent.
1493  *
1494  *  - type_info:   An entity representing the type information of this class.
1495  *                 This entity can be of arbitrari type, Firm did not use it yet.
1496  *                 It allows to express the coupling of a type with an entity
1497  *                 representing this type.  This information is useful for lowering
1498  *                 of InstOf and TypeChk nodes.  Default: NULL
1499  *
1500  *  - vtable_size: The size of this class virtual function table.
1501  *                 Default:  0
1502  *
1503  *  - final:       A final class is always a leaf in the class hierarchy.  Final
1504  *                 classes cannot be super classes of other ones.  As this information
1505  *                 can only be computed in whole world compilations, we allow to
1506  *                 set this flag.  It is used in optimizations if get_opt_closed_world()
1507  *                 is false.  Default:  false
1508  *
1509  *  - interface:   The class represents an interface.  This flag can be set to distinguish
1510  *                 between interfaces, abstract classes and other classes that all may
1511  *                 have the peculiarity peculiarity_description.  Depending on this flag
1512  *                 the lowering might do different actions.  Default:  false
1513  *
1514  *  - abstract :   The class represents an abstract class.  This flag can be set to distinguish
1515  *                 between interfaces, abstract classes and other classes that all may
1516  *                 have the peculiarity peculiarity_description.  Depending on this flag
1517  *                 the lowering might do different actions.  Default:  false
1518  */
1519
1520 /** Creates a new class type. */
1521 ir_type *new_type_class (ident *name);
1522
1523 /** Creates a new class type with debug information. */
1524 ir_type *new_d_type_class (ident *name, dbg_info *db);
1525
1526 /* --- manipulate private fields of class type  --- */
1527
1528 /** Adds the entity as member of the class.  */
1529 void add_class_member   (ir_type *clss, ir_entity *member);
1530
1531 /** Returns the number of members of this class. */
1532 int get_class_n_members (const ir_type *clss);
1533
1534 /** Returns the member at position pos, 0 <= pos < n_member */
1535 ir_entity *get_class_member   (const ir_type *clss, int pos);
1536
1537 /** Returns index of mem in clss, -1 if not contained. */
1538 int get_class_member_index(const ir_type *clss, ir_entity *mem);
1539
1540 /** Finds the member with name 'name'. If several members with the same
1541  *  name returns one of them.  Returns NULL if no member found. */
1542 ir_entity *get_class_member_by_name(ir_type *clss, ident *name);
1543
1544 /** Overwrites the member at position pos, 0 <= pos < n_member with
1545  *  the passed entity. */
1546 void set_class_member   (ir_type *clss, ir_entity *member, int pos);
1547
1548 /** Replaces complete member list in class type by the list passed.
1549  *
1550  *  Copies the list passed. This function is necessary to reduce the number of members.
1551  *  members is an array of entities, num the size of this array.  Sets all
1552  *  owners of the members passed to clss. */
1553 void set_class_members  (ir_type *clss, ir_entity *members[], int arity);
1554
1555 /** Finds member in the list of members and removes it.
1556  *
1557  *  Shrinks the member list, so iterate from the end!!!
1558  *  Does not deallocate the entity.  */
1559 void remove_class_member(ir_type *clss, ir_entity *member);
1560
1561
1562 /** Adds subtype as subtype to clss.
1563  *
1564  *  Checks whether clss is a supertype of subtype.  If not
1565  *  adds also clss as supertype to subtype.  */
1566 void    add_class_subtype   (ir_type *clss, ir_type *subtype);
1567
1568 /** Returns the number of subtypes */
1569 int     get_class_n_subtypes (const ir_type *clss);
1570
1571 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
1572 ir_type *get_class_subtype   (ir_type *clss, int pos);
1573
1574 /** Returns the index to access subclass as subtype of class.
1575  *
1576  *  If subclass is no direct subtype of class returns -1.
1577  */
1578 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
1579
1580 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
1581  *
1582  *  Does not set the corresponding supertype relation for subtype: this might
1583  *  be a different position! */
1584 void    set_class_subtype   (ir_type *clss, ir_type *subtype, int pos);
1585
1586 /** Finds subtype in the list of subtypes and removes it  */
1587 void    remove_class_subtype(ir_type *clss, ir_type *subtype);
1588
1589 /* Convenience macros */
1590 #define add_class_derived_type(clss, drvtype)       add_class_subtype(clss, drvtype)
1591 #define get_class_n_derived_types(clss)             get_class_n_subtypes(clss)
1592 #define get_class_derived_type(clss, pos)           get_class_subtype(clss, pos)
1593 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
1594 #define set_class_derived_type(clss, drvtype, pos)  set_class_subtype(clss, drvtype, pos)
1595 #define remove_class_derived_type(clss, drvtype)    remove_class_subtype(clss, drvtype)
1596
1597 /** Adds supertype as supertype to class.
1598  *
1599  *  Checks whether clss is a subtype of supertype.  If not
1600  *  adds also clss as subtype to supertype.  */
1601 void    add_class_supertype   (ir_type *clss, ir_type *supertype);
1602
1603 /** Returns the number of supertypes */
1604 int     get_class_n_supertypes (const ir_type *clss);
1605
1606 /** Returns the index to access superclass as supertype of class.
1607  *
1608  *  If superclass is no direct supertype of class returns -1.
1609  */
1610 int     get_class_supertype_index(ir_type *clss, ir_type *super_clss);
1611
1612 /** Gets the supertype at position pos,  0 <= pos < n_supertype. */
1613 ir_type *get_class_supertype   (ir_type *clss, int pos);
1614
1615 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
1616  *
1617  *  Does not set the corresponding subtype relation for supertype: this might
1618  *  be at a different position! */
1619 void    set_class_supertype   (ir_type *clss, ir_type *supertype, int pos);
1620
1621 /** Finds supertype in the list of supertypes and removes it */
1622 void    remove_class_supertype(ir_type *clss, ir_type *supertype);
1623
1624 /** Convenience macro */
1625 #define add_class_base_type(clss, basetype)  add_class_supertype(clss, basetype)
1626 #define get_class_n_base_types(clss)  get_class_n_supertypes(clss)
1627 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
1628 #define get_class_base_type(clss, pos)  get_class_supertype(clss, pos)
1629 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
1630 #define remove_class_base_type(clss, basetype)  remove_class_supertype(clss, basetype)
1631
1632 /** Convenience macro */
1633 #define add_class_base_type(clss, basetype)        add_class_supertype(clss, basetype)
1634 #define get_class_n_base_types(clss)               get_class_n_supertypes(clss)
1635 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
1636 #define get_class_base_type(clss, pos)             get_class_supertype(clss, pos)
1637 #define set_class_base_type(clss, basetype, pos)   set_class_supertype(clss, basetype, pos)
1638 #define remove_class_base_type(clss, basetype)     remove_class_supertype(clss, basetype)
1639
1640 /** Returns a human readable string for a peculiarity. */
1641 const char *get_peculiarity_name(ir_peculiarity p);
1642
1643 /** Returns the peculiarity of the class. */
1644 ir_peculiarity get_class_peculiarity (const ir_type *clss);
1645 /** Sets the peculiarity of the class. */
1646 void           set_class_peculiarity (ir_type *clss, ir_peculiarity pec);
1647
1648 /** Returns the type info entity of a class. */
1649 ir_entity *get_class_type_info(const ir_type *clss);
1650
1651 /** Set a type info entity for the class. */
1652 void set_class_type_info(ir_type *clss, ir_entity *ent);
1653
1654 /** Returns the size of the virtual function table. */
1655 unsigned get_class_vtable_size(const ir_type *clss);
1656
1657 /** Sets a new size of the virtual function table. */
1658 void set_class_vtable_size(ir_type *clss, unsigned size);
1659
1660 /** Returns non-zero if a class is final. */
1661 int is_class_final(const ir_type *clss);
1662
1663 /** Sets the class final flag. */
1664 void set_class_final(ir_type *clss, int flag);
1665
1666 /** Return non-zero if a class is an interface */
1667 int is_class_interface(const ir_type *clss);
1668
1669 /** Sets the class interface flag. */
1670 void set_class_interface(ir_type *clss, int flag);
1671
1672 /** Return non-zero if a class is an abstract class. */
1673 int is_class_abstract(const ir_type *clss);
1674
1675 /** Sets the class abstract flag. */
1676 void set_class_abstract(ir_type *clss, int flag);
1677
1678 /** Set and get a class' dfn --
1679    @todo This is an undocumented field, subject to change! */
1680 void set_class_dfn (ir_type *clss, int dfn);
1681 int  get_class_dfn (const ir_type *clss);
1682
1683 /** Returns true if a type is a class type. */
1684 int is_Class_type(const ir_type *clss);
1685
1686 /**
1687  *  @page struct_type   Representation of a struct type
1688  *
1689  *  A struct type represents aggregate types that consist of a list
1690  *  of fields.
1691  *
1692  *  The following attributes are private to this type kind:
1693  *  - member:  All entities belonging to this class.  This are the fields
1694  *             that can have any of the following types:  type_class,
1695  *             type_struct, type_union, type_array, type_enumeration,
1696  *             type_pointer, type_primitive.
1697  *             This is a dynamic list that can be grown with an "add_" function,
1698  *             but not shrinked.
1699  *             This is a dynamic list that can be grown with an "add_" function,
1700  *             but not shrinked.
1701  */
1702 /** Creates a new type struct */
1703 ir_type *new_type_struct (ident *name);
1704 /** Creates a new type struct with debug information. */
1705 ir_type *new_d_type_struct (ident *name, dbg_info* db);
1706
1707 /* --- manipulate private fields of struct --- */
1708
1709 /** Adds the entity as member of the struct.  */
1710 void add_struct_member   (ir_type *strct, ir_entity *member);
1711
1712 /** Returns the number of members of this struct. */
1713 int get_struct_n_members (const ir_type *strct);
1714
1715 /** Returns the member at position pos, 0 <= pos < n_member */
1716 ir_entity *get_struct_member   (const ir_type *strct, int pos);
1717
1718 /** Returns index of member in strct, -1 if not contained. */
1719 int get_struct_member_index(const ir_type *strct, ir_entity *member);
1720
1721 /** Overwrites the member at position pos, 0 <= pos < n_member with
1722    the passed entity. */
1723 void set_struct_member   (ir_type *strct, int pos, ir_entity *member);
1724
1725 /** Finds member in the list of members and removes it. */
1726 void remove_struct_member (ir_type *strct, ir_entity *member);
1727
1728 /** Returns true if a type is a struct type. */
1729 int is_Struct_type(const ir_type *strct);
1730
1731 /**
1732  * @page method_type    Representation of a method type
1733  *
1734  * A method type represents a method, function or procedure type.
1735  * It contains a list of the parameter and result types, as these
1736  * are part of the type description.  These lists should not
1737  * be changed by a optimization, as a change creates a new method
1738  * type.  Therefore optimizations should allocated new method types.
1739  * The set_ routines are only for construction by a frontend.
1740  *
1741  * - n_params:   Number of parameters to the procedure.
1742  *               A procedure in FIRM has only call by value parameters.
1743  *
1744  * - param_type: A list with the types of parameters.  This list is ordered.
1745  *               The nth type in this list corresponds to the nth element
1746  *               in the parameter tuple that is a result of the start node.
1747  *               (See ircons.h for more information.)
1748  *
1749  * - value_param_ents
1750  *               A list of entities (whose owner is a struct private to the
1751  *               method type) that represent parameters passed by value.
1752  *
1753  * - n_res:      The number of results of the method.  In general, procedures
1754  *               have zero results, functions one.
1755  *
1756  * - res_type:   A list with the types of parameters.  This list is ordered.
1757  *               The nth type in this list corresponds to the nth input to
1758  *               Return nodes.  (See ircons.h for more information.)
1759  *
1760  * - value_res_ents
1761  *               A list of entities (whose owner is a struct private to the
1762  *               method type) that represent results passed by value.
1763  */
1764
1765 /* These macros define the suffixes for the types and entities used
1766    to represent value parameters / results. */
1767 #define VALUE_PARAMS_SUFFIX  "val_param"
1768 #define VALUE_RESS_SUFFIX    "val_res"
1769
1770 /** Create a new method type.
1771  *
1772  * @param name      the name (ident) of this type
1773  * @param n_param   the number of parameters
1774  * @param n_res     the number of results
1775  *
1776  * The arrays for the parameter and result types are not initialized by
1777  * the constructor.
1778  */
1779 ir_type *new_type_method (ident *name, int n_param, int n_res);
1780
1781 /** Create a new method type with debug information.
1782  *
1783  * @param name      the name (ident) of this type
1784  * @param n_param   the number of parameters
1785  * @param n_res     the number of results
1786  * @param db        user defined debug information
1787  *
1788  * The arrays for the parameter and result types are not initialized by
1789  * the constructor.
1790  */
1791 ir_type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
1792
1793 /* -- manipulate private fields of method. -- */
1794
1795 /** Returns the number of parameters of this method. */
1796 int   get_method_n_params  (const ir_type *method);
1797
1798 /** Returns the type of the parameter at position pos of a method. */
1799 ir_type *get_method_param_type(ir_type *method, int pos);
1800 /** Sets the type of the parameter at position pos of a method.
1801     Also changes the type in the pass-by-value representation by just
1802     changing the type of the corresponding entity if the representation is constructed. */
1803 void  set_method_param_type(ir_type *method, int pos, ir_type *tp);
1804 /** Returns an entity that represents the copied value argument.  Only necessary
1805    for compounds passed by value. This information is constructed only on demand. */
1806 ir_entity *get_method_value_param_ent(ir_type *method, int pos);
1807 /**
1808  * Returns a type that represents the copied value arguments if one
1809  * was allocated, else NULL.
1810  */
1811 ir_type *get_method_value_param_type(const ir_type *method);
1812 /** Returns an ident representing the parameters name. Returns NULL if not set.
1813     For debug support only. */
1814 ident *get_method_param_ident(ir_type *method, int pos);
1815 /** Returns a string representing the parameters name. Returns NULL if not set.
1816     For debug support only. */
1817 const char *get_method_param_name(ir_type *method, int pos);
1818 /** Sets an ident representing the parameters name. For debug support only. */
1819 void set_method_param_ident(ir_type *method, int pos, ident *id);
1820
1821 /** Returns the number of results of a method type. */
1822 int   get_method_n_ress   (const ir_type *method);
1823 /** Returns the return type of a method type at position pos. */
1824 ir_type *get_method_res_type(ir_type *method, int pos);
1825 /** Sets the type of the result at position pos of a method.
1826     Also changes the type in the pass-by-value representation by just
1827     changing the type of the corresponding entity if the representation is constructed. */
1828 void  set_method_res_type(ir_type *method, int pos, ir_type *tp);
1829 /** Returns an entity that represents the copied value result.  Only necessary
1830    for compounds passed by value. This information is constructed only on demand. */
1831 ir_entity *get_method_value_res_ent(ir_type *method, int pos);
1832
1833 /**
1834  * Returns a type that represents the copied value results.
1835  */
1836 ir_type *get_method_value_res_type(const ir_type *method);
1837
1838 /**
1839  * This enum flags the variadicity of methods (methods with a
1840  * variable amount of arguments (e.g. C's printf). Default is
1841  * non_variadic.
1842  */
1843 typedef enum variadicity {
1844   variadicity_non_variadic, /**< non variadic */
1845   variadicity_variadic      /**< variadic */
1846 } variadicity;
1847
1848 /** Returns the null-terminated name of this variadicity. */
1849 const char *get_variadicity_name(variadicity vari);
1850
1851 /** Returns the variadicity of a method. */
1852 variadicity get_method_variadicity(const ir_type *method);
1853
1854 /** Sets the variadicity of a method. */
1855 void set_method_variadicity(ir_type *method, variadicity vari);
1856
1857 /**
1858  * Returns the first variadic parameter index of a type.
1859  * If this index was NOT set, the index of the last parameter
1860  * of the method type plus one is returned for variadic functions.
1861  * Non-variadic function types always return -1 here.
1862  */
1863 int get_method_first_variadic_param_index(const ir_type *method);
1864
1865 /**
1866  * Sets the first variadic parameter index. This allows to specify
1867  * a complete call type (containing the type of all parameters)
1868  * but still have the knowledge, which parameter must be passed as
1869  * variadic one.
1870  */
1871 void set_method_first_variadic_param_index(ir_type *method, int index);
1872
1873 /** Returns the mask of the additional graph properties. */
1874 unsigned get_method_additional_properties(const ir_type *method);
1875
1876 /** Sets the mask of the additional graph properties. */
1877 void set_method_additional_properties(ir_type *method, unsigned property_mask);
1878
1879 /** Sets one additional graph property. */
1880 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
1881
1882 /**
1883  * Calling conventions: lower 24 bits are the number of register parameters,
1884  * upper 8 encode the calling conventions.
1885  */
1886 typedef enum {
1887   cc_reg_param        = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
1888                                          This flag may be set as default on some architectures. */
1889   cc_last_on_top      = 0x02000000, /**< The last non-register parameter is transmitted on top of
1890                                          the stack. This is equivalent to the pascal
1891                                          calling convention. If this flag is not set, the first
1892                                          non-register parameter is used (stdcall or cdecl
1893                                          calling convention) */
1894   cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
1895                                          function calls (stdcall). */
1896   cc_this_call        = 0x08000000, /**< The first parameter is a this pointer and is transmitted
1897                                          in a special way. */
1898
1899   cc_bits             = (0xFF << 24)  /**< the calling convention bits */
1900 } calling_convention;
1901
1902 /* some often used cases: made as defines because firmjni cannot handle two
1903    equal enum values. */
1904
1905 /** cdecl calling convention */
1906 #define cc_cdecl_set    (0)
1907 /** stdcall calling convention */
1908 #define cc_stdcall_set  cc_callee_clear_stk
1909 /** fastcall calling convention */
1910 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
1911
1912 /** Returns the default calling convention for method types. */
1913 unsigned get_default_cc_mask(void);
1914
1915 /**
1916  * check for the CDECL calling convention
1917  */
1918 #define IS_CDECL(cc_mask)     (((cc_mask) & cc_bits) == cc_cdecl_set)
1919
1920 /**
1921  * check for the STDCALL calling convention
1922  */
1923 #define IS_STDCALL(cc_mask)   (((cc_mask) & cc_bits) == cc_stdcall_set)
1924
1925 /**
1926  * check for the FASTCALL calling convention
1927  */
1928 #define IS_FASTCALL(cc_mask)  (((cc_mask) & cc_bits) == cc_fastcall_set)
1929
1930 /**
1931  * Sets the CDECL convention bits.
1932  */
1933 #define SET_CDECL(cc_mask)    (((cc_mask) & ~cc_bits) | cc_cdecl_set)
1934
1935 /**
1936  * Set. the STDCALL convention bits.
1937  */
1938 #define SET_STDCALL(cc_mask)  (((cc_mask) & ~cc_bits) | cc_stdcall_set)
1939
1940 /**
1941  * Sets the FASTCALL convention bits.
1942  */
1943 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
1944
1945 /** Returns the calling convention of an entities graph. */
1946 unsigned get_method_calling_convention(const ir_type *method);
1947
1948 /** Sets the calling convention of an entities graph. */
1949 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
1950
1951 /** Returns the number of registers parameters, 0 means default. */
1952 unsigned get_method_n_regparams(ir_type *method);
1953
1954 /** Sets the number of registers parameters, 0 means default. */
1955 void set_method_n_regparams(ir_type *method, unsigned n_regs);
1956
1957 /** Returns true if a type is a method type. */
1958 int   is_Method_type     (const ir_type *method);
1959
1960 /**
1961  *   @page union_type   Representation of a union (variant) type.
1962  *
1963  *   The union type represents union types.  Note that this representation
1964  *   resembles the C union type.  For tagged variant types like in Pascal or Modula
1965  *   a combination of a struct and a union type must be used.
1966  *
1967  *   - n_types:     Number of unioned types.
1968  *   - members:     Entities for unioned types.  Fixed length array.
1969  *                  This is a dynamic list that can be grown with an "add_" function,
1970  *                  but not shrinked.
1971  */
1972 /** Creates a new type union. */
1973 ir_type   *new_type_union (ident *name);
1974
1975 /** Creates a new type union with debug information. */
1976 ir_type   *new_d_type_union (ident *name, dbg_info* db);
1977
1978 /* --- manipulate private fields of struct --- */
1979
1980 /** Returns the number of unioned types of this union */
1981 int     get_union_n_members      (const ir_type *uni);
1982
1983 /** Adds a new entity to a union type */
1984 void    add_union_member (ir_type *uni, ir_entity *member);
1985
1986 /** Returns the entity at position pos of a union */
1987 ir_entity *get_union_member (const ir_type *uni, int pos);
1988
1989 /** Returns index of member in uni, -1 if not contained. */
1990 int     get_union_member_index(const ir_type *uni, ir_entity *member);
1991
1992 /** Overwrites a entity at position pos in a union type. */
1993 void    set_union_member (ir_type *uni, int pos, ir_entity *member);
1994
1995 /** Finds member in the list of members and removes it. */
1996 void    remove_union_member (ir_type *uni, ir_entity *member);
1997
1998 /** Returns true if a type is a union type. */
1999 int     is_Union_type          (const ir_type *uni);
2000
2001 /**
2002  * @page array_type Representation of an array type
2003  *
2004  * The array type represents rectangular multi dimensional arrays.
2005  * The constants representing the bounds must be allocated to
2006  * get_const_code_irg() by setting current_ir_graph accordingly.
2007  *
2008  * - n_dimensions:    Number of array dimensions.
2009  * - *lower_bound:    Lower bounds of dimensions.  Usually all 0.
2010  * - *upper_bound:    Upper bounds or dimensions.
2011  * - *element_type:   The type of the array elements.
2012  * - *element_ent:    An entity for the array elements to be used for
2013  *                      element selection with Sel.
2014  * @todo
2015  *   Do we need several entities?  One might want
2016  *   to select a dimension and not a single element in case of multi
2017  *   dimensional arrays.
2018  */
2019
2020 /** Create a new type array.
2021  *
2022  * Sets n_dimension to dimension and all dimension entries to NULL.
2023  * Initializes order to the order of the dimensions.
2024  * The entity for array elements is built automatically.
2025  * Set dimension sizes after call to constructor with set_* routines.
2026  */
2027 ir_type *new_type_array         (ident *name, int n_dimensions,
2028                   ir_type *element_type);
2029
2030 /** Create a new type array with debug information.
2031  *
2032  * Sets n_dimension to dimension and all dimension entries to NULL.
2033  * Initializes order to the order of the dimensions.
2034  * The entity for array elements is built automatically.
2035  * Set dimension sizes after call to constructor with set_* routines.
2036  * A legal array type must have at least one dimension set.
2037  */
2038 ir_type *new_d_type_array         (ident *name, int n_dimensions,
2039                   ir_type *element_type, dbg_info* db);
2040
2041 /* --- manipulate private fields of array type --- */
2042
2043 /** Returns the number of array dimensions of this type. */
2044 int   get_array_n_dimensions (const ir_type *array);
2045
2046 /**
2047  * Allocates Const nodes of mode_Is for one array dimension.
2048  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
2049  */
2050 void  set_array_bounds_int   (ir_type *array, int dimension, int lower_bound,
2051                                                           int upper_bound);
2052 /**
2053  * Sets the bounds for one array dimension.
2054  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
2055  */
2056 void  set_array_bounds       (ir_type *array, int dimension, ir_node *lower_bound,
2057                                                           ir_node *upper_bound);
2058 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
2059 void  set_array_lower_bound  (ir_type *array, int dimension, ir_node *lower_bound);
2060
2061 /** Allocates Const nodes of mode_Is for the lower bound of an array
2062     dimension, i.e. [lower,upper[ */
2063 void  set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound);
2064
2065 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
2066 void  set_array_upper_bound  (ir_type *array, int dimension, ir_node *upper_bound);
2067
2068 /** Allocates Const nodes of mode_Is for the upper bound of an array
2069     dimension, i.e. [lower,upper[. */
2070 void  set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound);
2071
2072 /** Returns true if lower bound != Unknown. */
2073 int       has_array_lower_bound     (const ir_type *array, int dimension);
2074 /** Returns the lower bound of an array. */
2075 ir_node * get_array_lower_bound     (const ir_type *array, int dimension);
2076 /** Works only if bound is Const node with tarval that can be converted to long. */
2077 long      get_array_lower_bound_int (const ir_type *array, int dimension);
2078 /** returns true if lower bound != Unknown */
2079 int       has_array_upper_bound     (const ir_type *array, int dimension);
2080 /** Returns the upper bound of an array. */
2081 ir_node * get_array_upper_bound     (const ir_type *array, int dimension);
2082 /** Works only if bound is Const node with tarval that can be converted to long. */
2083 long      get_array_upper_bound_int (const ir_type *array, int dimension);
2084
2085 /** Sets an array dimension to a specific order. */
2086 void set_array_order (ir_type *array, int dimension, int order);
2087
2088 /** Returns the order of an array dimension. */
2089 int  get_array_order (const ir_type *array, int dimension);
2090
2091 /** Find the array dimension that is placed at order order. */
2092 int find_array_dimension(const ir_type *array, int order);
2093
2094 /** Sets the array element type. */
2095 void  set_array_element_type (ir_type *array, ir_type* tp);
2096
2097 /** Gets the array element type. */
2098 ir_type *get_array_element_type (ir_type *array);
2099
2100 /** Sets the array element entity. */
2101 void  set_array_element_entity (ir_type *array, ir_entity *ent);
2102
2103 /** Get the array element entity. */
2104 ir_entity *get_array_element_entity (const ir_type *array);
2105
2106 /** Returns true if a type is an array type. */
2107 int    is_Array_type(const ir_type *array);
2108
2109 /**
2110  * @page enumeration_type   Representation of an enumeration type
2111  *
2112  * Enumeration types need not necessarily be represented explicitly
2113  * by Firm types, as the frontend can lower them to integer constants as
2114  * well.  For debugging purposes or similar tasks this information is useful.
2115  * The type state layout_fixed is set, if all enumeration constant have
2116  * there tarvals assigned.  Until then
2117  *
2118  * - *const:        The target values representing the constants used to
2119  *                  represent individual enumerations.
2120  */
2121
2122 /** Create a new type enumeration -- set the enumerators independently. */
2123 ir_type   *new_type_enumeration(ident *name, int n_enums);
2124
2125 /** Create a new type enumeration with debug information -- set the enumerators independently. */
2126 ir_type   *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db);
2127
2128 /* --- manipulate fields of enumeration type. --- */
2129
2130 /** Set an enumeration constant to a enumeration type at a given position. */
2131 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con);
2132
2133 /** Returns the number of enumeration values of this enumeration */
2134 int     get_enumeration_n_enums(const ir_type *enumeration);
2135
2136 /** Returns the enumeration constant at a given position. */
2137 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos);
2138
2139 /** Returns the enumeration type owner of an enumeration constant. */
2140 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst);
2141
2142 /** Sets the enumeration constant value. */
2143 void    set_enumeration_value(ir_enum_const *enum_cnst, tarval *con);
2144
2145 /** Returns the enumeration constant value. */
2146 tarval *get_enumeration_value(const ir_enum_const *enum_cnst);
2147
2148 /** Assign an ident to an enumeration constant. */
2149 void    set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id);
2150
2151 /** Returns the assigned ident of an enumeration constant. */
2152 ident  *get_enumeration_nameid(const ir_enum_const *enum_cnst);
2153
2154 /** Returns the assigned name of an enumeration constant. */
2155 const char *get_enumeration_name(const ir_enum_const *enum_cnst);
2156
2157 /** Returns true if a type is a enumeration type. */
2158 int     is_Enumeration_type(const ir_type *enumeration);
2159
2160 /**
2161  * @page pointer_type   Representation of a pointer type
2162  *
2163  * The mode of the pointer type must be a reference mode.
2164  *
2165  * Pointer types:
2166  * - points_to:      The type of the entity this pointer points to.
2167  */
2168
2169 /** Creates a new type pointer. */
2170 ir_type *new_type_pointer           (ident *name, ir_type *points_to, ir_mode *ptr_mode);
2171
2172 /** Creates a new type pointer with debug information. */
2173 ir_type *new_d_type_pointer         (ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
2174
2175 /* --- manipulate fields of type_pointer --- */
2176
2177 /** Sets the type to which a pointer points to. */
2178 void  set_pointer_points_to_type (ir_type *pointer, ir_type *tp);
2179
2180 /** Returns the type to which a pointer points to. */
2181 ir_type *get_pointer_points_to_type (ir_type *pointer);
2182
2183 /** Returns true if a type is a pointer type. */
2184 int   is_Pointer_type            (const ir_type *pointer);
2185
2186 /** Returns the first pointer type that has as points_to tp.
2187  *  Not efficient: O(#types).
2188  *  If not found returns firm_unknown_type. */
2189 ir_type *find_pointer_type_to_type (ir_type *tp);
2190
2191 /**
2192  * @page primitive_type Representation of a primitive type
2193  *
2194  * Primitive types are types that represent atomic data values that
2195  * map directly to modes.  They don't have private attributes.  The
2196  * important information they carry is held in the common mode field.
2197  */
2198 /** Creates a new primitive type. */
2199 ir_type *new_type_primitive(ident *name, ir_mode *mode);
2200
2201 /** Creates a new primitive type with debug information. */
2202 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info* db);
2203
2204 /** Returns true if a type is a primitive type. */
2205 int  is_Primitive_type(const ir_type *primitive);
2206
2207
2208 /**
2209  * @page none_type The None type
2210  *
2211  *  This type is an auxiliary type dedicated to support type analyses.
2212  *
2213  *  The none type represents that there is no type.  The type can be used to
2214  *  initialize fields of type* that actually can not contain a type or that
2215  *  are initialized for an analysis. There exists exactly one type none.
2216  *  This type is not on the type list in ir_prog. It is
2217  *  allocated when initializing the type module.
2218  *
2219  *  The following values are set:
2220  *    - mode:  mode_BAD
2221  *    - name:  "type_none"
2222  *    - state: layout_fixed
2223  *    - size:  0
2224  */
2225 /** A variable that contains the only none type. */
2226 extern ir_type *firm_none_type;
2227
2228 /** Returns the none type. */
2229 ir_type *get_none_type(void);
2230
2231 /**
2232  * @page unknown_type  The Unknown type
2233  *
2234  *  This type is an auxiliary type dedicated to support type analyses.
2235  *
2236  *  The unknown type represents that there could be a type, but it is not
2237  *  known.  This type can be used to initialize fields before an analysis (not known
2238  *  yet) or to represent the top of a lattice (could not be determined).  There exists
2239  *  exactly one type unknown. This type is not on the type list in ir_prog.  It is
2240  *  allocated when initializing the type module.
2241  *
2242  *  The following values are set:
2243  *    - mode:  mode_ANY
2244  *    - name:  "type_unknown"
2245  *    - state: layout_fixed
2246  *    - size:  0
2247  */
2248 /** A variable that contains the only unknown type. */
2249 extern ir_type *firm_unknown_type;
2250
2251 /** Returns the unknown type. */
2252 ir_type *get_unknown_type(void);
2253
2254
2255 /**
2256  *  Checks whether a type is atomic.
2257  *  @param tp   any type
2258  *  @return true if type is primitive, pointer or enumeration
2259  */
2260 int is_atomic_type(const ir_type *tp);
2261
2262 /* --- Support for compound types --- */
2263
2264 /**
2265  * Gets the number of elements in a Firm compound type.
2266  *
2267  * This is just a comfortability function, because structs and
2268  * classes can often be treated be the same code, but they have
2269  * different access functions to their members.
2270  *
2271  * @param tp  The type (must be struct, union or class).
2272  *
2273  * @return Number of members in the compound type.
2274  */
2275 int get_compound_n_members(const ir_type *tp);
2276
2277 /**
2278  * Gets the member of a Firm compound type at position pos.
2279  *
2280  * @param tp  The type (must be struct, union or class).
2281  * @param pos The number of the member.
2282  *
2283  * @return The member entity at position pos.
2284  *
2285  * @see get_compound_n_members() for justification of existence.
2286  */
2287 ir_entity *get_compound_member(const ir_type *tp, int pos);
2288
2289 /** Returns index of member in tp, -1 if not contained. */
2290 int     get_compound_member_index(const ir_type *tp, ir_entity *member);
2291
2292 /**
2293  * Checks whether a type is a compound type.
2294  *
2295  * @param tp - any type
2296  *
2297  * @return true if the type is class, structure, union or array type.
2298  */
2299 int is_compound_type(const ir_type *tp);
2300
2301 /**
2302  * Checks, whether a type is a frame type.
2303  */
2304 int is_frame_type(const ir_type *tp);
2305
2306 /**
2307  * Checks, whether a type is a value parameter type.
2308  */
2309 int is_value_param_type(const ir_type *tp);
2310
2311 /**
2312  * Checks, whether a type is a lowered type.
2313  */
2314 int is_lowered_type(const ir_type *tp);
2315
2316 /**
2317  * Makes a new frame type. Frame types are class types,
2318  * so all class access functions work.
2319  * Frame types are not in the global list of types.
2320  */
2321 ir_type *new_type_frame(ident *name);
2322
2323 /**
2324  * Sets a lowered type for a type. This sets both associations
2325  * and marks lowered_type as a "lowered" one.
2326  */
2327 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
2328
2329 /**
2330  * Gets the lowered/unlowered type of a type or NULL if this type
2331  * has no lowered/unlowered one.
2332  */
2333 ir_type *get_associated_type(const ir_type *tp);
2334
2335 /**
2336  * Allocate an area of size bytes aligned at alignment
2337  * at the start or the end of a frame type.
2338  * The frame type must already have a fixed layout.
2339  *
2340  * @param frame_type a frame type
2341  * @param size       the size of the entity
2342  * @param alignment  the alignment of the entity
2343  * @param at_start   if true, put the area at the frame type's start, else at end
2344  *
2345  * @return the entity representing the area
2346  */
2347 ir_entity *frame_alloc_area(ir_type *frame_type, int size, int alignment, int at_start);
2348
2349 /*-----------------------------------------------------------------*/
2350 /** Debug aides                                                   **/
2351 /*-----------------------------------------------------------------*/
2352
2353 /**
2354  *  Outputs a unique number for this type if libfirm is compiled for
2355  *  debugging, (configure with --enable-debug) else returns the address
2356  *  of the type cast to long.
2357  */
2358 long get_type_nr(const ir_type *tp);
2359
2360 /* ------------------------------------------------------------------------ */
2361
2362 /**  Type for a function that compares two types.
2363  *
2364  *   @param tp1  The first type to compare.
2365  *   @param tp2  The second type to compare.
2366  */
2367 typedef int (compare_types_func_t)(const void *tp1, const void *tp2);
2368
2369 /** Compares two types by their name.
2370  *
2371  * Compares the opcode and the name of the types. If these are
2372  * equal returns 0, else non-zero.
2373  */
2374 int compare_names (const void *tp1, const void *tp2);
2375
2376 /** Compares two types strict.
2377  *
2378  * returns 0 if tp1 == tp2, else non-zero
2379  */
2380 int compare_strict (const void *tp1, const void *tp2);
2381
2382 /* ------------------------------------------------------------------------ */
2383
2384 /**  Type for a function that computes a hash value for a type.
2385  *
2386  *   @param tp The type to compute a hash for.
2387  */
2388 typedef int (hash_types_func_t)(ir_type *tp);
2389
2390 /** Computes a hash value by the type name.
2391  *
2392  * Uses the name of the type and the type opcode to compute the hash.
2393  */
2394 int firm_hash_name (ir_type *tp);
2395
2396 /* ------------------------------------------------------------------------ */
2397
2398 /** Finalize type construction.
2399  *
2400  * Indicate that a type is so far completed that it can be
2401  * distinguished from other types.  Mature_type hashes the type into a
2402  * table.  It uses the function in compare_types_func to compare the
2403  * types.
2404  *
2405  * If it finds a type identical to tp it returns this type.  It turns
2406  * tp into the Id type.  All places formerly pointing to tp will now
2407  * point to the found type.  All entities of tp now refer to the found
2408  * type as their owner, but they are not a member of this type.  This
2409  * is invalid firm -- the entities must be replaced by entities of the
2410  * found type.  The Id type will be removed from the representation
2411  * automatically, but within an unknown time span.  It occupies memory
2412  * for this time.
2413  *
2414  * @param tp     The type to mature.
2415  */
2416 ir_type *    mature_type(ir_type *tp);
2417
2418 /** Finalize type construction.
2419  *
2420  * Indicate that a type is so far completed that it can be
2421  * distinguished from other types.  Mature_type hashes the type into a
2422  * table.  It uses the function in compare_types_func to compare the
2423  * types.
2424  *
2425  * If it finds a type identical to tp it returns this type.  It frees
2426  * type tp and all its entities.
2427  *
2428  * @param tp     The type to mature.
2429  */
2430 ir_type *    mature_type_free(ir_type *tp);
2431
2432 /** Finalize type construction.
2433  *
2434  * Indicate that a type is so far completed that it can be
2435  * distinguished from other types.  Mature_type hashes the type into a
2436  * table.  It uses the function in compare_types_func to compare the
2437  * types.
2438  *
2439  * If it find a type identical to tp it returns this type.  It frees
2440  * the entities and turns the type into an Id type.  All places
2441  * formerly pointing to tp will now point to the found type.  The Id
2442  * type will be removed from the representation automatically, but
2443  * within an unknown time span.  It occupies memory for this time.
2444  *
2445  * @param tp     The type to mature.
2446  */
2447 ir_type *    mature_type_free_entities(ir_type *tp);
2448
2449 /**
2450  * The interface type for the type identify module;
2451  */
2452 typedef struct _type_identify_if_t {
2453         compare_types_func_t *cmp;    /**< The function that should be used to compare two types.
2454                                            If NULL, compare_strict() will be used. */
2455         hash_types_func_t *hash;      /**< The function that should be used to calculate a hash
2456                                            value of a type. If NULL, hash_name() will be used. */
2457 } type_identify_if_t;
2458
2459 /**
2460  * Initialise the type identifier module.
2461  *
2462  * @param ti_if    The interface functions for this module.
2463  *
2464  * If the parameter ti_if is NULL, the default functions compare_strict() and
2465  * firm_hash_name() will be used.
2466  */
2467 void init_type_identify(type_identify_if_t *ti_if);
2468
2469 /** A data type to treat types and entities as the same. */
2470 typedef union {
2471   ir_type   *typ;   /**< points to a type */
2472   ir_entity *ent;   /**< points to an entity */
2473 } type_or_ent;
2474
2475 /** Type of argument functions for type walkers.
2476  *
2477  * @param tore    points to the visited type or entity
2478  * @param env     free environment pointer
2479  */
2480 typedef void type_walk_func(type_or_ent *tore, void *env);
2481
2482 /**  The class walk function
2483  *
2484  * @param clss    points to the visited class
2485  * @param env     free environment pointer
2486  */
2487 typedef void class_walk_func(ir_type *clss, void *env);
2488
2489 /** Touches every type and entity in unspecified order.  If new
2490  *  types/entities are created during the traversal these will
2491  *  be visited, too.
2492  *  Does not touch frame types or types for value params ... */
2493 void type_walk(type_walk_func *pre, type_walk_func *post, void *env);
2494
2495 /** Walks over all type information reachable from an ir graph.
2496  *
2497  *  Walks over all type information reachable from irg, i.e., starts a
2498  *  type walk at the irgs entity, the irgs frame type and all types and
2499  *  entities that are attributes to firm nodes. */
2500 void type_walk_irg(ir_graph *irg, type_walk_func *pre, type_walk_func *post,
2501                    void *env);
2502
2503 /**
2504     Touches every class in specified order:
2505     - first the super class
2506     - second the class itself
2507     - third the sub classes.  If new classes are created
2508     during the traversal these will be visited, too.
2509
2510     @todo should be named class-walk
2511
2512     @deprecated will be removed?
2513 */
2514 void type_walk_super2sub(type_walk_func *pre, type_walk_func *post, void *env);
2515
2516 /** Walker for class types in inheritance order.
2517  *
2518  *  Touches every class in specified order:
2519  *   - first the super class
2520  *   - second the class itself
2521  *   If new classes are created during the traversal these
2522  *   will be visited, too.
2523  * Starts the walk at arbitrary classes.
2524  * Executes pre when first visiting a class.  Executes post after
2525  * visiting all superclasses.
2526  *
2527  * The arguments pre, post, env may be NULL. */
2528 void type_walk_super(type_walk_func *pre, type_walk_func *post, void *env);
2529
2530 /** Same as type_walk_super2sub, but visits only class types.
2531    Executes pre for a class if all superclasses have been visited.
2532    Then iterates to subclasses.  Executes post after return from
2533    subclass.
2534    Does not visit global type, frame types.
2535
2536    @bug ?? something is wrong with this.
2537 */
2538 void class_walk_super2sub(class_walk_func *pre, class_walk_func *post,
2539                           void *env);
2540
2541 /**
2542  * the entity walk function.  A function type for entity walkers.
2543  *
2544  * @param ent     points to the visited entity
2545  * @param env     free environment pointer
2546  */
2547 typedef void entity_walk_func(ir_entity *ent, void *env);
2548
2549 /**
2550  * Walks over all entities in the type.
2551  *
2552  * @param tp    the type
2553  * @param doit  the entity walker function
2554  * @param env   environment, will be passed to the walker function
2555  */
2556 void walk_types_entities(ir_type *tp, entity_walk_func *doit, void *env);
2557
2558 #endif