diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-10-08 01:46:45 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-10-08 01:46:45 +0200 |
| commit | d392f0c54a7748b816ab327758ae835f73cd1c0a (patch) | |
| tree | a7c029dffab842acbd4f5f0cab5685a93e9b9747 /boot/name_analysis.cc | |
| parent | d4b7f5e52a33f2d15aafca7f1b040f3f2821fd0c (diff) | |
| download | elna-d392f0c54a7748b816ab327758ae835f73cd1c0a.tar.gz | |
Allow initialized declarations without an explicit type
Diffstat (limited to 'boot/name_analysis.cc')
| -rw-r--r-- | boot/name_analysis.cc | 48 |
1 files changed, 44 insertions, 4 deletions
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<constant_value> 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<constant_type>(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<constant_type>() != 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<constant_type>() != nullptr)) + { const source_position position_span = source_position( declaration->identifiers.front().id().position().start(), declaration->identifiers.back().id().position().end()); add_error<declaration_format_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<constant_type>(variable_type)); + } + } + else + { + variable_type = resolve_type(*declaration->variable_type); + } } - else if (resolve_aliases(variable_type).get<constant_type>() != nullptr) + else { + if (declaration->variable_type != nullptr) + { + variable_type = resolve_type(*declaration->variable_type); + } + if (declaration->variable_type == nullptr + || resolve_aliases(variable_type).get<constant_type>() != nullptr) + { const source_position position_span = source_position( declaration->identifiers.front().id().position().start(), declaration->identifiers.back().id().position().end()); add_error<declaration_format_error>(position_span, declaration_format_error::not_initialized{ extract_identifiers(declaration->identifiers) }); + } } for (const auto& variable_identifier : declaration->identifiers) { |
