12f23e83875c7561e3e2aa8171fa6cb197fadb00
[m6w6/ext-psi] / src / validate.c
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #include "data.h"
33
34 typedef bool (*psi_validate_list_entry)(struct psi_validate_scope *scope,
35 struct psi_data *dst, void *ptr);
36
37 struct psi_validate_list {
38 struct psi_plist *src;
39 struct psi_plist **dst;
40 struct psi_plist *cur;
41 struct psi_plist *chk;
42 };
43
44 static inline void psi_validate_list(struct psi_validate_scope *scope,
45 struct psi_data *dst, struct psi_validate_list *list,
46 psi_validate_list_entry validate)
47 {
48 if (!list->cur) {
49 list->cur = list->src;
50 }
51
52 if (psi_plist_count(list->cur)) {
53 size_t i = 0;
54 void *ptr = NULL;
55
56 list->chk = psi_plist_init(NULL);
57
58 while (psi_plist_get(list->cur, i++, &ptr)) {
59 *dst->last_error = 0;
60
61 PSI_DEBUG_PRINT(dst, "PSI: validate %s ", "»");
62 if (validate(scope, dst, ptr)) {
63 PSI_DEBUG_PRINT(dst, " %s\n", "✔");
64 *list->dst = psi_plist_add(*list->dst, &ptr);
65 } else {
66 PSI_DEBUG_PRINT(dst, " %s (%s)\n", "✘", dst->last_error);
67 list->chk = psi_plist_add(list->chk, &ptr);
68 }
69 }
70
71 if (list->cur != list->src) {
72 psi_plist_free(list->cur);
73 }
74 list->cur = list->chk;
75 }
76 }
77
78 static bool psi_validate_type(struct psi_validate_scope *scope,
79 struct psi_data *dst, void *ptr)
80 {
81 struct psi_decl_arg *def = ptr;
82
83 PSI_DEBUG_PRINT(dst, "typedef %s", def->var->name->val);
84 return psi_decl_arg_validate_typedef(dst, def, scope);
85 }
86
87 static bool psi_validate_struct(struct psi_validate_scope *scope,
88 struct psi_data *dst, void *ptr)
89 {
90 struct psi_decl_struct *str = ptr;
91
92 PSI_DEBUG_PRINT(dst, "struct %s", str->name->val);
93 if (psi_decl_struct_validate(dst, str, scope)) {
94 PSI_DEBUG_PRINT(dst, "::(%zu, %zu)", str->align, str->size);
95 return true;
96 }
97 return false;
98 }
99
100 static bool psi_validate_union(struct psi_validate_scope *scope,
101 struct psi_data *dst, void *ptr)
102 {
103 struct psi_decl_union *unn = ptr;
104
105 PSI_DEBUG_PRINT(dst, "union %s", unn->name->val);
106 if (psi_decl_union_validate(dst, unn, scope)) {
107 PSI_DEBUG_PRINT(dst, "::(%zu, %zu)", unn->align, unn->size);
108 return true;
109 }
110 return false;
111 }
112
113 static bool psi_validate_enum(struct psi_validate_scope *scope,
114 struct psi_data *dst, void *ptr)
115 {
116 struct psi_decl_enum *enm = ptr;
117
118 PSI_DEBUG_PRINT(dst, "enum %s", enm->name->val);
119 return psi_decl_enum_validate(dst, enm);
120 }
121
122 static bool psi_validate_extvar(struct psi_validate_scope *scope,
123 struct psi_data *dst, void *ptr)
124 {
125 struct psi_decl_extvar *evar = ptr;
126
127 PSI_DEBUG_PRINT(dst, "extvar %s", evar->arg->var->name->val);
128 if (psi_decl_extvar_validate(dst, evar, scope)) {
129 dst->decls = psi_plist_add(dst->decls, &evar->getter);
130 dst->decls = psi_plist_add(dst->decls, &evar->setter);
131 return true;
132 }
133 return false;
134 }
135
136 static bool psi_validate_decl(struct psi_validate_scope *scope,
137 struct psi_data *dst, void *ptr)
138 {
139 struct psi_decl *decl = ptr;
140
141 PSI_DEBUG_PRINT(dst, "decl %s", decl->func->var->name->val);
142 return psi_decl_validate(dst, decl, scope);
143 }
144
145 static bool psi_validate_const(struct psi_validate_scope *scope,
146 struct psi_data *dst, void *ptr)
147 {
148 struct psi_const *cnst = ptr;
149
150 PSI_DEBUG_PRINT(dst, "constant %s", cnst->name->val);
151 return psi_const_validate(dst, cnst, scope);
152 }
153
154 static bool psi_validate_impl(struct psi_validate_scope *scope,
155 struct psi_data *dst, void *ptr)
156 {
157 struct psi_impl *impl = ptr;
158
159 PSI_DEBUG_PRINT(dst, "impl %s", impl->func->name->val);
160 return psi_impl_validate(dst, impl, scope);
161 }
162
163 bool psi_validate(struct psi_validate_scope *scope,
164 struct psi_data *dst, struct psi_data *src)
165 {
166 struct psi_validate_list types = {src->types, &dst->types};
167 struct psi_validate_list structs = {src->structs, &dst->structs};
168 struct psi_validate_list unions = {src->unions, &dst->unions};
169 struct psi_validate_list enums = {src->enums, &dst->enums};
170 struct psi_validate_list vars = {src->vars, &dst->vars};
171 struct psi_validate_list decls = {src->decls, &dst->decls};
172 struct psi_validate_list consts = {src->consts, &dst->consts};
173 struct psi_validate_list impls = {src->impls, &dst->impls};
174 unsigned flags = dst->flags;
175 size_t check_count = ~0;
176
177 /* fail early if libraries are not found */
178 if (!psi_decl_file_validate(dst, src)) {
179 return false;
180 }
181
182 /* silence loop */
183 dst->flags |= PSI_SILENT;
184
185 while (true) {
186 size_t count_all = psi_plist_count(types.cur ?: types.src)
187 + psi_plist_count(structs.cur ?: structs.src)
188 + psi_plist_count(unions.cur ?: unions.src)
189 + psi_plist_count(enums.cur ?: enums.src)
190 + psi_plist_count(vars.cur ?: vars.src)
191 + psi_plist_count(decls.cur ?: decls.src);
192
193 if (check_count != count_all) {
194 check_count = count_all;
195
196 PSI_DEBUG_PRINT(dst,
197 "PSI: validate data(%p) %zu type checks remaining\n",
198 src, check_count);
199
200 psi_validate_list(scope, dst, &types, psi_validate_type);
201 psi_validate_list(scope, dst, &structs, psi_validate_struct);
202 psi_validate_list(scope, dst, &unions, psi_validate_union);
203 psi_validate_list(scope, dst, &enums, psi_validate_enum);
204 psi_validate_list(scope, dst, &vars, psi_validate_extvar);
205 psi_validate_list(scope, dst, &decls, psi_validate_decl);
206 continue;
207 }
208
209 /* nothing changed; bail out */
210 if (count_all) {
211 src->errors += count_all;
212
213 if ((dst->flags & PSI_SILENT) && !(flags & PSI_SILENT)) {
214 /* one last error-spitting round, if not explicitly suppressed */
215 dst->flags ^= PSI_SILENT;
216 check_count = ~0;
217
218 PSI_DEBUG_PRINT(dst, "PSI: validation bail out with %zu"
219 " type checks remaining, errors follow\n", count_all);
220 continue;
221 }
222 }
223
224 break;
225 }
226
227 /* reset original flags */
228 dst->flags = flags;
229
230 psi_validate_list(scope, dst, &consts, psi_validate_const);
231 psi_validate_list(scope, dst, &impls, psi_validate_impl);
232
233 if (types.cur != types.src) {
234 psi_plist_free(types.cur);
235 }
236 if (structs.cur != structs.src) {
237 psi_plist_free(structs.cur);
238 }
239 if (unions.cur != unions.src) {
240 psi_plist_free(unions.cur);
241 }
242 if (enums.cur != enums.src) {
243 psi_plist_free(enums.cur);
244 }
245 if (vars.cur != vars.src) {
246 psi_plist_free(vars.cur);
247 }
248 if (decls.cur != decls.src) {
249 psi_plist_free(decls.cur);
250 }
251 if (consts.cur != consts.src) {
252 psi_plist_free(consts.cur);
253 }
254 if (impls.cur != impls.src) {
255 psi_plist_free(impls.cur);
256 }
257
258 return true;
259 }