marshal: fix enum return value
[m6w6/ext-psi] / src / data.c
1 /*******************************************************************************
2 Copyright (c) 2016, 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 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include "php_globals.h"
30
31 #include <dlfcn.h>
32 #include <ctype.h>
33
34 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data,
35 psi_error_cb error, unsigned flags)
36 {
37 if (!data) {
38 data = calloc(1, sizeof(*data));
39 }
40
41 data->error = error;
42 data->flags = flags;
43
44 if (!data->consts) {
45 data->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
46 }
47 if (!data->types) {
48 data->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
49 }
50 if (!data->structs) {
51 data->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
52 }
53 if (!data->unions) {
54 data->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
55 }
56 if (!data->enums) {
57 data->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
58 }
59 if (!data->decls) {
60 data->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
61 }
62 if (!data->impls) {
63 data->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
64 }
65 if (!data->libs) {
66 data->libs = psi_plist_init((psi_plist_dtor) psi_libs_free);
67 }
68 return data;
69 }
70
71 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error,
72 unsigned flags)
73 {
74 if (!data) {
75 data = calloc(1, sizeof(*data));
76 }
77
78 data->error = error;
79 data->flags = flags;
80
81 if (!data->consts) {
82 data->consts = psi_plist_init(NULL);
83 }
84 if (!data->types) {
85 data->types = psi_plist_init(NULL);
86 }
87 if (!data->structs) {
88 data->structs = psi_plist_init(NULL);
89 }
90 if (!data->unions) {
91 data->unions = psi_plist_init(NULL);
92 }
93 if (!data->enums) {
94 data->enums = psi_plist_init(NULL);
95 }
96 if (!data->decls) {
97 data->decls = psi_plist_init(NULL);
98 }
99 if (!data->impls) {
100 data->impls = psi_plist_init(NULL);
101 }
102 if (!data->libs) {
103 data->libs = psi_plist_init(NULL);
104 }
105 return data;
106 }
107
108 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src)
109 {
110 if (!dest) {
111 dest = malloc(sizeof(*dest));
112 }
113 *dest = *src;
114 memset(src, 0, sizeof(*src));
115 return dest;
116 }
117
118 void psi_data_dtor(struct psi_data *data)
119 {
120 if (data->consts) {
121 psi_plist_free(data->consts);
122 }
123 if (data->types) {
124 psi_plist_free(data->types);
125 }
126 if (data->structs) {
127 psi_plist_free(data->structs);
128 }
129 if (data->unions) {
130 psi_plist_free(data->unions);
131 }
132 if (data->enums) {
133 psi_plist_free(data->enums);
134 }
135 if (data->decls) {
136 psi_plist_free(data->decls);
137 }
138 if (data->impls) {
139 psi_plist_free(data->impls);
140 }
141 if (data->libs) {
142 psi_plist_free(data->libs);
143 }
144
145 psi_decl_file_dtor(&data->file);
146 }
147
148 void psi_data_dump(int fd, struct psi_data *D)
149 {
150 if (D->file.fn) {
151 dprintf(fd, "// filename=%s (%u errors)\n", D->file.fn, D->errors);
152 if (D->file.ln) {
153 dprintf(fd, "lib \"%s\";\n", D->file.ln);
154 }
155 } else {
156 dprintf(fd, "// builtin predef\n");
157 }
158 if (psi_plist_count(D->types)) {
159 size_t i = 0;
160 struct psi_decl_arg *def;
161
162 while (psi_plist_get(D->types, i++, &def)) {
163 dprintf(fd, "typedef ");
164 psi_decl_arg_dump(fd, def, 0);
165 dprintf(fd, ";\n");
166 }
167 dprintf(fd, "\n");
168 }
169 if (psi_plist_count(D->unions)) {
170 size_t i = 0;
171 struct psi_decl_union *unn;
172
173 while (psi_plist_get(D->unions, i++, &unn)) {
174 if (!psi_decl_type_is_anon(unn->name, "union")) {
175 psi_decl_union_dump(fd, unn);
176 dprintf(fd, "\n");
177 }
178 }
179 dprintf(fd, "\n");
180 }
181 if (psi_plist_count(D->structs)) {
182 size_t i = 0;
183 struct psi_decl_struct *strct;
184
185 while (psi_plist_get(D->structs, i++, &strct)) {
186 if (!psi_decl_type_is_anon(strct->name, "struct")) {
187 psi_decl_struct_dump(fd, strct);
188 dprintf(fd, "\n");
189 }
190 }
191 dprintf(fd, "\n");
192 }
193 if (psi_plist_count(D->enums)) {
194 size_t i = 0;
195 struct psi_decl_enum *enm;
196
197 while (psi_plist_get(D->enums, i++, &enm)) {
198 if (!psi_decl_type_is_anon(enm->name, "enum")) {
199 psi_decl_enum_dump(fd, enm, 0);
200 dprintf(fd, "\n");
201 }
202 }
203 dprintf(fd, "\n");
204 }
205 if (psi_plist_count(D->consts)) {
206 size_t i = 0;
207 struct psi_const *c;
208
209 while (psi_plist_get(D->consts, i++, &c)) {
210 psi_const_dump(fd, c);
211 dprintf(fd, "\n");
212 }
213 dprintf(fd, "\n");
214 }
215 if (psi_plist_count(D->decls)) {
216 size_t i = 0;
217 struct psi_decl *decl;
218
219 while (psi_plist_get(D->decls, i++, &decl)) {
220 psi_decl_dump(fd, decl);
221 dprintf(fd, "\n");
222 }
223 dprintf(fd, "\n");
224 }
225 if (psi_plist_count(D->impls)) {
226 size_t i = 0;
227 struct psi_impl *impl;
228
229 while (psi_plist_get(D->impls, i++, &impl)) {
230 psi_impl_dump(fd, impl);
231 dprintf(fd, "\n");
232 }
233 dprintf(fd, "\n");
234 }
235 }
236
237 bool psi_data_validate(struct psi_data *dst, struct psi_data *src)
238 {
239 void *dlopened = NULL;
240 size_t check_count = ~0;
241 struct psi_plist *check_types = src->types;
242 struct psi_plist *check_structs = src->structs;
243 struct psi_plist *check_unions = src->unions;
244 struct psi_plist *check_enums = src->enums;
245 struct psi_plist *check_decls = src->decls;
246 unsigned flags = dst->flags;
247 unsigned errors = src->errors;
248 struct psi_validate_stack type_stack;
249
250 /* fail early if library is not found */
251 if (!psi_decl_file_validate(dst, src, &dlopened)) {
252 return false;
253 }
254
255 psi_validate_stack_ctor(&type_stack);
256
257 dst->flags |= PSI_SILENT;
258
259 while (check_count) {
260 struct psi_plist *recheck_types;
261 struct psi_plist *recheck_structs;
262 struct psi_plist *recheck_unions;
263 struct psi_plist *recheck_enums;
264 struct psi_plist *recheck_decls;
265 size_t count_types = psi_plist_count(check_types);
266 size_t count_structs = psi_plist_count(check_structs);
267 size_t count_unions = psi_plist_count(check_unions);
268 size_t count_enums = psi_plist_count(check_enums);
269 size_t count_decls = psi_plist_count(check_decls);
270 size_t count_all = count_types + count_structs + count_unions
271 + count_enums + count_decls;
272
273 if (check_count == count_all) {
274 /* nothing changed; bail out */
275 if (count_all && (dst->flags & PSI_SILENT) && !(flags & PSI_SILENT)) {
276 /* one last error-spitting round, if not explicitly suppressed */
277 dst->flags ^= PSI_SILENT;
278 check_count = ~0;
279
280 PSI_DEBUG_PRINT(dst, "PSI: validation bail out with %zu"
281 " type checks remaining, errors follow\n", count_all);
282 continue;
283 }
284 check_count = 0;
285 } else {
286 recheck_types = count_types ? psi_plist_init(NULL) : NULL;
287 recheck_structs = count_structs ? psi_plist_init(NULL) : NULL;
288 recheck_unions = count_unions ? psi_plist_init(NULL) : NULL;
289 recheck_enums = count_enums ? psi_plist_init(NULL) : NULL;
290 recheck_decls = count_decls ? psi_plist_init(NULL) : NULL;
291
292 check_count = count_all;
293 src->errors = errors + check_count;
294
295 PSI_DEBUG_PRINT(dst, "PSI: validate data(%p) %zu type checks remaining\n",
296 src, check_count);
297
298 if (count_types) {
299 size_t i = 0;
300 struct psi_decl_arg *def;
301
302 while (psi_plist_get(check_types, i++, &def)) {
303 *dst->last_error = 0;
304 dst->types = psi_plist_add(dst->types, &def);
305 PSI_DEBUG_PRINT(dst, "PSI: validate typedef %s ", def->var->name);
306 if (psi_decl_arg_validate_typedef(PSI_DATA(dst), def, &type_stack)) {
307 PSI_DEBUG_PRINT(dst, "%s\n", "✔");
308 } else {
309 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
310 recheck_types = psi_plist_add(recheck_types, &def);
311 psi_plist_pop(dst->types, NULL);
312 }
313 }
314 }
315 if (count_structs) {
316 size_t i = 0;
317 struct psi_decl_struct *str;
318
319 while (psi_plist_get(check_structs, i++, &str)) {
320 *dst->last_error = 0;
321 dst->structs = psi_plist_add(dst->structs, &str);
322 PSI_DEBUG_PRINT(dst, "PSI: validate struct %s ", str->name);
323 if (psi_decl_struct_validate(PSI_DATA(dst), str, &type_stack)) {
324 PSI_DEBUG_PRINT(dst, "%s ::(%zu, %zu)\n", "✔", str->align, str->size);
325 } else {
326 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
327 recheck_structs = psi_plist_add(recheck_structs, &str);
328 psi_plist_pop(dst->structs, NULL);
329 }
330 }
331 }
332 if (count_unions) {
333 size_t i = 0;
334 struct psi_decl_union *unn;
335
336 while (psi_plist_get(check_unions, i++, &unn)) {
337 *dst->last_error = 0;
338 dst->unions = psi_plist_add(dst->unions, &unn);
339 PSI_DEBUG_PRINT(dst, "PSI: validate union %s ", unn->name);
340 if (psi_decl_union_validate(PSI_DATA(dst), unn, &type_stack)) {
341 PSI_DEBUG_PRINT(dst, "%s ::(%zu, %zu)\n", "✔", unn->align, unn->size);
342 } else {
343 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
344 recheck_unions = psi_plist_add(recheck_unions, &unn);
345 psi_plist_pop(dst->unions, NULL);
346 }
347 }
348 }
349 if (count_enums) {
350 size_t i = 0;
351 struct psi_decl_enum *enm;
352
353 while (psi_plist_get(check_enums, i++, &enm)) {
354 *dst->last_error = 0;
355 PSI_DEBUG_PRINT(dst, "PSI: validate enum %s ", enm->name);
356 if (psi_decl_enum_validate(PSI_DATA(dst), enm)) {
357 PSI_DEBUG_PRINT(dst, "%s\n", "✔");
358 dst->enums = psi_plist_add(dst->enums, &enm);
359 } else {
360 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
361 recheck_enums = psi_plist_add(recheck_enums, &enm);
362 }
363 }
364 }
365 if (count_decls) {
366 size_t i = 0;
367 struct psi_decl *decl;
368
369 while (psi_plist_get(check_decls, i++, &decl)) {
370 *dst->last_error = 0;
371 PSI_DEBUG_PRINT(dst, "PSI: validate decl %s ", decl->func->var->name);
372 if (psi_decl_validate(PSI_DATA(dst), decl, dlopened, &type_stack)) {
373 PSI_DEBUG_PRINT(dst, "%s\n", "✔");
374 dst->decls = psi_plist_add(dst->decls, &decl);
375 } else {
376 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
377 recheck_decls = psi_plist_add(recheck_decls, &decl);
378 }
379 }
380 }
381 }
382
383 if (check_types && check_types != src->types) {
384 psi_plist_free(check_types);
385 }
386 check_types = recheck_types;
387 if (check_structs && check_structs != src->structs) {
388 psi_plist_free(check_structs);
389 }
390 check_structs = recheck_structs;
391 if (check_unions && check_unions != src->unions) {
392 psi_plist_free(check_unions);
393 }
394 check_unions = recheck_unions;
395 if (check_enums && check_enums != src->enums) {
396 psi_plist_free(check_enums);
397 }
398 check_enums = recheck_enums;
399 if (check_decls && check_decls != src->decls) {
400 psi_plist_free(check_decls);
401 }
402 check_decls = recheck_decls;
403 }
404
405 /* reset original flags */
406 dst->flags = flags;
407
408 if (dst->structs) {
409 size_t i = 0;
410 struct psi_decl_struct *str;
411
412 while (psi_plist_get(dst->structs, i++, &str)) {
413 size_t nlen = strlen(str->name);
414 size_t slen = sizeof("psi\\SIZEOF_STRUCT_");
415 size_t alen = sizeof("psi\\ALIGNOF_STRUCT_");
416 char *nptr = str->name, *sname, *aname;
417 struct psi_const *cnst;
418 struct psi_const_type *ctyp;
419 struct psi_impl_def_val *cval;
420
421 sname = malloc(slen + nlen + 1);
422 strcpy(sname, "psi\\SIZEOF_STRUCT_");
423 aname = malloc(alen + nlen + 1);
424 strcpy(aname, "psi\\ALIGNOF_STRUCT_");
425
426 nptr = str->name;
427 while (*nptr) {
428 size_t off = nptr - str->name;
429 sname[slen - 1 + off] = aname[alen - 1 + off] = toupper(*nptr++);
430 }
431 sname[slen - 1 + nlen] = aname[alen - 1 + nlen] = 0;
432
433 ctyp = psi_const_type_init(PSI_T_INT, "int");
434 cval = psi_impl_def_val_init(PSI_T_INT, NULL);
435 cval->ival.zend.lval = str->size;
436 cnst = psi_const_init(ctyp, sname, cval);
437 src->consts = psi_plist_add(src->consts, &cnst);
438 free(sname);
439
440 ctyp = psi_const_type_init(PSI_T_INT, "int");
441 cval = psi_impl_def_val_init(PSI_T_INT, NULL);
442 cval->ival.zend.lval = str->align;
443 cnst = psi_const_init(ctyp, aname, cval);
444 src->consts = psi_plist_add(src->consts, &cnst);
445 free(aname);
446 }
447 }
448
449 if (src->consts) {
450 size_t i = 0;
451 struct psi_const *cnst;
452
453 while (psi_plist_get(src->consts, i++, &cnst)) {
454 *dst->last_error = 0;
455 PSI_DEBUG_PRINT(dst, "PSI: validate const %s ", cnst->name);
456 if (psi_const_validate(PSI_DATA(dst), cnst)) {
457 PSI_DEBUG_PRINT(dst, "%s\n", "✔");
458 dst->consts = psi_plist_add(dst->consts, &cnst);
459 } else {
460 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
461 ++src->errors;
462 }
463 }
464 }
465
466 if (src->impls) {
467 size_t i = 0;
468 struct psi_impl *impl;
469
470 while (psi_plist_get(src->impls, i++, &impl)) {
471 *dst->last_error = 0;
472 PSI_DEBUG_PRINT(dst, "PSI: validate impl %s ", impl->func->name);
473 if (psi_impl_validate(PSI_DATA(dst), impl)) {
474 PSI_DEBUG_PRINT(dst, "%s\n", "✔");
475 dst->impls = psi_plist_add(dst->impls, &impl);
476 } else {
477 PSI_DEBUG_PRINT(dst, "%s (%s)\n", "✘", dst->last_error);
478 ++src->errors;
479 }
480 }
481 }
482
483 psi_validate_stack_dtor(&type_stack);
484
485 return true;
486 }
487