Lines Matching refs:token

40 #include "token.h"
51 static struct token *statement(struct token *token, struct statement **tree);
52 static struct token *handle_attributes(struct token *token, struct decl_state *ctx);
54 typedef struct token *declarator_t(struct token *, struct symbol *, struct decl_state *);
62 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
63 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
64 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
65 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
66 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
67 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
68 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
69 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
70 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
71 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
72 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
73 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
74 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
75 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
76 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
78 typedef struct token *attr_t(struct token *, struct symbol *,
116 static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod)
119 warning(token->pos, "duplicated asm modifier");
617 static struct token *skip_to(struct token *token, int op)
619 while (!match_op(token, op) && !eof_token(token))
620 token = token->next;
621 return token;
624 static struct token bad_token = { .pos.type = TOKEN_BAD };
625 struct token *expect(struct token *token, int op, const char *where)
627 if (!match_op(token, op)) {
628 if (token != &bad_token) {
629 bad_token.next = token;
630 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
631 sparse_error(token->pos, "got %s", show_token(token));
634 return skip_to(token, op);
637 return token->next;
642 // @token: the current token
644 // If the current token is from a previous error, an error message
646 // Otherwise, @errmsg is displayed followed by the current token.
647 static void unexpected(struct token *token, const char *errmsg)
649 if (token == &bad_token)
651 sparse_error(token->pos, "%s", errmsg);
652 sparse_error(token->pos, "got %s", show_token(token));
670 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
705 struct symbol *label_symbol(struct token *token, int used)
707 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
709 sym = alloc_symbol(token->pos, SYM_LABEL);
710 bind_symbol(sym, token->ident, NS_LABEL);
718 static struct token *struct_union_enum_specifier(enum type type,
719 struct token *token, struct decl_state *ctx,
720 struct token *(*parse)(struct token *, struct symbol *))
726 token = handle_attributes(token, &attr);
727 if (token_type(token) == TOKEN_IDENT) {
728 sym = lookup_symbol(token->ident, NS_STRUCT);
731 (match_op(token->next,';') || match_op(token->next,'{')))) {
734 sym = alloc_symbol(token->pos, type);
735 bind_symbol(sym, token->ident, NS_STRUCT);
738 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
740 repos = &token->pos;
741 token = token->next;
742 if (!match_op(token, '{'))
743 return token;
749 error_die(token->pos, "redefinition of %s", show_typename (sym));
754 } else if (match_op(token, '{')) {
756 sym = alloc_symbol(token->pos, type);
760 sparse_error(token->pos, "expected declaration");
762 return token;
765 token = parse(token->next, sym);
766 token = expect(token, '}', "at end of specifier");
768 token = handle_attributes(token, &attr);
769 apply_ctype(token->pos, &sym->ctype, &attr.ctype);
772 sym->endpos = token->pos;
774 return token;
777 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
780 struct token *res;
781 res = struct_declaration_list(token, &sym->symbol_list);
795 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
797 return struct_declaration_list(token, &sym->symbol_list);
800 static struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
802 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
805 static struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
807 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
911 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
920 while (token_type(token) == TOKEN_IDENT) {
922 struct token *next = token->next;
940 error_die(token->pos, "can't increment the last enum member");
944 expr = alloc_expression(token->pos, EXPR_VALUE);
949 sym = alloc_symbol(token->pos, SYM_NODE);
950 bind_symbol(sym, token->ident, NS_SYMBOL);
992 sparse_error(token->pos, "bad enum definition");
1000 token = next;
1002 sym->endpos = token->pos;
1004 if (!match_op(token, ','))
1006 token = token->next;
1009 sparse_error(token->pos, "empty enum definition");
1033 return token;
1036 return token;
1039 static struct token *enum_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1041 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
1050 static struct token *typeof_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1053 if (!match_op(token, '(')) {
1054 sparse_error(token->pos, "expected '(' after typeof");
1055 return token;
1057 if (lookup_type(token->next)) {
1059 token = typename(token->next, &sym, NULL);
1061 apply_ctype(token->pos, &ctx->ctype, &sym->ctype);
1063 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1064 token = parse_expression(token->next, &typeof_sym->initializer);
1066 typeof_sym->endpos = token->pos;
1068 sparse_error(token->pos, "expected expression after the '(' token");
1073 return expect(token, ')', "after typeof");
1076 static struct token *autotype_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1080 return token;
1083 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1086 if (match_op(token, '('))
1087 token = parens_expression(token, &expr, "in attribute");
1088 return token;
1091 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1097 return token;
1100 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1105 if (match_op(token, '(')) {
1106 token = parens_expression(token, &expr, "in attribute");
1111 warning(token->pos, "I don't like non-power-of-2 alignments");
1112 return token;
1115 return token;
1130 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1132 apply_mod(&token->pos, &ctx->ctype.modifiers, attr->ctype.modifiers);
1133 return token;
1136 static struct token *attribute_function(struct token *token, struct symbol *attr, struct decl_state *ctx)
1138 apply_mod(&token->pos, &ctx->f_modifiers, attr->ctype.modifiers);
1139 return token;
1142 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1145 attribute_modifier(token, attr, ctx);
1146 return token;
1159 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1163 struct token *next;
1165 token = expect(token, '(', "after address_space attribute");
1166 switch (token_type(token)) {
1168 next = primary_expression(token, &expr);
1174 next = token->next;
1175 as = token->ident;
1178 next = token->next;
1181 warning(token->pos, "invalid address space name");
1186 sparse_error(token->pos,
1191 token = expect(next, ')', "after address_space attribute");
1192 return token;
1253 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1255 token = expect(token, '(', "after mode attribute");
1256 if (token_type(token) == TOKEN_IDENT) {
1257 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1261 sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident));
1262 token = token->next;
1264 sparse_error(token->pos, "expect attribute mode symbol\n");
1265 token = expect(token, ')', "after mode attribute");
1266 return token;
1269 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1275 token = expect(token, '(', "after context attribute");
1276 token = conditional_expression(token, &args[0]);
1277 token = expect(token, ',', "after context 1st argument");
1278 token = conditional_expression(token, &args[1]);
1279 if (match_op(token, ',')) {
1280 token = token->next;
1281 token = conditional_expression(token, &args[2]);
1282 token = expect(token, ')', "after context 3rd argument");
1286 token = expect(token, ')', "after context 2nd argument");
1291 return token;
1294 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1299 warning(token->pos, "attribute designated_init applied to non-structure type");
1300 return token;
1303 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1306 warning(token->pos, "attribute __transparent_union__");
1311 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1312 return token;
1315 static struct token *recover_unknown_attribute(struct token *token)
1320 warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
1321 token = token->next;
1322 if (match_op(token, '('))
1323 token = parens_expression(token, &expr, "in attribute");
1324 return token;
1327 static struct token *attribute_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1329 token = expect(token, '(', "after attribute");
1330 token = expect(token, '(', "after attribute");
1332 while (token_type(token) == TOKEN_IDENT) {
1333 struct symbol *attr = lookup_keyword(token->ident, NS_KEYWORD);
1335 token = attr->op->attribute(token->next, attr, ctx);
1337 token = recover_unknown_attribute(token);
1339 if (!match_op(token, ','))
1341 token = token->next;
1344 token = expect(token, ')', "after attribute");
1345 token = expect(token, ')', "after attribute");
1346 return token;
1356 static struct token *storage_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1374 static struct token *thread_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1387 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1390 return token;
1393 static struct token *alignas_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1397 if (!match_op(token, '(')) {
1398 sparse_error(token->pos, "expected '(' after _Alignas");
1399 return token;
1401 if (lookup_type(token->next)) {
1403 token = typename(token->next, &sym, NULL);
1406 token = expect(token, ')', "after _Alignas(...");
1409 token = parens_expression(token, &expr, "after _Alignas");
1411 return token;
1416 warning(token->pos, "non-positive alignment");
1417 return token;
1420 warning(token->pos, "non-power-of-2 alignment");
1421 return token;
1425 return token;
1428 static struct token *generic_qualifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1505 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1520 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1526 while (token_type(token) == TOKEN_IDENT) {
1527 struct symbol *s = lookup_symbol(token->ident,
1536 apply_ctype(token->pos, &ctx->ctype, &s->ctype);
1537 token = token->next;
1542 specifier_conflict(token->pos,
1544 token->ident);
1557 specifier_conflict(token->pos,
1565 token = token->next;
1567 token = s->op->declarator(token, s, ctx);
1585 sparse_error(token->pos, "invalid modifier");
1586 return token;
1588 type = alloc_symbol(token->pos, SYM_BASETYPE);
1596 return token;
1599 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1604 while (token_type(token) == TOKEN_IDENT) {
1605 struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF);
1609 sparse_error(token->pos, "duplicate array static declarator");
1611 token = token->next;
1613 if (match_op(token, '*') && match_op(token->next, ']')) {
1615 token = token->next;
1617 token = assignment_expression(token, &expr);
1620 return token;
1623 static struct token *parameter_type_list(struct token *, struct symbol *);
1624 static struct token *identifier_list(struct token *, struct symbol *);
1625 static struct token *declarator(struct token *token, struct decl_state *ctx);
1627 static struct token *handle_asm_name(struct token *token, struct decl_state *ctx)
1632 if (token_type(token) != TOKEN_IDENT)
1633 return token;
1634 keyword = lookup_keyword(token->ident, NS_KEYWORD);
1636 return token;
1638 return token;
1640 token = token->next;
1641 token = expect(token, '(', "after asm");
1642 token = string_expression(token, &expr, "asm name");
1643 token = expect(token, ')', "after asm");
1644 return token;
1648 // test if @token is '__attribute__' (or one of its variant)
1649 static bool match_attribute(struct token *token)
1653 if (token_type(token) != TOKEN_IDENT)
1655 sym = lookup_keyword(token->ident, NS_TYPEDEF);
1661 static struct token *skip_attribute(struct token *token)
1663 token = token->next;
1664 if (match_op(token, '(')) {
1666 token = token->next;
1667 while (depth && !eof_token(token)) {
1668 if (token_type(token) == TOKEN_SPECIAL) {
1669 if (token->special == '(')
1671 else if (token->special == ')')
1674 token = token->next;
1677 return token;
1680 static struct token *skip_attributes(struct token *token)
1682 while (match_attribute(token)) {
1683 token = expect(token->next, '(', "after attribute");
1684 token = expect(token, '(', "after attribute");
1685 while (token_type(token) == TOKEN_IDENT) {
1686 token = skip_attribute(token);
1687 if (!match_op(token, ','))
1689 token = token->next;
1691 token = expect(token, ')', "after attribute");
1692 token = expect(token, ')', "after attribute");
1694 return token;
1697 static struct token *handle_attributes(struct token *token, struct decl_state *ctx)
1699 while (match_attribute(token))
1700 token = attribute_specifier(token->next, NULL, ctx);
1701 return token;
1704 static int is_nested(struct token *token, struct token **p,
1714 struct token *next = token->next;
1734 static enum kind which_func(struct token *token,
1738 struct token *next = token->next;
1745 warning(token->pos,
1773 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1776 struct token *next;
1779 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1780 *ctx->ident = token->ident;
1781 token = token->next;
1782 } else if (match_op(token, '(') &&
1783 is_nested(token, &next, ctx->prefer_abstract)) {
1785 if (token->next != next)
1786 next = handle_attributes(token->next, ctx);
1787 token = declarator(next, ctx);
1788 token = expect(token, ')', "in nested declarator");
1794 if (match_op(token, '(')) {
1795 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1797 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1799 token = token->next;
1801 token = identifier_list(token, fn);
1803 token = parameter_type_list(token, fn);
1804 token = expect(token, ')', "in function declarator");
1805 fn->endpos = token->pos;
1806 return token;
1809 while (match_op(token, '[')) {
1811 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1812 token = abstract_array_declarator(token->next, array);
1813 token = expect(token, ']', "in abstract_array_declarator");
1814 array->endpos = token->pos;
1817 return token;
1820 static struct token *pointer(struct token *token, struct decl_state *ctx)
1822 while (match_op(token,'*')) {
1823 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1834 token = handle_qualifiers(token->next, ctx);
1835 ctx->ctype.base_type->endpos = token->pos;
1837 return token;
1840 static struct token *declarator(struct token *token, struct decl_state *ctx)
1842 token = pointer(token, ctx);
1843 return direct_declarator(token, ctx);
1846 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1854 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1857 return conditional_expression(token->next, &expr);
1860 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1861 token = conditional_expression(token->next, &expr);
1866 sparse_error(token->pos, "bitfield '%s' has invalid width (%lld)",
1876 sparse_error(token->pos, "dubious one-bit signed bitfield");
1883 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1887 bitfield->endpos = token->pos;
1889 return token;
1892 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1898 token = declaration_specifiers(token, &ctx);
1902 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1905 token = declarator(token, &ctx);
1906 if (match_op(token, ':'))
1907 token = handle_bitfield(token, &ctx);
1909 token = handle_attributes(token, &ctx);
1910 apply_modifiers(token->pos, &ctx);
1914 decl->endpos = token->pos;
1916 if (!match_op(token, ','))
1918 token = token->next;
1921 return token;
1924 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1926 while (!match_op(token, '}')) {
1927 if (match_ident(token, &_Static_assert_ident)) {
1928 token = parse_static_assert(token, NULL);
1931 if (!match_op(token, ';'))
1932 token = declaration_list(token, list);
1933 if (!match_op(token, ';')) {
1934 sparse_error(token->pos, "expected ; at end of declaration");
1937 token = token->next;
1939 return token;
1942 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1946 token = declaration_specifiers(token, &ctx);
1948 token = declarator(token, &ctx);
1949 token = handle_attributes(token, &ctx);
1950 apply_modifiers(token->pos, &ctx);
1953 sym->endpos = token->pos;
1955 return token;
1958 struct token *typename(struct token *token, struct symbol **p, int *forced)
1962 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1964 token = declaration_specifiers(token, &ctx);
1965 token = declarator(token, &ctx);
1966 apply_modifiers(token->pos, &ctx);
1968 sym->endpos = token->pos;
1975 return token;
1978 static struct token *expression_statement(struct token *token, struct expression **tree)
1980 token = parse_expression(token, tree);
1981 return expect(token, ';', "at end of statement");
1984 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1988 if (match_op(token->next, ':') || match_op(token->next, ')'))
1989 return token->next;
1992 if (match_op(token->next, '[') &&
1993 token_type(token->next->next) == TOKEN_IDENT &&
1994 match_op(token->next->next->next, ']')) {
1995 op->name = token->next->next->ident;
1996 token = token->next->next->next;
1998 token = token->next;
1999 token = string_expression(token, &op->constraint, "asm constraint");
2000 token = parens_expression(token, &op->expr, "in asm parameter");
2002 } while (match_op(token, ','));
2003 return token;
2006 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
2012 token = primary_expression(token->next, &expr);
2015 } while (match_op(token, ','));
2016 return token;
2019 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2025 token = token->next; /* skip ':' and ',' */
2026 if (token_type(token) != TOKEN_IDENT)
2027 return token;
2028 label = label_symbol(token, 1);
2030 token = token->next;
2031 } while (match_op(token, ','));
2032 return token;
2035 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2039 token = token->next;
2041 while (token_type(token) == TOKEN_IDENT) {
2042 struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF);
2044 s->op->asm_modifier(token, &mods, s->ctype.modifiers);
2045 else if (token->ident == &goto_ident)
2046 asm_modifier(token, &mods, MOD_ASM_GOTO);
2047 token = token->next;
2049 token = expect(token, '(', "after asm");
2050 token = string_expression(token, &stmt->asm_string, "inline asm");
2051 if (match_op(token, ':'))
2052 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2053 if (match_op(token, ':'))
2054 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2055 if (match_op(token, ':'))
2056 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2057 if (match_op(token, ':') && (mods & MOD_ASM_GOTO))
2058 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2059 token = expect(token, ')', "after asm");
2060 return expect(token, ';', "at end of asm-statement");
2063 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2067 token = expect(token->next, '(', "after _Static_assert");
2068 token = constant_expression(token, &cond);
2070 sparse_error(token->pos, "Expected constant expression");
2071 if (match_op(token, ',')) {
2072 token = token->next;
2073 token = string_expression(token, &message, "_Static_assert()");
2077 token = expect(token, ')', "after diagnostic message in _Static_assert");
2078 token = expect(token, ';', "after _Static_assert()");
2090 return token;
2218 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2223 error_die(token->pos, "internal error: return without a function target");
2226 return expression_statement(token->next, &stmt->ret_value);
2240 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2247 token = expect(token->next, '(', "after 'for'");
2252 if (lookup_type(token)) {
2253 token = external_declaration(token, &syms, validate_for_loop_decl);
2255 token = parse_expression(token, &e1);
2256 token = expect(token, ';', "in 'for'");
2258 token = parse_expression(token, &e2);
2259 token = expect(token, ';', "in 'for'");
2260 token = parse_expression(token, &e3);
2261 token = expect(token, ')', "in 'for'");
2262 token = statement(token, &iterator);
2272 return token;
2275 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2281 token = parens_expression(token->next, &expr, "after 'while'");
2282 token = statement(token, &iterator);
2289 return token;
2292 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2298 token = statement(token->next, &iterator);
2299 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2300 token = token->next;
2302 sparse_error(token->pos, "expected 'while' after 'do'");
2303 token = parens_expression(token, &expr, "after 'do-while'");
2312 return expect(token, ';', "after statement");
2315 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2318 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2319 token = statement(token, &stmt->if_true);
2320 if (token_type(token) != TOKEN_IDENT)
2321 return token;
2322 if (token->ident != &else_ident)
2323 return token;
2324 return statement(token->next, &stmt->if_false);
2327 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2330 token = expect(token, ':', "after default/case");
2332 return statement(token, &stmt->case_statement);
2335 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2337 token = parse_expression(token->next, &stmt->case_expression);
2338 if (match_op(token, SPECIAL_ELLIPSIS))
2339 token = parse_expression(token->next, &stmt->case_to);
2340 return case_statement(token, stmt);
2343 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2345 return case_statement(token->next, stmt);
2348 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2350 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2355 return expect(token->next, ';', "at end of statement");
2358 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2362 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2363 token = statement(token, &stmt->switch_statement);
2365 return token;
2389 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2392 token = token->next;
2393 if (match_op(token, '*')) {
2394 token = parse_expression(token->next, &stmt->goto_expression);
2396 } else if (token_type(token) == TOKEN_IDENT) {
2397 struct symbol *label = label_symbol(token, 1);
2400 token = token->next;
2402 sparse_error(token->pos, "Expected identifier or goto expression");
2404 return expect(token, ';', "at end of statement");
2407 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2410 token = token->next;
2411 token = expect(token, '(', "after __context__ statement");
2412 token = assignment_expression(token, &stmt->expression);
2414 unexpected(token, "expression expected after '('");
2415 if (match_op(token, ',')) {
2416 token = token->next;
2418 token = assignment_expression(token, &stmt->expression);
2420 unexpected(token, "expression expected after ','");
2422 token = expect(token, ')', "at end of __context__ statement");
2423 return expect(token, ';', "at end of statement");
2426 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2429 token = token->next;
2430 token = expect(token, '(', "after __range__ statement");
2431 token = assignment_expression(token, &stmt->range_expression);
2432 token = expect(token, ',', "after range expression");
2433 token = assignment_expression(token, &stmt->range_low);
2434 token = expect(token, ',', "after low range");
2435 token = assignment_expression(token, &stmt->range_high);
2436 token = expect(token, ')', "after range statement");
2437 return expect(token, ';', "after range statement");
2440 static struct token *handle_label_attributes(struct token *token, struct symbol *label)
2444 token = handle_attributes(token, &ctx);
2446 return token;
2449 static struct token *statement(struct token *token, struct statement **tree)
2451 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2454 if (token_type(token) == TOKEN_IDENT) {
2455 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2457 return s->op->statement(token, stmt);
2459 if (match_op(token->next, ':')) {
2460 struct symbol *s = label_symbol(token, 0);
2461 token = handle_label_attributes(token->next->next, s);
2465 return statement(token, tree);
2475 if (match_op(token, '}')) {
2476 warning(token->pos, "statement expected after label");
2477 stmt->label_statement = alloc_statement(token->pos, STMT_NONE);
2478 return token;
2480 return statement(token, &stmt->label_statement);
2484 if (match_op(token, '{')) {
2485 token = compound_statement(token->next, stmt);
2486 return expect(token, '}', "at end of compound statement");
2490 return expression_statement(token, &stmt->expression);
2494 static struct token *label_statement(struct token *token)
2496 while (token_type(token) == TOKEN_IDENT) {
2497 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2499 bind_symbol_with_scope(sym, token->ident, NS_LABEL, block_scope);
2501 token = token->next;
2502 if (!match_op(token, ','))
2504 token = token->next;
2506 return expect(token, ';', "at end of label declaration");
2509 static struct token * statement_list(struct token *token, struct statement_list **list)
2512 while (token_type(token) == TOKEN_IDENT &&
2513 token->ident == &__label___ident)
2514 token = label_statement(token->next);
2517 if (eof_token(token))
2519 if (match_op(token, '}'))
2521 if (match_ident(token, &_Static_assert_ident)) {
2522 token = parse_static_assert(token, NULL);
2525 if (lookup_type(token)) {
2527 warning(token->pos, "mixing declarations and code");
2530 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2531 token = external_declaration(token, &stmt->declaration, NULL);
2534 token = statement(token, &stmt);
2538 return token;
2541 static struct token *identifier_list(struct token *token, struct symbol *fn)
2545 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2546 sym->ident = token->ident;
2547 token = token->next;
2548 sym->endpos = token->pos;
2551 if (!match_op(token, ',') ||
2552 token_type(token->next) != TOKEN_IDENT ||
2553 lookup_type(token->next))
2555 token = token->next;
2557 return token;
2560 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2567 if (match_op(token, SPECIAL_ELLIPSIS)) {
2569 token = token->next;
2573 sym = alloc_symbol(token->pos, SYM_NODE);
2574 token = parameter_declaration(token, sym);
2579 warning(token->pos, "void parameter");
2582 if (!match_op(token, ','))
2584 token = token->next;
2586 return token;
2589 struct token *compound_statement(struct token *token, struct statement *stmt)
2593 token = statement_list(token, &stmt->stmts);
2595 return token;
2598 static struct expression *identifier_expression(struct token *token)
2600 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2601 expr->expr_ident = token->ident;
2622 static struct token *single_initializer(struct expression **ep, struct token *token)
2625 struct token *next = token->next;
2631 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2632 struct expression *expr = identifier_expression(token);
2634 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2635 token = initializer(&expr->ident_expression, next->next);
2638 return token;
2641 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2642 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2647 token = next->next;
2648 } else if (match_op(token, '[')) {
2650 token = constant_expression(token->next, &from);
2652 sparse_error(token->pos, "Expected constant expression");
2655 if (match_op(token, SPECIAL_ELLIPSIS))
2656 token = constant_expression(token->next, &to);
2660 token = expect(token, ']', "at end of initializer index");
2668 if (!match_op(token, '='))
2669 warning(token->pos, "obsolete array initializer, use C99 syntax");
2674 token = expect(token, '=', "at end of initializer index");
2676 token = initializer(tail, token);
2679 return token;
2682 static struct token *initializer_list(struct expression_list **list, struct token *token)
2687 token = single_initializer(&expr, token);
2691 if (!match_op(token, ','))
2693 token = token->next;
2695 return token;
2698 struct token *initializer(struct expression **tree, struct token *token)
2700 if (match_op(token, '{')) {
2701 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2704 struct token *next = token->next;
2713 token = initializer_list(&expr->expr_list, token->next);
2714 return expect(token, '}', "at end of initializer");
2716 return assignment_expression(token, tree);
2736 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2770 token = statement_list(token->next, &stmt->stmts);
2799 return expect(token, '}', "at end of function");
2845 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2851 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2854 token = declaration_list(token, &args);
2855 if (!match_op(token, ';')) {
2856 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2859 token = token->next;
2860 } while (lookup_type(token));
2864 if (!match_op(token, '{')) {
2865 sparse_error(token->pos, "expected function body");
2866 return token;
2868 return parse_function_body(token, decl, list);
2871 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2873 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2874 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2878 stmt = alloc_statement(token->pos, STMT_NONE);
2881 token = parse_asm_statement(token, stmt);
2884 return token;
2887 struct token *external_declaration(struct token *token, struct symbol_list **list,
2899 if (token_type(token) == TOKEN_IDENT) {
2900 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2902 return s->op->toplevel(token, list);
2906 token = declaration_specifiers(token, &ctx);
2908 decl = alloc_symbol(token->pos, SYM_NODE);
2910 if (match_op(token, ';')) {
2911 apply_modifiers(token->pos, &ctx);
2912 return token->next;
2916 token = declarator(token, &ctx);
2917 token = handle_asm_name(token, &ctx);
2918 token = handle_attributes(token, &ctx);
2919 apply_modifiers(token->pos, &ctx);
2923 decl->endpos = token->pos;
2927 warning(token->pos, "missing identifier in declaration");
2928 return expect(token, ';', "at the end of type declaration");
2970 if (lookup_type(token))
2971 return parse_k_r_arguments(token, decl, list);
2972 if (match_op(token, '{'))
2973 return parse_function_body(token, decl, list);
2978 sparse_error(token->pos, "void declaration");
2986 if (!is_typedef && match_op(token, '=')) {
2987 struct token *next = token->next;
2988 token = initializer(&decl->initializer, next);
2989 if (token == next)
2990 sparse_error(token->pos, "expression expected before '%s'", show_token(token));
3020 else if (match_op(token, ','))
3033 if (!match_op(token, ','))
3036 token = token->next;
3038 decl = alloc_symbol(token->pos, SYM_NODE);
3040 token = handle_attributes(token, &ctx);
3041 token = declarator(token, &ctx);
3042 token = handle_asm_name(token, &ctx);
3043 token = handle_attributes(token, &ctx);
3044 apply_modifiers(token->pos, &ctx);
3047 decl->endpos = token->pos;
3049 sparse_error(token->pos, "expected identifier name in type definition");
3050 return token;
3065 return expect(token, ';', "at end of declaration");