parse.c: Adding va_end().
[smatch.git] / parse.c
blobdfb922f099379c6120eb7096537afa8850a8399c
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-2004 Linus Torvalds
8 * Copyright (C) 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
22 #include "lib.h"
23 #include "allocate.h"
24 #include "token.h"
25 #include "parse.h"
26 #include "symbol.h"
27 #include "scope.h"
28 #include "expression.h"
29 #include "target.h"
31 #define warn_on_mixed (1)
33 static struct symbol_list **function_symbol_list;
34 struct symbol_list *function_computed_target_list;
35 struct statement_list *function_computed_goto_list;
37 static struct token *statement(struct token *token, struct statement **tree);
38 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords);
40 static struct token *struct_specifier(struct token *token, struct ctype *ctype);
41 static struct token *union_specifier(struct token *token, struct ctype *ctype);
42 static struct token *enum_specifier(struct token *token, struct ctype *ctype);
43 static struct token *attribute_specifier(struct token *token, struct ctype *ctype);
44 static struct token *typeof_specifier(struct token *token, struct ctype *ctype);
46 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
47 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
48 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
49 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
50 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
51 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
52 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
53 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
54 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
55 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
56 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
57 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
58 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
59 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
60 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype);
63 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype);
64 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype);
65 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype);
66 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype);
67 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype);
68 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype);
69 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype);
70 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype);
73 static struct symbol_op modifier_op = {
74 .type = KW_MODIFIER,
77 static struct symbol_op qualifier_op = {
78 .type = KW_QUALIFIER,
81 static struct symbol_op typeof_op = {
82 .type = KW_TYPEOF,
83 .declarator = typeof_specifier,
86 static struct symbol_op attribute_op = {
87 .type = KW_ATTRIBUTE,
88 .declarator = attribute_specifier,
91 static struct symbol_op struct_op = {
92 .type = KW_SPECIFIER,
93 .declarator = struct_specifier,
96 static struct symbol_op union_op = {
97 .type = KW_SPECIFIER,
98 .declarator = union_specifier,
101 static struct symbol_op enum_op = {
102 .type = KW_SPECIFIER,
103 .declarator = enum_specifier,
108 static struct symbol_op if_op = {
109 .statement = parse_if_statement,
112 static struct symbol_op return_op = {
113 .statement = parse_return_statement,
116 static struct symbol_op loop_iter_op = {
117 .statement = parse_loop_iterator,
120 static struct symbol_op default_op = {
121 .statement = parse_default_statement,
124 static struct symbol_op case_op = {
125 .statement = parse_case_statement,
128 static struct symbol_op switch_op = {
129 .statement = parse_switch_statement,
132 static struct symbol_op for_op = {
133 .statement = parse_for_statement,
136 static struct symbol_op while_op = {
137 .statement = parse_while_statement,
140 static struct symbol_op do_op = {
141 .statement = parse_do_statement,
144 static struct symbol_op goto_op = {
145 .statement = parse_goto_statement,
148 static struct symbol_op __context___op = {
149 .statement = parse_context_statement,
152 static struct symbol_op range_op = {
153 .statement = parse_range_statement,
156 static struct symbol_op asm_op = {
157 .type = KW_ASM,
158 .declarator = parse_asm_declarator,
159 .statement = parse_asm_statement,
160 .toplevel = toplevel_asm_declaration,
163 static struct symbol_op packed_op = {
164 .attribute = attribute_packed,
167 static struct symbol_op aligned_op = {
168 .attribute = attribute_aligned,
171 static struct symbol_op attr_mod_op = {
172 .attribute = attribute_modifier,
175 static struct symbol_op address_space_op = {
176 .attribute = attribute_address_space,
179 static struct symbol_op mode_op = {
180 .attribute = attribute_mode,
183 static struct symbol_op context_op = {
184 .attribute = attribute_context,
187 static struct symbol_op transparent_union_op = {
188 .attribute = attribute_transparent_union,
191 static struct symbol_op ignore_attr_op = {
192 .attribute = ignore_attribute,
195 static struct symbol_op mode_spec_op = {
196 .type = KW_MODE,
199 static struct init_keyword {
200 const char *name;
201 enum namespace ns;
202 unsigned long modifiers;
203 struct symbol_op *op;
204 } keyword_table[] = {
205 /* Type qualifiers */
206 { "const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
207 { "__const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
208 { "__const__", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
209 { "volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
210 { "__volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
211 { "__volatile__", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
213 /* Typedef.. */
214 { "typedef", NS_TYPEDEF, MOD_TYPEDEF, .op = &modifier_op },
216 /* Extended types */
217 { "typeof", NS_TYPEDEF, .op = &typeof_op },
218 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
219 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
221 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
222 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
224 { "struct", NS_TYPEDEF, .op = &struct_op },
225 { "union", NS_TYPEDEF, .op = &union_op },
226 { "enum", NS_TYPEDEF, .op = &enum_op },
228 { "inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
229 { "__inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
230 { "__inline__", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
232 /* Ignored for now.. */
233 { "restrict", NS_TYPEDEF, .op = &qualifier_op},
234 { "__restrict", NS_TYPEDEF, .op = &qualifier_op},
236 /* Statement */
237 { "if", NS_KEYWORD, .op = &if_op },
238 { "return", NS_KEYWORD, .op = &return_op },
239 { "break", NS_KEYWORD, .op = &loop_iter_op },
240 { "continue", NS_KEYWORD, .op = &loop_iter_op },
241 { "default", NS_KEYWORD, .op = &default_op },
242 { "case", NS_KEYWORD, .op = &case_op },
243 { "switch", NS_KEYWORD, .op = &switch_op },
244 { "for", NS_KEYWORD, .op = &for_op },
245 { "while", NS_KEYWORD, .op = &while_op },
246 { "do", NS_KEYWORD, .op = &do_op },
247 { "goto", NS_KEYWORD, .op = &goto_op },
248 { "__context__",NS_KEYWORD, .op = &__context___op },
249 { "__range__", NS_KEYWORD, .op = &range_op },
250 { "asm", NS_KEYWORD, .op = &asm_op },
251 { "__asm", NS_KEYWORD, .op = &asm_op },
252 { "__asm__", NS_KEYWORD, .op = &asm_op },
254 /* Attribute */
255 { "packed", NS_KEYWORD, .op = &packed_op },
256 { "__packed__", NS_KEYWORD, .op = &packed_op },
257 { "aligned", NS_KEYWORD, .op = &aligned_op },
258 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
259 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
260 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
261 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
262 { "force", NS_KEYWORD, MOD_FORCE, .op = &attr_mod_op },
263 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
264 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
265 { "address_space",NS_KEYWORD, .op = &address_space_op },
266 { "mode", NS_KEYWORD, .op = &mode_op },
267 { "context", NS_KEYWORD, .op = &context_op },
268 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
270 { "__mode__", NS_KEYWORD, .op = &mode_op },
271 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
272 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
273 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
274 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
275 { "SI", NS_KEYWORD, .op = &mode_spec_op },
276 { "__SI__", NS_KEYWORD, .op = &mode_spec_op },
277 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
278 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
279 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
280 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
282 /* Ignored attributes */
283 { "nothrow", NS_KEYWORD, .op = &ignore_attr_op },
284 { "__nothrow", NS_KEYWORD, .op = &ignore_attr_op },
285 { "__nothrow__", NS_KEYWORD, .op = &ignore_attr_op },
286 { "__malloc__", NS_KEYWORD, .op = &ignore_attr_op },
287 { "nonnull", NS_KEYWORD, .op = &ignore_attr_op },
288 { "__nonnull", NS_KEYWORD, .op = &ignore_attr_op },
289 { "__nonnull__", NS_KEYWORD, .op = &ignore_attr_op },
290 { "format", NS_KEYWORD, .op = &ignore_attr_op },
291 { "__format__", NS_KEYWORD, .op = &ignore_attr_op },
292 { "format_arg", NS_KEYWORD, .op = &ignore_attr_op },
293 { "__format_arg__", NS_KEYWORD, .op = &ignore_attr_op },
294 { "section", NS_KEYWORD, .op = &ignore_attr_op },
295 { "__section__",NS_KEYWORD, .op = &ignore_attr_op },
296 { "unused", NS_KEYWORD, .op = &ignore_attr_op },
297 { "__unused__", NS_KEYWORD, .op = &ignore_attr_op },
298 { "const", NS_KEYWORD, .op = &ignore_attr_op },
299 { "__const", NS_KEYWORD, .op = &ignore_attr_op },
300 { "__const__", NS_KEYWORD, .op = &ignore_attr_op },
301 { "noreturn", NS_KEYWORD, .op = &ignore_attr_op },
302 { "__noreturn__", NS_KEYWORD, .op = &ignore_attr_op },
303 { "no_instrument_function", NS_KEYWORD, .op = &ignore_attr_op },
304 { "__no_instrument_function__", NS_KEYWORD, .op = &ignore_attr_op },
305 { "sentinel", NS_KEYWORD, .op = &ignore_attr_op },
306 { "__sentinel__", NS_KEYWORD, .op = &ignore_attr_op },
307 { "regparm", NS_KEYWORD, .op = &ignore_attr_op },
308 { "__regparm__", NS_KEYWORD, .op = &ignore_attr_op },
309 { "weak", NS_KEYWORD, .op = &ignore_attr_op },
310 { "__weak__", NS_KEYWORD, .op = &ignore_attr_op },
311 { "alias", NS_KEYWORD, .op = &ignore_attr_op },
312 { "__alias__", NS_KEYWORD, .op = &ignore_attr_op },
313 { "pure", NS_KEYWORD, .op = &ignore_attr_op },
314 { "__pure__", NS_KEYWORD, .op = &ignore_attr_op },
315 { "always_inline", NS_KEYWORD, .op = &ignore_attr_op },
316 { "__always_inline__", NS_KEYWORD, .op = &ignore_attr_op },
317 { "syscall_linkage", NS_KEYWORD, .op = &ignore_attr_op },
318 { "__syscall_linkage__", NS_KEYWORD, .op = &ignore_attr_op },
319 { "visibility", NS_KEYWORD, .op = &ignore_attr_op },
320 { "__visibility__", NS_KEYWORD, .op = &ignore_attr_op },
321 { "deprecated", NS_KEYWORD, .op = &ignore_attr_op },
322 { "__deprecated__", NS_KEYWORD, .op = &ignore_attr_op },
323 { "noinline", NS_KEYWORD, .op = &ignore_attr_op },
324 { "__noinline__", NS_KEYWORD, .op = &ignore_attr_op },
325 { "used", NS_KEYWORD, .op = &ignore_attr_op },
326 { "__used__", NS_KEYWORD, .op = &ignore_attr_op },
327 { "warn_unused_result", NS_KEYWORD, .op = &ignore_attr_op },
328 { "__warn_unused_result__", NS_KEYWORD, .op = &ignore_attr_op },
329 { "model", NS_KEYWORD, .op = &ignore_attr_op },
330 { "__model__", NS_KEYWORD, .op = &ignore_attr_op },
331 { "cdecl", NS_KEYWORD, .op = &ignore_attr_op },
332 { "__cdecl__", NS_KEYWORD, .op = &ignore_attr_op },
333 { "stdcall", NS_KEYWORD, .op = &ignore_attr_op },
334 { "__stdcall__", NS_KEYWORD, .op = &ignore_attr_op },
335 { "fastcall", NS_KEYWORD, .op = &ignore_attr_op },
336 { "__fastcall__", NS_KEYWORD, .op = &ignore_attr_op },
337 { "dllimport", NS_KEYWORD, .op = &ignore_attr_op },
338 { "__dllimport__", NS_KEYWORD, .op = &ignore_attr_op },
339 { "dllexport", NS_KEYWORD, .op = &ignore_attr_op },
340 { "__dllexport__", NS_KEYWORD, .op = &ignore_attr_op },
341 { "constructor", NS_KEYWORD, .op = &ignore_attr_op },
342 { "__constructor__", NS_KEYWORD, .op = &ignore_attr_op },
343 { "destructor", NS_KEYWORD, .op = &ignore_attr_op },
344 { "__destructor__", NS_KEYWORD, .op = &ignore_attr_op },
347 void init_parser(int stream)
349 int i;
350 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
351 struct init_keyword *ptr = keyword_table + i;
352 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
353 sym->ident->keyword = 1;
354 sym->ctype.modifiers = ptr->modifiers;
355 sym->op = ptr->op;
359 // Add a symbol to the list of function-local symbols
360 static void fn_local_symbol(struct symbol *sym)
362 if (function_symbol_list)
363 add_symbol(function_symbol_list, sym);
366 static int SENTINEL_ATTR match_idents(struct token *token, ...)
368 va_list args;
369 struct ident * next;
371 if (token_type(token) != TOKEN_IDENT)
372 return 0;
374 va_start(args, token);
375 do {
376 next = va_arg(args, struct ident *);
377 } while (next && token->ident != next);
378 va_end(args);
380 return next && token->ident == next;
384 struct statement *alloc_statement(struct position pos, int type)
386 struct statement *stmt = __alloc_statement(0);
387 stmt->type = type;
388 stmt->pos = pos;
389 return stmt;
392 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
394 static int apply_modifiers(struct position pos, struct ctype *ctype)
396 struct symbol *base;
398 while ((base = ctype->base_type)) {
399 switch (base->type) {
400 case SYM_FN:
401 case SYM_ENUM:
402 case SYM_ARRAY:
403 case SYM_BITFIELD:
404 case SYM_PTR:
405 ctype = &base->ctype;
406 continue;
408 break;
411 /* Turn the "virtual types" into real types with real sizes etc */
412 if (ctype->base_type == &int_type) {
413 ctype->base_type = ctype_integer(ctype->modifiers);
414 ctype->modifiers &= ~MOD_SPECIFIER;
415 } else if (ctype->base_type == &fp_type) {
416 ctype->base_type = ctype_fp(ctype->modifiers);
417 ctype->modifiers &= ~MOD_SPECIFIER;
420 if (ctype->modifiers & MOD_BITWISE) {
421 struct symbol *type;
422 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
423 if (!is_int_type(ctype->base_type)) {
424 sparse_error(pos, "invalid modifier");
425 return 1;
427 type = alloc_symbol(pos, SYM_BASETYPE);
428 *type = *ctype->base_type;
429 type->ctype.base_type = ctype->base_type;
430 type->type = SYM_RESTRICT;
431 type->ctype.modifiers &= ~MOD_SPECIFIER;
432 ctype->base_type = type;
433 create_fouled(type);
435 return 0;
438 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
440 struct symbol *sym = alloc_symbol(pos, type);
442 sym->ctype.base_type = ctype->base_type;
443 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
445 ctype->base_type = sym;
446 ctype->modifiers &= MOD_STORAGE;
447 return sym;
450 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
452 struct symbol *sym = lookup_symbol(token->ident, ns);
453 if (!sym) {
454 sym = alloc_symbol(token->pos, type);
455 bind_symbol(sym, token->ident, ns);
456 if (type == SYM_LABEL)
457 fn_local_symbol(sym);
459 return sym;
463 * NOTE! NS_LABEL is not just a different namespace,
464 * it also ends up using function scope instead of the
465 * regular symbol scope.
467 struct symbol *label_symbol(struct token *token)
469 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
472 static struct token *struct_union_enum_specifier(enum type type,
473 struct token *token, struct ctype *ctype,
474 struct token *(*parse)(struct token *, struct symbol *))
476 struct symbol *sym;
477 struct position *repos;
479 ctype->modifiers = 0;
480 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
481 if (token_type(token) == TOKEN_IDENT) {
482 sym = lookup_symbol(token->ident, NS_STRUCT);
483 if (!sym ||
484 (sym->scope != block_scope &&
485 (match_op(token->next,';') || match_op(token->next,'{')))) {
486 // Either a new symbol, or else an out-of-scope
487 // symbol being redefined.
488 sym = alloc_symbol(token->pos, type);
489 bind_symbol(sym, token->ident, NS_STRUCT);
491 if (sym->type != type)
492 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
493 ctype->base_type = sym;
494 repos = &token->pos;
495 token = token->next;
496 if (match_op(token, '{')) {
497 // The following test is actually wrong for empty
498 // structs, but (1) they are not C99, (2) gcc does
499 // the same thing, and (3) it's easier.
500 if (sym->symbol_list)
501 error_die(token->pos, "redefinition of %s", show_typename (sym));
502 sym->pos = *repos;
503 token = parse(token->next, sym);
504 token = expect(token, '}', "at end of struct-union-enum-specifier");
506 // Mark the structure as needing re-examination
507 sym->examined = 0;
508 sym->endpos = token->pos;
510 return token;
513 // private struct/union/enum type
514 if (!match_op(token, '{')) {
515 sparse_error(token->pos, "expected declaration");
516 ctype->base_type = &bad_ctype;
517 return token;
520 sym = alloc_symbol(token->pos, type);
521 token = parse(token->next, sym);
522 ctype->base_type = sym;
523 token = expect(token, '}', "at end of specifier");
524 sym->endpos = token->pos;
526 return token;
529 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
531 struct symbol *field, *last = NULL;
532 struct token *res;
533 res = struct_declaration_list(token, &sym->symbol_list);
534 FOR_EACH_PTR(sym->symbol_list, field) {
535 if (!field->ident) {
536 struct symbol *base = field->ctype.base_type;
537 if (base && base->type == SYM_BITFIELD)
538 continue;
540 if (last)
541 last->next_subobject = field;
542 last = field;
543 } END_FOR_EACH_PTR(field);
544 return res;
547 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
549 return struct_declaration_list(token, &sym->symbol_list);
552 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
554 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
557 static struct token *union_specifier(struct token *token, struct ctype *ctype)
559 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_union_declaration);
563 typedef struct {
564 int x;
565 unsigned long long y;
566 } Num;
568 static void upper_boundary(Num *n, Num *v)
570 if (n->x > v->x)
571 return;
572 if (n->x < v->x) {
573 *n = *v;
574 return;
576 if (n->y < v->y)
577 n->y = v->y;
580 static void lower_boundary(Num *n, Num *v)
582 if (n->x < v->x)
583 return;
584 if (n->x > v->x) {
585 *n = *v;
586 return;
588 if (n->y > v->y)
589 n->y = v->y;
592 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
594 int shift = type->bit_size;
595 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
597 if (!is_unsigned)
598 shift--;
599 if (upper->x == 0 && upper->y >> shift)
600 return 0;
601 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
602 return 1;
603 return 0;
606 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
608 if (s1->bit_size < s2->bit_size) {
609 s1 = s2;
610 } else if (s1->bit_size == s2->bit_size) {
611 if (s2->ctype.modifiers & MOD_UNSIGNED)
612 s1 = s2;
614 if (s1->bit_size < bits_in_int)
615 return &int_ctype;
616 return s1;
619 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
621 struct symbol *sym;
623 FOR_EACH_PTR(list, sym) {
624 struct expression *expr = sym->initializer;
625 struct symbol *ctype;
626 if (expr->type != EXPR_VALUE)
627 continue;
628 ctype = expr->ctype;
629 if (ctype->bit_size == base_type->bit_size)
630 continue;
631 cast_value(expr, base_type, expr, ctype);
632 } END_FOR_EACH_PTR(sym);
635 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
637 unsigned long long lastval = 0;
638 struct symbol *ctype = NULL, *base_type = NULL;
639 Num upper = {-1, 0}, lower = {1, 0};
640 struct symbol_list *entries = NULL;
642 parent->examined = 1;
643 parent->ctype.base_type = &int_ctype;
644 while (token_type(token) == TOKEN_IDENT) {
645 struct expression *expr = NULL;
646 struct token *next = token->next;
647 struct symbol *sym;
649 sym = alloc_symbol(token->pos, SYM_NODE);
650 bind_symbol(sym, token->ident, NS_SYMBOL);
651 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
653 if (match_op(next, '=')) {
654 next = constant_expression(next->next, &expr);
655 lastval = get_expression_value(expr);
656 ctype = &void_ctype;
657 if (expr && expr->ctype)
658 ctype = expr->ctype;
659 } else if (!ctype) {
660 ctype = &int_ctype;
661 } else if (is_int_type(ctype)) {
662 lastval++;
663 } else {
664 error_die(token->pos, "can't increment the last enum member");
667 if (!expr) {
668 expr = alloc_expression(token->pos, EXPR_VALUE);
669 expr->value = lastval;
670 expr->ctype = ctype;
673 sym->initializer = expr;
674 sym->enum_member = 1;
675 sym->ctype.base_type = parent;
676 add_ptr_list(&entries, sym);
678 if (base_type != &bad_ctype) {
679 if (ctype->type == SYM_NODE)
680 ctype = ctype->ctype.base_type;
681 if (ctype->type == SYM_ENUM) {
682 if (ctype == parent)
683 ctype = base_type;
684 else
685 ctype = ctype->ctype.base_type;
688 * base_type rules:
689 * - if all enums are of the same type, then
690 * the base_type is that type (two first
691 * cases)
692 * - if enums are of different types, they
693 * all have to be integer types, and the
694 * base type is at least "int_ctype".
695 * - otherwise the base_type is "bad_ctype".
697 if (!base_type) {
698 base_type = ctype;
699 } else if (ctype == base_type) {
700 /* nothing */
701 } else if (is_int_type(base_type) && is_int_type(ctype)) {
702 base_type = bigger_enum_type(base_type, ctype);
703 } else
704 base_type = &bad_ctype;
705 parent->ctype.base_type = base_type;
707 if (is_int_type(base_type)) {
708 Num v = {.y = lastval};
709 if (ctype->ctype.modifiers & MOD_UNSIGNED)
710 v.x = 0;
711 else if ((long long)lastval >= 0)
712 v.x = 0;
713 else
714 v.x = -1;
715 upper_boundary(&upper, &v);
716 lower_boundary(&lower, &v);
718 token = next;
720 sym->endpos = token->pos;
722 if (!match_op(token, ','))
723 break;
724 token = token->next;
726 if (!base_type) {
727 sparse_error(token->pos, "bad enum definition");
728 base_type = &bad_ctype;
730 else if (!is_int_type(base_type))
731 base_type = base_type;
732 else if (type_is_ok(base_type, &upper, &lower))
733 base_type = base_type;
734 else if (type_is_ok(&int_ctype, &upper, &lower))
735 base_type = &int_ctype;
736 else if (type_is_ok(&uint_ctype, &upper, &lower))
737 base_type = &uint_ctype;
738 else if (type_is_ok(&long_ctype, &upper, &lower))
739 base_type = &long_ctype;
740 else if (type_is_ok(&ulong_ctype, &upper, &lower))
741 base_type = &ulong_ctype;
742 else if (type_is_ok(&llong_ctype, &upper, &lower))
743 base_type = &llong_ctype;
744 else if (type_is_ok(&ullong_ctype, &upper, &lower))
745 base_type = &ullong_ctype;
746 else
747 base_type = &bad_ctype;
748 parent->ctype.base_type = base_type;
749 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
750 parent->examined = 0;
752 cast_enum_list(entries, base_type);
753 free_ptr_list(&entries);
755 return token;
758 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
760 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
762 ctype = &ctype->base_type->ctype;
763 if (!ctype->base_type)
764 ctype->base_type = &incomplete_ctype;
766 return ret;
769 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
771 struct symbol *sym;
773 if (!match_op(token, '(')) {
774 sparse_error(token->pos, "expected '(' after typeof");
775 return token;
777 if (lookup_type(token->next)) {
778 token = typename(token->next, &sym, 0);
779 *ctype = sym->ctype;
780 } else {
781 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
782 token = parse_expression(token->next, &typeof_sym->initializer);
784 ctype->modifiers = 0;
785 typeof_sym->endpos = token->pos;
786 ctype->base_type = typeof_sym;
788 return expect(token, ')', "after typeof");
791 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
793 struct expression *expr = NULL;
794 if (match_op(token, '('))
795 token = parens_expression(token, &expr, "in attribute");
796 return token;
799 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
801 ctype->alignment = 1;
802 return token;
805 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
807 int alignment = max_alignment;
808 struct expression *expr = NULL;
810 if (match_op(token, '(')) {
811 token = parens_expression(token, &expr, "in attribute");
812 if (expr)
813 alignment = const_expression_value(expr);
815 ctype->alignment = alignment;
816 return token;
819 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
821 ctype->modifiers |= attr->ctype.modifiers;
822 return token;
825 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
827 struct expression *expr = NULL;
828 token = expect(token, '(', "after address_space attribute");
829 token = conditional_expression(token, &expr);
830 if (expr)
831 ctype->as = const_expression_value(expr);
832 token = expect(token, ')', "after address_space attribute");
833 return token;
836 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
838 token = expect(token, '(', "after mode attribute");
839 if (token_type(token) == TOKEN_IDENT) {
840 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
841 if (mode && mode->op->type == KW_MODE)
842 ctype->modifiers |= mode->ctype.modifiers;
843 else
844 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
845 token = token->next;
846 } else
847 sparse_error(token->pos, "expect attribute mode symbol\n");
848 token = expect(token, ')', "after mode attribute");
849 return token;
852 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
854 struct context *context = alloc_context();
855 struct expression *args[3];
856 int argc = 0;
858 token = expect(token, '(', "after context attribute");
859 while (!match_op(token, ')')) {
860 struct expression *expr = NULL;
861 token = conditional_expression(token, &expr);
862 if (!expr)
863 break;
864 if (argc < 3)
865 args[argc++] = expr;
866 if (!match_op(token, ','))
867 break;
868 token = token->next;
871 switch(argc) {
872 case 0:
873 sparse_error(token->pos, "expected context input/output values");
874 break;
875 case 1:
876 context->in = get_expression_value(args[0]);
877 break;
878 case 2:
879 context->in = get_expression_value(args[0]);
880 context->out = get_expression_value(args[1]);
881 break;
882 case 3:
883 context->context = args[0];
884 context->in = get_expression_value(args[1]);
885 context->out = get_expression_value(args[2]);
886 break;
889 if (argc)
890 add_ptr_list(&ctype->contexts, context);
892 token = expect(token, ')', "after context attribute");
893 return token;
896 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
898 if (Wtransparent_union)
899 sparse_error(token->pos, "ignoring attribute __transparent_union__");
900 return token;
903 static struct token *recover_unknown_attribute(struct token *token)
905 struct expression *expr = NULL;
907 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
908 token = token->next;
909 if (match_op(token, '('))
910 token = parens_expression(token, &expr, "in attribute");
911 return token;
914 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
916 ctype->modifiers = 0;
917 token = expect(token, '(', "after attribute");
918 token = expect(token, '(', "after attribute");
920 for (;;) {
921 struct ident *attribute_name;
922 struct symbol *attr;
924 if (eof_token(token))
925 break;
926 if (match_op(token, ';'))
927 break;
928 if (token_type(token) != TOKEN_IDENT)
929 break;
930 attribute_name = token->ident;
931 attr = lookup_keyword(attribute_name, NS_KEYWORD);
932 if (attr && attr->op->attribute)
933 token = attr->op->attribute(token->next, attr, ctype);
934 else
935 token = recover_unknown_attribute(token);
937 if (!match_op(token, ','))
938 break;
939 token = token->next;
942 token = expect(token, ')', "after attribute");
943 token = expect(token, ')', "after attribute");
944 return token;
947 struct symbol * ctype_integer(unsigned long spec)
949 static struct symbol *const integer_ctypes[][3] = {
950 { &llong_ctype, &sllong_ctype, &ullong_ctype },
951 { &long_ctype, &slong_ctype, &ulong_ctype },
952 { &short_ctype, &sshort_ctype, &ushort_ctype },
953 { &char_ctype, &schar_ctype, &uchar_ctype },
954 { &int_ctype, &sint_ctype, &uint_ctype },
956 struct symbol *const (*ctype)[3];
957 int sub;
959 ctype = integer_ctypes;
960 if (!(spec & MOD_LONGLONG)) {
961 ctype++;
962 if (!(spec & MOD_LONG)) {
963 ctype++;
964 if (!(spec & MOD_SHORT)) {
965 ctype++;
966 if (!(spec & MOD_CHAR))
967 ctype++;
972 sub = ((spec & MOD_UNSIGNED)
974 : ((spec & MOD_EXPLICITLY_SIGNED)
976 : 0));
978 return ctype[0][sub];
981 struct symbol * ctype_fp(unsigned long spec)
983 if (spec & MOD_LONGLONG)
984 return &ldouble_ctype;
985 if (spec & MOD_LONG)
986 return &double_ctype;
987 return &float_ctype;
990 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
992 unsigned long mod = thistype->modifiers;
994 if (mod) {
995 unsigned long old = ctype->modifiers;
996 unsigned long extra = 0, dup, conflict;
998 if (mod & old & MOD_LONG) {
999 extra = MOD_LONGLONG | MOD_LONG;
1000 mod &= ~MOD_LONG;
1001 old &= ~MOD_LONG;
1003 dup = (mod & old) | (extra & old) | (extra & mod);
1004 if (dup)
1005 sparse_error(pos, "Just how %sdo you want this type to be?",
1006 modifier_string(dup));
1008 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
1009 if (conflict)
1010 sparse_error(pos, "You cannot have both long and short modifiers.");
1012 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
1013 if (conflict)
1014 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
1016 // Only one storage modifier allowed, except that "inline" doesn't count.
1017 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
1018 conflict &= (conflict - 1);
1019 if (conflict)
1020 sparse_error(pos, "multiple storage classes");
1022 ctype->modifiers = old | mod | extra;
1025 /* Context */
1026 concat_ptr_list((struct ptr_list *)thistype->contexts,
1027 (struct ptr_list **)&ctype->contexts);
1029 /* Alignment */
1030 if (thistype->alignment & (thistype->alignment-1)) {
1031 warning(pos, "I don't like non-power-of-2 alignments");
1032 thistype->alignment = 0;
1034 if (thistype->alignment > ctype->alignment)
1035 ctype->alignment = thistype->alignment;
1037 /* Address space */
1038 if (thistype->as)
1039 ctype->as = thistype->as;
1042 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
1044 unsigned long banned, wrong;
1045 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1046 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1048 if (s->type == SYM_KEYWORD)
1049 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1050 else if (s->ctype.base_type == &fp_type)
1051 banned = BANNED_SIGN;
1052 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1053 banned = 0;
1054 else {
1055 // label_type
1056 // void_type
1057 // bad_type
1058 // vector_type <-- whatever that is
1059 banned = BANNED_SIZE | BANNED_SIGN;
1062 wrong = mod & banned;
1063 if (wrong)
1064 sparse_error(*pos, "modifier %sis invalid in this context",
1065 modifier_string (wrong));
1068 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1070 struct token *token;
1072 while ( (token = next) != NULL ) {
1073 struct ctype thistype;
1074 struct ident *ident;
1075 struct symbol *s, *type;
1076 unsigned long mod;
1078 next = token->next;
1079 if (token_type(token) != TOKEN_IDENT)
1080 break;
1081 ident = token->ident;
1083 s = lookup_symbol(ident, NS_TYPEDEF);
1084 if (!s)
1085 break;
1086 thistype = s->ctype;
1087 mod = thistype.modifiers;
1088 if (qual) {
1089 if (s->type != SYM_KEYWORD)
1090 break;
1091 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1092 break;
1094 if (s->type == SYM_KEYWORD && s->op->declarator) {
1095 next = s->op->declarator(next, &thistype);
1096 mod = thistype.modifiers;
1098 type = thistype.base_type;
1099 if (type) {
1100 if (qual)
1101 break;
1102 if (ctype->base_type)
1103 break;
1104 /* User types only mix with qualifiers */
1105 if (mod & MOD_USERTYPE) {
1106 if (ctype->modifiers & MOD_SPECIFIER)
1107 break;
1109 ctype->base_type = type;
1112 check_modifiers(&token->pos, s, ctype->modifiers);
1113 apply_ctype(token->pos, &thistype, ctype);
1116 if (!ctype->base_type) {
1117 struct symbol *base = &incomplete_ctype;
1120 * If we have modifiers, we'll default to an integer
1121 * type, and "ctype_integer()" will turn this into
1122 * a specific one.
1124 if (ctype->modifiers & MOD_SPECIFIER)
1125 base = &int_type;
1126 ctype->base_type = base;
1128 return token;
1131 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1133 struct expression *expr = NULL;
1135 token = parse_expression(token, &expr);
1136 sym->array_size = expr;
1137 return token;
1140 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1141 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1143 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1145 struct symbol *keyword;
1146 for (;;) {
1147 struct ctype thistype = { 0, };
1148 if (token_type(token) != TOKEN_IDENT)
1149 break;
1150 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1151 if (!keyword || keyword->type != SYM_KEYWORD)
1152 break;
1153 if (!(keyword->op->type & keywords))
1154 break;
1155 token = keyword->op->declarator(token->next, &thistype);
1156 apply_ctype(token->pos, &thistype, ctype);
1158 return token;
1161 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1163 struct ctype *ctype = &decl->ctype;
1165 if (p && token_type(token) == TOKEN_IDENT) {
1166 *p = token->ident;
1167 token = token->next;
1170 for (;;) {
1171 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
1173 if (token_type(token) != TOKEN_SPECIAL)
1174 return token;
1177 * This can be either a parameter list or a grouping.
1178 * For the direct (non-abstract) case, we know if must be
1179 * a parameter list if we already saw the identifier.
1180 * For the abstract case, we know if must be a parameter
1181 * list if it is empty or starts with a type.
1183 if (token->special == '(') {
1184 struct symbol *sym;
1185 struct token *next = token->next;
1186 int fn;
1188 next = handle_attributes(next, ctype, KW_ATTRIBUTE);
1189 fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1191 if (!fn) {
1192 struct symbol *base_type = ctype->base_type;
1193 token = declarator(next, decl, p);
1194 token = expect(token, ')', "in nested declarator");
1195 while (ctype->base_type != base_type)
1196 ctype = &ctype->base_type->ctype;
1197 p = NULL;
1198 continue;
1201 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1202 token = parameter_type_list(next, sym, p);
1203 token = expect(token, ')', "in function declarator");
1204 sym->endpos = token->pos;
1205 continue;
1207 if (token->special == '[') {
1208 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1209 token = abstract_array_declarator(token->next, array);
1210 token = expect(token, ']', "in abstract_array_declarator");
1211 array->endpos = token->pos;
1212 ctype = &array->ctype;
1213 continue;
1215 break;
1217 return token;
1220 static struct token *pointer(struct token *token, struct ctype *ctype)
1222 unsigned long modifiers;
1223 struct symbol *base_type;
1225 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1226 base_type = ctype->base_type;
1227 ctype->modifiers = modifiers;
1229 while (match_op(token,'*')) {
1230 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1231 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1232 ptr->ctype.as = ctype->as;
1233 concat_ptr_list((struct ptr_list *)ctype->contexts,
1234 (struct ptr_list **)&ptr->ctype.contexts);
1235 ptr->ctype.base_type = base_type;
1237 base_type = ptr;
1238 ctype->modifiers = modifiers & MOD_STORAGE;
1239 ctype->base_type = base_type;
1240 ctype->as = 0;
1241 free_ptr_list(&ctype->contexts);
1243 token = declaration_specifiers(token->next, ctype, 1);
1244 modifiers = ctype->modifiers;
1245 ctype->base_type->endpos = token->pos;
1247 return token;
1250 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1252 token = pointer(token, &sym->ctype);
1253 return direct_declarator(token, sym, p);
1256 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1258 struct ctype *ctype = &decl->ctype;
1259 struct expression *expr;
1260 struct symbol *bitfield;
1261 long long width;
1263 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1264 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1265 show_typename(ctype->base_type));
1266 // Parse this to recover gracefully.
1267 return conditional_expression(token->next, &expr);
1270 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1271 token = conditional_expression(token->next, &expr);
1272 width = const_expression_value(expr);
1273 bitfield->bit_size = width;
1275 if (width < 0 || width > INT_MAX) {
1276 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1277 width = -1;
1278 } else if (decl->ident && width == 0) {
1279 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1280 show_ident(decl->ident));
1281 width = -1;
1282 } else if (decl->ident) {
1283 struct symbol *base_type = bitfield->ctype.base_type;
1284 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1285 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1286 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1287 // Valid values are either {-1;0} or {0}, depending on integer
1288 // representation. The latter makes for very efficient code...
1289 sparse_error(token->pos, "dubious one-bit signed bitfield");
1291 if (Wdefault_bitfield_sign &&
1292 bitfield_type->type != SYM_ENUM &&
1293 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1294 is_signed) {
1295 // The sign of bitfields is unspecified by default.
1296 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1299 bitfield->bit_size = width;
1300 bitfield->endpos = token->pos;
1301 return token;
1304 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1306 struct ctype ctype = {0, };
1308 token = declaration_specifiers(token, &ctype, 0);
1309 for (;;) {
1310 struct ident *ident = NULL;
1311 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1312 decl->ctype = ctype;
1313 token = declarator(token, decl, &ident);
1314 decl->ident = ident;
1315 if (match_op(token, ':')) {
1316 token = handle_bitfield(token, decl);
1317 token = handle_attributes(token, &decl->ctype, KW_ATTRIBUTE | KW_ASM);
1319 apply_modifiers(token->pos, &decl->ctype);
1320 add_symbol(list, decl);
1321 decl->endpos = token->pos;
1322 if (!match_op(token, ','))
1323 break;
1324 token = token->next;
1326 return token;
1329 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1331 while (!match_op(token, '}')) {
1332 if (!match_op(token, ';'))
1333 token = declaration_list(token, list);
1334 if (!match_op(token, ';')) {
1335 sparse_error(token->pos, "expected ; at end of declaration");
1336 break;
1338 token = token->next;
1340 return token;
1343 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1345 struct ident *ident = NULL;
1346 struct symbol *sym;
1347 struct ctype ctype = { 0, };
1349 token = declaration_specifiers(token, &ctype, 0);
1350 sym = alloc_symbol(token->pos, SYM_NODE);
1351 sym->ctype = ctype;
1352 *tree = sym;
1353 token = declarator(token, sym, &ident);
1354 sym->ident = ident;
1355 apply_modifiers(token->pos, &sym->ctype);
1356 sym->endpos = token->pos;
1357 return token;
1360 struct token *typename(struct token *token, struct symbol **p, int mod)
1362 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1363 *p = sym;
1364 token = declaration_specifiers(token, &sym->ctype, 0);
1365 token = declarator(token, sym, NULL);
1366 apply_modifiers(token->pos, &sym->ctype);
1367 if (sym->ctype.modifiers & MOD_STORAGE & ~mod)
1368 warning(sym->pos, "storage class in typename (%s)",
1369 show_typename(sym));
1370 sym->endpos = token->pos;
1371 return token;
1374 static struct token *expression_statement(struct token *token, struct expression **tree)
1376 token = parse_expression(token, tree);
1377 return expect(token, ';', "at end of statement");
1380 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1381 struct expression_list **inout)
1383 struct expression *expr;
1385 /* Allow empty operands */
1386 if (match_op(token->next, ':') || match_op(token->next, ')'))
1387 return token->next;
1388 do {
1389 struct ident *ident = NULL;
1390 if (match_op(token->next, '[') &&
1391 token_type(token->next->next) == TOKEN_IDENT &&
1392 match_op(token->next->next->next, ']')) {
1393 ident = token->next->next->ident;
1394 token = token->next->next->next;
1396 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1397 token = primary_expression(token->next, &expr);
1398 add_expression(inout, expr);
1399 token = parens_expression(token, &expr, "in asm parameter");
1400 add_expression(inout, expr);
1401 } while (match_op(token, ','));
1402 return token;
1405 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1406 struct expression_list **clobbers)
1408 struct expression *expr;
1410 do {
1411 token = primary_expression(token->next, &expr);
1412 add_expression(clobbers, expr);
1413 } while (match_op(token, ','));
1414 return token;
1417 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1419 token = token->next;
1420 stmt->type = STMT_ASM;
1421 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1422 token = token->next;
1424 token = expect(token, '(', "after asm");
1425 token = parse_expression(token, &stmt->asm_string);
1426 if (match_op(token, ':'))
1427 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1428 if (match_op(token, ':'))
1429 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1430 if (match_op(token, ':'))
1431 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1432 token = expect(token, ')', "after asm");
1433 return expect(token, ';', "at end of asm-statement");
1436 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1438 struct expression *expr;
1439 token = expect(token, '(', "after asm");
1440 token = parse_expression(token->next, &expr);
1441 token = expect(token, ')', "after asm");
1442 return token;
1445 /* Make a statement out of an expression */
1446 static struct statement *make_statement(struct expression *expr)
1448 struct statement *stmt;
1450 if (!expr)
1451 return NULL;
1452 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1453 stmt->expression = expr;
1454 return stmt;
1458 * All iterators have two symbols associated with them:
1459 * the "continue" and "break" symbols, which are targets
1460 * for continue and break statements respectively.
1462 * They are in a special name-space, but they follow
1463 * all the normal visibility rules, so nested iterators
1464 * automatically work right.
1466 static void start_iterator(struct statement *stmt)
1468 struct symbol *cont, *brk;
1470 start_symbol_scope();
1471 cont = alloc_symbol(stmt->pos, SYM_NODE);
1472 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1473 brk = alloc_symbol(stmt->pos, SYM_NODE);
1474 bind_symbol(brk, &break_ident, NS_ITERATOR);
1476 stmt->type = STMT_ITERATOR;
1477 stmt->iterator_break = brk;
1478 stmt->iterator_continue = cont;
1479 fn_local_symbol(brk);
1480 fn_local_symbol(cont);
1483 static void end_iterator(struct statement *stmt)
1485 end_symbol_scope();
1488 static struct statement *start_function(struct symbol *sym)
1490 struct symbol *ret;
1491 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1493 start_function_scope();
1494 ret = alloc_symbol(sym->pos, SYM_NODE);
1495 ret->ctype = sym->ctype.base_type->ctype;
1496 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1497 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1498 bind_symbol(ret, &return_ident, NS_ITERATOR);
1499 stmt->ret = ret;
1500 fn_local_symbol(ret);
1502 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1503 current_fn = sym;
1505 return stmt;
1508 static void end_function(struct symbol *sym)
1510 current_fn = NULL;
1511 end_function_scope();
1515 * A "switch()" statement, like an iterator, has a
1516 * the "break" symbol associated with it. It works
1517 * exactly like the iterator break - it's the target
1518 * for any break-statements in scope, and means that
1519 * "break" handling doesn't even need to know whether
1520 * it's breaking out of an iterator or a switch.
1522 * In addition, the "case" symbol is a marker for the
1523 * case/default statements to find the switch statement
1524 * that they are associated with.
1526 static void start_switch(struct statement *stmt)
1528 struct symbol *brk, *switch_case;
1530 start_symbol_scope();
1531 brk = alloc_symbol(stmt->pos, SYM_NODE);
1532 bind_symbol(brk, &break_ident, NS_ITERATOR);
1534 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1535 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1536 switch_case->stmt = stmt;
1538 stmt->type = STMT_SWITCH;
1539 stmt->switch_break = brk;
1540 stmt->switch_case = switch_case;
1542 fn_local_symbol(brk);
1543 fn_local_symbol(switch_case);
1546 static void end_switch(struct statement *stmt)
1548 if (!stmt->switch_case->symbol_list)
1549 warning(stmt->pos, "switch with no cases");
1550 end_symbol_scope();
1553 static void add_case_statement(struct statement *stmt)
1555 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1556 struct symbol *sym;
1558 if (!target) {
1559 sparse_error(stmt->pos, "not in switch scope");
1560 stmt->type = STMT_NONE;
1561 return;
1563 sym = alloc_symbol(stmt->pos, SYM_NODE);
1564 add_symbol(&target->symbol_list, sym);
1565 sym->stmt = stmt;
1566 stmt->case_label = sym;
1567 fn_local_symbol(sym);
1570 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1572 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1574 if (!target)
1575 error_die(token->pos, "internal error: return without a function target");
1576 stmt->type = STMT_RETURN;
1577 stmt->ret_target = target;
1578 return expression_statement(token->next, &stmt->ret_value);
1581 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1583 struct symbol_list *syms;
1584 struct expression *e1, *e2, *e3;
1585 struct statement *iterator;
1587 start_iterator(stmt);
1588 token = expect(token->next, '(', "after 'for'");
1590 syms = NULL;
1591 e1 = NULL;
1592 /* C99 variable declaration? */
1593 if (lookup_type(token)) {
1594 token = external_declaration(token, &syms);
1595 } else {
1596 token = parse_expression(token, &e1);
1597 token = expect(token, ';', "in 'for'");
1599 token = parse_expression(token, &e2);
1600 token = expect(token, ';', "in 'for'");
1601 token = parse_expression(token, &e3);
1602 token = expect(token, ')', "in 'for'");
1603 token = statement(token, &iterator);
1605 stmt->iterator_syms = syms;
1606 stmt->iterator_pre_statement = make_statement(e1);
1607 stmt->iterator_pre_condition = e2;
1608 stmt->iterator_post_statement = make_statement(e3);
1609 stmt->iterator_post_condition = NULL;
1610 stmt->iterator_statement = iterator;
1611 end_iterator(stmt);
1613 return token;
1616 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1618 struct expression *expr;
1619 struct statement *iterator;
1621 start_iterator(stmt);
1622 token = parens_expression(token->next, &expr, "after 'while'");
1623 token = statement(token, &iterator);
1625 stmt->iterator_pre_condition = expr;
1626 stmt->iterator_post_condition = NULL;
1627 stmt->iterator_statement = iterator;
1628 end_iterator(stmt);
1630 return token;
1633 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1635 struct expression *expr;
1636 struct statement *iterator;
1638 start_iterator(stmt);
1639 token = statement(token->next, &iterator);
1640 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1641 token = token->next;
1642 else
1643 sparse_error(token->pos, "expected 'while' after 'do'");
1644 token = parens_expression(token, &expr, "after 'do-while'");
1646 stmt->iterator_post_condition = expr;
1647 stmt->iterator_statement = iterator;
1648 end_iterator(stmt);
1650 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1651 warning(iterator->pos, "do-while statement is not a compound statement");
1653 return expect(token, ';', "after statement");
1656 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1658 stmt->type = STMT_IF;
1659 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1660 token = statement(token, &stmt->if_true);
1661 if (token_type(token) != TOKEN_IDENT)
1662 return token;
1663 if (token->ident != &else_ident)
1664 return token;
1665 return statement(token->next, &stmt->if_false);
1668 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1670 stmt->type = STMT_CASE;
1671 token = expect(token, ':', "after default/case");
1672 add_case_statement(stmt);
1673 return statement(token, &stmt->case_statement);
1676 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1678 token = parse_expression(token->next, &stmt->case_expression);
1679 if (match_op(token, SPECIAL_ELLIPSIS))
1680 token = parse_expression(token->next, &stmt->case_to);
1681 return case_statement(token, stmt);
1684 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1686 return case_statement(token->next, stmt);
1689 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1691 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1692 stmt->type = STMT_GOTO;
1693 stmt->goto_label = target;
1694 if (!target)
1695 sparse_error(stmt->pos, "break/continue not in iterator scope");
1696 return expect(token->next, ';', "at end of statement");
1699 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1701 stmt->type = STMT_SWITCH;
1702 start_switch(stmt);
1703 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1704 token = statement(token, &stmt->switch_statement);
1705 end_switch(stmt);
1706 return token;
1709 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1711 stmt->type = STMT_GOTO;
1712 token = token->next;
1713 if (match_op(token, '*')) {
1714 token = parse_expression(token->next, &stmt->goto_expression);
1715 add_statement(&function_computed_goto_list, stmt);
1716 } else if (token_type(token) == TOKEN_IDENT) {
1717 stmt->goto_label = label_symbol(token);
1718 token = token->next;
1719 } else {
1720 sparse_error(token->pos, "Expected identifier or goto expression");
1722 return expect(token, ';', "at end of statement");
1725 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1727 stmt->type = STMT_CONTEXT;
1728 token = parse_expression(token->next, &stmt->expression);
1729 if(stmt->expression->type == EXPR_PREOP
1730 && stmt->expression->op == '('
1731 && stmt->expression->unop->type == EXPR_COMMA) {
1732 struct expression *expr;
1733 expr = stmt->expression->unop;
1734 stmt->context = expr->left;
1735 stmt->expression = expr->right;
1737 return expect(token, ';', "at end of statement");
1740 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1742 stmt->type = STMT_RANGE;
1743 token = assignment_expression(token->next, &stmt->range_expression);
1744 token = expect(token, ',', "after range expression");
1745 token = assignment_expression(token, &stmt->range_low);
1746 token = expect(token, ',', "after low range");
1747 token = assignment_expression(token, &stmt->range_high);
1748 return expect(token, ';', "after range statement");
1751 static struct token *statement(struct token *token, struct statement **tree)
1753 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1755 *tree = stmt;
1756 if (token_type(token) == TOKEN_IDENT) {
1757 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1758 if (s && s->op->statement)
1759 return s->op->statement(token, stmt);
1761 if (match_op(token->next, ':')) {
1762 stmt->type = STMT_LABEL;
1763 stmt->label_identifier = label_symbol(token);
1764 token = handle_attributes(token->next->next, &stmt->label_identifier->ctype, KW_ATTRIBUTE);
1765 return statement(token, &stmt->label_statement);
1769 if (match_op(token, '{')) {
1770 stmt->type = STMT_COMPOUND;
1771 start_symbol_scope();
1772 token = compound_statement(token->next, stmt);
1773 end_symbol_scope();
1775 return expect(token, '}', "at end of compound statement");
1778 stmt->type = STMT_EXPRESSION;
1779 return expression_statement(token, &stmt->expression);
1782 static struct token * statement_list(struct token *token, struct statement_list **list)
1784 int seen_statement = 0;
1785 for (;;) {
1786 struct statement * stmt;
1787 if (eof_token(token))
1788 break;
1789 if (match_op(token, '}'))
1790 break;
1791 if (lookup_type(token)) {
1792 if (seen_statement) {
1793 warning(token->pos, "mixing declarations and code");
1794 seen_statement = 0;
1796 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1797 token = external_declaration(token, &stmt->declaration);
1798 } else {
1799 seen_statement = warn_on_mixed;
1800 token = statement(token, &stmt);
1802 add_statement(list, stmt);
1804 return token;
1807 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1809 struct symbol_list **list = &fn->arguments;
1811 if (match_op(token, ')')) {
1812 // No warning for "void oink ();"
1813 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1814 if (p && !match_op(token->next, ';'))
1815 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1816 return token;
1819 for (;;) {
1820 struct symbol *sym;
1822 if (match_op(token, SPECIAL_ELLIPSIS)) {
1823 if (!*list)
1824 warning(token->pos, "variadic functions must have one named argument");
1825 fn->variadic = 1;
1826 token = token->next;
1827 break;
1830 sym = alloc_symbol(token->pos, SYM_NODE);
1831 token = parameter_declaration(token, &sym);
1832 if (sym->ctype.base_type == &void_ctype) {
1833 /* Special case: (void) */
1834 if (!*list && !sym->ident)
1835 break;
1836 warning(token->pos, "void parameter");
1838 add_symbol(list, sym);
1839 sym->endpos = token->pos;
1840 if (!match_op(token, ','))
1841 break;
1842 token = token->next;
1845 return token;
1848 struct token *compound_statement(struct token *token, struct statement *stmt)
1850 token = statement_list(token, &stmt->stmts);
1851 return token;
1854 static struct expression *identifier_expression(struct token *token)
1856 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1857 expr->expr_ident = token->ident;
1858 return expr;
1861 static struct expression *index_expression(struct expression *from, struct expression *to)
1863 int idx_from, idx_to;
1864 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1866 idx_from = const_expression_value(from);
1867 idx_to = idx_from;
1868 if (to) {
1869 idx_to = const_expression_value(to);
1870 if (idx_to < idx_from || idx_from < 0)
1871 warning(from->pos, "nonsense array initializer index range");
1873 expr->idx_from = idx_from;
1874 expr->idx_to = idx_to;
1875 return expr;
1878 static struct token *single_initializer(struct expression **ep, struct token *token)
1880 int expect_equal = 0;
1881 struct token *next = token->next;
1882 struct expression **tail = ep;
1883 int nested;
1885 *ep = NULL;
1887 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1888 struct expression *expr = identifier_expression(token);
1889 if (Wold_initializer)
1890 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1891 token = initializer(&expr->ident_expression, next->next);
1892 if (expr->ident_expression)
1893 *ep = expr;
1894 return token;
1897 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1898 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1899 struct expression *expr = identifier_expression(next);
1900 *tail = expr;
1901 tail = &expr->ident_expression;
1902 expect_equal = 1;
1903 token = next->next;
1904 } else if (match_op(token, '[')) {
1905 struct expression *from = NULL, *to = NULL, *expr;
1906 token = constant_expression(token->next, &from);
1907 if (!from) {
1908 sparse_error(token->pos, "Expected constant expression");
1909 break;
1911 if (match_op(token, SPECIAL_ELLIPSIS))
1912 token = constant_expression(token->next, &to);
1913 expr = index_expression(from, to);
1914 *tail = expr;
1915 tail = &expr->idx_expression;
1916 token = expect(token, ']', "at end of initializer index");
1917 if (nested)
1918 expect_equal = 1;
1919 } else {
1920 break;
1923 if (nested && !expect_equal) {
1924 if (!match_op(token, '='))
1925 warning(token->pos, "obsolete array initializer, use C99 syntax");
1926 else
1927 expect_equal = 1;
1929 if (expect_equal)
1930 token = expect(token, '=', "at end of initializer index");
1932 token = initializer(tail, token);
1933 if (!*tail)
1934 *ep = NULL;
1935 return token;
1938 static struct token *initializer_list(struct expression_list **list, struct token *token)
1940 struct expression *expr;
1942 for (;;) {
1943 token = single_initializer(&expr, token);
1944 if (!expr)
1945 break;
1946 add_expression(list, expr);
1947 if (!match_op(token, ','))
1948 break;
1949 token = token->next;
1951 return token;
1954 struct token *initializer(struct expression **tree, struct token *token)
1956 if (match_op(token, '{')) {
1957 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1958 *tree = expr;
1959 token = initializer_list(&expr->expr_list, token->next);
1960 return expect(token, '}', "at end of initializer");
1962 return assignment_expression(token, tree);
1965 static void declare_argument(struct symbol *sym, struct symbol *fn)
1967 if (!sym->ident) {
1968 sparse_error(sym->pos, "no identifier for function argument");
1969 return;
1971 bind_symbol(sym, sym->ident, NS_SYMBOL);
1974 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1975 struct symbol_list **list)
1977 struct symbol_list **old_symbol_list;
1978 struct symbol *base_type = decl->ctype.base_type;
1979 struct statement *stmt, **p;
1980 struct symbol *arg;
1982 old_symbol_list = function_symbol_list;
1983 if (decl->ctype.modifiers & MOD_INLINE) {
1984 function_symbol_list = &decl->inline_symbol_list;
1985 p = &base_type->inline_stmt;
1986 } else {
1987 function_symbol_list = &decl->symbol_list;
1988 p = &base_type->stmt;
1990 function_computed_target_list = NULL;
1991 function_computed_goto_list = NULL;
1993 if (decl->ctype.modifiers & MOD_EXTERN) {
1994 if (!(decl->ctype.modifiers & MOD_INLINE))
1995 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1997 if (!(decl->ctype.modifiers & MOD_STATIC))
1998 decl->ctype.modifiers |= MOD_EXTERN;
2000 stmt = start_function(decl);
2002 *p = stmt;
2003 FOR_EACH_PTR (base_type->arguments, arg) {
2004 declare_argument(arg, base_type);
2005 } END_FOR_EACH_PTR(arg);
2007 token = compound_statement(token->next, stmt);
2009 end_function(decl);
2010 if (!(decl->ctype.modifiers & MOD_INLINE))
2011 add_symbol(list, decl);
2012 check_declaration(decl);
2013 function_symbol_list = old_symbol_list;
2014 if (function_computed_goto_list) {
2015 if (!function_computed_target_list)
2016 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2017 else {
2018 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2019 stmt->target_list = function_computed_target_list;
2020 } END_FOR_EACH_PTR(stmt);
2023 return expect(token, '}', "at end of function");
2026 static void promote_k_r_types(struct symbol *arg)
2028 struct symbol *base = arg->ctype.base_type;
2029 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2030 arg->ctype.base_type = &int_ctype;
2034 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2036 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2037 struct symbol *arg;
2039 FOR_EACH_PTR(real_args, arg) {
2040 struct symbol *type;
2042 /* This is quadratic in the number of arguments. We _really_ don't care */
2043 FOR_EACH_PTR(argtypes, type) {
2044 if (type->ident == arg->ident)
2045 goto match;
2046 } END_FOR_EACH_PTR(type);
2047 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2048 continue;
2049 match:
2050 type->used = 1;
2051 /* "char" and "short" promote to "int" */
2052 promote_k_r_types(type);
2054 arg->ctype = type->ctype;
2055 } END_FOR_EACH_PTR(arg);
2057 FOR_EACH_PTR(argtypes, arg) {
2058 if (!arg->used)
2059 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2060 } END_FOR_EACH_PTR(arg);
2064 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2065 struct symbol_list **list)
2067 struct symbol_list *args = NULL;
2069 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2070 do {
2071 token = declaration_list(token, &args);
2072 if (!match_op(token, ';')) {
2073 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2074 break;
2076 token = token->next;
2077 } while (lookup_type(token));
2079 apply_k_r_types(args, decl);
2081 if (!match_op(token, '{')) {
2082 sparse_error(token->pos, "expected function body");
2083 return token;
2085 return parse_function_body(token, decl, list);
2088 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2090 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2091 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2092 struct statement *stmt;
2094 anon->ctype.base_type = fn;
2095 stmt = alloc_statement(token->pos, STMT_NONE);
2096 fn->stmt = stmt;
2098 token = parse_asm_statement(token, stmt);
2100 add_symbol(list, anon);
2101 return token;
2104 struct token *external_declaration(struct token *token, struct symbol_list **list)
2106 struct ident *ident = NULL;
2107 struct symbol *decl;
2108 struct ctype ctype = { 0, };
2109 struct symbol *base_type;
2110 int is_typedef;
2112 /* Top-level inline asm? */
2113 if (token_type(token) == TOKEN_IDENT) {
2114 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2115 if (s && s->op->toplevel)
2116 return s->op->toplevel(token, list);
2119 /* Parse declaration-specifiers, if any */
2120 token = declaration_specifiers(token, &ctype, 0);
2121 decl = alloc_symbol(token->pos, SYM_NODE);
2122 decl->ctype = ctype;
2123 token = declarator(token, decl, &ident);
2124 apply_modifiers(token->pos, &decl->ctype);
2126 decl->endpos = token->pos;
2128 /* Just a type declaration? */
2129 if (!ident)
2130 return expect(token, ';', "end of type declaration");
2132 /* type define declaration? */
2133 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2135 /* Typedefs don't have meaningful storage */
2136 if (is_typedef) {
2137 ctype.modifiers &= ~MOD_STORAGE;
2138 decl->ctype.modifiers &= ~MOD_STORAGE;
2139 decl->ctype.modifiers |= MOD_USERTYPE;
2142 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2144 base_type = decl->ctype.base_type;
2146 if (is_typedef) {
2147 if (base_type && !base_type->ident)
2148 base_type->ident = ident;
2149 } else if (base_type && base_type->type == SYM_FN) {
2150 /* K&R argument declaration? */
2151 if (lookup_type(token))
2152 return parse_k_r_arguments(token, decl, list);
2153 if (match_op(token, '{'))
2154 return parse_function_body(token, decl, list);
2156 if (!(decl->ctype.modifiers & MOD_STATIC))
2157 decl->ctype.modifiers |= MOD_EXTERN;
2158 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2159 sparse_error(token->pos, "void declaration");
2162 for (;;) {
2163 if (!is_typedef && match_op(token, '=')) {
2164 if (decl->ctype.modifiers & MOD_EXTERN) {
2165 warning(decl->pos, "symbol with external linkage has initializer");
2166 decl->ctype.modifiers &= ~MOD_EXTERN;
2168 token = initializer(&decl->initializer, token->next);
2170 if (!is_typedef) {
2171 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2172 add_symbol(list, decl);
2173 fn_local_symbol(decl);
2176 check_declaration(decl);
2178 if (!match_op(token, ','))
2179 break;
2181 token = token->next;
2182 ident = NULL;
2183 decl = alloc_symbol(token->pos, SYM_NODE);
2184 decl->ctype = ctype;
2185 token = declaration_specifiers(token, &decl->ctype, 1);
2186 token = declarator(token, decl, &ident);
2187 apply_modifiers(token->pos, &decl->ctype);
2188 decl->endpos = token->pos;
2189 if (!ident) {
2190 sparse_error(token->pos, "expected identifier name in type definition");
2191 return token;
2194 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2196 /* Function declarations are automatically extern unless specifically static */
2197 base_type = decl->ctype.base_type;
2198 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2199 if (!(decl->ctype.modifiers & MOD_STATIC))
2200 decl->ctype.modifiers |= MOD_EXTERN;
2203 return expect(token, ';', "at end of declaration");