Fix the annotated inline call position
[smatch.git] / parse.c
blobf0e4020013bc2c8544982742d5a22eecae4c2828
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 #define warn_on_mixed (1)
33 static struct symbol_list **function_symbol_list;
34 struct symbol_list *function_computed_target_list;
35 struct statement_list *function_computed_goto_list;
37 static struct token *statement(struct token *token, struct statement **tree);
38 static struct token *handle_attributes(struct token *token, struct ctype *ctype);
40 static struct token *struct_specifier(struct token *token, struct ctype *ctype);
41 static struct token *union_specifier(struct token *token, struct ctype *ctype);
42 static struct token *enum_specifier(struct token *token, struct ctype *ctype);
43 static struct token *attribute_specifier(struct token *token, struct ctype *ctype);
44 static struct token *typeof_specifier(struct token *token, struct ctype *ctype);
46 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
47 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
48 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
49 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
50 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
51 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
52 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
53 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
54 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
55 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
56 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
57 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
58 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
59 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
60 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype);
63 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype);
64 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype);
65 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype);
66 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype);
67 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype);
68 static struct token *attribute_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 transparent_union_op = {
188 .attribute = attribute_transparent_union,
191 static struct symbol_op ignore_attr_op = {
192 .attribute = ignore_attribute,
195 static struct symbol_op mode_spec_op = {
196 .type = KW_MODE,
199 static struct init_keyword {
200 const char *name;
201 enum namespace ns;
202 unsigned long modifiers;
203 struct symbol_op *op;
204 } keyword_table[] = {
205 /* Type qualifiers */
206 { "const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
207 { "__const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
208 { "__const__", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
209 { "volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
210 { "__volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
211 { "__volatile__", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
213 /* Typedef.. */
214 { "typedef", NS_TYPEDEF, MOD_TYPEDEF, .op = &modifier_op },
216 /* Extended types */
217 { "typeof", NS_TYPEDEF, .op = &typeof_op },
218 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
219 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
221 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
222 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
224 { "struct", NS_TYPEDEF, .op = &struct_op },
225 { "union", NS_TYPEDEF, .op = &union_op },
226 { "enum", NS_TYPEDEF, .op = &enum_op },
228 { "inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
229 { "__inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
230 { "__inline__", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
232 /* Ignored for now.. */
233 { "restrict", NS_TYPEDEF, .op = &qualifier_op},
234 { "__restrict", NS_TYPEDEF, .op = &qualifier_op},
236 /* Statement */
237 { "if", NS_KEYWORD, .op = &if_op },
238 { "return", NS_KEYWORD, .op = &return_op },
239 { "break", NS_KEYWORD, .op = &loop_iter_op },
240 { "continue", NS_KEYWORD, .op = &loop_iter_op },
241 { "default", NS_KEYWORD, .op = &default_op },
242 { "case", NS_KEYWORD, .op = &case_op },
243 { "switch", NS_KEYWORD, .op = &switch_op },
244 { "for", NS_KEYWORD, .op = &for_op },
245 { "while", NS_KEYWORD, .op = &while_op },
246 { "do", NS_KEYWORD, .op = &do_op },
247 { "goto", NS_KEYWORD, .op = &goto_op },
248 { "__context__",NS_KEYWORD, .op = &__context___op },
249 { "__range__", NS_KEYWORD, .op = &range_op },
250 { "asm", NS_KEYWORD, .op = &asm_op },
251 { "__asm", NS_KEYWORD, .op = &asm_op },
252 { "__asm__", NS_KEYWORD, .op = &asm_op },
254 /* Attribute */
255 { "packed", NS_KEYWORD, .op = &packed_op },
256 { "__packed__", NS_KEYWORD, .op = &packed_op },
257 { "aligned", NS_KEYWORD, .op = &aligned_op },
258 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
259 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
260 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
261 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
262 { "force", NS_KEYWORD, MOD_FORCE, .op = &attr_mod_op },
263 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
264 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
265 { "address_space",NS_KEYWORD, .op = &address_space_op },
266 { "mode", NS_KEYWORD, .op = &mode_op },
267 { "context", NS_KEYWORD, .op = &context_op },
268 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
270 { "__mode__", NS_KEYWORD, .op = &mode_op },
271 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
272 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
273 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
274 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
275 { "SI", NS_KEYWORD, .op = &mode_spec_op },
276 { "__SI__", NS_KEYWORD, .op = &mode_spec_op },
277 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
278 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
279 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
280 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
282 /* Ignored attributes */
283 { "nothrow", NS_KEYWORD, .op = &ignore_attr_op },
284 { "__nothrow", NS_KEYWORD, .op = &ignore_attr_op },
285 { "__nothrow__", NS_KEYWORD, .op = &ignore_attr_op },
286 { "__malloc__", NS_KEYWORD, .op = &ignore_attr_op },
287 { "nonnull", NS_KEYWORD, .op = &ignore_attr_op },
288 { "__nonnull", NS_KEYWORD, .op = &ignore_attr_op },
289 { "__nonnull__", NS_KEYWORD, .op = &ignore_attr_op },
290 { "format", NS_KEYWORD, .op = &ignore_attr_op },
291 { "__format__", 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 { "syscall_linkage", NS_KEYWORD, .op = &ignore_attr_op },
316 { "visibility", NS_KEYWORD, .op = &ignore_attr_op },
317 { "__visibility__", NS_KEYWORD, .op = &ignore_attr_op },
318 { "deprecated", NS_KEYWORD, .op = &ignore_attr_op },
319 { "__deprecated__", NS_KEYWORD, .op = &ignore_attr_op },
320 { "noinline", NS_KEYWORD, .op = &ignore_attr_op },
321 { "__used__", NS_KEYWORD, .op = &ignore_attr_op },
322 { "warn_unused_result", NS_KEYWORD, .op = &ignore_attr_op },
323 { "__warn_unused_result__", NS_KEYWORD, .op = &ignore_attr_op },
324 { "model", NS_KEYWORD, .op = &ignore_attr_op },
325 { "__model__", NS_KEYWORD, .op = &ignore_attr_op },
328 void init_parser(int stream)
330 int i;
331 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
332 struct init_keyword *ptr = keyword_table + i;
333 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
334 sym->ident->keyword = 1;
335 sym->ctype.modifiers = ptr->modifiers;
336 sym->op = ptr->op;
340 // Add a symbol to the list of function-local symbols
341 static void fn_local_symbol(struct symbol *sym)
343 if (function_symbol_list)
344 add_symbol(function_symbol_list, sym);
347 static int SENTINEL_ATTR match_idents(struct token *token, ...)
349 va_list args;
351 if (token_type(token) != TOKEN_IDENT)
352 return 0;
354 va_start(args, token);
355 for (;;) {
356 struct ident * next = va_arg(args, struct ident *);
357 if (!next)
358 return 0;
359 if (token->ident == next)
360 return 1;
365 struct statement *alloc_statement(struct position pos, int type)
367 struct statement *stmt = __alloc_statement(0);
368 stmt->type = type;
369 stmt->pos = pos;
370 return stmt;
373 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
375 static int apply_modifiers(struct position pos, struct ctype *ctype)
377 struct symbol *base;
379 while ((base = ctype->base_type)) {
380 switch (base->type) {
381 case SYM_FN:
382 case SYM_ENUM:
383 case SYM_ARRAY:
384 case SYM_BITFIELD:
385 case SYM_PTR:
386 ctype = &base->ctype;
387 continue;
389 break;
392 /* Turn the "virtual types" into real types with real sizes etc */
393 if (ctype->base_type == &int_type) {
394 ctype->base_type = ctype_integer(ctype->modifiers);
395 ctype->modifiers &= ~MOD_SPECIFIER;
396 } else if (ctype->base_type == &fp_type) {
397 ctype->base_type = ctype_fp(ctype->modifiers);
398 ctype->modifiers &= ~MOD_SPECIFIER;
401 if (ctype->modifiers & MOD_BITWISE) {
402 struct symbol *type;
403 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
404 if (!is_int_type(ctype->base_type)) {
405 sparse_error(pos, "invalid modifier");
406 return 1;
408 type = alloc_symbol(pos, SYM_BASETYPE);
409 *type = *ctype->base_type;
410 type->ctype.base_type = ctype->base_type;
411 type->type = SYM_RESTRICT;
412 type->ctype.modifiers &= ~MOD_SPECIFIER;
413 ctype->base_type = type;
414 create_fouled(type);
416 return 0;
419 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
421 struct symbol *sym = alloc_symbol(pos, type);
423 sym->ctype.base_type = ctype->base_type;
424 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
426 ctype->base_type = sym;
427 ctype->modifiers &= MOD_STORAGE;
428 return sym;
431 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
433 struct symbol *sym = lookup_symbol(token->ident, ns);
434 if (!sym) {
435 sym = alloc_symbol(token->pos, type);
436 bind_symbol(sym, token->ident, ns);
437 if (type == SYM_LABEL)
438 fn_local_symbol(sym);
440 return sym;
444 * NOTE! NS_LABEL is not just a different namespace,
445 * it also ends up using function scope instead of the
446 * regular symbol scope.
448 struct symbol *label_symbol(struct token *token)
450 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
453 static struct token *struct_union_enum_specifier(enum type type,
454 struct token *token, struct ctype *ctype,
455 struct token *(*parse)(struct token *, struct symbol *))
457 struct symbol *sym;
458 struct position *repos;
460 ctype->modifiers = 0;
461 token = handle_attributes(token, ctype);
462 if (token_type(token) == TOKEN_IDENT) {
463 sym = lookup_symbol(token->ident, NS_STRUCT);
464 if (!sym ||
465 (sym->scope != block_scope &&
466 (match_op(token->next,';') || match_op(token->next,'{')))) {
467 // Either a new symbol, or else an out-of-scope
468 // symbol being redefined.
469 sym = alloc_symbol(token->pos, type);
470 bind_symbol(sym, token->ident, NS_STRUCT);
472 if (sym->type != type)
473 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
474 ctype->base_type = sym;
475 repos = &token->pos;
476 token = token->next;
477 if (match_op(token, '{')) {
478 // The following test is actually wrong for empty
479 // structs, but (1) they are not C99, (2) gcc does
480 // the same thing, and (3) it's easier.
481 if (sym->symbol_list)
482 error_die(token->pos, "redefinition of %s", show_typename (sym));
483 sym->pos = *repos;
484 token = parse(token->next, sym);
485 token = expect(token, '}', "at end of struct-union-enum-specifier");
487 // Mark the structure as needing re-examination
488 sym->examined = 0;
490 return token;
493 // private struct/union/enum type
494 if (!match_op(token, '{')) {
495 sparse_error(token->pos, "expected declaration");
496 ctype->base_type = &bad_ctype;
497 return token;
500 sym = alloc_symbol(token->pos, type);
501 token = parse(token->next, sym);
502 ctype->base_type = sym;
503 return expect(token, '}', "at end of specifier");
506 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
508 return struct_declaration_list(token, &sym->symbol_list);
511 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
513 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
516 static struct token *union_specifier(struct token *token, struct ctype *ctype)
518 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_struct_declaration);
522 typedef struct {
523 int x;
524 unsigned long long y;
525 } Num;
527 static void upper_boundary(Num *n, Num *v)
529 if (n->x > v->x)
530 return;
531 if (n->x < v->x) {
532 *n = *v;
533 return;
535 if (n->y < v->y)
536 n->y = v->y;
539 static void lower_boundary(Num *n, Num *v)
541 if (n->x < v->x)
542 return;
543 if (n->x > v->x) {
544 *n = *v;
545 return;
547 if (n->y > v->y)
548 n->y = v->y;
551 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
553 int shift = type->bit_size;
554 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
556 if (!is_unsigned)
557 shift--;
558 if (upper->x == 0 && upper->y >> shift)
559 return 0;
560 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
561 return 1;
562 return 0;
565 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
567 if (s1->bit_size < s2->bit_size) {
568 s1 = s2;
569 } else if (s1->bit_size == s2->bit_size) {
570 if (s2->ctype.modifiers & MOD_UNSIGNED)
571 s1 = s2;
573 if (s1->bit_size < bits_in_int)
574 return &int_ctype;
575 return s1;
578 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
580 struct symbol *sym;
582 FOR_EACH_PTR(list, sym) {
583 struct expression *expr = sym->initializer;
584 struct symbol *ctype;
585 if (expr->type != EXPR_VALUE)
586 continue;
587 ctype = expr->ctype;
588 if (ctype->bit_size == base_type->bit_size)
589 continue;
590 cast_value(expr, base_type, expr, ctype);
591 } END_FOR_EACH_PTR(sym);
594 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
596 unsigned long long lastval = 0;
597 struct symbol *ctype = NULL, *base_type = NULL;
598 Num upper = {-1, 0}, lower = {1, 0};
599 struct symbol_list *entries = NULL;
601 parent->examined = 1;
602 parent->ctype.base_type = &int_ctype;
603 while (token_type(token) == TOKEN_IDENT) {
604 struct expression *expr = NULL;
605 struct token *next = token->next;
606 struct symbol *sym;
608 sym = alloc_symbol(token->pos, SYM_NODE);
609 bind_symbol(sym, token->ident, NS_SYMBOL);
610 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
612 if (match_op(next, '=')) {
613 next = constant_expression(next->next, &expr);
614 lastval = get_expression_value(expr);
615 ctype = &void_ctype;
616 if (expr && expr->ctype)
617 ctype = expr->ctype;
618 } else if (!ctype) {
619 ctype = &int_ctype;
620 } else if (is_int_type(ctype)) {
621 lastval++;
622 } else {
623 error_die(token->pos, "can't increment the last enum member");
626 if (!expr) {
627 expr = alloc_expression(token->pos, EXPR_VALUE);
628 expr->value = lastval;
629 expr->ctype = ctype;
632 sym->initializer = expr;
633 sym->ctype.base_type = parent;
634 add_ptr_list(&entries, sym);
636 if (base_type != &bad_ctype) {
637 if (ctype->type == SYM_NODE)
638 ctype = ctype->ctype.base_type;
639 if (ctype->type == SYM_ENUM) {
640 if (ctype == parent)
641 ctype = base_type;
642 else
643 ctype = ctype->ctype.base_type;
646 * base_type rules:
647 * - if all enums are of the same type, then
648 * the base_type is that type (two first
649 * cases)
650 * - if enums are of different types, they
651 * all have to be integer types, and the
652 * base type is at least "int_ctype".
653 * - otherwise the base_type is "bad_ctype".
655 if (!base_type) {
656 base_type = ctype;
657 } else if (ctype == base_type) {
658 /* nothing */
659 } else if (is_int_type(base_type) && is_int_type(ctype)) {
660 base_type = bigger_enum_type(base_type, ctype);
661 } else
662 base_type = &bad_ctype;
663 parent->ctype.base_type = base_type;
665 if (is_int_type(base_type)) {
666 Num v = {.y = lastval};
667 if (ctype->ctype.modifiers & MOD_UNSIGNED)
668 v.x = 0;
669 else if ((long long)lastval >= 0)
670 v.x = 0;
671 else
672 v.x = -1;
673 upper_boundary(&upper, &v);
674 lower_boundary(&lower, &v);
676 token = next;
677 if (!match_op(token, ','))
678 break;
679 token = token->next;
681 if (!base_type) {
682 sparse_error(token->pos, "bad enum definition");
683 base_type = &bad_ctype;
685 else if (!is_int_type(base_type))
686 base_type = base_type;
687 else if (type_is_ok(base_type, &upper, &lower))
688 base_type = base_type;
689 else if (type_is_ok(&int_ctype, &upper, &lower))
690 base_type = &int_ctype;
691 else if (type_is_ok(&uint_ctype, &upper, &lower))
692 base_type = &uint_ctype;
693 else if (type_is_ok(&long_ctype, &upper, &lower))
694 base_type = &long_ctype;
695 else if (type_is_ok(&ulong_ctype, &upper, &lower))
696 base_type = &ulong_ctype;
697 else if (type_is_ok(&llong_ctype, &upper, &lower))
698 base_type = &llong_ctype;
699 else if (type_is_ok(&ullong_ctype, &upper, &lower))
700 base_type = &ullong_ctype;
701 else
702 base_type = &bad_ctype;
703 parent->ctype.base_type = base_type;
704 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
705 parent->examined = 0;
707 cast_enum_list(entries, base_type);
708 free_ptr_list(&entries);
710 return token;
713 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
715 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
717 ctype = &ctype->base_type->ctype;
718 if (!ctype->base_type)
719 ctype->base_type = &incomplete_ctype;
721 return ret;
724 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
726 struct symbol *sym;
728 if (!match_op(token, '(')) {
729 sparse_error(token->pos, "expected '(' after typeof");
730 return token;
732 if (lookup_type(token->next)) {
733 token = typename(token->next, &sym);
734 *ctype = sym->ctype;
735 } else {
736 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
737 token = parse_expression(token->next, &typeof_sym->initializer);
739 ctype->modifiers = 0;
740 ctype->base_type = typeof_sym;
742 return expect(token, ')', "after typeof");
745 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
747 struct expression *expr = NULL;
748 if (match_op(token, '('))
749 token = parens_expression(token, &expr, "in attribute");
750 return token;
753 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
755 ctype->alignment = 1;
756 return token;
759 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
761 int alignment = max_alignment;
762 struct expression *expr = NULL;
764 if (match_op(token, '(')) {
765 token = parens_expression(token, &expr, "in attribute");
766 if (expr)
767 alignment = get_expression_value(expr);
769 ctype->alignment = alignment;
770 return token;
773 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
775 ctype->modifiers |= attr->ctype.modifiers;
776 return token;
779 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
781 struct expression *expr = NULL;
782 token = expect(token, '(', "after address_space attribute");
783 token = conditional_expression(token, &expr);
784 if (expr)
785 ctype->as = get_expression_value(expr);
786 token = expect(token, ')', "after address_space attribute");
787 return token;
790 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
792 token = expect(token, '(', "after mode attribute");
793 if (token_type(token) == TOKEN_IDENT) {
794 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
795 if (mode && mode->op->type == KW_MODE)
796 ctype->modifiers |= mode->ctype.modifiers;
797 else
798 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
799 token = token->next;
800 } else
801 sparse_error(token->pos, "expect attribute mode symbol\n");
802 token = expect(token, ')', "after mode attribute");
803 return token;
806 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
808 struct context *context = alloc_context();
809 struct expression *args[3];
810 int argc = 0;
812 token = expect(token, '(', "after context attribute");
813 while (!match_op(token, ')')) {
814 struct expression *expr = NULL;
815 token = conditional_expression(token, &expr);
816 if (!expr)
817 break;
818 if (argc < 3)
819 args[argc++] = expr;
820 if (!match_op(token, ','))
821 break;
822 token = token->next;
825 switch(argc) {
826 case 0:
827 sparse_error(token->pos, "expected context input/output values");
828 break;
829 case 1:
830 context->in = get_expression_value(args[0]);
831 break;
832 case 2:
833 context->in = get_expression_value(args[0]);
834 context->out = get_expression_value(args[1]);
835 break;
836 case 3:
837 context->context = args[0];
838 context->in = get_expression_value(args[1]);
839 context->out = get_expression_value(args[2]);
840 break;
843 if (argc)
844 add_ptr_list(&ctype->contexts, context);
846 token = expect(token, ')', "after context attribute");
847 return token;
850 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
852 if (Wtransparent_union)
853 sparse_error(token->pos, "ignoring attribute __transparent_union__");
854 return token;
857 static struct token *recover_unknown_attribute(struct token *token)
859 struct expression *expr = NULL;
861 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
862 token = token->next;
863 if (match_op(token, '('))
864 token = parens_expression(token, &expr, "in attribute");
865 return token;
868 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
870 ctype->modifiers = 0;
871 token = expect(token, '(', "after attribute");
872 token = expect(token, '(', "after attribute");
874 for (;;) {
875 struct ident *attribute_name;
876 struct symbol *attr;
878 if (eof_token(token))
879 break;
880 if (match_op(token, ';'))
881 break;
882 if (token_type(token) != TOKEN_IDENT)
883 break;
884 attribute_name = token->ident;
885 attr = lookup_keyword(attribute_name, NS_KEYWORD);
886 if (attr && attr->op->attribute)
887 token = attr->op->attribute(token->next, attr, ctype);
888 else
889 token = recover_unknown_attribute(token);
891 if (!match_op(token, ','))
892 break;
893 token = token->next;
896 token = expect(token, ')', "after attribute");
897 token = expect(token, ')', "after attribute");
898 return token;
901 struct symbol * ctype_integer(unsigned long spec)
903 static struct symbol *const integer_ctypes[][3] = {
904 { &llong_ctype, &sllong_ctype, &ullong_ctype },
905 { &long_ctype, &slong_ctype, &ulong_ctype },
906 { &short_ctype, &sshort_ctype, &ushort_ctype },
907 { &char_ctype, &schar_ctype, &uchar_ctype },
908 { &int_ctype, &sint_ctype, &uint_ctype },
910 struct symbol *const (*ctype)[3];
911 int sub;
913 ctype = integer_ctypes;
914 if (!(spec & MOD_LONGLONG)) {
915 ctype++;
916 if (!(spec & MOD_LONG)) {
917 ctype++;
918 if (!(spec & MOD_SHORT)) {
919 ctype++;
920 if (!(spec & MOD_CHAR))
921 ctype++;
926 sub = ((spec & MOD_UNSIGNED)
928 : ((spec & MOD_EXPLICITLY_SIGNED)
930 : 0));
932 return ctype[0][sub];
935 struct symbol * ctype_fp(unsigned long spec)
937 if (spec & MOD_LONGLONG)
938 return &ldouble_ctype;
939 if (spec & MOD_LONG)
940 return &double_ctype;
941 return &float_ctype;
944 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
946 unsigned long mod = thistype->modifiers;
948 if (mod) {
949 unsigned long old = ctype->modifiers;
950 unsigned long extra = 0, dup, conflict;
952 if (mod & old & MOD_LONG) {
953 extra = MOD_LONGLONG | MOD_LONG;
954 mod &= ~MOD_LONG;
955 old &= ~MOD_LONG;
957 dup = (mod & old) | (extra & old) | (extra & mod);
958 if (dup)
959 sparse_error(pos, "Just how %sdo you want this type to be?",
960 modifier_string(dup));
962 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
963 if (conflict)
964 sparse_error(pos, "You cannot have both long and short modifiers.");
966 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
967 if (conflict)
968 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
970 // Only one storage modifier allowed, except that "inline" doesn't count.
971 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
972 conflict &= (conflict - 1);
973 if (conflict)
974 sparse_error(pos, "multiple storage classes");
976 ctype->modifiers = old | mod | extra;
979 /* Context */
980 concat_ptr_list((struct ptr_list *)thistype->contexts,
981 (struct ptr_list **)&ctype->contexts);
983 /* Alignment */
984 if (thistype->alignment & (thistype->alignment-1)) {
985 warning(pos, "I don't like non-power-of-2 alignments");
986 thistype->alignment = 0;
988 if (thistype->alignment > ctype->alignment)
989 ctype->alignment = thistype->alignment;
991 /* Address space */
992 if (thistype->as)
993 ctype->as = thistype->as;
996 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
998 unsigned long banned, wrong;
999 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1000 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1002 if (s->type == SYM_KEYWORD)
1003 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1004 else if (s->ctype.base_type == &fp_type)
1005 banned = BANNED_SIGN;
1006 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1007 banned = 0;
1008 else {
1009 // label_type
1010 // void_type
1011 // bad_type
1012 // vector_type <-- whatever that is
1013 banned = BANNED_SIZE | BANNED_SIGN;
1016 wrong = mod & banned;
1017 if (wrong)
1018 sparse_error(*pos, "modifier %sis invalid in this context",
1019 modifier_string (wrong));
1022 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1024 struct token *token;
1026 while ( (token = next) != NULL ) {
1027 struct ctype thistype;
1028 struct ident *ident;
1029 struct symbol *s, *type;
1030 unsigned long mod;
1032 next = token->next;
1033 if (token_type(token) != TOKEN_IDENT)
1034 break;
1035 ident = token->ident;
1037 s = lookup_symbol(ident, NS_TYPEDEF);
1038 if (!s)
1039 break;
1040 thistype = s->ctype;
1041 mod = thistype.modifiers;
1042 if (qual) {
1043 if (s->type != SYM_KEYWORD)
1044 break;
1045 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1046 break;
1048 if (s->type == SYM_KEYWORD && s->op->declarator) {
1049 next = s->op->declarator(next, &thistype);
1050 mod = thistype.modifiers;
1052 type = thistype.base_type;
1053 if (type) {
1054 if (qual)
1055 break;
1056 if (ctype->base_type)
1057 break;
1058 /* User types only mix with qualifiers */
1059 if (mod & MOD_USERTYPE) {
1060 if (ctype->modifiers & MOD_SPECIFIER)
1061 break;
1063 ctype->base_type = type;
1066 check_modifiers(&token->pos, s, ctype->modifiers);
1067 apply_ctype(token->pos, &thistype, ctype);
1070 if (!ctype->base_type) {
1071 struct symbol *base = &incomplete_ctype;
1074 * If we have modifiers, we'll default to an integer
1075 * type, and "ctype_integer()" will turn this into
1076 * a specific one.
1078 if (ctype->modifiers & MOD_SPECIFIER)
1079 base = &int_type;
1080 ctype->base_type = base;
1082 return token;
1085 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1087 struct expression *expr = NULL;
1089 token = parse_expression(token, &expr);
1090 sym->array_size = expr;
1091 return token;
1094 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1095 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1097 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
1099 struct symbol *keyword;
1100 for (;;) {
1101 struct ctype thistype = { 0, };
1102 if (token_type(token) != TOKEN_IDENT)
1103 break;
1104 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1105 if (!keyword || keyword->type != SYM_KEYWORD)
1106 break;
1107 if (!(keyword->op->type & (KW_ATTRIBUTE | KW_ASM)))
1108 break;
1109 token = keyword->op->declarator(token->next, &thistype);
1110 apply_ctype(token->pos, &thistype, ctype);
1112 return token;
1115 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1117 struct ctype *ctype = &decl->ctype;
1119 if (p && token_type(token) == TOKEN_IDENT) {
1120 *p = token->ident;
1121 token = token->next;
1124 for (;;) {
1125 token = handle_attributes(token, ctype);
1127 if (token_type(token) != TOKEN_SPECIAL)
1128 return token;
1131 * This can be either a parameter list or a grouping.
1132 * For the direct (non-abstract) case, we know if must be
1133 * a parameter list if we already saw the identifier.
1134 * For the abstract case, we know if must be a parameter
1135 * list if it is empty or starts with a type.
1137 if (token->special == '(') {
1138 struct symbol *sym;
1139 struct token *next = token->next;
1140 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1142 if (!fn) {
1143 struct symbol *base_type = ctype->base_type;
1144 token = declarator(next, decl, p);
1145 token = expect(token, ')', "in nested declarator");
1146 while (ctype->base_type != base_type)
1147 ctype = &ctype->base_type->ctype;
1148 p = NULL;
1149 continue;
1152 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1153 token = parameter_type_list(next, sym, p);
1154 token = expect(token, ')', "in function declarator");
1155 continue;
1157 if (token->special == '[') {
1158 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1159 token = abstract_array_declarator(token->next, array);
1160 token = expect(token, ']', "in abstract_array_declarator");
1161 ctype = &array->ctype;
1162 continue;
1164 break;
1166 return token;
1169 static struct token *pointer(struct token *token, struct ctype *ctype)
1171 unsigned long modifiers;
1172 struct symbol *base_type;
1174 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1175 base_type = ctype->base_type;
1176 ctype->modifiers = modifiers;
1178 while (match_op(token,'*')) {
1179 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1180 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1181 ptr->ctype.as = ctype->as;
1182 concat_ptr_list((struct ptr_list *)ctype->contexts,
1183 (struct ptr_list **)&ptr->ctype.contexts);
1184 ptr->ctype.base_type = base_type;
1186 base_type = ptr;
1187 ctype->modifiers = modifiers & MOD_STORAGE;
1188 ctype->base_type = base_type;
1189 ctype->as = 0;
1190 free_ptr_list(&ctype->contexts);
1192 token = declaration_specifiers(token->next, ctype, 1);
1193 modifiers = ctype->modifiers;
1195 return token;
1198 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1200 token = pointer(token, &sym->ctype);
1201 return direct_declarator(token, sym, p);
1204 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1206 struct ctype *ctype = &decl->ctype;
1207 struct expression *expr;
1208 struct symbol *bitfield;
1209 long long width;
1211 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1212 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1213 show_typename(ctype->base_type));
1214 // Parse this to recover gracefully.
1215 return conditional_expression(token->next, &expr);
1218 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1219 token = conditional_expression(token->next, &expr);
1220 width = get_expression_value(expr);
1221 bitfield->bit_size = width;
1223 if (width < 0 || width > INT_MAX) {
1224 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1225 width = -1;
1226 } else if (decl->ident && width == 0) {
1227 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1228 show_ident(decl->ident));
1229 width = -1;
1230 } else if (decl->ident) {
1231 struct symbol *base_type = bitfield->ctype.base_type;
1232 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1233 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1234 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1235 // Valid values are either {-1;0} or {0}, depending on integer
1236 // representation. The latter makes for very efficient code...
1237 sparse_error(token->pos, "dubious one-bit signed bitfield");
1239 if (Wdefault_bitfield_sign &&
1240 bitfield_type->type != SYM_ENUM &&
1241 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1242 is_signed) {
1243 // The sign of bitfields is unspecified by default.
1244 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1247 bitfield->bit_size = width;
1248 return token;
1251 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1253 struct ctype ctype = {0, };
1255 token = declaration_specifiers(token, &ctype, 0);
1256 for (;;) {
1257 struct ident *ident = NULL;
1258 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1259 decl->ctype = ctype;
1260 token = declarator(token, decl, &ident);
1261 decl->ident = ident;
1262 if (match_op(token, ':')) {
1263 token = handle_bitfield(token, decl);
1264 token = handle_attributes(token, &decl->ctype);
1266 apply_modifiers(token->pos, &decl->ctype);
1267 add_symbol(list, decl);
1268 if (!match_op(token, ','))
1269 break;
1270 token = token->next;
1272 return token;
1275 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1277 while (!match_op(token, '}')) {
1278 if (!match_op(token, ';'))
1279 token = declaration_list(token, list);
1280 if (!match_op(token, ';')) {
1281 sparse_error(token->pos, "expected ; at end of declaration");
1282 break;
1284 token = token->next;
1286 return token;
1289 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1291 struct ident *ident = NULL;
1292 struct symbol *sym;
1293 struct ctype ctype = { 0, };
1295 token = declaration_specifiers(token, &ctype, 0);
1296 sym = alloc_symbol(token->pos, SYM_NODE);
1297 sym->ctype = ctype;
1298 *tree = sym;
1299 token = declarator(token, sym, &ident);
1300 sym->ident = ident;
1301 apply_modifiers(token->pos, &sym->ctype);
1302 return token;
1305 struct token *typename(struct token *token, struct symbol **p)
1307 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1308 *p = sym;
1309 token = declaration_specifiers(token, &sym->ctype, 0);
1310 token = declarator(token, sym, NULL);
1311 apply_modifiers(token->pos, &sym->ctype);
1312 return token;
1315 static struct token *expression_statement(struct token *token, struct expression **tree)
1317 token = parse_expression(token, tree);
1318 return expect(token, ';', "at end of statement");
1321 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1322 struct expression_list **inout)
1324 struct expression *expr;
1326 /* Allow empty operands */
1327 if (match_op(token->next, ':') || match_op(token->next, ')'))
1328 return token->next;
1329 do {
1330 struct ident *ident = NULL;
1331 if (match_op(token->next, '[') &&
1332 token_type(token->next->next) == TOKEN_IDENT &&
1333 match_op(token->next->next->next, ']')) {
1334 ident = token->next->next->ident;
1335 token = token->next->next->next;
1337 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1338 token = primary_expression(token->next, &expr);
1339 add_expression(inout, expr);
1340 token = parens_expression(token, &expr, "in asm parameter");
1341 add_expression(inout, expr);
1342 } while (match_op(token, ','));
1343 return token;
1346 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1347 struct expression_list **clobbers)
1349 struct expression *expr;
1351 do {
1352 token = primary_expression(token->next, &expr);
1353 add_expression(clobbers, expr);
1354 } while (match_op(token, ','));
1355 return token;
1358 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1360 token = token->next;
1361 stmt->type = STMT_ASM;
1362 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1363 token = token->next;
1365 token = expect(token, '(', "after asm");
1366 token = parse_expression(token, &stmt->asm_string);
1367 if (match_op(token, ':'))
1368 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1369 if (match_op(token, ':'))
1370 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1371 if (match_op(token, ':'))
1372 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1373 token = expect(token, ')', "after asm");
1374 return expect(token, ';', "at end of asm-statement");
1377 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1379 struct expression *expr;
1380 token = expect(token, '(', "after asm");
1381 token = parse_expression(token->next, &expr);
1382 token = expect(token, ')', "after asm");
1383 return token;
1386 /* Make a statement out of an expression */
1387 static struct statement *make_statement(struct expression *expr)
1389 struct statement *stmt;
1391 if (!expr)
1392 return NULL;
1393 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1394 stmt->expression = expr;
1395 return stmt;
1399 * All iterators have two symbols associated with them:
1400 * the "continue" and "break" symbols, which are targets
1401 * for continue and break statements respectively.
1403 * They are in a special name-space, but they follow
1404 * all the normal visibility rules, so nested iterators
1405 * automatically work right.
1407 static void start_iterator(struct statement *stmt)
1409 struct symbol *cont, *brk;
1411 start_symbol_scope();
1412 cont = alloc_symbol(stmt->pos, SYM_NODE);
1413 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1414 brk = alloc_symbol(stmt->pos, SYM_NODE);
1415 bind_symbol(brk, &break_ident, NS_ITERATOR);
1417 stmt->type = STMT_ITERATOR;
1418 stmt->iterator_break = brk;
1419 stmt->iterator_continue = cont;
1420 fn_local_symbol(brk);
1421 fn_local_symbol(cont);
1424 static void end_iterator(struct statement *stmt)
1426 end_symbol_scope();
1429 static struct statement *start_function(struct symbol *sym)
1431 struct symbol *ret;
1432 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1434 start_function_scope();
1435 ret = alloc_symbol(sym->pos, SYM_NODE);
1436 ret->ctype = sym->ctype.base_type->ctype;
1437 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1438 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1439 bind_symbol(ret, &return_ident, NS_ITERATOR);
1440 stmt->ret = ret;
1441 fn_local_symbol(ret);
1443 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1444 current_fn = sym;
1446 return stmt;
1449 static void end_function(struct symbol *sym)
1451 current_fn = NULL;
1452 end_function_scope();
1456 * A "switch()" statement, like an iterator, has a
1457 * the "break" symbol associated with it. It works
1458 * exactly like the iterator break - it's the target
1459 * for any break-statements in scope, and means that
1460 * "break" handling doesn't even need to know whether
1461 * it's breaking out of an iterator or a switch.
1463 * In addition, the "case" symbol is a marker for the
1464 * case/default statements to find the switch statement
1465 * that they are associated with.
1467 static void start_switch(struct statement *stmt)
1469 struct symbol *brk, *switch_case;
1471 start_symbol_scope();
1472 brk = alloc_symbol(stmt->pos, SYM_NODE);
1473 bind_symbol(brk, &break_ident, NS_ITERATOR);
1475 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1476 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1477 switch_case->stmt = stmt;
1479 stmt->type = STMT_SWITCH;
1480 stmt->switch_break = brk;
1481 stmt->switch_case = switch_case;
1483 fn_local_symbol(brk);
1484 fn_local_symbol(switch_case);
1487 static void end_switch(struct statement *stmt)
1489 if (!stmt->switch_case->symbol_list)
1490 warning(stmt->pos, "switch with no cases");
1491 end_symbol_scope();
1494 static void add_case_statement(struct statement *stmt)
1496 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1497 struct symbol *sym;
1499 if (!target) {
1500 sparse_error(stmt->pos, "not in switch scope");
1501 stmt->type = STMT_NONE;
1502 return;
1504 sym = alloc_symbol(stmt->pos, SYM_NODE);
1505 add_symbol(&target->symbol_list, sym);
1506 sym->stmt = stmt;
1507 stmt->case_label = sym;
1508 fn_local_symbol(sym);
1511 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1513 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1515 if (!target)
1516 error_die(token->pos, "internal error: return without a function target");
1517 stmt->type = STMT_RETURN;
1518 stmt->ret_target = target;
1519 return expression_statement(token->next, &stmt->ret_value);
1522 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1524 struct symbol_list *syms;
1525 struct expression *e1, *e2, *e3;
1526 struct statement *iterator;
1528 start_iterator(stmt);
1529 token = expect(token->next, '(', "after 'for'");
1531 syms = NULL;
1532 e1 = NULL;
1533 /* C99 variable declaration? */
1534 if (lookup_type(token)) {
1535 token = external_declaration(token, &syms);
1536 } else {
1537 token = parse_expression(token, &e1);
1538 token = expect(token, ';', "in 'for'");
1540 token = parse_expression(token, &e2);
1541 token = expect(token, ';', "in 'for'");
1542 token = parse_expression(token, &e3);
1543 token = expect(token, ')', "in 'for'");
1544 token = statement(token, &iterator);
1546 stmt->iterator_syms = syms;
1547 stmt->iterator_pre_statement = make_statement(e1);
1548 stmt->iterator_pre_condition = e2;
1549 stmt->iterator_post_statement = make_statement(e3);
1550 stmt->iterator_post_condition = NULL;
1551 stmt->iterator_statement = iterator;
1552 end_iterator(stmt);
1554 return token;
1557 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1559 struct expression *expr;
1560 struct statement *iterator;
1562 start_iterator(stmt);
1563 token = parens_expression(token->next, &expr, "after 'while'");
1564 token = statement(token, &iterator);
1566 stmt->iterator_pre_condition = expr;
1567 stmt->iterator_post_condition = NULL;
1568 stmt->iterator_statement = iterator;
1569 end_iterator(stmt);
1571 return token;
1574 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1576 struct expression *expr;
1577 struct statement *iterator;
1579 start_iterator(stmt);
1580 token = statement(token->next, &iterator);
1581 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1582 token = token->next;
1583 else
1584 sparse_error(token->pos, "expected 'while' after 'do'");
1585 token = parens_expression(token, &expr, "after 'do-while'");
1587 stmt->iterator_post_condition = expr;
1588 stmt->iterator_statement = iterator;
1589 end_iterator(stmt);
1591 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1592 warning(iterator->pos, "do-while statement is not a compound statement");
1594 return expect(token, ';', "after statement");
1597 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1599 stmt->type = STMT_IF;
1600 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1601 token = statement(token, &stmt->if_true);
1602 if (token_type(token) != TOKEN_IDENT)
1603 return token;
1604 if (token->ident != &else_ident)
1605 return token;
1606 return statement(token->next, &stmt->if_false);
1609 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1611 stmt->type = STMT_CASE;
1612 token = expect(token, ':', "after default/case");
1613 add_case_statement(stmt);
1614 return statement(token, &stmt->case_statement);
1617 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1619 token = parse_expression(token->next, &stmt->case_expression);
1620 if (match_op(token, SPECIAL_ELLIPSIS))
1621 token = parse_expression(token->next, &stmt->case_to);
1622 return case_statement(token, stmt);
1625 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1627 return case_statement(token->next, stmt);
1630 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1632 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1633 stmt->type = STMT_GOTO;
1634 stmt->goto_label = target;
1635 if (!target)
1636 sparse_error(stmt->pos, "break/continue not in iterator scope");
1637 return expect(token->next, ';', "at end of statement");
1640 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1642 stmt->type = STMT_SWITCH;
1643 start_switch(stmt);
1644 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1645 token = statement(token, &stmt->switch_statement);
1646 end_switch(stmt);
1647 return token;
1650 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1652 stmt->type = STMT_GOTO;
1653 token = token->next;
1654 if (match_op(token, '*')) {
1655 token = parse_expression(token->next, &stmt->goto_expression);
1656 add_statement(&function_computed_goto_list, stmt);
1657 } else if (token_type(token) == TOKEN_IDENT) {
1658 stmt->goto_label = label_symbol(token);
1659 token = token->next;
1660 } else {
1661 sparse_error(token->pos, "Expected identifier or goto expression");
1663 return expect(token, ';', "at end of statement");
1666 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1668 stmt->type = STMT_CONTEXT;
1669 token = parse_expression(token->next, &stmt->expression);
1670 if(stmt->expression->type == EXPR_PREOP
1671 && stmt->expression->op == '('
1672 && stmt->expression->unop->type == EXPR_COMMA) {
1673 struct expression *expr;
1674 expr = stmt->expression->unop;
1675 stmt->context = expr->left;
1676 stmt->expression = expr->right;
1678 return expect(token, ';', "at end of statement");
1681 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1683 stmt->type = STMT_RANGE;
1684 token = assignment_expression(token->next, &stmt->range_expression);
1685 token = expect(token, ',', "after range expression");
1686 token = assignment_expression(token, &stmt->range_low);
1687 token = expect(token, ',', "after low range");
1688 token = assignment_expression(token, &stmt->range_high);
1689 return expect(token, ';', "after range statement");
1692 static struct token *statement(struct token *token, struct statement **tree)
1694 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1696 *tree = stmt;
1697 if (token_type(token) == TOKEN_IDENT) {
1698 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1699 if (s && s->op->statement)
1700 return s->op->statement(token, stmt);
1702 if (match_op(token->next, ':')) {
1703 stmt->type = STMT_LABEL;
1704 stmt->label_identifier = label_symbol(token);
1705 return statement(token->next->next, &stmt->label_statement);
1709 if (match_op(token, '{')) {
1710 stmt->type = STMT_COMPOUND;
1711 start_symbol_scope();
1712 token = compound_statement(token->next, stmt);
1713 end_symbol_scope();
1715 return expect(token, '}', "at end of compound statement");
1718 stmt->type = STMT_EXPRESSION;
1719 return expression_statement(token, &stmt->expression);
1722 static struct token * statement_list(struct token *token, struct statement_list **list)
1724 int seen_statement = 0;
1725 for (;;) {
1726 struct statement * stmt;
1727 if (eof_token(token))
1728 break;
1729 if (match_op(token, '}'))
1730 break;
1731 if (lookup_type(token)) {
1732 if (seen_statement) {
1733 warning(token->pos, "mixing declarations and code");
1734 seen_statement = 0;
1736 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1737 token = external_declaration(token, &stmt->declaration);
1738 } else {
1739 seen_statement = warn_on_mixed;
1740 token = statement(token, &stmt);
1742 add_statement(list, stmt);
1744 return token;
1747 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1749 struct symbol_list **list = &fn->arguments;
1751 if (match_op(token, ')')) {
1752 // No warning for "void oink ();"
1753 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1754 if (p && !match_op(token->next, ';'))
1755 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1756 return token;
1759 for (;;) {
1760 struct symbol *sym;
1762 if (match_op(token, SPECIAL_ELLIPSIS)) {
1763 if (!*list)
1764 warning(token->pos, "variadic functions must have one named argument");
1765 fn->variadic = 1;
1766 token = token->next;
1767 break;
1770 sym = alloc_symbol(token->pos, SYM_NODE);
1771 token = parameter_declaration(token, &sym);
1772 if (sym->ctype.base_type == &void_ctype) {
1773 /* Special case: (void) */
1774 if (!*list && !sym->ident)
1775 break;
1776 warning(token->pos, "void parameter");
1778 add_symbol(list, sym);
1779 if (!match_op(token, ','))
1780 break;
1781 token = token->next;
1784 return token;
1787 struct token *compound_statement(struct token *token, struct statement *stmt)
1789 token = statement_list(token, &stmt->stmts);
1790 return token;
1793 static struct expression *identifier_expression(struct token *token)
1795 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1796 expr->expr_ident = token->ident;
1797 return expr;
1800 static struct expression *index_expression(struct expression *from, struct expression *to)
1802 int idx_from, idx_to;
1803 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1805 idx_from = get_expression_value(from);
1806 idx_to = idx_from;
1807 if (to) {
1808 idx_to = get_expression_value(to);
1809 if (idx_to < idx_from || idx_from < 0)
1810 warning(from->pos, "nonsense array initializer index range");
1812 expr->idx_from = idx_from;
1813 expr->idx_to = idx_to;
1814 return expr;
1817 static struct token *single_initializer(struct expression **ep, struct token *token)
1819 int expect_equal = 0;
1820 struct token *next = token->next;
1821 struct expression **tail = ep;
1822 int nested;
1824 *ep = NULL;
1826 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1827 struct expression *expr = identifier_expression(token);
1828 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1829 token = initializer(&expr->ident_expression, next->next);
1830 if (expr->ident_expression)
1831 *ep = expr;
1832 return token;
1835 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1836 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1837 struct expression *expr = identifier_expression(next);
1838 *tail = expr;
1839 tail = &expr->ident_expression;
1840 expect_equal = 1;
1841 token = next->next;
1842 } else if (match_op(token, '[')) {
1843 struct expression *from = NULL, *to = NULL, *expr;
1844 token = constant_expression(token->next, &from);
1845 if (!from) {
1846 sparse_error(token->pos, "Expected constant expression");
1847 break;
1849 if (match_op(token, SPECIAL_ELLIPSIS))
1850 token = constant_expression(token->next, &to);
1851 expr = index_expression(from, to);
1852 *tail = expr;
1853 tail = &expr->idx_expression;
1854 token = expect(token, ']', "at end of initializer index");
1855 if (nested)
1856 expect_equal = 1;
1857 } else {
1858 break;
1861 if (nested && !expect_equal) {
1862 if (!match_op(token, '='))
1863 warning(token->pos, "obsolete array initializer, use C99 syntax");
1864 else
1865 expect_equal = 1;
1867 if (expect_equal)
1868 token = expect(token, '=', "at end of initializer index");
1870 token = initializer(tail, token);
1871 if (!*tail)
1872 *ep = NULL;
1873 return token;
1876 static struct token *initializer_list(struct expression_list **list, struct token *token)
1878 struct expression *expr;
1880 for (;;) {
1881 token = single_initializer(&expr, token);
1882 if (!expr)
1883 break;
1884 add_expression(list, expr);
1885 if (!match_op(token, ','))
1886 break;
1887 token = token->next;
1889 return token;
1892 struct token *initializer(struct expression **tree, struct token *token)
1894 if (match_op(token, '{')) {
1895 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1896 *tree = expr;
1897 token = initializer_list(&expr->expr_list, token->next);
1898 return expect(token, '}', "at end of initializer");
1900 return assignment_expression(token, tree);
1903 static void declare_argument(struct symbol *sym, struct symbol *fn)
1905 if (!sym->ident) {
1906 sparse_error(sym->pos, "no identifier for function argument");
1907 return;
1909 bind_symbol(sym, sym->ident, NS_SYMBOL);
1912 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1913 struct symbol_list **list)
1915 struct symbol_list **old_symbol_list;
1916 struct symbol *base_type = decl->ctype.base_type;
1917 struct statement *stmt, **p;
1918 struct symbol *arg;
1920 old_symbol_list = function_symbol_list;
1921 if (decl->ctype.modifiers & MOD_INLINE) {
1922 function_symbol_list = &decl->inline_symbol_list;
1923 p = &base_type->inline_stmt;
1924 } else {
1925 function_symbol_list = &decl->symbol_list;
1926 p = &base_type->stmt;
1928 function_computed_target_list = NULL;
1929 function_computed_goto_list = NULL;
1931 if (decl->ctype.modifiers & MOD_EXTERN) {
1932 if (!(decl->ctype.modifiers & MOD_INLINE))
1933 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1935 if (!(decl->ctype.modifiers & MOD_STATIC))
1936 decl->ctype.modifiers |= MOD_EXTERN;
1938 stmt = start_function(decl);
1940 *p = stmt;
1941 FOR_EACH_PTR (base_type->arguments, arg) {
1942 declare_argument(arg, base_type);
1943 } END_FOR_EACH_PTR(arg);
1945 token = compound_statement(token->next, stmt);
1947 end_function(decl);
1948 if (!(decl->ctype.modifiers & MOD_INLINE))
1949 add_symbol(list, decl);
1950 check_declaration(decl);
1951 function_symbol_list = old_symbol_list;
1952 if (function_computed_goto_list) {
1953 if (!function_computed_target_list)
1954 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1955 else {
1956 struct statement *stmt;
1957 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1958 stmt->target_list = function_computed_target_list;
1959 } END_FOR_EACH_PTR(stmt);
1962 return expect(token, '}', "at end of function");
1965 static void promote_k_r_types(struct symbol *arg)
1967 struct symbol *base = arg->ctype.base_type;
1968 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1969 arg->ctype.base_type = &int_ctype;
1973 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1975 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1976 struct symbol *arg;
1978 FOR_EACH_PTR(real_args, arg) {
1979 struct symbol *type;
1981 /* This is quadratic in the number of arguments. We _really_ don't care */
1982 FOR_EACH_PTR(argtypes, type) {
1983 if (type->ident == arg->ident)
1984 goto match;
1985 } END_FOR_EACH_PTR(type);
1986 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1987 continue;
1988 match:
1989 type->used = 1;
1990 /* "char" and "short" promote to "int" */
1991 promote_k_r_types(type);
1993 arg->ctype = type->ctype;
1994 } END_FOR_EACH_PTR(arg);
1996 FOR_EACH_PTR(argtypes, arg) {
1997 if (!arg->used)
1998 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1999 } END_FOR_EACH_PTR(arg);
2003 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2004 struct symbol_list **list)
2006 struct symbol_list *args = NULL;
2008 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2009 do {
2010 token = declaration_list(token, &args);
2011 if (!match_op(token, ';')) {
2012 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2013 break;
2015 token = token->next;
2016 } while (lookup_type(token));
2018 apply_k_r_types(args, decl);
2020 if (!match_op(token, '{')) {
2021 sparse_error(token->pos, "expected function body");
2022 return token;
2024 return parse_function_body(token, decl, list);
2027 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2029 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2030 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2031 struct statement *stmt;
2033 anon->ctype.base_type = fn;
2034 stmt = alloc_statement(token->pos, STMT_NONE);
2035 fn->stmt = stmt;
2037 token = parse_asm_statement(token, stmt);
2039 add_symbol(list, anon);
2040 return token;
2043 struct token *external_declaration(struct token *token, struct symbol_list **list)
2045 struct ident *ident = NULL;
2046 struct symbol *decl;
2047 struct ctype ctype = { 0, };
2048 struct symbol *base_type;
2049 int is_typedef;
2051 /* Top-level inline asm? */
2052 if (token_type(token) == TOKEN_IDENT) {
2053 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2054 if (s && s->op->toplevel)
2055 return s->op->toplevel(token, list);
2058 /* Parse declaration-specifiers, if any */
2059 token = declaration_specifiers(token, &ctype, 0);
2060 decl = alloc_symbol(token->pos, SYM_NODE);
2061 decl->ctype = ctype;
2062 token = declarator(token, decl, &ident);
2063 apply_modifiers(token->pos, &decl->ctype);
2065 /* Just a type declaration? */
2066 if (!ident)
2067 return expect(token, ';', "end of type declaration");
2069 /* type define declaration? */
2070 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2072 /* Typedefs don't have meaningful storage */
2073 if (is_typedef) {
2074 ctype.modifiers &= ~MOD_STORAGE;
2075 decl->ctype.modifiers &= ~MOD_STORAGE;
2076 decl->ctype.modifiers |= MOD_USERTYPE;
2079 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2081 base_type = decl->ctype.base_type;
2083 if (is_typedef) {
2084 if (base_type && !base_type->ident)
2085 base_type->ident = ident;
2086 } else if (base_type && base_type->type == SYM_FN) {
2087 /* K&R argument declaration? */
2088 if (lookup_type(token))
2089 return parse_k_r_arguments(token, decl, list);
2090 if (match_op(token, '{'))
2091 return parse_function_body(token, decl, list);
2093 if (!(decl->ctype.modifiers & MOD_STATIC))
2094 decl->ctype.modifiers |= MOD_EXTERN;
2095 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2096 sparse_error(token->pos, "void declaration");
2099 for (;;) {
2100 if (!is_typedef && match_op(token, '=')) {
2101 if (decl->ctype.modifiers & MOD_EXTERN) {
2102 warning(decl->pos, "symbol with external linkage has initializer");
2103 decl->ctype.modifiers &= ~MOD_EXTERN;
2105 token = initializer(&decl->initializer, token->next);
2107 if (!is_typedef) {
2108 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2109 add_symbol(list, decl);
2110 fn_local_symbol(decl);
2113 check_declaration(decl);
2115 if (!match_op(token, ','))
2116 break;
2118 token = token->next;
2119 ident = NULL;
2120 decl = alloc_symbol(token->pos, SYM_NODE);
2121 decl->ctype = ctype;
2122 token = declaration_specifiers(token, &decl->ctype, 1);
2123 token = declarator(token, decl, &ident);
2124 apply_modifiers(token->pos, &decl->ctype);
2125 if (!ident) {
2126 sparse_error(token->pos, "expected identifier name in type definition");
2127 return token;
2130 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2132 /* Function declarations are automatically extern unless specifically static */
2133 base_type = decl->ctype.base_type;
2134 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2135 if (!(decl->ctype.modifiers & MOD_STATIC))
2136 decl->ctype.modifiers |= MOD_EXTERN;
2139 return expect(token, ';', "at end of declaration");