56d7b2ea43eab6caad086c2cdd1dea03be010b09
11 #include "php_psi_stdinc.h"
12 #include "php_psi_stdtypes.h"
13 #include "php_psi_macros.h"
14 #include "php_psi_redirs.h"
20 static int validate_lib(PSI_Data
*data
, void **dlopened
) {
22 const char *ptr
= data
->psi
.file
.ln
;
26 /* FIXME: assume stdlib */
28 } else if (!strchr(ptr
, '/')) {
29 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.%s", ptr
, PHP_PSI_SHLIB_SUFFIX
);
30 if (MAXPATHLEN
== len
) {
31 data
->error(data
, NULL
, PSI_WARNING
, "Library name too long: '%s'", ptr
);
36 if (!(*dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
37 data
->error(data
, NULL
, PSI_WARNING
, "Could not open library '%s': %s.",
38 data
->psi
.file
.ln
, dlerror());
44 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
46 struct psi_std_type
*stdtyp
;
51 if (defs
) for (i
= 0; i
< defs
->count
; ++i
) {
52 decl_arg
*def
= defs
->list
[i
];
54 if (def
->type
->type
!= type
->type
&& !strcmp(def
->var
->name
, type
->name
)) {
55 type
->real
= def
->type
;
59 for (stdtyp
= &psi_std_types
[0]; stdtyp
->type_tag
; ++stdtyp
) {
60 if (!strcmp(type
->name
, stdtyp
->alias
?: stdtyp
->type_name
)) {
61 type
->type
= stdtyp
->type_tag
;
69 static inline int locate_decl_type_struct(decl_structs
*structs
, decl_type
*type
) {
75 if (structs
) for (i
= 0; i
< structs
->count
; ++i
) {
76 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
77 type
->strct
= structs
->list
[i
];
84 static inline int locate_decl_type_union(decl_unions
*unions
, decl_type
*type
) {
90 if (unions
) for (i
= 0; i
< unions
->count
; ++i
) {
91 if (!strcmp(unions
->list
[i
]->name
, type
->name
)) {
92 type
->unn
= unions
->list
[i
];
99 static inline int locate_decl_type_enum(decl_enums
*enums
, decl_type
*type
) {
105 if (enums
) for (i
= 0; i
< enums
->count
; ++i
) {
106 if (!strcmp(enums
->list
[i
]->name
, type
->name
)) {
107 type
->enm
= enums
->list
[i
];
114 static inline int validate_decl_struct(PSI_Data
*data
, decl_struct
*s
);
115 static inline int validate_decl_union(PSI_Data
*data
, decl_union
*u
);
116 static inline int validate_decl_enum(PSI_Data
*data
, decl_enum
*e
);
118 static inline int validate_decl_type(PSI_Data
*data
, decl_type
*type
) {
119 switch (type
->type
) {
125 if (!locate_decl_type_alias(data
->defs
, type
)) {
129 return validate_decl_type(data
, type
->real
);
133 if (!locate_decl_type_struct(data
->structs
, type
)) {
138 if (!locate_decl_type_union(data
->unions
, type
)) {
143 if (!locate_decl_type_enum(data
->enums
, type
)) {
149 static inline int validate_decl_typedef(PSI_Data
*data
, decl_arg
*def
) {
150 if (!validate_decl_type(data
, def
->type
)) {
151 data
->error(data
, def
->token
, PSI_WARNING
,
152 "Type '%s' cannot be aliased to %s'%s'",
153 def
->type
->name
, def
->type
->type
== PSI_T_STRUCT
?"struct ":"",
157 if (def
->type
->type
== PSI_T_VOID
&& def
->var
->pointer_level
) {
158 def
->type
->type
= PSI_T_POINTER
;
163 static inline int validate_constant(PSI_Data
*data
, constant
*c
) {
168 static inline int validate_decl_arg(PSI_Data
*data
, decl_arg
*arg
) {
169 if (!validate_decl_type(data
, arg
->type
)) {
170 data
->error(data
, arg
->type
->token
, PSI_WARNING
,
171 "Cannot use '%s' as type for '%s'",
172 arg
->type
->name
, arg
->var
->name
);
178 static int psi_sort_struct_arg_cmp(const void *_a
, const void *_b
) {
179 decl_arg
*a
= *(decl_arg
**)_a
, *b
= *(decl_arg
**)_b
;
181 if (a
->layout
->pos
== b
->layout
->pos
) {
182 if (a
->layout
->len
== b
->layout
->len
) {
184 } else if (a
->layout
->len
> b
->layout
->len
) {
189 } else if (a
->layout
->pos
> b
->layout
->pos
) {
195 static void psi_sort_struct_arg_swp(void *a
, void *b
) {
196 decl_arg
**_a
= a
, **_b
= b
, *_c
;
202 static inline void psi_sort_struct_args(void **args
, size_t count
) {
203 zend_insert_sort(args
, count
, sizeof(*args
),
204 psi_sort_struct_arg_cmp
, psi_sort_struct_arg_swp
);
207 static inline int validate_decl_struct_darg(PSI_Data
*data
, decl_arg
*darg
, void *current
) {
208 decl_type
*real
= real_decl_type(darg
->type
);
210 /* pre-validate any structs/unions/enums */
211 switch (real
->type
) {
213 if (current
&& current
== real
->strct
) {
216 if (!validate_decl_struct(data
, real
->strct
)) {
221 if (current
&& current
== real
->unn
) {
224 if (!validate_decl_union(data
, real
->unn
)) {
229 if (current
&& current
== real
->enm
) {
232 if (!validate_decl_enum(data
, real
->enm
)) {
241 static inline size_t sizeof_decl_arg(decl_arg
*darg
) {
243 decl_type
*real
= real_decl_type(darg
->type
);
245 if (darg
->var
->array_size
) {
246 if (darg
->var
->pointer_level
> 2) {
247 size
= psi_t_size(PSI_T_POINTER
) * darg
->var
->array_size
;
249 size
= psi_t_size(real
->type
) * darg
->var
->array_size
;
251 } else if (darg
->var
->pointer_level
) {
252 size
= psi_t_size(PSI_T_POINTER
);
254 switch (real
->type
) {
256 size
= real
->unn
->size
;
259 size
= real
->strct
->size
;
263 size
= psi_t_size(real
->type
);
273 static inline size_t alignof_decl_type(decl_type
*t
);
274 static inline size_t alignof_decl_arg(decl_arg
*darg
);
275 static inline size_t alignof_decl_union(decl_union
*u
);
276 static inline size_t alignof_decl_struct(decl_struct
*s
);
278 static inline size_t alignof_decl_args(decl_args
*args
) {
279 size_t i
, maxalign
= 0;
281 for (i
= 0; i
< args
->count
; ++i
) {
282 decl_arg
*darg
= args
->args
[i
];
283 size_t align
= alignof_decl_arg(darg
);
285 if (align
> maxalign
) {
293 static inline size_t alignof_decl_struct(decl_struct
*s
) {
295 s
->align
= alignof_decl_args(s
->args
);
300 static inline size_t alignof_decl_union(decl_union
*u
) {
302 u
->align
= alignof_decl_args(u
->args
);
307 static inline size_t alignof_decl_type(decl_type
*t
) {
308 decl_type
*real
= real_decl_type(t
);
311 switch (real
->type
) {
313 align
= alignof_decl_struct(real
->strct
);
316 align
= alignof_decl_union(real
->unn
);
320 align
= psi_t_alignment(real
->type
);
326 static inline size_t alignof_decl_arg(decl_arg
*darg
) {
329 if (darg
->var
->pointer_level
&& (!darg
->var
->array_size
|| darg
->var
->pointer_level
> 2)) {
330 align
= psi_t_alignment(PSI_T_POINTER
);
332 align
= alignof_decl_type(darg
->type
);
338 static inline size_t align_decl_arg(decl_arg
*darg
, size_t *pos
, size_t *len
) {
339 size_t align
= alignof_decl_arg(darg
);
343 *len
= sizeof_decl_arg(darg
);
344 *pos
= psi_align(align
, *pos
);
349 static inline int validate_decl_struct(PSI_Data
*data
, decl_struct
*s
) {
350 size_t i
, pos
, len
, size
, align
;
352 if (!s
->size
&& !s
->args
->count
) {
353 data
->error(data
, s
->token
, PSI_WARNING
,
354 "Cannot compute size of empty struct %s",
359 for (i
= 0; i
< s
->args
->count
; ++i
) {
360 decl_arg
*darg
= s
->args
->args
[i
];
362 if (!validate_decl_arg(data
, darg
)) {
366 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
367 darg
->var
->arg
= darg
;
369 if (!validate_decl_struct_darg(data
, darg
, s
)) {
371 } else if (darg
->layout
) {
372 pos
= darg
->layout
->pos
;
374 align
= align_decl_arg(darg
, &pos
, &len
);
376 if (darg
->layout
->len
!= len
) {
377 data
->error(data
, darg
->token
, PSI_WARNING
,
378 "Computed size %zu of %s.%s does not match"
379 " pre-defined size %zu of type '%s'",
380 len
, s
->name
, darg
->var
->name
, darg
->layout
->len
,
383 if (darg
->layout
->pos
!= pos
) {
384 data
->error(data
, darg
->token
, PSI_WARNING
,
385 "Computed offset %zu of %s.%s does not match"
386 " pre-defined offset %zu",
387 pos
, s
->name
, darg
->var
->name
, darg
->layout
->pos
);
391 pos
= s
->args
->args
[i
-1]->layout
->pos
+
392 s
->args
->args
[i
-1]->layout
->len
;
397 align
= align_decl_arg(darg
, &pos
, &len
);
398 darg
->layout
= init_decl_struct_layout(pos
, len
);
401 if (align
> s
->align
) {
406 psi_sort_struct_args((void **) s
->args
->args
, s
->args
->count
);
408 if (s
->args
->count
) {
409 decl_arg
*darg
= s
->args
->args
[s
->args
->count
-1];
411 size
= darg
->layout
->pos
+ darg
->layout
->len
;
412 if (s
->size
< size
) {
413 s
->size
= psi_align(size
, s
->align
);
420 static inline int validate_decl_union(PSI_Data
*data
, decl_union
*u
) {
421 size_t i
, pos
, len
, size
= 0, align
;
423 if (!u
->size
&& !u
->args
->count
) {
424 data
->error(data
, u
->token
, PSI_WARNING
,
425 "Cannot compute size of empty union %s",
430 for (i
= 0; i
< u
->args
->count
; ++i
) {
431 decl_arg
*darg
= u
->args
->args
[i
];
433 if (!validate_decl_arg(data
, darg
)) {
437 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
438 darg
->var
->arg
= darg
;
440 if (!validate_decl_struct_darg(data
, darg
, u
)) {
442 } else if (darg
->layout
) {
443 pos
= darg
->layout
->pos
;
445 align
= align_decl_arg(darg
, &pos
, &len
);
447 if (darg
->layout
->pos
!= 0) {
448 data
->error(data
, darg
->token
, PSI_WARNING
,
449 "Offset of %s.%s should be 0",
450 u
->name
, darg
->var
->name
);
451 darg
->layout
->pos
= 0;
453 if (darg
->layout
->len
!= len
) {
454 data
->error(data
, darg
->token
, PSI_WARNING
,
455 "Computed size %zu of %s.%s does not match"
456 " pre-defined size %zu of type '%s'",
457 len
, u
->name
, darg
->var
->name
, darg
->layout
->len
,
463 align
= align_decl_arg(darg
, &pos
, &len
);
464 darg
->layout
= init_decl_struct_layout(pos
, len
);
470 if (align
> u
->align
) {
475 psi_sort_struct_args((void **) u
->args
->args
, u
->args
->count
);
477 if (u
->size
< size
) {
478 u
->size
= psi_align(size
, u
->align
);
484 static const char * const abi_ccs
[] = {
486 "extern", /* > - all the same */
492 static inline int validate_decl_abi(PSI_Data
*data
, decl_abi
*abi
) {
495 for (i
= 0; i
< sizeof(abi_ccs
)/sizeof(char*); ++i
) {
496 if (strcasecmp(abi
->convention
, abi_ccs
[i
])) {
502 static inline int validate_decl_func(PSI_Data
*data
, void *dl
, decl
*decl
, decl_arg
*func
)
504 struct psi_func_redir
*redir
;
506 if (!strcmp(func
->var
->name
, "dlsym")) {
507 data
->error(data
, func
->token
, PSI_WARNING
, "Cannot dlsym dlsym (sic!)");
511 if (!validate_decl_arg(data
, func
)) {
514 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
515 if (!strcmp(func
->var
->name
, redir
->name
)) {
516 decl
->call
.sym
= redir
->func
;
519 if (!decl
->call
.sym
) {
521 # define RTLD_NEXT ((void *) -1l)
523 decl
->call
.sym
= dlsym(dl
?: RTLD_NEXT
, func
->var
->name
);
524 if (!decl
->call
.sym
) {
525 data
->error(data
, func
->token
, PSI_WARNING
,
526 "Failed to locate symbol '%s': %s",
527 func
->var
->name
, dlerror() ?: "not found");
533 static inline int validate_decl(PSI_Data
*data
, void *dl
, decl
*decl
) {
534 if (!validate_decl_abi(data
, decl
->abi
)) {
535 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
536 "Invalid calling convention: '%s'", decl
->abi
->token
->text
);
539 if (!validate_decl_func(data
, dl
, decl
, decl
->func
)) {
545 for (i
= 0; i
< decl
->args
->count
; ++i
) {
546 if (!validate_decl_arg(data
, decl
->args
->args
[i
])) {
553 static inline decl_arg
*locate_decl_var_arg(decl_var
*var
, decl_args
*args
, decl_arg
*func
) {
556 if (args
) for (i
= 0; i
< args
->count
; ++i
) {
557 decl_arg
*arg
= args
->args
[i
];
559 if (!strcmp(var
->name
, arg
->var
->name
)) {
560 ZEND_ASSERT(!var
->arg
|| var
->arg
== arg
);
561 return var
->arg
= arg
;
564 if (func
&& !strcmp(var
->name
, func
->var
->name
)) {
565 return var
->arg
= func
;
570 static inline decl_arg
*locate_struct_member(decl_struct
*s
, decl_var
*var
) {
572 return locate_decl_var_arg(var
, s
->args
, NULL
);
577 static inline constant
*locate_num_exp_constant(num_exp
*exp
, constants
*consts
) {
580 for (i
= 0; i
< consts
->count
; ++i
) {
581 constant
*cnst
= consts
->list
[i
];
583 if (!strcmp(cnst
->name
, exp
->u
.numb
)) {
585 return exp
->u
.cnst
= cnst
;
591 static inline decl_enum_item
*locate_num_exp_enum_item_ex(num_exp
*exp
, decl_enum
*e
) {
594 if (e
) for (k
= 0; k
< e
->items
->count
; ++k
) {
595 decl_enum_item
*i
= e
->items
->list
[k
];
597 if (!strcmp(i
->name
, exp
->u
.dvar
->name
)) {
598 free_decl_var(exp
->u
.dvar
);
606 static inline decl_enum_item
*locate_num_exp_enum_item(num_exp
*exp
, decl_enums
*enums
) {
609 if (enums
) for (j
= 0; j
< enums
->count
; ++j
) {
610 decl_enum
*e
= enums
->list
[j
];
611 decl_enum_item
*i
= locate_num_exp_enum_item_ex(exp
, e
);
619 static inline int validate_num_exp(PSI_Data
*data
, num_exp
*exp
, decl_args
*dargs
, decl_arg
*func
, decl_enum
*enm
) {
621 switch (exp
->operator) {
623 exp
->calculator
= psi_calc_add
;
626 exp
->calculator
= psi_calc_sub
;
629 exp
->calculator
= psi_calc_mul
;
632 exp
->calculator
= psi_calc_div
;
634 EMPTY_SWITCH_DEFAULT_CASE();
636 if (!validate_num_exp(data
, exp
->operand
, dargs
, func
, enm
)) {
642 if (!locate_decl_var_arg(exp
->u
.dvar
, dargs
, func
)) {
643 if (!locate_num_exp_enum_item(exp
, data
->enums
) && !locate_num_exp_enum_item_ex(exp
, enm
)) {
644 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown variable '%s' in numeric expression",
651 if (!locate_num_exp_constant(exp
, data
->consts
)) {
652 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown constant '%s' in numeric expression",
665 static inline int validate_decl_enum(PSI_Data
*data
, decl_enum
*e
) {
668 if (!e
->items
|| !e
->items
->count
) {
669 data
->error(data
, e
->token
, PSI_WARNING
, "Empty enum '%s'", e
->name
);
673 for (j
= 0; j
< e
->items
->count
; ++j
) {
674 decl_enum_item
*i
= e
->items
->list
[j
];
678 i
->inc
.t
= PSI_T_NUMBER
;
680 i
->inc
.operator = PSI_T_PLUS
;
681 i
->inc
.operand
= i
->prev
->num
?: &i
->prev
->inc
;
684 i
->inc
.t
= PSI_T_NUMBER
;
689 if (!validate_num_exp(data
, i
->num
, NULL
, NULL
, e
)) {
697 static inline int validate_set_value_handler(set_value
*set
) {
698 switch (set
->func
->type
) {
700 set
->func
->handler
= psi_to_bool
;
703 set
->func
->handler
= psi_to_int
;
706 set
->func
->handler
= psi_to_double
;
708 case PSI_T_TO_STRING
:
709 set
->func
->handler
= psi_to_string
;
712 set
->func
->handler
= psi_to_array
;
714 case PSI_T_TO_OBJECT
:
715 set
->func
->handler
= psi_to_object
;
718 set
->func
->handler
= psi_to_void
;
721 if (set
->outer
.set
&& set
->outer
.set
->func
->type
== PSI_T_TO_ARRAY
) {
722 set
->func
->handler
= psi_to_recursive
;
723 set
->inner
= set
->outer
.set
->inner
;
732 static inline void decl_var_arg_v(decl_args
*args
, va_list argp
) {
736 memset(args
, 0, sizeof(*args
));
738 while ((argc
= va_arg(argp
, int))) {
739 argv
= va_arg(argp
, decl_arg
**);
741 add_decl_arg(args
, *argv
++);
745 static inline int validate_set_value_ex(PSI_Data
*data
, set_value
*set
, decl_arg
*ref
, decl_args
*ref_list
) {
748 decl_var
*set_var
= set
->vars
->vars
[0];
750 if (!validate_set_value_handler(set
)) {
751 data
->error(data
, set
->func
->token
, PSI_WARNING
, "Invalid cast '%s' in `set` statement", set
->func
->name
);
755 for (i
= 0; i
< set
->vars
->count
; ++i
) {
756 decl_var
*svar
= set
->vars
->vars
[i
];
757 if (!svar
->arg
&& !locate_decl_var_arg(svar
, ref_list
, NULL
)) {
758 data
->error(data
, svar
->token
, PSI_WARNING
, "Unknown variable '%s' in `set` statement", svar
->name
);
766 ref_type
= real_decl_type(ref
->type
);
768 if (set
->inner
&& set
->inner
->count
) {
769 int is_to_array
= (set
->func
->type
== PSI_T_TO_ARRAY
);
770 int is_pointer_to_struct
= (ref_type
->type
== PSI_T_STRUCT
&& ref
->var
->pointer_level
);
772 if (!is_to_array
&& !is_pointer_to_struct
) {
773 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts only work with "
774 "to_array() casts on structs or pointers: %s(%s...", set
->func
->name
, set
->vars
->vars
[0]->name
);
779 if (!validate_num_exp(data
, set
->num
, ref_list
, ref
, NULL
)) {
784 if (ref_type
->type
== PSI_T_STRUCT
) {
785 /* to_array(struct, to_...) */
786 if (!set
->outer
.set
|| set
->outer
.set
->inner
->vals
!= set
->inner
->vals
) {
787 for (i
= 0; i
< set
->inner
->count
; ++i
) {
788 decl_var
*sub_var
= set
->inner
->vals
[i
]->vars
->vars
[0];
789 decl_arg
*sub_ref
= locate_struct_member(ref_type
->strct
, sub_var
);
792 if (!validate_set_value_ex(data
, set
->inner
->vals
[i
], sub_ref
, ref_type
->strct
->args
)) {
798 } else if (set
->inner
&& set
->inner
->count
== 1) {
799 /* to_array(ptr, to_string(*ptr)) */
800 decl_var
*sub_var
= set
->inner
->vals
[0]->vars
->vars
[0];
801 decl_arg
*sub_ref
= locate_decl_var_arg(sub_var
, ref_list
, ref
);
804 if (strcmp(sub_var
->name
, set_var
->name
)) {
805 data
->error(data
, sub_var
->token
, E_WARNING
, "Inner `set` statement casts on pointers must reference the same variable");
808 if (!validate_set_value_ex(data
, set
->inner
->vals
[0], sub_ref
, ref_list
)) {
812 } else if (set
->inner
&& set
->inner
->count
> 1) {
813 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts on pointers may only occur once");
819 static inline int validate_set_value(PSI_Data
*data
, set_value
*set
, ...) {
821 decl_args args
= {0};
825 decl_var_arg_v(&args
, argp
);
828 check
= validate_set_value_ex(data
, set
, NULL
, &args
);
834 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
838 for (i
= 0; i
< decls
->count
; ++i
) {
839 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->set
->vars
->vars
[0]->name
)) {
840 ret
->decl
= decls
->list
[i
]->func
;
841 return decls
->list
[i
];
848 static inline int validate_impl_ret_stmt(PSI_Data
*data
, impl
*impl
) {
851 /* we must have exactly one ret stmt delcaring the native func to call */
852 /* and which type cast to apply */
853 if (impl
->stmts
->ret
.count
!= 1) {
854 if (impl
->stmts
->ret
.count
> 1) {
855 data
->error(data
, impl
->stmts
->ret
.list
[1]->token
, PSI_WARNING
,
856 "Too many `return` statements for implmentation %s;"
857 " found %zu, exactly one is needed",
858 impl
->func
->name
, impl
->stmts
->ret
.count
);
860 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
861 "Missing `return` statement for implementation %s",
867 ret
= impl
->stmts
->ret
.list
[0];
869 if (!(impl
->decl
= locate_impl_decl(data
->decls
, ret
))) {
870 data
->error(data
, ret
->token
, PSI_WARNING
,
871 "Missing declaration '%s' for `return` statment for implementation %s",
872 ret
->set
->vars
->vars
[0]->name
, impl
->func
->name
);
876 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)) {
880 impl
->decl
->impl
= impl
;
885 static inline int validate_let_func(PSI_Data
*data
, let_func
*func
, impl
*impl
) {
889 if (impl
->func
->args
) {
890 for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
891 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
893 if (!strcmp(func
->var
->name
, iarg
->var
->name
)) {
901 data
->error(data
, func
->var
->token
, PSI_WARNING
, "Unknown value '$%s' of `let` statement"
902 " for cast '%s' of implementation '%s'",
903 func
->var
->name
, func
->name
, impl
->func
->name
);
909 static inline int validate_let_callback(PSI_Data
*data
, decl_var
*let_var
, let_callback
*cb
, impl
*impl
) {
914 if (!validate_let_func(data
, cb
->func
, impl
)) {
918 cb_type
= real_decl_type(let_var
->arg
->type
);
919 if (cb_type
->type
!= PSI_T_FUNCTION
) {
920 data
->error(data
, let_var
->token
, PSI_WARNING
, "Not a function: %s", let_var
->name
);
923 cb_func
= cb_type
->func
;
924 for (i
= 0; i
< cb
->args
->count
; ++i
) {
925 if (!validate_set_value(data
, cb
->args
->vals
[i
], cb_func
->args
->count
, cb_func
->args
->args
, 0)) {
931 static inline int validate_impl_let_stmts(PSI_Data
*data
, impl
*impl
) {
933 /* we can have multiple let stmts */
935 /* check that we have a decl arg for every let stmt */
936 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
937 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
941 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
942 let_var
= let
->val
->data
.var
;
947 if (!locate_decl_var_arg(let_var
, impl
->decl
->args
, impl
->decl
->func
)) {
948 data
->error(data
, let_var
->token
, PSI_WARNING
, "Unknown variable '%s' in `let` statement"
949 " of implementation '%s'", let_var
->name
, impl
->func
->name
);
953 switch (let
->val
->kind
) {
957 /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */
958 /* e.g. let foo = *bar; */
959 let
->var
->pointer_level
= let
->val
->data
.var
->pointer_level
;
960 let
->var
->arg
= init_decl_arg(
962 real_decl_type(let
->val
->data
.var
->arg
->type
)->type
,
963 real_decl_type(let
->val
->data
.var
->arg
->type
)->name
),
966 let
->var
->pointer_level
,
967 let
->var
->array_size
));
970 if (!validate_num_exp(data
, let
->val
->data
.num
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
975 if (!validate_num_exp(data
, let
->val
->data
.alloc
->nmemb
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
978 if (!validate_num_exp(data
, let
->val
->data
.alloc
->size
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
982 case PSI_LET_CALLBACK
:
983 if (!validate_let_callback(data
, let
->var
, let
->val
->data
.callback
, impl
)) {
988 if (!validate_let_func(data
, let
->val
->data
.func
, impl
)) {
995 /* check that we have a let stmt for every decl arg */
996 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
997 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
1000 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
1001 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
1003 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
1010 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1011 "Missing `let` statement for arg '%s %.*s%s'"
1012 " of declaration '%s' for implementation '%s'",
1013 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
1014 darg
->var
->name
, impl
->decl
->func
->var
->name
, impl
->func
->name
);
1021 static inline int validate_impl_set_stmts(PSI_Data
*data
, impl
*impl
) {
1023 /* we can have any count of set stmts; processing out vars */
1024 /* check that set stmts reference known variables */
1025 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
1026 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
1029 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
1030 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
1032 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
1039 data
->error(data
, set
->var
->token
, PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
1040 " of implementation '%s'",
1041 set
->var
->name
, impl
->func
->name
);
1045 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
1046 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
1049 if (impl
->decl
->args
) {
1050 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1051 decl_arg
*set_arg
= impl
->decl
->args
->args
[k
];
1053 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
1055 set_var
->arg
= set_arg
;
1056 if (!validate_set_value(data
, set
->val
, 1, &set_arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1064 for (k
= 0; k
< impl
->stmts
->let
.count
; ++k
) {
1065 let_stmt
*let
= impl
->stmts
->let
.list
[k
];
1067 /* check temp vars */
1068 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
1069 if (!strcmp(set_var
->name
, let
->var
->name
)) {
1071 set_var
->arg
= let
->var
->arg
;
1072 if (!validate_set_value(data
, set
->val
, 1, &set_var
->arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1082 data
->error(data
, set_var
->token
, PSI_WARNING
, "Unknown value '%s' of `set` statement"
1083 " for variable '$%s' of implementation '%s'",
1084 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
1091 static inline decl
*locate_free_decl(decls
*decls
, free_call
*f
) {
1095 for (i
= 0; i
< decls
->count
; ++i
) {
1096 if (!strcmp(decls
->list
[i
]->func
->var
->name
, f
->func
)) {
1097 f
->decl
= decls
->list
[i
];
1098 return decls
->list
[i
];
1105 static inline int validate_impl_free_stmts(PSI_Data
*data
, impl
*impl
) {
1107 /* we can have any count of free stmts; freeing any out vars */
1108 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
1109 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1111 for (j
= 0; j
< fre
->calls
->count
; ++j
) {
1112 free_call
*free_call
= fre
->calls
->list
[j
];
1114 /* first find the decl of the free func */
1115 if (!locate_free_decl(data
->decls
, free_call
)) {
1116 data
->error(data
, free_call
->token
, PSI_WARNING
,
1117 "Missing declaration '%s' in `free` statement"
1118 " of implementation '%s'",
1119 free_call
->func
, impl
->func
->name
);
1125 /* now check for known vars */
1126 for (l
= 0; l
< free_call
->vars
->count
; ++l
) {
1128 decl_var
*free_var
= free_call
->vars
->vars
[l
];
1130 if (!strcmp(free_var
->name
, impl
->decl
->func
->var
->name
)) {
1132 free_var
->arg
= impl
->decl
->func
;
1133 } else if (impl
->decl
->args
) {
1134 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1135 decl_arg
*free_arg
= impl
->decl
->args
->args
[k
];
1137 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
1139 free_var
->arg
= free_arg
;
1146 data
->error(data
, free_var
->token
, PSI_WARNING
,
1147 "Unknown variable '%s' of `free` statement"
1148 " of implementation '%s'",
1149 free_var
->name
, impl
->func
->name
);
1157 static inline int validate_impl_stmts(PSI_Data
*data
, impl
*impl
) {
1159 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1160 "Missing body for implementation %s!",
1165 if (!validate_impl_ret_stmt(data
, impl
)) {
1169 if (!validate_impl_let_stmts(data
, impl
)) {
1172 if (!validate_impl_set_stmts(data
, impl
)) {
1175 if (!validate_impl_free_stmts(data
, impl
)) {
1182 static inline int validate_impl_args(PSI_Data
*data
, impl
*impl
) {
1186 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
1187 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
1192 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1193 "Non-optional argument %zu '$%s' of implementation '%s'"
1194 " follows optional argument",
1195 i
+1, iarg
->var
->name
, impl
->func
->name
);
1203 static inline int validate_impl(PSI_Data
*data
, impl
*impl
) {
1204 if (!validate_impl_args(data
, impl
)) {
1207 return validate_impl_stmts(data
, impl
);
1211 int PSI_ContextValidate(PSI_Context
*C
, PSI_Parser
*P
)
1214 void *dlopened
= NULL
;
1215 size_t i
, count
= C
->count
++, check_round
, check_count
;
1216 decl_typedefs
*check_defs
= P
->defs
;
1217 decl_structs
*check_structs
= P
->structs
;
1218 decl_unions
*check_unions
= P
->unions
;
1219 decl_enums
*check_enums
= P
->enums
;
1220 unsigned silent
= C
->flags
& PSI_PARSER_SILENT
;
1222 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
1223 D
= PSI_DataExchange(&C
->data
[count
], PSI_DATA(P
));
1225 #define REVALIDATE(what) do { \
1226 if (check_round && check_ ##what) { \
1227 free(check_ ##what->list); \
1228 free(check_ ##what); \
1230 check_ ##what = recheck_ ##what; \
1232 #define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
1233 #define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
1236 /* no warnings on first round */
1237 C
->flags
|= PSI_PARSER_SILENT
;
1239 for (check_round
= 0, check_count
= 0; CHECK_TOTAL
&& check_count
!= CHECK_TOTAL
; ++check_round
) {
1240 decl_typedefs
*recheck_defs
= NULL
;
1241 decl_structs
*recheck_structs
= NULL
;
1242 decl_unions
*recheck_unions
= NULL
;
1243 decl_enums
*recheck_enums
= NULL
;
1245 check_count
= CHECK_TOTAL
;
1247 for (i
= 0; i
< CHECK_COUNT(defs
); ++i
) {
1248 if (validate_decl_typedef(PSI_DATA(C
), check_defs
->list
[i
])) {
1249 C
->defs
= add_decl_typedef(C
->defs
, check_defs
->list
[i
]);
1251 recheck_defs
= add_decl_typedef(recheck_defs
, check_defs
->list
[i
]);
1254 for (i
= 0; i
< CHECK_COUNT(structs
); ++i
) {
1255 if (validate_decl_struct(PSI_DATA(C
), check_structs
->list
[i
])) {
1256 C
->structs
= add_decl_struct(C
->structs
, check_structs
->list
[i
]);
1258 recheck_structs
= add_decl_struct(recheck_structs
, check_structs
->list
[i
]);
1261 for (i
= 0; i
< CHECK_COUNT(unions
); ++i
) {
1262 if (validate_decl_union(PSI_DATA(C
), check_unions
->list
[i
])) {
1263 C
->unions
= add_decl_union(C
->unions
, check_unions
->list
[i
]);
1265 recheck_unions
= add_decl_union(recheck_unions
, check_unions
->list
[i
]);
1268 for (i
= 0; i
< CHECK_COUNT(enums
); ++i
) {
1269 if (validate_decl_enum(PSI_DATA(C
), check_enums
->list
[i
])) {
1270 C
->enums
= add_decl_enum(C
->enums
, check_enums
->list
[i
]);
1272 recheck_enums
= add_decl_enum(recheck_enums
, check_enums
->list
[i
]);
1277 REVALIDATE(structs
);
1281 if (check_round
== 0 && !silent
) {
1282 C
->flags
&= ~PSI_PARSER_SILENT
;
1288 for (i
= 0; i
< D
->consts
->count
; ++i
) {
1289 if (validate_constant(PSI_DATA(C
), D
->consts
->list
[i
])) {
1290 C
->consts
= add_constant(C
->consts
, D
->consts
->list
[i
]);
1295 if (!validate_lib(D
, &dlopened
)) {
1299 add_decl_lib(&C
->psi
.libs
, dlopened
);
1302 for (i
= 0; i
< D
->decls
->count
; ++i
) {
1303 if (validate_decl(PSI_DATA(C
), dlopened
, D
->decls
->list
[i
])) {
1304 C
->decls
= add_decl(C
->decls
, D
->decls
->list
[i
]);
1309 for (i
= 0; i
< D
->impls
->count
; ++i
) {
1310 if (validate_impl(PSI_DATA(C
), D
->impls
->list
[i
])) {
1311 C
->impls
= add_impl(C
->impls
, D
->impls
->list
[i
]);
1319 int PSI_ContextValidateData(PSI_Data
*dest
, PSI_Data
*source
)
1324 if (source
->defs
) for (i
= 0; i
< source
->defs
->count
; ++i
) {
1325 decl_arg
*def
= source
->defs
->list
[i
];
1327 if (validate_decl_typedef(source
, def
)) {
1329 dest
->defs
= add_decl_typedef(dest
->defs
, def
);
1336 if (source
->consts
) for (i
= 0; i
< source
->consts
->count
; ++i
) {
1337 constant
*constant
= source
->consts
->list
[i
];
1339 if (validate_constant(source
, constant
)) {
1341 dest
->consts
= add_constant(dest
->consts
, constant
);
1348 if (source
->structs
) for (i
= 0; i
< source
->structs
->count
; ++i
) {
1349 decl_struct
*dstruct
= source
->structs
->list
[i
];
1351 if (validate_decl_struct(source
, dstruct
)) {
1353 dest
->structs
= add_decl_struct(dest
->structs
, dstruct
);
1360 if (source
->unions
) for (i
= 0; i
< source
->unions
->count
; ++i
) {
1361 decl_union
*dunion
= source
->unions
->list
[i
];
1363 if (validate_decl_union(source
, dunion
)) {
1365 dest
->unions
= add_decl_union(dest
->unions
, dunion
);
1372 if (source
->enums
) for (i
= 0; i
< source
->enums
->count
; ++i
) {
1373 decl_enum
*denum
= source
->enums
->list
[i
];
1375 if (validate_decl_enum(source
, denum
)) {
1377 dest
->enums
= add_decl_enum(dest
->enums
, denum
);
1384 if (source
->decls
) for (i
= 0; i
< source
->decls
->count
; ++i
) {
1385 decl
*decl
= source
->decls
->list
[i
];
1387 if (validate_decl(source
, NULL
, decl
)) {
1389 dest
->decls
= add_decl(dest
->decls
, decl
);
1396 if (source
->impls
) for (i
= 0; i
< source
->impls
->count
; ++i
) {
1397 impl
*impl
= source
->impls
->list
[i
];
1399 if (validate_impl(source
, impl
)) {
1401 dest
->impls
= add_impl(dest
->impls
, impl
);