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
{
732 struct set_value
*set
;
735 struct set_value
**inner
;
739 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
740 set_value
*val
= calloc(1, sizeof(*val
));
745 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
746 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
747 val
->inner
[val
->count
-1] = inner
;
751 static inline void free_set_value(set_value
*val
) {
752 free_set_func(val
->func
);
753 free_decl_vars(val
->vars
);
756 for (i
= 0; i
< val
->count
; ++i
) {
757 free_set_value(val
->inner
[i
]);
764 typedef struct set_stmt
{
770 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
771 set_stmt
*set
= calloc(1, sizeof(*set
));
777 static inline void free_set_stmt(set_stmt
*set
) {
778 free_impl_var(set
->var
);
779 free_set_value(set
->val
);
783 typedef struct return_stmt
{
788 static inline return_stmt
*init_return_stmt(set_value
*val
) {
789 return_stmt
*ret
= calloc(1, sizeof(*ret
));
794 static inline void free_return_stmt(return_stmt
*ret
) {
795 //free_set_func(ret->func);
796 //free_decl_var(ret->decl);
797 free_set_value(ret
->set
);
801 typedef struct free_call
{
807 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
808 free_call
*f
= calloc(1, sizeof(*f
));
809 f
->func
= strdup(func
);
814 static inline void free_free_call(free_call
*f
) {
819 typedef struct free_calls
{
824 static inline free_calls
*init_free_calls(free_call
*f
) {
825 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
828 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
834 static inline void free_free_calls(free_calls
*fcs
) {
837 for (i
= 0; i
< fcs
->count
; ++i
) {
838 free_free_call(fcs
->list
[i
]);
844 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
845 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
846 fcs
->list
[fcs
->count
-1] = f
;
850 typedef struct free_stmt
{
854 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
855 free_stmt
*f
= calloc(1, sizeof(*f
));
860 static inline void free_free_stmt(free_stmt
*f
) {
861 free_free_calls(f
->calls
);
865 typedef struct impl_stmt
{
876 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
877 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
883 static inline void free_impl_stmt(impl_stmt
*stmt
) {
884 switch (stmt
->type
) {
886 free_let_stmt(stmt
->s
.let
);
889 free_set_stmt(stmt
->s
.set
);
892 free_return_stmt(stmt
->s
.ret
);
895 free_free_stmt(stmt
->s
.fre
);
901 typedef struct impl_stmts
{
920 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
921 list
= realloc(list
, count
* sizeof(list
));
922 ((void **)list
)[count
-1] = stmt
;
926 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
927 switch (stmt
->type
) {
929 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
932 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
935 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
938 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
945 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
946 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
947 return add_impl_stmt(stmts
, stmt
);
950 static inline void free_impl_stmts(impl_stmts
*stmts
) {
953 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
954 free_let_stmt(stmts
->let
.list
[i
]);
956 free(stmts
->let
.list
);
957 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
958 free_return_stmt(stmts
->ret
.list
[i
]);
960 free(stmts
->ret
.list
);
961 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
962 free_set_stmt(stmts
->set
.list
[i
]);
964 free(stmts
->set
.list
);
965 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
966 free_free_stmt(stmts
->fre
.list
[i
]);
968 free(stmts
->fre
.list
);
972 typedef struct impl
{
978 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
979 impl
*i
= calloc(1, sizeof(*i
));
985 static inline void free_impl(impl
*impl
) {
986 free_impl_func(impl
->func
);
987 free_impl_stmts(impl
->stmts
);
991 typedef struct impls
{
996 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
998 impls
= calloc(1, sizeof(*impls
));
1000 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1001 impls
->list
[impls
->count
-1] = impl
;
1005 static void free_impls(impls
*impls
) {
1008 for (i
= 0; i
< impls
->count
; ++i
) {
1009 free_impl(impls
->list
[i
]);
1016 #define PSI_ERROR 16
1017 #define PSI_WARNING 32
1018 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
1020 typedef struct decl_file
{
1025 static inline void free_decl_file(decl_file
*file
) {
1032 memset(file
, 0, sizeof(*file
));
1035 typedef struct decl_libs
{
1040 static inline void free_decl_libs(decl_libs
*libs
) {
1043 for (i
= 0; i
< libs
->count
; ++i
) {
1045 dlclose(libs
->dl
[i
]);
1050 memset(libs
, 0, sizeof(*libs
));
1053 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1054 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1055 libs
->dl
[libs
->count
-1] = dlopened
;
1058 #define PSI_DATA(D) ((PSI_Data *) (D))
1059 #define PSI_DATA_MEMBERS \
1060 constants *consts; \
1061 decl_typedefs *defs; \
1062 decl_structs *structs; \
1070 typedef struct PSI_Data
{
1074 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1076 dest
= malloc(sizeof(*dest
));
1078 memcpy(dest
, src
, sizeof(*dest
));
1079 memset(src
, 0, sizeof(*src
));
1083 static inline void PSI_DataDtor(PSI_Data
*data
) {
1085 free_constants(data
->consts
);
1088 free_decl_typedefs(data
->defs
);
1090 if (data
->structs
) {
1091 free_decl_structs(data
->structs
);
1094 free_decls(data
->decls
);
1097 free_impls(data
->impls
);
1099 free_decl_file(&data
->psi
.file
);
1102 typedef struct PSI_Parser
{
1110 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1113 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1117 if (P
->cur
< P
->tok
) {
1121 token_len
= P
->cur
- P
->tok
;
1123 T
= calloc(1, sizeof(*T
) + token_len
);
1126 T
->size
= token_len
;
1127 T
->text
[token_len
] = 0;
1128 memcpy(T
->text
, P
->tok
, token_len
);
1133 #define PSI_PARSER_DEBUG 0x1
1135 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1136 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1137 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1138 token_t
PSI_ParserScan(PSI_Parser
*P
);
1139 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
1140 void PSI_ParserDtor(PSI_Parser
*P
);
1141 void PSI_ParserFree(PSI_Parser
**P
);