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