8df41eafe50cf95aceba8e8fd9e32a938c809d1f
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
);
224 free_decl_args(d
->args
);
228 typedef struct decls
{
233 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
235 decls
= calloc(1, sizeof(*decls
));
237 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
238 decls
->list
[decls
->count
-1] = decl
;
242 static inline void free_decls(decls
*decls
) {
245 for (i
= 0; i
< decls
->count
; ++i
) {
246 free_decl(decls
->list
[i
]);
252 typedef struct decl_struct_layout
{
255 } decl_struct_layout
;
257 typedef struct decl_struct
{
260 decl_struct_layout
*layout
;
263 static inline decl_struct
*init_decl_struct(char *name
, decl_args
*args
) {
264 decl_struct
*s
= calloc(1, sizeof(*s
));
265 s
->name
= strdup(name
);
270 static inline void free_decl_struct(decl_struct
*s
) {
271 free_decl_args(s
->args
);
276 typedef struct decl_structs
{
281 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
283 ss
= calloc(1, sizeof(*ss
));
285 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
286 ss
->list
[ss
->count
-1] = s
;
290 static inline void free_decl_structs(decl_structs
*ss
) {
293 for (i
= 0; i
< ss
->count
; ++i
) {
294 free_decl_struct(ss
->list
[i
]);
300 typedef union impl_val
{
311 static inline impl_val
*deref_impl_val(unsigned level
, impl_val
*ret_val
, decl_arg
*darg
) {
314 for (i
= level
; i
< darg
->var
->pointer_level
&& ret_val
->ptr
; ++i
) {
315 ret_val
= *(void **)ret_val
;
320 typedef struct impl_type
{
325 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
326 impl_type
*t
= malloc(sizeof(*t
));
329 t
->name
= (char *) strdup((const char *) name
);
333 static inline void free_impl_type(impl_type
*type
) {
338 typedef struct impl_var
{
340 unsigned reference
:1;
343 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
344 impl_var
*var
= malloc(sizeof(*var
));
345 var
->name
= (char *) strdup((const char *) name
);
346 var
->reference
= is_reference
;
350 static inline void free_impl_var(impl_var
*var
) {
355 typedef struct impl_def_val
{
360 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
361 impl_def_val
*def
= malloc(sizeof(*def
));
363 def
->text
= strdup(T
->text
);
367 static inline void free_impl_def_val(impl_def_val
*def
) {
372 typedef struct impl_arg
{
380 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
381 impl_arg
*arg
= malloc(sizeof(*arg
));
388 static inline void free_impl_arg(impl_arg
*arg
) {
389 free_impl_type(arg
->type
);
390 free_impl_var(arg
->var
);
392 free_impl_def_val(arg
->def
);
397 typedef struct impl_args
{
402 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
403 impl_args
*args
= malloc(sizeof(*args
));
404 args
->args
= malloc(sizeof(*args
->args
));
415 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
416 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
417 args
->args
[args
->count
-1] = arg
;
421 static inline void free_impl_args(impl_args
*args
) {
424 for (i
= 0; i
< args
->count
; ++i
) {
425 free_impl_arg(args
->args
[i
]);
431 typedef struct impl_func
{
434 impl_type
*return_type
;
435 unsigned return_reference
:1;
438 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
439 impl_func
*func
= malloc(sizeof(*func
));
440 func
->name
= strdup(name
);
441 func
->args
= args
? args
: init_impl_args(NULL
);
442 func
->return_type
= type
;
443 func
->return_reference
= ret_reference
;
447 static inline void free_impl_func(impl_func
*f
) {
448 free_impl_type(f
->return_type
);
449 free_impl_args(f
->args
);
454 typedef struct let_func
{
460 static inline let_func
*init_let_func(token_t type
, char *name
, size_t size
) {
461 let_func
*func
= malloc(sizeof(*func
));
463 func
->name
= strdup(name
);
468 static inline void free_let_func(let_func
*func
) {
473 typedef struct let_value
{
476 unsigned is_reference
:1;
479 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
480 let_value
*val
= malloc(sizeof(*val
));
481 val
->is_reference
= is_reference
;
487 static inline void free_let_value(let_value
*val
) {
489 free_let_func(val
->func
);
492 free_impl_var(val
->var
);
497 typedef struct let_stmt
{
506 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
507 let_stmt
*let
= calloc(1, sizeof(*let
));
513 static inline void free_let_stmt(let_stmt
*stmt
) {
514 free_decl_var(stmt
->var
);
516 free_let_value(stmt
->val
);
521 typedef struct set_func
{
526 static inline set_func
*init_set_func(token_t type
, char *name
) {
527 set_func
*func
= malloc(sizeof(*func
));
529 func
->name
= (char *) strdup((const char *) name
);
533 static inline void free_set_func(set_func
*func
) {
538 typedef struct set_value
{
543 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
544 set_value
*val
= malloc(sizeof(*val
));
550 static inline void free_set_value(set_value
*val
) {
551 free_set_func(val
->func
);
552 free_decl_vars(val
->vars
);
556 typedef struct set_stmt
{
562 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
563 set_stmt
*set
= malloc(sizeof(*set
));
569 static inline void free_set_stmt(set_stmt
*set
) {
570 free_impl_var(set
->var
);
571 free_set_value(set
->val
);
575 typedef struct return_stmt
{
580 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
581 return_stmt
*ret
= malloc(sizeof(*ret
));
587 static inline void free_return_stmt(return_stmt
*ret
) {
588 free_set_func(ret
->func
);
589 free_decl_var(ret
->decl
);
593 typedef struct free_stmt
{
597 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
598 free_stmt
*free_
= malloc(sizeof(*free_
));
603 static inline void free_free_stmt(free_stmt
*free_
) {
604 free_decl_vars(free_
->vars
);
608 typedef struct impl_stmt
{
619 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
620 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
626 static inline void free_impl_stmt(impl_stmt
*stmt
) {
627 switch (stmt
->type
) {
629 free_let_stmt(stmt
->s
.let
);
632 free_set_stmt(stmt
->s
.set
);
635 free_return_stmt(stmt
->s
.ret
);
638 free_free_stmt(stmt
->s
.fre
);
644 typedef struct impl_stmts
{
663 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
664 list
= realloc(list
, count
* sizeof(list
));
665 ((void **)list
)[count
-1] = stmt
;
669 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
670 switch (stmt
->type
) {
672 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
675 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
678 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
681 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
688 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
689 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
690 return add_impl_stmt(stmts
, stmt
);
693 static inline void free_impl_stmts(impl_stmts
*stmts
) {
696 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
697 free_let_stmt(stmts
->let
.list
[i
]);
699 free(stmts
->let
.list
);
700 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
701 free_return_stmt(stmts
->ret
.list
[i
]);
703 free(stmts
->ret
.list
);
704 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
705 free_set_stmt(stmts
->set
.list
[i
]);
707 free(stmts
->set
.list
);
708 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
709 free_free_stmt(stmts
->fre
.list
[i
]);
711 free(stmts
->fre
.list
);
715 typedef struct impl
{
721 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
722 impl
*i
= malloc(sizeof(*i
));
728 static inline void free_impl(impl
*impl
) {
729 free_impl_func(impl
->func
);
730 free_impl_stmts(impl
->stmts
);
734 typedef struct impls
{
739 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
741 impls
= calloc(1, sizeof(*impls
));
743 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
744 impls
->list
[impls
->count
-1] = impl
;
748 static void free_impls(impls
*impls
) {
751 for (i
= 0; i
< impls
->count
; ++i
) {
752 free_impl(impls
->list
[i
]);
758 typedef struct const_type
{
763 static inline const_type
*init_const_type(token_t type
, const char *name
) {
764 const_type
*ct
= malloc(sizeof(*ct
));
766 ct
->name
= strdup(name
);
770 static inline void free_const_type(const_type
*type
) {
775 typedef struct constant
{
781 static inline constant
*init_constant(const_type
*type
, char *name
, impl_def_val
*val
) {
782 constant
*c
= malloc(sizeof(*c
));
784 c
->name
= strdup(name
);
789 static inline void free_constant(constant
*constant
) {
790 free_const_type(constant
->type
);
791 free(constant
->name
);
792 free_impl_def_val(constant
->val
);
796 typedef struct constants
{
801 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
803 constants
= calloc(1, sizeof(*constants
));
805 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
806 constants
->list
[constants
->count
-1] = constant
;
810 static inline void free_constants(constants
*c
) {
813 for (i
= 0; i
< c
->count
; ++i
) {
814 free_constant(c
->list
[i
]);
821 #define PSI_WARNING 32
822 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
824 #define PSI_DATA_MEMBERS \
826 decl_typedefs *defs; \
827 decl_structs *structs; \
833 typedef struct PSI_Data
{
837 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
838 memcpy(dest
, src
, sizeof(*dest
));
839 memset(src
, 0, sizeof(*src
));
842 static inline void PSI_DataDtor(PSI_Data
*data
) {
844 free_constants(data
->consts
);
847 free_decl_typedefs(data
->defs
);
850 free_decl_structs(data
->structs
);
853 free_decls(data
->decls
);
856 free_impls(data
->impls
);
866 typedef struct PSI_Parser
{
874 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
877 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
881 if (P
->cur
<= P
->tok
) {
885 token_len
= P
->cur
- P
->tok
;
887 T
= malloc(sizeof(*T
) + token_len
);
891 T
->text
[token_len
] = 0;
892 memcpy(T
->text
, P
->tok
, token_len
);
897 #define PSI_PARSER_DEBUG 0x1
899 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
900 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
901 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
902 token_t
PSI_ParserScan(PSI_Parser
*P
);
903 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
904 void PSI_ParserDtor(PSI_Parser
*P
);
905 void PSI_ParserFree(PSI_Parser
**P
);