8add34d5ec53be4697a038b4e467e59c24517596
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(struct 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
)) {
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
) {
73 if (type
->real
.strct
) {
76 if (structs
) for (i
= 0; i
< structs
->count
; ++i
) {
77 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
78 type
->real
.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
->real
.unn
= unions
->list
[i
];
100 static inline int locate_decl_type_enum(decl_enums
*enums
, decl_type
*type
) {
103 if (type
->real
.enm
) {
106 if (enums
) for (i
= 0; i
< enums
->count
; ++i
) {
107 if (!strcmp(enums
->list
[i
]->name
, type
->name
)) {
108 type
->real
.enm
= enums
->list
[i
];
115 static inline int locate_decl_type_decl(decls
*decls
, decl_type
*type
) {
118 if (type
->real
.func
) {
121 if (decls
) for (i
= 0; i
< decls
->count
; ++i
) {
122 if (!strcmp(decls
->list
[i
]->func
->var
->name
, type
->name
)) {
123 type
->real
.func
= decls
->list
[i
];
131 static inline int validate_decl_nodl(struct psi_data
*data
, decl
*decl
);
132 static inline int validate_decl_struct(struct psi_data
*data
, decl_struct
*s
);
133 static inline int validate_decl_union(struct psi_data
*data
, decl_union
*u
);
134 static inline int validate_decl_enum(struct psi_data
*data
, decl_enum
*e
);
136 static inline int validate_decl_type(struct psi_data
*data
, decl_type
*type
, decl_arg
*def
) {
137 if (weak_decl_type(type
)) {
138 if (!locate_decl_type_alias(data
->defs
, type
)) {
141 if (type
->real
.def
) {
142 return validate_decl_type(data
, type
->real
.def
->type
, type
->real
.def
);
147 switch (type
->type
) {
149 if (!locate_decl_type_struct(data
->structs
, type
)) {
154 if (!locate_decl_type_union(data
->unions
, type
)) {
159 if (!locate_decl_type_enum(data
->enums
, type
)) {
164 if (!locate_decl_type_decl(data
->decls
, type
)) {
167 if (!validate_decl_nodl(data
, type
->real
.func
)) {
174 static inline int validate_decl_typedef(struct psi_data
*data
, decl_arg
*def
) {
175 if (!validate_decl_type(data
, def
->type
, def
)) {
178 switch (def
->type
->type
) {
179 case PSI_T_STRUCT
: pre
= "struct "; break;
180 case PSI_T_UNION
: pre
= "union "; break;
181 case PSI_T_ENUM
: pre
= "enum "; break;
182 default: pre
= ""; break;
184 data
->error(data
, def
->token
, PSI_WARNING
,
185 "Type '%s' cannot be aliased to %s'%s'",
186 def
->var
->name
, pre
, def
->type
->name
);
189 if (def
->type
->type
== PSI_T_VOID
) {
190 if (def
->var
->pointer_level
) {
191 def
->type
->type
= PSI_T_POINTER
;
193 data
->error(data
, def
->token
, PSI_WARNING
,
194 "Type '%s' cannot be aliased to 'void'",
202 static inline int validate_constant(struct psi_data
*data
, constant
*c
) {
207 static inline int validate_decl_arg(struct psi_data
*data
, decl_arg
*arg
) {
208 if (!validate_decl_type(data
, arg
->type
, NULL
)) {
209 data
->error(data
, arg
->type
->token
, PSI_WARNING
,
210 "Cannot use '%s' as type for '%s'",
211 arg
->type
->name
, arg
->var
->name
);
217 static int psi_sort_struct_arg_cmp(const void *_a
, const void *_b
) {
218 decl_arg
*a
= *(decl_arg
**)_a
, *b
= *(decl_arg
**)_b
;
220 if (a
->layout
->pos
== b
->layout
->pos
) {
221 if (a
->layout
->len
== b
->layout
->len
) {
223 } else if (a
->layout
->len
> b
->layout
->len
) {
228 } else if (a
->layout
->pos
> b
->layout
->pos
) {
234 static void psi_sort_struct_arg_swp(void *a
, void *b
) {
235 decl_arg
**_a
= a
, **_b
= b
, *_c
;
241 static inline void psi_sort_struct_args(void **args
, size_t count
) {
242 zend_insert_sort(args
, count
, sizeof(*args
),
243 psi_sort_struct_arg_cmp
, psi_sort_struct_arg_swp
);
246 static inline int validate_decl_struct_darg(struct psi_data
*data
, decl_arg
*darg
, void *current
) {
247 decl_type
*real
= real_decl_type(darg
->type
);
249 /* pre-validate any structs/unions/enums */
250 switch (real
->type
) {
252 if (current
&& current
== real
->real
.strct
) {
255 if (!locate_decl_type_struct(data
->structs
, real
)) {
258 if (!validate_decl_struct(data
, real
->real
.strct
)) {
263 if (current
&& current
== real
->real
.unn
) {
266 if (!locate_decl_type_union(data
->unions
, real
)) {
269 if (!validate_decl_union(data
, real
->real
.unn
)) {
274 if (current
&& current
== real
->real
.enm
) {
277 if (!locate_decl_type_enum(data
->enums
, real
)) {
280 if (!validate_decl_enum(data
, real
->real
.enm
)) {
289 static inline size_t sizeof_decl_arg(decl_arg
*darg
) {
291 decl_type
*real
= real_decl_type(darg
->type
);
293 if (darg
->var
->array_size
) {
294 if (darg
->var
->pointer_level
> 2) {
295 size
= psi_t_size(PSI_T_POINTER
) * darg
->var
->array_size
;
297 size
= psi_t_size(real
->type
) * darg
->var
->array_size
;
299 } else if (darg
->var
->pointer_level
) {
300 size
= psi_t_size(PSI_T_POINTER
);
302 switch (real
->type
) {
304 size
= real
->real
.unn
->size
;
307 size
= real
->real
.strct
->size
;
311 size
= psi_t_size(real
->type
);
321 static inline size_t alignof_decl_type(decl_type
*t
);
322 static inline size_t alignof_decl_arg(decl_arg
*darg
);
323 static inline size_t alignof_decl_union(decl_union
*u
);
324 static inline size_t alignof_decl_struct(decl_struct
*s
);
326 static inline size_t alignof_decl_args(decl_args
*args
) {
327 size_t i
, maxalign
= 0;
329 for (i
= 0; i
< args
->count
; ++i
) {
330 decl_arg
*darg
= args
->args
[i
];
331 size_t align
= alignof_decl_arg(darg
);
333 if (align
> maxalign
) {
341 static inline size_t alignof_decl_struct(decl_struct
*s
) {
343 s
->align
= alignof_decl_args(s
->args
);
348 static inline size_t alignof_decl_union(decl_union
*u
) {
350 u
->align
= alignof_decl_args(u
->args
);
355 static inline size_t alignof_decl_type(decl_type
*t
) {
356 decl_type
*real
= real_decl_type(t
);
359 switch (real
->type
) {
361 align
= alignof_decl_struct(real
->real
.strct
);
364 align
= alignof_decl_union(real
->real
.unn
);
368 align
= psi_t_alignment(real
->type
);
374 static inline size_t alignof_decl_arg(decl_arg
*darg
) {
377 if (darg
->var
->pointer_level
&& (!darg
->var
->array_size
|| darg
->var
->pointer_level
> 2)) {
378 align
= psi_t_alignment(PSI_T_POINTER
);
380 align
= alignof_decl_type(darg
->type
);
386 static inline size_t align_decl_arg(decl_arg
*darg
, size_t *pos
, size_t *len
) {
387 size_t align
= alignof_decl_arg(darg
);
391 *len
= sizeof_decl_arg(darg
);
392 *pos
= psi_align(align
, *pos
);
397 static inline int validate_decl_struct(struct psi_data
*data
, decl_struct
*s
) {
398 size_t i
, pos
, len
, size
, align
;
400 if (!s
->size
&& !s
->args
->count
) {
401 data
->error(data
, s
->token
, PSI_WARNING
,
402 "Cannot compute size of empty struct '%s'",
407 for (i
= 0; i
< s
->args
->count
; ++i
) {
408 decl_arg
*darg
= s
->args
->args
[i
];
410 if (!validate_decl_arg(data
, darg
)) {
414 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
415 darg
->var
->arg
= darg
;
417 if (!validate_decl_struct_darg(data
, darg
, s
)) {
419 } else if (darg
->layout
) {
420 pos
= darg
->layout
->pos
;
422 align
= align_decl_arg(darg
, &pos
, &len
);
424 if (darg
->layout
->len
!= len
) {
425 data
->error(data
, darg
->token
, PSI_WARNING
,
426 "Computed size %zu of %s.%s does not match"
427 " pre-defined size %zu of type '%s'",
428 len
, s
->name
, darg
->var
->name
, darg
->layout
->len
,
431 if (darg
->layout
->pos
!= pos
) {
432 data
->error(data
, darg
->token
, PSI_WARNING
,
433 "Computed offset %zu of %s.%s does not match"
434 " pre-defined offset %zu",
435 pos
, s
->name
, darg
->var
->name
, darg
->layout
->pos
);
439 pos
= s
->args
->args
[i
-1]->layout
->pos
+
440 s
->args
->args
[i
-1]->layout
->len
;
445 align
= align_decl_arg(darg
, &pos
, &len
);
446 darg
->layout
= init_decl_struct_layout(pos
, len
);
449 if (align
> s
->align
) {
454 psi_sort_struct_args((void **) s
->args
->args
, s
->args
->count
);
456 if (s
->args
->count
) {
457 decl_arg
*darg
= s
->args
->args
[s
->args
->count
-1];
459 size
= darg
->layout
->pos
+ darg
->layout
->len
;
460 if (s
->size
< size
) {
461 s
->size
= psi_align(size
, s
->align
);
468 static inline int validate_decl_union(struct psi_data
*data
, decl_union
*u
) {
469 size_t i
, pos
, len
, size
= 0, align
;
471 if (!u
->size
&& !u
->args
->count
) {
472 data
->error(data
, u
->token
, PSI_WARNING
,
473 "Cannot compute size of empty union %s",
478 for (i
= 0; i
< u
->args
->count
; ++i
) {
479 decl_arg
*darg
= u
->args
->args
[i
];
481 if (!validate_decl_arg(data
, darg
)) {
485 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
486 darg
->var
->arg
= darg
;
488 if (!validate_decl_struct_darg(data
, darg
, u
)) {
490 } else if (darg
->layout
) {
491 pos
= darg
->layout
->pos
;
493 align
= align_decl_arg(darg
, &pos
, &len
);
495 if (darg
->layout
->pos
!= 0) {
496 data
->error(data
, darg
->token
, PSI_WARNING
,
497 "Offset of %s.%s should be 0",
498 u
->name
, darg
->var
->name
);
499 darg
->layout
->pos
= 0;
501 if (darg
->layout
->len
!= len
) {
502 data
->error(data
, darg
->token
, PSI_WARNING
,
503 "Computed size %zu of %s.%s does not match"
504 " pre-defined size %zu of type '%s'",
505 len
, u
->name
, darg
->var
->name
, darg
->layout
->len
,
511 align
= align_decl_arg(darg
, &pos
, &len
);
512 darg
->layout
= init_decl_struct_layout(pos
, len
);
518 if (align
> u
->align
) {
523 psi_sort_struct_args((void **) u
->args
->args
, u
->args
->count
);
525 if (u
->size
< size
) {
526 u
->size
= psi_align(size
, u
->align
);
532 static const char * const abi_ccs
[] = {
534 "extern", /* > - all the same */
540 static inline int validate_decl_abi(struct psi_data
*data
, decl_abi
*abi
) {
543 for (i
= 0; i
< sizeof(abi_ccs
)/sizeof(char*); ++i
) {
544 if (strcasecmp(abi
->convention
, abi_ccs
[i
])) {
550 static inline int validate_decl_func(struct psi_data
*data
, void *dl
, decl
*decl
, decl_arg
*func
)
552 struct psi_func_redir
*redir
;
554 if (!strcmp(func
->var
->name
, "dlsym")) {
555 data
->error(data
, func
->token
, PSI_WARNING
, "Cannot dlsym dlsym (sic!)");
559 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
560 if (!strcmp(func
->var
->name
, redir
->name
)) {
561 decl
->call
.sym
= redir
->func
;
564 if (!decl
->call
.sym
) {
566 # define RTLD_NEXT ((void *) -1l)
568 decl
->call
.sym
= dlsym(dl
?: RTLD_NEXT
, func
->var
->name
);
569 if (!decl
->call
.sym
) {
570 data
->error(data
, func
->token
, PSI_WARNING
,
571 "Failed to locate symbol '%s': %s",
572 func
->var
->name
, dlerror() ?: "not found");
577 static inline int validate_decl_nodl(struct psi_data
*data
, decl
*decl
) {
578 if (!validate_decl_abi(data
, decl
->abi
)) {
579 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
580 "Invalid calling convention: '%s'", decl
->abi
->token
->text
);
583 if (!validate_decl_arg(data
, decl
->func
)) {
589 for (i
= 0; i
< decl
->args
->count
; ++i
) {
590 if (!validate_decl_arg(data
, decl
->args
->args
[i
])) {
597 static inline int validate_decl(struct psi_data
*data
, void *dl
, decl
*decl
) {
598 if (!validate_decl_nodl(data
, decl
)) {
601 if (!validate_decl_func(data
, dl
, decl
, decl
->func
)) {
606 static inline decl_arg
*locate_decl_var_arg(decl_var
*var
, decl_args
*args
, decl_arg
*func
) {
609 if (args
) for (i
= 0; i
< args
->count
; ++i
) {
610 decl_arg
*arg
= args
->args
[i
];
612 if (!strcmp(var
->name
, arg
->var
->name
)) {
613 ZEND_ASSERT(!var
->arg
|| var
->arg
== arg
);
614 return var
->arg
= arg
;
617 if (func
&& !strcmp(var
->name
, func
->var
->name
)) {
618 return var
->arg
= func
;
623 static inline decl_arg
*locate_struct_member(decl_struct
*s
, decl_var
*var
) {
625 return locate_decl_var_arg(var
, s
->args
, NULL
);
630 static inline constant
*locate_num_exp_constant(num_exp
*exp
, constants
*consts
) {
633 for (i
= 0; i
< consts
->count
; ++i
) {
634 constant
*cnst
= consts
->list
[i
];
636 if (!strcmp(cnst
->name
, exp
->u
.numb
)) {
638 return exp
->u
.cnst
= cnst
;
644 static inline decl_enum_item
*locate_num_exp_enum_item_ex(num_exp
*exp
, decl_enum
*e
) {
647 if (e
) for (k
= 0; k
< e
->items
->count
; ++k
) {
648 decl_enum_item
*i
= e
->items
->list
[k
];
650 if (!strcmp(i
->name
, exp
->u
.dvar
->name
)) {
651 free_decl_var(exp
->u
.dvar
);
659 static inline decl_enum_item
*locate_num_exp_enum_item(num_exp
*exp
, decl_enums
*enums
) {
662 if (enums
) for (j
= 0; j
< enums
->count
; ++j
) {
663 decl_enum
*e
= enums
->list
[j
];
664 decl_enum_item
*i
= locate_num_exp_enum_item_ex(exp
, e
);
672 static inline int validate_num_exp(struct psi_data
*data
, num_exp
*exp
, decl_args
*dargs
, decl_arg
*func
, decl_enum
*enm
) {
674 switch (exp
->operator) {
676 exp
->calculator
= psi_calc_add
;
679 exp
->calculator
= psi_calc_sub
;
682 exp
->calculator
= psi_calc_mul
;
685 exp
->calculator
= psi_calc_div
;
687 EMPTY_SWITCH_DEFAULT_CASE();
689 if (!validate_num_exp(data
, exp
->operand
, dargs
, func
, enm
)) {
695 if (!locate_decl_var_arg(exp
->u
.dvar
, dargs
, func
)) {
696 if (!locate_num_exp_enum_item(exp
, data
->enums
) && !locate_num_exp_enum_item_ex(exp
, enm
)) {
697 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown variable '%s' in numeric expression",
704 if (!locate_num_exp_constant(exp
, data
->consts
)) {
705 data
->error(data
, exp
->token
, PSI_WARNING
, "Unknown constant '%s' in numeric expression",
718 static inline int validate_decl_enum(struct psi_data
*data
, decl_enum
*e
) {
721 if (!e
->items
|| !e
->items
->count
) {
722 data
->error(data
, e
->token
, PSI_WARNING
, "Empty enum '%s'", e
->name
);
726 for (j
= 0; j
< e
->items
->count
; ++j
) {
727 decl_enum_item
*i
= e
->items
->list
[j
];
731 i
->inc
.t
= PSI_T_NUMBER
;
733 i
->inc
.operator = PSI_T_PLUS
;
734 i
->inc
.operand
= i
->prev
->num
?: &i
->prev
->inc
;
737 i
->inc
.t
= PSI_T_NUMBER
;
742 if (!validate_num_exp(data
, i
->num
, NULL
, NULL
, e
)) {
750 static inline int validate_set_value_handler(set_value
*set
) {
751 switch (set
->func
->type
) {
752 case PSI_T_TO_BOOL
: set
->func
->handler
= psi_to_bool
; break;
753 case PSI_T_TO_INT
: set
->func
->handler
= psi_to_int
; break;
754 case PSI_T_TO_FLOAT
: set
->func
->handler
= psi_to_double
; break;
755 case PSI_T_TO_STRING
: set
->func
->handler
= psi_to_string
; break;
756 case PSI_T_TO_ARRAY
: set
->func
->handler
= psi_to_array
; break;
757 case PSI_T_TO_OBJECT
: set
->func
->handler
= psi_to_object
; break;
758 case PSI_T_VOID
: set
->func
->handler
= psi_to_void
; break;
759 case PSI_T_ZVAL
: set
->func
->handler
= psi_to_zval
; break;
761 if (set
->outer
.set
&& set
->outer
.set
->func
->type
== PSI_T_TO_ARRAY
) {
762 set
->func
->handler
= psi_to_recursive
;
763 set
->inner
= set
->outer
.set
->inner
;
772 static inline void decl_var_arg_v(decl_args
*args
, va_list argp
) {
776 memset(args
, 0, sizeof(*args
));
778 while ((argc
= va_arg(argp
, int))) {
779 argv
= va_arg(argp
, decl_arg
**);
781 add_decl_arg(args
, *argv
++);
785 static inline int validate_set_value_ex(struct psi_data
*data
, set_value
*set
, decl_arg
*ref
, decl_args
*ref_list
) {
788 decl_var
*set_var
= set
->vars
->vars
[0];
790 if (!validate_set_value_handler(set
)) {
791 data
->error(data
, set
->func
->token
, PSI_WARNING
, "Invalid cast '%s' in `set` statement", set
->func
->name
);
795 for (i
= 0; i
< set
->vars
->count
; ++i
) {
796 decl_var
*svar
= set
->vars
->vars
[i
];
797 if (!svar
->arg
&& !locate_decl_var_arg(svar
, ref_list
, NULL
)) {
798 data
->error(data
, svar
->token
, PSI_WARNING
, "Unknown variable '%s' in `set` statement", svar
->name
);
806 ref_type
= real_decl_type(ref
->type
);
808 if (set
->inner
&& set
->inner
->count
) {
809 int is_to_array
= (set
->func
->type
== PSI_T_TO_ARRAY
);
810 int is_pointer_to_struct
= (ref_type
->type
== PSI_T_STRUCT
&& ref
->var
->pointer_level
);
812 if (!is_to_array
&& !is_pointer_to_struct
) {
813 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts only work with "
814 "to_array() casts on structs or pointers: %s(%s...", set
->func
->name
, set
->vars
->vars
[0]->name
);
819 if (!validate_num_exp(data
, set
->num
, ref_list
, ref
, NULL
)) {
824 if (set
->inner
&& ref_type
->type
== PSI_T_STRUCT
) {
825 /* to_array(struct, to_...) */
826 if (!set
->outer
.set
|| set
->outer
.set
->inner
->vals
!= set
->inner
->vals
) {
827 for (i
= 0; i
< set
->inner
->count
; ++i
) {
828 decl_var
*sub_var
= set
->inner
->vals
[i
]->vars
->vars
[0];
829 decl_arg
*sub_ref
= locate_struct_member(ref_type
->real
.strct
, sub_var
);
832 if (!validate_set_value_ex(data
, set
->inner
->vals
[i
], sub_ref
, ref_type
->real
.strct
->args
)) {
838 } else if (set
->inner
&& set
->inner
->count
== 1) {
839 /* to_array(ptr, to_string(*ptr)) */
840 decl_var
*sub_var
= set
->inner
->vals
[0]->vars
->vars
[0];
841 decl_arg
*sub_ref
= locate_decl_var_arg(sub_var
, ref_list
, ref
);
844 if (strcmp(sub_var
->name
, set_var
->name
)) {
845 data
->error(data
, sub_var
->token
, E_WARNING
, "Inner `set` statement casts on pointers must reference the same variable");
848 if (!validate_set_value_ex(data
, set
->inner
->vals
[0], sub_ref
, ref_list
)) {
852 } else if (set
->inner
&& set
->inner
->count
> 1) {
853 data
->error(data
, set
->func
->token
, E_WARNING
, "Inner `set` statement casts on pointers may only occur once");
859 static inline int validate_set_value(struct psi_data
*data
, set_value
*set
, ...) {
861 decl_args args
= {0};
865 decl_var_arg_v(&args
, argp
);
868 check
= validate_set_value_ex(data
, set
, NULL
, &args
);
874 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
878 for (i
= 0; i
< decls
->count
; ++i
) {
879 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->set
->vars
->vars
[0]->name
)) {
880 ret
->decl
= decls
->list
[i
]->func
;
881 return decls
->list
[i
];
888 static inline int validate_impl_ret_stmt(struct psi_data
*data
, impl
*impl
) {
891 /* we must have exactly one ret stmt delcaring the native func to call */
892 /* and which type cast to apply */
893 if (impl
->stmts
->ret
.count
!= 1) {
894 if (impl
->stmts
->ret
.count
> 1) {
895 data
->error(data
, impl
->stmts
->ret
.list
[1]->token
, PSI_WARNING
,
896 "Too many `return` statements for implmentation %s;"
897 " found %zu, exactly one is needed",
898 impl
->func
->name
, impl
->stmts
->ret
.count
);
900 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
901 "Missing `return` statement for implementation %s",
907 ret
= impl
->stmts
->ret
.list
[0];
909 if (!(impl
->decl
= locate_impl_decl(data
->decls
, ret
))) {
910 data
->error(data
, ret
->token
, PSI_WARNING
,
911 "Missing declaration '%s' for `return` statment for implementation %s",
912 ret
->set
->vars
->vars
[0]->name
, impl
->func
->name
);
916 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)) {
920 //impl->decl->impl = impl;
925 static inline impl_arg
*locate_impl_var_arg(impl_var
*var
, impl_args
*args
) {
928 for (i
= 0; i
< args
->count
; ++i
) {
929 impl_arg
*iarg
= args
->args
[i
];
931 if (!strcmp(var
->name
, iarg
->var
->name
)) {
932 return var
->arg
= iarg
;
939 static inline int validate_let_func(struct psi_data
*data
, let_func
*func
, impl
*impl
) {
940 if (impl
->func
->args
) {
941 if (!locate_impl_var_arg(func
->var
, impl
->func
->args
)) {
942 data
->error(data
, func
->var
->token
, PSI_WARNING
,
943 "Unknown variable '$%s' of `let` statement"
944 " for cast '%s' of implementation '%s'",
945 func
->var
->name
, func
->name
, impl
->func
->name
);
949 switch (func
->type
) {
950 case PSI_T_BOOLVAL
: func
->handler
= psi_let_boolval
; break;
951 case PSI_T_INTVAL
: func
->handler
= psi_let_intval
; break;
952 case PSI_T_FLOATVAL
: func
->handler
= psi_let_floatval
; break;
953 case PSI_T_STRVAL
: func
->handler
= psi_let_strval
; break;
954 case PSI_T_STRLEN
: func
->handler
= psi_let_strlen
; break;
955 case PSI_T_PATHVAL
: func
->handler
= psi_let_pathval
; break;
956 case PSI_T_ARRVAL
: func
->handler
= psi_let_arrval
; break;
957 case PSI_T_OBJVAL
: func
->handler
= psi_let_objval
; break;
958 case PSI_T_ZVAL
: func
->handler
= psi_let_zval
; break;
959 EMPTY_SWITCH_DEFAULT_CASE();
964 static inline int validate_let_callback(struct psi_data
*data
, decl_var
*cb_var
, let_callback
*cb
, impl
*impl
) {
967 decl_type
*cb_type
= real_decl_type(cb_var
->arg
->type
);
969 if (!validate_let_func(data
, cb
->func
, impl
)) {
973 if (cb_type
->type
!= PSI_T_FUNCTION
) {
974 data
->error(data
, cb_var
->token
, PSI_WARNING
, "Not a function: %s", cb_var
->name
);
977 cb_func
= cb_type
->real
.func
;
978 for (i
= 0; i
< cb
->args
->count
; ++i
) {
979 if (!validate_set_value(data
, cb
->args
->vals
[i
], cb_func
->args
->count
, cb_func
->args
->args
, 0)) {
984 if (!validate_decl_nodl(data
, cb_func
)) {
993 static inline int validate_impl_let_stmts(struct psi_data
*data
, impl
*impl
) {
995 /* we can have multiple let stmts */
997 /* check that we have a decl arg for every let stmt */
998 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
999 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
1002 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
1003 let_var
= let
->val
->data
.var
;
1008 if (!locate_decl_var_arg(let_var
, impl
->decl
->args
, impl
->decl
->func
)) {
1009 data
->error(data
, let_var
->token
, PSI_WARNING
, "Unknown variable '%s' in `let` statement"
1010 " of implementation '%s'", let_var
->name
, impl
->func
->name
);
1014 switch (let
->val
->kind
) {
1018 /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */
1019 /* e.g. let foo = *bar; */
1020 let
->var
->pointer_level
= let
->val
->data
.var
->pointer_level
;
1021 let
->var
->arg
= init_decl_arg(
1023 real_decl_type(let
->val
->data
.var
->arg
->type
)->type
,
1024 real_decl_type(let
->val
->data
.var
->arg
->type
)->name
),
1027 let
->var
->pointer_level
,
1028 let
->var
->array_size
));
1030 case PSI_LET_NUMEXP
:
1031 if (!validate_num_exp(data
, let
->val
->data
.num
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1035 case PSI_LET_CALLOC
:
1036 if (!validate_num_exp(data
, let
->val
->data
.alloc
->nmemb
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1039 if (!validate_num_exp(data
, let
->val
->data
.alloc
->size
, impl
->decl
->args
, impl
->decl
->func
, NULL
)) {
1043 case PSI_LET_CALLBACK
:
1044 if (!validate_let_callback(data
, let
->var
, let
->val
->data
.callback
, impl
)) {
1049 if (!validate_let_func(data
, let
->val
->data
.func
, impl
)) {
1056 /* check that we have a let stmt for every decl arg */
1057 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
1058 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
1061 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
1062 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
1064 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
1070 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1071 "Missing `let` statement for arg '%s %.*s%s'"
1072 " of declaration '%s' for implementation '%s'",
1073 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
1074 darg
->var
->name
, impl
->decl
->func
->var
->name
, impl
->func
->name
);
1081 static inline int validate_impl_set_stmts(struct psi_data
*data
, impl
*impl
) {
1083 /* we can have any count of set stmts; processing out vars */
1084 /* check that set stmts reference known variables */
1085 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
1086 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
1089 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
1090 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
1092 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
1099 data
->error(data
, set
->var
->token
, PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
1100 " of implementation '%s'",
1101 set
->var
->name
, impl
->func
->name
);
1105 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
1106 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
1109 if (impl
->decl
->args
) {
1110 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1111 decl_arg
*set_arg
= impl
->decl
->args
->args
[k
];
1113 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
1115 set_var
->arg
= set_arg
;
1116 if (!validate_set_value(data
, set
->val
, 1, &set_arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1124 for (k
= 0; k
< impl
->stmts
->let
.count
; ++k
) {
1125 let_stmt
*let
= impl
->stmts
->let
.list
[k
];
1127 /* check temp vars */
1128 if (let
->val
&& let
->val
->kind
== PSI_LET_TMP
) {
1129 if (!strcmp(set_var
->name
, let
->var
->name
)) {
1131 set_var
->arg
= let
->var
->arg
;
1132 if (!validate_set_value(data
, set
->val
, 1, &set_var
->arg
, 1, &impl
->decl
->func
, impl
->decl
->args
->count
, impl
->decl
->args
->args
, 0)) {
1142 data
->error(data
, set_var
->token
, PSI_WARNING
, "Unknown value '%s' of `set` statement"
1143 " for variable '$%s' of implementation '%s'",
1144 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
1151 static inline decl
*locate_free_decl(decls
*decls
, free_call
*f
) {
1155 for (i
= 0; i
< decls
->count
; ++i
) {
1156 if (!strcmp(decls
->list
[i
]->func
->var
->name
, f
->func
)) {
1157 f
->decl
= decls
->list
[i
];
1158 return decls
->list
[i
];
1165 static inline int validate_impl_free_stmts(struct psi_data
*data
, impl
*impl
) {
1167 /* we can have any count of free stmts; freeing any out vars */
1168 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
1169 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1171 for (j
= 0; j
< fre
->calls
->count
; ++j
) {
1172 free_call
*free_call
= fre
->calls
->list
[j
];
1174 /* first find the decl of the free func */
1175 if (!locate_free_decl(data
->decls
, free_call
)) {
1176 data
->error(data
, free_call
->token
, PSI_WARNING
,
1177 "Missing declaration '%s' in `free` statement"
1178 " of implementation '%s'",
1179 free_call
->func
, impl
->func
->name
);
1185 /* now check for known vars */
1186 for (l
= 0; l
< free_call
->vars
->count
; ++l
) {
1188 decl_var
*free_var
= free_call
->vars
->vars
[l
];
1190 if (!strcmp(free_var
->name
, impl
->decl
->func
->var
->name
)) {
1192 free_var
->arg
= impl
->decl
->func
;
1193 } else if (impl
->decl
->args
) {
1194 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
1195 decl_arg
*free_arg
= impl
->decl
->args
->args
[k
];
1197 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
1199 free_var
->arg
= free_arg
;
1206 data
->error(data
, free_var
->token
, PSI_WARNING
,
1207 "Unknown variable '%s' of `free` statement"
1208 " of implementation '%s'",
1209 free_var
->name
, impl
->func
->name
);
1217 static inline int validate_impl_stmts(struct psi_data
*data
, impl
*impl
) {
1219 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1220 "Missing body for implementation %s!",
1225 if (!validate_impl_ret_stmt(data
, impl
)) {
1229 if (!validate_impl_let_stmts(data
, impl
)) {
1232 if (!validate_impl_set_stmts(data
, impl
)) {
1235 if (!validate_impl_free_stmts(data
, impl
)) {
1242 static inline int validate_impl_args(struct psi_data
*data
, impl
*impl
) {
1246 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
1247 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
1252 data
->error(data
, impl
->func
->token
, PSI_WARNING
,
1253 "Non-optional argument %zu '$%s' of implementation '%s'"
1254 " follows optional argument",
1255 i
+1, iarg
->var
->name
, impl
->func
->name
);
1263 static inline int validate_impl(struct psi_data
*data
, impl
*impl
) {
1264 if (!validate_impl_args(data
, impl
)) {
1267 return validate_impl_stmts(data
, impl
);
1271 int psi_context_validate(struct psi_context
*C
, struct psi_parser
*P
)
1274 void *dlopened
= NULL
;
1275 size_t i
, count
= C
->count
++, check_round
, check_count
;
1276 decl_typedefs
*check_defs
= P
->defs
;
1277 decl_structs
*check_structs
= P
->structs
;
1278 decl_unions
*check_unions
= P
->unions
;
1279 decl_enums
*check_enums
= P
->enums
;
1280 unsigned silent
= C
->flags
& PSI_PARSER_SILENT
;
1282 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
1283 D
= psi_data_exchange(&C
->data
[count
], PSI_DATA(P
));
1285 #define REVALIDATE(what) do { \
1286 if (check_round && check_ ##what) { \
1287 free(check_ ##what->list); \
1288 free(check_ ##what); \
1290 check_ ##what = recheck_ ##what; \
1292 #define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
1293 #define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
1296 /* no warnings on first round */
1297 C
->flags
|= PSI_PARSER_SILENT
;
1299 for (check_round
= 0, check_count
= 0; CHECK_TOTAL
&& check_count
!= CHECK_TOTAL
; ++check_round
) {
1300 decl_typedefs
*recheck_defs
= NULL
;
1301 decl_structs
*recheck_structs
= NULL
;
1302 decl_unions
*recheck_unions
= NULL
;
1303 decl_enums
*recheck_enums
= NULL
;
1305 check_count
= CHECK_TOTAL
;
1307 for (i
= 0; i
< CHECK_COUNT(defs
); ++i
) {
1308 if (validate_decl_typedef(PSI_DATA(C
), check_defs
->list
[i
])) {
1309 C
->defs
= add_decl_typedef(C
->defs
, check_defs
->list
[i
]);
1311 recheck_defs
= add_decl_typedef(recheck_defs
, check_defs
->list
[i
]);
1314 for (i
= 0; i
< CHECK_COUNT(structs
); ++i
) {
1315 if (validate_decl_struct(PSI_DATA(C
), check_structs
->list
[i
])) {
1316 C
->structs
= add_decl_struct(C
->structs
, check_structs
->list
[i
]);
1318 recheck_structs
= add_decl_struct(recheck_structs
, check_structs
->list
[i
]);
1321 for (i
= 0; i
< CHECK_COUNT(unions
); ++i
) {
1322 if (validate_decl_union(PSI_DATA(C
), check_unions
->list
[i
])) {
1323 C
->unions
= add_decl_union(C
->unions
, check_unions
->list
[i
]);
1325 recheck_unions
= add_decl_union(recheck_unions
, check_unions
->list
[i
]);
1328 for (i
= 0; i
< CHECK_COUNT(enums
); ++i
) {
1329 if (validate_decl_enum(PSI_DATA(C
), check_enums
->list
[i
])) {
1330 C
->enums
= add_decl_enum(C
->enums
, check_enums
->list
[i
]);
1332 recheck_enums
= add_decl_enum(recheck_enums
, check_enums
->list
[i
]);
1337 REVALIDATE(structs
);
1341 if (check_round
== 0 && !silent
) {
1342 C
->flags
&= ~PSI_PARSER_SILENT
;
1348 for (i
= 0; i
< D
->consts
->count
; ++i
) {
1349 if (validate_constant(PSI_DATA(C
), D
->consts
->list
[i
])) {
1350 C
->consts
= add_constant(C
->consts
, D
->consts
->list
[i
]);
1355 if (!validate_lib(D
, &dlopened
)) {
1359 add_decl_lib(&C
->psi
.libs
, dlopened
);
1362 for (i
= 0; i
< D
->decls
->count
; ++i
) {
1363 if (validate_decl(PSI_DATA(C
), dlopened
, D
->decls
->list
[i
])) {
1364 C
->decls
= add_decl(C
->decls
, D
->decls
->list
[i
]);
1369 for (i
= 0; i
< D
->impls
->count
; ++i
) {
1370 if (validate_impl(PSI_DATA(C
), D
->impls
->list
[i
])) {
1371 C
->impls
= add_impl(C
->impls
, D
->impls
->list
[i
]);
1379 int psi_context_validate_data(struct psi_data
*dest
, struct psi_data
*source
)
1384 if (source
->defs
) for (i
= 0; i
< source
->defs
->count
; ++i
) {
1385 decl_arg
*def
= source
->defs
->list
[i
];
1387 if (validate_decl_typedef(source
, def
)) {
1389 dest
->defs
= add_decl_typedef(dest
->defs
, def
);
1396 if (source
->consts
) for (i
= 0; i
< source
->consts
->count
; ++i
) {
1397 constant
*constant
= source
->consts
->list
[i
];
1399 if (validate_constant(source
, constant
)) {
1401 dest
->consts
= add_constant(dest
->consts
, constant
);
1408 if (source
->structs
) for (i
= 0; i
< source
->structs
->count
; ++i
) {
1409 decl_struct
*dstruct
= source
->structs
->list
[i
];
1411 if (validate_decl_struct(source
, dstruct
)) {
1413 dest
->structs
= add_decl_struct(dest
->structs
, dstruct
);
1420 if (source
->unions
) for (i
= 0; i
< source
->unions
->count
; ++i
) {
1421 decl_union
*dunion
= source
->unions
->list
[i
];
1423 if (validate_decl_union(source
, dunion
)) {
1425 dest
->unions
= add_decl_union(dest
->unions
, dunion
);
1432 if (source
->enums
) for (i
= 0; i
< source
->enums
->count
; ++i
) {
1433 decl_enum
*denum
= source
->enums
->list
[i
];
1435 if (validate_decl_enum(source
, denum
)) {
1437 dest
->enums
= add_decl_enum(dest
->enums
, denum
);
1444 if (source
->decls
) for (i
= 0; i
< source
->decls
->count
; ++i
) {
1445 decl
*decl
= source
->decls
->list
[i
];
1447 if (validate_decl(source
, NULL
, decl
)) {
1449 dest
->decls
= add_decl(dest
->decls
, decl
);
1456 if (source
->impls
) for (i
= 0; i
< source
->impls
->count
; ++i
) {
1457 impl
*impl
= source
->impls
->list
[i
];
1459 if (validate_impl(source
, impl
)) {
1461 dest
->impls
= add_impl(dest
->impls
, impl
);