fix package.xml
[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 #include "debug.h"
34
35 typedef bool (*psi_validate_list_entry)(struct psi_validate_scope *scope,
36 struct psi_data *dst, void *ptr);
37
38 struct psi_validate_list {
39 struct psi_plist *src;
40 struct psi_plist **dst;
41 struct psi_plist *cur;
42 struct psi_plist *chk;
43 };
44
45 static inline void psi_validate_list(struct psi_validate_scope *scope,
46 struct psi_data *dst, struct psi_validate_list *list,
47 psi_validate_list_entry validate)
48 {
49 if (!list->cur) {
50 list->cur = list->src;
51 }
52
53 if (psi_plist_count(list->cur)) {
54 size_t i = 0;
55 void *ptr = NULL;
56
57 list->chk = psi_plist_init(NULL);
58
59 while (psi_plist_get(list->cur, i++, &ptr)) {
60 *dst->last_error = 0;
61
62 PSI_DEBUG_PRINT(dst, "PSI: validate %s ", "»");
63 if (validate(scope, dst, ptr)) {
64 PSI_DEBUG_PRINT(dst, " %s\n", "✔");
65 *list->dst = psi_plist_add(*list->dst, &ptr);
66 } else {
67 PSI_DEBUG_PRINT(dst, " %s (%s)\n", "✘", dst->last_error);
68 list->chk = psi_plist_add(list->chk, &ptr);
69 }
70 }
71
72 if (list->cur != list->src) {
73 psi_plist_free(list->cur);
74 }
75 list->cur = list->chk;
76 }
77 }
78
79 static bool psi_validate_type(struct psi_validate_scope *scope,
80 struct psi_data *dst, void *ptr)
81 {
82 struct psi_decl_arg *def = ptr;
83
84 PSI_DEBUG_PRINT(dst, "typedef %s", def->var->name->val);
85 return psi_decl_arg_validate_typedef(dst, def, scope);
86 }
87
88 static bool psi_validate_struct(struct psi_validate_scope *scope,
89 struct psi_data *dst, void *ptr)
90 {
91 struct psi_decl_struct *str = ptr;
92
93 PSI_DEBUG_PRINT(dst, "struct %s", str->name->val);
94 if (psi_decl_struct_validate(dst, str, scope)) {
95 PSI_DEBUG_PRINT(dst, "::(%zu, %zu)", str->align, str->size);
96 return true;
97 }
98 return false;
99 }
100
101 static bool psi_validate_union(struct psi_validate_scope *scope,
102 struct psi_data *dst, void *ptr)
103 {
104 struct psi_decl_union *unn = ptr;
105
106 PSI_DEBUG_PRINT(dst, "union %s", unn->name->val);
107 if (psi_decl_union_validate(dst, unn, scope)) {
108 PSI_DEBUG_PRINT(dst, "::(%zu, %zu)", unn->align, unn->size);
109 return true;
110 }
111 return false;
112 }
113
114 static bool psi_validate_enum(struct psi_validate_scope *scope,
115 struct psi_data *dst, void *ptr)
116 {
117 struct psi_decl_enum *enm = ptr;
118
119 PSI_DEBUG_PRINT(dst, "enum %s", enm->name->val);
120 return psi_decl_enum_validate(dst, enm);
121 }
122
123 static bool psi_validate_extvar(struct psi_validate_scope *scope,
124 struct psi_data *dst, void *ptr)
125 {
126 struct psi_decl_extvar *evar = ptr;
127
128 PSI_DEBUG_PRINT(dst, "extvar %s", evar->arg->var->name->val);
129 if (psi_decl_extvar_validate(dst, evar, scope)) {
130 dst->decls = psi_plist_add(dst->decls, &evar->getter);
131 dst->decls = psi_plist_add(dst->decls, &evar->setter);
132 return true;
133 }
134 return false;
135 }
136
137 static bool psi_validate_decl(struct psi_validate_scope *scope,
138 struct psi_data *dst, void *ptr)
139 {
140 struct psi_decl *decl = ptr;
141
142 PSI_DEBUG_PRINT(dst, "decl %s", decl->func->var->name->val);
143 return psi_decl_validate(dst, decl, scope);
144 }
145
146 static bool psi_validate_const(struct psi_validate_scope *scope,
147 struct psi_data *dst, void *ptr)
148 {
149 struct psi_const *cnst = ptr;
150
151 PSI_DEBUG_PRINT(dst, "constant %s", cnst->name->val);
152 return psi_const_validate(dst, cnst, scope);
153 }
154
155 static bool psi_validate_impl(struct psi_validate_scope *scope,
156 struct psi_data *dst, void *ptr)
157 {
158 struct psi_impl *impl = ptr;
159
160 PSI_DEBUG_PRINT(dst, "impl %s", impl->func->name->val);
161 return psi_impl_validate(dst, impl, scope);
162 }
163
164 bool psi_validate(struct psi_validate_scope *scope,
165 struct psi_data *dst, struct psi_data *src)
166 {
167 struct psi_validate_list types = {src->types, &dst->types};
168 struct psi_validate_list structs = {src->structs, &dst->structs};
169 struct psi_validate_list unions = {src->unions, &dst->unions};
170 struct psi_validate_list enums = {src->enums, &dst->enums};
171 struct psi_validate_list vars = {src->vars, &dst->vars};
172 struct psi_validate_list decls = {src->decls, &dst->decls};
173 struct psi_validate_list consts = {src->consts, &dst->consts};
174 struct psi_validate_list impls = {src->impls, &dst->impls};
175 unsigned flags = dst->flags;
176 size_t check_count = ~0;
177
178 /* fail early if libraries are not found */
179 if (!psi_decl_file_validate(dst, src)) {
180 return false;
181 }
182
183 /* silence loop */
184 dst->flags |= PSI_SILENT;
185
186 while (true) {
187 size_t count_all = psi_plist_count(types.cur ?: types.src)
188 + psi_plist_count(structs.cur ?: structs.src)
189 + psi_plist_count(unions.cur ?: unions.src)
190 + psi_plist_count(enums.cur ?: enums.src)
191 + psi_plist_count(vars.cur ?: vars.src)
192 + psi_plist_count(decls.cur ?: decls.src);
193
194 if (check_count != count_all) {
195 check_count = count_all;
196
197 PSI_DEBUG_PRINT(dst,
198 "PSI: validate data(%p) %zu type checks remaining\n",
199 src, check_count);
200
201 psi_validate_list(scope, dst, &types, psi_validate_type);
202 psi_validate_list(scope, dst, &structs, psi_validate_struct);
203 psi_validate_list(scope, dst, &unions, psi_validate_union);
204 psi_validate_list(scope, dst, &enums, psi_validate_enum);
205 psi_validate_list(scope, dst, &vars, psi_validate_extvar);
206 psi_validate_list(scope, dst, &decls, psi_validate_decl);
207 continue;
208 }
209
210 /* nothing changed; bail out */
211 if (count_all) {
212 src->errors += count_all;
213
214 if ((dst->flags & PSI_SILENT) && !(flags & PSI_SILENT)) {
215 /* one last error-spitting round, if not explicitly suppressed */
216 dst->flags ^= PSI_SILENT;
217 check_count = ~0;
218
219 PSI_DEBUG_PRINT(dst, "PSI: validation bail out with %zu"
220 " type checks remaining, errors follow\n", count_all);
221 continue;
222 }
223 }
224
225 break;
226 }
227
228 /* reset original flags */
229 dst->flags = flags;
230
231 psi_validate_list(scope, dst, &consts, psi_validate_const);
232 psi_validate_list(scope, dst, &impls, psi_validate_impl);
233
234 if (types.cur != types.src) {
235 psi_plist_free(types.cur);
236 }
237 if (structs.cur != structs.src) {
238 psi_plist_free(structs.cur);
239 }
240 if (unions.cur != unions.src) {
241 psi_plist_free(unions.cur);
242 }
243 if (enums.cur != enums.src) {
244 psi_plist_free(enums.cur);
245 }
246 if (vars.cur != vars.src) {
247 psi_plist_free(vars.cur);
248 }
249 if (decls.cur != decls.src) {
250 psi_plist_free(decls.cur);
251 }
252 if (consts.cur != consts.src) {
253 psi_plist_free(consts.cur);
254 }
255 if (impls.cur != impls.src) {
256 psi_plist_free(impls.cur);
257 }
258
259 return true;
260 }