@@ -123,10 +123,6 @@ struct ArchetypeBuilder::Implementation {
123
123
// / archetypes.
124
124
llvm::MapVector<GenericTypeParamKey, PotentialArchetype*> PotentialArchetypes;
125
125
126
- // / A vector containing the same-type requirements introduced into the
127
- // / system.
128
- SmallVector<SameTypeRequirement, 4 > SameTypeRequirements;
129
-
130
126
// / The number of nested types that haven't yet been resolved to archetypes.
131
127
// / Once all requirements have been added, this will be zero in well-formed
132
128
// / code.
@@ -1131,8 +1127,6 @@ bool ArchetypeBuilder::addSameTypeRequirementBetweenArchetypes(
1131
1127
PotentialArchetype *T2,
1132
1128
RequirementSource Source)
1133
1129
{
1134
- auto OrigT1 = T1, OrigT2 = T2;
1135
-
1136
1130
// Operate on the representatives
1137
1131
T1 = T1->getRepresentative ();
1138
1132
T2 = T2->getRepresentative ();
@@ -1199,11 +1193,6 @@ bool ArchetypeBuilder::addSameTypeRequirementBetweenArchetypes(
1199
1193
for (auto equiv : T2->EquivalenceClass )
1200
1194
T1->EquivalenceClass .push_back (equiv);
1201
1195
1202
- if (!T1->wasRenamed () && !T2->wasRenamed ()) {
1203
- // Record this same-type requirement.
1204
- Impl->SameTypeRequirements .push_back ({ OrigT1, OrigT2 });
1205
- }
1206
-
1207
1196
// FIXME: superclass requirements!
1208
1197
1209
1198
// Add all of the protocol conformance requirements of T2 to T1.
@@ -1229,7 +1218,6 @@ bool ArchetypeBuilder::addSameTypeRequirementToConcrete(
1229
1218
Type Concrete,
1230
1219
RequirementSource Source) {
1231
1220
// Operate on the representative.
1232
- auto OrigT = T;
1233
1221
T = T->getRepresentative ();
1234
1222
1235
1223
assert (!T->ArchetypeOrConcreteType .getAsArchetype ()
@@ -1279,8 +1267,6 @@ bool ArchetypeBuilder::addSameTypeRequirementToConcrete(
1279
1267
T->ArchetypeOrConcreteType = NestedType::forConcreteType (Concrete);
1280
1268
T->SameTypeSource = Source;
1281
1269
1282
- Impl->SameTypeRequirements .push_back ({OrigT, Concrete});
1283
-
1284
1270
// Recursively resolve the associated types to their concrete types.
1285
1271
for (auto nested : T->getNestedTypes ()) {
1286
1272
AssociatedTypeDecl *assocType
@@ -1804,11 +1790,6 @@ ArchetypeBuilder::getArchetype(GenericTypeParamDecl *GenericParam) {
1804
1790
return known->second ->getType (*this ).getAsArchetype ();
1805
1791
}
1806
1792
1807
- ArrayRef<ArchetypeBuilder::SameTypeRequirement>
1808
- ArchetypeBuilder::getSameTypeRequirements () const {
1809
- return Impl->SameTypeRequirements ;
1810
- }
1811
-
1812
1793
template <typename F>
1813
1794
void ArchetypeBuilder::visitPotentialArchetypes (F f) {
1814
1795
// Stack containing all of the potential archetypes to visit.
@@ -1838,32 +1819,6 @@ void ArchetypeBuilder::visitPotentialArchetypes(F f) {
1838
1819
}
1839
1820
}
1840
1821
1841
- namespace {
1842
- // / \brief Function object that orders potential archetypes by name.
1843
- struct OrderPotentialArchetypeByName {
1844
- using PotentialArchetype = ArchetypeBuilder::PotentialArchetype;
1845
-
1846
- bool operator ()(std::pair<Identifier, PotentialArchetype *> X,
1847
- std::pair<Identifier, PotentialArchetype *> Y) const {
1848
- return X.first .str () < Y.second ->getName ().str ();
1849
- }
1850
-
1851
- bool operator ()(std::pair<Identifier, PotentialArchetype *> X,
1852
- Identifier Y) const {
1853
- return X.first .str () < Y.str ();
1854
- }
1855
-
1856
- bool operator ()(Identifier X,
1857
- std::pair<Identifier, PotentialArchetype *> Y) const {
1858
- return X.str () < Y.first .str ();
1859
- }
1860
-
1861
- bool operator ()(Identifier X, Identifier Y) const {
1862
- return X.str () < Y.str ();
1863
- }
1864
- };
1865
- }
1866
-
1867
1822
void ArchetypeBuilder::enumerateRequirements (llvm::function_ref<
1868
1823
void (RequirementKind kind,
1869
1824
PotentialArchetype *archetype,
@@ -2061,173 +2016,78 @@ Type ArchetypeBuilder::substDependentType(Type type) {
2061
2016
return substConcreteTypesForDependentTypes (*this , type);
2062
2017
}
2063
2018
2064
- // / Add the requirements for the given potential archetype and its nested
2065
- // / potential archetypes to the set of requirements.
2066
- static void
2067
- addRequirements (
2068
- ArchetypeBuilder &builder, Type type,
2069
- ArchetypeBuilder::PotentialArchetype *pa,
2070
- llvm::SmallPtrSet<ArchetypeBuilder::PotentialArchetype *, 16 > &knownPAs,
2071
- SmallVectorImpl<Requirement> &requirements) {
2072
-
2073
- auto &ctx = builder.getASTContext ();
2074
-
2075
- // If the potential archetype has been bound away to a concrete type,
2076
- // it needs no requirements.
2077
- if (pa->isConcreteType ())
2078
- return ;
2079
-
2080
- // Add a value witness marker.
2081
- requirements.push_back (Requirement (RequirementKind::WitnessMarker,
2082
- type, Type ()));
2083
-
2084
- // Add superclass requirement, if needed.
2085
- if (auto superclass = pa->getSuperclass ()) {
2086
- // FIXME: What if the superclass type involves a type parameter?
2087
- requirements.push_back (Requirement (RequirementKind::Superclass,
2088
- type, superclass));
2089
- }
2090
-
2091
- // Add conformance requirements.
2092
- SmallVector<ProtocolDecl *, 4 > protocols;
2093
- for (const auto &conforms : pa->getConformsTo ()) {
2094
- protocols.push_back (conforms.first );
2095
- }
2096
-
2097
- ProtocolType::canonicalizeProtocols (protocols);
2098
- for (auto proto : protocols) {
2099
- requirements.push_back (Requirement (RequirementKind::Conformance, type,
2100
- ProtocolType::get (proto, ctx)));
2101
- }
2102
- }
2103
-
2104
- static void
2105
- addNestedRequirements (
2106
- ArchetypeBuilder &builder,
2107
- ArchetypeBuilder::PotentialArchetype *pa,
2108
- llvm::SmallPtrSet<ArchetypeBuilder::PotentialArchetype *, 16 > &knownPAs,
2109
- SmallVectorImpl<Requirement> &requirements) {
2110
- using PotentialArchetype = ArchetypeBuilder::PotentialArchetype;
2111
-
2112
- // Collect the nested types, sorted by name.
2113
- // FIXME: Could collect these from the conformance requirements, above.
2114
- SmallVector<std::pair<Identifier, PotentialArchetype*>, 16 > nestedTypes;
2115
- for (const auto &nested : pa->getNestedTypes ()) {
2116
- // FIXME: Dropping requirements among different associated types of the
2117
- // same name.
2118
- // Skip type aliases, which are just shortcuts down the tree.
2119
- if (nested.second .front ()->getTypeAliasDecl ())
2120
- continue ;
2121
- nestedTypes.push_back (std::make_pair (nested.first , nested.second .front ()));
2122
- }
2123
- std::sort (nestedTypes.begin (), nestedTypes.end (),
2124
- OrderPotentialArchetypeByName ());
2125
-
2126
- // Add requirements for associated types.
2127
- for (const auto &nested : nestedTypes) {
2128
- auto rep = nested.second ->getRepresentative ();
2129
- if (knownPAs.insert (rep).second ) {
2130
- // Form the dependent type that refers to this archetype.
2131
- auto assocType = nested.second ->getResolvedAssociatedType ();
2132
- if (!assocType)
2133
- continue ; // FIXME: If we do this late enough, there will be no failure.
2134
-
2135
- // Skip nested types bound to concrete types.
2136
- if (rep->isConcreteType ())
2137
- continue ;
2019
+ // / Collect the set of requirements placed on the given generic parameters and
2020
+ // / their associated types.
2021
+ static void collectRequirements (ArchetypeBuilder &builder,
2022
+ ArrayRef<GenericTypeParamType *> params,
2023
+ SmallVectorImpl<Requirement> &requirements,
2024
+ bool buildingCanonicalManglingSignature) {
2025
+ builder.enumerateRequirements ([&](RequirementKind kind,
2026
+ ArchetypeBuilder::PotentialArchetype *archetype,
2027
+ llvm::PointerUnion<Type, ArchetypeBuilder::PotentialArchetype *> type,
2028
+ RequirementSource source) {
2029
+ // Filter out redundant requirements.
2030
+ switch (source.getKind ()) {
2031
+ case RequirementSource::Explicit:
2032
+ case RequirementSource::Inferred:
2033
+ // The requirement was explicit and required, keep it.
2034
+ break ;
2138
2035
2139
- auto nestedType =
2140
- rep->getDependentType (builder, /* allowUnresolved*/ false );
2036
+ case RequirementSource::Protocol:
2037
+ // We drop these requirements in canonical mangling signatures.
2038
+ if (buildingCanonicalManglingSignature)
2039
+ return ;
2040
+ break ;
2141
2041
2142
- // Skip unresolved nested types.
2143
- if (nestedType-> is <ErrorType>())
2144
- continue ;
2042
+ case RequirementSource::Redundant:
2043
+ // The requirement was redundant, drop it.
2044
+ return ;
2145
2045
2146
- addRequirements (builder, nestedType, rep, knownPAs, requirements);
2147
- addNestedRequirements (builder, rep, knownPAs, requirements);
2046
+ case RequirementSource::OuterScope:
2047
+ assert (!buildingCanonicalManglingSignature &&
2048
+ " mangling signature shouldn't have an outer scope" );
2049
+ break ;
2148
2050
}
2149
- }
2150
- }
2151
2051
2052
+ auto depTy = archetype->getDependentType (builder, false );
2152
2053
2153
- // / Collect the set of requirements placed on the given generic parameters and
2154
- // / their associated types.
2155
- static void collectRequirements (ArchetypeBuilder &builder,
2156
- ArrayRef<GenericTypeParamType *> params,
2157
- SmallVectorImpl<Requirement> &requirements) {
2158
- typedef ArchetypeBuilder::PotentialArchetype PotentialArchetype;
2159
-
2160
- // Find the "primary" potential archetypes, from which we'll collect all
2161
- // of the requirements.
2162
- llvm::SmallPtrSet<PotentialArchetype *, 16 > knownPAs;
2163
- llvm::SmallVector<GenericTypeParamType *, 8 > primary;
2164
- for (auto param : params) {
2165
- auto pa = builder.resolveArchetype (param);
2166
- assert (pa && " Missing potential archetype for generic parameter" );
2167
-
2168
- // We only care about the representative.
2169
- pa = pa->getRepresentative ();
2170
-
2171
- if (knownPAs.insert (pa).second )
2172
- primary.push_back (param);
2173
- }
2054
+ if (depTy->is <ErrorType>())
2055
+ return ;
2174
2056
2175
- // Add all of the conformance and superclass requirements placed on the given
2176
- // generic parameters and their associated types.
2177
- unsigned primaryIdx = 0 , numPrimary = primary.size ();
2178
- while (primaryIdx < numPrimary) {
2179
- unsigned depth = primary[primaryIdx]->getDepth ();
2180
-
2181
- // For each of the primary potential archetypes, add the requirements.
2182
- // Stop when we hit a parameter at a different depth.
2183
- // FIXME: This algorithm falls out from the way the "all archetypes" lists
2184
- // are structured. Once those lists no longer exist or are no longer
2185
- // "the truth", we can simplify this algorithm considerably.
2186
- unsigned lastPrimaryIdx = primaryIdx;
2187
- for (unsigned idx = primaryIdx;
2188
- idx < numPrimary && primary[idx]->getDepth () == depth;
2189
- ++idx, ++lastPrimaryIdx) {
2190
- auto param = primary[idx];
2191
- auto pa = builder.resolveArchetype (param)->getRepresentative ();
2192
-
2193
- // Add other requirements.
2194
- addRequirements (builder, param, pa, knownPAs, requirements);
2057
+ if (kind == RequirementKind::WitnessMarker) {
2058
+ requirements.push_back (Requirement (kind, depTy, Type ()));
2059
+ return ;
2195
2060
}
2196
2061
2197
- // For each of the primary potential archetypes, add the nested requirements.
2198
- for (unsigned idx = primaryIdx; idx < lastPrimaryIdx; ++idx) {
2199
- auto param = primary[idx];
2200
- auto pa = builder.resolveArchetype (param)->getRepresentative ();
2201
- addNestedRequirements (builder, pa, knownPAs, requirements);
2062
+ Type repTy;
2063
+ if (auto concreteTy = type.dyn_cast <Type>()) {
2064
+ // Maybe we were equated to a concrete type...
2065
+ repTy = concreteTy;
2066
+ } else {
2067
+ // ...or to a dependent type.
2068
+ repTy = type.get <ArchetypeBuilder::PotentialArchetype *>()
2069
+ ->getDependentType (builder, false );
2202
2070
}
2203
2071
2204
- primaryIdx = lastPrimaryIdx;
2205
- }
2206
-
2207
-
2208
- // Add all of the same-type requirements.
2209
- for (auto req : builder.getSameTypeRequirements ()) {
2210
- auto firstType = req.first ->getDependentType (builder, false );
2211
- Type secondType;
2212
- if (auto concrete = req.second .dyn_cast <Type>())
2213
- secondType = concrete;
2214
- else if (auto secondPA = req.second .dyn_cast <PotentialArchetype*>())
2215
- secondType = secondPA->getDependentType (builder, false );
2072
+ if (repTy->is <ErrorType>())
2073
+ return ;
2216
2074
2217
- if (firstType->is <ErrorType>() || secondType->is <ErrorType>() ||
2218
- firstType->isEqual (secondType))
2219
- continue ;
2075
+ if (buildingCanonicalManglingSignature) {
2076
+ depTy = depTy->getCanonicalType ();
2077
+ repTy = repTy->getCanonicalType ();
2078
+ }
2220
2079
2221
- requirements.push_back (Requirement (RequirementKind::SameType,
2222
- firstType, secondType));
2223
- }
2080
+ requirements.push_back (Requirement (kind, depTy, repTy));
2081
+ });
2224
2082
}
2225
2083
2226
2084
GenericSignature *ArchetypeBuilder::getGenericSignature (
2227
- ArrayRef<GenericTypeParamType *> genericParamTypes) {
2085
+ ArrayRef<GenericTypeParamType *> genericParamTypes,
2086
+ bool buildingCanonicalManglingSignature) {
2228
2087
// Collect the requirements placed on the generic parameter types.
2229
2088
SmallVector<Requirement, 4 > requirements;
2230
- collectRequirements (*this , genericParamTypes, requirements);
2089
+ collectRequirements (*this , genericParamTypes, requirements,
2090
+ buildingCanonicalManglingSignature);
2231
2091
2232
2092
auto sig = GenericSignature::get (genericParamTypes, requirements);
2233
2093
return sig;
0 commit comments