2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
8 * Copyright (C) 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
28 #include "expression.h"
31 #define warn_on_mixed (1)
33 static struct symbol_list
**function_symbol_list
;
34 struct symbol_list
*function_computed_target_list
;
35 struct statement_list
*function_computed_goto_list
;
37 static struct token
*statement(struct token
*token
, struct statement
**tree
);
38 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
);
40 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
);
41 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
);
42 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
);
43 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
);
44 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
);
46 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
47 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
48 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
49 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
50 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
51 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
52 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
53 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
54 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
55 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
56 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
57 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
58 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
59 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
60 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
);
63 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
64 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
65 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
66 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
67 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
68 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
69 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
70 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
73 static struct symbol_op modifier_op
= {
77 static struct symbol_op qualifier_op
= {
81 static struct symbol_op typeof_op
= {
83 .declarator
= typeof_specifier
,
86 static struct symbol_op attribute_op
= {
88 .declarator
= attribute_specifier
,
91 static struct symbol_op struct_op
= {
93 .declarator
= struct_specifier
,
96 static struct symbol_op union_op
= {
98 .declarator
= union_specifier
,
101 static struct symbol_op enum_op
= {
102 .type
= KW_SPECIFIER
,
103 .declarator
= enum_specifier
,
108 static struct symbol_op if_op
= {
109 .statement
= parse_if_statement
,
112 static struct symbol_op return_op
= {
113 .statement
= parse_return_statement
,
116 static struct symbol_op loop_iter_op
= {
117 .statement
= parse_loop_iterator
,
120 static struct symbol_op default_op
= {
121 .statement
= parse_default_statement
,
124 static struct symbol_op case_op
= {
125 .statement
= parse_case_statement
,
128 static struct symbol_op switch_op
= {
129 .statement
= parse_switch_statement
,
132 static struct symbol_op for_op
= {
133 .statement
= parse_for_statement
,
136 static struct symbol_op while_op
= {
137 .statement
= parse_while_statement
,
140 static struct symbol_op do_op
= {
141 .statement
= parse_do_statement
,
144 static struct symbol_op goto_op
= {
145 .statement
= parse_goto_statement
,
148 static struct symbol_op __context___op
= {
149 .statement
= parse_context_statement
,
152 static struct symbol_op range_op
= {
153 .statement
= parse_range_statement
,
156 static struct symbol_op asm_op
= {
158 .declarator
= parse_asm_declarator
,
159 .statement
= parse_asm_statement
,
160 .toplevel
= toplevel_asm_declaration
,
163 static struct symbol_op packed_op
= {
164 .attribute
= attribute_packed
,
167 static struct symbol_op aligned_op
= {
168 .attribute
= attribute_aligned
,
171 static struct symbol_op attr_mod_op
= {
172 .attribute
= attribute_modifier
,
175 static struct symbol_op address_space_op
= {
176 .attribute
= attribute_address_space
,
179 static struct symbol_op mode_op
= {
180 .attribute
= attribute_mode
,
183 static struct symbol_op context_op
= {
184 .attribute
= attribute_context
,
187 static struct symbol_op transparent_union_op
= {
188 .attribute
= attribute_transparent_union
,
191 static struct symbol_op ignore_attr_op
= {
192 .attribute
= ignore_attribute
,
195 static struct symbol_op mode_spec_op
= {
199 static struct init_keyword
{
202 unsigned long modifiers
;
203 struct symbol_op
*op
;
204 } keyword_table
[] = {
205 /* Type qualifiers */
206 { "const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
207 { "__const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
208 { "__const__", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
209 { "volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
210 { "__volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
211 { "__volatile__", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
214 { "typedef", NS_TYPEDEF
, MOD_TYPEDEF
, .op
= &modifier_op
},
217 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
218 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
219 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
221 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
222 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
224 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
225 { "union", NS_TYPEDEF
, .op
= &union_op
},
226 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
228 { "inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
229 { "__inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
230 { "__inline__", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
232 /* Ignored for now.. */
233 { "restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
234 { "__restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
237 { "if", NS_KEYWORD
, .op
= &if_op
},
238 { "return", NS_KEYWORD
, .op
= &return_op
},
239 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
240 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
241 { "default", NS_KEYWORD
, .op
= &default_op
},
242 { "case", NS_KEYWORD
, .op
= &case_op
},
243 { "switch", NS_KEYWORD
, .op
= &switch_op
},
244 { "for", NS_KEYWORD
, .op
= &for_op
},
245 { "while", NS_KEYWORD
, .op
= &while_op
},
246 { "do", NS_KEYWORD
, .op
= &do_op
},
247 { "goto", NS_KEYWORD
, .op
= &goto_op
},
248 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
249 { "__range__", NS_KEYWORD
, .op
= &range_op
},
250 { "asm", NS_KEYWORD
, .op
= &asm_op
},
251 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
252 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
255 { "packed", NS_KEYWORD
, .op
= &packed_op
},
256 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
257 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
258 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
259 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
260 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
261 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
262 { "force", NS_KEYWORD
, MOD_FORCE
, .op
= &attr_mod_op
},
263 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
264 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
265 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
266 { "mode", NS_KEYWORD
, .op
= &mode_op
},
267 { "context", NS_KEYWORD
, .op
= &context_op
},
268 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
270 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
271 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_spec_op
},
272 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_spec_op
},
273 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_spec_op
},
274 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_spec_op
},
275 { "SI", NS_KEYWORD
, .op
= &mode_spec_op
},
276 { "__SI__", NS_KEYWORD
, .op
= &mode_spec_op
},
277 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_spec_op
},
278 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_spec_op
},
279 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_spec_op
},
280 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_spec_op
},
282 /* Ignored attributes */
283 { "nothrow", NS_KEYWORD
, .op
= &ignore_attr_op
},
284 { "__nothrow", NS_KEYWORD
, .op
= &ignore_attr_op
},
285 { "__nothrow__", NS_KEYWORD
, .op
= &ignore_attr_op
},
286 { "__malloc__", NS_KEYWORD
, .op
= &ignore_attr_op
},
287 { "nonnull", NS_KEYWORD
, .op
= &ignore_attr_op
},
288 { "__nonnull", NS_KEYWORD
, .op
= &ignore_attr_op
},
289 { "__nonnull__", NS_KEYWORD
, .op
= &ignore_attr_op
},
290 { "format", NS_KEYWORD
, .op
= &ignore_attr_op
},
291 { "__format__", NS_KEYWORD
, .op
= &ignore_attr_op
},
292 { "__format_arg__", NS_KEYWORD
, .op
= &ignore_attr_op
},
293 { "section", NS_KEYWORD
, .op
= &ignore_attr_op
},
294 { "__section__",NS_KEYWORD
, .op
= &ignore_attr_op
},
295 { "unused", NS_KEYWORD
, .op
= &ignore_attr_op
},
296 { "__unused__", NS_KEYWORD
, .op
= &ignore_attr_op
},
297 { "const", NS_KEYWORD
, .op
= &ignore_attr_op
},
298 { "__const", NS_KEYWORD
, .op
= &ignore_attr_op
},
299 { "__const__", NS_KEYWORD
, .op
= &ignore_attr_op
},
300 { "noreturn", NS_KEYWORD
, .op
= &ignore_attr_op
},
301 { "__noreturn__", NS_KEYWORD
, .op
= &ignore_attr_op
},
302 { "no_instrument_function", NS_KEYWORD
, .op
= &ignore_attr_op
},
303 { "__no_instrument_function__", NS_KEYWORD
, .op
= &ignore_attr_op
},
304 { "sentinel", NS_KEYWORD
, .op
= &ignore_attr_op
},
305 { "__sentinel__", NS_KEYWORD
, .op
= &ignore_attr_op
},
306 { "regparm", NS_KEYWORD
, .op
= &ignore_attr_op
},
307 { "__regparm__", NS_KEYWORD
, .op
= &ignore_attr_op
},
308 { "weak", NS_KEYWORD
, .op
= &ignore_attr_op
},
309 { "__weak__", NS_KEYWORD
, .op
= &ignore_attr_op
},
310 { "alias", NS_KEYWORD
, .op
= &ignore_attr_op
},
311 { "__alias__", NS_KEYWORD
, .op
= &ignore_attr_op
},
312 { "pure", NS_KEYWORD
, .op
= &ignore_attr_op
},
313 { "__pure__", NS_KEYWORD
, .op
= &ignore_attr_op
},
314 { "always_inline", NS_KEYWORD
, .op
= &ignore_attr_op
},
315 { "syscall_linkage", NS_KEYWORD
, .op
= &ignore_attr_op
},
316 { "visibility", NS_KEYWORD
, .op
= &ignore_attr_op
},
317 { "__visibility__", NS_KEYWORD
, .op
= &ignore_attr_op
},
318 { "deprecated", NS_KEYWORD
, .op
= &ignore_attr_op
},
319 { "__deprecated__", NS_KEYWORD
, .op
= &ignore_attr_op
},
320 { "noinline", NS_KEYWORD
, .op
= &ignore_attr_op
},
321 { "__used__", NS_KEYWORD
, .op
= &ignore_attr_op
},
322 { "warn_unused_result", NS_KEYWORD
, .op
= &ignore_attr_op
},
323 { "__warn_unused_result__", NS_KEYWORD
, .op
= &ignore_attr_op
},
324 { "model", NS_KEYWORD
, .op
= &ignore_attr_op
},
325 { "__model__", NS_KEYWORD
, .op
= &ignore_attr_op
},
328 void init_parser(int stream
)
331 for (i
= 0; i
< sizeof keyword_table
/sizeof keyword_table
[0]; i
++) {
332 struct init_keyword
*ptr
= keyword_table
+ i
;
333 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
334 sym
->ident
->keyword
= 1;
335 sym
->ctype
.modifiers
= ptr
->modifiers
;
340 // Add a symbol to the list of function-local symbols
341 static void fn_local_symbol(struct symbol
*sym
)
343 if (function_symbol_list
)
344 add_symbol(function_symbol_list
, sym
);
347 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
351 if (token_type(token
) != TOKEN_IDENT
)
354 va_start(args
, token
);
356 struct ident
* next
= va_arg(args
, struct ident
*);
359 if (token
->ident
== next
)
365 struct statement
*alloc_statement(struct position pos
, int type
)
367 struct statement
*stmt
= __alloc_statement(0);
373 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
375 static int apply_modifiers(struct position pos
, struct ctype
*ctype
)
379 while ((base
= ctype
->base_type
)) {
380 switch (base
->type
) {
386 ctype
= &base
->ctype
;
392 /* Turn the "virtual types" into real types with real sizes etc */
393 if (ctype
->base_type
== &int_type
) {
394 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
395 ctype
->modifiers
&= ~MOD_SPECIFIER
;
396 } else if (ctype
->base_type
== &fp_type
) {
397 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
398 ctype
->modifiers
&= ~MOD_SPECIFIER
;
401 if (ctype
->modifiers
& MOD_BITWISE
) {
403 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
404 if (!is_int_type(ctype
->base_type
)) {
405 sparse_error(pos
, "invalid modifier");
408 type
= alloc_symbol(pos
, SYM_BASETYPE
);
409 *type
= *ctype
->base_type
;
410 type
->ctype
.base_type
= ctype
->base_type
;
411 type
->type
= SYM_RESTRICT
;
412 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
413 ctype
->base_type
= type
;
419 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
421 struct symbol
*sym
= alloc_symbol(pos
, type
);
423 sym
->ctype
.base_type
= ctype
->base_type
;
424 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
426 ctype
->base_type
= sym
;
427 ctype
->modifiers
&= MOD_STORAGE
;
431 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
433 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
435 sym
= alloc_symbol(token
->pos
, type
);
436 bind_symbol(sym
, token
->ident
, ns
);
437 if (type
== SYM_LABEL
)
438 fn_local_symbol(sym
);
444 * NOTE! NS_LABEL is not just a different namespace,
445 * it also ends up using function scope instead of the
446 * regular symbol scope.
448 struct symbol
*label_symbol(struct token
*token
)
450 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
453 static struct token
*struct_union_enum_specifier(enum type type
,
454 struct token
*token
, struct ctype
*ctype
,
455 struct token
*(*parse
)(struct token
*, struct symbol
*))
458 struct position
*repos
;
460 ctype
->modifiers
= 0;
461 token
= handle_attributes(token
, ctype
);
462 if (token_type(token
) == TOKEN_IDENT
) {
463 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
465 (sym
->scope
!= block_scope
&&
466 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
467 // Either a new symbol, or else an out-of-scope
468 // symbol being redefined.
469 sym
= alloc_symbol(token
->pos
, type
);
470 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
472 if (sym
->type
!= type
)
473 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
474 ctype
->base_type
= sym
;
477 if (match_op(token
, '{')) {
478 // The following test is actually wrong for empty
479 // structs, but (1) they are not C99, (2) gcc does
480 // the same thing, and (3) it's easier.
481 if (sym
->symbol_list
)
482 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
484 token
= parse(token
->next
, sym
);
485 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
487 // Mark the structure as needing re-examination
493 // private struct/union/enum type
494 if (!match_op(token
, '{')) {
495 sparse_error(token
->pos
, "expected declaration");
496 ctype
->base_type
= &bad_ctype
;
500 sym
= alloc_symbol(token
->pos
, type
);
501 token
= parse(token
->next
, sym
);
502 ctype
->base_type
= sym
;
503 return expect(token
, '}', "at end of specifier");
506 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
508 return struct_declaration_list(token
, &sym
->symbol_list
);
511 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
)
513 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctype
, parse_struct_declaration
);
516 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
)
518 return struct_union_enum_specifier(SYM_UNION
, token
, ctype
, parse_struct_declaration
);
524 unsigned long long y
;
527 static void upper_boundary(Num
*n
, Num
*v
)
539 static void lower_boundary(Num
*n
, Num
*v
)
551 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
553 int shift
= type
->bit_size
;
554 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
558 if (upper
->x
== 0 && upper
->y
>> shift
)
560 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
565 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
567 if (s1
->bit_size
< s2
->bit_size
) {
569 } else if (s1
->bit_size
== s2
->bit_size
) {
570 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
573 if (s1
->bit_size
< bits_in_int
)
578 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
582 FOR_EACH_PTR(list
, sym
) {
583 struct expression
*expr
= sym
->initializer
;
584 struct symbol
*ctype
;
585 if (expr
->type
!= EXPR_VALUE
)
588 if (ctype
->bit_size
== base_type
->bit_size
)
590 cast_value(expr
, base_type
, expr
, ctype
);
591 } END_FOR_EACH_PTR(sym
);
594 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
596 unsigned long long lastval
= 0;
597 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
598 Num upper
= {-1, 0}, lower
= {1, 0};
599 struct symbol_list
*entries
= NULL
;
601 parent
->examined
= 1;
602 parent
->ctype
.base_type
= &int_ctype
;
603 while (token_type(token
) == TOKEN_IDENT
) {
604 struct expression
*expr
= NULL
;
605 struct token
*next
= token
->next
;
608 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
609 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
610 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
612 if (match_op(next
, '=')) {
613 next
= constant_expression(next
->next
, &expr
);
614 lastval
= get_expression_value(expr
);
616 if (expr
&& expr
->ctype
)
620 } else if (is_int_type(ctype
)) {
623 error_die(token
->pos
, "can't increment the last enum member");
627 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
628 expr
->value
= lastval
;
632 sym
->initializer
= expr
;
633 sym
->ctype
.base_type
= parent
;
634 add_ptr_list(&entries
, sym
);
636 if (base_type
!= &bad_ctype
) {
637 if (ctype
->type
== SYM_NODE
)
638 ctype
= ctype
->ctype
.base_type
;
639 if (ctype
->type
== SYM_ENUM
) {
643 ctype
= ctype
->ctype
.base_type
;
647 * - if all enums are of the same type, then
648 * the base_type is that type (two first
650 * - if enums are of different types, they
651 * all have to be integer types, and the
652 * base type is at least "int_ctype".
653 * - otherwise the base_type is "bad_ctype".
657 } else if (ctype
== base_type
) {
659 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
660 base_type
= bigger_enum_type(base_type
, ctype
);
662 base_type
= &bad_ctype
;
663 parent
->ctype
.base_type
= base_type
;
665 if (is_int_type(base_type
)) {
666 Num v
= {.y
= lastval
};
667 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
669 else if ((long long)lastval
>= 0)
673 upper_boundary(&upper
, &v
);
674 lower_boundary(&lower
, &v
);
677 if (!match_op(token
, ','))
682 sparse_error(token
->pos
, "bad enum definition");
683 base_type
= &bad_ctype
;
685 else if (!is_int_type(base_type
))
686 base_type
= base_type
;
687 else if (type_is_ok(base_type
, &upper
, &lower
))
688 base_type
= base_type
;
689 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
690 base_type
= &int_ctype
;
691 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
692 base_type
= &uint_ctype
;
693 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
694 base_type
= &long_ctype
;
695 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
696 base_type
= &ulong_ctype
;
697 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
698 base_type
= &llong_ctype
;
699 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
700 base_type
= &ullong_ctype
;
702 base_type
= &bad_ctype
;
703 parent
->ctype
.base_type
= base_type
;
704 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
705 parent
->examined
= 0;
707 cast_enum_list(entries
, base_type
);
708 free_ptr_list(&entries
);
713 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
715 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
717 ctype
= &ctype
->base_type
->ctype
;
718 if (!ctype
->base_type
)
719 ctype
->base_type
= &incomplete_ctype
;
724 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
728 if (!match_op(token
, '(')) {
729 sparse_error(token
->pos
, "expected '(' after typeof");
732 if (lookup_type(token
->next
)) {
733 token
= typename(token
->next
, &sym
);
736 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
737 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
739 ctype
->modifiers
= 0;
740 ctype
->base_type
= typeof_sym
;
742 return expect(token
, ')', "after typeof");
745 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
747 struct expression
*expr
= NULL
;
748 if (match_op(token
, '('))
749 token
= parens_expression(token
, &expr
, "in attribute");
753 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
755 ctype
->alignment
= 1;
759 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
761 int alignment
= max_alignment
;
762 struct expression
*expr
= NULL
;
764 if (match_op(token
, '(')) {
765 token
= parens_expression(token
, &expr
, "in attribute");
767 alignment
= get_expression_value(expr
);
769 ctype
->alignment
= alignment
;
773 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
775 ctype
->modifiers
|= attr
->ctype
.modifiers
;
779 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
781 struct expression
*expr
= NULL
;
782 token
= expect(token
, '(', "after address_space attribute");
783 token
= conditional_expression(token
, &expr
);
785 ctype
->as
= get_expression_value(expr
);
786 token
= expect(token
, ')', "after address_space attribute");
790 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
792 token
= expect(token
, '(', "after mode attribute");
793 if (token_type(token
) == TOKEN_IDENT
) {
794 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
795 if (mode
&& mode
->op
->type
== KW_MODE
)
796 ctype
->modifiers
|= mode
->ctype
.modifiers
;
798 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
801 sparse_error(token
->pos
, "expect attribute mode symbol\n");
802 token
= expect(token
, ')', "after mode attribute");
806 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
808 struct context
*context
= alloc_context();
809 struct expression
*args
[3];
812 token
= expect(token
, '(', "after context attribute");
813 while (!match_op(token
, ')')) {
814 struct expression
*expr
= NULL
;
815 token
= conditional_expression(token
, &expr
);
820 if (!match_op(token
, ','))
827 sparse_error(token
->pos
, "expected context input/output values");
830 context
->in
= get_expression_value(args
[0]);
833 context
->in
= get_expression_value(args
[0]);
834 context
->out
= get_expression_value(args
[1]);
837 context
->context
= args
[0];
838 context
->in
= get_expression_value(args
[1]);
839 context
->out
= get_expression_value(args
[2]);
844 add_ptr_list(&ctype
->contexts
, context
);
846 token
= expect(token
, ')', "after context attribute");
850 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
852 if (Wtransparent_union
)
853 sparse_error(token
->pos
, "ignoring attribute __transparent_union__");
857 static struct token
*recover_unknown_attribute(struct token
*token
)
859 struct expression
*expr
= NULL
;
861 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
863 if (match_op(token
, '('))
864 token
= parens_expression(token
, &expr
, "in attribute");
868 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
870 ctype
->modifiers
= 0;
871 token
= expect(token
, '(', "after attribute");
872 token
= expect(token
, '(', "after attribute");
875 struct ident
*attribute_name
;
878 if (eof_token(token
))
880 if (match_op(token
, ';'))
882 if (token_type(token
) != TOKEN_IDENT
)
884 attribute_name
= token
->ident
;
885 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
886 if (attr
&& attr
->op
->attribute
)
887 token
= attr
->op
->attribute(token
->next
, attr
, ctype
);
889 token
= recover_unknown_attribute(token
);
891 if (!match_op(token
, ','))
896 token
= expect(token
, ')', "after attribute");
897 token
= expect(token
, ')', "after attribute");
901 struct symbol
* ctype_integer(unsigned long spec
)
903 static struct symbol
*const integer_ctypes
[][3] = {
904 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
905 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
906 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
907 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
908 { &int_ctype
, &sint_ctype
, &uint_ctype
},
910 struct symbol
*const (*ctype
)[3];
913 ctype
= integer_ctypes
;
914 if (!(spec
& MOD_LONGLONG
)) {
916 if (!(spec
& MOD_LONG
)) {
918 if (!(spec
& MOD_SHORT
)) {
920 if (!(spec
& MOD_CHAR
))
926 sub
= ((spec
& MOD_UNSIGNED
)
928 : ((spec
& MOD_EXPLICITLY_SIGNED
)
932 return ctype
[0][sub
];
935 struct symbol
* ctype_fp(unsigned long spec
)
937 if (spec
& MOD_LONGLONG
)
938 return &ldouble_ctype
;
940 return &double_ctype
;
944 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
946 unsigned long mod
= thistype
->modifiers
;
949 unsigned long old
= ctype
->modifiers
;
950 unsigned long extra
= 0, dup
, conflict
;
952 if (mod
& old
& MOD_LONG
) {
953 extra
= MOD_LONGLONG
| MOD_LONG
;
957 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
959 sparse_error(pos
, "Just how %sdo you want this type to be?",
960 modifier_string(dup
));
962 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
964 sparse_error(pos
, "You cannot have both long and short modifiers.");
966 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
968 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
970 // Only one storage modifier allowed, except that "inline" doesn't count.
971 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
972 conflict
&= (conflict
- 1);
974 sparse_error(pos
, "multiple storage classes");
976 ctype
->modifiers
= old
| mod
| extra
;
980 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
981 (struct ptr_list
**)&ctype
->contexts
);
984 if (thistype
->alignment
& (thistype
->alignment
-1)) {
985 warning(pos
, "I don't like non-power-of-2 alignments");
986 thistype
->alignment
= 0;
988 if (thistype
->alignment
> ctype
->alignment
)
989 ctype
->alignment
= thistype
->alignment
;
993 ctype
->as
= thistype
->as
;
996 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
998 unsigned long banned
, wrong
;
999 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
1000 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
1002 if (s
->type
== SYM_KEYWORD
)
1003 banned
= s
->op
->type
== KW_SPECIFIER
? (BANNED_SIZE
| BANNED_SIGN
) : 0;
1004 else if (s
->ctype
.base_type
== &fp_type
)
1005 banned
= BANNED_SIGN
;
1006 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
1012 // vector_type <-- whatever that is
1013 banned
= BANNED_SIZE
| BANNED_SIGN
;
1016 wrong
= mod
& banned
;
1018 sparse_error(*pos
, "modifier %sis invalid in this context",
1019 modifier_string (wrong
));
1022 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
1024 struct token
*token
;
1026 while ( (token
= next
) != NULL
) {
1027 struct ctype thistype
;
1028 struct ident
*ident
;
1029 struct symbol
*s
, *type
;
1033 if (token_type(token
) != TOKEN_IDENT
)
1035 ident
= token
->ident
;
1037 s
= lookup_symbol(ident
, NS_TYPEDEF
);
1040 thistype
= s
->ctype
;
1041 mod
= thistype
.modifiers
;
1043 if (s
->type
!= SYM_KEYWORD
)
1045 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1048 if (s
->type
== SYM_KEYWORD
&& s
->op
->declarator
) {
1049 next
= s
->op
->declarator(next
, &thistype
);
1050 mod
= thistype
.modifiers
;
1052 type
= thistype
.base_type
;
1056 if (ctype
->base_type
)
1058 /* User types only mix with qualifiers */
1059 if (mod
& MOD_USERTYPE
) {
1060 if (ctype
->modifiers
& MOD_SPECIFIER
)
1063 ctype
->base_type
= type
;
1066 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
1067 apply_ctype(token
->pos
, &thistype
, ctype
);
1070 if (!ctype
->base_type
) {
1071 struct symbol
*base
= &incomplete_ctype
;
1074 * If we have modifiers, we'll default to an integer
1075 * type, and "ctype_integer()" will turn this into
1078 if (ctype
->modifiers
& MOD_SPECIFIER
)
1080 ctype
->base_type
= base
;
1085 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1087 struct expression
*expr
= NULL
;
1089 token
= parse_expression(token
, &expr
);
1090 sym
->array_size
= expr
;
1094 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
1095 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
1097 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
)
1099 struct symbol
*keyword
;
1101 struct ctype thistype
= { 0, };
1102 if (token_type(token
) != TOKEN_IDENT
)
1104 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1105 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1107 if (!(keyword
->op
->type
& (KW_ATTRIBUTE
| KW_ASM
)))
1109 token
= keyword
->op
->declarator(token
->next
, &thistype
);
1110 apply_ctype(token
->pos
, &thistype
, ctype
);
1115 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
1117 struct ctype
*ctype
= &decl
->ctype
;
1119 if (p
&& token_type(token
) == TOKEN_IDENT
) {
1121 token
= token
->next
;
1125 token
= handle_attributes(token
, ctype
);
1127 if (token_type(token
) != TOKEN_SPECIAL
)
1131 * This can be either a parameter list or a grouping.
1132 * For the direct (non-abstract) case, we know if must be
1133 * a parameter list if we already saw the identifier.
1134 * For the abstract case, we know if must be a parameter
1135 * list if it is empty or starts with a type.
1137 if (token
->special
== '(') {
1139 struct token
*next
= token
->next
;
1140 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
1143 struct symbol
*base_type
= ctype
->base_type
;
1144 token
= declarator(next
, decl
, p
);
1145 token
= expect(token
, ')', "in nested declarator");
1146 while (ctype
->base_type
!= base_type
)
1147 ctype
= &ctype
->base_type
->ctype
;
1152 sym
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1153 token
= parameter_type_list(next
, sym
, p
);
1154 token
= expect(token
, ')', "in function declarator");
1157 if (token
->special
== '[') {
1158 struct symbol
*array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1159 token
= abstract_array_declarator(token
->next
, array
);
1160 token
= expect(token
, ']', "in abstract_array_declarator");
1161 ctype
= &array
->ctype
;
1169 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
1171 unsigned long modifiers
;
1172 struct symbol
*base_type
;
1174 modifiers
= ctype
->modifiers
& ~MOD_TYPEDEF
;
1175 base_type
= ctype
->base_type
;
1176 ctype
->modifiers
= modifiers
;
1178 while (match_op(token
,'*')) {
1179 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1180 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
1181 ptr
->ctype
.as
= ctype
->as
;
1182 concat_ptr_list((struct ptr_list
*)ctype
->contexts
,
1183 (struct ptr_list
**)&ptr
->ctype
.contexts
);
1184 ptr
->ctype
.base_type
= base_type
;
1187 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
1188 ctype
->base_type
= base_type
;
1190 free_ptr_list(&ctype
->contexts
);
1192 token
= declaration_specifiers(token
->next
, ctype
, 1);
1193 modifiers
= ctype
->modifiers
;
1198 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
1200 token
= pointer(token
, &sym
->ctype
);
1201 return direct_declarator(token
, sym
, p
);
1204 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
1206 struct ctype
*ctype
= &decl
->ctype
;
1207 struct expression
*expr
;
1208 struct symbol
*bitfield
;
1211 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1212 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1213 show_typename(ctype
->base_type
));
1214 // Parse this to recover gracefully.
1215 return conditional_expression(token
->next
, &expr
);
1218 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1219 token
= conditional_expression(token
->next
, &expr
);
1220 width
= get_expression_value(expr
);
1221 bitfield
->bit_size
= width
;
1223 if (width
< 0 || width
> INT_MAX
) {
1224 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1226 } else if (decl
->ident
&& width
== 0) {
1227 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1228 show_ident(decl
->ident
));
1230 } else if (decl
->ident
) {
1231 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1232 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1233 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1234 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1235 // Valid values are either {-1;0} or {0}, depending on integer
1236 // representation. The latter makes for very efficient code...
1237 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1239 if (Wdefault_bitfield_sign
&&
1240 bitfield_type
->type
!= SYM_ENUM
&&
1241 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1243 // The sign of bitfields is unspecified by default.
1244 sparse_error(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1247 bitfield
->bit_size
= width
;
1251 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1253 struct ctype ctype
= {0, };
1255 token
= declaration_specifiers(token
, &ctype
, 0);
1257 struct ident
*ident
= NULL
;
1258 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1259 decl
->ctype
= ctype
;
1260 token
= declarator(token
, decl
, &ident
);
1261 decl
->ident
= ident
;
1262 if (match_op(token
, ':')) {
1263 token
= handle_bitfield(token
, decl
);
1264 token
= handle_attributes(token
, &decl
->ctype
);
1266 apply_modifiers(token
->pos
, &decl
->ctype
);
1267 add_symbol(list
, decl
);
1268 if (!match_op(token
, ','))
1270 token
= token
->next
;
1275 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1277 while (!match_op(token
, '}')) {
1278 if (!match_op(token
, ';'))
1279 token
= declaration_list(token
, list
);
1280 if (!match_op(token
, ';')) {
1281 sparse_error(token
->pos
, "expected ; at end of declaration");
1284 token
= token
->next
;
1289 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
1291 struct ident
*ident
= NULL
;
1293 struct ctype ctype
= { 0, };
1295 token
= declaration_specifiers(token
, &ctype
, 0);
1296 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1299 token
= declarator(token
, sym
, &ident
);
1301 apply_modifiers(token
->pos
, &sym
->ctype
);
1305 struct token
*typename(struct token
*token
, struct symbol
**p
)
1307 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1309 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
1310 token
= declarator(token
, sym
, NULL
);
1311 apply_modifiers(token
->pos
, &sym
->ctype
);
1315 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1317 token
= parse_expression(token
, tree
);
1318 return expect(token
, ';', "at end of statement");
1321 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1322 struct expression_list
**inout
)
1324 struct expression
*expr
;
1326 /* Allow empty operands */
1327 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1330 struct ident
*ident
= NULL
;
1331 if (match_op(token
->next
, '[') &&
1332 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1333 match_op(token
->next
->next
->next
, ']')) {
1334 ident
= token
->next
->next
->ident
;
1335 token
= token
->next
->next
->next
;
1337 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1338 token
= primary_expression(token
->next
, &expr
);
1339 add_expression(inout
, expr
);
1340 token
= parens_expression(token
, &expr
, "in asm parameter");
1341 add_expression(inout
, expr
);
1342 } while (match_op(token
, ','));
1346 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1347 struct expression_list
**clobbers
)
1349 struct expression
*expr
;
1352 token
= primary_expression(token
->next
, &expr
);
1353 add_expression(clobbers
, expr
);
1354 } while (match_op(token
, ','));
1358 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1360 token
= token
->next
;
1361 stmt
->type
= STMT_ASM
;
1362 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1363 token
= token
->next
;
1365 token
= expect(token
, '(', "after asm");
1366 token
= parse_expression(token
, &stmt
->asm_string
);
1367 if (match_op(token
, ':'))
1368 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1369 if (match_op(token
, ':'))
1370 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1371 if (match_op(token
, ':'))
1372 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1373 token
= expect(token
, ')', "after asm");
1374 return expect(token
, ';', "at end of asm-statement");
1377 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
)
1379 struct expression
*expr
;
1380 token
= expect(token
, '(', "after asm");
1381 token
= parse_expression(token
->next
, &expr
);
1382 token
= expect(token
, ')', "after asm");
1386 /* Make a statement out of an expression */
1387 static struct statement
*make_statement(struct expression
*expr
)
1389 struct statement
*stmt
;
1393 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1394 stmt
->expression
= expr
;
1399 * All iterators have two symbols associated with them:
1400 * the "continue" and "break" symbols, which are targets
1401 * for continue and break statements respectively.
1403 * They are in a special name-space, but they follow
1404 * all the normal visibility rules, so nested iterators
1405 * automatically work right.
1407 static void start_iterator(struct statement
*stmt
)
1409 struct symbol
*cont
, *brk
;
1411 start_symbol_scope();
1412 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1413 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1414 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1415 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1417 stmt
->type
= STMT_ITERATOR
;
1418 stmt
->iterator_break
= brk
;
1419 stmt
->iterator_continue
= cont
;
1420 fn_local_symbol(brk
);
1421 fn_local_symbol(cont
);
1424 static void end_iterator(struct statement
*stmt
)
1429 static struct statement
*start_function(struct symbol
*sym
)
1432 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1434 start_function_scope();
1435 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1436 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1437 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1438 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1439 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1441 fn_local_symbol(ret
);
1443 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1449 static void end_function(struct symbol
*sym
)
1452 end_function_scope();
1456 * A "switch()" statement, like an iterator, has a
1457 * the "break" symbol associated with it. It works
1458 * exactly like the iterator break - it's the target
1459 * for any break-statements in scope, and means that
1460 * "break" handling doesn't even need to know whether
1461 * it's breaking out of an iterator or a switch.
1463 * In addition, the "case" symbol is a marker for the
1464 * case/default statements to find the switch statement
1465 * that they are associated with.
1467 static void start_switch(struct statement
*stmt
)
1469 struct symbol
*brk
, *switch_case
;
1471 start_symbol_scope();
1472 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1473 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1475 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1476 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1477 switch_case
->stmt
= stmt
;
1479 stmt
->type
= STMT_SWITCH
;
1480 stmt
->switch_break
= brk
;
1481 stmt
->switch_case
= switch_case
;
1483 fn_local_symbol(brk
);
1484 fn_local_symbol(switch_case
);
1487 static void end_switch(struct statement
*stmt
)
1489 if (!stmt
->switch_case
->symbol_list
)
1490 warning(stmt
->pos
, "switch with no cases");
1494 static void add_case_statement(struct statement
*stmt
)
1496 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1500 sparse_error(stmt
->pos
, "not in switch scope");
1501 stmt
->type
= STMT_NONE
;
1504 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1505 add_symbol(&target
->symbol_list
, sym
);
1507 stmt
->case_label
= sym
;
1508 fn_local_symbol(sym
);
1511 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1513 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1516 error_die(token
->pos
, "internal error: return without a function target");
1517 stmt
->type
= STMT_RETURN
;
1518 stmt
->ret_target
= target
;
1519 return expression_statement(token
->next
, &stmt
->ret_value
);
1522 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1524 struct symbol_list
*syms
;
1525 struct expression
*e1
, *e2
, *e3
;
1526 struct statement
*iterator
;
1528 start_iterator(stmt
);
1529 token
= expect(token
->next
, '(', "after 'for'");
1533 /* C99 variable declaration? */
1534 if (lookup_type(token
)) {
1535 token
= external_declaration(token
, &syms
);
1537 token
= parse_expression(token
, &e1
);
1538 token
= expect(token
, ';', "in 'for'");
1540 token
= parse_expression(token
, &e2
);
1541 token
= expect(token
, ';', "in 'for'");
1542 token
= parse_expression(token
, &e3
);
1543 token
= expect(token
, ')', "in 'for'");
1544 token
= statement(token
, &iterator
);
1546 stmt
->iterator_syms
= syms
;
1547 stmt
->iterator_pre_statement
= make_statement(e1
);
1548 stmt
->iterator_pre_condition
= e2
;
1549 stmt
->iterator_post_statement
= make_statement(e3
);
1550 stmt
->iterator_post_condition
= NULL
;
1551 stmt
->iterator_statement
= iterator
;
1557 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1559 struct expression
*expr
;
1560 struct statement
*iterator
;
1562 start_iterator(stmt
);
1563 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1564 token
= statement(token
, &iterator
);
1566 stmt
->iterator_pre_condition
= expr
;
1567 stmt
->iterator_post_condition
= NULL
;
1568 stmt
->iterator_statement
= iterator
;
1574 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1576 struct expression
*expr
;
1577 struct statement
*iterator
;
1579 start_iterator(stmt
);
1580 token
= statement(token
->next
, &iterator
);
1581 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1582 token
= token
->next
;
1584 sparse_error(token
->pos
, "expected 'while' after 'do'");
1585 token
= parens_expression(token
, &expr
, "after 'do-while'");
1587 stmt
->iterator_post_condition
= expr
;
1588 stmt
->iterator_statement
= iterator
;
1591 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
1592 warning(iterator
->pos
, "do-while statement is not a compound statement");
1594 return expect(token
, ';', "after statement");
1597 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
1599 stmt
->type
= STMT_IF
;
1600 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1601 token
= statement(token
, &stmt
->if_true
);
1602 if (token_type(token
) != TOKEN_IDENT
)
1604 if (token
->ident
!= &else_ident
)
1606 return statement(token
->next
, &stmt
->if_false
);
1609 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
1611 stmt
->type
= STMT_CASE
;
1612 token
= expect(token
, ':', "after default/case");
1613 add_case_statement(stmt
);
1614 return statement(token
, &stmt
->case_statement
);
1617 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
1619 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1620 if (match_op(token
, SPECIAL_ELLIPSIS
))
1621 token
= parse_expression(token
->next
, &stmt
->case_to
);
1622 return case_statement(token
, stmt
);
1625 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
1627 return case_statement(token
->next
, stmt
);
1630 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
1632 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1633 stmt
->type
= STMT_GOTO
;
1634 stmt
->goto_label
= target
;
1636 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1637 return expect(token
->next
, ';', "at end of statement");
1640 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
1642 stmt
->type
= STMT_SWITCH
;
1644 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1645 token
= statement(token
, &stmt
->switch_statement
);
1650 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
1652 stmt
->type
= STMT_GOTO
;
1653 token
= token
->next
;
1654 if (match_op(token
, '*')) {
1655 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1656 add_statement(&function_computed_goto_list
, stmt
);
1657 } else if (token_type(token
) == TOKEN_IDENT
) {
1658 stmt
->goto_label
= label_symbol(token
);
1659 token
= token
->next
;
1661 sparse_error(token
->pos
, "Expected identifier or goto expression");
1663 return expect(token
, ';', "at end of statement");
1666 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
1668 stmt
->type
= STMT_CONTEXT
;
1669 token
= parse_expression(token
->next
, &stmt
->expression
);
1670 if(stmt
->expression
->type
== EXPR_PREOP
1671 && stmt
->expression
->op
== '('
1672 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
1673 struct expression
*expr
;
1674 expr
= stmt
->expression
->unop
;
1675 stmt
->context
= expr
->left
;
1676 stmt
->expression
= expr
->right
;
1678 return expect(token
, ';', "at end of statement");
1681 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
1683 stmt
->type
= STMT_RANGE
;
1684 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1685 token
= expect(token
, ',', "after range expression");
1686 token
= assignment_expression(token
, &stmt
->range_low
);
1687 token
= expect(token
, ',', "after low range");
1688 token
= assignment_expression(token
, &stmt
->range_high
);
1689 return expect(token
, ';', "after range statement");
1692 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1694 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1697 if (token_type(token
) == TOKEN_IDENT
) {
1698 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1699 if (s
&& s
->op
->statement
)
1700 return s
->op
->statement(token
, stmt
);
1702 if (match_op(token
->next
, ':')) {
1703 stmt
->type
= STMT_LABEL
;
1704 stmt
->label_identifier
= label_symbol(token
);
1705 return statement(token
->next
->next
, &stmt
->label_statement
);
1709 if (match_op(token
, '{')) {
1710 stmt
->type
= STMT_COMPOUND
;
1711 start_symbol_scope();
1712 token
= compound_statement(token
->next
, stmt
);
1715 return expect(token
, '}', "at end of compound statement");
1718 stmt
->type
= STMT_EXPRESSION
;
1719 return expression_statement(token
, &stmt
->expression
);
1722 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1724 int seen_statement
= 0;
1726 struct statement
* stmt
;
1727 if (eof_token(token
))
1729 if (match_op(token
, '}'))
1731 if (lookup_type(token
)) {
1732 if (seen_statement
) {
1733 warning(token
->pos
, "mixing declarations and code");
1736 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1737 token
= external_declaration(token
, &stmt
->declaration
);
1739 seen_statement
= warn_on_mixed
;
1740 token
= statement(token
, &stmt
);
1742 add_statement(list
, stmt
);
1747 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1749 struct symbol_list
**list
= &fn
->arguments
;
1751 if (match_op(token
, ')')) {
1752 // No warning for "void oink ();"
1753 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1754 if (p
&& !match_op(token
->next
, ';'))
1755 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1762 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1764 warning(token
->pos
, "variadic functions must have one named argument");
1766 token
= token
->next
;
1770 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1771 token
= parameter_declaration(token
, &sym
);
1772 if (sym
->ctype
.base_type
== &void_ctype
) {
1773 /* Special case: (void) */
1774 if (!*list
&& !sym
->ident
)
1776 warning(token
->pos
, "void parameter");
1778 add_symbol(list
, sym
);
1779 if (!match_op(token
, ','))
1781 token
= token
->next
;
1787 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1789 token
= statement_list(token
, &stmt
->stmts
);
1793 static struct expression
*identifier_expression(struct token
*token
)
1795 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1796 expr
->expr_ident
= token
->ident
;
1800 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1802 int idx_from
, idx_to
;
1803 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1805 idx_from
= get_expression_value(from
);
1808 idx_to
= get_expression_value(to
);
1809 if (idx_to
< idx_from
|| idx_from
< 0)
1810 warning(from
->pos
, "nonsense array initializer index range");
1812 expr
->idx_from
= idx_from
;
1813 expr
->idx_to
= idx_to
;
1817 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1819 int expect_equal
= 0;
1820 struct token
*next
= token
->next
;
1821 struct expression
**tail
= ep
;
1826 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1827 struct expression
*expr
= identifier_expression(token
);
1828 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1829 token
= initializer(&expr
->ident_expression
, next
->next
);
1830 if (expr
->ident_expression
)
1835 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1836 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1837 struct expression
*expr
= identifier_expression(next
);
1839 tail
= &expr
->ident_expression
;
1842 } else if (match_op(token
, '[')) {
1843 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1844 token
= constant_expression(token
->next
, &from
);
1846 sparse_error(token
->pos
, "Expected constant expression");
1849 if (match_op(token
, SPECIAL_ELLIPSIS
))
1850 token
= constant_expression(token
->next
, &to
);
1851 expr
= index_expression(from
, to
);
1853 tail
= &expr
->idx_expression
;
1854 token
= expect(token
, ']', "at end of initializer index");
1861 if (nested
&& !expect_equal
) {
1862 if (!match_op(token
, '='))
1863 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1868 token
= expect(token
, '=', "at end of initializer index");
1870 token
= initializer(tail
, token
);
1876 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1878 struct expression
*expr
;
1881 token
= single_initializer(&expr
, token
);
1884 add_expression(list
, expr
);
1885 if (!match_op(token
, ','))
1887 token
= token
->next
;
1892 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1894 if (match_op(token
, '{')) {
1895 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1897 token
= initializer_list(&expr
->expr_list
, token
->next
);
1898 return expect(token
, '}', "at end of initializer");
1900 return assignment_expression(token
, tree
);
1903 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1906 sparse_error(sym
->pos
, "no identifier for function argument");
1909 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1912 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1913 struct symbol_list
**list
)
1915 struct symbol_list
**old_symbol_list
;
1916 struct symbol
*base_type
= decl
->ctype
.base_type
;
1917 struct statement
*stmt
, **p
;
1920 old_symbol_list
= function_symbol_list
;
1921 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1922 function_symbol_list
= &decl
->inline_symbol_list
;
1923 p
= &base_type
->inline_stmt
;
1925 function_symbol_list
= &decl
->symbol_list
;
1926 p
= &base_type
->stmt
;
1928 function_computed_target_list
= NULL
;
1929 function_computed_goto_list
= NULL
;
1931 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
1932 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1933 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
1935 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
1936 decl
->ctype
.modifiers
|= MOD_EXTERN
;
1938 stmt
= start_function(decl
);
1941 FOR_EACH_PTR (base_type
->arguments
, arg
) {
1942 declare_argument(arg
, base_type
);
1943 } END_FOR_EACH_PTR(arg
);
1945 token
= compound_statement(token
->next
, stmt
);
1948 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
1949 add_symbol(list
, decl
);
1950 check_declaration(decl
);
1951 function_symbol_list
= old_symbol_list
;
1952 if (function_computed_goto_list
) {
1953 if (!function_computed_target_list
)
1954 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
1956 struct statement
*stmt
;
1957 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
1958 stmt
->target_list
= function_computed_target_list
;
1959 } END_FOR_EACH_PTR(stmt
);
1962 return expect(token
, '}', "at end of function");
1965 static void promote_k_r_types(struct symbol
*arg
)
1967 struct symbol
*base
= arg
->ctype
.base_type
;
1968 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
1969 arg
->ctype
.base_type
= &int_ctype
;
1973 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
1975 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
1978 FOR_EACH_PTR(real_args
, arg
) {
1979 struct symbol
*type
;
1981 /* This is quadratic in the number of arguments. We _really_ don't care */
1982 FOR_EACH_PTR(argtypes
, type
) {
1983 if (type
->ident
== arg
->ident
)
1985 } END_FOR_EACH_PTR(type
);
1986 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
1990 /* "char" and "short" promote to "int" */
1991 promote_k_r_types(type
);
1993 arg
->ctype
= type
->ctype
;
1994 } END_FOR_EACH_PTR(arg
);
1996 FOR_EACH_PTR(argtypes
, arg
) {
1998 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
1999 } END_FOR_EACH_PTR(arg
);
2003 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2004 struct symbol_list
**list
)
2006 struct symbol_list
*args
= NULL
;
2008 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2010 token
= declaration_list(token
, &args
);
2011 if (!match_op(token
, ';')) {
2012 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2015 token
= token
->next
;
2016 } while (lookup_type(token
));
2018 apply_k_r_types(args
, decl
);
2020 if (!match_op(token
, '{')) {
2021 sparse_error(token
->pos
, "expected function body");
2024 return parse_function_body(token
, decl
, list
);
2027 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2029 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2030 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2031 struct statement
*stmt
;
2033 anon
->ctype
.base_type
= fn
;
2034 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2037 token
= parse_asm_statement(token
, stmt
);
2039 add_symbol(list
, anon
);
2043 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2045 struct ident
*ident
= NULL
;
2046 struct symbol
*decl
;
2047 struct ctype ctype
= { 0, };
2048 struct symbol
*base_type
;
2051 /* Top-level inline asm? */
2052 if (token_type(token
) == TOKEN_IDENT
) {
2053 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2054 if (s
&& s
->op
->toplevel
)
2055 return s
->op
->toplevel(token
, list
);
2058 /* Parse declaration-specifiers, if any */
2059 token
= declaration_specifiers(token
, &ctype
, 0);
2060 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2061 decl
->ctype
= ctype
;
2062 token
= declarator(token
, decl
, &ident
);
2063 apply_modifiers(token
->pos
, &decl
->ctype
);
2065 /* Just a type declaration? */
2067 return expect(token
, ';', "end of type declaration");
2069 /* type define declaration? */
2070 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
2072 /* Typedefs don't have meaningful storage */
2074 ctype
.modifiers
&= ~MOD_STORAGE
;
2075 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
2076 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2079 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2081 base_type
= decl
->ctype
.base_type
;
2084 if (base_type
&& !base_type
->ident
)
2085 base_type
->ident
= ident
;
2086 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2087 /* K&R argument declaration? */
2088 if (lookup_type(token
))
2089 return parse_k_r_arguments(token
, decl
, list
);
2090 if (match_op(token
, '{'))
2091 return parse_function_body(token
, decl
, list
);
2093 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2094 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2095 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2096 sparse_error(token
->pos
, "void declaration");
2100 if (!is_typedef
&& match_op(token
, '=')) {
2101 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2102 warning(decl
->pos
, "symbol with external linkage has initializer");
2103 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2105 token
= initializer(&decl
->initializer
, token
->next
);
2108 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2109 add_symbol(list
, decl
);
2110 fn_local_symbol(decl
);
2113 check_declaration(decl
);
2115 if (!match_op(token
, ','))
2118 token
= token
->next
;
2120 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2121 decl
->ctype
= ctype
;
2122 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
2123 token
= declarator(token
, decl
, &ident
);
2124 apply_modifiers(token
->pos
, &decl
->ctype
);
2126 sparse_error(token
->pos
, "expected identifier name in type definition");
2130 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2132 /* Function declarations are automatically extern unless specifically static */
2133 base_type
= decl
->ctype
.base_type
;
2134 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2135 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2136 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2139 return expect(token
, ';', "at end of declaration");