• Home
  • Raw
  • Download

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