Skip to content

Commit 36ef7eb

Browse files
committed
Sema: Add ProtocolDecl::DefaultWitnesses
NFC for now, since the code that uses this is not checked in yet.
1 parent 32bcbae commit 36ef7eb

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

include/swift/AST/Decl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/TypeAlignments.h"
2828
#include "swift/Basic/OptionalEnum.h"
2929
#include "swift/Basic/Range.h"
30+
#include "llvm/ADT/DenseMap.h"
3031
#include "llvm/ADT/DenseSet.h"
3132
#include "llvm/ADT/SmallPtrSet.h"
3233
#include "llvm/Support/TrailingObjects.h"
@@ -3350,6 +3351,8 @@ class ProtocolDecl : public NominalTypeDecl {
33503351

33513352
ArrayRef<ProtocolDecl *> InheritedProtocols;
33523353

3354+
llvm::DenseMap<ValueDecl *, ConcreteDeclRef> DefaultWitnesses;
3355+
33533356
/// True if the protocol has requirements that cannot be satisfied (e.g.
33543357
/// because they could not be imported from Objective-C).
33553358
unsigned HasMissingRequirements : 1;
@@ -3494,6 +3497,23 @@ class ProtocolDecl : public NominalTypeDecl {
34943497
HasMissingRequirements = newValue;
34953498
}
34963499

3500+
/// Returns the default witness for a requirement, or nullptr if there is
3501+
/// no default.
3502+
ConcreteDeclRef getDefaultWitness(ValueDecl *requirement) {
3503+
auto found = DefaultWitnesses.find(requirement);
3504+
if (found == DefaultWitnesses.end())
3505+
return nullptr;
3506+
return found->second;
3507+
}
3508+
3509+
/// Record the default witness for a requirement.
3510+
void setDefaultWitness(ValueDecl *requirement, ConcreteDeclRef witness) {
3511+
assert(witness);
3512+
auto pair = DefaultWitnesses.insert(std::make_pair(requirement, witness));
3513+
assert(pair.second && "Already have a default witness!");
3514+
(void) pair;
3515+
}
3516+
34973517
/// Set the list of inherited protocols.
34983518
void setInheritedProtocols(ArrayRef<ProtocolDecl *> protocols) {
34993519
assert(!InheritedProtocolsSet && "protocols already set");

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ namespace {
350350
/// \brief Associated type substitutions needed to match the witness.
351351
SmallVector<Substitution, 2> WitnessSubstitutions;
352352

353+
ConcreteDeclRef getWitness(ASTContext &ctx) const {
354+
if (WitnessSubstitutions.empty())
355+
return Witness;
356+
return ConcreteDeclRef(ctx, Witness, WitnessSubstitutions);
357+
}
358+
353359
/// Classify the provided optionality issues for use in diagnostics.
354360
/// FIXME: Enumify this
355361
unsigned classifyOptionalityIssues(ValueDecl *requirement) const {
@@ -1849,12 +1855,7 @@ void ConformanceChecker::recordWitness(ValueDecl *requirement,
18491855
}
18501856

18511857
// Record this witness in the conformance.
1852-
ConcreteDeclRef witness;
1853-
if (match.WitnessSubstitutions.empty())
1854-
witness = match.Witness;
1855-
else
1856-
witness = ConcreteDeclRef(TC.Context, match.Witness,
1857-
match.WitnessSubstitutions);
1858+
ConcreteDeclRef witness = match.getWitness(TC.Context);
18581859
Conformance->setWitness(requirement, witness);
18591860

18601861
// Synthesize accessors for the protocol witness table to use.

0 commit comments

Comments
 (0)