3 * File name: ir/tr/trvrfy.c
4 * Purpose: Check types and entities for correctness.
5 * Author: Michael Beck, Goetz Lindenmaier
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 #include "irgraph_t.h" /* for checking whether constant code is allocated
23 static int check_class(type *tp) {
27 /*printf("\n"); DDMT(tp);*/
29 for (i = 0; i < get_class_n_members(tp); i++) {
31 entity *mem = get_class_member(tp, i);
32 assert(mem && "NULL members not allowed");
33 /*printf(" %d, %d", get_entity_n_overwrites(mem), get_class_n_supertypes(tp)); DDME(mem);*/
34 if (!mem) return error_null_mem;
36 if (get_entity_n_overwrites(mem) > get_class_n_supertypes(tp)) {
38 assert(get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp));
40 for (j = 0; j < get_entity_n_overwrites(mem); j++) {
41 entity *ovw = get_entity_overwrites(mem, j);
42 /*printf(" overwrites: "); DDME(ovw);*/
43 /* Check whether ovw is member of one of tp's supertypes. If so,
44 the representation is correct. */
46 for (k = 0; k < get_class_n_supertypes(tp); k++) {
47 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
54 assert(found && "overwrites an entity not contained in direct supertype");
55 return error_ent_not_cont;
66 static int check_array(type *tp) {
67 int i, n_dim = get_array_n_dimensions(tp);
68 for (i = 0; i < n_dim; ++i)
69 assert(has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i));
78 static int check_primitive(type *tp) {
79 assert(is_mode(get_type_mode(tp)));
88 * Currently checks class types only.
90 static int check_type(type *tp) {
91 switch (get_type_tpop_code(tp)) {
93 return check_class(tp);
95 return check_array(tp);
97 return check_primitive(tp);
104 * helper environment struct for constant_on_wrong_obstack()
112 * called by the walker
114 static void on_irg_storage(ir_node *n, void *env) {
115 struct myenv * myenv = env;
117 myenv->res = node_is_in_irgs_storage(myenv->irg, n);
119 /* We also test whether the setting of the visited flag is legal. */
120 assert(get_irn_visited(n) <= get_irg_visited(myenv->irg) &&
121 "Visited flag of node is larger than that of corresponding irg.");
125 * checks wheater a given constant IR node is NOT on the
128 static int constant_on_wrong_irg(ir_node *n) {
131 env.res = 1; /* on right obstack */
132 env.irg = get_const_code_irg();
134 irg_walk(n, on_irg_storage, NULL, (void *)&env);
139 * Check if constants node are NOT on the constant IR graph.
141 static int constants_on_wrong_irg(entity *ent) {
142 if (get_entity_variability(ent) == variability_uninitialized) return 0;
144 if (is_compound_entity(ent)) {
146 for (i = 0; i < get_compound_ent_n_values(ent); i++) {
147 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
151 /* Might not be set if entity belongs to a description or is external allocated. */
152 if (get_atomic_ent_value(ent))
153 return constant_on_wrong_irg(get_atomic_ent_value(ent));
154 else if (get_entity_visibility(ent) != visibility_external_allocated)
155 assert((is_Class_type(get_entity_owner(ent)) &&
156 get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description) &&
157 "Value in constant atomic entity not set.");
163 * Check an entity. Currently, we check only if initialized constants
164 * are build on the const irg graph.
167 * 0 if no error encountered
170 static int check_entity(entity *ent) {
173 current_ir_graph = get_const_code_irg();
174 if (constants_on_wrong_irg(ent)) {
175 assert(0 && "Contants placed on wrong IRG");
176 return error_const_on_wrong_irg;
179 rem_vpi = get_visit_pseudo_irgs();
180 set_visit_pseudo_irgs(1);
181 if ((get_entity_peculiarity(ent) == peculiarity_existent) &&
182 (get_entity_visibility(ent) != visibility_external_allocated) &&
183 (is_Method_type(get_entity_type(ent))) &&
184 (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
185 assert(0 && "Method ents with pec_exist must have an irg");
186 return error_existent_entity_without_irg;
188 set_visit_pseudo_irgs(rem_vpi);
190 if (get_entity_peculiarity(ent) == peculiarity_inherited) {
191 entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
192 assert(get_entity_peculiarity(impl) == peculiarity_existent &&
193 "inherited entities must have constant pointing to existent entity.");
200 * check types and entities
202 static void check_tore(type_or_ent *tore, void *env) {
206 *res = check_type((type *)tore);
208 assert(is_entity(tore));
209 *res = check_entity((entity *)tore);
214 * Verify types and entities.
219 type_walk(check_tore, NULL, &res);