-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler_HIR_Model.H
More file actions
869 lines (771 loc) · 29.1 KB
/
Copy pathCompiler_HIR_Model.H
File metadata and controls
869 lines (771 loc) · 29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/*
Aleph_w
Data structures & Algorithms
version 2.0.0b
https://github.com/lrleon/Aleph-w
This file is part of Aleph-w library
Copyright (c) 2002-2026 Leandro Rabindranath Leon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/** @file Compiler_HIR_Model.H
* @brief Reusable typed high-level IR model independent from any concrete frontend.
*
* This header defines the stable HIR node model, ownership context, and
* deterministic dump helpers. It is the reusable boundary that language
* frontends can target before later execution or lowering stages.
*
* @ingroup Utilities
*/
#ifndef COMPILER_HIR_MODEL_H
#define COMPILER_HIR_MODEL_H
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <Compiler_Operator.H>
#include <Compiler_Types.H>
#include <ah-arena.H>
#include <ah-errors.H>
#include <tpl_dynArray.H>
namespace Aleph {
/** @brief Merges two spans without depending on the parser-oriented AST layer. */
inline Source_Span compiler_hir_merge_spans(const Source_Span &lhs, const Source_Span &rhs) noexcept
{
if (not lhs.is_valid())
return rhs;
if (not rhs.is_valid())
return lhs;
return lhs.file_id == rhs.file_id ? Source_Span{lhs.file_id, lhs.begin, rhs.end} : lhs;
}
struct Compiler_HIR_Expr;
struct Compiler_HIR_Stmt;
struct Compiler_HIR_Block_Stmt;
struct Compiler_HIR_Function;
struct Compiler_HIR_Module;
/** @brief Kinds of HIR expressions. */
enum class Compiler_HIR_Expr_Kind
{
Invalid,
Constant,
Variable,
Unary,
Binary,
Call
};
/** @brief Kinds of HIR statements. */
enum class Compiler_HIR_Stmt_Kind
{
Invalid,
Eval,
Let,
Return,
Block,
If,
While,
Break,
Continue
};
/** @brief Kinds of HIR constants. */
enum class Compiler_HIR_Constant_Kind
{
Integer,
String,
Character,
Bool,
Unit
};
/** @brief Returns a stable debug name for an HIR expression kind. */
inline const char *compiler_hir_expr_kind_name(const Compiler_HIR_Expr_Kind kind) noexcept
{
switch (kind)
{
case Compiler_HIR_Expr_Kind::Invalid:
return "Invalid";
case Compiler_HIR_Expr_Kind::Constant:
return "Constant";
case Compiler_HIR_Expr_Kind::Variable:
return "Variable";
case Compiler_HIR_Expr_Kind::Unary:
return "Unary";
case Compiler_HIR_Expr_Kind::Binary:
return "Binary";
case Compiler_HIR_Expr_Kind::Call:
return "Call";
}
return "Unknown";
}
/** @brief Returns a stable debug name for an HIR statement kind. */
inline const char *compiler_hir_stmt_kind_name(const Compiler_HIR_Stmt_Kind kind) noexcept
{
switch (kind)
{
case Compiler_HIR_Stmt_Kind::Invalid:
return "Invalid";
case Compiler_HIR_Stmt_Kind::Eval:
return "Eval";
case Compiler_HIR_Stmt_Kind::Let:
return "Let";
case Compiler_HIR_Stmt_Kind::Return:
return "Return";
case Compiler_HIR_Stmt_Kind::Block:
return "Block";
case Compiler_HIR_Stmt_Kind::If:
return "If";
case Compiler_HIR_Stmt_Kind::While:
return "While";
case Compiler_HIR_Stmt_Kind::Break:
return "Break";
case Compiler_HIR_Stmt_Kind::Continue:
return "Continue";
}
return "Unknown";
}
/** @brief Returns a stable debug name for an HIR constant kind. */
inline const char *compiler_hir_constant_kind_name(const Compiler_HIR_Constant_Kind kind) noexcept
{
switch (kind)
{
case Compiler_HIR_Constant_Kind::Integer:
return "Integer";
case Compiler_HIR_Constant_Kind::String:
return "String";
case Compiler_HIR_Constant_Kind::Character:
return "Char";
case Compiler_HIR_Constant_Kind::Bool:
return "Bool";
case Compiler_HIR_Constant_Kind::Unit:
return "Unit";
}
return "Unknown";
}
/** @brief Base node carrying a source span. */
struct Compiler_HIR_Node
{
Source_Span span; ///< Source region associated with the HIR node.
explicit Compiler_HIR_Node(const Source_Span &sp = {}) noexcept : span(sp) {}
};
/** @brief Arena-backed ownership context for HIR nodes. */
class Compiler_HIR_Context
{
struct Owned_Object
{
void *ptr = nullptr;
void (*destroy)(void *) noexcept = nullptr;
};
AhArenaAllocator arena;
DynArray<Owned_Object> owned;
public:
/** @brief Constructs a context with `arena_size` bytes.
*
* @param arena_size Arena capacity in bytes.
* @throws std::runtime_error If the arena cannot be created.
*/
explicit Compiler_HIR_Context(const size_t arena_size = AhArenaAllocator::DEFAULT_SIZE)
: arena(arena_size)
{}
Compiler_HIR_Context(const Compiler_HIR_Context &) = delete;
Compiler_HIR_Context &operator=(const Compiler_HIR_Context &) = delete;
~Compiler_HIR_Context() noexcept
{
reset();
}
/** @brief Allocates and constructs one HIR object.
*
* @tparam T Object type to create.
* @param args Constructor arguments forwarded to `T`.
* @return Pointer stable until `reset()` or destruction.
* @throws std::runtime_error If arena allocation fails.
*/
template <typename T, typename... Args>
T *make(Args &&...args)
{
T *ptr = allocate<T>(arena, std::forward<Args>(args)...);
ah_runtime_error_unless(ptr != nullptr) << "Compiler_HIR_Context: arena allocation failed";
owned.append({ptr,
[](void *raw) noexcept
{
static_cast<T *>(raw)->~T();
}});
return ptr;
}
/** @brief Destroys all tracked objects and rewinds the arena. */
void reset() noexcept
{
for (size_t i = owned.size(); i > 0; --i)
{
auto &[ptr, destroy] = owned.access(i - 1);
if (destroy != nullptr and ptr != nullptr)
destroy(ptr);
}
owned.clear();
arena.reset();
}
/** @brief Returns bytes currently consumed in the arena. */
size_t allocated_size() const noexcept
{
return arena.allocated_size();
}
/** @brief Returns total arena capacity in bytes. */
size_t capacity() const noexcept
{
return arena.capacity();
}
};
/** @brief Base class for HIR expressions. */
struct Compiler_HIR_Expr : public Compiler_HIR_Node
{
Compiler_HIR_Expr_Kind kind = Compiler_HIR_Expr_Kind::Invalid; ///< Runtime node kind.
Compiler_Type_Id type_id = 0; ///< Inferred or resolved type.
Compiler_HIR_Expr(const Compiler_HIR_Expr_Kind k = Compiler_HIR_Expr_Kind::Invalid,
const Compiler_Type_Id ty = 0,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Node(sp), kind(k), type_id(ty)
{}
};
/** @brief Base class for HIR statements. */
struct Compiler_HIR_Stmt : public Compiler_HIR_Node
{
Compiler_HIR_Stmt_Kind kind = Compiler_HIR_Stmt_Kind::Invalid; ///< Runtime node kind.
Compiler_HIR_Stmt(const Compiler_HIR_Stmt_Kind k = Compiler_HIR_Stmt_Kind::Invalid,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Node(sp), kind(k)
{}
};
/** @brief Invalid HIR expression placeholder. */
struct Compiler_HIR_Invalid_Expr final : public Compiler_HIR_Expr
{
explicit Compiler_HIR_Invalid_Expr(const Compiler_Type_Id ty = 0, const Source_Span &sp = {}) noexcept
: Compiler_HIR_Expr(Compiler_HIR_Expr_Kind::Invalid, ty, sp)
{}
};
/** @brief Typed constant expression. */
struct Compiler_HIR_Constant_Expr final : public Compiler_HIR_Expr
{
Compiler_HIR_Constant_Kind constant_kind
= Compiler_HIR_Constant_Kind::Unit; ///< Constant category.
std::string text; ///< Original or normalized constant spelling.
bool bool_value = false; ///< Decoded boolean value for Bool constants.
Compiler_HIR_Constant_Expr(const Compiler_HIR_Constant_Kind k = Compiler_HIR_Constant_Kind::Unit,
const Compiler_Type_Id ty = 0,
std::string value = "",
const Source_Span &sp = {},
const bool b = false) noexcept
: Compiler_HIR_Expr(Compiler_HIR_Expr_Kind::Constant, ty, sp), constant_kind(k),
text(std::move(value)), bool_value(b)
{}
};
/** @brief Typed variable reference expression. */
struct Compiler_HIR_Variable_Expr final : public Compiler_HIR_Expr
{
std::string name; ///< Variable or function name.
Source_Span name_span; ///< Span of the original identifier.
Compiler_HIR_Variable_Expr(std::string value = "",
const Compiler_Type_Id ty = 0,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Expr(Compiler_HIR_Expr_Kind::Variable, ty, sp), name(std::move(value)),
name_span(sp)
{}
};
/** @brief Typed unary expression. */
struct Compiler_HIR_Unary_Expr final : public Compiler_HIR_Expr
{
Compiler_Operator_Kind op = Compiler_Operator_Kind::Invalid; ///< Unary operator kind.
Source_Span operator_span; ///< Span of the operator token.
Compiler_HIR_Expr *operand = nullptr; ///< Operand expression.
Compiler_HIR_Unary_Expr(const Compiler_Operator_Kind k = Compiler_Operator_Kind::Invalid,
const Compiler_Type_Id ty = 0,
const Source_Span &op_sp = {},
Compiler_HIR_Expr *expr = nullptr) noexcept
: Compiler_HIR_Expr(Compiler_HIR_Expr_Kind::Unary,
ty,
compiler_hir_merge_spans(op_sp, expr != nullptr ? expr->span : Source_Span())),
op(k), operator_span(op_sp), operand(expr)
{}
};
/** @brief Typed binary expression. */
struct Compiler_HIR_Binary_Expr final : public Compiler_HIR_Expr
{
Compiler_Operator_Kind op = Compiler_Operator_Kind::Invalid; ///< Binary operator kind.
Source_Span operator_span; ///< Span of the operator token.
Compiler_HIR_Expr *left = nullptr; ///< Left operand.
Compiler_HIR_Expr *right = nullptr; ///< Right operand.
Compiler_HIR_Binary_Expr(Compiler_HIR_Expr *lhs = nullptr,
const Compiler_Operator_Kind k = Compiler_Operator_Kind::Invalid,
const Compiler_Type_Id ty = 0,
const Source_Span &op_sp = {},
Compiler_HIR_Expr *rhs = nullptr) noexcept
: Compiler_HIR_Expr(Compiler_HIR_Expr_Kind::Binary,
ty,
lhs != nullptr
? compiler_hir_merge_spans(lhs->span, rhs != nullptr ? rhs->span : op_sp)
: compiler_hir_merge_spans(op_sp,
rhs != nullptr ? rhs->span : Source_Span())),
op(k), operator_span(op_sp), left(lhs), right(rhs)
{}
};
/** @brief Typed function call expression. */
struct Compiler_HIR_Call_Expr final : public Compiler_HIR_Expr
{
Compiler_HIR_Expr *callee = nullptr; ///< Called expression.
DynArray<Compiler_HIR_Expr *> arguments; ///< Call arguments.
Source_Span lparen_span; ///< Span of `'('`.
Source_Span rparen_span; ///< Span of `')'`.
Compiler_HIR_Call_Expr(Compiler_HIR_Expr *func = nullptr,
const Compiler_Type_Id ty = 0,
const Source_Span &lparen = {},
const Source_Span &rparen = {}) noexcept
: Compiler_HIR_Expr(Compiler_HIR_Expr_Kind::Call,
ty,
func != nullptr ? compiler_hir_merge_spans(func->span, rparen)
: compiler_hir_merge_spans(lparen, rparen)),
callee(func), lparen_span(lparen), rparen_span(rparen)
{}
};
/** @brief Invalid HIR statement placeholder. */
struct Compiler_HIR_Invalid_Stmt final : public Compiler_HIR_Stmt
{
explicit Compiler_HIR_Invalid_Stmt(const Source_Span &sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Invalid, sp)
{}
};
/** @brief Expression statement in HIR. */
struct Compiler_HIR_Eval_Stmt final : public Compiler_HIR_Stmt
{
Compiler_HIR_Expr *expr = nullptr; ///< Evaluated expression.
explicit Compiler_HIR_Eval_Stmt(Compiler_HIR_Expr *value = nullptr,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Eval, value != nullptr ? value->span : sp),
expr(value)
{}
};
/** @brief Typed lexical binding statement. */
struct Compiler_HIR_Let_Stmt final : public Compiler_HIR_Stmt
{
std::string name; ///< Bound identifier.
Source_Span name_span; ///< Span of the binding name.
Compiler_Type_Id binding_type = 0; ///< Inferred binding type.
Compiler_HIR_Expr *initializer = nullptr; ///< Optional initializer.
Compiler_HIR_Let_Stmt(std::string value = "",
const Compiler_Type_Id ty = 0,
const Source_Span &name_sp = {},
Compiler_HIR_Expr *init = nullptr,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Let,
sp.is_valid()
? sp
: (init != nullptr ? compiler_hir_merge_spans(name_sp, init->span)
: name_sp)),
name(std::move(value)), name_span(name_sp), binding_type(ty), initializer(init)
{}
};
/** @brief Return statement in HIR. */
struct Compiler_HIR_Return_Stmt final : public Compiler_HIR_Stmt
{
Compiler_HIR_Expr *value = nullptr; ///< Returned value, or `nullptr`.
explicit Compiler_HIR_Return_Stmt(Compiler_HIR_Expr *expr = nullptr,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Return,
sp.is_valid() ? sp : (expr != nullptr ? expr->span : Source_Span())),
value(expr)
{}
};
/** @brief Structured block statement. */
struct Compiler_HIR_Block_Stmt final : public Compiler_HIR_Stmt
{
DynArray<Compiler_HIR_Stmt *> statements; ///< Nested statements.
Source_Span lbrace_span; ///< Span of `'{'`.
Source_Span rbrace_span; ///< Span of `'}'`.
Compiler_HIR_Block_Stmt(const Source_Span &lbrace = {}, const Source_Span &rbrace = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Block, compiler_hir_merge_spans(lbrace, rbrace)),
lbrace_span(lbrace), rbrace_span(rbrace)
{}
};
/** @brief Structured conditional statement. */
struct Compiler_HIR_If_Stmt final : public Compiler_HIR_Stmt
{
Compiler_HIR_Expr *condition = nullptr; ///< Condition expression.
Compiler_HIR_Stmt *then_branch = nullptr; ///< True branch.
Compiler_HIR_Stmt *else_branch = nullptr; ///< Optional false branch.
Source_Span if_span; ///< Span of `if`.
Source_Span else_span; ///< Span of `else`, if present.
Compiler_HIR_If_Stmt(Compiler_HIR_Expr *cond = nullptr,
Compiler_HIR_Stmt *then_stmt = nullptr,
Compiler_HIR_Stmt *else_stmt = nullptr,
const Source_Span &if_sp = {},
const Source_Span &else_sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::If,
compiler_hir_merge_spans(if_sp,
else_stmt != nullptr
? else_stmt->span
: (then_stmt != nullptr
? then_stmt->span
: (cond != nullptr ? cond->span : else_sp)))),
condition(cond), then_branch(then_stmt), else_branch(else_stmt), if_span(if_sp),
else_span(else_sp)
{}
};
/** @brief Structured while-loop statement. */
struct Compiler_HIR_While_Stmt final : public Compiler_HIR_Stmt
{
Compiler_HIR_Expr *condition = nullptr; ///< Loop condition.
Compiler_HIR_Stmt *body = nullptr; ///< Loop body.
Source_Span keyword_span; ///< Span of `while`.
Compiler_HIR_While_Stmt(Compiler_HIR_Expr *cond = nullptr,
Compiler_HIR_Stmt *stmt = nullptr,
const Source_Span &kw = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::While,
compiler_hir_merge_spans(kw,
stmt != nullptr
? stmt->span
: (cond != nullptr ? cond->span : Source_Span()))),
condition(cond), body(stmt), keyword_span(kw)
{}
};
/** @brief `break` statement in HIR. */
struct Compiler_HIR_Break_Stmt final : public Compiler_HIR_Stmt
{
explicit Compiler_HIR_Break_Stmt(const Source_Span &sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Break, sp)
{}
};
/** @brief `continue` statement in HIR. */
struct Compiler_HIR_Continue_Stmt final : public Compiler_HIR_Stmt
{
explicit Compiler_HIR_Continue_Stmt(const Source_Span &sp = {}) noexcept
: Compiler_HIR_Stmt(Compiler_HIR_Stmt_Kind::Continue, sp)
{}
};
/** @brief Parameter metadata for one HIR function. */
struct Compiler_HIR_Param
{
std::string name; ///< Parameter name.
Source_Span span; ///< Parameter source span.
Compiler_Type_Id type_id = 0; ///< Inferred or declared parameter type.
};
/** @brief Typed HIR function. */
struct Compiler_HIR_Function : public Compiler_HIR_Node
{
std::string name; ///< Function name.
Source_Span name_span; ///< Span of the function name.
Compiler_Type_Id type_id = 0; ///< Full function type.
DynArray<Compiler_HIR_Param> parameters; ///< Parameters in declaration order.
Compiler_HIR_Block_Stmt *body = nullptr; ///< Function body.
Compiler_HIR_Function(std::string value = "",
const Compiler_Type_Id ty = 0,
const Source_Span &name_sp = {},
Compiler_HIR_Block_Stmt *block = nullptr,
const Source_Span &sp = {}) noexcept
: Compiler_HIR_Node(
sp.is_valid()
? sp
: compiler_hir_merge_spans(name_sp, block != nullptr ? block->span : Source_Span())),
name(std::move(value)), name_span(name_sp), type_id(ty), body(block)
{}
};
/** @brief Top-level HIR module. */
struct Compiler_HIR_Module : public Compiler_HIR_Node
{
DynArray<Compiler_HIR_Function *> functions; ///< Lowered top-level functions.
DynArray<Compiler_HIR_Stmt *> statements; ///< Lowered top-level statements.
explicit Compiler_HIR_Module(const Source_Span &sp = {}) noexcept : Compiler_HIR_Node(sp) {}
};
namespace Compiler_HIR_Detail {
inline std::string escape_text(std::string_view text)
{
std::string escaped;
escaped.reserve(text.size());
for (const char ch : text)
switch (ch)
{
case '\\':
escaped += "\\\\";
break;
case '"':
escaped += "\\\"";
break;
case '\n':
escaped += "\\n";
break;
case '\r':
escaped += "\\r";
break;
case '\t':
escaped += "\\t";
break;
default:
escaped.push_back(ch);
}
return escaped;
}
inline void append_indent(std::ostream &out, const size_t indent)
{
out << std::string(indent, ' ');
}
inline std::string type_text(const Compiler_Type_Context *types, const Compiler_Type_Id id)
{
if (types == nullptr)
return "<no-types>";
if (id == 0)
return "<unknown-type>";
return types->to_string(id);
}
inline void dump_expr(const Compiler_HIR_Expr *expr,
std::ostream &out,
const size_t indent,
const Compiler_Type_Context *types);
inline void dump_stmt(const Compiler_HIR_Stmt *stmt,
std::ostream &out,
const size_t indent,
const Compiler_Type_Context *types);
inline void dump_expr(const Compiler_HIR_Expr *expr,
std::ostream &out,
const size_t indent,
const Compiler_Type_Context *types)
{
append_indent(out, indent);
if (expr == nullptr)
{
out << "<null-hir-expr>\n";
return;
}
switch (expr->kind)
{
case Compiler_HIR_Expr_Kind::Invalid:
out << "InvalidExpr: " << type_text(types, expr->type_id) << '\n';
break;
case Compiler_HIR_Expr_Kind::Constant:
{
const auto *node = static_cast<const Compiler_HIR_Constant_Expr *>(expr);
out << "Constant(" << compiler_hir_constant_kind_name(node->constant_kind);
if (node->constant_kind == Compiler_HIR_Constant_Kind::Bool)
out << ", " << (node->bool_value ? "true" : "false");
else if (node->constant_kind != Compiler_HIR_Constant_Kind::Unit)
out << ", " << node->text;
out << "): " << type_text(types, node->type_id) << '\n';
break;
}
case Compiler_HIR_Expr_Kind::Variable:
{
const auto *node = static_cast<const Compiler_HIR_Variable_Expr *>(expr);
out << "Variable(" << node->name << "): " << type_text(types, node->type_id) << '\n';
break;
}
case Compiler_HIR_Expr_Kind::Unary:
{
const auto *node = static_cast<const Compiler_HIR_Unary_Expr *>(expr);
out << "Unary(" << compiler_operator_name(node->op)
<< "): " << type_text(types, node->type_id) << '\n';
dump_expr(node->operand, out, indent + 2, types);
break;
}
case Compiler_HIR_Expr_Kind::Binary:
{
const auto *node = static_cast<const Compiler_HIR_Binary_Expr *>(expr);
out << "Binary(" << compiler_operator_name(node->op)
<< "): " << type_text(types, node->type_id) << '\n';
dump_expr(node->left, out, indent + 2, types);
dump_expr(node->right, out, indent + 2, types);
break;
}
case Compiler_HIR_Expr_Kind::Call:
{
const auto *node = static_cast<const Compiler_HIR_Call_Expr *>(expr);
out << "Call: " << type_text(types, node->type_id) << '\n';
append_indent(out, indent + 2);
out << "Callee:\n";
dump_expr(node->callee, out, indent + 4, types);
append_indent(out, indent + 2);
out << "Args:\n";
for (size_t i = 0; i < node->arguments.size(); ++i)
dump_expr(node->arguments.access(i), out, indent + 4, types);
break;
}
}
}
inline void dump_stmt(const Compiler_HIR_Stmt *stmt,
std::ostream &out,
const size_t indent,
const Compiler_Type_Context *types)
{
append_indent(out, indent);
if (stmt == nullptr)
{
out << "<null-hir-stmt>\n";
return;
}
switch (stmt->kind)
{
case Compiler_HIR_Stmt_Kind::Invalid:
out << "InvalidStmt\n";
break;
case Compiler_HIR_Stmt_Kind::Eval:
{
const auto *node = static_cast<const Compiler_HIR_Eval_Stmt *>(stmt);
out << "Eval\n";
dump_expr(node->expr, out, indent + 2, types);
break;
}
case Compiler_HIR_Stmt_Kind::Let:
{
const auto *node = static_cast<const Compiler_HIR_Let_Stmt *>(stmt);
out << "Let(" << node->name << "): " << type_text(types, node->binding_type) << '\n';
if (node->initializer != nullptr)
dump_expr(node->initializer, out, indent + 2, types);
break;
}
case Compiler_HIR_Stmt_Kind::Return:
{
const auto *node = static_cast<const Compiler_HIR_Return_Stmt *>(stmt);
out << "Return\n";
if (node->value != nullptr)
dump_expr(node->value, out, indent + 2, types);
break;
}
case Compiler_HIR_Stmt_Kind::Block:
{
const auto *node = static_cast<const Compiler_HIR_Block_Stmt *>(stmt);
out << "Block\n";
for (size_t i = 0; i < node->statements.size(); ++i)
dump_stmt(node->statements.access(i), out, indent + 2, types);
break;
}
case Compiler_HIR_Stmt_Kind::If:
{
const auto *node = static_cast<const Compiler_HIR_If_Stmt *>(stmt);
out << "If\n";
append_indent(out, indent + 2);
out << "Condition:\n";
dump_expr(node->condition, out, indent + 4, types);
append_indent(out, indent + 2);
out << "Then:\n";
dump_stmt(node->then_branch, out, indent + 4, types);
if (node->else_branch != nullptr)
{
append_indent(out, indent + 2);
out << "Else:\n";
dump_stmt(node->else_branch, out, indent + 4, types);
}
break;
}
case Compiler_HIR_Stmt_Kind::While:
{
const auto *node = static_cast<const Compiler_HIR_While_Stmt *>(stmt);
out << "While\n";
append_indent(out, indent + 2);
out << "Condition:\n";
dump_expr(node->condition, out, indent + 4, types);
append_indent(out, indent + 2);
out << "Body:\n";
dump_stmt(node->body, out, indent + 4, types);
break;
}
case Compiler_HIR_Stmt_Kind::Break:
out << "Break\n";
break;
case Compiler_HIR_Stmt_Kind::Continue:
out << "Continue\n";
break;
}
}
} // namespace Compiler_HIR_Detail
/** @brief Dumps one HIR expression in a deterministic text format. */
inline std::string compiler_dump_hir_expr(const Compiler_HIR_Expr *expr,
const Compiler_Type_Context &types)
{
std::ostringstream out;
Compiler_HIR_Detail::dump_expr(expr, out, 0, &types);
return out.str();
}
/** @brief Dumps one HIR statement in a deterministic text format. */
inline std::string compiler_dump_hir_stmt(const Compiler_HIR_Stmt *stmt,
const Compiler_Type_Context &types)
{
std::ostringstream out;
Compiler_HIR_Detail::dump_stmt(stmt, out, 0, &types);
return out.str();
}
/** @brief Dumps one HIR function in a deterministic text format. */
inline std::string compiler_dump_hir_function(const Compiler_HIR_Function *function,
const Compiler_Type_Context &types)
{
std::ostringstream out;
if (function == nullptr)
{
out << "<null-hir-function>\n";
return out.str();
}
out << "Function(" << function->name << "): " << types.to_string(function->type_id) << '\n';
if (function->parameters.size() > 0)
{
out << " Params:";
for (size_t i = 0; i < function->parameters.size(); ++i)
{
const auto ¶m = function->parameters.access(i);
if (i > 0)
out << ",";
out << " " << param.name << ": " << types.to_string(param.type_id);
}
out << '\n';
}
out << " Body:\n";
Compiler_HIR_Detail::dump_stmt(function->body, out, 4, &types);
return out.str();
}
/** @brief Dumps one HIR module in a deterministic text format. */
inline std::string compiler_dump_hir_module(const Compiler_HIR_Module *module,
const Compiler_Type_Context &types)
{
std::ostringstream out;
if (module == nullptr)
{
out << "<null-hir-module>\n";
return out.str();
}
out << "HIRModule\n";
for (size_t i = 0; i < module->functions.size(); ++i)
{
Compiler_HIR_Detail::append_indent(out, 2);
const auto *function = module->functions.access(i);
if (function == nullptr)
{
out << "Function: <null>\n";
continue;
}
out << "Function(" << function->name << "): " << types.to_string(function->type_id) << '\n';
if (function->parameters.size() > 0)
{
Compiler_HIR_Detail::append_indent(out, 4);
out << "Params:";
for (size_t j = 0; j < function->parameters.size(); ++j)
{
const auto ¶m = function->parameters.access(j);
if (j > 0)
out << ",";
out << " " << param.name << ": " << types.to_string(param.type_id);
}
out << '\n';
}
Compiler_HIR_Detail::append_indent(out, 4);
out << "Body:\n";
Compiler_HIR_Detail::dump_stmt(function->body, out, 6, &types);
}
for (size_t i = 0; i < module->statements.size(); ++i)
Compiler_HIR_Detail::dump_stmt(module->statements.access(i), out, 2, &types);
return out.str();
}
} // namespace Aleph
#endif