2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Representation of all program known entities.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
43 #include "irgraph_t.h"
44 #include "callgraph.h"
46 #include "compound_path.h"
48 /*-----------------------------------------------------------------*/
50 /*-----------------------------------------------------------------*/
52 ir_entity *unknown_entity = NULL;
54 ir_entity *get_unknown_entity(void) { return unknown_entity; }
56 /** The name of the unknown entity. */
57 #define UNKNOWN_ENTITY_NAME "unknown_entity"
59 /*-----------------------------------------------------------------*/
61 /*-----------------------------------------------------------------*/
63 static ir_entity *intern_new_entity(ir_type *owner, ident *name, ir_type *type,
68 res = XMALLOCZ(ir_entity);
76 res->volatility = volatility_non_volatile;
77 res->aligned = align_is_aligned;
78 res->usage = ir_usage_unknown;
79 res->compiler_gen = 0;
80 res->visibility = ir_visibility_default;
82 res->offset_bit_remainder = 0;
85 res->repr_class = NULL;
87 res->nr = get_irp_new_node_nr();
90 /* Remember entity in its owner. */
92 add_compound_member(owner, res);
95 set_entity_dbg_info(res, dbgi);
100 ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
103 ir_entity *res = intern_new_entity(owner, name, type, db);
105 if (is_Method_type(type)) {
106 ir_graph *irg = get_const_code_irg();
108 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
110 set_atomic_ent_value(res, new_r_SymConst(irg, mode, sym, symconst_addr_ent));
111 res->linkage = IR_LINKAGE_CONSTANT;
112 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
113 res->attr.mtd_attr.vtable_number = IR_VTABLE_NUM_NOT_SET;
114 res->attr.mtd_attr.param_access = NULL;
115 res->attr.mtd_attr.param_weight = NULL;
116 res->attr.mtd_attr.irg = NULL;
117 } else if (is_compound_type(type)) {
118 res->attr.cmpd_attr.values = NULL;
119 res->attr.cmpd_attr.val_paths = NULL;
120 } else if (is_code_type(type)) {
121 res->attr.code_attr.label = (ir_label_t) -1;
124 hook_new_entity(res);
128 ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
130 return new_d_entity(owner, name, type, NULL);
133 static ident *make_parameter_entity_name(size_t pos)
136 snprintf(buf, sizeof(buf), "parameter.%lu", (unsigned long) pos);
137 return new_id_from_str(buf);
140 ir_entity *new_d_parameter_entity(ir_type *owner, size_t pos, ir_type *type,
143 ident *name = make_parameter_entity_name(pos);
144 ir_entity *res = intern_new_entity(owner, name, type, dbgi);
145 res->is_parameter = true;
146 res->attr.parameter.number = pos;
147 hook_new_entity(res);
151 ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
153 return new_d_parameter_entity(owner, pos, type, NULL);
157 * Free entity attributes.
159 * @param ent the entity
161 static void free_entity_attrs(ir_entity *ent)
163 if (ent->overwrites != NULL) {
164 DEL_ARR_F(ent->overwrites);
165 ent->overwrites = NULL;
167 if (ent->overwrittenby != NULL) {
168 DEL_ARR_F(ent->overwrittenby);
169 ent->overwrittenby = NULL;
172 if (ent->initializer != NULL) {
173 /* TODO: free initializers */
174 } else if (entity_has_compound_ent_values(ent)) {
175 /* can't free compound graph path as it might be used
177 ent->attr.cmpd_attr.val_paths = NULL;
179 if (is_compound_entity(ent)) {
180 ent->attr.cmpd_attr.values = NULL;
181 } else if (is_method_entity(ent)) {
182 if (ent->attr.mtd_attr.param_access) {
183 DEL_ARR_F(ent->attr.mtd_attr.param_access);
184 ent->attr.mtd_attr.param_access = NULL;
186 if (ent->attr.mtd_attr.param_weight) {
187 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
188 ent->attr.mtd_attr.param_weight = NULL;
194 * Creates a deep copy of an entity.
196 static ir_entity *deep_entity_copy(ir_entity *old)
198 ir_entity *newe = XMALLOC(ir_entity);
201 if (old->initializer != NULL) {
202 /* FIXME: the initializers are NOT copied */
203 } else if (entity_has_compound_ent_values(old)) {
204 newe->attr.cmpd_attr.values = NULL;
205 newe->attr.cmpd_attr.val_paths = NULL;
206 if (old->attr.cmpd_attr.values)
207 newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
209 /* FIXME: the compound graph paths are NOT copied */
210 if (old->attr.cmpd_attr.val_paths)
211 newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
212 } else if (is_method_entity(old)) {
213 /* do NOT copy them, reanalyze. This might be the best solution */
214 newe->attr.mtd_attr.param_access = NULL;
215 newe->attr.mtd_attr.param_weight = NULL;
217 newe->overwrites = NULL;
218 newe->overwrittenby = NULL;
221 newe->nr = get_irp_new_node_nr();
223 hook_new_entity(newe);
228 * Copies the entity if the new_owner is different from the
229 * owner of the old entity, else returns the old entity.
231 ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
234 assert(is_entity(old));
235 assert(is_compound_type(new_owner));
236 assert(get_type_state(new_owner) != layout_fixed);
238 if (old->owner == new_owner)
241 /* create a deep copy so we are safe of aliasing and double-freeing. */
242 newe = deep_entity_copy(old);
243 newe->owner = new_owner;
244 add_compound_member(new_owner, newe);
249 ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
252 assert(old && old->kind == k_entity);
254 if (old->name == new_name)
257 newe = deep_entity_copy(old);
258 newe->name = new_name;
259 newe->ld_name = NULL;
260 add_compound_member(old->owner, newe);
265 void free_entity(ir_entity *ent)
267 if (ent->owner != NULL && !is_Array_type(ent->owner))
268 remove_compound_member(ent->owner, ent);
270 assert(ent && ent->kind == k_entity);
271 free_entity_attrs(ent);
276 /* Outputs a unique number for this node */
277 long get_entity_nr(const ir_entity *ent)
279 assert(ent && ent->kind == k_entity);
283 return (long)PTR_TO_INT(ent);
287 const char *(get_entity_name)(const ir_entity *ent)
289 return _get_entity_name(ent);
292 ident *(get_entity_ident)(const ir_entity *ent)
294 return _get_entity_ident(ent);
297 void (set_entity_ident)(ir_entity *ent, ident *id)
299 _set_entity_ident(ent, id);
302 ir_type *(get_entity_owner)(const ir_entity *ent)
304 return _get_entity_owner(ent);
307 void set_entity_owner(ir_entity *ent, ir_type *owner)
309 assert(is_entity(ent));
310 assert(is_compound_type(owner));
312 remove_compound_member(ent->owner, ent);
313 add_compound_member(owner, ent);
317 ident *(get_entity_ld_ident)(const ir_entity *ent)
319 return _get_entity_ld_ident(ent);
322 void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
324 _set_entity_ld_ident(ent, ld_ident);
327 const char *(get_entity_ld_name)(const ir_entity *ent)
329 return _get_entity_ld_name(ent);
332 int entity_has_ld_ident(const ir_entity *entity)
334 return entity->ld_name != NULL;
337 ir_type *(get_entity_type)(const ir_entity *ent)
339 return _get_entity_type(ent);
342 void (set_entity_type)(ir_entity *ent, ir_type *type)
344 _set_entity_type(ent, type);
347 ir_volatility (get_entity_volatility)(const ir_entity *ent)
349 return _get_entity_volatility(ent);
352 void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
354 _set_entity_volatility(ent, vol);
357 /* Return the name of the volatility. */
358 const char *get_volatility_name(ir_volatility var)
360 #define X(a) case a: return #a
362 X(volatility_non_volatile);
363 X(volatility_is_volatile);
364 default: return "BAD VALUE";
369 ir_align (get_entity_aligned)(const ir_entity *ent)
371 return _get_entity_aligned(ent);
374 void (set_entity_aligned)(ir_entity *ent, ir_align a)
376 _set_entity_aligned(ent, a);
379 unsigned (get_entity_alignment)(const ir_entity *ent)
381 return _get_entity_alignment(ent);
384 void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
386 _set_entity_alignment(ent, alignment);
389 /* Return the name of the alignment. */
390 const char *get_align_name(ir_align a)
392 #define X(a) case a: return #a
394 X(align_non_aligned);
396 default: return "BAD VALUE";
401 void set_entity_label(ir_entity *ent, ir_label_t label)
403 ent->attr.code_attr.label = label;
406 ir_label_t get_entity_label(const ir_entity *ent)
408 return ent->attr.code_attr.label;
411 static void verify_visibility(const ir_entity *entity)
413 if (get_entity_visibility(entity) == ir_visibility_external
414 && !is_method_entity(entity)) {
415 assert(!entity_has_definition(entity));
419 void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
421 entity->visibility = visibility;
422 verify_visibility(entity);
425 ir_visibility get_entity_visibility(const ir_entity *entity)
427 return (ir_visibility)entity->visibility;
430 void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
432 entity->linkage = linkage;
435 ir_linkage (get_entity_linkage)(const ir_entity *entity)
437 return get_entity_linkage(entity);
440 void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
442 entity->linkage |= linkage;
445 void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
447 entity->linkage &= ~linkage;
450 /* Checks if an entity is compiler generated */
451 int (is_entity_compiler_generated)(const ir_entity *ent)
453 return _is_entity_compiler_generated(ent);
456 /* Sets/resets the compiler generated flag */
457 void (set_entity_compiler_generated)(ir_entity *ent, int flag)
459 _set_entity_compiler_generated(ent, flag);
462 ir_entity_usage (get_entity_usage)(const ir_entity *ent)
464 return _get_entity_usage(ent);
467 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
469 _set_entity_usage(ent, flags);
472 /* Set has no effect for existent entities of type method. */
473 ir_node *get_atomic_ent_value(ir_entity *entity)
475 ir_initializer_t *initializer = get_entity_initializer(entity);
477 assert(entity && is_atomic_entity(entity));
478 if (initializer == NULL) {
479 ir_type *type = get_entity_type(entity);
480 return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
483 switch (get_initializer_kind(initializer)) {
484 case IR_INITIALIZER_NULL: {
485 ir_type *type = get_entity_type(entity);
486 ir_mode *mode = get_type_mode(type);
487 return new_r_Const(get_const_code_irg(), get_mode_null(mode));
489 case IR_INITIALIZER_TARVAL: {
490 ir_tarval *tv = get_initializer_tarval_value(initializer);
491 return new_r_Const(get_const_code_irg(), tv);
493 case IR_INITIALIZER_CONST:
494 return get_initializer_const_value(initializer);
495 case IR_INITIALIZER_COMPOUND:
496 panic("compound initializer in atomic entity not allowed (%+F)", entity);
499 panic("invalid initializer kind in get_atomic_ent_value(%+F)", entity);
502 void set_atomic_ent_value(ir_entity *entity, ir_node *val)
504 ir_initializer_t *initializer;
506 assert(is_atomic_entity(entity));
508 assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
509 initializer = create_initializer_const(val);
510 entity->initializer = initializer;
513 /* Returns true if the the node is representable as code on
515 int is_irn_const_expression(ir_node *n)
517 /* we are in danger iff an exception will arise. TODO: be more precisely,
518 * for instance Div. will NOT rise if divisor != 0
520 if (is_binop(n) && !is_fragile_op(n))
521 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
523 switch (get_irn_opcode(n)) {
530 return is_irn_const_expression(get_irn_n(n, 0));
538 * Copies a firm subgraph that complies to the restrictions for
539 * constant expressions to block.
541 ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
543 ir_graph *irg = get_irn_irg(block);
547 /* @@@ GL I think we should implement this using the routines from irgopt for
548 dead node elimination/inlineing. */
551 switch (get_irn_opcode(n)) {
553 nn = new_rd_Const(dbg, irg, get_Const_tarval(n));
556 nn = new_rd_SymConst(dbg, irg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n));
559 nn = new_rd_Add(dbg, block,
560 copy_const_value(dbg, get_Add_left(n), block),
561 copy_const_value(dbg, get_Add_right(n), block), m);
564 nn = new_rd_Sub(dbg, block,
565 copy_const_value(dbg, get_Sub_left(n), block),
566 copy_const_value(dbg, get_Sub_right(n), block), m);
569 nn = new_rd_Mul(dbg, block,
570 copy_const_value(dbg, get_Mul_left(n), block),
571 copy_const_value(dbg, get_Mul_right(n), block), m);
574 nn = new_rd_And(dbg, block,
575 copy_const_value(dbg, get_And_left(n), block),
576 copy_const_value(dbg, get_And_right(n), block), m);
579 nn = new_rd_Or(dbg, block,
580 copy_const_value(dbg, get_Or_left(n), block),
581 copy_const_value(dbg, get_Or_right(n), block), m);
584 nn = new_rd_Eor(dbg, block,
585 copy_const_value(dbg, get_Eor_left(n), block),
586 copy_const_value(dbg, get_Eor_right(n), block), m);
589 nn = new_rd_Cast(dbg, block,
590 copy_const_value(dbg, get_Cast_op(n), block),
594 nn = new_rd_Conv(dbg, block,
595 copy_const_value(dbg, get_Conv_op(n), block), m);
598 nn = new_r_Unknown(irg, m); break;
600 panic("opcode invalid or not implemented");
605 /** Return the name of the initializer kind. */
606 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
608 #define X(a) case a: return #a
610 X(IR_INITIALIZER_CONST);
611 X(IR_INITIALIZER_TARVAL);
612 X(IR_INITIALIZER_NULL);
613 X(IR_INITIALIZER_COMPOUND);
614 default: return "BAD VALUE";
619 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
621 ir_initializer_t *get_initializer_null(void)
623 return &null_initializer;
626 ir_initializer_t *create_initializer_const(ir_node *value)
628 struct obstack *obst = get_irg_obstack(get_const_code_irg());
630 ir_initializer_t *initializer
631 = (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
632 initializer->kind = IR_INITIALIZER_CONST;
633 initializer->consti.value = value;
638 ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
640 struct obstack *obst = get_irg_obstack(get_const_code_irg());
642 ir_initializer_t *initializer
643 = (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
644 initializer->kind = IR_INITIALIZER_TARVAL;
645 initializer->tarval.value = tv;
650 ir_initializer_t *create_initializer_compound(size_t n_entries)
652 struct obstack *obst = get_irg_obstack(get_const_code_irg());
655 size_t size = sizeof(ir_initializer_compound_t)
656 + n_entries * sizeof(ir_initializer_t*)
657 - sizeof(ir_initializer_t*);
659 ir_initializer_t *initializer
660 = (ir_initializer_t*)obstack_alloc(obst, size);
661 initializer->kind = IR_INITIALIZER_COMPOUND;
662 initializer->compound.n_initializers = n_entries;
664 for (i = 0; i < n_entries; ++i) {
665 initializer->compound.initializers[i] = get_initializer_null();
671 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
673 assert(initializer->kind == IR_INITIALIZER_CONST);
674 return skip_Id(initializer->consti.value);
677 ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
679 assert(initializer->kind == IR_INITIALIZER_TARVAL);
680 return initializer->tarval.value;
683 size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
685 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
686 return initializer->compound.n_initializers;
689 void set_initializer_compound_value(ir_initializer_t *initializer,
690 size_t index, ir_initializer_t *value)
692 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
693 assert(index < initializer->compound.n_initializers);
695 initializer->compound.initializers[index] = value;
698 ir_initializer_t *get_initializer_compound_value(
699 const ir_initializer_t *initializer, size_t index)
701 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
702 assert(index < initializer->compound.n_initializers);
704 return initializer->compound.initializers[index];
707 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
709 return initializer->kind;
712 static void check_entity_initializer(ir_entity *entity)
715 ir_initializer_t *initializer = entity->initializer;
716 ir_type *entity_tp = get_entity_type(entity);
717 switch (initializer->kind) {
718 case IR_INITIALIZER_COMPOUND:
719 assert(is_compound_type(entity_tp) || is_Array_type(entity_tp));
721 case IR_INITIALIZER_CONST:
722 /* methods are initialized by a SymConst */
723 assert(is_atomic_type(entity_tp) || is_Method_type(entity_tp));
725 case IR_INITIALIZER_TARVAL:
726 assert(is_atomic_type(entity_tp));
728 case IR_INITIALIZER_NULL:
734 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
736 entity->initializer = initializer;
737 check_entity_initializer(entity);
740 int has_entity_initializer(const ir_entity *entity)
742 return entity->initializer != NULL;
745 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
747 return entity->initializer;
750 int (get_entity_offset)(const ir_entity *ent)
752 return _get_entity_offset(ent);
755 void (set_entity_offset)(ir_entity *ent, int offset)
757 _set_entity_offset(ent, offset);
760 unsigned char (get_entity_offset_bits_remainder)(const ir_entity *ent)
762 return _get_entity_offset_bits_remainder(ent);
765 void (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset)
767 _set_entity_offset_bits_remainder(ent, offset);
770 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
772 if (ent->overwrites == NULL) {
773 ent->overwrites = NEW_ARR_F(ir_entity*, 0);
775 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
776 if (overwritten->overwrittenby == NULL) {
777 overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
779 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
782 size_t get_entity_n_overwrites(const ir_entity *ent)
784 if (ent->overwrites == NULL)
786 return ARR_LEN(ent->overwrites);
789 size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
792 size_t n = get_entity_n_overwrites(ent);
793 for (i = 0; i < n; ++i) {
794 if (get_entity_overwrites(ent, i) == overwritten)
800 ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
802 assert(pos < get_entity_n_overwrites(ent));
803 return ent->overwrites[pos];
806 void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
808 assert(pos < get_entity_n_overwrites(ent));
809 ent->overwrites[pos] = overwritten;
812 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
815 size_t n = get_entity_n_overwrites(ent);
816 for (i = 0; i < n; ++i) {
817 if (ent->overwrites[i] == overwritten) {
818 for (; i < n - 1; i++)
819 ent->overwrites[i] = ent->overwrites[i+1];
820 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
827 size_t get_entity_n_overwrittenby(const ir_entity *ent)
829 if (ent->overwrittenby == NULL)
831 return ARR_LEN(ent->overwrittenby);
834 size_t get_entity_overwrittenby_index(const ir_entity *ent,
835 ir_entity *overwrites)
838 size_t n = get_entity_n_overwrittenby(ent);
839 for (i = 0; i < n; ++i) {
840 if (get_entity_overwrittenby(ent, i) == overwrites)
846 ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
848 assert(pos < get_entity_n_overwrittenby(ent));
849 return ent->overwrittenby[pos];
852 void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
854 assert(pos < get_entity_n_overwrittenby(ent));
855 ent->overwrittenby[pos] = overwrites;
858 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
861 size_t n = get_entity_n_overwrittenby(ent);
862 for (i = 0; i < n; ++i) {
863 if (ent->overwrittenby[i] == overwrites) {
864 for (; i < n - 1; ++i)
865 ent->overwrittenby[i] = ent->overwrittenby[i+1];
866 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
872 void *(get_entity_link)(const ir_entity *ent)
874 return _get_entity_link(ent);
877 void (set_entity_link)(ir_entity *ent, void *l)
879 _set_entity_link(ent, l);
882 ir_graph *(get_entity_irg)(const ir_entity *ent)
884 return _get_entity_irg(ent);
887 void set_entity_irg(ir_entity *ent, ir_graph *irg)
889 assert(is_method_entity(ent));
890 assert(get_entity_peculiarity(ent) == peculiarity_existent);
891 ent->attr.mtd_attr.irg = irg;
894 int (is_parameter_entity)(const ir_entity *entity)
896 return _is_parameter_entity(entity);
899 size_t (get_entity_parameter_number)(const ir_entity *entity)
901 return _get_entity_parameter_number(entity);
904 void set_entity_parameter_number(ir_entity *entity, size_t n)
906 assert(is_parameter_entity(entity));
907 entity->attr.parameter.number = n;
910 unsigned get_entity_vtable_number(const ir_entity *ent)
912 assert(is_method_entity((ir_entity *)ent));
913 return ent->attr.mtd_attr.vtable_number;
916 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
918 assert(is_method_entity(ent));
919 ent->attr.mtd_attr.vtable_number = vtable_number;
922 int (is_entity)(const void *thing)
924 return _is_entity(thing);
927 int is_atomic_entity(const ir_entity *ent)
929 ir_type *t = get_entity_type(ent);
930 const tp_op *op = get_type_tpop(t);
931 return (op == type_primitive || op == type_pointer ||
932 op == type_enumeration || op == type_method);
935 int is_compound_entity(const ir_entity *ent)
937 ir_type *t = get_entity_type(ent);
938 const tp_op *op = get_type_tpop(t);
939 return (op == type_class || op == type_struct ||
940 op == type_array || op == type_union);
943 int is_method_entity(const ir_entity *ent)
945 ir_type *t = get_entity_type(ent);
946 return is_Method_type(t);
949 ir_visited_t (get_entity_visited)(const ir_entity *ent)
951 return _get_entity_visited(ent);
954 void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
956 _set_entity_visited(ent, num);
959 void (mark_entity_visited)(ir_entity *ent)
961 _mark_entity_visited(ent);
964 int (entity_visited)(const ir_entity *ent)
966 return _entity_visited(ent);
969 int (entity_not_visited)(const ir_entity *ent)
971 return _entity_not_visited(ent);
974 mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
978 assert(is_method_entity(ent));
980 /* first check, if the graph has additional properties */
981 irg = get_entity_irg(ent);
984 return get_irg_additional_properties(irg);
986 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
987 return get_method_additional_properties(get_entity_type(ent));
989 return ent->attr.mtd_attr.irg_add_properties;
992 void set_entity_additional_properties(ir_entity *ent, mtp_additional_properties property_mask)
996 assert(is_method_entity(ent));
998 /* first check, if the graph exists */
999 irg = get_entity_irg(ent);
1001 set_irg_additional_properties(irg, property_mask);
1003 /* do not allow to set the mtp_property_inherited flag or
1004 * the automatic inheritance of flags will not work */
1005 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1009 void add_entity_additional_properties(ir_entity *ent, mtp_additional_properties properties)
1013 assert(is_method_entity(ent));
1015 /* first check, if the graph exists */
1016 irg = get_entity_irg(ent);
1018 add_irg_additional_properties(irg, properties);
1020 mtp_additional_properties mask = ent->attr.mtd_attr.irg_add_properties;
1022 if (mask & mtp_property_inherited)
1023 mask = get_method_additional_properties(get_entity_type(ent));
1025 /* do not allow to set the mtp_property_inherited flag or
1026 * the automatic inheritance of flags will not work */
1027 ent->attr.mtd_attr.irg_add_properties = mask | (properties & ~mtp_property_inherited);
1031 /* Returns the class type that this type info entity represents or NULL
1032 if ent is no type info entity. */
1033 ir_type *(get_entity_repr_class)(const ir_entity *ent)
1035 return _get_entity_repr_class(ent);
1038 dbg_info *(get_entity_dbg_info)(const ir_entity *ent)
1040 return _get_entity_dbg_info(ent);
1043 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db)
1045 _set_entity_dbg_info(ent, db);
1048 int entity_is_externally_visible(const ir_entity *entity)
1050 return get_entity_visibility(entity) != ir_visibility_local
1051 || (get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER);
1054 int entity_has_definition(const ir_entity *entity)
1056 return entity->initializer != NULL
1057 || get_entity_irg(entity) != NULL
1058 || entity_has_compound_ent_values(entity);
1061 void ir_init_entity(void)
1063 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1064 assert(!unknown_entity && "Call firm_init_entity() only once!");
1066 unknown_entity = new_d_entity(NULL, new_id_from_str(UNKNOWN_ENTITY_NAME),
1067 firm_unknown_type, NULL);
1068 set_entity_visibility(unknown_entity, ir_visibility_external);
1069 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1072 void ir_finish_entity(void)
1074 if (unknown_entity != NULL) {
1075 free_entity(unknown_entity);
1076 unknown_entity = NULL;
1080 ir_allocation get_entity_allocation(const ir_entity *entity)
1082 return (ir_allocation)entity->allocation;
1085 void set_entity_allocation(ir_entity *entity, ir_allocation allocation)
1087 entity->allocation = allocation;
1090 ir_peculiarity get_entity_peculiarity(const ir_entity *entity)
1092 return (ir_peculiarity)entity->peculiarity;
1095 void set_entity_peculiarity(ir_entity *entity, ir_peculiarity peculiarity)
1097 entity->peculiarity = peculiarity;
1100 void set_entity_final(ir_entity *entity, int final)
1102 entity->final = final;
1105 int is_entity_final(const ir_entity *entity)
1107 return entity->final;