[PATCH] casts are not lvalues
[smatch.git] / parse.c
blobb8eba3fd5cc6ee6075aa1f541c8762e494861dd8
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 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
20 #include "lib.h"
21 #include "token.h"
22 #include "parse.h"
23 #include "symbol.h"
24 #include "scope.h"
25 #include "expression.h"
26 #include "target.h"
28 static struct symbol_list **function_symbol_list;
29 struct symbol_list *function_computed_target_list;
30 struct statement_list *function_computed_goto_list;
32 // Add a symbol to the list of function-local symbols
33 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
35 static struct token *statement(struct token *token, struct statement **tree);
36 static struct token *external_declaration(struct token *token, struct symbol_list **list);
38 static int match_idents(struct token *token, ...)
40 va_list args;
42 if (token_type(token) != TOKEN_IDENT)
43 return 0;
45 va_start(args, token);
46 for (;;) {
47 struct ident * next = va_arg(args, struct ident *);
48 if (!next)
49 return 0;
50 if (token->ident == next)
51 return 1;
56 struct statement *alloc_statement(struct position pos, int type)
58 struct statement *stmt = __alloc_statement(0);
59 stmt->type = type;
60 stmt->pos = pos;
61 return stmt;
64 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
66 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
68 struct symbol *sym = alloc_symbol(pos, type);
70 sym->ctype.base_type = ctype->base_type;
71 sym->ctype.modifiers = ctype->modifiers & ~(MOD_STORAGE | MOD_EXPLICITLY_SIGNED);
73 ctype->base_type = sym;
74 ctype->modifiers &= MOD_STORAGE;
75 return sym;
78 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
80 struct symbol *sym = lookup_symbol(token->ident, ns);
81 if (!sym) {
82 sym = alloc_symbol(token->pos, type);
83 sym->ident = token->ident;
84 bind_symbol(sym, token->ident, ns);
85 if (type == SYM_LABEL)
86 fn_local_symbol(sym);
88 return sym;
92 * NOTE! NS_LABEL is not just a different namespace,
93 * it also ends up using function scope instead of the
94 * regular symbol scope.
96 struct symbol *label_symbol(struct token *token)
98 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
101 struct token *struct_union_enum_specifier(enum namespace ns, enum type type,
102 struct token *token, struct ctype *ctype,
103 struct token *(*parse)(struct token *, struct symbol *))
105 struct symbol *sym;
107 ctype->modifiers = 0;
108 if (token_type(token) == TOKEN_IDENT) {
109 sym = lookup_or_create_symbol(ns, type, token);
110 token = token->next;
111 ctype->base_type = sym;
112 if (match_op(token, '{')) {
113 token = parse(token->next, sym);
114 token = expect(token, '}', "at end of struct-union-enum-specifier");
116 return token;
119 // private struct/union/enum type
120 if (!match_op(token, '{')) {
121 warn(token->pos, "expected declaration");
122 ctype->base_type = &bad_type;
123 return token;
126 sym = alloc_symbol(token->pos, type);
127 token = parse(token->next, sym);
128 ctype->base_type = sym;
129 return expect(token, '}', "at end of specifier");
132 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
134 return struct_declaration_list(token, &sym->symbol_list);
137 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
139 return struct_union_enum_specifier(NS_STRUCT, type, token, ctype, parse_struct_declaration);
142 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
144 int nextval = 0;
145 while (token_type(token) == TOKEN_IDENT) {
146 struct token *next = token->next;
147 struct symbol *sym;
149 sym = alloc_symbol(token->pos, SYM_ENUM);
150 bind_symbol(sym, token->ident, NS_SYMBOL);
151 sym->ctype.base_type = parent;
152 parent->ctype.base_type = &int_ctype;
154 if (match_op(next, '=')) {
155 struct expression *expr;
156 next = constant_expression(next->next, &expr);
157 nextval = get_expression_value(expr);
159 sym->value = nextval;
161 token = next;
162 if (!match_op(token, ','))
163 break;
164 token = token->next;
165 nextval = nextval + 1;
167 return token;
170 struct token *enum_specifier(struct token *token, struct ctype *ctype)
172 return struct_union_enum_specifier(NS_ENUM, SYM_ENUM, token, ctype, parse_enum_declaration);
175 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
177 struct symbol *sym;
179 if (!match_op(token, '(')) {
180 warn(token->pos, "expected '(' after typeof");
181 return token;
183 if (lookup_type(token->next)) {
184 token = typename(token->next, &sym);
185 *ctype = sym->ctype;
186 } else {
187 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
188 token = parse_expression(token->next, &typeof_sym->initializer);
190 ctype->modifiers = 0;
191 ctype->base_type = typeof_sym;
193 return expect(token, ')', "after typeof");
196 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
198 if (match_string_ident(attribute, "packed") ||
199 match_string_ident(attribute, "__packed__")) {
200 ctype->alignment = 1;
201 return NULL;
203 if (match_string_ident(attribute, "aligned") ||
204 match_string_ident(attribute, "__aligned__")) {
205 int alignment = max_alignment;
206 if (expr)
207 alignment = get_expression_value(expr);
208 ctype->alignment = alignment;
209 return NULL;
211 if (match_string_ident(attribute, "nocast")) {
212 ctype->modifiers |= MOD_NOCAST;
213 return NULL;
215 if (match_string_ident(attribute, "noderef")) {
216 ctype->modifiers |= MOD_NODEREF;
217 return NULL;
219 if (match_string_ident(attribute, "safe")) {
220 ctype->modifiers |= MOD_SAFE;
221 return NULL;
223 if (match_string_ident(attribute, "force")) {
224 ctype->modifiers |= MOD_FORCE;
225 return NULL;
227 if (match_string_ident(attribute, "address_space")) {
228 if (!expr)
229 return "expected address space number";
230 ctype->as = get_expression_value(expr);
231 return NULL;
233 if (match_string_ident(attribute, "context")) {
234 if (expr && expr->type == EXPR_COMMA) {
235 int mask = get_expression_value(expr->left);
236 int value = get_expression_value(expr->right);
237 if (value & ~mask)
238 return "nonsense attribute types";
239 ctype->contextmask |= mask;
240 ctype->context |= value;
241 return NULL;
243 return "expected context mask and value";
245 if (match_string_ident(attribute, "mode") ||
246 match_string_ident(attribute, "__mode__")) {
247 if (expr && expr->type == EXPR_SYMBOL) {
248 struct ident *ident = expr->symbol_name;
251 * Match against __QI__/__HI__/__SI__/__DI__
253 * FIXME! This is broken - we don't actually get
254 * the type information updated properly at this
255 * stage for some reason.
257 if (match_string_ident(ident, "__QI__") ||
258 match_string_ident(ident, "QI")) {
259 ctype->modifiers |= MOD_CHAR;
260 ctype->base_type = ctype_integer(ctype->modifiers);
261 return NULL;
263 if (match_string_ident(ident, "__HI__") ||
264 match_string_ident(ident, "HI")) {
265 ctype->modifiers |= MOD_SHORT;
266 ctype->base_type = ctype_integer(ctype->modifiers);
267 return NULL;
269 if (match_string_ident(ident, "__SI__") ||
270 match_string_ident(ident, "SI")) {
271 /* Nothing? */
272 return NULL;
274 if (match_string_ident(ident, "__DI__") ||
275 match_string_ident(ident, "DI")) {
276 ctype->modifiers |= MOD_LONGLONG;
277 ctype->base_type = ctype_integer(ctype->modifiers);
278 return NULL;
280 if (match_string_ident(ident, "__word__") ||
281 match_string_ident(ident, "word")) {
282 ctype->modifiers |= MOD_LONG;
283 ctype->base_type = ctype_integer(ctype->modifiers);
284 return NULL;
286 return "unknown mode attribute";
288 return "expected attribute mode symbol";
291 /* Throw away for now.. */
292 if (match_string_ident(attribute, "format") ||
293 match_string_ident(attribute, "__format__"))
294 return NULL;
295 if (match_string_ident(attribute, "section") ||
296 match_string_ident(attribute, "__section__"))
297 return NULL;
298 if (match_string_ident(attribute, "unused") ||
299 match_string_ident(attribute, "__unused__"))
300 return NULL;
301 if (match_string_ident(attribute, "const") ||
302 match_string_ident(attribute, "__const__"))
303 return NULL;
304 if (match_string_ident(attribute, "noreturn") ||
305 match_string_ident(attribute, "__noreturn__"))
306 return NULL;
307 if (match_string_ident(attribute, "regparm"))
308 return NULL;
309 if (match_string_ident(attribute, "weak"))
310 return NULL;
311 if (match_string_ident(attribute, "alias"))
312 return NULL;
313 if (match_string_ident(attribute, "pure"))
314 return NULL;
315 if (match_string_ident(attribute, "always_inline"))
316 return NULL;
317 if (match_string_ident(attribute, "syscall_linkage"))
318 return NULL;
319 if (match_string_ident(attribute, "visibility"))
320 return NULL;
322 return "unknown attribute";
325 struct token *attribute_specifier(struct token *token, struct ctype *ctype)
327 ctype->modifiers = 0;
328 token = expect(token, '(', "after attribute");
329 token = expect(token, '(', "after attribute");
331 for (;;) {
332 const char *error;
333 struct ident *attribute_name;
334 struct expression *attribute_expr;
336 if (eof_token(token))
337 break;
338 if (match_op(token, ';'))
339 break;
340 if (token_type(token) != TOKEN_IDENT)
341 break;
342 attribute_name = token->ident;
343 token = token->next;
344 attribute_expr = NULL;
345 if (match_op(token, '('))
346 token = parens_expression(token, &attribute_expr, "in attribute");
347 error = handle_attribute(ctype, attribute_name, attribute_expr);
348 if (error)
349 warn(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
350 if (!match_op(token, ','))
351 break;
352 token = token->next;
355 token = expect(token, ')', "after attribute");
356 token = expect(token, ')', "after attribute");
357 return token;
360 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
361 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
363 struct symbol * ctype_integer(unsigned int spec)
365 static struct symbol *const integer_ctypes[][2] = {
366 { &llong_ctype, &ullong_ctype },
367 { &long_ctype, &ulong_ctype },
368 { &short_ctype, &ushort_ctype },
369 { &char_ctype, &uchar_ctype },
370 { &int_ctype, &uint_ctype },
372 struct symbol *const (*ctype)[2];
374 ctype = integer_ctypes;
375 if (!(spec & MOD_LONGLONG)) {
376 ctype++;
377 if (!(spec & MOD_LONG)) {
378 ctype++;
379 if (!(spec & MOD_SHORT)) {
380 ctype++;
381 if (!(spec & MOD_CHAR))
382 ctype++;
386 return ctype[0][(spec & MOD_UNSIGNED) != 0];
389 struct symbol * ctype_fp(unsigned int spec)
391 if (spec & MOD_LONGLONG)
392 return &ldouble_ctype;
393 if (spec & MOD_LONG)
394 return &double_ctype;
395 return &float_ctype;
398 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
400 unsigned long mod = thistype->modifiers;
402 if (mod) {
403 unsigned long old = ctype->modifiers;
404 unsigned long extra = 0, dup, conflict;
406 if (mod & old & MOD_LONG) {
407 extra = MOD_LONGLONG | MOD_LONG;
408 mod &= ~MOD_LONG;
409 old &= ~MOD_LONG;
411 dup = (mod & old) | (extra & old) | (extra & mod);
412 if (dup)
413 warn(pos, "Just how %sdo you want this type to be?",
414 modifier_string(dup));
416 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
417 if (conflict)
418 warn(pos, "You cannot have both long and short modifiers.");
420 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
421 if (conflict)
422 warn(pos, "You cannot have both signed and unsigned modifiers.");
424 // Only one storage modifier allowed, except that "inline" doesn't count.
425 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
426 conflict &= (conflict - 1);
427 if (conflict)
428 warn(pos, "multiple storage classes");
430 ctype->modifiers = old | mod | extra;
433 /* Context mask and value */
434 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
435 warn(pos, "inconsistent attribute types");
436 thistype->context = 0;
437 thistype->contextmask = 0;
439 ctype->context |= thistype->context;
440 ctype->contextmask |= thistype->contextmask;
442 /* Alignment */
443 if (thistype->alignment & (thistype->alignment-1)) {
444 warn(pos, "I don't like non-power-of-2 alignments");
445 thistype->alignment = 0;
447 if (thistype->alignment > ctype->alignment)
448 ctype->alignment = thistype->alignment;
450 /* Address space */
451 ctype->as = thistype->as;
454 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
456 unsigned long banned, wrong;
457 unsigned long this_mod = s->ctype.modifiers;
458 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
459 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
461 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
462 banned = BANNED_SIZE | BANNED_SIGN;
463 else if (this_mod & MOD_SPECIALBITS)
464 banned = 0;
465 else if (s->ctype.base_type == &fp_type)
466 banned = BANNED_SIGN;
467 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
468 banned = 0;
469 else {
470 // label_type
471 // void_type
472 // bad_type
473 // vector_type <-- whatever that is
474 banned = BANNED_SIZE | BANNED_SIGN;
477 wrong = mod & banned;
478 if (wrong)
479 warn(*pos, "modifier %sis invalid in this context",
480 modifier_string (wrong));
484 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
486 struct token *token;
488 while ( (token = next) != NULL ) {
489 struct ctype thistype;
490 struct ident *ident;
491 struct symbol *s, *type;
492 unsigned long mod;
494 next = token->next;
495 if (token_type(token) != TOKEN_IDENT)
496 break;
497 ident = token->ident;
499 s = lookup_symbol(ident, NS_TYPEDEF);
500 if (!s)
501 break;
502 thistype = s->ctype;
503 mod = thistype.modifiers;
504 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
505 break;
506 check_modifiers(&token->pos, s, ctype->modifiers);
507 if (mod & MOD_SPECIALBITS) {
508 if (mod & MOD_STRUCTOF)
509 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
510 else if (mod & MOD_UNIONOF)
511 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
512 else if (mod & MOD_ENUMOF)
513 next = enum_specifier(next, &thistype);
514 else if (mod & MOD_ATTRIBUTE)
515 next = attribute_specifier(next, &thistype);
516 else if (mod & MOD_TYPEOF)
517 next = typeof_specifier(next, &thistype);
518 mod = thistype.modifiers;
520 type = thistype.base_type;
521 if (type) {
522 if (qual)
523 break;
524 if (ctype->base_type)
525 break;
526 /* User types only mix with qualifiers */
527 if (mod & MOD_USERTYPE) {
528 if (ctype->modifiers & MOD_SPECIFIER)
529 break;
531 ctype->base_type = type;
534 apply_ctype(token->pos, &thistype, ctype);
537 /* Turn the "virtual types" into real types with real sizes etc */
538 if (!ctype->base_type) {
539 struct symbol *base = &incomplete_ctype;
542 * If we have modifiers, we'll default to an integer
543 * type, and "ctype_integer()" will turn this into
544 * a specific one.
546 if (ctype->modifiers & MOD_SPECIFIER)
547 base = &int_type;
548 ctype->base_type = base;
551 if (ctype->base_type == &int_type) {
552 ctype->base_type = ctype_integer(ctype->modifiers & MOD_SPECIFIER);
553 ctype->modifiers &= ~MOD_SPECIFIER;
554 return token;
556 if (ctype->base_type == &fp_type) {
557 ctype->base_type = ctype_fp(ctype->modifiers & MOD_SPECIFIER);
558 ctype->modifiers &= ~MOD_SPECIFIER;
559 return token;
561 return token;
564 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
566 struct expression *expr = NULL;
568 token = parse_expression(token, &expr);
569 sym->array_size = expr;
570 return token;
573 static struct token *parameter_type_list(struct token *, struct symbol *);
574 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
576 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
578 struct ctype *ctype = &(*tree)->ctype;
580 if (p && token_type(token) == TOKEN_IDENT) {
581 *p = token->ident;
582 token = token->next;
585 for (;;) {
586 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
587 struct ctype thistype = { 0, };
588 token = attribute_specifier(token->next, &thistype);
589 apply_ctype(token->pos, &thistype, ctype);
590 continue;
592 if (token_type(token) != TOKEN_SPECIAL)
593 return token;
596 * This can be either a parameter list or a grouping.
597 * For the direct (non-abstract) case, we know if must be
598 * a paramter list if we already saw the identifier.
599 * For the abstract case, we know if must be a parameter
600 * list if it is empty or starts with a type.
602 if (token->special == '(') {
603 struct symbol *sym;
604 struct token *next = token->next;
605 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
607 if (!fn) {
608 struct symbol *base_type = ctype->base_type;
609 token = declarator(next, tree, p);
610 token = expect(token, ')', "in nested declarator");
611 while (ctype->base_type != base_type)
612 ctype = &ctype->base_type->ctype;
613 p = NULL;
614 continue;
617 sym = indirect(token->pos, ctype, SYM_FN);
618 token = parameter_type_list(next, sym);
619 token = expect(token, ')', "in function declarator");
620 continue;
622 if (token->special == '[') {
623 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
624 token = abstract_array_declarator(token->next, array);
625 token = expect(token, ']', "in abstract_array_declarator");
626 ctype = &array->ctype;
627 continue;
629 break;
631 if (p) {
632 (*tree)->ident = *p;
634 return token;
637 static struct token *pointer(struct token *token, struct ctype *ctype)
639 unsigned long modifiers;
640 struct symbol *base_type;
642 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
643 base_type = ctype->base_type;
644 ctype->modifiers = modifiers;
646 while (match_op(token,'*')) {
647 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
648 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
649 ptr->ctype.as = ctype->as;
650 ptr->ctype.context = ctype->context;
651 ptr->ctype.contextmask = ctype->contextmask;
652 ptr->ctype.base_type = base_type;
654 base_type = ptr;
655 ctype->modifiers = modifiers & MOD_STORAGE;
656 ctype->base_type = base_type;
657 ctype->as = 0;
658 ctype->context = 0;
659 ctype->contextmask = 0;
661 token = declaration_specifiers(token->next, ctype, 1);
662 modifiers = ctype->modifiers;
664 return token;
667 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
669 token = pointer(token, &(*tree)->ctype);
670 return direct_declarator(token, tree, p);
673 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
675 while (!match_op(token, '}')) {
676 struct ctype ctype = {0, };
678 token = declaration_specifiers(token, &ctype, 0);
679 for (;;) {
680 struct ident *ident = NULL;
681 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
682 decl->ctype = ctype;
683 token = declarator(token, &decl, &ident);
684 if (match_op(token, ':')) {
685 struct ctype *ctype = &decl->ctype;
686 struct expression *expr;
687 struct symbol *bitfield;
688 long long width;
690 if (is_int_type (ctype->base_type)) {
691 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
692 token = conditional_expression(token->next, &expr);
693 width = get_expression_value(expr);
694 bitfield->fieldwidth = width;
695 if (width < 0) {
696 warn(token->pos, "invalid negative bitfield width, %lld.", width);
697 bitfield->fieldwidth = 8;
698 } else if (decl->ident && width == 0) {
699 warn(token->pos, "invalid named zero-width bitfield `%s'",
700 show_ident (decl->ident));
701 bitfield->fieldwidth = 8;
702 } else if (width != bitfield->fieldwidth) {
703 // Overflow.
704 unsigned int stupid_gcc = -1;
705 bitfield->fieldwidth = stupid_gcc;
706 warn(token->pos, "truncating large bitfield from %lld to %d bits", width, bitfield->fieldwidth);
708 } else {
709 warn(token->pos, "invalid bitfield specifier for type %s.", show_typename (ctype->base_type));
710 // Parse this to recover gracefully.
711 token = conditional_expression(token->next, &expr);
713 while (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
714 struct ctype thistype = { 0, };
715 token = attribute_specifier(token->next, &thistype);
716 apply_ctype(token->pos, &thistype, ctype);
719 add_symbol(list, decl);
720 if (!match_op(token, ','))
721 break;
722 token = token->next;
724 if (!match_op(token, ';')) {
725 warn(token->pos, "expected ; at end of declaration");
726 break;
728 token = token->next;
730 return token;
733 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
735 struct ident *ident = NULL;
736 struct symbol *sym;
737 struct ctype ctype = { 0, };
739 token = declaration_specifiers(token, &ctype, 0);
740 sym = alloc_symbol(token->pos, SYM_NODE);
741 sym->ctype = ctype;
742 *tree = sym;
743 token = declarator(token, tree, &ident);
744 return token;
747 struct token *typename(struct token *token, struct symbol **p)
749 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
750 *p = sym;
751 token = declaration_specifiers(token, &sym->ctype, 0);
752 return declarator(token, &sym, NULL);
755 struct token *expression_statement(struct token *token, struct expression **tree)
757 token = parse_expression(token, tree);
758 return expect(token, ';', "at end of statement");
761 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
763 struct expression *expr;
765 /* Allow empty operands */
766 if (match_op(token->next, ':') || match_op(token->next, ')'))
767 return token->next;
768 do {
769 if (match_op(token->next, '[') &&
770 token_type(token->next->next) == TOKEN_IDENT &&
771 match_op(token->next->next->next, ']'))
772 token = token->next->next->next;
773 token = primary_expression(token->next, &expr);
774 token = parens_expression(token, &expr, "in asm parameter");
775 } while (match_op(token, ','));
776 return token;
779 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
781 struct expression *expr;
783 do {
784 token = primary_expression(token->next, &expr);
785 } while (match_op(token, ','));
786 return token;
789 static struct token *parse_asm(struct token *token, struct statement *stmt)
791 struct expression *expr;
793 stmt->type = STMT_ASM;
794 if (match_idents(token, &__volatile___ident, &volatile_ident, NULL)) {
795 token = token->next;
797 token = expect(token, '(', "after asm");
798 token = parse_expression(token->next, &expr);
799 if (match_op(token, ':'))
800 token = parse_asm_operands(token, stmt);
801 if (match_op(token, ':'))
802 token = parse_asm_operands(token, stmt);
803 if (match_op(token, ':'))
804 token = parse_asm_clobbers(token, stmt);
805 token = expect(token, ')', "after asm");
806 return expect(token, ';', "at end of asm-statement");
809 /* Make a statement out of an expression */
810 static struct statement *make_statement(struct expression *expr)
812 struct statement *stmt;
814 if (!expr)
815 return NULL;
816 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
817 stmt->expression = expr;
818 return stmt;
822 * All iterators have two symbols associated with them:
823 * the "continue" and "break" symbols, which are targets
824 * for continue and break statements respectively.
826 * They are in a special name-space, but they follow
827 * all the normal visibility rules, so nested iterators
828 * automatically work right.
830 static void start_iterator(struct statement *stmt)
832 struct symbol *cont, *brk;
834 start_symbol_scope();
835 cont = alloc_symbol(stmt->pos, SYM_NODE);
836 cont->ident = &continue_ident;
837 bind_symbol(cont, &continue_ident, NS_ITERATOR);
838 brk = alloc_symbol(stmt->pos, SYM_NODE);
839 brk->ident = &break_ident;
840 bind_symbol(brk, &break_ident, NS_ITERATOR);
842 stmt->type = STMT_ITERATOR;
843 stmt->iterator_break = brk;
844 stmt->iterator_continue = cont;
845 fn_local_symbol(brk);
846 fn_local_symbol(cont);
849 static void end_iterator(struct statement *stmt)
851 end_symbol_scope();
854 static struct statement *start_function(struct symbol *sym)
856 struct symbol *ret;
857 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
859 start_function_scope();
860 ret = alloc_symbol(sym->pos, SYM_NODE);
861 ret->ident = &return_ident;
862 ret->ctype = sym->ctype.base_type->ctype;
863 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
864 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
865 bind_symbol(ret, &return_ident, NS_ITERATOR);
866 stmt->ret = ret;
868 fn_local_symbol(ret);
869 return stmt;
872 static void end_function(struct symbol *sym)
874 end_function_scope();
878 * A "switch()" statement, like an iterator, has a
879 * the "break" symbol associated with it. It works
880 * exactly like the iterator break - it's the target
881 * for any break-statements in scope, and means that
882 * "break" handling doesn't even need to know whether
883 * it's breaking out of an iterator or a switch.
885 * In addition, the "case" symbol is a marker for the
886 * case/default statements to find the switch statement
887 * that they are associated with.
889 static void start_switch(struct statement *stmt)
891 struct symbol *brk, *switch_case;
893 start_symbol_scope();
894 brk = alloc_symbol(stmt->pos, SYM_NODE);
895 brk->ident = &break_ident;
896 bind_symbol(brk, &break_ident, NS_ITERATOR);
898 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
899 switch_case->ident = &case_ident;
900 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
901 switch_case->stmt = stmt;
903 stmt->type = STMT_SWITCH;
904 stmt->switch_break = brk;
905 stmt->switch_case = switch_case;
907 fn_local_symbol(brk);
908 fn_local_symbol(switch_case);
911 static void end_switch(struct statement *stmt)
913 if (!stmt->switch_case->symbol_list)
914 warn(stmt->pos, "switch with no cases");
915 end_symbol_scope();
918 static void add_case_statement(struct statement *stmt)
920 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
921 struct symbol *sym;
923 if (!target) {
924 warn(stmt->pos, "not in switch scope");
925 return;
927 sym = alloc_symbol(stmt->pos, SYM_NODE);
928 add_symbol(&target->symbol_list, sym);
929 sym->stmt = stmt;
930 stmt->case_label = sym;
931 fn_local_symbol(sym);
934 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
936 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
938 if (!target)
939 error(token->pos, "internal error: return without a function target");
940 stmt->type = STMT_RETURN;
941 stmt->ret_target = target;
942 return expression_statement(token->next, &stmt->ret_value);
945 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
947 struct symbol_list *syms;
948 struct expression *e1, *e2, *e3;
949 struct statement *iterator;
951 start_iterator(stmt);
952 token = expect(token->next, '(', "after 'for'");
954 syms = NULL;
955 e1 = NULL;
956 /* C99 variable declaration? */
957 if (lookup_type(token)) {
958 token = external_declaration(token, &syms);
959 } else {
960 token = parse_expression(token, &e1);
961 token = expect(token, ';', "in 'for'");
963 token = parse_expression(token, &e2);
964 token = expect(token, ';', "in 'for'");
965 token = parse_expression(token, &e3);
966 token = expect(token, ')', "in 'for'");
967 token = statement(token, &iterator);
969 stmt->iterator_syms = syms;
970 stmt->iterator_pre_statement = make_statement(e1);
971 stmt->iterator_pre_condition = e2;
972 stmt->iterator_post_statement = make_statement(e3);
973 stmt->iterator_post_condition = e2;
974 stmt->iterator_statement = iterator;
975 end_iterator(stmt);
977 return token;
980 struct token *parse_while_statement(struct token *token, struct statement *stmt)
982 struct expression *expr;
983 struct statement *iterator;
985 start_iterator(stmt);
986 token = parens_expression(token->next, &expr, "after 'while'");
987 token = statement(token, &iterator);
989 stmt->iterator_pre_condition = expr;
990 stmt->iterator_post_condition = expr;
991 stmt->iterator_statement = iterator;
992 end_iterator(stmt);
994 return token;
997 struct token *parse_do_statement(struct token *token, struct statement *stmt)
999 struct expression *expr;
1000 struct statement *iterator;
1002 start_iterator(stmt);
1003 token = statement(token->next, &iterator);
1004 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1005 token = token->next;
1006 else
1007 warn(token->pos, "expected 'while' after 'do'");
1008 token = parens_expression(token, &expr, "after 'do-while'");
1010 stmt->iterator_post_condition = expr;
1011 stmt->iterator_statement = iterator;
1012 end_iterator(stmt);
1014 return expect(token, ';', "after statement");
1017 static struct token *statement(struct token *token, struct statement **tree)
1019 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1021 *tree = stmt;
1022 if (token_type(token) == TOKEN_IDENT) {
1023 if (token->ident == &if_ident) {
1024 stmt->type = STMT_IF;
1025 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1026 token = statement(token, &stmt->if_true);
1027 if (token_type(token) != TOKEN_IDENT)
1028 return token;
1029 if (token->ident != &else_ident)
1030 return token;
1031 return statement(token->next, &stmt->if_false);
1034 if (token->ident == &return_ident)
1035 return parse_return_statement(token, stmt);
1037 if (token->ident == &break_ident || token->ident == &continue_ident) {
1038 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1039 stmt->type = STMT_GOTO;
1040 stmt->goto_label = target;
1041 if (!target)
1042 warn(stmt->pos, "break/continue not in iterator scope");
1043 return expect(token->next, ';', "at end of statement");
1045 if (token->ident == &default_ident) {
1046 token = token->next;
1047 goto default_statement;
1049 if (token->ident == &case_ident) {
1050 token = parse_expression(token->next, &stmt->case_expression);
1051 if (match_op(token, SPECIAL_ELLIPSIS))
1052 token = parse_expression(token->next, &stmt->case_to);
1053 default_statement:
1054 stmt->type = STMT_CASE;
1055 token = expect(token, ':', "after default/case");
1056 add_case_statement(stmt);
1057 return statement(token, &stmt->case_statement);
1059 if (token->ident == &switch_ident) {
1060 stmt->type = STMT_SWITCH;
1061 start_switch(stmt);
1062 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1063 token = statement(token, &stmt->switch_statement);
1064 end_switch(stmt);
1065 return token;
1067 if (token->ident == &for_ident)
1068 return parse_for_statement(token, stmt);
1070 if (token->ident == &while_ident)
1071 return parse_while_statement(token, stmt);
1073 if (token->ident == &do_ident)
1074 return parse_do_statement(token, stmt);
1076 if (token->ident == &goto_ident) {
1077 stmt->type = STMT_GOTO;
1078 token = token->next;
1079 if (match_op(token, '*')) {
1080 token = parse_expression(token->next, &stmt->goto_expression);
1081 add_statement(&function_computed_goto_list, stmt);
1082 } else if (token_type(token) == TOKEN_IDENT) {
1083 stmt->goto_label = label_symbol(token);
1084 token = token->next;
1085 } else {
1086 warn(token->pos, "Expected identifier or goto expression");
1088 return expect(token, ';', "at end of statement");
1090 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1091 return parse_asm(token->next, stmt);
1093 if (match_op(token->next, ':')) {
1094 stmt->type = STMT_LABEL;
1095 stmt->label_identifier = label_symbol(token);
1096 return statement(token->next->next, &stmt->label_statement);
1100 if (match_op(token, '{')) {
1101 stmt->type = STMT_COMPOUND;
1102 start_symbol_scope();
1103 token = compound_statement(token->next, stmt);
1104 end_symbol_scope();
1106 return expect(token, '}', "at end of compound statement");
1109 stmt->type = STMT_EXPRESSION;
1110 return expression_statement(token, &stmt->expression);
1113 struct token * statement_list(struct token *token, struct statement_list **list)
1115 for (;;) {
1116 struct statement * stmt;
1117 if (eof_token(token))
1118 break;
1119 if (match_op(token, '}'))
1120 break;
1121 token = statement(token, &stmt);
1122 add_statement(list, stmt);
1124 return token;
1127 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1129 struct symbol_list **list = &fn->arguments;
1130 for (;;) {
1131 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1133 if (match_op(token, SPECIAL_ELLIPSIS)) {
1134 fn->variadic = 1;
1135 token = token->next;
1136 break;
1139 if (!lookup_type(token)) {
1140 warn(token->pos, "non-ANSI parameter list");
1141 break;
1143 token = parameter_declaration(token, &sym);
1144 if (sym->ctype.base_type == &void_ctype) {
1145 /* Special case: (void) */
1146 if (!*list && !sym->ident)
1147 break;
1148 warn(token->pos, "void parameter");
1150 add_symbol(list, sym);
1151 if (!match_op(token, ','))
1152 break;
1153 token = token->next;
1156 return token;
1159 struct token *compound_statement(struct token *token, struct statement *stmt)
1161 while (!eof_token(token)) {
1162 if (!lookup_type(token))
1163 break;
1164 token = external_declaration(token, &stmt->syms);
1166 token = statement_list(token, &stmt->stmts);
1167 return token;
1170 static struct expression *identifier_expression(struct token *token)
1172 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1173 expr->expr_ident = token->ident;
1174 return expr;
1177 static struct expression *index_expression(struct expression *from, struct expression *to)
1179 int idx_from, idx_to;
1180 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1182 idx_from = get_expression_value(from);
1183 idx_to = idx_from;
1184 if (to) {
1185 idx_to = get_expression_value(to);
1186 if (idx_to < idx_from || idx_from < 0)
1187 warn(from->pos, "nonsense array initializer index range");
1189 expr->idx_from = idx_from;
1190 expr->idx_to = idx_to;
1191 return expr;
1194 static struct token *initializer_list(struct expression_list **list, struct token *token)
1196 for (;;) {
1197 struct token *next = token->next;
1198 struct expression *expr;
1200 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
1201 add_expression(list, identifier_expression(next));
1202 token = next->next->next;
1203 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1204 add_expression(list, identifier_expression(token));
1205 token = next->next;
1206 } else if (match_op(token, '[')) {
1207 struct expression *from = NULL, *to = NULL;
1208 token = constant_expression(token->next, &from);
1209 if (match_op(token, SPECIAL_ELLIPSIS))
1210 token = constant_expression(token->next, &to);
1211 add_expression(list, index_expression(from, to));
1212 token = expect(token, ']', "at end of initializer index");
1213 token = expect(token, '=', "at end of initializer index");
1216 expr = NULL;
1217 token = initializer(&expr, token);
1218 if (!expr)
1219 break;
1220 add_expression(list, expr);
1221 if (!match_op(token, ','))
1222 break;
1223 token = token->next;
1225 return token;
1228 struct token *initializer(struct expression **tree, struct token *token)
1230 if (match_op(token, '{')) {
1231 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1232 *tree = expr;
1233 token = initializer_list(&expr->expr_list, token->next);
1234 return expect(token, '}', "at end of initializer");
1236 return assignment_expression(token, tree);
1239 static void declare_argument(struct symbol *sym, struct symbol *fn)
1241 if (!sym->ident) {
1242 warn(sym->pos, "no identifier for function argument");
1243 return;
1245 bind_symbol(sym, sym->ident, NS_SYMBOL);
1248 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1249 struct symbol_list **list)
1251 struct symbol *base_type = decl->ctype.base_type;
1252 struct statement *stmt;
1253 struct symbol *arg;
1255 function_symbol_list = &decl->symbol_list;
1256 function_computed_target_list = NULL;
1257 function_computed_goto_list = NULL;
1259 if (decl->ctype.modifiers & MOD_EXTERN) {
1260 if (!(decl->ctype.modifiers & MOD_INLINE))
1261 warn(decl->pos, "function with external linkage has definition");
1263 if (!(decl->ctype.modifiers & MOD_STATIC))
1264 decl->ctype.modifiers |= MOD_EXTERN;
1266 stmt = start_function(decl);
1268 base_type->stmt = stmt;
1269 FOR_EACH_PTR (base_type->arguments, arg) {
1270 declare_argument(arg, base_type);
1271 } END_FOR_EACH_PTR;
1273 token = compound_statement(token->next, stmt);
1275 end_function(decl);
1276 if (!(decl->ctype.modifiers & MOD_INLINE))
1277 add_symbol(list, decl);
1278 check_declaration(decl);
1279 function_symbol_list = NULL;
1280 if (function_computed_goto_list) {
1281 if (!function_computed_target_list)
1282 warn(decl->pos, "function has computed goto but no targets?");
1283 else {
1284 struct statement *stmt;
1285 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1286 stmt->target_list = function_computed_target_list;
1287 } END_FOR_EACH_PTR;
1290 return expect(token, '}', "at end of function");
1293 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1295 struct ident *ident = NULL;
1296 struct symbol *decl;
1297 struct ctype ctype = { 0, };
1298 struct symbol *base_type;
1299 int is_typedef;
1301 /* Top-level inline asm? */
1302 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1303 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1304 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1305 struct statement *stmt;
1307 anon->ctype.base_type = fn;
1308 function_symbol_list = &anon->symbol_list;
1309 stmt = start_function(anon);
1310 token = parse_asm(token->next, stmt);
1311 end_function(anon);
1312 function_symbol_list = NULL;
1313 add_symbol(list, anon);
1314 return token;
1317 /* Parse declaration-specifiers, if any */
1318 token = declaration_specifiers(token, &ctype, 0);
1319 decl = alloc_symbol(token->pos, SYM_NODE);
1320 decl->ctype = ctype;
1321 token = declarator(token, &decl, &ident);
1323 /* Just a type declaration? */
1324 if (!ident)
1325 return expect(token, ';', "end of type declaration");
1327 decl->ident = ident;
1329 /* type define declaration? */
1330 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1332 /* Typedef's don't have meaningful storage */
1333 if (is_typedef) {
1334 ctype.modifiers &= ~MOD_STORAGE;
1335 decl->ctype.modifiers &= ~MOD_STORAGE;
1336 decl->ctype.modifiers |= MOD_USERTYPE;
1339 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1341 base_type = decl->ctype.base_type;
1342 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1343 if (match_op(token, '{'))
1344 return parse_function_body(token, decl, list);
1346 if (!(decl->ctype.modifiers & MOD_STATIC))
1347 decl->ctype.modifiers |= MOD_EXTERN;
1348 } else if (!is_typedef && base_type == &void_ctype) {
1349 warn(token->pos, "void declaration");
1352 for (;;) {
1353 if (token_type(token) == TOKEN_IDENT) {
1354 if (token->ident == &asm_ident || token->ident == &__asm_ident || token->ident == &__asm___ident) {
1355 struct expression *expr;
1357 token = expect(token->next, '(', "after asm");
1358 token = parse_expression(token->next, &expr);
1359 token = expect(token, ')', "after asm");
1362 if (!is_typedef && match_op(token, '=')) {
1363 if (decl->ctype.modifiers & MOD_EXTERN) {
1364 warn(decl->pos, "symbol with external linkage has initializer");
1365 decl->ctype.modifiers &= ~MOD_EXTERN;
1367 token = initializer(&decl->initializer, token->next);
1369 if (!is_typedef) {
1370 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1371 add_symbol(list, decl);
1372 if (function_symbol_list)
1373 fn_local_symbol(decl);
1376 check_declaration(decl);
1378 if (!match_op(token, ','))
1379 break;
1381 token = token->next;
1382 ident = NULL;
1383 decl = alloc_symbol(token->pos, SYM_NODE);
1384 decl->ctype = ctype;
1385 token = declaration_specifiers(token, &decl->ctype, 1);
1386 token = declarator(token, &decl, &ident);
1387 if (!ident) {
1388 warn(token->pos, "expected identifier name in type definition");
1389 return token;
1392 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1394 /* Function declarations are automatically extern unless specifically static */
1395 base_type = decl->ctype.base_type;
1396 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1397 if (!(decl->ctype.modifiers & MOD_STATIC))
1398 decl->ctype.modifiers |= MOD_EXTERN;
1401 return expect(token, ';', "at end of declaration");
1404 void translation_unit(struct token *token, struct symbol_list **list)
1406 while (!eof_token(token))
1407 token = external_declaration(token, list);
1408 // They aren't needed any more
1409 clear_token_alloc();