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
);
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 decl_state
*ctx
, 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 (ctx
->ctype
.base_type
)
1118 /* User types only mix with qualifiers */
1119 if (mod
& MOD_USERTYPE
) {
1120 if (ctx
->ctype
.modifiers
& MOD_SPECIFIER
)
1123 ctx
->ctype
.base_type
= type
;
1126 check_modifiers(&token
->pos
, s
, ctx
->ctype
.modifiers
);
1127 apply_ctype(token
->pos
, &thistype
, &ctx
->ctype
);
1130 if (!ctx
->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 (ctx
->ctype
.modifiers
& MOD_SPECIFIER
)
1140 ctx
->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
*);
1155 static struct token
*identifier_list(struct token
*, struct symbol
*);
1156 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1158 static struct token
*skip_attribute(struct token
*token
)
1160 token
= token
->next
;
1161 if (match_op(token
, '(')) {
1163 token
= token
->next
;
1164 while (depth
&& !eof_token(token
)) {
1165 if (token_type(token
) == TOKEN_SPECIAL
) {
1166 if (token
->special
== '(')
1168 else if (token
->special
== ')')
1171 token
= token
->next
;
1177 static struct token
*skip_attributes(struct token
*token
)
1179 struct symbol
*keyword
;
1181 if (token_type(token
) != TOKEN_IDENT
)
1183 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1184 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1186 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1188 token
= expect(token
->next
, '(', "after attribute");
1189 token
= expect(token
, '(', "after attribute");
1191 if (eof_token(token
))
1193 if (match_op(token
, ';'))
1195 if (token_type(token
) != TOKEN_IDENT
)
1197 token
= skip_attribute(token
);
1198 if (!match_op(token
, ','))
1200 token
= token
->next
;
1202 token
= expect(token
, ')', "after attribute");
1203 token
= expect(token
, ')', "after attribute");
1208 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
, unsigned int keywords
)
1210 struct symbol
*keyword
;
1212 struct ctype thistype
= { 0, };
1213 if (token_type(token
) != TOKEN_IDENT
)
1215 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1216 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1218 if (!(keyword
->op
->type
& keywords
))
1220 token
= keyword
->op
->declarator(token
->next
, &thistype
);
1221 apply_ctype(token
->pos
, &thistype
, ctype
);
1222 keywords
&= KW_ATTRIBUTE
;
1227 static int is_nested(struct token
*token
, struct token
**p
,
1228 int prefer_abstract
)
1231 * This can be either a parameter list or a grouping.
1232 * For the direct (non-abstract) case, we know if must be
1233 * a parameter list if we already saw the identifier.
1234 * For the abstract case, we know if must be a parameter
1235 * list if it is empty or starts with a type.
1237 struct token
*next
= token
->next
;
1239 *p
= next
= skip_attributes(next
);
1241 if (token_type(next
) == TOKEN_IDENT
) {
1242 if (lookup_type(next
))
1243 return !prefer_abstract
;
1247 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1254 Empty
, K_R
, Proto
, Bad_Func
,
1257 static enum kind
which_func(struct token
*token
,
1259 int prefer_abstract
)
1261 struct token
*next
= token
->next
;
1263 if (token_type(next
) == TOKEN_IDENT
) {
1264 if (lookup_type(next
))
1266 /* identifier list not in definition; complain */
1267 if (prefer_abstract
)
1269 "identifier list not in definition");
1273 if (token_type(next
) != TOKEN_SPECIAL
)
1276 if (next
->special
== ')') {
1277 /* don't complain about those */
1278 if (!n
|| match_op(next
->next
, ';'))
1281 "non-ANSI function declaration of function '%s'",
1286 if (next
->special
== SPECIAL_ELLIPSIS
) {
1288 "variadic functions must have one named argument");
1295 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1297 struct ctype
*ctype
= &ctx
->ctype
;
1299 struct ident
**p
= ctx
->ident
;
1301 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1302 *ctx
->ident
= token
->ident
;
1303 token
= token
->next
;
1304 } else if (match_op(token
, '(') &&
1305 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1306 struct symbol
*base_type
= ctype
->base_type
;
1307 if (token
->next
!= next
)
1308 next
= handle_attributes(token
->next
, ctype
,
1310 token
= declarator(next
, ctx
);
1311 token
= expect(token
, ')', "in nested declarator");
1312 while (ctype
->base_type
!= base_type
)
1313 ctype
= &ctype
->base_type
->ctype
;
1317 if (match_op(token
, '(')) {
1318 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1320 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1321 token
= token
->next
;
1323 token
= identifier_list(token
, fn
);
1324 else if (kind
== Proto
)
1325 token
= parameter_type_list(token
, fn
);
1326 token
= expect(token
, ')', "in function declarator");
1327 fn
->endpos
= token
->pos
;
1331 while (match_op(token
, '[')) {
1332 struct symbol
*array
;
1333 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1334 token
= abstract_array_declarator(token
->next
, array
);
1335 token
= expect(token
, ']', "in abstract_array_declarator");
1336 array
->endpos
= token
->pos
;
1337 ctype
= &array
->ctype
;
1342 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1344 unsigned long modifiers
;
1345 struct symbol
*base_type
;
1347 modifiers
= ctx
->ctype
.modifiers
& ~MOD_TYPEDEF
;
1348 base_type
= ctx
->ctype
.base_type
;
1349 ctx
->ctype
.modifiers
= modifiers
;
1351 while (match_op(token
,'*')) {
1352 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1353 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
1354 ptr
->ctype
.as
= ctx
->ctype
.as
;
1355 concat_ptr_list((struct ptr_list
*)ctx
->ctype
.contexts
,
1356 (struct ptr_list
**)&ptr
->ctype
.contexts
);
1357 ptr
->ctype
.base_type
= base_type
;
1360 ctx
->ctype
.modifiers
= modifiers
& MOD_STORAGE
;
1361 ctx
->ctype
.base_type
= base_type
;
1363 free_ptr_list(&ctx
->ctype
.contexts
);
1365 token
= declaration_specifiers(token
->next
, ctx
, 1);
1366 modifiers
= ctx
->ctype
.modifiers
;
1367 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1372 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1374 token
= pointer(token
, ctx
);
1375 return direct_declarator(token
, ctx
);
1378 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1380 struct ctype
*ctype
= &ctx
->ctype
;
1381 struct expression
*expr
;
1382 struct symbol
*bitfield
;
1385 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1386 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1387 show_typename(ctype
->base_type
));
1388 // Parse this to recover gracefully.
1389 return conditional_expression(token
->next
, &expr
);
1392 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1393 token
= conditional_expression(token
->next
, &expr
);
1394 width
= const_expression_value(expr
);
1395 bitfield
->bit_size
= width
;
1397 if (width
< 0 || width
> INT_MAX
) {
1398 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1400 } else if (*ctx
->ident
&& width
== 0) {
1401 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1402 show_ident(*ctx
->ident
));
1404 } else if (*ctx
->ident
) {
1405 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1406 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1407 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1408 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1409 // Valid values are either {-1;0} or {0}, depending on integer
1410 // representation. The latter makes for very efficient code...
1411 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1413 if (Wdefault_bitfield_sign
&&
1414 bitfield_type
->type
!= SYM_ENUM
&&
1415 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1417 // The sign of bitfields is unspecified by default.
1418 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1421 bitfield
->bit_size
= width
;
1422 bitfield
->endpos
= token
->pos
;
1426 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1428 struct decl_state ctx
= {.prefer_abstract
= 0};
1431 token
= declaration_specifiers(token
, &ctx
, 0);
1434 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1435 ctx
.ident
= &decl
->ident
;
1437 token
= declarator(token
, &ctx
);
1438 if (match_op(token
, ':'))
1439 token
= handle_bitfield(token
, &ctx
);
1441 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
);
1442 apply_modifiers(token
->pos
, &ctx
.ctype
);
1444 decl
->ctype
= ctx
.ctype
;
1445 decl
->endpos
= token
->pos
;
1446 add_symbol(list
, decl
);
1447 if (!match_op(token
, ','))
1449 token
= token
->next
;
1455 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1457 while (!match_op(token
, '}')) {
1458 if (!match_op(token
, ';'))
1459 token
= declaration_list(token
, list
);
1460 if (!match_op(token
, ';')) {
1461 sparse_error(token
->pos
, "expected ; at end of declaration");
1464 token
= token
->next
;
1469 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1471 struct decl_state ctx
= {.prefer_abstract
= 1};
1473 token
= declaration_specifiers(token
, &ctx
, 0);
1474 ctx
.ident
= &sym
->ident
;
1475 token
= declarator(token
, &ctx
);
1476 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
);
1477 apply_modifiers(token
->pos
, &ctx
.ctype
);
1478 sym
->ctype
= ctx
.ctype
;
1479 sym
->endpos
= token
->pos
;
1483 struct token
*typename(struct token
*token
, struct symbol
**p
, int mod
)
1485 struct decl_state ctx
= {.prefer_abstract
= 1};
1486 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1488 token
= declaration_specifiers(token
, &ctx
, 0);
1489 token
= declarator(token
, &ctx
);
1490 apply_modifiers(token
->pos
, &ctx
.ctype
);
1491 if (ctx
.ctype
.modifiers
& MOD_STORAGE
& ~mod
)
1492 warning(sym
->pos
, "storage class in typename (%s)",
1493 show_typename(sym
));
1494 sym
->ctype
= ctx
.ctype
;
1495 sym
->endpos
= token
->pos
;
1499 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1501 token
= parse_expression(token
, tree
);
1502 return expect(token
, ';', "at end of statement");
1505 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1506 struct expression_list
**inout
)
1508 struct expression
*expr
;
1510 /* Allow empty operands */
1511 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1514 struct ident
*ident
= NULL
;
1515 if (match_op(token
->next
, '[') &&
1516 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1517 match_op(token
->next
->next
->next
, ']')) {
1518 ident
= token
->next
->next
->ident
;
1519 token
= token
->next
->next
->next
;
1521 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1522 token
= primary_expression(token
->next
, &expr
);
1523 add_expression(inout
, expr
);
1524 token
= parens_expression(token
, &expr
, "in asm parameter");
1525 add_expression(inout
, expr
);
1526 } while (match_op(token
, ','));
1530 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1531 struct expression_list
**clobbers
)
1533 struct expression
*expr
;
1536 token
= primary_expression(token
->next
, &expr
);
1537 add_expression(clobbers
, expr
);
1538 } while (match_op(token
, ','));
1542 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1544 token
= token
->next
;
1545 stmt
->type
= STMT_ASM
;
1546 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1547 token
= token
->next
;
1549 token
= expect(token
, '(', "after asm");
1550 token
= parse_expression(token
, &stmt
->asm_string
);
1551 if (match_op(token
, ':'))
1552 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1553 if (match_op(token
, ':'))
1554 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1555 if (match_op(token
, ':'))
1556 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1557 token
= expect(token
, ')', "after asm");
1558 return expect(token
, ';', "at end of asm-statement");
1561 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
)
1563 struct expression
*expr
;
1564 token
= expect(token
, '(', "after asm");
1565 token
= parse_expression(token
->next
, &expr
);
1566 token
= expect(token
, ')', "after asm");
1570 /* Make a statement out of an expression */
1571 static struct statement
*make_statement(struct expression
*expr
)
1573 struct statement
*stmt
;
1577 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1578 stmt
->expression
= expr
;
1583 * All iterators have two symbols associated with them:
1584 * the "continue" and "break" symbols, which are targets
1585 * for continue and break statements respectively.
1587 * They are in a special name-space, but they follow
1588 * all the normal visibility rules, so nested iterators
1589 * automatically work right.
1591 static void start_iterator(struct statement
*stmt
)
1593 struct symbol
*cont
, *brk
;
1595 start_symbol_scope();
1596 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1597 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1598 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1599 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1601 stmt
->type
= STMT_ITERATOR
;
1602 stmt
->iterator_break
= brk
;
1603 stmt
->iterator_continue
= cont
;
1604 fn_local_symbol(brk
);
1605 fn_local_symbol(cont
);
1608 static void end_iterator(struct statement
*stmt
)
1613 static struct statement
*start_function(struct symbol
*sym
)
1616 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1618 start_function_scope();
1619 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1620 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1621 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1622 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1623 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1625 fn_local_symbol(ret
);
1627 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1633 static void end_function(struct symbol
*sym
)
1636 end_function_scope();
1640 * A "switch()" statement, like an iterator, has a
1641 * the "break" symbol associated with it. It works
1642 * exactly like the iterator break - it's the target
1643 * for any break-statements in scope, and means that
1644 * "break" handling doesn't even need to know whether
1645 * it's breaking out of an iterator or a switch.
1647 * In addition, the "case" symbol is a marker for the
1648 * case/default statements to find the switch statement
1649 * that they are associated with.
1651 static void start_switch(struct statement
*stmt
)
1653 struct symbol
*brk
, *switch_case
;
1655 start_symbol_scope();
1656 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1657 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1659 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1660 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1661 switch_case
->stmt
= stmt
;
1663 stmt
->type
= STMT_SWITCH
;
1664 stmt
->switch_break
= brk
;
1665 stmt
->switch_case
= switch_case
;
1667 fn_local_symbol(brk
);
1668 fn_local_symbol(switch_case
);
1671 static void end_switch(struct statement
*stmt
)
1673 if (!stmt
->switch_case
->symbol_list
)
1674 warning(stmt
->pos
, "switch with no cases");
1678 static void add_case_statement(struct statement
*stmt
)
1680 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1684 sparse_error(stmt
->pos
, "not in switch scope");
1685 stmt
->type
= STMT_NONE
;
1688 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1689 add_symbol(&target
->symbol_list
, sym
);
1691 stmt
->case_label
= sym
;
1692 fn_local_symbol(sym
);
1695 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1697 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1700 error_die(token
->pos
, "internal error: return without a function target");
1701 stmt
->type
= STMT_RETURN
;
1702 stmt
->ret_target
= target
;
1703 return expression_statement(token
->next
, &stmt
->ret_value
);
1706 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1708 struct symbol_list
*syms
;
1709 struct expression
*e1
, *e2
, *e3
;
1710 struct statement
*iterator
;
1712 start_iterator(stmt
);
1713 token
= expect(token
->next
, '(', "after 'for'");
1717 /* C99 variable declaration? */
1718 if (lookup_type(token
)) {
1719 token
= external_declaration(token
, &syms
);
1721 token
= parse_expression(token
, &e1
);
1722 token
= expect(token
, ';', "in 'for'");
1724 token
= parse_expression(token
, &e2
);
1725 token
= expect(token
, ';', "in 'for'");
1726 token
= parse_expression(token
, &e3
);
1727 token
= expect(token
, ')', "in 'for'");
1728 token
= statement(token
, &iterator
);
1730 stmt
->iterator_syms
= syms
;
1731 stmt
->iterator_pre_statement
= make_statement(e1
);
1732 stmt
->iterator_pre_condition
= e2
;
1733 stmt
->iterator_post_statement
= make_statement(e3
);
1734 stmt
->iterator_post_condition
= NULL
;
1735 stmt
->iterator_statement
= iterator
;
1741 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1743 struct expression
*expr
;
1744 struct statement
*iterator
;
1746 start_iterator(stmt
);
1747 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1748 token
= statement(token
, &iterator
);
1750 stmt
->iterator_pre_condition
= expr
;
1751 stmt
->iterator_post_condition
= NULL
;
1752 stmt
->iterator_statement
= iterator
;
1758 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1760 struct expression
*expr
;
1761 struct statement
*iterator
;
1763 start_iterator(stmt
);
1764 token
= statement(token
->next
, &iterator
);
1765 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1766 token
= token
->next
;
1768 sparse_error(token
->pos
, "expected 'while' after 'do'");
1769 token
= parens_expression(token
, &expr
, "after 'do-while'");
1771 stmt
->iterator_post_condition
= expr
;
1772 stmt
->iterator_statement
= iterator
;
1775 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
1776 warning(iterator
->pos
, "do-while statement is not a compound statement");
1778 return expect(token
, ';', "after statement");
1781 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
1783 stmt
->type
= STMT_IF
;
1784 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1785 token
= statement(token
, &stmt
->if_true
);
1786 if (token_type(token
) != TOKEN_IDENT
)
1788 if (token
->ident
!= &else_ident
)
1790 return statement(token
->next
, &stmt
->if_false
);
1793 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
1795 stmt
->type
= STMT_CASE
;
1796 token
= expect(token
, ':', "after default/case");
1797 add_case_statement(stmt
);
1798 return statement(token
, &stmt
->case_statement
);
1801 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
1803 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1804 if (match_op(token
, SPECIAL_ELLIPSIS
))
1805 token
= parse_expression(token
->next
, &stmt
->case_to
);
1806 return case_statement(token
, stmt
);
1809 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
1811 return case_statement(token
->next
, stmt
);
1814 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
1816 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1817 stmt
->type
= STMT_GOTO
;
1818 stmt
->goto_label
= target
;
1820 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1821 return expect(token
->next
, ';', "at end of statement");
1824 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
1826 stmt
->type
= STMT_SWITCH
;
1828 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1829 token
= statement(token
, &stmt
->switch_statement
);
1834 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
1836 stmt
->type
= STMT_GOTO
;
1837 token
= token
->next
;
1838 if (match_op(token
, '*')) {
1839 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1840 add_statement(&function_computed_goto_list
, stmt
);
1841 } else if (token_type(token
) == TOKEN_IDENT
) {
1842 stmt
->goto_label
= label_symbol(token
);
1843 token
= token
->next
;
1845 sparse_error(token
->pos
, "Expected identifier or goto expression");
1847 return expect(token
, ';', "at end of statement");
1850 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
1852 stmt
->type
= STMT_CONTEXT
;
1853 token
= parse_expression(token
->next
, &stmt
->expression
);
1854 if(stmt
->expression
->type
== EXPR_PREOP
1855 && stmt
->expression
->op
== '('
1856 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
1857 struct expression
*expr
;
1858 expr
= stmt
->expression
->unop
;
1859 stmt
->context
= expr
->left
;
1860 stmt
->expression
= expr
->right
;
1862 return expect(token
, ';', "at end of statement");
1865 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
1867 stmt
->type
= STMT_RANGE
;
1868 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1869 token
= expect(token
, ',', "after range expression");
1870 token
= assignment_expression(token
, &stmt
->range_low
);
1871 token
= expect(token
, ',', "after low range");
1872 token
= assignment_expression(token
, &stmt
->range_high
);
1873 return expect(token
, ';', "after range statement");
1876 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1878 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1881 if (token_type(token
) == TOKEN_IDENT
) {
1882 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1883 if (s
&& s
->op
->statement
)
1884 return s
->op
->statement(token
, stmt
);
1886 if (match_op(token
->next
, ':')) {
1887 stmt
->type
= STMT_LABEL
;
1888 stmt
->label_identifier
= label_symbol(token
);
1889 token
= handle_attributes(token
->next
->next
, &stmt
->label_identifier
->ctype
, KW_ATTRIBUTE
);
1890 return statement(token
, &stmt
->label_statement
);
1894 if (match_op(token
, '{')) {
1895 stmt
->type
= STMT_COMPOUND
;
1896 start_symbol_scope();
1897 token
= compound_statement(token
->next
, stmt
);
1900 return expect(token
, '}', "at end of compound statement");
1903 stmt
->type
= STMT_EXPRESSION
;
1904 return expression_statement(token
, &stmt
->expression
);
1907 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1909 int seen_statement
= 0;
1911 struct statement
* stmt
;
1912 if (eof_token(token
))
1914 if (match_op(token
, '}'))
1916 if (lookup_type(token
)) {
1917 if (seen_statement
) {
1918 warning(token
->pos
, "mixing declarations and code");
1921 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1922 token
= external_declaration(token
, &stmt
->declaration
);
1924 seen_statement
= Wdeclarationafterstatement
;
1925 token
= statement(token
, &stmt
);
1927 add_statement(list
, stmt
);
1932 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
1934 struct symbol_list
**list
= &fn
->arguments
;
1936 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1937 sym
->ident
= token
->ident
;
1938 token
= token
->next
;
1939 sym
->endpos
= token
->pos
;
1940 add_symbol(list
, sym
);
1941 if (!match_op(token
, ',') ||
1942 token_type(token
->next
) != TOKEN_IDENT
||
1943 lookup_type(token
->next
))
1945 token
= token
->next
;
1950 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1952 struct symbol_list
**list
= &fn
->arguments
;
1957 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1959 token
= token
->next
;
1963 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1964 token
= parameter_declaration(token
, sym
);
1965 if (sym
->ctype
.base_type
== &void_ctype
) {
1966 /* Special case: (void) */
1967 if (!*list
&& !sym
->ident
)
1969 warning(token
->pos
, "void parameter");
1971 add_symbol(list
, sym
);
1972 if (!match_op(token
, ','))
1974 token
= token
->next
;
1979 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
1981 token
= statement_list(token
, &stmt
->stmts
);
1985 static struct expression
*identifier_expression(struct token
*token
)
1987 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
1988 expr
->expr_ident
= token
->ident
;
1992 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
1994 int idx_from
, idx_to
;
1995 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
1997 idx_from
= const_expression_value(from
);
2000 idx_to
= const_expression_value(to
);
2001 if (idx_to
< idx_from
|| idx_from
< 0)
2002 warning(from
->pos
, "nonsense array initializer index range");
2004 expr
->idx_from
= idx_from
;
2005 expr
->idx_to
= idx_to
;
2009 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2011 int expect_equal
= 0;
2012 struct token
*next
= token
->next
;
2013 struct expression
**tail
= ep
;
2018 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2019 struct expression
*expr
= identifier_expression(token
);
2020 if (Wold_initializer
)
2021 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2022 token
= initializer(&expr
->ident_expression
, next
->next
);
2023 if (expr
->ident_expression
)
2028 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2029 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2030 struct expression
*expr
= identifier_expression(next
);
2032 tail
= &expr
->ident_expression
;
2035 } else if (match_op(token
, '[')) {
2036 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2037 token
= constant_expression(token
->next
, &from
);
2039 sparse_error(token
->pos
, "Expected constant expression");
2042 if (match_op(token
, SPECIAL_ELLIPSIS
))
2043 token
= constant_expression(token
->next
, &to
);
2044 expr
= index_expression(from
, to
);
2046 tail
= &expr
->idx_expression
;
2047 token
= expect(token
, ']', "at end of initializer index");
2054 if (nested
&& !expect_equal
) {
2055 if (!match_op(token
, '='))
2056 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2061 token
= expect(token
, '=', "at end of initializer index");
2063 token
= initializer(tail
, token
);
2069 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2071 struct expression
*expr
;
2074 token
= single_initializer(&expr
, token
);
2077 add_expression(list
, expr
);
2078 if (!match_op(token
, ','))
2080 token
= token
->next
;
2085 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2087 if (match_op(token
, '{')) {
2088 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2090 token
= initializer_list(&expr
->expr_list
, token
->next
);
2091 return expect(token
, '}', "at end of initializer");
2093 return assignment_expression(token
, tree
);
2096 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2099 sparse_error(sym
->pos
, "no identifier for function argument");
2102 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2105 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2106 struct symbol_list
**list
)
2108 struct symbol_list
**old_symbol_list
;
2109 struct symbol
*base_type
= decl
->ctype
.base_type
;
2110 struct statement
*stmt
, **p
;
2111 struct symbol
*prev
;
2114 old_symbol_list
= function_symbol_list
;
2115 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2116 function_symbol_list
= &decl
->inline_symbol_list
;
2117 p
= &base_type
->inline_stmt
;
2119 function_symbol_list
= &decl
->symbol_list
;
2120 p
= &base_type
->stmt
;
2122 function_computed_target_list
= NULL
;
2123 function_computed_goto_list
= NULL
;
2125 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2126 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2127 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2129 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2130 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2132 stmt
= start_function(decl
);
2135 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2136 declare_argument(arg
, base_type
);
2137 } END_FOR_EACH_PTR(arg
);
2139 token
= compound_statement(token
->next
, stmt
);
2142 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2143 add_symbol(list
, decl
);
2144 check_declaration(decl
);
2145 decl
->definition
= decl
;
2146 prev
= decl
->same_symbol
;
2147 if (prev
&& prev
->definition
) {
2148 warning(decl
->pos
, "multiple definitions for function '%s'",
2149 show_ident(decl
->ident
));
2150 info(prev
->definition
->pos
, " the previous one is here");
2153 prev
->definition
= decl
;
2154 prev
= prev
->same_symbol
;
2157 function_symbol_list
= old_symbol_list
;
2158 if (function_computed_goto_list
) {
2159 if (!function_computed_target_list
)
2160 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2162 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2163 stmt
->target_list
= function_computed_target_list
;
2164 } END_FOR_EACH_PTR(stmt
);
2167 return expect(token
, '}', "at end of function");
2170 static void promote_k_r_types(struct symbol
*arg
)
2172 struct symbol
*base
= arg
->ctype
.base_type
;
2173 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2174 arg
->ctype
.base_type
= &int_ctype
;
2178 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2180 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2183 FOR_EACH_PTR(real_args
, arg
) {
2184 struct symbol
*type
;
2186 /* This is quadratic in the number of arguments. We _really_ don't care */
2187 FOR_EACH_PTR(argtypes
, type
) {
2188 if (type
->ident
== arg
->ident
)
2190 } END_FOR_EACH_PTR(type
);
2191 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2195 /* "char" and "short" promote to "int" */
2196 promote_k_r_types(type
);
2198 arg
->ctype
= type
->ctype
;
2199 } END_FOR_EACH_PTR(arg
);
2201 FOR_EACH_PTR(argtypes
, arg
) {
2203 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2204 } END_FOR_EACH_PTR(arg
);
2208 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2209 struct symbol_list
**list
)
2211 struct symbol_list
*args
= NULL
;
2213 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2215 token
= declaration_list(token
, &args
);
2216 if (!match_op(token
, ';')) {
2217 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2220 token
= token
->next
;
2221 } while (lookup_type(token
));
2223 apply_k_r_types(args
, decl
);
2225 if (!match_op(token
, '{')) {
2226 sparse_error(token
->pos
, "expected function body");
2229 return parse_function_body(token
, decl
, list
);
2232 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2234 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2235 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2236 struct statement
*stmt
;
2238 anon
->ctype
.base_type
= fn
;
2239 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2242 token
= parse_asm_statement(token
, stmt
);
2244 add_symbol(list
, anon
);
2248 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2250 struct ident
*ident
= NULL
;
2251 struct symbol
*decl
;
2252 struct decl_state ctx
= { .ident
= &ident
};
2254 struct symbol
*base_type
;
2257 /* Top-level inline asm? */
2258 if (token_type(token
) == TOKEN_IDENT
) {
2259 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2260 if (s
&& s
->op
->toplevel
)
2261 return s
->op
->toplevel(token
, list
);
2264 /* Parse declaration-specifiers, if any */
2265 token
= declaration_specifiers(token
, &ctx
, 0);
2266 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2267 /* Just a type declaration? */
2268 if (match_op(token
, ';')) {
2269 apply_modifiers(token
->pos
, &ctx
.ctype
);
2274 token
= declarator(token
, &ctx
);
2275 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
| KW_ASM
);
2276 apply_modifiers(token
->pos
, &ctx
.ctype
);
2278 decl
->ctype
= ctx
.ctype
;
2279 decl
->endpos
= token
->pos
;
2281 /* Just a type declaration? */
2283 warning(token
->pos
, "missing identifier in declaration");
2284 return expect(token
, ';', "at the end of type declaration");
2287 /* type define declaration? */
2288 is_typedef
= (saved
.modifiers
& MOD_TYPEDEF
) != 0;
2290 /* Typedefs don't have meaningful storage */
2292 saved
.modifiers
&= ~MOD_STORAGE
;
2293 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
2294 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2297 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2299 base_type
= decl
->ctype
.base_type
;
2302 if (base_type
&& !base_type
->ident
) {
2303 switch (base_type
->type
) {
2308 base_type
->ident
= ident
;
2311 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2312 /* K&R argument declaration? */
2313 if (lookup_type(token
))
2314 return parse_k_r_arguments(token
, decl
, list
);
2315 if (match_op(token
, '{'))
2316 return parse_function_body(token
, decl
, list
);
2318 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2319 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2320 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2321 sparse_error(token
->pos
, "void declaration");
2325 if (!is_typedef
&& match_op(token
, '=')) {
2326 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2327 warning(decl
->pos
, "symbol with external linkage has initializer");
2328 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2330 token
= initializer(&decl
->initializer
, token
->next
);
2333 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2334 add_symbol(list
, decl
);
2335 fn_local_symbol(decl
);
2338 check_declaration(decl
);
2339 if (decl
->same_symbol
)
2340 decl
->definition
= decl
->same_symbol
->definition
;
2342 if (!match_op(token
, ','))
2345 token
= token
->next
;
2347 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2349 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
);
2350 token
= declarator(token
, &ctx
);
2351 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
| KW_ASM
);
2352 apply_modifiers(token
->pos
, &ctx
.ctype
);
2353 decl
->ctype
= ctx
.ctype
;
2354 decl
->endpos
= token
->pos
;
2356 sparse_error(token
->pos
, "expected identifier name in type definition");
2360 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2362 /* Function declarations are automatically extern unless specifically static */
2363 base_type
= decl
->ctype
.base_type
;
2364 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2365 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2366 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2369 return expect(token
, ';', "at end of declaration");