7 #include "parser_proc.h"
11 typedef struct PSI_Token
{
18 static inline PSI_Token
*PSI_TokenAlloc(PSI_Lexer
*L
, token_t t
) {
22 if (L
->cur
<= L
->tok
) {
26 token_len
= L
->cur
- L
->tok
;
28 T
= malloc(sizeof(*T
) + token_len
);
32 T
->text
[token_len
] = 0;
33 memcpy(T
->text
, L
->tok
, token_len
);
38 typedef struct decl_type
{
41 struct decl_type
*real
;
44 static inline decl_type
*init_decl_type(token_t type
, char *name
) {
45 decl_type
*t
= malloc(sizeof(*t
));
47 t
->name
= strdup(name
);
51 static inline void free_decl_type(decl_type
*type
) {
56 typedef struct decl_typedef
{
61 static inline decl_typedef
*init_decl_typedef(char *name
, decl_type
*type
) {
62 decl_typedef
*t
= malloc(sizeof(*t
));
63 t
->alias
= strdup(name
);
68 static inline void free_decl_typedef(decl_typedef
*t
) {
70 free_decl_type(t
->type
);
74 typedef struct decl_typedefs
{
79 static decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
81 defs
= calloc(1, sizeof(*defs
));
83 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
84 defs
->list
[defs
->count
-1] = def
;
88 static void free_decl_typedefs(decl_typedefs
*defs
) {
91 for (i
= 0; i
< defs
->count
; ++i
) {
92 free_decl_typedef(defs
->list
[i
]);
98 typedef struct decl_var
{
100 unsigned pointer_level
;
103 static inline decl_var
*init_decl_var(char *name
, unsigned pl
) {
104 decl_var
*v
= malloc(sizeof(*v
));
105 v
->name
= (char *) strdup((const char *) name
);
106 v
->pointer_level
= pl
;
110 static inline void free_decl_var(decl_var
*var
) {
115 typedef struct decl_arg
{
120 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
121 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 impl_type
{
257 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
258 impl_type
*t
= malloc(sizeof(*t
));
261 t
->name
= (char *) strdup((const char *) name
);
265 static inline void free_impl_type(impl_type
*type
) {
270 typedef struct impl_var
{
272 unsigned reference
:1;
275 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
276 impl_var
*var
= malloc(sizeof(*var
));
277 var
->name
= (char *) strdup((const char *) name
);
278 var
->reference
= is_reference
;
282 static inline void free_impl_var(impl_var
*var
) {
287 typedef struct impl_def_val
{
296 static inline impl_def_val
*init_impl_def_val() {
297 impl_def_val
*def
= malloc(sizeof(*def
));
303 static inline void free_impl_def_val(impl_def_val
*def
) {
307 typedef struct impl_arg
{
313 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
314 impl_arg
*arg
= malloc(sizeof(*arg
));
321 static inline void free_impl_arg(impl_arg
*arg
) {
322 free_impl_type(arg
->type
);
323 free_impl_var(arg
->var
);
325 free_impl_def_val(arg
->def
);
330 typedef struct impl_args
{
335 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
336 impl_args
*args
= malloc(sizeof(*args
));
337 args
->args
= malloc(sizeof(*args
->args
));
348 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
349 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
350 args
->args
[args
->count
-1] = arg
;
354 static inline void free_impl_args(impl_args
*args
) {
357 for (i
= 0; i
< args
->count
; ++i
) {
358 free_impl_arg(args
->args
[i
]);
364 typedef struct impl_func
{
367 impl_type
*return_type
;
370 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
) {
371 impl_func
*func
= malloc(sizeof(*func
));
372 func
->name
= strdup(name
);
373 func
->args
= args
? args
: init_impl_args(NULL
);
374 func
->return_type
= type
;
378 static inline void free_impl_func(impl_func
*f
) {
379 free_impl_type(f
->return_type
);
380 free_impl_args(f
->args
);
385 typedef struct let_func
{
390 static inline let_func
*init_let_func(token_t type
, char *name
) {
391 let_func
*func
= malloc(sizeof(*func
));
393 func
->name
= (char *) strdup((const char *) name
);
397 static inline void free_let_func(let_func
*func
) {
402 typedef struct let_value
{
405 unsigned null_pointer_ref
:1;
408 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int null_pointer_ref
) {
409 let_value
*val
= malloc(sizeof(*val
));
410 val
->null_pointer_ref
= null_pointer_ref
;
416 static inline void free_let_value(let_value
*val
) {
418 free_let_func(val
->func
);
421 free_impl_var(val
->var
);
426 typedef struct let_stmt
{
431 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
432 let_stmt
*let
= malloc(sizeof(*let
));
438 static inline void free_let_stmt(let_stmt
*stmt
) {
439 free_decl_var(stmt
->var
);
440 free_let_value(stmt
->val
);
444 typedef struct set_func
{
449 static inline set_func
*init_set_func(token_t type
, char *name
) {
450 set_func
*func
= malloc(sizeof(*func
));
452 func
->name
= (char *) strdup((const char *) name
);
456 static inline void free_set_func(set_func
*func
) {
461 typedef struct set_value
{
466 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
467 set_value
*val
= malloc(sizeof(*val
));
473 static inline void free_set_value(set_value
*val
) {
474 free_set_func(val
->func
);
475 free_decl_vars(val
->vars
);
479 typedef struct set_stmt
{
484 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
485 set_stmt
*set
= malloc(sizeof(*set
));
491 static inline void free_set_stmt(set_stmt
*set
) {
492 free_impl_var(set
->var
);
493 free_set_value(set
->val
);
497 typedef struct ret_stmt
{
502 static inline ret_stmt
*init_ret_stmt(set_func
*func
, decl_var
*decl
) {
503 ret_stmt
*ret
= malloc(sizeof(*ret
));
509 static inline void free_ret_stmt(ret_stmt
*ret
) {
510 free_set_func(ret
->func
);
511 free_decl_var(ret
->decl
);
515 typedef struct impl_stmt
{
525 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
526 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
532 static inline void free_impl_stmt(impl_stmt
*stmt
) {
533 switch (stmt
->type
) {
535 free_let_stmt(stmt
->s
.let
);
538 free_set_stmt(stmt
->s
.set
);
541 free_ret_stmt(stmt
->s
.ret
);
547 typedef struct impl_stmts
{
552 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
553 impl_stmts
*stmts
= malloc(sizeof(*stmts
));
555 stmts
->stmts
= malloc(sizeof(*stmts
->stmts
));
556 stmts
->stmts
[0] = stmt
;
560 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
561 stmts
->stmts
= realloc(stmts
->stmts
, ++stmts
->count
* sizeof(*stmts
->stmts
));
562 stmts
->stmts
[stmts
->count
-1] = stmt
;
566 static inline void free_impl_stmts(impl_stmts
*stmts
) {
569 for (i
= 0; i
< stmts
->count
; ++i
) {
570 free_impl_stmt(stmts
->stmts
[i
]);
576 typedef struct impl
{
581 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
582 impl
*i
= malloc(sizeof(*i
));
588 static inline void free_impl(impl
*impl
) {
589 free_impl_func(impl
->func
);
590 free_impl_stmts(impl
->stmts
);
594 typedef struct impls
{
599 static impls
*add_impl(impls
*impls
, impl
*impl
) {
601 impls
= calloc(1, sizeof(*impls
));
603 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
604 impls
->list
[impls
->count
-1] = impl
;
608 static void free_impls(impls
*impls
) {
611 for (i
= 0; i
< impls
->count
; ++i
) {
612 free_impl(impls
->list
[i
]);
618 typedef struct PSI_Data
{
626 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
627 memcpy(dest
, src
, sizeof(*dest
));
628 memset(src
, 0, sizeof(*src
));
631 static inline void PSI_DataDtor(PSI_Data
*data
) {
633 free_decl_typedefs(data
->defs
);
636 free_decls(data
->decls
);
639 free_impls(data
->impls
);