-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
value.h
1744 lines (1429 loc) · 53.5 KB
/
value.h
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_EXPLORER_AST_VALUE_H_
#define CARBON_EXPLORER_AST_VALUE_H_
#include <optional>
#include <string>
#include <variant>
#include <vector>
#include "common/ostream.h"
#include "explorer/ast/address.h"
#include "explorer/ast/bindings.h"
#include "explorer/ast/declaration.h"
#include "explorer/ast/element.h"
#include "explorer/ast/element_path.h"
#include "explorer/ast/expression_category.h"
#include "explorer/ast/statement.h"
#include "explorer/base/nonnull.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Compiler.h"
namespace Carbon {
class AssociatedConstant;
class ChoiceType;
class TupleValue;
// A trait type that describes how to allocate an instance of `T` in an arena.
// Returns the created object, which is not required to be of type `T`.
template <typename T>
struct AllocateTrait {
template <typename... Args>
static auto New(Nonnull<Arena*> arena, Args&&... args) -> Nonnull<const T*> {
return arena->New<T>(std::forward<Args>(args)...);
}
};
using VTable =
llvm::StringMap<std::pair<Nonnull<const CallableDeclaration*>, int>>;
// Returns a pointer to an empty VTable that will never be deallocated.
//
// Using this instead of `new VTable()` avoids unnecessary allocations, and
// takes better advantage of Arena canonicalization when a VTable pointer is
// used as a constructor argument.
inline auto EmptyVTable() -> Nonnull<const VTable*> {
static Nonnull<const VTable*> result = new VTable();
return result;
}
// Abstract base class of all AST nodes representing values.
//
// Value and its derived classes support LLVM-style RTTI, including
// llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
// class derived from Value must provide a `classof` operation, and
// every concrete derived class must have a corresponding enumerator
// in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
// details.
//
// Arena's canonicalization support is enabled for Value and all derived types.
// As a result, all Values must be immutable, and all their constructor
// arguments must be copyable, equality-comparable, and hashable. See
// Arena's documentation for details.
class Value : public Printable<Value> {
public:
using EnableCanonicalizedAllocation = void;
enum class Kind {
#define CARBON_VALUE_KIND(kind) kind,
#include "explorer/ast/value_kinds.def"
};
Value(const Value&) = delete;
auto operator=(const Value&) -> Value& = delete;
// Call `f` on this value, cast to its most-derived type. `R` specifies the
// expected return type of `f`.
template <typename R, typename F>
auto Visit(F f) const -> R;
void Print(llvm::raw_ostream& out) const;
// Returns the sub-Value specified by `path`, which must be a valid element
// path for *this. If the sub-Value is a method and its self_pattern is an
// AddrPattern, then pass the LocationValue representing the receiver as
// `me_value`, otherwise pass `*this`.
auto GetElement(Nonnull<Arena*> arena, const ElementPath& path,
SourceLocation source_loc,
std::optional<Nonnull<const Value*>> me_value) const
-> ErrorOr<Nonnull<const Value*>>;
// Returns a copy of *this, but with the sub-Value specified by `path`
// set to `field_value`. `path` must be a valid field path for *this.
auto SetField(Nonnull<Arena*> arena, const ElementPath& path,
Nonnull<const Value*> field_value,
SourceLocation source_loc) const
-> ErrorOr<Nonnull<const Value*>>;
// Returns the enumerator corresponding to the most-derived type of this
// object.
auto kind() const -> Kind { return kind_; }
protected:
// Constructs a Value. `kind` must be the enumerator corresponding to the
// most-derived type being constructed.
explicit Value(Kind kind) : kind_(kind) {}
private:
const Kind kind_;
};
// Returns whether the fully-resolved kind that this value will eventually have
// is currently unknown, because it depends on a generic parameter.
inline auto IsValueKindDependent(Nonnull<const Value*> type) -> bool {
return type->kind() == Value::Kind::VariableType ||
type->kind() == Value::Kind::AssociatedConstant;
}
// Base class for types holding contextual information by which we can
// determine whether values are equal.
class EqualityContext {
public:
virtual auto VisitEqualValues(
Nonnull<const Value*> value,
llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const
-> bool = 0;
protected:
virtual ~EqualityContext() = default;
};
auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2,
std::optional<Nonnull<const EqualityContext*>> equality_ctx)
-> bool;
auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
std::optional<Nonnull<const EqualityContext*>> equality_ctx)
-> bool;
// Call the given `visitor` on all values nested within the given value,
// including `value` itself, in a preorder traversal. Aborts and returns
// `false` if `visitor` returns `false`, otherwise returns `true`.
auto VisitNestedValues(Nonnull<const Value*> value,
llvm::function_ref<bool(const Value*)> visitor) -> bool;
// An integer value.
class IntValue : public Value {
public:
explicit IntValue(int value) : Value(Kind::IntValue), value_(value) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::IntValue;
}
template <typename F>
auto Decompose(F f) const {
return f(value_);
}
auto value() const -> int { return value_; }
private:
int value_;
};
// A function or bound method value.
class FunctionOrMethodValue : public Value {
public:
explicit FunctionOrMethodValue(
Kind kind, Nonnull<const FunctionDeclaration*> declaration,
Nonnull<const Bindings*> bindings)
: Value(kind), declaration_(declaration), bindings_(bindings) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::FunctionValue ||
value->kind() == Kind::BoundMethodValue;
}
auto declaration() const -> const FunctionDeclaration& {
return *declaration_;
}
auto bindings() const -> const Bindings& { return *bindings_; }
auto type_args() const -> const BindingMap& { return bindings_->args(); }
auto witnesses() const -> const ImplWitnessMap& {
return bindings_->witnesses();
}
private:
Nonnull<const FunctionDeclaration*> declaration_;
Nonnull<const Bindings*> bindings_;
};
// A function value.
class FunctionValue : public FunctionOrMethodValue {
public:
explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration,
Nonnull<const Bindings*> bindings)
: FunctionOrMethodValue(Kind::FunctionValue, declaration, bindings) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::FunctionValue;
}
template <typename F>
auto Decompose(F f) const {
return f(&declaration(), &bindings());
}
};
// A bound method value. It includes the receiver object.
class BoundMethodValue : public FunctionOrMethodValue {
public:
explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
Nonnull<const Value*> receiver,
Nonnull<const Bindings*> bindings)
: FunctionOrMethodValue(Kind::BoundMethodValue, declaration, bindings),
receiver_(receiver) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::BoundMethodValue;
}
template <typename F>
auto Decompose(F f) const {
return f(&declaration(), receiver_, &bindings());
}
auto receiver() const -> Nonnull<const Value*> { return receiver_; }
private:
Nonnull<const Value*> receiver_;
};
// A destructor value.
class DestructorValue : public Value {
public:
explicit DestructorValue(Nonnull<const DestructorDeclaration*> declaration)
: Value(Kind::DestructorValue), declaration_(declaration) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::DestructorValue;
}
template <typename F>
auto Decompose(F f) const {
return f(declaration_);
}
auto declaration() const -> const DestructorDeclaration& {
return *declaration_;
}
private:
Nonnull<const DestructorDeclaration*> declaration_;
};
// The value of a location in memory.
class LocationValue : public Value {
public:
explicit LocationValue(Address value)
: Value(Kind::LocationValue), value_(std::move(value)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::LocationValue;
}
template <typename F>
auto Decompose(F f) const {
return f(value_);
}
auto address() const -> const Address& { return value_; }
private:
Address value_;
};
// Contains the result of the evaluation of an expression, including a value,
// the original expression category, and an optional address if available.
class ExpressionResult {
public:
static auto Value(Nonnull<const Carbon::Value*> v) -> ExpressionResult {
return ExpressionResult(v, std::nullopt, ExpressionCategory::Value);
}
static auto Reference(Nonnull<const Carbon::Value*> v, Address address)
-> ExpressionResult {
return ExpressionResult(v, std::move(address),
ExpressionCategory::Reference);
}
static auto Initializing(Nonnull<const Carbon::Value*> v, Address address)
-> ExpressionResult {
return ExpressionResult(v, std::move(address),
ExpressionCategory::Initializing);
}
ExpressionResult(Nonnull<const Carbon::Value*> v,
std::optional<Address> address, ExpressionCategory cat)
: value_(v), address_(std::move(address)), expr_cat_(cat) {}
auto value() const -> Nonnull<const Carbon::Value*> { return value_; }
auto address() const -> const std::optional<Address>& { return address_; }
auto expression_category() const -> ExpressionCategory { return expr_cat_; }
private:
Nonnull<const Carbon::Value*> value_;
std::optional<Address> address_;
ExpressionCategory expr_cat_;
};
// Represents the result of the evaluation of a reference expression, and
// holds the resulting `Value*` and its `Address`.
class ReferenceExpressionValue : public Value {
public:
ReferenceExpressionValue(Nonnull<const Value*> value, Address address)
: Value(Kind::ReferenceExpressionValue),
value_(value),
address_(std::move(address)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::ReferenceExpressionValue;
}
template <typename F>
auto Decompose(F f) const {
return f(value_, address_);
}
auto value() const -> Nonnull<const Value*> { return value_; }
auto address() const -> const Address& { return address_; }
private:
Nonnull<const Value*> value_;
Address address_;
};
// A pointer value
class PointerValue : public Value {
public:
explicit PointerValue(Address value)
: Value(Kind::PointerValue), value_(std::move(value)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::PointerValue;
}
template <typename F>
auto Decompose(F f) const {
return f(value_);
}
auto address() const -> const Address& { return value_; }
private:
Address value_;
};
// A bool value.
class BoolValue : public Value {
public:
explicit BoolValue(bool value) : Value(Kind::BoolValue), value_(value) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::BoolValue;
}
template <typename F>
auto Decompose(F f) const {
return f(value_);
}
auto value() const -> bool { return value_; }
private:
bool value_;
};
// A value of a struct type. Note that the expression `{}` is a value of type
// `{} as type`; the former is a `StructValue` and the latter is a
// `StructType`.
class StructValue : public Value {
public:
explicit StructValue(std::vector<NamedValue> elements)
: Value(Kind::StructValue), elements_(std::move(elements)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::StructValue;
}
template <typename F>
auto Decompose(F f) const {
return f(elements_);
}
auto elements() const -> llvm::ArrayRef<NamedValue> { return elements_; }
// Returns the value of the field named `name` in this struct, or
// nullopt if there is no such field.
auto FindField(std::string_view name) const
-> std::optional<Nonnull<const Value*>>;
private:
std::vector<NamedValue> elements_;
};
// A value of a nominal class type, i.e., an object.
class NominalClassValue : public Value {
public:
static constexpr llvm::StringLiteral BaseField{"base"};
// Takes the class type, inits, an optional base, a pointer to a
// NominalClassValue*, that must be common to all NominalClassValue of the
// same object. The pointee is updated, when `NominalClassValue`s are
// constructed, to point to the `NominalClassValue` corresponding to the
// child-most class type. Sets *class_value_ptr = this, which corresponds to
// the static type of the value matching its dynamic type.
NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits,
std::optional<Nonnull<const NominalClassValue*>> base,
Nonnull<const NominalClassValue** const> class_value_ptr);
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::NominalClassValue;
}
template <typename F>
auto Decompose(F f) const {
return f(type_, inits_, base_, class_value_ptr_);
}
auto type() const -> const Value& { return *type_; }
auto inits() const -> const Value& { return *inits_; }
auto base() const -> std::optional<Nonnull<const NominalClassValue*>> {
return base_;
}
// Returns a pointer of pointer to the child-most class value.
auto class_value_ptr() const -> Nonnull<const NominalClassValue**> {
return class_value_ptr_;
}
private:
Nonnull<const Value*> type_;
Nonnull<const Value*> inits_; // The initializing StructValue.
std::optional<Nonnull<const NominalClassValue*>> base_;
Nonnull<const NominalClassValue** const> class_value_ptr_;
};
// An alternative constructor value.
class AlternativeConstructorValue : public Value {
public:
AlternativeConstructorValue(Nonnull<const ChoiceType*> choice,
Nonnull<const AlternativeSignature*> alternative)
: Value(Kind::AlternativeConstructorValue),
choice_(choice),
alternative_(alternative) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::AlternativeConstructorValue;
}
template <typename F>
auto Decompose(F f) const {
return f(&choice(), &alternative());
}
auto choice() const -> const ChoiceType& { return *choice_; }
auto alternative() const -> const AlternativeSignature& {
return *alternative_;
}
private:
Nonnull<const ChoiceType*> choice_;
Nonnull<const AlternativeSignature*> alternative_;
};
// An alternative value.
class AlternativeValue : public Value {
public:
AlternativeValue(Nonnull<const ChoiceType*> choice,
Nonnull<const AlternativeSignature*> alternative,
std::optional<Nonnull<const TupleValue*>> argument)
: Value(Kind::AlternativeValue),
choice_(choice),
alternative_(alternative),
argument_(argument) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::AlternativeValue;
}
template <typename F>
auto Decompose(F f) const {
return f(&choice(), &alternative(), argument_);
}
auto choice() const -> const ChoiceType& { return *choice_; }
auto alternative() const -> const AlternativeSignature& {
return *alternative_;
}
auto argument() const -> std::optional<Nonnull<const TupleValue*>> {
return argument_;
}
private:
Nonnull<const ChoiceType*> choice_;
Nonnull<const AlternativeSignature*> alternative_;
std::optional<Nonnull<const TupleValue*>> argument_;
};
// Base class for tuple types and tuple values. These are the same other than
// their type-of-type, but we separate them to make it easier to tell types and
// values apart.
class TupleValueBase : public Value {
public:
explicit TupleValueBase(Value::Kind kind,
std::vector<Nonnull<const Value*>> elements)
: Value(kind), elements_(std::move(elements)) {}
auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
return elements_;
}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::TupleValue ||
value->kind() == Kind::TupleType;
}
template <typename F>
auto Decompose(F f) const {
return f(elements_);
}
private:
std::vector<Nonnull<const Value*>> elements_;
};
// A tuple value.
class TupleValue : public TupleValueBase {
public:
// An empty tuple.
static auto Empty() -> Nonnull<const TupleValue*> {
static const TupleValue empty =
TupleValue(std::vector<Nonnull<const Value*>>());
return static_cast<Nonnull<const TupleValue*>>(&empty);
}
explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
: TupleValueBase(Kind::TupleValue, std::move(elements)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::TupleValue;
}
};
// A tuple type. These values are produced by converting a tuple value
// containing only types to type `type`.
class TupleType : public TupleValueBase {
public:
// The unit type.
static auto Empty() -> Nonnull<const TupleType*> {
static const TupleType empty =
TupleType(std::vector<Nonnull<const Value*>>());
return static_cast<Nonnull<const TupleType*>>(&empty);
}
explicit TupleType(std::vector<Nonnull<const Value*>> elements)
: TupleValueBase(Kind::TupleType, std::move(elements)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::TupleType;
}
};
// A binding placeholder value.
class BindingPlaceholderValue : public Value {
public:
// Represents the `_` placeholder.
explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
// Represents a named placeholder.
explicit BindingPlaceholderValue(ValueNodeView value_node)
: Value(Kind::BindingPlaceholderValue),
value_node_(std::move(value_node)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::BindingPlaceholderValue;
}
template <typename F>
auto Decompose(F f) const {
return value_node_ ? f(*value_node_) : f();
}
auto value_node() const -> const std::optional<ValueNodeView>& {
return value_node_;
}
private:
std::optional<ValueNodeView> value_node_;
};
// Value for addr pattern
class AddrValue : public Value {
public:
explicit AddrValue(Nonnull<const Value*> pattern)
: Value(Kind::AddrValue), pattern_(pattern) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::AddrValue;
}
template <typename F>
auto Decompose(F f) const {
return f(pattern_);
}
auto pattern() const -> const Value& { return *pattern_; }
private:
Nonnull<const Value*> pattern_;
};
// Value for uninitialized local variables.
class UninitializedValue : public Value {
public:
explicit UninitializedValue(Nonnull<const Value*> pattern)
: Value(Kind::UninitializedValue), pattern_(pattern) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::UninitializedValue;
}
template <typename F>
auto Decompose(F f) const {
return f(pattern_);
}
auto pattern() const -> const Value& { return *pattern_; }
private:
Nonnull<const Value*> pattern_;
};
// The int type.
class IntType : public Value {
public:
IntType() : Value(Kind::IntType) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::IntType;
}
template <typename F>
auto Decompose(F f) const {
return f();
}
};
// The bool type.
class BoolType : public Value {
public:
BoolType() : Value(Kind::BoolType) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::BoolType;
}
template <typename F>
auto Decompose(F f) const {
return f();
}
};
// A type type.
class TypeType : public Value {
public:
TypeType() : Value(Kind::TypeType) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::TypeType;
}
template <typename F>
auto Decompose(F f) const {
return f();
}
};
// A function type.
class FunctionType : public Value {
public:
// An explicit function parameter that is a `:!` binding:
//
// fn MakeEmptyVector(T:! type) -> Vector(T);
struct GenericParameter : public HashFromDecompose<GenericParameter> {
template <typename F>
auto Decompose(F f) const {
return f(index, binding);
}
size_t index;
Nonnull<const GenericBinding*> binding;
};
// For methods with unbound `self` parameters.
struct MethodSelf : public HashFromDecompose<MethodSelf> {
template <typename F>
auto Decompose(F f) const {
return f(addr_self, self_type);
}
// True if `self` parameter uses an `addr` pattern.
bool addr_self;
// Type of `self` parameter.
const Value* self_type;
};
FunctionType(std::optional<MethodSelf> method_self,
Nonnull<const Value*> parameters,
Nonnull<const Value*> return_type)
: FunctionType(method_self, parameters, {}, return_type, {}, {},
/*is_initializing=*/false) {}
FunctionType(std::optional<MethodSelf> method_self,
Nonnull<const Value*> parameters,
std::vector<GenericParameter> generic_parameters,
Nonnull<const Value*> return_type,
std::vector<Nonnull<const GenericBinding*>> deduced_bindings,
std::vector<Nonnull<const ImplBinding*>> impl_bindings,
bool is_initializing)
: Value(Kind::FunctionType),
method_self_(method_self),
parameters_(parameters),
generic_parameters_(std::move(generic_parameters)),
return_type_(return_type),
deduced_bindings_(std::move(deduced_bindings)),
impl_bindings_(std::move(impl_bindings)),
is_initializing_(is_initializing) {}
struct ExceptSelf : public HashFromDecompose<ExceptSelf> {
template <typename F>
auto Decompose(F f) const {
return f();
}
};
FunctionType(ExceptSelf, const FunctionType* clone)
: FunctionType(std::nullopt, clone->parameters_,
clone->generic_parameters_, clone->return_type_,
clone->deduced_bindings_, clone->impl_bindings_,
clone->is_initializing_) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::FunctionType;
}
template <typename F>
auto Decompose(F f) const {
return f(method_self_, parameters_, generic_parameters_, return_type_,
deduced_bindings_, impl_bindings_, is_initializing_);
}
// The type of the function parameter tuple.
auto parameters() const -> const Value& { return *parameters_; }
// Parameters that use a generic `:!` binding at the top level.
auto generic_parameters() const -> llvm::ArrayRef<GenericParameter> {
return generic_parameters_;
}
// The function return type.
auto return_type() const -> const Value& { return *return_type_; }
// All generic bindings in this function's signature that should be deduced
// in a call. This excludes any generic parameters.
auto deduced_bindings() const
-> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
return deduced_bindings_;
}
// The bindings for the impl witness tables required by the
// bounds on the type parameters of the generic function.
auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
return impl_bindings_;
}
// Return whether the function type is an initializing expression or not.
auto is_initializing() const -> bool { return is_initializing_; }
// Binding for the implicit `self` parameter, if this is an unbound method.
auto method_self() const -> std::optional<MethodSelf> { return method_self_; }
private:
std::optional<MethodSelf> method_self_;
Nonnull<const Value*> parameters_;
std::vector<GenericParameter> generic_parameters_;
Nonnull<const Value*> return_type_;
std::vector<Nonnull<const GenericBinding*>> deduced_bindings_;
std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
bool is_initializing_;
};
// A pointer type.
class PointerType : public Value {
public:
// Constructs a pointer type with the given pointee type.
explicit PointerType(Nonnull<const Value*> pointee_type)
: Value(Kind::PointerType), pointee_type_(pointee_type) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::PointerType;
}
template <typename F>
auto Decompose(F f) const {
return f(pointee_type_);
}
auto pointee_type() const -> const Value& { return *pointee_type_; }
private:
Nonnull<const Value*> pointee_type_;
};
// The `auto` type.
class AutoType : public Value {
public:
AutoType() : Value(Kind::AutoType) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::AutoType;
}
template <typename F>
auto Decompose(F f) const {
return f();
}
};
// A struct type.
class StructType : public Value {
public:
StructType() : StructType(std::vector<NamedValue>{}) {}
explicit StructType(std::vector<NamedValue> fields)
: Value(Kind::StructType), fields_(std::move(fields)) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::StructType;
}
template <typename F>
auto Decompose(F f) const {
return f(fields_);
}
auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
private:
std::vector<NamedValue> fields_;
};
// A class type.
class NominalClassType : public Value {
public:
explicit NominalClassType(
Nonnull<const ClassDeclaration*> declaration,
Nonnull<const Bindings*> bindings,
std::optional<Nonnull<const NominalClassType*>> base,
Nonnull<const VTable*> class_vtable)
: Value(Kind::NominalClassType),
declaration_(declaration),
bindings_(bindings),
base_(base),
vtable_(class_vtable),
hierarchy_level_(base ? (*base)->hierarchy_level() + 1 : 0) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::NominalClassType;
}
template <typename F>
auto Decompose(F f) const {
return f(declaration_, bindings_, base_, vtable_);
}
auto declaration() const -> const ClassDeclaration& { return *declaration_; }
auto bindings() const -> const Bindings& { return *bindings_; }
auto base() const -> std::optional<Nonnull<const NominalClassType*>> {
return base_;
}
auto type_args() const -> const BindingMap& { return bindings_->args(); }
// Witnesses for each of the class's impl bindings.
auto witnesses() const -> const ImplWitnessMap& {
return bindings_->witnesses();
}
auto vtable() const -> const VTable& { return *vtable_; }
// Returns how many levels from the top ancestor class it is. i.e. a class
// with no base returns `0`, while a class with a `.base` and `.base.base`
// returns `2`.
auto hierarchy_level() const -> int { return hierarchy_level_; }
// Returns whether this a parameterized class. That is, a class with
// parameters and no corresponding arguments.
auto IsParameterized() const -> bool {
return declaration_->type_params().has_value() && type_args().empty();
}
// Returns whether this class is, or inherits `other`.
auto InheritsClass(Nonnull<const Value*> other) const -> bool;
private:
Nonnull<const ClassDeclaration*> declaration_;
Nonnull<const Bindings*> bindings_ = Bindings::None();
const std::optional<Nonnull<const NominalClassType*>> base_;
Nonnull<const VTable*> vtable_;
int hierarchy_level_;
};
class MixinPseudoType : public Value {
public:
explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration)
: Value(Kind::MixinPseudoType), declaration_(declaration) {
CARBON_CHECK(!declaration->params().has_value(),
"missing arguments for parameterized mixin type");
}
explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration,
Nonnull<const Bindings*> bindings)
: Value(Kind::MixinPseudoType),
declaration_(declaration),
bindings_(bindings) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::MixinPseudoType;
}
template <typename F>
auto Decompose(F f) const {
return f(declaration_, bindings_);
}
auto declaration() const -> const MixinDeclaration& { return *declaration_; }
auto bindings() const -> const Bindings& { return *bindings_; }
auto args() const -> const BindingMap& { return bindings_->args(); }
auto witnesses() const -> const ImplWitnessMap& {
return bindings_->witnesses();
}
auto FindFunction(const std::string_view& name) const
-> std::optional<Nonnull<const FunctionValue*>>;
private:
Nonnull<const MixinDeclaration*> declaration_;
Nonnull<const Bindings*> bindings_ = Bindings::None();
};
// Returns the value of the function named `name` in this class, or
// nullopt if there is no such function.
auto FindFunction(std::string_view name,
llvm::ArrayRef<Nonnull<Declaration*>> members)
-> std::optional<Nonnull<const FunctionValue*>>;
// Returns the value of the function named `name` in this class and its
// parents, or nullopt if there is no such function.
auto FindFunctionWithParents(std::string_view name,
const ClassDeclaration& class_decl)
-> std::optional<Nonnull<const FunctionValue*>>;
// Return the declaration of the member with the given name.
auto FindMember(std::string_view name,
llvm::ArrayRef<Nonnull<Declaration*>> members)
-> std::optional<Nonnull<const Declaration*>>;
// An interface type.
class InterfaceType : public Value {
public:
explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
: Value(Kind::InterfaceType), declaration_(declaration) {
CARBON_CHECK(!declaration->params().has_value(),
"missing arguments for parameterized interface type");
}
explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
Nonnull<const Bindings*> bindings)
: Value(Kind::InterfaceType),
declaration_(declaration),
bindings_(bindings) {}
static auto classof(const Value* value) -> bool {
return value->kind() == Kind::InterfaceType;
}
template <typename F>
auto Decompose(F f) const {
return f(declaration_, bindings_);