From d048b633dd02014f8bff1b512ce4d5ae2b8b7903 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 25 Sep 2026 10:59:05 +0200 Subject: Fix move related memory leaks in AST helpers --- boot/parser.yy | 143 +++++++++++++++++++++++++++------------------------------ 1 file changed, 67 insertions(+), 76 deletions(-) (limited to 'boot/parser.yy') diff --git a/boot/parser.yy b/boot/parser.yy index 1e732c6..cc73aef 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -129,9 +129,9 @@ along with GCC; see the file COPYING3. If not see %left "+" "-" %left "*" "/" "%" -%type > literal; +%type literal; %type > case_labels; -%type switch_case; +%type > switch_case; %type > switch_cases; %type variable_declaration; %type > variable_declarations; @@ -141,14 +141,13 @@ along with GCC; see the file COPYING3. If not see %type binary_expression; %type > optional_expressions required_expressions actual_parameter_list; %type designator_expression; -%type > call_expression; +%type call_expression; %type statement; %type > statements statement_part optional_begin_statements; -%type block; %type procedure_declaration; -%type > procedure_heading; +%type procedure_heading; %type return_declaration; -%type > type_declaration; +%type type_declaration; %type > type_declarations sections section; %type , std::vector>> required_declarations optional_declarations; @@ -157,8 +156,8 @@ along with GCC; see the file COPYING3. If not see %type > field_declarations; %type > field_initializer; %type > field_initializers; -%type > elsif_then_statements elsif_do_statements; -%type else_statements; +%type > elsif_then_statements elsif_do_statements;; +%type > else_statements block; %type > identifier with_counter; %type > identifier_definition attributed_identifier; %type > identifier_definitions; @@ -168,7 +167,7 @@ along with GCC; see the file COPYING3. If not see %type > required_identifiers optional_identifiers generic_parameters; %type name_reference; %type > import_declarations; -%type > array_type_expression; +%type array_type_expression; %type > unit; %% program: unit @@ -231,12 +230,12 @@ sections: | section { $$ = $1; } procedure_body: block procedure_return - { $$ = std::make_unique(std::move($1), $2); } + { $$ = std::make_unique(std::move(*$1), $2); } block: "var" variable_declarations statement_part - { $$ = boot::block(std::move($2), std::move($3)); } + { $$ = std::make_unique($2, $3); } | optional_begin_statements - { $$ = boot::block(std::vector(), std::move($1)); } + { $$ = std::make_unique(std::vector(), $1); } statement_part: %empty {} | "begin" statements { $$ = $2; } @@ -277,26 +276,28 @@ return_declaration: %empty {} | "->" "!" { $$ = boot::procedure_type_expression::return_t(std::monostate{}); } | "->" type_expression { $$ = boot::procedure_type_expression::return_t($2); } -procedure_heading: "(" field_declarations ")" attributes return_declaration +procedure_heading: + "(" field_declarations ")" attributes return_declaration { - $$ = std::make_unique(boot::make_position(@$), $2, $5); + $$ = new boot::procedure_type_expression(boot::make_position(@$), $2, $5); $$->attributes = $4; } procedure_declaration: "proc" identifier_definition generic_parameters procedure_heading procedure_body { $$ = new boot::procedure_declaration(boot::make_position(@$), - std::move(*$2), $4.release(), std::move(*$5)); + std::move(*$2), $4, std::move(*$5)); $$->parameters = $3; } | "proc" identifier_definition generic_parameters procedure_heading "extern" { - $$ = new boot::procedure_declaration(boot::make_position(@$), std::move(*$2), $4.release()); + $$ = new boot::procedure_declaration(boot::make_position(@$), std::move(*$2), $4); $$->parameters = $3; } -call_expression: designator_expression actual_parameter_list +call_expression: + designator_expression actual_parameter_list { - $$ = std::make_unique(boot::make_position(@$), $1, $2); + $$ = new boot::procedure_call(boot::make_position(@$), $1, $2); } with_counter: "with" identifier { $$ = $2; } @@ -304,20 +305,18 @@ with_counter: elsif_do_statements: "elsif" expression "do" block elsif_do_statements { - boot::conditional_statements *branch = new boot::conditional_statements($2, std::move($4)); $$ = $5; - $$.emplace($$.begin(), branch); + $$.emplace($$.begin(), $2, std::move(*$4)); } | %empty {} else_statements: - "else" block { $$ = new boot::block(std::move($2)); } + "else" block { $$ = $2; } | %empty { $$ = nullptr; } elsif_then_statements: "elsif" expression "then" block elsif_then_statements { - boot::conditional_statements *branch = new boot::conditional_statements($2, std::move($4)); $$ = $5; - $$.emplace($$.begin(), branch); + $$.emplace($$.begin(), $2, std::move(*$4)); } | %empty {} procedure_return: @@ -327,94 +326,94 @@ literal: INTEGER { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed, false); } | INTEGER8 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | INTEGER16 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | INTEGER32 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | INTEGER64 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | WORD { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed, false); } | WORD8 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | WORD16 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | WORD32 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | WORD64 { auto [magnitude, wants_signed] = $1; - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from(magnitude), wants_signed); } | SINGLE { - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::float_literal::from($1), boot::integer_sign::unmarked); } | DOUBLE { - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), boot::float_literal::from($1), boot::integer_sign::unmarked); } | BOOLEAN { - $$ = std::make_unique>(boot::make_position(@$), $1, boot::integer_sign::_unsigned); + $$ = new boot::literal(boot::make_position(@$), $1, boot::integer_sign::_unsigned); } | CHARACTER { - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), $1, boot::integer_sign::_unsigned); } | "nil" { - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), nullptr, boot::integer_sign::_unsigned); } | STRING { - $$ = std::make_unique>(boot::make_position(@$), + $$ = new boot::literal(boot::make_position(@$), $1, boot::integer_sign::_unsigned); } simple_expression: - literal { $$ = $1.release(); } + literal { $$ = $1; } | designator_expression { $$ = $1; } | TRAIT "(" type_expressions ")" { @@ -425,7 +424,7 @@ simple_expression: { $$ = new boot::cast_expression(boot::make_position(@$), $5, $3); } - | call_expression { $$ = $1.release(); } + | call_expression { $$ = $1; } | "(" expression ")" { $$ = $2; } | name_reference "{" field_initializers "}" { @@ -583,42 +582,42 @@ statement: { $$ = new boot::assign_statement(boot::make_position(@$), $1, $3); } | "while" expression "do" block elsif_do_statements "end" { - boot::conditional_statements *body = new boot::conditional_statements($2, std::move($4)); + boot::conditional_statements *body = new boot::conditional_statements($2, std::move(*$4)); $$ = new boot::while_statement(boot::make_position(@$), body, $5); } | "repeat" block "until" expression { - $$ = new boot::repeat_statement(boot::make_position(@$), std::move($2), $4); + $$ = new boot::repeat_statement(boot::make_position(@$), std::move(*$2), $4); } | "for" identifier "of" expression with_counter "do" block "end" { $$ = new boot::for_statement(boot::make_position(@$), std::move(*$2), $4, - std::move($7), $5.release()); + std::move(*$7), $5.release()); } | "if" expression "then" block elsif_then_statements else_statements "end" { - boot::conditional_statements *then = new boot::conditional_statements($2, std::move($4)); - $$ = new boot::if_statement(boot::make_position(@$), then, $5, $6); + boot::conditional_statements *then = new boot::conditional_statements($2, std::move(*$4)); + $$ = new boot::if_statement(boot::make_position(@$), then, $5, $6.release()); } - | call_expression { $$ = $1.release(); } + | call_expression { $$ = $1; } | "defer" block "end" - { $$ = new boot::defer_statement(boot::make_position(@$), std::move($2)); } + { $$ = new boot::defer_statement(boot::make_position(@$), std::move(*$2)); } | "block" identifier block "end" - { $$ = new boot::block_statement(boot::make_position(@$), std::move(*$2), std::move($3)); } + { $$ = new boot::block_statement(boot::make_position(@$), std::move(*$2), std::move(*$3)); } | "break" identifier { $$ = new boot::break_statement(boot::make_position(@$), std::move(*$2)); } | "case" expression "of" switch_cases else_statements "end" - { $$ = new boot::case_statement(boot::make_position(@$), $2, $4, $5); } + { $$ = new boot::case_statement(boot::make_position(@$), $2, $4, $5.release()); } | %empty { $$ = new boot::empty_statement(boot::make_position(@$)); } -switch_case: case_labels ":" block - { $$ = { .labels = $1, .body = std::move($3) }; } +switch_case: + case_labels ":" block { $$ = std::make_unique($1, std::move(*$3)); } switch_cases: switch_case "|" switch_cases { $$ = $3; - $$.emplace($$.cbegin(), $1); + $$.insert($$.cbegin(), std::move(*$1)); } - | switch_case { $$.push_back($1); } + | switch_case { $$.push_back(std::move(*$1)); } case_labels: expression "," case_labels { @@ -653,22 +652,17 @@ field_initializers: $$.emplace($$.cbegin(), std::move(*$1)); } | field_initializer { $$.push_back(std::move(*$1)); } -array_type_expression: "[" expression "]" type_expression - { $$ = std::make_unique(boot::make_position(@$), $4, $2); } +array_type_expression: + "[" expression "]" type_expression + { $$ = new boot::array_type_expression(boot::make_position(@$), $4, $2); } type_expression: - array_type_expression { $$ = $1.release(); } + array_type_expression { $$ = $1; } | "[" "]" type_expression - { - $$ = new boot::slice_type_expression(boot::make_position(@$), $3); - } + { $$ = new boot::slice_type_expression(boot::make_position(@$), $3); } | "const" type_expression - { - $$ = new boot::constant_type_expression(boot::make_position(@$), $2); - } + { $$ = new boot::constant_type_expression(boot::make_position(@$), $2); } | "^" type_expression - { - $$ = new boot::pointer_type_expression(boot::make_position(@$), $2); - } + { $$ = new boot::pointer_type_expression(boot::make_position(@$), $2); } | "record" attributes field_declarations "end" { auto *record = new boot::record_type_expression(boot::make_position(@$), $3); @@ -688,13 +682,9 @@ type_expression: $$ = record; } | "proc" procedure_heading - { - $$ = $2.release(); - } + { $$ = $2; } | "(" required_identifiers ")" - { - $$ = new boot::enumeration_type_expression(boot::make_position(@$), $2); - } + { $$ = new boot::enumeration_type_expression(boot::make_position(@$), $2); } | name_reference { $$ = $1; } generic_parameters: %empty {} @@ -749,14 +739,15 @@ import_declarations: { $$.emplace_back(new boot::import_declaration(boot::make_position(@$), $1)); } -type_declaration: identifier_definition generic_parameters "=" type_expression +type_declaration: + identifier_definition generic_parameters "=" type_expression { - $$ = std::make_unique(boot::make_position(@$), std::move(*$1), $4); + $$ = new boot::type_declaration(boot::make_position(@$), std::move(*$1), $4); $$->parameters = $2; } | identifier_definition generic_parameters "=" "extern" { - $$ = std::make_unique(boot::make_position(@$), std::move(*$1), + $$ = new boot::type_declaration(boot::make_position(@$), std::move(*$1), new boot::extern_type_expression(boot::make_position(@$))); $$->parameters = $2; } @@ -764,7 +755,7 @@ type_declarations: type_declaration type_declarations { $$ = $2; - $$.insert($$.cbegin(), $1.release()); + $$.insert($$.cbegin(), $1); } | %empty {} actual_parameter_list: -- cgit v1.2.3