Propagate decl_state to declaration_specifiers()
[smatch.git] / parse.c
bloba2048a24987f6cf807d4fb4fd76593d9625ba8b5
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_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 = {
72 .type = KW_MODIFIER,
75 static struct symbol_op qualifier_op = {
76 .type = KW_QUALIFIER,
79 static struct symbol_op typeof_op = {
80 .type = KW_TYPEOF,
81 .declarator = typeof_specifier,
84 static struct symbol_op attribute_op = {
85 .type = KW_ATTRIBUTE,
86 .declarator = attribute_specifier,
89 static struct symbol_op struct_op = {
90 .type = KW_SPECIFIER,
91 .declarator = struct_specifier,
94 static struct symbol_op union_op = {
95 .type = KW_SPECIFIER,
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 = {
155 .type = KW_ASM,
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 = {
194 .type = KW_MODE,
197 static struct init_keyword {
198 const char *name;
199 enum namespace ns;
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 },
211 /* Typedef.. */
212 { "typedef", NS_TYPEDEF, MOD_TYPEDEF, .op = &modifier_op },
214 /* Extended types */
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},
234 /* Statement */
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 },
252 /* Attribute */
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)
352 int i;
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;
358 sym->op = ptr->op;
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, ...)
371 va_list args;
372 struct ident * next;
374 if (token_type(token) != TOKEN_IDENT)
375 return 0;
377 va_start(args, token);
378 do {
379 next = va_arg(args, struct ident *);
380 } while (next && token->ident != next);
381 va_end(args);
383 return next && token->ident == next;
387 struct statement *alloc_statement(struct position pos, int type)
389 struct statement *stmt = __alloc_statement(0);
390 stmt->type = type;
391 stmt->pos = pos;
392 return stmt;
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)
399 struct symbol *base;
401 while ((base = ctype->base_type)) {
402 switch (base->type) {
403 case SYM_FN:
404 case SYM_ENUM:
405 case SYM_ARRAY:
406 case SYM_BITFIELD:
407 case SYM_PTR:
408 ctype = &base->ctype;
409 continue;
411 break;
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) {
424 struct symbol *type;
425 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
426 if (!is_int_type(ctype->base_type)) {
427 sparse_error(pos, "invalid modifier");
428 return 1;
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;
436 create_fouled(type);
438 return 0;
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;
450 return sym;
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);
456 if (!sym) {
457 sym = alloc_symbol(token->pos, type);
458 bind_symbol(sym, token->ident, ns);
459 if (type == SYM_LABEL)
460 fn_local_symbol(sym);
462 return 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)
470 return sym;
472 return NULL;
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);
483 if (sym)
484 return sym;
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 *))
492 struct symbol *sym;
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);
499 if (!sym ||
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;
510 repos = &token->pos;
511 token = token->next;
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));
518 sym->pos = *repos;
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
523 sym->examined = 0;
524 sym->endpos = token->pos;
526 return token;
529 // private struct/union/enum type
530 if (!match_op(token, '{')) {
531 sparse_error(token->pos, "expected declaration");
532 ctype->base_type = &bad_ctype;
533 return token;
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;
542 return token;
545 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
547 struct symbol *field, *last = NULL;
548 struct token *res;
549 res = struct_declaration_list(token, &sym->symbol_list);
550 FOR_EACH_PTR(sym->symbol_list, field) {
551 if (!field->ident) {
552 struct symbol *base = field->ctype.base_type;
553 if (base && base->type == SYM_BITFIELD)
554 continue;
556 if (last)
557 last->next_subobject = field;
558 last = field;
559 } END_FOR_EACH_PTR(field);
560 return res;
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);
579 typedef struct {
580 int x;
581 unsigned long long y;
582 } Num;
584 static void upper_boundary(Num *n, Num *v)
586 if (n->x > v->x)
587 return;
588 if (n->x < v->x) {
589 *n = *v;
590 return;
592 if (n->y < v->y)
593 n->y = v->y;
596 static void lower_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 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;
613 if (!is_unsigned)
614 shift--;
615 if (upper->x == 0 && upper->y >> shift)
616 return 0;
617 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
618 return 1;
619 return 0;
622 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
624 if (s1->bit_size < s2->bit_size) {
625 s1 = s2;
626 } else if (s1->bit_size == s2->bit_size) {
627 if (s2->ctype.modifiers & MOD_UNSIGNED)
628 s1 = s2;
630 if (s1->bit_size < bits_in_int)
631 return &int_ctype;
632 return s1;
635 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
637 struct symbol *sym;
639 FOR_EACH_PTR(list, sym) {
640 struct expression *expr = sym->initializer;
641 struct symbol *ctype;
642 if (expr->type != EXPR_VALUE)
643 continue;
644 ctype = expr->ctype;
645 if (ctype->bit_size == base_type->bit_size)
646 continue;
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;
662 struct symbol *sym;
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);
671 ctype = &void_ctype;
672 if (expr && expr->ctype)
673 ctype = expr->ctype;
674 } else if (!ctype) {
675 ctype = &int_ctype;
676 } else if (is_int_type(ctype)) {
677 lastval++;
678 } else {
679 error_die(token->pos, "can't increment the last enum member");
682 if (!expr) {
683 expr = alloc_expression(token->pos, EXPR_VALUE);
684 expr->value = lastval;
685 expr->ctype = ctype;
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) {
697 if (ctype == parent)
698 ctype = base_type;
699 else
700 ctype = ctype->ctype.base_type;
703 * base_type rules:
704 * - if all enums are of the same type, then
705 * the base_type is that type (two first
706 * cases)
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".
712 if (!base_type) {
713 base_type = ctype;
714 } else if (ctype == base_type) {
715 /* nothing */
716 } else if (is_int_type(base_type) && is_int_type(ctype)) {
717 base_type = bigger_enum_type(base_type, ctype);
718 } else
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)
725 v.x = 0;
726 else if ((long long)lastval >= 0)
727 v.x = 0;
728 else
729 v.x = -1;
730 upper_boundary(&upper, &v);
731 lower_boundary(&lower, &v);
733 token = next;
735 sym->endpos = token->pos;
737 if (!match_op(token, ','))
738 break;
739 token = token->next;
741 if (!base_type) {
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;
761 else
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);
769 return token;
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;
780 return ret;
783 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
785 struct symbol *sym;
787 if (!match_op(token, '(')) {
788 sparse_error(token->pos, "expected '(' after typeof");
789 return token;
791 if (lookup_type(token->next)) {
792 token = typename(token->next, &sym, 0);
793 *ctype = sym->ctype;
794 } else {
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");
810 return token;
813 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
815 ctype->alignment = 1;
816 return token;
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");
826 if (expr)
827 alignment = const_expression_value(expr);
829 ctype->alignment = alignment;
830 return token;
833 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
835 ctype->modifiers |= attr->ctype.modifiers;
836 return token;
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);
844 if (expr)
845 ctype->as = const_expression_value(expr);
846 token = expect(token, ')', "after address_space attribute");
847 return token;
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;
857 else
858 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
859 token = token->next;
860 } else
861 sparse_error(token->pos, "expect attribute mode symbol\n");
862 token = expect(token, ')', "after mode attribute");
863 return token;
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];
870 int argc = 0;
872 token = expect(token, '(', "after context attribute");
873 while (!match_op(token, ')')) {
874 struct expression *expr = NULL;
875 token = conditional_expression(token, &expr);
876 if (!expr)
877 break;
878 if (argc < 3)
879 args[argc++] = expr;
880 if (!match_op(token, ','))
881 break;
882 token = token->next;
885 switch(argc) {
886 case 0:
887 sparse_error(token->pos, "expected context input/output values");
888 break;
889 case 1:
890 context->in = get_expression_value(args[0]);
891 break;
892 case 2:
893 context->in = get_expression_value(args[0]);
894 context->out = get_expression_value(args[1]);
895 break;
896 case 3:
897 context->context = args[0];
898 context->in = get_expression_value(args[1]);
899 context->out = get_expression_value(args[2]);
900 break;
903 if (argc)
904 add_ptr_list(&ctype->contexts, context);
906 token = expect(token, ')', "after context attribute");
907 return token;
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__");
914 return token;
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));
922 token = token->next;
923 if (match_op(token, '('))
924 token = parens_expression(token, &expr, "in attribute");
925 return token;
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");
934 for (;;) {
935 struct ident *attribute_name;
936 struct symbol *attr;
938 if (eof_token(token))
939 break;
940 if (match_op(token, ';'))
941 break;
942 if (token_type(token) != TOKEN_IDENT)
943 break;
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);
948 else
949 token = recover_unknown_attribute(token);
951 if (!match_op(token, ','))
952 break;
953 token = token->next;
956 token = expect(token, ')', "after attribute");
957 token = expect(token, ')', "after attribute");
958 return token;
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];
971 int sub;
973 ctype = integer_ctypes;
974 if (!(spec & MOD_LONGLONG)) {
975 ctype++;
976 if (!(spec & MOD_LONG)) {
977 ctype++;
978 if (!(spec & MOD_SHORT)) {
979 ctype++;
980 if (!(spec & MOD_CHAR))
981 ctype++;
986 sub = ((spec & MOD_UNSIGNED)
988 : ((spec & MOD_EXPLICITLY_SIGNED)
990 : 0));
992 return ctype[0][sub];
995 struct symbol * ctype_fp(unsigned long spec)
997 if (spec & MOD_LONGLONG)
998 return &ldouble_ctype;
999 if (spec & MOD_LONG)
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;
1008 if (mod) {
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;
1014 mod &= ~MOD_LONG;
1015 old &= ~MOD_LONG;
1017 dup = (mod & old) | (extra & old) | (extra & mod);
1018 if (dup)
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));
1023 if (conflict)
1024 sparse_error(pos, "You cannot have both long and short modifiers.");
1026 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
1027 if (conflict)
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);
1033 if (conflict)
1034 sparse_error(pos, "multiple storage classes");
1036 ctype->modifiers = old | mod | extra;
1039 /* Context */
1040 concat_ptr_list((struct ptr_list *)thistype->contexts,
1041 (struct ptr_list **)&ctype->contexts);
1043 /* Alignment */
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;
1051 /* Address space */
1052 if (thistype->as)
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))
1067 banned = 0;
1068 else {
1069 // label_type
1070 // void_type
1071 // bad_type
1072 // vector_type <-- whatever that is
1073 banned = BANNED_SIZE | BANNED_SIGN;
1076 wrong = mod & banned;
1077 if (wrong)
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;
1090 unsigned long mod;
1092 next = token->next;
1093 if (token_type(token) != TOKEN_IDENT)
1094 break;
1095 ident = token->ident;
1097 s = lookup_symbol(ident, NS_TYPEDEF);
1098 if (!s)
1099 break;
1100 thistype = s->ctype;
1101 mod = thistype.modifiers;
1102 if (qual) {
1103 if (s->type != SYM_KEYWORD)
1104 break;
1105 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1106 break;
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;
1113 if (type) {
1114 if (qual)
1115 break;
1116 if (ctx->ctype.base_type)
1117 break;
1118 /* User types only mix with qualifiers */
1119 if (mod & MOD_USERTYPE) {
1120 if (ctx->ctype.modifiers & MOD_SPECIFIER)
1121 break;
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
1136 * a specific one.
1138 if (ctx->ctype.modifiers & MOD_SPECIFIER)
1139 base = &int_type;
1140 ctx->ctype.base_type = base;
1142 return token;
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;
1151 return token;
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, '(')) {
1162 int depth = 1;
1163 token = token->next;
1164 while (depth && !eof_token(token)) {
1165 if (token_type(token) == TOKEN_SPECIAL) {
1166 if (token->special == '(')
1167 depth++;
1168 else if (token->special == ')')
1169 depth--;
1171 token = token->next;
1174 return token;
1177 static struct token *skip_attributes(struct token *token)
1179 struct symbol *keyword;
1180 for (;;) {
1181 if (token_type(token) != TOKEN_IDENT)
1182 break;
1183 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1184 if (!keyword || keyword->type != SYM_KEYWORD)
1185 break;
1186 if (!(keyword->op->type & KW_ATTRIBUTE))
1187 break;
1188 token = expect(token->next, '(', "after attribute");
1189 token = expect(token, '(', "after attribute");
1190 for (;;) {
1191 if (eof_token(token))
1192 break;
1193 if (match_op(token, ';'))
1194 break;
1195 if (token_type(token) != TOKEN_IDENT)
1196 break;
1197 token = skip_attribute(token);
1198 if (!match_op(token, ','))
1199 break;
1200 token = token->next;
1202 token = expect(token, ')', "after attribute");
1203 token = expect(token, ')', "after attribute");
1205 return token;
1208 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1210 struct symbol *keyword;
1211 for (;;) {
1212 struct ctype thistype = { 0, };
1213 if (token_type(token) != TOKEN_IDENT)
1214 break;
1215 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1216 if (!keyword || keyword->type != SYM_KEYWORD)
1217 break;
1218 if (!(keyword->op->type & keywords))
1219 break;
1220 token = keyword->op->declarator(token->next, &thistype);
1221 apply_ctype(token->pos, &thistype, ctype);
1222 keywords &= KW_ATTRIBUTE;
1224 return token;
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;
1244 return 1;
1247 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1248 return 0;
1250 return 1;
1253 enum kind {
1254 Empty, K_R, Proto, Bad_Func,
1257 static enum kind which_func(struct token *token,
1258 struct ident **n,
1259 int prefer_abstract)
1261 struct token *next = token->next;
1263 if (token_type(next) == TOKEN_IDENT) {
1264 if (lookup_type(next))
1265 return Proto;
1266 /* identifier list not in definition; complain */
1267 if (prefer_abstract)
1268 warning(token->pos,
1269 "identifier list not in definition");
1270 return K_R;
1273 if (token_type(next) != TOKEN_SPECIAL)
1274 return Bad_Func;
1276 if (next->special == ')') {
1277 /* don't complain about those */
1278 if (!n || match_op(next->next, ';'))
1279 return Empty;
1280 warning(next->pos,
1281 "non-ANSI function declaration of function '%s'",
1282 show_ident(*n));
1283 return Empty;
1286 if (next->special == SPECIAL_ELLIPSIS) {
1287 warning(next->pos,
1288 "variadic functions must have one named argument");
1289 return Proto;
1292 return Bad_Func;
1295 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1297 struct ctype *ctype = &ctx->ctype;
1298 struct token *next;
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,
1309 KW_ATTRIBUTE);
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;
1314 p = NULL;
1317 if (match_op(token, '(')) {
1318 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1319 struct symbol *fn;
1320 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1321 token = token->next;
1322 if (kind == K_R)
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;
1328 return token;
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;
1339 return token;
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;
1359 base_type = ptr;
1360 ctx->ctype.modifiers = modifiers & MOD_STORAGE;
1361 ctx->ctype.base_type = base_type;
1362 ctx->ctype.as = 0;
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;
1369 return token;
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;
1383 long long width;
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);
1399 width = -1;
1400 } else if (*ctx->ident && width == 0) {
1401 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1402 show_ident(*ctx->ident));
1403 width = -1;
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) &&
1416 is_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;
1423 return token;
1426 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1428 struct decl_state ctx = {.prefer_abstract = 0};
1429 struct ctype saved;
1431 token = declaration_specifiers(token, &ctx, 0);
1432 saved = ctx.ctype;
1433 for (;;) {
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, ','))
1448 break;
1449 token = token->next;
1450 ctx.ctype = saved;
1452 return token;
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");
1462 break;
1464 token = token->next;
1466 return token;
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;
1480 return token;
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);
1487 *p = sym;
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;
1496 return token;
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, ')'))
1512 return token->next;
1513 do {
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, ','));
1527 return token;
1530 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1531 struct expression_list **clobbers)
1533 struct expression *expr;
1535 do {
1536 token = primary_expression(token->next, &expr);
1537 add_expression(clobbers, expr);
1538 } while (match_op(token, ','));
1539 return 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");
1567 return token;
1570 /* Make a statement out of an expression */
1571 static struct statement *make_statement(struct expression *expr)
1573 struct statement *stmt;
1575 if (!expr)
1576 return NULL;
1577 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1578 stmt->expression = expr;
1579 return stmt;
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)
1610 end_symbol_scope();
1613 static struct statement *start_function(struct symbol *sym)
1615 struct symbol *ret;
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);
1624 stmt->ret = ret;
1625 fn_local_symbol(ret);
1627 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1628 current_fn = sym;
1630 return stmt;
1633 static void end_function(struct symbol *sym)
1635 current_fn = NULL;
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");
1675 end_symbol_scope();
1678 static void add_case_statement(struct statement *stmt)
1680 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1681 struct symbol *sym;
1683 if (!target) {
1684 sparse_error(stmt->pos, "not in switch scope");
1685 stmt->type = STMT_NONE;
1686 return;
1688 sym = alloc_symbol(stmt->pos, SYM_NODE);
1689 add_symbol(&target->symbol_list, sym);
1690 sym->stmt = stmt;
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);
1699 if (!target)
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'");
1715 syms = NULL;
1716 e1 = NULL;
1717 /* C99 variable declaration? */
1718 if (lookup_type(token)) {
1719 token = external_declaration(token, &syms);
1720 } else {
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;
1736 end_iterator(stmt);
1738 return token;
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;
1753 end_iterator(stmt);
1755 return token;
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;
1767 else
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;
1773 end_iterator(stmt);
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)
1787 return token;
1788 if (token->ident != &else_ident)
1789 return token;
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;
1819 if (!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;
1827 start_switch(stmt);
1828 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1829 token = statement(token, &stmt->switch_statement);
1830 end_switch(stmt);
1831 return token;
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;
1844 } else {
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);
1880 *tree = stmt;
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);
1898 end_symbol_scope();
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;
1910 for (;;) {
1911 struct statement * stmt;
1912 if (eof_token(token))
1913 break;
1914 if (match_op(token, '}'))
1915 break;
1916 if (lookup_type(token)) {
1917 if (seen_statement) {
1918 warning(token->pos, "mixing declarations and code");
1919 seen_statement = 0;
1921 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1922 token = external_declaration(token, &stmt->declaration);
1923 } else {
1924 seen_statement = Wdeclarationafterstatement;
1925 token = statement(token, &stmt);
1927 add_statement(list, stmt);
1929 return token;
1932 static struct token *identifier_list(struct token *token, struct symbol *fn)
1934 struct symbol_list **list = &fn->arguments;
1935 for (;;) {
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))
1944 break;
1945 token = token->next;
1947 return token;
1950 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1952 struct symbol_list **list = &fn->arguments;
1954 for (;;) {
1955 struct symbol *sym;
1957 if (match_op(token, SPECIAL_ELLIPSIS)) {
1958 fn->variadic = 1;
1959 token = token->next;
1960 break;
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)
1968 break;
1969 warning(token->pos, "void parameter");
1971 add_symbol(list, sym);
1972 if (!match_op(token, ','))
1973 break;
1974 token = token->next;
1976 return token;
1979 struct token *compound_statement(struct token *token, struct statement *stmt)
1981 token = statement_list(token, &stmt->stmts);
1982 return token;
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;
1989 return expr;
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);
1998 idx_to = idx_from;
1999 if (to) {
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;
2006 return expr;
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;
2014 int nested;
2016 *ep = NULL;
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)
2024 *ep = expr;
2025 return token;
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);
2031 *tail = expr;
2032 tail = &expr->ident_expression;
2033 expect_equal = 1;
2034 token = next->next;
2035 } else if (match_op(token, '[')) {
2036 struct expression *from = NULL, *to = NULL, *expr;
2037 token = constant_expression(token->next, &from);
2038 if (!from) {
2039 sparse_error(token->pos, "Expected constant expression");
2040 break;
2042 if (match_op(token, SPECIAL_ELLIPSIS))
2043 token = constant_expression(token->next, &to);
2044 expr = index_expression(from, to);
2045 *tail = expr;
2046 tail = &expr->idx_expression;
2047 token = expect(token, ']', "at end of initializer index");
2048 if (nested)
2049 expect_equal = 1;
2050 } else {
2051 break;
2054 if (nested && !expect_equal) {
2055 if (!match_op(token, '='))
2056 warning(token->pos, "obsolete array initializer, use C99 syntax");
2057 else
2058 expect_equal = 1;
2060 if (expect_equal)
2061 token = expect(token, '=', "at end of initializer index");
2063 token = initializer(tail, token);
2064 if (!*tail)
2065 *ep = NULL;
2066 return token;
2069 static struct token *initializer_list(struct expression_list **list, struct token *token)
2071 struct expression *expr;
2073 for (;;) {
2074 token = single_initializer(&expr, token);
2075 if (!expr)
2076 break;
2077 add_expression(list, expr);
2078 if (!match_op(token, ','))
2079 break;
2080 token = token->next;
2082 return token;
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);
2089 *tree = expr;
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)
2098 if (!sym->ident) {
2099 sparse_error(sym->pos, "no identifier for function argument");
2100 return;
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;
2112 struct symbol *arg;
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;
2118 } else {
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);
2134 *p = stmt;
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);
2141 end_function(decl);
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");
2151 } else {
2152 while (prev) {
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));
2161 else {
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;
2181 struct symbol *arg;
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)
2189 goto match;
2190 } END_FOR_EACH_PTR(type);
2191 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2192 continue;
2193 match:
2194 type->used = 1;
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) {
2202 if (!arg->used)
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));
2214 do {
2215 token = declaration_list(token, &args);
2216 if (!match_op(token, ';')) {
2217 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2218 break;
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");
2227 return token;
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);
2240 fn->stmt = stmt;
2242 token = parse_asm_statement(token, stmt);
2244 add_symbol(list, anon);
2245 return token;
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 };
2253 struct ctype saved;
2254 struct symbol *base_type;
2255 int is_typedef;
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);
2270 return token->next;
2273 saved = 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? */
2282 if (!ident) {
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 */
2291 if (is_typedef) {
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;
2301 if (is_typedef) {
2302 if (base_type && !base_type->ident) {
2303 switch (base_type->type) {
2304 case SYM_STRUCT:
2305 case SYM_UNION:
2306 case SYM_ENUM:
2307 case SYM_RESTRICT:
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");
2324 for (;;) {
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);
2332 if (!is_typedef) {
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, ','))
2343 break;
2345 token = token->next;
2346 ident = NULL;
2347 decl = alloc_symbol(token->pos, SYM_NODE);
2348 ctx.ctype = saved;
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;
2355 if (!ident) {
2356 sparse_error(token->pos, "expected identifier name in type definition");
2357 return token;
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");