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
{
24 size_t size
, line
, col
;
29 typedef union impl_val
{
50 uint8_t _dbg
[sizeof(void *)];
53 typedef struct decl_type
{
57 struct decl_type
*real
;
58 struct decl_struct
*strct
;
61 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
62 decl_type
*t
= calloc(1, sizeof(*t
));
64 t
->name
= strdup(name
);
68 static inline decl_type
*real_decl_type(decl_type
*type
) {
75 static inline void free_decl_type(decl_type
*type
) {
80 typedef struct decl_typedef
{
85 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
86 decl_typedef
*t
= calloc(1, sizeof(*t
));
87 t
->alias
= strdup(name
);
92 static inline void free_decl_typedef(decl_typedef
*t
) {
94 free_decl_type(t
->type
);
98 typedef struct decl_typedefs
{
103 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
105 defs
= calloc(1, sizeof(*defs
));
107 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
108 defs
->list
[defs
->count
-1] = def
;
112 static void free_decl_typedefs(decl_typedefs
*defs
) {
115 for (i
= 0; i
< defs
->count
; ++i
) {
116 free_decl_typedef(defs
->list
[i
]);
122 typedef struct decl_var
{
124 unsigned pointer_level
;
126 struct decl_arg
*arg
;
129 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
130 decl_var
*v
= calloc(1, sizeof(*v
));
131 v
->name
= (char *) strdup((const char *) name
);
132 v
->pointer_level
= pl
;
137 static inline void free_decl_var(decl_var
*var
) {
142 typedef struct decl_struct_layout
{
145 } decl_struct_layout
;
147 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
148 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
155 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
159 typedef struct decl_arg
{
162 decl_struct_layout
*layout
;
163 struct let_stmt
*let
;
169 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
170 decl_arg
*arg
= calloc(1, sizeof(*arg
));
174 arg
->ptr
= &arg
->val
;
178 static inline void free_decl_arg(decl_arg
*arg
) {
179 free_decl_type(arg
->type
);
180 free_decl_var(arg
->var
);
182 free_decl_struct_layout(arg
->layout
);
187 typedef struct decl_vars
{
192 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
193 decl_vars
*vars
= calloc(1, sizeof(*vars
));
196 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
202 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
203 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
204 vars
->vars
[vars
->count
-1] = var
;
208 static inline void free_decl_vars(decl_vars
*vars
) {
211 for (i
= 0; i
< vars
->count
; ++i
) {
212 free_decl_var(vars
->vars
[i
]);
218 typedef struct decl_args
{
224 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
225 decl_args
*args
= calloc(1, sizeof(*args
));
228 args
->args
= calloc(1, sizeof(*args
->args
));
234 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
235 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
236 args
->args
[args
->count
-1] = arg
;
240 static inline void free_decl_args(decl_args
*args
) {
243 for (i
= 0; i
< args
->count
; ++i
) {
244 free_decl_arg(args
->args
[i
]);
250 typedef struct decl_abi
{
254 static inline decl_abi
*init_decl_abi(const char *convention
) {
255 decl_abi
*abi
= calloc(1, sizeof(*abi
));
256 abi
->convention
= strdup(convention
);
260 static inline void free_decl_abi(decl_abi
*abi
) {
261 free(abi
->convention
);
265 typedef struct decl
{
277 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
278 decl
*d
= calloc(1, sizeof(*d
));
285 static inline void free_decl(decl
*d
) {
286 free_decl_abi(d
->abi
);
287 free_decl_arg(d
->func
);
289 free_decl_args(d
->args
);
294 typedef struct decls
{
299 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
301 decls
= calloc(1, sizeof(*decls
));
303 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
304 decls
->list
[decls
->count
-1] = decl
;
308 static inline void free_decls(decls
*decls
) {
311 for (i
= 0; i
< decls
->count
; ++i
) {
312 free_decl(decls
->list
[i
]);
318 typedef struct decl_struct
{
324 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
325 decl_struct
*s
= calloc(1, sizeof(*s
));
326 s
->name
= strdup(name
);
331 static inline void free_decl_struct(decl_struct
*s
) {
333 free_decl_args(s
->args
);
339 typedef struct decl_structs
{
344 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
346 ss
= calloc(1, sizeof(*ss
));
348 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
349 ss
->list
[ss
->count
-1] = s
;
353 static inline void free_decl_structs(decl_structs
*ss
) {
356 for (i
= 0; i
< ss
->count
; ++i
) {
357 free_decl_struct(ss
->list
[i
]);
363 typedef struct impl_type
{
368 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
369 impl_type
*t
= calloc(1, sizeof(*t
));
372 t
->name
= strdup(name
);
376 static inline void free_impl_type(impl_type
*type
) {
381 typedef struct impl_var
{
383 unsigned reference
:1;
386 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
387 impl_var
*var
= calloc(1, sizeof(*var
));
388 var
->name
= strdup(name
);
389 var
->reference
= is_reference
;
393 static inline void free_impl_var(impl_var
*var
) {
398 typedef struct impl_def_val
{
403 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
404 impl_def_val
*def
= calloc(1, sizeof(*def
));
406 def
->text
= strdup(text
);
410 static inline void free_impl_def_val(impl_def_val
*def
) {
415 typedef struct const_type
{
420 static inline const_type
*init_const_type(token_t type
, const char *name
) {
421 const_type
*ct
= calloc(1, sizeof(*ct
));
423 ct
->name
= strdup(name
);
427 static inline void free_const_type(const_type
*type
) {
432 typedef struct constant
{
438 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
439 constant
*c
= calloc(1, sizeof(*c
));
441 c
->name
= strdup(name
);
446 static inline void free_constant(constant
*constant
) {
447 free_const_type(constant
->type
);
448 free(constant
->name
);
449 free_impl_def_val(constant
->val
);
453 typedef struct constants
{
458 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
460 constants
= calloc(1, sizeof(*constants
));
462 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
463 constants
->list
[constants
->count
-1] = constant
;
467 static inline void free_constants(constants
*c
) {
470 for (i
= 0; i
< c
->count
; ++i
) {
471 free_constant(c
->list
[i
]);
477 typedef struct impl_arg
{
485 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
486 impl_arg
*arg
= calloc(1, sizeof(*arg
));
493 static inline void free_impl_arg(impl_arg
*arg
) {
494 free_impl_type(arg
->type
);
495 free_impl_var(arg
->var
);
497 free_impl_def_val(arg
->def
);
502 typedef struct impl_args
{
508 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
509 impl_args
*args
= calloc(1, sizeof(*args
));
512 args
->args
= calloc(1, sizeof(*args
->args
));
518 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
519 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
520 args
->args
[args
->count
-1] = arg
;
524 static inline void free_impl_args(impl_args
*args
) {
527 for (i
= 0; i
< args
->count
; ++i
) {
528 free_impl_arg(args
->args
[i
]);
534 typedef struct impl_func
{
537 impl_type
*return_type
;
538 unsigned return_reference
:1;
541 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
542 impl_func
*func
= calloc(1, sizeof(*func
));
543 func
->name
= strdup(name
);
544 func
->args
= args
? args
: init_impl_args(NULL
);
545 func
->return_type
= type
;
546 func
->return_reference
= ret_reference
;
550 static inline void free_impl_func(impl_func
*f
) {
551 free_impl_type(f
->return_type
);
552 free_impl_args(f
->args
);
557 typedef struct num_exp
{
565 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
566 struct num_exp
*operand
;
569 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
570 num_exp
*exp
= calloc(1, sizeof(*exp
));
571 switch (exp
->t
= t
) {
574 exp
->u
.numb
= strdup(num
);
579 EMPTY_SWITCH_DEFAULT_CASE();
584 static inline void free_num_exp(num_exp
*exp
) {
592 free_decl_var(exp
->u
.dvar
);
594 EMPTY_SWITCH_DEFAULT_CASE();
597 free_num_exp(exp
->operand
);
602 typedef struct let_calloc
{
607 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
608 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
609 alloc
->nmemb
= nmemb
;
614 static inline void free_let_calloc(let_calloc
*alloc
) {
615 free_num_exp(alloc
->nmemb
);
616 free_num_exp(alloc
->size
);
620 typedef struct let_func
{
627 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
628 let_func
*func
= calloc(1, sizeof(*func
));
630 func
->name
= strdup(name
);
635 static inline void free_let_func(let_func
*func
) {
636 free_impl_var(func
->var
);
641 #define PSI_LET_REFERENCE 0x1;
642 typedef struct let_val
{
658 unsigned is_reference
:1;
664 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
665 let_val
*let
= calloc(1, sizeof(*let
));
666 switch (let
->kind
= kind
) {
670 let
->data
.num
= data
;
673 let
->data
.alloc
= data
;
676 let
->data
.func
= data
;
679 let
->data
.var
= data
;
681 EMPTY_SWITCH_DEFAULT_CASE();
686 static inline void free_let_val(let_val
*let
) {
691 free_num_exp(let
->data
.num
);
694 free_let_calloc(let
->data
.alloc
);
697 free_let_func(let
->data
.func
);
700 free_decl_var(let
->data
.var
);
702 EMPTY_SWITCH_DEFAULT_CASE();
707 typedef struct let_stmt
{
714 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
715 let_stmt
*let
= calloc(1, sizeof(*let
));
721 static inline void free_let_stmt(let_stmt
*stmt
) {
723 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
724 free_decl_arg(stmt
->var
->arg
);
726 free_let_val(stmt
->val
);
728 free_decl_var(stmt
->var
);
734 typedef struct set_func
{
737 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
740 static inline set_func
*init_set_func(token_t type
, const char *name
) {
741 set_func
*func
= calloc(1, sizeof(*func
));
743 func
->name
= strdup(name
);
747 static inline void free_set_func(set_func
*func
) {
752 typedef struct set_value
{
757 struct set_value
*set
;
760 struct set_value
**inner
;
764 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
765 set_value
*val
= calloc(1, sizeof(*val
));
770 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
771 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
772 val
->inner
[val
->count
-1] = inner
;
776 static inline void free_set_value(set_value
*val
) {
777 free_set_func(val
->func
);
778 free_decl_vars(val
->vars
);
781 for (i
= 0; i
< val
->count
; ++i
) {
782 free_set_value(val
->inner
[i
]);
787 free_num_exp(val
->num
);
792 typedef struct set_stmt
{
798 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
799 set_stmt
*set
= calloc(1, sizeof(*set
));
805 static inline void free_set_stmt(set_stmt
*set
) {
806 free_impl_var(set
->var
);
807 free_set_value(set
->val
);
811 typedef struct return_stmt
{
816 static inline return_stmt
*init_return_stmt(set_value
*val
) {
817 return_stmt
*ret
= calloc(1, sizeof(*ret
));
822 static inline void free_return_stmt(return_stmt
*ret
) {
823 //free_set_func(ret->func);
824 //free_decl_var(ret->decl);
825 free_set_value(ret
->set
);
829 typedef struct free_call
{
835 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
836 free_call
*f
= calloc(1, sizeof(*f
));
837 f
->func
= strdup(func
);
842 static inline void free_free_call(free_call
*f
) {
844 free_decl_vars(f
->vars
);
848 typedef struct free_calls
{
853 static inline free_calls
*init_free_calls(free_call
*f
) {
854 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
857 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
863 static inline void free_free_calls(free_calls
*fcs
) {
866 for (i
= 0; i
< fcs
->count
; ++i
) {
867 free_free_call(fcs
->list
[i
]);
873 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
874 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
875 fcs
->list
[fcs
->count
-1] = f
;
879 typedef struct free_stmt
{
883 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
884 free_stmt
*f
= calloc(1, sizeof(*f
));
889 static inline void free_free_stmt(free_stmt
*f
) {
890 free_free_calls(f
->calls
);
894 typedef struct impl_stmt
{
905 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
906 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
912 static inline void free_impl_stmt(impl_stmt
*stmt
) {
913 switch (stmt
->type
) {
915 free_let_stmt(stmt
->s
.let
);
918 free_set_stmt(stmt
->s
.set
);
921 free_return_stmt(stmt
->s
.ret
);
924 free_free_stmt(stmt
->s
.fre
);
930 typedef struct impl_stmts
{
949 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
950 list
= realloc(list
, count
* sizeof(list
));
951 ((void **)list
)[count
-1] = stmt
;
955 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
956 switch (stmt
->type
) {
958 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
961 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
964 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
967 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
974 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
975 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
976 return add_impl_stmt(stmts
, stmt
);
979 static inline void free_impl_stmts(impl_stmts
*stmts
) {
982 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
983 free_let_stmt(stmts
->let
.list
[i
]);
985 free(stmts
->let
.list
);
986 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
987 free_return_stmt(stmts
->ret
.list
[i
]);
989 free(stmts
->ret
.list
);
990 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
991 free_set_stmt(stmts
->set
.list
[i
]);
993 free(stmts
->set
.list
);
994 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
995 free_free_stmt(stmts
->fre
.list
[i
]);
997 free(stmts
->fre
.list
);
1001 typedef struct impl
{
1007 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1008 impl
*i
= calloc(1, sizeof(*i
));
1014 static inline void free_impl(impl
*impl
) {
1015 free_impl_func(impl
->func
);
1016 free_impl_stmts(impl
->stmts
);
1020 typedef struct impls
{
1025 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1027 impls
= calloc(1, sizeof(*impls
));
1029 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1030 impls
->list
[impls
->count
-1] = impl
;
1034 static void free_impls(impls
*impls
) {
1037 for (i
= 0; i
< impls
->count
; ++i
) {
1038 free_impl(impls
->list
[i
]);
1044 typedef struct decl_file
{
1049 static inline void free_decl_file(decl_file
*file
) {
1056 memset(file
, 0, sizeof(*file
));
1059 typedef struct decl_libs
{
1064 static inline void free_decl_libs(decl_libs
*libs
) {
1067 for (i
= 0; i
< libs
->count
; ++i
) {
1069 dlclose(libs
->dl
[i
]);
1074 memset(libs
, 0, sizeof(*libs
));
1077 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1078 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1079 libs
->dl
[libs
->count
-1] = dlopened
;
1082 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1085 if (var
->arg
->var
!= var
) for (i
= 1; i
< var
->pointer_level
; ++i
) {
1086 ret_val
= *(void **) ret_val
;
1091 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1092 impl_val
*val
, *val_ptr
;
1095 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
1098 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1099 for (i
= 1; i
< var
->pointer_level
; ++i
) {
1100 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1101 val_ptr
= val_ptr
->ptr
;
1107 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1108 void *ptr
= (char *) struct_ptr
->ptr
+ set_arg
->layout
->pos
;
1109 impl_val
*val
= enref_impl_val(ptr
, set_arg
->var
);
1118 #define PSI_ERROR 16
1119 #define PSI_WARNING 32
1120 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
1122 #define PSI_DATA(D) ((PSI_Data *) (D))
1123 #define PSI_DATA_MEMBERS \
1124 constants *consts; \
1125 decl_typedefs *defs; \
1126 decl_structs *structs; \
1134 typedef struct PSI_Data
{
1138 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1140 dest
= malloc(sizeof(*dest
));
1142 memcpy(dest
, src
, sizeof(*dest
));
1143 memset(src
, 0, sizeof(*src
));
1147 static inline void PSI_DataDtor(PSI_Data
*data
) {
1149 free_constants(data
->consts
);
1152 free_decl_typedefs(data
->defs
);
1154 if (data
->structs
) {
1155 free_decl_structs(data
->structs
);
1158 free_decls(data
->decls
);
1161 free_impls(data
->impls
);
1163 free_decl_file(&data
->psi
.file
);
1166 typedef struct PSI_Parser
{
1174 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1177 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1179 size_t token_len
, fname_len
;
1182 if (P
->cur
< P
->tok
) {
1187 token_len
= P
->cur
- P
->tok
;
1188 fname_len
= strlen(P
->psi
.file
.fn
);
1190 T
= calloc(1, sizeof(*T
) + token_len
+ fname_len
+ 1);
1191 T
->type
= token_typ
;
1192 T
->size
= token_len
;
1195 T
->file
= &T
->buf
[0];
1196 T
->text
= &T
->buf
[fname_len
+ 1];
1198 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1199 memcpy(T
->text
, P
->tok
, token_len
);
1204 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1205 size_t fname_len
= strlen(src
->file
);
1206 size_t strct_len
= sizeof(*src
) + src
->size
+ fname_len
+ 1;
1207 PSI_Token
*ptr
= malloc(strct_len
);
1209 memcpy(ptr
, src
, strct_len
);
1211 ptr
->file
= &ptr
->buf
[0];
1212 ptr
->text
= &ptr
->buf
[fname_len
+ 1];
1217 #define PSI_PARSER_DEBUG 0x1
1219 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1220 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1221 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1222 token_t
PSI_ParserScan(PSI_Parser
*P
);
1223 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1224 void PSI_ParserDtor(PSI_Parser
*P
);
1225 void PSI_ParserFree(PSI_Parser
**P
);