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