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
31 #include "irgraph_t.h" /* for checking whether constant code is allocated
40 * in RELEASE mode, returns ret if the expression expr evaluates to zero
41 * in ASSERT mode, asserts the expression expr (and the string string).
43 #define ASSERT_AND_RET(expr, string, ret) if (!(expr)) return (ret)
46 * in RELEASE mode, returns ret if the expression expr evaluates to zero
47 * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
49 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) if (!(expr)) return (ret)
51 #define ASSERT_AND_RET(expr, string, ret) \
53 if (opt_do_node_verification == FIRM_VERIFICATION_ON) {\
54 assert((expr) && string); } \
56 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
57 fprintf(stderr, #expr " : " string "\n"); \
58 firm_vrfy_failure_msg = #expr " && " string; \
63 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
66 firm_vrfy_failure_msg = #expr " && " string; \
67 if (opt_do_node_verification != FIRM_VERIFICATION_ERROR_ONLY) { blk; } \
68 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
69 fprintf(stderr, #expr " : " string "\n"); \
70 else if (opt_do_node_verification == FIRM_VERIFICATION_ON) { \
71 assert((expr) && string); \
81 static const char *firm_vrfy_failure_msg;
84 * Show diagnostic if an entity overwrites another one not
85 * in direct superclasses.
87 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);
104 * Show diagnostic if an entity overwrites a wrong number of things.
106 static void show_ent_overwrite_cnt(ir_entity *ent) {
107 ir_type *owner = get_entity_owner(ent);
108 int i, j, k, found, show_stp = 0;
110 fprintf(stderr, "Type verification error:\n");
111 ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
112 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
113 ir_entity *ovw = get_entity_overwrites(ent, i);
114 ir_type *ov_own = get_entity_owner(ovw);
116 ir_fprintf(stderr, " %t::%e\n", ov_own, ovw);
117 for (k = 0; k < i; ++k)
118 if (ovw == get_entity_overwrites(ent, k)) {
119 ir_fprintf(stderr, " ->%t::%e entered more than once\n", ov_own, ovw);
124 for (j = get_class_n_supertypes(owner) - 1; j >= 0; --j) {
125 if (ov_own == get_class_supertype(owner, j)) {
126 show_stp = found = 1;
131 ir_fprintf(stderr, " ->%t not in super types of %t\n", ov_own, owner);
135 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
136 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
137 ir_type *super = get_class_supertype(owner, i);
138 ir_fprintf(stderr, " %t:\n", super);
144 * Shows a wrong entity allocation
146 static void show_ent_alloc_error(ir_entity *ent) {
147 ir_fprintf(stderr, "%+e owner %t has allocation %s\n",
148 ent, get_entity_type(ent),
149 get_allocation_name(get_entity_allocation(ent)));
152 #endif /* #ifndef NDEBUG */
157 static int check_class(ir_type *tp) {
161 for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
162 ir_entity *mem = get_class_member(tp, i);
165 tp == get_entity_owner(mem),
166 "class member with wrong owner",
167 error_ent_wrong_owner,
168 ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
172 "NULL members not allowed",
174 ir_fprintf(stderr, "Type verification error:\n%+F member %d is NULL\n", tp, i)
178 get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
179 "wrong number of entity overwrites",
180 error_wrong_ent_overwrites,
181 show_ent_overwrite_cnt(mem)
184 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
185 ir_entity *ovw = get_entity_overwrites(mem, j);
186 /*printf(" overwrites: "); DDME(ovw);*/
187 /* Check whether ovw is member of one of tp's supertypes. If so,
188 the representation is correct. */
190 for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
191 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
198 "overwrites an entity not contained in direct supertype",
200 show_ent_not_supertp(mem, ovw)
210 static int check_array(ir_type *tp) {
211 int i, n_dim = get_array_n_dimensions(tp);
213 for (i = 0; i < n_dim; ++i) {
215 has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
216 "array bound missing",
218 ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
228 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) {
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) {
263 get_irn_visited(n) <= get_irg_visited(irg),
264 "Visited flag of node is larger than that of corresponding irg.",
266 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
272 * helper environment struct for constant_on_wrong_obstack()
280 * called by the walker
282 static void on_irg_storage(ir_node *n, void *env) {
283 struct myenv *myenv = env;
285 /* We also test whether the setting of the visited flag is legal. */
286 myenv->res = node_is_in_irgs_storage(myenv->irg, n) &&
287 check_visited_flag(myenv->irg, n);
291 * checks whether a given constant IR node is NOT on the
294 static int constant_on_wrong_irg(ir_node *n) {
297 env.res = 1; /* on right obstack */
298 env.irg = get_const_code_irg();
300 irg_walk(n, on_irg_storage, NULL, (void *)&env);
305 * Check if constants node are NOT on the constant IR graph.
307 * @return NON-zero if an entity initializer constant is NOT on
308 * the current_ir_graph's obstack.
310 static int constants_on_wrong_irg(ir_entity *ent) {
311 if (get_entity_variability(ent) == variability_uninitialized) return 0;
313 if (is_compound_entity(ent)) {
314 if(!ent->has_initializer) {
316 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
317 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
322 /* Might not be set if entity belongs to a description or is external allocated. */
323 if (get_atomic_ent_value(ent))
324 return constant_on_wrong_irg(get_atomic_ent_value(ent));
325 else if (get_entity_visibility(ent) != visibility_external_allocated) {
327 is_Class_type(get_entity_owner(ent)) &&
328 get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description,
329 "Value in constant atomic entity not set.",
331 ir_fprintf(stderr, "%+e, owner %+F\n", ent, get_entity_owner(ent))
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);
349 ir_type *owner = get_entity_owner(ent);
351 current_ir_graph = get_const_code_irg();
353 constants_on_wrong_irg(ent) == 0,
354 "Contants placed on wrong IRG",
355 error_const_on_wrong_irg,
356 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
359 rem_vpi = get_visit_pseudo_irgs();
360 set_visit_pseudo_irgs(1);
361 if ((get_entity_peculiarity(ent) == peculiarity_existent) &&
362 (get_entity_visibility(ent) != visibility_external_allocated) &&
363 (is_Method_type(get_entity_type(ent))) &&
364 (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
367 "Method ents with pec_exist must have an irg",
368 error_existent_entity_without_irg,
369 ir_fprintf(stderr, "%+e\n", ent)
372 set_visit_pseudo_irgs(rem_vpi);
374 /* Originally, this test assumed, that only method entities have
375 pecularity_inherited. As I changed this, I have to test for method type before
377 if (get_entity_peculiarity(ent) == peculiarity_inherited) {
378 if (is_Method_type(get_entity_type(ent))) {
379 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
381 get_entity_peculiarity(impl) == peculiarity_existent,
382 "inherited method entities must have constant pointing to existent entity.",
383 error_inherited_ent_without_const,
384 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
389 /* Entities in global type are not dynamic or automatic allocated. */
390 if (owner == get_glob_type()) {
392 get_entity_allocation(ent) != allocation_dynamic &&
393 get_entity_allocation(ent) != allocation_automatic,
394 "Entities in global type are not allowed to by dynamic or automatic allocated",
395 error_glob_ent_allocation,
396 show_ent_alloc_error(ent)
400 if (get_entity_variability(ent) != variability_uninitialized) {
401 if (is_atomic_type(tp)) {
402 ir_node *val = get_atomic_ent_value(ent);
405 get_irn_mode(val) == get_type_mode(tp),
406 "Mode of constant in entity must match type.",
407 error_ent_const_mode,
408 ir_fprintf(stderr, "%+e const %+F, type %+F(%+F)\n",
409 ent, val, tp, get_type_mode(tp))
418 * check types and entities
420 static void check_tore(type_or_ent tore, void *env) {
423 if (is_type(tore.typ)) {
424 *res = check_type(tore.typ);
426 assert(is_entity(tore.ent));
427 *res = check_entity(tore.ent);
432 * Verify types and entities.
437 type_walk(check_tore, NULL, &res);