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
;
724 set
->count
= set
->outer
.set
->count
;
733 static inline void decl_var_arg_v(decl_args
*args
, va_list argp
) {
737 memset(args
, 0, sizeof(*args
));
739 while ((argc
= va_arg(argp
, int))) {
740 argv
= va_arg(argp
, decl_arg
**);
742 add_decl_arg(args
, *argv
++);
746 static inline int validate_set_value_ex(PSI_Data
*data
, set_value
*set
, decl_arg
*ref
, decl_args
*ref_list
) {
749 decl_var
*set_var
= set
->vars
->vars
[0];
751 if (!validate_set_value_handler(set
)) {
752 data
->error(data
, set
->func
->token
, PSI_WARNING
, "Invalid cast '%s' in `set` statement", set
->func
->name
);
756 for (i
= 0; i
< set
->vars
->count
; ++i
) {
757 decl_var
*svar
= set
->vars
->vars
[i
];
758 if (!svar
->arg
&& !locate_decl_var_arg(svar
, ref_list
, NULL
)) {
759 data
->error(data
, svar
->token
, PSI_WARNING
, "Unknown variable '%s' in `set` statement", svar
->name
);
767 ref_type
= real_decl_type(ref
->type
);
770 int is_to_array
= (set
->func
->type
== PSI_T_TO_ARRAY
);
771 int is_pointer_to_struct
= (ref_type
->type
== PSI_T_STRUCT
&& ref
->var
->pointer_level
);
773 if (!is_to_array
&& !is_pointer_to_struct
) {
774 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts only work with "
775 "to_array() casts on structs or pointers: %s(%s...", set
->func
->name
, set
->vars
->vars
[0]->name
);
780 if (!validate_num_exp(data
, set
->num
, ref_list
, ref
, NULL
)) {
785 if (ref_type
->type
== PSI_T_STRUCT
) {
786 /* to_array(struct, to_...) */
787 if (!set
->outer
.set
|| set
->outer
.set
->inner
!= set
->inner
) {
788 for (i
= 0; i
< set
->count
; ++i
) {
789 decl_var
*sub_var
= set
->inner
[i
]->vars
->vars
[0];
790 decl_arg
*sub_ref
= locate_struct_member(ref_type
->strct
, sub_var
);
793 if (!validate_set_value_ex(data
, set
->inner
[i
], sub_ref
, ref_type
->strct
->args
)) {
799 } else if (set
->count
== 1) {
800 /* to_array(ptr, to_string(*ptr)) */
801 decl_var
*sub_var
= set
->inner
[0]->vars
->vars
[0];
802 decl_arg
*sub_ref
= locate_decl_var_arg(sub_var
, ref_list
, ref
);
805 if (strcmp(sub_var
->name
, set_var
->name
)) {
806 data
->error(data
, sub_var
->token
, E_WARNING
, "Inner `set` statement casts on pointers must reference the same variable");
809 if (!validate_set_value_ex(data
, set
->inner
[0], sub_ref
, ref_list
)) {
813 } else if (set
->count
> 1) {
814 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts on pointers may only occur once");
820 static inline int validate_set_value(PSI_Data
*data
, set_value
*set
, ...) {
822 decl_args args
= {0};
826 decl_var_arg_v(&args
, argp
);
829 check
= validate_set_value_ex(data
, set
, NULL
, &args
);
835 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
839 for (i
= 0; i
< decls
->count
; ++i
) {
840 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->set
->vars
->vars
[0]->name
)) {
841 ret
->decl
= decls
->list
[i
]->func
;
842 return decls
->list
[i
];
849 static inline int validate_impl_ret_stmt(PSI_Data
*data
, impl
*impl
) {
852 /* we must have exactly one ret stmt delcaring the native func to call */
853 /* and which type cast to apply */
854 if (impl
->stmts
->ret
.count
!= 1) {
855 if (impl
->stmts
->ret
.count
> 1) {
856 data
->error(data
, impl
->stmts
->ret
.list
[1]->token
, PSI_WARNING
,
857 "Too many `return` statements for implmentation %s;"
858 " found %zu, exactly one is needed",
859 impl
->func
->name
, impl
->stmts
->ret
.count
);
861 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
862 "Missing `return` statement for implementation %s",
868 ret
= impl
->stmts
->ret
.list
[0];
870 if (!(impl
->decl
= locate_impl_decl(data
->decls
, ret
))) {
871 data
->error(data
, ret
->token
, PSI_WARNING
,
872 "Missing declaration '%s' for `return` statment for implementation %s",
873 ret
->set
->vars
->vars
[0]->name
, impl
->func
->name
);
877 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)) {
881 impl
->decl
->impl
= impl
;
886 static inline int validate_impl_let_stmts(PSI_Data
*data
, impl
*impl
) {
888 /* we can have multiple let stmts */
890 /* check that we have a decl arg for every let stmt */
891 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
892 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
896 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
897 let_var
= let
->val
->data
.var
;
902 if (!locate_decl_var_arg(let_var
, impl
->decl
->args
, impl
->decl
->func
)) {
903 data
->error(data
, let_var
->token
, PSI_WARNING
, "Unknown variable '%s' in `let` statement"
904 " of implementation '%s'", let_var
->name
, impl
->func
->name
);
908 switch (let
->val
->kind
) {
912 /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */
913 /* e.g. let foo = *bar; */
914 let
->var
->pointer_level
= let
->val
->data
.var
->pointer_level
;
915 let
->var
->arg
= init_decl_arg(
917 real_decl_type(let
->val
->data
.var
->arg
->type
)->type
,
918 real_decl_type(let
->val
->data
.var
->arg
->type
)->name
),
921 let
->var
->pointer_level
,
922 let
->var
->array_size
));
925 if (!validate_num_exp(data
, let
->val
->data
.num
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
930 if (!validate_num_exp(data
, let
->val
->data
.alloc
->nmemb
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
933 if (!validate_num_exp(data
, let
->val
->data
.alloc
->size
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
938 if (impl
->func
->args
) {
939 for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
940 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
942 if (!strcmp(let
->val
->data
.func
->var
->name
, iarg
->var
->name
)) {
943 let
->val
->data
.func
->arg
= iarg
;
950 data
->error(data
, let
->var
->token
, PSI_WARNING
, "Unknown value '$%s' of `let` statement"
951 " for variable '%s' of implementation '%s'",
952 let
->val
->data
.func
->var
->name
, let
->var
->name
, impl
->func
->name
);
959 /* check that we have a let stmt for every decl arg */
960 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
961 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
964 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
965 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
967 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
974 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
975 "Missing `let` statement for arg '%s %.*s%s'"
976 " of declaration '%s' for implementation '%s'",
977 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
978 darg
->var
->name
, impl
->decl
->func
->var
->name
, impl
->func
->name
);
985 static inline int validate_impl_set_stmts(PSI_Data
*data
, impl
*impl
) {
987 /* we can have any count of set stmts; processing out vars */
988 /* check that set stmts reference known variables */
989 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
990 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
993 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
994 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
996 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
1003 data
->error(data
, set
->var
->token
, PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
1004 " of implementation '%s'",
1005 set
->var
->name
, impl
->func
->name
);
1009 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
1010 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
1013 if (impl
->decl
->args
) {
1014 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1015 decl_arg
*set_arg
= impl
->decl
->args
->args
[k
];
1017 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
1019 set_var
->arg
= set_arg
;
1020 if (!validate_set_value(data
, set
->val
, 1, &set_arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1028 for (k
= 0; k
< impl
->stmts
->let
.count
; ++k
) {
1029 let_stmt
*let
= impl
->stmts
->let
.list
[k
];
1031 /* check temp vars */
1032 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
1033 if (!strcmp(set_var
->name
, let
->var
->name
)) {
1035 set_var
->arg
= let
->var
->arg
;
1036 if (!validate_set_value(data
, set
->val
, 1, &set_var
->arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1046 data
->error(data
, set_var
->token
, PSI_WARNING
, "Unknown value '%s' of `set` statement"
1047 " for variable '$%s' of implementation '%s'",
1048 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
1055 static inline decl
*locate_free_decl(decls
*decls
, free_call
*f
) {
1059 for (i
= 0; i
< decls
->count
; ++i
) {
1060 if (!strcmp(decls
->list
[i
]->func
->var
->name
, f
->func
)) {
1061 f
->decl
= decls
->list
[i
];
1062 return decls
->list
[i
];
1069 static inline int validate_impl_free_stmts(PSI_Data
*data
, impl
*impl
) {
1071 /* we can have any count of free stmts; freeing any out vars */
1072 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
1073 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1075 for (j
= 0; j
< fre
->calls
->count
; ++j
) {
1076 free_call
*free_call
= fre
->calls
->list
[j
];
1078 /* first find the decl of the free func */
1079 if (!locate_free_decl(data
->decls
, free_call
)) {
1080 data
->error(data
, free_call
->token
, PSI_WARNING
,
1081 "Missing declaration '%s' in `free` statement"
1082 " of implementation '%s'",
1083 free_call
->func
, impl
->func
->name
);
1089 /* now check for known vars */
1090 for (l
= 0; l
< free_call
->vars
->count
; ++l
) {
1092 decl_var
*free_var
= free_call
->vars
->vars
[l
];
1094 if (!strcmp(free_var
->name
, impl
->decl
->func
->var
->name
)) {
1096 free_var
->arg
= impl
->decl
->func
;
1097 } else if (impl
->decl
->args
) {
1098 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1099 decl_arg
*free_arg
= impl
->decl
->args
->args
[k
];
1101 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
1103 free_var
->arg
= free_arg
;
1110 data
->error(data
, free_var
->token
, PSI_WARNING
,
1111 "Unknown variable '%s' of `free` statement"
1112 " of implementation '%s'",
1113 free_var
->name
, impl
->func
->name
);
1121 static inline int validate_impl_stmts(PSI_Data
*data
, impl
*impl
) {
1123 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1124 "Missing body for implementation %s!",
1129 if (!validate_impl_ret_stmt(data
, impl
)) {
1133 if (!validate_impl_let_stmts(data
, impl
)) {
1136 if (!validate_impl_set_stmts(data
, impl
)) {
1139 if (!validate_impl_free_stmts(data
, impl
)) {
1146 static inline int validate_impl_args(PSI_Data
*data
, impl
*impl
) {
1150 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
1151 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
1156 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1157 "Non-optional argument %zu '$%s' of implementation '%s'"
1158 " follows optional argument",
1159 i
+1, iarg
->var
->name
, impl
->func
->name
);
1167 static inline int validate_impl(PSI_Data
*data
, impl
*impl
) {
1168 if (!validate_impl_args(data
, impl
)) {
1171 return validate_impl_stmts(data
, impl
);
1175 int PSI_ContextValidate(PSI_Context
*C
, PSI_Parser
*P
)
1178 void *dlopened
= NULL
;
1179 size_t i
, count
= C
->count
++, check_round
, check_count
;
1180 decl_typedefs
*check_defs
= P
->defs
;
1181 decl_structs
*check_structs
= P
->structs
;
1182 decl_unions
*check_unions
= P
->unions
;
1183 decl_enums
*check_enums
= P
->enums
;
1184 unsigned silent
= C
->flags
& PSI_PARSER_SILENT
;
1186 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
1187 D
= PSI_DataExchange(&C
->data
[count
], PSI_DATA(P
));
1189 #define REVALIDATE(what) do { \
1190 if (check_round && check_ ##what) { \
1191 free(check_ ##what->list); \
1192 free(check_ ##what); \
1194 check_ ##what = recheck_ ##what; \
1196 #define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
1197 #define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
1200 /* no warnings on first round */
1201 C
->flags
|= PSI_PARSER_SILENT
;
1203 for (check_round
= 0, check_count
= 0; CHECK_TOTAL
&& check_count
!= CHECK_TOTAL
; ++check_round
) {
1204 decl_typedefs
*recheck_defs
= NULL
;
1205 decl_structs
*recheck_structs
= NULL
;
1206 decl_unions
*recheck_unions
= NULL
;
1207 decl_enums
*recheck_enums
= NULL
;
1209 check_count
= CHECK_TOTAL
;
1211 for (i
= 0; i
< CHECK_COUNT(defs
); ++i
) {
1212 if (validate_decl_typedef(PSI_DATA(C
), check_defs
->list
[i
])) {
1213 C
->defs
= add_decl_typedef(C
->defs
, check_defs
->list
[i
]);
1215 recheck_defs
= add_decl_typedef(recheck_defs
, check_defs
->list
[i
]);
1218 for (i
= 0; i
< CHECK_COUNT(structs
); ++i
) {
1219 if (validate_decl_struct(PSI_DATA(C
), check_structs
->list
[i
])) {
1220 C
->structs
= add_decl_struct(C
->structs
, check_structs
->list
[i
]);
1222 recheck_structs
= add_decl_struct(recheck_structs
, check_structs
->list
[i
]);
1225 for (i
= 0; i
< CHECK_COUNT(unions
); ++i
) {
1226 if (validate_decl_union(PSI_DATA(C
), check_unions
->list
[i
])) {
1227 C
->unions
= add_decl_union(C
->unions
, check_unions
->list
[i
]);
1229 recheck_unions
= add_decl_union(recheck_unions
, check_unions
->list
[i
]);
1232 for (i
= 0; i
< CHECK_COUNT(enums
); ++i
) {
1233 if (validate_decl_enum(PSI_DATA(C
), check_enums
->list
[i
])) {
1234 C
->enums
= add_decl_enum(C
->enums
, check_enums
->list
[i
]);
1236 recheck_enums
= add_decl_enum(recheck_enums
, check_enums
->list
[i
]);
1241 REVALIDATE(structs
);
1245 if (check_round
== 0 && !silent
) {
1246 C
->flags
&= ~PSI_PARSER_SILENT
;
1252 for (i
= 0; i
< D
->consts
->count
; ++i
) {
1253 if (validate_constant(PSI_DATA(C
), D
->consts
->list
[i
])) {
1254 C
->consts
= add_constant(C
->consts
, D
->consts
->list
[i
]);
1259 if (!validate_lib(D
, &dlopened
)) {
1263 add_decl_lib(&C
->psi
.libs
, dlopened
);
1266 for (i
= 0; i
< D
->decls
->count
; ++i
) {
1267 if (validate_decl(PSI_DATA(C
), dlopened
, D
->decls
->list
[i
])) {
1268 C
->decls
= add_decl(C
->decls
, D
->decls
->list
[i
]);
1273 for (i
= 0; i
< D
->impls
->count
; ++i
) {
1274 if (validate_impl(PSI_DATA(C
), D
->impls
->list
[i
])) {
1275 C
->impls
= add_impl(C
->impls
, D
->impls
->list
[i
]);
1283 int PSI_ContextValidateData(PSI_Data
*dest
, PSI_Data
*source
)
1288 if (source
->defs
) for (i
= 0; i
< source
->defs
->count
; ++i
) {
1289 decl_arg
*def
= source
->defs
->list
[i
];
1291 if (validate_decl_typedef(source
, def
)) {
1293 dest
->defs
= add_decl_typedef(dest
->defs
, def
);
1300 if (source
->consts
) for (i
= 0; i
< source
->consts
->count
; ++i
) {
1301 constant
*constant
= source
->consts
->list
[i
];
1303 if (validate_constant(source
, constant
)) {
1305 dest
->consts
= add_constant(dest
->consts
, constant
);
1312 if (source
->structs
) for (i
= 0; i
< source
->structs
->count
; ++i
) {
1313 decl_struct
*dstruct
= source
->structs
->list
[i
];
1315 if (validate_decl_struct(source
, dstruct
)) {
1317 dest
->structs
= add_decl_struct(dest
->structs
, dstruct
);
1324 if (source
->unions
) for (i
= 0; i
< source
->unions
->count
; ++i
) {
1325 decl_union
*dunion
= source
->unions
->list
[i
];
1327 if (validate_decl_union(source
, dunion
)) {
1329 dest
->unions
= add_decl_union(dest
->unions
, dunion
);
1336 if (source
->enums
) for (i
= 0; i
< source
->enums
->count
; ++i
) {
1337 decl_enum
*denum
= source
->enums
->list
[i
];
1339 if (validate_decl_enum(source
, denum
)) {
1341 dest
->enums
= add_decl_enum(dest
->enums
, denum
);
1348 if (source
->decls
) for (i
= 0; i
< source
->decls
->count
; ++i
) {
1349 decl
*decl
= source
->decls
->list
[i
];
1351 if (validate_decl(source
, NULL
, decl
)) {
1353 dest
->decls
= add_decl(dest
->decls
, decl
);
1360 if (source
->impls
) for (i
= 0; i
< source
->impls
->count
; ++i
) {
1361 impl
*impl
= source
->impls
->list
[i
];
1363 if (validate_impl(source
, impl
)) {
1365 dest
->impls
= add_impl(dest
->impls
, impl
);