Introducing statement keywords
[smatch.git] / parse.c
blob63e6bc32217d11d57976e0da5659a6bd7d96a2f1
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(struct token *token, struct statement *stmt);
61 static struct symbol_op modifier_op = {
62 .type = KW_MODIFIER,
65 static struct symbol_op qualifier_op = {
66 .type = KW_QUALIFIER,
69 static struct symbol_op typeof_op = {
70 .type = KW_TYPEOF,
71 .declarator = typeof_specifier,
74 static struct symbol_op attribute_op = {
75 .type = KW_ATTRIBUTE,
76 .declarator = attribute_specifier,
79 static struct symbol_op struct_op = {
80 .type = KW_SPECIFIER,
81 .declarator = struct_specifier,
84 static struct symbol_op union_op = {
85 .type = KW_SPECIFIER,
86 .declarator = union_specifier,
89 static struct symbol_op enum_op = {
90 .type = KW_SPECIFIER,
91 .declarator = enum_specifier,
96 static struct symbol_op if_op = {
97 .statement = parse_if_statement,
100 static struct symbol_op return_op = {
101 .statement = parse_return_statement,
104 static struct symbol_op loop_iter_op = {
105 .statement = parse_loop_iterator,
108 static struct symbol_op default_op = {
109 .statement = parse_default_statement,
112 static struct symbol_op case_op = {
113 .statement = parse_case_statement,
116 static struct symbol_op switch_op = {
117 .statement = parse_switch_statement,
120 static struct symbol_op for_op = {
121 .statement = parse_for_statement,
124 static struct symbol_op while_op = {
125 .statement = parse_while_statement,
128 static struct symbol_op do_op = {
129 .statement = parse_do_statement,
132 static struct symbol_op goto_op = {
133 .statement = parse_goto_statement,
136 static struct symbol_op context_op = {
137 .statement = parse_context_statement,
140 static struct symbol_op range_op = {
141 .statement = parse_range_statement,
144 static struct symbol_op asm_op = {
145 .statement = parse_asm,
148 static struct init_keyword {
149 const char *name;
150 enum namespace ns;
151 unsigned long modifiers;
152 struct symbol_op *op;
153 } keyword_table[] = {
154 /* Type qualifiers */
155 { "const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
156 { "__const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
157 { "__const__", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
158 { "volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
159 { "__volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
160 { "__volatile__", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
162 /* Typedef.. */
163 { "typedef", NS_TYPEDEF, MOD_TYPEDEF, .op = &modifier_op },
165 /* Extended types */
166 { "typeof", NS_TYPEDEF, .op = &typeof_op },
167 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
168 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
170 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
171 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
173 { "struct", NS_TYPEDEF, .op = &struct_op },
174 { "union", NS_TYPEDEF, .op = &union_op },
175 { "enum", NS_TYPEDEF, .op = &enum_op },
177 { "inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
178 { "__inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
179 { "__inline__", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
181 /* Ignored for now.. */
182 { "restrict", NS_TYPEDEF, .op = &qualifier_op},
183 { "__restrict", NS_TYPEDEF, .op = &qualifier_op},
185 /* Statement */
186 { "if", NS_KEYWORD, .op = &if_op },
187 { "return", NS_KEYWORD, .op = &return_op },
188 { "break", NS_KEYWORD, .op = &loop_iter_op },
189 { "continue", NS_KEYWORD, .op = &loop_iter_op },
190 { "default", NS_KEYWORD, .op = &default_op },
191 { "case", NS_KEYWORD, .op = &case_op },
192 { "switch", NS_KEYWORD, .op = &switch_op },
193 { "for", NS_KEYWORD, .op = &for_op },
194 { "while", NS_KEYWORD, .op = &while_op },
195 { "do", NS_KEYWORD, .op = &do_op },
196 { "goto", NS_KEYWORD, .op = &goto_op },
197 { "__context__",NS_KEYWORD, .op = &context_op },
198 { "__range__", NS_KEYWORD, .op = &range_op },
199 { "asm", NS_KEYWORD, .op = &asm_op },
200 { "__asm", NS_KEYWORD, .op = &asm_op },
201 { "__asm__", NS_KEYWORD, .op = &asm_op },
205 void init_parser(int stream)
207 int i;
208 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
209 struct init_keyword *ptr = keyword_table + i;
210 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
211 sym->ident->keyword = 1;
212 sym->ctype.modifiers = ptr->modifiers;
213 sym->op = ptr->op;
217 // Add a symbol to the list of function-local symbols
218 static void fn_local_symbol(struct symbol *sym)
220 if (function_symbol_list)
221 add_symbol(function_symbol_list, sym);
224 static int match_idents(struct token *token, ...)
226 va_list args;
228 if (token_type(token) != TOKEN_IDENT)
229 return 0;
231 va_start(args, token);
232 for (;;) {
233 struct ident * next = va_arg(args, struct ident *);
234 if (!next)
235 return 0;
236 if (token->ident == next)
237 return 1;
242 struct statement *alloc_statement(struct position pos, int type)
244 struct statement *stmt = __alloc_statement(0);
245 stmt->type = type;
246 stmt->pos = pos;
247 return stmt;
250 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
252 static int apply_modifiers(struct position pos, struct ctype *ctype)
254 struct symbol *base;
256 while ((base = ctype->base_type)) {
257 switch (base->type) {
258 case SYM_FN:
259 case SYM_ENUM:
260 case SYM_ARRAY:
261 case SYM_BITFIELD:
262 case SYM_PTR:
263 ctype = &base->ctype;
264 continue;
266 break;
269 /* Turn the "virtual types" into real types with real sizes etc */
270 if (ctype->base_type == &int_type) {
271 ctype->base_type = ctype_integer(ctype->modifiers);
272 ctype->modifiers &= ~MOD_SPECIFIER;
273 } else if (ctype->base_type == &fp_type) {
274 ctype->base_type = ctype_fp(ctype->modifiers);
275 ctype->modifiers &= ~MOD_SPECIFIER;
278 if (ctype->modifiers & MOD_BITWISE) {
279 struct symbol *type;
280 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
281 if (!is_int_type(ctype->base_type)) {
282 sparse_error(pos, "invalid modifier");
283 return 1;
285 type = alloc_symbol(pos, SYM_BASETYPE);
286 *type = *ctype->base_type;
287 type->ctype.base_type = ctype->base_type;
288 type->type = SYM_RESTRICT;
289 type->ctype.modifiers &= ~MOD_SPECIFIER;
290 ctype->base_type = type;
291 create_fouled(type);
293 return 0;
296 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
298 struct symbol *sym = alloc_symbol(pos, type);
300 sym->ctype.base_type = ctype->base_type;
301 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
303 ctype->base_type = sym;
304 ctype->modifiers &= MOD_STORAGE;
305 return sym;
308 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
310 struct symbol *sym = lookup_symbol(token->ident, ns);
311 if (!sym) {
312 sym = alloc_symbol(token->pos, type);
313 bind_symbol(sym, token->ident, ns);
314 if (type == SYM_LABEL)
315 fn_local_symbol(sym);
317 return sym;
321 * NOTE! NS_LABEL is not just a different namespace,
322 * it also ends up using function scope instead of the
323 * regular symbol scope.
325 struct symbol *label_symbol(struct token *token)
327 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
330 static struct token *struct_union_enum_specifier(enum type type,
331 struct token *token, struct ctype *ctype,
332 struct token *(*parse)(struct token *, struct symbol *))
334 struct symbol *sym;
335 struct position *repos;
337 ctype->modifiers = 0;
338 token = handle_attributes(token, ctype);
339 if (token_type(token) == TOKEN_IDENT) {
340 sym = lookup_symbol(token->ident, NS_STRUCT);
341 if (!sym ||
342 (sym->scope != block_scope &&
343 (match_op(token->next,';') || match_op(token->next,'{')))) {
344 // Either a new symbol, or else an out-of-scope
345 // symbol being redefined.
346 sym = alloc_symbol(token->pos, type);
347 bind_symbol(sym, token->ident, NS_STRUCT);
349 if (sym->type != type)
350 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
351 ctype->base_type = sym;
352 repos = &token->pos;
353 token = token->next;
354 if (match_op(token, '{')) {
355 // The following test is actually wrong for empty
356 // structs, but (1) they are not C99, (2) gcc does
357 // the same thing, and (3) it's easier.
358 if (sym->symbol_list)
359 error_die(token->pos, "redefinition of %s", show_typename (sym));
360 sym->pos = *repos;
361 token = parse(token->next, sym);
362 token = expect(token, '}', "at end of struct-union-enum-specifier");
364 // Mark the structure as needing re-examination
365 sym->examined = 0;
367 return token;
370 // private struct/union/enum type
371 if (!match_op(token, '{')) {
372 sparse_error(token->pos, "expected declaration");
373 ctype->base_type = &bad_ctype;
374 return token;
377 sym = alloc_symbol(token->pos, type);
378 token = parse(token->next, sym);
379 ctype->base_type = sym;
380 return expect(token, '}', "at end of specifier");
383 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
385 return struct_declaration_list(token, &sym->symbol_list);
388 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
390 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
393 static struct token *union_specifier(struct token *token, struct ctype *ctype)
395 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_struct_declaration);
399 typedef struct {
400 int x;
401 unsigned long long y;
402 } Num;
404 static void upper_boundary(Num *n, Num *v)
406 if (n->x > v->x)
407 return;
408 if (n->x < v->x) {
409 *n = *v;
410 return;
412 if (n->y < v->y)
413 n->y = v->y;
416 static void lower_boundary(Num *n, Num *v)
418 if (n->x < v->x)
419 return;
420 if (n->x > v->x) {
421 *n = *v;
422 return;
424 if (n->y > v->y)
425 n->y = v->y;
428 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
430 int shift = type->bit_size;
431 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
433 if (!is_unsigned)
434 shift--;
435 if (upper->x == 0 && upper->y >> shift)
436 return 0;
437 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
438 return 1;
439 return 0;
442 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
444 if (s1->bit_size < s2->bit_size) {
445 s1 = s2;
446 } else if (s1->bit_size == s2->bit_size) {
447 if (s2->ctype.modifiers & MOD_UNSIGNED)
448 s1 = s2;
450 if (s1->bit_size < bits_in_int)
451 return &int_ctype;
452 return s1;
455 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
457 struct symbol *sym;
459 FOR_EACH_PTR(list, sym) {
460 struct expression *expr = sym->initializer;
461 struct symbol *ctype;
462 if (expr->type != EXPR_VALUE)
463 continue;
464 ctype = expr->ctype;
465 if (ctype->bit_size == base_type->bit_size)
466 continue;
467 cast_value(expr, base_type, expr, ctype);
468 } END_FOR_EACH_PTR(sym);
471 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
473 unsigned long long lastval = 0;
474 struct symbol *ctype = NULL, *base_type = NULL;
475 Num upper = {-1, 0}, lower = {1, 0};
476 struct symbol_list *entries = NULL;
478 parent->examined = 1;
479 parent->ctype.base_type = &int_ctype;
480 while (token_type(token) == TOKEN_IDENT) {
481 struct expression *expr = NULL;
482 struct token *next = token->next;
483 struct symbol *sym;
485 sym = alloc_symbol(token->pos, SYM_NODE);
486 bind_symbol(sym, token->ident, NS_SYMBOL);
487 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
489 if (match_op(next, '=')) {
490 next = constant_expression(next->next, &expr);
491 lastval = get_expression_value(expr);
492 ctype = &void_ctype;
493 if (expr && expr->ctype)
494 ctype = expr->ctype;
495 } else if (!ctype) {
496 ctype = &int_ctype;
497 } else if (is_int_type(ctype)) {
498 lastval++;
499 } else {
500 error_die(token->pos, "can't increment the last enum member");
503 if (!expr) {
504 expr = alloc_expression(token->pos, EXPR_VALUE);
505 expr->value = lastval;
506 expr->ctype = ctype;
509 sym->initializer = expr;
510 sym->ctype.base_type = parent;
511 add_ptr_list(&entries, sym);
513 if (base_type != &bad_ctype) {
514 if (ctype->type == SYM_NODE)
515 ctype = ctype->ctype.base_type;
516 if (ctype->type == SYM_ENUM) {
517 if (ctype == parent)
518 ctype = base_type;
519 else
520 ctype = ctype->ctype.base_type;
523 * base_type rules:
524 * - if all enum's are of the same type, then
525 * the base_type is that type (two first
526 * cases)
527 * - if enums are of different types, they
528 * all have to be integer types, and the
529 * base type is at least "int_ctype".
530 * - otherwise the base_type is "bad_ctype".
532 if (!base_type) {
533 base_type = ctype;
534 } else if (ctype == base_type) {
535 /* nothing */
536 } else if (is_int_type(base_type) && is_int_type(ctype)) {
537 base_type = bigger_enum_type(base_type, ctype);
538 } else
539 base_type = &bad_ctype;
540 parent->ctype.base_type = base_type;
542 if (is_int_type(base_type)) {
543 Num v = {.y = lastval};
544 if (ctype->ctype.modifiers & MOD_UNSIGNED)
545 v.x = 0;
546 else if ((long long)lastval >= 0)
547 v.x = 0;
548 else
549 v.x = -1;
550 upper_boundary(&upper, &v);
551 lower_boundary(&lower, &v);
553 token = next;
554 if (!match_op(token, ','))
555 break;
556 token = token->next;
558 if (!base_type) {
559 sparse_error(token->pos, "bad enum definition");
560 base_type = &bad_ctype;
562 else if (!is_int_type(base_type))
563 base_type = base_type;
564 else if (type_is_ok(base_type, &upper, &lower))
565 base_type = base_type;
566 else if (type_is_ok(&int_ctype, &upper, &lower))
567 base_type = &int_ctype;
568 else if (type_is_ok(&uint_ctype, &upper, &lower))
569 base_type = &uint_ctype;
570 else if (type_is_ok(&long_ctype, &upper, &lower))
571 base_type = &long_ctype;
572 else if (type_is_ok(&ulong_ctype, &upper, &lower))
573 base_type = &ulong_ctype;
574 else if (type_is_ok(&llong_ctype, &upper, &lower))
575 base_type = &llong_ctype;
576 else if (type_is_ok(&ullong_ctype, &upper, &lower))
577 base_type = &ullong_ctype;
578 else
579 base_type = &bad_ctype;
580 parent->ctype.base_type = base_type;
581 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
582 parent->examined = 0;
584 cast_enum_list(entries, base_type);
585 free_ptr_list(&entries);
587 return token;
590 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
592 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
594 ctype = &ctype->base_type->ctype;
595 if (!ctype->base_type)
596 ctype->base_type = &incomplete_ctype;
598 return ret;
601 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
603 struct symbol *sym;
605 if (!match_op(token, '(')) {
606 sparse_error(token->pos, "expected '(' after typeof");
607 return token;
609 if (lookup_type(token->next)) {
610 token = typename(token->next, &sym);
611 *ctype = sym->ctype;
612 } else {
613 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
614 token = parse_expression(token->next, &typeof_sym->initializer);
616 ctype->modifiers = 0;
617 ctype->base_type = typeof_sym;
619 return expect(token, ')', "after typeof");
622 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
624 if (attribute == &packed_ident ||
625 attribute == &__packed___ident) {
626 ctype->alignment = 1;
627 return NULL;
629 if (attribute == &aligned_ident ||
630 attribute == &__aligned___ident) {
631 int alignment = max_alignment;
632 if (expr)
633 alignment = get_expression_value(expr);
634 ctype->alignment = alignment;
635 return NULL;
637 if (attribute == &nocast_ident) {
638 ctype->modifiers |= MOD_NOCAST;
639 return NULL;
641 if (attribute == &noderef_ident) {
642 ctype->modifiers |= MOD_NODEREF;
643 return NULL;
645 if (attribute == &safe_ident) {
646 ctype->modifiers |= MOD_SAFE;
647 return NULL;
649 if (attribute == &force_ident) {
650 ctype->modifiers |= MOD_FORCE;
651 return NULL;
653 if (attribute == &bitwise_ident ||
654 attribute == &__bitwise___ident) {
655 if (Wbitwise)
656 ctype->modifiers |= MOD_BITWISE;
657 return NULL;
659 if (attribute == &address_space_ident) {
660 if (!expr)
661 return "expected address space number";
662 ctype->as = get_expression_value(expr);
663 return NULL;
665 if (attribute == &context_ident) {
666 if (expr && expr->type == EXPR_COMMA) {
667 struct context *context = alloc_context();
668 if(expr->left->type == EXPR_COMMA) {
669 context->context = expr->left->left;
670 context->in = get_expression_value(
671 expr->left->right);
672 } else {
673 context->context = NULL;
674 context->in = get_expression_value(expr->left);
676 context->out = get_expression_value(expr->right);
677 add_ptr_list(&ctype->contexts, context);
678 return NULL;
680 return "expected context input/output values";
682 if (attribute == &mode_ident ||
683 attribute == &__mode___ident) {
684 if (expr && expr->type == EXPR_SYMBOL) {
685 struct ident *ident = expr->symbol_name;
688 * Match against __QI__/__HI__/__SI__/__DI__
690 * FIXME! This is broken - we don't actually get
691 * the type information updated properly at this
692 * stage for some reason.
694 if (ident == &__QI___ident ||
695 ident == &QI_ident) {
696 ctype->modifiers |= MOD_CHAR;
697 return NULL;
699 if (ident == &__HI___ident ||
700 ident == &HI_ident) {
701 ctype->modifiers |= MOD_SHORT;
702 return NULL;
704 if (ident == &__SI___ident ||
705 ident == &SI_ident) {
706 /* Nothing? */
707 return NULL;
709 if (ident == &__DI___ident ||
710 ident == &DI_ident) {
711 ctype->modifiers |= MOD_LONGLONG;
712 return NULL;
714 if (ident == &__word___ident ||
715 ident == &word_ident) {
716 ctype->modifiers |= MOD_LONG;
717 return NULL;
719 return "unknown mode attribute";
721 return "expected attribute mode symbol";
724 /* Throw away for now.. */
725 if (attribute == &__transparent_union___ident) {
726 if (Wtransparent_union)
727 return "ignoring attribute __transparent_union__";
728 return NULL;
730 if (attribute == &nothrow_ident ||
731 attribute == &__nothrow_ident ||
732 attribute == &__nothrow___ident)
733 return NULL;
734 if (attribute == &__malloc___ident)
735 return NULL;
736 if (attribute == &nonnull_ident ||
737 attribute == &__nonnull_ident ||
738 attribute == &__nonnull___ident)
739 return NULL;
740 if (attribute == &format_ident ||
741 attribute == &__format___ident ||
742 attribute == &__format_arg___ident)
743 return NULL;
744 if (attribute == &section_ident ||
745 attribute == &__section___ident)
746 return NULL;
747 if (attribute == &unused_ident ||
748 attribute == &__unused___ident)
749 return NULL;
750 if (attribute == &const_ident ||
751 attribute == &__const_ident ||
752 attribute == &__const___ident)
753 return NULL;
754 if (attribute == &noreturn_ident ||
755 attribute == &__noreturn___ident)
756 return NULL;
757 if (attribute == &no_instrument_function_ident ||
758 attribute == &__no_instrument_function___ident)
759 return NULL;
760 if (attribute == &sentinel_ident ||
761 attribute == &__sentinel___ident)
762 return NULL;
763 if (attribute == &regparm_ident)
764 return NULL;
765 if (attribute == &weak_ident ||
766 attribute == &__weak___ident)
767 return NULL;
768 if (attribute == &alias_ident ||
769 attribute == &__alias___ident)
770 return NULL;
771 if (attribute == &pure_ident ||
772 attribute == &__pure___ident)
773 return NULL;
774 if (attribute == &always_inline_ident)
775 return NULL;
776 if (attribute == &syscall_linkage_ident)
777 return NULL;
778 if (attribute == &visibility_ident ||
779 attribute == &__visibility___ident)
780 return NULL;
781 if (attribute == &deprecated_ident ||
782 attribute == &__deprecated___ident)
783 return NULL;
784 if (attribute == &noinline_ident)
785 return NULL;
786 if (attribute == &__used___ident)
787 return NULL;
788 if (attribute == &warn_unused_result_ident ||
789 attribute == &__warn_unused_result___ident)
790 return NULL;
791 if (attribute == &model_ident ||
792 attribute == &__model___ident)
793 return NULL;
795 return "unknown attribute";
798 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
800 ctype->modifiers = 0;
801 token = expect(token, '(', "after attribute");
802 token = expect(token, '(', "after attribute");
804 for (;;) {
805 const char *error_str;
806 struct ident *attribute_name;
807 struct expression *attribute_expr;
809 if (eof_token(token))
810 break;
811 if (match_op(token, ';'))
812 break;
813 if (token_type(token) != TOKEN_IDENT)
814 break;
815 attribute_name = token->ident;
816 token = token->next;
817 attribute_expr = NULL;
818 if (match_op(token, '('))
819 token = parens_expression(token, &attribute_expr, "in attribute");
820 error_str = handle_attribute(ctype, attribute_name, attribute_expr);
821 if (error_str)
822 sparse_error(token->pos, "attribute '%s': %s", show_ident(attribute_name), error_str);
823 if (!match_op(token, ','))
824 break;
825 token = token->next;
828 token = expect(token, ')', "after attribute");
829 token = expect(token, ')', "after attribute");
830 return token;
833 struct symbol * ctype_integer(unsigned long spec)
835 static struct symbol *const integer_ctypes[][3] = {
836 { &llong_ctype, &sllong_ctype, &ullong_ctype },
837 { &long_ctype, &slong_ctype, &ulong_ctype },
838 { &short_ctype, &sshort_ctype, &ushort_ctype },
839 { &char_ctype, &schar_ctype, &uchar_ctype },
840 { &int_ctype, &sint_ctype, &uint_ctype },
842 struct symbol *const (*ctype)[3];
843 int sub;
845 ctype = integer_ctypes;
846 if (!(spec & MOD_LONGLONG)) {
847 ctype++;
848 if (!(spec & MOD_LONG)) {
849 ctype++;
850 if (!(spec & MOD_SHORT)) {
851 ctype++;
852 if (!(spec & MOD_CHAR))
853 ctype++;
858 sub = ((spec & MOD_UNSIGNED)
860 : ((spec & MOD_EXPLICITLY_SIGNED)
862 : 0));
864 return ctype[0][sub];
867 struct symbol * ctype_fp(unsigned long spec)
869 if (spec & MOD_LONGLONG)
870 return &ldouble_ctype;
871 if (spec & MOD_LONG)
872 return &double_ctype;
873 return &float_ctype;
876 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
878 unsigned long mod = thistype->modifiers;
880 if (mod) {
881 unsigned long old = ctype->modifiers;
882 unsigned long extra = 0, dup, conflict;
884 if (mod & old & MOD_LONG) {
885 extra = MOD_LONGLONG | MOD_LONG;
886 mod &= ~MOD_LONG;
887 old &= ~MOD_LONG;
889 dup = (mod & old) | (extra & old) | (extra & mod);
890 if (dup)
891 sparse_error(pos, "Just how %sdo you want this type to be?",
892 modifier_string(dup));
894 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
895 if (conflict)
896 sparse_error(pos, "You cannot have both long and short modifiers.");
898 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
899 if (conflict)
900 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
902 // Only one storage modifier allowed, except that "inline" doesn't count.
903 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
904 conflict &= (conflict - 1);
905 if (conflict)
906 sparse_error(pos, "multiple storage classes");
908 ctype->modifiers = old | mod | extra;
911 /* Context */
912 concat_ptr_list((struct ptr_list *)thistype->contexts,
913 (struct ptr_list **)&ctype->contexts);
915 /* Alignment */
916 if (thistype->alignment & (thistype->alignment-1)) {
917 warning(pos, "I don't like non-power-of-2 alignments");
918 thistype->alignment = 0;
920 if (thistype->alignment > ctype->alignment)
921 ctype->alignment = thistype->alignment;
923 /* Address space */
924 if (thistype->as)
925 ctype->as = thistype->as;
928 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
930 unsigned long banned, wrong;
931 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
932 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
934 if (s->type == SYM_KEYWORD)
935 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
936 else if (s->ctype.base_type == &fp_type)
937 banned = BANNED_SIGN;
938 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
939 banned = 0;
940 else {
941 // label_type
942 // void_type
943 // bad_type
944 // vector_type <-- whatever that is
945 banned = BANNED_SIZE | BANNED_SIGN;
948 wrong = mod & banned;
949 if (wrong)
950 sparse_error(*pos, "modifier %sis invalid in this context",
951 modifier_string (wrong));
954 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
956 struct token *token;
958 while ( (token = next) != NULL ) {
959 struct ctype thistype;
960 struct ident *ident;
961 struct symbol *s, *type;
962 unsigned long mod;
964 next = token->next;
965 if (token_type(token) != TOKEN_IDENT)
966 break;
967 ident = token->ident;
969 s = lookup_symbol(ident, NS_TYPEDEF);
970 if (!s)
971 break;
972 thistype = s->ctype;
973 mod = thistype.modifiers;
974 if (qual) {
975 if (s->type != SYM_KEYWORD)
976 break;
977 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
978 break;
980 if (s->type == SYM_KEYWORD && s->op->declarator) {
981 next = s->op->declarator(next, &thistype);
982 mod = thistype.modifiers;
984 type = thistype.base_type;
985 if (type) {
986 if (qual)
987 break;
988 if (ctype->base_type)
989 break;
990 /* User types only mix with qualifiers */
991 if (mod & MOD_USERTYPE) {
992 if (ctype->modifiers & MOD_SPECIFIER)
993 break;
995 ctype->base_type = type;
998 check_modifiers(&token->pos, s, ctype->modifiers);
999 apply_ctype(token->pos, &thistype, ctype);
1002 if (!ctype->base_type) {
1003 struct symbol *base = &incomplete_ctype;
1006 * If we have modifiers, we'll default to an integer
1007 * type, and "ctype_integer()" will turn this into
1008 * a specific one.
1010 if (ctype->modifiers & MOD_SPECIFIER)
1011 base = &int_type;
1012 ctype->base_type = base;
1014 return token;
1017 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1019 struct expression *expr = NULL;
1021 token = parse_expression(token, &expr);
1022 sym->array_size = expr;
1023 return token;
1026 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1027 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1029 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
1031 for (;;) {
1032 if (token_type(token) != TOKEN_IDENT)
1033 break;
1034 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
1035 struct ctype thistype = { 0, };
1036 token = attribute_specifier(token->next, &thistype);
1037 apply_ctype(token->pos, &thistype, ctype);
1038 continue;
1040 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident, NULL)) {
1041 struct expression *expr;
1042 token = expect(token->next, '(', "after asm");
1043 token = parse_expression(token->next, &expr);
1044 token = expect(token, ')', "after asm");
1045 continue;
1047 break;
1049 return token;
1052 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1054 struct ctype *ctype = &decl->ctype;
1056 if (p && token_type(token) == TOKEN_IDENT) {
1057 *p = token->ident;
1058 token = token->next;
1061 for (;;) {
1062 token = handle_attributes(token, ctype);
1064 if (token_type(token) != TOKEN_SPECIAL)
1065 return token;
1068 * This can be either a parameter list or a grouping.
1069 * For the direct (non-abstract) case, we know if must be
1070 * a parameter list if we already saw the identifier.
1071 * For the abstract case, we know if must be a parameter
1072 * list if it is empty or starts with a type.
1074 if (token->special == '(') {
1075 struct symbol *sym;
1076 struct token *next = token->next;
1077 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1079 if (!fn) {
1080 struct symbol *base_type = ctype->base_type;
1081 token = declarator(next, decl, p);
1082 token = expect(token, ')', "in nested declarator");
1083 while (ctype->base_type != base_type)
1084 ctype = &ctype->base_type->ctype;
1085 p = NULL;
1086 continue;
1089 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1090 token = parameter_type_list(next, sym, p);
1091 token = expect(token, ')', "in function declarator");
1092 continue;
1094 if (token->special == '[') {
1095 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1096 token = abstract_array_declarator(token->next, array);
1097 token = expect(token, ']', "in abstract_array_declarator");
1098 ctype = &array->ctype;
1099 continue;
1101 break;
1103 return token;
1106 static struct token *pointer(struct token *token, struct ctype *ctype)
1108 unsigned long modifiers;
1109 struct symbol *base_type;
1111 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1112 base_type = ctype->base_type;
1113 ctype->modifiers = modifiers;
1115 while (match_op(token,'*')) {
1116 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1117 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1118 ptr->ctype.as = ctype->as;
1119 concat_ptr_list((struct ptr_list *)ctype->contexts,
1120 (struct ptr_list **)&ptr->ctype.contexts);
1121 ptr->ctype.base_type = base_type;
1123 base_type = ptr;
1124 ctype->modifiers = modifiers & MOD_STORAGE;
1125 ctype->base_type = base_type;
1126 ctype->as = 0;
1127 free_ptr_list(&ctype->contexts);
1129 token = declaration_specifiers(token->next, ctype, 1);
1130 modifiers = ctype->modifiers;
1132 return token;
1135 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1137 token = pointer(token, &sym->ctype);
1138 return direct_declarator(token, sym, p);
1141 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1143 struct ctype *ctype = &decl->ctype;
1144 struct expression *expr;
1145 struct symbol *bitfield;
1146 long long width;
1148 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1149 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1150 show_typename(ctype->base_type));
1151 // Parse this to recover gracefully.
1152 return conditional_expression(token->next, &expr);
1155 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1156 token = conditional_expression(token->next, &expr);
1157 width = get_expression_value(expr);
1158 bitfield->bit_size = width;
1160 if (width < 0 || width > INT_MAX) {
1161 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1162 width = -1;
1163 } else if (decl->ident && width == 0) {
1164 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1165 show_ident(decl->ident));
1166 width = -1;
1167 } else if (decl->ident) {
1168 struct symbol *base_type = bitfield->ctype.base_type;
1169 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1170 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1171 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1172 // Valid values are either {-1;0} or {0}, depending on integer
1173 // representation. The latter makes for very efficient code...
1174 sparse_error(token->pos, "dubious one-bit signed bitfield");
1176 if (Wdefault_bitfield_sign &&
1177 bitfield_type->type != SYM_ENUM &&
1178 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1179 is_signed) {
1180 // The sign of bitfields is unspecified by default.
1181 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1184 bitfield->bit_size = width;
1185 return token;
1188 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1190 struct ctype ctype = {0, };
1192 token = declaration_specifiers(token, &ctype, 0);
1193 for (;;) {
1194 struct ident *ident = NULL;
1195 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1196 decl->ctype = ctype;
1197 token = declarator(token, decl, &ident);
1198 decl->ident = ident;
1199 if (match_op(token, ':')) {
1200 token = handle_bitfield(token, decl);
1201 token = handle_attributes(token, &decl->ctype);
1203 apply_modifiers(token->pos, &decl->ctype);
1204 add_symbol(list, decl);
1205 if (!match_op(token, ','))
1206 break;
1207 token = token->next;
1209 return token;
1212 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1214 while (!match_op(token, '}')) {
1215 if (!match_op(token, ';'))
1216 token = declaration_list(token, list);
1217 if (!match_op(token, ';')) {
1218 sparse_error(token->pos, "expected ; at end of declaration");
1219 break;
1221 token = token->next;
1223 return token;
1226 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1228 struct ident *ident = NULL;
1229 struct symbol *sym;
1230 struct ctype ctype = { 0, };
1232 token = declaration_specifiers(token, &ctype, 0);
1233 sym = alloc_symbol(token->pos, SYM_NODE);
1234 sym->ctype = ctype;
1235 *tree = sym;
1236 token = declarator(token, sym, &ident);
1237 sym->ident = ident;
1238 apply_modifiers(token->pos, &sym->ctype);
1239 return token;
1242 struct token *typename(struct token *token, struct symbol **p)
1244 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1245 *p = sym;
1246 token = declaration_specifiers(token, &sym->ctype, 0);
1247 token = declarator(token, sym, NULL);
1248 apply_modifiers(token->pos, &sym->ctype);
1249 return token;
1252 static struct token *expression_statement(struct token *token, struct expression **tree)
1254 token = parse_expression(token, tree);
1255 return expect(token, ';', "at end of statement");
1258 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1259 struct expression_list **inout)
1261 struct expression *expr;
1263 /* Allow empty operands */
1264 if (match_op(token->next, ':') || match_op(token->next, ')'))
1265 return token->next;
1266 do {
1267 struct ident *ident = NULL;
1268 if (match_op(token->next, '[') &&
1269 token_type(token->next->next) == TOKEN_IDENT &&
1270 match_op(token->next->next->next, ']')) {
1271 ident = token->next->next->ident;
1272 token = token->next->next->next;
1274 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1275 token = primary_expression(token->next, &expr);
1276 add_expression(inout, expr);
1277 token = parens_expression(token, &expr, "in asm parameter");
1278 add_expression(inout, expr);
1279 } while (match_op(token, ','));
1280 return token;
1283 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1284 struct expression_list **clobbers)
1286 struct expression *expr;
1288 do {
1289 token = primary_expression(token->next, &expr);
1290 add_expression(clobbers, expr);
1291 } while (match_op(token, ','));
1292 return token;
1295 static struct token *parse_asm(struct token *token, struct statement *stmt)
1297 token = token->next;
1298 stmt->type = STMT_ASM;
1299 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1300 token = token->next;
1302 token = expect(token, '(', "after asm");
1303 token = parse_expression(token, &stmt->asm_string);
1304 if (match_op(token, ':'))
1305 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1306 if (match_op(token, ':'))
1307 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1308 if (match_op(token, ':'))
1309 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1310 token = expect(token, ')', "after asm");
1311 return expect(token, ';', "at end of asm-statement");
1314 /* Make a statement out of an expression */
1315 static struct statement *make_statement(struct expression *expr)
1317 struct statement *stmt;
1319 if (!expr)
1320 return NULL;
1321 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1322 stmt->expression = expr;
1323 return stmt;
1327 * All iterators have two symbols associated with them:
1328 * the "continue" and "break" symbols, which are targets
1329 * for continue and break statements respectively.
1331 * They are in a special name-space, but they follow
1332 * all the normal visibility rules, so nested iterators
1333 * automatically work right.
1335 static void start_iterator(struct statement *stmt)
1337 struct symbol *cont, *brk;
1339 start_symbol_scope();
1340 cont = alloc_symbol(stmt->pos, SYM_NODE);
1341 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1342 brk = alloc_symbol(stmt->pos, SYM_NODE);
1343 bind_symbol(brk, &break_ident, NS_ITERATOR);
1345 stmt->type = STMT_ITERATOR;
1346 stmt->iterator_break = brk;
1347 stmt->iterator_continue = cont;
1348 fn_local_symbol(brk);
1349 fn_local_symbol(cont);
1352 static void end_iterator(struct statement *stmt)
1354 end_symbol_scope();
1357 static struct statement *start_function(struct symbol *sym)
1359 struct symbol *ret;
1360 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1362 start_function_scope();
1363 ret = alloc_symbol(sym->pos, SYM_NODE);
1364 ret->ctype = sym->ctype.base_type->ctype;
1365 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1366 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1367 bind_symbol(ret, &return_ident, NS_ITERATOR);
1368 stmt->ret = ret;
1369 fn_local_symbol(ret);
1371 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1372 current_fn = sym;
1374 return stmt;
1377 static void end_function(struct symbol *sym)
1379 current_fn = NULL;
1380 end_function_scope();
1384 * A "switch()" statement, like an iterator, has a
1385 * the "break" symbol associated with it. It works
1386 * exactly like the iterator break - it's the target
1387 * for any break-statements in scope, and means that
1388 * "break" handling doesn't even need to know whether
1389 * it's breaking out of an iterator or a switch.
1391 * In addition, the "case" symbol is a marker for the
1392 * case/default statements to find the switch statement
1393 * that they are associated with.
1395 static void start_switch(struct statement *stmt)
1397 struct symbol *brk, *switch_case;
1399 start_symbol_scope();
1400 brk = alloc_symbol(stmt->pos, SYM_NODE);
1401 bind_symbol(brk, &break_ident, NS_ITERATOR);
1403 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1404 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1405 switch_case->stmt = stmt;
1407 stmt->type = STMT_SWITCH;
1408 stmt->switch_break = brk;
1409 stmt->switch_case = switch_case;
1411 fn_local_symbol(brk);
1412 fn_local_symbol(switch_case);
1415 static void end_switch(struct statement *stmt)
1417 if (!stmt->switch_case->symbol_list)
1418 warning(stmt->pos, "switch with no cases");
1419 end_symbol_scope();
1422 static void add_case_statement(struct statement *stmt)
1424 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1425 struct symbol *sym;
1427 if (!target) {
1428 sparse_error(stmt->pos, "not in switch scope");
1429 stmt->type = STMT_NONE;
1430 return;
1432 sym = alloc_symbol(stmt->pos, SYM_NODE);
1433 add_symbol(&target->symbol_list, sym);
1434 sym->stmt = stmt;
1435 stmt->case_label = sym;
1436 fn_local_symbol(sym);
1439 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1441 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1443 if (!target)
1444 error_die(token->pos, "internal error: return without a function target");
1445 stmt->type = STMT_RETURN;
1446 stmt->ret_target = target;
1447 return expression_statement(token->next, &stmt->ret_value);
1450 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1452 struct symbol_list *syms;
1453 struct expression *e1, *e2, *e3;
1454 struct statement *iterator;
1456 start_iterator(stmt);
1457 token = expect(token->next, '(', "after 'for'");
1459 syms = NULL;
1460 e1 = NULL;
1461 /* C99 variable declaration? */
1462 if (lookup_type(token)) {
1463 token = external_declaration(token, &syms);
1464 } else {
1465 token = parse_expression(token, &e1);
1466 token = expect(token, ';', "in 'for'");
1468 token = parse_expression(token, &e2);
1469 token = expect(token, ';', "in 'for'");
1470 token = parse_expression(token, &e3);
1471 token = expect(token, ')', "in 'for'");
1472 token = statement(token, &iterator);
1474 stmt->iterator_syms = syms;
1475 stmt->iterator_pre_statement = make_statement(e1);
1476 stmt->iterator_pre_condition = e2;
1477 stmt->iterator_post_statement = make_statement(e3);
1478 stmt->iterator_post_condition = NULL;
1479 stmt->iterator_statement = iterator;
1480 end_iterator(stmt);
1482 return token;
1485 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1487 struct expression *expr;
1488 struct statement *iterator;
1490 start_iterator(stmt);
1491 token = parens_expression(token->next, &expr, "after 'while'");
1492 token = statement(token, &iterator);
1494 stmt->iterator_pre_condition = expr;
1495 stmt->iterator_post_condition = NULL;
1496 stmt->iterator_statement = iterator;
1497 end_iterator(stmt);
1499 return token;
1502 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1504 struct expression *expr;
1505 struct statement *iterator;
1507 start_iterator(stmt);
1508 token = statement(token->next, &iterator);
1509 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1510 token = token->next;
1511 else
1512 sparse_error(token->pos, "expected 'while' after 'do'");
1513 token = parens_expression(token, &expr, "after 'do-while'");
1515 stmt->iterator_post_condition = expr;
1516 stmt->iterator_statement = iterator;
1517 end_iterator(stmt);
1519 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1520 warning(iterator->pos, "do-while statement is not a compound statement");
1522 return expect(token, ';', "after statement");
1525 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1527 stmt->type = STMT_IF;
1528 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1529 token = statement(token, &stmt->if_true);
1530 if (token_type(token) != TOKEN_IDENT)
1531 return token;
1532 if (token->ident != &else_ident)
1533 return token;
1534 return statement(token->next, &stmt->if_false);
1537 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1539 stmt->type = STMT_CASE;
1540 token = expect(token, ':', "after default/case");
1541 add_case_statement(stmt);
1542 return statement(token, &stmt->case_statement);
1545 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1547 token = parse_expression(token->next, &stmt->case_expression);
1548 if (match_op(token, SPECIAL_ELLIPSIS))
1549 token = parse_expression(token->next, &stmt->case_to);
1550 return case_statement(token, stmt);
1553 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1555 return case_statement(token->next, stmt);
1558 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1560 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1561 stmt->type = STMT_GOTO;
1562 stmt->goto_label = target;
1563 if (!target)
1564 sparse_error(stmt->pos, "break/continue not in iterator scope");
1565 return expect(token->next, ';', "at end of statement");
1568 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1570 stmt->type = STMT_SWITCH;
1571 start_switch(stmt);
1572 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1573 token = statement(token, &stmt->switch_statement);
1574 end_switch(stmt);
1575 return token;
1578 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1580 stmt->type = STMT_GOTO;
1581 token = token->next;
1582 if (match_op(token, '*')) {
1583 token = parse_expression(token->next, &stmt->goto_expression);
1584 add_statement(&function_computed_goto_list, stmt);
1585 } else if (token_type(token) == TOKEN_IDENT) {
1586 stmt->goto_label = label_symbol(token);
1587 token = token->next;
1588 } else {
1589 sparse_error(token->pos, "Expected identifier or goto expression");
1591 return expect(token, ';', "at end of statement");
1594 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1596 stmt->type = STMT_CONTEXT;
1597 token = parse_expression(token->next, &stmt->expression);
1598 if(stmt->expression->type == EXPR_PREOP
1599 && stmt->expression->op == '('
1600 && stmt->expression->unop->type == EXPR_COMMA) {
1601 struct expression *expr;
1602 expr = stmt->expression->unop;
1603 stmt->context = expr->left;
1604 stmt->expression = expr->right;
1606 return expect(token, ';', "at end of statement");
1609 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1611 stmt->type = STMT_RANGE;
1612 token = assignment_expression(token->next, &stmt->range_expression);
1613 token = expect(token, ',', "after range expression");
1614 token = assignment_expression(token, &stmt->range_low);
1615 token = expect(token, ',', "after low range");
1616 token = assignment_expression(token, &stmt->range_high);
1617 return expect(token, ';', "after range statement");
1620 static struct token *statement(struct token *token, struct statement **tree)
1622 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1624 *tree = stmt;
1625 if (token_type(token) == TOKEN_IDENT) {
1626 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1627 if (s && s->op->statement)
1628 return s->op->statement(token, stmt);
1630 if (match_op(token->next, ':')) {
1631 stmt->type = STMT_LABEL;
1632 stmt->label_identifier = label_symbol(token);
1633 return statement(token->next->next, &stmt->label_statement);
1637 if (match_op(token, '{')) {
1638 stmt->type = STMT_COMPOUND;
1639 start_symbol_scope();
1640 token = compound_statement(token->next, stmt);
1641 end_symbol_scope();
1643 return expect(token, '}', "at end of compound statement");
1646 stmt->type = STMT_EXPRESSION;
1647 return expression_statement(token, &stmt->expression);
1650 static struct token * statement_list(struct token *token, struct statement_list **list)
1652 int seen_statement = 0;
1653 for (;;) {
1654 struct statement * stmt;
1655 if (eof_token(token))
1656 break;
1657 if (match_op(token, '}'))
1658 break;
1659 if (lookup_type(token)) {
1660 if (seen_statement) {
1661 warning(token->pos, "mixing declarations and code");
1662 seen_statement = 0;
1664 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1665 token = external_declaration(token, &stmt->declaration);
1666 } else {
1667 seen_statement = warn_on_mixed;
1668 token = statement(token, &stmt);
1670 add_statement(list, stmt);
1672 return token;
1675 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1677 struct symbol_list **list = &fn->arguments;
1679 if (match_op(token, ')')) {
1680 // No warning for "void oink ();"
1681 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1682 if (p && !match_op(token->next, ';'))
1683 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1684 return token;
1687 for (;;) {
1688 struct symbol *sym;
1690 if (match_op(token, SPECIAL_ELLIPSIS)) {
1691 if (!*list)
1692 warning(token->pos, "variadic functions must have one named argument");
1693 fn->variadic = 1;
1694 token = token->next;
1695 break;
1698 sym = alloc_symbol(token->pos, SYM_NODE);
1699 token = parameter_declaration(token, &sym);
1700 if (sym->ctype.base_type == &void_ctype) {
1701 /* Special case: (void) */
1702 if (!*list && !sym->ident)
1703 break;
1704 warning(token->pos, "void parameter");
1706 add_symbol(list, sym);
1707 if (!match_op(token, ','))
1708 break;
1709 token = token->next;
1712 return token;
1715 struct token *compound_statement(struct token *token, struct statement *stmt)
1717 token = statement_list(token, &stmt->stmts);
1718 return token;
1721 static struct expression *identifier_expression(struct token *token)
1723 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1724 expr->expr_ident = token->ident;
1725 return expr;
1728 static struct expression *index_expression(struct expression *from, struct expression *to)
1730 int idx_from, idx_to;
1731 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1733 idx_from = get_expression_value(from);
1734 idx_to = idx_from;
1735 if (to) {
1736 idx_to = get_expression_value(to);
1737 if (idx_to < idx_from || idx_from < 0)
1738 warning(from->pos, "nonsense array initializer index range");
1740 expr->idx_from = idx_from;
1741 expr->idx_to = idx_to;
1742 return expr;
1745 static struct token *single_initializer(struct expression **ep, struct token *token)
1747 int expect_equal = 0;
1748 struct token *next = token->next;
1749 struct expression **tail = ep;
1750 int nested;
1752 *ep = NULL;
1754 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1755 struct expression *expr = identifier_expression(token);
1756 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1757 token = initializer(&expr->ident_expression, next->next);
1758 if (expr->ident_expression)
1759 *ep = expr;
1760 return token;
1763 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1764 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1765 struct expression *expr = identifier_expression(next);
1766 *tail = expr;
1767 tail = &expr->ident_expression;
1768 expect_equal = 1;
1769 token = next->next;
1770 } else if (match_op(token, '[')) {
1771 struct expression *from = NULL, *to = NULL, *expr;
1772 token = constant_expression(token->next, &from);
1773 if (!from) {
1774 sparse_error(token->pos, "Expected constant expression");
1775 break;
1777 if (match_op(token, SPECIAL_ELLIPSIS))
1778 token = constant_expression(token->next, &to);
1779 expr = index_expression(from, to);
1780 *tail = expr;
1781 tail = &expr->idx_expression;
1782 token = expect(token, ']', "at end of initializer index");
1783 if (nested)
1784 expect_equal = 1;
1785 } else {
1786 break;
1789 if (nested && !expect_equal) {
1790 if (!match_op(token, '='))
1791 warning(token->pos, "obsolete array initializer, use C99 syntax");
1792 else
1793 expect_equal = 1;
1795 if (expect_equal)
1796 token = expect(token, '=', "at end of initializer index");
1798 token = initializer(tail, token);
1799 if (!*tail)
1800 *ep = NULL;
1801 return token;
1804 static struct token *initializer_list(struct expression_list **list, struct token *token)
1806 struct expression *expr;
1808 for (;;) {
1809 token = single_initializer(&expr, token);
1810 if (!expr)
1811 break;
1812 add_expression(list, expr);
1813 if (!match_op(token, ','))
1814 break;
1815 token = token->next;
1817 return token;
1820 struct token *initializer(struct expression **tree, struct token *token)
1822 if (match_op(token, '{')) {
1823 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1824 *tree = expr;
1825 token = initializer_list(&expr->expr_list, token->next);
1826 return expect(token, '}', "at end of initializer");
1828 return assignment_expression(token, tree);
1831 static void declare_argument(struct symbol *sym, struct symbol *fn)
1833 if (!sym->ident) {
1834 sparse_error(sym->pos, "no identifier for function argument");
1835 return;
1837 bind_symbol(sym, sym->ident, NS_SYMBOL);
1840 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1841 struct symbol_list **list)
1843 struct symbol_list **old_symbol_list;
1844 struct symbol *base_type = decl->ctype.base_type;
1845 struct statement *stmt, **p;
1846 struct symbol *arg;
1848 old_symbol_list = function_symbol_list;
1849 if (decl->ctype.modifiers & MOD_INLINE) {
1850 function_symbol_list = &decl->inline_symbol_list;
1851 p = &base_type->inline_stmt;
1852 } else {
1853 function_symbol_list = &decl->symbol_list;
1854 p = &base_type->stmt;
1856 function_computed_target_list = NULL;
1857 function_computed_goto_list = NULL;
1859 if (decl->ctype.modifiers & MOD_EXTERN) {
1860 if (!(decl->ctype.modifiers & MOD_INLINE))
1861 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1863 if (!(decl->ctype.modifiers & MOD_STATIC))
1864 decl->ctype.modifiers |= MOD_EXTERN;
1866 stmt = start_function(decl);
1868 *p = stmt;
1869 FOR_EACH_PTR (base_type->arguments, arg) {
1870 declare_argument(arg, base_type);
1871 } END_FOR_EACH_PTR(arg);
1873 token = compound_statement(token->next, stmt);
1875 end_function(decl);
1876 if (!(decl->ctype.modifiers & MOD_INLINE))
1877 add_symbol(list, decl);
1878 check_declaration(decl);
1879 function_symbol_list = old_symbol_list;
1880 if (function_computed_goto_list) {
1881 if (!function_computed_target_list)
1882 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1883 else {
1884 struct statement *stmt;
1885 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1886 stmt->target_list = function_computed_target_list;
1887 } END_FOR_EACH_PTR(stmt);
1890 return expect(token, '}', "at end of function");
1893 static void promote_k_r_types(struct symbol *arg)
1895 struct symbol *base = arg->ctype.base_type;
1896 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1897 arg->ctype.base_type = &int_ctype;
1901 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1903 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1904 struct symbol *arg;
1906 FOR_EACH_PTR(real_args, arg) {
1907 struct symbol *type;
1909 /* This is quadratic in the number of arguments. We _really_ don't care */
1910 FOR_EACH_PTR(argtypes, type) {
1911 if (type->ident == arg->ident)
1912 goto match;
1913 } END_FOR_EACH_PTR(type);
1914 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1915 continue;
1916 match:
1917 type->used = 1;
1918 /* "char" and "short" promote to "int" */
1919 promote_k_r_types(type);
1921 arg->ctype = type->ctype;
1922 } END_FOR_EACH_PTR(arg);
1924 FOR_EACH_PTR(argtypes, arg) {
1925 if (!arg->used)
1926 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1927 } END_FOR_EACH_PTR(arg);
1931 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1932 struct symbol_list **list)
1934 struct symbol_list *args = NULL;
1936 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
1937 do {
1938 token = declaration_list(token, &args);
1939 if (!match_op(token, ';')) {
1940 sparse_error(token->pos, "expected ';' at end of parameter declaration");
1941 break;
1943 token = token->next;
1944 } while (lookup_type(token));
1946 apply_k_r_types(args, decl);
1948 if (!match_op(token, '{')) {
1949 sparse_error(token->pos, "expected function body");
1950 return token;
1952 return parse_function_body(token, decl, list);
1955 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
1957 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1958 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1959 struct statement *stmt;
1961 anon->ctype.base_type = fn;
1962 stmt = alloc_statement(token->pos, STMT_NONE);
1963 fn->stmt = stmt;
1965 token = parse_asm(token, stmt);
1967 add_symbol(list, anon);
1968 return token;
1971 struct token *external_declaration(struct token *token, struct symbol_list **list)
1973 struct ident *ident = NULL;
1974 struct symbol *decl;
1975 struct ctype ctype = { 0, };
1976 struct symbol *base_type;
1977 int is_typedef;
1979 /* Top-level inline asm? */
1980 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL))
1981 return toplevel_asm_declaration(token, list);
1983 /* Parse declaration-specifiers, if any */
1984 token = declaration_specifiers(token, &ctype, 0);
1985 decl = alloc_symbol(token->pos, SYM_NODE);
1986 decl->ctype = ctype;
1987 token = declarator(token, decl, &ident);
1988 apply_modifiers(token->pos, &decl->ctype);
1990 /* Just a type declaration? */
1991 if (!ident)
1992 return expect(token, ';', "end of type declaration");
1994 /* type define declaration? */
1995 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1997 /* Typedef's don't have meaningful storage */
1998 if (is_typedef) {
1999 ctype.modifiers &= ~MOD_STORAGE;
2000 decl->ctype.modifiers &= ~MOD_STORAGE;
2001 decl->ctype.modifiers |= MOD_USERTYPE;
2004 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2006 base_type = decl->ctype.base_type;
2008 if (is_typedef) {
2009 if (base_type && !base_type->ident)
2010 base_type->ident = ident;
2011 } else if (base_type && base_type->type == SYM_FN) {
2012 /* K&R argument declaration? */
2013 if (lookup_type(token))
2014 return parse_k_r_arguments(token, decl, list);
2015 if (match_op(token, '{'))
2016 return parse_function_body(token, decl, list);
2018 if (!(decl->ctype.modifiers & MOD_STATIC))
2019 decl->ctype.modifiers |= MOD_EXTERN;
2020 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2021 sparse_error(token->pos, "void declaration");
2024 for (;;) {
2025 if (!is_typedef && match_op(token, '=')) {
2026 if (decl->ctype.modifiers & MOD_EXTERN) {
2027 warning(decl->pos, "symbol with external linkage has initializer");
2028 decl->ctype.modifiers &= ~MOD_EXTERN;
2030 token = initializer(&decl->initializer, token->next);
2032 if (!is_typedef) {
2033 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2034 add_symbol(list, decl);
2035 fn_local_symbol(decl);
2038 check_declaration(decl);
2040 if (!match_op(token, ','))
2041 break;
2043 token = token->next;
2044 ident = NULL;
2045 decl = alloc_symbol(token->pos, SYM_NODE);
2046 decl->ctype = ctype;
2047 token = declaration_specifiers(token, &decl->ctype, 1);
2048 token = declarator(token, decl, &ident);
2049 apply_modifiers(token->pos, &decl->ctype);
2050 if (!ident) {
2051 sparse_error(token->pos, "expected identifier name in type definition");
2052 return token;
2055 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2057 /* Function declarations are automatically extern unless specifically static */
2058 base_type = decl->ctype.base_type;
2059 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2060 if (!(decl->ctype.modifiers & MOD_STATIC))
2061 decl->ctype.modifiers |= MOD_EXTERN;
2064 return expect(token, ';', "at end of declaration");