-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathConformanceLookupTable.h
555 lines (457 loc) · 20.5 KB
/
ConformanceLookupTable.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
//===--- ConformanceLookupTable - Conformance Lookup Table ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the ConformanceLookupTable class, which manages protocol
// conformances for a given nominal type. Most clients should not access this
// table directly; rather, they should go through the NominalTypeDecl or
// DeclContext entry points.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_CONFORMANCE_LOOKUP_TABLE_H
#define SWIFT_AST_CONFORMANCE_LOOKUP_TABLE_H
#include "swift/AST/DeclContext.h"
#include "swift/AST/ConformanceAttributes.h"
#include "swift/AST/ProtocolConformanceOptions.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/TinyPtrVector.h"
#include <unordered_map>
namespace swift {
class ExtensionDecl;
class ModuleDecl;
/// Keeps track of the protocols to which a particular nominal type conforms.
///
/// This table is a lower-level detail that clients should generally not
/// access directly. Rather, one should use the protocol- and
/// conformance-centric entry points in \c NominalTypeDecl and \c DeclContext.
class ConformanceLookupTable : public ASTAllocated<ConformanceLookupTable> {
/// Describes the stage at which a particular nominal type or
/// extension's conformances has been processed.
enum class ConformanceStage : uint8_t {
/// The explicit conformances have been recorded in the lookup table.
RecordedExplicit,
/// Conformances from the superclass have been inherited.
Inherited,
/// The explicit conformances have been expanded out to include
/// the conformances they imply.
ExpandedImplied,
/// The complete set of conformances have been fully resolved to
/// assign conformances, diagnose conflicts, etc.
Resolved,
};
/// The number of conformance stages.
static const unsigned NumConformanceStages = 4;
/// An entry in the last-processed list, which contains a pointer to
/// the last extension that was processed at a particular stage (or
/// nullptr if no extensions have been processed) and indicates
/// whether the nominal type declaration itself has been processed
/// at that stage.
typedef llvm::PointerIntPair<ExtensionDecl *, 1, bool> LastProcessedEntry;
/// Array indicating how far we have gotten in processing each
/// nominal type and list of extensions for each stage of
/// conformance checking.
///
/// Uses std::unordered_map instead of DenseMap so that stable interior
/// references can be taken.
std::unordered_map<NominalTypeDecl *,
std::array<LastProcessedEntry, NumConformanceStages>>
LastProcessed;
struct ConformanceEntry;
/// Describes the "source" of a conformance, indicating where the
/// conformance came from.
class ConformanceSource {
void *Storage;
ConformanceEntryKind Kind;
ConformanceAttributes attributes;
ConformanceSource(void *ptr, ConformanceEntryKind kind)
: Storage(ptr), Kind(kind) { }
public:
/// Create an inherited conformance.
///
/// The given class will have an inherited conformance for the
/// requested protocol.
static ConformanceSource forInherited(ClassDecl *classDecl) {
return ConformanceSource(classDecl, ConformanceEntryKind::Inherited);
}
/// Create an explicit conformance.
///
/// The given declaration context (nominal type declaration or
/// extension thereof) explicitly specifies conformance to the
/// protocol.
static ConformanceSource forExplicit(DeclContext *dc) {
return ConformanceSource(dc, ConformanceEntryKind::Explicit);
}
/// Create an implied conformance.
///
/// Conformance to the protocol is implied by the given
/// conformance entry. The chain of conformance entries will
/// eventually terminate in a non-implied conformance.
static ConformanceSource forImplied(ConformanceEntry *entry) {
return ConformanceSource(entry, ConformanceEntryKind::Implied);
}
/// Create a synthesized conformance.
///
/// The given declaration context (for a type) will get a synthesized
/// conformance to the requested protocol.
static ConformanceSource forSynthesized(DeclContext *dc) {
return ConformanceSource(dc, ConformanceEntryKind::Synthesized);
}
static ConformanceSource forUnexpandedMacro(DeclContext *dc) {
return ConformanceSource(dc, ConformanceEntryKind::PreMacroExpansion);
}
/// Return a new conformance source with the given conformance
/// attributes.
ConformanceSource withAttributes(ConformanceAttributes attributes) {
ConformanceSource result(*this);
result.attributes |= attributes;
return result;
}
ConformanceAttributes getAttributes() const {
return attributes;
}
ProtocolConformanceOptions getOptions() const {
ProtocolConformanceOptions options;
if (getUncheckedLoc().isValid())
options |= ProtocolConformanceFlags::Unchecked;
if (getPreconcurrencyLoc().isValid())
options |= ProtocolConformanceFlags::Preconcurrency;
if (getUnsafeLoc().isValid())
options |= ProtocolConformanceFlags::Unsafe;
if (getNonisolatedLoc().isValid())
options |= ProtocolConformanceFlags::Nonisolated;
if (attributes.globalActorType)
options.setGlobalActorIsolation(attributes.globalActorType);
return options;
}
/// Retrieve the kind of conformance formed from this source.
ConformanceEntryKind getKind() const { return Kind; }
/// Retrieve kind of the conformance for ranking purposes.
///
/// The only difference between the ranking kind and the kind is
/// that implied conformances originating from a synthesized
/// or pre-macro-expansion conformance are considered to be synthesized (which has a
/// lower ranking).
ConformanceEntryKind getRankingKind() const {
switch (auto kind = getKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Inherited:
case ConformanceEntryKind::Synthesized:
case ConformanceEntryKind::PreMacroExpansion:
return kind;
case ConformanceEntryKind::Implied: {
auto impliedSourceKind =
getImpliedSource()->getDeclaredConformance()->getKind();
switch (impliedSourceKind) {
case ConformanceEntryKind::Synthesized:
case ConformanceEntryKind::PreMacroExpansion:
return impliedSourceKind;
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Inherited:
return ConformanceEntryKind::Implied;
case ConformanceEntryKind::Implied:
return getImpliedSource()->getRankingKind();
}
}
}
llvm_unreachable("Unhandled ConformanceEntryKind in switch.");
}
/// The location of the @unchecked attribute, if any.
SourceLoc getUncheckedLoc() const {
return attributes.uncheckedLoc;
}
SourceLoc getPreconcurrencyLoc() const {
return attributes.preconcurrencyLoc;
}
/// The location of the @unsafe attribute, if any.
SourceLoc getUnsafeLoc() const {
return attributes.unsafeLoc;
}
/// The location of the isolated modifier, if any.
SourceLoc getNonisolatedLoc() const {
return attributes.nonisolatedLoc;
}
/// For an inherited conformance, retrieve the class declaration
/// for the inheriting class.
ClassDecl *getInheritingClass() const {
assert(getKind() == ConformanceEntryKind::Inherited);
return static_cast<ClassDecl *>(Storage);
}
/// For an explicit conformance, retrieve the declaration context
/// that specifies the conformance.
DeclContext *getExplicitDeclContext() const {
assert(getKind() == ConformanceEntryKind::Explicit);
return static_cast<DeclContext *>(Storage);
}
DeclContext *getMacroGeneratedDeclContext() const {
assert(getKind() == ConformanceEntryKind::PreMacroExpansion);
return static_cast<DeclContext *>(Storage);
}
/// For a synthesized conformance, retrieve the nominal type decl
/// that will receive the conformance.
ConformanceEntry *getImpliedSource() const {
assert(getKind() == ConformanceEntryKind::Implied);
return static_cast<ConformanceEntry *>(Storage);
}
/// For a synthesized conformance, retrieve the nominal type decl
/// that will receive the conformance.
DeclContext *getSynthesizedDeclContext() const {
assert(getKind() == ConformanceEntryKind::Synthesized);
return static_cast<DeclContext *>(Storage);
}
/// Get the declaration context that this conformance will be
/// associated with.
DeclContext *getDeclContext() const;
};
/// An entry in the conformance table.
struct ConformanceEntry : public ASTAllocated<ConformanceEntry> {
/// The source location within the current context where the
/// protocol conformance was specified.
SourceLoc Loc;
/// If this conformance entry has been superseded, the conformance
/// that superseded it.
ConformanceEntry *SupersededBy = nullptr;
/// The source of this conformance entry , which is either a
/// DeclContext (for an explicitly-specified conformance) or a
/// link to the conformance that implied this conformance.
ConformanceSource Source;
/// Either the protocol to be resolved or the resolved protocol conformance.
llvm::PointerUnion<ProtocolDecl *, ProtocolConformance *> Conformance;
ConformanceEntry(SourceLoc loc, ProtocolDecl *protocol,
ConformanceSource source)
: Loc(loc), Source(source), Conformance(protocol) { }
/// Retrieve the location at which this conformance was declared
/// or synthesized.
SourceLoc getLoc() const { return Loc; }
/// Whether this conformance is already "fixed" and cannot be superseded.
bool isFixed() const {
// A conformance from an unexpanded macro can always be superseded.
if (getKind() == ConformanceEntryKind::PreMacroExpansion)
return true;
// If a conformance has been assigned, it cannot be superseded.
if (getConformance())
return true;
// Otherwise, only inherited conformances are fixed.
switch (getKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Implied:
case ConformanceEntryKind::Synthesized:
case ConformanceEntryKind::PreMacroExpansion:
return false;
case ConformanceEntryKind::Inherited:
return true;
}
llvm_unreachable("Unhandled ConformanceEntryKind in switch.");
}
/// Whether this protocol conformance was superseded by another
/// conformance.
bool isSuperseded() const { return SupersededBy != nullptr; }
/// Retrieve the conformance entry that superseded this one.
ConformanceEntry *getSupersededBy() const { return SupersededBy; }
/// Note that this conformance entry was superseded by the given
/// entry.
void markSupersededBy(ConformanceLookupTable &table,
ConformanceEntry *entry,
bool diagnose);
/// Determine the kind of conformance.
ConformanceEntryKind getKind() const {
return Source.getKind();
}
/// Determine the kind of conformance for ranking purposes.
ConformanceEntryKind getRankingKind() const {
return Source.getRankingKind();
}
/// Retrieve the declaration context associated with this conformance.
DeclContext *getDeclContext() const {
return Source.getDeclContext();
}
/// Retrieve the protocol to which this conformance entry refers.
ProtocolDecl *getProtocol() const;
/// Retrieve the conformance for this entry, if it has one.
ProtocolConformance *getConformance() const {
return Conformance.dyn_cast<ProtocolConformance *>();
}
/// Retrieve the conformance entry where the conformance was
/// declared.
const ConformanceEntry *getDeclaredConformance() const {
if (Source.getKind() == ConformanceEntryKind::Implied)
return Source.getImpliedSource()->getDeclaredConformance();
return this;
}
/// Retrieve the source location of the place where the
/// conformance was introduced, e.g., an explicit conformance or
/// the point at which a subclass inherits a conformance from its
/// superclass.
SourceLoc getDeclaredLoc() const {
if (Source.getKind() == ConformanceEntryKind::Implied)
return Source.getImpliedSource()->getDeclaredLoc();
return Loc;
}
SWIFT_DEBUG_DUMP;
void dump(raw_ostream &os, unsigned indent = 0) const;
};
/// The set of conformance entries for a given protocol.
typedef llvm::TinyPtrVector<ConformanceEntry *> ConformanceEntries;
/// The type of the internal conformance table.
typedef llvm::MapVector<ProtocolDecl *, ConformanceEntries> ConformanceTable;
/// The conformance table.
ConformanceTable Conformances;
typedef llvm::SmallVector<ProtocolDecl *, 2> ProtocolList;
/// List of all of the protocols to which a given context declares
/// conformance, both explicitly and implicitly.
llvm::MapVector<DeclContext *, SmallVector<ConformanceEntry *, 4>>
AllConformances;
/// The complete set of diagnostics about erroneously superseded
/// protocol conformances.
llvm::SmallDenseMap<DeclContext *, std::vector<ConformanceEntry *> >
AllSupersededDiagnostics;
/// Associates a conforming decl to its protocol conformance decls.
llvm::DenseMap<const ValueDecl *, llvm::TinyPtrVector<ValueDecl *>>
ConformingDeclMap;
/// Indicates whether we are visiting the superclass.
bool VisitingSuperclass = false;
/// Add a protocol.
bool addProtocol(ProtocolDecl *protocol, SourceLoc loc,
ConformanceSource source);
/// Add the protocols added by attached extension macros that are not
/// yet expanded.
void addMacroGeneratedProtocols(
NominalTypeDecl *nominal, ConformanceSource source);
/// Expand the implied conformances for the given DeclContext.
void expandImpliedConformances(NominalTypeDecl *nominal, DeclContext *dc);
/// A three-way ordering
enum class Ordering {
Before,
Equivalent,
After,
};
/// Determine whether the first conformance entry supersedes the
/// second when determining where to place the conformance.
///
/// \param diagnoseSuperseded When one entry is better than another,
/// whether to diagnose the problem as an error.
Ordering compareConformances(ConformanceEntry *lhs, ConformanceEntry *rhs,
bool &diagnoseSuperseded);
/// Resolve the set of conformances that will be generated for the
/// given protocol.
///
/// \returns true if any conformance entries were superseded by this
/// operation.
bool resolveConformances(ProtocolDecl *protocol);
/// Retrieve the declaration context that provides the
/// (non-inherited) conformance described by the given conformance
/// entry.
DeclContext *getConformingContext(NominalTypeDecl *nominal,
ConformanceEntry *entry);
/// Resolve the given conformance entry to an actual protocol conformance.
ProtocolConformance *getConformance(NominalTypeDecl *nominal,
ConformanceEntry *entry);
/// Enumerate each of the unhandled contexts (nominal type
/// declaration or extension) within the given stage.
///
/// \param stage The stage to process. Note that it is up to the
/// caller to ensure that prior stages have already been handled.
///
/// \param nominalFunc Function object to be invoked when the
/// nominal type declaration itself needs to be processed. It takes
/// the nominal type declaration and its result is ignored.
///
/// \param extensionFunc Function object to be invoked with a given
/// extension needs to be processed. It takes the extension as an
/// argument and its result is ignored.
template<typename NominalFunc, typename ExtensionFunc>
void forEachInStage(ConformanceStage stage,
NominalTypeDecl *nominal,
NominalFunc nominalFunc,
ExtensionFunc extensionFunc);
/// Inherit the conformances from the given superclass into the
/// given nominal type.
///
/// \param classDecl The class into which the conformances will be
/// inherited.
///
/// \param superclassDecl The superclass from which the conformances
/// will be inherited.
///
/// \param superclassExt If non-null, the superclass extension from
/// which conformances will be inherited. If null, the conformances
/// on the superclass declaration itself will be inherited.
void inheritConformances(ClassDecl *classDecl,
ClassDecl *superclassDecl,
ExtensionDecl *superclassExt);
/// Update a lookup table with conformances from newly-added extensions.
void updateLookupTable(NominalTypeDecl *nominal, ConformanceStage stage);
/// Register deserialized protocol conformances for the given (serialized)
/// declaration context.
void registerProtocolConformances(DeclContext *dc,
ArrayRef<ProtocolConformance *> conformances);
public:
/// Create a new conformance lookup table.
ConformanceLookupTable(ASTContext &ctx);
/// Destroy the conformance table.
void destroy();
/// Add a synthesized conformance to the lookup table.
void addSynthesizedConformance(NominalTypeDecl *nominal,
ProtocolDecl *protocol,
DeclContext *conformanceDC);
/// Register an externally-supplied protocol conformance.
void registerProtocolConformance(DeclContext *dc,
ProtocolConformance *conformance,
bool synthesized = false);
/// Look for conformances to the given protocol.
///
/// \param conformances Will be populated with the set of protocol
/// conformances found for this protocol and nominal type.
///
/// \returns true if any conformances were found.
bool lookupConformance(NominalTypeDecl *nominal,
ProtocolDecl *protocol,
SmallVectorImpl<ProtocolConformance *> &conformances);
/// Look for all of the conformances within the given declaration context.
void lookupConformances(NominalTypeDecl *nominal,
DeclContext *dc,
std::vector<ProtocolConformance *> *conformances,
SmallVectorImpl<ConformanceDiagnostic> *diagnostics);
/// Retrieve the complete set of protocols to which this nominal
/// type conforms (if the set contains a protocol, the same is true for any
/// inherited protocols).
///
/// \param sorted Whether to sort the protocols in canonical order.
void getAllProtocols(NominalTypeDecl *nominal,
SmallVectorImpl<ProtocolDecl *> &scratch, bool sorted);
/// Retrieve the complete set of protocol conformances for this
/// nominal type.
void getAllConformances(NominalTypeDecl *nominal,
bool sorted,
SmallVectorImpl<ProtocolConformance *> &scratch);
/// Retrieve the protocols that would be implicitly synthesized.
/// FIXME: This is a hack, because it's the wrong question to ask. It
/// skips over the possibility that there is an explicit conformance
/// somewhere.
void getImplicitProtocols(NominalTypeDecl *nominal,
SmallVectorImpl<ProtocolDecl *> &protocols);
/// Returns the protocol requirements that \c Member conforms to.
ArrayRef<ValueDecl *>
getSatisfiedProtocolRequirementsForMember(const ValueDecl *member,
NominalTypeDecl *nominal,
bool sorted);
SWIFT_DEBUG_DUMP;
void dump(raw_ostream &os) const;
/// Compare two protocol conformances to place them in some canonical order.
static int compareProtocolConformances(ProtocolConformance * const *lhsPtr,
ProtocolConformance * const *rhsPtr);
};
}
#endif /* SWIFT_AST_CONFORMANCE_LOOKUP_TABLE_H */