6dcd6bf64ebf44ebdfdcdf22a6d498ba5c9b5f22
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
));
147 static inline void free_decl_arg(decl_arg
*arg
) {
148 free_decl_type(arg
->type
);
149 free_decl_var(arg
->var
);
151 free_decl_struct_layout(arg
->layout
);
156 typedef struct decl_vars
{
161 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
162 decl_vars
*vars
= calloc(1, sizeof(*vars
));
165 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
171 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
172 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
173 vars
->vars
[vars
->count
-1] = var
;
177 static inline void free_decl_vars(decl_vars
*vars
) {
180 for (i
= 0; i
< vars
->count
; ++i
) {
181 free_decl_var(vars
->vars
[i
]);
187 typedef struct decl_args
{
192 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
193 decl_args
*args
= calloc(1, sizeof(*args
));
196 args
->args
= calloc(1, sizeof(*args
->args
));
202 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
203 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
204 args
->args
[args
->count
-1] = arg
;
208 static inline void free_decl_args(decl_args
*args
) {
211 for (i
= 0; i
< args
->count
; ++i
) {
212 free_decl_arg(args
->args
[i
]);
218 typedef struct decl_abi
{
222 static inline decl_abi
*init_decl_abi(const char *convention
) {
223 decl_abi
*abi
= calloc(1, sizeof(*abi
));
224 abi
->convention
= strdup(convention
);
228 static inline void free_decl_abi(decl_abi
*abi
) {
229 free(abi
->convention
);
233 typedef struct decl
{
240 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
241 decl
*d
= calloc(1, sizeof(*d
));
248 static inline void free_decl(decl
*d
) {
249 free_decl_abi(d
->abi
);
250 free_decl_arg(d
->func
);
252 free_decl_args(d
->args
);
257 typedef struct decls
{
262 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
264 decls
= calloc(1, sizeof(*decls
));
266 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
267 decls
->list
[decls
->count
-1] = decl
;
271 static inline void free_decls(decls
*decls
) {
274 for (i
= 0; i
< decls
->count
; ++i
) {
275 free_decl(decls
->list
[i
]);
281 typedef struct decl_struct
{
287 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
288 decl_struct
*s
= calloc(1, sizeof(*s
));
289 s
->name
= strdup(name
);
294 static inline void free_decl_struct(decl_struct
*s
) {
296 free_decl_args(s
->args
);
302 typedef struct decl_structs
{
307 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
309 ss
= calloc(1, sizeof(*ss
));
311 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
312 ss
->list
[ss
->count
-1] = s
;
316 static inline void free_decl_structs(decl_structs
*ss
) {
319 for (i
= 0; i
< ss
->count
; ++i
) {
320 free_decl_struct(ss
->list
[i
]);
326 typedef union impl_val
{
343 uint8_t _dbg
[sizeof(void *)];
346 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
349 if (var
->arg
->var
!= var
) for (i
= 0; i
< var
->pointer_level
; ++i
) {
350 ret_val
= *(void **) ret_val
;
355 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
356 impl_val
*val
, *val_ptr
;
359 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
362 val
= val_ptr
= calloc(var
->pointer_level
, sizeof(void *));
363 for (i
= 1; i
< var
->pointer_level
; ++i
) {
364 val_ptr
->ptr
= (void **) val_ptr
+ 1;
365 val_ptr
= val_ptr
->ptr
;
370 typedef struct impl_type
{
375 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
376 impl_type
*t
= calloc(1, sizeof(*t
));
379 t
->name
= strdup(name
);
383 static inline void free_impl_type(impl_type
*type
) {
388 typedef struct impl_var
{
390 unsigned reference
:1;
393 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
394 impl_var
*var
= calloc(1, sizeof(*var
));
395 var
->name
= strdup(name
);
396 var
->reference
= is_reference
;
400 static inline void free_impl_var(impl_var
*var
) {
405 typedef struct impl_def_val
{
410 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
411 impl_def_val
*def
= calloc(1, sizeof(*def
));
413 def
->text
= strdup(text
);
417 static inline void free_impl_def_val(impl_def_val
*def
) {
422 typedef struct impl_arg
{
430 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
431 impl_arg
*arg
= calloc(1, sizeof(*arg
));
438 static inline void free_impl_arg(impl_arg
*arg
) {
439 free_impl_type(arg
->type
);
440 free_impl_var(arg
->var
);
442 free_impl_def_val(arg
->def
);
447 typedef struct impl_args
{
452 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
453 impl_args
*args
= calloc(1, sizeof(*args
));
456 args
->args
= calloc(1, sizeof(*args
->args
));
462 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
463 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
464 args
->args
[args
->count
-1] = arg
;
468 static inline void free_impl_args(impl_args
*args
) {
471 for (i
= 0; i
< args
->count
; ++i
) {
472 free_impl_arg(args
->args
[i
]);
478 typedef struct impl_func
{
481 impl_type
*return_type
;
482 unsigned return_reference
:1;
485 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
486 impl_func
*func
= calloc(1, sizeof(*func
));
487 func
->name
= strdup(name
);
488 func
->args
= args
? args
: init_impl_args(NULL
);
489 func
->return_type
= type
;
490 func
->return_reference
= ret_reference
;
494 static inline void free_impl_func(impl_func
*f
) {
495 free_impl_type(f
->return_type
);
496 free_impl_args(f
->args
);
501 typedef struct let_calloc
{
506 static inline let_calloc
*init_let_calloc(long n
, decl_type
*type
) {
507 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
513 static inline void free_let_calloc(let_calloc
*alloc
) {
514 free_decl_type(alloc
->type
);
518 typedef struct let_func
{
524 static inline let_func
*init_let_func(token_t type
, const char *name
, let_calloc
*alloc
) {
525 let_func
*func
= calloc(1, sizeof(*func
));
527 func
->name
= strdup(name
);
532 static inline void free_let_func(let_func
*func
) {
534 free_let_calloc(func
->alloc
);
540 typedef struct let_value
{
543 unsigned is_reference
:1;
546 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
547 let_value
*val
= calloc(1, sizeof(*val
));
548 val
->is_reference
= is_reference
;
554 static inline void free_let_value(let_value
*val
) {
556 free_let_func(val
->func
);
559 free_impl_var(val
->var
);
564 typedef struct let_stmt
{
573 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
574 let_stmt
*let
= calloc(1, sizeof(*let
));
580 static inline void free_let_stmt(let_stmt
*stmt
) {
581 free_decl_var(stmt
->var
);
583 free_let_value(stmt
->val
);
590 typedef struct set_func
{
593 void (*handler
)(zval
*, token_t
, impl_val
*, struct set_value
*set
, decl_var
*);
596 static inline set_func
*init_set_func(token_t type
, const char *name
) {
597 set_func
*func
= calloc(1, sizeof(*func
));
599 func
->name
= strdup(name
);
603 static inline void free_set_func(set_func
*func
) {
608 typedef struct set_value
{
611 struct set_value
**inner
;
615 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
616 set_value
*val
= calloc(1, sizeof(*val
));
621 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
622 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
623 val
->inner
[val
->count
-1] = inner
;
627 static inline void free_set_value(set_value
*val
) {
628 free_set_func(val
->func
);
629 free_decl_vars(val
->vars
);
632 for (i
= 0; i
< val
->count
; ++i
) {
633 free_set_value(val
->inner
[i
]);
640 typedef struct set_stmt
{
646 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
647 set_stmt
*set
= calloc(1, sizeof(*set
));
653 static inline void free_set_stmt(set_stmt
*set
) {
654 free_impl_var(set
->var
);
655 free_set_value(set
->val
);
659 typedef struct return_stmt
{
664 static inline return_stmt
*init_return_stmt(set_value
*val
) {
665 return_stmt
*ret
= calloc(1, sizeof(*ret
));
670 static inline void free_return_stmt(return_stmt
*ret
) {
671 //free_set_func(ret->func);
672 //free_decl_var(ret->decl);
673 free_set_value(ret
->set
);
677 typedef struct free_stmt
{
681 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
682 free_stmt
*free_
= calloc(1, sizeof(*free_
));
687 static inline void free_free_stmt(free_stmt
*free_
) {
688 free_decl_vars(free_
->vars
);
692 typedef struct impl_stmt
{
703 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
704 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
710 static inline void free_impl_stmt(impl_stmt
*stmt
) {
711 switch (stmt
->type
) {
713 free_let_stmt(stmt
->s
.let
);
716 free_set_stmt(stmt
->s
.set
);
719 free_return_stmt(stmt
->s
.ret
);
722 free_free_stmt(stmt
->s
.fre
);
728 typedef struct impl_stmts
{
747 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
748 list
= realloc(list
, count
* sizeof(list
));
749 ((void **)list
)[count
-1] = stmt
;
753 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
754 switch (stmt
->type
) {
756 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
759 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
762 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
765 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
772 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
773 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
774 return add_impl_stmt(stmts
, stmt
);
777 static inline void free_impl_stmts(impl_stmts
*stmts
) {
780 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
781 free_let_stmt(stmts
->let
.list
[i
]);
783 free(stmts
->let
.list
);
784 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
785 free_return_stmt(stmts
->ret
.list
[i
]);
787 free(stmts
->ret
.list
);
788 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
789 free_set_stmt(stmts
->set
.list
[i
]);
791 free(stmts
->set
.list
);
792 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
793 free_free_stmt(stmts
->fre
.list
[i
]);
795 free(stmts
->fre
.list
);
799 typedef struct impl
{
805 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
806 impl
*i
= calloc(1, sizeof(*i
));
812 static inline void free_impl(impl
*impl
) {
813 free_impl_func(impl
->func
);
814 free_impl_stmts(impl
->stmts
);
818 typedef struct impls
{
823 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
825 impls
= calloc(1, sizeof(*impls
));
827 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
828 impls
->list
[impls
->count
-1] = impl
;
832 static void free_impls(impls
*impls
) {
835 for (i
= 0; i
< impls
->count
; ++i
) {
836 free_impl(impls
->list
[i
]);
842 typedef struct const_type
{
847 static inline const_type
*init_const_type(token_t type
, const char *name
) {
848 const_type
*ct
= calloc(1, sizeof(*ct
));
850 ct
->name
= strdup(name
);
854 static inline void free_const_type(const_type
*type
) {
859 typedef struct constant
{
865 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
866 constant
*c
= calloc(1, sizeof(*c
));
868 c
->name
= strdup(name
);
873 static inline void free_constant(constant
*constant
) {
874 free_const_type(constant
->type
);
875 free(constant
->name
);
876 free_impl_def_val(constant
->val
);
880 typedef struct constants
{
885 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
887 constants
= calloc(1, sizeof(*constants
));
889 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
890 constants
->list
[constants
->count
-1] = constant
;
894 static inline void free_constants(constants
*c
) {
897 for (i
= 0; i
< c
->count
; ++i
) {
898 free_constant(c
->list
[i
]);
905 #define PSI_WARNING 32
906 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
908 typedef struct decl_file
{
913 static inline void free_decl_file(decl_file
*file
) {
920 memset(file
, 0, sizeof(*file
));
923 typedef struct decl_libs
{
928 static inline void free_decl_libs(decl_libs
*libs
) {
931 for (i
= 0; i
< libs
->count
; ++i
) {
933 dlclose(libs
->dl
[i
]);
938 memset(libs
, 0, sizeof(*libs
));
941 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
942 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
943 libs
->dl
[libs
->count
-1] = dlopened
;
946 #define PSI_DATA(D) ((PSI_Data *) (D))
947 #define PSI_DATA_MEMBERS \
949 decl_typedefs *defs; \
950 decl_structs *structs; \
958 typedef struct PSI_Data
{
962 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
964 dest
= malloc(sizeof(*dest
));
966 memcpy(dest
, src
, sizeof(*dest
));
967 memset(src
, 0, sizeof(*src
));
971 static inline void PSI_DataDtor(PSI_Data
*data
) {
973 free_constants(data
->consts
);
976 free_decl_typedefs(data
->defs
);
979 free_decl_structs(data
->structs
);
982 free_decls(data
->decls
);
985 free_impls(data
->impls
);
987 free_decl_file(&data
->psi
.file
);
990 typedef struct PSI_Parser
{
998 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1001 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1005 if (P
->cur
<= P
->tok
) {
1009 token_len
= P
->cur
- P
->tok
;
1011 T
= calloc(1, sizeof(*T
) + token_len
);
1014 T
->size
= token_len
;
1015 T
->text
[token_len
] = 0;
1016 memcpy(T
->text
, P
->tok
, token_len
);
1021 #define PSI_PARSER_DEBUG 0x1
1023 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1024 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1025 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1026 token_t
PSI_ParserScan(PSI_Parser
*P
);
1027 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
1028 void PSI_ParserDtor(PSI_Parser
*P
);
1029 void PSI_ParserFree(PSI_Parser
**P
);