aboutsummaryrefslogtreecommitdiff
path: root/src/check_type.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2018-08-30 11:12:57 +0100
committergingerBill <bill@gingerbill.org>2018-08-30 11:12:57 +0100
commit12256beeb25ad33880cef5a8a808a40d80fa3c10 (patch)
tree139720e90c757673e3f1bea755b0f4896679ed9e /src/check_type.cpp
parent0858ae202488437f1634b8afef9fe70efd694483 (diff)
Prevent other parameters being the default value
Diffstat (limited to 'src/check_type.cpp')
-rw-r--r--src/check_type.cpp28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/check_type.cpp b/src/check_type.cpp
index f3ebf50a0..e56b20134 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -1008,6 +1008,20 @@ Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Oper
return t_invalid;
}
+bool is_expr_from_another_parameter(CheckerContext *ctx, Ast *expr) {
+ if (expr->kind == Ast_SelectorExpr) {
+ Ast *lhs = expr->SelectorExpr.expr;
+ return is_expr_from_another_parameter(ctx, lhs);
+ } else if (expr->kind == Ast_Ident) {
+ Operand x= {};
+ Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
+ if (e->flags & EntityFlag_Param) {
+ return true;
+ }
+ }
+ return false;
+}
+
ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
ParameterValue param_value = {};
@@ -1054,9 +1068,17 @@ ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type *
param_value.value = exact_value_procedure(e->identifier);
add_entity_use(ctx, e->identifier, e);
} else {
- param_value.kind = ParameterValue_Value;
- param_value.ast_value = expr;
- add_entity_use(ctx, e->identifier, e);
+ if (e->flags & EntityFlag_Param) {
+ error(expr, "Default parameter cannot be another parameter");
+ } else {
+ if (is_expr_from_another_parameter(ctx, expr)) {
+ error(expr, "Default parameter cannot be another parameter");
+ } else {
+ param_value.kind = ParameterValue_Value;
+ param_value.ast_value = expr;
+ add_entity_use(ctx, e->identifier, e);
+ }
+ }
}
} else if (allow_caller_location && o.mode == Addressing_Context) {
param_value.kind = ParameterValue_Value;