Fix up safe expression tests
[smatch.git] / parse.c
blob098808af4c97876068aa758a47cd0c554ba87f31
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 struct statement *alloc_statement(struct position pos, int type)
38 struct statement *stmt = __alloc_statement(0);
39 stmt->type = type;
40 stmt->pos = pos;
41 return stmt;
44 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
46 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
48 struct symbol *sym = alloc_symbol(pos, type);
50 sym->ctype.base_type = ctype->base_type;
51 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
53 ctype->base_type = sym;
54 ctype->modifiers &= MOD_STORAGE;
55 return sym;
58 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
60 struct symbol *sym = lookup_symbol(token->ident, ns);
61 if (!sym) {
62 sym = alloc_symbol(token->pos, type);
63 sym->ident = token->ident;
64 bind_symbol(sym, token->ident, ns);
65 if (type == SYM_LABEL)
66 fn_local_symbol(sym);
68 return sym;
72 * NOTE! NS_LABEL is not just a different namespace,
73 * it also ends up using function scope instead of the
74 * regular symbol scope.
76 struct symbol *label_symbol(struct token *token)
78 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
81 struct token *struct_union_enum_specifier(enum namespace ns, enum type type,
82 struct token *token, struct ctype *ctype,
83 struct token *(*parse)(struct token *, struct symbol *))
85 struct symbol *sym;
87 ctype->modifiers = 0;
88 if (token_type(token) == TOKEN_IDENT) {
89 sym = lookup_or_create_symbol(ns, type, token);
90 token = token->next;
91 ctype->base_type = sym;
92 if (match_op(token, '{')) {
93 token = parse(token->next, sym);
94 token = expect(token, '}', "at end of struct-union-enum-specifier");
96 return token;
99 // private struct/union/enum type
100 if (!match_op(token, '{')) {
101 warn(token->pos, "expected declaration");
102 ctype->base_type = &bad_type;
103 return token;
106 sym = alloc_symbol(token->pos, type);
107 token = parse(token->next, sym);
108 ctype->base_type = sym;
109 return expect(token, '}', "at end of specifier");
112 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
114 return struct_declaration_list(token, &sym->symbol_list);
117 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
119 return struct_union_enum_specifier(NS_STRUCT, type, token, ctype, parse_struct_declaration);
122 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
124 int nextval = 0;
125 while (token_type(token) == TOKEN_IDENT) {
126 struct token *next = token->next;
127 struct symbol *sym;
129 sym = alloc_symbol(token->pos, SYM_ENUM);
130 bind_symbol(sym, token->ident, NS_SYMBOL);
131 sym->ctype.base_type = parent;
133 if (match_op(next, '=')) {
134 struct expression *expr;
135 next = constant_expression(next->next, &expr);
136 nextval = get_expression_value(expr);
138 sym->value = nextval;
140 token = next;
141 if (!match_op(token, ','))
142 break;
143 token = token->next;
144 nextval = nextval + 1;
146 return token;
149 struct token *enum_specifier(struct token *token, struct ctype *ctype)
151 return struct_union_enum_specifier(NS_ENUM, SYM_ENUM, token, ctype, parse_enum_declaration);
154 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
156 struct symbol *sym;
158 if (!match_op(token, '(')) {
159 warn(token->pos, "expected '(' after typeof");
160 return token;
162 if (lookup_type(token->next)) {
163 token = typename(token->next, &sym);
164 *ctype = sym->ctype;
165 } else {
166 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
167 token = parse_expression(token->next, &typeof_sym->initializer);
169 ctype->modifiers = 0;
170 ctype->base_type = typeof_sym;
172 return expect(token, ')', "after typeof");
175 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
177 if (match_string_ident(attribute, "packed") ||
178 match_string_ident(attribute, "__packed__")) {
179 ctype->alignment = 1;
180 return NULL;
182 if (match_string_ident(attribute, "aligned") ||
183 match_string_ident(attribute, "__aligned__")) {
184 int alignment = MAX_ALIGNMENT;
185 if (expr)
186 alignment = get_expression_value(expr);
187 ctype->alignment = alignment;
188 return NULL;
190 if (match_string_ident(attribute, "nocast")) {
191 ctype->modifiers |= MOD_NOCAST;
192 return NULL;
194 if (match_string_ident(attribute, "noderef")) {
195 ctype->modifiers |= MOD_NODEREF;
196 return NULL;
198 if (match_string_ident(attribute, "safe")) {
199 ctype->modifiers |= MOD_SAFE;
200 return NULL;
202 if (match_string_ident(attribute, "address_space")) {
203 if (!expr)
204 return "expected address space number";
205 ctype->as = get_expression_value(expr);
206 return NULL;
208 if (match_string_ident(attribute, "context")) {
209 if (expr && expr->type == EXPR_COMMA) {
210 int mask = get_expression_value(expr->left);
211 int value = get_expression_value(expr->right);
212 if (value & ~mask)
213 return "nonsense attribute types";
214 ctype->contextmask |= mask;
215 ctype->context |= value;
216 return NULL;
218 return "expected context mask and value";
220 if (match_string_ident(attribute, "mode") ||
221 match_string_ident(attribute, "__mode__")) {
222 if (expr && expr->type == EXPR_SYMBOL) {
223 struct ident *ident = expr->symbol_name;
226 * Match against __HI__/__SI__/__DI__
228 * FIXME! This is broken - we don't actually get
229 * the type information updated properly at this
230 * stage for some reason.
232 if (match_string_ident(ident, "__HI__")) {
233 ctype->modifiers |= MOD_SHORT;
234 ctype->base_type = ctype_integer(ctype->modifiers);
235 return NULL;
237 if (match_string_ident(ident, "__SI__")) {
238 /* Nothing? */
239 return NULL;
241 if (match_string_ident(ident, "__DI__")) {
242 ctype->modifiers |= MOD_LONGLONG;
243 ctype->base_type = ctype_integer(ctype->modifiers);
244 return NULL;
246 return "unknown mode attribute";
248 return "expected attribute mode symbol";
251 /* Throw away for now.. */
252 if (match_string_ident(attribute, "format") ||
253 match_string_ident(attribute, "__format__"))
254 return NULL;
255 if (match_string_ident(attribute, "section") ||
256 match_string_ident(attribute, "__section__"))
257 return NULL;
258 if (match_string_ident(attribute, "unused") ||
259 match_string_ident(attribute, "__unused__"))
260 return NULL;
261 if (match_string_ident(attribute, "const") ||
262 match_string_ident(attribute, "__const__"))
263 return NULL;
264 if (match_string_ident(attribute, "noreturn"))
265 return NULL;
266 if (match_string_ident(attribute, "regparm"))
267 return NULL;
268 if (match_string_ident(attribute, "weak"))
269 return NULL;
271 return "unknown attribute";
274 struct token *attribute_specifier(struct token *token, struct ctype *ctype)
276 ctype->modifiers = 0;
277 token = expect(token, '(', "after attribute");
278 token = expect(token, '(', "after attribute");
280 for (;;) {
281 const char *error;
282 struct ident *attribute_name;
283 struct expression *attribute_expr;
285 if (eof_token(token))
286 break;
287 if (match_op(token, ';'))
288 break;
289 if (token_type(token) != TOKEN_IDENT)
290 break;
291 attribute_name = token->ident;
292 token = token->next;
293 attribute_expr = NULL;
294 if (match_op(token, '('))
295 token = parens_expression(token, &attribute_expr, "in attribute");
296 error = handle_attribute(ctype, attribute_name, attribute_expr);
297 if (error)
298 warn(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
299 if (!match_op(token, ','))
300 break;
301 token = token->next;
304 token = expect(token, ')', "after attribute");
305 token = expect(token, ')', "after attribute");
306 return token;
309 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
310 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
312 struct symbol * ctype_integer(unsigned int spec)
314 static struct symbol *const integer_ctypes[][2] = {
315 { &llong_ctype, &ullong_ctype },
316 { &long_ctype, &ulong_ctype },
317 { &short_ctype, &ushort_ctype },
318 { &char_ctype, &uchar_ctype },
319 { &int_ctype, &uint_ctype },
321 struct symbol *const (*ctype)[2];
323 ctype = integer_ctypes;
324 if (!(spec & MOD_LONGLONG)) {
325 ctype++;
326 if (!(spec & MOD_LONG)) {
327 ctype++;
328 if (!(spec & MOD_SHORT)) {
329 ctype++;
330 if (!(spec & MOD_CHAR))
331 ctype++;
335 return ctype[0][(spec & MOD_UNSIGNED) != 0];
338 struct symbol * ctype_fp(unsigned int spec)
340 if (spec & MOD_LONGLONG)
341 return &ldouble_ctype;
342 if (spec & MOD_LONG)
343 return &double_ctype;
344 return &float_ctype;
347 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
349 unsigned long mod = thistype->modifiers;
351 if (mod) {
352 unsigned long old = ctype->modifiers;
353 unsigned long extra = 0, dup;
355 if (mod & old & MOD_LONG) {
356 extra = MOD_LONGLONG | MOD_LONG;
357 mod &= ~MOD_LONG;
358 old &= ~MOD_LONG;
360 dup = (mod & old) | (extra & old) | (extra & mod);
361 if (dup)
362 warn(pos, "Just how %sdo you want this type to be?",
363 modifier_string(dup));
364 ctype->modifiers = old | mod | extra;
367 /* Context mask and value */
368 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
369 warn(pos, "inconsistend attribute types");
370 thistype->context = 0;
371 thistype->contextmask = 0;
373 ctype->context |= thistype->context;
374 ctype->contextmask |= thistype->contextmask;
376 /* Alignment */
377 if (thistype->alignment & (thistype->alignment-1)) {
378 warn(pos, "I don't like non-power-of-2 alignments");
379 thistype->alignment = 0;
381 if (thistype->alignment > ctype->alignment)
382 ctype->alignment = thistype->alignment;
384 /* Address space */
385 ctype->as = thistype->as;
389 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
391 struct token *token;
393 while ( (token = next) != NULL ) {
394 struct ctype thistype;
395 struct ident *ident;
396 struct symbol *s, *type;
397 unsigned long mod;
399 next = token->next;
400 if (token_type(token) != TOKEN_IDENT)
401 break;
402 ident = token->ident;
404 s = lookup_symbol(ident, NS_TYPEDEF);
405 if (!s)
406 break;
407 thistype = s->ctype;
408 mod = thistype.modifiers;
409 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
410 break;
411 if (mod & MOD_SPECIALBITS) {
412 if (mod & MOD_STRUCTOF)
413 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
414 else if (mod & MOD_UNIONOF)
415 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
416 else if (mod & MOD_ENUMOF)
417 next = enum_specifier(next, &thistype);
418 else if (mod & MOD_ATTRIBUTE)
419 next = attribute_specifier(next, &thistype);
420 else if (mod & MOD_TYPEOF)
421 next = typeof_specifier(next, &thistype);
422 mod = thistype.modifiers;
424 type = thistype.base_type;
425 if (type) {
426 if (qual)
427 break;
428 if (type != ctype->base_type) {
429 if (ctype->base_type)
430 break;
431 ctype->base_type = type;
435 apply_ctype(token->pos, &thistype, ctype);
438 /* Turn the "virtual types" into real types with real sizes etc */
439 if (!ctype->base_type && (ctype->modifiers & MOD_SPECIFIER))
440 ctype->base_type = &int_type;
442 if (ctype->base_type == &int_type) {
443 ctype->base_type = ctype_integer(ctype->modifiers & MOD_SPECIFIER);
444 ctype->modifiers &= ~MOD_SPECIFIER;
445 return token;
447 if (ctype->base_type == &fp_type) {
448 ctype->base_type = ctype_fp(ctype->modifiers & MOD_SPECIFIER);
449 ctype->modifiers &= ~MOD_SPECIFIER;
450 return token;
452 return token;
455 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
457 struct expression *expr = NULL;
459 token = parse_expression(token, &expr);
460 sym->array_size = expr;
461 return token;
464 static struct token *parameter_type_list(struct token *, struct symbol *);
465 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
467 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
469 struct ctype *ctype = &(*tree)->ctype;
471 if (p && token_type(token) == TOKEN_IDENT) {
472 *p = token->ident;
473 token = token->next;
476 for (;;) {
477 if (match_ident(token, &__attribute___ident) || match_ident(token, &__attribute_ident)) {
478 struct ctype thistype = { 0, };
479 token = attribute_specifier(token->next, &thistype);
480 apply_ctype(token->pos, &thistype, ctype);
481 continue;
483 if (token_type(token) != TOKEN_SPECIAL)
484 return token;
487 * This can be either a parameter list or a grouping.
488 * For the direct (non-abstract) case, we know if must be
489 * a paramter list if we already saw the identifier.
490 * For the abstract case, we know if must be a parameter
491 * list if it is empty or starts with a type.
493 if (token->special == '(') {
494 struct symbol *sym;
495 struct token *next = token->next;
496 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
498 if (!fn) {
499 struct symbol *base_type = ctype->base_type;
500 token = declarator(next, tree, p);
501 token = expect(token, ')', "in nested declarator");
502 while (ctype->base_type != base_type)
503 ctype = &ctype->base_type->ctype;
504 p = NULL;
505 continue;
508 sym = indirect(token->pos, ctype, SYM_FN);
509 token = parameter_type_list(next, sym);
510 token = expect(token, ')', "in function declarator");
511 continue;
513 if (token->special == '[') {
514 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
515 token = abstract_array_declarator(token->next, array);
516 token = expect(token, ']', "in abstract_array_declarator");
517 ctype = &array->ctype;
518 continue;
520 if (token->special == ':') {
521 struct symbol *bitfield;
522 struct expression *expr;
523 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
524 token = conditional_expression(token->next, &expr);
525 bitfield->fieldwidth = get_expression_value(expr);
526 continue;
528 break;
530 if (p) {
531 (*tree)->ident = *p;
533 return token;
536 static struct token *pointer(struct token *token, struct ctype *ctype)
538 unsigned long modifiers;
539 struct symbol *base_type;
541 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
542 base_type = ctype->base_type;
543 ctype->modifiers = modifiers;
545 while (match_op(token,'*')) {
546 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
547 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
548 ptr->ctype.as = ctype->as;
549 ptr->ctype.context = ctype->context;
550 ptr->ctype.contextmask = ctype->contextmask;
551 ptr->ctype.base_type = base_type;
553 base_type = ptr;
554 ctype->modifiers = modifiers & MOD_STORAGE;
555 ctype->base_type = base_type;
556 ctype->as = 0;
557 ctype->context = 0;
558 ctype->contextmask = 0;
560 token = declaration_specifiers(token->next, ctype, 1);
562 return token;
565 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
567 token = pointer(token, &(*tree)->ctype);
568 return direct_declarator(token, tree, p);
571 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
573 while (!match_op(token, '}')) {
574 struct ctype ctype = {0, };
576 token = declaration_specifiers(token, &ctype, 0);
577 for (;;) {
578 struct ident *ident = NULL;
579 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
580 decl->ctype = ctype;
581 token = pointer(token, &decl->ctype);
582 token = direct_declarator(token, &decl, &ident);
583 if (match_op(token, ':')) {
584 struct expression *expr;
585 token = parse_expression(token->next, &expr);
587 add_symbol(list, decl);
588 if (!match_op(token, ','))
589 break;
590 token = token->next;
592 if (!match_op(token, ';'))
593 break;
594 token = token->next;
596 return token;
599 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
601 struct ident *ident = NULL;
602 struct symbol *sym;
603 struct ctype ctype = { 0, };
605 token = declaration_specifiers(token, &ctype, 0);
606 sym = alloc_symbol(token->pos, SYM_NODE);
607 sym->ctype = ctype;
608 *tree = sym;
609 token = pointer(token, &sym->ctype);
610 token = direct_declarator(token, tree, &ident);
611 return token;
614 struct token *typename(struct token *token, struct symbol **p)
616 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
617 *p = sym;
618 token = declaration_specifiers(token, &sym->ctype, 0);
619 return declarator(token, &sym, NULL);
622 struct token *expression_statement(struct token *token, struct expression **tree)
624 token = parse_expression(token, tree);
625 return expect(token, ';', "at end of statement");
628 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
630 struct expression *expr;
632 /* Allow empty operands */
633 if (match_op(token->next, ':') || match_op(token->next, ')'))
634 return token->next;
635 do {
636 token = primary_expression(token->next, &expr);
637 token = parens_expression(token, &expr, "in asm parameter");
638 } while (match_op(token, ','));
639 return token;
642 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
644 struct expression *expr;
646 do {
647 token = primary_expression(token->next, &expr);
648 } while (match_op(token, ','));
649 return token;
652 /* Make a statement out of an expression */
653 static struct statement *make_statement(struct expression *expr)
655 struct statement *stmt;
657 if (!expr)
658 return NULL;
659 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
660 stmt->expression = expr;
661 return stmt;
665 * All iterators have two symbols associated with them:
666 * the "continue" and "break" symbols, which are targets
667 * for continue and break statements respectively.
669 * They are in a special name-space, but they follow
670 * all the normal visibility rules, so nested iterators
671 * automatically work right.
673 static void start_iterator(struct statement *stmt)
675 struct symbol *cont, *brk;
677 start_symbol_scope();
678 cont = alloc_symbol(stmt->pos, SYM_NODE);
679 cont->ident = &continue_ident;
680 bind_symbol(cont, &continue_ident, NS_ITERATOR);
681 brk = alloc_symbol(stmt->pos, SYM_NODE);
682 brk->ident = &break_ident;
683 bind_symbol(brk, &break_ident, NS_ITERATOR);
685 stmt->type = STMT_ITERATOR;
686 stmt->iterator_break = brk;
687 stmt->iterator_continue = cont;
688 fn_local_symbol(brk);
689 fn_local_symbol(cont);
692 static void end_iterator(struct statement *stmt)
694 end_symbol_scope();
697 static struct statement *start_function(struct symbol *sym)
699 struct symbol *ret;
700 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
702 start_function_scope();
703 ret = alloc_symbol(sym->pos, SYM_NODE);
704 ret->ident = &return_ident;
705 ret->ctype = sym->ctype.base_type->ctype;
706 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
707 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
708 bind_symbol(ret, &return_ident, NS_ITERATOR);
709 stmt->ret = ret;
711 fn_local_symbol(ret);
712 return stmt;
715 static void end_function(struct symbol *sym)
717 end_function_scope();
721 * A "switch()" statement, like an iterator, has a
722 * the "break" symbol associated with it. It works
723 * exactly like the iterator break - it's the target
724 * for any break-statements in scope, and means that
725 * "break" handling doesn't even need to know whether
726 * it's breaking out of an iterator or a switch.
728 * In addition, the "case" symbol is a marker for the
729 * case/default statements to find the switch statement
730 * that they are associated with.
732 static void start_switch(struct statement *stmt)
734 struct symbol *brk, *switch_case;
736 start_symbol_scope();
737 brk = alloc_symbol(stmt->pos, SYM_NODE);
738 brk->ident = &break_ident;
739 bind_symbol(brk, &break_ident, NS_ITERATOR);
741 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
742 switch_case->ident = &case_ident;
743 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
744 switch_case->stmt = stmt;
746 stmt->type = STMT_SWITCH;
747 stmt->switch_break = brk;
748 stmt->switch_case = switch_case;
750 fn_local_symbol(brk);
751 fn_local_symbol(switch_case);
754 static void end_switch(struct statement *stmt)
756 if (!stmt->switch_case->symbol_list)
757 warn(stmt->pos, "switch with no cases");
758 end_symbol_scope();
761 static void add_case_statement(struct statement *stmt)
763 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
764 struct symbol *sym;
766 if (!target) {
767 warn(stmt->pos, "not in switch scope");
768 return;
770 sym = alloc_symbol(stmt->pos, SYM_NODE);
771 add_symbol(&target->symbol_list, sym);
772 sym->stmt = stmt;
773 stmt->case_label = sym;
774 fn_local_symbol(sym);
777 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
779 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
781 if (!target)
782 error(token->pos, "internal error: return without a function target");
783 stmt->type = STMT_RETURN;
784 stmt->ret_target = target;
785 return expression_statement(token->next, &stmt->ret_value);
788 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
790 struct symbol_list *syms;
791 struct expression *e1, *e2, *e3;
792 struct statement *iterator;
794 start_iterator(stmt);
795 token = expect(token->next, '(', "after 'for'");
797 syms = NULL;
798 e1 = NULL;
799 /* C99 variable declaration? */
800 if (lookup_type(token)) {
801 token = external_declaration(token, &syms);
802 } else {
803 token = parse_expression(token, &e1);
804 token = expect(token, ';', "in 'for'");
806 token = parse_expression(token, &e2);
807 token = expect(token, ';', "in 'for'");
808 token = parse_expression(token, &e3);
809 token = expect(token, ')', "in 'for'");
810 token = statement(token, &iterator);
812 stmt->iterator_syms = syms;
813 stmt->iterator_pre_statement = make_statement(e1);
814 stmt->iterator_pre_condition = e2;
815 stmt->iterator_post_statement = make_statement(e3);
816 stmt->iterator_post_condition = e2;
817 stmt->iterator_statement = iterator;
818 end_iterator(stmt);
820 return token;
823 struct token *parse_while_statement(struct token *token, struct statement *stmt)
825 struct expression *expr;
826 struct statement *iterator;
828 start_iterator(stmt);
829 token = parens_expression(token->next, &expr, "after 'while'");
830 token = statement(token, &iterator);
832 stmt->iterator_pre_condition = expr;
833 stmt->iterator_post_condition = expr;
834 stmt->iterator_statement = iterator;
835 end_iterator(stmt);
837 return token;
840 struct token *parse_do_statement(struct token *token, struct statement *stmt)
842 struct expression *expr;
843 struct statement *iterator;
845 start_iterator(stmt);
846 token = statement(token->next, &iterator);
847 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
848 token = token->next;
849 else
850 warn(token->pos, "expected 'while' after 'do'");
851 token = parens_expression(token, &expr, "after 'do-while'");
853 stmt->iterator_post_condition = expr;
854 stmt->iterator_statement = iterator;
855 end_iterator(stmt);
857 return expect(token, ';', "after statement");
860 static struct token *statement(struct token *token, struct statement **tree)
862 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
864 *tree = stmt;
865 if (token_type(token) == TOKEN_IDENT) {
866 if (token->ident == &if_ident) {
867 stmt->type = STMT_IF;
868 token = parens_expression(token->next, &stmt->if_conditional, "after if");
869 token = statement(token, &stmt->if_true);
870 if (token_type(token) != TOKEN_IDENT)
871 return token;
872 if (token->ident != &else_ident)
873 return token;
874 return statement(token->next, &stmt->if_false);
877 if (token->ident == &return_ident)
878 return parse_return_statement(token, stmt);
880 if (token->ident == &break_ident || token->ident == &continue_ident) {
881 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
882 stmt->type = STMT_GOTO;
883 stmt->goto_label = target;
884 if (!target)
885 warn(stmt->pos, "break/continue not in iterator scope");
886 return expect(token->next, ';', "at end of statement");
888 if (token->ident == &default_ident) {
889 token = token->next;
890 goto default_statement;
892 if (token->ident == &case_ident) {
893 token = parse_expression(token->next, &stmt->case_expression);
894 if (match_op(token, SPECIAL_ELLIPSIS))
895 token = parse_expression(token->next, &stmt->case_to);
896 default_statement:
897 stmt->type = STMT_CASE;
898 token = expect(token, ':', "after default/case");
899 add_case_statement(stmt);
900 return statement(token, &stmt->case_statement);
902 if (token->ident == &switch_ident) {
903 stmt->type = STMT_SWITCH;
904 start_switch(stmt);
905 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
906 token = statement(token, &stmt->switch_statement);
907 end_switch(stmt);
908 return token;
910 if (token->ident == &for_ident)
911 return parse_for_statement(token, stmt);
913 if (token->ident == &while_ident)
914 return parse_while_statement(token, stmt);
916 if (token->ident == &do_ident)
917 return parse_do_statement(token, stmt);
919 if (token->ident == &goto_ident) {
920 stmt->type = STMT_GOTO;
921 token = token->next;
922 if (match_op(token, '*')) {
923 token = parse_expression(token->next, &stmt->goto_expression);
924 } else if (token_type(token) == TOKEN_IDENT) {
925 stmt->goto_label = label_symbol(token);
926 token = token->next;
927 } else {
928 warn(token->pos, "Expected identifier or goto expression");
930 return expect(token, ';', "at end of statement");
932 if (token->ident == &asm_ident || token->ident == &__asm___ident || token->ident == &__asm_ident) {
933 struct expression *expr;
934 stmt->type = STMT_ASM;
935 token = token->next;
936 if (token_type(token) == TOKEN_IDENT) {
937 if (token->ident == &__volatile___ident || token->ident == &volatile_ident)
938 token = token->next;
940 token = expect(token, '(', "after asm");
941 token = parse_expression(token->next, &expr);
942 if (match_op(token, ':'))
943 token = parse_asm_operands(token, stmt);
944 if (match_op(token, ':'))
945 token = parse_asm_operands(token, stmt);
946 if (match_op(token, ':'))
947 token = parse_asm_clobbers(token, stmt);
948 token = expect(token, ')', "after asm");
949 return expect(token, ';', "at end of asm-statement");
951 if (match_op(token->next, ':')) {
952 stmt->type = STMT_LABEL;
953 stmt->label_identifier = label_symbol(token);
954 return statement(token->next->next, &stmt->label_statement);
958 if (match_op(token, '{')) {
959 stmt->type = STMT_COMPOUND;
960 start_symbol_scope();
961 token = compound_statement(token->next, stmt);
962 end_symbol_scope();
964 return expect(token, '}', "at end of compound statement");
967 stmt->type = STMT_EXPRESSION;
968 return expression_statement(token, &stmt->expression);
971 struct token * statement_list(struct token *token, struct statement_list **list)
973 for (;;) {
974 struct statement * stmt;
975 if (eof_token(token))
976 break;
977 if (match_op(token, '}'))
978 break;
979 token = statement(token, &stmt);
980 add_statement(list, stmt);
982 return token;
985 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
987 struct symbol_list **list = &fn->arguments;
988 for (;;) {
989 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
991 if (match_op(token, SPECIAL_ELLIPSIS)) {
992 fn->variadic = 1;
993 token = token->next;
994 break;
997 if (!lookup_type(token)) {
998 warn(token->pos, "non-ANSI parameter list");
999 break;
1001 token = parameter_declaration(token, &sym);
1002 /* Special case: (void) */
1003 if (!*list && !sym->ident && sym->ctype.base_type == &void_ctype)
1004 break;
1005 add_symbol(list, sym);
1006 if (!match_op(token, ','))
1007 break;
1008 token = token->next;
1011 return token;
1014 struct token *compound_statement(struct token *token, struct statement *stmt)
1016 while (!eof_token(token)) {
1017 if (!lookup_type(token))
1018 break;
1019 token = external_declaration(token, &stmt->syms);
1021 token = statement_list(token, &stmt->stmts);
1022 return token;
1025 static struct expression *identifier_expression(struct token *token)
1027 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1028 expr->expr_ident = token->ident;
1029 return expr;
1032 static struct expression *index_expression(struct expression *from, struct expression *to)
1034 int idx_from, idx_to;
1035 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1037 idx_from = get_expression_value(from);
1038 idx_to = idx_from;
1039 if (to) {
1040 idx_to = get_expression_value(to);
1041 if (idx_to < idx_from || idx_from < 0)
1042 warn(from->pos, "nonsense array initializer index range");
1044 expr->idx_from = idx_from;
1045 expr->idx_to = idx_to;
1046 return expr;
1049 static struct token *initializer_list(struct expression_list **list, struct token *token)
1051 for (;;) {
1052 struct token *next = token->next;
1053 struct expression *expr;
1055 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
1056 add_expression(list, identifier_expression(next));
1057 token = next->next->next;
1058 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1059 add_expression(list, identifier_expression(token));
1060 token = next->next;
1061 } else if (match_op(token, '[')) {
1062 struct expression *from = NULL, *to = NULL;
1063 token = constant_expression(token->next, &from);
1064 if (match_op(token, SPECIAL_ELLIPSIS))
1065 token = constant_expression(token->next, &to);
1066 add_expression(list, index_expression(from, to));
1067 token = expect(token, ']', "at end of initializer index");
1068 token = expect(token, '=', "at end of initializer index");
1071 expr = NULL;
1072 token = initializer(&expr, token);
1073 if (!expr)
1074 break;
1075 add_expression(list, expr);
1076 if (!match_op(token, ','))
1077 break;
1078 token = token->next;
1080 return token;
1083 struct token *initializer(struct expression **tree, struct token *token)
1085 if (match_op(token, '{')) {
1086 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1087 *tree = expr;
1088 token = initializer_list(&expr->expr_list, token->next);
1089 return expect(token, '}', "at end of initializer");
1091 return assignment_expression(token, tree);
1094 static void declare_argument(struct symbol *sym, struct symbol *fn)
1096 if (!sym->ident) {
1097 warn(sym->pos, "no identifier for function argument");
1098 return;
1100 bind_symbol(sym, sym->ident, NS_SYMBOL);
1103 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1104 struct symbol_list **list)
1106 struct symbol *base_type = decl->ctype.base_type;
1107 struct statement *stmt;
1108 struct symbol *arg;
1110 function_symbol_list = &decl->symbol_list;
1111 if (decl->ctype.modifiers & MOD_EXTERN) {
1112 if (!(decl->ctype.modifiers & MOD_INLINE))
1113 warn(decl->pos, "function with external linkage has definition");
1115 if (!(decl->ctype.modifiers & MOD_STATIC))
1116 decl->ctype.modifiers |= MOD_EXTERN;
1118 stmt = start_function(decl);
1120 base_type->stmt = stmt;
1121 FOR_EACH_PTR (base_type->arguments, arg) {
1122 declare_argument(arg, base_type);
1123 } END_FOR_EACH_PTR;
1125 token = compound_statement(token->next, stmt);
1127 end_function(decl);
1128 if (!(decl->ctype.modifiers & MOD_INLINE))
1129 add_symbol(list, decl);
1130 check_declaration(decl);
1131 function_symbol_list = NULL;
1132 return expect(token, '}', "at end of function");
1135 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1137 struct ident *ident = NULL;
1138 struct symbol *decl;
1139 struct ctype ctype = { 0, };
1140 struct symbol *base_type;
1141 int is_typedef;
1143 /* Parse declaration-specifiers, if any */
1144 token = declaration_specifiers(token, &ctype, 0);
1145 decl = alloc_symbol(token->pos, SYM_NODE);
1146 decl->ctype = ctype;
1147 token = pointer(token, &decl->ctype);
1148 token = declarator(token, &decl, &ident);
1150 /* Just a type declaration? */
1151 if (!ident)
1152 return expect(token, ';', "end of type declaration");
1154 decl->ident = ident;
1156 /* type define declaration? */
1157 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1159 /* Typedef's don't have meaningful storage */
1160 if (is_typedef) {
1161 ctype.modifiers &= ~MOD_STORAGE;
1162 decl->ctype.modifiers &= ~MOD_STORAGE;
1165 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1167 base_type = decl->ctype.base_type;
1168 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1169 if (match_op(token, '{'))
1170 return parse_function_body(token, decl, list);
1172 if (!(decl->ctype.modifiers & MOD_STATIC))
1173 decl->ctype.modifiers |= MOD_EXTERN;
1176 for (;;) {
1177 if (token_type(token) == TOKEN_IDENT) {
1178 if (token->ident == &asm_ident || token->ident == &__asm_ident || token->ident == &__asm___ident) {
1179 struct expression *expr;
1181 token = expect(token->next, '(', "after asm");
1182 token = parse_expression(token->next, &expr);
1183 token = expect(token, ')', "after asm");
1186 if (!is_typedef && match_op(token, '=')) {
1187 if (decl->ctype.modifiers & MOD_EXTERN) {
1188 warn(decl->pos, "symbol with external linkage has initializer");
1189 decl->ctype.modifiers &= ~MOD_EXTERN;
1191 token = initializer(&decl->initializer, token->next);
1193 if (!is_typedef) {
1194 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1195 add_symbol(list, decl);
1196 if (function_symbol_list)
1197 fn_local_symbol(decl);
1200 check_declaration(decl);
1202 if (!match_op(token, ','))
1203 break;
1205 ident = NULL;
1206 decl = alloc_symbol(token->pos, SYM_NODE);
1207 decl->ctype = ctype;
1208 token = pointer(token, &decl->ctype);
1209 token = declarator(token->next, &decl, &ident);
1210 if (!ident) {
1211 warn(token->pos, "expected identifier name in type definition");
1212 return token;
1215 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1217 /* Function declarations are automatically extern unless specifically static */
1218 base_type = decl->ctype.base_type;
1219 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1220 if (!(decl->ctype.modifiers & MOD_STATIC))
1221 decl->ctype.modifiers |= MOD_EXTERN;
1224 return expect(token, ';', "at end of declaration");
1227 void translation_unit(struct token *token, struct symbol_list **list)
1229 while (!eof_token(token))
1230 token = external_declaration(token, list);
1231 // They aren't needed any more
1232 clear_token_alloc();