1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
29 #include "php_globals.h"
34 struct psi_data
*psi_data_ctor_with_dtors(struct psi_data
*data
,
35 psi_error_cb error
, unsigned flags
)
38 data
= calloc(1, sizeof(*data
));
45 data
->consts
= psi_plist_init((psi_plist_dtor
) psi_const_free
);
48 data
->types
= psi_plist_init((psi_plist_dtor
) psi_decl_arg_free
);
51 data
->structs
= psi_plist_init((psi_plist_dtor
) psi_decl_struct_free
);
54 data
->unions
= psi_plist_init((psi_plist_dtor
) psi_decl_union_free
);
57 data
->enums
= psi_plist_init((psi_plist_dtor
) psi_decl_enum_free
);
60 data
->decls
= psi_plist_init((psi_plist_dtor
) psi_decl_free
);
63 data
->vars
= psi_plist_init((psi_plist_dtor
) psi_decl_extvar_free
);
66 data
->impls
= psi_plist_init((psi_plist_dtor
) psi_impl_free
);
69 data
->libs
= psi_plist_init((psi_plist_dtor
) psi_libs_free
);
74 struct psi_data
*psi_data_ctor(struct psi_data
*data
, psi_error_cb error
,
78 data
= calloc(1, sizeof(*data
));
85 data
->consts
= psi_plist_init(NULL
);
88 data
->types
= psi_plist_init(NULL
);
91 data
->structs
= psi_plist_init(NULL
);
94 data
->unions
= psi_plist_init(NULL
);
97 data
->enums
= psi_plist_init(NULL
);
100 data
->decls
= psi_plist_init(NULL
);
103 data
->vars
= psi_plist_init(NULL
);
106 data
->impls
= psi_plist_init(NULL
);
109 data
->libs
= psi_plist_init(NULL
);
114 struct psi_data
*psi_data_exchange(struct psi_data
*dest
, struct psi_data
*src
)
117 dest
= malloc(sizeof(*dest
));
120 memset(src
, 0, sizeof(*src
));
124 void psi_data_dtor(struct psi_data
*data
)
127 psi_plist_free(data
->consts
);
130 psi_plist_free(data
->types
);
133 psi_plist_free(data
->structs
);
136 psi_plist_free(data
->unions
);
139 psi_plist_free(data
->enums
);
142 psi_plist_free(data
->decls
);
145 psi_plist_free(data
->vars
);
148 psi_plist_free(data
->impls
);
151 psi_plist_free(data
->libs
);
154 psi_decl_file_dtor(&data
->file
);
157 void psi_data_dump(int fd
, struct psi_data
*D
)
160 dprintf(fd
, "// filename=%s (%u errors)\n", D
->file
.fn
, D
->errors
);
162 dprintf(fd
, "lib \"%s\";\n", D
->file
.ln
);
165 dprintf(fd
, "// builtin predef\n");
167 if (psi_plist_count(D
->types
)) {
169 struct psi_decl_arg
*def
;
171 while (psi_plist_get(D
->types
, i
++, &def
)) {
172 dprintf(fd
, "typedef ");
173 psi_decl_arg_dump(fd
, def
, 0);
178 if (psi_plist_count(D
->unions
)) {
180 struct psi_decl_union
*unn
;
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
);
190 if (psi_plist_count(D
->structs
)) {
192 struct psi_decl_struct
*strct
;
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
);
202 if (psi_plist_count(D
->enums
)) {
204 struct psi_decl_enum
*enm
;
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);
214 if (psi_plist_count(D
->consts
)) {
218 while (psi_plist_get(D
->consts
, i
++, &c
)) {
219 psi_const_dump(fd
, c
);
224 if (psi_plist_count(D
->decls
)) {
226 struct psi_decl
*decl
;
228 while (psi_plist_get(D
->decls
, i
++, &decl
)) {
229 psi_decl_dump(fd
, decl
);
234 if (psi_plist_count(D
->vars
)) {
236 struct psi_decl_extvar
*evar
;
238 while (psi_plist_get(D
->vars
, i
++, &evar
)) {
239 psi_decl_extvar_dump(fd
, evar
);
243 if (psi_plist_count(D
->impls
)) {
245 struct psi_impl
*impl
;
247 while (psi_plist_get(D
->impls
, i
++, &impl
)) {
248 psi_impl_dump(fd
, impl
);
255 bool psi_data_validate(struct psi_data
*dst
, struct psi_data
*src
)
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
;
269 /* fail early if library is not found */
270 if (!psi_decl_file_validate(dst
, src
, &dlopened
)) {
274 psi_validate_stack_ctor(&type_stack
);
280 dst
->flags
|= PSI_SILENT
;
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
;
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
;
305 PSI_DEBUG_PRINT(dst
, "PSI: validation bail out with %zu"
306 " type checks remaining, errors follow\n", count_all
);
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
;
318 check_count
= count_all
;
319 src
->errors
= errors
+ check_count
;
321 PSI_DEBUG_PRINT(dst
, "PSI: validate data(%p) %zu type checks remaining\n",
326 struct psi_decl_arg
*def
;
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", "✔");
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
);
343 struct psi_decl_struct
*str
;
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
);
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
);
360 struct psi_decl_union
*unn
;
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
);
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
);
377 struct psi_decl_enum
*enm
;
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
);
386 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
387 recheck_enums
= psi_plist_add(recheck_enums
, &enm
);
393 struct psi_decl_extvar
*evar
;
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
);
404 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
405 recheck_vars
= psi_plist_add(recheck_vars
, &evar
);
411 struct psi_decl
*decl
;
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
);
420 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
421 recheck_decls
= psi_plist_add(recheck_decls
, &decl
);
427 if (check_types
&& check_types
!= src
->types
) {
428 psi_plist_free(check_types
);
430 check_types
= recheck_types
;
431 if (check_structs
&& check_structs
!= src
->structs
) {
432 psi_plist_free(check_structs
);
434 check_structs
= recheck_structs
;
435 if (check_unions
&& check_unions
!= src
->unions
) {
436 psi_plist_free(check_unions
);
438 check_unions
= recheck_unions
;
439 if (check_enums
&& check_enums
!= src
->enums
) {
440 psi_plist_free(check_enums
);
442 check_enums
= recheck_enums
;
443 if (check_vars
&& check_vars
!= src
->vars
) {
444 psi_plist_free(check_vars
);
446 check_vars
= recheck_vars
;
447 if (check_decls
&& check_decls
!= src
->decls
) {
448 psi_plist_free(check_decls
);
450 check_decls
= recheck_decls
;
453 /* reset original flags */
458 struct psi_decl_struct
*str
;
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
;
469 sname
= malloc(slen
+ nlen
+ 1);
470 strcpy(sname
, "psi\\SIZEOF_STRUCT_");
471 aname
= malloc(alen
+ nlen
+ 1);
472 strcpy(aname
, "psi\\ALIGNOF_STRUCT_");
476 size_t off
= nptr
- str
->name
;
477 sname
[slen
- 1 + off
] = aname
[alen
- 1 + off
] = toupper(*nptr
++);
479 sname
[slen
- 1 + nlen
] = aname
[alen
- 1 + nlen
] = 0;
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
);
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
);
499 struct psi_const
*cnst
;
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
);
508 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
516 struct psi_impl
*impl
;
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
);
525 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
531 psi_validate_stack_dtor(&type_stack
);