-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathLValue.h
288 lines (240 loc) · 9.58 KB
/
LValue.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
//===--- LValue.h - Logical LValue Representation ---------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A storage structure for keeping track of logical lvalues during SILGen.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LOWERING_LVALUE_H
#define SWIFT_LOWERING_LVALUE_H
#include "SILGenFunction.h"
#include "swift/Basic/DiverseList.h"
namespace swift {
namespace Lowering {
class SILGenFunction;
class ManagedValue;
class PhysicalPathComponent;
class LogicalPathComponent;
/// Information about the type of an l-value.
struct LValueTypeData {
AbstractionPattern OrigFormalType;
CanType SubstFormalType;
SILType TypeOfRValue;
};
/// An l-value path component represents a chunk of the access path to
/// an object. Path components may be either "physical" or "logical".
/// A physical path involves elementary address manipulations; these
/// address manipulations may be in some way dynamic, but they are
/// ultimately just pointer arithmetic. A logical path requires
/// getter/setter logic.
///
/// This divide between physical/logical is closely related to the
/// fragile/resilient split, with two primary differences:
/// - Any sort of implementation can be fragile. For example, a
/// computed variable can still be fragile, meaning that it is known
/// to be implemented with a getter/setter. The known
/// implementation must be a direct offset in order to qualify as
/// physical.
/// - A path component's implementation can be resilient and yet
/// still qualify for physical access if we are in a privileged
/// component.
class PathComponent {
LValueTypeData TypeData;
friend class LValue;
unsigned AllocatedSize : 31;
const unsigned IsPhysical : 1;
// This anchor method serves three purposes: it aligns the class to
// a pointer boundary, it makes the class a primary base so that
// subclasses will be at offset zero, and it anchors the v-table
// to a specific file.
virtual void _anchor();
PathComponent(const PathComponent &) = delete;
PathComponent &operator=(const PathComponent &) = delete;
protected:
PathComponent(LValueTypeData typeData, bool isPhysical)
: TypeData(typeData), IsPhysical(isPhysical) {}
virtual ~PathComponent() {}
public:
/// Returns sizeof(the final type), plus any extra storage required.
size_t allocated_size() const { return AllocatedSize; }
/// Is this component physical or logical? If physical, this will
/// be a subclass of PhysicalPathComponent. If logical, this will
/// be a subclass of LogicalPathComponent.
bool isPhysical() const { return IsPhysical; }
bool isLogical() const { return !IsPhysical; }
// These are implemented inline after the respective class declarations.
PhysicalPathComponent &asPhysical();
const PhysicalPathComponent &asPhysical() const;
LogicalPathComponent &asLogical();
const LogicalPathComponent &asLogical() const;
/// Returns the logical type-as-rvalue of the value addressed by the
/// component.
SILType getTypeOfRValue() const { return TypeData.TypeOfRValue; }
AbstractionPattern getOrigFormalType() const {
return TypeData.OrigFormalType;
}
CanType getSubstFormalType() const { return TypeData.SubstFormalType; }
const LValueTypeData &getTypeData() const { return TypeData; }
};
/// An abstract class for "physical" path components, i.e. path
/// components that can be accessed as address manipulations. See the
/// comment for PathComponent for more information.
class PhysicalPathComponent : public PathComponent {
virtual void _anchor();
protected:
PhysicalPathComponent(LValueTypeData typeData)
: PathComponent(typeData, true) {}
public:
virtual ManagedValue offset(SILGenFunction &gen,
SILLocation loc,
ManagedValue base) const = 0;
};
inline PhysicalPathComponent &PathComponent::asPhysical() {
assert(isPhysical());
return static_cast<PhysicalPathComponent&>(*this);
}
inline const PhysicalPathComponent &PathComponent::asPhysical() const {
assert(isPhysical());
return static_cast<const PhysicalPathComponent&>(*this);
}
/// An abstract class for "logical" path components, i.e. path
/// components that require getter/setter methods to access. See the
/// comment for PathComponent for more information.
class LogicalPathComponent : public PathComponent {
virtual void _anchor();
protected:
LogicalPathComponent(LValueTypeData typeData)
: PathComponent(typeData, false) {}
public:
/// Clone the path component onto the heap.
virtual std::unique_ptr<LogicalPathComponent>
clone(SILGenFunction &gen, SILLocation l) const = 0;
/// Set the property.
virtual void set(SILGenFunction &gen, SILLocation loc,
RValue &&value, ManagedValue base) const = 0;
/// Get the property.
virtual ManagedValue get(SILGenFunction &gen, SILLocation loc,
ManagedValue base, SGFContext c) const = 0;
/// Get the property, materialize a temporary lvalue for it, and if
/// we're in a writeback scope, register a writeback. This returns the
/// address of the buffer.
SILValue getMaterialized(SILGenFunction &gen, SILLocation loc,
ManagedValue base) const;
};
inline LogicalPathComponent &PathComponent::asLogical() {
assert(isLogical());
return static_cast<LogicalPathComponent&>(*this);
}
inline const LogicalPathComponent &PathComponent::asLogical() const {
assert(isLogical());
return static_cast<const LogicalPathComponent&>(*this);
}
/// An lvalue represents a reference to storage holding a value
/// of a type, as opposed to an rvalue, which is an actual value
/// of the type.
class LValue {
DiverseList<PathComponent, 128> Path;
/// Iterating to the end of the l-value is expensive, so we cache it
/// here.
LValueTypeData TypeData;
public:
LValue() = default;
LValue(const LValue &other) = default;
LValue(LValue &&other) = default;
bool isValid() const { return !Path.empty(); }
/// Is this lvalue purely physical?
bool isPhysical() const {
assert(isValid());
for (auto &component : Path)
if (!component.isPhysical())
return false;
return true;
}
/// Is the lvalue's final component physical?
bool isLastComponentPhysical() const {
assert(isValid());
auto component = begin(), next = begin(), e = end();
++next;
for (; next != e; component = next, ++next) { }
return component->isPhysical();
}
/// Add a new component at the end of the access path of this lvalue.
template <class T, class... A> T &add(A &&...args) {
T &component = Path.add<T>(std::forward<A>(args)...);
component.AllocatedSize = sizeof(T);
assert(component.allocated_size() == sizeof(T));
TypeData = component.getTypeData();
return component;
}
template <class T, class... A> T &addWithExtra(A &&...args) {
size_t extraSize = T::extra_storage_size(std::forward<A>(args)...);
T &component = Path.addWithExtra<T>(extraSize, std::forward<A>(args)...);
component.AllocatedSize = sizeof(T) + extraSize;
assert(component.allocated_size() == sizeof(T) + extraSize);
TypeData = component.getTypeData();
return component;
}
typedef DiverseListImpl<PathComponent>::iterator iterator;
typedef DiverseListImpl<PathComponent>::const_iterator const_iterator;
iterator begin() { return Path.begin(); }
iterator end() { return Path.end(); }
const_iterator begin() const { return Path.begin(); }
const_iterator end() const { return Path.end(); }
/// Returns the type-of-rvalue of the logical object referenced by
/// this l-value. Note that this may differ significantly from the
/// type of l-value.
SILType getTypeOfRValue() const { return TypeData.TypeOfRValue; }
CanType getSubstFormalType() const { return TypeData.SubstFormalType; }
AbstractionPattern getOrigFormalType() const {
return TypeData.OrigFormalType;
}
const LValueTypeData &getTypeData() const { return TypeData; }
};
/// RAII object to enable writebacks for logical lvalues evaluated within the
/// scope, which will be applied when the object goes out of scope.
class WritebackScope {
SILGenFunction *gen;
bool wasInWritebackScope;
size_t savedDepth;
public:
WritebackScope(SILGenFunction &gen);
~WritebackScope();
WritebackScope(const WritebackScope &) = delete;
WritebackScope &operator=(const WritebackScope &) = delete;
WritebackScope(WritebackScope &&o);
WritebackScope &operator=(WritebackScope &&o);
};
/// RAII object to disable writebacks for logical lvalues evaluated within the
/// scope. Used for LoadExprs.
class DisableWritebackScope {
SILGenFunction &gen;
bool wasInWritebackScope;
public:
DisableWritebackScope(SILGenFunction &gen)
: gen(gen), wasInWritebackScope(gen.InWritebackScope)
{
gen.InWritebackScope = false;
}
~DisableWritebackScope() {
gen.InWritebackScope = wasInWritebackScope;
}
};
/// RAII object used to enter an inout conversion scope. Writeback scopes formed
/// during the inout conversion scope will be no-ops.
class InOutConversionScope {
SILGenFunction &gen;
public:
InOutConversionScope(SILGenFunction &gen);
~InOutConversionScope();
};
} // end namespace Lowering
} // end namespace swift
#endif