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
65 typedef struct decl_type
{
71 struct decl_struct
*strct
;
72 struct decl_union
*unn
;
73 struct decl_enum
*enm
;
78 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
79 decl_type
*t
= calloc(1, sizeof(*t
));
81 t
->name
= strdup(name
);
85 static inline int weak_decl_type(decl_type
*type
) {
98 static inline void free_decl(struct decl
*decl
);
99 static inline void free_decl_type(decl_type
*type
) {
103 if (type
->type
== PSI_T_FUNCTION
) {
104 free_decl(type
->real
.func
);
110 typedef struct decl_var
{
113 unsigned pointer_level
;
115 struct decl_arg
*arg
;
118 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
119 decl_var
*v
= calloc(1, sizeof(*v
));
120 v
->name
= (char *) strdup((const char *) name
);
121 v
->pointer_level
= pl
;
126 static inline decl_var
*copy_decl_var(decl_var
*src
) {
127 decl_var
*dest
= calloc(1, sizeof(*dest
));
129 memcpy(dest
, src
, sizeof(*dest
));
130 dest
->name
= strdup(dest
->name
);
132 dest
->token
= PSI_TokenCopy(dest
->token
);
137 static inline void free_decl_var(decl_var
*var
) {
145 typedef struct decl_struct_layout
{
148 } decl_struct_layout
;
150 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
151 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
152 ZEND_ASSERT(pos
+len
);
158 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
162 typedef struct decl_arg
{
166 decl_struct_layout
*layout
;
173 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
174 decl_arg
*arg
= calloc(1, sizeof(*arg
));
175 arg
->token
= var
->token
;
179 arg
->ptr
= &arg
->val
;
184 static inline void free_decl_arg(decl_arg
*arg
) {
185 if (arg
->token
&& arg
->token
!= arg
->var
->token
) {
188 free_decl_type(arg
->type
);
189 free_decl_var(arg
->var
);
191 free_decl_struct_layout(arg
->layout
);
196 static inline decl_type
*real_decl_type(decl_type
*type
) {
197 while (weak_decl_type(type
)) {
198 type
= type
->real
.def
->type
;
203 typedef struct decl_typedefs
{
208 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_arg
*def
) {
210 defs
= calloc(1, sizeof(*defs
));
212 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
213 defs
->list
[defs
->count
-1] = def
;
217 static void free_decl_typedefs(decl_typedefs
*defs
) {
220 for (i
= 0; i
< defs
->count
; ++i
) {
221 free_decl_arg(defs
->list
[i
]);
227 typedef struct decl_vars
{
232 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
233 decl_vars
*vars
= calloc(1, sizeof(*vars
));
236 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
242 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
243 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
244 vars
->vars
[vars
->count
-1] = var
;
248 static inline void free_decl_vars(decl_vars
*vars
) {
251 for (i
= 0; i
< vars
->count
; ++i
) {
252 free_decl_var(vars
->vars
[i
]);
258 typedef struct decl_args
{
264 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
265 decl_args
*args
= calloc(1, sizeof(*args
));
268 args
->args
= calloc(1, sizeof(*args
->args
));
274 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
275 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
276 args
->args
[args
->count
-1] = arg
;
280 static inline void free_decl_args(decl_args
*args
) {
283 for (i
= 0; i
< args
->count
; ++i
) {
284 free_decl_arg(args
->args
[i
]);
290 typedef struct decl_abi
{
295 static inline decl_abi
*init_decl_abi(const char *convention
) {
296 decl_abi
*abi
= calloc(1, sizeof(*abi
));
297 abi
->convention
= strdup(convention
);
301 static inline void free_decl_abi(decl_abi
*abi
) {
305 free(abi
->convention
);
309 typedef struct decl_callinfo
{
317 typedef struct decl
{
325 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
326 decl
*d
= calloc(1, sizeof(*d
));
333 static inline void free_decl(decl
*d
) {
334 free_decl_abi(d
->abi
);
335 free_decl_arg(d
->func
);
337 free_decl_args(d
->args
);
342 typedef struct decls
{
347 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
349 decls
= calloc(1, sizeof(*decls
));
351 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
352 decls
->list
[decls
->count
-1] = decl
;
356 static inline void free_decls(decls
*decls
) {
359 for (i
= 0; i
< decls
->count
; ++i
) {
360 free_decl(decls
->list
[i
]);
367 typedef struct decl_struct
{
375 void (*dtor
)(void *type
);
379 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
380 decl_struct
*s
= calloc(1, sizeof(*s
));
381 s
->name
= strdup(name
);
386 static inline void free_decl_struct(decl_struct
*s
) {
391 free_decl_args(s
->args
);
393 if (s
->engine
.type
&& s
->engine
.dtor
) {
394 s
->engine
.dtor(s
->engine
.type
);
400 typedef struct decl_structs
{
405 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
407 ss
= calloc(1, sizeof(*ss
));
409 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
410 ss
->list
[ss
->count
-1] = s
;
414 static inline void free_decl_structs(decl_structs
*ss
) {
417 for (i
= 0; i
< ss
->count
; ++i
) {
418 free_decl_struct(ss
->list
[i
]);
424 typedef struct decl_union
{
432 static inline decl_union
*init_decl_union(const char *name
, decl_args
*args
) {
433 decl_union
*u
= calloc(1, sizeof(*u
));
434 u
->name
= strdup(name
);
439 static inline void free_decl_union(decl_union
*u
) {
444 free_decl_args(u
->args
);
450 typedef struct decl_unions
{
455 static inline decl_unions
*add_decl_union(decl_unions
*uu
, decl_union
*u
) {
457 uu
= calloc(1, sizeof(*uu
));
459 uu
->list
= realloc(uu
->list
, ++uu
->count
* sizeof(*uu
->list
));
460 uu
->list
[uu
->count
-1] = u
;
464 static inline void free_decl_unions(decl_unions
*uu
) {
467 for (i
= 0; i
< uu
->count
; ++i
) {
468 free_decl_union(uu
->list
[i
]);
474 typedef struct impl_type
{
479 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
480 impl_type
*t
= calloc(1, sizeof(*t
));
483 t
->name
= strdup(name
);
487 static inline void free_impl_type(impl_type
*type
) {
492 typedef struct impl_var
{
495 struct impl_arg
*arg
;
496 unsigned reference
:1;
499 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
500 impl_var
*var
= calloc(1, sizeof(*var
));
501 var
->name
= strdup(name
);
502 var
->reference
= is_reference
;
506 static inline impl_var
*copy_impl_var(impl_var
*var
) {
507 impl_var
*cpy
= malloc(sizeof(*cpy
));
509 memcpy(cpy
, var
, sizeof(*cpy
));
510 cpy
->name
= strdup(cpy
->name
);
512 cpy
->token
= PSI_TokenCopy(cpy
->token
);
517 static inline void free_impl_var(impl_var
*var
) {
525 typedef struct impl_def_val
{
530 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
531 impl_def_val
*def
= calloc(1, sizeof(*def
));
533 def
->text
= strdup(text
);
537 static inline void free_impl_def_val(impl_def_val
*def
) {
542 typedef struct const_type
{
547 static inline const_type
*init_const_type(token_t type
, const char *name
) {
548 const_type
*ct
= calloc(1, sizeof(*ct
));
550 ct
->name
= strdup(name
);
554 static inline void free_const_type(const_type
*type
) {
559 typedef struct constant
{
565 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
566 constant
*c
= calloc(1, sizeof(*c
));
568 c
->name
= strdup(name
);
573 static inline void free_constant(constant
*constant
) {
574 free_const_type(constant
->type
);
575 free(constant
->name
);
576 free_impl_def_val(constant
->val
);
580 typedef struct constants
{
585 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
587 constants
= calloc(1, sizeof(*constants
));
589 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
590 constants
->list
[constants
->count
-1] = constant
;
594 static inline void free_constants(constants
*c
) {
597 for (i
= 0; i
< c
->count
; ++i
) {
598 free_constant(c
->list
[i
]);
604 typedef struct impl_arg
{
612 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
613 impl_arg
*arg
= calloc(1, sizeof(*arg
));
621 static inline void free_impl_arg(impl_arg
*arg
) {
622 free_impl_type(arg
->type
);
623 free_impl_var(arg
->var
);
625 free_impl_def_val(arg
->def
);
630 typedef struct impl_vararg
{
632 struct impl_args
*args
;
638 typedef struct impl_args
{
644 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
645 impl_args
*args
= calloc(1, sizeof(*args
));
648 args
->args
= calloc(1, sizeof(*args
->args
));
654 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
655 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
656 args
->args
[args
->count
-1] = arg
;
660 static inline void free_impl_args(impl_args
*args
) {
663 for (i
= 0; i
< args
->count
; ++i
) {
664 free_impl_arg(args
->args
[i
]);
666 if (args
->vararg
.name
) {
667 free_impl_arg(args
->vararg
.name
);
673 typedef struct impl_func
{
677 impl_type
*return_type
;
678 unsigned return_reference
:1;
681 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
682 impl_func
*func
= calloc(1, sizeof(*func
));
683 func
->name
= strdup(name
);
684 func
->args
= args
? args
: init_impl_args(NULL
);
685 func
->return_type
= type
;
686 func
->return_reference
= ret_reference
;
690 static inline void free_impl_func(impl_func
*f
) {
694 free_impl_type(f
->return_type
);
695 free_impl_args(f
->args
);
700 typedef struct num_exp
{
707 struct decl_enum_item
*enm
;
710 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
711 struct num_exp
*operand
;
714 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
715 num_exp
*exp
= calloc(1, sizeof(*exp
));
716 switch (exp
->t
= t
) {
719 exp
->u
.numb
= strdup(num
);
724 EMPTY_SWITCH_DEFAULT_CASE();
729 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
731 num_exp
*num
= calloc(1, sizeof(*num
));
733 memcpy(num
, exp
, sizeof(*num
));
736 num
->token
= PSI_TokenCopy(num
->token
);
739 num
->operand
= copy_num_exp(num
->operand
);
744 num
->u
.numb
= strdup(num
->u
.numb
);
747 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
748 dvar
->arg
= num
->u
.dvar
->arg
;
749 if (num
->u
.dvar
->token
) {
750 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
758 static inline void free_num_exp(num_exp
*exp
) {
769 free_decl_var(exp
->u
.dvar
);
773 EMPTY_SWITCH_DEFAULT_CASE();
776 free_num_exp(exp
->operand
);
781 typedef struct decl_enum_item
{
786 struct decl_enum_item
*prev
;
789 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
790 decl_enum_item
*i
= calloc(1, sizeof(*i
));
792 i
->name
= strdup(name
);
797 static inline void free_decl_enum_item(decl_enum_item
*i
) {
801 if (i
->num
&& i
->num
!= &i
->inc
) {
802 free_num_exp(i
->num
);
808 typedef struct decl_enum_items
{
809 decl_enum_item
**list
;
813 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
814 decl_enum_items
*l
= calloc(1, sizeof(*l
));
818 l
->list
= calloc(1, sizeof(*l
->list
));
824 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
825 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
826 l
->list
[l
->count
] = i
;
828 i
->prev
= l
->list
[l
->count
- 1];
834 static inline void free_decl_enum_items(decl_enum_items
*l
) {
838 for (j
= 0; j
< l
->count
; ++j
) {
839 free_decl_enum_item(l
->list
[j
]);
846 typedef struct decl_enum
{
849 decl_enum_items
*items
;
852 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
853 decl_enum
*e
= calloc(1, sizeof(*e
));
855 e
->name
= strdup(name
);
860 static inline void free_decl_enum(decl_enum
*e
) {
865 free_decl_enum_items(e
->items
);
871 typedef struct decl_enums
{
876 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
878 es
= calloc(1, sizeof(*es
));
880 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
881 es
->list
[es
->count
-1] = e
;
885 static inline void free_decl_enums(decl_enums
*es
) {
889 for (j
= 0; j
< es
->count
; ++j
) {
890 free_decl_enum(es
->list
[j
]);
897 typedef struct let_calloc
{
902 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
903 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
904 alloc
->nmemb
= nmemb
;
909 static inline void free_let_calloc(let_calloc
*alloc
) {
910 free_num_exp(alloc
->nmemb
);
911 free_num_exp(alloc
->size
);
915 typedef struct let_callback
{
916 struct let_func
*func
;
917 struct set_values
*args
;
921 static inline void free_let_func(struct let_func
*func
);
922 static inline void free_set_values(struct set_values
*vals
);
923 static inline let_callback
*init_let_callback(struct let_func
*func
, struct set_values
*args
) {
924 let_callback
*cb
= calloc(1, sizeof(*cb
));
931 static inline void free_let_callback(let_callback
*cb
) {
932 free_let_func(cb
->func
);
933 free_set_values(cb
->args
);
937 typedef impl_val
*(*let_func_handler
)(impl_val
*tmp
, decl_type
*type
, impl_arg
*iarg
, void **to_free
);
939 typedef struct let_func
{
943 let_func_handler handler
;
946 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
947 let_func
*func
= calloc(1, sizeof(*func
));
949 func
->name
= strdup(name
);
954 static inline void free_let_func(let_func
*func
) {
955 free_impl_var(func
->var
);
960 #define PSI_LET_REFERENCE 0x1;
961 typedef struct let_val
{
973 let_callback
*callback
;
979 unsigned is_reference
:1;
985 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
986 let_val
*let
= calloc(1, sizeof(*let
));
987 switch (let
->kind
= kind
) {
991 let
->data
.num
= data
;
994 let
->data
.alloc
= data
;
996 case PSI_LET_CALLBACK
:
997 let
->data
.callback
= data
;
1000 let
->data
.func
= data
;
1003 let
->data
.var
= data
;
1005 EMPTY_SWITCH_DEFAULT_CASE();
1010 static inline void free_let_val(let_val
*let
) {
1011 switch (let
->kind
) {
1014 case PSI_LET_NUMEXP
:
1015 free_num_exp(let
->data
.num
);
1017 case PSI_LET_CALLOC
:
1018 free_let_calloc(let
->data
.alloc
);
1020 case PSI_LET_CALLBACK
:
1021 free_let_callback(let
->data
.callback
);
1024 free_let_func(let
->data
.func
);
1027 free_decl_var(let
->data
.var
);
1029 EMPTY_SWITCH_DEFAULT_CASE();
1034 typedef struct let_stmt
{
1039 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
1040 let_stmt
*let
= calloc(1, sizeof(*let
));
1046 static inline void free_let_stmt(let_stmt
*stmt
) {
1048 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
1049 free_decl_arg(stmt
->var
->arg
);
1051 free_let_val(stmt
->val
);
1053 free_decl_var(stmt
->var
);
1059 typedef struct set_func
{
1063 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
1066 static inline set_func
*init_set_func(token_t type
, const char *name
) {
1067 set_func
*func
= calloc(1, sizeof(*func
));
1069 func
->name
= strdup(name
);
1073 static inline void free_set_func(set_func
*func
) {
1081 typedef struct set_value
{
1086 struct set_value
*set
;
1089 struct set_values
*inner
;
1092 typedef struct set_values
{
1098 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
1099 set_value
*val
= calloc(1, sizeof(*val
));
1105 static inline set_values
*add_set_value(set_values
*vals
, set_value
*val
);
1106 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
1107 val
->inner
= add_set_value(val
->inner
, inner
);
1108 inner
->outer
.set
= val
;
1112 static inline void free_set_value(set_value
*val
) {
1114 free_set_func(val
->func
);
1117 free_decl_vars(val
->vars
);
1119 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1120 free_set_values(val
->inner
);
1123 free_num_exp(val
->num
);
1128 static inline set_values
*init_set_values(set_value
*val
) {
1129 set_values
*vals
= calloc(1, sizeof(*vals
));
1132 vals
->vals
= calloc(1, sizeof(val
));
1133 vals
->vals
[0] = val
;
1138 static inline set_values
*add_set_value(set_values
*vals
, set_value
*val
) {
1140 vals
= calloc(1, sizeof(*vals
));
1142 vals
->vals
= realloc(vals
->vals
, ++vals
->count
* sizeof(val
));
1143 vals
->vals
[vals
->count
-1] = val
;
1147 static inline void free_set_values(set_values
*vals
) {
1151 for (i
= 0; i
< vals
->count
; ++i
) {
1152 free_set_value(vals
->vals
[i
]);
1159 typedef struct set_stmt
{
1165 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1166 set_stmt
*set
= calloc(1, sizeof(*set
));
1172 static inline void free_set_stmt(set_stmt
*set
) {
1173 free_impl_var(set
->var
);
1174 free_set_value(set
->val
);
1178 typedef struct return_stmt
{
1184 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1185 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1190 static inline void free_return_stmt(return_stmt
*ret
) {
1194 free_set_value(ret
->set
);
1198 typedef struct free_call
{
1205 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1206 free_call
*f
= calloc(1, sizeof(*f
));
1207 f
->func
= strdup(func
);
1212 static inline void free_free_call(free_call
*f
) {
1217 free_decl_vars(f
->vars
);
1221 typedef struct free_calls
{
1226 static inline free_calls
*init_free_calls(free_call
*f
) {
1227 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1230 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1236 static inline void free_free_calls(free_calls
*fcs
) {
1239 for (i
= 0; i
< fcs
->count
; ++i
) {
1240 free_free_call(fcs
->list
[i
]);
1246 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1247 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1248 fcs
->list
[fcs
->count
-1] = f
;
1252 typedef struct free_stmt
{
1256 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1257 free_stmt
*f
= calloc(1, sizeof(*f
));
1262 static inline void free_free_stmt(free_stmt
*f
) {
1263 free_free_calls(f
->calls
);
1267 typedef struct impl_stmt
{
1278 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1279 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1285 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1286 switch (stmt
->type
) {
1288 free_let_stmt(stmt
->s
.let
);
1291 free_set_stmt(stmt
->s
.set
);
1294 free_return_stmt(stmt
->s
.ret
);
1297 free_free_stmt(stmt
->s
.fre
);
1303 typedef struct impl_stmts
{
1322 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1323 list
= realloc(list
, count
* sizeof(list
));
1324 ((void **)list
)[count
-1] = stmt
;
1328 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1329 switch (stmt
->type
) {
1331 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1334 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1337 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1340 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1347 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1348 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1349 return add_impl_stmt(stmts
, stmt
);
1352 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1355 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1356 free_let_stmt(stmts
->let
.list
[i
]);
1358 free(stmts
->let
.list
);
1359 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1360 free_return_stmt(stmts
->ret
.list
[i
]);
1362 free(stmts
->ret
.list
);
1363 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1364 free_set_stmt(stmts
->set
.list
[i
]);
1366 free(stmts
->set
.list
);
1367 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1368 free_free_stmt(stmts
->fre
.list
[i
]);
1370 free(stmts
->fre
.list
);
1374 typedef struct impl
{
1380 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1381 impl
*i
= calloc(1, sizeof(*i
));
1387 static inline void free_impl(impl
*impl
) {
1388 free_impl_func(impl
->func
);
1389 free_impl_stmts(impl
->stmts
);
1393 typedef struct impls
{
1398 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1400 impls
= calloc(1, sizeof(*impls
));
1402 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1403 impls
->list
[impls
->count
-1] = impl
;
1407 static void free_impls(impls
*impls
) {
1410 for (i
= 0; i
< impls
->count
; ++i
) {
1411 free_impl(impls
->list
[i
]);
1417 typedef struct decl_file
{
1422 static inline void free_decl_file(decl_file
*file
) {
1429 memset(file
, 0, sizeof(*file
));
1432 typedef struct decl_libs
{
1437 static inline void free_decl_libs(decl_libs
*libs
) {
1440 for (i
= 0; i
< libs
->count
; ++i
) {
1442 dlclose(libs
->dl
[i
]);
1447 memset(libs
, 0, sizeof(*libs
));
1450 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1451 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1452 libs
->dl
[libs
->count
-1] = dlopened
;
1455 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1458 ZEND_ASSERT(var
->arg
->var
!= var
);
1460 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1461 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1462 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1464 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1466 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1468 ret_val
= *(void **) ret_val
;
1473 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1474 impl_val
*val
, *val_ptr
;
1477 ZEND_ASSERT(var
->arg
->var
== var
);
1479 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1480 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1481 var
->array_size
, var
->arg
->var
->array_size
);
1483 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1487 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1488 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1490 fprintf(stderr
, "++\n");
1492 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1493 val_ptr
= val_ptr
->ptr
;
1499 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1500 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1502 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1508 #define PSI_ERROR 16
1509 #define PSI_WARNING 32
1510 typedef void (*psi_error_cb
)(void *context
, PSI_Token
*token
, int type
, const char *msg
, ...);
1512 #define PSI_DATA(D) ((PSI_Data *) (D))
1513 #define PSI_DATA_MEMBERS \
1514 constants *consts; \
1515 decl_typedefs *defs; \
1516 decl_structs *structs; \
1517 decl_unions *unions; \
1518 decl_enums *enums; \
1525 psi_error_cb error; \
1528 typedef struct PSI_Data
{
1532 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1534 dest
= malloc(sizeof(*dest
));
1536 memcpy(dest
, src
, sizeof(*dest
));
1537 memset(src
, 0, sizeof(*src
));
1541 static inline void PSI_DataDtor(PSI_Data
*data
) {
1543 free_constants(data
->consts
);
1546 free_decl_typedefs(data
->defs
);
1548 if (data
->structs
) {
1549 free_decl_structs(data
->structs
);
1552 free_decl_unions(data
->unions
);
1555 free_decl_enums(data
->enums
);
1558 free_decls(data
->decls
);
1561 free_impls(data
->impls
);
1563 free_decl_file(&data
->psi
.file
);
1566 typedef struct PSI_Parser
{
1572 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1575 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1576 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1579 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1581 size_t token_len
, fname_len
;
1584 if (P
->cur
< P
->tok
) {
1589 token_len
= P
->cur
- P
->tok
;
1590 fname_len
= strlen(P
->psi
.file
.fn
);
1592 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1593 T
->type
= token_typ
;
1594 T
->size
= token_len
;
1595 T
->text
= &T
->buf
[0];
1596 T
->file
= &T
->buf
[token_len
+ 1];
1600 memcpy(T
->text
, P
->tok
, token_len
);
1601 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1606 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1607 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1608 PSI_Token
*ptr
= malloc(strct_len
);
1610 memcpy(ptr
, src
, strct_len
);
1612 ptr
->text
= &ptr
->buf
[0];
1613 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1618 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1621 PSI_Token
*T
= NULL
;
1623 va_start(argv
, argc
);
1624 for (i
= 0; i
< argc
; ++i
) {
1625 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1628 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1630 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1631 T
->text
= &T
->buf
[0];
1632 T
->file
= &T
->buf
[T
->size
+ 1];
1633 T
->buf
[token_len
] = ' ';
1634 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1635 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1637 T
= PSI_TokenCopy(arg
);
1638 T
->type
= PSI_T_NAME
;
1646 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1650 va_start(argv
, argc
);
1651 for (i
= 0; i
< argc
; ++i
) {
1652 char *str
= va_arg(argv
, char *);
1653 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1655 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1656 T
->text
= &T
->buf
[0];
1657 T
->file
= &T
->buf
[T
->size
+ 1];
1658 T
->buf
[token_len
] = ' ';
1659 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1660 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1667 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
1668 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1669 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1673 static inline uint64_t psi_hash(char *digest_buf
, ...)
1675 uint64_t hash
= 5381;
1680 va_start(argv
, digest_buf
);
1681 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1682 while ((c
= *ptr
++)) {
1683 hash
= ((hash
<< 5) + hash
) + c
;
1689 sprintf(digest_buf
, "%" PRIx64
, hash
);
1695 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1698 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1699 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1702 #define PSI_PARSER_DEBUG 0x1
1703 #define PSI_PARSER_SILENT 0x2
1705 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1706 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1707 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1708 token_t
PSI_ParserScan(PSI_Parser
*P
);
1709 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1710 void PSI_ParserDtor(PSI_Parser
*P
);
1711 void PSI_ParserFree(PSI_Parser
**P
);