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_union
*unn
;
66 struct decl_enum
*enm
;
70 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
71 decl_type
*t
= calloc(1, sizeof(*t
));
73 t
->name
= strdup(name
);
77 static inline decl_type
*real_decl_type(decl_type
*type
) {
84 static inline void free_decl(struct decl
*decl
);
85 static inline void free_decl_type(decl_type
*type
) {
89 if (type
->type
== PSI_T_FUNCTION
) {
90 free_decl(type
->func
);
96 typedef struct decl_var
{
99 unsigned pointer_level
;
101 struct decl_arg
*arg
;
104 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
105 decl_var
*v
= calloc(1, sizeof(*v
));
106 v
->name
= (char *) strdup((const char *) name
);
107 v
->pointer_level
= pl
;
112 static inline decl_var
*copy_decl_var(decl_var
*src
) {
113 decl_var
*dest
= calloc(1, sizeof(*dest
));
115 memcpy(dest
, src
, sizeof(*dest
));
116 dest
->name
= strdup(dest
->name
);
118 dest
->token
= PSI_TokenCopy(dest
->token
);
123 static inline void free_decl_var(decl_var
*var
) {
131 typedef struct decl_struct_layout
{
134 } decl_struct_layout
;
136 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
137 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
138 ZEND_ASSERT(pos
+len
);
144 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
148 typedef struct decl_arg
{
152 decl_struct_layout
*layout
;
153 struct let_stmt
*let
;
159 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
160 decl_arg
*arg
= calloc(1, sizeof(*arg
));
161 arg
->token
= var
->token
;
165 arg
->ptr
= &arg
->val
;
169 static inline void free_decl_arg(decl_arg
*arg
) {
170 if (arg
->token
&& arg
->token
!= arg
->var
->token
) {
173 free_decl_type(arg
->type
);
174 free_decl_var(arg
->var
);
176 free_decl_struct_layout(arg
->layout
);
181 typedef struct decl_typedefs
{
186 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_arg
*def
) {
188 defs
= calloc(1, sizeof(*defs
));
190 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
191 defs
->list
[defs
->count
-1] = def
;
195 static void free_decl_typedefs(decl_typedefs
*defs
) {
198 for (i
= 0; i
< defs
->count
; ++i
) {
199 free_decl_arg(defs
->list
[i
]);
205 typedef struct decl_vars
{
210 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
211 decl_vars
*vars
= calloc(1, sizeof(*vars
));
214 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
220 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
221 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
222 vars
->vars
[vars
->count
-1] = var
;
226 static inline void free_decl_vars(decl_vars
*vars
) {
229 for (i
= 0; i
< vars
->count
; ++i
) {
230 free_decl_var(vars
->vars
[i
]);
236 typedef struct decl_args
{
242 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
243 decl_args
*args
= calloc(1, sizeof(*args
));
246 args
->args
= calloc(1, sizeof(*args
->args
));
252 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
253 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
254 args
->args
[args
->count
-1] = arg
;
258 static inline void free_decl_args(decl_args
*args
) {
261 for (i
= 0; i
< args
->count
; ++i
) {
262 free_decl_arg(args
->args
[i
]);
268 typedef struct decl_abi
{
273 static inline decl_abi
*init_decl_abi(const char *convention
) {
274 decl_abi
*abi
= calloc(1, sizeof(*abi
));
275 abi
->convention
= strdup(convention
);
279 static inline void free_decl_abi(decl_abi
*abi
) {
283 free(abi
->convention
);
287 typedef struct decl_callinfo
{
295 typedef struct decl
{
303 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
304 decl
*d
= calloc(1, sizeof(*d
));
311 static inline void free_decl(decl
*d
) {
312 free_decl_abi(d
->abi
);
313 free_decl_arg(d
->func
);
315 free_decl_args(d
->args
);
320 typedef struct decls
{
325 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
327 decls
= calloc(1, sizeof(*decls
));
329 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
330 decls
->list
[decls
->count
-1] = decl
;
334 static inline void free_decls(decls
*decls
) {
337 for (i
= 0; i
< decls
->count
; ++i
) {
338 free_decl(decls
->list
[i
]);
345 typedef struct decl_struct
{
353 void (*dtor
)(void *type
);
357 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
358 decl_struct
*s
= calloc(1, sizeof(*s
));
359 s
->name
= strdup(name
);
364 static inline void free_decl_struct(decl_struct
*s
) {
369 free_decl_args(s
->args
);
371 if (s
->engine
.type
&& s
->engine
.dtor
) {
372 s
->engine
.dtor(s
->engine
.type
);
378 typedef struct decl_structs
{
383 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
385 ss
= calloc(1, sizeof(*ss
));
387 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
388 ss
->list
[ss
->count
-1] = s
;
392 static inline void free_decl_structs(decl_structs
*ss
) {
395 for (i
= 0; i
< ss
->count
; ++i
) {
396 free_decl_struct(ss
->list
[i
]);
402 typedef struct decl_union
{
410 static inline decl_union
*init_decl_union(const char *name
, decl_args
*args
) {
411 decl_union
*u
= calloc(1, sizeof(*u
));
412 u
->name
= strdup(name
);
417 static inline void free_decl_union(decl_union
*u
) {
422 free_decl_args(u
->args
);
428 typedef struct decl_unions
{
433 static inline decl_unions
*add_decl_union(decl_unions
*uu
, decl_union
*u
) {
435 uu
= calloc(1, sizeof(*uu
));
437 uu
->list
= realloc(uu
->list
, ++uu
->count
* sizeof(*uu
->list
));
438 uu
->list
[uu
->count
-1] = u
;
442 static inline void free_decl_unions(decl_unions
*uu
) {
445 for (i
= 0; i
< uu
->count
; ++i
) {
446 free_decl_union(uu
->list
[i
]);
452 typedef struct impl_type
{
457 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
458 impl_type
*t
= calloc(1, sizeof(*t
));
461 t
->name
= strdup(name
);
465 static inline void free_impl_type(impl_type
*type
) {
470 typedef struct impl_var
{
473 unsigned reference
:1;
476 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
477 impl_var
*var
= calloc(1, sizeof(*var
));
478 var
->name
= strdup(name
);
479 var
->reference
= is_reference
;
483 static inline void free_impl_var(impl_var
*var
) {
491 typedef struct impl_def_val
{
496 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
497 impl_def_val
*def
= calloc(1, sizeof(*def
));
499 def
->text
= strdup(text
);
503 static inline void free_impl_def_val(impl_def_val
*def
) {
508 typedef struct const_type
{
513 static inline const_type
*init_const_type(token_t type
, const char *name
) {
514 const_type
*ct
= calloc(1, sizeof(*ct
));
516 ct
->name
= strdup(name
);
520 static inline void free_const_type(const_type
*type
) {
525 typedef struct constant
{
531 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
532 constant
*c
= calloc(1, sizeof(*c
));
534 c
->name
= strdup(name
);
539 static inline void free_constant(constant
*constant
) {
540 free_const_type(constant
->type
);
541 free(constant
->name
);
542 free_impl_def_val(constant
->val
);
546 typedef struct constants
{
551 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
553 constants
= calloc(1, sizeof(*constants
));
555 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
556 constants
->list
[constants
->count
-1] = constant
;
560 static inline void free_constants(constants
*c
) {
563 for (i
= 0; i
< c
->count
; ++i
) {
564 free_constant(c
->list
[i
]);
570 typedef struct impl_arg
{
578 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
579 impl_arg
*arg
= calloc(1, sizeof(*arg
));
586 static inline void free_impl_arg(impl_arg
*arg
) {
587 free_impl_type(arg
->type
);
588 free_impl_var(arg
->var
);
590 free_impl_def_val(arg
->def
);
595 typedef struct impl_vararg
{
597 struct impl_args
*args
;
603 typedef struct impl_args
{
609 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
610 impl_args
*args
= calloc(1, sizeof(*args
));
613 args
->args
= calloc(1, sizeof(*args
->args
));
619 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
620 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
621 args
->args
[args
->count
-1] = arg
;
625 static inline void free_impl_args(impl_args
*args
) {
628 for (i
= 0; i
< args
->count
; ++i
) {
629 free_impl_arg(args
->args
[i
]);
631 if (args
->vararg
.name
) {
632 free_impl_arg(args
->vararg
.name
);
638 typedef struct impl_func
{
642 impl_type
*return_type
;
643 unsigned return_reference
:1;
646 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
647 impl_func
*func
= calloc(1, sizeof(*func
));
648 func
->name
= strdup(name
);
649 func
->args
= args
? args
: init_impl_args(NULL
);
650 func
->return_type
= type
;
651 func
->return_reference
= ret_reference
;
655 static inline void free_impl_func(impl_func
*f
) {
659 free_impl_type(f
->return_type
);
660 free_impl_args(f
->args
);
665 typedef struct num_exp
{
672 struct decl_enum_item
*enm
;
675 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
676 struct num_exp
*operand
;
679 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
680 num_exp
*exp
= calloc(1, sizeof(*exp
));
681 switch (exp
->t
= t
) {
684 exp
->u
.numb
= strdup(num
);
689 EMPTY_SWITCH_DEFAULT_CASE();
694 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
696 num_exp
*num
= calloc(1, sizeof(*num
));
698 memcpy(num
, exp
, sizeof(*num
));
701 num
->token
= PSI_TokenCopy(num
->token
);
704 num
->operand
= copy_num_exp(num
->operand
);
709 num
->u
.numb
= strdup(num
->u
.numb
);
712 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
713 dvar
->arg
= num
->u
.dvar
->arg
;
714 if (num
->u
.dvar
->token
) {
715 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
723 static inline void free_num_exp(num_exp
*exp
) {
734 free_decl_var(exp
->u
.dvar
);
738 EMPTY_SWITCH_DEFAULT_CASE();
741 free_num_exp(exp
->operand
);
746 typedef struct decl_enum_item
{
751 struct decl_enum_item
*prev
;
754 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
755 decl_enum_item
*i
= calloc(1, sizeof(*i
));
757 i
->name
= strdup(name
);
762 static inline void free_decl_enum_item(decl_enum_item
*i
) {
766 if (i
->num
&& i
->num
!= &i
->inc
) {
767 free_num_exp(i
->num
);
773 typedef struct decl_enum_items
{
774 decl_enum_item
**list
;
778 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
779 decl_enum_items
*l
= calloc(1, sizeof(*l
));
783 l
->list
= calloc(1, sizeof(*l
->list
));
789 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
790 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
791 l
->list
[l
->count
] = i
;
793 i
->prev
= l
->list
[l
->count
- 1];
799 static inline void free_decl_enum_items(decl_enum_items
*l
) {
803 for (j
= 0; j
< l
->count
; ++j
) {
804 free_decl_enum_item(l
->list
[j
]);
811 typedef struct decl_enum
{
814 decl_enum_items
*items
;
817 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
818 decl_enum
*e
= calloc(1, sizeof(*e
));
820 e
->name
= strdup(name
);
825 static inline void free_decl_enum(decl_enum
*e
) {
830 free_decl_enum_items(e
->items
);
836 typedef struct decl_enums
{
841 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
843 es
= calloc(1, sizeof(*es
));
845 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
846 es
->list
[es
->count
-1] = e
;
850 static inline void free_decl_enums(decl_enums
*es
) {
854 for (j
= 0; j
< es
->count
; ++j
) {
855 free_decl_enum(es
->list
[j
]);
862 typedef struct let_calloc
{
867 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
868 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
869 alloc
->nmemb
= nmemb
;
874 static inline void free_let_calloc(let_calloc
*alloc
) {
875 free_num_exp(alloc
->nmemb
);
876 free_num_exp(alloc
->size
);
880 typedef struct let_func
{
887 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
888 let_func
*func
= calloc(1, sizeof(*func
));
890 func
->name
= strdup(name
);
895 static inline void free_let_func(let_func
*func
) {
896 free_impl_var(func
->var
);
901 #define PSI_LET_REFERENCE 0x1;
902 typedef struct let_val
{
918 unsigned is_reference
:1;
924 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
925 let_val
*let
= calloc(1, sizeof(*let
));
926 switch (let
->kind
= kind
) {
930 let
->data
.num
= data
;
933 let
->data
.alloc
= data
;
936 let
->data
.func
= data
;
939 let
->data
.var
= data
;
941 EMPTY_SWITCH_DEFAULT_CASE();
946 static inline void free_let_val(let_val
*let
) {
951 free_num_exp(let
->data
.num
);
954 free_let_calloc(let
->data
.alloc
);
957 free_let_func(let
->data
.func
);
960 free_decl_var(let
->data
.var
);
962 EMPTY_SWITCH_DEFAULT_CASE();
967 typedef struct let_stmt
{
974 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
975 let_stmt
*let
= calloc(1, sizeof(*let
));
981 static inline void free_let_stmt(let_stmt
*stmt
) {
983 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
984 free_decl_arg(stmt
->var
->arg
);
986 free_let_val(stmt
->val
);
988 free_decl_var(stmt
->var
);
994 typedef struct set_func
{
998 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
1001 static inline set_func
*init_set_func(token_t type
, const char *name
) {
1002 set_func
*func
= calloc(1, sizeof(*func
));
1004 func
->name
= strdup(name
);
1008 static inline void free_set_func(set_func
*func
) {
1016 typedef struct set_value
{
1021 struct set_value
*set
;
1024 struct set_value
**inner
;
1028 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
1029 set_value
*val
= calloc(1, sizeof(*val
));
1034 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
1035 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
1036 val
->inner
[val
->count
-1] = inner
;
1037 inner
->outer
.set
= val
;
1041 static inline void free_set_value(set_value
*val
) {
1043 free_set_func(val
->func
);
1046 free_decl_vars(val
->vars
);
1048 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1050 for (i
= 0; i
< val
->count
; ++i
) {
1051 free_set_value(val
->inner
[i
]);
1056 free_num_exp(val
->num
);
1061 typedef struct set_stmt
{
1067 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1068 set_stmt
*set
= calloc(1, sizeof(*set
));
1074 static inline void free_set_stmt(set_stmt
*set
) {
1075 free_impl_var(set
->var
);
1076 free_set_value(set
->val
);
1080 typedef struct return_stmt
{
1086 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1087 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1092 static inline void free_return_stmt(return_stmt
*ret
) {
1096 free_set_value(ret
->set
);
1100 typedef struct free_call
{
1107 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1108 free_call
*f
= calloc(1, sizeof(*f
));
1109 f
->func
= strdup(func
);
1114 static inline void free_free_call(free_call
*f
) {
1119 free_decl_vars(f
->vars
);
1123 typedef struct free_calls
{
1128 static inline free_calls
*init_free_calls(free_call
*f
) {
1129 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1132 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1138 static inline void free_free_calls(free_calls
*fcs
) {
1141 for (i
= 0; i
< fcs
->count
; ++i
) {
1142 free_free_call(fcs
->list
[i
]);
1148 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1149 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1150 fcs
->list
[fcs
->count
-1] = f
;
1154 typedef struct free_stmt
{
1158 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1159 free_stmt
*f
= calloc(1, sizeof(*f
));
1164 static inline void free_free_stmt(free_stmt
*f
) {
1165 free_free_calls(f
->calls
);
1169 typedef struct impl_stmt
{
1180 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1181 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1187 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1188 switch (stmt
->type
) {
1190 free_let_stmt(stmt
->s
.let
);
1193 free_set_stmt(stmt
->s
.set
);
1196 free_return_stmt(stmt
->s
.ret
);
1199 free_free_stmt(stmt
->s
.fre
);
1205 typedef struct impl_stmts
{
1224 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1225 list
= realloc(list
, count
* sizeof(list
));
1226 ((void **)list
)[count
-1] = stmt
;
1230 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1231 switch (stmt
->type
) {
1233 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1236 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1239 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1242 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1249 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1250 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1251 return add_impl_stmt(stmts
, stmt
);
1254 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1257 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1258 free_let_stmt(stmts
->let
.list
[i
]);
1260 free(stmts
->let
.list
);
1261 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1262 free_return_stmt(stmts
->ret
.list
[i
]);
1264 free(stmts
->ret
.list
);
1265 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1266 free_set_stmt(stmts
->set
.list
[i
]);
1268 free(stmts
->set
.list
);
1269 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1270 free_free_stmt(stmts
->fre
.list
[i
]);
1272 free(stmts
->fre
.list
);
1276 typedef struct impl
{
1282 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1283 impl
*i
= calloc(1, sizeof(*i
));
1289 static inline void free_impl(impl
*impl
) {
1290 free_impl_func(impl
->func
);
1291 free_impl_stmts(impl
->stmts
);
1295 typedef struct impls
{
1300 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1302 impls
= calloc(1, sizeof(*impls
));
1304 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1305 impls
->list
[impls
->count
-1] = impl
;
1309 static void free_impls(impls
*impls
) {
1312 for (i
= 0; i
< impls
->count
; ++i
) {
1313 free_impl(impls
->list
[i
]);
1319 typedef struct decl_file
{
1324 static inline void free_decl_file(decl_file
*file
) {
1331 memset(file
, 0, sizeof(*file
));
1334 typedef struct decl_libs
{
1339 static inline void free_decl_libs(decl_libs
*libs
) {
1342 for (i
= 0; i
< libs
->count
; ++i
) {
1344 dlclose(libs
->dl
[i
]);
1349 memset(libs
, 0, sizeof(*libs
));
1352 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1353 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1354 libs
->dl
[libs
->count
-1] = dlopened
;
1357 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1360 ZEND_ASSERT(var
->arg
->var
!= var
);
1362 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1363 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1364 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1366 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1368 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1370 ret_val
= *(void **) ret_val
;
1375 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1376 impl_val
*val
, *val_ptr
;
1379 ZEND_ASSERT(var
->arg
->var
== var
);
1381 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1382 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1383 var
->array_size
, var
->arg
->var
->array_size
);
1385 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1389 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1390 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1392 fprintf(stderr
, "++\n");
1394 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1395 val_ptr
= val_ptr
->ptr
;
1401 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1402 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1404 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1410 #define PSI_ERROR 16
1411 #define PSI_WARNING 32
1412 typedef void (*psi_error_cb
)(void *context
, PSI_Token
*token
, int type
, const char *msg
, ...);
1414 #define PSI_DATA(D) ((PSI_Data *) (D))
1415 #define PSI_DATA_MEMBERS \
1416 constants *consts; \
1417 decl_typedefs *defs; \
1418 decl_structs *structs; \
1419 decl_unions *unions; \
1420 decl_enums *enums; \
1427 psi_error_cb error; \
1430 typedef struct PSI_Data
{
1434 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1436 dest
= malloc(sizeof(*dest
));
1438 memcpy(dest
, src
, sizeof(*dest
));
1439 memset(src
, 0, sizeof(*src
));
1443 static inline void PSI_DataDtor(PSI_Data
*data
) {
1445 free_constants(data
->consts
);
1448 free_decl_typedefs(data
->defs
);
1450 if (data
->structs
) {
1451 free_decl_structs(data
->structs
);
1454 free_decl_unions(data
->unions
);
1457 free_decl_enums(data
->enums
);
1460 free_decls(data
->decls
);
1463 free_impls(data
->impls
);
1465 free_decl_file(&data
->psi
.file
);
1468 typedef struct PSI_Parser
{
1474 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1477 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1478 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1481 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1483 size_t token_len
, fname_len
;
1486 if (P
->cur
< P
->tok
) {
1491 token_len
= P
->cur
- P
->tok
;
1492 fname_len
= strlen(P
->psi
.file
.fn
);
1494 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1495 T
->type
= token_typ
;
1496 T
->size
= token_len
;
1497 T
->text
= &T
->buf
[0];
1498 T
->file
= &T
->buf
[token_len
+ 1];
1502 memcpy(T
->text
, P
->tok
, token_len
);
1503 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1508 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1509 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1510 PSI_Token
*ptr
= malloc(strct_len
);
1512 memcpy(ptr
, src
, strct_len
);
1514 ptr
->text
= &ptr
->buf
[0];
1515 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1520 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1523 PSI_Token
*T
= NULL
;
1525 va_start(argv
, argc
);
1526 for (i
= 0; i
< argc
; ++i
) {
1527 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1530 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1532 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1533 T
->text
= &T
->buf
[0];
1534 T
->file
= &T
->buf
[T
->size
+ 1];
1535 T
->buf
[token_len
] = ' ';
1536 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1537 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1539 T
= PSI_TokenCopy(arg
);
1540 T
->type
= PSI_T_NAME
;
1548 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1552 va_start(argv
, argc
);
1553 for (i
= 0; i
< argc
; ++i
) {
1554 char *str
= va_arg(argv
, char *);
1555 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1557 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1558 T
->text
= &T
->buf
[0];
1559 T
->file
= &T
->buf
[T
->size
+ 1];
1560 T
->buf
[token_len
] = ' ';
1561 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1562 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1569 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
1570 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1571 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1575 static inline uint64_t psi_hash(char *digest_buf
, ...)
1577 uint64_t hash
= 5381;
1582 va_start(argv
, digest_buf
);
1583 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1584 while ((c
= *ptr
++)) {
1585 hash
= ((hash
<< 5) + hash
) + c
;
1591 sprintf(digest_buf
, "%" PRIx64
, hash
);
1597 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1600 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1601 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1604 #define PSI_PARSER_DEBUG 0x1
1605 #define PSI_PARSER_SILENT 0x2
1607 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1608 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1609 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1610 token_t
PSI_ParserScan(PSI_Parser
*P
);
1611 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1612 void PSI_ParserDtor(PSI_Parser
*P
);
1613 void PSI_ParserFree(PSI_Parser
**P
);