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
);
283 static inline size_t decl_struct_size(decl_struct
*s
) {
284 size_t c
= s
->args
->count
- 1;
285 decl_type
*type
= real_decl_type(s
->args
->args
[c
]->type
);
286 return s
->layout
[c
].pos
+ psi_t_alignment(type
->type
);
289 typedef struct decl_structs
{
294 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
296 ss
= calloc(1, sizeof(*ss
));
298 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
299 ss
->list
[ss
->count
-1] = s
;
303 static inline void free_decl_structs(decl_structs
*ss
) {
306 for (i
= 0; i
< ss
->count
; ++i
) {
307 free_decl_struct(ss
->list
[i
]);
313 typedef union impl_val
{
325 static inline impl_val
*deref_impl_val(unsigned level
, impl_val
*ret_val
, decl_arg
*darg
) {
328 for (i
= level
; i
< darg
->var
->pointer_level
&& ret_val
->ptr
; ++i
) {
329 ret_val
= *(void **)ret_val
;
334 typedef struct impl_type
{
339 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
340 impl_type
*t
= malloc(sizeof(*t
));
343 t
->name
= (char *) strdup((const char *) name
);
347 static inline void free_impl_type(impl_type
*type
) {
352 typedef struct impl_var
{
354 unsigned reference
:1;
357 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
358 impl_var
*var
= malloc(sizeof(*var
));
359 var
->name
= (char *) strdup((const char *) name
);
360 var
->reference
= is_reference
;
364 static inline void free_impl_var(impl_var
*var
) {
369 typedef struct impl_def_val
{
374 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
375 impl_def_val
*def
= malloc(sizeof(*def
));
377 def
->text
= strdup(T
->text
);
381 static inline void free_impl_def_val(impl_def_val
*def
) {
386 typedef struct impl_arg
{
394 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
395 impl_arg
*arg
= malloc(sizeof(*arg
));
402 static inline void free_impl_arg(impl_arg
*arg
) {
403 free_impl_type(arg
->type
);
404 free_impl_var(arg
->var
);
406 free_impl_def_val(arg
->def
);
411 typedef struct impl_args
{
416 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
417 impl_args
*args
= malloc(sizeof(*args
));
418 args
->args
= malloc(sizeof(*args
->args
));
429 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
430 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
431 args
->args
[args
->count
-1] = arg
;
435 static inline void free_impl_args(impl_args
*args
) {
438 for (i
= 0; i
< args
->count
; ++i
) {
439 free_impl_arg(args
->args
[i
]);
445 typedef struct impl_func
{
448 impl_type
*return_type
;
449 unsigned return_reference
:1;
452 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
453 impl_func
*func
= malloc(sizeof(*func
));
454 func
->name
= strdup(name
);
455 func
->args
= args
? args
: init_impl_args(NULL
);
456 func
->return_type
= type
;
457 func
->return_reference
= ret_reference
;
461 static inline void free_impl_func(impl_func
*f
) {
462 free_impl_type(f
->return_type
);
463 free_impl_args(f
->args
);
468 typedef struct let_func
{
474 static inline let_func
*init_let_func(token_t type
, char *name
, size_t size
) {
475 let_func
*func
= malloc(sizeof(*func
));
477 func
->name
= strdup(name
);
482 static inline void free_let_func(let_func
*func
) {
487 typedef struct let_value
{
490 unsigned is_reference
:1;
493 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
494 let_value
*val
= malloc(sizeof(*val
));
495 val
->is_reference
= is_reference
;
501 static inline void free_let_value(let_value
*val
) {
503 free_let_func(val
->func
);
506 free_impl_var(val
->var
);
511 typedef struct let_stmt
{
520 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
521 let_stmt
*let
= calloc(1, sizeof(*let
));
527 static inline void free_let_stmt(let_stmt
*stmt
) {
528 free_decl_var(stmt
->var
);
530 free_let_value(stmt
->val
);
535 typedef struct set_func
{
540 static inline set_func
*init_set_func(token_t type
, char *name
) {
541 set_func
*func
= malloc(sizeof(*func
));
543 func
->name
= (char *) strdup((const char *) name
);
547 static inline void free_set_func(set_func
*func
) {
552 typedef struct set_value
{
557 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
558 set_value
*val
= malloc(sizeof(*val
));
564 static inline void free_set_value(set_value
*val
) {
565 free_set_func(val
->func
);
566 free_decl_vars(val
->vars
);
570 typedef struct set_stmt
{
576 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
577 set_stmt
*set
= malloc(sizeof(*set
));
583 static inline void free_set_stmt(set_stmt
*set
) {
584 free_impl_var(set
->var
);
585 free_set_value(set
->val
);
589 typedef struct return_stmt
{
594 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
595 return_stmt
*ret
= malloc(sizeof(*ret
));
601 static inline void free_return_stmt(return_stmt
*ret
) {
602 free_set_func(ret
->func
);
603 free_decl_var(ret
->decl
);
607 typedef struct free_stmt
{
611 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
612 free_stmt
*free_
= malloc(sizeof(*free_
));
617 static inline void free_free_stmt(free_stmt
*free_
) {
618 free_decl_vars(free_
->vars
);
622 typedef struct impl_stmt
{
633 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
634 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
640 static inline void free_impl_stmt(impl_stmt
*stmt
) {
641 switch (stmt
->type
) {
643 free_let_stmt(stmt
->s
.let
);
646 free_set_stmt(stmt
->s
.set
);
649 free_return_stmt(stmt
->s
.ret
);
652 free_free_stmt(stmt
->s
.fre
);
658 typedef struct impl_stmts
{
677 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
678 list
= realloc(list
, count
* sizeof(list
));
679 ((void **)list
)[count
-1] = stmt
;
683 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
684 switch (stmt
->type
) {
686 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
689 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
692 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
695 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
702 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
703 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
704 return add_impl_stmt(stmts
, stmt
);
707 static inline void free_impl_stmts(impl_stmts
*stmts
) {
710 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
711 free_let_stmt(stmts
->let
.list
[i
]);
713 free(stmts
->let
.list
);
714 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
715 free_return_stmt(stmts
->ret
.list
[i
]);
717 free(stmts
->ret
.list
);
718 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
719 free_set_stmt(stmts
->set
.list
[i
]);
721 free(stmts
->set
.list
);
722 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
723 free_free_stmt(stmts
->fre
.list
[i
]);
725 free(stmts
->fre
.list
);
729 typedef struct impl
{
735 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
736 impl
*i
= malloc(sizeof(*i
));
742 static inline void free_impl(impl
*impl
) {
743 free_impl_func(impl
->func
);
744 free_impl_stmts(impl
->stmts
);
748 typedef struct impls
{
753 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
755 impls
= calloc(1, sizeof(*impls
));
757 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
758 impls
->list
[impls
->count
-1] = impl
;
762 static void free_impls(impls
*impls
) {
765 for (i
= 0; i
< impls
->count
; ++i
) {
766 free_impl(impls
->list
[i
]);
772 typedef struct const_type
{
777 static inline const_type
*init_const_type(token_t type
, const char *name
) {
778 const_type
*ct
= malloc(sizeof(*ct
));
780 ct
->name
= strdup(name
);
784 static inline void free_const_type(const_type
*type
) {
789 typedef struct constant
{
795 static inline constant
*init_constant(const_type
*type
, char *name
, impl_def_val
*val
) {
796 constant
*c
= malloc(sizeof(*c
));
798 c
->name
= strdup(name
);
803 static inline void free_constant(constant
*constant
) {
804 free_const_type(constant
->type
);
805 free(constant
->name
);
806 free_impl_def_val(constant
->val
);
810 typedef struct constants
{
815 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
817 constants
= calloc(1, sizeof(*constants
));
819 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
820 constants
->list
[constants
->count
-1] = constant
;
824 static inline void free_constants(constants
*c
) {
827 for (i
= 0; i
< c
->count
; ++i
) {
828 free_constant(c
->list
[i
]);
835 #define PSI_WARNING 32
836 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
838 #define PSI_DATA_MEMBERS \
840 decl_typedefs *defs; \
841 decl_structs *structs; \
847 typedef struct PSI_Data
{
851 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
852 memcpy(dest
, src
, sizeof(*dest
));
853 memset(src
, 0, sizeof(*src
));
856 static inline void PSI_DataDtor(PSI_Data
*data
) {
858 free_constants(data
->consts
);
861 free_decl_typedefs(data
->defs
);
864 free_decl_structs(data
->structs
);
867 free_decls(data
->decls
);
870 free_impls(data
->impls
);
880 typedef struct PSI_Parser
{
888 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
891 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
895 if (P
->cur
<= P
->tok
) {
899 token_len
= P
->cur
- P
->tok
;
901 T
= malloc(sizeof(*T
) + token_len
);
905 T
->text
[token_len
] = 0;
906 memcpy(T
->text
, P
->tok
, token_len
);
911 #define PSI_PARSER_DEBUG 0x1
913 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
914 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
915 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
916 token_t
PSI_ParserScan(PSI_Parser
*P
);
917 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
918 void PSI_ParserDtor(PSI_Parser
*P
);
919 void PSI_ParserFree(PSI_Parser
**P
);