41#ifndef COMPILER_HIR_BUILDER_H
42#define COMPILER_HIR_BUILDER_H
59 if (not src.is_valid())
61 if (not dst.is_valid())
89 <<
"Compiler_HIR_Builder::append_function(): null function";
90 module.functions.append(function);
98 <<
"Compiler_HIR_Builder::append_top_level_statement(): null statement";
99 module.statements.append(stmt);
114 <<
"Compiler_HIR_Builder::append_statement(): null statement";
115 block.statements.append(stmt);
132 function.body = body;
133 function.
span = function.name_span;
144 function.
parameters.append({std::move(name), span, type_id});
161 const bool bool_value =
false)
const
247 <<
"Compiler_HIR_Builder::append_argument(): null argument";
248 call.arguments.append(argument);
Reusable typed high-level IR model independent from any concrete frontend.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
size_t size_t int32_t value
Small reusable helper that allocates and connects HIR nodes.
Compiler_HIR_Builder(Compiler_HIR_Context &context) noexcept
Builds HIR nodes inside context.
static void append_top_level_statement(Compiler_HIR_Module &module, Compiler_HIR_Stmt *stmt)
Appends one top-level statement to module and updates the span.
static void merge_span(Source_Span &dst, const Source_Span &src) noexcept
Compiler_HIR_Let_Stmt * make_let_stmt(std::string name, const Compiler_Type_Id type_id, const Source_Span &name_span={}, Compiler_HIR_Expr *initializer=nullptr, const Source_Span &span={}) const
Allocates one lexical binding statement.
Compiler_HIR_Break_Stmt * make_break_stmt(const Source_Span &span={}) const
Allocates one break statement.
static void set_body(Compiler_HIR_Function &function, Compiler_HIR_Block_Stmt *body) noexcept
Attaches body to function and refreshes the aggregate span.
Compiler_HIR_Constant_Expr * make_bool_constant(const bool value, const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one boolean constant expression.
Compiler_HIR_While_Stmt * make_while_stmt(Compiler_HIR_Expr *condition, Compiler_HIR_Stmt *body, const Source_Span &keyword_span={}) const
Allocates one structured while-loop statement.
Compiler_HIR_Variable_Expr * make_variable(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &span={}) const
Allocates one variable-reference expression.
Compiler_HIR_Call_Expr * make_call(Compiler_HIR_Expr *callee, const Compiler_Type_Id type_id=0, const Source_Span &lparen_span={}, const Source_Span &rparen_span={}) const
Allocates one call expression without arguments.
static void append_statement(Compiler_HIR_Block_Stmt &block, Compiler_HIR_Stmt *stmt)
Appends one nested statement to block and updates the span.
Compiler_HIR_If_Stmt * make_if_stmt(Compiler_HIR_Expr *condition, Compiler_HIR_Stmt *then_branch, Compiler_HIR_Stmt *else_branch=nullptr, const Source_Span &if_span={}, const Source_Span &else_span={}) const
Allocates one structured conditional statement.
static Compiler_HIR_Param & add_parameter(Compiler_HIR_Function &function, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Adds one parameter metadata entry to function.
Compiler_HIR_Invalid_Stmt * make_invalid_stmt(const Source_Span &span={}) const
Allocates one invalid statement placeholder.
Compiler_HIR_Block_Stmt * make_block(const Source_Span &lbrace={}, const Source_Span &rbrace={}) const
Allocates one structured block statement.
Compiler_HIR_Unary_Expr * make_unary(const Compiler_Operator_Kind op, Compiler_HIR_Expr *operand, const Compiler_Type_Id type_id=0, const Source_Span &operator_span={}) const
Allocates one unary expression.
static void append_function(Compiler_HIR_Module &module, Compiler_HIR_Function *function)
Appends one function to module and updates the module span.
Compiler_HIR_Context & context() const noexcept
Returns the underlying ownership context.
Compiler_HIR_Binary_Expr * make_binary(Compiler_HIR_Expr *lhs, const Compiler_Operator_Kind op, Compiler_HIR_Expr *rhs, const Compiler_Type_Id type_id=0, const Source_Span &operator_span={}) const
Allocates one binary expression.
Compiler_HIR_Function * make_function(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &name_span={}, Compiler_HIR_Block_Stmt *body=nullptr, const Source_Span &span={}) const
Allocates one function node without inserting it into any module.
Compiler_HIR_Context * hir
Compiler_HIR_Module * make_module(const Source_Span &span={}) const
Allocates one empty module.
Compiler_HIR_Invalid_Expr * make_invalid_expr(const Compiler_Type_Id type_id=0, const Source_Span &span={}) const
Allocates one invalid expression placeholder.
Compiler_HIR_Continue_Stmt * make_continue_stmt(const Source_Span &span={}) const
Allocates one continue statement.
Compiler_HIR_Eval_Stmt * make_eval_stmt(Compiler_HIR_Expr *expr, const Source_Span &span={}) const
Allocates one expression statement.
Compiler_HIR_Constant_Expr * make_unit_constant(const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one unit constant expression.
Compiler_HIR_Return_Stmt * make_return_stmt(Compiler_HIR_Expr *value=nullptr, const Source_Span &span={}) const
Allocates one return statement.
Compiler_HIR_Constant_Expr * make_string_constant(std::string text, const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one string constant expression.
Compiler_HIR_Constant_Expr * make_character_constant(std::string text, const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one character constant expression.
Compiler_HIR_Constant_Expr * make_integer_constant(const long long value, const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one integer constant expression.
Compiler_HIR_Constant_Expr * make_constant(const Compiler_HIR_Constant_Kind kind, const Compiler_Type_Id type_id, std::string text, const Source_Span &span={}, const bool bool_value=false) const
Allocates one generic constant expression.
static void append_argument(Compiler_HIR_Call_Expr &call, Compiler_HIR_Expr *argument)
Appends one argument to call and updates the aggregate span.
Arena-backed ownership context for HIR nodes.
T * make(Args &&...args)
Allocates and constructs one HIR object.
Main namespace for Aleph-w library functions.
Source_Span compiler_hir_merge_spans(const Source_Span &lhs, const Source_Span &rhs) noexcept
Merges two spans without depending on the parser-oriented AST layer.
Compiler_HIR_Constant_Kind
Kinds of HIR constants.
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
Structured block statement.
Typed function call expression.
Typed constant expression.
continue statement in HIR.
Expression statement in HIR.
Base class for HIR expressions.
DynArray< Compiler_HIR_Param > parameters
Parameters in declaration order.
Structured conditional statement.
Invalid HIR expression placeholder.
Invalid HIR statement placeholder.
Typed lexical binding statement.
Source_Span span
Source region associated with the HIR node.
Parameter metadata for one HIR function.
Base class for HIR statements.
Typed variable reference expression.
Structured while-loop statement.
Source_Span span
Aggregate source span.
Half-open byte range inside a source file.