diff --git a/src/compiler/parse_expr.c b/src/compiler/parse_expr.c index 9105b5464..59f1bca06 100644 --- a/src/compiler/parse_expr.c +++ b/src/compiler/parse_expr.c @@ -74,6 +74,34 @@ bool parse_range(ParseContext *c, Range *range) return true; } +static bool parse_expr_list(ParseContext *c, Expr ***exprs_ref, TokenType end_token) +{ + while (!try_consume(c, end_token)) + { + ASSIGN_EXPR_OR_RET(Expr *expr, parse_expr(c), false); + vec_add(*exprs_ref, expr); + if (!try_consume(c, TOKEN_COMMA)) + { + CONSUME_OR_RET(end_token, false); + return true; + } + } + return true; +} + +bool parse_expr_list_no_trail(ParseContext *c, Expr ***exprs_ref, TokenType end_token) +{ + if (try_consume(c, end_token)) return true; + while (true) + { + ASSIGN_EXPR_OR_RET(Expr *expr, parse_expr(c), false); + vec_add(*exprs_ref, expr); + if (try_consume(c, TOKEN_COMMA)) continue; + CONSUME_OR_RET(end_token, false); + return true; + } +} + /** * rethrow_expr ::= call_expr '!' */ @@ -955,14 +983,7 @@ static Expr *parse_generic_expr(ParseContext *c, Expr *left) Expr *subs_expr = expr_new_expr(EXPR_GENERIC_IDENT, left); subs_expr->generic_ident_expr.parent = exprid(left); - Expr **exprs = NULL; - do - { - ASSIGN_EXPR_OR_RET(Expr *param, parse_expr(c), poisoned_expr); - vec_add(exprs, param); - } while (try_consume(c, TOKEN_COMMA)); - CONSUME_OR_RET(TOKEN_RGENPAR, poisoned_expr); - subs_expr->generic_ident_expr.parmeters = exprs; + if (!parse_expr_list_no_trail(c, &subs_expr->generic_ident_expr.parmeters, TOKEN_RGENPAR)) return poisoned_expr; RANGE_EXTEND_PREV(subs_expr); return subs_expr; } @@ -1027,19 +1048,8 @@ static Expr *parse_ct_defined(ParseContext *c, Expr *left) assert(!left && "Unexpected left hand side"); Expr *defined = expr_new(EXPR_CT_DEFINED, c->span); advance(c); - Expr **list = NULL; CONSUME_OR_RET(TOKEN_LPAREN, poisoned_expr); - while (!try_consume(c, TOKEN_RPAREN)) - { - ASSIGN_EXPR_OR_RET(Expr *expr, parse_expr(c), poisoned_expr); - vec_add(list, expr); - if (!try_consume(c, TOKEN_COMMA)) - { - CONSUME_OR_RET(TOKEN_RPAREN, poisoned_expr); - break; - } - } - defined->expression_list = list; + if (!parse_expr_list(c, &defined->expression_list, TOKEN_RPAREN)) return poisoned_expr; return defined; } @@ -1112,8 +1122,7 @@ static Expr *parse_ct_concat_append(ParseContext *c, Expr *left) advance(c); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_expr); - if (!parse_arg_list(c, &expr->ct_concat, TOKEN_RPAREN, NULL, true)) return poisoned_expr; - CONSUME_OR_RET(TOKEN_RPAREN, poisoned_expr); + if (!parse_expr_list(c, &expr->ct_concat, TOKEN_RPAREN)) return poisoned_expr; RANGE_EXTEND_PREV(expr); return expr; } @@ -1147,16 +1156,7 @@ static Expr *parse_ct_and_or(ParseContext *c, Expr *left) expr->ct_and_or_expr.is_and = tok_is(c, TOKEN_CT_AND); advance(c); CONSUME_OR_RET(TOKEN_LPAREN, poisoned_expr); - Expr **exprs = NULL; - while (true) - { - ASSIGN_EXPR_OR_RET(Expr* internal, parse_expr(c), poisoned_expr); - vec_add(exprs, internal); - if (try_consume(c, TOKEN_COMMA)) continue; - CONSUME_OR_RET(TOKEN_RPAREN, poisoned_expr); - break; - } - expr->ct_and_or_expr.args = exprs; + if (!parse_expr_list(c, &expr->ct_and_or_expr.args, TOKEN_RPAREN)) return poisoned_expr; RANGE_EXTEND_PREV(expr); return expr; } diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index f9a8952b9..20b75a8f1 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -497,15 +497,8 @@ static inline TypeInfo *parse_generic_type(ParseContext *c, TypeInfo *type) assert(type_info_ok(type)); advance_and_verify(c, TOKEN_LGENPAR); - Expr **exprs = NULL; - do - { - ASSIGN_EXPR_OR_RET(Expr *param, parse_expr(c), poisoned_type_info); - vec_add(exprs, param); - } while (try_consume(c, TOKEN_COMMA)); - CONSUME_OR_RET(TOKEN_RGENPAR, poisoned_type_info); TypeInfo *generic_type = type_info_new(TYPE_INFO_GENERIC, type->span); - generic_type->generic.params = exprs; + if (!parse_expr_list_no_trail(c, &generic_type->generic.params, TOKEN_RGENPAR)) return poisoned_type_info; generic_type->generic.base = type; return generic_type; } @@ -1934,9 +1927,7 @@ static inline Decl *parse_def_ident(ParseContext *c) if (try_consume(c, TOKEN_LGENPAR)) { decl->define_decl.define_kind = DEFINE_IDENT_GENERIC; - Expr **params = parse_generic_parameters(c); - if (!params) return poisoned_decl; - decl->define_decl.generic_params = params; + if (!parse_expr_list_no_trail(c, &decl->define_decl.generic_params, TOKEN_RGENPAR)) return poisoned_decl; } if (!parse_attributes_for_global(c, decl)) return poisoned_decl; diff --git a/src/compiler/parser_internal.h b/src/compiler/parser_internal.h index 243187d10..f150ce06d 100644 --- a/src/compiler/parser_internal.h +++ b/src/compiler/parser_internal.h @@ -54,6 +54,7 @@ Expr *parse_expression_list(ParseContext *c, bool allow_decls); Decl *parse_local_decl_after_type(ParseContext *c, TypeInfo *type); Decl *parse_var_decl(ParseContext *c); bool parse_current_is_expr(ParseContext *c); +bool parse_expr_list_no_trail(ParseContext *c, Expr ***exprs_ref, TokenType end_token); bool parse_parameters(ParseContext *c, Decl ***params_ref, Decl **body_params, Variadic *variadic, int *vararg_index_ref, ParameterParseKind parse_kind); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 02e7777d7..043edc814 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -147,7 +147,6 @@ INLINE bool sema_call_expand_arguments(SemaContext *context, CalledDecl *callee, static inline int sema_call_find_index_of_named_parameter(SemaContext *context, Decl **func_params, Expr *expr); static inline bool sema_call_check_contract_param_match(SemaContext *context, Decl *param, Expr *expr); static bool sema_call_analyse_body_expansion(SemaContext *macro_context, Expr *call); -static bool sema_flattened_expr_is_const_initializer(SemaContext *context, Expr *expr); static bool sema_slice_len_is_in_range(SemaContext *context, Type *type, Expr *len_expr, bool from_end, bool *remove_from_end); static bool sema_slice_index_is_in_range(SemaContext *context, Type *type, Expr *index_expr, bool end_index, bool from_end, bool *remove_from_end); static Expr **sema_vasplat_append(SemaContext *context, Expr **init_expressions, Expr *expr); @@ -4101,12 +4100,6 @@ bool sema_flattened_expr_is_const(SemaContext *context, Expr *expr) return expr_is_const(expr); } -static bool sema_flattened_expr_is_const_initializer(SemaContext *context, Expr *expr) -{ - sema_expr_flatten_const(context, expr); - return expr_is_const_initializer(expr); -} - /** * Analyse "x.y" */ @@ -5435,19 +5428,6 @@ static bool sema_expr_analyse_sub(SemaContext *context, Expr *expr, Expr *left, } -INLINE bool sema_is_valid_pointer_offset_type(SemaContext *context, Type *canonical_type, - Expr *ptr_expr, Expr *add_expr, const char *error) -{ - if (!type_is_integer(canonical_type)) - { - RETURN_SEMA_ERROR(ptr_expr, "A value of type '%s' cannot %s '%s', an integer was expected here.", - type_to_error_string(add_expr->type), - error, - type_to_error_string(add_expr->type)); - } - return true; -} - /** * Analyse a + b * @return true if it succeeds.