more debug output
[libfirm] / ir / tr / trvrfy.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/trvrfy.c
4  * Purpose:     Check types and entities for correctness.
5  * Author:      Michael Beck, Goetz Lindenmaier
6  * Modified by:
7  * Created:     29.1.2003
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include "trvrfy.h"
17 #include "irgraph_t.h"  /* for checking whether constant code is allocated
18                            on proper obstack */
19
20 /**
21  * Check a class
22  */
23 static int check_class(type *tp) {
24   int i, j, k;
25   int found;
26
27   /*printf("\n"); DDMT(tp);*/
28
29   for (i = 0; i < get_class_n_members(tp); i++) {
30
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;
35
36     if (get_entity_n_overwrites(mem) > get_class_n_supertypes(tp)) {
37       DDMT(tp); DDME(mem);
38       assert(get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp));
39     }
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. */
45       found = false;
46       for (k = 0; k < get_class_n_supertypes(tp); k++) {
47         if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
48           found = true;
49           break;
50         }
51       }
52       if (!found) {
53         DDMT(tp); DDME(mem);
54         assert(found && "overwrites an entity not contained in direct supertype");
55         return error_ent_not_cont;
56       }
57     }
58
59   }
60   return 0;
61 }
62
63 /**
64  * Checks a type.
65  *
66  * Currently checks class types only.
67  */
68 static int check_type(type *tp) {
69   switch (get_type_tpop_code(tp)) {
70   case tpo_class:
71     return check_class(tp);
72   default: break;
73   }
74   return 0;
75 }
76
77 /**
78  * helper environment struct for constant_on_wrong_obstack()
79  */
80 struct myenv {
81   int res;
82   ir_graph *irg;
83 };
84
85 /**
86  * called by the walker
87  */
88 static void on_irg_storage(ir_node *n, void *env) {
89   struct myenv * myenv = env;
90
91   myenv->res = node_is_in_irgs_storage(myenv->irg, n);
92
93   /* We also test whether the setting of the visited flag is legal. */
94   assert(get_irn_visited(n) <= get_irg_visited(myenv->irg) &&
95          "Visited flag of node is larger than that of corresponding irg.");
96 }
97
98 /**
99  * checks wheater a given constant IR node is NOT on the
100  * constant IR graph.
101  */
102 static int constant_on_wrong_irg(ir_node *n) {
103   struct myenv env;
104
105   env.res = 1;  /* on right obstack */
106   env.irg = get_const_code_irg();
107
108   irg_walk(n, on_irg_storage, NULL, (void *)&env);
109   return ! env.res;
110 }
111
112 /*
113  * Check if constants node are NOT on the constant IR graph.
114  */
115 static int constants_on_wrong_irg(entity *ent) {
116   if (get_entity_variability(ent) == variability_uninitialized) return 0;
117
118   if (is_compound_entity(ent)) {
119     int i;
120     for (i = 0; i < get_compound_ent_n_values(ent); i++) {
121       if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
122         return 1;
123     }
124   } else {
125     /* Might not be set if entity belongs to a description. */
126     if (get_atomic_ent_value(ent))
127       return constant_on_wrong_irg(get_atomic_ent_value(ent));
128     else
129       assert((is_class_type(get_entity_owner(ent)) &&
130               get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description) &&
131              "Value in constant atomic entity not set.");
132   }
133   return 0;
134 }
135
136 /*
137  * Check an entity. Currently, we check only if initialized constants
138  * are build on the const irg graph.
139  *
140  * @return
141  *      0       if no error encountered
142  *      != 0    else
143  */
144 static int check_entity(entity *ent) {
145   current_ir_graph =  get_const_code_irg();
146   if (constants_on_wrong_irg(ent)) {
147     assert(0 && "Contants placed on wrong IRG");
148     return error_const_on_wrong_irg;
149   }
150
151   if ((get_entity_peculiarity(ent) == peculiarity_existent) &&
152       (get_entity_visibility(ent) != visibility_external_allocated) &&
153       (is_method_type(get_entity_type(ent)))                &&
154       (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
155     assert(0 && "Method ents with pec_exist must have an irg");
156     return error_existent_entity_without_irg;
157   }
158
159   return 0;
160 }
161
162 /*
163  * check types and entities
164  */
165 static void check_tore(type_or_ent *tore, void *env) {
166   int *res = env;
167   assert(tore);
168   if (is_type(tore)) {
169     *res = check_type((type *)tore);
170   } else {
171     assert(is_entity(tore));
172     *res = check_entity((entity *)tore);
173   }
174 }
175
176 /*
177  * Verify types and entities.
178  */
179 int tr_vrfy(void) {
180   int res;
181
182   type_walk(check_tore, NULL, &res);
183   return res;
184 }