forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Substitution.cpp
166 lines (142 loc) · 6.25 KB
/
Substitution.cpp
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
//===--- Substitution.cpp - Type substitutions ----------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the Substitution class and operations on it.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/Substitution.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Module.h"
#include "swift/AST/Types.h"
#include "llvm/ADT/DenseMap.h"
using namespace swift;
bool Substitution::operator==(const Substitution &other) const {
// The archetypes may be missing, but we can compare them directly
// because archetypes are always canonical.
return Archetype == other.Archetype &&
Replacement->getCanonicalType() == other.Replacement->getCanonicalType() &&
Conformance.equals(other.Conformance);
}
static void
getSubstitutionMaps(GenericParamList *context,
ArrayRef<Substitution> subs,
TypeSubstitutionMap &typeMap,
ArchetypeConformanceMap &conformanceMap) {
for (auto arch : context->getAllNestedArchetypes()) {
auto sub = subs.front();
subs = subs.slice(1);
// Save the conformances from the substitution so that we can substitute
// them into substitutions that map between archetypes.
conformanceMap[arch] = sub.getConformances();
if (arch->isPrimary())
typeMap[arch] = sub.getReplacement();
}
assert(subs.empty() && "did not use all substitutions?!");
}
Substitution::Substitution(ArchetypeType *Archetype,
Type Replacement,
ArrayRef<ProtocolConformance*> Conformance)
: Archetype(Archetype), Replacement(Replacement), Conformance(Conformance)
{
// The replacement type must be materializable.
assert(Replacement->isMaterializable()
&& "cannot substitute with a non-materializable type");
assert(Archetype && "missing archetype in substitution");
// The conformance list must match the archetype conformances.
if (!Replacement->hasDependentProtocolConformances()) {
assert(Conformance.size() == Archetype->getConformsTo().size()
&& "substitution conformances don't match archetype");
}
}
Substitution Substitution::subst(Module *module,
GenericParamList *context,
ArrayRef<Substitution> subs) const {
TypeSubstitutionMap subMap;
ArchetypeConformanceMap conformanceMap;
getSubstitutionMaps(context, subs,
subMap, conformanceMap);
return subst(module, subs, subMap, conformanceMap);
}
Substitution Substitution::subst(Module *module,
ArrayRef<Substitution> subs,
TypeSubstitutionMap &subMap,
ArchetypeConformanceMap &conformanceMap) const {
// Substitute the replacement.
Type substReplacement = Replacement.subst(module, subMap, None);
assert(substReplacement && "substitution replacement failed");
if (substReplacement->isEqual(Replacement))
return *this;
bool conformancesChanged = false;
SmallVector<ProtocolConformance *, 4> substConformance;
substConformance.reserve(Conformance.size());
// When substituting a concrete type for an archetype, we need to fill in the
// conformances.
if (auto replacementArch = Replacement->getAs<ArchetypeType>()) {
if (!substReplacement->hasDependentProtocolConformances()) {
// Find the conformances mapped to the archetype.
auto found = conformanceMap.find(replacementArch);
assert(found != conformanceMap.end()
&& "no conformances for replaced archetype?!");
auto &foundConformances = found->second;
// If the substituted replacement archetype has no conformances,
// then there are no conformances to substitute.
if (foundConformances.empty())
return Substitution{Archetype, substReplacement, Conformance};
conformancesChanged = true;
// Get the conformances for the type that apply to the original
// substituted archetype.
for (auto proto : Archetype->getConformsTo()) {
for (auto c : foundConformances) {
if (c->getProtocol() == proto) {
substConformance.push_back(c);
goto found_conformance;
}
if (c->getProtocol()->inheritsFrom(proto)) {
substConformance.push_back(c->getInheritedConformance(proto));
goto found_conformance;
}
}
// FIXME: AnyObject conformances can be synthesized from
// thin air. Gross.
if (proto->isSpecificProtocol(KnownProtocolKind::AnyObject)) {
auto classDecl
= substReplacement->getClassOrBoundGenericClass();
SmallVector<ProtocolConformance *, 1> conformances;
classDecl->lookupConformance(classDecl->getParentModule(),
proto, conformances);
substConformance.push_back(conformances.front());
goto found_conformance;
}
assert(false && "did not find conformance for archetype requirement?!");
found_conformance:;
}
}
} else {
// If we substituted a concrete type for another, we need to substitute the
// conformance to apply to the new type.
for (auto c : Conformance) {
auto substC = c->subst(module, substReplacement, subs,
subMap, conformanceMap);
if (c != substC)
conformancesChanged = true;
substConformance.push_back(substC);
}
}
ArrayRef<ProtocolConformance *> substConformanceRef;
if (conformancesChanged)
substConformanceRef = module->getASTContext().AllocateCopy(substConformance);
else
substConformanceRef = Conformance;
assert(substReplacement->hasDependentProtocolConformances()
|| substConformanceRef.size() == Archetype->getConformsTo().size());
return Substitution{Archetype, substReplacement, substConformanceRef};
}