9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
18 size_t psi_t_alignment(token_t
);
19 size_t psi_t_size(token_t
);
21 typedef struct PSI_Token
{
28 typedef struct decl_type
{
31 struct decl_type
*real
;
32 struct decl_struct
*strct
;
35 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
36 decl_type
*t
= calloc(1, sizeof(*t
));
38 t
->name
= strdup(name
);
42 static inline decl_type
*real_decl_type(decl_type
*type
) {
49 static inline void free_decl_type(decl_type
*type
) {
54 typedef struct decl_typedef
{
59 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
60 decl_typedef
*t
= calloc(1, sizeof(*t
));
61 t
->alias
= strdup(name
);
66 static inline void free_decl_typedef(decl_typedef
*t
) {
68 free_decl_type(t
->type
);
72 typedef struct decl_typedefs
{
77 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
79 defs
= calloc(1, sizeof(*defs
));
81 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
82 defs
->list
[defs
->count
-1] = def
;
86 static void free_decl_typedefs(decl_typedefs
*defs
) {
89 for (i
= 0; i
< defs
->count
; ++i
) {
90 free_decl_typedef(defs
->list
[i
]);
96 typedef struct decl_var
{
98 unsigned pointer_level
;
100 struct decl_arg
*arg
;
103 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
104 decl_var
*v
= calloc(1, sizeof(*v
));
105 v
->name
= (char *) strdup((const char *) name
);
106 v
->pointer_level
= pl
;
111 static inline void free_decl_var(decl_var
*var
) {
116 typedef struct decl_struct_layout
{
119 } decl_struct_layout
;
121 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
122 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
129 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
133 typedef struct decl_arg
{
136 decl_struct_layout
*layout
;
137 struct let_stmt
*let
;
140 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
141 decl_arg
*arg
= calloc(1, sizeof(*arg
));
148 static inline void free_decl_arg(decl_arg
*arg
) {
149 free_decl_type(arg
->type
);
150 free_decl_var(arg
->var
);
152 free_decl_struct_layout(arg
->layout
);
157 typedef struct decl_vars
{
162 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
163 decl_vars
*vars
= calloc(1, sizeof(*vars
));
166 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
172 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
173 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
174 vars
->vars
[vars
->count
-1] = var
;
178 static inline void free_decl_vars(decl_vars
*vars
) {
181 for (i
= 0; i
< vars
->count
; ++i
) {
182 free_decl_var(vars
->vars
[i
]);
188 typedef struct decl_args
{
193 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
194 decl_args
*args
= calloc(1, sizeof(*args
));
197 args
->args
= calloc(1, sizeof(*args
->args
));
203 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
204 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
205 args
->args
[args
->count
-1] = arg
;
209 static inline void free_decl_args(decl_args
*args
) {
212 for (i
= 0; i
< args
->count
; ++i
) {
213 free_decl_arg(args
->args
[i
]);
219 typedef struct decl_abi
{
223 static inline decl_abi
*init_decl_abi(const char *convention
) {
224 decl_abi
*abi
= calloc(1, sizeof(*abi
));
225 abi
->convention
= strdup(convention
);
229 static inline void free_decl_abi(decl_abi
*abi
) {
230 free(abi
->convention
);
234 typedef struct decl
{
246 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
247 decl
*d
= calloc(1, sizeof(*d
));
254 static inline void free_decl(decl
*d
) {
255 free_decl_abi(d
->abi
);
256 free_decl_arg(d
->func
);
258 free_decl_args(d
->args
);
263 typedef struct decls
{
268 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
270 decls
= calloc(1, sizeof(*decls
));
272 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
273 decls
->list
[decls
->count
-1] = decl
;
277 static inline void free_decls(decls
*decls
) {
280 for (i
= 0; i
< decls
->count
; ++i
) {
281 free_decl(decls
->list
[i
]);
287 typedef struct decl_struct
{
293 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
294 decl_struct
*s
= calloc(1, sizeof(*s
));
295 s
->name
= strdup(name
);
300 static inline void free_decl_struct(decl_struct
*s
) {
302 free_decl_args(s
->args
);
308 typedef struct decl_structs
{
313 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
315 ss
= calloc(1, sizeof(*ss
));
317 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
318 ss
->list
[ss
->count
-1] = s
;
322 static inline void free_decl_structs(decl_structs
*ss
) {
325 for (i
= 0; i
< ss
->count
; ++i
) {
326 free_decl_struct(ss
->list
[i
]);
332 typedef union impl_val
{
353 uint8_t _dbg
[sizeof(void *)];
356 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
359 if (var
->arg
->var
!= var
) for (i
= 1; i
< var
->pointer_level
; ++i
) {
360 ret_val
= *(void **) ret_val
;
365 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
366 impl_val
*val
, *val_ptr
;
369 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
372 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
373 for (i
= 1; i
< var
->pointer_level
; ++i
) {
374 val_ptr
->ptr
= (void **) val_ptr
+ 1;
375 val_ptr
= val_ptr
->ptr
;
380 typedef struct impl_type
{
385 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
386 impl_type
*t
= calloc(1, sizeof(*t
));
389 t
->name
= strdup(name
);
393 static inline void free_impl_type(impl_type
*type
) {
398 typedef struct impl_var
{
400 unsigned reference
:1;
403 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
404 impl_var
*var
= calloc(1, sizeof(*var
));
405 var
->name
= strdup(name
);
406 var
->reference
= is_reference
;
410 static inline void free_impl_var(impl_var
*var
) {
415 typedef struct impl_def_val
{
420 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
421 impl_def_val
*def
= calloc(1, sizeof(*def
));
423 def
->text
= strdup(text
);
427 static inline void free_impl_def_val(impl_def_val
*def
) {
432 typedef struct impl_arg
{
440 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
441 impl_arg
*arg
= calloc(1, sizeof(*arg
));
448 static inline void free_impl_arg(impl_arg
*arg
) {
449 free_impl_type(arg
->type
);
450 free_impl_var(arg
->var
);
452 free_impl_def_val(arg
->def
);
457 typedef struct impl_args
{
462 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
463 impl_args
*args
= calloc(1, sizeof(*args
));
466 args
->args
= calloc(1, sizeof(*args
->args
));
472 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
473 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
474 args
->args
[args
->count
-1] = arg
;
478 static inline void free_impl_args(impl_args
*args
) {
481 for (i
= 0; i
< args
->count
; ++i
) {
482 free_impl_arg(args
->args
[i
]);
488 typedef struct impl_func
{
491 impl_type
*return_type
;
492 unsigned return_reference
:1;
495 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
496 impl_func
*func
= calloc(1, sizeof(*func
));
497 func
->name
= strdup(name
);
498 func
->args
= args
? args
: init_impl_args(NULL
);
499 func
->return_type
= type
;
500 func
->return_reference
= ret_reference
;
504 static inline void free_impl_func(impl_func
*f
) {
505 free_impl_type(f
->return_type
);
506 free_impl_args(f
->args
);
511 typedef struct let_calloc
{
516 static inline let_calloc
*init_let_calloc(long n
, decl_type
*type
) {
517 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
523 static inline void free_let_calloc(let_calloc
*alloc
) {
524 free_decl_type(alloc
->type
);
528 typedef struct let_func
{
534 static inline let_func
*init_let_func(token_t type
, const char *name
, let_calloc
*alloc
) {
535 let_func
*func
= calloc(1, sizeof(*func
));
537 func
->name
= strdup(name
);
542 static inline void free_let_func(let_func
*func
) {
544 free_let_calloc(func
->alloc
);
550 typedef struct let_value
{
553 unsigned is_reference
:1;
556 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
557 let_value
*val
= calloc(1, sizeof(*val
));
558 val
->is_reference
= is_reference
;
564 static inline void free_let_value(let_value
*val
) {
566 free_let_func(val
->func
);
569 free_impl_var(val
->var
);
574 typedef struct let_stmt
{
583 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
584 let_stmt
*let
= calloc(1, sizeof(*let
));
590 static inline void free_let_stmt(let_stmt
*stmt
) {
591 free_decl_var(stmt
->var
);
593 free_let_value(stmt
->val
);
600 typedef struct set_func
{
603 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
606 static inline set_func
*init_set_func(token_t type
, const char *name
) {
607 set_func
*func
= calloc(1, sizeof(*func
));
609 func
->name
= strdup(name
);
613 static inline void free_set_func(set_func
*func
) {
618 typedef struct set_value
{
622 struct set_value
*set
;
625 struct set_value
**inner
;
629 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
630 set_value
*val
= calloc(1, sizeof(*val
));
635 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
636 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
637 val
->inner
[val
->count
-1] = inner
;
641 static inline void free_set_value(set_value
*val
) {
642 free_set_func(val
->func
);
643 free_decl_vars(val
->vars
);
646 for (i
= 0; i
< val
->count
; ++i
) {
647 free_set_value(val
->inner
[i
]);
654 typedef struct set_stmt
{
660 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
661 set_stmt
*set
= calloc(1, sizeof(*set
));
667 static inline void free_set_stmt(set_stmt
*set
) {
668 free_impl_var(set
->var
);
669 free_set_value(set
->val
);
673 typedef struct return_stmt
{
678 static inline return_stmt
*init_return_stmt(set_value
*val
) {
679 return_stmt
*ret
= calloc(1, sizeof(*ret
));
684 static inline void free_return_stmt(return_stmt
*ret
) {
685 //free_set_func(ret->func);
686 //free_decl_var(ret->decl);
687 free_set_value(ret
->set
);
691 typedef struct free_call
{
697 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
698 free_call
*f
= calloc(1, sizeof(*f
));
699 f
->func
= strdup(func
);
704 static inline void free_free_call(free_call
*f
) {
709 typedef struct free_calls
{
714 static inline free_calls
*init_free_calls(free_call
*f
) {
715 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
718 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
724 static inline void free_free_calls(free_calls
*fcs
) {
727 for (i
= 0; i
< fcs
->count
; ++i
) {
728 free_free_call(fcs
->list
[i
]);
734 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
735 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
736 fcs
->list
[fcs
->count
-1] = f
;
740 typedef struct free_stmt
{
744 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
745 free_stmt
*f
= calloc(1, sizeof(*f
));
750 static inline void free_free_stmt(free_stmt
*f
) {
751 free_free_calls(f
->calls
);
755 typedef struct impl_stmt
{
766 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
767 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
773 static inline void free_impl_stmt(impl_stmt
*stmt
) {
774 switch (stmt
->type
) {
776 free_let_stmt(stmt
->s
.let
);
779 free_set_stmt(stmt
->s
.set
);
782 free_return_stmt(stmt
->s
.ret
);
785 free_free_stmt(stmt
->s
.fre
);
791 typedef struct impl_stmts
{
810 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
811 list
= realloc(list
, count
* sizeof(list
));
812 ((void **)list
)[count
-1] = stmt
;
816 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
817 switch (stmt
->type
) {
819 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
822 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
825 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
828 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
835 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
836 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
837 return add_impl_stmt(stmts
, stmt
);
840 static inline void free_impl_stmts(impl_stmts
*stmts
) {
843 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
844 free_let_stmt(stmts
->let
.list
[i
]);
846 free(stmts
->let
.list
);
847 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
848 free_return_stmt(stmts
->ret
.list
[i
]);
850 free(stmts
->ret
.list
);
851 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
852 free_set_stmt(stmts
->set
.list
[i
]);
854 free(stmts
->set
.list
);
855 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
856 free_free_stmt(stmts
->fre
.list
[i
]);
858 free(stmts
->fre
.list
);
862 typedef struct impl
{
868 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
869 impl
*i
= calloc(1, sizeof(*i
));
875 static inline void free_impl(impl
*impl
) {
876 free_impl_func(impl
->func
);
877 free_impl_stmts(impl
->stmts
);
881 typedef struct impls
{
886 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
888 impls
= calloc(1, sizeof(*impls
));
890 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
891 impls
->list
[impls
->count
-1] = impl
;
895 static void free_impls(impls
*impls
) {
898 for (i
= 0; i
< impls
->count
; ++i
) {
899 free_impl(impls
->list
[i
]);
905 typedef struct const_type
{
910 static inline const_type
*init_const_type(token_t type
, const char *name
) {
911 const_type
*ct
= calloc(1, sizeof(*ct
));
913 ct
->name
= strdup(name
);
917 static inline void free_const_type(const_type
*type
) {
922 typedef struct constant
{
928 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
929 constant
*c
= calloc(1, sizeof(*c
));
931 c
->name
= strdup(name
);
936 static inline void free_constant(constant
*constant
) {
937 free_const_type(constant
->type
);
938 free(constant
->name
);
939 free_impl_def_val(constant
->val
);
943 typedef struct constants
{
948 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
950 constants
= calloc(1, sizeof(*constants
));
952 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
953 constants
->list
[constants
->count
-1] = constant
;
957 static inline void free_constants(constants
*c
) {
960 for (i
= 0; i
< c
->count
; ++i
) {
961 free_constant(c
->list
[i
]);
968 #define PSI_WARNING 32
969 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
971 typedef struct decl_file
{
976 static inline void free_decl_file(decl_file
*file
) {
983 memset(file
, 0, sizeof(*file
));
986 typedef struct decl_libs
{
991 static inline void free_decl_libs(decl_libs
*libs
) {
994 for (i
= 0; i
< libs
->count
; ++i
) {
996 dlclose(libs
->dl
[i
]);
1001 memset(libs
, 0, sizeof(*libs
));
1004 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1005 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1006 libs
->dl
[libs
->count
-1] = dlopened
;
1009 #define PSI_DATA(D) ((PSI_Data *) (D))
1010 #define PSI_DATA_MEMBERS \
1011 constants *consts; \
1012 decl_typedefs *defs; \
1013 decl_structs *structs; \
1021 typedef struct PSI_Data
{
1025 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1027 dest
= malloc(sizeof(*dest
));
1029 memcpy(dest
, src
, sizeof(*dest
));
1030 memset(src
, 0, sizeof(*src
));
1034 static inline void PSI_DataDtor(PSI_Data
*data
) {
1036 free_constants(data
->consts
);
1039 free_decl_typedefs(data
->defs
);
1041 if (data
->structs
) {
1042 free_decl_structs(data
->structs
);
1045 free_decls(data
->decls
);
1048 free_impls(data
->impls
);
1050 free_decl_file(&data
->psi
.file
);
1053 typedef struct PSI_Parser
{
1061 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1064 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1068 if (P
->cur
< P
->tok
) {
1072 token_len
= P
->cur
- P
->tok
;
1074 T
= calloc(1, sizeof(*T
) + token_len
);
1077 T
->size
= token_len
;
1078 T
->text
[token_len
] = 0;
1079 memcpy(T
->text
, P
->tok
, token_len
);
1084 #define PSI_PARSER_DEBUG 0x1
1086 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1087 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1088 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1089 token_t
PSI_ParserScan(PSI_Parser
*P
);
1090 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
1091 void PSI_ParserDtor(PSI_Parser
*P
);
1092 void PSI_ParserFree(PSI_Parser
**P
);