Let void have sizeof 1
[smatch.git] / parse.c
blobeb3187124b84a769932721ee9e192cedc956dfcf
1 /*
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
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
22 #include "lib.h"
23 #include "allocate.h"
24 #include "token.h"
25 #include "parse.h"
26 #include "symbol.h"
27 #include "scope.h"
28 #include "expression.h"
29 #include "target.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_conditional_context(struct token *token, struct symbol *attr, struct ctype *ctype);
68 static struct token *attribute_exact_context(struct token *token, struct symbol *attr, struct ctype *ctype);
69 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype);
70 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype);
73 static struct symbol_op modifier_op = {
74 .type = KW_MODIFIER,
77 static struct symbol_op qualifier_op = {
78 .type = KW_QUALIFIER,
81 static struct symbol_op typeof_op = {
82 .type = KW_TYPEOF,
83 .declarator = typeof_specifier,
86 static struct symbol_op attribute_op = {
87 .type = KW_ATTRIBUTE,
88 .declarator = attribute_specifier,
91 static struct symbol_op struct_op = {
92 .type = KW_SPECIFIER,
93 .declarator = struct_specifier,
96 static struct symbol_op union_op = {
97 .type = KW_SPECIFIER,
98 .declarator = union_specifier,
101 static struct symbol_op enum_op = {
102 .type = KW_SPECIFIER,
103 .declarator = enum_specifier,
108 static struct symbol_op if_op = {
109 .statement = parse_if_statement,
112 static struct symbol_op return_op = {
113 .statement = parse_return_statement,
116 static struct symbol_op loop_iter_op = {
117 .statement = parse_loop_iterator,
120 static struct symbol_op default_op = {
121 .statement = parse_default_statement,
124 static struct symbol_op case_op = {
125 .statement = parse_case_statement,
128 static struct symbol_op switch_op = {
129 .statement = parse_switch_statement,
132 static struct symbol_op for_op = {
133 .statement = parse_for_statement,
136 static struct symbol_op while_op = {
137 .statement = parse_while_statement,
140 static struct symbol_op do_op = {
141 .statement = parse_do_statement,
144 static struct symbol_op goto_op = {
145 .statement = parse_goto_statement,
148 static struct symbol_op __context___op = {
149 .statement = parse_context_statement,
152 static struct symbol_op range_op = {
153 .statement = parse_range_statement,
156 static struct symbol_op asm_op = {
157 .type = KW_ASM,
158 .declarator = parse_asm_declarator,
159 .statement = parse_asm_statement,
160 .toplevel = toplevel_asm_declaration,
163 static struct symbol_op packed_op = {
164 .attribute = attribute_packed,
167 static struct symbol_op aligned_op = {
168 .attribute = attribute_aligned,
171 static struct symbol_op attr_mod_op = {
172 .attribute = attribute_modifier,
175 static struct symbol_op address_space_op = {
176 .attribute = attribute_address_space,
179 static struct symbol_op mode_op = {
180 .attribute = attribute_mode,
183 static struct symbol_op context_op = {
184 .attribute = attribute_context,
187 static struct symbol_op conditional_context_op = {
188 .attribute = attribute_conditional_context,
191 static struct symbol_op exact_context_op = {
192 .attribute = attribute_exact_context,
195 static struct symbol_op transparent_union_op = {
196 .attribute = attribute_transparent_union,
199 static struct symbol_op ignore_attr_op = {
200 .attribute = ignore_attribute,
203 static struct symbol_op mode_spec_op = {
204 .type = KW_MODE,
207 static struct init_keyword {
208 const char *name;
209 enum namespace ns;
210 unsigned long modifiers;
211 struct symbol_op *op;
212 } keyword_table[] = {
213 /* Type qualifiers */
214 { "const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
215 { "__const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
216 { "__const__", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
217 { "volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
218 { "__volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
219 { "__volatile__", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
221 /* Typedef.. */
222 { "typedef", NS_TYPEDEF, MOD_TYPEDEF, .op = &modifier_op },
224 /* Extended types */
225 { "typeof", NS_TYPEDEF, .op = &typeof_op },
226 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
227 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
229 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
230 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
232 { "struct", NS_TYPEDEF, .op = &struct_op },
233 { "union", NS_TYPEDEF, .op = &union_op },
234 { "enum", NS_TYPEDEF, .op = &enum_op },
236 { "inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
237 { "__inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
238 { "__inline__", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
240 /* Ignored for now.. */
241 { "restrict", NS_TYPEDEF, .op = &qualifier_op},
242 { "__restrict", NS_TYPEDEF, .op = &qualifier_op},
244 /* Statement */
245 { "if", NS_KEYWORD, .op = &if_op },
246 { "return", NS_KEYWORD, .op = &return_op },
247 { "break", NS_KEYWORD, .op = &loop_iter_op },
248 { "continue", NS_KEYWORD, .op = &loop_iter_op },
249 { "default", NS_KEYWORD, .op = &default_op },
250 { "case", NS_KEYWORD, .op = &case_op },
251 { "switch", NS_KEYWORD, .op = &switch_op },
252 { "for", NS_KEYWORD, .op = &for_op },
253 { "while", NS_KEYWORD, .op = &while_op },
254 { "do", NS_KEYWORD, .op = &do_op },
255 { "goto", NS_KEYWORD, .op = &goto_op },
256 { "__context__",NS_KEYWORD, .op = &__context___op },
257 { "__range__", NS_KEYWORD, .op = &range_op },
258 { "asm", NS_KEYWORD, .op = &asm_op },
259 { "__asm", NS_KEYWORD, .op = &asm_op },
260 { "__asm__", NS_KEYWORD, .op = &asm_op },
262 /* Attribute */
263 { "packed", NS_KEYWORD, .op = &packed_op },
264 { "__packed__", NS_KEYWORD, .op = &packed_op },
265 { "aligned", NS_KEYWORD, .op = &aligned_op },
266 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
267 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
268 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
269 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
270 { "force", NS_KEYWORD, MOD_FORCE, .op = &attr_mod_op },
271 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
272 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
273 { "address_space",NS_KEYWORD, .op = &address_space_op },
274 { "mode", NS_KEYWORD, .op = &mode_op },
275 { "context", NS_KEYWORD, .op = &context_op },
276 { "conditional_context", NS_KEYWORD, .op = &conditional_context_op },
277 { "exact_context", NS_KEYWORD, .op = &exact_context_op },
278 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
280 { "__mode__", NS_KEYWORD, .op = &mode_op },
281 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
282 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
283 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
284 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
285 { "SI", NS_KEYWORD, .op = &mode_spec_op },
286 { "__SI__", NS_KEYWORD, .op = &mode_spec_op },
287 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
288 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
289 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
290 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
292 /* Ignored attributes */
293 { "nothrow", NS_KEYWORD, .op = &ignore_attr_op },
294 { "__nothrow", NS_KEYWORD, .op = &ignore_attr_op },
295 { "__nothrow__", NS_KEYWORD, .op = &ignore_attr_op },
296 { "malloc", NS_KEYWORD, .op = &ignore_attr_op },
297 { "__malloc__", NS_KEYWORD, .op = &ignore_attr_op },
298 { "nonnull", NS_KEYWORD, .op = &ignore_attr_op },
299 { "__nonnull", NS_KEYWORD, .op = &ignore_attr_op },
300 { "__nonnull__", NS_KEYWORD, .op = &ignore_attr_op },
301 { "format", NS_KEYWORD, .op = &ignore_attr_op },
302 { "__format__", NS_KEYWORD, .op = &ignore_attr_op },
303 { "format_arg", NS_KEYWORD, .op = &ignore_attr_op },
304 { "__format_arg__", NS_KEYWORD, .op = &ignore_attr_op },
305 { "section", NS_KEYWORD, .op = &ignore_attr_op },
306 { "__section__",NS_KEYWORD, .op = &ignore_attr_op },
307 { "unused", NS_KEYWORD, .op = &ignore_attr_op },
308 { "__unused__", NS_KEYWORD, .op = &ignore_attr_op },
309 { "const", NS_KEYWORD, .op = &ignore_attr_op },
310 { "__const", NS_KEYWORD, .op = &ignore_attr_op },
311 { "__const__", NS_KEYWORD, .op = &ignore_attr_op },
312 { "noreturn", NS_KEYWORD, .op = &ignore_attr_op },
313 { "__noreturn__", NS_KEYWORD, .op = &ignore_attr_op },
314 { "no_instrument_function", NS_KEYWORD, .op = &ignore_attr_op },
315 { "__no_instrument_function__", NS_KEYWORD, .op = &ignore_attr_op },
316 { "sentinel", NS_KEYWORD, .op = &ignore_attr_op },
317 { "__sentinel__", NS_KEYWORD, .op = &ignore_attr_op },
318 { "regparm", NS_KEYWORD, .op = &ignore_attr_op },
319 { "__regparm__", NS_KEYWORD, .op = &ignore_attr_op },
320 { "weak", NS_KEYWORD, .op = &ignore_attr_op },
321 { "__weak__", NS_KEYWORD, .op = &ignore_attr_op },
322 { "alias", NS_KEYWORD, .op = &ignore_attr_op },
323 { "__alias__", NS_KEYWORD, .op = &ignore_attr_op },
324 { "pure", NS_KEYWORD, .op = &ignore_attr_op },
325 { "__pure__", NS_KEYWORD, .op = &ignore_attr_op },
326 { "always_inline", NS_KEYWORD, .op = &ignore_attr_op },
327 { "__always_inline__", NS_KEYWORD, .op = &ignore_attr_op },
328 { "syscall_linkage", NS_KEYWORD, .op = &ignore_attr_op },
329 { "__syscall_linkage__", NS_KEYWORD, .op = &ignore_attr_op },
330 { "visibility", NS_KEYWORD, .op = &ignore_attr_op },
331 { "__visibility__", NS_KEYWORD, .op = &ignore_attr_op },
332 { "deprecated", NS_KEYWORD, .op = &ignore_attr_op },
333 { "__deprecated__", NS_KEYWORD, .op = &ignore_attr_op },
334 { "noinline", NS_KEYWORD, .op = &ignore_attr_op },
335 { "__noinline__", NS_KEYWORD, .op = &ignore_attr_op },
336 { "used", NS_KEYWORD, .op = &ignore_attr_op },
337 { "__used__", NS_KEYWORD, .op = &ignore_attr_op },
338 { "warn_unused_result", NS_KEYWORD, .op = &ignore_attr_op },
339 { "__warn_unused_result__", NS_KEYWORD, .op = &ignore_attr_op },
340 { "model", NS_KEYWORD, .op = &ignore_attr_op },
341 { "__model__", NS_KEYWORD, .op = &ignore_attr_op },
342 { "cdecl", NS_KEYWORD, .op = &ignore_attr_op },
343 { "__cdecl__", NS_KEYWORD, .op = &ignore_attr_op },
344 { "stdcall", NS_KEYWORD, .op = &ignore_attr_op },
345 { "__stdcall__", NS_KEYWORD, .op = &ignore_attr_op },
346 { "fastcall", NS_KEYWORD, .op = &ignore_attr_op },
347 { "__fastcall__", NS_KEYWORD, .op = &ignore_attr_op },
348 { "dllimport", NS_KEYWORD, .op = &ignore_attr_op },
349 { "__dllimport__", NS_KEYWORD, .op = &ignore_attr_op },
350 { "dllexport", NS_KEYWORD, .op = &ignore_attr_op },
351 { "__dllexport__", NS_KEYWORD, .op = &ignore_attr_op },
352 { "constructor", NS_KEYWORD, .op = &ignore_attr_op },
353 { "__constructor__", NS_KEYWORD, .op = &ignore_attr_op },
354 { "destructor", NS_KEYWORD, .op = &ignore_attr_op },
355 { "__destructor__", NS_KEYWORD, .op = &ignore_attr_op },
356 { "cold", NS_KEYWORD, .op = &ignore_attr_op },
357 { "__cold__", NS_KEYWORD, .op = &ignore_attr_op },
358 { "hot", NS_KEYWORD, .op = &ignore_attr_op },
359 { "__hot__", NS_KEYWORD, .op = &ignore_attr_op },
362 void init_parser(int stream)
364 int i;
365 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
366 struct init_keyword *ptr = keyword_table + i;
367 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
368 sym->ident->keyword = 1;
369 sym->ctype.modifiers = ptr->modifiers;
370 sym->op = ptr->op;
374 // Add a symbol to the list of function-local symbols
375 static void fn_local_symbol(struct symbol *sym)
377 if (function_symbol_list)
378 add_symbol(function_symbol_list, sym);
381 static int SENTINEL_ATTR match_idents(struct token *token, ...)
383 va_list args;
384 struct ident * next;
386 if (token_type(token) != TOKEN_IDENT)
387 return 0;
389 va_start(args, token);
390 do {
391 next = va_arg(args, struct ident *);
392 } while (next && token->ident != next);
393 va_end(args);
395 return next && token->ident == next;
399 struct statement *alloc_statement(struct position pos, int type)
401 struct statement *stmt = __alloc_statement(0);
402 stmt->type = type;
403 stmt->pos = pos;
404 return stmt;
407 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
409 static int apply_modifiers(struct position pos, struct ctype *ctype)
411 struct symbol *base;
413 while ((base = ctype->base_type)) {
414 switch (base->type) {
415 case SYM_FN:
416 case SYM_ENUM:
417 case SYM_ARRAY:
418 case SYM_BITFIELD:
419 case SYM_PTR:
420 ctype = &base->ctype;
421 continue;
423 break;
426 /* Turn the "virtual types" into real types with real sizes etc */
427 if (ctype->base_type == &int_type) {
428 ctype->base_type = ctype_integer(ctype->modifiers);
429 ctype->modifiers &= ~MOD_SPECIFIER;
430 } else if (ctype->base_type == &fp_type) {
431 ctype->base_type = ctype_fp(ctype->modifiers);
432 ctype->modifiers &= ~MOD_SPECIFIER;
435 if (ctype->modifiers & MOD_BITWISE) {
436 struct symbol *type;
437 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
438 if (!is_int_type(ctype->base_type)) {
439 sparse_error(pos, "invalid modifier");
440 return 1;
442 type = alloc_symbol(pos, SYM_BASETYPE);
443 *type = *ctype->base_type;
444 type->ctype.base_type = ctype->base_type;
445 type->type = SYM_RESTRICT;
446 type->ctype.modifiers &= ~MOD_SPECIFIER;
447 ctype->base_type = type;
448 create_fouled(type);
450 return 0;
453 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
455 struct symbol *sym = alloc_symbol(pos, type);
457 sym->ctype.base_type = ctype->base_type;
458 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
460 ctype->base_type = sym;
461 ctype->modifiers &= MOD_STORAGE;
462 return sym;
465 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
467 struct symbol *sym = lookup_symbol(token->ident, ns);
468 if (!sym) {
469 sym = alloc_symbol(token->pos, type);
470 bind_symbol(sym, token->ident, ns);
471 if (type == SYM_LABEL)
472 fn_local_symbol(sym);
474 return sym;
477 static struct symbol * local_label(struct token *token)
479 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL);
481 if (sym && sym->ctype.modifiers & MOD_LABEL)
482 return sym;
484 return NULL;
488 * NOTE! NS_LABEL is not just a different namespace,
489 * it also ends up using function scope instead of the
490 * regular symbol scope.
492 struct symbol *label_symbol(struct token *token)
494 struct symbol *sym = local_label(token);
495 if (sym)
496 return sym;
497 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
500 static struct token *struct_union_enum_specifier(enum type type,
501 struct token *token, struct ctype *ctype,
502 struct token *(*parse)(struct token *, struct symbol *))
504 struct symbol *sym;
505 struct position *repos;
507 ctype->modifiers = 0;
508 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
509 if (token_type(token) == TOKEN_IDENT) {
510 sym = lookup_symbol(token->ident, NS_STRUCT);
511 if (!sym ||
512 (is_outer_scope(sym->scope) &&
513 (match_op(token->next,';') || match_op(token->next,'{')))) {
514 // Either a new symbol, or else an out-of-scope
515 // symbol being redefined.
516 sym = alloc_symbol(token->pos, type);
517 bind_symbol(sym, token->ident, NS_STRUCT);
519 if (sym->type != type)
520 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
521 ctype->base_type = sym;
522 repos = &token->pos;
523 token = token->next;
524 if (match_op(token, '{')) {
525 // The following test is actually wrong for empty
526 // structs, but (1) they are not C99, (2) gcc does
527 // the same thing, and (3) it's easier.
528 if (sym->symbol_list)
529 error_die(token->pos, "redefinition of %s", show_typename (sym));
530 sym->pos = *repos;
531 token = parse(token->next, sym);
532 token = expect(token, '}', "at end of struct-union-enum-specifier");
534 // Mark the structure as needing re-examination
535 sym->examined = 0;
536 sym->endpos = token->pos;
538 return token;
541 // private struct/union/enum type
542 if (!match_op(token, '{')) {
543 sparse_error(token->pos, "expected declaration");
544 ctype->base_type = &bad_ctype;
545 return token;
548 sym = alloc_symbol(token->pos, type);
549 token = parse(token->next, sym);
550 ctype->base_type = sym;
551 token = expect(token, '}', "at end of specifier");
552 sym->endpos = token->pos;
554 return token;
557 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
559 struct symbol *field, *last = NULL;
560 struct token *res;
561 res = struct_declaration_list(token, &sym->symbol_list);
562 FOR_EACH_PTR(sym->symbol_list, field) {
563 if (!field->ident) {
564 struct symbol *base = field->ctype.base_type;
565 if (base && base->type == SYM_BITFIELD)
566 continue;
568 if (last)
569 last->next_subobject = field;
570 last = field;
571 } END_FOR_EACH_PTR(field);
572 return res;
575 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
577 return struct_declaration_list(token, &sym->symbol_list);
580 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
582 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
585 static struct token *union_specifier(struct token *token, struct ctype *ctype)
587 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_union_declaration);
591 typedef struct {
592 int x;
593 unsigned long long y;
594 } Num;
596 static void upper_boundary(Num *n, Num *v)
598 if (n->x > v->x)
599 return;
600 if (n->x < v->x) {
601 *n = *v;
602 return;
604 if (n->y < v->y)
605 n->y = v->y;
608 static void lower_boundary(Num *n, Num *v)
610 if (n->x < v->x)
611 return;
612 if (n->x > v->x) {
613 *n = *v;
614 return;
616 if (n->y > v->y)
617 n->y = v->y;
620 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
622 int shift = type->bit_size;
623 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
625 if (!is_unsigned)
626 shift--;
627 if (upper->x == 0 && upper->y >> shift)
628 return 0;
629 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
630 return 1;
631 return 0;
634 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
636 if (s1->bit_size < s2->bit_size) {
637 s1 = s2;
638 } else if (s1->bit_size == s2->bit_size) {
639 if (s2->ctype.modifiers & MOD_UNSIGNED)
640 s1 = s2;
642 if (s1->bit_size < bits_in_int)
643 return &int_ctype;
644 return s1;
647 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
649 struct symbol *sym;
651 FOR_EACH_PTR(list, sym) {
652 struct expression *expr = sym->initializer;
653 struct symbol *ctype;
654 if (expr->type != EXPR_VALUE)
655 continue;
656 ctype = expr->ctype;
657 if (ctype->bit_size == base_type->bit_size)
658 continue;
659 cast_value(expr, base_type, expr, ctype);
660 } END_FOR_EACH_PTR(sym);
663 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
665 unsigned long long lastval = 0;
666 struct symbol *ctype = NULL, *base_type = NULL;
667 Num upper = {-1, 0}, lower = {1, 0};
669 parent->examined = 1;
670 parent->ctype.base_type = &int_ctype;
671 while (token_type(token) == TOKEN_IDENT) {
672 struct expression *expr = NULL;
673 struct token *next = token->next;
674 struct symbol *sym;
676 sym = alloc_symbol(token->pos, SYM_NODE);
677 bind_symbol(sym, token->ident, NS_SYMBOL);
678 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
680 if (match_op(next, '=')) {
681 next = constant_expression(next->next, &expr);
682 lastval = get_expression_value(expr);
683 ctype = &void_ctype;
684 if (expr && expr->ctype)
685 ctype = expr->ctype;
686 } else if (!ctype) {
687 ctype = &int_ctype;
688 } else if (is_int_type(ctype)) {
689 lastval++;
690 } else {
691 error_die(token->pos, "can't increment the last enum member");
694 if (!expr) {
695 expr = alloc_expression(token->pos, EXPR_VALUE);
696 expr->value = lastval;
697 expr->ctype = ctype;
700 sym->initializer = expr;
701 sym->enum_member = 1;
702 sym->ctype.base_type = parent;
703 add_ptr_list(&parent->symbol_list, sym);
705 if (base_type != &bad_ctype) {
706 if (ctype->type == SYM_NODE)
707 ctype = ctype->ctype.base_type;
708 if (ctype->type == SYM_ENUM) {
709 if (ctype == parent)
710 ctype = base_type;
711 else
712 ctype = ctype->ctype.base_type;
715 * base_type rules:
716 * - if all enums are of the same type, then
717 * the base_type is that type (two first
718 * cases)
719 * - if enums are of different types, they
720 * all have to be integer types, and the
721 * base type is at least "int_ctype".
722 * - otherwise the base_type is "bad_ctype".
724 if (!base_type) {
725 base_type = ctype;
726 } else if (ctype == base_type) {
727 /* nothing */
728 } else if (is_int_type(base_type) && is_int_type(ctype)) {
729 base_type = bigger_enum_type(base_type, ctype);
730 } else
731 base_type = &bad_ctype;
732 parent->ctype.base_type = base_type;
734 if (is_int_type(base_type)) {
735 Num v = {.y = lastval};
736 if (ctype->ctype.modifiers & MOD_UNSIGNED)
737 v.x = 0;
738 else if ((long long)lastval >= 0)
739 v.x = 0;
740 else
741 v.x = -1;
742 upper_boundary(&upper, &v);
743 lower_boundary(&lower, &v);
745 token = next;
747 sym->endpos = token->pos;
749 if (!match_op(token, ','))
750 break;
751 token = token->next;
753 if (!base_type) {
754 sparse_error(token->pos, "bad enum definition");
755 base_type = &bad_ctype;
757 else if (!is_int_type(base_type))
758 base_type = base_type;
759 else if (type_is_ok(base_type, &upper, &lower))
760 base_type = base_type;
761 else if (type_is_ok(&int_ctype, &upper, &lower))
762 base_type = &int_ctype;
763 else if (type_is_ok(&uint_ctype, &upper, &lower))
764 base_type = &uint_ctype;
765 else if (type_is_ok(&long_ctype, &upper, &lower))
766 base_type = &long_ctype;
767 else if (type_is_ok(&ulong_ctype, &upper, &lower))
768 base_type = &ulong_ctype;
769 else if (type_is_ok(&llong_ctype, &upper, &lower))
770 base_type = &llong_ctype;
771 else if (type_is_ok(&ullong_ctype, &upper, &lower))
772 base_type = &ullong_ctype;
773 else
774 base_type = &bad_ctype;
775 parent->ctype.base_type = base_type;
776 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
777 parent->examined = 0;
779 cast_enum_list(parent->symbol_list, base_type);
781 return token;
784 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
786 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
788 ctype = &ctype->base_type->ctype;
789 if (!ctype->base_type)
790 ctype->base_type = &incomplete_ctype;
792 return ret;
795 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
797 struct symbol *sym;
799 if (!match_op(token, '(')) {
800 sparse_error(token->pos, "expected '(' after typeof");
801 return token;
803 if (lookup_type(token->next)) {
804 token = typename(token->next, &sym, 0);
805 *ctype = sym->ctype;
806 } else {
807 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
808 token = parse_expression(token->next, &typeof_sym->initializer);
810 ctype->modifiers = 0;
811 typeof_sym->endpos = token->pos;
812 ctype->base_type = typeof_sym;
814 return expect(token, ')', "after typeof");
817 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
819 struct expression *expr = NULL;
820 if (match_op(token, '('))
821 token = parens_expression(token, &expr, "in attribute");
822 return token;
825 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
827 ctype->alignment = 1;
828 return token;
831 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
833 int alignment = max_alignment;
834 struct expression *expr = NULL;
836 if (match_op(token, '(')) {
837 token = parens_expression(token, &expr, "in attribute");
838 if (expr)
839 alignment = const_expression_value(expr);
841 ctype->alignment = alignment;
842 return token;
845 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
847 ctype->modifiers |= attr->ctype.modifiers;
848 return token;
851 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
853 struct expression *expr = NULL;
854 token = expect(token, '(', "after address_space attribute");
855 token = conditional_expression(token, &expr);
856 if (expr)
857 ctype->as = const_expression_value(expr);
858 token = expect(token, ')', "after address_space attribute");
859 return token;
862 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
864 token = expect(token, '(', "after mode attribute");
865 if (token_type(token) == TOKEN_IDENT) {
866 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
867 if (mode && mode->op->type == KW_MODE)
868 ctype->modifiers |= mode->ctype.modifiers;
869 else
870 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
871 token = token->next;
872 } else
873 sparse_error(token->pos, "expect attribute mode symbol\n");
874 token = expect(token, ')', "after mode attribute");
875 return token;
878 static struct token *_attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype, int exact)
880 struct context *context = alloc_context();
881 struct expression *args[3];
882 int argc = 0;
884 token = expect(token, '(', "after context attribute");
885 while (!match_op(token, ')')) {
886 struct expression *expr = NULL;
887 token = conditional_expression(token, &expr);
888 if (!expr)
889 break;
890 if (argc < 3)
891 args[argc++] = expr;
892 else
893 argc++;
894 if (!match_op(token, ','))
895 break;
896 token = token->next;
899 switch(argc) {
900 case 0:
901 sparse_error(token->pos, "expected context input/output values");
902 break;
903 case 1:
904 context->in = get_expression_value(args[0]);
905 break;
906 case 2:
907 context->in = get_expression_value(args[0]);
908 context->out = get_expression_value(args[1]);
909 break;
910 case 3:
911 context->context = args[0];
912 context->in = get_expression_value(args[1]);
913 context->out = get_expression_value(args[2]);
914 break;
915 default:
916 sparse_error(token->pos, "too many arguments to context attribute");
917 break;
920 context->exact = exact;
921 context->out_false = context->out;
923 if (argc)
924 add_ptr_list(&ctype->contexts, context);
926 token = expect(token, ')', "after context attribute");
927 return token;
930 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
932 return _attribute_context(token, attr, ctype, 0);
935 static struct token *attribute_exact_context(struct token *token, struct symbol *attr, struct ctype *ctype)
937 return _attribute_context(token, attr, ctype, 1);
940 static struct token *attribute_conditional_context(struct token *token, struct symbol *attr, struct ctype *ctype)
942 struct context *context = alloc_context();
943 struct expression *args[4];
944 int argc = 0;
946 token = expect(token, '(', "after conditional_context attribute");
947 while (!match_op(token, ')')) {
948 struct expression *expr = NULL;
949 token = conditional_expression(token, &expr);
950 if (!expr)
951 break;
952 if (argc < 4)
953 args[argc++] = expr;
954 else
955 argc++;
956 if (!match_op(token, ','))
957 break;
958 token = token->next;
961 switch(argc) {
962 case 3:
963 context->in = get_expression_value(args[0]);
964 context->out = get_expression_value(args[1]);
965 context->out_false = get_expression_value(args[2]);
966 break;
967 case 4:
968 context->context = args[0];
969 context->in = get_expression_value(args[1]);
970 context->out = get_expression_value(args[2]);
971 context->out_false = get_expression_value(args[3]);
972 break;
973 default:
974 sparse_error(token->pos, "invalid number of arguments to conditional_context attribute");
975 break;
978 if (argc)
979 add_ptr_list(&ctype->contexts, context);
981 token = expect(token, ')', "after conditional_context attribute");
982 return token;
985 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
987 if (Wtransparent_union)
988 warning(token->pos, "ignoring attribute __transparent_union__");
989 return token;
992 static struct token *recover_unknown_attribute(struct token *token)
994 struct expression *expr = NULL;
996 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
997 token = token->next;
998 if (match_op(token, '('))
999 token = parens_expression(token, &expr, "in attribute");
1000 return token;
1003 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
1005 ctype->modifiers = 0;
1006 token = expect(token, '(', "after attribute");
1007 token = expect(token, '(', "after attribute");
1009 for (;;) {
1010 struct ident *attribute_name;
1011 struct symbol *attr;
1013 if (eof_token(token))
1014 break;
1015 if (match_op(token, ';'))
1016 break;
1017 if (token_type(token) != TOKEN_IDENT)
1018 break;
1019 attribute_name = token->ident;
1020 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1021 if (attr && attr->op->attribute)
1022 token = attr->op->attribute(token->next, attr, ctype);
1023 else
1024 token = recover_unknown_attribute(token);
1026 if (!match_op(token, ','))
1027 break;
1028 token = token->next;
1031 token = expect(token, ')', "after attribute");
1032 token = expect(token, ')', "after attribute");
1033 return token;
1036 struct symbol * ctype_integer(unsigned long spec)
1038 static struct symbol *const integer_ctypes[][3] = {
1039 { &llong_ctype, &sllong_ctype, &ullong_ctype },
1040 { &long_ctype, &slong_ctype, &ulong_ctype },
1041 { &short_ctype, &sshort_ctype, &ushort_ctype },
1042 { &char_ctype, &schar_ctype, &uchar_ctype },
1043 { &int_ctype, &sint_ctype, &uint_ctype },
1045 struct symbol *const (*ctype)[3];
1046 int sub;
1048 ctype = integer_ctypes;
1049 if (!(spec & MOD_LONGLONG)) {
1050 ctype++;
1051 if (!(spec & MOD_LONG)) {
1052 ctype++;
1053 if (!(spec & MOD_SHORT)) {
1054 ctype++;
1055 if (!(spec & MOD_CHAR))
1056 ctype++;
1061 sub = ((spec & MOD_UNSIGNED)
1063 : ((spec & MOD_EXPLICITLY_SIGNED)
1065 : 0));
1067 return ctype[0][sub];
1070 struct symbol * ctype_fp(unsigned long spec)
1072 if (spec & MOD_LONGLONG)
1073 return &ldouble_ctype;
1074 if (spec & MOD_LONG)
1075 return &double_ctype;
1076 return &float_ctype;
1079 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1081 unsigned long mod = thistype->modifiers;
1083 if (mod) {
1084 unsigned long old = ctype->modifiers;
1085 unsigned long extra = 0, dup, conflict;
1087 if (mod & old & MOD_LONG) {
1088 extra = MOD_LONGLONG | MOD_LONG;
1089 mod &= ~MOD_LONG;
1090 old &= ~MOD_LONG;
1092 dup = (mod & old) | (extra & old) | (extra & mod);
1093 if (dup)
1094 sparse_error(pos, "Just how %sdo you want this type to be?",
1095 modifier_string(dup));
1097 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
1098 if (conflict)
1099 sparse_error(pos, "You cannot have both long and short modifiers.");
1101 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
1102 if (conflict)
1103 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
1105 // Only one storage modifier allowed, except that "inline" doesn't count.
1106 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
1107 conflict &= (conflict - 1);
1108 if (conflict)
1109 sparse_error(pos, "multiple storage classes");
1111 ctype->modifiers = old | mod | extra;
1114 /* Context */
1115 concat_ptr_list((struct ptr_list *)thistype->contexts,
1116 (struct ptr_list **)&ctype->contexts);
1118 /* Alignment */
1119 if (thistype->alignment & (thistype->alignment-1)) {
1120 warning(pos, "I don't like non-power-of-2 alignments");
1121 thistype->alignment = 0;
1123 if (thistype->alignment > ctype->alignment)
1124 ctype->alignment = thistype->alignment;
1126 /* Address space */
1127 if (thistype->as)
1128 ctype->as = thistype->as;
1131 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
1133 unsigned long banned, wrong;
1134 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1135 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1137 if (s->type == SYM_KEYWORD)
1138 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1139 else if (s->ctype.base_type == &fp_type)
1140 banned = BANNED_SIGN;
1141 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1142 banned = 0;
1143 else {
1144 // label_type
1145 // void_type
1146 // bad_type
1147 // vector_type <-- whatever that is
1148 banned = BANNED_SIZE | BANNED_SIGN;
1151 wrong = mod & banned;
1152 if (wrong)
1153 sparse_error(*pos, "modifier %sis invalid in this context",
1154 modifier_string (wrong));
1157 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1159 struct token *token;
1161 while ( (token = next) != NULL ) {
1162 struct ctype thistype;
1163 struct ident *ident;
1164 struct symbol *s, *type;
1165 unsigned long mod;
1167 next = token->next;
1168 if (token_type(token) != TOKEN_IDENT)
1169 break;
1170 ident = token->ident;
1172 s = lookup_symbol(ident, NS_TYPEDEF);
1173 if (!s)
1174 break;
1175 thistype = s->ctype;
1176 mod = thistype.modifiers;
1177 if (qual) {
1178 if (s->type != SYM_KEYWORD)
1179 break;
1180 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1181 break;
1183 if (s->type == SYM_KEYWORD && s->op->declarator) {
1184 next = s->op->declarator(next, &thistype);
1185 mod = thistype.modifiers;
1187 type = thistype.base_type;
1188 if (type) {
1189 if (qual)
1190 break;
1191 if (ctype->base_type)
1192 break;
1193 /* User types only mix with qualifiers */
1194 if (mod & MOD_USERTYPE) {
1195 if (ctype->modifiers & MOD_SPECIFIER)
1196 break;
1198 ctype->base_type = type;
1201 check_modifiers(&token->pos, s, ctype->modifiers);
1202 apply_ctype(token->pos, &thistype, ctype);
1205 if (!ctype->base_type) {
1206 struct symbol *base = &incomplete_ctype;
1209 * If we have modifiers, we'll default to an integer
1210 * type, and "ctype_integer()" will turn this into
1211 * a specific one.
1213 if (ctype->modifiers & MOD_SPECIFIER)
1214 base = &int_type;
1215 ctype->base_type = base;
1217 return token;
1220 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1222 struct expression *expr = NULL;
1224 token = parse_expression(token, &expr);
1225 sym->array_size = expr;
1226 return token;
1229 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1230 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1232 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1234 struct symbol *keyword;
1235 for (;;) {
1236 struct ctype thistype = { 0, };
1237 if (token_type(token) != TOKEN_IDENT)
1238 break;
1239 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1240 if (!keyword || keyword->type != SYM_KEYWORD)
1241 break;
1242 if (!(keyword->op->type & keywords))
1243 break;
1244 token = keyword->op->declarator(token->next, &thistype);
1245 apply_ctype(token->pos, &thistype, ctype);
1247 return token;
1250 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1252 struct ctype *ctype = &decl->ctype;
1254 if (p && token_type(token) == TOKEN_IDENT) {
1255 *p = token->ident;
1256 token = token->next;
1259 for (;;) {
1260 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
1262 if (token_type(token) != TOKEN_SPECIAL)
1263 return token;
1266 * This can be either a parameter list or a grouping.
1267 * For the direct (non-abstract) case, we know if must be
1268 * a parameter list if we already saw the identifier.
1269 * For the abstract case, we know if must be a parameter
1270 * list if it is empty or starts with a type.
1272 if (token->special == '(') {
1273 struct symbol *sym;
1274 struct token *next = token->next;
1275 int fn;
1277 next = handle_attributes(next, ctype, KW_ATTRIBUTE);
1278 fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1280 if (!fn) {
1281 struct symbol *base_type = ctype->base_type;
1282 token = declarator(next, decl, p);
1283 token = expect(token, ')', "in nested declarator");
1284 while (ctype->base_type != base_type)
1285 ctype = &ctype->base_type->ctype;
1286 p = NULL;
1287 continue;
1290 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1291 token = parameter_type_list(next, sym, p);
1292 token = expect(token, ')', "in function declarator");
1293 sym->endpos = token->pos;
1294 continue;
1296 if (token->special == '[') {
1297 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1298 token = abstract_array_declarator(token->next, array);
1299 token = expect(token, ']', "in abstract_array_declarator");
1300 array->endpos = token->pos;
1301 ctype = &array->ctype;
1302 continue;
1304 break;
1306 return token;
1309 static struct token *pointer(struct token *token, struct ctype *ctype)
1311 unsigned long modifiers;
1312 struct symbol *base_type;
1314 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1315 base_type = ctype->base_type;
1316 ctype->modifiers = modifiers;
1318 while (match_op(token,'*')) {
1319 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1320 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1321 ptr->ctype.as = ctype->as;
1322 concat_ptr_list((struct ptr_list *)ctype->contexts,
1323 (struct ptr_list **)&ptr->ctype.contexts);
1324 ptr->ctype.base_type = base_type;
1326 base_type = ptr;
1327 ctype->modifiers = modifiers & MOD_STORAGE;
1328 ctype->base_type = base_type;
1329 ctype->as = 0;
1330 free_ptr_list(&ctype->contexts);
1332 token = declaration_specifiers(token->next, ctype, 1);
1333 modifiers = ctype->modifiers;
1334 ctype->base_type->endpos = token->pos;
1336 return token;
1339 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1341 token = pointer(token, &sym->ctype);
1342 return direct_declarator(token, sym, p);
1345 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1347 struct ctype *ctype = &decl->ctype;
1348 struct expression *expr;
1349 struct symbol *bitfield;
1350 long long width;
1352 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1353 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1354 show_typename(ctype->base_type));
1355 // Parse this to recover gracefully.
1356 return conditional_expression(token->next, &expr);
1359 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1360 token = conditional_expression(token->next, &expr);
1361 width = const_expression_value(expr);
1362 bitfield->bit_size = width;
1364 if (width < 0 || width > INT_MAX) {
1365 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1366 width = -1;
1367 } else if (decl->ident && width == 0) {
1368 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1369 show_ident(decl->ident));
1370 width = -1;
1371 } else if (decl->ident) {
1372 struct symbol *base_type = bitfield->ctype.base_type;
1373 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1374 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1375 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1376 // Valid values are either {-1;0} or {0}, depending on integer
1377 // representation. The latter makes for very efficient code...
1378 sparse_error(token->pos, "dubious one-bit signed bitfield");
1380 if (Wdefault_bitfield_sign &&
1381 bitfield_type->type != SYM_ENUM &&
1382 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1383 is_signed) {
1384 // The sign of bitfields is unspecified by default.
1385 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1388 bitfield->bit_size = width;
1389 bitfield->endpos = token->pos;
1390 return token;
1393 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1395 struct ctype ctype = {0, };
1397 token = declaration_specifiers(token, &ctype, 0);
1398 for (;;) {
1399 struct ident *ident = NULL;
1400 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1401 decl->ctype = ctype;
1402 token = declarator(token, decl, &ident);
1403 decl->ident = ident;
1404 if (match_op(token, ':')) {
1405 token = handle_bitfield(token, decl);
1406 token = handle_attributes(token, &decl->ctype, KW_ATTRIBUTE | KW_ASM);
1408 apply_modifiers(token->pos, &decl->ctype);
1409 add_symbol(list, decl);
1410 decl->endpos = token->pos;
1411 if (!match_op(token, ','))
1412 break;
1413 token = token->next;
1415 return token;
1418 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1420 while (!match_op(token, '}')) {
1421 if (!match_op(token, ';'))
1422 token = declaration_list(token, list);
1423 if (!match_op(token, ';')) {
1424 sparse_error(token->pos, "expected ; at end of declaration");
1425 break;
1427 token = token->next;
1429 return token;
1432 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1434 struct ident *ident = NULL;
1435 struct symbol *sym;
1436 struct ctype ctype = { 0, };
1438 token = declaration_specifiers(token, &ctype, 0);
1439 sym = alloc_symbol(token->pos, SYM_NODE);
1440 sym->ctype = ctype;
1441 *tree = sym;
1442 token = declarator(token, sym, &ident);
1443 sym->ident = ident;
1444 apply_modifiers(token->pos, &sym->ctype);
1445 sym->endpos = token->pos;
1446 return token;
1449 struct token *typename(struct token *token, struct symbol **p, int mod)
1451 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1452 *p = sym;
1453 token = declaration_specifiers(token, &sym->ctype, 0);
1454 token = declarator(token, sym, NULL);
1455 apply_modifiers(token->pos, &sym->ctype);
1456 if (sym->ctype.modifiers & MOD_STORAGE & ~mod)
1457 warning(sym->pos, "storage class in typename (%s)",
1458 show_typename(sym));
1459 sym->endpos = token->pos;
1460 return token;
1463 static struct token *expression_statement(struct token *token, struct expression **tree)
1465 token = parse_expression(token, tree);
1466 return expect(token, ';', "at end of statement");
1469 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1470 struct expression_list **inout)
1472 struct expression *expr;
1474 /* Allow empty operands */
1475 if (match_op(token->next, ':') || match_op(token->next, ')'))
1476 return token->next;
1477 do {
1478 struct ident *ident = NULL;
1479 if (match_op(token->next, '[') &&
1480 token_type(token->next->next) == TOKEN_IDENT &&
1481 match_op(token->next->next->next, ']')) {
1482 ident = token->next->next->ident;
1483 token = token->next->next->next;
1485 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1486 token = primary_expression(token->next, &expr);
1487 add_expression(inout, expr);
1488 token = parens_expression(token, &expr, "in asm parameter");
1489 add_expression(inout, expr);
1490 } while (match_op(token, ','));
1491 return token;
1494 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1495 struct expression_list **clobbers)
1497 struct expression *expr;
1499 do {
1500 token = primary_expression(token->next, &expr);
1501 add_expression(clobbers, expr);
1502 } while (match_op(token, ','));
1503 return token;
1506 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1508 token = token->next;
1509 stmt->type = STMT_ASM;
1510 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1511 token = token->next;
1513 token = expect(token, '(', "after asm");
1514 token = parse_expression(token, &stmt->asm_string);
1515 if (match_op(token, ':'))
1516 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1517 if (match_op(token, ':'))
1518 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1519 if (match_op(token, ':'))
1520 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1521 token = expect(token, ')', "after asm");
1522 return expect(token, ';', "at end of asm-statement");
1525 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1527 struct expression *expr;
1528 token = expect(token, '(', "after asm");
1529 token = parse_expression(token->next, &expr);
1530 token = expect(token, ')', "after asm");
1531 return token;
1534 /* Make a statement out of an expression */
1535 static struct statement *make_statement(struct expression *expr)
1537 struct statement *stmt;
1539 if (!expr)
1540 return NULL;
1541 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1542 stmt->expression = expr;
1543 return stmt;
1547 * All iterators have two symbols associated with them:
1548 * the "continue" and "break" symbols, which are targets
1549 * for continue and break statements respectively.
1551 * They are in a special name-space, but they follow
1552 * all the normal visibility rules, so nested iterators
1553 * automatically work right.
1555 static void start_iterator(struct statement *stmt)
1557 struct symbol *cont, *brk;
1559 start_symbol_scope();
1560 cont = alloc_symbol(stmt->pos, SYM_NODE);
1561 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1562 brk = alloc_symbol(stmt->pos, SYM_NODE);
1563 bind_symbol(brk, &break_ident, NS_ITERATOR);
1565 stmt->type = STMT_ITERATOR;
1566 stmt->iterator_break = brk;
1567 stmt->iterator_continue = cont;
1568 fn_local_symbol(brk);
1569 fn_local_symbol(cont);
1572 static void end_iterator(struct statement *stmt)
1574 end_symbol_scope();
1577 static struct statement *start_function(struct symbol *sym)
1579 struct symbol *ret;
1580 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1582 start_function_scope();
1583 ret = alloc_symbol(sym->pos, SYM_NODE);
1584 ret->ctype = sym->ctype.base_type->ctype;
1585 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1586 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1587 bind_symbol(ret, &return_ident, NS_ITERATOR);
1588 stmt->ret = ret;
1589 fn_local_symbol(ret);
1591 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1592 current_fn = sym;
1594 return stmt;
1597 static void end_function(struct symbol *sym)
1599 current_fn = NULL;
1600 end_function_scope();
1604 * A "switch()" statement, like an iterator, has a
1605 * the "break" symbol associated with it. It works
1606 * exactly like the iterator break - it's the target
1607 * for any break-statements in scope, and means that
1608 * "break" handling doesn't even need to know whether
1609 * it's breaking out of an iterator or a switch.
1611 * In addition, the "case" symbol is a marker for the
1612 * case/default statements to find the switch statement
1613 * that they are associated with.
1615 static void start_switch(struct statement *stmt)
1617 struct symbol *brk, *switch_case;
1619 start_symbol_scope();
1620 brk = alloc_symbol(stmt->pos, SYM_NODE);
1621 bind_symbol(brk, &break_ident, NS_ITERATOR);
1623 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1624 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1625 switch_case->stmt = stmt;
1627 stmt->type = STMT_SWITCH;
1628 stmt->switch_break = brk;
1629 stmt->switch_case = switch_case;
1631 fn_local_symbol(brk);
1632 fn_local_symbol(switch_case);
1635 static void end_switch(struct statement *stmt)
1637 if (!stmt->switch_case->symbol_list)
1638 warning(stmt->pos, "switch with no cases");
1639 end_symbol_scope();
1642 static void add_case_statement(struct statement *stmt)
1644 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1645 struct symbol *sym;
1647 if (!target) {
1648 sparse_error(stmt->pos, "not in switch scope");
1649 stmt->type = STMT_NONE;
1650 return;
1652 sym = alloc_symbol(stmt->pos, SYM_NODE);
1653 add_symbol(&target->symbol_list, sym);
1654 sym->stmt = stmt;
1655 stmt->case_label = sym;
1656 fn_local_symbol(sym);
1659 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1661 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1663 if (!target)
1664 error_die(token->pos, "internal error: return without a function target");
1665 stmt->type = STMT_RETURN;
1666 stmt->ret_target = target;
1667 return expression_statement(token->next, &stmt->ret_value);
1670 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1672 struct symbol_list *syms;
1673 struct expression *e1, *e2, *e3;
1674 struct statement *iterator;
1676 start_iterator(stmt);
1677 token = expect(token->next, '(', "after 'for'");
1679 syms = NULL;
1680 e1 = NULL;
1681 /* C99 variable declaration? */
1682 if (lookup_type(token)) {
1683 token = external_declaration(token, &syms);
1684 } else {
1685 token = parse_expression(token, &e1);
1686 token = expect(token, ';', "in 'for'");
1688 token = parse_expression(token, &e2);
1689 token = expect(token, ';', "in 'for'");
1690 token = parse_expression(token, &e3);
1691 token = expect(token, ')', "in 'for'");
1692 token = statement(token, &iterator);
1694 stmt->iterator_syms = syms;
1695 stmt->iterator_pre_statement = make_statement(e1);
1696 stmt->iterator_pre_condition = e2;
1697 stmt->iterator_post_statement = make_statement(e3);
1698 stmt->iterator_post_condition = NULL;
1699 stmt->iterator_statement = iterator;
1700 end_iterator(stmt);
1702 return token;
1705 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1707 struct expression *expr;
1708 struct statement *iterator;
1710 start_iterator(stmt);
1711 token = parens_expression(token->next, &expr, "after 'while'");
1712 token = statement(token, &iterator);
1714 stmt->iterator_pre_condition = expr;
1715 stmt->iterator_post_condition = NULL;
1716 stmt->iterator_statement = iterator;
1717 end_iterator(stmt);
1719 return token;
1722 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1724 struct expression *expr;
1725 struct statement *iterator;
1727 start_iterator(stmt);
1728 token = statement(token->next, &iterator);
1729 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1730 token = token->next;
1731 else
1732 sparse_error(token->pos, "expected 'while' after 'do'");
1733 token = parens_expression(token, &expr, "after 'do-while'");
1735 stmt->iterator_post_condition = expr;
1736 stmt->iterator_statement = iterator;
1737 end_iterator(stmt);
1739 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1740 warning(iterator->pos, "do-while statement is not a compound statement");
1742 return expect(token, ';', "after statement");
1745 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1747 stmt->type = STMT_IF;
1748 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1749 token = statement(token, &stmt->if_true);
1750 if (token_type(token) != TOKEN_IDENT)
1751 return token;
1752 if (token->ident != &else_ident)
1753 return token;
1754 return statement(token->next, &stmt->if_false);
1757 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1759 stmt->type = STMT_CASE;
1760 token = expect(token, ':', "after default/case");
1761 add_case_statement(stmt);
1762 return statement(token, &stmt->case_statement);
1765 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1767 token = parse_expression(token->next, &stmt->case_expression);
1768 if (match_op(token, SPECIAL_ELLIPSIS))
1769 token = parse_expression(token->next, &stmt->case_to);
1770 return case_statement(token, stmt);
1773 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1775 return case_statement(token->next, stmt);
1778 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1780 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1781 stmt->type = STMT_GOTO;
1782 stmt->goto_label = target;
1783 if (!target)
1784 sparse_error(stmt->pos, "break/continue not in iterator scope");
1785 return expect(token->next, ';', "at end of statement");
1788 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1790 stmt->type = STMT_SWITCH;
1791 start_switch(stmt);
1792 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1793 token = statement(token, &stmt->switch_statement);
1794 end_switch(stmt);
1795 return token;
1798 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1800 stmt->type = STMT_GOTO;
1801 token = token->next;
1802 if (match_op(token, '*')) {
1803 token = parse_expression(token->next, &stmt->goto_expression);
1804 add_statement(&function_computed_goto_list, stmt);
1805 } else if (token_type(token) == TOKEN_IDENT) {
1806 stmt->goto_label = label_symbol(token);
1807 token = token->next;
1808 } else {
1809 sparse_error(token->pos, "Expected identifier or goto expression");
1811 return expect(token, ';', "at end of statement");
1814 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1816 struct expression *args[3];
1817 int argc = 0;
1819 stmt->type = STMT_CONTEXT;
1820 token = token->next;
1821 token = expect(token, '(', "after __context__ statement");
1822 while (!match_op(token, ')')) {
1823 struct expression *expr = NULL;
1824 token = conditional_expression(token, &expr);
1825 if (!expr)
1826 break;
1827 if (argc < 3)
1828 args[argc++] = expr;
1829 else
1830 argc++;
1831 if (!match_op(token, ','))
1832 break;
1833 token = token->next;
1836 stmt->expression = args[0];
1837 stmt->context = NULL;
1839 switch (argc) {
1840 case 0:
1841 sparse_error(token->pos, "__context__ statement needs argument(s)");
1842 return token;
1843 case 1:
1844 /* already done */
1845 break;
1846 case 2:
1847 if (args[0]->type != STMT_EXPRESSION) {
1848 stmt->context = args[0];
1849 stmt->expression = args[1];
1850 } else {
1851 stmt->expression = args[0];
1852 stmt->required = args[1];
1854 break;
1855 case 3:
1856 stmt->context = args[0];
1857 stmt->expression = args[1];
1858 stmt->required = args[2];
1859 break;
1860 default:
1861 sparse_error(token->pos, "too many arguments for __context__ statement");
1862 return token->next;
1865 return expect(token, ')', "at end of __context__");
1868 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1870 stmt->type = STMT_RANGE;
1871 token = assignment_expression(token->next, &stmt->range_expression);
1872 token = expect(token, ',', "after range expression");
1873 token = assignment_expression(token, &stmt->range_low);
1874 token = expect(token, ',', "after low range");
1875 token = assignment_expression(token, &stmt->range_high);
1876 return expect(token, ';', "after range statement");
1879 static struct token *statement(struct token *token, struct statement **tree)
1881 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1883 *tree = stmt;
1884 if (token_type(token) == TOKEN_IDENT) {
1885 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1886 if (s && s->op->statement)
1887 return s->op->statement(token, stmt);
1889 if (match_op(token->next, ':')) {
1890 stmt->type = STMT_LABEL;
1891 stmt->label_identifier = label_symbol(token);
1892 token = handle_attributes(token->next->next, &stmt->label_identifier->ctype, KW_ATTRIBUTE);
1893 return statement(token, &stmt->label_statement);
1897 if (match_op(token, '{')) {
1898 stmt->type = STMT_COMPOUND;
1899 start_symbol_scope();
1900 token = compound_statement(token->next, stmt);
1901 end_symbol_scope();
1903 return expect(token, '}', "at end of compound statement");
1906 stmt->type = STMT_EXPRESSION;
1907 return expression_statement(token, &stmt->expression);
1910 static struct token * statement_list(struct token *token, struct statement_list **list)
1912 int seen_statement = 0;
1913 for (;;) {
1914 struct statement * stmt;
1915 if (eof_token(token))
1916 break;
1917 if (match_op(token, '}'))
1918 break;
1919 if (lookup_type(token)) {
1920 if (seen_statement) {
1921 warning(token->pos, "mixing declarations and code");
1922 seen_statement = 0;
1924 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1925 token = external_declaration(token, &stmt->declaration);
1926 } else {
1927 seen_statement = Wdeclarationafterstatement;
1928 token = statement(token, &stmt);
1930 add_statement(list, stmt);
1932 return token;
1935 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1937 struct symbol_list **list = &fn->arguments;
1939 if (match_op(token, ')')) {
1940 // No warning for "void oink ();"
1941 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1942 if (p && !match_op(token->next, ';'))
1943 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1944 return token;
1947 for (;;) {
1948 struct symbol *sym;
1950 if (match_op(token, SPECIAL_ELLIPSIS)) {
1951 if (!*list)
1952 warning(token->pos, "variadic functions must have one named argument");
1953 fn->variadic = 1;
1954 token = token->next;
1955 break;
1958 sym = alloc_symbol(token->pos, SYM_NODE);
1959 token = parameter_declaration(token, &sym);
1960 if (sym->ctype.base_type == &void_ctype) {
1961 /* Special case: (void) */
1962 if (!*list && !sym->ident)
1963 break;
1964 warning(token->pos, "void parameter");
1966 add_symbol(list, sym);
1967 sym->endpos = token->pos;
1968 if (!match_op(token, ','))
1969 break;
1970 token = token->next;
1973 return token;
1976 struct token *compound_statement(struct token *token, struct statement *stmt)
1978 token = statement_list(token, &stmt->stmts);
1979 return token;
1982 static struct expression *identifier_expression(struct token *token)
1984 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1985 expr->expr_ident = token->ident;
1986 return expr;
1989 static struct expression *index_expression(struct expression *from, struct expression *to)
1991 int idx_from, idx_to;
1992 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1994 idx_from = const_expression_value(from);
1995 idx_to = idx_from;
1996 if (to) {
1997 idx_to = const_expression_value(to);
1998 if (idx_to < idx_from || idx_from < 0)
1999 warning(from->pos, "nonsense array initializer index range");
2001 expr->idx_from = idx_from;
2002 expr->idx_to = idx_to;
2003 return expr;
2006 static struct token *single_initializer(struct expression **ep, struct token *token)
2008 int expect_equal = 0;
2009 struct token *next = token->next;
2010 struct expression **tail = ep;
2011 int nested;
2013 *ep = NULL;
2015 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2016 struct expression *expr = identifier_expression(token);
2017 if (Wold_initializer)
2018 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2019 token = initializer(&expr->ident_expression, next->next);
2020 if (expr->ident_expression)
2021 *ep = expr;
2022 return token;
2025 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2026 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2027 struct expression *expr = identifier_expression(next);
2028 *tail = expr;
2029 tail = &expr->ident_expression;
2030 expect_equal = 1;
2031 token = next->next;
2032 } else if (match_op(token, '[')) {
2033 struct expression *from = NULL, *to = NULL, *expr;
2034 token = constant_expression(token->next, &from);
2035 if (!from) {
2036 sparse_error(token->pos, "Expected constant expression");
2037 break;
2039 if (match_op(token, SPECIAL_ELLIPSIS))
2040 token = constant_expression(token->next, &to);
2041 expr = index_expression(from, to);
2042 *tail = expr;
2043 tail = &expr->idx_expression;
2044 token = expect(token, ']', "at end of initializer index");
2045 if (nested)
2046 expect_equal = 1;
2047 } else {
2048 break;
2051 if (nested && !expect_equal) {
2052 if (!match_op(token, '='))
2053 warning(token->pos, "obsolete array initializer, use C99 syntax");
2054 else
2055 expect_equal = 1;
2057 if (expect_equal)
2058 token = expect(token, '=', "at end of initializer index");
2060 token = initializer(tail, token);
2061 if (!*tail)
2062 *ep = NULL;
2063 return token;
2066 static struct token *initializer_list(struct expression_list **list, struct token *token)
2068 struct expression *expr;
2070 for (;;) {
2071 token = single_initializer(&expr, token);
2072 if (!expr)
2073 break;
2074 add_expression(list, expr);
2075 if (!match_op(token, ','))
2076 break;
2077 token = token->next;
2079 return token;
2082 struct token *initializer(struct expression **tree, struct token *token)
2084 if (match_op(token, '{')) {
2085 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2086 *tree = expr;
2087 token = initializer_list(&expr->expr_list, token->next);
2088 return expect(token, '}', "at end of initializer");
2090 return assignment_expression(token, tree);
2093 static void declare_argument(struct symbol *sym, struct symbol *fn)
2095 if (!sym->ident) {
2096 sparse_error(sym->pos, "no identifier for function argument");
2097 return;
2099 bind_symbol(sym, sym->ident, NS_SYMBOL);
2102 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2103 struct symbol_list **list)
2105 struct symbol_list **old_symbol_list;
2106 struct symbol *base_type = decl->ctype.base_type;
2107 struct statement *stmt, **p;
2108 struct symbol *arg;
2110 old_symbol_list = function_symbol_list;
2111 if (decl->ctype.modifiers & MOD_INLINE) {
2112 function_symbol_list = &decl->inline_symbol_list;
2113 p = &base_type->inline_stmt;
2114 } else {
2115 function_symbol_list = &decl->symbol_list;
2116 p = &base_type->stmt;
2118 function_computed_target_list = NULL;
2119 function_computed_goto_list = NULL;
2121 if (decl->ctype.modifiers & MOD_EXTERN) {
2122 if (!(decl->ctype.modifiers & MOD_INLINE))
2123 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2125 if (!(decl->ctype.modifiers & MOD_STATIC))
2126 decl->ctype.modifiers |= MOD_EXTERN;
2128 stmt = start_function(decl);
2130 *p = stmt;
2131 FOR_EACH_PTR (base_type->arguments, arg) {
2132 declare_argument(arg, base_type);
2133 } END_FOR_EACH_PTR(arg);
2135 token = compound_statement(token->next, stmt);
2137 end_function(decl);
2138 if (!(decl->ctype.modifiers & MOD_INLINE))
2139 add_symbol(list, decl);
2140 check_declaration(decl);
2141 function_symbol_list = old_symbol_list;
2142 if (function_computed_goto_list) {
2143 if (!function_computed_target_list)
2144 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2145 else {
2146 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2147 stmt->target_list = function_computed_target_list;
2148 } END_FOR_EACH_PTR(stmt);
2151 return expect(token, '}', "at end of function");
2154 static void promote_k_r_types(struct symbol *arg)
2156 struct symbol *base = arg->ctype.base_type;
2157 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2158 arg->ctype.base_type = &int_ctype;
2162 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2164 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2165 struct symbol *arg;
2167 FOR_EACH_PTR(real_args, arg) {
2168 struct symbol *type;
2170 /* This is quadratic in the number of arguments. We _really_ don't care */
2171 FOR_EACH_PTR(argtypes, type) {
2172 if (type->ident == arg->ident)
2173 goto match;
2174 } END_FOR_EACH_PTR(type);
2175 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2176 continue;
2177 match:
2178 type->used = 1;
2179 /* "char" and "short" promote to "int" */
2180 promote_k_r_types(type);
2182 arg->ctype = type->ctype;
2183 } END_FOR_EACH_PTR(arg);
2185 FOR_EACH_PTR(argtypes, arg) {
2186 if (!arg->used)
2187 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2188 } END_FOR_EACH_PTR(arg);
2192 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2193 struct symbol_list **list)
2195 struct symbol_list *args = NULL;
2197 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2198 do {
2199 token = declaration_list(token, &args);
2200 if (!match_op(token, ';')) {
2201 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2202 break;
2204 token = token->next;
2205 } while (lookup_type(token));
2207 apply_k_r_types(args, decl);
2209 if (!match_op(token, '{')) {
2210 sparse_error(token->pos, "expected function body");
2211 return token;
2213 return parse_function_body(token, decl, list);
2216 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2218 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2219 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2220 struct statement *stmt;
2222 anon->ctype.base_type = fn;
2223 stmt = alloc_statement(token->pos, STMT_NONE);
2224 fn->stmt = stmt;
2226 token = parse_asm_statement(token, stmt);
2228 add_symbol(list, anon);
2229 return token;
2232 struct token *external_declaration(struct token *token, struct symbol_list **list)
2234 struct ident *ident = NULL;
2235 struct symbol *decl;
2236 struct ctype ctype = { 0, };
2237 struct symbol *base_type;
2238 int is_typedef;
2240 /* Top-level inline asm? */
2241 if (token_type(token) == TOKEN_IDENT) {
2242 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2243 if (s && s->op->toplevel)
2244 return s->op->toplevel(token, list);
2247 /* Parse declaration-specifiers, if any */
2248 token = declaration_specifiers(token, &ctype, 0);
2249 decl = alloc_symbol(token->pos, SYM_NODE);
2250 decl->ctype = ctype;
2251 token = declarator(token, decl, &ident);
2252 apply_modifiers(token->pos, &decl->ctype);
2254 decl->endpos = token->pos;
2256 /* Just a type declaration? */
2257 if (!ident)
2258 return expect(token, ';', "end of type declaration");
2260 /* type define declaration? */
2261 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2263 /* Typedefs don't have meaningful storage */
2264 if (is_typedef) {
2265 ctype.modifiers &= ~MOD_STORAGE;
2266 decl->ctype.modifiers &= ~MOD_STORAGE;
2267 decl->ctype.modifiers |= MOD_USERTYPE;
2270 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2272 base_type = decl->ctype.base_type;
2274 if (is_typedef) {
2275 if (base_type && !base_type->ident) {
2276 switch (base_type->type) {
2277 case SYM_STRUCT:
2278 case SYM_UNION:
2279 case SYM_ENUM:
2280 case SYM_RESTRICT:
2281 base_type->ident = ident;
2284 } else if (base_type && base_type->type == SYM_FN) {
2285 /* K&R argument declaration? */
2286 if (lookup_type(token))
2287 return parse_k_r_arguments(token, decl, list);
2288 if (match_op(token, '{'))
2289 return parse_function_body(token, decl, list);
2291 if (!(decl->ctype.modifiers & MOD_STATIC))
2292 decl->ctype.modifiers |= MOD_EXTERN;
2293 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2294 sparse_error(token->pos, "void declaration");
2297 for (;;) {
2298 if (!is_typedef && match_op(token, '=')) {
2299 if (decl->ctype.modifiers & MOD_EXTERN) {
2300 warning(decl->pos, "symbol with external linkage has initializer");
2301 decl->ctype.modifiers &= ~MOD_EXTERN;
2303 token = initializer(&decl->initializer, token->next);
2305 if (!is_typedef) {
2306 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2307 add_symbol(list, decl);
2308 fn_local_symbol(decl);
2311 check_declaration(decl);
2313 if (!match_op(token, ','))
2314 break;
2316 token = token->next;
2317 ident = NULL;
2318 decl = alloc_symbol(token->pos, SYM_NODE);
2319 decl->ctype = ctype;
2320 token = declaration_specifiers(token, &decl->ctype, 1);
2321 token = declarator(token, decl, &ident);
2322 apply_modifiers(token->pos, &decl->ctype);
2323 decl->endpos = token->pos;
2324 if (!ident) {
2325 sparse_error(token->pos, "expected identifier name in type definition");
2326 return token;
2329 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2331 /* Function declarations are automatically extern unless specifically static */
2332 base_type = decl->ctype.base_type;
2333 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2334 if (!(decl->ctype.modifiers & MOD_STATIC))
2335 decl->ctype.modifiers |= MOD_EXTERN;
2338 return expect(token, ';', "at end of declaration");