1321f6cc8b4292948f2a793cba4f8184b9be33ea
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
, 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(char *name
, decl_type
*type
) {
60 decl_typedef
*t
= malloc(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(char *name
, unsigned pl
, unsigned as
) {
104 decl_var
*v
= malloc(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_arg
{
119 struct let_stmt
*let
;
122 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
123 decl_arg
*arg
= malloc(sizeof(*arg
));
130 static inline void free_decl_arg(decl_arg
*arg
) {
131 free_decl_type(arg
->type
);
132 free_decl_var(arg
->var
);
136 typedef struct decl_vars
{
141 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
142 decl_vars
*vars
= malloc(sizeof(*vars
));
144 vars
->vars
= malloc(sizeof(*vars
->vars
));
149 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
150 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
151 vars
->vars
[vars
->count
-1] = var
;
155 static inline void free_decl_vars(decl_vars
*vars
) {
158 for (i
= 0; i
< vars
->count
; ++i
) {
159 free_decl_var(vars
->vars
[i
]);
165 typedef struct decl_args
{
170 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
171 decl_args
*args
= malloc(sizeof(*args
));
173 args
->args
= malloc(sizeof(*args
->args
));
178 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
179 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
180 args
->args
[args
->count
-1] = arg
;
184 static inline void free_decl_args(decl_args
*args
) {
187 for (i
= 0; i
< args
->count
; ++i
) {
188 free_decl_arg(args
->args
[i
]);
194 typedef struct decl_abi
{
198 static inline decl_abi
*init_decl_abi(char *convention
) {
199 decl_abi
*abi
= malloc(sizeof(*abi
));
200 abi
->convention
= strdup(convention
);
204 static inline void free_decl_abi(decl_abi
*abi
) {
205 free(abi
->convention
);
209 typedef struct decl
{
216 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
217 decl
*d
= malloc(sizeof(*d
));
224 static inline void free_decl(decl
*d
) {
225 free_decl_abi(d
->abi
);
226 free_decl_arg(d
->func
);
228 free_decl_args(d
->args
);
233 typedef struct decls
{
238 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
240 decls
= calloc(1, sizeof(*decls
));
242 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
243 decls
->list
[decls
->count
-1] = decl
;
247 static inline void free_decls(decls
*decls
) {
250 for (i
= 0; i
< decls
->count
; ++i
) {
251 free_decl(decls
->list
[i
]);
257 typedef struct decl_struct_layout
{
260 } decl_struct_layout
;
262 typedef struct decl_struct
{
265 decl_struct_layout
*layout
;
268 static inline decl_struct
*init_decl_struct(char *name
, decl_args
*args
) {
269 decl_struct
*s
= calloc(1, sizeof(*s
));
270 s
->name
= strdup(name
);
275 static inline void free_decl_struct(decl_struct
*s
) {
277 free_decl_args(s
->args
);
286 static inline size_t decl_struct_size(decl_struct
*s
) {
287 size_t c
= s
->args
->count
- 1;
288 decl_type
*type
= real_decl_type(s
->args
->args
[c
]->type
);
289 return s
->layout
[c
].pos
+ psi_t_alignment(type
->type
);
292 typedef struct decl_structs
{
297 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
299 ss
= calloc(1, sizeof(*ss
));
301 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
302 ss
->list
[ss
->count
-1] = s
;
306 static inline void free_decl_structs(decl_structs
*ss
) {
309 for (i
= 0; i
< ss
->count
; ++i
) {
310 free_decl_struct(ss
->list
[i
]);
316 typedef union impl_val
{
328 static inline impl_val
*deref_impl_val(unsigned level
, impl_val
*ret_val
, decl_arg
*darg
) {
331 for (i
= level
; i
< darg
->var
->pointer_level
&& ret_val
->ptr
; ++i
) {
332 ret_val
= *(void **)ret_val
;
337 typedef struct impl_type
{
342 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
343 impl_type
*t
= malloc(sizeof(*t
));
346 t
->name
= (char *) strdup((const char *) name
);
350 static inline void free_impl_type(impl_type
*type
) {
355 typedef struct impl_var
{
357 unsigned reference
:1;
360 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
361 impl_var
*var
= malloc(sizeof(*var
));
362 var
->name
= (char *) strdup((const char *) name
);
363 var
->reference
= is_reference
;
367 static inline void free_impl_var(impl_var
*var
) {
372 typedef struct impl_def_val
{
377 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
378 impl_def_val
*def
= malloc(sizeof(*def
));
380 def
->text
= strdup(T
->text
);
384 static inline void free_impl_def_val(impl_def_val
*def
) {
389 typedef struct impl_arg
{
397 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
398 impl_arg
*arg
= malloc(sizeof(*arg
));
405 static inline void free_impl_arg(impl_arg
*arg
) {
406 free_impl_type(arg
->type
);
407 free_impl_var(arg
->var
);
409 free_impl_def_val(arg
->def
);
414 typedef struct impl_args
{
419 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
420 impl_args
*args
= malloc(sizeof(*args
));
421 args
->args
= malloc(sizeof(*args
->args
));
432 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
433 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
434 args
->args
[args
->count
-1] = arg
;
438 static inline void free_impl_args(impl_args
*args
) {
441 for (i
= 0; i
< args
->count
; ++i
) {
442 free_impl_arg(args
->args
[i
]);
448 typedef struct impl_func
{
451 impl_type
*return_type
;
452 unsigned return_reference
:1;
455 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
456 impl_func
*func
= malloc(sizeof(*func
));
457 func
->name
= strdup(name
);
458 func
->args
= args
? args
: init_impl_args(NULL
);
459 func
->return_type
= type
;
460 func
->return_reference
= ret_reference
;
464 static inline void free_impl_func(impl_func
*f
) {
465 free_impl_type(f
->return_type
);
466 free_impl_args(f
->args
);
471 typedef struct let_func
{
477 static inline let_func
*init_let_func(token_t type
, char *name
, size_t size
) {
478 let_func
*func
= malloc(sizeof(*func
));
480 func
->name
= strdup(name
);
485 static inline void free_let_func(let_func
*func
) {
490 typedef struct let_value
{
493 unsigned is_reference
:1;
496 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
497 let_value
*val
= malloc(sizeof(*val
));
498 val
->is_reference
= is_reference
;
504 static inline void free_let_value(let_value
*val
) {
506 free_let_func(val
->func
);
509 free_impl_var(val
->var
);
514 typedef struct let_stmt
{
523 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
524 let_stmt
*let
= calloc(1, sizeof(*let
));
530 static inline void free_let_stmt(let_stmt
*stmt
) {
531 free_decl_var(stmt
->var
);
533 free_let_value(stmt
->val
);
538 typedef struct set_func
{
543 static inline set_func
*init_set_func(token_t type
, char *name
) {
544 set_func
*func
= malloc(sizeof(*func
));
546 func
->name
= (char *) strdup((const char *) name
);
550 static inline void free_set_func(set_func
*func
) {
555 typedef struct set_value
{
560 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
561 set_value
*val
= malloc(sizeof(*val
));
567 static inline void free_set_value(set_value
*val
) {
568 free_set_func(val
->func
);
569 free_decl_vars(val
->vars
);
573 typedef struct set_stmt
{
579 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
580 set_stmt
*set
= malloc(sizeof(*set
));
586 static inline void free_set_stmt(set_stmt
*set
) {
587 free_impl_var(set
->var
);
588 free_set_value(set
->val
);
592 typedef struct return_stmt
{
597 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
598 return_stmt
*ret
= malloc(sizeof(*ret
));
604 static inline void free_return_stmt(return_stmt
*ret
) {
605 free_set_func(ret
->func
);
606 free_decl_var(ret
->decl
);
610 typedef struct free_stmt
{
614 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
615 free_stmt
*free_
= malloc(sizeof(*free_
));
620 static inline void free_free_stmt(free_stmt
*free_
) {
621 free_decl_vars(free_
->vars
);
625 typedef struct impl_stmt
{
636 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
637 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
643 static inline void free_impl_stmt(impl_stmt
*stmt
) {
644 switch (stmt
->type
) {
646 free_let_stmt(stmt
->s
.let
);
649 free_set_stmt(stmt
->s
.set
);
652 free_return_stmt(stmt
->s
.ret
);
655 free_free_stmt(stmt
->s
.fre
);
661 typedef struct impl_stmts
{
680 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
681 list
= realloc(list
, count
* sizeof(list
));
682 ((void **)list
)[count
-1] = stmt
;
686 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
687 switch (stmt
->type
) {
689 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
692 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
695 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
698 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
705 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
706 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
707 return add_impl_stmt(stmts
, stmt
);
710 static inline void free_impl_stmts(impl_stmts
*stmts
) {
713 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
714 free_let_stmt(stmts
->let
.list
[i
]);
716 free(stmts
->let
.list
);
717 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
718 free_return_stmt(stmts
->ret
.list
[i
]);
720 free(stmts
->ret
.list
);
721 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
722 free_set_stmt(stmts
->set
.list
[i
]);
724 free(stmts
->set
.list
);
725 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
726 free_free_stmt(stmts
->fre
.list
[i
]);
728 free(stmts
->fre
.list
);
732 typedef struct impl
{
738 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
739 impl
*i
= malloc(sizeof(*i
));
745 static inline void free_impl(impl
*impl
) {
746 free_impl_func(impl
->func
);
747 free_impl_stmts(impl
->stmts
);
751 typedef struct impls
{
756 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
758 impls
= calloc(1, sizeof(*impls
));
760 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
761 impls
->list
[impls
->count
-1] = impl
;
765 static void free_impls(impls
*impls
) {
768 for (i
= 0; i
< impls
->count
; ++i
) {
769 free_impl(impls
->list
[i
]);
775 typedef struct const_type
{
780 static inline const_type
*init_const_type(token_t type
, const char *name
) {
781 const_type
*ct
= malloc(sizeof(*ct
));
783 ct
->name
= strdup(name
);
787 static inline void free_const_type(const_type
*type
) {
792 typedef struct constant
{
798 static inline constant
*init_constant(const_type
*type
, char *name
, impl_def_val
*val
) {
799 constant
*c
= malloc(sizeof(*c
));
801 c
->name
= strdup(name
);
806 static inline void free_constant(constant
*constant
) {
807 free_const_type(constant
->type
);
808 free(constant
->name
);
809 free_impl_def_val(constant
->val
);
813 typedef struct constants
{
818 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
820 constants
= calloc(1, sizeof(*constants
));
822 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
823 constants
->list
[constants
->count
-1] = constant
;
827 static inline void free_constants(constants
*c
) {
830 for (i
= 0; i
< c
->count
; ++i
) {
831 free_constant(c
->list
[i
]);
838 #define PSI_WARNING 32
839 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
841 #define PSI_DATA_MEMBERS \
843 decl_typedefs *defs; \
844 decl_structs *structs; \
850 typedef struct PSI_Data
{
854 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
855 memcpy(dest
, src
, sizeof(*dest
));
856 memset(src
, 0, sizeof(*src
));
859 static inline void PSI_DataDtor(PSI_Data
*data
) {
861 free_constants(data
->consts
);
864 free_decl_typedefs(data
->defs
);
867 free_decl_structs(data
->structs
);
870 free_decls(data
->decls
);
873 free_impls(data
->impls
);
883 typedef struct PSI_Parser
{
891 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
894 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
898 if (P
->cur
<= P
->tok
) {
902 token_len
= P
->cur
- P
->tok
;
904 T
= malloc(sizeof(*T
) + token_len
);
908 T
->text
[token_len
] = 0;
909 memcpy(T
->text
, P
->tok
, token_len
);
914 #define PSI_PARSER_DEBUG 0x1
916 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
917 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
918 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
919 token_t
PSI_ParserScan(PSI_Parser
*P
);
920 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
921 void PSI_ParserDtor(PSI_Parser
*P
);
922 void PSI_ParserFree(PSI_Parser
**P
);