1 #define _CONCAT(x,y) x##y
2 #define CONCAT(x,y) _CONCAT(x,y)
3 #define COUNTED(x) CONCAT(parse_ ##x## _, __LINE__)
6 #define DEF(dn, dv) dn dv
7 #define LET(nt, rule) nt ::= rule.
8 #define PARSE(nt, rule) nt ::= rule.
9 #define PARSE_NAMED(nt, nt_name, rule) NAMED(nt, nt_name) ::= rule.
10 #define PARSE_TYPED(nt, nt_name, rule) TYPED(nt, nt_name) ::= rule.
12 #define NAMED(t, name) t(name)
13 #define TYPED(t, name) t(name)
14 #define TOKEN_TYPE(token, type_) %type token {type_}
15 #define TOKEN_DTOR(token, dtor) %destructor token {dtor}
22 #define PARSE(nt, rule) \
23 static void COUNTED(nt) (struct psi_parser *P rule)
24 #define PARSE_NAMED(nt, nt_name, rule) \
25 static void COUNTED(nt) (struct psi_parser *P NAMED(nt, nt_name) rule)
26 #define PARSE_TYPED(nt, nt_name, rule) \
27 static void COUNTED(nt) (struct psi_parser *P TYPED(nt, nt_name) rule)
29 #define NAMED(t, name) , struct psi_token *name
30 #define TYPED(t, name) , TOKEN_TYPE_NAME(t) name
31 #define TOKEN_TYPE_NAME(token) _##token##_type
32 #define TOKEN_TYPE(token, type) typedef type TOKEN_TYPE_NAME(token);
33 #define TOKEN_DTOR(token, dtor)
37 void psi_error(int, const char *, int, const char *, ...);
40 DEF(%name
, psi_parser_proc_
)
41 DEF(%token_prefix
, PSI_T_
)
42 DEF(%token_type
, {struct psi_token
*})
43 DEF(%token_destructor
, {free($$
);})
44 DEF(%default_destructor
, {(void)P
;})
45 DEF(%extra_argument
, {struct psi_parser
*P
})
47 /* TOKEN is defined inside syntax_error */
50 if (TOKEN
&& TOKEN
->type
!= PSI_T_EOF
) {
51 psi_error(PSI_WARNING
, TOKEN
->file
, TOKEN
->line
, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN
->text
, TOKEN
->col
);
53 psi_error(PSI_WARNING
, P
->psi
.file
.fn
, P
->line
, "PSI syntax error: Unexpected end of input");
58 DEF(%left
, PLUS MINUS
.)
59 DEF(%left
, SLASH ASTERISK
.)
60 DEF(%fallback
, NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING
.)
62 DEF(%token_class
, const_type_token BOOL INT FLOAT STRING
.)
63 DEF(%token_class
, decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME
.)
64 DEF(%token_class
, impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING
.)
65 DEF(%token_class
, num_exp_token NUMBER NSNAME
.)
66 DEF(%token_class
, num_exp_op_token PLUS MINUS ASTERISK SLASH
.)
67 DEF(%token_class
, let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL
.)
68 DEF(%token_class
, set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID
.)
69 DEF(%token_class
, impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE
.)
71 TOKEN_TYPE(decl_enum
, decl_enum
*)
72 TOKEN_DTOR(decl_enum
, free_decl_enum($$
);)
73 TOKEN_TYPE(decl_enum_items
, decl_enum_items
*)
74 TOKEN_DTOR(decl_enum_items
, free_decl_enum_items($$
);)
75 TOKEN_TYPE(decl_enum_item
, decl_enum_item
*)
76 TOKEN_DTOR(decl_enum_item
, free_decl_enum_item($$
);)
77 TOKEN_TYPE(decl_struct_args_block
, decl_args
*)
78 TOKEN_DTOR(decl_struct_args_block
, free_decl_args($$
);) /* there was a typo */
79 TOKEN_TYPE(decl_struct_args
, decl_args
*)
80 TOKEN_DTOR(decl_struct_args
, free_decl_args($$
);)
81 TOKEN_TYPE(decl_struct
, decl_struct
*)
82 TOKEN_DTOR(decl_struct
, free_decl_struct($$
);)
83 TOKEN_TYPE(align_and_size
, decl_struct_layout
)
84 TOKEN_TYPE(decl_union
, decl_union
*)
85 TOKEN_DTOR(decl_union
, free_decl_union($$
);)
86 TOKEN_TYPE(const_type
, const_type
*)
87 TOKEN_DTOR(const_type
, free_const_type($$
);)
88 TOKEN_TYPE(constant
, constant
*)
89 TOKEN_DTOR(constant
, free_constant($$
);)
90 TOKEN_TYPE(decl_typedef
, decl_arg
*)
91 TOKEN_DTOR(decl_typedef
, free_decl_arg($$
);)
92 TOKEN_TYPE(decl_typedef_body_ex
, decl_arg
*)
93 TOKEN_DTOR(decl_typedef_body_ex
, free_decl_arg($$
);)
94 TOKEN_TYPE(decl_typedef_body
, decl_arg
*)
95 TOKEN_DTOR(decl_typedef_body
, free_decl_arg($$
);)
96 TOKEN_TYPE(decl_typedef_body_fn_args
, decl_args
*)
97 TOKEN_DTOR(decl_typedef_body_fn_args
, free_decl_args($$
);)
98 TOKEN_TYPE(decl
, decl
*)
99 TOKEN_DTOR(decl
, free_decl($$
);)
100 TOKEN_TYPE(decl_func
, decl_arg
*)
101 TOKEN_DTOR(decl_func
, free_decl_arg($$
);)
102 TOKEN_TYPE(decl_abi
, decl_abi
*)
103 TOKEN_DTOR(decl_abi
, free_decl_abi($$
);)
104 TOKEN_TYPE(decl_var
, decl_var
*)
105 TOKEN_DTOR(decl_var
, free_decl_var($$
);)
106 TOKEN_TYPE(decl_vars
, decl_vars
*)
107 TOKEN_DTOR(decl_vars
, free_decl_vars($$
);)
108 TOKEN_TYPE(decl_arg
, decl_arg
*)
109 TOKEN_DTOR(decl_arg
, free_decl_arg($$
);)
110 TOKEN_TYPE(decl_args
, decl_args
*)
111 TOKEN_DTOR(decl_args
, free_decl_args($$
);)
112 TOKEN_TYPE(struct_args
, decl_args
*)
113 TOKEN_DTOR(struct_args
, free_decl_args($$
);)
114 TOKEN_TYPE(struct_arg
, decl_arg
*)
115 TOKEN_DTOR(struct_arg
, free_decl_arg($$
);)
116 TOKEN_TYPE(struct_layout
, decl_struct_layout
*)
117 TOKEN_DTOR(struct_layout
, free_decl_struct_layout($$
);)
118 TOKEN_TYPE(decl_type
, decl_type
*)
119 TOKEN_DTOR(decl_type
, free_decl_type($$
);)
120 TOKEN_TYPE(const_decl_type
, decl_type
*)
121 TOKEN_DTOR(const_decl_type
, free_decl_type($$
);)
122 TOKEN_TYPE(impl
, impl
*)
123 TOKEN_DTOR(impl
, free_impl($$
);)
124 TOKEN_TYPE(impl_func
, impl_func
*)
125 TOKEN_DTOR(impl_func
, free_impl_func($$
);)
126 TOKEN_TYPE(impl_def_val
, impl_def_val
*)
127 TOKEN_DTOR(impl_def_val
, free_impl_def_val($$
);)
128 TOKEN_TYPE(impl_var
, impl_var
*)
129 TOKEN_DTOR(impl_var
, free_impl_var($$
);)
130 TOKEN_TYPE(impl_arg
, impl_arg
*)
131 TOKEN_DTOR(impl_arg
, free_impl_arg($$
);)
132 TOKEN_TYPE(impl_args
, impl_args
*)
133 TOKEN_DTOR(impl_args
, free_impl_args($$
);)
134 TOKEN_TYPE(impl_vararg
, impl_arg
*)
135 TOKEN_DTOR(impl_vararg
, free_impl_arg($$
);)
136 TOKEN_TYPE(impl_arg_list
, impl_args
*)
137 TOKEN_DTOR(impl_arg_list
, free_impl_args($$
);)
138 TOKEN_TYPE(impl_stmts
, impl_stmts
*)
139 TOKEN_DTOR(impl_stmts
, free_impl_stmts($$
);)
140 TOKEN_TYPE(impl_stmt
, impl_stmt
*)
141 TOKEN_DTOR(impl_stmt
, free_impl_stmt($$
);)
142 TOKEN_TYPE(num_exp
, num_exp
*)
143 TOKEN_DTOR(num_exp
, free_num_exp($$
);)
144 TOKEN_TYPE(let_stmt
, let_stmt
*)
145 TOKEN_DTOR(let_stmt
, free_let_stmt($$
);)
146 TOKEN_TYPE(let_calloc
, let_calloc
*)
147 TOKEN_DTOR(let_calloc
, free_let_calloc($$
);)
148 TOKEN_TYPE(let_func
, let_func
*)
149 TOKEN_DTOR(let_func
, free_let_func($$
);)
150 TOKEN_TYPE(callback_arg_list
, set_values
*)
151 TOKEN_DTOR(callback_arg_list
, free_set_values($$
);)
152 TOKEN_TYPE(callback_args
, set_values
*)
153 TOKEN_DTOR(callback_args
, free_set_values($$
);)
154 TOKEN_TYPE(let_val
, let_val
*)
155 TOKEN_DTOR(let_val
, free_let_val($$
);)
156 TOKEN_TYPE(let_vals
, let_vals
*)
157 TOKEN_DTOR(let_vals
, free_let_vals($$
);)
158 TOKEN_TYPE(set_stmt
, set_stmt
*)
159 TOKEN_DTOR(set_stmt
, free_set_stmt($$
);)
160 TOKEN_TYPE(set_value
, set_value
*)
161 TOKEN_DTOR(set_value
, free_set_value($$
);)
162 TOKEN_TYPE(set_vals
, set_value
*)
163 TOKEN_DTOR(set_vals
, free_set_value($$
);)
164 TOKEN_TYPE(set_func
, set_func
*)
165 TOKEN_DTOR(set_func
, free_set_func($$
);)
166 TOKEN_TYPE(return_stmt
, return_stmt
*)
167 TOKEN_DTOR(return_stmt
, free_return_stmt($$
);)
168 TOKEN_TYPE(free_stmt
, free_stmt
*)
169 TOKEN_DTOR(free_stmt
, free_free_stmt($$
);)
170 TOKEN_TYPE(free_calls
, free_calls
*)
171 TOKEN_DTOR(free_calls
, free_free_calls($$
);)
172 TOKEN_TYPE(free_call
, free_call
*)
173 TOKEN_DTOR(free_call
, free_free_call($$
);)
174 TOKEN_TYPE(impl_type
, impl_type
*)
175 TOKEN_DTOR(impl_type
, free_impl_type($$
);)
176 TOKEN_TYPE(reference
, char)
177 TOKEN_TYPE(indirection
, unsigned)
178 TOKEN_TYPE(pointers
, unsigned)
182 LET(blocks
, blocks block
)
187 PARSE(block
, NAMED(LIB
, token
) NAMED(QUOTED_STRING
, libname
) TOKEN(EOS
)) {
188 if (P
->psi
.file
.ln
) {
189 P
->error(P
, token
, PSI_WARNING
, "Extra 'lib %s' statement has no effect", libname
->text
);
191 P
->psi
.file
.ln
= strndup(libname
->text
+ 1, libname
->size
- 2);
196 PARSE(block
, TYPED(decl
, decl
)) {
197 P
->decls
= add_decl(P
->decls
, decl
);
200 PARSE(block
, TYPED(impl
, impl
)) {
201 P
->impls
= add_impl(P
->impls
, impl
);
204 PARSE(block
, TYPED(decl_typedef
, def
)) {
205 P
->defs
= add_decl_typedef(P
->defs
, def
);
206 switch (def
->type
->type
) {
208 if (def
->type
->real
.strct
) {
209 P
->structs
= add_decl_struct(P
->structs
, def
->type
->real
.strct
);
213 if (def
->type
->real
.unn
) {
214 P
->unions
= add_decl_union(P
->unions
, def
->type
->real
.unn
);
218 if (def
->type
->real
.enm
) {
219 P
->enums
= add_decl_enum(P
->enums
, def
->type
->real
.enm
);
225 PARSE(block
, TYPED(constant
, constant
)) {
226 P
->consts
= add_constant(P
->consts
, constant
);
229 PARSE(block
, TYPED(decl_struct
, strct
)) {
230 P
->structs
= add_decl_struct(P
->structs
, strct
);
233 PARSE(block
, TYPED(decl_union
, u
)) {
234 P
->unions
= add_decl_union(P
->unions
, u
);
237 PARSE(block
, TYPED(decl_enum
, e
)) {
238 P
->enums
= add_decl_enum(P
->enums
, e
);
241 PARSE_NAMED(optional_name
, n
, ) {
245 PARSE_NAMED(optional_name
, n
,
250 PARSE_NAMED(enum_name
, n
,
251 NAMED(ENUM
, E
) NAMED(optional_name
, N
)) {
258 psi_token_hash(E
, digest
);
259 n
= psi_token_translit(psi_token_append(E
, 1, digest
), " ", "@");
263 PARSE_TYPED(decl_enum
, e
,
266 TYPED(decl_enum_items
, list
)
268 e
= init_decl_enum(N
->text
, list
);
272 PARSE_TYPED(decl_enum_items
, l
,
273 TYPED(decl_enum_item
, i
)) {
274 l
= init_decl_enum_items(i
);
276 PARSE_TYPED(decl_enum_items
, l
,
277 TYPED(decl_enum_items
, l_
)
279 TYPED(decl_enum_item
, i
)) {
280 l
= add_decl_enum_item(l_
, i
);
283 PARSE_TYPED(decl_enum_item
, i
,
286 TYPED(num_exp
, num
)) {
287 i
= init_decl_enum_item(N
->text
, num
);
290 PARSE_TYPED(decl_enum_item
, i
,
292 i
= init_decl_enum_item(N
->text
, NULL
);
296 PARSE_NAMED(union_name
, n
,
298 NAMED(optional_name
, N
)) {
305 psi_token_hash(U
, digest
);
306 n
= psi_token_translit(psi_token_append(U
, 1, digest
), " ", "@");
310 PARSE_NAMED(struct_name
, n
,
312 NAMED(optional_name
, N
)) {
319 psi_token_hash(S
, digest
);
320 n
= psi_token_translit(psi_token_append(S
, 1, digest
), " ", "@");
324 PARSE_TYPED(decl_struct_args_block
, args_
,
326 TYPED(struct_args
, args
)
331 PARSE_TYPED(decl_struct_args
, args_
,
332 TYPED(decl_struct_args_block
, args
)) {
335 PARSE_TYPED(decl_struct_args
, args_
,
337 args_
= init_decl_args(NULL
);
340 PARSE_TYPED(decl_struct
, strct
,
343 TYPED(align_and_size
, as
)
344 TYPED(decl_struct_args
, args
)) {
345 strct
= init_decl_struct(N
->text
, args
);
346 strct
->align
= as
.pos
;
347 strct
->size
= as
.len
;
351 PARSE_TYPED(align_and_size
, as
, ) {
355 PARSE_TYPED(align_and_size
, as
,
363 as
.pos
= atol(A
->text
);
364 as
.len
= atol(S
->text
);
369 PARSE_TYPED(decl_union
, u
,
372 TYPED(align_and_size
, as
)
373 TYPED(decl_struct_args
, args
)) {
374 u
= init_decl_union(N
->text
, args
);
380 PARSE_TYPED(const_type
, type_
,
381 NAMED(const_type_token
, T
)) {
382 type_
= init_const_type(T
->type
, T
->text
);
386 PARSE_TYPED(constant
, constant
,
388 TYPED(const_type
, type
)
391 TYPED(impl_def_val
, val
)
393 constant
= init_constant(type
, T
->text
, val
);
397 PARSE_TYPED(decl_typedef
, def
,
399 TYPED(decl_typedef_body
, def_
)
405 PARSE_TYPED(decl_typedef_body_ex
, def
,
406 NAMED(struct_name
, N
)
407 TYPED(align_and_size
, as
)
408 TYPED(decl_struct_args_block
, args
)
409 TYPED(decl_var
, var
)) {
410 def
= init_decl_arg(init_decl_type(PSI_T_STRUCT
, N
->text
), var
);
411 def
->type
->token
= psi_token_copy(N
);
412 def
->type
->real
.strct
= init_decl_struct(N
->text
, args
);
413 def
->type
->real
.strct
->token
= N
;
414 def
->type
->real
.strct
->align
= as
.pos
;
415 def
->type
->real
.strct
->size
= as
.len
;
417 PARSE_TYPED(decl_typedef_body_ex
, def
,
419 TYPED(align_and_size
, as
)
420 TYPED(decl_struct_args_block
, args
)
421 TYPED(decl_var
, var
)) {
422 def
= init_decl_arg(init_decl_type(PSI_T_UNION
, N
->text
), var
);
423 def
->type
->token
= psi_token_copy(N
);
424 def
->type
->real
.unn
= init_decl_union(N
->text
, args
);
425 def
->type
->real
.unn
->token
= N
;
426 def
->type
->real
.unn
->align
= as
.pos
;
427 def
->type
->real
.unn
->size
= as
.len
;
429 PARSE_TYPED(decl_typedef_body_ex
, def
,
431 NAMED(NAME
, ALIAS
)) {
432 def
= init_decl_arg(init_decl_type(PSI_T_ENUM
, e
->name
), init_decl_var(ALIAS
->text
, 0, 0));
433 def
->var
->token
= ALIAS
;
434 def
->type
->token
= psi_token_copy(e
->token
);
435 def
->type
->real
.enm
= e
;
438 PARSE_TYPED(decl_typedef_body
, def
,
439 TYPED(decl_typedef_body_ex
, def_
)) {
443 PARSE_TYPED(decl_typedef_body_fn_args
, args
,
445 TYPED(decl_args
, args_
)
449 PARSE_TYPED(decl_typedef_body
, def
,
450 TYPED(decl_func
, func_
)
451 TYPED(decl_typedef_body_fn_args
, args
)) {
452 def
= init_decl_arg(init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
), copy_decl_var(func_
->var
));
453 def
->type
->token
= psi_token_copy(func_
->token
);
454 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
456 PARSE_TYPED(decl_typedef_body
, def
,
457 TYPED(decl_arg
, arg
)) {
461 PARSE_TYPED(decl
, decl
,
463 TYPED(decl_func
, func
)
465 TYPED(decl_args
, args
)
468 decl
= init_decl(abi
, func
, args
);
471 PARSE_TYPED(decl_func
, func
,
472 TYPED(decl_arg
, arg
)) {
475 /* special case for void functions */
476 PARSE_TYPED(decl_func
, func
,
479 func
= init_decl_arg(
480 init_decl_type(T
->type
, T
->text
),
481 init_decl_var(N
->text
, 0, 0)
483 func
->type
->token
= T
;
484 func
->var
->token
= N
;
487 PARSE_TYPED(decl_typedef_body
, def
,
489 TYPED(indirection
, decl_i
)
491 TYPED(indirection
, type_i
)
494 TYPED(decl_typedef_body_fn_args
, args
)) {
495 decl_arg
*func_
= init_decl_arg(
496 init_decl_type(T
->type
, T
->text
),
497 init_decl_var(N
->text
, decl_i
, 0)
499 func_
->type
->token
= T
;
500 func_
->var
->token
= N
;
504 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
505 copy_decl_var(func_
->var
)
507 def
->var
->pointer_level
= type_i
;
508 def
->type
->token
= psi_token_copy(func_
->token
);
509 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
511 PARSE_TYPED(decl_typedef_body
, def
,
514 TYPED(pointers
, decl_i
)
516 TYPED(indirection
, type_i
)
519 TYPED(decl_typedef_body_fn_args
, args
)) {
520 decl_arg
*func_
= init_decl_arg(
521 init_decl_type(T
->type
, T
->text
),
522 init_decl_var(N
->text
, decl_i
, 0)
524 func_
->type
->token
= T
;
525 func_
->var
->token
= N
;
529 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
530 copy_decl_var(func_
->var
)
532 def
->var
->pointer_level
= type_i
;
533 def
->type
->token
= psi_token_copy(func_
->token
);
534 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
537 PARSE_TYPED(decl_abi
, abi
,
539 abi
= init_decl_abi(T
->text
);
543 PARSE_TYPED(decl_var
, var
,
544 TYPED(indirection
, p
)
546 var
= init_decl_var(T
->text
, p
, 0);
549 PARSE_TYPED(decl_var
, var
,
550 TYPED(indirection
, p
)
555 var
= init_decl_var(T
->text
, p
+1, atol(D
->text
));
560 PARSE_TYPED(decl_vars
, vars
,
561 TYPED(decl_var
, var
)) {
562 vars
= init_decl_vars(var
);
564 PARSE_TYPED(decl_vars
, vars
,
565 TYPED(decl_vars
, vars_
)
567 TYPED(decl_var
, var
)) {
568 vars
= add_decl_var(vars_
, var
);
571 PARSE_TYPED(decl_arg
, arg_
,
572 TYPED(const_decl_type
, type
)
573 TYPED(decl_var
, var
)) {
574 arg_
= init_decl_arg(type
, var
);
576 PARSE_TYPED(decl_typedef_body
, def
,
577 TYPED(const_decl_type
, type_
)
578 TYPED(indirection
, decl_i
)
580 TYPED(indirection
, type_i
)
583 TYPED(decl_typedef_body_fn_args
, args
)) {
584 decl_arg
*func_
= init_decl_arg(
586 init_decl_var(N
->text
, decl_i
, 0)
588 func_
->var
->token
= N
;
592 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
593 copy_decl_var(func_
->var
)
595 def
->var
->pointer_level
= type_i
;
596 def
->type
->token
= psi_token_copy(func_
->token
);
597 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
600 /* void pointers need a specific rule */
601 PARSE_TYPED(decl_arg
, arg_
,
605 arg_
= init_decl_arg(
606 init_decl_type(T
->type
, T
->text
),
607 init_decl_var(N
->text
, p
, 0)
609 arg_
->type
->token
= T
;
610 arg_
->var
->token
= N
;
613 PARSE_TYPED(decl_arg
, arg_
,
618 arg_
= init_decl_arg(
619 init_decl_type(T
->type
, T
->text
),
620 init_decl_var(N
->text
, p
, 0)
622 arg_
->type
->token
= T
;
623 arg_
->var
->token
= N
;
629 PARSE_TYPED(decl_args
, args
,
630 TYPED(decl_arg
, arg
)) {
631 args
= init_decl_args(arg
);
633 PARSE_TYPED(decl_args
, args
,
634 TYPED(decl_args
, args_
)
636 TYPED(decl_arg
, arg
)) {
637 args
= add_decl_arg(args_
, arg
);
639 PARSE_TYPED(decl_args
, args
,
640 TYPED(decl_args
, args_
)
647 PARSE_TYPED(struct_args
, args
,
648 TYPED(struct_arg
, arg
)) {
649 args
= init_decl_args(arg
);
651 PARSE_TYPED(struct_args
, args
,
652 TYPED(struct_args
, args_
)
653 TYPED(struct_arg
, arg
)) {
654 args
= add_decl_arg(args_
, arg
);
657 PARSE_TYPED(struct_arg
, arg_
,
658 TYPED(decl_typedef_body_ex
, def
)
661 switch (def
->type
->type
) {
663 if (def
->type
->real
.strct
) {
664 P
->structs
= add_decl_struct(P
->structs
, def
->type
->real
.strct
);
668 if (def
->type
->real
.unn
) {
669 P
->unions
= add_decl_union(P
->unions
, def
->type
->real
.unn
);
673 if (def
->type
->real
.enm
) {
674 P
->enums
= add_decl_enum(P
->enums
, def
->type
->real
.enm
);
679 PARSE_TYPED(struct_arg
, arg
,
680 TYPED(decl_arg
, arg_
)
681 TYPED(struct_layout
, layout_
)
683 arg_
->layout
= layout_
;
687 PARSE_TYPED(struct_layout
, layout
, ) {
690 PARSE_TYPED(struct_layout
, layout
,
698 layout
= init_decl_struct_layout(atol(POS
->text
), atol(SIZ
->text
));
703 /* un/signed, urgh */
704 PARSE_NAMED(decl_scalar_type
, type_
,
708 PARSE_NAMED(decl_scalar_type
, type_
,
710 NAMED(decl_scalar_type_short
, s
)) {
712 type_
= psi_token_cat(2, S
, s
);
719 PARSE_NAMED(decl_scalar_type_short
, s
, ) {
723 PARSE_NAMED(decl_scalar_type_short
, s
,
727 PARSE_NAMED(decl_scalar_type
, type_
,
731 PARSE_NAMED(decl_scalar_type
, type_
,
733 NAMED(decl_scalar_type_long
, l
)) {
735 type_
= psi_token_cat(2, L
, l
);
742 PARSE_NAMED(decl_scalar_type_long
, l
, ) {
745 PARSE_NAMED(decl_scalar_type_long
, l
,
749 PARSE_NAMED(decl_scalar_type_long
, l
,
751 NAMED(decl_scalar_type_long_long
, ll
)) {
753 l
= psi_token_cat(2, L
, ll
);
760 PARSE_NAMED(decl_scalar_type_long_long
, ll
, ) {
763 PARSE_NAMED(decl_scalar_type_long_long
, ll
,
767 PARSE_TYPED(decl_type
, type_
,
769 NAMED(decl_scalar_type
, N
)) {
770 struct psi_token
*T
= psi_token_cat(2, U
, N
);
771 type_
= init_decl_type(T
->type
, T
->text
);
776 PARSE_TYPED(decl_type
, type_
,
778 NAMED(decl_scalar_type
, N
)) {
779 struct psi_token
*T
= psi_token_cat(2, S
, N
);
780 type_
= init_decl_type(T
->type
, T
->text
);
785 PARSE_TYPED(decl_type
, type_
,
786 NAMED(UNSIGNED
, U
)) {
787 type_
= init_decl_type(PSI_T_NAME
, U
->text
);
790 PARSE_TYPED(decl_type
, type_
,
792 type_
= init_decl_type(PSI_T_NAME
, S
->text
);
795 PARSE_TYPED(decl_type
, type_
,
796 NAMED(decl_scalar_type
, N
)) {
797 type_
= init_decl_type(N
->type
, N
->text
);
801 PARSE_TYPED(decl_type
, type_
,
804 type_
= init_decl_type(S
->type
, T
->text
);
808 PARSE_TYPED(decl_type
, type_
,
811 type_
= init_decl_type(U
->type
, T
->text
);
815 PARSE_TYPED(decl_type
, type_
,
818 type_
= init_decl_type(E
->type
, T
->text
);
822 PARSE_TYPED(decl_type
, type_
,
823 NAMED(decl_type_token
, T
)) {
824 type_
= init_decl_type(T
->type
, T
->text
);
829 PARSE_TYPED(const_decl_type
, type
,
830 TYPED(decl_type
, type_
)) {
833 PARSE_TYPED(const_decl_type
, type
,
835 TYPED(decl_type
, type_
)) {
839 PARSE_TYPED(impl
, impl
,
840 TYPED(impl_func
, func
)
842 TYPED(impl_stmts
, stmts
)
844 impl
= init_impl(func
, stmts
);
847 PARSE_TYPED(impl_func
, func
,
851 TYPED(impl_args
, args
)
853 TYPED(impl_type
, type
)) {
854 func
= init_impl_func(NAME
->text
, args
, type
, r
);
858 PARSE_TYPED(impl_def_val
, def
,
859 NAMED(impl_def_val_token
, T
)) {
860 def
= init_impl_def_val(T
->type
, T
->text
);
864 PARSE_TYPED(impl_var
, var
,
866 NAMED(DOLLAR_NAME
, T
)) {
867 var
= init_impl_var(T
->text
, r
);
871 PARSE_TYPED(impl_arg
, arg
,
872 TYPED(impl_type
, type
)
873 TYPED(impl_var
, var
)) {
874 arg
= init_impl_arg(type
, var
, NULL
);
876 PARSE_TYPED(impl_arg
, arg
,
877 TYPED(impl_type
, type
)
880 TYPED(impl_def_val
, def
)) {
881 arg
= init_impl_arg(type
, var
, def
);
884 PARSE_TYPED(impl_args
, args
,
889 PARSE_TYPED(impl_args
, args
,
891 TYPED(impl_arg_list
, args_
)
895 PARSE_TYPED(impl_args
, args
,
897 TYPED(impl_arg_list
, args_
)
899 TYPED(impl_vararg
, va
)
902 args
->vararg
.name
= va
;
905 PARSE_TYPED(impl_vararg
, va
,
906 TYPED(impl_type
, type
)
909 NAMED(DOLLAR_NAME
, T
)) {
910 va
= init_impl_arg(type
, init_impl_var(T
->text
, r
), NULL
);
914 PARSE_TYPED(impl_arg_list
, args
,
915 TYPED(impl_arg
, arg
)) {
916 args
= init_impl_args(arg
);
918 PARSE_TYPED(impl_arg_list
, args
,
919 TYPED(impl_arg_list
, args_
)
921 TYPED(impl_arg
, arg
)) {
922 args
= add_impl_arg(args_
, arg
);
925 PARSE_TYPED(impl_stmts
, stmts
,
926 TYPED(impl_stmt
, stmt
)) {
927 stmts
= init_impl_stmts(stmt
);
929 PARSE_TYPED(impl_stmts
, stmts
,
930 TYPED(impl_stmts
, stmts_
)
931 TYPED(impl_stmt
, stmt
)) {
932 stmts
= add_impl_stmt(stmts_
, stmt
);
935 PARSE_TYPED(impl_stmt
, stmt
,
936 TYPED(let_stmt
, let
)) {
937 stmt
= init_impl_stmt(PSI_T_LET
, let
);
939 PARSE_TYPED(impl_stmt
, stmt
,
940 TYPED(set_stmt
, set
)) {
941 stmt
= init_impl_stmt(PSI_T_SET
, set
);
943 PARSE_TYPED(impl_stmt
, stmt
,
944 TYPED(return_stmt
, ret
)) {
945 stmt
= init_impl_stmt(PSI_T_RETURN
, ret
);
947 PARSE_TYPED(impl_stmt
, stmt
,
948 TYPED(free_stmt
, free
)) {
949 stmt
= init_impl_stmt(PSI_T_FREE
, free
);
952 PARSE_TYPED(num_exp
, exp
,
953 NAMED(num_exp_token
, tok
)) {
954 exp
= init_num_exp(tok
->type
, tok
->text
);
957 PARSE_TYPED(num_exp
, exp
,
958 TYPED(decl_var
, var
)) {
959 exp
= init_num_exp(PSI_T_NAME
, var
);
960 exp
->token
= psi_token_copy(var
->token
);
962 PARSE_TYPED(num_exp
, exp
,
964 NAMED(num_exp_op_token
, operator_
)
965 TYPED(num_exp
, operand_
)) {
966 exp_
->operator = operator_
->type
;
967 exp_
->operand
= operand_
;
972 PARSE_TYPED(let_stmt
, let
,
976 let
= init_let_stmt(var
, init_let_val(PSI_LET_NULL
, NULL
));
978 PARSE_TYPED(let_stmt
, let
,
985 val
->flags
.one
.is_reference
= r
? 1 : 0;
986 let
= init_let_stmt(var
, val
);
988 PARSE_TYPED(let_stmt
, let
,
994 let
= init_let_stmt(var
, init_let_val(PSI_LET_TMP
, val
));
997 PARSE_TYPED(let_calloc
, alloc
,
998 TYPED(num_exp
, nmemb
)
1000 TYPED(num_exp
, size
)) {
1001 alloc
= init_let_calloc(nmemb
, size
);
1004 PARSE_TYPED(let_func
, func
,
1005 NAMED(let_func_token
, T
)
1007 TYPED(impl_var
, var
)
1009 func
= init_let_func(T
->type
, T
->text
, var
);
1013 PARSE_TYPED(let_vals
, vals
,
1014 TYPED(let_val
, val
)) {
1015 vals
= init_let_vals(val
);
1017 PARSE_TYPED(let_vals
, vals
,
1018 TYPED(let_vals
, vals_
)
1020 TYPED(let_val
, val
)) {
1021 vals
= add_let_val(vals_
, val
);
1024 PARSE_TYPED(let_func
, func
,
1025 NAMED(let_func_token
, T
)
1027 TYPED(impl_var
, var
)
1029 TYPED(let_vals
, vals
)
1031 func
= init_let_func(T
->type
, T
->text
, var
);
1036 LET(callback_arg_list
, )
1037 PARSE_TYPED(callback_arg_list
, args
,
1038 TYPED(callback_args
, args_
)) {
1042 PARSE_TYPED(callback_args
, args
,
1043 TYPED(set_value
, val
)) {
1044 args
= init_set_values(val
);
1046 PARSE_TYPED(callback_args
, args
,
1047 TYPED(callback_args
, args_
)
1049 TYPED(set_value
, val
)) {
1050 args
= add_set_value(args_
, val
);
1052 PARSE_NAMED(callback_rval
, rval
,
1053 NAMED(let_func_token
, F
)) {
1056 PARSE_NAMED(callback_rval
, rval
,
1061 PARSE_TYPED(let_val
, val
,
1063 val
= init_let_val(PSI_LET_NULL
, NULL
);
1065 PARSE_TYPED(let_val
, val
,
1066 TYPED(num_exp
, exp
)) {
1067 val
= init_let_val(PSI_LET_NUMEXP
, exp
);
1069 PARSE_TYPED(let_val
, val
,
1072 TYPED(let_calloc
, alloc
)
1074 val
= init_let_val(PSI_LET_CALLOC
, alloc
);
1076 PARSE_TYPED(let_val
, val
,
1077 TYPED(let_func
, func_
)) {
1078 val
= init_let_val(PSI_LET_FUNC
, func_
);
1080 PARSE_TYPED(let_val
, val
,
1082 NAMED(callback_rval
, F
)
1084 TYPED(impl_var
, var
)
1086 TYPED(callback_arg_list
, args_
)
1089 val
= init_let_val(PSI_LET_CALLBACK
, init_let_callback(
1090 init_let_func(F
->type
, F
->text
, var
), args_
));
1094 PARSE_TYPED(set_stmt
, set
,
1096 TYPED(impl_var
, var
)
1098 TYPED(set_value
, val
)
1100 set
= init_set_stmt(var
, val
);
1103 PARSE_TYPED(set_value
, val
,
1104 TYPED(set_func
, func
)
1106 TYPED(decl_var
, var
)
1108 val
= init_set_value(func
, init_decl_vars(var
));
1110 PARSE_TYPED(set_value
, val
,
1111 TYPED(set_func
, func
)
1113 TYPED(decl_var
, var
)
1115 TYPED(num_exp
, num_
)
1117 val
= init_set_value(func
, init_decl_vars(var
));
1120 PARSE_TYPED(set_value
, val
,
1121 TYPED(set_func
, func_
)
1123 TYPED(decl_var
, var
)
1127 free_set_func(func_
);
1128 val
= init_set_value(init_set_func(T
->type
, T
->text
), init_decl_vars(var
));
1129 val
->func
->token
= T
;
1131 PARSE_TYPED(set_value
, val
,
1132 TYPED(set_func
, func_
)
1134 TYPED(decl_var
, var
)
1136 TYPED(set_vals
, vals
)
1140 val
->vars
= init_decl_vars(var
);
1142 PARSE_TYPED(set_value
, val
,
1143 TYPED(set_func
, func_
)
1145 TYPED(decl_var
, var
)
1147 TYPED(num_exp
, num_
)
1149 TYPED(set_vals
, vals
)
1154 val
->vars
= init_decl_vars(var
);
1157 PARSE_TYPED(set_vals
, vals
,
1158 TYPED(set_value
, val
)) {
1159 vals
= add_inner_set_value(init_set_value(NULL
, NULL
), val
);
1161 PARSE_TYPED(set_vals
, vals
,
1162 TYPED(set_vals
, vals_
)
1164 TYPED(set_value
, val
)) {
1165 vals
= add_inner_set_value(vals_
, val
);
1168 PARSE_TYPED(set_func
, func
,
1169 NAMED(set_func_token
, T
)) {
1170 func
= init_set_func(T
->type
, T
->text
);
1174 PARSE_TYPED(return_stmt
, ret
,
1176 TYPED(set_value
, val
)
1178 ret
= init_return_stmt(val
);
1182 PARSE_TYPED(free_stmt
, free
,
1184 TYPED(free_calls
, calls
)
1186 free
= init_free_stmt(calls
);
1189 PARSE_TYPED(free_calls
, calls
,
1190 TYPED(free_call
, call
)) {
1191 calls
= init_free_calls(call
);
1193 PARSE_TYPED(free_calls
, calls
,
1194 TYPED(free_calls
, calls_
)
1196 TYPED(free_call
, call
)) {
1197 calls
= add_free_call(calls_
, call
);
1200 PARSE_TYPED(free_call
, call
,
1203 TYPED(decl_vars
, vars
)
1205 call
= init_free_call(F
->text
, vars
);
1209 PARSE_TYPED(impl_type
, type_
,
1210 NAMED(impl_type_token
, T
)) {
1211 type_
= init_impl_type(T
->type
, T
->text
);
1215 PARSE_TYPED(reference
, r
, ) {
1218 PARSE_TYPED(reference
, r
,
1223 PARSE_TYPED(indirection
, i
, ){
1226 PARSE_TYPED(indirection
, i
,
1227 TYPED(pointers
, p
)) {
1231 PARSE_TYPED(pointers
, p
,
1235 PARSE_TYPED(pointers
, p
,