9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
15 #define PSI_T_POINTER PSI_T_ASTERISK
19 size_t psi_t_alignment(token_t
);
20 size_t psi_t_size(token_t
);
22 typedef struct PSI_Token
{
29 typedef union impl_val
{
50 uint8_t _dbg
[sizeof(void *)];
53 typedef struct decl_type
{
56 struct decl_type
*real
;
57 struct decl_struct
*strct
;
60 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
61 decl_type
*t
= calloc(1, sizeof(*t
));
63 t
->name
= strdup(name
);
67 static inline decl_type
*real_decl_type(decl_type
*type
) {
74 static inline void free_decl_type(decl_type
*type
) {
79 typedef struct decl_typedef
{
84 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
85 decl_typedef
*t
= calloc(1, sizeof(*t
));
86 t
->alias
= strdup(name
);
91 static inline void free_decl_typedef(decl_typedef
*t
) {
93 free_decl_type(t
->type
);
97 typedef struct decl_typedefs
{
102 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
104 defs
= calloc(1, sizeof(*defs
));
106 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
107 defs
->list
[defs
->count
-1] = def
;
111 static void free_decl_typedefs(decl_typedefs
*defs
) {
114 for (i
= 0; i
< defs
->count
; ++i
) {
115 free_decl_typedef(defs
->list
[i
]);
121 typedef struct decl_var
{
123 unsigned pointer_level
;
125 struct decl_arg
*arg
;
128 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
129 decl_var
*v
= calloc(1, sizeof(*v
));
130 v
->name
= (char *) strdup((const char *) name
);
131 v
->pointer_level
= pl
;
136 static inline void free_decl_var(decl_var
*var
) {
141 typedef struct decl_struct_layout
{
144 } decl_struct_layout
;
146 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
147 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
154 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
158 typedef struct decl_arg
{
161 decl_struct_layout
*layout
;
162 struct let_stmt
*let
;
168 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
169 decl_arg
*arg
= calloc(1, sizeof(*arg
));
173 arg
->ptr
= &arg
->val
;
177 static inline void free_decl_arg(decl_arg
*arg
) {
178 free_decl_type(arg
->type
);
179 free_decl_var(arg
->var
);
181 free_decl_struct_layout(arg
->layout
);
186 typedef struct decl_vars
{
191 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
192 decl_vars
*vars
= calloc(1, sizeof(*vars
));
195 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
201 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
202 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
203 vars
->vars
[vars
->count
-1] = var
;
207 static inline void free_decl_vars(decl_vars
*vars
) {
210 for (i
= 0; i
< vars
->count
; ++i
) {
211 free_decl_var(vars
->vars
[i
]);
217 typedef struct decl_args
{
222 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
223 decl_args
*args
= calloc(1, sizeof(*args
));
226 args
->args
= calloc(1, sizeof(*args
->args
));
232 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
233 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
234 args
->args
[args
->count
-1] = arg
;
238 static inline void free_decl_args(decl_args
*args
) {
241 for (i
= 0; i
< args
->count
; ++i
) {
242 free_decl_arg(args
->args
[i
]);
248 typedef struct decl_abi
{
252 static inline decl_abi
*init_decl_abi(const char *convention
) {
253 decl_abi
*abi
= calloc(1, sizeof(*abi
));
254 abi
->convention
= strdup(convention
);
258 static inline void free_decl_abi(decl_abi
*abi
) {
259 free(abi
->convention
);
263 typedef struct decl
{
275 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
276 decl
*d
= calloc(1, sizeof(*d
));
283 static inline void free_decl(decl
*d
) {
284 free_decl_abi(d
->abi
);
285 free_decl_arg(d
->func
);
287 free_decl_args(d
->args
);
292 typedef struct decls
{
297 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
299 decls
= calloc(1, sizeof(*decls
));
301 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
302 decls
->list
[decls
->count
-1] = decl
;
306 static inline void free_decls(decls
*decls
) {
309 for (i
= 0; i
< decls
->count
; ++i
) {
310 free_decl(decls
->list
[i
]);
316 typedef struct decl_struct
{
322 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
323 decl_struct
*s
= calloc(1, sizeof(*s
));
324 s
->name
= strdup(name
);
329 static inline void free_decl_struct(decl_struct
*s
) {
331 free_decl_args(s
->args
);
337 typedef struct decl_structs
{
342 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
344 ss
= calloc(1, sizeof(*ss
));
346 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
347 ss
->list
[ss
->count
-1] = s
;
351 static inline void free_decl_structs(decl_structs
*ss
) {
354 for (i
= 0; i
< ss
->count
; ++i
) {
355 free_decl_struct(ss
->list
[i
]);
361 typedef struct impl_type
{
366 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
367 impl_type
*t
= calloc(1, sizeof(*t
));
370 t
->name
= strdup(name
);
374 static inline void free_impl_type(impl_type
*type
) {
379 typedef struct impl_var
{
381 unsigned reference
:1;
384 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
385 impl_var
*var
= calloc(1, sizeof(*var
));
386 var
->name
= strdup(name
);
387 var
->reference
= is_reference
;
391 static inline void free_impl_var(impl_var
*var
) {
396 typedef struct impl_def_val
{
401 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
402 impl_def_val
*def
= calloc(1, sizeof(*def
));
404 def
->text
= strdup(text
);
408 static inline void free_impl_def_val(impl_def_val
*def
) {
413 typedef struct const_type
{
418 static inline const_type
*init_const_type(token_t type
, const char *name
) {
419 const_type
*ct
= calloc(1, sizeof(*ct
));
421 ct
->name
= strdup(name
);
425 static inline void free_const_type(const_type
*type
) {
430 typedef struct constant
{
436 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
437 constant
*c
= calloc(1, sizeof(*c
));
439 c
->name
= strdup(name
);
444 static inline void free_constant(constant
*constant
) {
445 free_const_type(constant
->type
);
446 free(constant
->name
);
447 free_impl_def_val(constant
->val
);
451 typedef struct constants
{
456 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
458 constants
= calloc(1, sizeof(*constants
));
460 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
461 constants
->list
[constants
->count
-1] = constant
;
465 static inline void free_constants(constants
*c
) {
468 for (i
= 0; i
< c
->count
; ++i
) {
469 free_constant(c
->list
[i
]);
475 typedef struct impl_arg
{
483 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
484 impl_arg
*arg
= calloc(1, sizeof(*arg
));
491 static inline void free_impl_arg(impl_arg
*arg
) {
492 free_impl_type(arg
->type
);
493 free_impl_var(arg
->var
);
495 free_impl_def_val(arg
->def
);
500 typedef struct impl_args
{
505 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
506 impl_args
*args
= calloc(1, sizeof(*args
));
509 args
->args
= calloc(1, sizeof(*args
->args
));
515 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
516 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
517 args
->args
[args
->count
-1] = arg
;
521 static inline void free_impl_args(impl_args
*args
) {
524 for (i
= 0; i
< args
->count
; ++i
) {
525 free_impl_arg(args
->args
[i
]);
531 typedef struct impl_func
{
534 impl_type
*return_type
;
535 unsigned return_reference
:1;
538 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
539 impl_func
*func
= calloc(1, sizeof(*func
));
540 func
->name
= strdup(name
);
541 func
->args
= args
? args
: init_impl_args(NULL
);
542 func
->return_type
= type
;
543 func
->return_reference
= ret_reference
;
547 static inline void free_impl_func(impl_func
*f
) {
548 free_impl_type(f
->return_type
);
549 free_impl_args(f
->args
);
554 typedef struct num_exp
{
562 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
563 struct num_exp
*operand
;
566 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
567 num_exp
*exp
= calloc(1, sizeof(*exp
));
568 switch (exp
->t
= t
) {
571 exp
->u
.numb
= strdup(num
);
576 EMPTY_SWITCH_DEFAULT_CASE();
581 static inline void free_num_exp(num_exp
*exp
) {
589 free_decl_var(exp
->u
.dvar
);
591 EMPTY_SWITCH_DEFAULT_CASE();
594 free_num_exp(exp
->operand
);
599 typedef struct let_calloc
{
604 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
605 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
606 alloc
->nmemb
= nmemb
;
611 static inline void free_let_calloc(let_calloc
*alloc
) {
612 free_num_exp(alloc
->nmemb
);
613 free_num_exp(alloc
->size
);
617 typedef struct let_func
{
624 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
625 let_func
*func
= calloc(1, sizeof(*func
));
627 func
->name
= strdup(name
);
632 static inline void free_let_func(let_func
*func
) {
633 free_impl_var(func
->var
);
638 #define PSI_LET_REFERENCE 0x1;
639 typedef struct let_val
{
655 unsigned is_reference
:1;
661 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
662 let_val
*let
= calloc(1, sizeof(*let
));
663 switch (let
->kind
= kind
) {
667 let
->data
.num
= data
;
670 let
->data
.alloc
= data
;
673 let
->data
.func
= data
;
676 let
->data
.var
= data
;
678 EMPTY_SWITCH_DEFAULT_CASE();
683 static inline void free_let_val(let_val
*let
) {
688 free_num_exp(let
->data
.num
);
691 free_let_calloc(let
->data
.alloc
);
694 free_let_func(let
->data
.func
);
697 free_decl_var(let
->data
.var
);
699 EMPTY_SWITCH_DEFAULT_CASE();
704 typedef struct let_stmt
{
711 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
712 let_stmt
*let
= calloc(1, sizeof(*let
));
718 static inline void free_let_stmt(let_stmt
*stmt
) {
720 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
721 free_decl_arg(stmt
->var
->arg
);
723 free_let_val(stmt
->val
);
725 free_decl_var(stmt
->var
);
731 typedef struct set_func
{
734 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
737 static inline set_func
*init_set_func(token_t type
, const char *name
) {
738 set_func
*func
= calloc(1, sizeof(*func
));
740 func
->name
= strdup(name
);
744 static inline void free_set_func(set_func
*func
) {
749 typedef struct set_value
{
754 struct set_value
*set
;
757 struct set_value
**inner
;
761 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
762 set_value
*val
= calloc(1, sizeof(*val
));
767 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
768 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
769 val
->inner
[val
->count
-1] = inner
;
773 static inline void free_set_value(set_value
*val
) {
774 free_set_func(val
->func
);
775 free_decl_vars(val
->vars
);
778 for (i
= 0; i
< val
->count
; ++i
) {
779 free_set_value(val
->inner
[i
]);
784 free_num_exp(val
->num
);
789 typedef struct set_stmt
{
795 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
796 set_stmt
*set
= calloc(1, sizeof(*set
));
802 static inline void free_set_stmt(set_stmt
*set
) {
803 free_impl_var(set
->var
);
804 free_set_value(set
->val
);
808 typedef struct return_stmt
{
813 static inline return_stmt
*init_return_stmt(set_value
*val
) {
814 return_stmt
*ret
= calloc(1, sizeof(*ret
));
819 static inline void free_return_stmt(return_stmt
*ret
) {
820 //free_set_func(ret->func);
821 //free_decl_var(ret->decl);
822 free_set_value(ret
->set
);
826 typedef struct free_call
{
832 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
833 free_call
*f
= calloc(1, sizeof(*f
));
834 f
->func
= strdup(func
);
839 static inline void free_free_call(free_call
*f
) {
841 free_decl_vars(f
->vars
);
845 typedef struct free_calls
{
850 static inline free_calls
*init_free_calls(free_call
*f
) {
851 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
854 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
860 static inline void free_free_calls(free_calls
*fcs
) {
863 for (i
= 0; i
< fcs
->count
; ++i
) {
864 free_free_call(fcs
->list
[i
]);
870 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
871 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
872 fcs
->list
[fcs
->count
-1] = f
;
876 typedef struct free_stmt
{
880 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
881 free_stmt
*f
= calloc(1, sizeof(*f
));
886 static inline void free_free_stmt(free_stmt
*f
) {
887 free_free_calls(f
->calls
);
891 typedef struct impl_stmt
{
902 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
903 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
909 static inline void free_impl_stmt(impl_stmt
*stmt
) {
910 switch (stmt
->type
) {
912 free_let_stmt(stmt
->s
.let
);
915 free_set_stmt(stmt
->s
.set
);
918 free_return_stmt(stmt
->s
.ret
);
921 free_free_stmt(stmt
->s
.fre
);
927 typedef struct impl_stmts
{
946 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
947 list
= realloc(list
, count
* sizeof(list
));
948 ((void **)list
)[count
-1] = stmt
;
952 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
953 switch (stmt
->type
) {
955 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
958 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
961 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
964 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
971 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
972 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
973 return add_impl_stmt(stmts
, stmt
);
976 static inline void free_impl_stmts(impl_stmts
*stmts
) {
979 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
980 free_let_stmt(stmts
->let
.list
[i
]);
982 free(stmts
->let
.list
);
983 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
984 free_return_stmt(stmts
->ret
.list
[i
]);
986 free(stmts
->ret
.list
);
987 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
988 free_set_stmt(stmts
->set
.list
[i
]);
990 free(stmts
->set
.list
);
991 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
992 free_free_stmt(stmts
->fre
.list
[i
]);
994 free(stmts
->fre
.list
);
998 typedef struct impl
{
1004 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1005 impl
*i
= calloc(1, sizeof(*i
));
1011 static inline void free_impl(impl
*impl
) {
1012 free_impl_func(impl
->func
);
1013 free_impl_stmts(impl
->stmts
);
1017 typedef struct impls
{
1022 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1024 impls
= calloc(1, sizeof(*impls
));
1026 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1027 impls
->list
[impls
->count
-1] = impl
;
1031 static void free_impls(impls
*impls
) {
1034 for (i
= 0; i
< impls
->count
; ++i
) {
1035 free_impl(impls
->list
[i
]);
1041 typedef struct decl_file
{
1046 static inline void free_decl_file(decl_file
*file
) {
1053 memset(file
, 0, sizeof(*file
));
1056 typedef struct decl_libs
{
1061 static inline void free_decl_libs(decl_libs
*libs
) {
1064 for (i
= 0; i
< libs
->count
; ++i
) {
1066 dlclose(libs
->dl
[i
]);
1071 memset(libs
, 0, sizeof(*libs
));
1074 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1075 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1076 libs
->dl
[libs
->count
-1] = dlopened
;
1079 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1082 if (var
->arg
->var
!= var
) for (i
= 1; i
< var
->pointer_level
; ++i
) {
1083 ret_val
= *(void **) ret_val
;
1088 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1089 impl_val
*val
, *val_ptr
;
1092 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
1095 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1096 for (i
= 1; i
< var
->pointer_level
; ++i
) {
1097 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1098 val_ptr
= val_ptr
->ptr
;
1104 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1105 void *ptr
= (char *) struct_ptr
->ptr
+ set_arg
->layout
->pos
;
1106 impl_val
*val
= enref_impl_val(ptr
, set_arg
->var
);
1115 #define PSI_ERROR 16
1116 #define PSI_WARNING 32
1117 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
1119 #define PSI_DATA(D) ((PSI_Data *) (D))
1120 #define PSI_DATA_MEMBERS \
1121 constants *consts; \
1122 decl_typedefs *defs; \
1123 decl_structs *structs; \
1131 typedef struct PSI_Data
{
1135 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1137 dest
= malloc(sizeof(*dest
));
1139 memcpy(dest
, src
, sizeof(*dest
));
1140 memset(src
, 0, sizeof(*src
));
1144 static inline void PSI_DataDtor(PSI_Data
*data
) {
1146 free_constants(data
->consts
);
1149 free_decl_typedefs(data
->defs
);
1151 if (data
->structs
) {
1152 free_decl_structs(data
->structs
);
1155 free_decls(data
->decls
);
1158 free_impls(data
->impls
);
1160 free_decl_file(&data
->psi
.file
);
1163 typedef struct PSI_Parser
{
1171 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1174 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1178 if (P
->cur
< P
->tok
) {
1182 token_len
= P
->cur
- P
->tok
;
1184 T
= calloc(1, sizeof(*T
) + token_len
);
1187 T
->size
= token_len
;
1188 T
->text
[token_len
] = 0;
1189 memcpy(T
->text
, P
->tok
, token_len
);
1194 #define PSI_PARSER_DEBUG 0x1
1196 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1197 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1198 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1199 token_t
PSI_ParserScan(PSI_Parser
*P
);
1200 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
1201 void PSI_ParserDtor(PSI_Parser
*P
);
1202 void PSI_ParserFree(PSI_Parser
**P
);