Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_HIR_Builder.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
41#ifndef COMPILER_HIR_BUILDER_H
42#define COMPILER_HIR_BUILDER_H
43
44#include <Compiler_HIR_Model.H>
45
46namespace Aleph {
54{
56
57 static void merge_span(Source_Span &dst, const Source_Span &src) noexcept
58 {
59 if (not src.is_valid())
60 return;
61 if (not dst.is_valid())
62 {
63 dst = src;
64 return;
65 }
67 }
68
69public:
72
78
81 {
82 return hir->make<Compiler_HIR_Module>(span);
83 }
84
87 {
88 ah_runtime_error_unless(function != nullptr)
89 << "Compiler_HIR_Builder::append_function(): null function";
90 module.functions.append(function);
91 merge_span(module.span, function->span);
92 }
93
96 {
97 ah_runtime_error_unless(stmt != nullptr)
98 << "Compiler_HIR_Builder::append_top_level_statement(): null statement";
99 module.statements.append(stmt);
100 merge_span(module.span, stmt->span);
101 }
102
105 const Source_Span &rbrace = {}) const
106 {
108 }
109
112 {
113 ah_runtime_error_unless(stmt != nullptr)
114 << "Compiler_HIR_Builder::append_statement(): null statement";
115 block.statements.append(stmt);
116 merge_span(block.span, stmt->span);
117 }
118
121 const Compiler_Type_Id type_id = 0,
122 const Source_Span &name_span = {},
123 Compiler_HIR_Block_Stmt *body = nullptr,
124 const Source_Span &span = {}) const
125 {
126 return hir->make<Compiler_HIR_Function>(std::move(name), type_id, name_span, body, span);
127 }
128
130 static void set_body(Compiler_HIR_Function &function, Compiler_HIR_Block_Stmt *body) noexcept
131 {
132 function.body = body;
133 function.span = function.name_span;
134 if (body != nullptr)
135 merge_span(function.span, body->span);
136 }
137
140 std::string name,
141 const Compiler_Type_Id type_id,
142 const Source_Span &span = {})
143 {
144 function.parameters.append({std::move(name), span, type_id});
145 merge_span(function.span, span);
146 return function.parameters.access(function.parameters.size() - 1);
147 }
148
151 const Source_Span &span = {}) const
152 {
153 return hir->make<Compiler_HIR_Invalid_Expr>(type_id, span);
154 }
155
158 const Compiler_Type_Id type_id,
159 std::string text,
160 const Source_Span &span = {},
161 const bool bool_value = false) const
162 {
163 return hir->make<Compiler_HIR_Constant_Expr>(kind, type_id, std::move(text), span, bool_value);
164 }
165
168 const Source_Span &span = {}) const
169 {
170 return make_constant(Compiler_HIR_Constant_Kind::Unit, type_id, "unit", span, false);
171 }
172
175 const Compiler_Type_Id type_id,
176 const Source_Span &span = {}) const
177 {
178 return make_constant(
179 Compiler_HIR_Constant_Kind::Bool, type_id, value ? "true" : "false", span, value);
180 }
181
184 const Compiler_Type_Id type_id,
185 const Source_Span &span = {}) const
186 {
187 return make_constant(
188 Compiler_HIR_Constant_Kind::Integer, type_id, std::to_string(value), span, false);
189 }
190
193 const Compiler_Type_Id type_id,
194 const Source_Span &span = {}) const
195 {
196 return make_constant(Compiler_HIR_Constant_Kind::String, type_id, std::move(text), span, false);
197 }
198
201 const Compiler_Type_Id type_id,
202 const Source_Span &span = {}) const
203 {
204 return make_constant(Compiler_HIR_Constant_Kind::Character, type_id, std::move(text), span, false);
205 }
206
209 const Compiler_Type_Id type_id = 0,
210 const Source_Span &span = {}) const
211 {
212 return hir->make<Compiler_HIR_Variable_Expr>(std::move(name), type_id, span);
213 }
214
217 Compiler_HIR_Expr *operand,
218 const Compiler_Type_Id type_id = 0,
219 const Source_Span &operator_span = {}) const
220 {
221 return hir->make<Compiler_HIR_Unary_Expr>(op, type_id, operator_span, operand);
222 }
223
226 const Compiler_Operator_Kind op,
228 const Compiler_Type_Id type_id = 0,
229 const Source_Span &operator_span = {}) const
230 {
231 return hir->make<Compiler_HIR_Binary_Expr>(lhs, op, type_id, operator_span, rhs);
232 }
233
236 const Compiler_Type_Id type_id = 0,
237 const Source_Span &lparen_span = {},
238 const Source_Span &rparen_span = {}) const
239 {
240 return hir->make<Compiler_HIR_Call_Expr>(callee, type_id, lparen_span, rparen_span);
241 }
242
245 {
247 << "Compiler_HIR_Builder::append_argument(): null argument";
248 call.arguments.append(argument);
249 merge_span(call.span, argument->span);
250 }
251
254 {
255 return hir->make<Compiler_HIR_Invalid_Stmt>(span);
256 }
257
260 {
261 return hir->make<Compiler_HIR_Eval_Stmt>(expr, span);
262 }
263
266 const Compiler_Type_Id type_id,
267 const Source_Span &name_span = {},
268 Compiler_HIR_Expr *initializer = nullptr,
269 const Source_Span &span = {}) const
270 {
271 return hir->make<Compiler_HIR_Let_Stmt>(std::move(name), type_id, name_span, initializer, span);
272 }
273
276 const Source_Span &span = {}) const
277 {
278 return hir->make<Compiler_HIR_Return_Stmt>(value, span);
279 }
280
283 Compiler_HIR_Stmt *then_branch,
284 Compiler_HIR_Stmt *else_branch = nullptr,
285 const Source_Span &if_span = {},
286 const Source_Span &else_span = {}) const
287 {
288 return hir->make<Compiler_HIR_If_Stmt>(condition, then_branch, else_branch, if_span, else_span);
289 }
290
293 Compiler_HIR_Stmt *body,
294 const Source_Span &keyword_span = {}) const
295 {
296 return hir->make<Compiler_HIR_While_Stmt>(condition, body, keyword_span);
297 }
298
301 {
302 return hir->make<Compiler_HIR_Break_Stmt>(span);
303 }
304
307 {
308 return hir->make<Compiler_HIR_Continue_Stmt>(span);
309 }
310};
311} // namespace Aleph
312
313#endif
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.
Definition ah-errors.H:255
size_t size_t int32_t value
Definition ca-c-api.h:116
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_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.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
size_t Compiler_Type_Id
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
Structured block statement.
Typed function call expression.
DynArray< Compiler_HIR_Expr * > arguments
Call arguments.
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.
Definition SSA.H:166
Half-open byte range inside a source file.
Definition ah-source.H:100