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
{
896 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
897 let_func
*func
= calloc(1, sizeof(*func
));
899 func
->name
= strdup(name
);
904 static inline void free_let_func(let_func
*func
) {
905 free_impl_var(func
->var
);
910 #define PSI_LET_REFERENCE 0x1;
911 typedef struct let_val
{
927 unsigned is_reference
:1;
933 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
934 let_val
*let
= calloc(1, sizeof(*let
));
935 switch (let
->kind
= kind
) {
939 let
->data
.num
= data
;
942 let
->data
.alloc
= data
;
945 let
->data
.func
= data
;
948 let
->data
.var
= data
;
950 EMPTY_SWITCH_DEFAULT_CASE();
955 static inline void free_let_val(let_val
*let
) {
960 free_num_exp(let
->data
.num
);
963 free_let_calloc(let
->data
.alloc
);
966 free_let_func(let
->data
.func
);
969 free_decl_var(let
->data
.var
);
971 EMPTY_SWITCH_DEFAULT_CASE();
976 typedef struct let_stmt
{
983 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
984 let_stmt
*let
= calloc(1, sizeof(*let
));
990 static inline void free_let_stmt(let_stmt
*stmt
) {
992 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
993 free_decl_arg(stmt
->var
->arg
);
995 free_let_val(stmt
->val
);
997 free_decl_var(stmt
->var
);
1003 typedef struct set_func
{
1007 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
1010 static inline set_func
*init_set_func(token_t type
, const char *name
) {
1011 set_func
*func
= calloc(1, sizeof(*func
));
1013 func
->name
= strdup(name
);
1017 static inline void free_set_func(set_func
*func
) {
1025 typedef struct set_value
{
1030 struct set_value
*set
;
1033 struct set_value
**inner
;
1037 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
1038 set_value
*val
= calloc(1, sizeof(*val
));
1043 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
1044 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
1045 val
->inner
[val
->count
-1] = inner
;
1046 inner
->outer
.set
= val
;
1050 static inline void free_set_value(set_value
*val
) {
1052 free_set_func(val
->func
);
1055 free_decl_vars(val
->vars
);
1057 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1059 for (i
= 0; i
< val
->count
; ++i
) {
1060 free_set_value(val
->inner
[i
]);
1065 free_num_exp(val
->num
);
1070 typedef struct set_stmt
{
1076 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1077 set_stmt
*set
= calloc(1, sizeof(*set
));
1083 static inline void free_set_stmt(set_stmt
*set
) {
1084 free_impl_var(set
->var
);
1085 free_set_value(set
->val
);
1089 typedef struct return_stmt
{
1095 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1096 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1101 static inline void free_return_stmt(return_stmt
*ret
) {
1105 free_set_value(ret
->set
);
1109 typedef struct free_call
{
1116 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1117 free_call
*f
= calloc(1, sizeof(*f
));
1118 f
->func
= strdup(func
);
1123 static inline void free_free_call(free_call
*f
) {
1128 free_decl_vars(f
->vars
);
1132 typedef struct free_calls
{
1137 static inline free_calls
*init_free_calls(free_call
*f
) {
1138 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1141 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1147 static inline void free_free_calls(free_calls
*fcs
) {
1150 for (i
= 0; i
< fcs
->count
; ++i
) {
1151 free_free_call(fcs
->list
[i
]);
1157 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1158 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1159 fcs
->list
[fcs
->count
-1] = f
;
1163 typedef struct free_stmt
{
1167 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1168 free_stmt
*f
= calloc(1, sizeof(*f
));
1173 static inline void free_free_stmt(free_stmt
*f
) {
1174 free_free_calls(f
->calls
);
1178 typedef struct impl_stmt
{
1189 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1190 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1196 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1197 switch (stmt
->type
) {
1199 free_let_stmt(stmt
->s
.let
);
1202 free_set_stmt(stmt
->s
.set
);
1205 free_return_stmt(stmt
->s
.ret
);
1208 free_free_stmt(stmt
->s
.fre
);
1214 typedef struct impl_stmts
{
1233 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1234 list
= realloc(list
, count
* sizeof(list
));
1235 ((void **)list
)[count
-1] = stmt
;
1239 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1240 switch (stmt
->type
) {
1242 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1245 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1248 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1251 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1258 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1259 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1260 return add_impl_stmt(stmts
, stmt
);
1263 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1266 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1267 free_let_stmt(stmts
->let
.list
[i
]);
1269 free(stmts
->let
.list
);
1270 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1271 free_return_stmt(stmts
->ret
.list
[i
]);
1273 free(stmts
->ret
.list
);
1274 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1275 free_set_stmt(stmts
->set
.list
[i
]);
1277 free(stmts
->set
.list
);
1278 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1279 free_free_stmt(stmts
->fre
.list
[i
]);
1281 free(stmts
->fre
.list
);
1285 typedef struct impl
{
1291 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1292 impl
*i
= calloc(1, sizeof(*i
));
1298 static inline void free_impl(impl
*impl
) {
1299 free_impl_func(impl
->func
);
1300 free_impl_stmts(impl
->stmts
);
1304 typedef struct impls
{
1309 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1311 impls
= calloc(1, sizeof(*impls
));
1313 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1314 impls
->list
[impls
->count
-1] = impl
;
1318 static void free_impls(impls
*impls
) {
1321 for (i
= 0; i
< impls
->count
; ++i
) {
1322 free_impl(impls
->list
[i
]);
1328 typedef struct decl_file
{
1333 static inline void free_decl_file(decl_file
*file
) {
1340 memset(file
, 0, sizeof(*file
));
1343 typedef struct decl_libs
{
1348 static inline void free_decl_libs(decl_libs
*libs
) {
1351 for (i
= 0; i
< libs
->count
; ++i
) {
1353 dlclose(libs
->dl
[i
]);
1358 memset(libs
, 0, sizeof(*libs
));
1361 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1362 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1363 libs
->dl
[libs
->count
-1] = dlopened
;
1366 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1369 ZEND_ASSERT(var
->arg
->var
!= var
);
1371 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1372 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1373 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1375 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1377 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1379 ret_val
= *(void **) ret_val
;
1384 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1385 impl_val
*val
, *val_ptr
;
1388 ZEND_ASSERT(var
->arg
->var
== var
);
1390 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1391 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1392 var
->array_size
, var
->arg
->var
->array_size
);
1394 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1398 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1399 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1401 fprintf(stderr
, "++\n");
1403 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1404 val_ptr
= val_ptr
->ptr
;
1410 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1411 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1413 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1419 #define PSI_ERROR 16
1420 #define PSI_WARNING 32
1421 typedef void (*psi_error_cb
)(void *context
, PSI_Token
*token
, int type
, const char *msg
, ...);
1423 #define PSI_DATA(D) ((PSI_Data *) (D))
1424 #define PSI_DATA_MEMBERS \
1425 constants *consts; \
1426 decl_typedefs *defs; \
1427 decl_structs *structs; \
1428 decl_unions *unions; \
1429 decl_enums *enums; \
1436 psi_error_cb error; \
1439 typedef struct PSI_Data
{
1443 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1445 dest
= malloc(sizeof(*dest
));
1447 memcpy(dest
, src
, sizeof(*dest
));
1448 memset(src
, 0, sizeof(*src
));
1452 static inline void PSI_DataDtor(PSI_Data
*data
) {
1454 free_constants(data
->consts
);
1457 free_decl_typedefs(data
->defs
);
1459 if (data
->structs
) {
1460 free_decl_structs(data
->structs
);
1463 free_decl_unions(data
->unions
);
1466 free_decl_enums(data
->enums
);
1469 free_decls(data
->decls
);
1472 free_impls(data
->impls
);
1474 free_decl_file(&data
->psi
.file
);
1477 typedef struct PSI_Parser
{
1483 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1486 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1487 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1490 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1492 size_t token_len
, fname_len
;
1495 if (P
->cur
< P
->tok
) {
1500 token_len
= P
->cur
- P
->tok
;
1501 fname_len
= strlen(P
->psi
.file
.fn
);
1503 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1504 T
->type
= token_typ
;
1505 T
->size
= token_len
;
1506 T
->text
= &T
->buf
[0];
1507 T
->file
= &T
->buf
[token_len
+ 1];
1511 memcpy(T
->text
, P
->tok
, token_len
);
1512 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1517 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1518 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1519 PSI_Token
*ptr
= malloc(strct_len
);
1521 memcpy(ptr
, src
, strct_len
);
1523 ptr
->text
= &ptr
->buf
[0];
1524 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1529 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1532 PSI_Token
*T
= NULL
;
1534 va_start(argv
, argc
);
1535 for (i
= 0; i
< argc
; ++i
) {
1536 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1539 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1541 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1542 T
->text
= &T
->buf
[0];
1543 T
->file
= &T
->buf
[T
->size
+ 1];
1544 T
->buf
[token_len
] = ' ';
1545 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1546 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1548 T
= PSI_TokenCopy(arg
);
1549 T
->type
= PSI_T_NAME
;
1557 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1561 va_start(argv
, argc
);
1562 for (i
= 0; i
< argc
; ++i
) {
1563 char *str
= va_arg(argv
, char *);
1564 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1566 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1567 T
->text
= &T
->buf
[0];
1568 T
->file
= &T
->buf
[T
->size
+ 1];
1569 T
->buf
[token_len
] = ' ';
1570 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1571 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1578 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
1579 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1580 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1584 static inline uint64_t psi_hash(char *digest_buf
, ...)
1586 uint64_t hash
= 5381;
1591 va_start(argv
, digest_buf
);
1592 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1593 while ((c
= *ptr
++)) {
1594 hash
= ((hash
<< 5) + hash
) + c
;
1600 sprintf(digest_buf
, "%" PRIx64
, hash
);
1606 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1609 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1610 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1613 #define PSI_PARSER_DEBUG 0x1
1614 #define PSI_PARSER_SILENT 0x2
1616 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1617 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1618 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1619 token_t
PSI_ParserScan(PSI_Parser
*P
);
1620 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1621 void PSI_ParserDtor(PSI_Parser
*P
);
1622 void PSI_ParserFree(PSI_Parser
**P
);