[PATCH] speed up (and fix corner case in) tokenizer
[smatch.git] / parse.c
blob2dffe675292ae37614403bb32c7221130b38d470
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;
30 // Add a symbol to the list of function-local symbols
31 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
33 static struct token *statement(struct token *token, struct statement **tree);
34 static struct token *external_declaration(struct token *token, struct symbol_list **list);
36 static int match_idents(struct token *token, ...)
38 va_list args;
40 if (token_type(token) != TOKEN_IDENT)
41 return 0;
43 va_start(args, token);
44 for (;;) {
45 struct ident * next = va_arg(args, struct ident *);
46 if (!next)
47 return 0;
48 if (token->ident == next)
49 return 1;
54 struct statement *alloc_statement(struct position pos, int type)
56 struct statement *stmt = __alloc_statement(0);
57 stmt->type = type;
58 stmt->pos = pos;
59 return stmt;
62 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
64 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
66 struct symbol *sym = alloc_symbol(pos, type);
68 sym->ctype.base_type = ctype->base_type;
69 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
71 ctype->base_type = sym;
72 ctype->modifiers &= MOD_STORAGE;
73 return sym;
76 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
78 struct symbol *sym = lookup_symbol(token->ident, ns);
79 if (!sym) {
80 sym = alloc_symbol(token->pos, type);
81 sym->ident = token->ident;
82 bind_symbol(sym, token->ident, ns);
83 if (type == SYM_LABEL)
84 fn_local_symbol(sym);
86 return sym;
90 * NOTE! NS_LABEL is not just a different namespace,
91 * it also ends up using function scope instead of the
92 * regular symbol scope.
94 struct symbol *label_symbol(struct token *token)
96 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
99 struct token *struct_union_enum_specifier(enum namespace ns, enum type type,
100 struct token *token, struct ctype *ctype,
101 struct token *(*parse)(struct token *, struct symbol *))
103 struct symbol *sym;
105 ctype->modifiers = 0;
106 if (token_type(token) == TOKEN_IDENT) {
107 sym = lookup_or_create_symbol(ns, type, token);
108 token = token->next;
109 ctype->base_type = sym;
110 if (match_op(token, '{')) {
111 token = parse(token->next, sym);
112 token = expect(token, '}', "at end of struct-union-enum-specifier");
114 return token;
117 // private struct/union/enum type
118 if (!match_op(token, '{')) {
119 warn(token->pos, "expected declaration");
120 ctype->base_type = &bad_type;
121 return token;
124 sym = alloc_symbol(token->pos, type);
125 token = parse(token->next, sym);
126 ctype->base_type = sym;
127 return expect(token, '}', "at end of specifier");
130 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
132 return struct_declaration_list(token, &sym->symbol_list);
135 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
137 return struct_union_enum_specifier(NS_STRUCT, type, token, ctype, parse_struct_declaration);
140 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
142 int nextval = 0;
143 while (token_type(token) == TOKEN_IDENT) {
144 struct token *next = token->next;
145 struct symbol *sym;
147 sym = alloc_symbol(token->pos, SYM_ENUM);
148 bind_symbol(sym, token->ident, NS_SYMBOL);
149 sym->ctype.base_type = parent;
150 parent->ctype.base_type = &int_ctype;
152 if (match_op(next, '=')) {
153 struct expression *expr;
154 next = constant_expression(next->next, &expr);
155 nextval = get_expression_value(expr);
157 sym->value = nextval;
159 token = next;
160 if (!match_op(token, ','))
161 break;
162 token = token->next;
163 nextval = nextval + 1;
165 return token;
168 struct token *enum_specifier(struct token *token, struct ctype *ctype)
170 return struct_union_enum_specifier(NS_ENUM, SYM_ENUM, token, ctype, parse_enum_declaration);
173 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
175 struct symbol *sym;
177 if (!match_op(token, '(')) {
178 warn(token->pos, "expected '(' after typeof");
179 return token;
181 if (lookup_type(token->next)) {
182 token = typename(token->next, &sym);
183 *ctype = sym->ctype;
184 } else {
185 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
186 token = parse_expression(token->next, &typeof_sym->initializer);
188 ctype->modifiers = 0;
189 ctype->base_type = typeof_sym;
191 return expect(token, ')', "after typeof");
194 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
196 if (match_string_ident(attribute, "packed") ||
197 match_string_ident(attribute, "__packed__")) {
198 ctype->alignment = 1;
199 return NULL;
201 if (match_string_ident(attribute, "aligned") ||
202 match_string_ident(attribute, "__aligned__")) {
203 int alignment = max_alignment;
204 if (expr)
205 alignment = get_expression_value(expr);
206 ctype->alignment = alignment;
207 return NULL;
209 if (match_string_ident(attribute, "nocast")) {
210 ctype->modifiers |= MOD_NOCAST;
211 return NULL;
213 if (match_string_ident(attribute, "noderef")) {
214 ctype->modifiers |= MOD_NODEREF;
215 return NULL;
217 if (match_string_ident(attribute, "safe")) {
218 ctype->modifiers |= MOD_SAFE;
219 return NULL;
221 if (match_string_ident(attribute, "force")) {
222 ctype->modifiers |= MOD_FORCE;
223 return NULL;
225 if (match_string_ident(attribute, "address_space")) {
226 if (!expr)
227 return "expected address space number";
228 ctype->as = get_expression_value(expr);
229 return NULL;
231 if (match_string_ident(attribute, "context")) {
232 if (expr && expr->type == EXPR_COMMA) {
233 int mask = get_expression_value(expr->left);
234 int value = get_expression_value(expr->right);
235 if (value & ~mask)
236 return "nonsense attribute types";
237 ctype->contextmask |= mask;
238 ctype->context |= value;
239 return NULL;
241 return "expected context mask and value";
243 if (match_string_ident(attribute, "mode") ||
244 match_string_ident(attribute, "__mode__")) {
245 if (expr && expr->type == EXPR_SYMBOL) {
246 struct ident *ident = expr->symbol_name;
249 * Match against __QI__/__HI__/__SI__/__DI__
251 * FIXME! This is broken - we don't actually get
252 * the type information updated properly at this
253 * stage for some reason.
255 if (match_string_ident(ident, "__QI__") ||
256 match_string_ident(ident, "QI")) {
257 ctype->modifiers |= MOD_SHORT;
258 ctype->base_type = ctype_integer(ctype->modifiers);
259 return NULL;
261 if (match_string_ident(ident, "__HI__") ||
262 match_string_ident(ident, "HI")) {
263 ctype->modifiers |= MOD_SHORT;
264 ctype->base_type = ctype_integer(ctype->modifiers);
265 return NULL;
267 if (match_string_ident(ident, "__SI__") ||
268 match_string_ident(ident, "SI")) {
269 /* Nothing? */
270 return NULL;
272 if (match_string_ident(ident, "__DI__") ||
273 match_string_ident(ident, "DI")) {
274 ctype->modifiers |= MOD_LONGLONG;
275 ctype->base_type = ctype_integer(ctype->modifiers);
276 return NULL;
278 return "unknown mode attribute";
280 return "expected attribute mode symbol";
283 /* Throw away for now.. */
284 if (match_string_ident(attribute, "format") ||
285 match_string_ident(attribute, "__format__"))
286 return NULL;
287 if (match_string_ident(attribute, "section") ||
288 match_string_ident(attribute, "__section__"))
289 return NULL;
290 if (match_string_ident(attribute, "unused") ||
291 match_string_ident(attribute, "__unused__"))
292 return NULL;
293 if (match_string_ident(attribute, "const") ||
294 match_string_ident(attribute, "__const__"))
295 return NULL;
296 if (match_string_ident(attribute, "noreturn"))
297 return NULL;
298 if (match_string_ident(attribute, "regparm"))
299 return NULL;
300 if (match_string_ident(attribute, "weak"))
301 return NULL;
302 if (match_string_ident(attribute, "alias"))
303 return NULL;
304 if (match_string_ident(attribute, "pure"))
305 return NULL;
306 if (match_string_ident(attribute, "always_inline"))
307 return NULL;
308 if (match_string_ident(attribute, "syscall_linkage"))
309 return NULL;
311 return "unknown attribute";
314 struct token *attribute_specifier(struct token *token, struct ctype *ctype)
316 ctype->modifiers = 0;
317 token = expect(token, '(', "after attribute");
318 token = expect(token, '(', "after attribute");
320 for (;;) {
321 const char *error;
322 struct ident *attribute_name;
323 struct expression *attribute_expr;
325 if (eof_token(token))
326 break;
327 if (match_op(token, ';'))
328 break;
329 if (token_type(token) != TOKEN_IDENT)
330 break;
331 attribute_name = token->ident;
332 token = token->next;
333 attribute_expr = NULL;
334 if (match_op(token, '('))
335 token = parens_expression(token, &attribute_expr, "in attribute");
336 error = handle_attribute(ctype, attribute_name, attribute_expr);
337 if (error)
338 warn(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
339 if (!match_op(token, ','))
340 break;
341 token = token->next;
344 token = expect(token, ')', "after attribute");
345 token = expect(token, ')', "after attribute");
346 return token;
349 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
350 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
352 struct symbol * ctype_integer(unsigned int spec)
354 static struct symbol *const integer_ctypes[][2] = {
355 { &llong_ctype, &ullong_ctype },
356 { &long_ctype, &ulong_ctype },
357 { &short_ctype, &ushort_ctype },
358 { &char_ctype, &uchar_ctype },
359 { &int_ctype, &uint_ctype },
361 struct symbol *const (*ctype)[2];
363 ctype = integer_ctypes;
364 if (!(spec & MOD_LONGLONG)) {
365 ctype++;
366 if (!(spec & MOD_LONG)) {
367 ctype++;
368 if (!(spec & MOD_SHORT)) {
369 ctype++;
370 if (!(spec & MOD_CHAR))
371 ctype++;
375 return ctype[0][(spec & MOD_UNSIGNED) != 0];
378 struct symbol * ctype_fp(unsigned int spec)
380 if (spec & MOD_LONGLONG)
381 return &ldouble_ctype;
382 if (spec & MOD_LONG)
383 return &double_ctype;
384 return &float_ctype;
387 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
389 unsigned long mod = thistype->modifiers;
391 if (mod) {
392 unsigned long old = ctype->modifiers;
393 unsigned long extra = 0, dup;
395 if (mod & old & MOD_LONG) {
396 extra = MOD_LONGLONG | MOD_LONG;
397 mod &= ~MOD_LONG;
398 old &= ~MOD_LONG;
400 dup = (mod & old) | (extra & old) | (extra & mod);
401 if (dup)
402 warn(pos, "Just how %sdo you want this type to be?",
403 modifier_string(dup));
404 ctype->modifiers = old | mod | extra;
407 /* Context mask and value */
408 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
409 warn(pos, "inconsistend attribute types");
410 thistype->context = 0;
411 thistype->contextmask = 0;
413 ctype->context |= thistype->context;
414 ctype->contextmask |= thistype->contextmask;
416 /* Alignment */
417 if (thistype->alignment & (thistype->alignment-1)) {
418 warn(pos, "I don't like non-power-of-2 alignments");
419 thistype->alignment = 0;
421 if (thistype->alignment > ctype->alignment)
422 ctype->alignment = thistype->alignment;
424 /* Address space */
425 ctype->as = thistype->as;
429 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
431 struct token *token;
433 while ( (token = next) != NULL ) {
434 struct ctype thistype;
435 struct ident *ident;
436 struct symbol *s, *type;
437 unsigned long mod;
439 next = token->next;
440 if (token_type(token) != TOKEN_IDENT)
441 break;
442 ident = token->ident;
444 s = lookup_symbol(ident, NS_TYPEDEF);
445 if (!s)
446 break;
447 thistype = s->ctype;
448 mod = thistype.modifiers;
449 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
450 break;
451 if (mod & MOD_SPECIALBITS) {
452 if (mod & MOD_STRUCTOF)
453 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
454 else if (mod & MOD_UNIONOF)
455 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
456 else if (mod & MOD_ENUMOF)
457 next = enum_specifier(next, &thistype);
458 else if (mod & MOD_ATTRIBUTE)
459 next = attribute_specifier(next, &thistype);
460 else if (mod & MOD_TYPEOF)
461 next = typeof_specifier(next, &thistype);
462 mod = thistype.modifiers;
464 type = thistype.base_type;
465 if (type) {
466 if (qual)
467 break;
468 if (ctype->base_type)
469 break;
470 /* User types only mix with qualifiers */
471 if (mod & MOD_USERTYPE) {
472 if (ctype->modifiers & MOD_SPECIFIER)
473 break;
475 ctype->base_type = type;
478 apply_ctype(token->pos, &thistype, ctype);
481 /* Turn the "virtual types" into real types with real sizes etc */
482 if (!ctype->base_type) {
483 struct symbol *base = &incomplete_ctype;
486 * If we have modifiers, we'll default to an integer
487 * type, and "ctype_integer()" will turn this into
488 * a specific one.
490 if (ctype->modifiers & MOD_SPECIFIER)
491 base = &int_type;
492 ctype->base_type = base;
495 if (ctype->base_type == &int_type) {
496 ctype->base_type = ctype_integer(ctype->modifiers & MOD_SPECIFIER);
497 ctype->modifiers &= ~MOD_SPECIFIER;
498 return token;
500 if (ctype->base_type == &fp_type) {
501 ctype->base_type = ctype_fp(ctype->modifiers & MOD_SPECIFIER);
502 ctype->modifiers &= ~MOD_SPECIFIER;
503 return token;
505 return token;
508 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
510 struct expression *expr = NULL;
512 token = parse_expression(token, &expr);
513 sym->array_size = expr;
514 return token;
517 static struct token *parameter_type_list(struct token *, struct symbol *);
518 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
520 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
522 struct ctype *ctype = &(*tree)->ctype;
524 if (p && token_type(token) == TOKEN_IDENT) {
525 *p = token->ident;
526 token = token->next;
529 for (;;) {
530 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
531 struct ctype thistype = { 0, };
532 token = attribute_specifier(token->next, &thistype);
533 apply_ctype(token->pos, &thistype, ctype);
534 continue;
536 if (token_type(token) != TOKEN_SPECIAL)
537 return token;
540 * This can be either a parameter list or a grouping.
541 * For the direct (non-abstract) case, we know if must be
542 * a paramter list if we already saw the identifier.
543 * For the abstract case, we know if must be a parameter
544 * list if it is empty or starts with a type.
546 if (token->special == '(') {
547 struct symbol *sym;
548 struct token *next = token->next;
549 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
551 if (!fn) {
552 struct symbol *base_type = ctype->base_type;
553 token = declarator(next, tree, p);
554 token = expect(token, ')', "in nested declarator");
555 while (ctype->base_type != base_type)
556 ctype = &ctype->base_type->ctype;
557 p = NULL;
558 continue;
561 sym = indirect(token->pos, ctype, SYM_FN);
562 token = parameter_type_list(next, sym);
563 token = expect(token, ')', "in function declarator");
564 continue;
566 if (token->special == '[') {
567 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
568 token = abstract_array_declarator(token->next, array);
569 token = expect(token, ']', "in abstract_array_declarator");
570 ctype = &array->ctype;
571 continue;
573 if (token->special == ':') {
574 struct symbol *bitfield;
575 struct expression *expr;
576 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
577 token = conditional_expression(token->next, &expr);
578 bitfield->fieldwidth = get_expression_value(expr);
579 continue;
581 break;
583 if (p) {
584 (*tree)->ident = *p;
586 return token;
589 static struct token *pointer(struct token *token, struct ctype *ctype)
591 unsigned long modifiers;
592 struct symbol *base_type;
594 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
595 base_type = ctype->base_type;
596 ctype->modifiers = modifiers;
598 while (match_op(token,'*')) {
599 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
600 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
601 ptr->ctype.as = ctype->as;
602 ptr->ctype.context = ctype->context;
603 ptr->ctype.contextmask = ctype->contextmask;
604 ptr->ctype.base_type = base_type;
606 base_type = ptr;
607 ctype->modifiers = modifiers & MOD_STORAGE;
608 ctype->base_type = base_type;
609 ctype->as = 0;
610 ctype->context = 0;
611 ctype->contextmask = 0;
613 token = declaration_specifiers(token->next, ctype, 1);
614 modifiers = ctype->modifiers;
616 return token;
619 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
621 token = pointer(token, &(*tree)->ctype);
622 return direct_declarator(token, tree, p);
625 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
627 while (!match_op(token, '}')) {
628 struct ctype ctype = {0, };
630 token = declaration_specifiers(token, &ctype, 0);
631 for (;;) {
632 struct ident *ident = NULL;
633 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
634 decl->ctype = ctype;
635 token = declarator(token, &decl, &ident);
636 if (match_op(token, ':')) {
637 struct expression *expr;
638 token = parse_expression(token->next, &expr);
640 add_symbol(list, decl);
641 if (!match_op(token, ','))
642 break;
643 token = token->next;
645 if (!match_op(token, ';'))
646 break;
647 token = token->next;
649 return token;
652 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
654 struct ident *ident = NULL;
655 struct symbol *sym;
656 struct ctype ctype = { 0, };
658 token = declaration_specifiers(token, &ctype, 0);
659 sym = alloc_symbol(token->pos, SYM_NODE);
660 sym->ctype = ctype;
661 *tree = sym;
662 token = declarator(token, tree, &ident);
663 return token;
666 struct token *typename(struct token *token, struct symbol **p)
668 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
669 *p = sym;
670 token = declaration_specifiers(token, &sym->ctype, 0);
671 return declarator(token, &sym, NULL);
674 struct token *expression_statement(struct token *token, struct expression **tree)
676 token = parse_expression(token, tree);
677 return expect(token, ';', "at end of statement");
680 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
682 struct expression *expr;
684 /* Allow empty operands */
685 if (match_op(token->next, ':') || match_op(token->next, ')'))
686 return token->next;
687 do {
688 token = primary_expression(token->next, &expr);
689 token = parens_expression(token, &expr, "in asm parameter");
690 } while (match_op(token, ','));
691 return token;
694 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
696 struct expression *expr;
698 do {
699 token = primary_expression(token->next, &expr);
700 } while (match_op(token, ','));
701 return token;
704 static struct token *parse_asm(struct token *token, struct statement *stmt)
706 struct expression *expr;
708 stmt->type = STMT_ASM;
709 if (match_idents(token, &__volatile___ident, &volatile_ident)) {
710 token = token->next;
712 token = expect(token, '(', "after asm");
713 token = parse_expression(token->next, &expr);
714 if (match_op(token, ':'))
715 token = parse_asm_operands(token, stmt);
716 if (match_op(token, ':'))
717 token = parse_asm_operands(token, stmt);
718 if (match_op(token, ':'))
719 token = parse_asm_clobbers(token, stmt);
720 token = expect(token, ')', "after asm");
721 return expect(token, ';', "at end of asm-statement");
724 /* Make a statement out of an expression */
725 static struct statement *make_statement(struct expression *expr)
727 struct statement *stmt;
729 if (!expr)
730 return NULL;
731 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
732 stmt->expression = expr;
733 return stmt;
737 * All iterators have two symbols associated with them:
738 * the "continue" and "break" symbols, which are targets
739 * for continue and break statements respectively.
741 * They are in a special name-space, but they follow
742 * all the normal visibility rules, so nested iterators
743 * automatically work right.
745 static void start_iterator(struct statement *stmt)
747 struct symbol *cont, *brk;
749 start_symbol_scope();
750 cont = alloc_symbol(stmt->pos, SYM_NODE);
751 cont->ident = &continue_ident;
752 bind_symbol(cont, &continue_ident, NS_ITERATOR);
753 brk = alloc_symbol(stmt->pos, SYM_NODE);
754 brk->ident = &break_ident;
755 bind_symbol(brk, &break_ident, NS_ITERATOR);
757 stmt->type = STMT_ITERATOR;
758 stmt->iterator_break = brk;
759 stmt->iterator_continue = cont;
760 fn_local_symbol(brk);
761 fn_local_symbol(cont);
764 static void end_iterator(struct statement *stmt)
766 end_symbol_scope();
769 static struct statement *start_function(struct symbol *sym)
771 struct symbol *ret;
772 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
774 start_function_scope();
775 ret = alloc_symbol(sym->pos, SYM_NODE);
776 ret->ident = &return_ident;
777 ret->ctype = sym->ctype.base_type->ctype;
778 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
779 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
780 bind_symbol(ret, &return_ident, NS_ITERATOR);
781 stmt->ret = ret;
783 fn_local_symbol(ret);
784 return stmt;
787 static void end_function(struct symbol *sym)
789 end_function_scope();
793 * A "switch()" statement, like an iterator, has a
794 * the "break" symbol associated with it. It works
795 * exactly like the iterator break - it's the target
796 * for any break-statements in scope, and means that
797 * "break" handling doesn't even need to know whether
798 * it's breaking out of an iterator or a switch.
800 * In addition, the "case" symbol is a marker for the
801 * case/default statements to find the switch statement
802 * that they are associated with.
804 static void start_switch(struct statement *stmt)
806 struct symbol *brk, *switch_case;
808 start_symbol_scope();
809 brk = alloc_symbol(stmt->pos, SYM_NODE);
810 brk->ident = &break_ident;
811 bind_symbol(brk, &break_ident, NS_ITERATOR);
813 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
814 switch_case->ident = &case_ident;
815 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
816 switch_case->stmt = stmt;
818 stmt->type = STMT_SWITCH;
819 stmt->switch_break = brk;
820 stmt->switch_case = switch_case;
822 fn_local_symbol(brk);
823 fn_local_symbol(switch_case);
826 static void end_switch(struct statement *stmt)
828 if (!stmt->switch_case->symbol_list)
829 warn(stmt->pos, "switch with no cases");
830 end_symbol_scope();
833 static void add_case_statement(struct statement *stmt)
835 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
836 struct symbol *sym;
838 if (!target) {
839 warn(stmt->pos, "not in switch scope");
840 return;
842 sym = alloc_symbol(stmt->pos, SYM_NODE);
843 add_symbol(&target->symbol_list, sym);
844 sym->stmt = stmt;
845 stmt->case_label = sym;
846 fn_local_symbol(sym);
849 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
851 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
853 if (!target)
854 error(token->pos, "internal error: return without a function target");
855 stmt->type = STMT_RETURN;
856 stmt->ret_target = target;
857 return expression_statement(token->next, &stmt->ret_value);
860 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
862 struct symbol_list *syms;
863 struct expression *e1, *e2, *e3;
864 struct statement *iterator;
866 start_iterator(stmt);
867 token = expect(token->next, '(', "after 'for'");
869 syms = NULL;
870 e1 = NULL;
871 /* C99 variable declaration? */
872 if (lookup_type(token)) {
873 token = external_declaration(token, &syms);
874 } else {
875 token = parse_expression(token, &e1);
876 token = expect(token, ';', "in 'for'");
878 token = parse_expression(token, &e2);
879 token = expect(token, ';', "in 'for'");
880 token = parse_expression(token, &e3);
881 token = expect(token, ')', "in 'for'");
882 token = statement(token, &iterator);
884 stmt->iterator_syms = syms;
885 stmt->iterator_pre_statement = make_statement(e1);
886 stmt->iterator_pre_condition = e2;
887 stmt->iterator_post_statement = make_statement(e3);
888 stmt->iterator_post_condition = e2;
889 stmt->iterator_statement = iterator;
890 end_iterator(stmt);
892 return token;
895 struct token *parse_while_statement(struct token *token, struct statement *stmt)
897 struct expression *expr;
898 struct statement *iterator;
900 start_iterator(stmt);
901 token = parens_expression(token->next, &expr, "after 'while'");
902 token = statement(token, &iterator);
904 stmt->iterator_pre_condition = expr;
905 stmt->iterator_post_condition = expr;
906 stmt->iterator_statement = iterator;
907 end_iterator(stmt);
909 return token;
912 struct token *parse_do_statement(struct token *token, struct statement *stmt)
914 struct expression *expr;
915 struct statement *iterator;
917 start_iterator(stmt);
918 token = statement(token->next, &iterator);
919 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
920 token = token->next;
921 else
922 warn(token->pos, "expected 'while' after 'do'");
923 token = parens_expression(token, &expr, "after 'do-while'");
925 stmt->iterator_post_condition = expr;
926 stmt->iterator_statement = iterator;
927 end_iterator(stmt);
929 return expect(token, ';', "after statement");
932 static struct token *statement(struct token *token, struct statement **tree)
934 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
936 *tree = stmt;
937 if (token_type(token) == TOKEN_IDENT) {
938 if (token->ident == &if_ident) {
939 stmt->type = STMT_IF;
940 token = parens_expression(token->next, &stmt->if_conditional, "after if");
941 token = statement(token, &stmt->if_true);
942 if (token_type(token) != TOKEN_IDENT)
943 return token;
944 if (token->ident != &else_ident)
945 return token;
946 return statement(token->next, &stmt->if_false);
949 if (token->ident == &return_ident)
950 return parse_return_statement(token, stmt);
952 if (token->ident == &break_ident || token->ident == &continue_ident) {
953 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
954 stmt->type = STMT_GOTO;
955 stmt->goto_label = target;
956 if (!target)
957 warn(stmt->pos, "break/continue not in iterator scope");
958 return expect(token->next, ';', "at end of statement");
960 if (token->ident == &default_ident) {
961 token = token->next;
962 goto default_statement;
964 if (token->ident == &case_ident) {
965 token = parse_expression(token->next, &stmt->case_expression);
966 if (match_op(token, SPECIAL_ELLIPSIS))
967 token = parse_expression(token->next, &stmt->case_to);
968 default_statement:
969 stmt->type = STMT_CASE;
970 token = expect(token, ':', "after default/case");
971 add_case_statement(stmt);
972 return statement(token, &stmt->case_statement);
974 if (token->ident == &switch_ident) {
975 stmt->type = STMT_SWITCH;
976 start_switch(stmt);
977 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
978 token = statement(token, &stmt->switch_statement);
979 end_switch(stmt);
980 return token;
982 if (token->ident == &for_ident)
983 return parse_for_statement(token, stmt);
985 if (token->ident == &while_ident)
986 return parse_while_statement(token, stmt);
988 if (token->ident == &do_ident)
989 return parse_do_statement(token, stmt);
991 if (token->ident == &goto_ident) {
992 stmt->type = STMT_GOTO;
993 token = token->next;
994 if (match_op(token, '*')) {
995 token = parse_expression(token->next, &stmt->goto_expression);
996 } else if (token_type(token) == TOKEN_IDENT) {
997 stmt->goto_label = label_symbol(token);
998 token = token->next;
999 } else {
1000 warn(token->pos, "Expected identifier or goto expression");
1002 return expect(token, ';', "at end of statement");
1004 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1005 return parse_asm(token->next, stmt);
1007 if (match_op(token->next, ':')) {
1008 stmt->type = STMT_LABEL;
1009 stmt->label_identifier = label_symbol(token);
1010 return statement(token->next->next, &stmt->label_statement);
1014 if (match_op(token, '{')) {
1015 stmt->type = STMT_COMPOUND;
1016 start_symbol_scope();
1017 token = compound_statement(token->next, stmt);
1018 end_symbol_scope();
1020 return expect(token, '}', "at end of compound statement");
1023 stmt->type = STMT_EXPRESSION;
1024 return expression_statement(token, &stmt->expression);
1027 struct token * statement_list(struct token *token, struct statement_list **list)
1029 for (;;) {
1030 struct statement * stmt;
1031 if (eof_token(token))
1032 break;
1033 if (match_op(token, '}'))
1034 break;
1035 token = statement(token, &stmt);
1036 add_statement(list, stmt);
1038 return token;
1041 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1043 struct symbol_list **list = &fn->arguments;
1044 for (;;) {
1045 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1047 if (match_op(token, SPECIAL_ELLIPSIS)) {
1048 fn->variadic = 1;
1049 token = token->next;
1050 break;
1053 if (!lookup_type(token)) {
1054 warn(token->pos, "non-ANSI parameter list");
1055 break;
1057 token = parameter_declaration(token, &sym);
1058 /* Special case: (void) */
1059 if (!*list && !sym->ident && sym->ctype.base_type == &void_ctype)
1060 break;
1061 add_symbol(list, sym);
1062 if (!match_op(token, ','))
1063 break;
1064 token = token->next;
1067 return token;
1070 struct token *compound_statement(struct token *token, struct statement *stmt)
1072 while (!eof_token(token)) {
1073 if (!lookup_type(token))
1074 break;
1075 token = external_declaration(token, &stmt->syms);
1077 token = statement_list(token, &stmt->stmts);
1078 return token;
1081 static struct expression *identifier_expression(struct token *token)
1083 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1084 expr->expr_ident = token->ident;
1085 return expr;
1088 static struct expression *index_expression(struct expression *from, struct expression *to)
1090 int idx_from, idx_to;
1091 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1093 idx_from = get_expression_value(from);
1094 idx_to = idx_from;
1095 if (to) {
1096 idx_to = get_expression_value(to);
1097 if (idx_to < idx_from || idx_from < 0)
1098 warn(from->pos, "nonsense array initializer index range");
1100 expr->idx_from = idx_from;
1101 expr->idx_to = idx_to;
1102 return expr;
1105 static struct token *initializer_list(struct expression_list **list, struct token *token)
1107 for (;;) {
1108 struct token *next = token->next;
1109 struct expression *expr;
1111 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
1112 add_expression(list, identifier_expression(next));
1113 token = next->next->next;
1114 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1115 add_expression(list, identifier_expression(token));
1116 token = next->next;
1117 } else if (match_op(token, '[')) {
1118 struct expression *from = NULL, *to = NULL;
1119 token = constant_expression(token->next, &from);
1120 if (match_op(token, SPECIAL_ELLIPSIS))
1121 token = constant_expression(token->next, &to);
1122 add_expression(list, index_expression(from, to));
1123 token = expect(token, ']', "at end of initializer index");
1124 token = expect(token, '=', "at end of initializer index");
1127 expr = NULL;
1128 token = initializer(&expr, token);
1129 if (!expr)
1130 break;
1131 add_expression(list, expr);
1132 if (!match_op(token, ','))
1133 break;
1134 token = token->next;
1136 return token;
1139 struct token *initializer(struct expression **tree, struct token *token)
1141 if (match_op(token, '{')) {
1142 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1143 *tree = expr;
1144 token = initializer_list(&expr->expr_list, token->next);
1145 return expect(token, '}', "at end of initializer");
1147 return assignment_expression(token, tree);
1150 static void declare_argument(struct symbol *sym, struct symbol *fn)
1152 if (!sym->ident) {
1153 warn(sym->pos, "no identifier for function argument");
1154 return;
1156 bind_symbol(sym, sym->ident, NS_SYMBOL);
1159 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1160 struct symbol_list **list)
1162 struct symbol *base_type = decl->ctype.base_type;
1163 struct statement *stmt;
1164 struct symbol *arg;
1166 function_symbol_list = &decl->symbol_list;
1167 if (decl->ctype.modifiers & MOD_EXTERN) {
1168 if (!(decl->ctype.modifiers & MOD_INLINE))
1169 warn(decl->pos, "function with external linkage has definition");
1171 if (!(decl->ctype.modifiers & MOD_STATIC))
1172 decl->ctype.modifiers |= MOD_EXTERN;
1174 stmt = start_function(decl);
1176 base_type->stmt = stmt;
1177 FOR_EACH_PTR (base_type->arguments, arg) {
1178 declare_argument(arg, base_type);
1179 } END_FOR_EACH_PTR;
1181 token = compound_statement(token->next, stmt);
1183 end_function(decl);
1184 if (!(decl->ctype.modifiers & MOD_INLINE))
1185 add_symbol(list, decl);
1186 check_declaration(decl);
1187 function_symbol_list = NULL;
1188 return expect(token, '}', "at end of function");
1191 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1193 struct ident *ident = NULL;
1194 struct symbol *decl;
1195 struct ctype ctype = { 0, };
1196 struct symbol *base_type;
1197 int is_typedef;
1199 /* Top-level inline asm? */
1200 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident)) {
1201 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1202 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1203 struct statement *stmt;
1205 anon->ctype.base_type = fn;
1206 function_symbol_list = &anon->symbol_list;
1207 stmt = start_function(anon);
1208 token = parse_asm(token->next, stmt);
1209 end_function(anon);
1210 function_symbol_list = NULL;
1211 add_symbol(list, anon);
1212 return token;
1215 /* Parse declaration-specifiers, if any */
1216 token = declaration_specifiers(token, &ctype, 0);
1217 decl = alloc_symbol(token->pos, SYM_NODE);
1218 decl->ctype = ctype;
1219 token = declarator(token, &decl, &ident);
1221 /* Just a type declaration? */
1222 if (!ident)
1223 return expect(token, ';', "end of type declaration");
1225 decl->ident = ident;
1227 /* type define declaration? */
1228 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1230 /* Typedef's don't have meaningful storage */
1231 if (is_typedef) {
1232 ctype.modifiers &= ~MOD_STORAGE;
1233 decl->ctype.modifiers &= ~MOD_STORAGE;
1234 decl->ctype.modifiers |= MOD_USERTYPE;
1237 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1239 base_type = decl->ctype.base_type;
1240 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1241 if (match_op(token, '{'))
1242 return parse_function_body(token, decl, list);
1244 if (!(decl->ctype.modifiers & MOD_STATIC))
1245 decl->ctype.modifiers |= MOD_EXTERN;
1248 for (;;) {
1249 if (token_type(token) == TOKEN_IDENT) {
1250 if (token->ident == &asm_ident || token->ident == &__asm_ident || token->ident == &__asm___ident) {
1251 struct expression *expr;
1253 token = expect(token->next, '(', "after asm");
1254 token = parse_expression(token->next, &expr);
1255 token = expect(token, ')', "after asm");
1258 if (!is_typedef && match_op(token, '=')) {
1259 if (decl->ctype.modifiers & MOD_EXTERN) {
1260 warn(decl->pos, "symbol with external linkage has initializer");
1261 decl->ctype.modifiers &= ~MOD_EXTERN;
1263 token = initializer(&decl->initializer, token->next);
1265 if (!is_typedef) {
1266 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1267 add_symbol(list, decl);
1268 if (function_symbol_list)
1269 fn_local_symbol(decl);
1272 check_declaration(decl);
1274 if (!match_op(token, ','))
1275 break;
1277 token = token->next;
1278 ident = NULL;
1279 decl = alloc_symbol(token->pos, SYM_NODE);
1280 decl->ctype = ctype;
1281 token = declaration_specifiers(token, &decl->ctype, 1);
1282 token = declarator(token, &decl, &ident);
1283 if (!ident) {
1284 warn(token->pos, "expected identifier name in type definition");
1285 return token;
1288 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1290 /* Function declarations are automatically extern unless specifically static */
1291 base_type = decl->ctype.base_type;
1292 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1293 if (!(decl->ctype.modifiers & MOD_STATIC))
1294 decl->ctype.modifiers |= MOD_EXTERN;
1297 return expect(token, ';', "at end of declaration");
1300 void translation_unit(struct token *token, struct symbol_list **list)
1302 while (!eof_token(token))
1303 token = external_declaration(token, list);
1304 // They aren't needed any more
1305 clear_token_alloc();