From d392f0c54a7748b816ab327758ae835f73cd1c0a Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 8 Oct 2026 01:46:45 +0200 Subject: Allow initialized declarations without an explicit type --- boot/name_analysis.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'boot/name_analysis.cc') diff --git a/boot/name_analysis.cc b/boot/name_analysis.cc index 5ad2a75..c82f211 100644 --- a/boot/name_analysis.cc +++ b/boot/name_analysis.cc @@ -1447,12 +1447,24 @@ namespace elna::boot void declaration_visitor::visit(variable_declaration *declaration) { - const type variable_type = resolve_type(declaration->variable_type()); + type variable_type; std::optional computed; if (declaration->initializer != nullptr) { declaration->initializer->accept(this); + if (declaration->variable_type == nullptr) + { + variable_type = declaration->initializer->type_decoration; + if (declaration->is_const) + { + variable_type = type(std::make_shared(variable_type)); + } + } + else + { + variable_type = resolve_type(*declaration->variable_type); + } computed = this->constant_evaluator.evaluate(*declaration->initializer); if (!computed.has_value()) @@ -1461,13 +1473,21 @@ namespace elna::boot non_constant_expression_error::initializer{ extract_identifiers(declaration->identifiers) }); } } - else if (!declaration->is_extern && resolve_aliases(variable_type).get() != nullptr) + else { + if (declaration->variable_type != nullptr) + { + variable_type = resolve_type(*declaration->variable_type); + } + if (!declaration->is_extern && (declaration->variable_type == nullptr + || resolve_aliases(variable_type).get() != nullptr)) + { const source_position position_span = source_position( declaration->identifiers.front().id().position().start(), declaration->identifiers.back().id().position().end()); add_error(position_span, declaration_format_error::not_initialized{ extract_identifiers(declaration->identifiers) }); + } } for (const auto& variable_identifier : declaration->identifiers) { @@ -1567,19 +1587,39 @@ namespace elna::boot { if (!this->bag.is_global()) { - const type variable_type = resolve_type(declaration->variable_type()); + type variable_type; if (declaration->initializer != nullptr) { declaration->initializer->accept(this); + if (declaration->variable_type == nullptr) + { + variable_type = declaration->initializer->type_decoration; + if (declaration->is_const) + { + variable_type = type(std::make_shared(variable_type)); + } + } + else + { + variable_type = resolve_type(*declaration->variable_type); + } } - else if (resolve_aliases(variable_type).get() != nullptr) + else { + if (declaration->variable_type != nullptr) + { + variable_type = resolve_type(*declaration->variable_type); + } + if (declaration->variable_type == nullptr + || resolve_aliases(variable_type).get() != nullptr) + { const source_position position_span = source_position( declaration->identifiers.front().id().position().start(), declaration->identifiers.back().id().position().end()); add_error(position_span, declaration_format_error::not_initialized{ extract_identifiers(declaration->identifiers) }); + } } for (const auto& variable_identifier : declaration->identifiers) { -- cgit v1.2.3