-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathMetadataPath.h
227 lines (190 loc) · 7.11 KB
/
MetadataPath.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
//===--- MetadataPath.h - Path for lazily finding type metadata -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the MetadataPath type, which efficiently records the
// path to a metadata object.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_METADATAPATH_H
#define SWIFT_IRGEN_METADATAPATH_H
#include "swift/Basic/EncodedSequence.h"
#include "swift/Reflection/MetadataSource.h"
namespace llvm {
class Value;
}
namespace swift {
class ProtocolDecl;
class CanType;
class Decl;
namespace irgen {
class IRGenFunction;
class LocalTypeDataKey;
/// A path from one source metadata --- either Swift type metadata or a Swift
/// protocol conformance --- to another.
class MetadataPath {
class Component {
public:
enum class Kind {
// Some components carry indices.
// P means the primary index.
/// Base protocol P of a protocol.
InheritedProtocol,
/// Witness table at requirement index P of a generic nominal type.
NominalTypeArgumentConformance,
/// Type metadata at requirement index P of a generic nominal type.
NominalTypeArgument,
LastWithPrimaryIndex = NominalTypeArgument,
// Everything past this point has no index.
/// The parent metadata of a nominal type.
NominalParent,
/// An impossible path.
Impossible,
};
private:
unsigned Primary;
enum {
KindMask = 0xF,
IndexShift = 4,
};
static bool hasPrimaryIndex(Kind kind) {
return kind <= Kind::LastWithPrimaryIndex;
}
explicit Component(unsigned primary)
: Primary(primary) {}
public:
explicit Component(Kind kind)
: Primary(unsigned(kind)) {
assert(!hasPrimaryIndex(kind));
}
explicit Component(Kind kind, unsigned primaryIndex)
: Primary(unsigned(kind) | (primaryIndex << IndexShift)) {
assert(hasPrimaryIndex(kind));
}
Kind getKind() const { return Kind(Primary & KindMask); }
unsigned getPrimaryIndex() const {
assert(hasPrimaryIndex(getKind()));
return (Primary >> IndexShift);
}
/// Return an abstract measurement of the cost of this component.
unsigned cost() const {
// Right now, all components cost the same: they take one load.
// In the future, maybe some components will be cheaper (no loads,
// like loading from a superclass's metadata) or more expensive
// (multiple loads, or even a call).
return 1;
}
static Component decode(const EncodedSequenceBase::Chunk *&ptr) {
unsigned primary = EncodedSequenceBase::decodeIndex(ptr);
return Component(primary);
}
void encode(EncodedSequenceBase::Chunk *&ptr) const {
EncodedSequenceBase::encodeIndex(Primary, ptr);
}
unsigned getEncodedSize() const {
auto size = EncodedSequenceBase::getEncodedIndexSize(Primary);
return size;
}
};
EncodedSequence<Component> Path;
public:
MetadataPath() {}
using iterator = EncodedSequence<Component>::iterator;
template <class ValueType>
using Map = EncodedSequence<Component>::Map<ValueType>;
/// Add a step to this path which will cause a dynamic assertion if
/// it's followed.
void addImpossibleComponent() {
Path.push_back(Component(Component::Kind::Impossible));
}
/// Add a step to this path which gets the parent metadata.
void addNominalParentComponent() {
Path.push_back(Component(Component::Kind::NominalParent));
}
/// Add a step to this path which gets the type metadata stored at
/// requirement index n in a generic type metadata.
void addNominalTypeArgumentComponent(unsigned index) {
Path.push_back(Component(Component::Kind::NominalTypeArgument, index));
}
/// Add a step to this path which gets the protocol witness table
/// stored at requirement index n in a generic type metadata.
void addNominalTypeArgumentConformanceComponent(unsigned index) {
Path.push_back(Component(Component::Kind::NominalTypeArgumentConformance,
index));
}
/// Add a step to this path which gets the kth inherited protocol from a
/// witness table.
///
/// k is computed including protocols which do not have witness tables.
void addInheritedProtocolComponent(unsigned index) {
Path.push_back(Component(Component::Kind::InheritedProtocol, index));
}
/// Return an abstract measurement of the cost of this path.
unsigned cost() const {
unsigned cost = 0;
for (const Component &component : Path)
cost += component.cost();
return cost;
}
/// Given a pointer to type metadata, follow a path from it.
llvm::Value *followFromTypeMetadata(IRGenFunction &IGF,
CanType sourceType,
llvm::Value *source,
Map<llvm::Value*> *cache) const;
/// Given a pointer to a protocol witness table, follow a path from it.
llvm::Value *followFromWitnessTable(IRGenFunction &IGF,
CanType conformingType,
ProtocolConformanceRef conformance,
llvm::Value *source,
Map<llvm::Value*> *cache) const;
template <typename Allocator>
const reflection::MetadataSource *
getMetadataSource(Allocator &A,
const reflection::MetadataSource *Root) const {
if (Root == nullptr)
return nullptr;
for (auto C : Path) {
switch (C.getKind()) {
case Component::Kind::NominalParent:
Root = A.template createParent(Root);
continue;
case Component::Kind::NominalTypeArgument:
Root = A.template createGenericArgument(C.getPrimaryIndex(), Root);
continue;
default:
return nullptr;
}
}
return Root;
}
void dump() const;
void print(llvm::raw_ostream &out) const;
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
const MetadataPath &path) {
path.print(out);
return out;
}
private:
static llvm::Value *follow(IRGenFunction &IGF,
LocalTypeDataKey key,
llvm::Value *source,
MetadataPath::iterator begin,
MetadataPath::iterator end,
Map<llvm::Value*> *cache);
/// Follow a single component of a metadata path.
static llvm::Value *followComponent(IRGenFunction &IGF,
LocalTypeDataKey &key,
llvm::Value *source,
Component component);
};
} // end namespace irgen
} // end namespace swift
#endif