diff options
| author | Ginger Bill <bill@gingerbill.org> | 2017-10-01 20:10:13 +0100 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2017-10-01 20:10:13 +0100 |
| commit | 1f24f105cc49cff6bbe1034215ae86cb4450f831 (patch) | |
| tree | baaa36721f662c9d6c98b6fa49996110174a3c7c /src/check_expr.cpp | |
| parent | 8f39ebbe5a7628e8d4597d39f9253c23fead53a6 (diff) | |
"Constant" procedure values for default values in structs
Diffstat (limited to 'src/check_expr.cpp')
| -rw-r--r-- | src/check_expr.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index dd172634b..afa8327a0 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -58,6 +58,7 @@ void check_expr_with_type_hint (Checker *c, Operand *o, AstNode *e, Typ Type * check_type (Checker *c, AstNode *expression, Type *named_type = nullptr); void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def); Entity * check_selector (Checker *c, Operand *operand, AstNode *node, Type *type_hint); +Entity * check_ident (Checker *c, Operand *o, AstNode *n, Type *named_type, Type *type_hint, bool allow_import_name); void check_not_tuple (Checker *c, Operand *operand); void convert_to_typed (Checker *c, Operand *operand, Type *target_type, i32 level); gbString expr_to_string (AstNode *expression); @@ -1037,9 +1038,23 @@ Array<Entity *> check_struct_fields(Checker *c, AstNode *node, Array<AstNode *> } else if (o.mode != Addressing_Constant) { if (default_value->kind == AstNode_ProcLit) { value = exact_value_procedure(default_value); - // error(default_value, "A procedure literal as a default param is not yet supported"); } else { - error(default_value, "Default parameter must be a constant"); + Entity *e = nullptr; + if (o.mode == Addressing_Value && is_type_proc(o.type)) { + Operand x = {}; + if (default_value->kind == AstNode_Ident) { + e = check_ident(c, &x, default_value, nullptr, nullptr, false); + } else if (default_value->kind == AstNode_SelectorExpr) { + e = check_selector(c, &x, default_value, nullptr); + } + } + + if (e != nullptr && e->kind == Entity_Procedure) { + value = exact_value_procedure(e->identifier); + add_entity_use(c, e->identifier, e); + } else { + error(default_value, "Default parameter must be a constant"); + } } } else { value = o.value; |