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
)) {
174 data
->error(data
, def
->token
, PSI_WARNING
,
175 "Type '%s' cannot be aliased to %s'%s'",
176 def
->type
->name
, def
->type
->type
== PSI_T_STRUCT
?"struct ":"",
180 if (def
->type
->type
== PSI_T_VOID
&& def
->var
->pointer_level
) {
181 def
->type
->type
= PSI_T_POINTER
;
186 static inline int validate_constant(PSI_Data
*data
, constant
*c
) {
191 static inline int validate_decl_arg(PSI_Data
*data
, decl_arg
*arg
) {
192 if (!validate_decl_type(data
, arg
->type
)) {
193 data
->error(data
, arg
->type
->token
, PSI_WARNING
,
194 "Cannot use '%s' as type for '%s'",
195 arg
->type
->name
, arg
->var
->name
);
201 static int psi_sort_struct_arg_cmp(const void *_a
, const void *_b
) {
202 decl_arg
*a
= *(decl_arg
**)_a
, *b
= *(decl_arg
**)_b
;
204 if (a
->layout
->pos
== b
->layout
->pos
) {
205 if (a
->layout
->len
== b
->layout
->len
) {
207 } else if (a
->layout
->len
> b
->layout
->len
) {
212 } else if (a
->layout
->pos
> b
->layout
->pos
) {
218 static void psi_sort_struct_arg_swp(void *a
, void *b
) {
219 decl_arg
**_a
= a
, **_b
= b
, *_c
;
225 static inline void psi_sort_struct_args(void **args
, size_t count
) {
226 zend_insert_sort(args
, count
, sizeof(*args
),
227 psi_sort_struct_arg_cmp
, psi_sort_struct_arg_swp
);
230 static inline int validate_decl_struct_darg(PSI_Data
*data
, decl_arg
*darg
, void *current
) {
231 decl_type
*real
= real_decl_type(darg
->type
);
233 /* pre-validate any structs/unions/enums */
234 switch (real
->type
) {
236 if (current
&& current
== real
->strct
) {
239 if (!validate_decl_struct(data
, real
->strct
)) {
244 if (current
&& current
== real
->unn
) {
247 if (!validate_decl_union(data
, real
->unn
)) {
252 if (current
&& current
== real
->enm
) {
255 if (!validate_decl_enum(data
, real
->enm
)) {
264 static inline size_t sizeof_decl_arg(decl_arg
*darg
) {
266 decl_type
*real
= real_decl_type(darg
->type
);
268 if (darg
->var
->array_size
) {
269 if (darg
->var
->pointer_level
> 2) {
270 size
= psi_t_size(PSI_T_POINTER
) * darg
->var
->array_size
;
272 size
= psi_t_size(real
->type
) * darg
->var
->array_size
;
274 } else if (darg
->var
->pointer_level
) {
275 size
= psi_t_size(PSI_T_POINTER
);
277 switch (real
->type
) {
279 size
= real
->unn
->size
;
282 size
= real
->strct
->size
;
286 size
= psi_t_size(real
->type
);
296 static inline size_t alignof_decl_type(decl_type
*t
);
297 static inline size_t alignof_decl_arg(decl_arg
*darg
);
298 static inline size_t alignof_decl_union(decl_union
*u
);
299 static inline size_t alignof_decl_struct(decl_struct
*s
);
301 static inline size_t alignof_decl_args(decl_args
*args
) {
302 size_t i
, maxalign
= 0;
304 for (i
= 0; i
< args
->count
; ++i
) {
305 decl_arg
*darg
= args
->args
[i
];
306 size_t align
= alignof_decl_arg(darg
);
308 if (align
> maxalign
) {
316 static inline size_t alignof_decl_struct(decl_struct
*s
) {
318 s
->align
= alignof_decl_args(s
->args
);
323 static inline size_t alignof_decl_union(decl_union
*u
) {
325 u
->align
= alignof_decl_args(u
->args
);
330 static inline size_t alignof_decl_type(decl_type
*t
) {
331 decl_type
*real
= real_decl_type(t
);
334 switch (real
->type
) {
336 align
= alignof_decl_struct(real
->strct
);
339 align
= alignof_decl_union(real
->unn
);
343 align
= psi_t_alignment(real
->type
);
349 static inline size_t alignof_decl_arg(decl_arg
*darg
) {
352 if (darg
->var
->pointer_level
&& (!darg
->var
->array_size
|| darg
->var
->pointer_level
> 2)) {
353 align
= psi_t_alignment(PSI_T_POINTER
);
355 align
= alignof_decl_type(darg
->type
);
361 static inline size_t align_decl_arg(decl_arg
*darg
, size_t *pos
, size_t *len
) {
362 size_t align
= alignof_decl_arg(darg
);
366 *len
= sizeof_decl_arg(darg
);
367 *pos
= psi_align(align
, *pos
);
372 static inline int validate_decl_struct(PSI_Data
*data
, decl_struct
*s
) {
373 size_t i
, pos
, len
, size
, align
;
375 if (!s
->size
&& !s
->args
->count
) {
376 data
->error(data
, s
->token
, PSI_WARNING
,
377 "Cannot compute size of empty struct %s",
382 for (i
= 0; i
< s
->args
->count
; ++i
) {
383 decl_arg
*darg
= s
->args
->args
[i
];
385 if (!validate_decl_arg(data
, darg
)) {
389 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
390 darg
->var
->arg
= darg
;
392 if (!validate_decl_struct_darg(data
, darg
, s
)) {
394 } else if (darg
->layout
) {
395 pos
= darg
->layout
->pos
;
397 align
= align_decl_arg(darg
, &pos
, &len
);
399 if (darg
->layout
->len
!= len
) {
400 data
->error(data
, darg
->token
, PSI_WARNING
,
401 "Computed size %zu of %s.%s does not match"
402 " pre-defined size %zu of type '%s'",
403 len
, s
->name
, darg
->var
->name
, darg
->layout
->len
,
406 if (darg
->layout
->pos
!= pos
) {
407 data
->error(data
, darg
->token
, PSI_WARNING
,
408 "Computed offset %zu of %s.%s does not match"
409 " pre-defined offset %zu",
410 pos
, s
->name
, darg
->var
->name
, darg
->layout
->pos
);
414 pos
= s
->args
->args
[i
-1]->layout
->pos
+
415 s
->args
->args
[i
-1]->layout
->len
;
420 align
= align_decl_arg(darg
, &pos
, &len
);
421 darg
->layout
= init_decl_struct_layout(pos
, len
);
424 if (align
> s
->align
) {
429 psi_sort_struct_args((void **) s
->args
->args
, s
->args
->count
);
431 if (s
->args
->count
) {
432 decl_arg
*darg
= s
->args
->args
[s
->args
->count
-1];
434 size
= darg
->layout
->pos
+ darg
->layout
->len
;
435 if (s
->size
< size
) {
436 s
->size
= psi_align(size
, s
->align
);
443 static inline int validate_decl_union(PSI_Data
*data
, decl_union
*u
) {
444 size_t i
, pos
, len
, size
= 0, align
;
446 if (!u
->size
&& !u
->args
->count
) {
447 data
->error(data
, u
->token
, PSI_WARNING
,
448 "Cannot compute size of empty union %s",
453 for (i
= 0; i
< u
->args
->count
; ++i
) {
454 decl_arg
*darg
= u
->args
->args
[i
];
456 if (!validate_decl_arg(data
, darg
)) {
460 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
461 darg
->var
->arg
= darg
;
463 if (!validate_decl_struct_darg(data
, darg
, u
)) {
465 } else if (darg
->layout
) {
466 pos
= darg
->layout
->pos
;
468 align
= align_decl_arg(darg
, &pos
, &len
);
470 if (darg
->layout
->pos
!= 0) {
471 data
->error(data
, darg
->token
, PSI_WARNING
,
472 "Offset of %s.%s should be 0",
473 u
->name
, darg
->var
->name
);
474 darg
->layout
->pos
= 0;
476 if (darg
->layout
->len
!= len
) {
477 data
->error(data
, darg
->token
, PSI_WARNING
,
478 "Computed size %zu of %s.%s does not match"
479 " pre-defined size %zu of type '%s'",
480 len
, u
->name
, darg
->var
->name
, darg
->layout
->len
,
486 align
= align_decl_arg(darg
, &pos
, &len
);
487 darg
->layout
= init_decl_struct_layout(pos
, len
);
493 if (align
> u
->align
) {
498 psi_sort_struct_args((void **) u
->args
->args
, u
->args
->count
);
500 if (u
->size
< size
) {
501 u
->size
= psi_align(size
, u
->align
);
507 static const char * const abi_ccs
[] = {
509 "extern", /* > - all the same */
515 static inline int validate_decl_abi(PSI_Data
*data
, decl_abi
*abi
) {
518 for (i
= 0; i
< sizeof(abi_ccs
)/sizeof(char*); ++i
) {
519 if (strcasecmp(abi
->convention
, abi_ccs
[i
])) {
525 static inline int validate_decl_func(PSI_Data
*data
, void *dl
, decl
*decl
, decl_arg
*func
)
527 struct psi_func_redir
*redir
;
529 if (!strcmp(func
->var
->name
, "dlsym")) {
530 data
->error(data
, func
->token
, PSI_WARNING
, "Cannot dlsym dlsym (sic!)");
534 if (!validate_decl_arg(data
, func
)) {
537 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
538 if (!strcmp(func
->var
->name
, redir
->name
)) {
539 decl
->call
.sym
= redir
->func
;
542 if (!decl
->call
.sym
) {
544 # define RTLD_NEXT ((void *) -1l)
546 decl
->call
.sym
= dlsym(dl
?: RTLD_NEXT
, func
->var
->name
);
547 if (!decl
->call
.sym
) {
548 data
->error(data
, func
->token
, PSI_WARNING
,
549 "Failed to locate symbol '%s': %s",
550 func
->var
->name
, dlerror() ?: "not found");
556 static inline int validate_decl(PSI_Data
*data
, void *dl
, decl
*decl
) {
557 if (!validate_decl_abi(data
, decl
->abi
)) {
558 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
559 "Invalid calling convention: '%s'", decl
->abi
->token
->text
);
562 if (!validate_decl_func(data
, dl
, decl
, decl
->func
)) {
568 for (i
= 0; i
< decl
->args
->count
; ++i
) {
569 if (!validate_decl_arg(data
, decl
->args
->args
[i
])) {
576 static inline decl_arg
*locate_decl_var_arg(decl_var
*var
, decl_args
*args
, decl_arg
*func
) {
579 if (args
) for (i
= 0; i
< args
->count
; ++i
) {
580 decl_arg
*arg
= args
->args
[i
];
582 if (!strcmp(var
->name
, arg
->var
->name
)) {
583 ZEND_ASSERT(!var
->arg
|| var
->arg
== arg
);
584 return var
->arg
= arg
;
587 if (func
&& !strcmp(var
->name
, func
->var
->name
)) {
588 return var
->arg
= func
;
593 static inline decl_arg
*locate_struct_member(decl_struct
*s
, decl_var
*var
) {
595 return locate_decl_var_arg(var
, s
->args
, NULL
);
600 static inline constant
*locate_num_exp_constant(num_exp
*exp
, constants
*consts
) {
603 for (i
= 0; i
< consts
->count
; ++i
) {
604 constant
*cnst
= consts
->list
[i
];
606 if (!strcmp(cnst
->name
, exp
->u
.numb
)) {
608 return exp
->u
.cnst
= cnst
;
614 static inline decl_enum_item
*locate_num_exp_enum_item_ex(num_exp
*exp
, decl_enum
*e
) {
617 if (e
) for (k
= 0; k
< e
->items
->count
; ++k
) {
618 decl_enum_item
*i
= e
->items
->list
[k
];
620 if (!strcmp(i
->name
, exp
->u
.dvar
->name
)) {
621 free_decl_var(exp
->u
.dvar
);
629 static inline decl_enum_item
*locate_num_exp_enum_item(num_exp
*exp
, decl_enums
*enums
) {
632 if (enums
) for (j
= 0; j
< enums
->count
; ++j
) {
633 decl_enum
*e
= enums
->list
[j
];
634 decl_enum_item
*i
= locate_num_exp_enum_item_ex(exp
, e
);
642 static inline int validate_num_exp(PSI_Data
*data
, num_exp
*exp
, decl_args
*dargs
, decl_arg
*func
, decl_enum
*enm
) {
644 switch (exp
->operator) {
646 exp
->calculator
= psi_calc_add
;
649 exp
->calculator
= psi_calc_sub
;
652 exp
->calculator
= psi_calc_mul
;
655 exp
->calculator
= psi_calc_div
;
657 EMPTY_SWITCH_DEFAULT_CASE();
659 if (!validate_num_exp(data
, exp
->operand
, dargs
, func
, enm
)) {
665 if (!locate_decl_var_arg(exp
->u
.dvar
, dargs
, func
)) {
666 if (!locate_num_exp_enum_item(exp
, data
->enums
) && !locate_num_exp_enum_item_ex(exp
, enm
)) {
667 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown variable '%s' in numeric expression",
674 if (!locate_num_exp_constant(exp
, data
->consts
)) {
675 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown constant '%s' in numeric expression",
688 static inline int validate_decl_enum(PSI_Data
*data
, decl_enum
*e
) {
691 if (!e
->items
|| !e
->items
->count
) {
692 data
->error(data
, e
->token
, PSI_WARNING
, "Empty enum '%s'", e
->name
);
696 for (j
= 0; j
< e
->items
->count
; ++j
) {
697 decl_enum_item
*i
= e
->items
->list
[j
];
701 i
->inc
.t
= PSI_T_NUMBER
;
703 i
->inc
.operator = PSI_T_PLUS
;
704 i
->inc
.operand
= i
->prev
->num
?: &i
->prev
->inc
;
707 i
->inc
.t
= PSI_T_NUMBER
;
712 if (!validate_num_exp(data
, i
->num
, NULL
, NULL
, e
)) {
720 static inline int validate_set_value_handler(set_value
*set
) {
721 switch (set
->func
->type
) {
723 set
->func
->handler
= psi_to_bool
;
726 set
->func
->handler
= psi_to_int
;
729 set
->func
->handler
= psi_to_double
;
731 case PSI_T_TO_STRING
:
732 set
->func
->handler
= psi_to_string
;
735 set
->func
->handler
= psi_to_array
;
737 case PSI_T_TO_OBJECT
:
738 set
->func
->handler
= psi_to_object
;
741 set
->func
->handler
= psi_to_void
;
744 if (set
->outer
.set
&& set
->outer
.set
->func
->type
== PSI_T_TO_ARRAY
) {
745 set
->func
->handler
= psi_to_recursive
;
746 set
->inner
= set
->outer
.set
->inner
;
755 static inline void decl_var_arg_v(decl_args
*args
, va_list argp
) {
759 memset(args
, 0, sizeof(*args
));
761 while ((argc
= va_arg(argp
, int))) {
762 argv
= va_arg(argp
, decl_arg
**);
764 add_decl_arg(args
, *argv
++);
768 static inline int validate_set_value_ex(PSI_Data
*data
, set_value
*set
, decl_arg
*ref
, decl_args
*ref_list
) {
771 decl_var
*set_var
= set
->vars
->vars
[0];
773 if (!validate_set_value_handler(set
)) {
774 data
->error(data
, set
->func
->token
, PSI_WARNING
, "Invalid cast '%s' in `set` statement", set
->func
->name
);
778 for (i
= 0; i
< set
->vars
->count
; ++i
) {
779 decl_var
*svar
= set
->vars
->vars
[i
];
780 if (!svar
->arg
&& !locate_decl_var_arg(svar
, ref_list
, NULL
)) {
781 data
->error(data
, svar
->token
, PSI_WARNING
, "Unknown variable '%s' in `set` statement", svar
->name
);
789 ref_type
= real_decl_type(ref
->type
);
791 if (set
->inner
&& set
->inner
->count
) {
792 int is_to_array
= (set
->func
->type
== PSI_T_TO_ARRAY
);
793 int is_pointer_to_struct
= (ref_type
->type
== PSI_T_STRUCT
&& ref
->var
->pointer_level
);
795 if (!is_to_array
&& !is_pointer_to_struct
) {
796 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts only work with "
797 "to_array() casts on structs or pointers: %s(%s...", set
->func
->name
, set
->vars
->vars
[0]->name
);
802 if (!validate_num_exp(data
, set
->num
, ref_list
, ref
, NULL
)) {
807 if (set
->inner
&& ref_type
->type
== PSI_T_STRUCT
) {
808 /* to_array(struct, to_...) */
809 if (!set
->outer
.set
|| set
->outer
.set
->inner
->vals
!= set
->inner
->vals
) {
810 for (i
= 0; i
< set
->inner
->count
; ++i
) {
811 decl_var
*sub_var
= set
->inner
->vals
[i
]->vars
->vars
[0];
812 decl_arg
*sub_ref
= locate_struct_member(ref_type
->strct
, sub_var
);
815 if (!validate_set_value_ex(data
, set
->inner
->vals
[i
], sub_ref
, ref_type
->strct
->args
)) {
821 } else if (set
->inner
&& set
->inner
->count
== 1) {
822 /* to_array(ptr, to_string(*ptr)) */
823 decl_var
*sub_var
= set
->inner
->vals
[0]->vars
->vars
[0];
824 decl_arg
*sub_ref
= locate_decl_var_arg(sub_var
, ref_list
, ref
);
827 if (strcmp(sub_var
->name
, set_var
->name
)) {
828 data
->error(data
, sub_var
->token
, E_WARNING
, "Inner `set` statement casts on pointers must reference the same variable");
831 if (!validate_set_value_ex(data
, set
->inner
->vals
[0], sub_ref
, ref_list
)) {
835 } else if (set
->inner
&& set
->inner
->count
> 1) {
836 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts on pointers may only occur once");
842 static inline int validate_set_value(PSI_Data
*data
, set_value
*set
, ...) {
844 decl_args args
= {0};
848 decl_var_arg_v(&args
, argp
);
851 check
= validate_set_value_ex(data
, set
, NULL
, &args
);
857 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
861 for (i
= 0; i
< decls
->count
; ++i
) {
862 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->set
->vars
->vars
[0]->name
)) {
863 ret
->decl
= decls
->list
[i
]->func
;
864 return decls
->list
[i
];
871 static inline int validate_impl_ret_stmt(PSI_Data
*data
, impl
*impl
) {
874 /* we must have exactly one ret stmt delcaring the native func to call */
875 /* and which type cast to apply */
876 if (impl
->stmts
->ret
.count
!= 1) {
877 if (impl
->stmts
->ret
.count
> 1) {
878 data
->error(data
, impl
->stmts
->ret
.list
[1]->token
, PSI_WARNING
,
879 "Too many `return` statements for implmentation %s;"
880 " found %zu, exactly one is needed",
881 impl
->func
->name
, impl
->stmts
->ret
.count
);
883 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
884 "Missing `return` statement for implementation %s",
890 ret
= impl
->stmts
->ret
.list
[0];
892 if (!(impl
->decl
= locate_impl_decl(data
->decls
, ret
))) {
893 data
->error(data
, ret
->token
, PSI_WARNING
,
894 "Missing declaration '%s' for `return` statment for implementation %s",
895 ret
->set
->vars
->vars
[0]->name
, impl
->func
->name
);
899 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)) {
903 //impl->decl->impl = impl;
908 static inline impl_arg
*locate_impl_var_arg(impl_var
*var
, impl_args
*args
) {
911 for (i
= 0; i
< args
->count
; ++i
) {
912 impl_arg
*iarg
= args
->args
[i
];
914 if (!strcmp(var
->name
, iarg
->var
->name
)) {
915 return var
->arg
= iarg
;
922 static inline int validate_let_func(PSI_Data
*data
, let_func
*func
, impl
*impl
) {
923 if (impl
->func
->args
) {
924 if (!locate_impl_var_arg(func
->var
, impl
->func
->args
)) {
925 data
->error(data
, func
->var
->token
, PSI_WARNING
,
926 "Unknown variable '$%s' of `let` statement"
927 " for cast '%s' of implementation '%s'",
928 func
->var
->name
, func
->name
, impl
->func
->name
);
932 switch (func
->type
) {
933 case PSI_T_BOOLVAL
: func
->handler
= psi_let_boolval
; break;
934 case PSI_T_INTVAL
: func
->handler
= psi_let_intval
; break;
935 case PSI_T_FLOATVAL
: func
->handler
= psi_let_floatval
; break;
936 case PSI_T_STRVAL
: func
->handler
= psi_let_strval
; break;
937 case PSI_T_STRLEN
: func
->handler
= psi_let_strlen
; break;
938 case PSI_T_PATHVAL
: func
->handler
= psi_let_pathval
; break;
939 case PSI_T_ARRVAL
: func
->handler
= psi_let_arrval
; break;
940 case PSI_T_OBJVAL
: func
->handler
= psi_let_objval
; break;
941 EMPTY_SWITCH_DEFAULT_CASE();
946 static inline int validate_let_callback(PSI_Data
*data
, decl_var
*cb_var
, let_callback
*cb
, impl
*impl
) {
949 decl_type
*cb_type
= real_decl_type(cb_var
->arg
->type
);
951 if (!validate_let_func(data
, cb
->func
, impl
)) {
955 if (cb_type
->type
!= PSI_T_FUNCTION
) {
956 data
->error(data
, cb_var
->token
, PSI_WARNING
, "Not a function: %s", cb_var
->name
);
959 cb_func
= cb_type
->func
;
960 for (i
= 0; i
< cb
->args
->count
; ++i
) {
961 if (!validate_set_value(data
, cb
->args
->vals
[i
], cb_func
->args
->count
, cb_func
->args
->args
, 0)) {
966 if (!validate_decl(data
, NULL
, cb_func
)) {
975 static inline int validate_impl_let_stmts(PSI_Data
*data
, impl
*impl
) {
977 /* we can have multiple let stmts */
979 /* check that we have a decl arg for every let stmt */
980 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
981 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
984 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
985 let_var
= let
->val
->data
.var
;
990 if (!locate_decl_var_arg(let_var
, impl
->decl
->args
, impl
->decl
->func
)) {
991 data
->error(data
, let_var
->token
, PSI_WARNING
, "Unknown variable '%s' in `let` statement"
992 " of implementation '%s'", let_var
->name
, impl
->func
->name
);
996 switch (let
->val
->kind
) {
1000 /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */
1001 /* e.g. let foo = *bar; */
1002 let
->var
->pointer_level
= let
->val
->data
.var
->pointer_level
;
1003 let
->var
->arg
= init_decl_arg(
1005 real_decl_type(let
->val
->data
.var
->arg
->type
)->type
,
1006 real_decl_type(let
->val
->data
.var
->arg
->type
)->name
),
1009 let
->var
->pointer_level
,
1010 let
->var
->array_size
));
1012 case PSI_LET_NUMEXP
:
1013 if (!validate_num_exp(data
, let
->val
->data
.num
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1017 case PSI_LET_CALLOC
:
1018 if (!validate_num_exp(data
, let
->val
->data
.alloc
->nmemb
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1021 if (!validate_num_exp(data
, let
->val
->data
.alloc
->size
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1025 case PSI_LET_CALLBACK
:
1026 if (!validate_let_callback(data
, let
->var
, let
->val
->data
.callback
, impl
)) {
1031 if (!validate_let_func(data
, let
->val
->data
.func
, impl
)) {
1038 /* check that we have a let stmt for every decl arg */
1039 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
1040 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
1043 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
1044 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
1046 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
1053 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1054 "Missing `let` statement for arg '%s %.*s%s'"
1055 " of declaration '%s' for implementation '%s'",
1056 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
1057 darg
->var
->name
, impl
->decl
->func
->var
->name
, impl
->func
->name
);
1064 static inline int validate_impl_set_stmts(PSI_Data
*data
, impl
*impl
) {
1066 /* we can have any count of set stmts; processing out vars */
1067 /* check that set stmts reference known variables */
1068 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
1069 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
1072 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
1073 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
1075 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
1082 data
->error(data
, set
->var
->token
, PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
1083 " of implementation '%s'",
1084 set
->var
->name
, impl
->func
->name
);
1088 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
1089 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
1092 if (impl
->decl
->args
) {
1093 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1094 decl_arg
*set_arg
= impl
->decl
->args
->args
[k
];
1096 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
1098 set_var
->arg
= set_arg
;
1099 if (!validate_set_value(data
, set
->val
, 1, &set_arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1107 for (k
= 0; k
< impl
->stmts
->let
.count
; ++k
) {
1108 let_stmt
*let
= impl
->stmts
->let
.list
[k
];
1110 /* check temp vars */
1111 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
1112 if (!strcmp(set_var
->name
, let
->var
->name
)) {
1114 set_var
->arg
= let
->var
->arg
;
1115 if (!validate_set_value(data
, set
->val
, 1, &set_var
->arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1125 data
->error(data
, set_var
->token
, PSI_WARNING
, "Unknown value '%s' of `set` statement"
1126 " for variable '$%s' of implementation '%s'",
1127 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
1134 static inline decl
*locate_free_decl(decls
*decls
, free_call
*f
) {
1138 for (i
= 0; i
< decls
->count
; ++i
) {
1139 if (!strcmp(decls
->list
[i
]->func
->var
->name
, f
->func
)) {
1140 f
->decl
= decls
->list
[i
];
1141 return decls
->list
[i
];
1148 static inline int validate_impl_free_stmts(PSI_Data
*data
, impl
*impl
) {
1150 /* we can have any count of free stmts; freeing any out vars */
1151 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
1152 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1154 for (j
= 0; j
< fre
->calls
->count
; ++j
) {
1155 free_call
*free_call
= fre
->calls
->list
[j
];
1157 /* first find the decl of the free func */
1158 if (!locate_free_decl(data
->decls
, free_call
)) {
1159 data
->error(data
, free_call
->token
, PSI_WARNING
,
1160 "Missing declaration '%s' in `free` statement"
1161 " of implementation '%s'",
1162 free_call
->func
, impl
->func
->name
);
1168 /* now check for known vars */
1169 for (l
= 0; l
< free_call
->vars
->count
; ++l
) {
1171 decl_var
*free_var
= free_call
->vars
->vars
[l
];
1173 if (!strcmp(free_var
->name
, impl
->decl
->func
->var
->name
)) {
1175 free_var
->arg
= impl
->decl
->func
;
1176 } else if (impl
->decl
->args
) {
1177 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1178 decl_arg
*free_arg
= impl
->decl
->args
->args
[k
];
1180 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
1182 free_var
->arg
= free_arg
;
1189 data
->error(data
, free_var
->token
, PSI_WARNING
,
1190 "Unknown variable '%s' of `free` statement"
1191 " of implementation '%s'",
1192 free_var
->name
, impl
->func
->name
);
1200 static inline int validate_impl_stmts(PSI_Data
*data
, impl
*impl
) {
1202 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1203 "Missing body for implementation %s!",
1208 if (!validate_impl_ret_stmt(data
, impl
)) {
1212 if (!validate_impl_let_stmts(data
, impl
)) {
1215 if (!validate_impl_set_stmts(data
, impl
)) {
1218 if (!validate_impl_free_stmts(data
, impl
)) {
1225 static inline int validate_impl_args(PSI_Data
*data
, impl
*impl
) {
1229 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
1230 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
1235 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1236 "Non-optional argument %zu '$%s' of implementation '%s'"
1237 " follows optional argument",
1238 i
+1, iarg
->var
->name
, impl
->func
->name
);
1246 static inline int validate_impl(PSI_Data
*data
, impl
*impl
) {
1247 if (!validate_impl_args(data
, impl
)) {
1250 return validate_impl_stmts(data
, impl
);
1254 int PSI_ContextValidate(PSI_Context
*C
, PSI_Parser
*P
)
1257 void *dlopened
= NULL
;
1258 size_t i
, count
= C
->count
++, check_round
, check_count
;
1259 decl_typedefs
*check_defs
= P
->defs
;
1260 decl_structs
*check_structs
= P
->structs
;
1261 decl_unions
*check_unions
= P
->unions
;
1262 decl_enums
*check_enums
= P
->enums
;
1263 unsigned silent
= C
->flags
& PSI_PARSER_SILENT
;
1265 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
1266 D
= PSI_DataExchange(&C
->data
[count
], PSI_DATA(P
));
1268 #define REVALIDATE(what) do { \
1269 if (check_round && check_ ##what) { \
1270 free(check_ ##what->list); \
1271 free(check_ ##what); \
1273 check_ ##what = recheck_ ##what; \
1275 #define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
1276 #define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
1279 /* no warnings on first round */
1280 C
->flags
|= PSI_PARSER_SILENT
;
1282 for (check_round
= 0, check_count
= 0; CHECK_TOTAL
&& check_count
!= CHECK_TOTAL
; ++check_round
) {
1283 decl_typedefs
*recheck_defs
= NULL
;
1284 decl_structs
*recheck_structs
= NULL
;
1285 decl_unions
*recheck_unions
= NULL
;
1286 decl_enums
*recheck_enums
= NULL
;
1288 check_count
= CHECK_TOTAL
;
1290 for (i
= 0; i
< CHECK_COUNT(defs
); ++i
) {
1291 if (validate_decl_typedef(PSI_DATA(C
), check_defs
->list
[i
])) {
1292 C
->defs
= add_decl_typedef(C
->defs
, check_defs
->list
[i
]);
1294 recheck_defs
= add_decl_typedef(recheck_defs
, check_defs
->list
[i
]);
1297 for (i
= 0; i
< CHECK_COUNT(structs
); ++i
) {
1298 if (validate_decl_struct(PSI_DATA(C
), check_structs
->list
[i
])) {
1299 C
->structs
= add_decl_struct(C
->structs
, check_structs
->list
[i
]);
1301 recheck_structs
= add_decl_struct(recheck_structs
, check_structs
->list
[i
]);
1304 for (i
= 0; i
< CHECK_COUNT(unions
); ++i
) {
1305 if (validate_decl_union(PSI_DATA(C
), check_unions
->list
[i
])) {
1306 C
->unions
= add_decl_union(C
->unions
, check_unions
->list
[i
]);
1308 recheck_unions
= add_decl_union(recheck_unions
, check_unions
->list
[i
]);
1311 for (i
= 0; i
< CHECK_COUNT(enums
); ++i
) {
1312 if (validate_decl_enum(PSI_DATA(C
), check_enums
->list
[i
])) {
1313 C
->enums
= add_decl_enum(C
->enums
, check_enums
->list
[i
]);
1315 recheck_enums
= add_decl_enum(recheck_enums
, check_enums
->list
[i
]);
1320 REVALIDATE(structs
);
1324 if (check_round
== 0 && !silent
) {
1325 C
->flags
&= ~PSI_PARSER_SILENT
;
1331 for (i
= 0; i
< D
->consts
->count
; ++i
) {
1332 if (validate_constant(PSI_DATA(C
), D
->consts
->list
[i
])) {
1333 C
->consts
= add_constant(C
->consts
, D
->consts
->list
[i
]);
1338 if (!validate_lib(D
, &dlopened
)) {
1342 add_decl_lib(&C
->psi
.libs
, dlopened
);
1345 for (i
= 0; i
< D
->decls
->count
; ++i
) {
1346 if (validate_decl(PSI_DATA(C
), dlopened
, D
->decls
->list
[i
])) {
1347 C
->decls
= add_decl(C
->decls
, D
->decls
->list
[i
]);
1352 for (i
= 0; i
< D
->impls
->count
; ++i
) {
1353 if (validate_impl(PSI_DATA(C
), D
->impls
->list
[i
])) {
1354 C
->impls
= add_impl(C
->impls
, D
->impls
->list
[i
]);
1362 int PSI_ContextValidateData(PSI_Data
*dest
, PSI_Data
*source
)
1367 if (source
->defs
) for (i
= 0; i
< source
->defs
->count
; ++i
) {
1368 decl_arg
*def
= source
->defs
->list
[i
];
1370 if (validate_decl_typedef(source
, def
)) {
1372 dest
->defs
= add_decl_typedef(dest
->defs
, def
);
1379 if (source
->consts
) for (i
= 0; i
< source
->consts
->count
; ++i
) {
1380 constant
*constant
= source
->consts
->list
[i
];
1382 if (validate_constant(source
, constant
)) {
1384 dest
->consts
= add_constant(dest
->consts
, constant
);
1391 if (source
->structs
) for (i
= 0; i
< source
->structs
->count
; ++i
) {
1392 decl_struct
*dstruct
= source
->structs
->list
[i
];
1394 if (validate_decl_struct(source
, dstruct
)) {
1396 dest
->structs
= add_decl_struct(dest
->structs
, dstruct
);
1403 if (source
->unions
) for (i
= 0; i
< source
->unions
->count
; ++i
) {
1404 decl_union
*dunion
= source
->unions
->list
[i
];
1406 if (validate_decl_union(source
, dunion
)) {
1408 dest
->unions
= add_decl_union(dest
->unions
, dunion
);
1415 if (source
->enums
) for (i
= 0; i
< source
->enums
->count
; ++i
) {
1416 decl_enum
*denum
= source
->enums
->list
[i
];
1418 if (validate_decl_enum(source
, denum
)) {
1420 dest
->enums
= add_decl_enum(dest
->enums
, denum
);
1427 if (source
->decls
) for (i
= 0; i
< source
->decls
->count
; ++i
) {
1428 decl
*decl
= source
->decls
->list
[i
];
1430 if (validate_decl(source
, NULL
, decl
)) {
1432 dest
->decls
= add_decl(dest
->decls
, decl
);
1439 if (source
->impls
) for (i
= 0; i
< source
->impls
->count
; ++i
) {
1440 impl
*impl
= source
->impls
->list
[i
];
1442 if (validate_impl(source
, impl
)) {
1444 dest
->impls
= add_impl(dest
->impls
, impl
);