Don't warn about signedness for hax/octal constants. They are commonly
[smatch.git] / parse.c
bloba1c9c6ee9988f189fcf034e926dc50bf4b133f62
1 /*
2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
9 */
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
19 #include "lib.h"
20 #include "token.h"
21 #include "parse.h"
22 #include "symbol.h"
23 #include "scope.h"
24 #include "expression.h"
26 struct statement *alloc_statement(struct position pos, int type)
28 struct statement *stmt = __alloc_statement(0);
29 stmt->type = type;
30 stmt->pos = pos;
31 return stmt;
34 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
36 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
38 struct symbol *sym = alloc_symbol(pos, type);
40 sym->ctype.base_type = ctype->base_type;
41 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
43 ctype->base_type = sym;
44 ctype->modifiers &= MOD_STORAGE;
45 return sym;
48 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
50 struct symbol *sym = lookup_symbol(token->ident, ns);
51 if (!sym) {
52 sym = alloc_symbol(token->pos, type);
53 sym->ident = token->ident;
54 bind_symbol(sym, token->ident, ns);
56 return sym;
60 * NOTE! NS_LABEL is not just a different namespace,
61 * it also ends up using function scope instead of the
62 * regular symbol scope.
64 static struct symbol *label_symbol(struct token *token)
66 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
69 struct token *struct_union_enum_specifier(enum namespace ns, enum type type,
70 struct token *token, struct ctype *ctype,
71 struct token *(*parse)(struct token *, struct symbol *))
73 struct symbol *sym;
75 ctype->modifiers = 0;
76 if (token_type(token) == TOKEN_IDENT) {
77 sym = lookup_or_create_symbol(ns, type, token);
78 token = token->next;
79 ctype->base_type = sym;
80 if (match_op(token, '{')) {
81 token = parse(token->next, sym);
82 token = expect(token, '}', "at end of struct-union-enum-specifier");
84 return token;
87 // private struct/union/enum type
88 if (!match_op(token, '{')) {
89 warn(token->pos, "expected declaration");
90 ctype->base_type = &bad_type;
91 return token;
94 sym = alloc_symbol(token->pos, type);
95 token = parse(token->next, sym);
96 ctype->base_type = sym;
97 return expect(token, '}', "at end of specifier");
100 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
102 return struct_declaration_list(token, &sym->symbol_list);
105 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
107 return struct_union_enum_specifier(NS_STRUCT, type, token, ctype, parse_struct_declaration);
110 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
112 int nextval = 0;
113 while (token_type(token) == TOKEN_IDENT) {
114 struct token *next = token->next;
115 struct symbol *sym;
117 sym = alloc_symbol(token->pos, SYM_NODE);
118 bind_symbol(sym, token->ident, NS_SYMBOL);
119 sym->ctype.base_type = parent;
121 if (match_op(next, '=')) {
122 struct expression *expr;
123 next = constant_expression(next->next, &expr);
124 nextval = get_expression_value(expr);
126 sym->value = nextval;
128 token = next;
129 if (!match_op(token, ','))
130 break;
131 token = token->next;
132 nextval = nextval + 1;
134 return token;
137 struct token *enum_specifier(struct token *token, struct ctype *ctype)
139 return struct_union_enum_specifier(NS_ENUM, SYM_ENUM, token, ctype, parse_enum_declaration);
142 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
144 struct symbol *sym;
146 if (!match_op(token, '(')) {
147 warn(token->pos, "expected '(' after typeof");
148 return token;
150 if (lookup_type(token->next)) {
151 token = typename(token->next, &sym);
152 *ctype = sym->ctype;
153 } else {
154 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
155 token = parse_expression(token->next, &typeof_sym->initializer);
157 ctype->modifiers = 0;
158 ctype->base_type = typeof_sym;
160 return expect(token, ')', "after typeof");
163 static void handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
165 if (match_string_ident(attribute, "packed")) {
166 ctype->alignment = 1;
167 return;
169 if (match_string_ident(attribute, "aligned")) {
170 ctype->alignment = get_expression_value(expr);
171 return;
173 if (match_string_ident(attribute, "nocast")) {
174 ctype->modifiers |= MOD_NOCAST;
175 return;
177 if (match_string_ident(attribute, "noderef")) {
178 ctype->modifiers |= MOD_NODEREF;
179 return;
181 if (match_string_ident(attribute, "address_space")) {
182 ctype->as = get_expression_value(expr);
183 return;
185 if (match_string_ident(attribute, "context")) {
186 if (expr->type == EXPR_COMMA) {
187 int mask = get_expression_value(expr->left);
188 int value = get_expression_value(expr->right);
189 if (value & ~mask) {
190 warn(expr->pos, "nonsense attribute types");
191 return;
193 ctype->contextmask |= mask;
194 ctype->context |= value;
195 return;
201 struct token *attribute_specifier(struct token *token, struct ctype *ctype)
203 ctype->modifiers = 0;
204 token = expect(token, '(', "after attribute");
205 token = expect(token, '(', "after attribute");
207 for (;;) {
208 struct ident *attribute_name;
209 struct expression *attribute_expr;
211 if (eof_token(token))
212 break;
213 if (match_op(token, ';'))
214 break;
215 if (token_type(token) != TOKEN_IDENT)
216 break;
217 attribute_name = token->ident;
218 token = token->next;
219 attribute_expr = NULL;
220 if (match_op(token, '('))
221 token = parens_expression(token, &attribute_expr, "in attribute");
222 handle_attribute(ctype, attribute_name, attribute_expr);
223 if (!match_op(token, ','))
224 break;
225 token = token->next;
228 token = expect(token, ')', "after attribute");
229 token = expect(token, ')', "after attribute");
230 return token;
233 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
234 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED)
236 struct symbol * ctype_integer(unsigned int spec)
238 static struct symbol *const integer_ctypes[][2] = {
239 { &llong_ctype, &ullong_ctype },
240 { &long_ctype, &ulong_ctype },
241 { &short_ctype, &ushort_ctype },
242 { &char_ctype, &uchar_ctype },
243 { &int_ctype, &uint_ctype },
245 struct symbol *const (*ctype)[2];
247 ctype = integer_ctypes;
248 if (!(spec & MOD_LONGLONG)) {
249 ctype++;
250 if (!(spec & MOD_LONG)) {
251 ctype++;
252 if (!(spec & MOD_SHORT)) {
253 ctype++;
254 if (!(spec & MOD_CHAR))
255 ctype++;
259 return ctype[0][(spec & MOD_UNSIGNED) != 0];
262 struct symbol * ctype_fp(unsigned int spec)
264 if (spec & MOD_LONGLONG)
265 return &ldouble_ctype;
266 if (spec & MOD_LONG)
267 return &double_ctype;
268 return &float_ctype;
271 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
273 unsigned long mod = thistype->modifiers;
275 if (mod) {
276 unsigned long old = ctype->modifiers;
277 unsigned long extra = 0, dup;
279 if (mod & old & MOD_LONG) {
280 extra = MOD_LONGLONG | MOD_LONG;
281 mod &= ~MOD_LONG;
282 old &= ~MOD_LONG;
284 dup = (mod & old) | (extra & old) | (extra & mod);
285 if (dup)
286 warn(pos, "Just how %sdo you want this type to be?",
287 modifier_string(dup));
288 ctype->modifiers = old | mod | extra;
291 /* Context mask and value */
292 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
293 warn(pos, "inconsistend attribute types");
294 thistype->context = 0;
295 thistype->contextmask = 0;
297 ctype->context |= thistype->context;
298 ctype->contextmask |= thistype->contextmask;
300 /* Alignment */
301 if (thistype->alignment & (thistype->alignment-1)) {
302 warn(pos, "I don't like non-power-of-2 alignments");
303 thistype->alignment = 0;
305 if (thistype->alignment > ctype->alignment)
306 ctype->alignment = thistype->alignment;
308 /* Address space */
309 ctype->as = thistype->as;
313 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
315 struct token *token;
317 while ( (token = next) != NULL ) {
318 struct ctype thistype;
319 struct ident *ident;
320 struct symbol *s, *type;
321 unsigned long mod;
323 next = token->next;
324 if (token_type(token) != TOKEN_IDENT)
325 break;
326 ident = token->ident;
328 s = lookup_symbol(ident, NS_TYPEDEF);
329 if (!s)
330 break;
331 thistype = s->ctype;
332 mod = thistype.modifiers;
333 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
334 break;
335 if (mod & MOD_SPECIALBITS) {
336 if (mod & MOD_STRUCTOF)
337 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
338 else if (mod & MOD_UNIONOF)
339 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
340 else if (mod & MOD_ENUMOF)
341 next = enum_specifier(next, &thistype);
342 else if (mod & MOD_ATTRIBUTE)
343 next = attribute_specifier(next, &thistype);
344 else if (mod & MOD_TYPEOF)
345 next = typeof_specifier(next, &thistype);
346 mod = thistype.modifiers;
348 type = thistype.base_type;
349 if (type) {
350 if (qual)
351 break;
352 if (type != ctype->base_type) {
353 if (ctype->base_type) {
354 warn(token->pos, "Strange mix of types");
355 continue;
357 ctype->base_type = type;
361 apply_ctype(token->pos, &thistype, ctype);
364 /* Turn the "virtual types" into real types with real sizes etc */
365 if (!ctype->base_type && (ctype->modifiers & MOD_SPECIFIER))
366 ctype->base_type = &int_type;
368 if (ctype->base_type == &int_type) {
369 ctype->base_type = ctype_integer(ctype->modifiers & MOD_SPECIFIER);
370 ctype->modifiers &= ~MOD_SPECIFIER;
371 return token;
373 if (ctype->base_type == &fp_type) {
374 ctype->base_type = ctype_fp(ctype->modifiers & MOD_SPECIFIER);
375 ctype->modifiers &= ~MOD_SPECIFIER;
376 return token;
378 return token;
381 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
383 struct expression *expr = NULL;
385 token = parse_expression(token, &expr);
386 if (expr)
387 sym->array_size = get_expression_value(expr);
388 else
389 sym->array_size = -1;
390 return token;
393 static struct token *parameter_type_list(struct token *, struct symbol *);
394 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
396 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
398 struct ctype *ctype = &(*tree)->ctype;
400 if (p && token_type(token) == TOKEN_IDENT) {
401 *p = token->ident;
402 token = token->next;
405 for (;;) {
406 if (match_ident(token, &__attribute___ident) || match_ident(token, &__attribute_ident)) {
407 struct ctype thistype = { 0, };
408 token = attribute_specifier(token->next, &thistype);
409 apply_ctype(token->pos, &thistype, ctype);
410 continue;
412 if (token_type(token) != TOKEN_SPECIAL)
413 return token;
416 * This can be either a parameter list or a grouping.
417 * For the direct (non-abstract) case, we know if must be
418 * a paramter list if we already saw the identifier.
419 * For the abstract case, we know if must be a parameter
420 * list if it is empty or starts with a type.
422 if (token->special == '(') {
423 struct symbol *sym;
424 struct token *next = token->next;
425 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
427 if (!fn) {
428 struct symbol *base_type = ctype->base_type;
429 token = declarator(next, tree, p);
430 token = expect(token, ')', "in nested declarator");
431 while (ctype->base_type != base_type)
432 ctype = &ctype->base_type->ctype;
433 p = NULL;
434 continue;
437 sym = indirect(token->pos, ctype, SYM_FN);
438 token = parameter_type_list(next, sym);
439 token = expect(token, ')', "in function declarator");
440 continue;
442 if (token->special == '[') {
443 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
444 token = abstract_array_declarator(token->next, array);
445 token = expect(token, ']', "in abstract_array_declarator");
446 ctype = &array->ctype;
447 continue;
449 if (token->special == ':') {
450 struct symbol *bitfield;
451 struct expression *expr;
452 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
453 token = conditional_expression(token->next, &expr);
454 bitfield->fieldwidth = get_expression_value(expr);
455 continue;
457 break;
459 if (p) {
460 (*tree)->ident = *p;
462 return token;
465 static struct token *pointer(struct token *token, struct ctype *ctype)
467 unsigned long modifiers;
468 struct symbol *base_type;
470 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
471 base_type = ctype->base_type;
472 ctype->modifiers = modifiers;
474 while (match_op(token,'*')) {
475 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
476 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
477 ptr->ctype.as = ctype->as;
478 ptr->ctype.context = ctype->context;
479 ptr->ctype.contextmask = ctype->contextmask;
480 ptr->ctype.base_type = base_type;
482 base_type = ptr;
483 ctype->modifiers = modifiers & MOD_STORAGE;
484 ctype->base_type = base_type;
485 ctype->as = 0;
486 ctype->context = 0;
487 ctype->contextmask = 0;
489 token = declaration_specifiers(token->next, ctype, 1);
491 return token;
494 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
496 token = pointer(token, &(*tree)->ctype);
497 return direct_declarator(token, tree, p);
500 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
502 while (!match_op(token, '}')) {
503 struct ctype ctype = {0, };
505 token = declaration_specifiers(token, &ctype, 0);
506 for (;;) {
507 struct ident *ident = NULL;
508 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
509 decl->ctype = ctype;
510 token = pointer(token, &decl->ctype);
511 token = direct_declarator(token, &decl, &ident);
512 if (match_op(token, ':')) {
513 struct expression *expr;
514 token = parse_expression(token->next, &expr);
516 add_symbol(list, decl);
517 if (!match_op(token, ','))
518 break;
519 token = token->next;
521 if (!match_op(token, ';'))
522 break;
523 token = token->next;
525 return token;
528 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
530 struct ident *ident = NULL;
531 struct symbol *sym;
532 struct ctype ctype = { 0, };
534 token = declaration_specifiers(token, &ctype, 0);
535 sym = alloc_symbol(token->pos, SYM_NODE);
536 sym->ctype = ctype;
537 *tree = sym;
538 token = pointer(token, &sym->ctype);
539 token = direct_declarator(token, tree, &ident);
540 return token;
543 struct token *typename(struct token *token, struct symbol **p)
545 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
546 *p = sym;
547 token = declaration_specifiers(token, &sym->ctype, 0);
548 return declarator(token, &sym, NULL);
551 struct token *expression_statement(struct token *token, struct expression **tree)
553 token = parse_expression(token, tree);
554 return expect(token, ';', "at end of statement");
557 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
559 struct expression *expr;
561 /* Allow empty operands */
562 if (match_op(token->next, ':') || match_op(token->next, ')'))
563 return token->next;
564 do {
565 token = primary_expression(token->next, &expr);
566 token = parens_expression(token, &expr, "in asm parameter");
567 } while (match_op(token, ','));
568 return token;
571 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
573 struct expression *expr;
575 do {
576 token = primary_expression(token->next, &expr);
577 } while (match_op(token, ','));
578 return token;
581 /* Make a statement out of an expression */
582 static struct statement *make_statement(struct expression *expr)
584 struct statement *stmt;
586 if (!expr)
587 return NULL;
588 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
589 stmt->expression = expr;
590 return stmt;
593 static void start_iterator(struct statement *stmt)
595 struct symbol *cont, *brk;
597 start_symbol_scope();
598 cont = alloc_symbol(stmt->pos, SYM_NODE);
599 cont->ident = &continue_ident;
600 bind_symbol(cont, &continue_ident, NS_ITERATOR);
601 brk = alloc_symbol(stmt->pos, SYM_NODE);
602 brk->ident = &break_ident;
603 bind_symbol(brk, &break_ident, NS_ITERATOR);
605 stmt->type = STMT_ITERATOR;
606 stmt->iterator_break = brk;
607 stmt->iterator_continue = cont;
610 static void end_iterator(void)
612 end_symbol_scope();
615 static void start_switch(struct statement *stmt)
617 struct symbol *brk;
619 start_symbol_scope();
620 brk = alloc_symbol(stmt->pos, SYM_NODE);
621 brk->ident = &break_ident;
622 bind_symbol(brk, &break_ident, NS_ITERATOR);
624 stmt->type = STMT_SWITCH;
625 stmt->switch_break = brk;
628 static void end_switch(void)
630 end_symbol_scope();
633 struct token *statement(struct token *token, struct statement **tree)
635 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
637 *tree = stmt;
638 if (token_type(token) == TOKEN_IDENT) {
639 if (token->ident == &if_ident) {
640 stmt->type = STMT_IF;
641 token = parens_expression(token->next, &stmt->if_conditional, "after if");
642 token = statement(token, &stmt->if_true);
643 if (token_type(token) != TOKEN_IDENT)
644 return token;
645 if (token->ident != &else_ident)
646 return token;
647 return statement(token->next, &stmt->if_false);
649 if (token->ident == &return_ident) {
650 stmt->type = STMT_RETURN;
651 return expression_statement(token->next, &stmt->expression);
653 if (token->ident == &break_ident || token->ident == &continue_ident) {
654 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
655 stmt->type = STMT_GOTO;
656 stmt->goto_label = target;
657 if (!target)
658 warn(stmt->pos, "break/continue not in iterator scope");
659 return expect(token->next, ';', "at end of statement");
661 if (token->ident == &default_ident) {
662 token = token->next;
663 goto default_statement;
665 if (token->ident == &case_ident) {
666 token = parse_expression(token->next, &stmt->case_expression);
667 if (match_op(token, SPECIAL_ELLIPSIS))
668 token = parse_expression(token->next, &stmt->case_to);
669 default_statement:
670 stmt->type = STMT_CASE;
671 token = expect(token, ':', "after default/case");
672 return statement(token, &stmt->case_statement);
674 if (token->ident == &switch_ident) {
675 stmt->type = STMT_SWITCH;
676 start_switch(stmt);
677 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
678 token = statement(token, &stmt->switch_statement);
679 end_switch();
680 return token;
682 if (token->ident == &for_ident) {
683 struct expression *e1, *e2, *e3;
684 struct statement *iterator;
686 start_iterator(stmt);
687 token = expect(token->next, '(', "after 'for'");
688 token = parse_expression(token, &e1);
689 token = expect(token, ';', "in 'for'");
690 token = parse_expression(token, &e2);
691 token = expect(token, ';', "in 'for'");
692 token = parse_expression(token, &e3);
693 token = expect(token, ')', "in 'for'");
694 token = statement(token, &iterator);
696 stmt->iterator_pre_statement = make_statement(e1);
697 stmt->iterator_pre_condition = e2;
698 stmt->iterator_post_statement = make_statement(e3);
699 stmt->iterator_post_condition = e2;
700 stmt->iterator_statement = iterator;
701 end_iterator();
703 return token;
705 if (token->ident == &while_ident) {
706 struct expression *expr;
707 struct statement *iterator;
709 start_iterator(stmt);
710 token = parens_expression(token->next, &expr, "after 'while'");
711 token = statement(token, &iterator);
713 stmt->iterator_pre_condition = expr;
714 stmt->iterator_post_condition = expr;
715 stmt->iterator_statement = iterator;
716 end_iterator();
718 return token;
720 if (token->ident == &do_ident) {
721 struct expression *expr;
722 struct statement *iterator;
724 start_iterator(stmt);
725 token = statement(token->next, &iterator);
726 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
727 token = token->next;
728 else
729 warn(token->pos, "expected 'while' after 'do'");
730 token = parens_expression(token, &expr, "after 'do-while'");
732 stmt->iterator_post_condition = expr;
733 stmt->iterator_statement = iterator;
734 end_iterator();
736 return expect(token, ';', "after statement");
738 if (token->ident == &goto_ident) {
739 stmt->type = STMT_GOTO;
740 token = token->next;
741 if (token_type(token) == TOKEN_IDENT) {
742 stmt->goto_label = label_symbol(token);
743 token = token->next;
744 } else
745 warn(token->pos, "invalid label");
746 return expect(token, ';', "at end of statement");
748 if (token->ident == &asm_ident || token->ident == &__asm___ident || token->ident == &__asm_ident) {
749 struct expression *expr;
750 stmt->type = STMT_ASM;
751 token = token->next;
752 if (token_type(token) == TOKEN_IDENT) {
753 if (token->ident == &__volatile___ident || token->ident == &volatile_ident)
754 token = token->next;
756 token = expect(token, '(', "after asm");
757 token = parse_expression(token->next, &expr);
758 if (match_op(token, ':'))
759 token = parse_asm_operands(token, stmt);
760 if (match_op(token, ':'))
761 token = parse_asm_operands(token, stmt);
762 if (match_op(token, ':'))
763 token = parse_asm_clobbers(token, stmt);
764 token = expect(token, ')', "after asm");
765 return expect(token, ';', "at end of asm-statement");
767 if (match_op(token->next, ':')) {
768 stmt->type = STMT_LABEL;
769 stmt->label_identifier = label_symbol(token);
770 return statement(token->next->next, &stmt->label_statement);
774 if (match_op(token, '{')) {
775 stmt->type = STMT_COMPOUND;
776 start_symbol_scope();
777 token = compound_statement(token->next, stmt);
778 end_symbol_scope();
780 return expect(token, '}', "at end of compound statement");
783 stmt->type = STMT_EXPRESSION;
784 return expression_statement(token, &stmt->expression);
787 struct token * statement_list(struct token *token, struct statement_list **list)
789 for (;;) {
790 struct statement * stmt;
791 if (eof_token(token))
792 break;
793 if (match_op(token, '}'))
794 break;
795 token = statement(token, &stmt);
796 add_statement(list, stmt);
798 return token;
801 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
803 struct symbol_list **list = &fn->arguments;
804 for (;;) {
805 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
807 if (match_op(token, SPECIAL_ELLIPSIS)) {
808 fn->variadic = 1;
809 token = token->next;
810 break;
813 token = parameter_declaration(token, &sym);
814 /* Special case: (void) */
815 if (!*list && !sym->ident && sym->ctype.base_type == &void_ctype)
816 break;
817 add_symbol(list, sym);
818 if (!match_op(token, ','))
819 break;
820 token = token->next;
823 return token;
826 static struct token *external_declaration(struct token *token, struct symbol_list **list);
828 struct token *compound_statement(struct token *token, struct statement *stmt)
830 while (!eof_token(token)) {
831 if (!lookup_type(token))
832 break;
833 token = external_declaration(token, &stmt->syms);
835 token = statement_list(token, &stmt->stmts);
836 return token;
839 static struct expression *identifier_expression(struct token *token)
841 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
842 expr->expr_ident = token->ident;
843 return expr;
846 static struct expression *index_expression(struct expression *from, struct expression *to)
848 int idx_from, idx_to;
849 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
851 idx_from = get_expression_value(from);
852 idx_to = idx_from;
853 if (to) {
854 idx_to = get_expression_value(to);
855 if (idx_to < idx_from || idx_from < 0)
856 warn(from->pos, "nonsense array initializer index range");
858 expr->idx_from = idx_from;
859 expr->idx_to = idx_to;
860 return expr;
863 static struct token *initializer_list(struct expression_list **list, struct token *token)
865 for (;;) {
866 struct token *next = token->next;
867 struct expression *expr;
869 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
870 add_expression(list, identifier_expression(next));
871 token = next->next->next;
872 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
873 add_expression(list, identifier_expression(token));
874 token = next->next;
875 } else if (match_op(token, '[')) {
876 struct expression *from = NULL, *to = NULL;
877 token = constant_expression(token->next, &from);
878 if (match_op(token, SPECIAL_ELLIPSIS))
879 token = constant_expression(token->next, &to);
880 add_expression(list, index_expression(from, to));
881 token = expect(token, ']', "at end of initializer index");
882 token = expect(token, '=', "at end of initializer index");
885 expr = NULL;
886 token = initializer(&expr, token);
887 if (!expr)
888 break;
889 add_expression(list, expr);
890 if (!match_op(token, ','))
891 break;
892 token = token->next;
894 return token;
897 struct token *initializer(struct expression **tree, struct token *token)
899 if (match_op(token, '{')) {
900 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
901 *tree = expr;
902 token = initializer_list(&expr->expr_list, token->next);
903 return expect(token, '}', "at end of initializer");
905 return assignment_expression(token, tree);
908 static void declare_argument(struct symbol *sym, void *data, int flags)
910 struct symbol *decl = data;
912 if (!sym->ident) {
913 warn(decl->pos, "no identifier for function argument");
914 return;
916 bind_symbol(sym, sym->ident, NS_SYMBOL);
919 static struct token *external_declaration(struct token *token, struct symbol_list **list)
921 struct ident *ident = NULL;
922 struct symbol *decl;
923 struct ctype ctype = { 0, };
924 struct symbol *base_type;
925 int is_typedef;
927 /* Parse declaration-specifiers, if any */
928 token = declaration_specifiers(token, &ctype, 0);
929 decl = alloc_symbol(token->pos, SYM_NODE);
930 decl->ctype = ctype;
931 token = pointer(token, &decl->ctype);
932 token = declarator(token, &decl, &ident);
934 /* Just a type declaration? */
935 if (!ident)
936 return expect(token, ';', "end of type declaration");
938 decl->ident = ident;
940 /* type define declaration? */
941 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
942 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
944 base_type = decl->ctype.base_type;
945 if (!is_typedef && base_type && base_type->type == SYM_FN) {
946 if (match_op(token, '{')) {
947 if (decl->ctype.modifiers & MOD_EXTERN) {
948 if (!(decl->ctype.modifiers & MOD_INLINE))
949 warn(decl->pos, "function with external linkage has definition");
951 if (!(decl->ctype.modifiers & MOD_STATIC))
952 decl->ctype.modifiers |= MOD_EXTERN;
953 base_type->stmt = alloc_statement(token->pos, STMT_COMPOUND);
954 start_function_scope();
955 symbol_iterate(base_type->arguments, declare_argument, decl);
956 token = compound_statement(token->next, base_type->stmt);
957 end_function_scope();
958 if (!(decl->ctype.modifiers & MOD_INLINE))
959 add_symbol(list, decl);
960 check_declaration(decl);
961 return expect(token, '}', "at end of function");
963 if (!(decl->ctype.modifiers & MOD_STATIC))
964 decl->ctype.modifiers |= MOD_EXTERN;
967 for (;;) {
968 if (!is_typedef && match_op(token, '=')) {
969 if (decl->ctype.modifiers & MOD_EXTERN) {
970 warn(decl->pos, "symbol with external linkage has initializer");
971 decl->ctype.modifiers &= ~MOD_EXTERN;
973 token = initializer(&decl->initializer, token->next);
975 if (!is_typedef && !(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE)))
976 add_symbol(list, decl);
977 check_declaration(decl);
979 if (!match_op(token, ','))
980 break;
982 ident = NULL;
983 decl = alloc_symbol(token->pos, SYM_NODE);
984 decl->ctype = ctype;
985 token = pointer(token, &decl->ctype);
986 token = declarator(token->next, &decl, &ident);
987 if (!ident) {
988 warn(token->pos, "expected identifier name in type definition");
989 return token;
992 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
994 /* Function declarations are automatically extern unless specifically static */
995 base_type = decl->ctype.base_type;
996 if (!is_typedef && base_type && base_type->type == SYM_FN) {
997 if (!(decl->ctype.modifiers & MOD_STATIC))
998 decl->ctype.modifiers |= MOD_EXTERN;
1001 return expect(token, ';', "at end of declaration");
1004 void translation_unit(struct token *token, struct symbol_list **list)
1006 while (!eof_token(token))
1007 token = external_declaration(token, list);
1008 // They aren't needed any more
1009 clear_token_alloc();