2 * Copyright (C) 1995-2008 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
29 #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;
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);
103 * Show diagnostic if an entity overwrites a wrong number of things.
105 static void show_ent_overwrite_cnt(ir_entity *ent)
107 ir_type *owner = get_entity_owner(ent);
112 bool show_stp = false;
114 fprintf(stderr, "Type verification error:\n");
115 ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
116 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
117 ir_entity *ovw = get_entity_overwrites(ent, i);
118 ir_type *ov_own = get_entity_owner(ovw);
119 size_t n_supertypes = get_class_n_supertypes(owner);
121 ir_fprintf(stderr, " %t::%e\n", ov_own, ovw);
122 for (k = 0; k < i; ++k) {
123 if (ovw == get_entity_overwrites(ent, k)) {
124 ir_fprintf(stderr, " ->%t::%e entered more than once\n", ov_own, ovw);
130 for (j = 0; j < n_supertypes; ++j) {
131 if (ov_own == get_class_supertype(owner, j)) {
132 show_stp = found = true;
137 ir_fprintf(stderr, " ->%t not in super types of %t\n", ov_own, owner);
141 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
142 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
143 ir_type *super = get_class_supertype(owner, i);
144 ir_fprintf(stderr, " %t:\n", super);
149 #endif /* #ifndef NDEBUG */
154 static int check_class(ir_type *tp)
159 for (i = get_class_n_members(tp) - 1; i >= 0; --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 %d 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)
182 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
183 ir_entity *ovw = get_entity_overwrites(mem, j);
184 /*printf(" overwrites: "); DDME(ovw);*/
185 /* Check whether ovw is member of one of tp's supertypes. If so,
186 the representation is correct. */
188 for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
189 if (get_class_member_index(get_class_supertype(tp, k), ovw) != (size_t)-1) {
196 "overwrites an entity not contained in direct supertype",
198 show_ent_not_supertp(mem, ovw)
208 static int check_array(ir_type *tp)
210 int i, n_dim = get_array_n_dimensions(tp);
212 for (i = 0; i < n_dim; ++i) {
214 has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
215 "array bound missing",
217 ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
227 static int check_primitive(ir_type *tp)
230 is_mode(get_type_mode(tp)),
231 "Primitive type without mode",
233 ir_fprintf(stderr, "%+F\n", tp)
243 * 0 if no error encountered
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 int n = get_initializer_compound_n_entries(initializer);
320 for (i = 0; i < n; ++i) {
321 ir_initializer_t *sub
322 = get_initializer_compound_value(initializer, i);
323 if (initializer_constant_on_wrong_irg(sub))
329 panic("invalid initializer in initializer_on_wrong_irg");
333 * Check if constants node are NOT on the constant IR graph.
335 * @return NON-zero if an entity initializer constant is NOT on
336 * the current_ir_graph's obstack.
338 static int constants_on_wrong_irg(ir_entity *ent)
340 if (ent->initializer != NULL) {
341 return initializer_constant_on_wrong_irg(ent->initializer);
342 } else if (entity_has_compound_ent_values(ent)) {
344 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
345 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
353 * Check an entity. Currently, we check only if initialized constants
354 * are build on the const irg graph.
357 * 0 if no error encountered
358 * != 0 a trverify_error_codes code
360 int check_entity(ir_entity *ent)
362 ir_type *tp = get_entity_type(ent);
364 current_ir_graph = get_const_code_irg();
366 constants_on_wrong_irg(ent) == 0,
367 "Contants placed on wrong IRG",
368 error_const_on_wrong_irg,
369 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
372 /* Originally, this test assumed, that only method entities have
373 pecularity_inherited. As I changed this, I have to test for method type
374 before doing the test. */
375 if (get_entity_peculiarity(ent) == peculiarity_existent
376 && is_method_entity(ent)) {
378 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
381 "inherited method entities must have constant pointing to existent entity.",
382 error_inherited_ent_without_const,
383 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
387 if (is_atomic_entity(ent) && ent->initializer != NULL) {
388 ir_mode *mode = NULL;
389 ir_initializer_t *initializer = ent->initializer;
390 switch (initializer->kind) {
391 case IR_INITIALIZER_CONST:
392 mode = get_irn_mode(get_initializer_const_value(initializer));
394 case IR_INITIALIZER_TARVAL:
395 mode = get_tarval_mode(get_initializer_tarval_value(initializer));
397 case IR_INITIALIZER_NULL:
398 case IR_INITIALIZER_COMPOUND:
402 mode == NULL || mode == get_type_mode(tp),
403 "Mode of constant in entity must match type.",
404 error_ent_const_mode,
405 ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
406 ent, tp, get_type_mode(tp))
413 * check types and entities
415 static void check_tore(type_or_ent tore, void *env)
417 int *res = (int*)env;
419 if (is_type(tore.typ)) {
420 *res = check_type(tore.typ);
422 assert(is_entity(tore.ent));
423 *res = check_entity(tore.ent);
428 * Verify types and entities.
432 static ident *empty = NULL;
434 ir_type *constructors;
435 ir_type *destructors;
436 ir_type *thread_locals;
441 empty = new_id_from_chars("", 0);
443 type_walk(check_tore, NULL, &res);
445 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
446 const ir_type *type = get_segment_type(s);
448 for (e = 0; e < get_compound_n_members(type); ++e) {
449 ir_entity *entity = get_compound_member(type, e);
450 ASSERT_AND_RET(get_entity_ld_ident(entity) != NULL ||
451 get_entity_visibility(entity) == ir_visibility_private,
452 "segment members must have a name or visibility_private",
457 constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
458 for (i = get_compound_n_members(constructors)-1; i >= 0; --i) {
459 const ir_entity *entity = get_compound_member(constructors, i);
460 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
461 "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
463 /* Mach-O doesn't like labels in this section */
464 ASSERT_AND_RET(get_entity_ld_ident(entity),
465 "entity in constructors should have ld_ident=''", 1);
467 destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
468 for (i = get_compound_n_members(destructors)-1; i >= 0; --i) {
469 const ir_entity *entity = get_compound_member(destructors, i);
470 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
471 "entity without LINKAGE_HIDDEN_USER in destructors is pointless",
473 /* Mach-O doesn't like labels in this section */
474 ASSERT_AND_RET(get_entity_ld_ident(entity),
475 "entity in destructors should have ld_ident=''", 1);
477 thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
478 for (i = get_compound_n_members(thread_locals)-1; i >= 0; --i) {
479 const ir_entity *entity = get_compound_member(thread_locals, i);
480 /* this is odd and should not be allowed I think */
481 ASSERT_AND_RET(!is_method_entity(entity),
482 "method in THREAD_LOCAL segment", 1);
483 ASSERT_AND_RET(! (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT),
484 "thread locals must not be constant", 1);