parse.c:
[smatch.git] / parse.c
bloba12a7046188294bc3c0bed5612b4634f9d92abd2
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;
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 return NULL;
306 if (match_string_ident(attribute, "regparm"))
307 return NULL;
308 if (match_string_ident(attribute, "weak"))
309 return NULL;
310 if (match_string_ident(attribute, "alias"))
311 return NULL;
312 if (match_string_ident(attribute, "pure"))
313 return NULL;
314 if (match_string_ident(attribute, "always_inline"))
315 return NULL;
316 if (match_string_ident(attribute, "syscall_linkage"))
317 return NULL;
318 if (match_string_ident(attribute, "visibility"))
319 return NULL;
321 return "unknown attribute";
324 struct token *attribute_specifier(struct token *token, struct ctype *ctype)
326 ctype->modifiers = 0;
327 token = expect(token, '(', "after attribute");
328 token = expect(token, '(', "after attribute");
330 for (;;) {
331 const char *error;
332 struct ident *attribute_name;
333 struct expression *attribute_expr;
335 if (eof_token(token))
336 break;
337 if (match_op(token, ';'))
338 break;
339 if (token_type(token) != TOKEN_IDENT)
340 break;
341 attribute_name = token->ident;
342 token = token->next;
343 attribute_expr = NULL;
344 if (match_op(token, '('))
345 token = parens_expression(token, &attribute_expr, "in attribute");
346 error = handle_attribute(ctype, attribute_name, attribute_expr);
347 if (error)
348 warn(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
349 if (!match_op(token, ','))
350 break;
351 token = token->next;
354 token = expect(token, ')', "after attribute");
355 token = expect(token, ')', "after attribute");
356 return token;
359 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
360 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
362 struct symbol * ctype_integer(unsigned int spec)
364 static struct symbol *const integer_ctypes[][2] = {
365 { &llong_ctype, &ullong_ctype },
366 { &long_ctype, &ulong_ctype },
367 { &short_ctype, &ushort_ctype },
368 { &char_ctype, &uchar_ctype },
369 { &int_ctype, &uint_ctype },
371 struct symbol *const (*ctype)[2];
373 ctype = integer_ctypes;
374 if (!(spec & MOD_LONGLONG)) {
375 ctype++;
376 if (!(spec & MOD_LONG)) {
377 ctype++;
378 if (!(spec & MOD_SHORT)) {
379 ctype++;
380 if (!(spec & MOD_CHAR))
381 ctype++;
385 return ctype[0][(spec & MOD_UNSIGNED) != 0];
388 struct symbol * ctype_fp(unsigned int spec)
390 if (spec & MOD_LONGLONG)
391 return &ldouble_ctype;
392 if (spec & MOD_LONG)
393 return &double_ctype;
394 return &float_ctype;
397 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
399 unsigned long mod = thistype->modifiers;
401 if (mod) {
402 unsigned long old = ctype->modifiers;
403 unsigned long extra = 0, dup, conflict;
405 if (mod & old & MOD_LONG) {
406 extra = MOD_LONGLONG | MOD_LONG;
407 mod &= ~MOD_LONG;
408 old &= ~MOD_LONG;
410 dup = (mod & old) | (extra & old) | (extra & mod);
411 if (dup)
412 warn(pos, "Just how %sdo you want this type to be?",
413 modifier_string(dup));
415 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
416 if (conflict)
417 warn(pos, "You cannot have both long and short modifiers.");
419 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
420 if (conflict)
421 warn(pos, "You cannot have both signed and unsigned modifiers.");
423 // Only one storage modifier allowed, except that "inline" doesn't count.
424 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
425 conflict &= (conflict - 1);
426 if (conflict)
427 warn(pos, "multiple storage classes");
429 ctype->modifiers = old | mod | extra;
432 /* Context mask and value */
433 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
434 warn(pos, "inconsistent attribute types");
435 thistype->context = 0;
436 thistype->contextmask = 0;
438 ctype->context |= thistype->context;
439 ctype->contextmask |= thistype->contextmask;
441 /* Alignment */
442 if (thistype->alignment & (thistype->alignment-1)) {
443 warn(pos, "I don't like non-power-of-2 alignments");
444 thistype->alignment = 0;
446 if (thistype->alignment > ctype->alignment)
447 ctype->alignment = thistype->alignment;
449 /* Address space */
450 ctype->as = thistype->as;
454 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
456 struct token *token;
458 while ( (token = next) != NULL ) {
459 struct ctype thistype;
460 struct ident *ident;
461 struct symbol *s, *type;
462 unsigned long mod;
464 next = token->next;
465 if (token_type(token) != TOKEN_IDENT)
466 break;
467 ident = token->ident;
469 s = lookup_symbol(ident, NS_TYPEDEF);
470 if (!s)
471 break;
472 thistype = s->ctype;
473 mod = thistype.modifiers;
474 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
475 break;
476 if (mod & MOD_SPECIALBITS) {
477 if (mod & MOD_STRUCTOF)
478 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
479 else if (mod & MOD_UNIONOF)
480 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
481 else if (mod & MOD_ENUMOF)
482 next = enum_specifier(next, &thistype);
483 else if (mod & MOD_ATTRIBUTE)
484 next = attribute_specifier(next, &thistype);
485 else if (mod & MOD_TYPEOF)
486 next = typeof_specifier(next, &thistype);
487 mod = thistype.modifiers;
489 type = thistype.base_type;
490 if (type) {
491 if (qual)
492 break;
493 if (ctype->base_type)
494 break;
495 /* User types only mix with qualifiers */
496 if (mod & MOD_USERTYPE) {
497 if (ctype->modifiers & MOD_SPECIFIER)
498 break;
500 ctype->base_type = type;
503 apply_ctype(token->pos, &thistype, ctype);
506 /* Turn the "virtual types" into real types with real sizes etc */
507 if (!ctype->base_type) {
508 struct symbol *base = &incomplete_ctype;
511 * If we have modifiers, we'll default to an integer
512 * type, and "ctype_integer()" will turn this into
513 * a specific one.
515 if (ctype->modifiers & MOD_SPECIFIER)
516 base = &int_type;
517 ctype->base_type = base;
520 if (ctype->base_type == &int_type) {
521 ctype->base_type = ctype_integer(ctype->modifiers & MOD_SPECIFIER);
522 ctype->modifiers &= ~MOD_SPECIFIER;
523 return token;
525 if (ctype->base_type == &fp_type) {
526 ctype->base_type = ctype_fp(ctype->modifiers & MOD_SPECIFIER);
527 ctype->modifiers &= ~MOD_SPECIFIER;
528 return token;
530 return token;
533 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
535 struct expression *expr = NULL;
537 token = parse_expression(token, &expr);
538 sym->array_size = expr;
539 return token;
542 static struct token *parameter_type_list(struct token *, struct symbol *);
543 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
545 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
547 struct ctype *ctype = &(*tree)->ctype;
549 if (p && token_type(token) == TOKEN_IDENT) {
550 *p = token->ident;
551 token = token->next;
554 for (;;) {
555 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
556 struct ctype thistype = { 0, };
557 token = attribute_specifier(token->next, &thistype);
558 apply_ctype(token->pos, &thistype, ctype);
559 continue;
561 if (token_type(token) != TOKEN_SPECIAL)
562 return token;
565 * This can be either a parameter list or a grouping.
566 * For the direct (non-abstract) case, we know if must be
567 * a paramter list if we already saw the identifier.
568 * For the abstract case, we know if must be a parameter
569 * list if it is empty or starts with a type.
571 if (token->special == '(') {
572 struct symbol *sym;
573 struct token *next = token->next;
574 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
576 if (!fn) {
577 struct symbol *base_type = ctype->base_type;
578 token = declarator(next, tree, p);
579 token = expect(token, ')', "in nested declarator");
580 while (ctype->base_type != base_type)
581 ctype = &ctype->base_type->ctype;
582 p = NULL;
583 continue;
586 sym = indirect(token->pos, ctype, SYM_FN);
587 token = parameter_type_list(next, sym);
588 token = expect(token, ')', "in function declarator");
589 continue;
591 if (token->special == '[') {
592 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
593 token = abstract_array_declarator(token->next, array);
594 token = expect(token, ']', "in abstract_array_declarator");
595 ctype = &array->ctype;
596 continue;
598 if (token->special == ':') {
599 if (is_int_type (ctype->base_type)) {
600 struct symbol *bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
601 struct expression *expr;
602 token = conditional_expression(token->next, &expr);
603 bitfield->fieldwidth = get_expression_value(expr);
604 } else
605 error(token->pos, "Invalid bitfield specifier for type %s.", show_typename (ctype->base_type));
606 break;
608 break;
610 if (p) {
611 (*tree)->ident = *p;
613 return token;
616 static struct token *pointer(struct token *token, struct ctype *ctype)
618 unsigned long modifiers;
619 struct symbol *base_type;
621 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
622 base_type = ctype->base_type;
623 ctype->modifiers = modifiers;
625 while (match_op(token,'*')) {
626 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
627 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
628 ptr->ctype.as = ctype->as;
629 ptr->ctype.context = ctype->context;
630 ptr->ctype.contextmask = ctype->contextmask;
631 ptr->ctype.base_type = base_type;
633 base_type = ptr;
634 ctype->modifiers = modifiers & MOD_STORAGE;
635 ctype->base_type = base_type;
636 ctype->as = 0;
637 ctype->context = 0;
638 ctype->contextmask = 0;
640 token = declaration_specifiers(token->next, ctype, 1);
641 modifiers = ctype->modifiers;
643 return token;
646 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
648 token = pointer(token, &(*tree)->ctype);
649 return direct_declarator(token, tree, p);
652 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
654 while (!match_op(token, '}')) {
655 struct ctype ctype = {0, };
657 token = declaration_specifiers(token, &ctype, 0);
658 for (;;) {
659 struct ident *ident = NULL;
660 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
661 decl->ctype = ctype;
662 token = declarator(token, &decl, &ident);
663 if (match_op(token, ':')) {
664 struct expression *expr;
665 token = parse_expression(token->next, &expr);
667 add_symbol(list, decl);
668 if (!match_op(token, ','))
669 break;
670 token = token->next;
672 if (!match_op(token, ';')) {
673 warn(token->pos, "expected ; at end of declaration");
674 break;
676 token = token->next;
678 return token;
681 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
683 struct ident *ident = NULL;
684 struct symbol *sym;
685 struct ctype ctype = { 0, };
687 token = declaration_specifiers(token, &ctype, 0);
688 sym = alloc_symbol(token->pos, SYM_NODE);
689 sym->ctype = ctype;
690 *tree = sym;
691 token = declarator(token, tree, &ident);
692 return token;
695 struct token *typename(struct token *token, struct symbol **p)
697 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
698 *p = sym;
699 token = declaration_specifiers(token, &sym->ctype, 0);
700 return declarator(token, &sym, NULL);
703 struct token *expression_statement(struct token *token, struct expression **tree)
705 token = parse_expression(token, tree);
706 return expect(token, ';', "at end of statement");
709 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
711 struct expression *expr;
713 /* Allow empty operands */
714 if (match_op(token->next, ':') || match_op(token->next, ')'))
715 return token->next;
716 do {
717 if (match_op(token->next, '[') &&
718 token_type(token->next->next) == TOKEN_IDENT &&
719 match_op(token->next->next->next, ']'))
720 token = token->next->next->next;
721 token = primary_expression(token->next, &expr);
722 token = parens_expression(token, &expr, "in asm parameter");
723 } while (match_op(token, ','));
724 return token;
727 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
729 struct expression *expr;
731 do {
732 token = primary_expression(token->next, &expr);
733 } while (match_op(token, ','));
734 return token;
737 static struct token *parse_asm(struct token *token, struct statement *stmt)
739 struct expression *expr;
741 stmt->type = STMT_ASM;
742 if (match_idents(token, &__volatile___ident, &volatile_ident)) {
743 token = token->next;
745 token = expect(token, '(', "after asm");
746 token = parse_expression(token->next, &expr);
747 if (match_op(token, ':'))
748 token = parse_asm_operands(token, stmt);
749 if (match_op(token, ':'))
750 token = parse_asm_operands(token, stmt);
751 if (match_op(token, ':'))
752 token = parse_asm_clobbers(token, stmt);
753 token = expect(token, ')', "after asm");
754 return expect(token, ';', "at end of asm-statement");
757 /* Make a statement out of an expression */
758 static struct statement *make_statement(struct expression *expr)
760 struct statement *stmt;
762 if (!expr)
763 return NULL;
764 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
765 stmt->expression = expr;
766 return stmt;
770 * All iterators have two symbols associated with them:
771 * the "continue" and "break" symbols, which are targets
772 * for continue and break statements respectively.
774 * They are in a special name-space, but they follow
775 * all the normal visibility rules, so nested iterators
776 * automatically work right.
778 static void start_iterator(struct statement *stmt)
780 struct symbol *cont, *brk;
782 start_symbol_scope();
783 cont = alloc_symbol(stmt->pos, SYM_NODE);
784 cont->ident = &continue_ident;
785 bind_symbol(cont, &continue_ident, NS_ITERATOR);
786 brk = alloc_symbol(stmt->pos, SYM_NODE);
787 brk->ident = &break_ident;
788 bind_symbol(brk, &break_ident, NS_ITERATOR);
790 stmt->type = STMT_ITERATOR;
791 stmt->iterator_break = brk;
792 stmt->iterator_continue = cont;
793 fn_local_symbol(brk);
794 fn_local_symbol(cont);
797 static void end_iterator(struct statement *stmt)
799 end_symbol_scope();
802 static struct statement *start_function(struct symbol *sym)
804 struct symbol *ret;
805 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
807 start_function_scope();
808 ret = alloc_symbol(sym->pos, SYM_NODE);
809 ret->ident = &return_ident;
810 ret->ctype = sym->ctype.base_type->ctype;
811 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
812 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
813 bind_symbol(ret, &return_ident, NS_ITERATOR);
814 stmt->ret = ret;
816 fn_local_symbol(ret);
817 return stmt;
820 static void end_function(struct symbol *sym)
822 end_function_scope();
826 * A "switch()" statement, like an iterator, has a
827 * the "break" symbol associated with it. It works
828 * exactly like the iterator break - it's the target
829 * for any break-statements in scope, and means that
830 * "break" handling doesn't even need to know whether
831 * it's breaking out of an iterator or a switch.
833 * In addition, the "case" symbol is a marker for the
834 * case/default statements to find the switch statement
835 * that they are associated with.
837 static void start_switch(struct statement *stmt)
839 struct symbol *brk, *switch_case;
841 start_symbol_scope();
842 brk = alloc_symbol(stmt->pos, SYM_NODE);
843 brk->ident = &break_ident;
844 bind_symbol(brk, &break_ident, NS_ITERATOR);
846 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
847 switch_case->ident = &case_ident;
848 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
849 switch_case->stmt = stmt;
851 stmt->type = STMT_SWITCH;
852 stmt->switch_break = brk;
853 stmt->switch_case = switch_case;
855 fn_local_symbol(brk);
856 fn_local_symbol(switch_case);
859 static void end_switch(struct statement *stmt)
861 if (!stmt->switch_case->symbol_list)
862 warn(stmt->pos, "switch with no cases");
863 end_symbol_scope();
866 static void add_case_statement(struct statement *stmt)
868 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
869 struct symbol *sym;
871 if (!target) {
872 warn(stmt->pos, "not in switch scope");
873 return;
875 sym = alloc_symbol(stmt->pos, SYM_NODE);
876 add_symbol(&target->symbol_list, sym);
877 sym->stmt = stmt;
878 stmt->case_label = sym;
879 fn_local_symbol(sym);
882 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
884 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
886 if (!target)
887 error(token->pos, "internal error: return without a function target");
888 stmt->type = STMT_RETURN;
889 stmt->ret_target = target;
890 return expression_statement(token->next, &stmt->ret_value);
893 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
895 struct symbol_list *syms;
896 struct expression *e1, *e2, *e3;
897 struct statement *iterator;
899 start_iterator(stmt);
900 token = expect(token->next, '(', "after 'for'");
902 syms = NULL;
903 e1 = NULL;
904 /* C99 variable declaration? */
905 if (lookup_type(token)) {
906 token = external_declaration(token, &syms);
907 } else {
908 token = parse_expression(token, &e1);
909 token = expect(token, ';', "in 'for'");
911 token = parse_expression(token, &e2);
912 token = expect(token, ';', "in 'for'");
913 token = parse_expression(token, &e3);
914 token = expect(token, ')', "in 'for'");
915 token = statement(token, &iterator);
917 stmt->iterator_syms = syms;
918 stmt->iterator_pre_statement = make_statement(e1);
919 stmt->iterator_pre_condition = e2;
920 stmt->iterator_post_statement = make_statement(e3);
921 stmt->iterator_post_condition = e2;
922 stmt->iterator_statement = iterator;
923 end_iterator(stmt);
925 return token;
928 struct token *parse_while_statement(struct token *token, struct statement *stmt)
930 struct expression *expr;
931 struct statement *iterator;
933 start_iterator(stmt);
934 token = parens_expression(token->next, &expr, "after 'while'");
935 token = statement(token, &iterator);
937 stmt->iterator_pre_condition = expr;
938 stmt->iterator_post_condition = expr;
939 stmt->iterator_statement = iterator;
940 end_iterator(stmt);
942 return token;
945 struct token *parse_do_statement(struct token *token, struct statement *stmt)
947 struct expression *expr;
948 struct statement *iterator;
950 start_iterator(stmt);
951 token = statement(token->next, &iterator);
952 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
953 token = token->next;
954 else
955 warn(token->pos, "expected 'while' after 'do'");
956 token = parens_expression(token, &expr, "after 'do-while'");
958 stmt->iterator_post_condition = expr;
959 stmt->iterator_statement = iterator;
960 end_iterator(stmt);
962 return expect(token, ';', "after statement");
965 static struct token *statement(struct token *token, struct statement **tree)
967 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
969 *tree = stmt;
970 if (token_type(token) == TOKEN_IDENT) {
971 if (token->ident == &if_ident) {
972 stmt->type = STMT_IF;
973 token = parens_expression(token->next, &stmt->if_conditional, "after if");
974 token = statement(token, &stmt->if_true);
975 if (token_type(token) != TOKEN_IDENT)
976 return token;
977 if (token->ident != &else_ident)
978 return token;
979 return statement(token->next, &stmt->if_false);
982 if (token->ident == &return_ident)
983 return parse_return_statement(token, stmt);
985 if (token->ident == &break_ident || token->ident == &continue_ident) {
986 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
987 stmt->type = STMT_GOTO;
988 stmt->goto_label = target;
989 if (!target)
990 warn(stmt->pos, "break/continue not in iterator scope");
991 return expect(token->next, ';', "at end of statement");
993 if (token->ident == &default_ident) {
994 token = token->next;
995 goto default_statement;
997 if (token->ident == &case_ident) {
998 token = parse_expression(token->next, &stmt->case_expression);
999 if (match_op(token, SPECIAL_ELLIPSIS))
1000 token = parse_expression(token->next, &stmt->case_to);
1001 default_statement:
1002 stmt->type = STMT_CASE;
1003 token = expect(token, ':', "after default/case");
1004 add_case_statement(stmt);
1005 return statement(token, &stmt->case_statement);
1007 if (token->ident == &switch_ident) {
1008 stmt->type = STMT_SWITCH;
1009 start_switch(stmt);
1010 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1011 token = statement(token, &stmt->switch_statement);
1012 end_switch(stmt);
1013 return token;
1015 if (token->ident == &for_ident)
1016 return parse_for_statement(token, stmt);
1018 if (token->ident == &while_ident)
1019 return parse_while_statement(token, stmt);
1021 if (token->ident == &do_ident)
1022 return parse_do_statement(token, stmt);
1024 if (token->ident == &goto_ident) {
1025 stmt->type = STMT_GOTO;
1026 token = token->next;
1027 if (match_op(token, '*')) {
1028 token = parse_expression(token->next, &stmt->goto_expression);
1029 add_statement(&function_computed_goto_list, stmt);
1030 } else if (token_type(token) == TOKEN_IDENT) {
1031 stmt->goto_label = label_symbol(token);
1032 token = token->next;
1033 } else {
1034 warn(token->pos, "Expected identifier or goto expression");
1036 return expect(token, ';', "at end of statement");
1038 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1039 return parse_asm(token->next, stmt);
1041 if (match_op(token->next, ':')) {
1042 stmt->type = STMT_LABEL;
1043 stmt->label_identifier = label_symbol(token);
1044 return statement(token->next->next, &stmt->label_statement);
1048 if (match_op(token, '{')) {
1049 stmt->type = STMT_COMPOUND;
1050 start_symbol_scope();
1051 token = compound_statement(token->next, stmt);
1052 end_symbol_scope();
1054 return expect(token, '}', "at end of compound statement");
1057 stmt->type = STMT_EXPRESSION;
1058 return expression_statement(token, &stmt->expression);
1061 struct token * statement_list(struct token *token, struct statement_list **list)
1063 for (;;) {
1064 struct statement * stmt;
1065 if (eof_token(token))
1066 break;
1067 if (match_op(token, '}'))
1068 break;
1069 token = statement(token, &stmt);
1070 add_statement(list, stmt);
1072 return token;
1075 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1077 struct symbol_list **list = &fn->arguments;
1078 for (;;) {
1079 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1081 if (match_op(token, SPECIAL_ELLIPSIS)) {
1082 fn->variadic = 1;
1083 token = token->next;
1084 break;
1087 if (!lookup_type(token)) {
1088 warn(token->pos, "non-ANSI parameter list");
1089 break;
1091 token = parameter_declaration(token, &sym);
1092 /* Special case: (void) */
1093 if (!*list && !sym->ident && sym->ctype.base_type == &void_ctype)
1094 break;
1095 add_symbol(list, sym);
1096 if (!match_op(token, ','))
1097 break;
1098 token = token->next;
1101 return token;
1104 struct token *compound_statement(struct token *token, struct statement *stmt)
1106 while (!eof_token(token)) {
1107 if (!lookup_type(token))
1108 break;
1109 token = external_declaration(token, &stmt->syms);
1111 token = statement_list(token, &stmt->stmts);
1112 return token;
1115 static struct expression *identifier_expression(struct token *token)
1117 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1118 expr->expr_ident = token->ident;
1119 return expr;
1122 static struct expression *index_expression(struct expression *from, struct expression *to)
1124 int idx_from, idx_to;
1125 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1127 idx_from = get_expression_value(from);
1128 idx_to = idx_from;
1129 if (to) {
1130 idx_to = get_expression_value(to);
1131 if (idx_to < idx_from || idx_from < 0)
1132 warn(from->pos, "nonsense array initializer index range");
1134 expr->idx_from = idx_from;
1135 expr->idx_to = idx_to;
1136 return expr;
1139 static struct token *initializer_list(struct expression_list **list, struct token *token)
1141 for (;;) {
1142 struct token *next = token->next;
1143 struct expression *expr;
1145 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
1146 add_expression(list, identifier_expression(next));
1147 token = next->next->next;
1148 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1149 add_expression(list, identifier_expression(token));
1150 token = next->next;
1151 } else if (match_op(token, '[')) {
1152 struct expression *from = NULL, *to = NULL;
1153 token = constant_expression(token->next, &from);
1154 if (match_op(token, SPECIAL_ELLIPSIS))
1155 token = constant_expression(token->next, &to);
1156 add_expression(list, index_expression(from, to));
1157 token = expect(token, ']', "at end of initializer index");
1158 token = expect(token, '=', "at end of initializer index");
1161 expr = NULL;
1162 token = initializer(&expr, token);
1163 if (!expr)
1164 break;
1165 add_expression(list, expr);
1166 if (!match_op(token, ','))
1167 break;
1168 token = token->next;
1170 return token;
1173 struct token *initializer(struct expression **tree, struct token *token)
1175 if (match_op(token, '{')) {
1176 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1177 *tree = expr;
1178 token = initializer_list(&expr->expr_list, token->next);
1179 return expect(token, '}', "at end of initializer");
1181 return assignment_expression(token, tree);
1184 static void declare_argument(struct symbol *sym, struct symbol *fn)
1186 if (!sym->ident) {
1187 warn(sym->pos, "no identifier for function argument");
1188 return;
1190 bind_symbol(sym, sym->ident, NS_SYMBOL);
1193 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1194 struct symbol_list **list)
1196 struct symbol *base_type = decl->ctype.base_type;
1197 struct statement *stmt;
1198 struct symbol *arg;
1200 function_symbol_list = &decl->symbol_list;
1201 function_computed_target_list = NULL;
1202 function_computed_goto_list = NULL;
1204 if (decl->ctype.modifiers & MOD_EXTERN) {
1205 if (!(decl->ctype.modifiers & MOD_INLINE))
1206 warn(decl->pos, "function with external linkage has definition");
1208 if (!(decl->ctype.modifiers & MOD_STATIC))
1209 decl->ctype.modifiers |= MOD_EXTERN;
1211 stmt = start_function(decl);
1213 base_type->stmt = stmt;
1214 FOR_EACH_PTR (base_type->arguments, arg) {
1215 declare_argument(arg, base_type);
1216 } END_FOR_EACH_PTR;
1218 token = compound_statement(token->next, stmt);
1220 end_function(decl);
1221 if (!(decl->ctype.modifiers & MOD_INLINE))
1222 add_symbol(list, decl);
1223 check_declaration(decl);
1224 function_symbol_list = NULL;
1225 if (function_computed_goto_list) {
1226 if (!function_computed_target_list)
1227 warn(decl->pos, "function has computed goto but no targets?");
1228 else {
1229 struct statement *stmt;
1230 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1231 stmt->target_list = function_computed_target_list;
1232 } END_FOR_EACH_PTR;
1235 return expect(token, '}', "at end of function");
1238 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1240 struct ident *ident = NULL;
1241 struct symbol *decl;
1242 struct ctype ctype = { 0, };
1243 struct symbol *base_type;
1244 int is_typedef;
1246 /* Top-level inline asm? */
1247 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident)) {
1248 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1249 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1250 struct statement *stmt;
1252 anon->ctype.base_type = fn;
1253 function_symbol_list = &anon->symbol_list;
1254 stmt = start_function(anon);
1255 token = parse_asm(token->next, stmt);
1256 end_function(anon);
1257 function_symbol_list = NULL;
1258 add_symbol(list, anon);
1259 return token;
1262 /* Parse declaration-specifiers, if any */
1263 token = declaration_specifiers(token, &ctype, 0);
1264 decl = alloc_symbol(token->pos, SYM_NODE);
1265 decl->ctype = ctype;
1266 token = declarator(token, &decl, &ident);
1268 /* Just a type declaration? */
1269 if (!ident)
1270 return expect(token, ';', "end of type declaration");
1272 decl->ident = ident;
1274 /* type define declaration? */
1275 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1277 /* Typedef's don't have meaningful storage */
1278 if (is_typedef) {
1279 ctype.modifiers &= ~MOD_STORAGE;
1280 decl->ctype.modifiers &= ~MOD_STORAGE;
1281 decl->ctype.modifiers |= MOD_USERTYPE;
1284 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1286 base_type = decl->ctype.base_type;
1287 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1288 if (match_op(token, '{'))
1289 return parse_function_body(token, decl, list);
1291 if (!(decl->ctype.modifiers & MOD_STATIC))
1292 decl->ctype.modifiers |= MOD_EXTERN;
1295 for (;;) {
1296 if (token_type(token) == TOKEN_IDENT) {
1297 if (token->ident == &asm_ident || token->ident == &__asm_ident || token->ident == &__asm___ident) {
1298 struct expression *expr;
1300 token = expect(token->next, '(', "after asm");
1301 token = parse_expression(token->next, &expr);
1302 token = expect(token, ')', "after asm");
1305 if (!is_typedef && match_op(token, '=')) {
1306 if (decl->ctype.modifiers & MOD_EXTERN) {
1307 warn(decl->pos, "symbol with external linkage has initializer");
1308 decl->ctype.modifiers &= ~MOD_EXTERN;
1310 token = initializer(&decl->initializer, token->next);
1312 if (!is_typedef) {
1313 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1314 add_symbol(list, decl);
1315 if (function_symbol_list)
1316 fn_local_symbol(decl);
1319 check_declaration(decl);
1321 if (!match_op(token, ','))
1322 break;
1324 token = token->next;
1325 ident = NULL;
1326 decl = alloc_symbol(token->pos, SYM_NODE);
1327 decl->ctype = ctype;
1328 token = declaration_specifiers(token, &decl->ctype, 1);
1329 token = declarator(token, &decl, &ident);
1330 if (!ident) {
1331 warn(token->pos, "expected identifier name in type definition");
1332 return token;
1335 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1337 /* Function declarations are automatically extern unless specifically static */
1338 base_type = decl->ctype.base_type;
1339 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1340 if (!(decl->ctype.modifiers & MOD_STATIC))
1341 decl->ctype.modifiers |= MOD_EXTERN;
1344 return expect(token, ';', "at end of declaration");
1347 void translation_unit(struct token *token, struct symbol_list **list)
1349 while (!eof_token(token))
1350 token = external_declaration(token, list);
1351 // They aren't needed any more
1352 clear_token_alloc();