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"
36 static void report_error(const char *fmt, ...)
40 fprintf(stderr, "Verify warning: ");
42 ir_vfprintf(stderr, fmt, ap);
47 static bool check_class_member(const ir_type *tp, const ir_entity *entity)
50 if (get_entity_n_overwrites(entity) > get_class_n_supertypes(tp)) {
51 report_error("member %+F of %+F has too many overwrites", entity, tp);
57 static bool check_compound_type(const ir_type *tp)
60 bool is_class = is_Class_type(tp);
61 size_t n = get_compound_n_members(tp);
64 for (i = 0; i < n; ++i) {
65 ir_entity *member = get_compound_member(tp, i);
69 report_error("%+F has a NULL member\n", tp);
73 owner = get_entity_owner(member);
75 report_error("member %+F of %+F has owner %+F\n", member, tp, owner);
79 fine &= check_class_member(tp, member);
85 static bool check_array_type(const ir_type *tp)
88 size_t n_dim = get_array_n_dimensions(tp);
91 for (i = 0; i < n_dim; ++i) {
92 if (!has_array_lower_bound(tp, i) && !has_array_upper_bound(tp, i)) {
93 report_error("missing array bound in %+F in dimension %zu", tp, i);
100 static bool check_type_mode(const ir_type *tp)
103 if (get_type_mode(tp) == NULL) {
104 report_error("type %+F has no mode", tp);
110 static bool check_primitive_type(const ir_type *tp)
112 return check_type_mode(tp);
115 static bool check_pointer_type(const ir_type *tp)
117 return check_type_mode(tp);
120 int check_type(const ir_type *tp)
122 switch (get_type_tpop_code(tp)) {
125 case tpo_class: return check_compound_type(tp);
126 case tpo_array: return check_array_type(tp);
127 case tpo_primitive: return check_primitive_type(tp);
128 case tpo_pointer: return check_pointer_type(tp);
129 case tpo_enumeration:
131 case tpo_uninitialized:
140 static bool check_visited_flag(ir_graph *irg, ir_node *n)
143 if (get_irn_visited(n) > get_irg_visited(irg)) {
144 report_error("visited flag of %+F is larger than that of corresponding irg %+F", n, irg);
150 typedef struct myenv {
155 static void on_irg_storage(ir_node *n, void *data)
157 myenv *env = (myenv*)data;
159 /* We also test whether the setting of the visited flag is legal. */
160 env->fine &= node_is_in_irgs_storage(env->irg, n);
161 env->fine &= check_visited_flag(env->irg, n);
164 static bool constant_on_wrong_irg(ir_node *n)
169 env.irg = get_const_code_irg();
171 irg_walk(n, on_irg_storage, NULL, (void *)&env);
175 static bool initializer_constant_on_wrong_irg(const ir_initializer_t *initializer)
177 switch (get_initializer_kind(initializer)) {
178 case IR_INITIALIZER_NULL:
180 case IR_INITIALIZER_TARVAL:
182 case IR_INITIALIZER_CONST:
183 return constant_on_wrong_irg(get_initializer_const_value(initializer));
184 case IR_INITIALIZER_COMPOUND: {
186 size_t n = get_initializer_compound_n_entries(initializer);
188 for (i = 0; i < n; ++i) {
189 const ir_initializer_t *sub
190 = get_initializer_compound_value(initializer, i);
191 fine &= initializer_constant_on_wrong_irg(sub);
196 panic("invalid initializer in initializer_on_wrong_irg");
199 static bool constants_on_wrong_irg(const ir_entity *ent)
201 if (ent->initializer != NULL) {
202 return initializer_constant_on_wrong_irg(ent->initializer);
203 } else if (entity_has_compound_ent_values(ent)) {
205 size_t n = get_compound_ent_n_values(ent);
207 for (i = 0; i < n; ++i) {
208 fine &= constant_on_wrong_irg(get_compound_ent_value(ent, i));
215 static bool check_external_linkage(const ir_entity *entity, ir_linkage linkage,
216 const char *linkage_name)
219 if ((get_entity_linkage(entity) & linkage) == 0)
221 if (get_entity_visibility(entity) != ir_visibility_external) {
222 report_error("entity %+F has IR_LINKAGE_%s but is not externally visible", entity, linkage_name);
225 if (!entity_has_definition(entity)) {
226 report_error("entity %+F has IR_LINKAGE_%s but is just a declaration", entity, linkage_name);
232 int check_entity(const ir_entity *entity)
235 ir_type *tp = get_entity_type(entity);
236 ir_linkage linkage = get_entity_linkage(entity);
238 fine &= constants_on_wrong_irg(entity);
240 if (is_method_entity(entity)) {
241 ir_graph *irg = get_entity_irg(entity);
243 ir_entity *irg_entity = get_irg_entity(irg);
244 if (irg_entity != entity) {
245 report_error("entity(%+F)->irg->entity(%+F) relation invalid",
250 if (get_entity_peculiarity(entity) == peculiarity_existent) {
251 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(entity));
253 report_error("inherited method entity %+F must have constant pointing to existent entity.", entity);
259 if (linkage & IR_LINKAGE_NO_CODEGEN) {
260 if (!is_method_entity(entity)) {
261 report_error("entity %+F has IR_LINKAGE_NO_CODEGEN but is not a function", entity);
263 } else if (get_entity_irg(entity) == NULL) {
264 report_error("entity %+F has IR_LINKAGE_NO_CODEGEN but has no ir-graph anyway", entity);
267 if (get_entity_visibility(entity) != ir_visibility_external) {
268 report_error("entity %+F has IR_LINKAGE_NO_CODEGEN but is not externally visible", entity);
272 check_external_linkage(entity, IR_LINKAGE_WEAK, "WEAK");
273 check_external_linkage(entity, IR_LINKAGE_GARBAGE_COLLECT,
275 check_external_linkage(entity, IR_LINKAGE_MERGE, "MERGE");
277 if (is_atomic_entity(entity) && entity->initializer != NULL) {
278 ir_mode *mode = NULL;
279 ir_initializer_t *initializer = entity->initializer;
280 switch (initializer->kind) {
281 case IR_INITIALIZER_CONST:
282 mode = get_irn_mode(get_initializer_const_value(initializer));
284 case IR_INITIALIZER_TARVAL:
285 mode = get_tarval_mode(get_initializer_tarval_value(initializer));
287 case IR_INITIALIZER_NULL:
288 case IR_INITIALIZER_COMPOUND:
291 if (mode != NULL && mode != get_type_mode(tp)) {
292 report_error("initializer of entity %+F has wrong mode.", entity);
299 static void check_tore(type_or_ent tore, void *env)
301 bool *fine = (bool*)env;
303 if (is_type(tore.typ)) {
304 *fine &= check_type(tore.typ);
306 assert(is_entity(tore.ent));
307 *fine &= check_entity(tore.ent);
314 ir_type *constructors;
315 ir_type *destructors;
316 ir_type *thread_locals;
320 type_walk(check_tore, NULL, &fine);
322 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
323 const ir_type *type = get_segment_type(s);
325 for (e = 0; e < get_compound_n_members(type); ++e) {
326 ir_entity *entity = get_compound_member(type, e);
327 if (get_entity_ld_ident(entity) == NULL &&
328 get_entity_visibility(entity) != ir_visibility_private) {
329 report_error("public segment member %+F has no name",
336 constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
337 for (i = 0, n = get_compound_n_members(constructors); i < n; ++i) {
338 const ir_entity *entity = get_compound_member(constructors, i);
339 if ((get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER) == 0) {
340 report_error("entity %+F in constructors without LINKAGE_HIDDEN_USER",
344 /* Mach-O doesn't like labels in this section */
345 if (get_entity_ld_name(entity)[0] != '\0') {
346 report_error("entity %+F in constructors must not have an ld_name",
351 destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
352 for (i = 0, n = get_compound_n_members(destructors); i < n; ++i) {
353 const ir_entity *entity = get_compound_member(destructors, i);
354 if ((get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER) == 0) {
355 report_error("entity %+F in destructors without LINKAGE_HIDDEN_USER",
359 /* Mach-O doesn't like labels in this section */
360 if (get_entity_ld_name(entity)[0] != '\0') {
361 report_error("entity %+F in destructors must not have an ld_name",
366 thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
367 for (i = 0, n = get_compound_n_members(thread_locals); i < n; ++i) {
368 const ir_entity *entity = get_compound_member(thread_locals, i);
369 /* this is odd and should not be allowed I think */
370 if (is_method_entity(entity)) {
371 report_error("method %+F in thread local segment");
374 if (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT) {
375 report_error("entity %+F in thread local segment is constant");