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
24 #include "expression.h"
26 struct statement
*alloc_statement(struct position pos
, int type
)
28 struct statement
*stmt
= __alloc_statement(0);
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
;
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
);
52 sym
= alloc_symbol(token
->pos
, type
);
53 sym
->ident
= token
->ident
;
54 bind_symbol(sym
, token
->ident
, ns
);
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
*))
76 if (token_type(token
) == TOKEN_IDENT
) {
77 sym
= lookup_or_create_symbol(ns
, type
, token
);
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");
87 // private struct/union/enum type
88 if (!match_op(token
, '{')) {
89 warn(token
->pos
, "expected declaration");
90 ctype
->base_type
= &bad_type
;
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
)
113 while (token_type(token
) == TOKEN_IDENT
) {
114 struct token
*next
= token
->next
;
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
;
129 if (!match_op(token
, ','))
132 nextval
= nextval
+ 1;
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
)
146 if (!match_op(token
, '(')) {
147 warn(token
->pos
, "expected '(' after typeof");
150 if (lookup_type(token
->next
)) {
151 token
= typename(token
->next
, &sym
);
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;
169 if (match_string_ident(attribute
, "aligned")) {
170 ctype
->alignment
= get_expression_value(expr
);
173 if (match_string_ident(attribute
, "nocast")) {
174 ctype
->modifiers
|= MOD_NOCAST
;
177 if (match_string_ident(attribute
, "noderef")) {
178 ctype
->modifiers
|= MOD_NODEREF
;
181 if (match_string_ident(attribute
, "address_space")) {
182 ctype
->as
= get_expression_value(expr
);
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
);
190 warn(expr
->pos
, "nonsense attribute types");
193 ctype
->contextmask
|= mask
;
194 ctype
->context
|= value
;
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");
208 struct ident
*attribute_name
;
209 struct expression
*attribute_expr
;
211 if (eof_token(token
))
213 if (match_op(token
, ';'))
215 if (token_type(token
) != TOKEN_IDENT
)
217 attribute_name
= token
->ident
;
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
, ','))
228 token
= expect(token
, ')', "after attribute");
229 token
= expect(token
, ')', "after attribute");
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
)) {
250 if (!(spec
& MOD_LONG
)) {
252 if (!(spec
& MOD_SHORT
)) {
254 if (!(spec
& MOD_CHAR
))
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
;
267 return &double_ctype
;
271 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
273 unsigned long mod
= thistype
->modifiers
;
276 unsigned long old
= ctype
->modifiers
;
277 unsigned long extra
= 0, dup
;
279 if (mod
& old
& MOD_LONG
) {
280 extra
= MOD_LONGLONG
| MOD_LONG
;
284 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
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
;
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
;
309 ctype
->as
= thistype
->as
;
313 static struct token
*declaration_specifiers(struct token
*next
, struct ctype
*ctype
, int qual
)
317 while ( (token
= next
) != NULL
) {
318 struct ctype thistype
;
320 struct symbol
*s
, *type
;
324 if (token_type(token
) != TOKEN_IDENT
)
326 ident
= token
->ident
;
328 s
= lookup_symbol(ident
, NS_TYPEDEF
);
332 mod
= thistype
.modifiers
;
333 if (qual
&& (mod
& ~(MOD_ATTRIBUTE
| MOD_CONST
| MOD_VOLATILE
)))
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
;
352 if (type
!= ctype
->base_type
) {
353 if (ctype
->base_type
) {
354 warn(token
->pos
, "Strange mix of types");
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
;
373 if (ctype
->base_type
== &fp_type
) {
374 ctype
->base_type
= ctype_fp(ctype
->modifiers
& MOD_SPECIFIER
);
375 ctype
->modifiers
&= ~MOD_SPECIFIER
;
381 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
383 struct expression
*expr
= NULL
;
385 token
= parse_expression(token
, &expr
);
387 sym
->array_size
= get_expression_value(expr
);
389 sym
->array_size
= -1;
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
) {
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
);
412 if (token_type(token
) != TOKEN_SPECIAL
)
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
== '(') {
424 struct token
*next
= token
->next
;
425 int fn
= (p
&& *p
) || match_op(next
, ')') || lookup_type(next
);
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
;
437 sym
= indirect(token
->pos
, ctype
, SYM_FN
);
438 token
= parameter_type_list(next
, sym
);
439 token
= expect(token
, ')', "in function declarator");
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
;
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
);
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
;
483 ctype
->modifiers
= modifiers
& MOD_STORAGE
;
484 ctype
->base_type
= base_type
;
487 ctype
->contextmask
= 0;
489 token
= declaration_specifiers(token
->next
, ctype
, 1);
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);
507 struct ident
*ident
= NULL
;
508 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
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
, ','))
521 if (!match_op(token
, ';'))
528 static struct token
*parameter_declaration(struct token
*token
, struct symbol
**tree
)
530 struct ident
*ident
= NULL
;
532 struct ctype ctype
= { 0, };
534 token
= declaration_specifiers(token
, &ctype
, 0);
535 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
538 token
= pointer(token
, &sym
->ctype
);
539 token
= direct_declarator(token
, tree
, &ident
);
543 struct token
*typename(struct token
*token
, struct symbol
**p
)
545 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
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
, ')'))
565 token
= primary_expression(token
->next
, &expr
);
566 token
= parens_expression(token
, &expr
, "in asm parameter");
567 } while (match_op(token
, ','));
571 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
)
573 struct expression
*expr
;
576 token
= primary_expression(token
->next
, &expr
);
577 } while (match_op(token
, ','));
581 /* Make a statement out of an expression */
582 static struct statement
*make_statement(struct expression
*expr
)
584 struct statement
*stmt
;
588 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
589 stmt
->expression
= expr
;
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)
615 static void start_switch(struct statement
*stmt
)
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)
633 struct token
*statement(struct token
*token
, struct statement
**tree
)
635 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
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
)
645 if (token
->ident
!= &else_ident
)
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
;
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
) {
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
);
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
;
677 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
678 token
= statement(token
, &stmt
->switch_statement
);
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
;
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
;
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
)
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
;
736 return expect(token
, ';', "after statement");
738 if (token
->ident
== &goto_ident
) {
739 stmt
->type
= STMT_GOTO
;
741 if (token_type(token
) == TOKEN_IDENT
) {
742 stmt
->goto_label
= label_symbol(token
);
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
;
752 if (token_type(token
) == TOKEN_IDENT
) {
753 if (token
->ident
== &__volatile___ident
|| token
->ident
== &volatile_ident
)
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
);
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
)
790 struct statement
* stmt
;
791 if (eof_token(token
))
793 if (match_op(token
, '}'))
795 token
= statement(token
, &stmt
);
796 add_statement(list
, stmt
);
801 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
803 struct symbol_list
**list
= &fn
->arguments
;
805 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
807 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
813 token
= parameter_declaration(token
, &sym
);
814 /* Special case: (void) */
815 if (!*list
&& !sym
->ident
&& sym
->ctype
.base_type
== &void_ctype
)
817 add_symbol(list
, sym
);
818 if (!match_op(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
))
833 token
= external_declaration(token
, &stmt
->syms
);
835 token
= statement_list(token
, &stmt
->stmts
);
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
;
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
);
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
;
863 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
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
));
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");
886 token
= initializer(&expr
, token
);
889 add_expression(list
, expr
);
890 if (!match_op(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
);
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
;
913 warn(decl
->pos
, "no identifier for function argument");
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
;
923 struct ctype ctype
= { 0, };
924 struct symbol
*base_type
;
927 /* Parse declaration-specifiers, if any */
928 token
= declaration_specifiers(token
, &ctype
, 0);
929 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
931 token
= pointer(token
, &decl
->ctype
);
932 token
= declarator(token
, &decl
, &ident
);
934 /* Just a type declaration? */
936 return expect(token
, ';', "end of type declaration");
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
;
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
, ','))
983 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
985 token
= pointer(token
, &decl
->ctype
);
986 token
= declarator(token
->next
, &decl
, &ident
);
988 warn(token
->pos
, "expected identifier name in type definition");
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();