-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNodes.h
More file actions
954 lines (701 loc) · 22.2 KB
/
Copy pathNodes.h
File metadata and controls
954 lines (701 loc) · 22.2 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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
#pragma once
#include <string_view>
#include <filesystem>
#include <stdexcept>
#include <utility>
#include <variant>
#include <vector>
#include <format>
#include <ranges>
#include <span>
#include "SourceLocation.h"
#include "utils/utils.h"
#include "lexer/Tokens.h"
#include "symbols/IdentManager.h"
#include "types/SwTypes.h"
#include "utils/FileSystem.h"
#define SW_NODE_LIST \
SW_NODE(ND_INVALID, Node) \
SW_NODE(ND_EXPR, Expression) \
SW_NODE(ND_INT, IntLit) \
SW_NODE(ND_FLOAT, FloatLit) \
SW_NODE(ND_OP, Op) \
SW_NODE(ND_VAR, Var) \
SW_NODE(ND_STR, StrLit) \
SW_NODE(ND_CHAR, CharLit) \
SW_NODE(ND_CALL, FuncCall) \
SW_NODE(ND_IDENT, Ident) \
SW_NODE(ND_FUNC, Function) \
SW_NODE(ND_PARAM, Parameter) \
SW_NODE(ND_RET, ReturnStatement) \
SW_NODE(ND_COND, Condition) \
SW_NODE(ND_WHILE, WhileLoop) \
SW_NODE(ND_STRUCT, Struct) \
SW_NODE(ND_IMPORT, ImportNode) \
SW_NODE(ND_ARRAY, ArrayLit) \
SW_NODE(ND_TYPE, TypeWrapper) \
SW_NODE(ND_BOOL, BoolLit) \
SW_NODE(ND_SCOPE, Scope) \
SW_NODE(ND_BREAK, BreakStmt) \
SW_NODE(ND_CONTINUE, ContinueStmt) \
SW_NODE(ND_INTRINSIC, Intrinsic) \
SW_NODE(ND_PROTOCOL, Protocol) \
SW_NODE(ND_UNDEFINED, UndefinedValue) \
SW_NODE(ND_ENUM, Enum) \
SW_NODE(ND_GEN_ARG, GenericArg) \
SW_NODE(ND_GEN_ARG_LIST, GenericArgList) \
SW_NODE(ND_GENERIC_PARAM, GenericParam) \
SW_NODE(ND_FOR_LOOP, ForLoop) \
SW_NODE(ND_TYPE_ALIAS, TypeAlias) \
SW_NODE(ND_PROTOCOL_IMPL, ProtocolImpl)
#define SW_NODE(x, y) x,
enum NodeType {
SW_NODE_LIST
};
#undef SW_NODE
struct Node;
struct Var;
struct Type;
struct Ident;
struct FunctionType;
class Parser;
class Namespace;
class IdentInfo;
class LLVMBackend;
// The common base class of all the nodes
struct Node {
SourceLocation location;
NodeType kind = ND_INVALID;
Node() = default;
explicit Node(const NodeType ty)
: kind(ty) {}
virtual ~Node() = default;
/// Returns a tag which identifies the node's kind
[[nodiscard]] virtual NodeType getNodeType() const {
return kind;
}
/// Is the node allowed to appear in global context (E.g. Functions, Vars)?
[[nodiscard]] virtual bool isGlobal() const {
return false;
}
/// Is the node representing a literal?
[[nodiscard]] virtual bool isLiteral() const {
return false;
}
/// Fetches the `IdentInfo*` that a node is holding, if any
virtual IdentInfo* getIdentInfo() {
throw std::runtime_error("getIdentInfo called on Node instance");
}
/// Convenient method to fetch the wrapped-node from an expression
virtual Node* getExprValue() {
throw std::runtime_error("getExprValue called on Node instance");
}
/// Returns the wrapped-node if the node is a wrapper, returns the instance itself otherwise
virtual Node* getWrappedNodeOrInstance() {
return this;
}
[[nodiscard]] virtual Ident* getIdent() {
return nullptr;
}
virtual Type* getSwType() {
throw std::runtime_error("getSwType: unimplemented!");
}
template <typename From>
std::add_pointer_t<From> to() {
return dynamic_cast<std::add_pointer_t<From>>(this);
}
};
struct GenericParam final : Node {
std::string_view name;
IdentInfo* id;
std::span<Ident*> constraints;
explicit
GenericParam() : Node(ND_GENERIC_PARAM), id(nullptr) {}
[[nodiscard]]
NodeType getNodeType() const override {
return ND_GENERIC_PARAM;
}
};
/// Nodes which can appear in the Global Scope inherit from `GlobalNode`.
struct GlobalNode : Node {
std::string_view name;
std::string_view extern_attributes;
std::span<GenericParam*> generic_params;
bool is_extern = false;
bool is_exported = false;
bool is_monomorphization = false;
explicit GlobalNode(const NodeType ty)
: Node(ty) {}
[[nodiscard]]
bool isGlobal() const override {
return true;
}
};
struct Expression final : Node {
bool is_comptime = false;
Node* expr = nullptr;
Type* expr_type = nullptr;
explicit Expression(): Node(ND_EXPR) {}
static Expression makeExpression(Node* node) {
Expression expr;
expr.expr = node;
return expr;
}
// set the type of sub-expression instances to `to`
void setType(Type* to);
IdentInfo* getIdentInfo() override {
return expr->getIdentInfo();
}
[[nodiscard]] Node* getExprValue() override {
return expr;
}
[[nodiscard]] NodeType getNodeType() const override {
return ND_EXPR;
}
Node* getWrappedNodeOrInstance() override {
return expr->getWrappedNodeOrInstance();
}
bool operator==(const Expression& other) const {
return this->expr == other.expr && this->expr_type == other.expr_type;
}
Type* getSwType() override {
return expr_type;
}
};
struct Op final : Node {
int8_t arity = 2; // the no. of operands the operator requires
Token::TokenValue tokenid{};
std::span<Node*> operands; // the operands
// for the special case of the `&` operator, where a `mut` can appear right after it
bool is_mutable = false;
enum OpTag_t {
BINARY_ADD,
BINARY_SUB,
MUL,
DIV,
MOD,
EXP,
EXP_ASSIGN,
UNARY_ADD,
UNARY_SUB,
LOGICAL_AND,
LOGICAL_OR,
LOGICAL_NOT,
LOGICAL_NOTEQUAL,
LOGICAL_EQUAL,
GREATER_THAN,
GREATER_THAN_OR_EQUAL,
LESS_THAN,
LESS_THAN_OR_EQUAL,
DOT,
INDEXING_OP,
DEREFERENCE,
ADDRESS_TAKING,
CAST_OP,
ASSIGNMENT,
ADD_ASSIGN,
MUL_ASSIGN,
SUB_ASSIGN,
DIV_ASSIGN,
MOD_ASSIGN,
BITWISE_OR,
BITWISE_AND,
BITWISE_XOR,
BITWISE_NOT,
BITWISE_LSHIFT,
BITWISE_RSHIFT,
BITWISE_OR_ASSIGN,
BITWISE_AND_ASSIGN,
BITWISE_XOR_ASSIGN,
BITWISE_LSHIFT_ASSIGN,
BITWISE_RSHIFT_ASSIGN,
ALIGNOF,
SIZEOF,
TYPEOF,
INVALID
};
OpTag_t op_type = INVALID;
Type* common_type = nullptr; // the common type of its operands
explicit Op()
: Node(ND_OP) {}
explicit Op(Token::TokenValue tokenid, int8_t arity);
static OpTag_t getTagFor(Token::TokenValue tokenid, int arity);
// set the type of sub-expression instances to `to`
void setType(Type* to) const;
[[nodiscard]]
NodeType getNodeType() const override { return ND_OP; }
Type* getSwType() override { return common_type; }
[[nodiscard]] Node* getLHS() const {
assert(!operands.empty());
return operands.at(0);
}
[[nodiscard]] Node* getRHS() const {
assert(operands.size() >= 2);
return operands.at(1);
}
static int getLBPFor(OpTag_t op);
static int getRBPFor(OpTag_t op);
static int getPBPFor(OpTag_t op);
};
struct ReturnStatement final : Node {
Expression* value = nullptr;
FunctionType* parent_fn_type = nullptr; // holds the function-signature of the parent
explicit
ReturnStatement()
: Node(ND_RET) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_RET;
}
};
struct UndefinedValue final : Node {
explicit UndefinedValue() : Node(ND_UNDEFINED) {}
};
struct IntLit final : Node {
std::string_view value;
explicit IntLit(const std::string_view val)
: Node(ND_INT), value(val) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_INT;
}
[[nodiscard]] bool isLiteral() const override {
return true;
}
};
struct FloatLit final : Node {
std::string_view value;
explicit FloatLit(const std::string_view val)
: Node(ND_FLOAT), value(val) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_FLOAT;
}
[[nodiscard]] bool isLiteral() const override {
return true;
}
};
struct BoolLit final : Node {
bool value;
explicit BoolLit(const bool is_true)
: Node(ND_BOOL), value(is_true) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_BOOL;
}
[[nodiscard]] bool isLiteral() const override {
return true;
}
};
struct CharLit final : Node {
char value;
explicit CharLit(const char val)
: Node(ND_CHAR)
, value(val) {}
};
struct StrLit final : Node {
std::string_view value;
explicit StrLit(const std::string_view val)
: Node(ND_STR), value(val) {}
[[nodiscard]]
NodeType getNodeType() const override {
return ND_STR;
}
[[nodiscard]]
bool isLiteral() const override {
return true;
}
};
struct GenericArg;
struct GenericArgList final : Node {
std::span<GenericArg*> generic_args;
GenericArgList()
: Node(ND_GEN_ARG_LIST) {}
[[nodiscard]] auto front() const { return generic_args.front(); }
[[nodiscard]] auto begin() const { return generic_args.begin(); }
[[nodiscard]] auto end() const { return generic_args.end(); }
[[nodiscard]] auto size() const { return generic_args.size(); }
[[nodiscard]] bool empty() const { return generic_args.empty(); }
[[nodiscard]] auto at(const std::size_t i) const { return generic_args.at(i); }
};
struct Ident final : Node {
struct Qualifier {
std::string_view name;
GenericArgList generic_args;
IdentInfo* value = nullptr; // for partial resolution
};
bool has_generic_args = false;
IdentInfo* value = nullptr;
std::span<Qualifier> full_qualification;
// to keep pre-computed generic instantiation steps to avoid repetitive looping
std::unordered_map<Type*, Type*> type_instantiation_map;
explicit Ident()
: Node(ND_IDENT) {}
explicit Ident(IdentInfo* val)
: Ident() { value = val; }
/// Convenience constructor for the creation of temporary objects, not to be used for
/// the construction of AST nodes
explicit Ident(const std::span<Qualifier> full_qualification)
: full_qualification(full_qualification) {}
IdentInfo* getIdentInfo() override {
return value;
}
[[nodiscard]] NodeType getNodeType() const override {
return ND_IDENT;
}
[[nodiscard]] Ident* getIdent() override {
return this;
}
[[nodiscard]]
std::string toString() const {
std::string ret;
for (const auto& qual : full_qualification) {
ret += std::string(qual.name) + "::";
} ret.pop_back();
ret.pop_back();
return ret;
}
};
/// Encodes all the necessary information needed to get a `Type*`.
struct TypeWrapper final : Node {
using ArraySize_t = std::variant<std::monostate, Node*, std::size_t>;
enum Modifiers { Reference, Pointer };
Type* type = nullptr;
bool is_mutable = false;
bool is_slice = false;
bool is_pointer = false;
bool is_reference = false;
Ident* type_id = nullptr;
Expression* array_size{}; // set if the type is an array
TypeWrapper* of_type{}; // set in the case of wrapper types (refs, ptr, arrays, slices)
explicit TypeWrapper()
: Node(ND_TYPE) {}
explicit TypeWrapper(Type* ty)
: Node(ND_TYPE), type(ty) {}
[[nodiscard]]
Type* getSwType() override {
return type;
}
[[nodiscard]]
NodeType getNodeType() const override {
return ND_TYPE;
}
};
struct TypeAlias final : Node {
std::string_view alias;
IdentInfo* ident = nullptr;
TypeWrapper* alias_for = nullptr;
TypeAlias()
: Node(ND_TYPE_ALIAS) {}
[[nodiscard]] NodeType getNodeType() const override { return ND_TYPE_ALIAS; }
};
struct GenericArg final : Node {
explicit GenericArg(): Node(ND_GEN_ARG) {}
explicit GenericArg(Expression* expr)
: GenericArg() { value = expr; }
explicit GenericArg(TypeWrapper* ty)
: GenericArg() { value = ty; }
[[nodiscard]]
bool isType() const {
return std::holds_alternative<TypeWrapper*>(value);
}
[[nodiscard]]
bool isEmpty() const {
return std::holds_alternative<std::monostate>(value);
}
[[nodiscard]]
bool isExpression() const {
return std::holds_alternative<Expression*>(value);
}
Expression* getExpr() const {
return std::get<Expression*>(value);
}
TypeWrapper* getType() const {
return std::get<TypeWrapper*>(value);
}
private:
std::variant<std::monostate, Expression*, TypeWrapper*> value;
};
struct Var final : GlobalNode {
IdentInfo* var_ident = nullptr;
TypeWrapper* var_type = nullptr;
Expression* value = nullptr;
bool initialized = false;
bool is_const = false;
bool is_volatile = false;
bool is_comptime = false;
explicit Var(): GlobalNode(ND_VAR) {}
[[nodiscard]] NodeType getNodeType() const override { return ND_VAR; }
IdentInfo* getIdentInfo() override {
return var_ident;
}
[[nodiscard]] Node* getExprValue() override { return value->expr; }
};
struct Scope final : Node {
Scope* parent_scope = nullptr;
Namespace* symbols = nullptr;
std::span<Node*> children;
explicit Scope()
: Node(ND_SCOPE) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_SCOPE;
}
};
struct Parameter final : Node {
IdentInfo* ident = nullptr;
TypeWrapper* type = nullptr;
std::string_view name{};
Expression* value = nullptr;
bool is_const = false;
bool is_variadic = false;
bool is_instance_param = false;
bool is_initialized = false;
Parameter()
: Node(ND_PARAM) {}
};
struct Function final : GlobalNode {
bool is_static_method = true;
IdentInfo* ident = nullptr;
Scope* children = nullptr;
std::span<Parameter*> params;
TypeWrapper* return_type = nullptr;
explicit Function()
: GlobalNode(ND_FUNC) {}
IdentInfo* getIdentInfo() override {
return ident;
}
[[nodiscard]] NodeType getNodeType() const override {
return ND_FUNC;
}
};
struct FuncCall final : Node {
Ident* ident = nullptr;
Type* signature = nullptr; // supposed to hold the signature of the callee
std::span<Expression*> args;
GenericArgList generic_args;
explicit FuncCall()
: Node(ND_CALL) {}
IdentInfo* getIdentInfo() override {
return ident->getIdentInfo();
}
Type* getSwType() override {
return signature;
}
[[nodiscard]] Ident* getIdent() override {
return ident;
}
[[nodiscard]] NodeType getNodeType() const override { return ND_CALL; }
};
struct Intrinsic final : Node {
enum Kind { INVALID, SIZEOF, TYPEOF, MEMCPY, MEMSET, ADV_PTR };
Kind intrinsic_type = INVALID;
std::span<Expression*> args;
Ident* ident = nullptr;
explicit Intrinsic()
: Node(ND_INTRINSIC) {}
void operator=(const FuncCall* call) {
args = call->args;
ident = call->ident;
static const std::unordered_map<std::string_view, Kind> tag_map = {
{"sizeof", SIZEOF},
{"typeof", TYPEOF},
{"memset", MEMSET},
{"memcpy", MEMCPY},
{"advance_pointer", ADV_PTR}
}; intrinsic_type = tag_map.at(ident->full_qualification.at(0).name);
}
[[nodiscard]] NodeType getNodeType() const override { return ND_INTRINSIC; }
};
struct ImportNode final : GlobalNode {
struct ImportedSymbol_t {
std::string_view actual_name;
std::string_view assigned_alias;
};
bool is_wildcard = false;
sw::FileHandle* mod_handle = nullptr;
std::string_view alias;
std::span<ImportedSymbol_t> imported_symbols{};
explicit ImportNode()
: GlobalNode(ND_IMPORT) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_IMPORT;
}
};
struct ArrayLit final : Node {
Type* type = nullptr;
std::span<Expression*> elements;
explicit ArrayLit()
: Node(ND_ARRAY) {}
Type* getSwType() override {
return type;
}
[[nodiscard]]
bool isLiteral() const override {
return true;
}
};
struct WhileLoop final : Node {
Expression* condition = nullptr;
Scope* children = nullptr;
explicit WhileLoop()
: Node(ND_WHILE) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_WHILE;
}
};
struct BreakStmt final : Node {
explicit BreakStmt(): Node(ND_BREAK) {}
[[nodiscard]] NodeType getNodeType() const override { return ND_BREAK;}
};
struct ContinueStmt final : Node {
explicit ContinueStmt(): Node(ND_CONTINUE) {}
[[nodiscard]] NodeType getNodeType() const override { return ND_CONTINUE; }
};
struct Enum final : GlobalNode {
IdentInfo* ident;
std::optional<TypeWrapper*> enum_type;
std::unordered_map<std::string_view, int> entries;
explicit Enum()
: GlobalNode(ND_ENUM)
, ident(nullptr) {}
int counter = 0;
void addEntry(const std::string_view id) {
entries.emplace(id, counter++);
}
};
struct Protocol final : GlobalNode {
template <bool keep_dynamic = false>
struct MethodSignature {
std::string_view name;
TypeWrapper* return_type{};
// true if the method's first parameter is an instance parameter (`&self`).
// in that case the first element of `params` is a placeholder (nullptr)
// whose type is resolved to a reference of the implementing type during
// conformance checking
bool is_instance_method = false;
std::conditional_t<keep_dynamic,
std::vector<TypeWrapper*>,
std::span<TypeWrapper*>> params{};
bool operator==(const MethodSignature& other) const {
if (name != other.name || is_instance_method != other.is_instance_method) return false;
if ((return_type && other.return_type) ? return_type->type != other.return_type->type
: return_type != other.return_type) return false;
return std::equal(params.begin(), params.end(),
other.params.begin(), other.params.end(),
[](const TypeWrapper* a, const TypeWrapper* b) {
if (!a || !b) return a == b;
return a->type == b->type;
});
}
operator MethodSignature<true>() const {
MethodSignature<true> result;
result.name = name;
result.return_type = return_type;
for (auto ty : params) {
result.params.push_back(ty);
} return result;
}
[[nodiscard]]
std::string toString() const {
std::string res = std::format("fn {}(", name);
for (const TypeWrapper* ty : params) {
res += "_: " + (ty ? (ty->type ? ty->type->toString() : "???") : "self");
res += ',';
}
res += "): " + (return_type->type ? return_type->type->toString() : "???");
return res;
}
};
IdentInfo* ident = nullptr;
std::span<Ident*> dependencies;
std::span<MethodSignature<>> methods;
std::span<TypeAlias*> type_aliases;
IdentInfo* getIdentInfo() override {
return ident;
}
explicit Protocol()
: GlobalNode(ND_PROTOCOL) {}
[[nodiscard]]
NodeType getNodeType() const override { return ND_PROTOCOL; }
};
template <>
struct std::hash<TypeWrapper> {
std::size_t operator()(const TypeWrapper& t) const noexcept {
return std::hash<Type*>{}(t.type);
}
};
template <>
struct std::hash<Protocol::MethodSignature<>> {
std::size_t operator()(const Protocol::MethodSignature<>& m) const noexcept {
std::size_t arg_hash = 0;
for (auto& arg : m.params) {
arg_hash = combineHashes(arg_hash, std::hash<TypeWrapper*>{}(arg));
}
return combineHashes(
std::hash<const char*>{}(m.name.data()),
std::hash<TypeWrapper*>{}(m.return_type),
arg_hash
);
}
};
template <>
struct std::hash<Protocol::MethodSignature<true>> {
std::size_t operator()(const Protocol::MethodSignature<true>& m) const noexcept {
std::size_t arg_hash = 0;
for (auto& arg : m.params) {
arg_hash = combineHashes(arg_hash, std::hash<TypeWrapper*>{}(arg));
}
return combineHashes(
std::hash<const char*>{}(m.name.data()),
std::hash<TypeWrapper*>{}(m.return_type),
arg_hash
);
}
};
struct ProtocolImpl final : GlobalNode {
Ident* protocol = nullptr;
TypeWrapper* impl_for = nullptr;
Scope* children = nullptr;
std::span<TypeAlias*> type_aliases;
ProtocolImpl(): GlobalNode(ND_PROTOCOL_IMPL) {}
[[nodiscard]]
TypeWrapper* getAliasTypeFor(const std::string_view name) const {
for (const TypeAlias* alias : type_aliases) {
if (alias->alias == name)
return alias->alias_for;
} return nullptr;
}
};
struct ForLoop final : Node {
ForLoop(): Node(ND_FOR_LOOP) {}
IdentInfo* loop_var_id = nullptr;
TypeWrapper* loop_var_type = nullptr;
std::string_view loop_var_name{};
Expression* iterable = nullptr;
Scope* children = nullptr;
bool is_comptime = false;
};
struct Struct final : GlobalNode {
IdentInfo* ident = nullptr;
Scope* members = nullptr;
std::span<Ident*> protocols;
explicit Struct() : GlobalNode(ND_STRUCT) {}
[[nodiscard]] NodeType getNodeType() const override {
return ND_STRUCT;
}
IdentInfo* getIdentInfo() override {
return ident;
}
};
struct Condition final : Node {
bool is_comptime = false;
Expression* bool_expr = nullptr;
using elif_t = std::tuple<Expression*, Scope*>;
Scope* if_children{};
std::span<elif_t> elif_children{};
Scope* else_children{};
explicit Condition()
: Node(ND_COND) {}
[[nodiscard]] Node* getExprValue() override {
return bool_expr->expr;
}
[[nodiscard]] NodeType getNodeType() const override {
return ND_COND;
}
};