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