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