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 struct decl_type
{
32 struct decl_type
*real
;
33 struct decl_struct
*strct
;
36 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
37 decl_type
*t
= calloc(1, sizeof(*t
));
39 t
->name
= strdup(name
);
43 static inline decl_type
*real_decl_type(decl_type
*type
) {
50 static inline void free_decl_type(decl_type
*type
) {
55 typedef struct decl_typedef
{
60 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
61 decl_typedef
*t
= calloc(1, sizeof(*t
));
62 t
->alias
= strdup(name
);
67 static inline void free_decl_typedef(decl_typedef
*t
) {
69 free_decl_type(t
->type
);
73 typedef struct decl_typedefs
{
78 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
80 defs
= calloc(1, sizeof(*defs
));
82 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
83 defs
->list
[defs
->count
-1] = def
;
87 static void free_decl_typedefs(decl_typedefs
*defs
) {
90 for (i
= 0; i
< defs
->count
; ++i
) {
91 free_decl_typedef(defs
->list
[i
]);
97 typedef struct decl_var
{
99 unsigned pointer_level
;
101 struct decl_arg
*arg
;
104 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
105 decl_var
*v
= calloc(1, sizeof(*v
));
106 v
->name
= (char *) strdup((const char *) name
);
107 v
->pointer_level
= pl
;
112 static inline void free_decl_var(decl_var
*var
) {
117 typedef struct decl_struct_layout
{
120 } decl_struct_layout
;
122 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
123 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
130 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
134 typedef struct decl_arg
{
137 decl_struct_layout
*layout
;
138 struct let_stmt
*let
;
141 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
142 decl_arg
*arg
= calloc(1, sizeof(*arg
));
149 static inline void free_decl_arg(decl_arg
*arg
) {
150 free_decl_type(arg
->type
);
151 free_decl_var(arg
->var
);
153 free_decl_struct_layout(arg
->layout
);
158 typedef struct decl_vars
{
163 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
164 decl_vars
*vars
= calloc(1, sizeof(*vars
));
167 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
173 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
174 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
175 vars
->vars
[vars
->count
-1] = var
;
179 static inline void free_decl_vars(decl_vars
*vars
) {
182 for (i
= 0; i
< vars
->count
; ++i
) {
183 free_decl_var(vars
->vars
[i
]);
189 typedef struct decl_args
{
194 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
195 decl_args
*args
= calloc(1, sizeof(*args
));
198 args
->args
= calloc(1, sizeof(*args
->args
));
204 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
205 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
206 args
->args
[args
->count
-1] = arg
;
210 static inline void free_decl_args(decl_args
*args
) {
213 for (i
= 0; i
< args
->count
; ++i
) {
214 free_decl_arg(args
->args
[i
]);
220 typedef struct decl_abi
{
224 static inline decl_abi
*init_decl_abi(const char *convention
) {
225 decl_abi
*abi
= calloc(1, sizeof(*abi
));
226 abi
->convention
= strdup(convention
);
230 static inline void free_decl_abi(decl_abi
*abi
) {
231 free(abi
->convention
);
235 typedef struct decl
{
247 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
248 decl
*d
= calloc(1, sizeof(*d
));
255 static inline void free_decl(decl
*d
) {
256 free_decl_abi(d
->abi
);
257 free_decl_arg(d
->func
);
259 free_decl_args(d
->args
);
264 typedef struct decls
{
269 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
271 decls
= calloc(1, sizeof(*decls
));
273 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
274 decls
->list
[decls
->count
-1] = decl
;
278 static inline void free_decls(decls
*decls
) {
281 for (i
= 0; i
< decls
->count
; ++i
) {
282 free_decl(decls
->list
[i
]);
288 typedef struct decl_struct
{
294 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
295 decl_struct
*s
= calloc(1, sizeof(*s
));
296 s
->name
= strdup(name
);
301 static inline void free_decl_struct(decl_struct
*s
) {
303 free_decl_args(s
->args
);
309 typedef struct decl_structs
{
314 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
316 ss
= calloc(1, sizeof(*ss
));
318 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
319 ss
->list
[ss
->count
-1] = s
;
323 static inline void free_decl_structs(decl_structs
*ss
) {
326 for (i
= 0; i
< ss
->count
; ++i
) {
327 free_decl_struct(ss
->list
[i
]);
333 typedef union impl_val
{
354 uint8_t _dbg
[sizeof(void *)];
357 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
360 if (var
->arg
->var
!= var
) for (i
= 1; i
< var
->pointer_level
; ++i
) {
361 ret_val
= *(void **) ret_val
;
366 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
367 impl_val
*val
, *val_ptr
;
370 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
373 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
374 for (i
= 1; i
< var
->pointer_level
; ++i
) {
375 val_ptr
->ptr
= (void **) val_ptr
+ 1;
376 val_ptr
= val_ptr
->ptr
;
382 typedef struct impl_type
{
387 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
388 impl_type
*t
= calloc(1, sizeof(*t
));
391 t
->name
= strdup(name
);
395 static inline void free_impl_type(impl_type
*type
) {
400 typedef struct impl_var
{
402 unsigned reference
:1;
405 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
406 impl_var
*var
= calloc(1, sizeof(*var
));
407 var
->name
= strdup(name
);
408 var
->reference
= is_reference
;
412 static inline void free_impl_var(impl_var
*var
) {
417 typedef struct impl_def_val
{
422 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
423 impl_def_val
*def
= calloc(1, sizeof(*def
));
425 def
->text
= strdup(text
);
429 static inline void free_impl_def_val(impl_def_val
*def
) {
434 typedef struct const_type
{
439 static inline const_type
*init_const_type(token_t type
, const char *name
) {
440 const_type
*ct
= calloc(1, sizeof(*ct
));
442 ct
->name
= strdup(name
);
446 static inline void free_const_type(const_type
*type
) {
451 typedef struct constant
{
457 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
458 constant
*c
= calloc(1, sizeof(*c
));
460 c
->name
= strdup(name
);
465 static inline void free_constant(constant
*constant
) {
466 free_const_type(constant
->type
);
467 free(constant
->name
);
468 free_impl_def_val(constant
->val
);
472 typedef struct constants
{
477 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
479 constants
= calloc(1, sizeof(*constants
));
481 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
482 constants
->list
[constants
->count
-1] = constant
;
486 static inline void free_constants(constants
*c
) {
489 for (i
= 0; i
< c
->count
; ++i
) {
490 free_constant(c
->list
[i
]);
496 typedef struct impl_arg
{
504 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
505 impl_arg
*arg
= calloc(1, sizeof(*arg
));
512 static inline void free_impl_arg(impl_arg
*arg
) {
513 free_impl_type(arg
->type
);
514 free_impl_var(arg
->var
);
516 free_impl_def_val(arg
->def
);
521 typedef struct impl_args
{
526 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
527 impl_args
*args
= calloc(1, sizeof(*args
));
530 args
->args
= calloc(1, sizeof(*args
->args
));
536 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
537 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
538 args
->args
[args
->count
-1] = arg
;
542 static inline void free_impl_args(impl_args
*args
) {
545 for (i
= 0; i
< args
->count
; ++i
) {
546 free_impl_arg(args
->args
[i
]);
552 typedef struct impl_func
{
555 impl_type
*return_type
;
556 unsigned return_reference
:1;
559 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
560 impl_func
*func
= calloc(1, sizeof(*func
));
561 func
->name
= strdup(name
);
562 func
->args
= args
? args
: init_impl_args(NULL
);
563 func
->return_type
= type
;
564 func
->return_reference
= ret_reference
;
568 static inline void free_impl_func(impl_func
*f
) {
569 free_impl_type(f
->return_type
);
570 free_impl_args(f
->args
);
575 typedef struct num_exp
{
583 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
584 struct num_exp
*operand
;
587 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
588 num_exp
*exp
= calloc(1, sizeof(*exp
));
589 switch (exp
->t
= t
) {
592 exp
->u
.numb
= strdup(num
);
597 EMPTY_SWITCH_DEFAULT_CASE();
602 static inline void free_num_exp(num_exp
*exp
) {
610 free_decl_var(exp
->u
.dvar
);
612 EMPTY_SWITCH_DEFAULT_CASE();
615 free_num_exp(exp
->operand
);
620 typedef struct let_calloc
{
625 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
626 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
627 alloc
->nmemb
= nmemb
;
632 static inline void free_let_calloc(let_calloc
*alloc
) {
633 free_num_exp(alloc
->nmemb
);
634 free_num_exp(alloc
->size
);
638 typedef struct let_func
{
644 static inline let_func
*init_let_func(token_t type
, const char *name
, let_calloc
*alloc
) {
645 let_func
*func
= calloc(1, sizeof(*func
));
647 func
->name
= strdup(name
);
652 static inline void free_let_func(let_func
*func
) {
654 free_let_calloc(func
->alloc
);
660 typedef struct let_value
{
663 unsigned is_reference
:1;
666 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
667 let_value
*val
= calloc(1, sizeof(*val
));
668 val
->is_reference
= is_reference
;
674 static inline void free_let_value(let_value
*val
) {
676 free_let_func(val
->func
);
679 free_impl_var(val
->var
);
684 typedef struct let_stmt
{
693 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
694 let_stmt
*let
= calloc(1, sizeof(*let
));
700 static inline void free_let_stmt(let_stmt
*stmt
) {
701 free_decl_var(stmt
->var
);
703 free_let_value(stmt
->val
);
710 typedef struct set_func
{
713 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
716 static inline set_func
*init_set_func(token_t type
, const char *name
) {
717 set_func
*func
= calloc(1, sizeof(*func
));
719 func
->name
= strdup(name
);
723 static inline void free_set_func(set_func
*func
) {
728 typedef struct set_value
{
733 struct set_value
*set
;
736 struct set_value
**inner
;
740 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
741 set_value
*val
= calloc(1, sizeof(*val
));
746 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
747 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
748 val
->inner
[val
->count
-1] = inner
;
752 static inline void free_set_value(set_value
*val
) {
753 free_set_func(val
->func
);
754 free_decl_vars(val
->vars
);
757 for (i
= 0; i
< val
->count
; ++i
) {
758 free_set_value(val
->inner
[i
]);
765 typedef struct set_stmt
{
771 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
772 set_stmt
*set
= calloc(1, sizeof(*set
));
778 static inline void free_set_stmt(set_stmt
*set
) {
779 free_impl_var(set
->var
);
780 free_set_value(set
->val
);
784 typedef struct return_stmt
{
789 static inline return_stmt
*init_return_stmt(set_value
*val
) {
790 return_stmt
*ret
= calloc(1, sizeof(*ret
));
795 static inline void free_return_stmt(return_stmt
*ret
) {
796 //free_set_func(ret->func);
797 //free_decl_var(ret->decl);
798 free_set_value(ret
->set
);
802 typedef struct free_call
{
808 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
809 free_call
*f
= calloc(1, sizeof(*f
));
810 f
->func
= strdup(func
);
815 static inline void free_free_call(free_call
*f
) {
820 typedef struct free_calls
{
825 static inline free_calls
*init_free_calls(free_call
*f
) {
826 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
829 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
835 static inline void free_free_calls(free_calls
*fcs
) {
838 for (i
= 0; i
< fcs
->count
; ++i
) {
839 free_free_call(fcs
->list
[i
]);
845 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
846 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
847 fcs
->list
[fcs
->count
-1] = f
;
851 typedef struct free_stmt
{
855 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
856 free_stmt
*f
= calloc(1, sizeof(*f
));
861 static inline void free_free_stmt(free_stmt
*f
) {
862 free_free_calls(f
->calls
);
866 typedef struct impl_stmt
{
877 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
878 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
884 static inline void free_impl_stmt(impl_stmt
*stmt
) {
885 switch (stmt
->type
) {
887 free_let_stmt(stmt
->s
.let
);
890 free_set_stmt(stmt
->s
.set
);
893 free_return_stmt(stmt
->s
.ret
);
896 free_free_stmt(stmt
->s
.fre
);
902 typedef struct impl_stmts
{
921 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
922 list
= realloc(list
, count
* sizeof(list
));
923 ((void **)list
)[count
-1] = stmt
;
927 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
928 switch (stmt
->type
) {
930 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
933 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
936 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
939 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
946 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
947 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
948 return add_impl_stmt(stmts
, stmt
);
951 static inline void free_impl_stmts(impl_stmts
*stmts
) {
954 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
955 free_let_stmt(stmts
->let
.list
[i
]);
957 free(stmts
->let
.list
);
958 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
959 free_return_stmt(stmts
->ret
.list
[i
]);
961 free(stmts
->ret
.list
);
962 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
963 free_set_stmt(stmts
->set
.list
[i
]);
965 free(stmts
->set
.list
);
966 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
967 free_free_stmt(stmts
->fre
.list
[i
]);
969 free(stmts
->fre
.list
);
973 typedef struct impl
{
979 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
980 impl
*i
= calloc(1, sizeof(*i
));
986 static inline void free_impl(impl
*impl
) {
987 free_impl_func(impl
->func
);
988 free_impl_stmts(impl
->stmts
);
992 typedef struct impls
{
997 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
999 impls
= calloc(1, sizeof(*impls
));
1001 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1002 impls
->list
[impls
->count
-1] = impl
;
1006 static void free_impls(impls
*impls
) {
1009 for (i
= 0; i
< impls
->count
; ++i
) {
1010 free_impl(impls
->list
[i
]);
1016 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1017 void *ptr
= (char *) struct_ptr
->ptr
+ set_arg
->layout
->pos
;
1018 impl_val
*val
= enref_impl_val(ptr
, set_arg
->var
);
1027 #define PSI_ERROR 16
1028 #define PSI_WARNING 32
1029 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
1031 typedef struct decl_file
{
1036 static inline void free_decl_file(decl_file
*file
) {
1043 memset(file
, 0, sizeof(*file
));
1046 typedef struct decl_libs
{
1051 static inline void free_decl_libs(decl_libs
*libs
) {
1054 for (i
= 0; i
< libs
->count
; ++i
) {
1056 dlclose(libs
->dl
[i
]);
1061 memset(libs
, 0, sizeof(*libs
));
1064 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1065 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1066 libs
->dl
[libs
->count
-1] = dlopened
;
1069 #define PSI_DATA(D) ((PSI_Data *) (D))
1070 #define PSI_DATA_MEMBERS \
1071 constants *consts; \
1072 decl_typedefs *defs; \
1073 decl_structs *structs; \
1081 typedef struct PSI_Data
{
1085 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1087 dest
= malloc(sizeof(*dest
));
1089 memcpy(dest
, src
, sizeof(*dest
));
1090 memset(src
, 0, sizeof(*src
));
1094 static inline void PSI_DataDtor(PSI_Data
*data
) {
1096 free_constants(data
->consts
);
1099 free_decl_typedefs(data
->defs
);
1101 if (data
->structs
) {
1102 free_decl_structs(data
->structs
);
1105 free_decls(data
->decls
);
1108 free_impls(data
->impls
);
1110 free_decl_file(&data
->psi
.file
);
1113 typedef struct PSI_Parser
{
1121 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1124 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1128 if (P
->cur
< P
->tok
) {
1132 token_len
= P
->cur
- P
->tok
;
1134 T
= calloc(1, sizeof(*T
) + token_len
);
1137 T
->size
= token_len
;
1138 T
->text
[token_len
] = 0;
1139 memcpy(T
->text
, P
->tok
, token_len
);
1144 #define PSI_PARSER_DEBUG 0x1
1146 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1147 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1148 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1149 token_t
PSI_ParserScan(PSI_Parser
*P
);
1150 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
1151 void PSI_ParserDtor(PSI_Parser
*P
);
1152 void PSI_ParserFree(PSI_Parser
**P
);