a5d3604d4a9e3e1ee3e43ba03e00e0f18c3e25ac
11 #include "php_psi_stdinc.h"
12 #include "php_psi_stdtypes.h"
13 #include "php_psi_macros.h"
14 #include "php_psi_redirs.h"
21 static int validate_lib(PSI_Data
*data
, void **dlopened
) {
23 const char *ptr
= data
->psi
.file
.ln
;
27 /* FIXME: assume stdlib */
29 } else if (!strchr(ptr
, '/')) {
30 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.%s", ptr
, PHP_PSI_SHLIB_SUFFIX
);
31 if (MAXPATHLEN
== len
) {
32 data
->error(data
, NULL
, PSI_WARNING
, "Library name too long: '%s'", ptr
);
37 if (!(*dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
38 data
->error(data
, NULL
, PSI_WARNING
, "Could not open library '%s': %s.",
39 data
->psi
.file
.ln
, dlerror());
45 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
47 struct psi_std_type
*stdtyp
;
52 if (defs
) for (i
= 0; i
< defs
->count
; ++i
) {
53 decl_arg
*def
= defs
->list
[i
];
55 if (def
->type
->type
!= type
->type
&& !strcmp(def
->var
->name
, type
->name
)) {
56 type
->real
= def
->type
;
60 for (stdtyp
= &psi_std_types
[0]; stdtyp
->type_tag
; ++stdtyp
) {
61 if (!strcmp(type
->name
, stdtyp
->alias
?: stdtyp
->type_name
)) {
62 type
->type
= stdtyp
->type_tag
;
70 static inline int locate_decl_type_struct(decl_structs
*structs
, decl_type
*type
) {
76 if (structs
) for (i
= 0; i
< structs
->count
; ++i
) {
77 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
78 type
->strct
= structs
->list
[i
];
85 static inline int locate_decl_type_union(decl_unions
*unions
, decl_type
*type
) {
91 if (unions
) for (i
= 0; i
< unions
->count
; ++i
) {
92 if (!strcmp(unions
->list
[i
]->name
, type
->name
)) {
93 type
->unn
= unions
->list
[i
];
100 static inline int locate_decl_type_enum(decl_enums
*enums
, decl_type
*type
) {
106 if (enums
) for (i
= 0; i
< enums
->count
; ++i
) {
107 if (!strcmp(enums
->list
[i
]->name
, type
->name
)) {
108 type
->enm
= enums
->list
[i
];
115 static inline int locate_decl_type_decl(decls
*decls
, decl_type
*type
) {
121 if (decls
) for (i
= 0; i
< decls
->count
; ++i
) {
122 if (!strcmp(decls
->list
[i
]->func
->var
->name
, type
->name
)) {
123 type
->func
= decls
->list
[i
];
131 static inline int validate_decl_struct(PSI_Data
*data
, decl_struct
*s
);
132 static inline int validate_decl_union(PSI_Data
*data
, decl_union
*u
);
133 static inline int validate_decl_enum(PSI_Data
*data
, decl_enum
*e
);
135 static inline int validate_decl_type(PSI_Data
*data
, decl_type
*type
) {
136 switch (type
->type
) {
142 if (!locate_decl_type_alias(data
->defs
, type
)) {
146 return validate_decl_type(data
, type
->real
);
150 if (!locate_decl_type_struct(data
->structs
, type
)) {
155 if (!locate_decl_type_union(data
->unions
, type
)) {
160 if (!locate_decl_type_enum(data
->enums
, type
)) {
165 if (!locate_decl_type_decl(data
->decls
, type
)) {
172 static inline int validate_decl_typedef(PSI_Data
*data
, decl_arg
*def
) {
173 if (!validate_decl_type(data
, def
->type
)) {
176 switch (def
->type
->type
) {
177 case PSI_T_STRUCT
: pre
= "struct "; break;
178 case PSI_T_UNION
: pre
= "union "; break;
179 case PSI_T_ENUM
: pre
= "enum "; break;
180 default: pre
= ""; break;
182 data
->error(data
, def
->token
, PSI_WARNING
,
183 "Type '%s' cannot be aliased to %s'%s'",
184 def
->var
->name
, pre
, def
->type
->name
);
187 if (def
->type
->type
== PSI_T_VOID
) {
188 if (def
->var
->pointer_level
) {
189 def
->type
->type
= PSI_T_POINTER
;
191 data
->error(data
, def
->token
, PSI_WARNING
,
192 "Type '%s' cannot be aliased to 'void'",
200 static inline int validate_constant(PSI_Data
*data
, constant
*c
) {
205 static inline int validate_decl_arg(PSI_Data
*data
, decl_arg
*arg
) {
206 if (!validate_decl_type(data
, arg
->type
)) {
207 data
->error(data
, arg
->type
->token
, PSI_WARNING
,
208 "Cannot use '%s' as type for '%s'",
209 arg
->type
->name
, arg
->var
->name
);
212 decl_type
*real
= real_decl_type(arg
->type
);
214 if (real
->type
== PSI_T_FUNCTION
) {
220 static int psi_sort_struct_arg_cmp(const void *_a
, const void *_b
) {
221 decl_arg
*a
= *(decl_arg
**)_a
, *b
= *(decl_arg
**)_b
;
223 if (a
->layout
->pos
== b
->layout
->pos
) {
224 if (a
->layout
->len
== b
->layout
->len
) {
226 } else if (a
->layout
->len
> b
->layout
->len
) {
231 } else if (a
->layout
->pos
> b
->layout
->pos
) {
237 static void psi_sort_struct_arg_swp(void *a
, void *b
) {
238 decl_arg
**_a
= a
, **_b
= b
, *_c
;
244 static inline void psi_sort_struct_args(void **args
, size_t count
) {
245 zend_insert_sort(args
, count
, sizeof(*args
),
246 psi_sort_struct_arg_cmp
, psi_sort_struct_arg_swp
);
249 static inline int validate_decl_struct_darg(PSI_Data
*data
, decl_arg
*darg
, void *current
) {
250 decl_type
*real
= real_decl_type(darg
->type
);
252 /* pre-validate any structs/unions/enums */
253 switch (real
->type
) {
255 if (current
&& current
== real
->strct
) {
258 if (!validate_decl_struct(data
, real
->strct
)) {
263 if (current
&& current
== real
->unn
) {
266 if (!validate_decl_union(data
, real
->unn
)) {
271 if (current
&& current
== real
->enm
) {
274 if (!validate_decl_enum(data
, real
->enm
)) {
283 static inline size_t sizeof_decl_arg(decl_arg
*darg
) {
285 decl_type
*real
= real_decl_type(darg
->type
);
287 if (darg
->var
->array_size
) {
288 if (darg
->var
->pointer_level
> 2) {
289 size
= psi_t_size(PSI_T_POINTER
) * darg
->var
->array_size
;
291 size
= psi_t_size(real
->type
) * darg
->var
->array_size
;
293 } else if (darg
->var
->pointer_level
) {
294 size
= psi_t_size(PSI_T_POINTER
);
296 switch (real
->type
) {
298 size
= real
->unn
->size
;
301 size
= real
->strct
->size
;
305 size
= psi_t_size(real
->type
);
315 static inline size_t alignof_decl_type(decl_type
*t
);
316 static inline size_t alignof_decl_arg(decl_arg
*darg
);
317 static inline size_t alignof_decl_union(decl_union
*u
);
318 static inline size_t alignof_decl_struct(decl_struct
*s
);
320 static inline size_t alignof_decl_args(decl_args
*args
) {
321 size_t i
, maxalign
= 0;
323 for (i
= 0; i
< args
->count
; ++i
) {
324 decl_arg
*darg
= args
->args
[i
];
325 size_t align
= alignof_decl_arg(darg
);
327 if (align
> maxalign
) {
335 static inline size_t alignof_decl_struct(decl_struct
*s
) {
337 s
->align
= alignof_decl_args(s
->args
);
342 static inline size_t alignof_decl_union(decl_union
*u
) {
344 u
->align
= alignof_decl_args(u
->args
);
349 static inline size_t alignof_decl_type(decl_type
*t
) {
350 decl_type
*real
= real_decl_type(t
);
353 switch (real
->type
) {
355 align
= alignof_decl_struct(real
->strct
);
358 align
= alignof_decl_union(real
->unn
);
362 align
= psi_t_alignment(real
->type
);
368 static inline size_t alignof_decl_arg(decl_arg
*darg
) {
371 if (darg
->var
->pointer_level
&& (!darg
->var
->array_size
|| darg
->var
->pointer_level
> 2)) {
372 align
= psi_t_alignment(PSI_T_POINTER
);
374 align
= alignof_decl_type(darg
->type
);
380 static inline size_t align_decl_arg(decl_arg
*darg
, size_t *pos
, size_t *len
) {
381 size_t align
= alignof_decl_arg(darg
);
385 *len
= sizeof_decl_arg(darg
);
386 *pos
= psi_align(align
, *pos
);
391 static inline int validate_decl_struct(PSI_Data
*data
, decl_struct
*s
) {
392 size_t i
, pos
, len
, size
, align
;
394 if (!s
->size
&& !s
->args
->count
) {
395 data
->error(data
, s
->token
, PSI_WARNING
,
396 "Cannot compute size of empty struct '%s'",
401 for (i
= 0; i
< s
->args
->count
; ++i
) {
402 decl_arg
*darg
= s
->args
->args
[i
];
404 if (!validate_decl_arg(data
, darg
)) {
408 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
409 darg
->var
->arg
= darg
;
411 if (!validate_decl_struct_darg(data
, darg
, s
)) {
413 } else if (darg
->layout
) {
414 pos
= darg
->layout
->pos
;
416 align
= align_decl_arg(darg
, &pos
, &len
);
418 if (darg
->layout
->len
!= len
) {
419 data
->error(data
, darg
->token
, PSI_WARNING
,
420 "Computed size %zu of %s.%s does not match"
421 " pre-defined size %zu of type '%s'",
422 len
, s
->name
, darg
->var
->name
, darg
->layout
->len
,
425 if (darg
->layout
->pos
!= pos
) {
426 data
->error(data
, darg
->token
, PSI_WARNING
,
427 "Computed offset %zu of %s.%s does not match"
428 " pre-defined offset %zu",
429 pos
, s
->name
, darg
->var
->name
, darg
->layout
->pos
);
433 pos
= s
->args
->args
[i
-1]->layout
->pos
+
434 s
->args
->args
[i
-1]->layout
->len
;
439 align
= align_decl_arg(darg
, &pos
, &len
);
440 darg
->layout
= init_decl_struct_layout(pos
, len
);
443 if (align
> s
->align
) {
448 psi_sort_struct_args((void **) s
->args
->args
, s
->args
->count
);
450 if (s
->args
->count
) {
451 decl_arg
*darg
= s
->args
->args
[s
->args
->count
-1];
453 size
= darg
->layout
->pos
+ darg
->layout
->len
;
454 if (s
->size
< size
) {
455 s
->size
= psi_align(size
, s
->align
);
462 static inline int validate_decl_union(PSI_Data
*data
, decl_union
*u
) {
463 size_t i
, pos
, len
, size
= 0, align
;
465 if (!u
->size
&& !u
->args
->count
) {
466 data
->error(data
, u
->token
, PSI_WARNING
,
467 "Cannot compute size of empty union %s",
472 for (i
= 0; i
< u
->args
->count
; ++i
) {
473 decl_arg
*darg
= u
->args
->args
[i
];
475 if (!validate_decl_arg(data
, darg
)) {
479 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
480 darg
->var
->arg
= darg
;
482 if (!validate_decl_struct_darg(data
, darg
, u
)) {
484 } else if (darg
->layout
) {
485 pos
= darg
->layout
->pos
;
487 align
= align_decl_arg(darg
, &pos
, &len
);
489 if (darg
->layout
->pos
!= 0) {
490 data
->error(data
, darg
->token
, PSI_WARNING
,
491 "Offset of %s.%s should be 0",
492 u
->name
, darg
->var
->name
);
493 darg
->layout
->pos
= 0;
495 if (darg
->layout
->len
!= len
) {
496 data
->error(data
, darg
->token
, PSI_WARNING
,
497 "Computed size %zu of %s.%s does not match"
498 " pre-defined size %zu of type '%s'",
499 len
, u
->name
, darg
->var
->name
, darg
->layout
->len
,
505 align
= align_decl_arg(darg
, &pos
, &len
);
506 darg
->layout
= init_decl_struct_layout(pos
, len
);
512 if (align
> u
->align
) {
517 psi_sort_struct_args((void **) u
->args
->args
, u
->args
->count
);
519 if (u
->size
< size
) {
520 u
->size
= psi_align(size
, u
->align
);
526 static const char * const abi_ccs
[] = {
528 "extern", /* > - all the same */
534 static inline int validate_decl_abi(PSI_Data
*data
, decl_abi
*abi
) {
537 for (i
= 0; i
< sizeof(abi_ccs
)/sizeof(char*); ++i
) {
538 if (strcasecmp(abi
->convention
, abi_ccs
[i
])) {
544 static inline int validate_decl_func(PSI_Data
*data
, void *dl
, decl
*decl
, decl_arg
*func
)
546 struct psi_func_redir
*redir
;
548 if (!strcmp(func
->var
->name
, "dlsym")) {
549 data
->error(data
, func
->token
, PSI_WARNING
, "Cannot dlsym dlsym (sic!)");
553 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
554 if (!strcmp(func
->var
->name
, redir
->name
)) {
555 decl
->call
.sym
= redir
->func
;
558 if (!decl
->call
.sym
) {
560 # define RTLD_NEXT ((void *) -1l)
562 decl
->call
.sym
= dlsym(dl
?: RTLD_NEXT
, func
->var
->name
);
563 if (!decl
->call
.sym
) {
564 data
->error(data
, func
->token
, PSI_WARNING
,
565 "Failed to locate symbol '%s': %s",
566 func
->var
->name
, dlerror() ?: "not found");
571 static inline int validate_decl_nodl(PSI_Data
*data
, decl
*decl
) {
572 if (!validate_decl_abi(data
, decl
->abi
)) {
573 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
574 "Invalid calling convention: '%s'", decl
->abi
->token
->text
);
577 if (!validate_decl_arg(data
, decl
->func
)) {
583 for (i
= 0; i
< decl
->args
->count
; ++i
) {
584 if (!validate_decl_arg(data
, decl
->args
->args
[i
])) {
591 static inline int validate_decl(PSI_Data
*data
, void *dl
, decl
*decl
) {
592 if (!validate_decl_nodl(data
, decl
)) {
595 if (!validate_decl_func(data
, dl
, decl
, decl
->func
)) {
600 static inline decl_arg
*locate_decl_var_arg(decl_var
*var
, decl_args
*args
, decl_arg
*func
) {
603 if (args
) for (i
= 0; i
< args
->count
; ++i
) {
604 decl_arg
*arg
= args
->args
[i
];
606 if (!strcmp(var
->name
, arg
->var
->name
)) {
607 ZEND_ASSERT(!var
->arg
|| var
->arg
== arg
);
608 return var
->arg
= arg
;
611 if (func
&& !strcmp(var
->name
, func
->var
->name
)) {
612 return var
->arg
= func
;
617 static inline decl_arg
*locate_struct_member(decl_struct
*s
, decl_var
*var
) {
619 return locate_decl_var_arg(var
, s
->args
, NULL
);
624 static inline constant
*locate_num_exp_constant(num_exp
*exp
, constants
*consts
) {
627 for (i
= 0; i
< consts
->count
; ++i
) {
628 constant
*cnst
= consts
->list
[i
];
630 if (!strcmp(cnst
->name
, exp
->u
.numb
)) {
632 return exp
->u
.cnst
= cnst
;
638 static inline decl_enum_item
*locate_num_exp_enum_item_ex(num_exp
*exp
, decl_enum
*e
) {
641 if (e
) for (k
= 0; k
< e
->items
->count
; ++k
) {
642 decl_enum_item
*i
= e
->items
->list
[k
];
644 if (!strcmp(i
->name
, exp
->u
.dvar
->name
)) {
645 free_decl_var(exp
->u
.dvar
);
653 static inline decl_enum_item
*locate_num_exp_enum_item(num_exp
*exp
, decl_enums
*enums
) {
656 if (enums
) for (j
= 0; j
< enums
->count
; ++j
) {
657 decl_enum
*e
= enums
->list
[j
];
658 decl_enum_item
*i
= locate_num_exp_enum_item_ex(exp
, e
);
666 static inline int validate_num_exp(PSI_Data
*data
, num_exp
*exp
, decl_args
*dargs
, decl_arg
*func
, decl_enum
*enm
) {
668 switch (exp
->operator) {
670 exp
->calculator
= psi_calc_add
;
673 exp
->calculator
= psi_calc_sub
;
676 exp
->calculator
= psi_calc_mul
;
679 exp
->calculator
= psi_calc_div
;
681 EMPTY_SWITCH_DEFAULT_CASE();
683 if (!validate_num_exp(data
, exp
->operand
, dargs
, func
, enm
)) {
689 if (!locate_decl_var_arg(exp
->u
.dvar
, dargs
, func
)) {
690 if (!locate_num_exp_enum_item(exp
, data
->enums
) && !locate_num_exp_enum_item_ex(exp
, enm
)) {
691 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown variable '%s' in numeric expression",
698 if (!locate_num_exp_constant(exp
, data
->consts
)) {
699 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown constant '%s' in numeric expression",
712 static inline int validate_decl_enum(PSI_Data
*data
, decl_enum
*e
) {
715 if (!e
->items
|| !e
->items
->count
) {
716 data
->error(data
, e
->token
, PSI_WARNING
, "Empty enum '%s'", e
->name
);
720 for (j
= 0; j
< e
->items
->count
; ++j
) {
721 decl_enum_item
*i
= e
->items
->list
[j
];
725 i
->inc
.t
= PSI_T_NUMBER
;
727 i
->inc
.operator = PSI_T_PLUS
;
728 i
->inc
.operand
= i
->prev
->num
?: &i
->prev
->inc
;
731 i
->inc
.t
= PSI_T_NUMBER
;
736 if (!validate_num_exp(data
, i
->num
, NULL
, NULL
, e
)) {
744 static inline int validate_set_value_handler(set_value
*set
) {
745 switch (set
->func
->type
) {
746 case PSI_T_TO_BOOL
: set
->func
->handler
= psi_to_bool
; break;
747 case PSI_T_TO_INT
: set
->func
->handler
= psi_to_int
; break;
748 case PSI_T_TO_FLOAT
: set
->func
->handler
= psi_to_double
; break;
749 case PSI_T_TO_STRING
: set
->func
->handler
= psi_to_string
; break;
750 case PSI_T_TO_ARRAY
: set
->func
->handler
= psi_to_array
; break;
751 case PSI_T_TO_OBJECT
: set
->func
->handler
= psi_to_object
; break;
752 case PSI_T_VOID
: set
->func
->handler
= psi_to_void
; break;
753 case PSI_T_ZVAL
: set
->func
->handler
= psi_to_zval
; break;
755 if (set
->outer
.set
&& set
->outer
.set
->func
->type
== PSI_T_TO_ARRAY
) {
756 set
->func
->handler
= psi_to_recursive
;
757 set
->inner
= set
->outer
.set
->inner
;
766 static inline void decl_var_arg_v(decl_args
*args
, va_list argp
) {
770 memset(args
, 0, sizeof(*args
));
772 while ((argc
= va_arg(argp
, int))) {
773 argv
= va_arg(argp
, decl_arg
**);
775 add_decl_arg(args
, *argv
++);
779 static inline int validate_set_value_ex(PSI_Data
*data
, set_value
*set
, decl_arg
*ref
, decl_args
*ref_list
) {
782 decl_var
*set_var
= set
->vars
->vars
[0];
784 if (!validate_set_value_handler(set
)) {
785 data
->error(data
, set
->func
->token
, PSI_WARNING
, "Invalid cast '%s' in `set` statement", set
->func
->name
);
789 for (i
= 0; i
< set
->vars
->count
; ++i
) {
790 decl_var
*svar
= set
->vars
->vars
[i
];
791 if (!svar
->arg
&& !locate_decl_var_arg(svar
, ref_list
, NULL
)) {
792 data
->error(data
, svar
->token
, PSI_WARNING
, "Unknown variable '%s' in `set` statement", svar
->name
);
800 ref_type
= real_decl_type(ref
->type
);
802 if (set
->inner
&& set
->inner
->count
) {
803 int is_to_array
= (set
->func
->type
== PSI_T_TO_ARRAY
);
804 int is_pointer_to_struct
= (ref_type
->type
== PSI_T_STRUCT
&& ref
->var
->pointer_level
);
806 if (!is_to_array
&& !is_pointer_to_struct
) {
807 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts only work with "
808 "to_array() casts on structs or pointers: %s(%s...", set
->func
->name
, set
->vars
->vars
[0]->name
);
813 if (!validate_num_exp(data
, set
->num
, ref_list
, ref
, NULL
)) {
818 if (set
->inner
&& ref_type
->type
== PSI_T_STRUCT
) {
819 /* to_array(struct, to_...) */
820 if (!set
->outer
.set
|| set
->outer
.set
->inner
->vals
!= set
->inner
->vals
) {
821 for (i
= 0; i
< set
->inner
->count
; ++i
) {
822 decl_var
*sub_var
= set
->inner
->vals
[i
]->vars
->vars
[0];
823 decl_arg
*sub_ref
= locate_struct_member(ref_type
->strct
, sub_var
);
826 if (!validate_set_value_ex(data
, set
->inner
->vals
[i
], sub_ref
, ref_type
->strct
->args
)) {
832 } else if (set
->inner
&& set
->inner
->count
== 1) {
833 /* to_array(ptr, to_string(*ptr)) */
834 decl_var
*sub_var
= set
->inner
->vals
[0]->vars
->vars
[0];
835 decl_arg
*sub_ref
= locate_decl_var_arg(sub_var
, ref_list
, ref
);
838 if (strcmp(sub_var
->name
, set_var
->name
)) {
839 data
->error(data
, sub_var
->token
, E_WARNING
, "Inner `set` statement casts on pointers must reference the same variable");
842 if (!validate_set_value_ex(data
, set
->inner
->vals
[0], sub_ref
, ref_list
)) {
846 } else if (set
->inner
&& set
->inner
->count
> 1) {
847 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts on pointers may only occur once");
853 static inline int validate_set_value(PSI_Data
*data
, set_value
*set
, ...) {
855 decl_args args
= {0};
859 decl_var_arg_v(&args
, argp
);
862 check
= validate_set_value_ex(data
, set
, NULL
, &args
);
868 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
872 for (i
= 0; i
< decls
->count
; ++i
) {
873 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->set
->vars
->vars
[0]->name
)) {
874 ret
->decl
= decls
->list
[i
]->func
;
875 return decls
->list
[i
];
882 static inline int validate_impl_ret_stmt(PSI_Data
*data
, impl
*impl
) {
885 /* we must have exactly one ret stmt delcaring the native func to call */
886 /* and which type cast to apply */
887 if (impl
->stmts
->ret
.count
!= 1) {
888 if (impl
->stmts
->ret
.count
> 1) {
889 data
->error(data
, impl
->stmts
->ret
.list
[1]->token
, PSI_WARNING
,
890 "Too many `return` statements for implmentation %s;"
891 " found %zu, exactly one is needed",
892 impl
->func
->name
, impl
->stmts
->ret
.count
);
894 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
895 "Missing `return` statement for implementation %s",
901 ret
= impl
->stmts
->ret
.list
[0];
903 if (!(impl
->decl
= locate_impl_decl(data
->decls
, ret
))) {
904 data
->error(data
, ret
->token
, PSI_WARNING
,
905 "Missing declaration '%s' for `return` statment for implementation %s",
906 ret
->set
->vars
->vars
[0]->name
, impl
->func
->name
);
910 if (!validate_set_value(data
, ret
->set
, 1, &ret
->decl
, impl
->decl
->args
? (int) impl
->decl
->args
->count
: 0, impl
->decl
->args
? impl
->decl
->args
->args
: NULL
, 0)) {
914 //impl->decl->impl = impl;
919 static inline impl_arg
*locate_impl_var_arg(impl_var
*var
, impl_args
*args
) {
922 for (i
= 0; i
< args
->count
; ++i
) {
923 impl_arg
*iarg
= args
->args
[i
];
925 if (!strcmp(var
->name
, iarg
->var
->name
)) {
926 return var
->arg
= iarg
;
933 static inline int validate_let_func(PSI_Data
*data
, let_func
*func
, impl
*impl
) {
934 if (impl
->func
->args
) {
935 if (!locate_impl_var_arg(func
->var
, impl
->func
->args
)) {
936 data
->error(data
, func
->var
->token
, PSI_WARNING
,
937 "Unknown variable '$%s' of `let` statement"
938 " for cast '%s' of implementation '%s'",
939 func
->var
->name
, func
->name
, impl
->func
->name
);
943 switch (func
->type
) {
944 case PSI_T_BOOLVAL
: func
->handler
= psi_let_boolval
; break;
945 case PSI_T_INTVAL
: func
->handler
= psi_let_intval
; break;
946 case PSI_T_FLOATVAL
: func
->handler
= psi_let_floatval
; break;
947 case PSI_T_STRVAL
: func
->handler
= psi_let_strval
; break;
948 case PSI_T_STRLEN
: func
->handler
= psi_let_strlen
; break;
949 case PSI_T_PATHVAL
: func
->handler
= psi_let_pathval
; break;
950 case PSI_T_ARRVAL
: func
->handler
= psi_let_arrval
; break;
951 case PSI_T_OBJVAL
: func
->handler
= psi_let_objval
; break;
952 case PSI_T_ZVAL
: func
->handler
= psi_let_zval
; break;
953 EMPTY_SWITCH_DEFAULT_CASE();
958 static inline int validate_let_callback(PSI_Data
*data
, decl_var
*cb_var
, let_callback
*cb
, impl
*impl
) {
961 decl_type
*cb_type
= real_decl_type(cb_var
->arg
->type
);
963 if (!validate_let_func(data
, cb
->func
, impl
)) {
967 if (cb_type
->type
!= PSI_T_FUNCTION
) {
968 data
->error(data
, cb_var
->token
, PSI_WARNING
, "Not a function: %s", cb_var
->name
);
971 cb_func
= cb_type
->func
;
972 for (i
= 0; i
< cb
->args
->count
; ++i
) {
973 if (!validate_set_value(data
, cb
->args
->vals
[i
], cb_func
->args
->count
, cb_func
->args
->args
, 0)) {
978 if (!validate_decl_nodl(data
, cb_func
)) {
987 static inline int validate_impl_let_stmts(PSI_Data
*data
, impl
*impl
) {
989 /* we can have multiple let stmts */
991 /* check that we have a decl arg for every let stmt */
992 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
993 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
996 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
997 let_var
= let
->val
->data
.var
;
1002 if (!locate_decl_var_arg(let_var
, impl
->decl
->args
, impl
->decl
->func
)) {
1003 data
->error(data
, let_var
->token
, PSI_WARNING
, "Unknown variable '%s' in `let` statement"
1004 " of implementation '%s'", let_var
->name
, impl
->func
->name
);
1008 switch (let
->val
->kind
) {
1012 /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */
1013 /* e.g. let foo = *bar; */
1014 let
->var
->pointer_level
= let
->val
->data
.var
->pointer_level
;
1015 let
->var
->arg
= init_decl_arg(
1017 real_decl_type(let
->val
->data
.var
->arg
->type
)->type
,
1018 real_decl_type(let
->val
->data
.var
->arg
->type
)->name
),
1021 let
->var
->pointer_level
,
1022 let
->var
->array_size
));
1024 case PSI_LET_NUMEXP
:
1025 if (!validate_num_exp(data
, let
->val
->data
.num
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1029 case PSI_LET_CALLOC
:
1030 if (!validate_num_exp(data
, let
->val
->data
.alloc
->nmemb
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1033 if (!validate_num_exp(data
, let
->val
->data
.alloc
->size
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1037 case PSI_LET_CALLBACK
:
1038 if (!validate_let_callback(data
, let
->var
, let
->val
->data
.callback
, impl
)) {
1043 if (!validate_let_func(data
, let
->val
->data
.func
, impl
)) {
1050 /* check that we have a let stmt for every decl arg */
1051 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
1052 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
1055 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
1056 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
1058 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
1064 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1065 "Missing `let` statement for arg '%s %.*s%s'"
1066 " of declaration '%s' for implementation '%s'",
1067 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
1068 darg
->var
->name
, impl
->decl
->func
->var
->name
, impl
->func
->name
);
1075 static inline int validate_impl_set_stmts(PSI_Data
*data
, impl
*impl
) {
1077 /* we can have any count of set stmts; processing out vars */
1078 /* check that set stmts reference known variables */
1079 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
1080 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
1083 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
1084 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
1086 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
1093 data
->error(data
, set
->var
->token
, PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
1094 " of implementation '%s'",
1095 set
->var
->name
, impl
->func
->name
);
1099 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
1100 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
1103 if (impl
->decl
->args
) {
1104 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1105 decl_arg
*set_arg
= impl
->decl
->args
->args
[k
];
1107 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
1109 set_var
->arg
= set_arg
;
1110 if (!validate_set_value(data
, set
->val
, 1, &set_arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1118 for (k
= 0; k
< impl
->stmts
->let
.count
; ++k
) {
1119 let_stmt
*let
= impl
->stmts
->let
.list
[k
];
1121 /* check temp vars */
1122 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
1123 if (!strcmp(set_var
->name
, let
->var
->name
)) {
1125 set_var
->arg
= let
->var
->arg
;
1126 if (!validate_set_value(data
, set
->val
, 1, &set_var
->arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1136 data
->error(data
, set_var
->token
, PSI_WARNING
, "Unknown value '%s' of `set` statement"
1137 " for variable '$%s' of implementation '%s'",
1138 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
1145 static inline decl
*locate_free_decl(decls
*decls
, free_call
*f
) {
1149 for (i
= 0; i
< decls
->count
; ++i
) {
1150 if (!strcmp(decls
->list
[i
]->func
->var
->name
, f
->func
)) {
1151 f
->decl
= decls
->list
[i
];
1152 return decls
->list
[i
];
1159 static inline int validate_impl_free_stmts(PSI_Data
*data
, impl
*impl
) {
1161 /* we can have any count of free stmts; freeing any out vars */
1162 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
1163 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1165 for (j
= 0; j
< fre
->calls
->count
; ++j
) {
1166 free_call
*free_call
= fre
->calls
->list
[j
];
1168 /* first find the decl of the free func */
1169 if (!locate_free_decl(data
->decls
, free_call
)) {
1170 data
->error(data
, free_call
->token
, PSI_WARNING
,
1171 "Missing declaration '%s' in `free` statement"
1172 " of implementation '%s'",
1173 free_call
->func
, impl
->func
->name
);
1179 /* now check for known vars */
1180 for (l
= 0; l
< free_call
->vars
->count
; ++l
) {
1182 decl_var
*free_var
= free_call
->vars
->vars
[l
];
1184 if (!strcmp(free_var
->name
, impl
->decl
->func
->var
->name
)) {
1186 free_var
->arg
= impl
->decl
->func
;
1187 } else if (impl
->decl
->args
) {
1188 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1189 decl_arg
*free_arg
= impl
->decl
->args
->args
[k
];
1191 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
1193 free_var
->arg
= free_arg
;
1200 data
->error(data
, free_var
->token
, PSI_WARNING
,
1201 "Unknown variable '%s' of `free` statement"
1202 " of implementation '%s'",
1203 free_var
->name
, impl
->func
->name
);
1211 static inline int validate_impl_stmts(PSI_Data
*data
, impl
*impl
) {
1213 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1214 "Missing body for implementation %s!",
1219 if (!validate_impl_ret_stmt(data
, impl
)) {
1223 if (!validate_impl_let_stmts(data
, impl
)) {
1226 if (!validate_impl_set_stmts(data
, impl
)) {
1229 if (!validate_impl_free_stmts(data
, impl
)) {
1236 static inline int validate_impl_args(PSI_Data
*data
, impl
*impl
) {
1240 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
1241 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
1246 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1247 "Non-optional argument %zu '$%s' of implementation '%s'"
1248 " follows optional argument",
1249 i
+1, iarg
->var
->name
, impl
->func
->name
);
1257 static inline int validate_impl(PSI_Data
*data
, impl
*impl
) {
1258 if (!validate_impl_args(data
, impl
)) {
1261 return validate_impl_stmts(data
, impl
);
1265 int PSI_ContextValidate(PSI_Context
*C
, PSI_Parser
*P
)
1268 void *dlopened
= NULL
;
1269 size_t i
, count
= C
->count
++, check_round
, check_count
;
1270 decl_typedefs
*check_defs
= P
->defs
;
1271 decl_structs
*check_structs
= P
->structs
;
1272 decl_unions
*check_unions
= P
->unions
;
1273 decl_enums
*check_enums
= P
->enums
;
1274 unsigned silent
= C
->flags
& PSI_PARSER_SILENT
;
1276 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
1277 D
= PSI_DataExchange(&C
->data
[count
], PSI_DATA(P
));
1279 #define REVALIDATE(what) do { \
1280 if (check_round && check_ ##what) { \
1281 free(check_ ##what->list); \
1282 free(check_ ##what); \
1284 check_ ##what = recheck_ ##what; \
1286 #define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
1287 #define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
1290 /* no warnings on first round */
1291 C
->flags
|= PSI_PARSER_SILENT
;
1293 for (check_round
= 0, check_count
= 0; CHECK_TOTAL
&& check_count
!= CHECK_TOTAL
; ++check_round
) {
1294 decl_typedefs
*recheck_defs
= NULL
;
1295 decl_structs
*recheck_structs
= NULL
;
1296 decl_unions
*recheck_unions
= NULL
;
1297 decl_enums
*recheck_enums
= NULL
;
1299 check_count
= CHECK_TOTAL
;
1301 for (i
= 0; i
< CHECK_COUNT(defs
); ++i
) {
1302 if (validate_decl_typedef(PSI_DATA(C
), check_defs
->list
[i
])) {
1303 C
->defs
= add_decl_typedef(C
->defs
, check_defs
->list
[i
]);
1305 recheck_defs
= add_decl_typedef(recheck_defs
, check_defs
->list
[i
]);
1308 for (i
= 0; i
< CHECK_COUNT(structs
); ++i
) {
1309 if (validate_decl_struct(PSI_DATA(C
), check_structs
->list
[i
])) {
1310 C
->structs
= add_decl_struct(C
->structs
, check_structs
->list
[i
]);
1312 recheck_structs
= add_decl_struct(recheck_structs
, check_structs
->list
[i
]);
1315 for (i
= 0; i
< CHECK_COUNT(unions
); ++i
) {
1316 if (validate_decl_union(PSI_DATA(C
), check_unions
->list
[i
])) {
1317 C
->unions
= add_decl_union(C
->unions
, check_unions
->list
[i
]);
1319 recheck_unions
= add_decl_union(recheck_unions
, check_unions
->list
[i
]);
1322 for (i
= 0; i
< CHECK_COUNT(enums
); ++i
) {
1323 if (validate_decl_enum(PSI_DATA(C
), check_enums
->list
[i
])) {
1324 C
->enums
= add_decl_enum(C
->enums
, check_enums
->list
[i
]);
1326 recheck_enums
= add_decl_enum(recheck_enums
, check_enums
->list
[i
]);
1331 REVALIDATE(structs
);
1335 if (check_round
== 0 && !silent
) {
1336 C
->flags
&= ~PSI_PARSER_SILENT
;
1342 for (i
= 0; i
< D
->consts
->count
; ++i
) {
1343 if (validate_constant(PSI_DATA(C
), D
->consts
->list
[i
])) {
1344 C
->consts
= add_constant(C
->consts
, D
->consts
->list
[i
]);
1349 if (!validate_lib(D
, &dlopened
)) {
1353 add_decl_lib(&C
->psi
.libs
, dlopened
);
1356 for (i
= 0; i
< D
->decls
->count
; ++i
) {
1357 if (validate_decl(PSI_DATA(C
), dlopened
, D
->decls
->list
[i
])) {
1358 C
->decls
= add_decl(C
->decls
, D
->decls
->list
[i
]);
1363 for (i
= 0; i
< D
->impls
->count
; ++i
) {
1364 if (validate_impl(PSI_DATA(C
), D
->impls
->list
[i
])) {
1365 C
->impls
= add_impl(C
->impls
, D
->impls
->list
[i
]);
1373 int PSI_ContextValidateData(PSI_Data
*dest
, PSI_Data
*source
)
1378 if (source
->defs
) for (i
= 0; i
< source
->defs
->count
; ++i
) {
1379 decl_arg
*def
= source
->defs
->list
[i
];
1381 if (validate_decl_typedef(source
, def
)) {
1383 dest
->defs
= add_decl_typedef(dest
->defs
, def
);
1390 if (source
->consts
) for (i
= 0; i
< source
->consts
->count
; ++i
) {
1391 constant
*constant
= source
->consts
->list
[i
];
1393 if (validate_constant(source
, constant
)) {
1395 dest
->consts
= add_constant(dest
->consts
, constant
);
1402 if (source
->structs
) for (i
= 0; i
< source
->structs
->count
; ++i
) {
1403 decl_struct
*dstruct
= source
->structs
->list
[i
];
1405 if (validate_decl_struct(source
, dstruct
)) {
1407 dest
->structs
= add_decl_struct(dest
->structs
, dstruct
);
1414 if (source
->unions
) for (i
= 0; i
< source
->unions
->count
; ++i
) {
1415 decl_union
*dunion
= source
->unions
->list
[i
];
1417 if (validate_decl_union(source
, dunion
)) {
1419 dest
->unions
= add_decl_union(dest
->unions
, dunion
);
1426 if (source
->enums
) for (i
= 0; i
< source
->enums
->count
; ++i
) {
1427 decl_enum
*denum
= source
->enums
->list
[i
];
1429 if (validate_decl_enum(source
, denum
)) {
1431 dest
->enums
= add_decl_enum(dest
->enums
, denum
);
1438 if (source
->decls
) for (i
= 0; i
< source
->decls
->count
; ++i
) {
1439 decl
*decl
= source
->decls
->list
[i
];
1441 if (validate_decl(source
, NULL
, decl
)) {
1443 dest
->decls
= add_decl(dest
->decls
, decl
);
1450 if (source
->impls
) for (i
= 0; i
< source
->impls
->count
; ++i
) {
1451 impl
*impl
= source
->impls
->list
[i
];
1453 if (validate_impl(source
, impl
)) {
1455 dest
->impls
= add_impl(dest
->impls
, impl
);