Debian Codegraph @

gcc/rust/typecheck/rust-tyty-region.h

raw
1 #ifndef RUST_TYTY_REGION_H
2 #define RUST_TYTY_REGION_H
3
4 namespace Rust {
5 namespace TyTy {
6
7 class Region
8 {
9 enum Variant : uint8_t
10 {
11 UNRESOLVED,
12 STATIC,
13 EARLY_BOUND,
14 LATE_BOUND,
15 NAMED,
16 ANONYMOUS,
17 ERASED,
18 };
19
20 uint32_t index;
21 uint16_t debruijn_index;
22 Variant variant;
23
24 public:
25 Region () : Region (UNRESOLVED) {}
26 Region (const Region &other)
27 : index (other.index), debruijn_index (other.debruijn_index),
28 variant (other.variant)
29 {}
30 Region (Region &&other) noexcept
31 : index (other.index), debruijn_index (other.debruijn_index),
32 variant (other.variant)
33 {}
34 Region &operator= (const Region &other)
35 {
36 if (this == &other)
37 return *this;
38 index = other.index;
39 debruijn_index = other.debruijn_index;
40 variant = other.variant;
41 return *this;
42 }
43 Region &operator= (Region &&other) noexcept
44 {
45 if (this == &other)
46 return *this;
47 index = other.index;
48 debruijn_index = other.debruijn_index;
49 variant = other.variant;
50 return *this;
51 }
52
53 static Region make_static () { return Region (STATIC); }
54 static Region make_early_bound (uint32_t index)
55 {
56 return Region (EARLY_BOUND, index);
57 }
58 static Region make_late_bound (uint32_t index, uint16_t debruijn_index)
59 {
60 return Region (LATE_BOUND, index, debruijn_index);
61 }
62 static Region make_named (uint32_t index) { return Region (NAMED, index); }
63 static Region make_anonymous () { return Region (ANONYMOUS); }
64 static Region make_erased () { return Region (ERASED); }
65
66 size_t get_index () const { return index; }
67
68 bool is_static () const { return variant == STATIC; }
69 bool is_early_bound () const { return variant == EARLY_BOUND; }
70 bool is_late_bound () const { return variant == LATE_BOUND; }
71 bool is_named () const { return variant == NAMED; }
72 bool is_anonymous () const { return variant == ANONYMOUS; }
73
74 void shift_down () { debruijn_index++; }
75
76 WARN_UNUSED_RESULT std::string as_string () const
77 {
78 switch (variant)
79 {
80 case UNRESOLVED:
81 return "'unresolved";
82 case STATIC:
83 return "'static";
84 case EARLY_BOUND:
85 return "'early(" + std::to_string (index) + ")";
86 case LATE_BOUND:
87 return "'late(" + std::to_string (debruijn_index) + ", "
88 + std::to_string (index) + ")";
89 case NAMED:
90 return "'named(" + std::to_string (index) + "";
91 case ANONYMOUS:
92 return "'_";
93 case ERASED:
94 return "'erased";
95 }
96
97 rust_unreachable ();
98 }
99
100 private:
101 explicit Region (Variant variant, uint32_t index = 0,
102 uint16_t debruijn_index = 0)
103 : index (index), debruijn_index (debruijn_index), variant (variant)
104 {}
105 };
106
107 } // namespace TyTy
108 } // namespace Rust
109
110 #endif // RUST_TYTY_REGION_H