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