7faa43b712f74a54b20cad9b657f89ac55ecf0ef
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_callinfo
{
273 typedef struct decl
{
281 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
282 decl
*d
= calloc(1, sizeof(*d
));
289 static inline void free_decl(decl
*d
) {
290 free_decl_abi(d
->abi
);
291 free_decl_arg(d
->func
);
293 free_decl_args(d
->args
);
298 typedef struct decls
{
303 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
305 decls
= calloc(1, sizeof(*decls
));
307 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
308 decls
->list
[decls
->count
-1] = decl
;
312 static inline void free_decls(decls
*decls
) {
315 for (i
= 0; i
< decls
->count
; ++i
) {
316 free_decl(decls
->list
[i
]);
322 typedef struct decl_struct
{
328 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
329 decl_struct
*s
= calloc(1, sizeof(*s
));
330 s
->name
= strdup(name
);
335 static inline void free_decl_struct(decl_struct
*s
) {
337 free_decl_args(s
->args
);
343 typedef struct decl_structs
{
348 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
350 ss
= calloc(1, sizeof(*ss
));
352 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
353 ss
->list
[ss
->count
-1] = s
;
357 static inline void free_decl_structs(decl_structs
*ss
) {
360 for (i
= 0; i
< ss
->count
; ++i
) {
361 free_decl_struct(ss
->list
[i
]);
367 typedef struct impl_type
{
372 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
373 impl_type
*t
= calloc(1, sizeof(*t
));
376 t
->name
= strdup(name
);
380 static inline void free_impl_type(impl_type
*type
) {
385 typedef struct impl_var
{
387 unsigned reference
:1;
390 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
391 impl_var
*var
= calloc(1, sizeof(*var
));
392 var
->name
= strdup(name
);
393 var
->reference
= is_reference
;
397 static inline void free_impl_var(impl_var
*var
) {
402 typedef struct impl_def_val
{
407 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
408 impl_def_val
*def
= calloc(1, sizeof(*def
));
410 def
->text
= strdup(text
);
414 static inline void free_impl_def_val(impl_def_val
*def
) {
419 typedef struct const_type
{
424 static inline const_type
*init_const_type(token_t type
, const char *name
) {
425 const_type
*ct
= calloc(1, sizeof(*ct
));
427 ct
->name
= strdup(name
);
431 static inline void free_const_type(const_type
*type
) {
436 typedef struct constant
{
442 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
443 constant
*c
= calloc(1, sizeof(*c
));
445 c
->name
= strdup(name
);
450 static inline void free_constant(constant
*constant
) {
451 free_const_type(constant
->type
);
452 free(constant
->name
);
453 free_impl_def_val(constant
->val
);
457 typedef struct constants
{
462 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
464 constants
= calloc(1, sizeof(*constants
));
466 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
467 constants
->list
[constants
->count
-1] = constant
;
471 static inline void free_constants(constants
*c
) {
474 for (i
= 0; i
< c
->count
; ++i
) {
475 free_constant(c
->list
[i
]);
481 typedef struct impl_arg
{
489 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
490 impl_arg
*arg
= calloc(1, sizeof(*arg
));
497 static inline void free_impl_arg(impl_arg
*arg
) {
498 free_impl_type(arg
->type
);
499 free_impl_var(arg
->var
);
501 free_impl_def_val(arg
->def
);
506 typedef struct impl_args
{
512 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
513 impl_args
*args
= calloc(1, sizeof(*args
));
516 args
->args
= calloc(1, sizeof(*args
->args
));
522 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
523 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
524 args
->args
[args
->count
-1] = arg
;
528 static inline void free_impl_args(impl_args
*args
) {
531 for (i
= 0; i
< args
->count
; ++i
) {
532 free_impl_arg(args
->args
[i
]);
538 typedef struct impl_func
{
541 impl_type
*return_type
;
542 unsigned return_reference
:1;
545 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
546 impl_func
*func
= calloc(1, sizeof(*func
));
547 func
->name
= strdup(name
);
548 func
->args
= args
? args
: init_impl_args(NULL
);
549 func
->return_type
= type
;
550 func
->return_reference
= ret_reference
;
554 static inline void free_impl_func(impl_func
*f
) {
555 free_impl_type(f
->return_type
);
556 free_impl_args(f
->args
);
561 typedef struct num_exp
{
569 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
570 struct num_exp
*operand
;
573 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
574 num_exp
*exp
= calloc(1, sizeof(*exp
));
575 switch (exp
->t
= t
) {
578 exp
->u
.numb
= strdup(num
);
583 EMPTY_SWITCH_DEFAULT_CASE();
588 static inline void free_num_exp(num_exp
*exp
) {
596 free_decl_var(exp
->u
.dvar
);
598 EMPTY_SWITCH_DEFAULT_CASE();
601 free_num_exp(exp
->operand
);
606 typedef struct let_calloc
{
611 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
612 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
613 alloc
->nmemb
= nmemb
;
618 static inline void free_let_calloc(let_calloc
*alloc
) {
619 free_num_exp(alloc
->nmemb
);
620 free_num_exp(alloc
->size
);
624 typedef struct let_func
{
631 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
632 let_func
*func
= calloc(1, sizeof(*func
));
634 func
->name
= strdup(name
);
639 static inline void free_let_func(let_func
*func
) {
640 free_impl_var(func
->var
);
645 #define PSI_LET_REFERENCE 0x1;
646 typedef struct let_val
{
662 unsigned is_reference
:1;
668 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
669 let_val
*let
= calloc(1, sizeof(*let
));
670 switch (let
->kind
= kind
) {
674 let
->data
.num
= data
;
677 let
->data
.alloc
= data
;
680 let
->data
.func
= data
;
683 let
->data
.var
= data
;
685 EMPTY_SWITCH_DEFAULT_CASE();
690 static inline void free_let_val(let_val
*let
) {
695 free_num_exp(let
->data
.num
);
698 free_let_calloc(let
->data
.alloc
);
701 free_let_func(let
->data
.func
);
704 free_decl_var(let
->data
.var
);
706 EMPTY_SWITCH_DEFAULT_CASE();
711 typedef struct let_stmt
{
718 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
719 let_stmt
*let
= calloc(1, sizeof(*let
));
725 static inline void free_let_stmt(let_stmt
*stmt
) {
727 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
728 free_decl_arg(stmt
->var
->arg
);
730 free_let_val(stmt
->val
);
732 free_decl_var(stmt
->var
);
738 typedef struct set_func
{
741 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
744 static inline set_func
*init_set_func(token_t type
, const char *name
) {
745 set_func
*func
= calloc(1, sizeof(*func
));
747 func
->name
= strdup(name
);
751 static inline void free_set_func(set_func
*func
) {
756 typedef struct set_value
{
761 struct set_value
*set
;
764 struct set_value
**inner
;
768 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
769 set_value
*val
= calloc(1, sizeof(*val
));
774 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
775 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
776 val
->inner
[val
->count
-1] = inner
;
780 static inline void free_set_value(set_value
*val
) {
781 free_set_func(val
->func
);
782 free_decl_vars(val
->vars
);
785 for (i
= 0; i
< val
->count
; ++i
) {
786 free_set_value(val
->inner
[i
]);
791 free_num_exp(val
->num
);
796 typedef struct set_stmt
{
802 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
803 set_stmt
*set
= calloc(1, sizeof(*set
));
809 static inline void free_set_stmt(set_stmt
*set
) {
810 free_impl_var(set
->var
);
811 free_set_value(set
->val
);
815 typedef struct return_stmt
{
820 static inline return_stmt
*init_return_stmt(set_value
*val
) {
821 return_stmt
*ret
= calloc(1, sizeof(*ret
));
826 static inline void free_return_stmt(return_stmt
*ret
) {
827 //free_set_func(ret->func);
828 //free_decl_var(ret->decl);
829 free_set_value(ret
->set
);
833 typedef struct free_call
{
839 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
840 free_call
*f
= calloc(1, sizeof(*f
));
841 f
->func
= strdup(func
);
846 static inline void free_free_call(free_call
*f
) {
848 free_decl_vars(f
->vars
);
852 typedef struct free_calls
{
857 static inline free_calls
*init_free_calls(free_call
*f
) {
858 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
861 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
867 static inline void free_free_calls(free_calls
*fcs
) {
870 for (i
= 0; i
< fcs
->count
; ++i
) {
871 free_free_call(fcs
->list
[i
]);
877 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
878 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
879 fcs
->list
[fcs
->count
-1] = f
;
883 typedef struct free_stmt
{
887 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
888 free_stmt
*f
= calloc(1, sizeof(*f
));
893 static inline void free_free_stmt(free_stmt
*f
) {
894 free_free_calls(f
->calls
);
898 typedef struct impl_stmt
{
909 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
910 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
916 static inline void free_impl_stmt(impl_stmt
*stmt
) {
917 switch (stmt
->type
) {
919 free_let_stmt(stmt
->s
.let
);
922 free_set_stmt(stmt
->s
.set
);
925 free_return_stmt(stmt
->s
.ret
);
928 free_free_stmt(stmt
->s
.fre
);
934 typedef struct impl_stmts
{
953 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
954 list
= realloc(list
, count
* sizeof(list
));
955 ((void **)list
)[count
-1] = stmt
;
959 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
960 switch (stmt
->type
) {
962 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
965 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
968 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
971 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
978 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
979 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
980 return add_impl_stmt(stmts
, stmt
);
983 static inline void free_impl_stmts(impl_stmts
*stmts
) {
986 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
987 free_let_stmt(stmts
->let
.list
[i
]);
989 free(stmts
->let
.list
);
990 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
991 free_return_stmt(stmts
->ret
.list
[i
]);
993 free(stmts
->ret
.list
);
994 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
995 free_set_stmt(stmts
->set
.list
[i
]);
997 free(stmts
->set
.list
);
998 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
999 free_free_stmt(stmts
->fre
.list
[i
]);
1001 free(stmts
->fre
.list
);
1005 typedef struct impl
{
1011 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1012 impl
*i
= calloc(1, sizeof(*i
));
1018 static inline void free_impl(impl
*impl
) {
1019 free_impl_func(impl
->func
);
1020 free_impl_stmts(impl
->stmts
);
1024 typedef struct impls
{
1029 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1031 impls
= calloc(1, sizeof(*impls
));
1033 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1034 impls
->list
[impls
->count
-1] = impl
;
1038 static void free_impls(impls
*impls
) {
1041 for (i
= 0; i
< impls
->count
; ++i
) {
1042 free_impl(impls
->list
[i
]);
1048 typedef struct decl_file
{
1053 static inline void free_decl_file(decl_file
*file
) {
1060 memset(file
, 0, sizeof(*file
));
1063 typedef struct decl_libs
{
1068 static inline void free_decl_libs(decl_libs
*libs
) {
1071 for (i
= 0; i
< libs
->count
; ++i
) {
1073 dlclose(libs
->dl
[i
]);
1078 memset(libs
, 0, sizeof(*libs
));
1081 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1082 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1083 libs
->dl
[libs
->count
-1] = dlopened
;
1086 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1089 if (var
->arg
->var
!= var
) for (i
= 1; i
< var
->pointer_level
; ++i
) {
1090 ret_val
= *(void **) ret_val
;
1095 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1096 impl_val
*val
, *val_ptr
;
1099 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
1102 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1103 for (i
= 1; i
< var
->pointer_level
; ++i
) {
1104 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1105 val_ptr
= val_ptr
->ptr
;
1111 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1112 void *ptr
= (char *) struct_ptr
->ptr
+ set_arg
->layout
->pos
;
1113 impl_val
*val
= enref_impl_val(ptr
, set_arg
->var
);
1122 #define PSI_ERROR 16
1123 #define PSI_WARNING 32
1124 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
1126 #define PSI_DATA(D) ((PSI_Data *) (D))
1127 #define PSI_DATA_MEMBERS \
1128 constants *consts; \
1129 decl_typedefs *defs; \
1130 decl_structs *structs; \
1138 typedef struct PSI_Data
{
1142 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1144 dest
= malloc(sizeof(*dest
));
1146 memcpy(dest
, src
, sizeof(*dest
));
1147 memset(src
, 0, sizeof(*src
));
1151 static inline void PSI_DataDtor(PSI_Data
*data
) {
1153 free_constants(data
->consts
);
1156 free_decl_typedefs(data
->defs
);
1158 if (data
->structs
) {
1159 free_decl_structs(data
->structs
);
1162 free_decls(data
->decls
);
1165 free_impls(data
->impls
);
1167 free_decl_file(&data
->psi
.file
);
1170 typedef struct PSI_Parser
{
1178 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1181 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1183 size_t token_len
, fname_len
;
1186 if (P
->cur
< P
->tok
) {
1191 token_len
= P
->cur
- P
->tok
;
1192 fname_len
= strlen(P
->psi
.file
.fn
);
1194 T
= calloc(1, sizeof(*T
) + token_len
+ fname_len
+ 1);
1195 T
->type
= token_typ
;
1196 T
->size
= token_len
;
1199 T
->file
= &T
->buf
[0];
1200 T
->text
= &T
->buf
[fname_len
+ 1];
1202 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1203 memcpy(T
->text
, P
->tok
, token_len
);
1208 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1209 size_t fname_len
= strlen(src
->file
);
1210 size_t strct_len
= sizeof(*src
) + src
->size
+ fname_len
+ 1;
1211 PSI_Token
*ptr
= malloc(strct_len
);
1213 memcpy(ptr
, src
, strct_len
);
1215 ptr
->file
= &ptr
->buf
[0];
1216 ptr
->text
= &ptr
->buf
[fname_len
+ 1];
1221 #define PSI_PARSER_DEBUG 0x1
1223 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1224 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1225 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1226 token_t
PSI_ParserScan(PSI_Parser
*P
);
1227 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1228 void PSI_ParserDtor(PSI_Parser
*P
);
1229 void PSI_ParserFree(PSI_Parser
**P
);