From 500c0676b3f6cd5a2297987d5b0dc7ccf34a28d9 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 13 Jul 2026 18:14:45 +0200 Subject: Make return statement part of the body and not a statement --- include/elna/boot/ast.h | 62 +++++++++++++++-------------------------- include/elna/boot/semantic.h | 5 +--- include/elna/gcc/elna-generic.h | 3 -- 3 files changed, 23 insertions(+), 47 deletions(-) (limited to 'include') diff --git a/include/elna/boot/ast.h b/include/elna/boot/ast.h index d9f3af8..ab79c30 100644 --- a/include/elna/boot/ast.h +++ b/include/elna/boot/ast.h @@ -64,7 +64,6 @@ namespace elna::boot class if_statement; class import_declaration; class while_statement; - class return_statement; class case_statement; class traits_expression; class unit; @@ -103,7 +102,6 @@ namespace elna::boot virtual void visit(if_statement *) = 0; virtual void visit(import_declaration *) = 0; virtual void visit(while_statement *) = 0; - virtual void visit(return_statement *) = 0; virtual void visit(defer_statement *) = 0; virtual void visit(case_statement *) = 0; virtual void visit(empty_statement *) = 0; @@ -148,7 +146,6 @@ namespace elna::boot [[noreturn]] virtual void visit(if_statement *) override; [[noreturn]] virtual void visit(import_declaration *) override; [[noreturn]] virtual void visit(while_statement *) override; - [[noreturn]] virtual void visit(return_statement *) override; [[noreturn]] virtual void visit(defer_statement *) override; [[noreturn]] virtual void visit(empty_statement *) override; [[noreturn]] virtual void visit(case_statement *) override; @@ -191,7 +188,6 @@ namespace elna::boot virtual void visit(if_statement *) override; virtual void visit(import_declaration *) override; virtual void visit(while_statement *statement) override; - virtual void visit(return_statement *statement) override; virtual void visit(defer_statement *statement) override; virtual void visit(empty_statement *) override; virtual void visit(case_statement *statement) override; @@ -416,26 +412,21 @@ namespace elna::boot struct procedure_body { - procedure_body(std::vector&& constants, - std::vector&& variables, std::vector&& statements); - procedure_body(std::vector&& constants, std::vector&& variables); + const std::vector constants; + const std::vector variables; + const std::vector entry_point; + expression *const return_expression{ nullptr }; + + procedure_body(std::vector&& constants, + std::vector&& variables, + std::vector&& entry_point, + expression *return_expr = nullptr); procedure_body(const procedure_body&) = delete; procedure_body(procedure_body&& that); procedure_body& operator=(const procedure_body&) = delete; - procedure_body& operator=(procedure_body&& that); - - const std::vector& variables(); - const std::vector& constants(); - const std::vector& statements(); virtual ~procedure_body(); - - private: - std::vector m_variables; - std::vector m_constants; - std::vector m_statements; - }; /** @@ -526,19 +517,6 @@ namespace elna::boot virtual ~conditional_statements(); }; - class return_statement : public statement - { - public: - expression *m_return_expression; - - return_statement(const struct position position, expression *return_expression); - void accept(parser_visitor *visitor) override; - - expression& return_expression(); - - virtual ~return_statement() override; - }; - struct switch_case { std::vector labels; @@ -728,19 +706,23 @@ namespace elna::boot virtual ~while_statement() override; }; - class unit : public node + class unit : public node, public procedure_body { public: - const std::optional> entry_point; - - std::vector imports; - std::vector constants; - std::vector types; - std::vector variables; - std::vector procedures; + const std::vector imports; + const std::vector types; + const std::vector procedures; unit(const struct position position); - unit(const struct position position, std::vector&& entry_point); + unit(const struct position position, + std::vector&& imports, + std::vector&& constants, + std::vector&& types, + std::vector&& variables, + std::vector&& procedures, + std::vector&& entry_point, + expression *const return_expression = nullptr); + bool has_body() const; virtual void accept(parser_visitor *visitor) override; virtual ~unit() override; diff --git a/include/elna/boot/semantic.h b/include/elna/boot/semantic.h index b94d269..1e81ebb 100644 --- a/include/elna/boot/semantic.h +++ b/include/elna/boot/semantic.h @@ -18,10 +18,8 @@ along with GCC; see the file COPYING3. If not see #pragma once #include -#include #include #include -#include #include "elna/boot/ast.h" #include "elna/boot/result.h" @@ -147,7 +145,6 @@ namespace elna::boot */ class type_analysis_visitor final : public walking_visitor, public error_container { - bool returns; symbol_bag bag; std::shared_ptr current_procedure; @@ -161,7 +158,7 @@ namespace elna::boot explicit type_analysis_visitor(symbol_bag bag); void visit(procedure_declaration *declaration) override; - void visit(return_statement *statement) override; + void visit(unit *unit) override; void visit(assign_statement *statement) override; void visit(variable_declaration *declaration) override; void visit(type_declaration *declaration) override; diff --git a/include/elna/gcc/elna-generic.h b/include/elna/gcc/elna-generic.h index 28bfe2d..2fe4d48 100644 --- a/include/elna/gcc/elna-generic.h +++ b/include/elna/gcc/elna-generic.h @@ -18,8 +18,6 @@ along with GCC; see the file COPYING3. If not see #pragma once #include -#include - #include "elna/boot/ast.h" #include "elna/boot/symbol.h" #include "elna/boot/semantic.h" @@ -89,7 +87,6 @@ namespace elna::gcc void visit(boot::if_statement *statement) override; void visit(boot::import_declaration *) override; void visit(boot::while_statement *statement) override; - void visit(boot::return_statement *statement) override; void visit(boot::defer_statement *statement) override; void visit(boot::empty_statement *) override; void visit(boot::case_statement *statement) override; -- cgit v1.2.3