daf99dc8eda359cc02e2a1d0fba8ae75bbee7ced
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
{
30 typedef union impl_val
{
45 #ifdef HAVE_LONG_DOUBLE
54 uint8_t _dbg
[sizeof(void *)];
57 typedef struct decl_type
{
61 struct decl_type
*real
;
62 struct decl_struct
*strct
;
63 struct decl_enum
*enm
;
66 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
67 decl_type
*t
= calloc(1, sizeof(*t
));
69 t
->name
= strdup(name
);
73 static inline decl_type
*real_decl_type(decl_type
*type
) {
80 static inline void free_decl_type(decl_type
*type
) {
88 typedef struct decl_typedef
{
94 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
95 decl_typedef
*t
= calloc(1, sizeof(*t
));
96 t
->alias
= strdup(name
);
101 static inline void free_decl_typedef(decl_typedef
*t
) {
106 free_decl_type(t
->type
);
110 typedef struct decl_typedefs
{
115 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
117 defs
= calloc(1, sizeof(*defs
));
119 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
120 defs
->list
[defs
->count
-1] = def
;
124 static void free_decl_typedefs(decl_typedefs
*defs
) {
127 for (i
= 0; i
< defs
->count
; ++i
) {
128 free_decl_typedef(defs
->list
[i
]);
134 typedef struct decl_var
{
137 unsigned pointer_level
;
139 struct decl_arg
*arg
;
142 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
143 decl_var
*v
= calloc(1, sizeof(*v
));
144 v
->name
= (char *) strdup((const char *) name
);
145 v
->pointer_level
= pl
;
150 static inline void free_decl_var(decl_var
*var
) {
158 typedef struct decl_struct_layout
{
161 } decl_struct_layout
;
163 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
164 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
171 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
175 typedef struct decl_arg
{
179 decl_struct_layout
*layout
;
180 struct let_stmt
*let
;
186 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
187 decl_arg
*arg
= calloc(1, sizeof(*arg
));
188 arg
->token
= var
->token
;
192 arg
->ptr
= &arg
->val
;
196 static inline void free_decl_arg(decl_arg
*arg
) {
197 free_decl_type(arg
->type
);
198 free_decl_var(arg
->var
);
200 free_decl_struct_layout(arg
->layout
);
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
{
352 void (*dtor
)(void *type
);
356 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
357 decl_struct
*s
= calloc(1, sizeof(*s
));
358 s
->name
= strdup(name
);
363 static inline void free_decl_struct(decl_struct
*s
) {
368 free_decl_args(s
->args
);
370 if (s
->engine
.type
&& s
->engine
.dtor
) {
371 s
->engine
.dtor(s
->engine
.type
);
377 typedef struct decl_structs
{
382 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
384 ss
= calloc(1, sizeof(*ss
));
386 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
387 ss
->list
[ss
->count
-1] = s
;
391 static inline void free_decl_structs(decl_structs
*ss
) {
394 for (i
= 0; i
< ss
->count
; ++i
) {
395 free_decl_struct(ss
->list
[i
]);
401 typedef struct impl_type
{
406 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
407 impl_type
*t
= calloc(1, sizeof(*t
));
410 t
->name
= strdup(name
);
414 static inline void free_impl_type(impl_type
*type
) {
419 typedef struct impl_var
{
422 unsigned reference
:1;
425 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
426 impl_var
*var
= calloc(1, sizeof(*var
));
427 var
->name
= strdup(name
);
428 var
->reference
= is_reference
;
432 static inline void free_impl_var(impl_var
*var
) {
440 typedef struct impl_def_val
{
445 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
446 impl_def_val
*def
= calloc(1, sizeof(*def
));
448 def
->text
= strdup(text
);
452 static inline void free_impl_def_val(impl_def_val
*def
) {
457 typedef struct const_type
{
462 static inline const_type
*init_const_type(token_t type
, const char *name
) {
463 const_type
*ct
= calloc(1, sizeof(*ct
));
465 ct
->name
= strdup(name
);
469 static inline void free_const_type(const_type
*type
) {
474 typedef struct constant
{
480 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
481 constant
*c
= calloc(1, sizeof(*c
));
483 c
->name
= strdup(name
);
488 static inline void free_constant(constant
*constant
) {
489 free_const_type(constant
->type
);
490 free(constant
->name
);
491 free_impl_def_val(constant
->val
);
495 typedef struct constants
{
500 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
502 constants
= calloc(1, sizeof(*constants
));
504 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
505 constants
->list
[constants
->count
-1] = constant
;
509 static inline void free_constants(constants
*c
) {
512 for (i
= 0; i
< c
->count
; ++i
) {
513 free_constant(c
->list
[i
]);
519 typedef struct impl_arg
{
527 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
528 impl_arg
*arg
= calloc(1, sizeof(*arg
));
535 static inline void free_impl_arg(impl_arg
*arg
) {
536 free_impl_type(arg
->type
);
537 free_impl_var(arg
->var
);
539 free_impl_def_val(arg
->def
);
544 typedef struct impl_vararg
{
546 struct impl_args
*args
;
552 typedef struct impl_args
{
558 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
559 impl_args
*args
= calloc(1, sizeof(*args
));
562 args
->args
= calloc(1, sizeof(*args
->args
));
568 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
569 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
570 args
->args
[args
->count
-1] = arg
;
574 static inline void free_impl_args(impl_args
*args
) {
577 for (i
= 0; i
< args
->count
; ++i
) {
578 free_impl_arg(args
->args
[i
]);
580 if (args
->vararg
.name
) {
581 free_impl_arg(args
->vararg
.name
);
587 typedef struct impl_func
{
591 impl_type
*return_type
;
592 unsigned return_reference
:1;
595 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
596 impl_func
*func
= calloc(1, sizeof(*func
));
597 func
->name
= strdup(name
);
598 func
->args
= args
? args
: init_impl_args(NULL
);
599 func
->return_type
= type
;
600 func
->return_reference
= ret_reference
;
604 static inline void free_impl_func(impl_func
*f
) {
608 free_impl_type(f
->return_type
);
609 free_impl_args(f
->args
);
614 typedef struct num_exp
{
621 struct decl_enum_item
*enm
;
624 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
625 struct num_exp
*operand
;
628 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
629 num_exp
*exp
= calloc(1, sizeof(*exp
));
630 switch (exp
->t
= t
) {
633 exp
->u
.numb
= strdup(num
);
638 EMPTY_SWITCH_DEFAULT_CASE();
643 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
);
644 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
646 num_exp
*num
= calloc(1, sizeof(*num
));
648 memcpy(num
, exp
, sizeof(*num
));
651 num
->token
= PSI_TokenCopy(num
->token
);
654 num
->operand
= copy_num_exp(num
->operand
);
659 num
->u
.numb
= strdup(num
->u
.numb
);
662 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
663 dvar
->arg
= num
->u
.dvar
->arg
;
664 if (num
->u
.dvar
->token
) {
665 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
673 static inline void free_num_exp(num_exp
*exp
) {
684 free_decl_var(exp
->u
.dvar
);
688 EMPTY_SWITCH_DEFAULT_CASE();
691 free_num_exp(exp
->operand
);
696 typedef struct decl_enum_item
{
701 struct decl_enum_item
*prev
;
704 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
705 decl_enum_item
*i
= calloc(1, sizeof(*i
));
707 i
->name
= strdup(name
);
712 static inline void free_decl_enum_item(decl_enum_item
*i
) {
716 if (i
->num
&& i
->num
!= &i
->inc
) {
717 free_num_exp(i
->num
);
723 typedef struct decl_enum_items
{
724 decl_enum_item
**list
;
728 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
729 decl_enum_items
*l
= calloc(1, sizeof(*l
));
733 l
->list
= calloc(1, sizeof(*l
->list
));
739 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
740 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
741 l
->list
[l
->count
] = i
;
743 i
->prev
= l
->list
[l
->count
- 1];
749 static inline void free_decl_enum_items(decl_enum_items
*l
) {
753 for (j
= 0; j
< l
->count
; ++j
) {
754 free_decl_enum_item(l
->list
[j
]);
761 typedef struct decl_enum
{
764 decl_enum_items
*items
;
767 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
768 decl_enum
*e
= calloc(1, sizeof(*e
));
770 e
->name
= strdup(name
);
775 static inline void free_decl_enum(decl_enum
*e
) {
780 free_decl_enum_items(e
->items
);
786 typedef struct decl_enums
{
791 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
793 es
= calloc(1, sizeof(*es
));
795 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
796 es
->list
[es
->count
-1] = e
;
800 static inline void free_decl_enums(decl_enums
*es
) {
804 for (j
= 0; j
< es
->count
; ++j
) {
805 free_decl_enum(es
->list
[j
]);
812 typedef struct let_calloc
{
817 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
818 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
819 alloc
->nmemb
= nmemb
;
824 static inline void free_let_calloc(let_calloc
*alloc
) {
825 free_num_exp(alloc
->nmemb
);
826 free_num_exp(alloc
->size
);
830 typedef struct let_func
{
837 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
838 let_func
*func
= calloc(1, sizeof(*func
));
840 func
->name
= strdup(name
);
845 static inline void free_let_func(let_func
*func
) {
846 free_impl_var(func
->var
);
851 #define PSI_LET_REFERENCE 0x1;
852 typedef struct let_val
{
868 unsigned is_reference
:1;
874 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
875 let_val
*let
= calloc(1, sizeof(*let
));
876 switch (let
->kind
= kind
) {
880 let
->data
.num
= data
;
883 let
->data
.alloc
= data
;
886 let
->data
.func
= data
;
889 let
->data
.var
= data
;
891 EMPTY_SWITCH_DEFAULT_CASE();
896 static inline void free_let_val(let_val
*let
) {
901 free_num_exp(let
->data
.num
);
904 free_let_calloc(let
->data
.alloc
);
907 free_let_func(let
->data
.func
);
910 free_decl_var(let
->data
.var
);
912 EMPTY_SWITCH_DEFAULT_CASE();
917 typedef struct let_stmt
{
924 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
925 let_stmt
*let
= calloc(1, sizeof(*let
));
931 static inline void free_let_stmt(let_stmt
*stmt
) {
933 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
934 free_decl_arg(stmt
->var
->arg
);
936 free_let_val(stmt
->val
);
938 free_decl_var(stmt
->var
);
944 typedef struct set_func
{
948 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
951 static inline set_func
*init_set_func(token_t type
, const char *name
) {
952 set_func
*func
= calloc(1, sizeof(*func
));
954 func
->name
= strdup(name
);
958 static inline void free_set_func(set_func
*func
) {
966 typedef struct set_value
{
971 struct set_value
*set
;
974 struct set_value
**inner
;
978 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
979 set_value
*val
= calloc(1, sizeof(*val
));
984 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
985 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
986 val
->inner
[val
->count
-1] = inner
;
987 inner
->outer
.set
= val
;
991 static inline void free_set_value(set_value
*val
) {
993 free_set_func(val
->func
);
996 free_decl_vars(val
->vars
);
998 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1000 for (i
= 0; i
< val
->count
; ++i
) {
1001 free_set_value(val
->inner
[i
]);
1006 free_num_exp(val
->num
);
1011 typedef struct set_stmt
{
1017 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1018 set_stmt
*set
= calloc(1, sizeof(*set
));
1024 static inline void free_set_stmt(set_stmt
*set
) {
1025 free_impl_var(set
->var
);
1026 free_set_value(set
->val
);
1030 typedef struct return_stmt
{
1036 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1037 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1042 static inline void free_return_stmt(return_stmt
*ret
) {
1046 free_set_value(ret
->set
);
1050 typedef struct free_call
{
1057 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1058 free_call
*f
= calloc(1, sizeof(*f
));
1059 f
->func
= strdup(func
);
1064 static inline void free_free_call(free_call
*f
) {
1069 free_decl_vars(f
->vars
);
1073 typedef struct free_calls
{
1078 static inline free_calls
*init_free_calls(free_call
*f
) {
1079 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1082 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1088 static inline void free_free_calls(free_calls
*fcs
) {
1091 for (i
= 0; i
< fcs
->count
; ++i
) {
1092 free_free_call(fcs
->list
[i
]);
1098 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1099 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1100 fcs
->list
[fcs
->count
-1] = f
;
1104 typedef struct free_stmt
{
1108 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1109 free_stmt
*f
= calloc(1, sizeof(*f
));
1114 static inline void free_free_stmt(free_stmt
*f
) {
1115 free_free_calls(f
->calls
);
1119 typedef struct impl_stmt
{
1130 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1131 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1137 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1138 switch (stmt
->type
) {
1140 free_let_stmt(stmt
->s
.let
);
1143 free_set_stmt(stmt
->s
.set
);
1146 free_return_stmt(stmt
->s
.ret
);
1149 free_free_stmt(stmt
->s
.fre
);
1155 typedef struct impl_stmts
{
1174 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1175 list
= realloc(list
, count
* sizeof(list
));
1176 ((void **)list
)[count
-1] = stmt
;
1180 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1181 switch (stmt
->type
) {
1183 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1186 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1189 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1192 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1199 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1200 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1201 return add_impl_stmt(stmts
, stmt
);
1204 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1207 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1208 free_let_stmt(stmts
->let
.list
[i
]);
1210 free(stmts
->let
.list
);
1211 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1212 free_return_stmt(stmts
->ret
.list
[i
]);
1214 free(stmts
->ret
.list
);
1215 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1216 free_set_stmt(stmts
->set
.list
[i
]);
1218 free(stmts
->set
.list
);
1219 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1220 free_free_stmt(stmts
->fre
.list
[i
]);
1222 free(stmts
->fre
.list
);
1226 typedef struct impl
{
1232 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1233 impl
*i
= calloc(1, sizeof(*i
));
1239 static inline void free_impl(impl
*impl
) {
1240 free_impl_func(impl
->func
);
1241 free_impl_stmts(impl
->stmts
);
1245 typedef struct impls
{
1250 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1252 impls
= calloc(1, sizeof(*impls
));
1254 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1255 impls
->list
[impls
->count
-1] = impl
;
1259 static void free_impls(impls
*impls
) {
1262 for (i
= 0; i
< impls
->count
; ++i
) {
1263 free_impl(impls
->list
[i
]);
1269 typedef struct decl_file
{
1274 static inline void free_decl_file(decl_file
*file
) {
1281 memset(file
, 0, sizeof(*file
));
1284 typedef struct decl_libs
{
1289 static inline void free_decl_libs(decl_libs
*libs
) {
1292 for (i
= 0; i
< libs
->count
; ++i
) {
1294 dlclose(libs
->dl
[i
]);
1299 memset(libs
, 0, sizeof(*libs
));
1302 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1303 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1304 libs
->dl
[libs
->count
-1] = dlopened
;
1307 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1310 ZEND_ASSERT(var
->arg
->var
!= var
);
1312 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1313 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1314 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1316 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1318 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1320 ret_val
= *(void **) ret_val
;
1325 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1326 impl_val
*val
, *val_ptr
;
1329 ZEND_ASSERT(var
->arg
->var
== var
);
1331 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1332 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1333 var
->array_size
, var
->arg
->var
->array_size
);
1335 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1339 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1340 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1342 fprintf(stderr
, "++\n");
1344 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1345 val_ptr
= val_ptr
->ptr
;
1351 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1352 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1354 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1360 #define PSI_ERROR 16
1361 #define PSI_WARNING 32
1362 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1364 #define PSI_DATA(D) ((PSI_Data *) (D))
1365 #define PSI_DATA_MEMBERS \
1366 constants *consts; \
1367 decl_typedefs *defs; \
1368 decl_structs *structs; \
1369 decl_enums *enums; \
1377 typedef struct PSI_Data
{
1381 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1383 dest
= malloc(sizeof(*dest
));
1385 memcpy(dest
, src
, sizeof(*dest
));
1386 memset(src
, 0, sizeof(*src
));
1390 static inline void PSI_DataDtor(PSI_Data
*data
) {
1392 free_constants(data
->consts
);
1395 free_decl_typedefs(data
->defs
);
1397 if (data
->structs
) {
1398 free_decl_structs(data
->structs
);
1401 free_decl_enums(data
->enums
);
1404 free_decls(data
->decls
);
1407 free_impls(data
->impls
);
1409 free_decl_file(&data
->psi
.file
);
1412 typedef struct PSI_Parser
{
1417 unsigned flags
, errors
, line
, col
;
1418 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1421 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1422 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1425 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1427 size_t token_len
, fname_len
;
1430 if (P
->cur
< P
->tok
) {
1435 token_len
= P
->cur
- P
->tok
;
1436 fname_len
= strlen(P
->psi
.file
.fn
);
1438 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1439 T
->type
= token_typ
;
1440 T
->size
= token_len
;
1441 T
->text
= &T
->buf
[0];
1442 T
->file
= &T
->buf
[token_len
+ 1];
1445 memcpy(T
->text
, P
->tok
, token_len
);
1446 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1451 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1452 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1453 PSI_Token
*ptr
= malloc(strct_len
);
1455 memcpy(ptr
, src
, strct_len
);
1457 ptr
->text
= &ptr
->buf
[0];
1458 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1463 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1466 PSI_Token
*T
= NULL
;
1468 va_start(argv
, argc
);
1469 for (i
= 0; i
< argc
; ++i
) {
1470 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1473 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1475 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1476 T
->text
= &T
->buf
[0];
1477 T
->file
= &T
->buf
[T
->size
+ 1];
1478 T
->buf
[token_len
] = ' ';
1479 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1480 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1482 T
= PSI_TokenCopy(arg
);
1483 T
->type
= PSI_T_NAME
;
1491 static inline const char *PSI_TokenLocation(PSI_Token
*t
) {
1492 return t
? t
->file
: "<builtin>:0:0";
1495 #define PSI_PARSER_DEBUG 0x1
1497 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1498 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1499 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1500 token_t
PSI_ParserScan(PSI_Parser
*P
);
1501 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1502 void PSI_ParserDtor(PSI_Parser
*P
);
1503 void PSI_ParserFree(PSI_Parser
**P
);