Lines Matching refs:token

51 static struct token *statement(struct token *token, struct statement **tree);
52 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int ke…
54 typedef struct token *declarator_t(struct token *, struct decl_state *);
64 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
65 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
66 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
67 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
68 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
69 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
70 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
71 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
72 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
73 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
74 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
75 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
76 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
77 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
78 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
80 typedef struct token *attr_t(struct token *, struct symbol *,
122 static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod) in asm_modifier() argument
125 warning(token->pos, "duplicated asm modifier"); in asm_modifier()
129 static void asm_modifier_volatile(struct token *token, unsigned long *mods) in asm_modifier_volatile() argument
131 asm_modifier(token, mods, MOD_VOLATILE); in asm_modifier_volatile()
134 static void asm_modifier_inline(struct token *token, unsigned long *mods) in asm_modifier_inline() argument
136 asm_modifier(token, mods, MOD_INLINE); in asm_modifier_inline()
645 static int SENTINEL_ATTR match_idents(struct token *token, ...) in match_idents() argument
650 if (token_type(token) != TOKEN_IDENT) in match_idents()
653 va_start(args, token); in match_idents()
656 } while (next && token->ident != next); in match_idents()
659 return next && token->ident == next; in match_idents()
671 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
706 struct symbol *label_symbol(struct token *token) in label_symbol() argument
708 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL); in label_symbol()
710 sym = alloc_symbol(token->pos, SYM_LABEL); in label_symbol()
711 bind_symbol(sym, token->ident, NS_LABEL); in label_symbol()
717 static struct token *struct_union_enum_specifier(enum type type, in struct_union_enum_specifier()
718 struct token *token, struct decl_state *ctx, in struct_union_enum_specifier() argument
719 struct token *(*parse)(struct token *, struct symbol *)) in struct_union_enum_specifier() argument
724 token = handle_attributes(token, ctx, KW_ATTRIBUTE); in struct_union_enum_specifier()
725 if (token_type(token) == TOKEN_IDENT) { in struct_union_enum_specifier()
726 sym = lookup_symbol(token->ident, NS_STRUCT); in struct_union_enum_specifier()
729 (match_op(token->next,';') || match_op(token->next,'{')))) { in struct_union_enum_specifier()
732 sym = alloc_symbol(token->pos, type); in struct_union_enum_specifier()
733 bind_symbol(sym, token->ident, NS_STRUCT); in struct_union_enum_specifier()
736 error_die(token->pos, "invalid tag applied to %s", show_typename (sym)); in struct_union_enum_specifier()
738 repos = &token->pos; in struct_union_enum_specifier()
739 token = token->next; in struct_union_enum_specifier()
740 if (match_op(token, '{')) { in struct_union_enum_specifier()
747 error_die(token->pos, "redefinition of %s", show_typename (sym)); in struct_union_enum_specifier()
749 token = parse(token->next, sym); in struct_union_enum_specifier()
750 token = expect(token, '}', "at end of struct-union-enum-specifier"); in struct_union_enum_specifier()
752 token = handle_attributes(token, &attr, KW_ATTRIBUTE); in struct_union_enum_specifier()
753 apply_ctype(token->pos, &attr.ctype, &sym->ctype); in struct_union_enum_specifier()
757 sym->endpos = token->pos; in struct_union_enum_specifier()
759 return token; in struct_union_enum_specifier()
763 if (!match_op(token, '{')) { in struct_union_enum_specifier()
764 sparse_error(token->pos, "expected declaration"); in struct_union_enum_specifier()
766 return token; in struct_union_enum_specifier()
769 sym = alloc_symbol(token->pos, type); in struct_union_enum_specifier()
770 token = parse(token->next, sym); in struct_union_enum_specifier()
772 token = expect(token, '}', "at end of specifier"); in struct_union_enum_specifier()
773 sym->endpos = token->pos; in struct_union_enum_specifier()
775 return token; in struct_union_enum_specifier()
778 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym) in parse_struct_declaration() argument
781 struct token *res; in parse_struct_declaration()
782 res = struct_declaration_list(token, &sym->symbol_list); in parse_struct_declaration()
796 static struct token *parse_union_declaration(struct token *token, struct symbol *sym) in parse_union_declaration() argument
798 return struct_declaration_list(token, &sym->symbol_list); in parse_union_declaration()
801 static struct token *struct_specifier(struct token *token, struct decl_state *ctx) in struct_specifier() argument
803 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration); in struct_specifier()
806 static struct token *union_specifier(struct token *token, struct decl_state *ctx) in union_specifier() argument
808 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration); in union_specifier()
916 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent) in parse_enum_declaration() argument
925 while (token_type(token) == TOKEN_IDENT) { in parse_enum_declaration()
927 struct token *next = token->next; in parse_enum_declaration()
941 error_die(token->pos, "can't increment the last enum member"); in parse_enum_declaration()
945 expr = alloc_expression(token->pos, EXPR_VALUE); in parse_enum_declaration()
950 sym = alloc_symbol(token->pos, SYM_NODE); in parse_enum_declaration()
951 bind_symbol(sym, token->ident, NS_SYMBOL); in parse_enum_declaration()
993 sparse_error(token->pos, "bad enum definition"); in parse_enum_declaration()
1001 token = next; in parse_enum_declaration()
1003 sym->endpos = token->pos; in parse_enum_declaration()
1005 if (!match_op(token, ',')) in parse_enum_declaration()
1007 token = token->next; in parse_enum_declaration()
1010 sparse_error(token->pos, "empty enum definition"); in parse_enum_declaration()
1034 return token; in parse_enum_declaration()
1037 return token; in parse_enum_declaration()
1040 static struct token *enum_specifier(struct token *token, struct decl_state *ctx) in enum_specifier() argument
1042 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration); in enum_specifier()
1051 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx) in typeof_specifier() argument
1055 if (!match_op(token, '(')) { in typeof_specifier()
1056 sparse_error(token->pos, "expected '(' after typeof"); in typeof_specifier()
1057 return token; in typeof_specifier()
1059 if (lookup_type(token->next)) { in typeof_specifier()
1060 token = typename(token->next, &sym, NULL); in typeof_specifier()
1062 apply_ctype(token->pos, &sym->ctype, &ctx->ctype); in typeof_specifier()
1064 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF); in typeof_specifier()
1065 token = parse_expression(token->next, &typeof_sym->initializer); in typeof_specifier()
1067 typeof_sym->endpos = token->pos; in typeof_specifier()
1069 sparse_error(token->pos, "expected expression after the '(' token"); in typeof_specifier()
1074 return expect(token, ')', "after typeof"); in typeof_specifier()
1077 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *… in ignore_attribute() argument
1080 if (match_op(token, '(')) in ignore_attribute()
1081 token = parens_expression(token, &expr, "in attribute"); in ignore_attribute()
1082 return token; in ignore_attribute()
1085 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *… in attribute_packed() argument
1089 return token; in attribute_packed()
1092 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state … in attribute_aligned() argument
1097 if (match_op(token, '(')) { in attribute_aligned()
1098 token = parens_expression(token, &expr, "in attribute"); in attribute_aligned()
1103 warning(token->pos, "I don't like non-power-of-2 alignments"); in attribute_aligned()
1104 return token; in attribute_aligned()
1107 return token; in attribute_aligned()
1117 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state… in attribute_modifier() argument
1119 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers); in attribute_modifier()
1120 return token; in attribute_modifier()
1123 static struct token *attribute_ext_visible(struct token *token, struct symbol *attr, struct decl_st… in attribute_ext_visible() argument
1126 return token; in attribute_ext_visible()
1129 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state … in attribute_bitwise() argument
1132 attribute_modifier(token, attr, ctx); in attribute_bitwise()
1133 return token; in attribute_bitwise()
1146 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_… in attribute_address_space() argument
1150 struct token *next; in attribute_address_space()
1152 token = expect(token, '(', "after address_space attribute"); in attribute_address_space()
1153 switch (token_type(token)) { in attribute_address_space()
1155 next = primary_expression(token, &expr); in attribute_address_space()
1161 next = token->next; in attribute_address_space()
1162 as = token->ident; in attribute_address_space()
1165 next = token->next; in attribute_address_space()
1168 warning(token->pos, "invalid address space name"); in attribute_address_space()
1173 sparse_error(token->pos, in attribute_address_space()
1178 token = expect(next, ')', "after address_space attribute"); in attribute_address_space()
1179 return token; in attribute_address_space()
1240 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ct… in attribute_mode() argument
1242 token = expect(token, '(', "after mode attribute"); in attribute_mode()
1243 if (token_type(token) == TOKEN_IDENT) { in attribute_mode()
1244 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD); in attribute_mode()
1248 sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident)); in attribute_mode()
1249 token = token->next; in attribute_mode()
1251 sparse_error(token->pos, "expect attribute mode symbol\n"); in attribute_mode()
1252 token = expect(token, ')', "after mode attribute"); in attribute_mode()
1253 return token; in attribute_mode()
1256 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state … in attribute_context() argument
1262 token = expect(token, '(', "after context attribute"); in attribute_context()
1263 token = conditional_expression(token, &args[0]); in attribute_context()
1264 token = expect(token, ',', "after context 1st argument"); in attribute_context()
1265 token = conditional_expression(token, &args[1]); in attribute_context()
1266 if (match_op(token, ',')) { in attribute_context()
1267 token = token->next; in attribute_context()
1268 token = conditional_expression(token, &args[2]); in attribute_context()
1269 token = expect(token, ')', "after context 3rd argument"); in attribute_context()
1273 token = expect(token, ')', "after context 2nd argument"); in attribute_context()
1278 return token; in attribute_context()
1281 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct dec… in attribute_designated_init() argument
1286 warning(token->pos, "attribute designated_init applied to non-structure type"); in attribute_designated_init()
1287 return token; in attribute_designated_init()
1290 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct d… in attribute_transparent_union() argument
1293 warning(token->pos, "attribute __transparent_union__"); in attribute_transparent_union()
1298 warning(token->pos, "attribute __transparent_union__ applied to non-union type"); in attribute_transparent_union()
1299 return token; in attribute_transparent_union()
1302 static struct token *recover_unknown_attribute(struct token *token) in recover_unknown_attribute() argument
1307 warning(token->pos, "unknown attribute '%s'", show_ident(token->ident)); in recover_unknown_attribute()
1308 token = token->next; in recover_unknown_attribute()
1309 if (match_op(token, '(')) in recover_unknown_attribute()
1310 token = parens_expression(token, &expr, "in attribute"); in recover_unknown_attribute()
1311 return token; in recover_unknown_attribute()
1314 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx) in attribute_specifier() argument
1316 token = expect(token, '(', "after attribute"); in attribute_specifier()
1317 token = expect(token, '(', "after attribute"); in attribute_specifier()
1323 if (eof_token(token)) in attribute_specifier()
1325 if (match_op(token, ';')) in attribute_specifier()
1327 if (token_type(token) != TOKEN_IDENT) in attribute_specifier()
1329 attribute_name = token->ident; in attribute_specifier()
1332 token = attr->op->attribute(token->next, attr, ctx); in attribute_specifier()
1334 token = recover_unknown_attribute(token); in attribute_specifier()
1336 if (!match_op(token, ',')) in attribute_specifier()
1338 token = token->next; in attribute_specifier()
1341 token = expect(token, ')', "after attribute"); in attribute_specifier()
1342 token = expect(token, ')', "after attribute"); in attribute_specifier()
1343 return token; in attribute_specifier()
1389 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx) in typedef_specifier()
1395 static struct token *auto_specifier(struct token *next, struct decl_state *ctx) in auto_specifier()
1401 static struct token *register_specifier(struct token *next, struct decl_state *ctx) in register_specifier()
1407 static struct token *static_specifier(struct token *next, struct decl_state *ctx) in static_specifier()
1413 static struct token *extern_specifier(struct token *next, struct decl_state *ctx) in extern_specifier()
1419 static struct token *thread_specifier(struct token *next, struct decl_state *ctx) in thread_specifier()
1433 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *c… in attribute_force() argument
1435 set_storage_class(&token->pos, ctx, SForced); in attribute_force()
1436 return token; in attribute_force()
1439 static struct token *inline_specifier(struct token *next, struct decl_state *ctx) in inline_specifier()
1445 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx) in noreturn_specifier()
1451 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx) in alignas_specifier() argument
1455 if (!match_op(token, '(')) { in alignas_specifier()
1456 sparse_error(token->pos, "expected '(' after _Alignas"); in alignas_specifier()
1457 return token; in alignas_specifier()
1459 if (lookup_type(token->next)) { in alignas_specifier()
1461 token = typename(token->next, &sym, NULL); in alignas_specifier()
1464 token = expect(token, ')', "after _Alignas(..."); in alignas_specifier()
1467 token = parens_expression(token, &expr, "after _Alignas"); in alignas_specifier()
1469 return token; in alignas_specifier()
1474 warning(token->pos, "non-positive alignment"); in alignas_specifier()
1475 return token; in alignas_specifier()
1478 warning(token->pos, "non-power-of-2 alignment"); in alignas_specifier()
1479 return token; in alignas_specifier()
1483 return token; in alignas_specifier()
1486 static struct token *const_qualifier(struct token *next, struct decl_state *ctx) in const_qualifier()
1492 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx) in volatile_qualifier()
1498 static struct token *restrict_qualifier(struct token *next, struct decl_state *ctx) in restrict_qualifier()
1504 static struct token *atomic_qualifier(struct token *next, struct decl_state *ctx) in atomic_qualifier()
1581 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx) in handle_qualifiers()
1598 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx) in declaration_specifiers() argument
1604 while (token_type(token) == TOKEN_IDENT) { in declaration_specifiers()
1605 struct symbol *s = lookup_symbol(token->ident, in declaration_specifiers()
1614 apply_ctype(token->pos, &s->ctype, &ctx->ctype); in declaration_specifiers()
1615 token = token->next; in declaration_specifiers()
1620 specifier_conflict(token->pos, in declaration_specifiers()
1622 token->ident); in declaration_specifiers()
1633 specifier_conflict(token->pos, in declaration_specifiers()
1641 token = token->next; in declaration_specifiers()
1643 token = s->op->declarator(token, ctx); in declaration_specifiers()
1661 sparse_error(token->pos, "invalid modifier"); in declaration_specifiers()
1662 return token; in declaration_specifiers()
1664 type = alloc_symbol(token->pos, SYM_BASETYPE); in declaration_specifiers()
1672 return token; in declaration_specifiers()
1675 static struct token *abstract_array_static_declarator(struct token *token, int *has_static) in abstract_array_static_declarator() argument
1677 while (token->ident == &static_ident) { in abstract_array_static_declarator()
1679 sparse_error(token->pos, "duplicate array static declarator"); in abstract_array_static_declarator()
1682 token = token->next; in abstract_array_static_declarator()
1684 return token; in abstract_array_static_declarator()
1688 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym) in abstract_array_declarator() argument
1693 token = abstract_array_static_declarator(token, &has_static); in abstract_array_declarator()
1695 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL)) in abstract_array_declarator()
1696 token = abstract_array_static_declarator(token->next, &has_static); in abstract_array_declarator()
1697 token = parse_expression(token, &expr); in abstract_array_declarator()
1699 return token; in abstract_array_declarator()
1702 static struct token *parameter_type_list(struct token *, struct symbol *);
1703 static struct token *identifier_list(struct token *, struct symbol *);
1704 static struct token *declarator(struct token *token, struct decl_state *ctx);
1706 static struct token *skip_attribute(struct token *token) in skip_attribute() argument
1708 token = token->next; in skip_attribute()
1709 if (match_op(token, '(')) { in skip_attribute()
1711 token = token->next; in skip_attribute()
1712 while (depth && !eof_token(token)) { in skip_attribute()
1713 if (token_type(token) == TOKEN_SPECIAL) { in skip_attribute()
1714 if (token->special == '(') in skip_attribute()
1716 else if (token->special == ')') in skip_attribute()
1719 token = token->next; in skip_attribute()
1722 return token; in skip_attribute()
1725 static struct token *skip_attributes(struct token *token) in skip_attributes() argument
1729 if (token_type(token) != TOKEN_IDENT) in skip_attributes()
1731 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF); in skip_attributes()
1736 token = expect(token->next, '(', "after attribute"); in skip_attributes()
1737 token = expect(token, '(', "after attribute"); in skip_attributes()
1739 if (eof_token(token)) in skip_attributes()
1741 if (match_op(token, ';')) in skip_attributes()
1743 if (token_type(token) != TOKEN_IDENT) in skip_attributes()
1745 token = skip_attribute(token); in skip_attributes()
1746 if (!match_op(token, ',')) in skip_attributes()
1748 token = token->next; in skip_attributes()
1750 token = expect(token, ')', "after attribute"); in skip_attributes()
1751 token = expect(token, ')', "after attribute"); in skip_attributes()
1753 return token; in skip_attributes()
1756 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int ke… in handle_attributes() argument
1760 if (token_type(token) != TOKEN_IDENT) in handle_attributes()
1762 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF); in handle_attributes()
1767 token = keyword->op->declarator(token->next, ctx); in handle_attributes()
1770 return token; in handle_attributes()
1773 static int is_nested(struct token *token, struct token **p, in is_nested() argument
1783 struct token *next = token->next; in is_nested()
1803 static enum kind which_func(struct token *token, in which_func() argument
1807 struct token *next = token->next; in which_func()
1814 warning(token->pos, in which_func()
1842 static struct token *direct_declarator(struct token *token, struct decl_state *ctx) in direct_declarator() argument
1845 struct token *next; in direct_declarator()
1848 if (ctx->ident && token_type(token) == TOKEN_IDENT) { in direct_declarator()
1849 *ctx->ident = token->ident; in direct_declarator()
1850 token = token->next; in direct_declarator()
1851 } else if (match_op(token, '(') && in direct_declarator()
1852 is_nested(token, &next, ctx->prefer_abstract)) { in direct_declarator()
1854 if (token->next != next) in direct_declarator()
1855 next = handle_attributes(token->next, ctx, in direct_declarator()
1857 token = declarator(next, ctx); in direct_declarator()
1858 token = expect(token, ')', "in nested declarator"); in direct_declarator()
1864 if (match_op(token, '(')) { in direct_declarator()
1865 enum kind kind = which_func(token, p, ctx->prefer_abstract); in direct_declarator()
1867 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN); in direct_declarator()
1868 token = token->next; in direct_declarator()
1870 token = identifier_list(token, fn); in direct_declarator()
1872 token = parameter_type_list(token, fn); in direct_declarator()
1873 token = expect(token, ')', "in function declarator"); in direct_declarator()
1874 fn->endpos = token->pos; in direct_declarator()
1875 return token; in direct_declarator()
1878 while (match_op(token, '[')) { in direct_declarator()
1880 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY); in direct_declarator()
1881 token = abstract_array_declarator(token->next, array); in direct_declarator()
1882 token = expect(token, ']', "in abstract_array_declarator"); in direct_declarator()
1883 array->endpos = token->pos; in direct_declarator()
1886 return token; in direct_declarator()
1889 static struct token *pointer(struct token *token, struct decl_state *ctx) in pointer() argument
1891 while (match_op(token,'*')) { in pointer()
1892 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR); in pointer()
1903 token = handle_qualifiers(token->next, ctx); in pointer()
1904 ctx->ctype.base_type->endpos = token->pos; in pointer()
1906 return token; in pointer()
1909 static struct token *declarator(struct token *token, struct decl_state *ctx) in declarator() argument
1911 token = pointer(token, ctx); in declarator()
1912 return direct_declarator(token, ctx); in declarator()
1915 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx) in handle_bitfield() argument
1923 sparse_error(token->pos, "invalid bitfield specifier for type %s.", in handle_bitfield()
1926 return conditional_expression(token->next, &expr); in handle_bitfield()
1929 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD); in handle_bitfield()
1930 token = conditional_expression(token->next, &expr); in handle_bitfield()
1935 sparse_error(token->pos, "invalid bitfield width, %lld.", width); in handle_bitfield()
1938 sparse_error(token->pos, "invalid named zero-width bitfield `%s'", in handle_bitfield()
1948 sparse_error(token->pos, "dubious one-bit signed bitfield"); in handle_bitfield()
1955 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'"); in handle_bitfield()
1959 bitfield->endpos = token->pos; in handle_bitfield()
1960 return token; in handle_bitfield()
1963 static struct token *declaration_list(struct token *token, struct symbol_list **list) in declaration_list() argument
1969 token = declaration_specifiers(token, &ctx); in declaration_list()
1973 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE); in declaration_list()
1976 token = declarator(token, &ctx); in declaration_list()
1977 if (match_op(token, ':')) in declaration_list()
1978 token = handle_bitfield(token, &ctx); in declaration_list()
1980 token = handle_attributes(token, &ctx, KW_ATTRIBUTE); in declaration_list()
1981 apply_modifiers(token->pos, &ctx); in declaration_list()
1985 decl->endpos = token->pos; in declaration_list()
1987 if (!match_op(token, ',')) in declaration_list()
1989 token = token->next; in declaration_list()
1992 return token; in declaration_list()
1995 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list) in struct_declaration_list() argument
1997 while (!match_op(token, '}')) { in struct_declaration_list()
1998 if (match_ident(token, &_Static_assert_ident)) { in struct_declaration_list()
1999 token = parse_static_assert(token, NULL); in struct_declaration_list()
2002 if (!match_op(token, ';')) in struct_declaration_list()
2003 token = declaration_list(token, list); in struct_declaration_list()
2004 if (!match_op(token, ';')) { in struct_declaration_list()
2005 sparse_error(token->pos, "expected ; at end of declaration"); in struct_declaration_list()
2008 token = token->next; in struct_declaration_list()
2010 return token; in struct_declaration_list()
2013 static struct token *parameter_declaration(struct token *token, struct symbol *sym) in parameter_declaration() argument
2017 token = declaration_specifiers(token, &ctx); in parameter_declaration()
2019 token = declarator(token, &ctx); in parameter_declaration()
2020 token = handle_attributes(token, &ctx, KW_ATTRIBUTE); in parameter_declaration()
2021 apply_modifiers(token->pos, &ctx); in parameter_declaration()
2024 sym->endpos = token->pos; in parameter_declaration()
2026 return token; in parameter_declaration()
2029 struct token *typename(struct token *token, struct symbol **p, int *forced) in typename() argument
2033 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE); in typename()
2035 token = declaration_specifiers(token, &ctx); in typename()
2036 token = declarator(token, &ctx); in typename()
2037 apply_modifiers(token->pos, &ctx); in typename()
2039 sym->endpos = token->pos; in typename()
2051 return token; in typename()
2054 static struct token *parse_underscore_Pragma(struct token *token) in parse_underscore_Pragma() argument
2056 struct token *next; in parse_underscore_Pragma()
2058 next = token->next; in parse_underscore_Pragma()
2070 static struct token *expression_statement(struct token *token, struct expression **tree) in expression_statement() argument
2072 if (match_ident(token, &_Pragma_ident)) in expression_statement()
2073 return parse_underscore_Pragma(token); in expression_statement()
2075 token = parse_expression(token, tree); in expression_statement()
2076 return expect(token, ';', "at end of statement"); in expression_statement()
2079 static struct token *parse_asm_operands(struct token *token, struct statement *stmt, in parse_asm_operands() argument
2083 if (match_op(token->next, ':') || match_op(token->next, ')')) in parse_asm_operands()
2084 return token->next; in parse_asm_operands()
2086 struct expression *op = alloc_expression(token->pos, EXPR_ASM_OPERAND); in parse_asm_operands()
2087 if (match_op(token->next, '[') && in parse_asm_operands()
2088 token_type(token->next->next) == TOKEN_IDENT && in parse_asm_operands()
2089 match_op(token->next->next->next, ']')) { in parse_asm_operands()
2090 op->name = token->next->next->ident; in parse_asm_operands()
2091 token = token->next->next->next; in parse_asm_operands()
2093 token = primary_expression(token->next, &op->constraint); in parse_asm_operands()
2094 token = parens_expression(token, &op->expr, "in asm parameter"); in parse_asm_operands()
2096 } while (match_op(token, ',')); in parse_asm_operands()
2097 return token; in parse_asm_operands()
2100 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt, in parse_asm_clobbers() argument
2106 token = primary_expression(token->next, &expr); in parse_asm_clobbers()
2109 } while (match_op(token, ',')); in parse_asm_clobbers()
2110 return token; in parse_asm_clobbers()
2113 static struct token *parse_asm_labels(struct token *token, struct statement *stmt, in parse_asm_labels() argument
2119 token = token->next; /* skip ':' and ',' */ in parse_asm_labels()
2120 if (token_type(token) != TOKEN_IDENT) in parse_asm_labels()
2121 return token; in parse_asm_labels()
2122 label = label_symbol(token); in parse_asm_labels()
2124 token = token->next; in parse_asm_labels()
2125 } while (match_op(token, ',')); in parse_asm_labels()
2126 return token; in parse_asm_labels()
2129 static struct token *parse_asm_statement(struct token *token, struct statement *stmt) in parse_asm_statement() argument
2133 token = token->next; in parse_asm_statement()
2135 while (token_type(token) == TOKEN_IDENT) { in parse_asm_statement()
2136 struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF); in parse_asm_statement()
2138 s->op->asm_modifier(token, &mods); in parse_asm_statement()
2139 else if (token->ident == &goto_ident) in parse_asm_statement()
2140 asm_modifier(token, &mods, MOD_ASM_GOTO); in parse_asm_statement()
2141 token = token->next; in parse_asm_statement()
2143 token = expect(token, '(', "after asm"); in parse_asm_statement()
2144 token = parse_expression(token, &stmt->asm_string); in parse_asm_statement()
2145 if (match_op(token, ':')) in parse_asm_statement()
2146 token = parse_asm_operands(token, stmt, &stmt->asm_outputs); in parse_asm_statement()
2147 if (match_op(token, ':')) in parse_asm_statement()
2148 token = parse_asm_operands(token, stmt, &stmt->asm_inputs); in parse_asm_statement()
2149 if (match_op(token, ':')) in parse_asm_statement()
2150 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers); in parse_asm_statement()
2151 if (match_op(token, ':') && (mods & MOD_ASM_GOTO)) in parse_asm_statement()
2152 token = parse_asm_labels(token, stmt, &stmt->asm_labels); in parse_asm_statement()
2153 token = expect(token, ')', "after asm"); in parse_asm_statement()
2154 return expect(token, ';', "at end of asm-statement"); in parse_asm_statement()
2157 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx) in parse_asm_declarator() argument
2160 token = expect(token, '(', "after asm"); in parse_asm_declarator()
2161 token = parse_expression(token->next, &expr); in parse_asm_declarator()
2162 token = expect(token, ')', "after asm"); in parse_asm_declarator()
2163 return token; in parse_asm_declarator()
2166 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused) in parse_static_assert() argument
2170 token = expect(token->next, '(', "after _Static_assert"); in parse_static_assert()
2171 token = constant_expression(token, &cond); in parse_static_assert()
2173 sparse_error(token->pos, "Expected constant expression"); in parse_static_assert()
2174 token = expect(token, ',', "after conditional expression in _Static_assert"); in parse_static_assert()
2175 token = parse_expression(token, &message); in parse_static_assert()
2179 pos = message ? message->pos : token->pos; in parse_static_assert()
2183 token = expect(token, ')', "after diagnostic message in _Static_assert"); in parse_static_assert()
2185 token = expect(token, ';', "after _Static_assert()"); in parse_static_assert()
2190 return token; in parse_static_assert()
2318 static struct token *parse_return_statement(struct token *token, struct statement *stmt) in parse_return_statement() argument
2323 error_die(token->pos, "internal error: return without a function target"); in parse_return_statement()
2326 return expression_statement(token->next, &stmt->ret_value); in parse_return_statement()
2340 static struct token *parse_for_statement(struct token *token, struct statement *stmt) in parse_for_statement() argument
2347 token = expect(token->next, '(', "after 'for'"); in parse_for_statement()
2352 if (lookup_type(token)) { in parse_for_statement()
2353 token = external_declaration(token, &syms, validate_for_loop_decl); in parse_for_statement()
2355 token = parse_expression(token, &e1); in parse_for_statement()
2356 token = expect(token, ';', "in 'for'"); in parse_for_statement()
2358 token = parse_expression(token, &e2); in parse_for_statement()
2359 token = expect(token, ';', "in 'for'"); in parse_for_statement()
2360 token = parse_expression(token, &e3); in parse_for_statement()
2361 token = expect(token, ')', "in 'for'"); in parse_for_statement()
2362 token = statement(token, &iterator); in parse_for_statement()
2372 return token; in parse_for_statement()
2375 static struct token *parse_while_statement(struct token *token, struct statement *stmt) in parse_while_statement() argument
2381 token = parens_expression(token->next, &expr, "after 'while'"); in parse_while_statement()
2382 token = statement(token, &iterator); in parse_while_statement()
2389 return token; in parse_while_statement()
2392 static struct token *parse_do_statement(struct token *token, struct statement *stmt) in parse_do_statement() argument
2398 token = statement(token->next, &iterator); in parse_do_statement()
2399 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident) in parse_do_statement()
2400 token = token->next; in parse_do_statement()
2402 sparse_error(token->pos, "expected 'while' after 'do'"); in parse_do_statement()
2403 token = parens_expression(token, &expr, "after 'do-while'"); in parse_do_statement()
2412 return expect(token, ';', "after statement"); in parse_do_statement()
2415 static struct token *parse_if_statement(struct token *token, struct statement *stmt) in parse_if_statement() argument
2418 token = parens_expression(token->next, &stmt->if_conditional, "after if"); in parse_if_statement()
2419 token = statement(token, &stmt->if_true); in parse_if_statement()
2420 if (token_type(token) != TOKEN_IDENT) in parse_if_statement()
2421 return token; in parse_if_statement()
2422 if (token->ident != &else_ident) in parse_if_statement()
2423 return token; in parse_if_statement()
2424 return statement(token->next, &stmt->if_false); in parse_if_statement()
2427 static inline struct token *case_statement(struct token *token, struct statement *stmt) in case_statement() argument
2430 token = expect(token, ':', "after default/case"); in case_statement()
2432 return statement(token, &stmt->case_statement); in case_statement()
2435 static struct token *parse_case_statement(struct token *token, struct statement *stmt) in parse_case_statement() argument
2437 token = parse_expression(token->next, &stmt->case_expression); in parse_case_statement()
2438 if (match_op(token, SPECIAL_ELLIPSIS)) in parse_case_statement()
2439 token = parse_expression(token->next, &stmt->case_to); in parse_case_statement()
2440 return case_statement(token, stmt); in parse_case_statement()
2443 static struct token *parse_default_statement(struct token *token, struct statement *stmt) in parse_default_statement() argument
2445 return case_statement(token->next, stmt); in parse_default_statement()
2448 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt) in parse_loop_iterator() argument
2450 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR); in parse_loop_iterator()
2455 return expect(token->next, ';', "at end of statement"); in parse_loop_iterator()
2458 static struct token *parse_switch_statement(struct token *token, struct statement *stmt) in parse_switch_statement() argument
2462 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'"); in parse_switch_statement()
2463 token = statement(token, &stmt->switch_statement); in parse_switch_statement()
2465 return token; in parse_switch_statement()
2468 static struct token *parse_goto_statement(struct token *token, struct statement *stmt) in parse_goto_statement() argument
2471 token = token->next; in parse_goto_statement()
2472 if (match_op(token, '*')) { in parse_goto_statement()
2473 token = parse_expression(token->next, &stmt->goto_expression); in parse_goto_statement()
2475 } else if (token_type(token) == TOKEN_IDENT) { in parse_goto_statement()
2476 stmt->goto_label = label_symbol(token); in parse_goto_statement()
2477 token = token->next; in parse_goto_statement()
2479 sparse_error(token->pos, "Expected identifier or goto expression"); in parse_goto_statement()
2481 return expect(token, ';', "at end of statement"); in parse_goto_statement()
2484 static struct token *parse_context_statement(struct token *token, struct statement *stmt) in parse_context_statement() argument
2487 token = token->next; in parse_context_statement()
2488 token = expect(token, '(', "after __context__ statement"); in parse_context_statement()
2489 token = assignment_expression(token, &stmt->expression); in parse_context_statement()
2491 unexpected(token, "expression expected after '('"); in parse_context_statement()
2492 if (match_op(token, ',')) { in parse_context_statement()
2493 token = token->next; in parse_context_statement()
2495 token = assignment_expression(token, &stmt->expression); in parse_context_statement()
2497 unexpected(token, "expression expected after ','"); in parse_context_statement()
2499 token = expect(token, ')', "at end of __context__ statement"); in parse_context_statement()
2500 return expect(token, ';', "at end of statement"); in parse_context_statement()
2503 static struct token *parse_range_statement(struct token *token, struct statement *stmt) in parse_range_statement() argument
2506 token = token->next; in parse_range_statement()
2507 token = expect(token, '(', "after __range__ statement"); in parse_range_statement()
2508 token = assignment_expression(token, &stmt->range_expression); in parse_range_statement()
2509 token = expect(token, ',', "after range expression"); in parse_range_statement()
2510 token = assignment_expression(token, &stmt->range_low); in parse_range_statement()
2511 token = expect(token, ',', "after low range"); in parse_range_statement()
2512 token = assignment_expression(token, &stmt->range_high); in parse_range_statement()
2513 token = expect(token, ')', "after range statement"); in parse_range_statement()
2514 return expect(token, ';', "after range statement"); in parse_range_statement()
2517 static struct token *statement(struct token *token, struct statement **tree) in statement() argument
2519 struct statement *stmt = alloc_statement(token->pos, STMT_NONE); in statement()
2522 if (token_type(token) == TOKEN_IDENT) { in statement()
2523 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD); in statement()
2525 return s->op->statement(token, stmt); in statement()
2527 if (match_op(token->next, ':')) { in statement()
2528 struct symbol *s = label_symbol(token); in statement()
2529 token = skip_attributes(token->next->next); in statement()
2533 return statement(token, tree); in statement()
2538 return statement(token, &stmt->label_statement); in statement()
2542 if (match_op(token, '{')) { in statement()
2545 token = compound_statement(token->next, stmt); in statement()
2548 return expect(token, '}', "at end of compound statement"); in statement()
2552 return expression_statement(token, &stmt->expression); in statement()
2556 static struct token *label_statement(struct token *token) in label_statement() argument
2558 while (token_type(token) == TOKEN_IDENT) { in label_statement()
2559 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL); in label_statement()
2561 bind_symbol(sym, token->ident, NS_SYMBOL); in label_statement()
2564 token = token->next; in label_statement()
2565 if (!match_op(token, ',')) in label_statement()
2567 token = token->next; in label_statement()
2569 return expect(token, ';', "at end of label declaration"); in label_statement()
2572 static struct token * statement_list(struct token *token, struct statement_list **list) in statement_list() argument
2575 while (token_type(token) == TOKEN_IDENT && in statement_list()
2576 token->ident == &__label___ident) in statement_list()
2577 token = label_statement(token->next); in statement_list()
2580 if (eof_token(token)) in statement_list()
2582 if (match_op(token, '}')) in statement_list()
2584 if (match_ident(token, &_Static_assert_ident)) { in statement_list()
2585 token = parse_static_assert(token, NULL); in statement_list()
2588 if (lookup_type(token)) { in statement_list()
2590 warning(token->pos, "mixing declarations and code"); in statement_list()
2593 stmt = alloc_statement(token->pos, STMT_DECLARATION); in statement_list()
2594 token = external_declaration(token, &stmt->declaration, NULL); in statement_list()
2597 token = statement(token, &stmt); in statement_list()
2601 return token; in statement_list()
2604 static struct token *identifier_list(struct token *token, struct symbol *fn) in identifier_list() argument
2608 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE); in identifier_list()
2609 sym->ident = token->ident; in identifier_list()
2610 token = token->next; in identifier_list()
2611 sym->endpos = token->pos; in identifier_list()
2614 if (!match_op(token, ',') || in identifier_list()
2615 token_type(token->next) != TOKEN_IDENT || in identifier_list()
2616 lookup_type(token->next)) in identifier_list()
2618 token = token->next; in identifier_list()
2620 return token; in identifier_list()
2623 static struct token *parameter_type_list(struct token *token, struct symbol *fn) in parameter_type_list() argument
2630 if (match_op(token, SPECIAL_ELLIPSIS)) { in parameter_type_list()
2632 token = token->next; in parameter_type_list()
2636 sym = alloc_symbol(token->pos, SYM_NODE); in parameter_type_list()
2637 token = parameter_declaration(token, sym); in parameter_type_list()
2642 warning(token->pos, "void parameter"); in parameter_type_list()
2645 if (!match_op(token, ',')) in parameter_type_list()
2647 token = token->next; in parameter_type_list()
2649 return token; in parameter_type_list()
2652 struct token *compound_statement(struct token *token, struct statement *stmt) in compound_statement() argument
2654 token = statement_list(token, &stmt->stmts); in compound_statement()
2655 return token; in compound_statement()
2658 static struct expression *identifier_expression(struct token *token) in identifier_expression() argument
2660 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER); in identifier_expression()
2661 expr->expr_ident = token->ident; in identifier_expression()
2682 static struct token *single_initializer(struct expression **ep, struct token *token) in single_initializer() argument
2685 struct token *next = token->next; in single_initializer()
2691 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) { in single_initializer()
2692 struct expression *expr = identifier_expression(token); in single_initializer()
2694 warning(token->pos, "obsolete struct initializer, use C99 syntax"); in single_initializer()
2695 token = initializer(&expr->ident_expression, next->next); in single_initializer()
2698 return token; in single_initializer()
2701 for (tail = ep, nested = 0; ; nested++, next = token->next) { in single_initializer()
2702 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) { in single_initializer()
2707 token = next->next; in single_initializer()
2708 } else if (match_op(token, '[')) { in single_initializer()
2710 token = constant_expression(token->next, &from); in single_initializer()
2712 sparse_error(token->pos, "Expected constant expression"); in single_initializer()
2715 if (match_op(token, SPECIAL_ELLIPSIS)) in single_initializer()
2716 token = constant_expression(token->next, &to); in single_initializer()
2720 token = expect(token, ']', "at end of initializer index"); in single_initializer()
2728 if (!match_op(token, '=')) in single_initializer()
2729 warning(token->pos, "obsolete array initializer, use C99 syntax"); in single_initializer()
2734 token = expect(token, '=', "at end of initializer index"); in single_initializer()
2736 token = initializer(tail, token); in single_initializer()
2739 return token; in single_initializer()
2742 static struct token *initializer_list(struct expression_list **list, struct token *token) in initializer_list() argument
2747 token = single_initializer(&expr, token); in initializer_list()
2751 if (!match_op(token, ',')) in initializer_list()
2753 token = token->next; in initializer_list()
2755 return token; in initializer_list()
2758 struct token *initializer(struct expression **tree, struct token *token) in initializer() argument
2760 if (match_op(token, '{')) { in initializer()
2761 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER); in initializer()
2763 token = initializer_list(&expr->expr_list, token->next); in initializer()
2764 return expect(token, '}', "at end of initializer"); in initializer()
2766 return assignment_expression(token, tree); in initializer()
2802 static struct token *parse_function_body(struct token *token, struct symbol *decl, in parse_function_body() argument
2836 token = compound_statement(token->next, stmt); in parse_function_body()
2873 return expect(token, '}', "at end of function"); in parse_function_body()
2919 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl, in parse_k_r_arguments() argument
2925 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident)); in parse_k_r_arguments()
2928 token = declaration_list(token, &args); in parse_k_r_arguments()
2929 if (!match_op(token, ';')) { in parse_k_r_arguments()
2930 sparse_error(token->pos, "expected ';' at end of parameter declaration"); in parse_k_r_arguments()
2933 token = token->next; in parse_k_r_arguments()
2934 } while (lookup_type(token)); in parse_k_r_arguments()
2938 if (!match_op(token, '{')) { in parse_k_r_arguments()
2939 sparse_error(token->pos, "expected function body"); in parse_k_r_arguments()
2940 return token; in parse_k_r_arguments()
2942 return parse_function_body(token, decl, list); in parse_k_r_arguments()
2945 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list) in toplevel_asm_declaration() argument
2947 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE); in toplevel_asm_declaration()
2948 struct symbol *fn = alloc_symbol(token->pos, SYM_FN); in toplevel_asm_declaration()
2952 stmt = alloc_statement(token->pos, STMT_NONE); in toplevel_asm_declaration()
2955 token = parse_asm_statement(token, stmt); in toplevel_asm_declaration()
2958 return token; in toplevel_asm_declaration()
2961 struct token *external_declaration(struct token *token, struct symbol_list **list, in external_declaration() argument
2972 if (match_ident(token, &_Pragma_ident)) in external_declaration()
2973 return parse_underscore_Pragma(token); in external_declaration()
2976 if (token_type(token) == TOKEN_IDENT) { in external_declaration()
2977 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD); in external_declaration()
2979 return s->op->toplevel(token, list); in external_declaration()
2983 token = declaration_specifiers(token, &ctx); in external_declaration()
2986 decl = alloc_symbol(token->pos, SYM_NODE); in external_declaration()
2988 if (match_op(token, ';')) { in external_declaration()
2989 apply_modifiers(token->pos, &ctx); in external_declaration()
2990 return token->next; in external_declaration()
2994 token = declarator(token, &ctx); in external_declaration()
2995 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM); in external_declaration()
2996 apply_modifiers(token->pos, &ctx); in external_declaration()
3000 decl->endpos = token->pos; in external_declaration()
3004 warning(token->pos, "missing identifier in declaration"); in external_declaration()
3005 return expect(token, ';', "at the end of type declaration"); in external_declaration()
3039 if (lookup_type(token)) in external_declaration()
3040 return parse_k_r_arguments(token, decl, list); in external_declaration()
3041 if (match_op(token, '{')) in external_declaration()
3042 return parse_function_body(token, decl, list); in external_declaration()
3047 sparse_error(token->pos, "void declaration"); in external_declaration()
3055 if (!is_typedef && match_op(token, '=')) { in external_declaration()
3056 token = initializer(&decl->initializer, token->next); in external_declaration()
3082 if (!match_op(token, ',')) in external_declaration()
3085 token = token->next; in external_declaration()
3087 decl = alloc_symbol(token->pos, SYM_NODE); in external_declaration()
3089 token = handle_attributes(token, &ctx, KW_ATTRIBUTE); in external_declaration()
3090 token = declarator(token, &ctx); in external_declaration()
3091 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM); in external_declaration()
3092 apply_modifiers(token->pos, &ctx); in external_declaration()
3095 decl->endpos = token->pos; in external_declaration()
3097 sparse_error(token->pos, "expected identifier name in type definition"); in external_declaration()
3098 return token; in external_declaration()
3113 return expect(token, ';', "at end of declaration"); in external_declaration()