Make prefixes for most (hopefully all) enum values. Means change your code, I/F has...
[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
13 #include "trvrfy.h"
14 #include "irgraph_t.h"  /* for checking whether constant code is allocated
15                            on proper obstack */
16
17 /**
18  * Check a class
19  */
20 static int check_class(type *tp) {
21   int i, j, k;
22   int found;
23
24   /*printf("\n"); DDMT(tp);*/
25
26   for (i = 0; i < get_class_n_members(tp); i++) {
27
28     entity *mem = get_class_member(tp, i);
29     assert(mem && "NULL members not allowed");
30     /*printf(" %d, %d", get_entity_n_overwrites(mem), get_class_n_supertypes(tp)); DDME(mem);*/
31     if (!mem) return error_null_mem;
32
33     if (get_entity_n_overwrites(mem) > get_class_n_supertypes(tp)) {
34       DDMT(tp); DDME(mem);
35       assert(get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp));
36     }
37     for (j = 0; j < get_entity_n_overwrites(mem); j++) {
38       entity *ovw = get_entity_overwrites(mem, j);
39       /*printf(" overwrites: "); DDME(ovw);*/
40       /* Check whether ovw is member of one of tp's supertypes. If so,
41          the representation is correct. */
42       found = false;
43       for (k = 0; k < get_class_n_supertypes(tp); k++) {
44         if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
45           found = true;
46           break;
47         }
48       }
49       if (!found) {
50         DDMT(tp); DDME(mem);
51         assert(found && "overwrites an entity not contained in direct supertype");
52         return error_ent_not_cont;
53       }
54     }
55
56   }
57   return 0;
58 }
59
60 /**
61  * Checks a type.
62  *
63  * Currently checks class types only.
64  */
65 static int check_type(type *tp) {
66   switch (get_type_tpop_code(tp)) {
67   case tpo_class:
68     return check_class(tp);
69   default: break;
70   }
71   return 0;
72 }
73
74 /**
75  * helper environment struct for constant_on_wrong_obstack()
76  */
77 struct myenv {
78   int res;
79   ir_graph *irg;
80 };
81
82 /**
83  * called by the walker
84  */
85 static void on_irg_storage(ir_node *n, void *env) {
86   struct myenv * myenv = env;
87
88   myenv->res = node_is_in_irgs_storage(myenv->irg, n);
89
90   /* We also test whether the setting of the visited flag is legal. */
91   assert(get_irn_visited(n) <= get_irg_visited(myenv->irg) &&
92          "Visited flag of node is larger than that of corresponding irg.");
93 }
94
95 /**
96  * checks wheater a given constant IR node is NOT on the
97  * constant IR graph.
98  */
99 static int constant_on_wrong_irg(ir_node *n) {
100   struct myenv env;
101
102   env.res = 1;  /* on right obstack */
103   env.irg = get_const_code_irg();
104
105   irg_walk(n, on_irg_storage, NULL, (void *)&env);
106   return ! env.res;
107 }
108
109 /*
110  * Check if constants node are NOT on the constant IR graph.
111  */
112 static int constants_on_wrong_irg(entity *ent) {
113   if (get_entity_variability(ent) == variability_uninitialized) return 0;
114
115   if (is_compound_entity(ent)) {
116     int i;
117     for (i = 0; i < get_compound_ent_n_values(ent); i++) {
118       if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
119         return 1;
120     }
121   } else {
122     /* Might not be set if entity belongs to a description. */
123     if (get_atomic_ent_value(ent))
124       return constant_on_wrong_irg(get_atomic_ent_value(ent));
125     else
126       assert((is_class_type(get_entity_owner(ent)) &&
127               get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description) &&
128              "Value in constant atomic entity not set.");
129   }
130   return 0;
131 }
132
133 /*
134  * Check an entity. Currently, we check only if initialized constants
135  * are build on the const irg graph.
136  *
137  * @return
138  *      0       if no error encountered
139  *      != 0    else
140  */
141 static int check_entity(entity *ent) {
142   current_ir_graph =  get_const_code_irg();
143   if (constants_on_wrong_irg(ent)) {
144     assert(0 && "Contants placed on wrong IRG");
145     return error_const_on_wrong_irg;
146   }
147   return 0;
148 }
149
150 /*
151  * check types and entities
152  */
153 static void check_tore(type_or_ent *tore, void *env) {
154   int *res = env;
155   assert(tore);
156   if (is_type(tore)) {
157     *res = check_type((type *)tore);
158   } else {
159     assert(is_entity(tore));
160     *res = check_entity((entity *)tore);
161   }
162 }
163
164 /*
165  * Verify types and entities.
166  */
167 int tr_vrfy(void) {
168   int res;
169
170   type_walk(check_tore, NULL, &res);
171   return res;
172 }