9 #include <Zend/zend_types.h>
10 #include <Zend/zend_API.h> /* fcall */
12 #include "parser_proc.h"
16 #define PSI_T_POINTER PSI_T_ASTERISK
17 #define PSI_T_LONG_DOUBLE (PSI_T_DOUBLE << 16)
21 size_t psi_t_alignment(token_t
);
22 size_t psi_t_size(token_t
);
24 typedef struct PSI_Token
{
26 unsigned size
, line
, col
;
31 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
);
33 typedef struct zend_fcall
{
35 zend_fcall_info_cache fcc
;
38 typedef union impl_val
{
53 #ifdef HAVE_LONG_DOUBLE
63 uint8_t _dbg
[sizeof(void *)];
66 typedef struct decl_type
{
70 struct decl_type
*real
;
71 struct decl_struct
*strct
;
72 struct decl_union
*unn
;
73 struct decl_enum
*enm
;
77 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
78 decl_type
*t
= calloc(1, sizeof(*t
));
80 t
->name
= strdup(name
);
84 static inline decl_type
*real_decl_type(decl_type
*type
) {
91 static inline void free_decl(struct decl
*decl
);
92 static inline void free_decl_type(decl_type
*type
) {
96 if (type
->type
== PSI_T_FUNCTION
) {
97 free_decl(type
->func
);
103 typedef struct decl_var
{
106 unsigned pointer_level
;
108 struct decl_arg
*arg
;
111 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
112 decl_var
*v
= calloc(1, sizeof(*v
));
113 v
->name
= (char *) strdup((const char *) name
);
114 v
->pointer_level
= pl
;
119 static inline decl_var
*copy_decl_var(decl_var
*src
) {
120 decl_var
*dest
= calloc(1, sizeof(*dest
));
122 memcpy(dest
, src
, sizeof(*dest
));
123 dest
->name
= strdup(dest
->name
);
125 dest
->token
= PSI_TokenCopy(dest
->token
);
130 static inline void free_decl_var(decl_var
*var
) {
138 typedef struct decl_struct_layout
{
141 } decl_struct_layout
;
143 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
144 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
145 ZEND_ASSERT(pos
+len
);
151 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
155 typedef struct decl_arg
{
159 decl_struct_layout
*layout
;
160 struct let_stmt
*let
; /* FIXME: decls must not point to impls !!! */
166 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
167 decl_arg
*arg
= calloc(1, sizeof(*arg
));
168 arg
->token
= var
->token
;
172 arg
->ptr
= &arg
->val
;
176 static inline void free_decl_arg(decl_arg
*arg
) {
177 if (arg
->token
&& arg
->token
!= arg
->var
->token
) {
180 free_decl_type(arg
->type
);
181 free_decl_var(arg
->var
);
183 free_decl_struct_layout(arg
->layout
);
188 typedef struct decl_typedefs
{
193 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_arg
*def
) {
195 defs
= calloc(1, sizeof(*defs
));
197 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
198 defs
->list
[defs
->count
-1] = def
;
202 static void free_decl_typedefs(decl_typedefs
*defs
) {
205 for (i
= 0; i
< defs
->count
; ++i
) {
206 free_decl_arg(defs
->list
[i
]);
212 typedef struct decl_vars
{
217 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
218 decl_vars
*vars
= calloc(1, sizeof(*vars
));
221 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
227 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
228 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
229 vars
->vars
[vars
->count
-1] = var
;
233 static inline void free_decl_vars(decl_vars
*vars
) {
236 for (i
= 0; i
< vars
->count
; ++i
) {
237 free_decl_var(vars
->vars
[i
]);
243 typedef struct decl_args
{
249 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
250 decl_args
*args
= calloc(1, sizeof(*args
));
253 args
->args
= calloc(1, sizeof(*args
->args
));
259 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
260 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
261 args
->args
[args
->count
-1] = arg
;
265 static inline void free_decl_args(decl_args
*args
) {
268 for (i
= 0; i
< args
->count
; ++i
) {
269 free_decl_arg(args
->args
[i
]);
275 typedef struct decl_abi
{
280 static inline decl_abi
*init_decl_abi(const char *convention
) {
281 decl_abi
*abi
= calloc(1, sizeof(*abi
));
282 abi
->convention
= strdup(convention
);
286 static inline void free_decl_abi(decl_abi
*abi
) {
290 free(abi
->convention
);
294 typedef struct decl_callinfo
{
302 typedef struct decl
{
310 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
311 decl
*d
= calloc(1, sizeof(*d
));
318 static inline void free_decl(decl
*d
) {
319 free_decl_abi(d
->abi
);
320 free_decl_arg(d
->func
);
322 free_decl_args(d
->args
);
327 typedef struct decls
{
332 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
334 decls
= calloc(1, sizeof(*decls
));
336 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
337 decls
->list
[decls
->count
-1] = decl
;
341 static inline void free_decls(decls
*decls
) {
344 for (i
= 0; i
< decls
->count
; ++i
) {
345 free_decl(decls
->list
[i
]);
352 typedef struct decl_struct
{
360 void (*dtor
)(void *type
);
364 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
365 decl_struct
*s
= calloc(1, sizeof(*s
));
366 s
->name
= strdup(name
);
371 static inline void free_decl_struct(decl_struct
*s
) {
376 free_decl_args(s
->args
);
378 if (s
->engine
.type
&& s
->engine
.dtor
) {
379 s
->engine
.dtor(s
->engine
.type
);
385 typedef struct decl_structs
{
390 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
392 ss
= calloc(1, sizeof(*ss
));
394 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
395 ss
->list
[ss
->count
-1] = s
;
399 static inline void free_decl_structs(decl_structs
*ss
) {
402 for (i
= 0; i
< ss
->count
; ++i
) {
403 free_decl_struct(ss
->list
[i
]);
409 typedef struct decl_union
{
417 static inline decl_union
*init_decl_union(const char *name
, decl_args
*args
) {
418 decl_union
*u
= calloc(1, sizeof(*u
));
419 u
->name
= strdup(name
);
424 static inline void free_decl_union(decl_union
*u
) {
429 free_decl_args(u
->args
);
435 typedef struct decl_unions
{
440 static inline decl_unions
*add_decl_union(decl_unions
*uu
, decl_union
*u
) {
442 uu
= calloc(1, sizeof(*uu
));
444 uu
->list
= realloc(uu
->list
, ++uu
->count
* sizeof(*uu
->list
));
445 uu
->list
[uu
->count
-1] = u
;
449 static inline void free_decl_unions(decl_unions
*uu
) {
452 for (i
= 0; i
< uu
->count
; ++i
) {
453 free_decl_union(uu
->list
[i
]);
459 typedef struct impl_type
{
464 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
465 impl_type
*t
= calloc(1, sizeof(*t
));
468 t
->name
= strdup(name
);
472 static inline void free_impl_type(impl_type
*type
) {
477 typedef struct impl_var
{
480 struct impl_arg
*arg
;
481 unsigned reference
:1;
484 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
485 impl_var
*var
= calloc(1, sizeof(*var
));
486 var
->name
= strdup(name
);
487 var
->reference
= is_reference
;
491 static inline impl_var
*copy_impl_var(impl_var
*var
) {
492 impl_var
*cpy
= malloc(sizeof(*cpy
));
494 memcpy(cpy
, var
, sizeof(*cpy
));
495 cpy
->name
= strdup(cpy
->name
);
497 cpy
->token
= PSI_TokenCopy(cpy
->token
);
502 static inline void free_impl_var(impl_var
*var
) {
510 typedef struct impl_def_val
{
515 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
516 impl_def_val
*def
= calloc(1, sizeof(*def
));
518 def
->text
= strdup(text
);
522 static inline void free_impl_def_val(impl_def_val
*def
) {
527 typedef struct const_type
{
532 static inline const_type
*init_const_type(token_t type
, const char *name
) {
533 const_type
*ct
= calloc(1, sizeof(*ct
));
535 ct
->name
= strdup(name
);
539 static inline void free_const_type(const_type
*type
) {
544 typedef struct constant
{
550 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
551 constant
*c
= calloc(1, sizeof(*c
));
553 c
->name
= strdup(name
);
558 static inline void free_constant(constant
*constant
) {
559 free_const_type(constant
->type
);
560 free(constant
->name
);
561 free_impl_def_val(constant
->val
);
565 typedef struct constants
{
570 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
572 constants
= calloc(1, sizeof(*constants
));
574 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
575 constants
->list
[constants
->count
-1] = constant
;
579 static inline void free_constants(constants
*c
) {
582 for (i
= 0; i
< c
->count
; ++i
) {
583 free_constant(c
->list
[i
]);
589 typedef struct impl_arg
{
597 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
598 impl_arg
*arg
= calloc(1, sizeof(*arg
));
606 static inline void free_impl_arg(impl_arg
*arg
) {
607 free_impl_type(arg
->type
);
608 free_impl_var(arg
->var
);
610 free_impl_def_val(arg
->def
);
615 typedef struct impl_vararg
{
617 struct impl_args
*args
;
623 typedef struct impl_args
{
629 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
630 impl_args
*args
= calloc(1, sizeof(*args
));
633 args
->args
= calloc(1, sizeof(*args
->args
));
639 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
640 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
641 args
->args
[args
->count
-1] = arg
;
645 static inline void free_impl_args(impl_args
*args
) {
648 for (i
= 0; i
< args
->count
; ++i
) {
649 free_impl_arg(args
->args
[i
]);
651 if (args
->vararg
.name
) {
652 free_impl_arg(args
->vararg
.name
);
658 typedef struct impl_func
{
662 impl_type
*return_type
;
663 unsigned return_reference
:1;
666 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
667 impl_func
*func
= calloc(1, sizeof(*func
));
668 func
->name
= strdup(name
);
669 func
->args
= args
? args
: init_impl_args(NULL
);
670 func
->return_type
= type
;
671 func
->return_reference
= ret_reference
;
675 static inline void free_impl_func(impl_func
*f
) {
679 free_impl_type(f
->return_type
);
680 free_impl_args(f
->args
);
685 typedef struct num_exp
{
692 struct decl_enum_item
*enm
;
695 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
696 struct num_exp
*operand
;
699 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
700 num_exp
*exp
= calloc(1, sizeof(*exp
));
701 switch (exp
->t
= t
) {
704 exp
->u
.numb
= strdup(num
);
709 EMPTY_SWITCH_DEFAULT_CASE();
714 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
716 num_exp
*num
= calloc(1, sizeof(*num
));
718 memcpy(num
, exp
, sizeof(*num
));
721 num
->token
= PSI_TokenCopy(num
->token
);
724 num
->operand
= copy_num_exp(num
->operand
);
729 num
->u
.numb
= strdup(num
->u
.numb
);
732 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
733 dvar
->arg
= num
->u
.dvar
->arg
;
734 if (num
->u
.dvar
->token
) {
735 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
743 static inline void free_num_exp(num_exp
*exp
) {
754 free_decl_var(exp
->u
.dvar
);
758 EMPTY_SWITCH_DEFAULT_CASE();
761 free_num_exp(exp
->operand
);
766 typedef struct decl_enum_item
{
771 struct decl_enum_item
*prev
;
774 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
775 decl_enum_item
*i
= calloc(1, sizeof(*i
));
777 i
->name
= strdup(name
);
782 static inline void free_decl_enum_item(decl_enum_item
*i
) {
786 if (i
->num
&& i
->num
!= &i
->inc
) {
787 free_num_exp(i
->num
);
793 typedef struct decl_enum_items
{
794 decl_enum_item
**list
;
798 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
799 decl_enum_items
*l
= calloc(1, sizeof(*l
));
803 l
->list
= calloc(1, sizeof(*l
->list
));
809 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
810 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
811 l
->list
[l
->count
] = i
;
813 i
->prev
= l
->list
[l
->count
- 1];
819 static inline void free_decl_enum_items(decl_enum_items
*l
) {
823 for (j
= 0; j
< l
->count
; ++j
) {
824 free_decl_enum_item(l
->list
[j
]);
831 typedef struct decl_enum
{
834 decl_enum_items
*items
;
837 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
838 decl_enum
*e
= calloc(1, sizeof(*e
));
840 e
->name
= strdup(name
);
845 static inline void free_decl_enum(decl_enum
*e
) {
850 free_decl_enum_items(e
->items
);
856 typedef struct decl_enums
{
861 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
863 es
= calloc(1, sizeof(*es
));
865 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
866 es
->list
[es
->count
-1] = e
;
870 static inline void free_decl_enums(decl_enums
*es
) {
874 for (j
= 0; j
< es
->count
; ++j
) {
875 free_decl_enum(es
->list
[j
]);
882 typedef struct let_calloc
{
887 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
888 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
889 alloc
->nmemb
= nmemb
;
894 static inline void free_let_calloc(let_calloc
*alloc
) {
895 free_num_exp(alloc
->nmemb
);
896 free_num_exp(alloc
->size
);
900 typedef struct let_callback
{
901 struct let_func
*func
;
902 struct set_values
*args
;
906 static inline void free_let_func(struct let_func
*func
);
907 static inline void free_set_values(struct set_values
*vals
);
908 static inline let_callback
*init_let_callback(struct let_func
*func
, struct set_values
*args
) {
909 let_callback
*cb
= calloc(1, sizeof(*cb
));
916 static inline void free_let_callback(let_callback
*cb
) {
917 free_let_func(cb
->func
);
918 free_set_values(cb
->args
);
922 typedef struct let_func
{
926 impl_val
*(*handler
)(impl_val
*tmp
, decl_type
*type
, impl_arg
*iarg
, void **to_free
);
929 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
930 let_func
*func
= calloc(1, sizeof(*func
));
932 func
->name
= strdup(name
);
937 static inline void free_let_func(let_func
*func
) {
938 free_impl_var(func
->var
);
943 #define PSI_LET_REFERENCE 0x1;
944 typedef struct let_val
{
956 let_callback
*callback
;
962 unsigned is_reference
:1;
968 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
969 let_val
*let
= calloc(1, sizeof(*let
));
970 switch (let
->kind
= kind
) {
974 let
->data
.num
= data
;
977 let
->data
.alloc
= data
;
979 case PSI_LET_CALLBACK
:
980 let
->data
.callback
= data
;
983 let
->data
.func
= data
;
986 let
->data
.var
= data
;
988 EMPTY_SWITCH_DEFAULT_CASE();
993 static inline void free_let_val(let_val
*let
) {
998 free_num_exp(let
->data
.num
);
1000 case PSI_LET_CALLOC
:
1001 free_let_calloc(let
->data
.alloc
);
1003 case PSI_LET_CALLBACK
:
1004 free_let_callback(let
->data
.callback
);
1007 free_let_func(let
->data
.func
);
1010 free_decl_var(let
->data
.var
);
1012 EMPTY_SWITCH_DEFAULT_CASE();
1017 typedef struct let_stmt
{
1024 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
1025 let_stmt
*let
= calloc(1, sizeof(*let
));
1031 static inline void free_let_stmt(let_stmt
*stmt
) {
1033 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
1034 free_decl_arg(stmt
->var
->arg
);
1036 free_let_val(stmt
->val
);
1038 free_decl_var(stmt
->var
);
1044 typedef struct set_func
{
1048 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
1051 static inline set_func
*init_set_func(token_t type
, const char *name
) {
1052 set_func
*func
= calloc(1, sizeof(*func
));
1054 func
->name
= strdup(name
);
1058 static inline void free_set_func(set_func
*func
) {
1066 typedef struct set_value
{
1071 struct set_value
*set
;
1074 struct set_values
*inner
;
1077 typedef struct set_values
{
1083 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
1084 set_value
*val
= calloc(1, sizeof(*val
));
1090 static inline set_values
*add_set_value(set_values
*vals
, set_value
*val
);
1091 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
1092 val
->inner
= add_set_value(val
->inner
, inner
);
1093 inner
->outer
.set
= val
;
1097 static inline void free_set_value(set_value
*val
) {
1099 free_set_func(val
->func
);
1102 free_decl_vars(val
->vars
);
1104 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1105 free_set_values(val
->inner
);
1108 free_num_exp(val
->num
);
1113 static inline set_values
*init_set_values(set_value
*val
) {
1114 set_values
*vals
= calloc(1, sizeof(*vals
));
1117 vals
->vals
= calloc(1, sizeof(val
));
1118 vals
->vals
[0] = val
;
1123 static inline set_values
*add_set_value(set_values
*vals
, set_value
*val
) {
1125 vals
= calloc(1, sizeof(*vals
));
1127 vals
->vals
= realloc(vals
->vals
, ++vals
->count
* sizeof(val
));
1128 vals
->vals
[vals
->count
-1] = val
;
1132 static inline void free_set_values(set_values
*vals
) {
1136 for (i
= 0; i
< vals
->count
; ++i
) {
1137 free_set_value(vals
->vals
[i
]);
1144 typedef struct set_stmt
{
1150 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1151 set_stmt
*set
= calloc(1, sizeof(*set
));
1157 static inline void free_set_stmt(set_stmt
*set
) {
1158 free_impl_var(set
->var
);
1159 free_set_value(set
->val
);
1163 typedef struct return_stmt
{
1169 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1170 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1175 static inline void free_return_stmt(return_stmt
*ret
) {
1179 free_set_value(ret
->set
);
1183 typedef struct free_call
{
1190 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1191 free_call
*f
= calloc(1, sizeof(*f
));
1192 f
->func
= strdup(func
);
1197 static inline void free_free_call(free_call
*f
) {
1202 free_decl_vars(f
->vars
);
1206 typedef struct free_calls
{
1211 static inline free_calls
*init_free_calls(free_call
*f
) {
1212 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1215 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1221 static inline void free_free_calls(free_calls
*fcs
) {
1224 for (i
= 0; i
< fcs
->count
; ++i
) {
1225 free_free_call(fcs
->list
[i
]);
1231 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1232 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1233 fcs
->list
[fcs
->count
-1] = f
;
1237 typedef struct free_stmt
{
1241 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1242 free_stmt
*f
= calloc(1, sizeof(*f
));
1247 static inline void free_free_stmt(free_stmt
*f
) {
1248 free_free_calls(f
->calls
);
1252 typedef struct impl_stmt
{
1263 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1264 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1270 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1271 switch (stmt
->type
) {
1273 free_let_stmt(stmt
->s
.let
);
1276 free_set_stmt(stmt
->s
.set
);
1279 free_return_stmt(stmt
->s
.ret
);
1282 free_free_stmt(stmt
->s
.fre
);
1288 typedef struct impl_stmts
{
1307 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1308 list
= realloc(list
, count
* sizeof(list
));
1309 ((void **)list
)[count
-1] = stmt
;
1313 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1314 switch (stmt
->type
) {
1316 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1319 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1322 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1325 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1332 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1333 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1334 return add_impl_stmt(stmts
, stmt
);
1337 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1340 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1341 free_let_stmt(stmts
->let
.list
[i
]);
1343 free(stmts
->let
.list
);
1344 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1345 free_return_stmt(stmts
->ret
.list
[i
]);
1347 free(stmts
->ret
.list
);
1348 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1349 free_set_stmt(stmts
->set
.list
[i
]);
1351 free(stmts
->set
.list
);
1352 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1353 free_free_stmt(stmts
->fre
.list
[i
]);
1355 free(stmts
->fre
.list
);
1359 typedef struct impl
{
1365 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1366 impl
*i
= calloc(1, sizeof(*i
));
1372 static inline void free_impl(impl
*impl
) {
1373 free_impl_func(impl
->func
);
1374 free_impl_stmts(impl
->stmts
);
1378 typedef struct impls
{
1383 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1385 impls
= calloc(1, sizeof(*impls
));
1387 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1388 impls
->list
[impls
->count
-1] = impl
;
1392 static void free_impls(impls
*impls
) {
1395 for (i
= 0; i
< impls
->count
; ++i
) {
1396 free_impl(impls
->list
[i
]);
1402 typedef struct decl_file
{
1407 static inline void free_decl_file(decl_file
*file
) {
1414 memset(file
, 0, sizeof(*file
));
1417 typedef struct decl_libs
{
1422 static inline void free_decl_libs(decl_libs
*libs
) {
1425 for (i
= 0; i
< libs
->count
; ++i
) {
1427 dlclose(libs
->dl
[i
]);
1432 memset(libs
, 0, sizeof(*libs
));
1435 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1436 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1437 libs
->dl
[libs
->count
-1] = dlopened
;
1440 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1443 ZEND_ASSERT(var
->arg
->var
!= var
);
1445 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1446 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1447 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1449 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1451 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1453 ret_val
= *(void **) ret_val
;
1458 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1459 impl_val
*val
, *val_ptr
;
1462 ZEND_ASSERT(var
->arg
->var
== var
);
1464 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1465 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1466 var
->array_size
, var
->arg
->var
->array_size
);
1468 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1472 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1473 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1475 fprintf(stderr
, "++\n");
1477 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1478 val_ptr
= val_ptr
->ptr
;
1484 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1485 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1487 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1493 #define PSI_ERROR 16
1494 #define PSI_WARNING 32
1495 typedef void (*psi_error_cb
)(void *context
, PSI_Token
*token
, int type
, const char *msg
, ...);
1497 #define PSI_DATA(D) ((PSI_Data *) (D))
1498 #define PSI_DATA_MEMBERS \
1499 constants *consts; \
1500 decl_typedefs *defs; \
1501 decl_structs *structs; \
1502 decl_unions *unions; \
1503 decl_enums *enums; \
1510 psi_error_cb error; \
1513 typedef struct PSI_Data
{
1517 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1519 dest
= malloc(sizeof(*dest
));
1521 memcpy(dest
, src
, sizeof(*dest
));
1522 memset(src
, 0, sizeof(*src
));
1526 static inline void PSI_DataDtor(PSI_Data
*data
) {
1528 free_constants(data
->consts
);
1531 free_decl_typedefs(data
->defs
);
1533 if (data
->structs
) {
1534 free_decl_structs(data
->structs
);
1537 free_decl_unions(data
->unions
);
1540 free_decl_enums(data
->enums
);
1543 free_decls(data
->decls
);
1546 free_impls(data
->impls
);
1548 free_decl_file(&data
->psi
.file
);
1551 typedef struct PSI_Parser
{
1557 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1560 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1561 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1564 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1566 size_t token_len
, fname_len
;
1569 if (P
->cur
< P
->tok
) {
1574 token_len
= P
->cur
- P
->tok
;
1575 fname_len
= strlen(P
->psi
.file
.fn
);
1577 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1578 T
->type
= token_typ
;
1579 T
->size
= token_len
;
1580 T
->text
= &T
->buf
[0];
1581 T
->file
= &T
->buf
[token_len
+ 1];
1585 memcpy(T
->text
, P
->tok
, token_len
);
1586 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1591 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1592 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1593 PSI_Token
*ptr
= malloc(strct_len
);
1595 memcpy(ptr
, src
, strct_len
);
1597 ptr
->text
= &ptr
->buf
[0];
1598 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1603 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1606 PSI_Token
*T
= NULL
;
1608 va_start(argv
, argc
);
1609 for (i
= 0; i
< argc
; ++i
) {
1610 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1613 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1615 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1616 T
->text
= &T
->buf
[0];
1617 T
->file
= &T
->buf
[T
->size
+ 1];
1618 T
->buf
[token_len
] = ' ';
1619 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1620 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1622 T
= PSI_TokenCopy(arg
);
1623 T
->type
= PSI_T_NAME
;
1631 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1635 va_start(argv
, argc
);
1636 for (i
= 0; i
< argc
; ++i
) {
1637 char *str
= va_arg(argv
, char *);
1638 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1640 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1641 T
->text
= &T
->buf
[0];
1642 T
->file
= &T
->buf
[T
->size
+ 1];
1643 T
->buf
[token_len
] = ' ';
1644 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1645 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1652 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
1653 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1654 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1658 static inline uint64_t psi_hash(char *digest_buf
, ...)
1660 uint64_t hash
= 5381;
1665 va_start(argv
, digest_buf
);
1666 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1667 while ((c
= *ptr
++)) {
1668 hash
= ((hash
<< 5) + hash
) + c
;
1674 sprintf(digest_buf
, "%" PRIx64
, hash
);
1680 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1683 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1684 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1687 #define PSI_PARSER_DEBUG 0x1
1688 #define PSI_PARSER_SILENT 0x2
1690 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1691 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1692 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1693 token_t
PSI_ParserScan(PSI_Parser
*P
);
1694 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1695 void PSI_ParserDtor(PSI_Parser
*P
);
1696 void PSI_ParserFree(PSI_Parser
**P
);