9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
17 typedef struct PSI_Token
{
24 typedef struct decl_type
{
27 struct decl_type
*real
;
28 struct decl_struct
*strct
;
31 static inline decl_type
*init_decl_type(token_t type
, char *name
) {
32 decl_type
*t
= malloc(sizeof(*t
));
34 t
->name
= strdup(name
);
39 static inline decl_type
*real_decl_type(decl_type
*type
) {
46 static inline void free_decl_type(decl_type
*type
) {
51 typedef struct decl_typedef
{
56 static inline decl_typedef
*init_decl_typedef(char *name
, decl_type
*type
) {
57 decl_typedef
*t
= malloc(sizeof(*t
));
58 t
->alias
= strdup(name
);
63 static inline void free_decl_typedef(decl_typedef
*t
) {
65 free_decl_type(t
->type
);
69 typedef struct decl_typedefs
{
74 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
76 defs
= calloc(1, sizeof(*defs
));
78 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
79 defs
->list
[defs
->count
-1] = def
;
83 static void free_decl_typedefs(decl_typedefs
*defs
) {
86 for (i
= 0; i
< defs
->count
; ++i
) {
87 free_decl_typedef(defs
->list
[i
]);
93 typedef struct decl_var
{
95 unsigned pointer_level
;
100 static inline decl_var
*init_decl_var(char *name
, unsigned pl
, unsigned as
) {
101 decl_var
*v
= malloc(sizeof(*v
));
102 v
->name
= (char *) strdup((const char *) name
);
103 v
->pointer_level
= pl
;
108 static inline void free_decl_var(decl_var
*var
) {
113 typedef struct decl_arg
{
116 struct let_stmt
*let
;
119 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
120 decl_arg
*arg
= malloc(sizeof(*arg
));
127 static inline void free_decl_arg(decl_arg
*arg
) {
128 free_decl_type(arg
->type
);
129 free_decl_var(arg
->var
);
133 typedef struct decl_vars
{
138 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
139 decl_vars
*vars
= malloc(sizeof(*vars
));
141 vars
->vars
= malloc(sizeof(*vars
->vars
));
146 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
147 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
148 vars
->vars
[vars
->count
-1] = var
;
152 static inline void free_decl_vars(decl_vars
*vars
) {
155 for (i
= 0; i
< vars
->count
; ++i
) {
156 free_decl_var(vars
->vars
[i
]);
162 typedef struct decl_args
{
167 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
168 decl_args
*args
= malloc(sizeof(*args
));
170 args
->args
= malloc(sizeof(*args
->args
));
175 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
176 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
177 args
->args
[args
->count
-1] = arg
;
181 static inline void free_decl_args(decl_args
*args
) {
184 for (i
= 0; i
< args
->count
; ++i
) {
185 free_decl_arg(args
->args
[i
]);
191 typedef struct decl_abi
{
195 static inline decl_abi
*init_decl_abi(char *convention
) {
196 decl_abi
*abi
= malloc(sizeof(*abi
));
197 abi
->convention
= strdup(convention
);
201 static inline void free_decl_abi(decl_abi
*abi
) {
202 free(abi
->convention
);
206 typedef struct decl
{
213 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
214 decl
*d
= malloc(sizeof(*d
));
221 static inline void free_decl(decl
*d
) {
222 free_decl_abi(d
->abi
);
223 free_decl_arg(d
->func
);
225 free_decl_args(d
->args
);
230 typedef struct decls
{
235 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
237 decls
= calloc(1, sizeof(*decls
));
239 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
240 decls
->list
[decls
->count
-1] = decl
;
244 static inline void free_decls(decls
*decls
) {
247 for (i
= 0; i
< decls
->count
; ++i
) {
248 free_decl(decls
->list
[i
]);
254 typedef struct decl_struct_layout
{
257 } decl_struct_layout
;
259 typedef struct decl_struct
{
262 decl_struct_layout
*layout
;
265 static inline decl_struct
*init_decl_struct(char *name
, decl_args
*args
) {
266 decl_struct
*s
= calloc(1, sizeof(*s
));
267 s
->name
= strdup(name
);
272 static inline void free_decl_struct(decl_struct
*s
) {
274 free_decl_args(s
->args
);
280 typedef struct decl_structs
{
285 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
287 ss
= calloc(1, sizeof(*ss
));
289 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
290 ss
->list
[ss
->count
-1] = s
;
294 static inline void free_decl_structs(decl_structs
*ss
) {
297 for (i
= 0; i
< ss
->count
; ++i
) {
298 free_decl_struct(ss
->list
[i
]);
304 typedef union impl_val
{
315 static inline impl_val
*deref_impl_val(unsigned level
, impl_val
*ret_val
, decl_arg
*darg
) {
318 for (i
= level
; i
< darg
->var
->pointer_level
&& ret_val
->ptr
; ++i
) {
319 ret_val
= *(void **)ret_val
;
324 typedef struct impl_type
{
329 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
330 impl_type
*t
= malloc(sizeof(*t
));
333 t
->name
= (char *) strdup((const char *) name
);
337 static inline void free_impl_type(impl_type
*type
) {
342 typedef struct impl_var
{
344 unsigned reference
:1;
347 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
348 impl_var
*var
= malloc(sizeof(*var
));
349 var
->name
= (char *) strdup((const char *) name
);
350 var
->reference
= is_reference
;
354 static inline void free_impl_var(impl_var
*var
) {
359 typedef struct impl_def_val
{
364 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
365 impl_def_val
*def
= malloc(sizeof(*def
));
367 def
->text
= strdup(T
->text
);
371 static inline void free_impl_def_val(impl_def_val
*def
) {
376 typedef struct impl_arg
{
384 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
385 impl_arg
*arg
= malloc(sizeof(*arg
));
392 static inline void free_impl_arg(impl_arg
*arg
) {
393 free_impl_type(arg
->type
);
394 free_impl_var(arg
->var
);
396 free_impl_def_val(arg
->def
);
401 typedef struct impl_args
{
406 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
407 impl_args
*args
= malloc(sizeof(*args
));
408 args
->args
= malloc(sizeof(*args
->args
));
419 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
420 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
421 args
->args
[args
->count
-1] = arg
;
425 static inline void free_impl_args(impl_args
*args
) {
428 for (i
= 0; i
< args
->count
; ++i
) {
429 free_impl_arg(args
->args
[i
]);
435 typedef struct impl_func
{
438 impl_type
*return_type
;
439 unsigned return_reference
:1;
442 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
443 impl_func
*func
= malloc(sizeof(*func
));
444 func
->name
= strdup(name
);
445 func
->args
= args
? args
: init_impl_args(NULL
);
446 func
->return_type
= type
;
447 func
->return_reference
= ret_reference
;
451 static inline void free_impl_func(impl_func
*f
) {
452 free_impl_type(f
->return_type
);
453 free_impl_args(f
->args
);
458 typedef struct let_func
{
464 static inline let_func
*init_let_func(token_t type
, char *name
, size_t size
) {
465 let_func
*func
= malloc(sizeof(*func
));
467 func
->name
= strdup(name
);
472 static inline void free_let_func(let_func
*func
) {
477 typedef struct let_value
{
480 unsigned is_reference
:1;
483 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
484 let_value
*val
= malloc(sizeof(*val
));
485 val
->is_reference
= is_reference
;
491 static inline void free_let_value(let_value
*val
) {
493 free_let_func(val
->func
);
496 free_impl_var(val
->var
);
501 typedef struct let_stmt
{
510 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
511 let_stmt
*let
= calloc(1, sizeof(*let
));
517 static inline void free_let_stmt(let_stmt
*stmt
) {
518 free_decl_var(stmt
->var
);
520 free_let_value(stmt
->val
);
525 typedef struct set_func
{
530 static inline set_func
*init_set_func(token_t type
, char *name
) {
531 set_func
*func
= malloc(sizeof(*func
));
533 func
->name
= (char *) strdup((const char *) name
);
537 static inline void free_set_func(set_func
*func
) {
542 typedef struct set_value
{
547 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
548 set_value
*val
= malloc(sizeof(*val
));
554 static inline void free_set_value(set_value
*val
) {
555 free_set_func(val
->func
);
556 free_decl_vars(val
->vars
);
560 typedef struct set_stmt
{
566 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
567 set_stmt
*set
= malloc(sizeof(*set
));
573 static inline void free_set_stmt(set_stmt
*set
) {
574 free_impl_var(set
->var
);
575 free_set_value(set
->val
);
579 typedef struct return_stmt
{
584 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
585 return_stmt
*ret
= malloc(sizeof(*ret
));
591 static inline void free_return_stmt(return_stmt
*ret
) {
592 free_set_func(ret
->func
);
593 free_decl_var(ret
->decl
);
597 typedef struct free_stmt
{
601 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
602 free_stmt
*free_
= malloc(sizeof(*free_
));
607 static inline void free_free_stmt(free_stmt
*free_
) {
608 free_decl_vars(free_
->vars
);
612 typedef struct impl_stmt
{
623 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
624 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
630 static inline void free_impl_stmt(impl_stmt
*stmt
) {
631 switch (stmt
->type
) {
633 free_let_stmt(stmt
->s
.let
);
636 free_set_stmt(stmt
->s
.set
);
639 free_return_stmt(stmt
->s
.ret
);
642 free_free_stmt(stmt
->s
.fre
);
648 typedef struct impl_stmts
{
667 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
668 list
= realloc(list
, count
* sizeof(list
));
669 ((void **)list
)[count
-1] = stmt
;
673 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
674 switch (stmt
->type
) {
676 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
679 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
682 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
685 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
692 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
693 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
694 return add_impl_stmt(stmts
, stmt
);
697 static inline void free_impl_stmts(impl_stmts
*stmts
) {
700 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
701 free_let_stmt(stmts
->let
.list
[i
]);
703 free(stmts
->let
.list
);
704 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
705 free_return_stmt(stmts
->ret
.list
[i
]);
707 free(stmts
->ret
.list
);
708 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
709 free_set_stmt(stmts
->set
.list
[i
]);
711 free(stmts
->set
.list
);
712 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
713 free_free_stmt(stmts
->fre
.list
[i
]);
715 free(stmts
->fre
.list
);
719 typedef struct impl
{
725 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
726 impl
*i
= malloc(sizeof(*i
));
732 static inline void free_impl(impl
*impl
) {
733 free_impl_func(impl
->func
);
734 free_impl_stmts(impl
->stmts
);
738 typedef struct impls
{
743 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
745 impls
= calloc(1, sizeof(*impls
));
747 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
748 impls
->list
[impls
->count
-1] = impl
;
752 static void free_impls(impls
*impls
) {
755 for (i
= 0; i
< impls
->count
; ++i
) {
756 free_impl(impls
->list
[i
]);
762 typedef struct const_type
{
767 static inline const_type
*init_const_type(token_t type
, const char *name
) {
768 const_type
*ct
= malloc(sizeof(*ct
));
770 ct
->name
= strdup(name
);
774 static inline void free_const_type(const_type
*type
) {
779 typedef struct constant
{
785 static inline constant
*init_constant(const_type
*type
, char *name
, impl_def_val
*val
) {
786 constant
*c
= malloc(sizeof(*c
));
788 c
->name
= strdup(name
);
793 static inline void free_constant(constant
*constant
) {
794 free_const_type(constant
->type
);
795 free(constant
->name
);
796 free_impl_def_val(constant
->val
);
800 typedef struct constants
{
805 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
807 constants
= calloc(1, sizeof(*constants
));
809 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
810 constants
->list
[constants
->count
-1] = constant
;
814 static inline void free_constants(constants
*c
) {
817 for (i
= 0; i
< c
->count
; ++i
) {
818 free_constant(c
->list
[i
]);
825 #define PSI_WARNING 32
826 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
828 #define PSI_DATA_MEMBERS \
830 decl_typedefs *defs; \
831 decl_structs *structs; \
837 typedef struct PSI_Data
{
841 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
842 memcpy(dest
, src
, sizeof(*dest
));
843 memset(src
, 0, sizeof(*src
));
846 static inline void PSI_DataDtor(PSI_Data
*data
) {
848 free_constants(data
->consts
);
851 free_decl_typedefs(data
->defs
);
854 free_decl_structs(data
->structs
);
857 free_decls(data
->decls
);
860 free_impls(data
->impls
);
870 typedef struct PSI_Parser
{
878 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
881 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
885 if (P
->cur
<= P
->tok
) {
889 token_len
= P
->cur
- P
->tok
;
891 T
= malloc(sizeof(*T
) + token_len
);
895 T
->text
[token_len
] = 0;
896 memcpy(T
->text
, P
->tok
, token_len
);
901 #define PSI_PARSER_DEBUG 0x1
903 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
904 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
905 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
906 token_t
PSI_ParserScan(PSI_Parser
*P
);
907 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
908 void PSI_ParserDtor(PSI_Parser
*P
);
909 void PSI_ParserFree(PSI_Parser
**P
);