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