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 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
;
67 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
68 decl_type
*t
= calloc(1, sizeof(*t
));
70 t
->name
= strdup(name
);
74 static inline decl_type
*real_decl_type(decl_type
*type
) {
81 static inline void free_decl_type(decl_type
*type
) {
89 typedef struct decl_typedef
{
95 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
96 decl_typedef
*t
= calloc(1, sizeof(*t
));
97 t
->alias
= strdup(name
);
102 static inline void free_decl_typedef(decl_typedef
*t
) {
107 free_decl_type(t
->type
);
111 typedef struct decl_typedefs
{
116 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
118 defs
= calloc(1, sizeof(*defs
));
120 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
121 defs
->list
[defs
->count
-1] = def
;
125 static void free_decl_typedefs(decl_typedefs
*defs
) {
128 for (i
= 0; i
< defs
->count
; ++i
) {
129 free_decl_typedef(defs
->list
[i
]);
135 typedef struct decl_var
{
138 unsigned pointer_level
;
140 struct decl_arg
*arg
;
143 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
144 decl_var
*v
= calloc(1, sizeof(*v
));
145 v
->name
= (char *) strdup((const char *) name
);
146 v
->pointer_level
= pl
;
151 static inline void free_decl_var(decl_var
*var
) {
159 typedef struct decl_struct_layout
{
162 } decl_struct_layout
;
164 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
165 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
166 ZEND_ASSERT(pos
+len
);
172 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
176 typedef struct decl_arg
{
180 decl_struct_layout
*layout
;
181 struct let_stmt
*let
;
187 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
188 decl_arg
*arg
= calloc(1, sizeof(*arg
));
189 arg
->token
= var
->token
;
193 arg
->ptr
= &arg
->val
;
197 static inline void free_decl_arg(decl_arg
*arg
) {
198 free_decl_type(arg
->type
);
199 free_decl_var(arg
->var
);
201 free_decl_struct_layout(arg
->layout
);
206 typedef struct decl_vars
{
211 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
212 decl_vars
*vars
= calloc(1, sizeof(*vars
));
215 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
221 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
222 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
223 vars
->vars
[vars
->count
-1] = var
;
227 static inline void free_decl_vars(decl_vars
*vars
) {
230 for (i
= 0; i
< vars
->count
; ++i
) {
231 free_decl_var(vars
->vars
[i
]);
237 typedef struct decl_args
{
243 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
244 decl_args
*args
= calloc(1, sizeof(*args
));
247 args
->args
= calloc(1, sizeof(*args
->args
));
253 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
254 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
255 args
->args
[args
->count
-1] = arg
;
259 static inline void free_decl_args(decl_args
*args
) {
262 for (i
= 0; i
< args
->count
; ++i
) {
263 free_decl_arg(args
->args
[i
]);
269 typedef struct decl_abi
{
274 static inline decl_abi
*init_decl_abi(const char *convention
) {
275 decl_abi
*abi
= calloc(1, sizeof(*abi
));
276 abi
->convention
= strdup(convention
);
280 static inline void free_decl_abi(decl_abi
*abi
) {
284 free(abi
->convention
);
288 typedef struct decl_callinfo
{
296 typedef struct decl
{
304 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
305 decl
*d
= calloc(1, sizeof(*d
));
312 static inline void free_decl(decl
*d
) {
313 free_decl_abi(d
->abi
);
314 free_decl_arg(d
->func
);
316 free_decl_args(d
->args
);
321 typedef struct decls
{
326 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
328 decls
= calloc(1, sizeof(*decls
));
330 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
331 decls
->list
[decls
->count
-1] = decl
;
335 static inline void free_decls(decls
*decls
) {
338 for (i
= 0; i
< decls
->count
; ++i
) {
339 free_decl(decls
->list
[i
]);
346 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 impl_type
{
407 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
408 impl_type
*t
= calloc(1, sizeof(*t
));
411 t
->name
= strdup(name
);
415 static inline void free_impl_type(impl_type
*type
) {
420 typedef struct impl_var
{
423 unsigned reference
:1;
426 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
427 impl_var
*var
= calloc(1, sizeof(*var
));
428 var
->name
= strdup(name
);
429 var
->reference
= is_reference
;
433 static inline void free_impl_var(impl_var
*var
) {
441 typedef struct impl_def_val
{
446 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
447 impl_def_val
*def
= calloc(1, sizeof(*def
));
449 def
->text
= strdup(text
);
453 static inline void free_impl_def_val(impl_def_val
*def
) {
458 typedef struct const_type
{
463 static inline const_type
*init_const_type(token_t type
, const char *name
) {
464 const_type
*ct
= calloc(1, sizeof(*ct
));
466 ct
->name
= strdup(name
);
470 static inline void free_const_type(const_type
*type
) {
475 typedef struct constant
{
481 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
482 constant
*c
= calloc(1, sizeof(*c
));
484 c
->name
= strdup(name
);
489 static inline void free_constant(constant
*constant
) {
490 free_const_type(constant
->type
);
491 free(constant
->name
);
492 free_impl_def_val(constant
->val
);
496 typedef struct constants
{
501 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
503 constants
= calloc(1, sizeof(*constants
));
505 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
506 constants
->list
[constants
->count
-1] = constant
;
510 static inline void free_constants(constants
*c
) {
513 for (i
= 0; i
< c
->count
; ++i
) {
514 free_constant(c
->list
[i
]);
520 typedef struct impl_arg
{
528 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
529 impl_arg
*arg
= calloc(1, sizeof(*arg
));
536 static inline void free_impl_arg(impl_arg
*arg
) {
537 free_impl_type(arg
->type
);
538 free_impl_var(arg
->var
);
540 free_impl_def_val(arg
->def
);
545 typedef struct impl_vararg
{
547 struct impl_args
*args
;
553 typedef struct impl_args
{
559 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
560 impl_args
*args
= calloc(1, sizeof(*args
));
563 args
->args
= calloc(1, sizeof(*args
->args
));
569 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
570 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
571 args
->args
[args
->count
-1] = arg
;
575 static inline void free_impl_args(impl_args
*args
) {
578 for (i
= 0; i
< args
->count
; ++i
) {
579 free_impl_arg(args
->args
[i
]);
581 if (args
->vararg
.name
) {
582 free_impl_arg(args
->vararg
.name
);
588 typedef struct impl_func
{
592 impl_type
*return_type
;
593 unsigned return_reference
:1;
596 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
597 impl_func
*func
= calloc(1, sizeof(*func
));
598 func
->name
= strdup(name
);
599 func
->args
= args
? args
: init_impl_args(NULL
);
600 func
->return_type
= type
;
601 func
->return_reference
= ret_reference
;
605 static inline void free_impl_func(impl_func
*f
) {
609 free_impl_type(f
->return_type
);
610 free_impl_args(f
->args
);
615 typedef struct num_exp
{
622 struct decl_enum_item
*enm
;
625 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
626 struct num_exp
*operand
;
629 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
630 num_exp
*exp
= calloc(1, sizeof(*exp
));
631 switch (exp
->t
= t
) {
634 exp
->u
.numb
= strdup(num
);
639 EMPTY_SWITCH_DEFAULT_CASE();
644 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
);
645 static inline num_exp
*copy_num_exp(num_exp
*exp
) {
647 num_exp
*num
= calloc(1, sizeof(*num
));
649 memcpy(num
, exp
, sizeof(*num
));
652 num
->token
= PSI_TokenCopy(num
->token
);
655 num
->operand
= copy_num_exp(num
->operand
);
660 num
->u
.numb
= strdup(num
->u
.numb
);
663 dvar
= init_decl_var(num
->u
.dvar
->name
, num
->u
.dvar
->pointer_level
, num
->u
.dvar
->array_size
);
664 dvar
->arg
= num
->u
.dvar
->arg
;
665 if (num
->u
.dvar
->token
) {
666 dvar
->token
= PSI_TokenCopy(num
->u
.dvar
->token
);
674 static inline void free_num_exp(num_exp
*exp
) {
685 free_decl_var(exp
->u
.dvar
);
689 EMPTY_SWITCH_DEFAULT_CASE();
692 free_num_exp(exp
->operand
);
697 typedef struct decl_enum_item
{
702 struct decl_enum_item
*prev
;
705 static inline decl_enum_item
*init_decl_enum_item(const char *name
, num_exp
*num
) {
706 decl_enum_item
*i
= calloc(1, sizeof(*i
));
708 i
->name
= strdup(name
);
713 static inline void free_decl_enum_item(decl_enum_item
*i
) {
717 if (i
->num
&& i
->num
!= &i
->inc
) {
718 free_num_exp(i
->num
);
724 typedef struct decl_enum_items
{
725 decl_enum_item
**list
;
729 static inline decl_enum_items
*init_decl_enum_items(decl_enum_item
*i
) {
730 decl_enum_items
*l
= calloc(1, sizeof(*l
));
734 l
->list
= calloc(1, sizeof(*l
->list
));
740 static inline decl_enum_items
*add_decl_enum_item(decl_enum_items
*l
, decl_enum_item
*i
) {
741 l
->list
= realloc(l
->list
, sizeof(*l
->list
) * (l
->count
+ 1));
742 l
->list
[l
->count
] = i
;
744 i
->prev
= l
->list
[l
->count
- 1];
750 static inline void free_decl_enum_items(decl_enum_items
*l
) {
754 for (j
= 0; j
< l
->count
; ++j
) {
755 free_decl_enum_item(l
->list
[j
]);
762 typedef struct decl_enum
{
765 decl_enum_items
*items
;
768 static inline decl_enum
*init_decl_enum(const char *name
, decl_enum_items
*l
) {
769 decl_enum
*e
= calloc(1, sizeof(*e
));
771 e
->name
= strdup(name
);
776 static inline void free_decl_enum(decl_enum
*e
) {
781 free_decl_enum_items(e
->items
);
787 typedef struct decl_enums
{
792 static inline decl_enums
* add_decl_enum(decl_enums
*es
, decl_enum
*e
) {
794 es
= calloc(1, sizeof(*es
));
796 es
->list
= realloc(es
->list
, ++es
->count
* sizeof(*es
->list
));
797 es
->list
[es
->count
-1] = e
;
801 static inline void free_decl_enums(decl_enums
*es
) {
805 for (j
= 0; j
< es
->count
; ++j
) {
806 free_decl_enum(es
->list
[j
]);
813 typedef struct let_calloc
{
818 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
819 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
820 alloc
->nmemb
= nmemb
;
825 static inline void free_let_calloc(let_calloc
*alloc
) {
826 free_num_exp(alloc
->nmemb
);
827 free_num_exp(alloc
->size
);
831 typedef struct let_func
{
838 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
839 let_func
*func
= calloc(1, sizeof(*func
));
841 func
->name
= strdup(name
);
846 static inline void free_let_func(let_func
*func
) {
847 free_impl_var(func
->var
);
852 #define PSI_LET_REFERENCE 0x1;
853 typedef struct let_val
{
869 unsigned is_reference
:1;
875 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
876 let_val
*let
= calloc(1, sizeof(*let
));
877 switch (let
->kind
= kind
) {
881 let
->data
.num
= data
;
884 let
->data
.alloc
= data
;
887 let
->data
.func
= data
;
890 let
->data
.var
= data
;
892 EMPTY_SWITCH_DEFAULT_CASE();
897 static inline void free_let_val(let_val
*let
) {
902 free_num_exp(let
->data
.num
);
905 free_let_calloc(let
->data
.alloc
);
908 free_let_func(let
->data
.func
);
911 free_decl_var(let
->data
.var
);
913 EMPTY_SWITCH_DEFAULT_CASE();
918 typedef struct let_stmt
{
925 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
926 let_stmt
*let
= calloc(1, sizeof(*let
));
932 static inline void free_let_stmt(let_stmt
*stmt
) {
934 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
935 free_decl_arg(stmt
->var
->arg
);
937 free_let_val(stmt
->val
);
939 free_decl_var(stmt
->var
);
945 typedef struct set_func
{
949 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
952 static inline set_func
*init_set_func(token_t type
, const char *name
) {
953 set_func
*func
= calloc(1, sizeof(*func
));
955 func
->name
= strdup(name
);
959 static inline void free_set_func(set_func
*func
) {
967 typedef struct set_value
{
972 struct set_value
*set
;
975 struct set_value
**inner
;
979 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
980 set_value
*val
= calloc(1, sizeof(*val
));
985 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
986 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
987 val
->inner
[val
->count
-1] = inner
;
988 inner
->outer
.set
= val
;
992 static inline void free_set_value(set_value
*val
) {
994 free_set_func(val
->func
);
997 free_decl_vars(val
->vars
);
999 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
1001 for (i
= 0; i
< val
->count
; ++i
) {
1002 free_set_value(val
->inner
[i
]);
1007 free_num_exp(val
->num
);
1012 typedef struct set_stmt
{
1018 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
1019 set_stmt
*set
= calloc(1, sizeof(*set
));
1025 static inline void free_set_stmt(set_stmt
*set
) {
1026 free_impl_var(set
->var
);
1027 free_set_value(set
->val
);
1031 typedef struct return_stmt
{
1037 static inline return_stmt
*init_return_stmt(set_value
*val
) {
1038 return_stmt
*ret
= calloc(1, sizeof(*ret
));
1043 static inline void free_return_stmt(return_stmt
*ret
) {
1047 free_set_value(ret
->set
);
1051 typedef struct free_call
{
1058 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
1059 free_call
*f
= calloc(1, sizeof(*f
));
1060 f
->func
= strdup(func
);
1065 static inline void free_free_call(free_call
*f
) {
1070 free_decl_vars(f
->vars
);
1074 typedef struct free_calls
{
1079 static inline free_calls
*init_free_calls(free_call
*f
) {
1080 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
1083 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
1089 static inline void free_free_calls(free_calls
*fcs
) {
1092 for (i
= 0; i
< fcs
->count
; ++i
) {
1093 free_free_call(fcs
->list
[i
]);
1099 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
1100 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
1101 fcs
->list
[fcs
->count
-1] = f
;
1105 typedef struct free_stmt
{
1109 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
1110 free_stmt
*f
= calloc(1, sizeof(*f
));
1115 static inline void free_free_stmt(free_stmt
*f
) {
1116 free_free_calls(f
->calls
);
1120 typedef struct impl_stmt
{
1131 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1132 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1138 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1139 switch (stmt
->type
) {
1141 free_let_stmt(stmt
->s
.let
);
1144 free_set_stmt(stmt
->s
.set
);
1147 free_return_stmt(stmt
->s
.ret
);
1150 free_free_stmt(stmt
->s
.fre
);
1156 typedef struct impl_stmts
{
1175 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1176 list
= realloc(list
, count
* sizeof(list
));
1177 ((void **)list
)[count
-1] = stmt
;
1181 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1182 switch (stmt
->type
) {
1184 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1187 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1190 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1193 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1200 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1201 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1202 return add_impl_stmt(stmts
, stmt
);
1205 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1208 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1209 free_let_stmt(stmts
->let
.list
[i
]);
1211 free(stmts
->let
.list
);
1212 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1213 free_return_stmt(stmts
->ret
.list
[i
]);
1215 free(stmts
->ret
.list
);
1216 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1217 free_set_stmt(stmts
->set
.list
[i
]);
1219 free(stmts
->set
.list
);
1220 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1221 free_free_stmt(stmts
->fre
.list
[i
]);
1223 free(stmts
->fre
.list
);
1227 typedef struct impl
{
1233 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1234 impl
*i
= calloc(1, sizeof(*i
));
1240 static inline void free_impl(impl
*impl
) {
1241 free_impl_func(impl
->func
);
1242 free_impl_stmts(impl
->stmts
);
1246 typedef struct impls
{
1251 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1253 impls
= calloc(1, sizeof(*impls
));
1255 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1256 impls
->list
[impls
->count
-1] = impl
;
1260 static void free_impls(impls
*impls
) {
1263 for (i
= 0; i
< impls
->count
; ++i
) {
1264 free_impl(impls
->list
[i
]);
1270 typedef struct decl_file
{
1275 static inline void free_decl_file(decl_file
*file
) {
1282 memset(file
, 0, sizeof(*file
));
1285 typedef struct decl_libs
{
1290 static inline void free_decl_libs(decl_libs
*libs
) {
1293 for (i
= 0; i
< libs
->count
; ++i
) {
1295 dlclose(libs
->dl
[i
]);
1300 memset(libs
, 0, sizeof(*libs
));
1303 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1304 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1305 libs
->dl
[libs
->count
-1] = dlopened
;
1308 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1311 ZEND_ASSERT(var
->arg
->var
!= var
);
1313 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1314 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1315 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1317 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1319 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1321 ret_val
= *(void **) ret_val
;
1326 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1327 impl_val
*val
, *val_ptr
;
1330 ZEND_ASSERT(var
->arg
->var
== var
);
1332 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1333 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1334 var
->array_size
, var
->arg
->var
->array_size
);
1336 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1340 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1341 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1343 fprintf(stderr
, "++\n");
1345 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1346 val_ptr
= val_ptr
->ptr
;
1352 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1353 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1355 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1361 #define PSI_ERROR 16
1362 #define PSI_WARNING 32
1363 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1365 #define PSI_DATA(D) ((PSI_Data *) (D))
1366 #define PSI_DATA_MEMBERS \
1367 constants *consts; \
1368 decl_typedefs *defs; \
1369 decl_structs *structs; \
1370 decl_enums *enums; \
1378 typedef struct PSI_Data
{
1382 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1384 dest
= malloc(sizeof(*dest
));
1386 memcpy(dest
, src
, sizeof(*dest
));
1387 memset(src
, 0, sizeof(*src
));
1391 static inline void PSI_DataDtor(PSI_Data
*data
) {
1393 free_constants(data
->consts
);
1396 free_decl_typedefs(data
->defs
);
1398 if (data
->structs
) {
1399 free_decl_structs(data
->structs
);
1402 free_decl_enums(data
->enums
);
1405 free_decls(data
->decls
);
1408 free_impls(data
->impls
);
1410 free_decl_file(&data
->psi
.file
);
1413 typedef struct PSI_Parser
{
1418 unsigned flags
, errors
, line
, col
;
1419 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1422 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1423 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1426 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1428 size_t token_len
, fname_len
;
1431 if (P
->cur
< P
->tok
) {
1436 token_len
= P
->cur
- P
->tok
;
1437 fname_len
= strlen(P
->psi
.file
.fn
);
1439 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1440 T
->type
= token_typ
;
1441 T
->size
= token_len
;
1442 T
->text
= &T
->buf
[0];
1443 T
->file
= &T
->buf
[token_len
+ 1];
1447 memcpy(T
->text
, P
->tok
, token_len
);
1448 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1453 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1454 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1455 PSI_Token
*ptr
= malloc(strct_len
);
1457 memcpy(ptr
, src
, strct_len
);
1459 ptr
->text
= &ptr
->buf
[0];
1460 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1465 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1468 PSI_Token
*T
= NULL
;
1470 va_start(argv
, argc
);
1471 for (i
= 0; i
< argc
; ++i
) {
1472 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1475 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1477 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1478 T
->text
= &T
->buf
[0];
1479 T
->file
= &T
->buf
[T
->size
+ 1];
1480 T
->buf
[token_len
] = ' ';
1481 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1482 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1484 T
= PSI_TokenCopy(arg
);
1485 T
->type
= PSI_T_NAME
;
1493 static inline PSI_Token
*PSI_TokenAppend(PSI_Token
*T
, unsigned argc
, ...) {
1497 va_start(argv
, argc
);
1498 for (i
= 0; i
< argc
; ++i
) {
1499 char *str
= va_arg(argv
, char *);
1500 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
1502 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= str_len
+ 1, fname_len
));
1503 T
->text
= &T
->buf
[0];
1504 T
->file
= &T
->buf
[T
->size
+ 1];
1505 T
->buf
[token_len
] = ' ';
1506 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1507 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
1514 static inline PSI_Token
*PSI_TokenTranslit(PSI_Token
*T
, char *from
, char *to
) {
1515 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
1519 static inline uint64_t psi_hash(char *digest_buf
, ...)
1521 uint64_t hash
= 5381;
1526 va_start(argv
, digest_buf
);
1527 while ((ptr
= va_arg(argv
, const uint8_t *))) {
1528 while ((c
= *ptr
++)) {
1529 hash
= ((hash
<< 5) + hash
) + c
;
1535 sprintf(digest_buf
, "%" PRIx64
, hash
);
1541 static inline uint64_t PSI_TokenHash(PSI_Token
*t
, char *digest_buf
) {
1544 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
1545 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);
1548 #define PSI_PARSER_DEBUG 0x1
1550 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1551 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1552 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1553 token_t
PSI_ParserScan(PSI_Parser
*P
);
1554 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1555 void PSI_ParserDtor(PSI_Parser
*P
);
1556 void PSI_ParserFree(PSI_Parser
**P
);