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"
38 * in RELEASE mode, returns ret if the expression expr evaluates to zero
39 * in ASSERT mode, asserts the expression expr (and the string string).
41 #define ASSERT_AND_RET(expr, string, ret) if (!(expr)) return (ret)
44 * in RELEASE mode, returns ret if the expression expr evaluates to zero
45 * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
47 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) if (!(expr)) return (ret)
49 #define ASSERT_AND_RET(expr, string, ret) \
51 if (opt_do_node_verification == FIRM_VERIFICATION_ON) {\
52 assert((expr) && string); } \
54 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
55 fprintf(stderr, #expr " : " string "\n"); \
56 firm_verify_failure_msg = #expr " && " string; \
61 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
64 firm_verify_failure_msg = #expr " && " string; \
65 if (opt_do_node_verification != FIRM_VERIFICATION_ERROR_ONLY) { blk; } \
66 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
67 fprintf(stderr, #expr " : " string "\n"); \
68 else if (opt_do_node_verification == FIRM_VERIFICATION_ON) { \
69 assert((expr) && string); \
79 static const char *firm_verify_failure_msg;
83 * Show diagnostic if an entity overwrites another one not
84 * in direct superclasses.
86 static void show_ent_not_supertp(ir_entity *ent, ir_entity *ovw)
88 ir_type *owner = get_entity_owner(ent);
89 ir_type *ov_own = get_entity_owner(ovw);
92 fprintf(stderr, "Type verification error:\n");
93 ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
94 ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
96 ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
97 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
98 ir_type *super = get_class_supertype(owner, i);
99 ir_fprintf(stderr, " %+F:\n", super);
105 * Show diagnostic if an entity overwrites a wrong number of things.
107 static void show_ent_overwrite_cnt(ir_entity *ent)
109 ir_type *owner = get_entity_owner(ent);
114 bool show_stp = false;
116 fprintf(stderr, "Type verification error:\n");
117 ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
118 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
119 ir_entity *ovw = get_entity_overwrites(ent, i);
120 ir_type *ov_own = get_entity_owner(ovw);
121 size_t n_supertypes = get_class_n_supertypes(owner);
123 ir_fprintf(stderr, " %t::%e\n", ov_own, ovw);
124 for (k = 0; k < i; ++k) {
125 if (ovw == get_entity_overwrites(ent, k)) {
126 ir_fprintf(stderr, " ->%t::%e entered more than once\n", ov_own, ovw);
132 for (j = 0; j < n_supertypes; ++j) {
133 if (ov_own == get_class_supertype(owner, j)) {
134 show_stp = found = true;
139 ir_fprintf(stderr, " ->%t not in super types of %t\n", ov_own, owner);
143 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
144 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
145 ir_type *super = get_class_supertype(owner, i);
146 ir_fprintf(stderr, " %t:\n", super);
151 #endif /* #ifndef NDEBUG */
156 static int check_class(ir_type *tp)
160 for (i = 0, n = get_class_n_members(tp); i < n; ++i) {
161 ir_entity *mem = get_class_member(tp, i);
164 tp == get_entity_owner(mem),
165 "class member with wrong owner",
166 error_ent_wrong_owner,
167 ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
171 "NULL members not allowed",
173 ir_fprintf(stderr, "Type verification error:\n%+F member %zu is NULL\n", tp, i)
177 get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
178 "wrong number of entity overwrites",
179 error_wrong_ent_overwrites,
180 show_ent_overwrite_cnt(mem)
186 /* check if the overwrite relation is flat, i.e. every overwrite
187 * is visible in every direct superclass. */
188 for (j = 0, m = get_entity_n_overwrites(mem); j < m; ++j) {
189 ir_entity *ovw = get_entity_overwrites(mem, j);
192 /* Check whether ovw is member of one of tp's supertypes. If so,
193 the representation is correct. */
194 for (k = 0, n_super = get_class_n_supertypes(tp); k < n_super; ++k) {
195 if (get_class_member_index(get_class_supertype(tp, k), ovw) != INVALID_MEMBER_INDEX) {
198 "overwrites an entity not contained in direct supertype",
200 show_ent_not_supertp(mem, ovw)
215 static int check_array(const ir_type *tp)
217 size_t i, n_dim = get_array_n_dimensions(tp);
219 for (i = 0; i < n_dim; ++i) {
221 has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
222 "array bound missing",
224 ir_fprintf(stderr, "%+F in dimension %zu\n", tp, i)
234 static int check_primitive(ir_type *tp)
237 is_mode(get_type_mode(tp)),
238 "Primitive type without mode",
240 ir_fprintf(stderr, "%+F\n", tp)
245 int check_type(ir_type *tp)
247 switch (get_type_tpop_code(tp)) {
249 return check_class(tp);
251 return check_array(tp);
253 return check_primitive(tp);
260 * checks the visited flag
262 static int check_visited_flag(ir_graph *irg, ir_node *n)
265 get_irn_visited(n) <= get_irg_visited(irg),
266 "Visited flag of node is larger than that of corresponding irg.",
268 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
274 * helper environment struct for constant_on_wrong_obstack()
276 typedef struct myenv {
282 * called by the walker
284 static void on_irg_storage(ir_node *n, void *data)
286 myenv *env = (myenv*)data;
288 /* We also test whether the setting of the visited flag is legal. */
289 env->res = node_is_in_irgs_storage(env->irg, n) &&
290 check_visited_flag(env->irg, n);
294 * checks whether a given constant IR node is NOT on the
297 static int constant_on_wrong_irg(ir_node *n)
301 env.res = 1; /* on right obstack */
302 env.irg = get_const_code_irg();
304 irg_walk(n, on_irg_storage, NULL, (void *)&env);
308 static int initializer_constant_on_wrong_irg(ir_initializer_t *initializer)
310 switch (get_initializer_kind(initializer)) {
311 case IR_INITIALIZER_NULL:
313 case IR_INITIALIZER_TARVAL:
315 case IR_INITIALIZER_CONST:
316 return constant_on_wrong_irg(get_initializer_const_value(initializer));
317 case IR_INITIALIZER_COMPOUND: {
318 size_t i, n = get_initializer_compound_n_entries(initializer);
319 for (i = 0; i < n; ++i) {
320 ir_initializer_t *sub
321 = get_initializer_compound_value(initializer, i);
322 if (initializer_constant_on_wrong_irg(sub))
328 panic("invalid initializer in initializer_on_wrong_irg");
332 * Check if constants node are NOT on the constant IR graph.
334 * @return NON-zero if an entity initializer constant is NOT on
335 * the current_ir_graph's obstack.
337 static int constants_on_wrong_irg(ir_entity *ent)
339 if (ent->initializer != NULL) {
340 return initializer_constant_on_wrong_irg(ent->initializer);
341 } else if (entity_has_compound_ent_values(ent)) {
343 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
344 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
351 int check_entity(ir_entity *ent)
353 ir_type *tp = get_entity_type(ent);
355 current_ir_graph = get_const_code_irg();
357 constants_on_wrong_irg(ent) == 0,
358 "Contants placed on wrong IRG",
359 error_const_on_wrong_irg,
360 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
363 /* Originally, this test assumed, that only method entities have
364 pecularity_inherited. As I changed this, I have to test for method type
365 before doing the test. */
366 if (get_entity_peculiarity(ent) == peculiarity_existent
367 && is_method_entity(ent)) {
369 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
372 "inherited method entities must have constant pointing to existent entity.",
373 error_inherited_ent_without_const,
374 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
378 if (is_atomic_entity(ent) && ent->initializer != NULL) {
379 ir_mode *mode = NULL;
380 ir_initializer_t *initializer = ent->initializer;
381 switch (initializer->kind) {
382 case IR_INITIALIZER_CONST:
383 mode = get_irn_mode(get_initializer_const_value(initializer));
385 case IR_INITIALIZER_TARVAL:
386 mode = get_tarval_mode(get_initializer_tarval_value(initializer));
388 case IR_INITIALIZER_NULL:
389 case IR_INITIALIZER_COMPOUND:
393 mode == NULL || mode == get_type_mode(tp),
394 "Mode of constant in entity must match type.",
395 error_ent_const_mode,
396 ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
397 ent, tp, get_type_mode(tp))
404 * check types and entities
406 static void check_tore(type_or_ent tore, void *env)
408 int *res = (int*)env;
410 if (is_type(tore.typ)) {
411 *res = check_type(tore.typ);
413 assert(is_entity(tore.ent));
414 *res = check_entity(tore.ent);
420 static ident *empty = NULL;
422 ir_type *constructors;
423 ir_type *destructors;
424 ir_type *thread_locals;
429 empty = new_id_from_chars("", 0);
431 type_walk(check_tore, NULL, &res);
433 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
434 const ir_type *type = get_segment_type(s);
436 for (e = 0; e < get_compound_n_members(type); ++e) {
437 ir_entity *entity = get_compound_member(type, e);
438 ASSERT_AND_RET(get_entity_ld_ident(entity) != NULL ||
439 get_entity_visibility(entity) == ir_visibility_private,
440 "segment members must have a name or visibility_private",
445 constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
446 for (i = 0, n = get_compound_n_members(constructors); i < n; ++i) {
447 const ir_entity *entity = get_compound_member(constructors, i);
448 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
449 "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
451 /* Mach-O doesn't like labels in this section */
452 ASSERT_AND_RET(get_entity_ld_ident(entity),
453 "entity in constructors should have ld_ident=''", 1);
455 destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
456 for (i = 0, n = get_compound_n_members(destructors); i < n; ++i) {
457 const ir_entity *entity = get_compound_member(destructors, i);
458 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
459 "entity without LINKAGE_HIDDEN_USER in destructors is pointless",
461 /* Mach-O doesn't like labels in this section */
462 ASSERT_AND_RET(get_entity_ld_ident(entity),
463 "entity in destructors should have ld_ident=''", 1);
465 thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
466 for (i = 0, n = get_compound_n_members(thread_locals); i < n; ++i) {
467 const ir_entity *entity = get_compound_member(thread_locals, i);
468 /* this is odd and should not be allowed I think */
469 ASSERT_AND_RET(!is_method_entity(entity),
470 "method in THREAD_LOCAL segment", 1);
471 ASSERT_AND_RET(! (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT),
472 "thread locals must not be constant", 1);