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
21 * @file tr_inheritance.c
22 * @brief Check types and entities for correctness.
24 * @author Michael Beck, Goetz Lindenmaier
28 #include "irgraph_t.h"
37 * in RELEASE mode, returns ret if the expression expr evaluates to zero
38 * in ASSERT mode, asserts the expression expr (and the string string).
40 #define ASSERT_AND_RET(expr, string, ret) if (!(expr)) return (ret)
43 * in RELEASE mode, returns ret if the expression expr evaluates to zero
44 * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
46 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) if (!(expr)) return (ret)
48 #define ASSERT_AND_RET(expr, string, ret) \
50 if (opt_do_node_verification == FIRM_VERIFICATION_ON) {\
51 assert((expr) && string); } \
53 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
54 fprintf(stderr, #expr " : " string "\n"); \
55 firm_verify_failure_msg = #expr " && " string; \
60 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
63 firm_verify_failure_msg = #expr " && " string; \
64 if (opt_do_node_verification != FIRM_VERIFICATION_ERROR_ONLY) { blk; } \
65 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
66 fprintf(stderr, #expr " : " string "\n"); \
67 else if (opt_do_node_verification == FIRM_VERIFICATION_ON) { \
68 assert((expr) && string); \
78 static const char *firm_verify_failure_msg;
82 * Show diagnostic if an entity overwrites another one not
83 * in direct superclasses.
85 static void show_ent_not_supertp(ir_entity *ent, ir_entity *ovw)
87 ir_type *owner = get_entity_owner(ent);
88 ir_type *ov_own = get_entity_owner(ovw);
91 fprintf(stderr, "Type verification error:\n");
92 ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
93 ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
95 ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
96 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
97 ir_type *super = get_class_supertype(owner, i);
98 ir_fprintf(stderr, " %+F:\n", super);
104 * Show diagnostic if an entity overwrites a wrong number of things.
106 static void show_ent_overwrite_cnt(ir_entity *ent)
108 ir_type *owner = get_entity_owner(ent);
113 bool show_stp = false;
115 fprintf(stderr, "Type verification error:\n");
116 ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
117 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
118 ir_entity *ovw = get_entity_overwrites(ent, i);
119 ir_type *ov_own = get_entity_owner(ovw);
120 size_t n_supertypes = get_class_n_supertypes(owner);
122 ir_fprintf(stderr, " %t::%e\n", ov_own, ovw);
123 for (k = 0; k < i; ++k) {
124 if (ovw == get_entity_overwrites(ent, k)) {
125 ir_fprintf(stderr, " ->%t::%e entered more than once\n", ov_own, ovw);
131 for (j = 0; j < n_supertypes; ++j) {
132 if (ov_own == get_class_supertype(owner, j)) {
133 show_stp = found = true;
138 ir_fprintf(stderr, " ->%t not in super types of %t\n", ov_own, owner);
142 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
143 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
144 ir_type *super = get_class_supertype(owner, i);
145 ir_fprintf(stderr, " %t:\n", super);
150 #endif /* #ifndef NDEBUG */
155 static int check_class(ir_type *tp)
159 for (i = 0, n = get_class_n_members(tp); i < n; ++i) {
160 ir_entity *mem = get_class_member(tp, i);
163 tp == get_entity_owner(mem),
164 "class member with wrong owner",
165 error_ent_wrong_owner,
166 ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
170 "NULL members not allowed",
172 ir_fprintf(stderr, "Type verification error:\n%+F member %zu is NULL\n", tp, i)
176 get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
177 "wrong number of entity overwrites",
178 error_wrong_ent_overwrites,
179 show_ent_overwrite_cnt(mem)
185 /* check if the overwrite relation is flat, i.e. every overwrite
186 * is visible in every direct superclass. */
187 for (j = 0, m = get_entity_n_overwrites(mem); j < m; ++j) {
188 ir_entity *ovw = get_entity_overwrites(mem, j);
191 /* Check whether ovw is member of one of tp's supertypes. If so,
192 the representation is correct. */
193 for (k = 0, n_super = get_class_n_supertypes(tp); k < n_super; ++k) {
194 if (get_class_member_index(get_class_supertype(tp, k), ovw) != INVALID_MEMBER_INDEX) {
197 "overwrites an entity not contained in direct supertype",
199 show_ent_not_supertp(mem, ovw)
214 static int check_array(const ir_type *tp)
216 size_t i, n_dim = get_array_n_dimensions(tp);
218 for (i = 0; i < n_dim; ++i) {
220 has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
221 "array bound missing",
223 ir_fprintf(stderr, "%+F in dimension %zu\n", tp, i)
233 static int check_primitive(ir_type *tp)
236 is_mode(get_type_mode(tp)),
237 "Primitive type without mode",
239 ir_fprintf(stderr, "%+F\n", tp)
244 int check_type(ir_type *tp)
246 switch (get_type_tpop_code(tp)) {
248 return check_class(tp);
250 return check_array(tp);
252 return check_primitive(tp);
259 * checks the visited flag
261 static int check_visited_flag(ir_graph *irg, ir_node *n)
264 get_irn_visited(n) <= get_irg_visited(irg),
265 "Visited flag of node is larger than that of corresponding irg.",
267 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
273 * helper environment struct for constant_on_wrong_obstack()
275 typedef struct myenv {
281 * called by the walker
283 static void on_irg_storage(ir_node *n, void *data)
285 myenv *env = (myenv*)data;
287 /* We also test whether the setting of the visited flag is legal. */
288 env->res = node_is_in_irgs_storage(env->irg, n) &&
289 check_visited_flag(env->irg, n);
293 * checks whether a given constant IR node is NOT on the
296 static int constant_on_wrong_irg(ir_node *n)
300 env.res = 1; /* on right obstack */
301 env.irg = get_const_code_irg();
303 irg_walk(n, on_irg_storage, NULL, (void *)&env);
307 static int initializer_constant_on_wrong_irg(ir_initializer_t *initializer)
309 switch (get_initializer_kind(initializer)) {
310 case IR_INITIALIZER_NULL:
312 case IR_INITIALIZER_TARVAL:
314 case IR_INITIALIZER_CONST:
315 return constant_on_wrong_irg(get_initializer_const_value(initializer));
316 case IR_INITIALIZER_COMPOUND: {
317 size_t i, n = get_initializer_compound_n_entries(initializer);
318 for (i = 0; i < n; ++i) {
319 ir_initializer_t *sub
320 = get_initializer_compound_value(initializer, i);
321 if (initializer_constant_on_wrong_irg(sub))
327 panic("invalid initializer in initializer_on_wrong_irg");
331 * Check if constants node are NOT on the constant IR graph.
333 * @return NON-zero if an entity initializer constant is NOT on
334 * the current_ir_graph's obstack.
336 static int constants_on_wrong_irg(ir_entity *ent)
338 if (ent->initializer != NULL) {
339 return initializer_constant_on_wrong_irg(ent->initializer);
340 } else if (entity_has_compound_ent_values(ent)) {
342 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
343 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
350 int check_entity(ir_entity *ent)
352 ir_type *tp = get_entity_type(ent);
354 current_ir_graph = get_const_code_irg();
356 constants_on_wrong_irg(ent) == 0,
357 "Contants placed on wrong IRG",
358 error_const_on_wrong_irg,
359 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
362 /* Originally, this test assumed, that only method entities have
363 pecularity_inherited. As I changed this, I have to test for method type
364 before doing the test. */
365 if (get_entity_peculiarity(ent) == peculiarity_existent
366 && is_method_entity(ent)) {
368 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
371 "inherited method entities must have constant pointing to existent entity.",
372 error_inherited_ent_without_const,
373 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
377 if (is_atomic_entity(ent) && ent->initializer != NULL) {
378 ir_mode *mode = NULL;
379 ir_initializer_t *initializer = ent->initializer;
380 switch (initializer->kind) {
381 case IR_INITIALIZER_CONST:
382 mode = get_irn_mode(get_initializer_const_value(initializer));
384 case IR_INITIALIZER_TARVAL:
385 mode = get_tarval_mode(get_initializer_tarval_value(initializer));
387 case IR_INITIALIZER_NULL:
388 case IR_INITIALIZER_COMPOUND:
392 mode == NULL || mode == get_type_mode(tp),
393 "Mode of constant in entity must match type.",
394 error_ent_const_mode,
395 ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
396 ent, tp, get_type_mode(tp))
403 * check types and entities
405 static void check_tore(type_or_ent tore, void *env)
407 int *res = (int*)env;
409 if (is_type(tore.typ)) {
410 *res = check_type(tore.typ);
412 assert(is_entity(tore.ent));
413 *res = check_entity(tore.ent);
419 static ident *empty = NULL;
421 ir_type *constructors;
422 ir_type *destructors;
423 ir_type *thread_locals;
428 empty = new_id_from_chars("", 0);
430 type_walk(check_tore, NULL, &res);
432 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
433 const ir_type *type = get_segment_type(s);
435 for (e = 0; e < get_compound_n_members(type); ++e) {
436 ir_entity *entity = get_compound_member(type, e);
437 ASSERT_AND_RET(get_entity_ld_ident(entity) != NULL ||
438 get_entity_visibility(entity) == ir_visibility_private,
439 "segment members must have a name or visibility_private",
444 constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
445 for (i = 0, n = get_compound_n_members(constructors); i < n; ++i) {
446 const ir_entity *entity = get_compound_member(constructors, i);
447 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
448 "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
450 /* Mach-O doesn't like labels in this section */
451 ASSERT_AND_RET(get_entity_ld_ident(entity),
452 "entity in constructors should have ld_ident=''", 1);
454 destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
455 for (i = 0, n = get_compound_n_members(destructors); i < n; ++i) {
456 const ir_entity *entity = get_compound_member(destructors, i);
457 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
458 "entity without LINKAGE_HIDDEN_USER in destructors is pointless",
460 /* Mach-O doesn't like labels in this section */
461 ASSERT_AND_RET(get_entity_ld_ident(entity),
462 "entity in destructors should have ld_ident=''", 1);
464 thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
465 for (i = 0, n = get_compound_n_members(thread_locals); i < n; ++i) {
466 const ir_entity *entity = get_compound_member(thread_locals, i);
467 /* this is odd and should not be allowed I think */
468 ASSERT_AND_RET(!is_method_entity(entity),
469 "method in THREAD_LOCAL segment", 1);
470 ASSERT_AND_RET(! (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT),
471 "thread locals must not be constant", 1);