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 static struct symbol_list
**function_symbol_list
;
32 struct symbol_list
*function_computed_target_list
;
33 struct statement_list
*function_computed_goto_list
;
35 static struct token
*statement(struct token
*token
, struct statement
**tree
);
36 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
, unsigned int keywords
);
38 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
);
39 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
);
40 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
);
41 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
);
42 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
);
44 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
45 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
46 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
47 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
48 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
49 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
50 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
51 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
52 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
53 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
54 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
55 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
56 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
57 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
58 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
);
61 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
62 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
63 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
64 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
65 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
66 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
67 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
68 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
71 static struct symbol_op modifier_op
= {
75 static struct symbol_op qualifier_op
= {
79 static struct symbol_op typeof_op
= {
81 .declarator
= typeof_specifier
,
84 static struct symbol_op attribute_op
= {
86 .declarator
= attribute_specifier
,
89 static struct symbol_op struct_op
= {
91 .declarator
= struct_specifier
,
94 static struct symbol_op union_op
= {
96 .declarator
= union_specifier
,
99 static struct symbol_op enum_op
= {
100 .type
= KW_SPECIFIER
,
101 .declarator
= enum_specifier
,
106 static struct symbol_op if_op
= {
107 .statement
= parse_if_statement
,
110 static struct symbol_op return_op
= {
111 .statement
= parse_return_statement
,
114 static struct symbol_op loop_iter_op
= {
115 .statement
= parse_loop_iterator
,
118 static struct symbol_op default_op
= {
119 .statement
= parse_default_statement
,
122 static struct symbol_op case_op
= {
123 .statement
= parse_case_statement
,
126 static struct symbol_op switch_op
= {
127 .statement
= parse_switch_statement
,
130 static struct symbol_op for_op
= {
131 .statement
= parse_for_statement
,
134 static struct symbol_op while_op
= {
135 .statement
= parse_while_statement
,
138 static struct symbol_op do_op
= {
139 .statement
= parse_do_statement
,
142 static struct symbol_op goto_op
= {
143 .statement
= parse_goto_statement
,
146 static struct symbol_op __context___op
= {
147 .statement
= parse_context_statement
,
150 static struct symbol_op range_op
= {
151 .statement
= parse_range_statement
,
154 static struct symbol_op asm_op
= {
156 .declarator
= parse_asm_declarator
,
157 .statement
= parse_asm_statement
,
158 .toplevel
= toplevel_asm_declaration
,
161 static struct symbol_op packed_op
= {
162 .attribute
= attribute_packed
,
165 static struct symbol_op aligned_op
= {
166 .attribute
= attribute_aligned
,
169 static struct symbol_op attr_mod_op
= {
170 .attribute
= attribute_modifier
,
173 static struct symbol_op address_space_op
= {
174 .attribute
= attribute_address_space
,
177 static struct symbol_op mode_op
= {
178 .attribute
= attribute_mode
,
181 static struct symbol_op context_op
= {
182 .attribute
= attribute_context
,
185 static struct symbol_op transparent_union_op
= {
186 .attribute
= attribute_transparent_union
,
189 static struct symbol_op ignore_attr_op
= {
190 .attribute
= ignore_attribute
,
193 static struct symbol_op mode_spec_op
= {
197 static struct init_keyword
{
200 unsigned long modifiers
;
201 struct symbol_op
*op
;
202 } keyword_table
[] = {
203 /* Type qualifiers */
204 { "const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
205 { "__const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
206 { "__const__", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
207 { "volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
208 { "__volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
209 { "__volatile__", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
212 { "typedef", NS_TYPEDEF
, MOD_TYPEDEF
, .op
= &modifier_op
},
215 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
216 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
217 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
219 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
220 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
222 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
223 { "union", NS_TYPEDEF
, .op
= &union_op
},
224 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
226 { "inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
227 { "__inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
228 { "__inline__", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
230 /* Ignored for now.. */
231 { "restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
232 { "__restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
235 { "if", NS_KEYWORD
, .op
= &if_op
},
236 { "return", NS_KEYWORD
, .op
= &return_op
},
237 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
238 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
239 { "default", NS_KEYWORD
, .op
= &default_op
},
240 { "case", NS_KEYWORD
, .op
= &case_op
},
241 { "switch", NS_KEYWORD
, .op
= &switch_op
},
242 { "for", NS_KEYWORD
, .op
= &for_op
},
243 { "while", NS_KEYWORD
, .op
= &while_op
},
244 { "do", NS_KEYWORD
, .op
= &do_op
},
245 { "goto", NS_KEYWORD
, .op
= &goto_op
},
246 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
247 { "__range__", NS_KEYWORD
, .op
= &range_op
},
248 { "asm", NS_KEYWORD
, .op
= &asm_op
},
249 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
250 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
253 { "packed", NS_KEYWORD
, .op
= &packed_op
},
254 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
255 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
256 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
257 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
258 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
259 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
260 { "force", NS_KEYWORD
, MOD_FORCE
, .op
= &attr_mod_op
},
261 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
262 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
263 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
264 { "mode", NS_KEYWORD
, .op
= &mode_op
},
265 { "context", NS_KEYWORD
, .op
= &context_op
},
266 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
268 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
269 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_spec_op
},
270 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_spec_op
},
271 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_spec_op
},
272 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_spec_op
},
273 { "SI", NS_KEYWORD
, .op
= &mode_spec_op
},
274 { "__SI__", NS_KEYWORD
, .op
= &mode_spec_op
},
275 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_spec_op
},
276 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_spec_op
},
277 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_spec_op
},
278 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_spec_op
},
280 /* Ignored attributes */
281 { "nothrow", NS_KEYWORD
, .op
= &ignore_attr_op
},
282 { "__nothrow", NS_KEYWORD
, .op
= &ignore_attr_op
},
283 { "__nothrow__", NS_KEYWORD
, .op
= &ignore_attr_op
},
284 { "malloc", NS_KEYWORD
, .op
= &ignore_attr_op
},
285 { "__malloc__", NS_KEYWORD
, .op
= &ignore_attr_op
},
286 { "nonnull", NS_KEYWORD
, .op
= &ignore_attr_op
},
287 { "__nonnull", NS_KEYWORD
, .op
= &ignore_attr_op
},
288 { "__nonnull__", NS_KEYWORD
, .op
= &ignore_attr_op
},
289 { "format", NS_KEYWORD
, .op
= &ignore_attr_op
},
290 { "__format__", NS_KEYWORD
, .op
= &ignore_attr_op
},
291 { "format_arg", 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 { "__always_inline__", NS_KEYWORD
, .op
= &ignore_attr_op
},
316 { "syscall_linkage", NS_KEYWORD
, .op
= &ignore_attr_op
},
317 { "__syscall_linkage__", NS_KEYWORD
, .op
= &ignore_attr_op
},
318 { "visibility", NS_KEYWORD
, .op
= &ignore_attr_op
},
319 { "__visibility__", NS_KEYWORD
, .op
= &ignore_attr_op
},
320 { "deprecated", NS_KEYWORD
, .op
= &ignore_attr_op
},
321 { "__deprecated__", NS_KEYWORD
, .op
= &ignore_attr_op
},
322 { "noinline", NS_KEYWORD
, .op
= &ignore_attr_op
},
323 { "__noinline__", NS_KEYWORD
, .op
= &ignore_attr_op
},
324 { "used", NS_KEYWORD
, .op
= &ignore_attr_op
},
325 { "__used__", NS_KEYWORD
, .op
= &ignore_attr_op
},
326 { "warn_unused_result", NS_KEYWORD
, .op
= &ignore_attr_op
},
327 { "__warn_unused_result__", NS_KEYWORD
, .op
= &ignore_attr_op
},
328 { "model", NS_KEYWORD
, .op
= &ignore_attr_op
},
329 { "__model__", NS_KEYWORD
, .op
= &ignore_attr_op
},
330 { "cdecl", NS_KEYWORD
, .op
= &ignore_attr_op
},
331 { "__cdecl__", NS_KEYWORD
, .op
= &ignore_attr_op
},
332 { "stdcall", NS_KEYWORD
, .op
= &ignore_attr_op
},
333 { "__stdcall__", NS_KEYWORD
, .op
= &ignore_attr_op
},
334 { "fastcall", NS_KEYWORD
, .op
= &ignore_attr_op
},
335 { "__fastcall__", NS_KEYWORD
, .op
= &ignore_attr_op
},
336 { "dllimport", NS_KEYWORD
, .op
= &ignore_attr_op
},
337 { "__dllimport__", NS_KEYWORD
, .op
= &ignore_attr_op
},
338 { "dllexport", NS_KEYWORD
, .op
= &ignore_attr_op
},
339 { "__dllexport__", NS_KEYWORD
, .op
= &ignore_attr_op
},
340 { "constructor", NS_KEYWORD
, .op
= &ignore_attr_op
},
341 { "__constructor__", NS_KEYWORD
, .op
= &ignore_attr_op
},
342 { "destructor", NS_KEYWORD
, .op
= &ignore_attr_op
},
343 { "__destructor__", NS_KEYWORD
, .op
= &ignore_attr_op
},
344 { "cold", NS_KEYWORD
, .op
= &ignore_attr_op
},
345 { "__cold__", NS_KEYWORD
, .op
= &ignore_attr_op
},
346 { "hot", NS_KEYWORD
, .op
= &ignore_attr_op
},
347 { "__hot__", NS_KEYWORD
, .op
= &ignore_attr_op
},
350 void init_parser(int stream
)
353 for (i
= 0; i
< sizeof keyword_table
/sizeof keyword_table
[0]; i
++) {
354 struct init_keyword
*ptr
= keyword_table
+ i
;
355 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
356 sym
->ident
->keyword
= 1;
357 sym
->ctype
.modifiers
= ptr
->modifiers
;
362 // Add a symbol to the list of function-local symbols
363 static void fn_local_symbol(struct symbol
*sym
)
365 if (function_symbol_list
)
366 add_symbol(function_symbol_list
, sym
);
369 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
374 if (token_type(token
) != TOKEN_IDENT
)
377 va_start(args
, token
);
379 next
= va_arg(args
, struct ident
*);
380 } while (next
&& token
->ident
!= next
);
383 return next
&& token
->ident
== next
;
387 struct statement
*alloc_statement(struct position pos
, int type
)
389 struct statement
*stmt
= __alloc_statement(0);
395 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
397 static int apply_modifiers(struct position pos
, struct ctype
*ctype
)
401 while ((base
= ctype
->base_type
)) {
402 switch (base
->type
) {
408 ctype
= &base
->ctype
;
414 /* Turn the "virtual types" into real types with real sizes etc */
415 if (ctype
->base_type
== &int_type
) {
416 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
417 ctype
->modifiers
&= ~MOD_SPECIFIER
;
418 } else if (ctype
->base_type
== &fp_type
) {
419 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
420 ctype
->modifiers
&= ~MOD_SPECIFIER
;
423 if (ctype
->modifiers
& MOD_BITWISE
) {
425 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
426 if (!is_int_type(ctype
->base_type
)) {
427 sparse_error(pos
, "invalid modifier");
430 type
= alloc_symbol(pos
, SYM_BASETYPE
);
431 *type
= *ctype
->base_type
;
432 type
->ctype
.base_type
= ctype
->base_type
;
433 type
->type
= SYM_RESTRICT
;
434 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
435 ctype
->base_type
= type
;
441 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
443 struct symbol
*sym
= alloc_symbol(pos
, type
);
445 sym
->ctype
.base_type
= ctype
->base_type
;
446 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
448 ctype
->base_type
= sym
;
449 ctype
->modifiers
&= MOD_STORAGE
;
453 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
455 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
457 sym
= alloc_symbol(token
->pos
, type
);
458 bind_symbol(sym
, token
->ident
, ns
);
459 if (type
== SYM_LABEL
)
460 fn_local_symbol(sym
);
465 static struct symbol
* local_label(struct token
*token
)
467 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
);
469 if (sym
&& sym
->ctype
.modifiers
& MOD_LABEL
)
476 * NOTE! NS_LABEL is not just a different namespace,
477 * it also ends up using function scope instead of the
478 * regular symbol scope.
480 struct symbol
*label_symbol(struct token
*token
)
482 struct symbol
*sym
= local_label(token
);
485 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
488 static struct token
*struct_union_enum_specifier(enum type type
,
489 struct token
*token
, struct ctype
*ctype
,
490 struct token
*(*parse
)(struct token
*, struct symbol
*))
493 struct position
*repos
;
495 ctype
->modifiers
= 0;
496 token
= handle_attributes(token
, ctype
, KW_ATTRIBUTE
| KW_ASM
);
497 if (token_type(token
) == TOKEN_IDENT
) {
498 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
500 (is_outer_scope(sym
->scope
) &&
501 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
502 // Either a new symbol, or else an out-of-scope
503 // symbol being redefined.
504 sym
= alloc_symbol(token
->pos
, type
);
505 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
507 if (sym
->type
!= type
)
508 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
509 ctype
->base_type
= sym
;
512 if (match_op(token
, '{')) {
513 // The following test is actually wrong for empty
514 // structs, but (1) they are not C99, (2) gcc does
515 // the same thing, and (3) it's easier.
516 if (sym
->symbol_list
)
517 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
519 token
= parse(token
->next
, sym
);
520 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
522 // Mark the structure as needing re-examination
524 sym
->endpos
= token
->pos
;
529 // private struct/union/enum type
530 if (!match_op(token
, '{')) {
531 sparse_error(token
->pos
, "expected declaration");
532 ctype
->base_type
= &bad_ctype
;
536 sym
= alloc_symbol(token
->pos
, type
);
537 token
= parse(token
->next
, sym
);
538 ctype
->base_type
= sym
;
539 token
= expect(token
, '}', "at end of specifier");
540 sym
->endpos
= token
->pos
;
545 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
547 struct symbol
*field
, *last
= NULL
;
549 res
= struct_declaration_list(token
, &sym
->symbol_list
);
550 FOR_EACH_PTR(sym
->symbol_list
, field
) {
552 struct symbol
*base
= field
->ctype
.base_type
;
553 if (base
&& base
->type
== SYM_BITFIELD
)
557 last
->next_subobject
= field
;
559 } END_FOR_EACH_PTR(field
);
563 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
565 return struct_declaration_list(token
, &sym
->symbol_list
);
568 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
)
570 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctype
, parse_struct_declaration
);
573 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
)
575 return struct_union_enum_specifier(SYM_UNION
, token
, ctype
, parse_union_declaration
);
581 unsigned long long y
;
584 static void upper_boundary(Num
*n
, Num
*v
)
596 static void lower_boundary(Num
*n
, Num
*v
)
608 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
610 int shift
= type
->bit_size
;
611 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
615 if (upper
->x
== 0 && upper
->y
>> shift
)
617 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
622 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
624 if (s1
->bit_size
< s2
->bit_size
) {
626 } else if (s1
->bit_size
== s2
->bit_size
) {
627 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
630 if (s1
->bit_size
< bits_in_int
)
635 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
639 FOR_EACH_PTR(list
, sym
) {
640 struct expression
*expr
= sym
->initializer
;
641 struct symbol
*ctype
;
642 if (expr
->type
!= EXPR_VALUE
)
645 if (ctype
->bit_size
== base_type
->bit_size
)
647 cast_value(expr
, base_type
, expr
, ctype
);
648 } END_FOR_EACH_PTR(sym
);
651 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
653 unsigned long long lastval
= 0;
654 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
655 Num upper
= {-1, 0}, lower
= {1, 0};
657 parent
->examined
= 1;
658 parent
->ctype
.base_type
= &int_ctype
;
659 while (token_type(token
) == TOKEN_IDENT
) {
660 struct expression
*expr
= NULL
;
661 struct token
*next
= token
->next
;
664 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
665 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
666 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
668 if (match_op(next
, '=')) {
669 next
= constant_expression(next
->next
, &expr
);
670 lastval
= get_expression_value(expr
);
672 if (expr
&& expr
->ctype
)
676 } else if (is_int_type(ctype
)) {
679 error_die(token
->pos
, "can't increment the last enum member");
683 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
684 expr
->value
= lastval
;
688 sym
->initializer
= expr
;
689 sym
->enum_member
= 1;
690 sym
->ctype
.base_type
= parent
;
691 add_ptr_list(&parent
->symbol_list
, sym
);
693 if (base_type
!= &bad_ctype
) {
694 if (ctype
->type
== SYM_NODE
)
695 ctype
= ctype
->ctype
.base_type
;
696 if (ctype
->type
== SYM_ENUM
) {
700 ctype
= ctype
->ctype
.base_type
;
704 * - if all enums are of the same type, then
705 * the base_type is that type (two first
707 * - if enums are of different types, they
708 * all have to be integer types, and the
709 * base type is at least "int_ctype".
710 * - otherwise the base_type is "bad_ctype".
714 } else if (ctype
== base_type
) {
716 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
717 base_type
= bigger_enum_type(base_type
, ctype
);
719 base_type
= &bad_ctype
;
720 parent
->ctype
.base_type
= base_type
;
722 if (is_int_type(base_type
)) {
723 Num v
= {.y
= lastval
};
724 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
726 else if ((long long)lastval
>= 0)
730 upper_boundary(&upper
, &v
);
731 lower_boundary(&lower
, &v
);
735 sym
->endpos
= token
->pos
;
737 if (!match_op(token
, ','))
742 sparse_error(token
->pos
, "bad enum definition");
743 base_type
= &bad_ctype
;
745 else if (!is_int_type(base_type
))
746 base_type
= base_type
;
747 else if (type_is_ok(base_type
, &upper
, &lower
))
748 base_type
= base_type
;
749 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
750 base_type
= &int_ctype
;
751 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
752 base_type
= &uint_ctype
;
753 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
754 base_type
= &long_ctype
;
755 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
756 base_type
= &ulong_ctype
;
757 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
758 base_type
= &llong_ctype
;
759 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
760 base_type
= &ullong_ctype
;
762 base_type
= &bad_ctype
;
763 parent
->ctype
.base_type
= base_type
;
764 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
765 parent
->examined
= 0;
767 cast_enum_list(parent
->symbol_list
, base_type
);
772 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
774 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
776 ctype
= &ctype
->base_type
->ctype
;
777 if (!ctype
->base_type
)
778 ctype
->base_type
= &incomplete_ctype
;
783 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
787 if (!match_op(token
, '(')) {
788 sparse_error(token
->pos
, "expected '(' after typeof");
791 if (lookup_type(token
->next
)) {
792 token
= typename(token
->next
, &sym
, 0);
795 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
796 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
798 ctype
->modifiers
= 0;
799 typeof_sym
->endpos
= token
->pos
;
800 ctype
->base_type
= typeof_sym
;
802 return expect(token
, ')', "after typeof");
805 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
807 struct expression
*expr
= NULL
;
808 if (match_op(token
, '('))
809 token
= parens_expression(token
, &expr
, "in attribute");
813 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
815 ctype
->alignment
= 1;
819 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
821 int alignment
= max_alignment
;
822 struct expression
*expr
= NULL
;
824 if (match_op(token
, '(')) {
825 token
= parens_expression(token
, &expr
, "in attribute");
827 alignment
= const_expression_value(expr
);
829 ctype
->alignment
= alignment
;
833 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
835 ctype
->modifiers
|= attr
->ctype
.modifiers
;
839 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
841 struct expression
*expr
= NULL
;
842 token
= expect(token
, '(', "after address_space attribute");
843 token
= conditional_expression(token
, &expr
);
845 ctype
->as
= const_expression_value(expr
);
846 token
= expect(token
, ')', "after address_space attribute");
850 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
852 token
= expect(token
, '(', "after mode attribute");
853 if (token_type(token
) == TOKEN_IDENT
) {
854 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
855 if (mode
&& mode
->op
->type
== KW_MODE
)
856 ctype
->modifiers
|= mode
->ctype
.modifiers
;
858 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
861 sparse_error(token
->pos
, "expect attribute mode symbol\n");
862 token
= expect(token
, ')', "after mode attribute");
866 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
868 struct context
*context
= alloc_context();
869 struct expression
*args
[3];
872 token
= expect(token
, '(', "after context attribute");
873 while (!match_op(token
, ')')) {
874 struct expression
*expr
= NULL
;
875 token
= conditional_expression(token
, &expr
);
880 if (!match_op(token
, ','))
887 sparse_error(token
->pos
, "expected context input/output values");
890 context
->in
= get_expression_value(args
[0]);
893 context
->in
= get_expression_value(args
[0]);
894 context
->out
= get_expression_value(args
[1]);
897 context
->context
= args
[0];
898 context
->in
= get_expression_value(args
[1]);
899 context
->out
= get_expression_value(args
[2]);
904 add_ptr_list(&ctype
->contexts
, context
);
906 token
= expect(token
, ')', "after context attribute");
910 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
912 if (Wtransparent_union
)
913 warning(token
->pos
, "ignoring attribute __transparent_union__");
917 static struct token
*recover_unknown_attribute(struct token
*token
)
919 struct expression
*expr
= NULL
;
921 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
923 if (match_op(token
, '('))
924 token
= parens_expression(token
, &expr
, "in attribute");
928 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
930 ctype
->modifiers
= 0;
931 token
= expect(token
, '(', "after attribute");
932 token
= expect(token
, '(', "after attribute");
935 struct ident
*attribute_name
;
938 if (eof_token(token
))
940 if (match_op(token
, ';'))
942 if (token_type(token
) != TOKEN_IDENT
)
944 attribute_name
= token
->ident
;
945 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
946 if (attr
&& attr
->op
->attribute
)
947 token
= attr
->op
->attribute(token
->next
, attr
, ctype
);
949 token
= recover_unknown_attribute(token
);
951 if (!match_op(token
, ','))
956 token
= expect(token
, ')', "after attribute");
957 token
= expect(token
, ')', "after attribute");
961 struct symbol
* ctype_integer(unsigned long spec
)
963 static struct symbol
*const integer_ctypes
[][3] = {
964 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
965 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
966 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
967 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
968 { &int_ctype
, &sint_ctype
, &uint_ctype
},
970 struct symbol
*const (*ctype
)[3];
973 ctype
= integer_ctypes
;
974 if (!(spec
& MOD_LONGLONG
)) {
976 if (!(spec
& MOD_LONG
)) {
978 if (!(spec
& MOD_SHORT
)) {
980 if (!(spec
& MOD_CHAR
))
986 sub
= ((spec
& MOD_UNSIGNED
)
988 : ((spec
& MOD_EXPLICITLY_SIGNED
)
992 return ctype
[0][sub
];
995 struct symbol
* ctype_fp(unsigned long spec
)
997 if (spec
& MOD_LONGLONG
)
998 return &ldouble_ctype
;
1000 return &double_ctype
;
1001 return &float_ctype
;
1004 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1006 unsigned long mod
= thistype
->modifiers
;
1009 unsigned long old
= ctype
->modifiers
;
1010 unsigned long extra
= 0, dup
, conflict
;
1012 if (mod
& old
& MOD_LONG
) {
1013 extra
= MOD_LONGLONG
| MOD_LONG
;
1017 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
1019 sparse_error(pos
, "Just how %sdo you want this type to be?",
1020 modifier_string(dup
));
1022 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
1024 sparse_error(pos
, "You cannot have both long and short modifiers.");
1026 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
1028 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
1030 // Only one storage modifier allowed, except that "inline" doesn't count.
1031 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
1032 conflict
&= (conflict
- 1);
1034 sparse_error(pos
, "multiple storage classes");
1036 ctype
->modifiers
= old
| mod
| extra
;
1040 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1041 (struct ptr_list
**)&ctype
->contexts
);
1044 if (thistype
->alignment
& (thistype
->alignment
-1)) {
1045 warning(pos
, "I don't like non-power-of-2 alignments");
1046 thistype
->alignment
= 0;
1048 if (thistype
->alignment
> ctype
->alignment
)
1049 ctype
->alignment
= thistype
->alignment
;
1053 ctype
->as
= thistype
->as
;
1056 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
1058 unsigned long banned
, wrong
;
1059 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
1060 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
1062 if (s
->type
== SYM_KEYWORD
)
1063 banned
= s
->op
->type
== KW_SPECIFIER
? (BANNED_SIZE
| BANNED_SIGN
) : 0;
1064 else if (s
->ctype
.base_type
== &fp_type
)
1065 banned
= BANNED_SIGN
;
1066 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
1072 // vector_type <-- whatever that is
1073 banned
= BANNED_SIZE
| BANNED_SIGN
;
1076 wrong
= mod
& banned
;
1078 sparse_error(*pos
, "modifier %sis invalid in this context",
1079 modifier_string (wrong
));
1082 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
1084 struct token
*token
;
1086 while ( (token
= next
) != NULL
) {
1087 struct ctype thistype
;
1088 struct ident
*ident
;
1089 struct symbol
*s
, *type
;
1093 if (token_type(token
) != TOKEN_IDENT
)
1095 ident
= token
->ident
;
1097 s
= lookup_symbol(ident
, NS_TYPEDEF
);
1100 thistype
= s
->ctype
;
1101 mod
= thistype
.modifiers
;
1103 if (s
->type
!= SYM_KEYWORD
)
1105 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1108 if (s
->type
== SYM_KEYWORD
&& s
->op
->declarator
) {
1109 next
= s
->op
->declarator(next
, &thistype
);
1110 mod
= thistype
.modifiers
;
1112 type
= thistype
.base_type
;
1116 if (ctype
->base_type
)
1118 /* User types only mix with qualifiers */
1119 if (mod
& MOD_USERTYPE
) {
1120 if (ctype
->modifiers
& MOD_SPECIFIER
)
1123 ctype
->base_type
= type
;
1126 check_modifiers(&token
->pos
, s
, ctype
->modifiers
);
1127 apply_ctype(token
->pos
, &thistype
, ctype
);
1130 if (!ctype
->base_type
) {
1131 struct symbol
*base
= &incomplete_ctype
;
1134 * If we have modifiers, we'll default to an integer
1135 * type, and "ctype_integer()" will turn this into
1138 if (ctype
->modifiers
& MOD_SPECIFIER
)
1140 ctype
->base_type
= base
;
1145 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1147 struct expression
*expr
= NULL
;
1149 token
= parse_expression(token
, &expr
);
1150 sym
->array_size
= expr
;
1154 static struct token
*parameter_type_list(struct token
*, struct symbol
*, struct ident
**p
);
1155 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
);
1157 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
, unsigned int keywords
)
1159 struct symbol
*keyword
;
1161 struct ctype thistype
= { 0, };
1162 if (token_type(token
) != TOKEN_IDENT
)
1164 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1165 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1167 if (!(keyword
->op
->type
& keywords
))
1169 token
= keyword
->op
->declarator(token
->next
, &thistype
);
1170 apply_ctype(token
->pos
, &thistype
, ctype
);
1175 static struct token
*direct_declarator(struct token
*token
, struct symbol
*decl
, struct ident
**p
)
1177 struct ctype
*ctype
= &decl
->ctype
;
1179 if (p
&& token_type(token
) == TOKEN_IDENT
) {
1181 token
= token
->next
;
1185 token
= handle_attributes(token
, ctype
, KW_ATTRIBUTE
| KW_ASM
);
1187 if (token_type(token
) != TOKEN_SPECIAL
)
1191 * This can be either a parameter list or a grouping.
1192 * For the direct (non-abstract) case, we know if must be
1193 * a parameter list if we already saw the identifier.
1194 * For the abstract case, we know if must be a parameter
1195 * list if it is empty or starts with a type.
1197 if (token
->special
== '(') {
1199 struct token
*next
= token
->next
;
1202 next
= handle_attributes(next
, ctype
, KW_ATTRIBUTE
);
1203 fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
1206 struct symbol
*base_type
= ctype
->base_type
;
1207 token
= declarator(next
, decl
, p
);
1208 token
= expect(token
, ')', "in nested declarator");
1209 while (ctype
->base_type
!= base_type
)
1210 ctype
= &ctype
->base_type
->ctype
;
1215 sym
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1216 token
= parameter_type_list(next
, sym
, p
);
1217 token
= expect(token
, ')', "in function declarator");
1218 sym
->endpos
= token
->pos
;
1221 if (token
->special
== '[') {
1222 struct symbol
*array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1223 token
= abstract_array_declarator(token
->next
, array
);
1224 token
= expect(token
, ']', "in abstract_array_declarator");
1225 array
->endpos
= token
->pos
;
1226 ctype
= &array
->ctype
;
1234 static struct token
*pointer(struct token
*token
, struct ctype
*ctype
)
1236 unsigned long modifiers
;
1237 struct symbol
*base_type
;
1239 modifiers
= ctype
->modifiers
& ~MOD_TYPEDEF
;
1240 base_type
= ctype
->base_type
;
1241 ctype
->modifiers
= modifiers
;
1243 while (match_op(token
,'*')) {
1244 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1245 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
1246 ptr
->ctype
.as
= ctype
->as
;
1247 concat_ptr_list((struct ptr_list
*)ctype
->contexts
,
1248 (struct ptr_list
**)&ptr
->ctype
.contexts
);
1249 ptr
->ctype
.base_type
= base_type
;
1252 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
1253 ctype
->base_type
= base_type
;
1255 free_ptr_list(&ctype
->contexts
);
1257 token
= declaration_specifiers(token
->next
, ctype
, 1);
1258 modifiers
= ctype
->modifiers
;
1259 ctype
->base_type
->endpos
= token
->pos
;
1264 static struct token
*declarator(struct token
*token
, struct symbol
*sym
, struct ident
**p
)
1266 token
= pointer(token
, &sym
->ctype
);
1267 return direct_declarator(token
, sym
, p
);
1270 static struct token
*handle_bitfield(struct token
*token
, struct symbol
*decl
)
1272 struct ctype
*ctype
= &decl
->ctype
;
1273 struct expression
*expr
;
1274 struct symbol
*bitfield
;
1277 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1278 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1279 show_typename(ctype
->base_type
));
1280 // Parse this to recover gracefully.
1281 return conditional_expression(token
->next
, &expr
);
1284 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1285 token
= conditional_expression(token
->next
, &expr
);
1286 width
= const_expression_value(expr
);
1287 bitfield
->bit_size
= width
;
1289 if (width
< 0 || width
> INT_MAX
) {
1290 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1292 } else if (decl
->ident
&& width
== 0) {
1293 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1294 show_ident(decl
->ident
));
1296 } else if (decl
->ident
) {
1297 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1298 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1299 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1300 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1301 // Valid values are either {-1;0} or {0}, depending on integer
1302 // representation. The latter makes for very efficient code...
1303 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1305 if (Wdefault_bitfield_sign
&&
1306 bitfield_type
->type
!= SYM_ENUM
&&
1307 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1309 // The sign of bitfields is unspecified by default.
1310 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1313 bitfield
->bit_size
= width
;
1314 bitfield
->endpos
= token
->pos
;
1318 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1320 struct ctype ctype
= {0, };
1322 token
= declaration_specifiers(token
, &ctype
, 0);
1324 struct ident
*ident
= NULL
;
1325 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1326 decl
->ctype
= ctype
;
1327 token
= declarator(token
, decl
, &ident
);
1328 decl
->ident
= ident
;
1329 if (match_op(token
, ':')) {
1330 token
= handle_bitfield(token
, decl
);
1331 token
= handle_attributes(token
, &decl
->ctype
, KW_ATTRIBUTE
| KW_ASM
);
1333 apply_modifiers(token
->pos
, &decl
->ctype
);
1334 add_symbol(list
, decl
);
1335 decl
->endpos
= token
->pos
;
1336 if (!match_op(token
, ','))
1338 token
= token
->next
;
1343 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1345 while (!match_op(token
, '}')) {
1346 if (!match_op(token
, ';'))
1347 token
= declaration_list(token
, list
);
1348 if (!match_op(token
, ';')) {
1349 sparse_error(token
->pos
, "expected ; at end of declaration");
1352 token
= token
->next
;
1357 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
1359 struct ident
*ident
= NULL
;
1361 struct ctype ctype
= { 0, };
1363 token
= declaration_specifiers(token
, &ctype
, 0);
1364 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1367 token
= declarator(token
, sym
, &ident
);
1369 apply_modifiers(token
->pos
, &sym
->ctype
);
1370 sym
->endpos
= token
->pos
;
1374 struct token
*typename(struct token
*token
, struct symbol
**p
, int mod
)
1376 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1378 token
= declaration_specifiers(token
, &sym
->ctype
, 0);
1379 token
= declarator(token
, sym
, NULL
);
1380 apply_modifiers(token
->pos
, &sym
->ctype
);
1381 if (sym
->ctype
.modifiers
& MOD_STORAGE
& ~mod
)
1382 warning(sym
->pos
, "storage class in typename (%s)",
1383 show_typename(sym
));
1384 sym
->endpos
= token
->pos
;
1388 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1390 token
= parse_expression(token
, tree
);
1391 return expect(token
, ';', "at end of statement");
1394 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1395 struct expression_list
**inout
)
1397 struct expression
*expr
;
1399 /* Allow empty operands */
1400 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1403 struct ident
*ident
= NULL
;
1404 if (match_op(token
->next
, '[') &&
1405 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1406 match_op(token
->next
->next
->next
, ']')) {
1407 ident
= token
->next
->next
->ident
;
1408 token
= token
->next
->next
->next
;
1410 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1411 token
= primary_expression(token
->next
, &expr
);
1412 add_expression(inout
, expr
);
1413 token
= parens_expression(token
, &expr
, "in asm parameter");
1414 add_expression(inout
, expr
);
1415 } while (match_op(token
, ','));
1419 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1420 struct expression_list
**clobbers
)
1422 struct expression
*expr
;
1425 token
= primary_expression(token
->next
, &expr
);
1426 add_expression(clobbers
, expr
);
1427 } while (match_op(token
, ','));
1431 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1433 token
= token
->next
;
1434 stmt
->type
= STMT_ASM
;
1435 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1436 token
= token
->next
;
1438 token
= expect(token
, '(', "after asm");
1439 token
= parse_expression(token
, &stmt
->asm_string
);
1440 if (match_op(token
, ':'))
1441 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1442 if (match_op(token
, ':'))
1443 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1444 if (match_op(token
, ':'))
1445 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1446 token
= expect(token
, ')', "after asm");
1447 return expect(token
, ';', "at end of asm-statement");
1450 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
)
1452 struct expression
*expr
;
1453 token
= expect(token
, '(', "after asm");
1454 token
= parse_expression(token
->next
, &expr
);
1455 token
= expect(token
, ')', "after asm");
1459 /* Make a statement out of an expression */
1460 static struct statement
*make_statement(struct expression
*expr
)
1462 struct statement
*stmt
;
1466 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1467 stmt
->expression
= expr
;
1472 * All iterators have two symbols associated with them:
1473 * the "continue" and "break" symbols, which are targets
1474 * for continue and break statements respectively.
1476 * They are in a special name-space, but they follow
1477 * all the normal visibility rules, so nested iterators
1478 * automatically work right.
1480 static void start_iterator(struct statement
*stmt
)
1482 struct symbol
*cont
, *brk
;
1484 start_symbol_scope();
1485 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1486 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1487 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1488 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1490 stmt
->type
= STMT_ITERATOR
;
1491 stmt
->iterator_break
= brk
;
1492 stmt
->iterator_continue
= cont
;
1493 fn_local_symbol(brk
);
1494 fn_local_symbol(cont
);
1497 static void end_iterator(struct statement
*stmt
)
1502 static struct statement
*start_function(struct symbol
*sym
)
1505 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1507 start_function_scope();
1508 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1509 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1510 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1511 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1512 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1514 fn_local_symbol(ret
);
1516 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1522 static void end_function(struct symbol
*sym
)
1525 end_function_scope();
1529 * A "switch()" statement, like an iterator, has a
1530 * the "break" symbol associated with it. It works
1531 * exactly like the iterator break - it's the target
1532 * for any break-statements in scope, and means that
1533 * "break" handling doesn't even need to know whether
1534 * it's breaking out of an iterator or a switch.
1536 * In addition, the "case" symbol is a marker for the
1537 * case/default statements to find the switch statement
1538 * that they are associated with.
1540 static void start_switch(struct statement
*stmt
)
1542 struct symbol
*brk
, *switch_case
;
1544 start_symbol_scope();
1545 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1546 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1548 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1549 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1550 switch_case
->stmt
= stmt
;
1552 stmt
->type
= STMT_SWITCH
;
1553 stmt
->switch_break
= brk
;
1554 stmt
->switch_case
= switch_case
;
1556 fn_local_symbol(brk
);
1557 fn_local_symbol(switch_case
);
1560 static void end_switch(struct statement
*stmt
)
1562 if (!stmt
->switch_case
->symbol_list
)
1563 warning(stmt
->pos
, "switch with no cases");
1567 static void add_case_statement(struct statement
*stmt
)
1569 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1573 sparse_error(stmt
->pos
, "not in switch scope");
1574 stmt
->type
= STMT_NONE
;
1577 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1578 add_symbol(&target
->symbol_list
, sym
);
1580 stmt
->case_label
= sym
;
1581 fn_local_symbol(sym
);
1584 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1586 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1589 error_die(token
->pos
, "internal error: return without a function target");
1590 stmt
->type
= STMT_RETURN
;
1591 stmt
->ret_target
= target
;
1592 return expression_statement(token
->next
, &stmt
->ret_value
);
1595 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1597 struct symbol_list
*syms
;
1598 struct expression
*e1
, *e2
, *e3
;
1599 struct statement
*iterator
;
1601 start_iterator(stmt
);
1602 token
= expect(token
->next
, '(', "after 'for'");
1606 /* C99 variable declaration? */
1607 if (lookup_type(token
)) {
1608 token
= external_declaration(token
, &syms
);
1610 token
= parse_expression(token
, &e1
);
1611 token
= expect(token
, ';', "in 'for'");
1613 token
= parse_expression(token
, &e2
);
1614 token
= expect(token
, ';', "in 'for'");
1615 token
= parse_expression(token
, &e3
);
1616 token
= expect(token
, ')', "in 'for'");
1617 token
= statement(token
, &iterator
);
1619 stmt
->iterator_syms
= syms
;
1620 stmt
->iterator_pre_statement
= make_statement(e1
);
1621 stmt
->iterator_pre_condition
= e2
;
1622 stmt
->iterator_post_statement
= make_statement(e3
);
1623 stmt
->iterator_post_condition
= NULL
;
1624 stmt
->iterator_statement
= iterator
;
1630 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1632 struct expression
*expr
;
1633 struct statement
*iterator
;
1635 start_iterator(stmt
);
1636 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1637 token
= statement(token
, &iterator
);
1639 stmt
->iterator_pre_condition
= expr
;
1640 stmt
->iterator_post_condition
= NULL
;
1641 stmt
->iterator_statement
= iterator
;
1647 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1649 struct expression
*expr
;
1650 struct statement
*iterator
;
1652 start_iterator(stmt
);
1653 token
= statement(token
->next
, &iterator
);
1654 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1655 token
= token
->next
;
1657 sparse_error(token
->pos
, "expected 'while' after 'do'");
1658 token
= parens_expression(token
, &expr
, "after 'do-while'");
1660 stmt
->iterator_post_condition
= expr
;
1661 stmt
->iterator_statement
= iterator
;
1664 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
1665 warning(iterator
->pos
, "do-while statement is not a compound statement");
1667 return expect(token
, ';', "after statement");
1670 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
1672 stmt
->type
= STMT_IF
;
1673 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1674 token
= statement(token
, &stmt
->if_true
);
1675 if (token_type(token
) != TOKEN_IDENT
)
1677 if (token
->ident
!= &else_ident
)
1679 return statement(token
->next
, &stmt
->if_false
);
1682 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
1684 stmt
->type
= STMT_CASE
;
1685 token
= expect(token
, ':', "after default/case");
1686 add_case_statement(stmt
);
1687 return statement(token
, &stmt
->case_statement
);
1690 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
1692 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1693 if (match_op(token
, SPECIAL_ELLIPSIS
))
1694 token
= parse_expression(token
->next
, &stmt
->case_to
);
1695 return case_statement(token
, stmt
);
1698 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
1700 return case_statement(token
->next
, stmt
);
1703 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
1705 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1706 stmt
->type
= STMT_GOTO
;
1707 stmt
->goto_label
= target
;
1709 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1710 return expect(token
->next
, ';', "at end of statement");
1713 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
1715 stmt
->type
= STMT_SWITCH
;
1717 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1718 token
= statement(token
, &stmt
->switch_statement
);
1723 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
1725 stmt
->type
= STMT_GOTO
;
1726 token
= token
->next
;
1727 if (match_op(token
, '*')) {
1728 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1729 add_statement(&function_computed_goto_list
, stmt
);
1730 } else if (token_type(token
) == TOKEN_IDENT
) {
1731 stmt
->goto_label
= label_symbol(token
);
1732 token
= token
->next
;
1734 sparse_error(token
->pos
, "Expected identifier or goto expression");
1736 return expect(token
, ';', "at end of statement");
1739 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
1741 stmt
->type
= STMT_CONTEXT
;
1742 token
= parse_expression(token
->next
, &stmt
->expression
);
1743 if(stmt
->expression
->type
== EXPR_PREOP
1744 && stmt
->expression
->op
== '('
1745 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
1746 struct expression
*expr
;
1747 expr
= stmt
->expression
->unop
;
1748 stmt
->context
= expr
->left
;
1749 stmt
->expression
= expr
->right
;
1751 return expect(token
, ';', "at end of statement");
1754 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
1756 stmt
->type
= STMT_RANGE
;
1757 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1758 token
= expect(token
, ',', "after range expression");
1759 token
= assignment_expression(token
, &stmt
->range_low
);
1760 token
= expect(token
, ',', "after low range");
1761 token
= assignment_expression(token
, &stmt
->range_high
);
1762 return expect(token
, ';', "after range statement");
1765 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1767 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1770 if (token_type(token
) == TOKEN_IDENT
) {
1771 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1772 if (s
&& s
->op
->statement
)
1773 return s
->op
->statement(token
, stmt
);
1775 if (match_op(token
->next
, ':')) {
1776 stmt
->type
= STMT_LABEL
;
1777 stmt
->label_identifier
= label_symbol(token
);
1778 token
= handle_attributes(token
->next
->next
, &stmt
->label_identifier
->ctype
, KW_ATTRIBUTE
);
1779 return statement(token
, &stmt
->label_statement
);
1783 if (match_op(token
, '{')) {
1784 stmt
->type
= STMT_COMPOUND
;
1785 start_symbol_scope();
1786 token
= compound_statement(token
->next
, stmt
);
1789 return expect(token
, '}', "at end of compound statement");
1792 stmt
->type
= STMT_EXPRESSION
;
1793 return expression_statement(token
, &stmt
->expression
);
1796 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1798 int seen_statement
= 0;
1800 struct statement
* stmt
;
1801 if (eof_token(token
))
1803 if (match_op(token
, '}'))
1805 if (lookup_type(token
)) {
1806 if (seen_statement
) {
1807 warning(token
->pos
, "mixing declarations and code");
1810 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1811 token
= external_declaration(token
, &stmt
->declaration
);
1813 seen_statement
= Wdeclarationafterstatement
;
1814 token
= statement(token
, &stmt
);
1816 add_statement(list
, stmt
);
1821 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
, struct ident
**p
)
1823 struct symbol_list
**list
= &fn
->arguments
;
1825 if (match_op(token
, ')')) {
1826 // No warning for "void oink ();"
1827 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1828 if (p
&& !match_op(token
->next
, ';'))
1829 warning(token
->pos
, "non-ANSI function declaration of function '%s'", show_ident(*p
));
1836 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1838 warning(token
->pos
, "variadic functions must have one named argument");
1840 token
= token
->next
;
1844 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1845 token
= parameter_declaration(token
, &sym
);
1846 if (sym
->ctype
.base_type
== &void_ctype
) {
1847 /* Special case: (void) */
1848 if (!*list
&& !sym
->ident
)
1850 warning(token
->pos
, "void parameter");
1852 add_symbol(list
, sym
);
1853 sym
->endpos
= token
->pos
;
1854 if (!match_op(token
, ','))
1856 token
= token
->next
;
1862 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1864 token
= statement_list(token
, &stmt
->stmts
);
1868 static struct expression
*identifier_expression(struct token
*token
)
1870 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1871 expr
->expr_ident
= token
->ident
;
1875 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1877 int idx_from
, idx_to
;
1878 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1880 idx_from
= const_expression_value(from
);
1883 idx_to
= const_expression_value(to
);
1884 if (idx_to
< idx_from
|| idx_from
< 0)
1885 warning(from
->pos
, "nonsense array initializer index range");
1887 expr
->idx_from
= idx_from
;
1888 expr
->idx_to
= idx_to
;
1892 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
1894 int expect_equal
= 0;
1895 struct token
*next
= token
->next
;
1896 struct expression
**tail
= ep
;
1901 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
1902 struct expression
*expr
= identifier_expression(token
);
1903 if (Wold_initializer
)
1904 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
1905 token
= initializer(&expr
->ident_expression
, next
->next
);
1906 if (expr
->ident_expression
)
1911 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
1912 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
1913 struct expression
*expr
= identifier_expression(next
);
1915 tail
= &expr
->ident_expression
;
1918 } else if (match_op(token
, '[')) {
1919 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
1920 token
= constant_expression(token
->next
, &from
);
1922 sparse_error(token
->pos
, "Expected constant expression");
1925 if (match_op(token
, SPECIAL_ELLIPSIS
))
1926 token
= constant_expression(token
->next
, &to
);
1927 expr
= index_expression(from
, to
);
1929 tail
= &expr
->idx_expression
;
1930 token
= expect(token
, ']', "at end of initializer index");
1937 if (nested
&& !expect_equal
) {
1938 if (!match_op(token
, '='))
1939 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
1944 token
= expect(token
, '=', "at end of initializer index");
1946 token
= initializer(tail
, token
);
1952 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
1954 struct expression
*expr
;
1957 token
= single_initializer(&expr
, token
);
1960 add_expression(list
, expr
);
1961 if (!match_op(token
, ','))
1963 token
= token
->next
;
1968 struct token
*initializer(struct expression
**tree
, struct token
*token
)
1970 if (match_op(token
, '{')) {
1971 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
1973 token
= initializer_list(&expr
->expr_list
, token
->next
);
1974 return expect(token
, '}', "at end of initializer");
1976 return assignment_expression(token
, tree
);
1979 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
1982 sparse_error(sym
->pos
, "no identifier for function argument");
1985 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
1988 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
1989 struct symbol_list
**list
)
1991 struct symbol_list
**old_symbol_list
;
1992 struct symbol
*base_type
= decl
->ctype
.base_type
;
1993 struct statement
*stmt
, **p
;
1994 struct symbol
*prev
;
1997 old_symbol_list
= function_symbol_list
;
1998 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
1999 function_symbol_list
= &decl
->inline_symbol_list
;
2000 p
= &base_type
->inline_stmt
;
2002 function_symbol_list
= &decl
->symbol_list
;
2003 p
= &base_type
->stmt
;
2005 function_computed_target_list
= NULL
;
2006 function_computed_goto_list
= NULL
;
2008 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2009 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2010 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2012 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2013 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2015 stmt
= start_function(decl
);
2018 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2019 declare_argument(arg
, base_type
);
2020 } END_FOR_EACH_PTR(arg
);
2022 token
= compound_statement(token
->next
, stmt
);
2025 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2026 add_symbol(list
, decl
);
2027 check_declaration(decl
);
2028 decl
->definition
= decl
;
2029 prev
= decl
->same_symbol
;
2030 if (prev
&& prev
->definition
) {
2031 warning(decl
->pos
, "multiple definitions for function '%s'",
2032 show_ident(decl
->ident
));
2033 info(prev
->definition
->pos
, " the previous one is here");
2036 prev
->definition
= decl
;
2037 prev
= prev
->same_symbol
;
2040 function_symbol_list
= old_symbol_list
;
2041 if (function_computed_goto_list
) {
2042 if (!function_computed_target_list
)
2043 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2045 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2046 stmt
->target_list
= function_computed_target_list
;
2047 } END_FOR_EACH_PTR(stmt
);
2050 return expect(token
, '}', "at end of function");
2053 static void promote_k_r_types(struct symbol
*arg
)
2055 struct symbol
*base
= arg
->ctype
.base_type
;
2056 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2057 arg
->ctype
.base_type
= &int_ctype
;
2061 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2063 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2066 FOR_EACH_PTR(real_args
, arg
) {
2067 struct symbol
*type
;
2069 /* This is quadratic in the number of arguments. We _really_ don't care */
2070 FOR_EACH_PTR(argtypes
, type
) {
2071 if (type
->ident
== arg
->ident
)
2073 } END_FOR_EACH_PTR(type
);
2074 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2078 /* "char" and "short" promote to "int" */
2079 promote_k_r_types(type
);
2081 arg
->ctype
= type
->ctype
;
2082 } END_FOR_EACH_PTR(arg
);
2084 FOR_EACH_PTR(argtypes
, arg
) {
2086 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2087 } END_FOR_EACH_PTR(arg
);
2091 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2092 struct symbol_list
**list
)
2094 struct symbol_list
*args
= NULL
;
2096 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2098 token
= declaration_list(token
, &args
);
2099 if (!match_op(token
, ';')) {
2100 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2103 token
= token
->next
;
2104 } while (lookup_type(token
));
2106 apply_k_r_types(args
, decl
);
2108 if (!match_op(token
, '{')) {
2109 sparse_error(token
->pos
, "expected function body");
2112 return parse_function_body(token
, decl
, list
);
2115 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2117 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2118 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2119 struct statement
*stmt
;
2121 anon
->ctype
.base_type
= fn
;
2122 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2125 token
= parse_asm_statement(token
, stmt
);
2127 add_symbol(list
, anon
);
2131 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2133 struct ident
*ident
= NULL
;
2134 struct symbol
*decl
;
2135 struct ctype ctype
= { 0, };
2136 struct symbol
*base_type
;
2139 /* Top-level inline asm? */
2140 if (token_type(token
) == TOKEN_IDENT
) {
2141 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2142 if (s
&& s
->op
->toplevel
)
2143 return s
->op
->toplevel(token
, list
);
2146 /* Parse declaration-specifiers, if any */
2147 token
= declaration_specifiers(token
, &ctype
, 0);
2148 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2149 decl
->ctype
= ctype
;
2150 token
= declarator(token
, decl
, &ident
);
2151 apply_modifiers(token
->pos
, &decl
->ctype
);
2153 decl
->endpos
= token
->pos
;
2155 /* Just a type declaration? */
2157 return expect(token
, ';', "end of type declaration");
2159 /* type define declaration? */
2160 is_typedef
= (ctype
.modifiers
& MOD_TYPEDEF
) != 0;
2162 /* Typedefs don't have meaningful storage */
2164 ctype
.modifiers
&= ~MOD_STORAGE
;
2165 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
2166 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2169 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2171 base_type
= decl
->ctype
.base_type
;
2174 if (base_type
&& !base_type
->ident
) {
2175 switch (base_type
->type
) {
2180 base_type
->ident
= ident
;
2183 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2184 /* K&R argument declaration? */
2185 if (lookup_type(token
))
2186 return parse_k_r_arguments(token
, decl
, list
);
2187 if (match_op(token
, '{'))
2188 return parse_function_body(token
, decl
, list
);
2190 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2191 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2192 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2193 sparse_error(token
->pos
, "void declaration");
2197 if (!is_typedef
&& match_op(token
, '=')) {
2198 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2199 warning(decl
->pos
, "symbol with external linkage has initializer");
2200 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2202 token
= initializer(&decl
->initializer
, token
->next
);
2205 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2206 add_symbol(list
, decl
);
2207 fn_local_symbol(decl
);
2210 check_declaration(decl
);
2211 if (decl
->same_symbol
)
2212 decl
->definition
= decl
->same_symbol
->definition
;
2214 if (!match_op(token
, ','))
2217 token
= token
->next
;
2219 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2220 decl
->ctype
= ctype
;
2221 token
= declaration_specifiers(token
, &decl
->ctype
, 1);
2222 token
= declarator(token
, decl
, &ident
);
2223 apply_modifiers(token
->pos
, &decl
->ctype
);
2224 decl
->endpos
= token
->pos
;
2226 sparse_error(token
->pos
, "expected identifier name in type definition");
2230 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2232 /* Function declarations are automatically extern unless specifically static */
2233 base_type
= decl
->ctype
.base_type
;
2234 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2235 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2236 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2239 return expect(token
, ';', "at end of declaration");