0f5671d38ebf7782f3b42ae68120df801182487a
9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
15 #define PSI_T_POINTER PSI_T_ASTERISK
16 #define PSI_T_LONG_DOUBLE (PSI_T_DOUBLE << 16)
20 size_t psi_t_alignment(token_t
);
21 size_t psi_t_size(token_t
);
23 typedef struct PSI_Token
{
25 unsigned size
, line
, col
;
30 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
);
32 typedef union impl_val
{
47 #ifdef HAVE_LONG_DOUBLE
56 uint8_t _dbg
[sizeof(void *)];
59 typedef struct decl_type
{
63 struct decl_type
*real
;
64 struct decl_struct
*strct
;
65 struct decl_enum
*enm
;
69 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
70 decl_type
*t
= calloc(1, sizeof(*t
));
72 t
->name
= strdup(name
);
76 static inline decl_type
*real_decl_type(decl_type
*type
) {
83 static inline void free_decl(struct decl
*decl
);
84 static inline void free_decl_type(decl_type
*type
) {
88 if (type
->type
== PSI_T_FUNCTION
) {
89 free_decl(type
->func
);
95 typedef struct decl_var
{
98 unsigned pointer_level
;
100 struct decl_arg
*arg
;
103 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
104 decl_var
*v
= calloc(1, sizeof(*v
));
105 v
->name
= (char *) strdup((const char *) name
);
106 v
->pointer_level
= pl
;
111 static inline decl_var
*copy_decl_var(decl_var
*src
) {
112 decl_var
*dest
= calloc(1, sizeof(*dest
));
114 memcpy(dest
, src
, sizeof(*dest
));
115 dest
->name
= strdup(dest
->name
);
117 dest
->token
= PSI_TokenCopy(dest
->token
);
122 static inline void free_decl_var(decl_var
*var
) {
130 typedef struct decl_struct_layout
{
133 } decl_struct_layout
;
135 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
136 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
137 ZEND_ASSERT(pos
+len
);
143 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
147 typedef struct decl_arg
{
151 decl_struct_layout
*layout
;
152 struct let_stmt
*let
;
158 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
159 decl_arg
*arg
= calloc(1, sizeof(*arg
));
160 arg
->token
= var
->token
;
164 arg
->ptr
= &arg
->val
;
168 static inline void free_decl_arg(decl_arg
*arg
) {
169 free_decl_type(arg
->type
);
170 free_decl_var(arg
->var
);
172 free_decl_struct_layout(arg
->layout
);
177 typedef struct decl_typedefs
{
182 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_arg
*def
) {
184 defs
= calloc(1, sizeof(*defs
));
186 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
187 defs
->list
[defs
->count
-1] = def
;
191 static void free_decl_typedefs(decl_typedefs
*defs
) {
194 for (i
= 0; i
< defs
->count
; ++i
) {
195 free_decl_arg(defs
->list
[i
]);
201 typedef struct decl_vars
{
206 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
207 decl_vars
*vars
= calloc(1, sizeof(*vars
));
210 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
216 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
217 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
218 vars
->vars
[vars
->count
-1] = var
;
222 static inline void free_decl_vars(decl_vars
*vars
) {
225 for (i
= 0; i
< vars
->count
; ++i
) {
226 free_decl_var(vars
->vars
[i
]);
232 typedef struct decl_args
{
238 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
239 decl_args
*args
= calloc(1, sizeof(*args
));
242 args
->args
= calloc(1, sizeof(*args
->args
));
248 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
249 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
250 args
->args
[args
->count
-1] = arg
;
254 static inline void free_decl_args(decl_args
*args
) {
257 for (i
= 0; i
< args
->count
; ++i
) {
258 free_decl_arg(args
->args
[i
]);
264 typedef struct decl_abi
{
269 static inline decl_abi
*init_decl_abi(const char *convention
) {
270 decl_abi
*abi
= calloc(1, sizeof(*abi
));
271 abi
->convention
= strdup(convention
);
275 static inline void free_decl_abi(decl_abi
*abi
) {
279 free(abi
->convention
);
283 typedef struct decl_callinfo
{
291 typedef struct decl
{
299 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
300 decl
*d
= calloc(1, sizeof(*d
));
307 static inline void free_decl(decl
*d
) {
308 free_decl_abi(d
->abi
);
309 free_decl_arg(d
->func
);
311 free_decl_args(d
->args
);
316 typedef struct decls
{
321 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
323 decls
= calloc(1, sizeof(*decls
));
325 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
326 decls
->list
[decls
->count
-1] = decl
;
330 static inline void free_decls(decls
*decls
) {
333 for (i
= 0; i
< decls
->count
; ++i
) {
334 free_decl(decls
->list
[i
]);
341 typedef struct decl_struct
{
348 void (*dtor
)(void *type
);
352 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
353 decl_struct
*s
= calloc(1, sizeof(*s
));
354 s
->name
= strdup(name
);
359 static inline void free_decl_struct(decl_struct
*s
) {
364 free_decl_args(s
->args
);
366 if (s
->engine
.type
&& s
->engine
.dtor
) {
367 s
->engine
.dtor(s
->engine
.type
);
373 typedef struct decl_structs
{
378 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
380 ss
= calloc(1, sizeof(*ss
));
382 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
383 ss
->list
[ss
->count
-1] = s
;
387 static inline void free_decl_structs(decl_structs
*ss
) {
390 for (i
= 0; i
< ss
->count
; ++i
) {
391 free_decl_struct(ss
->list
[i
]);
397 typedef struct impl_type
{
402 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
403 impl_type
*t
= calloc(1, sizeof(*t
));
406 t
->name
= strdup(name
);
410 static inline void free_impl_type(impl_type
*type
) {
415 typedef struct impl_var
{
418 unsigned reference
:1;
421 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
422 impl_var
*var
= calloc(1, sizeof(*var
));
423 var
->name
= strdup(name
);
424 var
->reference
= is_reference
;
428 static inline void free_impl_var(impl_var
*var
) {
436 typedef struct impl_def_val
{
441 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
442 impl_def_val
*def
= calloc(1, sizeof(*def
));
444 def
->text
= strdup(text
);
448 static inline void free_impl_def_val(impl_def_val
*def
) {
453 typedef struct const_type
{
458 static inline const_type
*init_const_type(token_t type
, const char *name
) {
459 const_type
*ct
= calloc(1, sizeof(*ct
));
461 ct
->name
= strdup(name
);
465 static inline void free_const_type(const_type
*type
) {
470 typedef struct constant
{
476 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
477 constant
*c
= calloc(1, sizeof(*c
));
479 c
->name
= strdup(name
);
484 static inline void free_constant(constant
*constant
) {
485 free_const_type(constant
->type
);
486 free(constant
->name
);
487 free_impl_def_val(constant
->val
);
491 typedef struct constants
{
496 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
498 constants
= calloc(1, sizeof(*constants
));
500 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
501 constants
->list
[constants
->count
-1] = constant
;
505 static inline void free_constants(constants
*c
) {
508 for (i
= 0; i
< c
->count
; ++i
) {
509 free_constant(c
->list
[i
]);
515 typedef struct impl_arg
{
523 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
524 impl_arg
*arg
= calloc(1, sizeof(*arg
));
531 static inline void free_impl_arg(impl_arg
*arg
) {
532 free_impl_type(arg
->type
);
533 free_impl_var(arg
->var
);
535 free_impl_def_val(arg
->def
);
540 typedef struct impl_vararg
{
542 struct impl_args
*args
;
548 typedef struct impl_args
{
554 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
555 impl_args
*args
= calloc(1, sizeof(*args
));
558 args
->args
= calloc(1, sizeof(*args
->args
));
564 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
565 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
566 args
->args
[args
->count
-1] = arg
;
570 static inline void free_impl_args(impl_args
*args
) {
573 for (i
= 0; i
< args
->count
; ++i
) {
574 free_impl_arg(args
->args
[i
]);
576 if (args
->vararg
.name
) {
577 free_impl_arg(args
->vararg
.name
);
583 typedef struct impl_func
{
587 impl_type
*return_type
;
588 unsigned return_reference
:1;
591 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
592 impl_func
*func
= calloc(1, sizeof(*func
));
593 func
->name
= strdup(name
);
594 func
->args
= args
? args
: init_impl_args(NULL
);
595 func
->return_type
= type
;
596 func
->return_reference
= ret_reference
;
600 static inline void free_impl_func(impl_func
*f
) {
604 free_impl_type(f
->return_type
);
605 free_impl_args(f
->args
);
610 typedef struct num_exp
{
617 struct decl_enum_item
*enm
;
620 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
621 struct num_exp
*operand
;
624 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
625 num_exp
*exp
= calloc(1, sizeof(*exp
));
626 switch (exp
->t
= t
) {
629 exp
->u
.numb
= strdup(num
);
634 EMPTY_SWITCH_DEFAULT_CASE();
639 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
641 num_exp
*num
= calloc(1, sizeof(*num
));
643 memcpy(num
, exp
, sizeof(*num
));
646 num
->token
= PSI_TokenCopy(num
->token
);
649 num
->operand
= copy_num_exp(num
->operand
);
654 num
->u
.numb
= strdup(num
->u
.numb
);
657 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
658 dvar
->arg
= num
->u
.dvar
->arg
;
659 if (num
->u
.dvar
->token
) {
660 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
668 static inline void free_num_exp(num_exp
*exp
) {
679 free_decl_var(exp
->u
.dvar
);
683 EMPTY_SWITCH_DEFAULT_CASE();
686 free_num_exp(exp
->operand
);
691 typedef struct decl_enum_item
{
696 struct decl_enum_item
*prev
;
699 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
700 decl_enum_item
*i
= calloc(1, sizeof(*i
));
702 i
->name
= strdup(name
);
707 static inline void free_decl_enum_item(decl_enum_item
*i
) {
711 if (i
->num
&& i
->num
!= &i
->inc
) {
712 free_num_exp(i
->num
);
718 typedef struct decl_enum_items
{
719 decl_enum_item
**list
;
723 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
724 decl_enum_items
*l
= calloc(1, sizeof(*l
));
728 l
->list
= calloc(1, sizeof(*l
->list
));
734 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
735 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
736 l
->list
[l
->count
] = i
;
738 i
->prev
= l
->list
[l
->count
- 1];
744 static inline void free_decl_enum_items(decl_enum_items
*l
) {
748 for (j
= 0; j
< l
->count
; ++j
) {
749 free_decl_enum_item(l
->list
[j
]);
756 typedef struct decl_enum
{
759 decl_enum_items
*items
;
762 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
763 decl_enum
*e
= calloc(1, sizeof(*e
));
765 e
->name
= strdup(name
);
770 static inline void free_decl_enum(decl_enum
*e
) {
775 free_decl_enum_items(e
->items
);
781 typedef struct decl_enums
{
786 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
788 es
= calloc(1, sizeof(*es
));
790 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
791 es
->list
[es
->count
-1] = e
;
795 static inline void free_decl_enums(decl_enums
*es
) {
799 for (j
= 0; j
< es
->count
; ++j
) {
800 free_decl_enum(es
->list
[j
]);
807 typedef struct let_calloc
{
812 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
813 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
814 alloc
->nmemb
= nmemb
;
819 static inline void free_let_calloc(let_calloc
*alloc
) {
820 free_num_exp(alloc
->nmemb
);
821 free_num_exp(alloc
->size
);
825 typedef struct let_func
{
832 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
833 let_func
*func
= calloc(1, sizeof(*func
));
835 func
->name
= strdup(name
);
840 static inline void free_let_func(let_func
*func
) {
841 free_impl_var(func
->var
);
846 #define PSI_LET_REFERENCE 0x1;
847 typedef struct let_val
{
863 unsigned is_reference
:1;
869 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
870 let_val
*let
= calloc(1, sizeof(*let
));
871 switch (let
->kind
= kind
) {
875 let
->data
.num
= data
;
878 let
->data
.alloc
= data
;
881 let
->data
.func
= data
;
884 let
->data
.var
= data
;
886 EMPTY_SWITCH_DEFAULT_CASE();
891 static inline void free_let_val(let_val
*let
) {
896 free_num_exp(let
->data
.num
);
899 free_let_calloc(let
->data
.alloc
);
902 free_let_func(let
->data
.func
);
905 free_decl_var(let
->data
.var
);
907 EMPTY_SWITCH_DEFAULT_CASE();
912 typedef struct let_stmt
{
919 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
920 let_stmt
*let
= calloc(1, sizeof(*let
));
926 static inline void free_let_stmt(let_stmt
*stmt
) {
928 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
929 free_decl_arg(stmt
->var
->arg
);
931 free_let_val(stmt
->val
);
933 free_decl_var(stmt
->var
);
939 typedef struct set_func
{
943 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
946 static inline set_func
*init_set_func(token_t type
, const char *name
) {
947 set_func
*func
= calloc(1, sizeof(*func
));
949 func
->name
= strdup(name
);
953 static inline void free_set_func(set_func
*func
) {
961 typedef struct set_value
{
966 struct set_value
*set
;
969 struct set_value
**inner
;
973 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
974 set_value
*val
= calloc(1, sizeof(*val
));
979 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
980 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
981 val
->inner
[val
->count
-1] = inner
;
982 inner
->outer
.set
= val
;
986 static inline void free_set_value(set_value
*val
) {
988 free_set_func(val
->func
);
991 free_decl_vars(val
->vars
);
993 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
995 for (i
= 0; i
< val
->count
; ++i
) {
996 free_set_value(val
->inner
[i
]);
1001 free_num_exp(val
->num
);
1006 typedef struct set_stmt
{
1012 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1013 set_stmt
*set
= calloc(1, sizeof(*set
));
1019 static inline void free_set_stmt(set_stmt
*set
) {
1020 free_impl_var(set
->var
);
1021 free_set_value(set
->val
);
1025 typedef struct return_stmt
{
1031 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1032 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1037 static inline void free_return_stmt(return_stmt
*ret
) {
1041 free_set_value(ret
->set
);
1045 typedef struct free_call
{
1052 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1053 free_call
*f
= calloc(1, sizeof(*f
));
1054 f
->func
= strdup(func
);
1059 static inline void free_free_call(free_call
*f
) {
1064 free_decl_vars(f
->vars
);
1068 typedef struct free_calls
{
1073 static inline free_calls
*init_free_calls(free_call
*f
) {
1074 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1077 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1083 static inline void free_free_calls(free_calls
*fcs
) {
1086 for (i
= 0; i
< fcs
->count
; ++i
) {
1087 free_free_call(fcs
->list
[i
]);
1093 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1094 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1095 fcs
->list
[fcs
->count
-1] = f
;
1099 typedef struct free_stmt
{
1103 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1104 free_stmt
*f
= calloc(1, sizeof(*f
));
1109 static inline void free_free_stmt(free_stmt
*f
) {
1110 free_free_calls(f
->calls
);
1114 typedef struct impl_stmt
{
1125 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1126 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1132 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1133 switch (stmt
->type
) {
1135 free_let_stmt(stmt
->s
.let
);
1138 free_set_stmt(stmt
->s
.set
);
1141 free_return_stmt(stmt
->s
.ret
);
1144 free_free_stmt(stmt
->s
.fre
);
1150 typedef struct impl_stmts
{
1169 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1170 list
= realloc(list
, count
* sizeof(list
));
1171 ((void **)list
)[count
-1] = stmt
;
1175 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1176 switch (stmt
->type
) {
1178 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1181 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1184 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1187 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1194 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1195 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1196 return add_impl_stmt(stmts
, stmt
);
1199 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1202 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1203 free_let_stmt(stmts
->let
.list
[i
]);
1205 free(stmts
->let
.list
);
1206 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1207 free_return_stmt(stmts
->ret
.list
[i
]);
1209 free(stmts
->ret
.list
);
1210 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1211 free_set_stmt(stmts
->set
.list
[i
]);
1213 free(stmts
->set
.list
);
1214 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1215 free_free_stmt(stmts
->fre
.list
[i
]);
1217 free(stmts
->fre
.list
);
1221 typedef struct impl
{
1227 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1228 impl
*i
= calloc(1, sizeof(*i
));
1234 static inline void free_impl(impl
*impl
) {
1235 free_impl_func(impl
->func
);
1236 free_impl_stmts(impl
->stmts
);
1240 typedef struct impls
{
1245 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1247 impls
= calloc(1, sizeof(*impls
));
1249 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1250 impls
->list
[impls
->count
-1] = impl
;
1254 static void free_impls(impls
*impls
) {
1257 for (i
= 0; i
< impls
->count
; ++i
) {
1258 free_impl(impls
->list
[i
]);
1264 typedef struct decl_file
{
1269 static inline void free_decl_file(decl_file
*file
) {
1276 memset(file
, 0, sizeof(*file
));
1279 typedef struct decl_libs
{
1284 static inline void free_decl_libs(decl_libs
*libs
) {
1287 for (i
= 0; i
< libs
->count
; ++i
) {
1289 dlclose(libs
->dl
[i
]);
1294 memset(libs
, 0, sizeof(*libs
));
1297 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1298 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1299 libs
->dl
[libs
->count
-1] = dlopened
;
1302 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1305 ZEND_ASSERT(var
->arg
->var
!= var
);
1307 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1308 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1309 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1311 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1313 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1315 ret_val
= *(void **) ret_val
;
1320 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1321 impl_val
*val
, *val_ptr
;
1324 ZEND_ASSERT(var
->arg
->var
== var
);
1326 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1327 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1328 var
->array_size
, var
->arg
->var
->array_size
);
1330 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1334 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1335 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1337 fprintf(stderr
, "++\n");
1339 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1340 val_ptr
= val_ptr
->ptr
;
1346 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1347 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1349 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1355 #define PSI_ERROR 16
1356 #define PSI_WARNING 32
1357 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1359 #define PSI_DATA(D) ((PSI_Data *) (D))
1360 #define PSI_DATA_MEMBERS \
1361 constants *consts; \
1362 decl_typedefs *defs; \
1363 decl_structs *structs; \
1364 decl_enums *enums; \
1372 typedef struct PSI_Data
{
1376 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1378 dest
= malloc(sizeof(*dest
));
1380 memcpy(dest
, src
, sizeof(*dest
));
1381 memset(src
, 0, sizeof(*src
));
1385 static inline void PSI_DataDtor(PSI_Data
*data
) {
1387 free_constants(data
->consts
);
1390 free_decl_typedefs(data
->defs
);
1392 if (data
->structs
) {
1393 free_decl_structs(data
->structs
);
1396 free_decl_enums(data
->enums
);
1399 free_decls(data
->decls
);
1402 free_impls(data
->impls
);
1404 free_decl_file(&data
->psi
.file
);
1407 typedef struct PSI_Parser
{
1412 unsigned flags
, errors
, line
, col
;
1413 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1416 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1417 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1420 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1422 size_t token_len
, fname_len
;
1425 if (P
->cur
< P
->tok
) {
1430 token_len
= P
->cur
- P
->tok
;
1431 fname_len
= strlen(P
->psi
.file
.fn
);
1433 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1434 T
->type
= token_typ
;
1435 T
->size
= token_len
;
1436 T
->text
= &T
->buf
[0];
1437 T
->file
= &T
->buf
[token_len
+ 1];
1441 memcpy(T
->text
, P
->tok
, token_len
);
1442 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1447 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1448 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1449 PSI_Token
*ptr
= malloc(strct_len
);
1451 memcpy(ptr
, src
, strct_len
);
1453 ptr
->text
= &ptr
->buf
[0];
1454 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1459 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1462 PSI_Token
*T
= NULL
;
1464 va_start(argv
, argc
);
1465 for (i
= 0; i
< argc
; ++i
) {
1466 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1469 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1471 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1472 T
->text
= &T
->buf
[0];
1473 T
->file
= &T
->buf
[T
->size
+ 1];
1474 T
->buf
[token_len
] = ' ';
1475 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1476 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1478 T
= PSI_TokenCopy(arg
);
1479 T
->type
= PSI_T_NAME
;
1487 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1491 va_start(argv
, argc
);
1492 for (i
= 0; i
< argc
; ++i
) {
1493 char *str
= va_arg(argv
, char *);
1494 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1496 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1497 T
->text
= &T
->buf
[0];
1498 T
->file
= &T
->buf
[T
->size
+ 1];
1499 T
->buf
[token_len
] = ' ';
1500 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1501 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1508 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
1509 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1510 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1514 static inline uint64_t psi_hash(char *digest_buf
, ...)
1516 uint64_t hash
= 5381;
1521 va_start(argv
, digest_buf
);
1522 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1523 while ((c
= *ptr
++)) {
1524 hash
= ((hash
<< 5) + hash
) + c
;
1530 sprintf(digest_buf
, "%" PRIx64
, hash
);
1536 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1539 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1540 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1543 #define PSI_PARSER_DEBUG 0x1
1545 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1546 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1547 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1548 token_t
PSI_ParserScan(PSI_Parser
*P
);
1549 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1550 void PSI_ParserDtor(PSI_Parser
*P
);
1551 void PSI_ParserFree(PSI_Parser
**P
);