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 union impl_val
{
48 #ifdef HAVE_LONG_DOUBLE
57 zend_fcall_info_cache
*fcc
;
61 uint8_t _dbg
[sizeof(void *)];
64 typedef struct decl_type
{
68 struct decl_type
*real
;
69 struct decl_struct
*strct
;
70 struct decl_union
*unn
;
71 struct decl_enum
*enm
;
75 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
76 decl_type
*t
= calloc(1, sizeof(*t
));
78 t
->name
= strdup(name
);
82 static inline decl_type
*real_decl_type(decl_type
*type
) {
89 static inline void free_decl(struct decl
*decl
);
90 static inline void free_decl_type(decl_type
*type
) {
94 if (type
->type
== PSI_T_FUNCTION
) {
95 free_decl(type
->func
);
101 typedef struct decl_var
{
104 unsigned pointer_level
;
106 struct decl_arg
*arg
;
109 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
110 decl_var
*v
= calloc(1, sizeof(*v
));
111 v
->name
= (char *) strdup((const char *) name
);
112 v
->pointer_level
= pl
;
117 static inline decl_var
*copy_decl_var(decl_var
*src
) {
118 decl_var
*dest
= calloc(1, sizeof(*dest
));
120 memcpy(dest
, src
, sizeof(*dest
));
121 dest
->name
= strdup(dest
->name
);
123 dest
->token
= PSI_TokenCopy(dest
->token
);
128 static inline void free_decl_var(decl_var
*var
) {
136 typedef struct decl_struct_layout
{
139 } decl_struct_layout
;
141 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
142 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
143 ZEND_ASSERT(pos
+len
);
149 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
153 typedef struct decl_arg
{
157 decl_struct_layout
*layout
;
158 struct let_stmt
*let
;
164 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
165 decl_arg
*arg
= calloc(1, sizeof(*arg
));
166 arg
->token
= var
->token
;
170 arg
->ptr
= &arg
->val
;
174 static inline void free_decl_arg(decl_arg
*arg
) {
175 if (arg
->token
&& arg
->token
!= arg
->var
->token
) {
178 free_decl_type(arg
->type
);
179 free_decl_var(arg
->var
);
181 free_decl_struct_layout(arg
->layout
);
186 typedef struct decl_typedefs
{
191 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_arg
*def
) {
193 defs
= calloc(1, sizeof(*defs
));
195 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
196 defs
->list
[defs
->count
-1] = def
;
200 static void free_decl_typedefs(decl_typedefs
*defs
) {
203 for (i
= 0; i
< defs
->count
; ++i
) {
204 free_decl_arg(defs
->list
[i
]);
210 typedef struct decl_vars
{
215 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
216 decl_vars
*vars
= calloc(1, sizeof(*vars
));
219 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
225 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
226 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
227 vars
->vars
[vars
->count
-1] = var
;
231 static inline void free_decl_vars(decl_vars
*vars
) {
234 for (i
= 0; i
< vars
->count
; ++i
) {
235 free_decl_var(vars
->vars
[i
]);
241 typedef struct decl_args
{
247 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
248 decl_args
*args
= calloc(1, sizeof(*args
));
251 args
->args
= calloc(1, sizeof(*args
->args
));
257 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
258 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
259 args
->args
[args
->count
-1] = arg
;
263 static inline void free_decl_args(decl_args
*args
) {
266 for (i
= 0; i
< args
->count
; ++i
) {
267 free_decl_arg(args
->args
[i
]);
273 typedef struct decl_abi
{
278 static inline decl_abi
*init_decl_abi(const char *convention
) {
279 decl_abi
*abi
= calloc(1, sizeof(*abi
));
280 abi
->convention
= strdup(convention
);
284 static inline void free_decl_abi(decl_abi
*abi
) {
288 free(abi
->convention
);
292 typedef struct decl_callinfo
{
300 void (*dtor
)(void *data
);
304 typedef struct decl
{
312 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
313 decl
*d
= calloc(1, sizeof(*d
));
320 static inline void free_decl(decl
*d
) {
321 free_decl_abi(d
->abi
);
322 free_decl_arg(d
->func
);
324 free_decl_args(d
->args
);
329 typedef struct decls
{
334 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
336 decls
= calloc(1, sizeof(*decls
));
338 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
339 decls
->list
[decls
->count
-1] = decl
;
343 static inline void free_decls(decls
*decls
) {
346 for (i
= 0; i
< decls
->count
; ++i
) {
347 free_decl(decls
->list
[i
]);
354 typedef struct decl_struct
{
362 void (*dtor
)(void *type
);
366 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
367 decl_struct
*s
= calloc(1, sizeof(*s
));
368 s
->name
= strdup(name
);
373 static inline void free_decl_struct(decl_struct
*s
) {
378 free_decl_args(s
->args
);
380 if (s
->engine
.type
&& s
->engine
.dtor
) {
381 s
->engine
.dtor(s
->engine
.type
);
387 typedef struct decl_structs
{
392 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
394 ss
= calloc(1, sizeof(*ss
));
396 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
397 ss
->list
[ss
->count
-1] = s
;
401 static inline void free_decl_structs(decl_structs
*ss
) {
404 for (i
= 0; i
< ss
->count
; ++i
) {
405 free_decl_struct(ss
->list
[i
]);
411 typedef struct decl_union
{
419 static inline decl_union
*init_decl_union(const char *name
, decl_args
*args
) {
420 decl_union
*u
= calloc(1, sizeof(*u
));
421 u
->name
= strdup(name
);
426 static inline void free_decl_union(decl_union
*u
) {
431 free_decl_args(u
->args
);
437 typedef struct decl_unions
{
442 static inline decl_unions
*add_decl_union(decl_unions
*uu
, decl_union
*u
) {
444 uu
= calloc(1, sizeof(*uu
));
446 uu
->list
= realloc(uu
->list
, ++uu
->count
* sizeof(*uu
->list
));
447 uu
->list
[uu
->count
-1] = u
;
451 static inline void free_decl_unions(decl_unions
*uu
) {
454 for (i
= 0; i
< uu
->count
; ++i
) {
455 free_decl_union(uu
->list
[i
]);
461 typedef struct impl_type
{
466 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
467 impl_type
*t
= calloc(1, sizeof(*t
));
470 t
->name
= strdup(name
);
474 static inline void free_impl_type(impl_type
*type
) {
479 typedef struct impl_var
{
482 unsigned reference
:1;
485 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
486 impl_var
*var
= calloc(1, sizeof(*var
));
487 var
->name
= strdup(name
);
488 var
->reference
= is_reference
;
492 static inline void free_impl_var(impl_var
*var
) {
500 typedef struct impl_def_val
{
505 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
506 impl_def_val
*def
= calloc(1, sizeof(*def
));
508 def
->text
= strdup(text
);
512 static inline void free_impl_def_val(impl_def_val
*def
) {
517 typedef struct const_type
{
522 static inline const_type
*init_const_type(token_t type
, const char *name
) {
523 const_type
*ct
= calloc(1, sizeof(*ct
));
525 ct
->name
= strdup(name
);
529 static inline void free_const_type(const_type
*type
) {
534 typedef struct constant
{
540 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
541 constant
*c
= calloc(1, sizeof(*c
));
543 c
->name
= strdup(name
);
548 static inline void free_constant(constant
*constant
) {
549 free_const_type(constant
->type
);
550 free(constant
->name
);
551 free_impl_def_val(constant
->val
);
555 typedef struct constants
{
560 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
562 constants
= calloc(1, sizeof(*constants
));
564 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
565 constants
->list
[constants
->count
-1] = constant
;
569 static inline void free_constants(constants
*c
) {
572 for (i
= 0; i
< c
->count
; ++i
) {
573 free_constant(c
->list
[i
]);
579 typedef struct impl_arg
{
587 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
588 impl_arg
*arg
= calloc(1, sizeof(*arg
));
595 static inline void free_impl_arg(impl_arg
*arg
) {
596 free_impl_type(arg
->type
);
597 free_impl_var(arg
->var
);
599 free_impl_def_val(arg
->def
);
604 typedef struct impl_vararg
{
606 struct impl_args
*args
;
612 typedef struct impl_args
{
618 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
619 impl_args
*args
= calloc(1, sizeof(*args
));
622 args
->args
= calloc(1, sizeof(*args
->args
));
628 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
629 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
630 args
->args
[args
->count
-1] = arg
;
634 static inline void free_impl_args(impl_args
*args
) {
637 for (i
= 0; i
< args
->count
; ++i
) {
638 free_impl_arg(args
->args
[i
]);
640 if (args
->vararg
.name
) {
641 free_impl_arg(args
->vararg
.name
);
647 typedef struct impl_func
{
651 impl_type
*return_type
;
652 unsigned return_reference
:1;
655 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
656 impl_func
*func
= calloc(1, sizeof(*func
));
657 func
->name
= strdup(name
);
658 func
->args
= args
? args
: init_impl_args(NULL
);
659 func
->return_type
= type
;
660 func
->return_reference
= ret_reference
;
664 static inline void free_impl_func(impl_func
*f
) {
668 free_impl_type(f
->return_type
);
669 free_impl_args(f
->args
);
674 typedef struct num_exp
{
681 struct decl_enum_item
*enm
;
684 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
685 struct num_exp
*operand
;
688 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
689 num_exp
*exp
= calloc(1, sizeof(*exp
));
690 switch (exp
->t
= t
) {
693 exp
->u
.numb
= strdup(num
);
698 EMPTY_SWITCH_DEFAULT_CASE();
703 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
705 num_exp
*num
= calloc(1, sizeof(*num
));
707 memcpy(num
, exp
, sizeof(*num
));
710 num
->token
= PSI_TokenCopy(num
->token
);
713 num
->operand
= copy_num_exp(num
->operand
);
718 num
->u
.numb
= strdup(num
->u
.numb
);
721 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
722 dvar
->arg
= num
->u
.dvar
->arg
;
723 if (num
->u
.dvar
->token
) {
724 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
732 static inline void free_num_exp(num_exp
*exp
) {
743 free_decl_var(exp
->u
.dvar
);
747 EMPTY_SWITCH_DEFAULT_CASE();
750 free_num_exp(exp
->operand
);
755 typedef struct decl_enum_item
{
760 struct decl_enum_item
*prev
;
763 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
764 decl_enum_item
*i
= calloc(1, sizeof(*i
));
766 i
->name
= strdup(name
);
771 static inline void free_decl_enum_item(decl_enum_item
*i
) {
775 if (i
->num
&& i
->num
!= &i
->inc
) {
776 free_num_exp(i
->num
);
782 typedef struct decl_enum_items
{
783 decl_enum_item
**list
;
787 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
788 decl_enum_items
*l
= calloc(1, sizeof(*l
));
792 l
->list
= calloc(1, sizeof(*l
->list
));
798 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
799 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
800 l
->list
[l
->count
] = i
;
802 i
->prev
= l
->list
[l
->count
- 1];
808 static inline void free_decl_enum_items(decl_enum_items
*l
) {
812 for (j
= 0; j
< l
->count
; ++j
) {
813 free_decl_enum_item(l
->list
[j
]);
820 typedef struct decl_enum
{
823 decl_enum_items
*items
;
826 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
827 decl_enum
*e
= calloc(1, sizeof(*e
));
829 e
->name
= strdup(name
);
834 static inline void free_decl_enum(decl_enum
*e
) {
839 free_decl_enum_items(e
->items
);
845 typedef struct decl_enums
{
850 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
852 es
= calloc(1, sizeof(*es
));
854 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
855 es
->list
[es
->count
-1] = e
;
859 static inline void free_decl_enums(decl_enums
*es
) {
863 for (j
= 0; j
< es
->count
; ++j
) {
864 free_decl_enum(es
->list
[j
]);
871 typedef struct let_calloc
{
876 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
877 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
878 alloc
->nmemb
= nmemb
;
883 static inline void free_let_calloc(let_calloc
*alloc
) {
884 free_num_exp(alloc
->nmemb
);
885 free_num_exp(alloc
->size
);
889 typedef struct let_func
{
894 set_values
*callback
;
897 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
898 let_func
*func
= calloc(1, sizeof(*func
));
900 func
->name
= strdup(name
);
905 static inline void free_let_func(let_func
*func
) {
906 free_impl_var(func
->var
);
911 #define PSI_LET_REFERENCE 0x1;
912 typedef struct let_val
{
928 unsigned is_reference
:1;
934 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
935 let_val
*let
= calloc(1, sizeof(*let
));
936 switch (let
->kind
= kind
) {
940 let
->data
.num
= data
;
943 let
->data
.alloc
= data
;
946 let
->data
.func
= data
;
949 let
->data
.var
= data
;
951 EMPTY_SWITCH_DEFAULT_CASE();
956 static inline void free_let_val(let_val
*let
) {
961 free_num_exp(let
->data
.num
);
964 free_let_calloc(let
->data
.alloc
);
967 free_let_func(let
->data
.func
);
970 free_decl_var(let
->data
.var
);
972 EMPTY_SWITCH_DEFAULT_CASE();
977 typedef struct let_stmt
{
984 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
985 let_stmt
*let
= calloc(1, sizeof(*let
));
991 static inline void free_let_stmt(let_stmt
*stmt
) {
993 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
994 free_decl_arg(stmt
->var
->arg
);
996 free_let_val(stmt
->val
);
998 free_decl_var(stmt
->var
);
1004 typedef struct set_func
{
1008 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
1011 static inline set_func
*init_set_func(token_t type
, const char *name
) {
1012 set_func
*func
= calloc(1, sizeof(*func
));
1014 func
->name
= strdup(name
);
1018 static inline void free_set_func(set_func
*func
) {
1026 typedef struct set_value
{
1031 struct set_value
*set
;
1034 struct set_value
**inner
;
1038 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
1039 set_value
*val
= calloc(1, sizeof(*val
));
1044 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
1045 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
1046 val
->inner
[val
->count
-1] = inner
;
1047 inner
->outer
.set
= val
;
1051 static inline void free_set_value(set_value
*val
) {
1053 free_set_func(val
->func
);
1056 free_decl_vars(val
->vars
);
1058 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1060 for (i
= 0; i
< val
->count
; ++i
) {
1061 free_set_value(val
->inner
[i
]);
1066 free_num_exp(val
->num
);
1071 typedef struct set_values
{
1076 static inline set_values
*init_set_values(set_value
*val
) {
1077 set_values
*vals
= calloc(1, sizeof(*vals
));
1080 vals
->vals
= calloc(1, sizeof(val
));
1081 vals
->vals
[0] = val
;
1086 static inline set_values
*add_set_value(set_values
*vals
, set_value
*val
) {
1087 vals
->vals
= realloc(vals
->vals
, ++vals
->count
* sizeof(val
));
1088 vals
->vals
[vals
->count
-1] = val
;
1092 static inline void free_set_values(set_values
*vals
) {
1099 typedef struct set_stmt
{
1105 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1106 set_stmt
*set
= calloc(1, sizeof(*set
));
1112 static inline void free_set_stmt(set_stmt
*set
) {
1113 free_impl_var(set
->var
);
1114 free_set_value(set
->val
);
1118 typedef struct return_stmt
{
1124 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1125 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1130 static inline void free_return_stmt(return_stmt
*ret
) {
1134 free_set_value(ret
->set
);
1138 typedef struct free_call
{
1145 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1146 free_call
*f
= calloc(1, sizeof(*f
));
1147 f
->func
= strdup(func
);
1152 static inline void free_free_call(free_call
*f
) {
1157 free_decl_vars(f
->vars
);
1161 typedef struct free_calls
{
1166 static inline free_calls
*init_free_calls(free_call
*f
) {
1167 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1170 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1176 static inline void free_free_calls(free_calls
*fcs
) {
1179 for (i
= 0; i
< fcs
->count
; ++i
) {
1180 free_free_call(fcs
->list
[i
]);
1186 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1187 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1188 fcs
->list
[fcs
->count
-1] = f
;
1192 typedef struct free_stmt
{
1196 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1197 free_stmt
*f
= calloc(1, sizeof(*f
));
1202 static inline void free_free_stmt(free_stmt
*f
) {
1203 free_free_calls(f
->calls
);
1207 typedef struct impl_stmt
{
1218 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1219 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1225 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1226 switch (stmt
->type
) {
1228 free_let_stmt(stmt
->s
.let
);
1231 free_set_stmt(stmt
->s
.set
);
1234 free_return_stmt(stmt
->s
.ret
);
1237 free_free_stmt(stmt
->s
.fre
);
1243 typedef struct impl_stmts
{
1262 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1263 list
= realloc(list
, count
* sizeof(list
));
1264 ((void **)list
)[count
-1] = stmt
;
1268 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1269 switch (stmt
->type
) {
1271 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1274 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1277 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1280 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1287 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1288 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1289 return add_impl_stmt(stmts
, stmt
);
1292 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1295 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1296 free_let_stmt(stmts
->let
.list
[i
]);
1298 free(stmts
->let
.list
);
1299 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1300 free_return_stmt(stmts
->ret
.list
[i
]);
1302 free(stmts
->ret
.list
);
1303 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1304 free_set_stmt(stmts
->set
.list
[i
]);
1306 free(stmts
->set
.list
);
1307 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1308 free_free_stmt(stmts
->fre
.list
[i
]);
1310 free(stmts
->fre
.list
);
1314 typedef struct impl
{
1320 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1321 impl
*i
= calloc(1, sizeof(*i
));
1327 static inline void free_impl(impl
*impl
) {
1328 free_impl_func(impl
->func
);
1329 free_impl_stmts(impl
->stmts
);
1333 typedef struct impls
{
1338 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1340 impls
= calloc(1, sizeof(*impls
));
1342 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1343 impls
->list
[impls
->count
-1] = impl
;
1347 static void free_impls(impls
*impls
) {
1350 for (i
= 0; i
< impls
->count
; ++i
) {
1351 free_impl(impls
->list
[i
]);
1357 typedef struct decl_file
{
1362 static inline void free_decl_file(decl_file
*file
) {
1369 memset(file
, 0, sizeof(*file
));
1372 typedef struct decl_libs
{
1377 static inline void free_decl_libs(decl_libs
*libs
) {
1380 for (i
= 0; i
< libs
->count
; ++i
) {
1382 dlclose(libs
->dl
[i
]);
1387 memset(libs
, 0, sizeof(*libs
));
1390 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1391 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1392 libs
->dl
[libs
->count
-1] = dlopened
;
1395 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1398 ZEND_ASSERT(var
->arg
->var
!= var
);
1400 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1401 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1402 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1404 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1406 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1408 ret_val
= *(void **) ret_val
;
1413 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1414 impl_val
*val
, *val_ptr
;
1417 ZEND_ASSERT(var
->arg
->var
== var
);
1419 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1420 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1421 var
->array_size
, var
->arg
->var
->array_size
);
1423 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1427 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1428 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1430 fprintf(stderr
, "++\n");
1432 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1433 val_ptr
= val_ptr
->ptr
;
1439 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1440 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1442 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1448 #define PSI_ERROR 16
1449 #define PSI_WARNING 32
1450 typedef void (*psi_error_cb
)(void *context
, PSI_Token
*token
, int type
, const char *msg
, ...);
1452 #define PSI_DATA(D) ((PSI_Data *) (D))
1453 #define PSI_DATA_MEMBERS \
1454 constants *consts; \
1455 decl_typedefs *defs; \
1456 decl_structs *structs; \
1457 decl_unions *unions; \
1458 decl_enums *enums; \
1465 psi_error_cb error; \
1468 typedef struct PSI_Data
{
1472 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1474 dest
= malloc(sizeof(*dest
));
1476 memcpy(dest
, src
, sizeof(*dest
));
1477 memset(src
, 0, sizeof(*src
));
1481 static inline void PSI_DataDtor(PSI_Data
*data
) {
1483 free_constants(data
->consts
);
1486 free_decl_typedefs(data
->defs
);
1488 if (data
->structs
) {
1489 free_decl_structs(data
->structs
);
1492 free_decl_unions(data
->unions
);
1495 free_decl_enums(data
->enums
);
1498 free_decls(data
->decls
);
1501 free_impls(data
->impls
);
1503 free_decl_file(&data
->psi
.file
);
1506 typedef struct PSI_Parser
{
1512 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1515 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1516 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1519 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1521 size_t token_len
, fname_len
;
1524 if (P
->cur
< P
->tok
) {
1529 token_len
= P
->cur
- P
->tok
;
1530 fname_len
= strlen(P
->psi
.file
.fn
);
1532 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1533 T
->type
= token_typ
;
1534 T
->size
= token_len
;
1535 T
->text
= &T
->buf
[0];
1536 T
->file
= &T
->buf
[token_len
+ 1];
1540 memcpy(T
->text
, P
->tok
, token_len
);
1541 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1546 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1547 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1548 PSI_Token
*ptr
= malloc(strct_len
);
1550 memcpy(ptr
, src
, strct_len
);
1552 ptr
->text
= &ptr
->buf
[0];
1553 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1558 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1561 PSI_Token
*T
= NULL
;
1563 va_start(argv
, argc
);
1564 for (i
= 0; i
< argc
; ++i
) {
1565 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1568 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1570 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1571 T
->text
= &T
->buf
[0];
1572 T
->file
= &T
->buf
[T
->size
+ 1];
1573 T
->buf
[token_len
] = ' ';
1574 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1575 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1577 T
= PSI_TokenCopy(arg
);
1578 T
->type
= PSI_T_NAME
;
1586 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1590 va_start(argv
, argc
);
1591 for (i
= 0; i
< argc
; ++i
) {
1592 char *str
= va_arg(argv
, char *);
1593 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1595 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1596 T
->text
= &T
->buf
[0];
1597 T
->file
= &T
->buf
[T
->size
+ 1];
1598 T
->buf
[token_len
] = ' ';
1599 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1600 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1607 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
1608 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1609 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1613 static inline uint64_t psi_hash(char *digest_buf
, ...)
1615 uint64_t hash
= 5381;
1620 va_start(argv
, digest_buf
);
1621 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1622 while ((c
= *ptr
++)) {
1623 hash
= ((hash
<< 5) + hash
) + c
;
1629 sprintf(digest_buf
, "%" PRIx64
, hash
);
1635 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1638 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1639 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1642 #define PSI_PARSER_DEBUG 0x1
1643 #define PSI_PARSER_SILENT 0x2
1645 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1646 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1647 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1648 token_t
PSI_ParserScan(PSI_Parser
*P
);
1649 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1650 void PSI_ParserDtor(PSI_Parser
*P
);
1651 void PSI_ParserFree(PSI_Parser
**P
);