This add a linearization phase. It's not even close to done
[smatch.git] / parse.c
blobe6fd2e0ec48341479e44153e653c4c43632938d3
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, "address_space")) {
199 if (!expr)
200 return "expected address space number";
201 ctype->as = get_expression_value(expr);
202 return NULL;
204 if (match_string_ident(attribute, "context")) {
205 if (expr->type == EXPR_COMMA) {
206 int mask = get_expression_value(expr->left);
207 int value = get_expression_value(expr->right);
208 if (value & ~mask)
209 return "nonsense attribute types";
210 ctype->contextmask |= mask;
211 ctype->context |= value;
212 return NULL;
214 return "expected context mask and value";
217 /* Throw away for now.. */
218 if (match_string_ident(attribute, "format") ||
219 match_string_ident(attribute, "__format__"))
220 return NULL;
221 if (match_string_ident(attribute, "section") ||
222 match_string_ident(attribute, "__section__"))
223 return NULL;
224 if (match_string_ident(attribute, "unused") ||
225 match_string_ident(attribute, "__unused__"))
226 return NULL;
227 if (match_string_ident(attribute, "const") ||
228 match_string_ident(attribute, "__const__"))
229 return NULL;
230 if (match_string_ident(attribute, "noreturn"))
231 return NULL;
232 if (match_string_ident(attribute, "regparm"))
233 return NULL;
234 if (match_string_ident(attribute, "weak"))
235 return NULL;
237 return "unknown attribute";
240 struct token *attribute_specifier(struct token *token, struct ctype *ctype)
242 ctype->modifiers = 0;
243 token = expect(token, '(', "after attribute");
244 token = expect(token, '(', "after attribute");
246 for (;;) {
247 const char *error;
248 struct ident *attribute_name;
249 struct expression *attribute_expr;
251 if (eof_token(token))
252 break;
253 if (match_op(token, ';'))
254 break;
255 if (token_type(token) != TOKEN_IDENT)
256 break;
257 attribute_name = token->ident;
258 token = token->next;
259 attribute_expr = NULL;
260 if (match_op(token, '('))
261 token = parens_expression(token, &attribute_expr, "in attribute");
262 error = handle_attribute(ctype, attribute_name, attribute_expr);
263 if (error)
264 warn(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
265 if (!match_op(token, ','))
266 break;
267 token = token->next;
270 token = expect(token, ')', "after attribute");
271 token = expect(token, ')', "after attribute");
272 return token;
275 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
276 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
278 struct symbol * ctype_integer(unsigned int spec)
280 static struct symbol *const integer_ctypes[][2] = {
281 { &llong_ctype, &ullong_ctype },
282 { &long_ctype, &ulong_ctype },
283 { &short_ctype, &ushort_ctype },
284 { &char_ctype, &uchar_ctype },
285 { &int_ctype, &uint_ctype },
287 struct symbol *const (*ctype)[2];
289 ctype = integer_ctypes;
290 if (!(spec & MOD_LONGLONG)) {
291 ctype++;
292 if (!(spec & MOD_LONG)) {
293 ctype++;
294 if (!(spec & MOD_SHORT)) {
295 ctype++;
296 if (!(spec & MOD_CHAR))
297 ctype++;
301 return ctype[0][(spec & MOD_UNSIGNED) != 0];
304 struct symbol * ctype_fp(unsigned int spec)
306 if (spec & MOD_LONGLONG)
307 return &ldouble_ctype;
308 if (spec & MOD_LONG)
309 return &double_ctype;
310 return &float_ctype;
313 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
315 unsigned long mod = thistype->modifiers;
317 if (mod) {
318 unsigned long old = ctype->modifiers;
319 unsigned long extra = 0, dup;
321 if (mod & old & MOD_LONG) {
322 extra = MOD_LONGLONG | MOD_LONG;
323 mod &= ~MOD_LONG;
324 old &= ~MOD_LONG;
326 dup = (mod & old) | (extra & old) | (extra & mod);
327 if (dup)
328 warn(pos, "Just how %sdo you want this type to be?",
329 modifier_string(dup));
330 ctype->modifiers = old | mod | extra;
333 /* Context mask and value */
334 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
335 warn(pos, "inconsistend attribute types");
336 thistype->context = 0;
337 thistype->contextmask = 0;
339 ctype->context |= thistype->context;
340 ctype->contextmask |= thistype->contextmask;
342 /* Alignment */
343 if (thistype->alignment & (thistype->alignment-1)) {
344 warn(pos, "I don't like non-power-of-2 alignments");
345 thistype->alignment = 0;
347 if (thistype->alignment > ctype->alignment)
348 ctype->alignment = thistype->alignment;
350 /* Address space */
351 ctype->as = thistype->as;
355 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
357 struct token *token;
359 while ( (token = next) != NULL ) {
360 struct ctype thistype;
361 struct ident *ident;
362 struct symbol *s, *type;
363 unsigned long mod;
365 next = token->next;
366 if (token_type(token) != TOKEN_IDENT)
367 break;
368 ident = token->ident;
370 s = lookup_symbol(ident, NS_TYPEDEF);
371 if (!s)
372 break;
373 thistype = s->ctype;
374 mod = thistype.modifiers;
375 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
376 break;
377 if (mod & MOD_SPECIALBITS) {
378 if (mod & MOD_STRUCTOF)
379 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
380 else if (mod & MOD_UNIONOF)
381 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
382 else if (mod & MOD_ENUMOF)
383 next = enum_specifier(next, &thistype);
384 else if (mod & MOD_ATTRIBUTE)
385 next = attribute_specifier(next, &thistype);
386 else if (mod & MOD_TYPEOF)
387 next = typeof_specifier(next, &thistype);
388 mod = thistype.modifiers;
390 type = thistype.base_type;
391 if (type) {
392 if (qual)
393 break;
394 if (type != ctype->base_type) {
395 if (ctype->base_type)
396 break;
397 ctype->base_type = type;
401 apply_ctype(token->pos, &thistype, ctype);
404 /* Turn the "virtual types" into real types with real sizes etc */
405 if (!ctype->base_type && (ctype->modifiers & MOD_SPECIFIER))
406 ctype->base_type = &int_type;
408 if (ctype->base_type == &int_type) {
409 ctype->base_type = ctype_integer(ctype->modifiers & MOD_SPECIFIER);
410 ctype->modifiers &= ~MOD_SPECIFIER;
411 return token;
413 if (ctype->base_type == &fp_type) {
414 ctype->base_type = ctype_fp(ctype->modifiers & MOD_SPECIFIER);
415 ctype->modifiers &= ~MOD_SPECIFIER;
416 return token;
418 return token;
421 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
423 struct expression *expr = NULL;
425 token = parse_expression(token, &expr);
426 sym->array_size = expr;
427 return token;
430 static struct token *parameter_type_list(struct token *, struct symbol *);
431 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
433 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
435 struct ctype *ctype = &(*tree)->ctype;
437 if (p && token_type(token) == TOKEN_IDENT) {
438 *p = token->ident;
439 token = token->next;
442 for (;;) {
443 if (match_ident(token, &__attribute___ident) || match_ident(token, &__attribute_ident)) {
444 struct ctype thistype = { 0, };
445 token = attribute_specifier(token->next, &thistype);
446 apply_ctype(token->pos, &thistype, ctype);
447 continue;
449 if (token_type(token) != TOKEN_SPECIAL)
450 return token;
453 * This can be either a parameter list or a grouping.
454 * For the direct (non-abstract) case, we know if must be
455 * a paramter list if we already saw the identifier.
456 * For the abstract case, we know if must be a parameter
457 * list if it is empty or starts with a type.
459 if (token->special == '(') {
460 struct symbol *sym;
461 struct token *next = token->next;
462 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
464 if (!fn) {
465 struct symbol *base_type = ctype->base_type;
466 token = declarator(next, tree, p);
467 token = expect(token, ')', "in nested declarator");
468 while (ctype->base_type != base_type)
469 ctype = &ctype->base_type->ctype;
470 p = NULL;
471 continue;
474 sym = indirect(token->pos, ctype, SYM_FN);
475 token = parameter_type_list(next, sym);
476 token = expect(token, ')', "in function declarator");
477 continue;
479 if (token->special == '[') {
480 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
481 token = abstract_array_declarator(token->next, array);
482 token = expect(token, ']', "in abstract_array_declarator");
483 ctype = &array->ctype;
484 continue;
486 if (token->special == ':') {
487 struct symbol *bitfield;
488 struct expression *expr;
489 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
490 token = conditional_expression(token->next, &expr);
491 bitfield->fieldwidth = get_expression_value(expr);
492 continue;
494 break;
496 if (p) {
497 (*tree)->ident = *p;
499 return token;
502 static struct token *pointer(struct token *token, struct ctype *ctype)
504 unsigned long modifiers;
505 struct symbol *base_type;
507 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
508 base_type = ctype->base_type;
509 ctype->modifiers = modifiers;
511 while (match_op(token,'*')) {
512 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
513 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
514 ptr->ctype.as = ctype->as;
515 ptr->ctype.context = ctype->context;
516 ptr->ctype.contextmask = ctype->contextmask;
517 ptr->ctype.base_type = base_type;
519 base_type = ptr;
520 ctype->modifiers = modifiers & MOD_STORAGE;
521 ctype->base_type = base_type;
522 ctype->as = 0;
523 ctype->context = 0;
524 ctype->contextmask = 0;
526 token = declaration_specifiers(token->next, ctype, 1);
528 return token;
531 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
533 token = pointer(token, &(*tree)->ctype);
534 return direct_declarator(token, tree, p);
537 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
539 while (!match_op(token, '}')) {
540 struct ctype ctype = {0, };
542 token = declaration_specifiers(token, &ctype, 0);
543 for (;;) {
544 struct ident *ident = NULL;
545 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
546 decl->ctype = ctype;
547 token = pointer(token, &decl->ctype);
548 token = direct_declarator(token, &decl, &ident);
549 if (match_op(token, ':')) {
550 struct expression *expr;
551 token = parse_expression(token->next, &expr);
553 add_symbol(list, decl);
554 if (!match_op(token, ','))
555 break;
556 token = token->next;
558 if (!match_op(token, ';'))
559 break;
560 token = token->next;
562 return token;
565 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
567 struct ident *ident = NULL;
568 struct symbol *sym;
569 struct ctype ctype = { 0, };
571 token = declaration_specifiers(token, &ctype, 0);
572 sym = alloc_symbol(token->pos, SYM_NODE);
573 sym->ctype = ctype;
574 *tree = sym;
575 token = pointer(token, &sym->ctype);
576 token = direct_declarator(token, tree, &ident);
577 return token;
580 struct token *typename(struct token *token, struct symbol **p)
582 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
583 *p = sym;
584 token = declaration_specifiers(token, &sym->ctype, 0);
585 return declarator(token, &sym, NULL);
588 struct token *expression_statement(struct token *token, struct expression **tree)
590 token = parse_expression(token, tree);
591 return expect(token, ';', "at end of statement");
594 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
596 struct expression *expr;
598 /* Allow empty operands */
599 if (match_op(token->next, ':') || match_op(token->next, ')'))
600 return token->next;
601 do {
602 token = primary_expression(token->next, &expr);
603 token = parens_expression(token, &expr, "in asm parameter");
604 } while (match_op(token, ','));
605 return token;
608 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
610 struct expression *expr;
612 do {
613 token = primary_expression(token->next, &expr);
614 } while (match_op(token, ','));
615 return token;
618 /* Make a statement out of an expression */
619 static struct statement *make_statement(struct expression *expr)
621 struct statement *stmt;
623 if (!expr)
624 return NULL;
625 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
626 stmt->expression = expr;
627 return stmt;
631 * All iterators have two symbols associated with them:
632 * the "continue" and "break" symbols, which are targets
633 * for continue and break statements respectively.
635 * They are in a special name-space, but they follow
636 * all the normal visibility rules, so nested iterators
637 * automatically work right.
639 static void start_iterator(struct statement *stmt)
641 struct symbol *cont, *brk;
643 start_symbol_scope();
644 cont = alloc_symbol(stmt->pos, SYM_NODE);
645 cont->ident = &continue_ident;
646 bind_symbol(cont, &continue_ident, NS_ITERATOR);
647 brk = alloc_symbol(stmt->pos, SYM_NODE);
648 brk->ident = &break_ident;
649 bind_symbol(brk, &break_ident, NS_ITERATOR);
651 stmt->type = STMT_ITERATOR;
652 stmt->iterator_break = brk;
653 stmt->iterator_continue = cont;
654 fn_local_symbol(brk);
655 fn_local_symbol(cont);
658 static void end_iterator(struct statement *stmt)
660 end_symbol_scope();
663 static struct statement *start_function(struct symbol *sym)
665 struct symbol *ret;
666 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
668 start_function_scope();
669 ret = alloc_symbol(sym->pos, SYM_NODE);
670 ret->ident = &return_ident;
671 ret->ctype = sym->ctype.base_type->ctype;
672 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
673 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
674 bind_symbol(ret, &return_ident, NS_ITERATOR);
675 stmt->ret = ret;
677 fn_local_symbol(ret);
678 return stmt;
681 static void end_function(struct symbol *sym)
683 end_function_scope();
687 * A "switch()" statement, like an iterator, has a
688 * the "break" symbol associated with it. It works
689 * exactly like the iterator break - it's the target
690 * for any break-statements in scope, and means that
691 * "break" handling doesn't even need to know whether
692 * it's breaking out of an iterator or a switch.
694 * In addition, the "case" symbol is a marker for the
695 * case/default statements to find the switch statement
696 * that they are associated with.
698 static void start_switch(struct statement *stmt)
700 struct symbol *brk, *switch_case;
702 start_symbol_scope();
703 brk = alloc_symbol(stmt->pos, SYM_NODE);
704 brk->ident = &break_ident;
705 bind_symbol(brk, &break_ident, NS_ITERATOR);
707 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
708 switch_case->ident = &case_ident;
709 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
710 switch_case->stmt = stmt;
712 stmt->type = STMT_SWITCH;
713 stmt->switch_break = brk;
714 stmt->switch_case = switch_case;
716 fn_local_symbol(brk);
717 fn_local_symbol(switch_case);
720 static void end_switch(struct statement *stmt)
722 if (!stmt->switch_case->symbol_list)
723 warn(stmt->pos, "switch with no cases");
724 end_symbol_scope();
727 static void add_case_statement(struct statement *stmt)
729 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
730 struct symbol *sym;
732 if (!target) {
733 warn(stmt->pos, "not in switch scope");
734 return;
736 sym = alloc_symbol(stmt->pos, SYM_NODE);
737 add_symbol(&target->symbol_list, sym);
738 sym->stmt = stmt;
739 stmt->case_label = sym;
740 fn_local_symbol(sym);
743 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
745 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
747 if (!target)
748 error(token->pos, "internal error: return without a function target");
749 stmt->type = STMT_RETURN;
750 stmt->ret_target = target;
751 return expression_statement(token->next, &stmt->ret_value);
754 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
756 struct symbol_list *syms;
757 struct expression *e1, *e2, *e3;
758 struct statement *iterator;
760 start_iterator(stmt);
761 token = expect(token->next, '(', "after 'for'");
763 syms = NULL;
764 e1 = NULL;
765 /* C99 variable declaration? */
766 if (lookup_type(token)) {
767 token = external_declaration(token, &syms);
768 } else {
769 token = parse_expression(token, &e1);
770 token = expect(token, ';', "in 'for'");
772 token = parse_expression(token, &e2);
773 token = expect(token, ';', "in 'for'");
774 token = parse_expression(token, &e3);
775 token = expect(token, ')', "in 'for'");
776 token = statement(token, &iterator);
778 stmt->iterator_syms = syms;
779 stmt->iterator_pre_statement = make_statement(e1);
780 stmt->iterator_pre_condition = e2;
781 stmt->iterator_post_statement = make_statement(e3);
782 stmt->iterator_post_condition = e2;
783 stmt->iterator_statement = iterator;
784 end_iterator(stmt);
786 return token;
789 struct token *parse_while_statement(struct token *token, struct statement *stmt)
791 struct expression *expr;
792 struct statement *iterator;
794 start_iterator(stmt);
795 token = parens_expression(token->next, &expr, "after 'while'");
796 token = statement(token, &iterator);
798 stmt->iterator_pre_condition = expr;
799 stmt->iterator_post_condition = expr;
800 stmt->iterator_statement = iterator;
801 end_iterator(stmt);
803 return token;
806 struct token *parse_do_statement(struct token *token, struct statement *stmt)
808 struct expression *expr;
809 struct statement *iterator;
811 start_iterator(stmt);
812 token = statement(token->next, &iterator);
813 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
814 token = token->next;
815 else
816 warn(token->pos, "expected 'while' after 'do'");
817 token = parens_expression(token, &expr, "after 'do-while'");
819 stmt->iterator_post_condition = expr;
820 stmt->iterator_statement = iterator;
821 end_iterator(stmt);
823 return expect(token, ';', "after statement");
826 static struct token *statement(struct token *token, struct statement **tree)
828 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
830 *tree = stmt;
831 if (token_type(token) == TOKEN_IDENT) {
832 if (token->ident == &if_ident) {
833 stmt->type = STMT_IF;
834 token = parens_expression(token->next, &stmt->if_conditional, "after if");
835 token = statement(token, &stmt->if_true);
836 if (token_type(token) != TOKEN_IDENT)
837 return token;
838 if (token->ident != &else_ident)
839 return token;
840 return statement(token->next, &stmt->if_false);
843 if (token->ident == &return_ident)
844 return parse_return_statement(token, stmt);
846 if (token->ident == &break_ident || token->ident == &continue_ident) {
847 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
848 stmt->type = STMT_GOTO;
849 stmt->goto_label = target;
850 if (!target)
851 warn(stmt->pos, "break/continue not in iterator scope");
852 return expect(token->next, ';', "at end of statement");
854 if (token->ident == &default_ident) {
855 token = token->next;
856 goto default_statement;
858 if (token->ident == &case_ident) {
859 token = parse_expression(token->next, &stmt->case_expression);
860 if (match_op(token, SPECIAL_ELLIPSIS))
861 token = parse_expression(token->next, &stmt->case_to);
862 default_statement:
863 stmt->type = STMT_CASE;
864 token = expect(token, ':', "after default/case");
865 add_case_statement(stmt);
866 return statement(token, &stmt->case_statement);
868 if (token->ident == &switch_ident) {
869 stmt->type = STMT_SWITCH;
870 start_switch(stmt);
871 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
872 token = statement(token, &stmt->switch_statement);
873 end_switch(stmt);
874 return token;
876 if (token->ident == &for_ident)
877 return parse_for_statement(token, stmt);
879 if (token->ident == &while_ident)
880 return parse_while_statement(token, stmt);
882 if (token->ident == &do_ident)
883 return parse_do_statement(token, stmt);
885 if (token->ident == &goto_ident) {
886 stmt->type = STMT_GOTO;
887 token = token->next;
888 if (match_op(token, '*')) {
889 token = parse_expression(token->next, &stmt->goto_expression);
890 } else if (token_type(token) == TOKEN_IDENT) {
891 stmt->goto_label = label_symbol(token);
892 token = token->next;
893 } else {
894 warn(token->pos, "Expected identifier or goto expression");
896 return expect(token, ';', "at end of statement");
898 if (token->ident == &asm_ident || token->ident == &__asm___ident || token->ident == &__asm_ident) {
899 struct expression *expr;
900 stmt->type = STMT_ASM;
901 token = token->next;
902 if (token_type(token) == TOKEN_IDENT) {
903 if (token->ident == &__volatile___ident || token->ident == &volatile_ident)
904 token = token->next;
906 token = expect(token, '(', "after asm");
907 token = parse_expression(token->next, &expr);
908 if (match_op(token, ':'))
909 token = parse_asm_operands(token, stmt);
910 if (match_op(token, ':'))
911 token = parse_asm_operands(token, stmt);
912 if (match_op(token, ':'))
913 token = parse_asm_clobbers(token, stmt);
914 token = expect(token, ')', "after asm");
915 return expect(token, ';', "at end of asm-statement");
917 if (match_op(token->next, ':')) {
918 stmt->type = STMT_LABEL;
919 stmt->label_identifier = label_symbol(token);
920 return statement(token->next->next, &stmt->label_statement);
924 if (match_op(token, '{')) {
925 stmt->type = STMT_COMPOUND;
926 start_symbol_scope();
927 token = compound_statement(token->next, stmt);
928 end_symbol_scope();
930 return expect(token, '}', "at end of compound statement");
933 stmt->type = STMT_EXPRESSION;
934 return expression_statement(token, &stmt->expression);
937 struct token * statement_list(struct token *token, struct statement_list **list)
939 for (;;) {
940 struct statement * stmt;
941 if (eof_token(token))
942 break;
943 if (match_op(token, '}'))
944 break;
945 token = statement(token, &stmt);
946 add_statement(list, stmt);
948 return token;
951 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
953 struct symbol_list **list = &fn->arguments;
954 for (;;) {
955 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
957 if (match_op(token, SPECIAL_ELLIPSIS)) {
958 fn->variadic = 1;
959 token = token->next;
960 break;
963 if (!lookup_type(token)) {
964 warn(token->pos, "non-ANSI parameter list");
965 break;
967 token = parameter_declaration(token, &sym);
968 /* Special case: (void) */
969 if (!*list && !sym->ident && sym->ctype.base_type == &void_ctype)
970 break;
971 add_symbol(list, sym);
972 if (!match_op(token, ','))
973 break;
974 token = token->next;
977 return token;
980 struct token *compound_statement(struct token *token, struct statement *stmt)
982 while (!eof_token(token)) {
983 if (!lookup_type(token))
984 break;
985 token = external_declaration(token, &stmt->syms);
987 token = statement_list(token, &stmt->stmts);
988 return token;
991 static struct expression *identifier_expression(struct token *token)
993 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
994 expr->expr_ident = token->ident;
995 return expr;
998 static struct expression *index_expression(struct expression *from, struct expression *to)
1000 int idx_from, idx_to;
1001 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1003 idx_from = get_expression_value(from);
1004 idx_to = idx_from;
1005 if (to) {
1006 idx_to = get_expression_value(to);
1007 if (idx_to < idx_from || idx_from < 0)
1008 warn(from->pos, "nonsense array initializer index range");
1010 expr->idx_from = idx_from;
1011 expr->idx_to = idx_to;
1012 return expr;
1015 static struct token *initializer_list(struct expression_list **list, struct token *token)
1017 for (;;) {
1018 struct token *next = token->next;
1019 struct expression *expr;
1021 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
1022 add_expression(list, identifier_expression(next));
1023 token = next->next->next;
1024 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1025 add_expression(list, identifier_expression(token));
1026 token = next->next;
1027 } else if (match_op(token, '[')) {
1028 struct expression *from = NULL, *to = NULL;
1029 token = constant_expression(token->next, &from);
1030 if (match_op(token, SPECIAL_ELLIPSIS))
1031 token = constant_expression(token->next, &to);
1032 add_expression(list, index_expression(from, to));
1033 token = expect(token, ']', "at end of initializer index");
1034 token = expect(token, '=', "at end of initializer index");
1037 expr = NULL;
1038 token = initializer(&expr, token);
1039 if (!expr)
1040 break;
1041 add_expression(list, expr);
1042 if (!match_op(token, ','))
1043 break;
1044 token = token->next;
1046 return token;
1049 struct token *initializer(struct expression **tree, struct token *token)
1051 if (match_op(token, '{')) {
1052 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1053 *tree = expr;
1054 token = initializer_list(&expr->expr_list, token->next);
1055 return expect(token, '}', "at end of initializer");
1057 return assignment_expression(token, tree);
1060 static void declare_argument(struct symbol *sym, struct symbol *fn)
1062 if (!sym->ident) {
1063 warn(sym->pos, "no identifier for function argument");
1064 return;
1066 bind_symbol(sym, sym->ident, NS_SYMBOL);
1069 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1070 struct symbol_list **list)
1072 struct symbol *base_type = decl->ctype.base_type;
1073 struct statement *stmt;
1074 struct symbol *arg;
1076 function_symbol_list = &decl->symbol_list;
1077 if (decl->ctype.modifiers & MOD_EXTERN) {
1078 if (!(decl->ctype.modifiers & MOD_INLINE))
1079 warn(decl->pos, "function with external linkage has definition");
1081 if (!(decl->ctype.modifiers & MOD_STATIC))
1082 decl->ctype.modifiers |= MOD_EXTERN;
1084 stmt = start_function(decl);
1086 base_type->stmt = stmt;
1087 FOR_EACH_PTR (base_type->arguments, arg) {
1088 declare_argument(arg, base_type);
1089 } END_FOR_EACH_PTR;
1091 token = compound_statement(token->next, stmt);
1093 end_function(decl);
1094 if (!(decl->ctype.modifiers & MOD_INLINE))
1095 add_symbol(list, decl);
1096 check_declaration(decl);
1097 function_symbol_list = NULL;
1098 return expect(token, '}', "at end of function");
1101 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1103 struct ident *ident = NULL;
1104 struct symbol *decl;
1105 struct ctype ctype = { 0, };
1106 struct symbol *base_type;
1107 int is_typedef;
1109 /* Parse declaration-specifiers, if any */
1110 token = declaration_specifiers(token, &ctype, 0);
1111 decl = alloc_symbol(token->pos, SYM_NODE);
1112 decl->ctype = ctype;
1113 token = pointer(token, &decl->ctype);
1114 token = declarator(token, &decl, &ident);
1116 /* Just a type declaration? */
1117 if (!ident)
1118 return expect(token, ';', "end of type declaration");
1120 decl->ident = ident;
1122 /* type define declaration? */
1123 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1125 /* Typedef's don't have meaningful storage */
1126 if (is_typedef) {
1127 ctype.modifiers &= ~MOD_STORAGE;
1128 decl->ctype.modifiers &= ~MOD_STORAGE;
1131 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1133 base_type = decl->ctype.base_type;
1134 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1135 if (match_op(token, '{'))
1136 return parse_function_body(token, decl, list);
1138 if (!(decl->ctype.modifiers & MOD_STATIC))
1139 decl->ctype.modifiers |= MOD_EXTERN;
1142 for (;;) {
1143 if (!is_typedef && match_op(token, '=')) {
1144 if (decl->ctype.modifiers & MOD_EXTERN) {
1145 warn(decl->pos, "symbol with external linkage has initializer");
1146 decl->ctype.modifiers &= ~MOD_EXTERN;
1148 token = initializer(&decl->initializer, token->next);
1150 if (!is_typedef) {
1151 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1152 add_symbol(list, decl);
1153 if (function_symbol_list)
1154 fn_local_symbol(decl);
1157 check_declaration(decl);
1159 if (!match_op(token, ','))
1160 break;
1162 ident = NULL;
1163 decl = alloc_symbol(token->pos, SYM_NODE);
1164 decl->ctype = ctype;
1165 token = pointer(token, &decl->ctype);
1166 token = declarator(token->next, &decl, &ident);
1167 if (!ident) {
1168 warn(token->pos, "expected identifier name in type definition");
1169 return token;
1172 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1174 /* Function declarations are automatically extern unless specifically static */
1175 base_type = decl->ctype.base_type;
1176 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1177 if (!(decl->ctype.modifiers & MOD_STATIC))
1178 decl->ctype.modifiers |= MOD_EXTERN;
1181 return expect(token, ';', "at end of declaration");
1184 void translation_unit(struct token *token, struct symbol_list **list)
1186 while (!eof_token(token))
1187 token = external_declaration(token, list);
1188 // They aren't needed any more
1189 clear_token_alloc();