From bd2b80d6ac582b9f0bcd96c86d4b79bfdb64eca1 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 11 Sep 2026 09:11:47 +0200 Subject: Implement aligned attribute --- boot/ast.cc | 69 ++++++++++++++++++++++++++++ boot/evaluator.cc | 14 ++++-- boot/materialization.cc | 48 +++++++++++++++++++ boot/name_analysis.cc | 119 ++++++++++++++++++++++++++++++++++++++---------- boot/parser.yy | 76 ++++++++++++++++++++++++------- boot/result.cc | 33 -------------- boot/symbol.cc | 9 +++- boot/type_check.cc | 28 ++++++------ 8 files changed, 301 insertions(+), 95 deletions(-) (limited to 'boot') diff --git a/boot/ast.cc b/boot/ast.cc index 9dbcf7f..08d3a78 100644 --- a/boot/ast.cc +++ b/boot/ast.cc @@ -17,6 +17,7 @@ along with GCC; see the file COPYING3. If not see #include "elna/boot/ast.h" +#include #include namespace elna::boot @@ -588,6 +589,74 @@ namespace elna::boot return nullptr; } + attribute::attribute(identifier&& name, std::vector arguments) + : m_name(std::move(name)), m_arguments(std::move(arguments)) + { + } + + attribute::attribute(attribute&& that) noexcept + : m_name(std::move(that.m_name)), m_arguments(std::move(that.m_arguments)) + { + } + + attribute::~attribute() + { + for (const expression *argument : this->arguments()) + { + delete argument; + } + } + + attribute& attribute::operator=(attribute&& that) noexcept + { + std::swap(this->m_name, that.m_name); + std::swap(this->m_arguments, that.m_arguments); + + return *this; + } + + const identifier& attribute::name() const + { + return this->m_name; + } + + const std::vector& attribute::arguments() const + { + return this->m_arguments; + } + + identifier_definition::identifier_definition(const std::string& name, + const source_position& position, const bool exported, std::vector&& attributes) + : attributes(std::move(attributes)), m_identifier(name, position), m_exported(exported) + { + } + + const std::string& identifier_definition::name() const + { + return this->m_identifier.name(); + } + + const identifier& identifier_definition::id() const + { + return this->m_identifier; + } + + bool identifier_definition::exported() const + { + return this->m_exported; + } + + std::vector extract_identifiers(const std::vector& identifiers) + { + std::vector result; + result.reserve(identifiers.size()); + + std::ranges::transform(identifiers, std::back_inserter(result), + [](const auto& identifier) { return identifier.id(); }); + + return result; + } + named_expression *type_expression::is_named() { return nullptr; diff --git a/boot/evaluator.cc b/boot/evaluator.cc index 0a6ebe5..0085c62 100644 --- a/boot/evaluator.cc +++ b/boot/evaluator.cc @@ -139,20 +139,26 @@ namespace elna::boot { for (auto const& field : current_record->fields) { - auto props = get_type_properties(field.second, target); + auto props = get_type_properties(field.second.field_type, target); if (!props.has_value()) { return std::nullopt; } - size = (size + props->alignment - 1) & ~(props->alignment - 1); + // #aligned replaces the field's own alignment outright, so that + // a smaller value packs it. + const std::size_t field_alignment = field.second.alignment.value_or(props->alignment); + + size = (size + field_alignment - 1) & ~(field_alignment - 1); size += props->size; - alignment = std::max(alignment, props->alignment); - current_offset = (current_offset + props->alignment - 1) & ~(props->alignment - 1); + alignment = std::max(alignment, field_alignment); + current_offset = (current_offset + field_alignment - 1) & ~(field_alignment - 1); result_map.insert(field.first, current_offset); current_offset += props->size; } } + alignment = subject->alignment.value_or(alignment); + return record_properties{ .offset_map = std::move(result_map), .size = (size + alignment - 1) & ~(alignment - 1), diff --git a/boot/materialization.cc b/boot/materialization.cc index 4b58ac2..5b200d5 100644 --- a/boot/materialization.cc +++ b/boot/materialization.cc @@ -33,6 +33,10 @@ namespace elna::boot return "Integer literal overflows"; case real_overflow: return "Real literal overflows"; + case local_export: + return "Local symbols cannot be exported"; + case field_export: + return "Record fields cannot be exported"; } __builtin_unreachable(); } @@ -42,6 +46,43 @@ namespace elna::boot { } + void materialization_visitor::visit(procedure_declaration *declaration) + { + this->in_global_scope = false; + walking_visitor::visit(declaration); + this->in_global_scope = true; + } + + void materialization_visitor::visit(variable_declaration *declaration) + { + walking_visitor::visit(declaration); + for (const auto& variable_identifier : declaration->identifiers) + { + if (!this->in_global_scope && variable_identifier.exported()) + { + add_error(variable_identifier.id().position(), + materialization_error::kind::local_export); + } + } + } + + void materialization_visitor::visit(record_type_expression *expression) + { + for (const auto& [field_identifiers, field_type] : expression->fields) + { + field_type->accept(this); + + for (const auto& field_identifier : field_identifiers) + { + if (field_identifier.exported()) + { + add_error(field_identifier.id().position(), + materialization_error::kind::field_export); + } + } + } + } + void materialization_visitor::visit(literal *literal) { switch (literal->wants_signed) @@ -96,4 +137,11 @@ namespace elna::boot materialization_error::kind::real_overflow); } } + + void materialization_visitor::visit_entry_point(unit *unit) + { + this->in_global_scope = false; + walking_visitor::visit_entry_point(unit); + this->in_global_scope = true; + } } diff --git a/boot/name_analysis.cc b/boot/name_analysis.cc index a979a37..caff18b 100644 --- a/boot/name_analysis.cc +++ b/boot/name_analysis.cc @@ -50,8 +50,6 @@ namespace elna::boot return "Symbol '" + this->name + "' not declared"; case not_a_type: return "'" + this->name + "' is not a type"; - case local_export: - return "Local symbol '" + this->name + "' cannot be exported"; default: __builtin_unreachable(); } @@ -59,6 +57,34 @@ namespace elna::boot }, this->payload); } + attribute_error::attribute_error(const source_position position, const std::string& name, + kind attribute_kind) + : diagnostic(position), name(name), m_kind(attribute_kind) + { + } + + std::string attribute_error::what() const + { + switch (this->m_kind) + { + using enum kind; + case unknown: + return "Attribute '#" + this->name + "' not declared"; + case duplicate: + return "Attribute '#" + this->name + "' specified more than once"; + case unsupported: + return "Attribute '#" + this->name + "' is not supported in this position"; + case argument_count: + return "Attribute '#" + this->name + "' takes exactly one argument"; + case not_constant: + return "Attribute '#" + this->name + "' requires a constant expression"; + case invalid_alignment: + return "Alignment must be a power of two"; + default: + __builtin_unreachable(); + } + } + std::optional symbol_declaration_error::note() const { if (std::holds_alternative(this->payload)) @@ -306,6 +332,7 @@ namespace elna::boot for (const auto& parameter_name : parameter_names) { + evaluate_alignment(parameter_name.attributes, false); result_type.first.parameters.push_back(parameter_type); result_type.second.push_back(parameter_name.name()); } @@ -313,19 +340,71 @@ namespace elna::boot return result_type; } - ordered_map resolving_visitor::build_composite_type(const std::vector& fields, - ordered_map& field_names, const type& aggregate) + std::optional resolving_visitor::evaluate_alignment( + const std::vector& attributes, const bool supported) + { + std::optional result; + + for (const attribute& written : attributes) + { + if (written.name().name() != "aligned") + { + add_error(written.name().position(), written.name().name(), + attribute_error::kind::unknown); + } + else if (!supported) + { + add_error(written.name().position(), written.name().name(), + attribute_error::kind::unsupported); + } + else if (written.arguments().size() != 1) + { + add_error(written.name().position(), written.name().name(), + attribute_error::kind::argument_count); + } + else if (result.has_value()) + { + add_error(written.name().position(), written.name().name(), + attribute_error::kind::duplicate); + } + else if (auto value = this->constant_evaluator.evaluate_index(*written.arguments().front())) + { + if (value.value() == 0 || (value.value() & (value.value() - 1)) != 0) + { + add_error(written.arguments().front()->position(), + written.name().name(), attribute_error::kind::invalid_alignment); + } + else + { + result = value; + } + } + else + { + add_error(written.arguments().front()->position(), + written.name().name(), attribute_error::kind::not_constant); + } + } + return result; + } + + ordered_map resolving_visitor::build_composite_type( + const std::vector& fields, + ordered_map& field_names, const type& aggregate, const bool supported) { - ordered_map result; + ordered_map result; for (const auto& field : fields) { const type field_type = resolve_type(*field.second); - for (const auto& field_name : field.first) + for (const identifier_definition& field_name : field.first) { + const std::optional alignment = + evaluate_alignment(field_name.attributes, supported); auto [existing, inserted] = field_names.insert(field_name.name(), field_origin{ .position = field.second->position(), .base_type = type() }); + if (!inserted) { std::optional base_name; @@ -338,12 +417,12 @@ namespace elna::boot base_name = alias->name; } } - add_error(field_name.position(), field_name.name(), aggregate, + add_error(field_name.id().position(), field_name.name(), aggregate, member_error::duplicate{ .original = existing->second.position, .base = base_name }); } else { - result.insert(field_name.name(), field_type); + result.insert(field_name.name(), field_info(field_type, alignment)); } } } @@ -432,16 +511,18 @@ namespace elna::boot } else if (!declaration->is_extern && resolve_aliases(variable_type).get() != nullptr) { - auto position_span = source_position(declaration->identifiers.front().id().position().start(), + auto 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 identifier_definition& variable_identifier : declaration->identifiers) + for (const auto& variable_identifier : declaration->identifiers) { auto variable_symbol = register_variable(variable_identifier.name(), variable_type, declaration->position(), declaration->is_extern); variable_symbol->exported = variable_identifier.exported(); + variable_symbol->alignment = evaluate_alignment(variable_identifier.attributes, true); variable_symbol->value = computed; } } @@ -609,6 +690,7 @@ namespace elna::boot ordered_map field_names; collect_field_names(result_type->base, field_names); result_type->fields = build_composite_type(expression->fields, field_names, type(result_type)); + result_type->alignment = evaluate_alignment(expression->attributes, true); this->current_type = type(result_type); } @@ -1216,10 +1298,10 @@ namespace elna::boot { return false; } - for (const auto& [field_name, field_type] : link->fields) + for (const auto& [field_name, field] : link->fields) { alias_path.resize(saved_path); - if (!find_alias_cycle(being_resolved, field_type, alias_path, in_record)) + if (!find_alias_cycle(being_resolved, field.field_type, alias_path, in_record)) { return false; } @@ -1407,19 +1489,6 @@ namespace elna::boot __builtin_unreachable(); } - void name_analysis_visitor::visit(variable_declaration *declaration) - { - resolving_visitor::visit(declaration); - for (const identifier_definition& variable_identifier : declaration->identifiers) - { - if (variable_identifier.exported()) - { - add_error(variable_identifier.id().position(), - variable_identifier.id().name(), symbol_declaration_error::kind::local_export); - } - } - } - void name_analysis_visitor::visit(procedure_declaration *declaration) { if (declaration->body.has_value()) diff --git a/boot/parser.yy b/boot/parser.yy index 00bdae8..21ff827 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -88,8 +88,7 @@ along with GCC; see the file COPYING3. If not see %token STRING %token BOOLEAN %token LEFT_PAREN "(" RIGHT_PAREN ")" LEFT_SQUARE "[" RIGHT_SQUARE "]" -%token GENERIC_OPEN "#[" -%token LEFT_BRACE "{" RIGHT_BRACE "}" + LEFT_BRACE "{" RIGHT_BRACE "}" GENERIC_OPEN "#[" %token ASSIGNMENT ":=" EXCLAMATION "!" ARROW "->" AT "@" HAT "^" @@ -149,14 +148,16 @@ along with GCC; see the file COPYING3. If not see %type > type_declarations type_part; %type > procedure_body; %type field_declaration; -%type > optional_fields required_fields; +%type > field_declarations; %type > field_initializer; %type > field_initializers; %type > elsif_then_statements elsif_do_statements; %type *> else_statements; %type > identifier with_counter; -%type > identifier_definition; +%type > identifier_definition attributed_identifier; %type > identifier_definitions; +%type > attribute; +%type > attributes; %type > import_declaration; %type > required_identifiers optional_identifiers generic_parameters; %type name_reference; @@ -188,18 +189,43 @@ identifier: identifier_definition: IDENTIFIER "*" { $$ = std::make_unique($1, boot::make_position(@1), true); } | IDENTIFIER { $$ = std::make_unique($1, boot::make_position(@1), false); } +attribute: + TRAIT "(" required_expressions ")" + { + $$ = std::make_unique(boot::identifier($1, boot::make_position(@1)), $3); + } +attributes: + %empty {} + | attribute attributes + { + $$ = $2; + $$.emplace($$.cbegin(), std::move(*$1)); + } +attributed_identifier: + attributes identifier_definition + { + $$ = $2; + $$->attributes = $1; + + // An empty rule is located where the previous token ended, which + // would push the declaration's position back over it. + if ($$->attributes.empty()) + { + @$ = @2; + } + } identifier_definitions: - identifier_definition "," identifier_definitions + attributed_identifier "," identifier_definitions { $$ = $3; $$.emplace($$.cbegin(), std::move(*$1)); } - | identifier_definition { $$.emplace_back(std::move(*$1)); } + | attributed_identifier { $$.emplace_back(std::move(*$1)); } return_declaration: %empty {} | "->" "!" { $$ = boot::procedure_type_expression::return_t(std::monostate{}); } | "->" type_expression { $$ = boot::procedure_type_expression::return_t($2); } -procedure_heading: "(" optional_fields ")" return_declaration +procedure_heading: "(" field_declarations ")" return_declaration { $$ = std::make_unique(boot::make_position(@$), $2, $4); } procedure_declaration: "proc" identifier_definition generic_parameters procedure_heading procedure_body @@ -555,16 +581,15 @@ statements: } | statement { $$.push_back($1); } field_declaration: - required_identifiers ":" type_expression { $$ = std::make_pair($1, std::shared_ptr($3)); } -required_fields: - field_declaration ";" required_fields + identifier_definitions ":" type_expression + { $$ = std::make_pair($1, std::shared_ptr($3)); } +field_declarations: + field_declaration ";" field_declarations { $$ = $3; $$.emplace($$.cbegin(), $1); } | field_declaration { $$.emplace_back($1); } -optional_fields: - required_fields { $$ = $1; } | %empty {} field_initializer: identifier ":" expression { $$ = std::make_unique(std::move(*$1), $3); } @@ -591,18 +616,35 @@ type_expression: { $$ = new boot::pointer_type_expression(boot::make_position(@$), $2); } - | "record" optional_fields "end" + | attributes "record" field_declarations "end" { - $$ = new boot::record_type_expression(boot::make_position(@$), $2); + std::vector written = $1; + + if (written.empty()) + { + @$ = @2 + @4; + } + auto *record = new boot::record_type_expression(boot::make_position(@$), $3); + + record->attributes = std::move(written); + $$ = record; } - | "record" "(" name_reference ")" optional_fields "end" + | attributes "record" "(" name_reference ")" field_declarations "end" { - boot::named_expression *reference = $3; + std::vector written = $1; - $$ = new boot::record_type_expression(boot::make_position(@$), $5, + if (written.empty()) + { + @$ = @2 + @7; + } + boot::named_expression *reference = $4; + auto *record = new boot::record_type_expression(boot::make_position(@$), $6, boot::identifier(reference->name, reference->position()), std::move(reference->arguments)); + + record->attributes = std::move(written); delete reference; + $$ = record; } | "proc" procedure_heading { diff --git a/boot/result.cc b/boot/result.cc index 635a121..c8ea267 100644 --- a/boot/result.cc +++ b/boot/result.cc @@ -17,7 +17,6 @@ along with GCC; see the file COPYING3. If not see #include "elna/boot/result.h" -#include #include #include #include @@ -115,27 +114,6 @@ namespace elna::boot return this->m_name == that; } - identifier_definition::identifier_definition(const std::string& name, - const source_position& position, const bool exported) - : m_identifier(name, position), m_exported(exported) - { - } - - const std::string& identifier_definition::name() const - { - return this->m_identifier.name(); - } - - const identifier& identifier_definition::id() const - { - return this->m_identifier; - } - - bool identifier_definition::exported() const - { - return this->m_exported; - } - std::optional previous_declaration_note( const std::optional& original, std::string_view label, const std::filesystem::path& file) @@ -159,17 +137,6 @@ namespace elna::boot return diagnostic_note{ .message = join(identifiers), .position = position_span, .file = {} }; } - std::vector extract_identifiers(const std::vector& identifiers) - { - std::vector result; - result.reserve(identifiers.size()); - - std::ranges::transform(identifiers, std::back_inserter(result), - [](const auto& identifier) { return identifier.id(); }); - - return result; - } - integer_literal::integer_literal(bool is_signed, std::size_t size) : m_signed(is_signed), m_size(size) { diff --git a/boot/symbol.cc b/boot/symbol.cc index 9092866..0814217 100644 --- a/boot/symbol.cc +++ b/boot/symbol.cc @@ -253,6 +253,11 @@ namespace elna::boot { } + field_info::field_info(type field_type, std::optional alignment) + : field_type(std::move(field_type)), alignment(alignment) + { + } + record_type::record_type(type base) : base(std::move(base)) { @@ -780,11 +785,11 @@ namespace elna::boot if (auto record = erase_generic(resolved_type).get()) { - for (const auto& [name, field_type] : record->fields) + for (const auto& [name, field] : record->fields) { if (name == field_name) { - return substitute_from(resolved_type, field_type); + return substitute_from(resolved_type, field.field_type); } } } diff --git a/boot/type_check.cc b/boot/type_check.cc index 5078da1..8847693 100644 --- a/boot/type_check.cc +++ b/boot/type_check.cc @@ -344,9 +344,9 @@ namespace elna::boot } else if (auto record = referent.get()) { - for (const auto& [field_name, field_type] : record->fields) + for (const auto& [field_name, field] : record->fields) { - if (contains_constant_member(field_type)) + if (contains_constant_member(field.field_type)) { return true; } @@ -378,9 +378,9 @@ namespace elna::boot if (auto record = referent.get()) { - for (const auto& [field_name, field_type] : record->fields) + for (const auto& [field_name, field] : record->fields) { - if (auto opaque = find_opaque_type(field_type)) + if (auto opaque = find_opaque_type(field.field_type)) { return opaque; } @@ -462,9 +462,9 @@ namespace elna::boot } if (auto record = resolve_aliases(checked).get()) { - for (const auto& [field_name, field_type] : record->fields) + for (const auto& [field_name, field] : record->fields) { - if (auto found = find_zero_sized(field_type, target)) + if (auto found = find_zero_sized(field.field_type, target)) { return found; } @@ -804,7 +804,7 @@ namespace elna::boot { walking_visitor::visit(declaration); - for (const identifier_definition& variable_identifier : declaration->identifiers) + for (const auto& variable_identifier : declaration->identifiers) { auto variable_symbol = this->bag.lookup(variable_identifier.name())->is_variable(); @@ -823,7 +823,7 @@ namespace elna::boot { return; } - for (const identifier_definition& variable_identifier : declaration->identifiers) + for (const auto& variable_identifier : declaration->identifiers) { auto variable_symbol = this->bag.lookup(variable_identifier.name())->is_variable(); if (!is_assignable_from(variable_symbol->symbol, declaration->initializer->type_decoration)) @@ -945,17 +945,17 @@ namespace elna::boot if (auto record = referent.get()) { - for (const auto& [field_name, field_type] : record->fields) + for (const auto& [field_name, field] : record->fields) { - if (auto opaque = find_opaque_type(field_type)) + if (auto opaque = find_opaque_type(field.field_type)) { add_error(declaration->position(), opaque.value(), type_requirement_error::kind::opaque_field); } - else if (has_zero_size(field_type, this->target)) + else if (has_zero_size(field.field_type, this->target)) { add_error(declaration->position(), - field_type, type_requirement_error::kind::zero_sized); + field.field_type, type_requirement_error::kind::zero_sized); } } } @@ -1060,9 +1060,9 @@ namespace elna::boot } if (auto record = subject.get()) { - for (const auto& [field_name, field_type] : record->fields) + for (const auto& [field_name, field] : record->fields) { - if (occurs_in(parameter, field_type)) + if (occurs_in(parameter, field.field_type)) { return true; } -- cgit v1.2.3