-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTerm.h
243 lines (186 loc) · 6.48 KB
/
Term.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
//===--- Term.h - A term in the generics rewrite system ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "Symbol.h"
#ifndef SWIFT_RQM_TERM_H
#define SWIFT_RQM_TERM_H
namespace llvm {
class raw_ostream;
}
namespace swift {
namespace rewriting {
/// A term is a sequence of one or more symbols.
///
/// The Term type is a uniqued, permanently-allocated representation,
/// used to represent terms in the rewrite rules themselves. See also
/// MutableTerm for the other representation.
///
/// The first symbol in the term must be a protocol, generic parameter, or
/// associated type symbol.
///
/// A layout, superclass or concrete type symbol must only appear at the
/// end of a term.
///
/// Out-of-line methods are documented in Term.cpp.
class Term final {
friend class RewriteContext;
struct Storage;
const Storage *Ptr;
Term(const Storage *ptr) : Ptr(ptr) {}
public:
size_t size() const;
const Symbol *begin() const;
const Symbol *end() const;
std::reverse_iterator<const Symbol *> rbegin() const;
std::reverse_iterator<const Symbol *> rend() const;
Symbol back() const;
Symbol operator[](size_t index) const;
/// Returns an opaque pointer that uniquely identifies this term.
const void *getOpaquePointer() const {
return Ptr;
}
static Term fromOpaquePointer(void *ptr) {
return Term((Storage *) ptr);
}
static Term get(const MutableTerm &term, RewriteContext &ctx);
const ProtocolDecl *getRootProtocol() const {
return begin()->getRootProtocol();
}
bool containsUnresolvedSymbols() const;
void dump(llvm::raw_ostream &out) const;
Optional<int> compare(Term other, RewriteContext &ctx) const;
friend bool operator==(Term lhs, Term rhs) {
return lhs.Ptr == rhs.Ptr;
}
friend bool operator!=(Term lhs, Term rhs) {
return !(lhs == rhs);
}
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out, Term term) {
term.dump(out);
return out;
}
};
/// A term is a sequence of one or more symbols.
///
/// The MutableTerm type is a dynamically-allocated representation,
/// used to represent temporary values in simplification and completion.
/// See also Term for the other representation.
///
/// The first symbol in the term must be a protocol, generic parameter, or
/// associated type symbol.
///
/// A layout constraint symbol must only appear at the end of a term.
///
/// Out-of-line methods are documented in RewriteSystem.cpp.
class MutableTerm final {
llvm::SmallVector<Symbol, 3> Symbols;
public:
/// Creates an empty term. At least one symbol must be added for the term
/// to become valid.
MutableTerm() {}
explicit MutableTerm(const Symbol *begin,
const Symbol *end)
: Symbols(begin, end) {}
explicit MutableTerm(llvm::SmallVector<Symbol, 3> &&symbols)
: Symbols(std::move(symbols)) {}
explicit MutableTerm(ArrayRef<Symbol> symbols)
: Symbols(symbols.begin(), symbols.end()) {}
explicit MutableTerm(Term term)
: Symbols(term.begin(), term.end()) {}
void add(Symbol symbol) {
Symbols.push_back(symbol);
}
void append(Term other) {
Symbols.append(other.begin(), other.end());
}
void append(const MutableTerm &other) {
Symbols.append(other.begin(), other.end());
}
void append(const Symbol *from, const Symbol *to) {
Symbols.append(from, to);
}
Optional<int> compare(const MutableTerm &other, RewriteContext &ctx) const;
bool empty() const { return Symbols.empty(); }
size_t size() const { return Symbols.size(); }
const ProtocolDecl *getRootProtocol() const {
return begin()->getRootProtocol();
}
const Symbol *begin() const { return Symbols.begin(); }
const Symbol *end() const { return Symbols.end(); }
Symbol *begin() { return Symbols.begin(); }
Symbol *end() { return Symbols.end(); }
std::reverse_iterator<const Symbol *> rbegin() const { return Symbols.rbegin(); }
std::reverse_iterator<const Symbol *> rend() const { return Symbols.rend(); }
std::reverse_iterator<Symbol *> rbegin() { return Symbols.rbegin(); }
std::reverse_iterator<Symbol *> rend() { return Symbols.rend(); }
Symbol back() const {
return Symbols.back();
}
Symbol &back() {
return Symbols.back();
}
Symbol operator[](size_t index) const {
return Symbols[index];
}
Symbol &operator[](size_t index) {
return Symbols[index];
}
void rewriteSubTerm(Symbol *from, Symbol *to, Term rhs);
void dump(llvm::raw_ostream &out) const;
friend bool operator==(const MutableTerm &lhs, const MutableTerm &rhs) {
if (lhs.size() != rhs.size())
return false;
return std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
friend bool operator!=(const MutableTerm &lhs, const MutableTerm &rhs) {
return !(lhs == rhs);
}
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
const MutableTerm &term) {
term.dump(out);
return out;
}
};
} // end namespace rewriting
} // end namespace swift
namespace llvm {
template<> struct DenseMapInfo<swift::rewriting::Term> {
static swift::rewriting::Term getEmptyKey() {
return swift::rewriting::Term::fromOpaquePointer(
llvm::DenseMapInfo<void *>::getEmptyKey());
}
static swift::rewriting::Term getTombstoneKey() {
return swift::rewriting::Term::fromOpaquePointer(
llvm::DenseMapInfo<void *>::getTombstoneKey());
}
static unsigned getHashValue(swift::rewriting::Term Val) {
return DenseMapInfo<void *>::getHashValue(Val.getOpaquePointer());
}
static bool isEqual(swift::rewriting::Term LHS,
swift::rewriting::Term RHS) {
return LHS == RHS;
}
};
template<>
struct PointerLikeTypeTraits<swift::rewriting::Term> {
public:
static inline void *getAsVoidPointer(swift::rewriting::Term Val) {
return const_cast<void *>(Val.getOpaquePointer());
}
static inline swift::rewriting::Term getFromVoidPointer(void *Ptr) {
return swift::rewriting::Term::fromOpaquePointer(Ptr);
}
enum { NumLowBitsAvailable = 1 };
};
} // end namespace llvm
#endif