Major memory saving.
[smatch.git] / parse.c
blob877414c066640ff18555e5850bced89104704cc4
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};
668 struct symbol_list *entries = NULL;
670 parent->examined = 1;
671 parent->ctype.base_type = &int_ctype;
672 while (token_type(token) == TOKEN_IDENT) {
673 struct expression *expr = NULL;
674 struct token *next = token->next;
675 struct symbol *sym;
677 sym = alloc_symbol(token->pos, SYM_NODE);
678 bind_symbol(sym, token->ident, NS_SYMBOL);
679 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
681 if (match_op(next, '=')) {
682 next = constant_expression(next->next, &expr);
683 lastval = get_expression_value(expr);
684 ctype = &void_ctype;
685 if (expr && expr->ctype)
686 ctype = expr->ctype;
687 } else if (!ctype) {
688 ctype = &int_ctype;
689 } else if (is_int_type(ctype)) {
690 lastval++;
691 } else {
692 error_die(token->pos, "can't increment the last enum member");
695 if (!expr) {
696 expr = alloc_expression(token->pos, EXPR_VALUE);
697 expr->value = lastval;
698 expr->ctype = ctype;
701 sym->initializer = expr;
702 sym->enum_member = 1;
703 sym->ctype.base_type = parent;
704 add_ptr_list(&entries, sym);
706 if (base_type != &bad_ctype) {
707 if (ctype->type == SYM_NODE)
708 ctype = ctype->ctype.base_type;
709 if (ctype->type == SYM_ENUM) {
710 if (ctype == parent)
711 ctype = base_type;
712 else
713 ctype = ctype->ctype.base_type;
716 * base_type rules:
717 * - if all enums are of the same type, then
718 * the base_type is that type (two first
719 * cases)
720 * - if enums are of different types, they
721 * all have to be integer types, and the
722 * base type is at least "int_ctype".
723 * - otherwise the base_type is "bad_ctype".
725 if (!base_type) {
726 base_type = ctype;
727 } else if (ctype == base_type) {
728 /* nothing */
729 } else if (is_int_type(base_type) && is_int_type(ctype)) {
730 base_type = bigger_enum_type(base_type, ctype);
731 } else
732 base_type = &bad_ctype;
733 parent->ctype.base_type = base_type;
735 if (is_int_type(base_type)) {
736 Num v = {.y = lastval};
737 if (ctype->ctype.modifiers & MOD_UNSIGNED)
738 v.x = 0;
739 else if ((long long)lastval >= 0)
740 v.x = 0;
741 else
742 v.x = -1;
743 upper_boundary(&upper, &v);
744 lower_boundary(&lower, &v);
746 token = next;
748 sym->endpos = token->pos;
750 if (!match_op(token, ','))
751 break;
752 token = token->next;
754 if (!base_type) {
755 sparse_error(token->pos, "bad enum definition");
756 base_type = &bad_ctype;
758 else if (!is_int_type(base_type))
759 base_type = base_type;
760 else if (type_is_ok(base_type, &upper, &lower))
761 base_type = base_type;
762 else if (type_is_ok(&int_ctype, &upper, &lower))
763 base_type = &int_ctype;
764 else if (type_is_ok(&uint_ctype, &upper, &lower))
765 base_type = &uint_ctype;
766 else if (type_is_ok(&long_ctype, &upper, &lower))
767 base_type = &long_ctype;
768 else if (type_is_ok(&ulong_ctype, &upper, &lower))
769 base_type = &ulong_ctype;
770 else if (type_is_ok(&llong_ctype, &upper, &lower))
771 base_type = &llong_ctype;
772 else if (type_is_ok(&ullong_ctype, &upper, &lower))
773 base_type = &ullong_ctype;
774 else
775 base_type = &bad_ctype;
776 parent->ctype.base_type = base_type;
777 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
778 parent->examined = 0;
780 cast_enum_list(entries, base_type);
781 free_ptr_list(&entries);
783 return token;
786 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
788 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
790 ctype = &ctype->base_type->ctype;
791 if (!ctype->base_type)
792 ctype->base_type = &incomplete_ctype;
794 return ret;
797 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
799 struct symbol *sym;
801 if (!match_op(token, '(')) {
802 sparse_error(token->pos, "expected '(' after typeof");
803 return token;
805 if (lookup_type(token->next)) {
806 token = typename(token->next, &sym, 0);
807 *ctype = sym->ctype;
808 } else {
809 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
810 token = parse_expression(token->next, &typeof_sym->initializer);
812 ctype->modifiers = 0;
813 typeof_sym->endpos = token->pos;
814 ctype->base_type = typeof_sym;
816 return expect(token, ')', "after typeof");
819 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
821 struct expression *expr = NULL;
822 if (match_op(token, '('))
823 token = parens_expression(token, &expr, "in attribute");
824 return token;
827 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
829 ctype->alignment = 1;
830 return token;
833 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
835 int alignment = max_alignment;
836 struct expression *expr = NULL;
838 if (match_op(token, '(')) {
839 token = parens_expression(token, &expr, "in attribute");
840 if (expr)
841 alignment = const_expression_value(expr);
843 ctype->alignment = alignment;
844 return token;
847 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
849 ctype->modifiers |= attr->ctype.modifiers;
850 return token;
853 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
855 struct expression *expr = NULL;
856 token = expect(token, '(', "after address_space attribute");
857 token = conditional_expression(token, &expr);
858 if (expr)
859 ctype->as = const_expression_value(expr);
860 token = expect(token, ')', "after address_space attribute");
861 return token;
864 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
866 token = expect(token, '(', "after mode attribute");
867 if (token_type(token) == TOKEN_IDENT) {
868 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
869 if (mode && mode->op->type == KW_MODE)
870 ctype->modifiers |= mode->ctype.modifiers;
871 else
872 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
873 token = token->next;
874 } else
875 sparse_error(token->pos, "expect attribute mode symbol\n");
876 token = expect(token, ')', "after mode attribute");
877 return token;
880 static struct token *_attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype, int exact)
882 struct context *context = alloc_context();
883 struct expression *args[3];
884 int argc = 0;
886 token = expect(token, '(', "after context attribute");
887 while (!match_op(token, ')')) {
888 struct expression *expr = NULL;
889 token = conditional_expression(token, &expr);
890 if (!expr)
891 break;
892 if (argc < 3)
893 args[argc++] = expr;
894 else
895 argc++;
896 if (!match_op(token, ','))
897 break;
898 token = token->next;
901 switch(argc) {
902 case 0:
903 sparse_error(token->pos, "expected context input/output values");
904 break;
905 case 1:
906 context->in = get_expression_value(args[0]);
907 break;
908 case 2:
909 context->in = get_expression_value(args[0]);
910 context->out = get_expression_value(args[1]);
911 break;
912 case 3:
913 context->context = args[0];
914 context->in = get_expression_value(args[1]);
915 context->out = get_expression_value(args[2]);
916 break;
917 default:
918 sparse_error(token->pos, "too many arguments to context attribute");
919 break;
922 context->exact = exact;
923 context->out_false = context->out;
925 if (argc)
926 add_ptr_list(&ctype->contexts, context);
928 token = expect(token, ')', "after context attribute");
929 return token;
932 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
934 return _attribute_context(token, attr, ctype, 0);
937 static struct token *attribute_exact_context(struct token *token, struct symbol *attr, struct ctype *ctype)
939 return _attribute_context(token, attr, ctype, 1);
942 static struct token *attribute_conditional_context(struct token *token, struct symbol *attr, struct ctype *ctype)
944 struct context *context = alloc_context();
945 struct expression *args[4];
946 int argc = 0;
948 token = expect(token, '(', "after conditional_context attribute");
949 while (!match_op(token, ')')) {
950 struct expression *expr = NULL;
951 token = conditional_expression(token, &expr);
952 if (!expr)
953 break;
954 if (argc < 4)
955 args[argc++] = expr;
956 else
957 argc++;
958 if (!match_op(token, ','))
959 break;
960 token = token->next;
963 switch(argc) {
964 case 3:
965 context->in = get_expression_value(args[0]);
966 context->out = get_expression_value(args[1]);
967 context->out_false = get_expression_value(args[2]);
968 break;
969 case 4:
970 context->context = args[0];
971 context->in = get_expression_value(args[1]);
972 context->out = get_expression_value(args[2]);
973 context->out_false = get_expression_value(args[3]);
974 break;
975 default:
976 sparse_error(token->pos, "invalid number of arguments to conditional_context attribute");
977 break;
980 if (argc)
981 add_ptr_list(&ctype->contexts, context);
983 token = expect(token, ')', "after conditional_context attribute");
984 return token;
987 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
989 if (Wtransparent_union)
990 sparse_error(token->pos, "ignoring attribute __transparent_union__");
991 return token;
994 static struct token *recover_unknown_attribute(struct token *token)
996 struct expression *expr = NULL;
998 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
999 token = token->next;
1000 if (match_op(token, '('))
1001 token = parens_expression(token, &expr, "in attribute");
1002 return token;
1005 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
1007 ctype->modifiers = 0;
1008 token = expect(token, '(', "after attribute");
1009 token = expect(token, '(', "after attribute");
1011 for (;;) {
1012 struct ident *attribute_name;
1013 struct symbol *attr;
1015 if (eof_token(token))
1016 break;
1017 if (match_op(token, ';'))
1018 break;
1019 if (token_type(token) != TOKEN_IDENT)
1020 break;
1021 attribute_name = token->ident;
1022 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1023 if (attr && attr->op->attribute)
1024 token = attr->op->attribute(token->next, attr, ctype);
1025 else
1026 token = recover_unknown_attribute(token);
1028 if (!match_op(token, ','))
1029 break;
1030 token = token->next;
1033 token = expect(token, ')', "after attribute");
1034 token = expect(token, ')', "after attribute");
1035 return token;
1038 struct symbol * ctype_integer(unsigned long spec)
1040 static struct symbol *const integer_ctypes[][3] = {
1041 { &llong_ctype, &sllong_ctype, &ullong_ctype },
1042 { &long_ctype, &slong_ctype, &ulong_ctype },
1043 { &short_ctype, &sshort_ctype, &ushort_ctype },
1044 { &char_ctype, &schar_ctype, &uchar_ctype },
1045 { &int_ctype, &sint_ctype, &uint_ctype },
1047 struct symbol *const (*ctype)[3];
1048 int sub;
1050 ctype = integer_ctypes;
1051 if (!(spec & MOD_LONGLONG)) {
1052 ctype++;
1053 if (!(spec & MOD_LONG)) {
1054 ctype++;
1055 if (!(spec & MOD_SHORT)) {
1056 ctype++;
1057 if (!(spec & MOD_CHAR))
1058 ctype++;
1063 sub = ((spec & MOD_UNSIGNED)
1065 : ((spec & MOD_EXPLICITLY_SIGNED)
1067 : 0));
1069 return ctype[0][sub];
1072 struct symbol * ctype_fp(unsigned long spec)
1074 if (spec & MOD_LONGLONG)
1075 return &ldouble_ctype;
1076 if (spec & MOD_LONG)
1077 return &double_ctype;
1078 return &float_ctype;
1081 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1083 unsigned long mod = thistype->modifiers;
1085 if (mod) {
1086 unsigned long old = ctype->modifiers;
1087 unsigned long extra = 0, dup, conflict;
1089 if (mod & old & MOD_LONG) {
1090 extra = MOD_LONGLONG | MOD_LONG;
1091 mod &= ~MOD_LONG;
1092 old &= ~MOD_LONG;
1094 dup = (mod & old) | (extra & old) | (extra & mod);
1095 if (dup)
1096 sparse_error(pos, "Just how %sdo you want this type to be?",
1097 modifier_string(dup));
1099 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
1100 if (conflict)
1101 sparse_error(pos, "You cannot have both long and short modifiers.");
1103 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
1104 if (conflict)
1105 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
1107 // Only one storage modifier allowed, except that "inline" doesn't count.
1108 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
1109 conflict &= (conflict - 1);
1110 if (conflict)
1111 sparse_error(pos, "multiple storage classes");
1113 ctype->modifiers = old | mod | extra;
1116 /* Context */
1117 concat_ptr_list((struct ptr_list *)thistype->contexts,
1118 (struct ptr_list **)&ctype->contexts);
1120 /* Alignment */
1121 if (thistype->alignment & (thistype->alignment-1)) {
1122 warning(pos, "I don't like non-power-of-2 alignments");
1123 thistype->alignment = 0;
1125 if (thistype->alignment > ctype->alignment)
1126 ctype->alignment = thistype->alignment;
1128 /* Address space */
1129 if (thistype->as)
1130 ctype->as = thistype->as;
1133 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
1135 unsigned long banned, wrong;
1136 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1137 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1139 if (s->type == SYM_KEYWORD)
1140 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1141 else if (s->ctype.base_type == &fp_type)
1142 banned = BANNED_SIGN;
1143 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1144 banned = 0;
1145 else {
1146 // label_type
1147 // void_type
1148 // bad_type
1149 // vector_type <-- whatever that is
1150 banned = BANNED_SIZE | BANNED_SIGN;
1153 wrong = mod & banned;
1154 if (wrong)
1155 sparse_error(*pos, "modifier %sis invalid in this context",
1156 modifier_string (wrong));
1159 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1161 struct token *token;
1163 while ( (token = next) != NULL ) {
1164 struct ctype thistype;
1165 struct ident *ident;
1166 struct symbol *s, *type;
1167 unsigned long mod;
1169 next = token->next;
1170 if (token_type(token) != TOKEN_IDENT)
1171 break;
1172 ident = token->ident;
1174 s = lookup_symbol(ident, NS_TYPEDEF);
1175 if (!s)
1176 break;
1177 thistype = s->ctype;
1178 mod = thistype.modifiers;
1179 if (qual) {
1180 if (s->type != SYM_KEYWORD)
1181 break;
1182 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1183 break;
1185 if (s->type == SYM_KEYWORD && s->op->declarator) {
1186 next = s->op->declarator(next, &thistype);
1187 mod = thistype.modifiers;
1189 type = thistype.base_type;
1190 if (type) {
1191 if (qual)
1192 break;
1193 if (ctype->base_type)
1194 break;
1195 /* User types only mix with qualifiers */
1196 if (mod & MOD_USERTYPE) {
1197 if (ctype->modifiers & MOD_SPECIFIER)
1198 break;
1200 ctype->base_type = type;
1203 check_modifiers(&token->pos, s, ctype->modifiers);
1204 apply_ctype(token->pos, &thistype, ctype);
1207 if (!ctype->base_type) {
1208 struct symbol *base = &incomplete_ctype;
1211 * If we have modifiers, we'll default to an integer
1212 * type, and "ctype_integer()" will turn this into
1213 * a specific one.
1215 if (ctype->modifiers & MOD_SPECIFIER)
1216 base = &int_type;
1217 ctype->base_type = base;
1219 return token;
1222 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1224 struct expression *expr = NULL;
1226 token = parse_expression(token, &expr);
1227 sym->array_size = expr;
1228 return token;
1231 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1232 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1234 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1236 struct symbol *keyword;
1237 for (;;) {
1238 struct ctype thistype = { 0, };
1239 if (token_type(token) != TOKEN_IDENT)
1240 break;
1241 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1242 if (!keyword || keyword->type != SYM_KEYWORD)
1243 break;
1244 if (!(keyword->op->type & keywords))
1245 break;
1246 token = keyword->op->declarator(token->next, &thistype);
1247 apply_ctype(token->pos, &thistype, ctype);
1249 return token;
1252 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1254 struct ctype *ctype = &decl->ctype;
1256 if (p && token_type(token) == TOKEN_IDENT) {
1257 *p = token->ident;
1258 token = token->next;
1261 for (;;) {
1262 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
1264 if (token_type(token) != TOKEN_SPECIAL)
1265 return token;
1268 * This can be either a parameter list or a grouping.
1269 * For the direct (non-abstract) case, we know if must be
1270 * a parameter list if we already saw the identifier.
1271 * For the abstract case, we know if must be a parameter
1272 * list if it is empty or starts with a type.
1274 if (token->special == '(') {
1275 struct symbol *sym;
1276 struct token *next = token->next;
1277 int fn;
1279 next = handle_attributes(next, ctype, KW_ATTRIBUTE);
1280 fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1282 if (!fn) {
1283 struct symbol *base_type = ctype->base_type;
1284 token = declarator(next, decl, p);
1285 token = expect(token, ')', "in nested declarator");
1286 while (ctype->base_type != base_type)
1287 ctype = &ctype->base_type->ctype;
1288 p = NULL;
1289 continue;
1292 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1293 token = parameter_type_list(next, sym, p);
1294 token = expect(token, ')', "in function declarator");
1295 sym->endpos = token->pos;
1296 continue;
1298 if (token->special == '[') {
1299 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1300 token = abstract_array_declarator(token->next, array);
1301 token = expect(token, ']', "in abstract_array_declarator");
1302 array->endpos = token->pos;
1303 ctype = &array->ctype;
1304 continue;
1306 break;
1308 return token;
1311 static struct token *pointer(struct token *token, struct ctype *ctype)
1313 unsigned long modifiers;
1314 struct symbol *base_type;
1316 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1317 base_type = ctype->base_type;
1318 ctype->modifiers = modifiers;
1320 while (match_op(token,'*')) {
1321 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1322 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1323 ptr->ctype.as = ctype->as;
1324 concat_ptr_list((struct ptr_list *)ctype->contexts,
1325 (struct ptr_list **)&ptr->ctype.contexts);
1326 ptr->ctype.base_type = base_type;
1328 base_type = ptr;
1329 ctype->modifiers = modifiers & MOD_STORAGE;
1330 ctype->base_type = base_type;
1331 ctype->as = 0;
1332 free_ptr_list(&ctype->contexts);
1334 token = declaration_specifiers(token->next, ctype, 1);
1335 modifiers = ctype->modifiers;
1336 ctype->base_type->endpos = token->pos;
1338 return token;
1341 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1343 token = pointer(token, &sym->ctype);
1344 return direct_declarator(token, sym, p);
1347 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1349 struct ctype *ctype = &decl->ctype;
1350 struct expression *expr;
1351 struct symbol *bitfield;
1352 long long width;
1354 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1355 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1356 show_typename(ctype->base_type));
1357 // Parse this to recover gracefully.
1358 return conditional_expression(token->next, &expr);
1361 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1362 token = conditional_expression(token->next, &expr);
1363 width = const_expression_value(expr);
1364 bitfield->bit_size = width;
1366 if (width < 0 || width > INT_MAX) {
1367 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1368 width = -1;
1369 } else if (decl->ident && width == 0) {
1370 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1371 show_ident(decl->ident));
1372 width = -1;
1373 } else if (decl->ident) {
1374 struct symbol *base_type = bitfield->ctype.base_type;
1375 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1376 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1377 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1378 // Valid values are either {-1;0} or {0}, depending on integer
1379 // representation. The latter makes for very efficient code...
1380 sparse_error(token->pos, "dubious one-bit signed bitfield");
1382 if (Wdefault_bitfield_sign &&
1383 bitfield_type->type != SYM_ENUM &&
1384 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1385 is_signed) {
1386 // The sign of bitfields is unspecified by default.
1387 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1390 bitfield->bit_size = width;
1391 bitfield->endpos = token->pos;
1392 return token;
1395 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1397 struct ctype ctype = {0, };
1399 token = declaration_specifiers(token, &ctype, 0);
1400 for (;;) {
1401 struct ident *ident = NULL;
1402 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1403 decl->ctype = ctype;
1404 token = declarator(token, decl, &ident);
1405 decl->ident = ident;
1406 if (match_op(token, ':')) {
1407 token = handle_bitfield(token, decl);
1408 token = handle_attributes(token, &decl->ctype, KW_ATTRIBUTE | KW_ASM);
1410 apply_modifiers(token->pos, &decl->ctype);
1411 add_symbol(list, decl);
1412 decl->endpos = token->pos;
1413 if (!match_op(token, ','))
1414 break;
1415 token = token->next;
1417 return token;
1420 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1422 while (!match_op(token, '}')) {
1423 if (!match_op(token, ';'))
1424 token = declaration_list(token, list);
1425 if (!match_op(token, ';')) {
1426 sparse_error(token->pos, "expected ; at end of declaration");
1427 break;
1429 token = token->next;
1431 return token;
1434 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1436 struct ident *ident = NULL;
1437 struct symbol *sym;
1438 struct ctype ctype = { 0, };
1440 token = declaration_specifiers(token, &ctype, 0);
1441 sym = alloc_symbol(token->pos, SYM_NODE);
1442 sym->ctype = ctype;
1443 *tree = sym;
1444 token = declarator(token, sym, &ident);
1445 sym->ident = ident;
1446 apply_modifiers(token->pos, &sym->ctype);
1447 sym->endpos = token->pos;
1448 return token;
1451 struct token *typename(struct token *token, struct symbol **p, int mod)
1453 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1454 *p = sym;
1455 token = declaration_specifiers(token, &sym->ctype, 0);
1456 token = declarator(token, sym, NULL);
1457 apply_modifiers(token->pos, &sym->ctype);
1458 if (sym->ctype.modifiers & MOD_STORAGE & ~mod)
1459 warning(sym->pos, "storage class in typename (%s)",
1460 show_typename(sym));
1461 sym->endpos = token->pos;
1462 return token;
1465 static struct token *expression_statement(struct token *token, struct expression **tree)
1467 token = parse_expression(token, tree);
1468 return expect(token, ';', "at end of statement");
1471 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1472 struct expression_list **inout)
1474 struct expression *expr;
1476 /* Allow empty operands */
1477 if (match_op(token->next, ':') || match_op(token->next, ')'))
1478 return token->next;
1479 do {
1480 struct ident *ident = NULL;
1481 if (match_op(token->next, '[') &&
1482 token_type(token->next->next) == TOKEN_IDENT &&
1483 match_op(token->next->next->next, ']')) {
1484 ident = token->next->next->ident;
1485 token = token->next->next->next;
1487 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1488 token = primary_expression(token->next, &expr);
1489 add_expression(inout, expr);
1490 token = parens_expression(token, &expr, "in asm parameter");
1491 add_expression(inout, expr);
1492 } while (match_op(token, ','));
1493 return token;
1496 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1497 struct expression_list **clobbers)
1499 struct expression *expr;
1501 do {
1502 token = primary_expression(token->next, &expr);
1503 add_expression(clobbers, expr);
1504 } while (match_op(token, ','));
1505 return token;
1508 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1510 token = token->next;
1511 stmt->type = STMT_ASM;
1512 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1513 token = token->next;
1515 token = expect(token, '(', "after asm");
1516 token = parse_expression(token, &stmt->asm_string);
1517 if (match_op(token, ':'))
1518 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1519 if (match_op(token, ':'))
1520 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1521 if (match_op(token, ':'))
1522 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1523 token = expect(token, ')', "after asm");
1524 return expect(token, ';', "at end of asm-statement");
1527 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1529 struct expression *expr;
1530 token = expect(token, '(', "after asm");
1531 token = parse_expression(token->next, &expr);
1532 token = expect(token, ')', "after asm");
1533 return token;
1536 /* Make a statement out of an expression */
1537 static struct statement *make_statement(struct expression *expr)
1539 struct statement *stmt;
1541 if (!expr)
1542 return NULL;
1543 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1544 stmt->expression = expr;
1545 return stmt;
1549 * All iterators have two symbols associated with them:
1550 * the "continue" and "break" symbols, which are targets
1551 * for continue and break statements respectively.
1553 * They are in a special name-space, but they follow
1554 * all the normal visibility rules, so nested iterators
1555 * automatically work right.
1557 static void start_iterator(struct statement *stmt)
1559 struct symbol *cont, *brk;
1561 start_symbol_scope();
1562 cont = alloc_symbol(stmt->pos, SYM_NODE);
1563 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1564 brk = alloc_symbol(stmt->pos, SYM_NODE);
1565 bind_symbol(brk, &break_ident, NS_ITERATOR);
1567 stmt->type = STMT_ITERATOR;
1568 stmt->iterator_break = brk;
1569 stmt->iterator_continue = cont;
1570 fn_local_symbol(brk);
1571 fn_local_symbol(cont);
1574 static void end_iterator(struct statement *stmt)
1576 end_symbol_scope();
1579 static struct statement *start_function(struct symbol *sym)
1581 struct symbol *ret;
1582 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1584 start_function_scope();
1585 ret = alloc_symbol(sym->pos, SYM_NODE);
1586 ret->ctype = sym->ctype.base_type->ctype;
1587 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1588 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1589 bind_symbol(ret, &return_ident, NS_ITERATOR);
1590 stmt->ret = ret;
1591 fn_local_symbol(ret);
1593 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1594 current_fn = sym;
1596 return stmt;
1599 static void end_function(struct symbol *sym)
1601 current_fn = NULL;
1602 end_function_scope();
1606 * A "switch()" statement, like an iterator, has a
1607 * the "break" symbol associated with it. It works
1608 * exactly like the iterator break - it's the target
1609 * for any break-statements in scope, and means that
1610 * "break" handling doesn't even need to know whether
1611 * it's breaking out of an iterator or a switch.
1613 * In addition, the "case" symbol is a marker for the
1614 * case/default statements to find the switch statement
1615 * that they are associated with.
1617 static void start_switch(struct statement *stmt)
1619 struct symbol *brk, *switch_case;
1621 start_symbol_scope();
1622 brk = alloc_symbol(stmt->pos, SYM_NODE);
1623 bind_symbol(brk, &break_ident, NS_ITERATOR);
1625 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1626 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1627 switch_case->stmt = stmt;
1629 stmt->type = STMT_SWITCH;
1630 stmt->switch_break = brk;
1631 stmt->switch_case = switch_case;
1633 fn_local_symbol(brk);
1634 fn_local_symbol(switch_case);
1637 static void end_switch(struct statement *stmt)
1639 if (!stmt->switch_case->symbol_list)
1640 warning(stmt->pos, "switch with no cases");
1641 end_symbol_scope();
1644 static void add_case_statement(struct statement *stmt)
1646 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1647 struct symbol *sym;
1649 if (!target) {
1650 sparse_error(stmt->pos, "not in switch scope");
1651 stmt->type = STMT_NONE;
1652 return;
1654 sym = alloc_symbol(stmt->pos, SYM_NODE);
1655 add_symbol(&target->symbol_list, sym);
1656 sym->stmt = stmt;
1657 stmt->case_label = sym;
1658 fn_local_symbol(sym);
1661 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1663 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1665 if (!target)
1666 error_die(token->pos, "internal error: return without a function target");
1667 stmt->type = STMT_RETURN;
1668 stmt->ret_target = target;
1669 return expression_statement(token->next, &stmt->ret_value);
1672 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1674 struct symbol_list *syms;
1675 struct expression *e1, *e2, *e3;
1676 struct statement *iterator;
1678 start_iterator(stmt);
1679 token = expect(token->next, '(', "after 'for'");
1681 syms = NULL;
1682 e1 = NULL;
1683 /* C99 variable declaration? */
1684 if (lookup_type(token)) {
1685 token = external_declaration(token, &syms);
1686 } else {
1687 token = parse_expression(token, &e1);
1688 token = expect(token, ';', "in 'for'");
1690 token = parse_expression(token, &e2);
1691 token = expect(token, ';', "in 'for'");
1692 token = parse_expression(token, &e3);
1693 token = expect(token, ')', "in 'for'");
1694 token = statement(token, &iterator);
1696 stmt->iterator_syms = syms;
1697 stmt->iterator_pre_statement = make_statement(e1);
1698 stmt->iterator_pre_condition = e2;
1699 stmt->iterator_post_statement = make_statement(e3);
1700 stmt->iterator_post_condition = NULL;
1701 stmt->iterator_statement = iterator;
1702 end_iterator(stmt);
1704 return token;
1707 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1709 struct expression *expr;
1710 struct statement *iterator;
1712 start_iterator(stmt);
1713 token = parens_expression(token->next, &expr, "after 'while'");
1714 token = statement(token, &iterator);
1716 stmt->iterator_pre_condition = expr;
1717 stmt->iterator_post_condition = NULL;
1718 stmt->iterator_statement = iterator;
1719 end_iterator(stmt);
1721 return token;
1724 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1726 struct expression *expr;
1727 struct statement *iterator;
1729 start_iterator(stmt);
1730 token = statement(token->next, &iterator);
1731 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1732 token = token->next;
1733 else
1734 sparse_error(token->pos, "expected 'while' after 'do'");
1735 token = parens_expression(token, &expr, "after 'do-while'");
1737 stmt->iterator_post_condition = expr;
1738 stmt->iterator_statement = iterator;
1739 end_iterator(stmt);
1741 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1742 warning(iterator->pos, "do-while statement is not a compound statement");
1744 return expect(token, ';', "after statement");
1747 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1749 stmt->type = STMT_IF;
1750 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1751 token = statement(token, &stmt->if_true);
1752 if (token_type(token) != TOKEN_IDENT)
1753 return token;
1754 if (token->ident != &else_ident)
1755 return token;
1756 return statement(token->next, &stmt->if_false);
1759 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1761 stmt->type = STMT_CASE;
1762 token = expect(token, ':', "after default/case");
1763 add_case_statement(stmt);
1764 return statement(token, &stmt->case_statement);
1767 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1769 token = parse_expression(token->next, &stmt->case_expression);
1770 if (match_op(token, SPECIAL_ELLIPSIS))
1771 token = parse_expression(token->next, &stmt->case_to);
1772 return case_statement(token, stmt);
1775 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1777 return case_statement(token->next, stmt);
1780 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1782 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1783 stmt->type = STMT_GOTO;
1784 stmt->goto_label = target;
1785 if (!target)
1786 sparse_error(stmt->pos, "break/continue not in iterator scope");
1787 return expect(token->next, ';', "at end of statement");
1790 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1792 stmt->type = STMT_SWITCH;
1793 start_switch(stmt);
1794 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1795 token = statement(token, &stmt->switch_statement);
1796 end_switch(stmt);
1797 return token;
1800 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1802 stmt->type = STMT_GOTO;
1803 token = token->next;
1804 if (match_op(token, '*')) {
1805 token = parse_expression(token->next, &stmt->goto_expression);
1806 add_statement(&function_computed_goto_list, stmt);
1807 } else if (token_type(token) == TOKEN_IDENT) {
1808 stmt->goto_label = label_symbol(token);
1809 token = token->next;
1810 } else {
1811 sparse_error(token->pos, "Expected identifier or goto expression");
1813 return expect(token, ';', "at end of statement");
1816 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1818 struct expression *args[3];
1819 int argc = 0;
1821 stmt->type = STMT_CONTEXT;
1822 token = token->next;
1823 token = expect(token, '(', "after __context__ statement");
1824 while (!match_op(token, ')')) {
1825 struct expression *expr = NULL;
1826 token = conditional_expression(token, &expr);
1827 if (!expr)
1828 break;
1829 if (argc < 3)
1830 args[argc++] = expr;
1831 else
1832 argc++;
1833 if (!match_op(token, ','))
1834 break;
1835 token = token->next;
1838 stmt->expression = args[0];
1839 stmt->context = NULL;
1841 switch (argc) {
1842 case 0:
1843 sparse_error(token->pos, "__context__ statement needs argument(s)");
1844 return token;
1845 case 1:
1846 /* already done */
1847 break;
1848 case 2:
1849 if (args[0]->type != STMT_EXPRESSION) {
1850 stmt->context = args[0];
1851 stmt->expression = args[1];
1852 } else {
1853 stmt->expression = args[0];
1854 stmt->required = args[1];
1856 break;
1857 case 3:
1858 stmt->context = args[0];
1859 stmt->expression = args[1];
1860 stmt->required = args[2];
1861 break;
1862 default:
1863 sparse_error(token->pos, "too many arguments for __context__ statement");
1864 return token->next;
1867 return expect(token, ')', "at end of __context__");
1870 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1872 stmt->type = STMT_RANGE;
1873 token = assignment_expression(token->next, &stmt->range_expression);
1874 token = expect(token, ',', "after range expression");
1875 token = assignment_expression(token, &stmt->range_low);
1876 token = expect(token, ',', "after low range");
1877 token = assignment_expression(token, &stmt->range_high);
1878 return expect(token, ';', "after range statement");
1881 static struct token *statement(struct token *token, struct statement **tree)
1883 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1885 *tree = stmt;
1886 if (token_type(token) == TOKEN_IDENT) {
1887 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1888 if (s && s->op->statement)
1889 return s->op->statement(token, stmt);
1891 if (match_op(token->next, ':')) {
1892 stmt->type = STMT_LABEL;
1893 stmt->label_identifier = label_symbol(token);
1894 token = handle_attributes(token->next->next, &stmt->label_identifier->ctype, KW_ATTRIBUTE);
1895 return statement(token, &stmt->label_statement);
1899 if (match_op(token, '{')) {
1900 stmt->type = STMT_COMPOUND;
1901 start_symbol_scope();
1902 token = compound_statement(token->next, stmt);
1903 end_symbol_scope();
1905 return expect(token, '}', "at end of compound statement");
1908 stmt->type = STMT_EXPRESSION;
1909 return expression_statement(token, &stmt->expression);
1912 static struct token * statement_list(struct token *token, struct statement_list **list)
1914 int seen_statement = 0;
1915 for (;;) {
1916 struct statement * stmt;
1917 if (eof_token(token))
1918 break;
1919 if (match_op(token, '}'))
1920 break;
1921 if (lookup_type(token)) {
1922 if (seen_statement) {
1923 warning(token->pos, "mixing declarations and code");
1924 seen_statement = 0;
1926 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1927 token = external_declaration(token, &stmt->declaration);
1928 } else {
1929 seen_statement = Wdeclarationafterstatement;
1930 token = statement(token, &stmt);
1932 add_statement(list, stmt);
1934 return token;
1937 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1939 struct symbol_list **list = &fn->arguments;
1941 if (match_op(token, ')')) {
1942 // No warning for "void oink ();"
1943 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1944 if (p && !match_op(token->next, ';'))
1945 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1946 return token;
1949 for (;;) {
1950 struct symbol *sym;
1952 if (match_op(token, SPECIAL_ELLIPSIS)) {
1953 if (!*list)
1954 warning(token->pos, "variadic functions must have one named argument");
1955 fn->variadic = 1;
1956 token = token->next;
1957 break;
1960 sym = alloc_symbol(token->pos, SYM_NODE);
1961 token = parameter_declaration(token, &sym);
1962 if (sym->ctype.base_type == &void_ctype) {
1963 /* Special case: (void) */
1964 if (!*list && !sym->ident)
1965 break;
1966 warning(token->pos, "void parameter");
1968 add_symbol(list, sym);
1969 sym->endpos = token->pos;
1970 if (!match_op(token, ','))
1971 break;
1972 token = token->next;
1975 return token;
1978 struct token *compound_statement(struct token *token, struct statement *stmt)
1980 token = statement_list(token, &stmt->stmts);
1981 return token;
1984 static struct expression *identifier_expression(struct token *token)
1986 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1987 expr->expr_ident = token->ident;
1988 return expr;
1991 static struct expression *index_expression(struct expression *from, struct expression *to)
1993 int idx_from, idx_to;
1994 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1996 idx_from = const_expression_value(from);
1997 idx_to = idx_from;
1998 if (to) {
1999 idx_to = const_expression_value(to);
2000 if (idx_to < idx_from || idx_from < 0)
2001 warning(from->pos, "nonsense array initializer index range");
2003 expr->idx_from = idx_from;
2004 expr->idx_to = idx_to;
2005 return expr;
2008 static struct token *single_initializer(struct expression **ep, struct token *token)
2010 int expect_equal = 0;
2011 struct token *next = token->next;
2012 struct expression **tail = ep;
2013 int nested;
2015 *ep = NULL;
2017 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2018 struct expression *expr = identifier_expression(token);
2019 if (Wold_initializer)
2020 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2021 token = initializer(&expr->ident_expression, next->next);
2022 if (expr->ident_expression)
2023 *ep = expr;
2024 return token;
2027 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2028 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2029 struct expression *expr = identifier_expression(next);
2030 *tail = expr;
2031 tail = &expr->ident_expression;
2032 expect_equal = 1;
2033 token = next->next;
2034 } else if (match_op(token, '[')) {
2035 struct expression *from = NULL, *to = NULL, *expr;
2036 token = constant_expression(token->next, &from);
2037 if (!from) {
2038 sparse_error(token->pos, "Expected constant expression");
2039 break;
2041 if (match_op(token, SPECIAL_ELLIPSIS))
2042 token = constant_expression(token->next, &to);
2043 expr = index_expression(from, to);
2044 *tail = expr;
2045 tail = &expr->idx_expression;
2046 token = expect(token, ']', "at end of initializer index");
2047 if (nested)
2048 expect_equal = 1;
2049 } else {
2050 break;
2053 if (nested && !expect_equal) {
2054 if (!match_op(token, '='))
2055 warning(token->pos, "obsolete array initializer, use C99 syntax");
2056 else
2057 expect_equal = 1;
2059 if (expect_equal)
2060 token = expect(token, '=', "at end of initializer index");
2062 token = initializer(tail, token);
2063 if (!*tail)
2064 *ep = NULL;
2065 return token;
2068 static struct token *initializer_list(struct expression_list **list, struct token *token)
2070 struct expression *expr;
2072 for (;;) {
2073 token = single_initializer(&expr, token);
2074 if (!expr)
2075 break;
2076 add_expression(list, expr);
2077 if (!match_op(token, ','))
2078 break;
2079 token = token->next;
2081 return token;
2084 struct token *initializer(struct expression **tree, struct token *token)
2086 if (match_op(token, '{')) {
2087 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2088 *tree = expr;
2089 token = initializer_list(&expr->expr_list, token->next);
2090 return expect(token, '}', "at end of initializer");
2092 return assignment_expression(token, tree);
2095 static void declare_argument(struct symbol *sym, struct symbol *fn)
2097 if (!sym->ident) {
2098 sparse_error(sym->pos, "no identifier for function argument");
2099 return;
2101 bind_symbol(sym, sym->ident, NS_SYMBOL);
2104 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2105 struct symbol_list **list)
2107 struct symbol_list **old_symbol_list;
2108 struct symbol *base_type = decl->ctype.base_type;
2109 struct statement *stmt, **p;
2110 struct symbol *arg;
2112 old_symbol_list = function_symbol_list;
2113 if (decl->ctype.modifiers & MOD_INLINE) {
2114 function_symbol_list = &decl->inline_symbol_list;
2115 p = &base_type->inline_stmt;
2116 } else {
2117 function_symbol_list = &decl->symbol_list;
2118 p = &base_type->stmt;
2120 function_computed_target_list = NULL;
2121 function_computed_goto_list = NULL;
2123 if (decl->ctype.modifiers & MOD_EXTERN) {
2124 if (!(decl->ctype.modifiers & MOD_INLINE))
2125 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2127 if (!(decl->ctype.modifiers & MOD_STATIC))
2128 decl->ctype.modifiers |= MOD_EXTERN;
2130 stmt = start_function(decl);
2132 *p = stmt;
2133 FOR_EACH_PTR (base_type->arguments, arg) {
2134 declare_argument(arg, base_type);
2135 } END_FOR_EACH_PTR(arg);
2137 token = compound_statement(token->next, stmt);
2139 end_function(decl);
2140 if (!(decl->ctype.modifiers & MOD_INLINE))
2141 add_symbol(list, decl);
2142 check_declaration(decl);
2143 function_symbol_list = old_symbol_list;
2144 if (function_computed_goto_list) {
2145 if (!function_computed_target_list)
2146 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2147 else {
2148 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2149 stmt->target_list = function_computed_target_list;
2150 } END_FOR_EACH_PTR(stmt);
2153 return expect(token, '}', "at end of function");
2156 static void promote_k_r_types(struct symbol *arg)
2158 struct symbol *base = arg->ctype.base_type;
2159 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2160 arg->ctype.base_type = &int_ctype;
2164 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2166 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2167 struct symbol *arg;
2169 FOR_EACH_PTR(real_args, arg) {
2170 struct symbol *type;
2172 /* This is quadratic in the number of arguments. We _really_ don't care */
2173 FOR_EACH_PTR(argtypes, type) {
2174 if (type->ident == arg->ident)
2175 goto match;
2176 } END_FOR_EACH_PTR(type);
2177 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2178 continue;
2179 match:
2180 type->used = 1;
2181 /* "char" and "short" promote to "int" */
2182 promote_k_r_types(type);
2184 arg->ctype = type->ctype;
2185 } END_FOR_EACH_PTR(arg);
2187 FOR_EACH_PTR(argtypes, arg) {
2188 if (!arg->used)
2189 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2190 } END_FOR_EACH_PTR(arg);
2194 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2195 struct symbol_list **list)
2197 struct symbol_list *args = NULL;
2199 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2200 do {
2201 token = declaration_list(token, &args);
2202 if (!match_op(token, ';')) {
2203 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2204 break;
2206 token = token->next;
2207 } while (lookup_type(token));
2209 apply_k_r_types(args, decl);
2211 if (!match_op(token, '{')) {
2212 sparse_error(token->pos, "expected function body");
2213 return token;
2215 return parse_function_body(token, decl, list);
2218 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2220 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2221 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2222 struct statement *stmt;
2224 anon->ctype.base_type = fn;
2225 stmt = alloc_statement(token->pos, STMT_NONE);
2226 fn->stmt = stmt;
2228 token = parse_asm_statement(token, stmt);
2230 add_symbol(list, anon);
2231 return token;
2234 struct token *external_declaration(struct token *token, struct symbol_list **list)
2236 struct ident *ident = NULL;
2237 struct symbol *decl;
2238 struct ctype ctype = { 0, };
2239 struct symbol *base_type;
2240 int is_typedef;
2242 /* Top-level inline asm? */
2243 if (token_type(token) == TOKEN_IDENT) {
2244 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2245 if (s && s->op->toplevel)
2246 return s->op->toplevel(token, list);
2249 /* Parse declaration-specifiers, if any */
2250 token = declaration_specifiers(token, &ctype, 0);
2251 decl = alloc_symbol(token->pos, SYM_NODE);
2252 decl->ctype = ctype;
2253 token = declarator(token, decl, &ident);
2254 apply_modifiers(token->pos, &decl->ctype);
2256 decl->endpos = token->pos;
2258 /* Just a type declaration? */
2259 if (!ident)
2260 return expect(token, ';', "end of type declaration");
2262 /* type define declaration? */
2263 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2265 /* Typedefs don't have meaningful storage */
2266 if (is_typedef) {
2267 ctype.modifiers &= ~MOD_STORAGE;
2268 decl->ctype.modifiers &= ~MOD_STORAGE;
2269 decl->ctype.modifiers |= MOD_USERTYPE;
2272 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2274 base_type = decl->ctype.base_type;
2276 if (is_typedef) {
2277 if (base_type && !base_type->ident) {
2278 switch (base_type->type) {
2279 case SYM_STRUCT:
2280 case SYM_UNION:
2281 case SYM_ENUM:
2282 case SYM_RESTRICT:
2283 base_type->ident = ident;
2286 } else if (base_type && base_type->type == SYM_FN) {
2287 /* K&R argument declaration? */
2288 if (lookup_type(token))
2289 return parse_k_r_arguments(token, decl, list);
2290 if (match_op(token, '{'))
2291 return parse_function_body(token, decl, list);
2293 if (!(decl->ctype.modifiers & MOD_STATIC))
2294 decl->ctype.modifiers |= MOD_EXTERN;
2295 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2296 sparse_error(token->pos, "void declaration");
2299 for (;;) {
2300 if (!is_typedef && match_op(token, '=')) {
2301 if (decl->ctype.modifiers & MOD_EXTERN) {
2302 warning(decl->pos, "symbol with external linkage has initializer");
2303 decl->ctype.modifiers &= ~MOD_EXTERN;
2305 token = initializer(&decl->initializer, token->next);
2307 if (!is_typedef) {
2308 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2309 add_symbol(list, decl);
2310 fn_local_symbol(decl);
2313 check_declaration(decl);
2315 if (!match_op(token, ','))
2316 break;
2318 token = token->next;
2319 ident = NULL;
2320 decl = alloc_symbol(token->pos, SYM_NODE);
2321 decl->ctype = ctype;
2322 token = declaration_specifiers(token, &decl->ctype, 1);
2323 token = declarator(token, decl, &ident);
2324 apply_modifiers(token->pos, &decl->ctype);
2325 decl->endpos = token->pos;
2326 if (!ident) {
2327 sparse_error(token->pos, "expected identifier name in type definition");
2328 return token;
2331 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2333 /* Function declarations are automatically extern unless specifically static */
2334 base_type = decl->ctype.base_type;
2335 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2336 if (!(decl->ctype.modifiers & MOD_STATIC))
2337 decl->ctype.modifiers |= MOD_EXTERN;
2340 return expect(token, ';', "at end of declaration");