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_vrfy_failure_msg = #expr " && " string; \
61 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
64 firm_vrfy_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_vrfy_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) {
86 ir_type *owner = get_entity_owner(ent);
87 ir_type *ov_own = get_entity_owner(ovw);
90 fprintf(stderr, "Type verification error:\n");
91 ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
92 ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
94 ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
95 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
96 ir_type *super = get_class_supertype(owner, i);
97 ir_fprintf(stderr, " %+F:\n", super);
102 * Show diagnostic if an entity overwrites a wrong number of things.
104 static void show_ent_overwrite_cnt(ir_entity *ent) {
105 ir_type *owner = get_entity_owner(ent);
106 int i, j, k, found, show_stp = 0;
108 fprintf(stderr, "Type verification error:\n");
109 ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
110 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
111 ir_entity *ovw = get_entity_overwrites(ent, i);
112 ir_type *ov_own = get_entity_owner(ovw);
114 ir_fprintf(stderr, " %t::%e\n", ov_own, ovw);
115 for (k = 0; k < i; ++k)
116 if (ovw == get_entity_overwrites(ent, k)) {
117 ir_fprintf(stderr, " ->%t::%e entered more than once\n", ov_own, ovw);
122 for (j = get_class_n_supertypes(owner) - 1; j >= 0; --j) {
123 if (ov_own == get_class_supertype(owner, j)) {
124 show_stp = found = 1;
129 ir_fprintf(stderr, " ->%t not in super types of %t\n", ov_own, owner);
133 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
134 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
135 ir_type *super = get_class_supertype(owner, i);
136 ir_fprintf(stderr, " %t:\n", super);
141 #endif /* #ifndef NDEBUG */
146 static int check_class(ir_type *tp) {
150 for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
151 ir_entity *mem = get_class_member(tp, i);
154 tp == get_entity_owner(mem),
155 "class member with wrong owner",
156 error_ent_wrong_owner,
157 ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
161 "NULL members not allowed",
163 ir_fprintf(stderr, "Type verification error:\n%+F member %d is NULL\n", tp, i)
167 get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
168 "wrong number of entity overwrites",
169 error_wrong_ent_overwrites,
170 show_ent_overwrite_cnt(mem)
173 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
174 ir_entity *ovw = get_entity_overwrites(mem, j);
175 /*printf(" overwrites: "); DDME(ovw);*/
176 /* Check whether ovw is member of one of tp's supertypes. If so,
177 the representation is correct. */
179 for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
180 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
187 "overwrites an entity not contained in direct supertype",
189 show_ent_not_supertp(mem, ovw)
199 static int check_array(ir_type *tp) {
200 int i, n_dim = get_array_n_dimensions(tp);
202 for (i = 0; i < n_dim; ++i) {
204 has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
205 "array bound missing",
207 ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
217 static int check_primitive(ir_type *tp) {
219 is_mode(get_type_mode(tp)),
220 "Primitive type without mode",
222 ir_fprintf(stderr, "%+F\n", tp)
232 * 0 if no error encountered
234 int check_type(ir_type *tp) {
235 switch (get_type_tpop_code(tp)) {
237 return check_class(tp);
239 return check_array(tp);
241 return check_primitive(tp);
248 * checks the visited flag
250 static int check_visited_flag(ir_graph *irg, ir_node *n) {
252 get_irn_visited(n) <= get_irg_visited(irg),
253 "Visited flag of node is larger than that of corresponding irg.",
255 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
261 * helper environment struct for constant_on_wrong_obstack()
269 * called by the walker
271 static void on_irg_storage(ir_node *n, void *env) {
272 struct myenv *myenv = env;
274 /* We also test whether the setting of the visited flag is legal. */
275 myenv->res = node_is_in_irgs_storage(myenv->irg, n) &&
276 check_visited_flag(myenv->irg, n);
280 * checks whether a given constant IR node is NOT on the
283 static int constant_on_wrong_irg(ir_node *n)
287 env.res = 1; /* on right obstack */
288 env.irg = get_const_code_irg();
290 irg_walk(n, on_irg_storage, NULL, (void *)&env);
294 static int initializer_constant_on_wrong_irg(ir_initializer_t *initializer)
296 switch (get_initializer_kind(initializer)) {
297 case IR_INITIALIZER_NULL:
299 case IR_INITIALIZER_TARVAL:
301 case IR_INITIALIZER_CONST:
302 return constant_on_wrong_irg(get_initializer_const_value(initializer));
303 case IR_INITIALIZER_COMPOUND: {
304 int n = get_initializer_compound_n_entries(initializer);
306 for (i = 0; i < n; ++i) {
307 ir_initializer_t *sub
308 = get_initializer_compound_value(initializer, i);
309 if (initializer_constant_on_wrong_irg(sub))
315 panic("invalid initializer in initializer_on_wrong_irg");
319 * Check if constants node are NOT on the constant IR graph.
321 * @return NON-zero if an entity initializer constant is NOT on
322 * the current_ir_graph's obstack.
324 static int constants_on_wrong_irg(ir_entity *ent)
326 if (ent->initializer != NULL) {
327 return initializer_constant_on_wrong_irg(ent->initializer);
328 } else if (entity_has_compound_ent_values(ent)) {
330 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
331 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
339 * Check an entity. Currently, we check only if initialized constants
340 * are build on the const irg graph.
343 * 0 if no error encountered
344 * != 0 a trvrfy_error_codes code
346 int check_entity(ir_entity *ent)
348 ir_type *tp = get_entity_type(ent);
350 current_ir_graph = get_const_code_irg();
352 constants_on_wrong_irg(ent) == 0,
353 "Contants placed on wrong IRG",
354 error_const_on_wrong_irg,
355 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
358 /* Originally, this test assumed, that only method entities have
359 pecularity_inherited. As I changed this, I have to test for method type
360 before doing the test. */
361 if (get_entity_peculiarity(ent) == peculiarity_existent
362 && is_method_entity(ent)) {
364 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
367 "inherited method entities must have constant pointing to existent entity.",
368 error_inherited_ent_without_const,
369 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
373 if (is_atomic_entity(ent) && ent->initializer != NULL) {
374 ir_mode *mode = NULL;
375 ir_initializer_t *initializer = ent->initializer;
376 switch (initializer->kind) {
377 case IR_INITIALIZER_CONST:
378 mode = get_irn_mode(get_initializer_const_value(initializer));
380 case IR_INITIALIZER_TARVAL:
381 mode = get_tarval_mode(get_initializer_tarval_value(initializer));
383 case IR_INITIALIZER_NULL:
384 case IR_INITIALIZER_COMPOUND:
388 mode == NULL || mode == get_type_mode(tp),
389 "Mode of constant in entity must match type.",
390 error_ent_const_mode,
391 ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
392 ent, tp, get_type_mode(tp))
399 * check types and entities
401 static void check_tore(type_or_ent tore, void *env) {
404 if (is_type(tore.typ)) {
405 *res = check_type(tore.typ);
407 assert(is_entity(tore.ent));
408 *res = check_entity(tore.ent);
413 * Verify types and entities.
418 ir_type *constructors;
419 ir_type *destructors;
422 type_walk(check_tore, NULL, &res);
424 constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
425 for (i = get_compound_n_members(constructors)-1; i >= 0; --i) {
426 ir_entity *entity = get_compound_member(constructors, i);
427 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
428 "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
431 destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
432 for (i = get_compound_n_members(destructors)-1; i >= 0; --i) {
433 ir_entity *entity = get_compound_member(destructors, i);
434 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
435 "entity without LINKAGE_HIDDEN_USER in destructors is pointless",