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");
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);
207 static bool check_external_linkage(const ir_entity *entity, ir_linkage linkage,
208 const char *linkage_name)
211 if ((get_entity_linkage(entity) & linkage) == 0)
213 if (get_entity_visibility(entity) != ir_visibility_external) {
214 report_error("entity %+F has IR_LINKAGE_%s but is not externally visible", entity, linkage_name);
217 if (!entity_has_definition(entity)) {
218 report_error("entity %+F has IR_LINKAGE_%s but is just a declaration", entity, linkage_name);
224 int check_entity(const ir_entity *entity)
227 ir_type *tp = get_entity_type(entity);
228 ir_linkage linkage = get_entity_linkage(entity);
230 fine &= constants_on_wrong_irg(entity);
232 if (is_method_entity(entity)) {
233 ir_graph *irg = get_entity_irg(entity);
235 ir_entity *irg_entity = get_irg_entity(irg);
236 if (irg_entity != entity) {
237 report_error("entity(%+F)->irg->entity(%+F) relation invalid",
242 if (get_entity_peculiarity(entity) == peculiarity_existent) {
243 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(entity));
245 report_error("inherited method entity %+F must have constant pointing to existent entity.", entity);
251 if (linkage & IR_LINKAGE_NO_CODEGEN) {
252 if (!is_method_entity(entity)) {
253 report_error("entity %+F has IR_LINKAGE_NO_CODEGEN but is not a function", entity);
255 } else if (get_entity_irg(entity) == NULL) {
256 report_error("entity %+F has IR_LINKAGE_NO_CODEGEN but has no ir-graph anyway", entity);
259 if (get_entity_visibility(entity) != ir_visibility_external) {
260 report_error("entity %+F has IR_LINKAGE_NO_CODEGEN but is not externally visible", entity);
264 check_external_linkage(entity, IR_LINKAGE_WEAK, "WEAK");
265 check_external_linkage(entity, IR_LINKAGE_GARBAGE_COLLECT,
267 check_external_linkage(entity, IR_LINKAGE_MERGE, "MERGE");
269 if (is_atomic_entity(entity) && entity->initializer != NULL) {
270 ir_mode *mode = NULL;
271 ir_initializer_t *initializer = entity->initializer;
272 switch (initializer->kind) {
273 case IR_INITIALIZER_CONST:
274 mode = get_irn_mode(get_initializer_const_value(initializer));
276 case IR_INITIALIZER_TARVAL:
277 mode = get_tarval_mode(get_initializer_tarval_value(initializer));
279 case IR_INITIALIZER_NULL:
280 case IR_INITIALIZER_COMPOUND:
283 if (mode != NULL && mode != get_type_mode(tp)) {
284 report_error("initializer of entity %+F has wrong mode.", entity);
291 static void check_tore(type_or_ent tore, void *env)
293 bool *fine = (bool*)env;
295 if (is_type(tore.typ)) {
296 *fine &= check_type(tore.typ);
298 assert(is_entity(tore.ent));
299 *fine &= check_entity(tore.ent);
306 ir_type *constructors;
307 ir_type *destructors;
308 ir_type *thread_locals;
312 type_walk(check_tore, NULL, &fine);
314 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
315 const ir_type *type = get_segment_type(s);
317 for (e = 0; e < get_compound_n_members(type); ++e) {
318 ir_entity *entity = get_compound_member(type, e);
319 if (get_entity_ld_ident(entity) == NULL &&
320 get_entity_visibility(entity) != ir_visibility_private) {
321 report_error("public segment member %+F has no name",
328 constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
329 for (i = 0, n = get_compound_n_members(constructors); i < n; ++i) {
330 const ir_entity *entity = get_compound_member(constructors, i);
331 if ((get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER) == 0) {
332 report_error("entity %+F in constructors without LINKAGE_HIDDEN_USER",
336 /* Mach-O doesn't like labels in this section */
337 if (get_entity_ld_name(entity)[0] != '\0') {
338 report_error("entity %+F in constructors must not have an ld_name",
343 destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
344 for (i = 0, n = get_compound_n_members(destructors); i < n; ++i) {
345 const ir_entity *entity = get_compound_member(destructors, i);
346 if ((get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER) == 0) {
347 report_error("entity %+F in destructors without LINKAGE_HIDDEN_USER",
351 /* Mach-O doesn't like labels in this section */
352 if (get_entity_ld_name(entity)[0] != '\0') {
353 report_error("entity %+F in destructors must not have an ld_name",
358 thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
359 for (i = 0, n = get_compound_n_members(thread_locals); i < n; ++i) {
360 const ir_entity *entity = get_compound_member(thread_locals, i);
361 /* this is odd and should not be allowed I think */
362 if (is_method_entity(entity)) {
363 report_error("method %+F in thread local segment");
366 if (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT) {
367 report_error("entity %+F in thread local segment is constant");