@@ -2144,23 +2144,18 @@ TypeExpr *TypeExpr::createForMemberDecl(DeclNameLoc ParentNameLoc,
2144
2144
assert (ParentNameLoc.isValid ());
2145
2145
assert (NameLoc.isValid ());
2146
2146
2147
- // Create a new list of components.
2148
- SmallVector<ComponentIdentTypeRepr *, 2 > Components;
2149
-
2150
- // The first component is the parent type.
2147
+ // The base component is the parent type.
2151
2148
auto *ParentComp = new (C) SimpleIdentTypeRepr (ParentNameLoc,
2152
2149
Parent->createNameRef ());
2153
2150
ParentComp->setValue (Parent, nullptr );
2154
- Components.push_back (ParentComp);
2155
2151
2156
- // The second component is the member we just found.
2157
- auto *NewComp = new (C) SimpleIdentTypeRepr (NameLoc,
2158
- Decl->createNameRef ());
2159
- NewComp->setValue (Decl, nullptr );
2160
- Components.push_back (NewComp);
2152
+ // The member component is the member we just found.
2153
+ auto *MemberComp =
2154
+ new (C) SimpleIdentTypeRepr (NameLoc, Decl->createNameRef ());
2155
+ MemberComp->setValue (Decl, nullptr );
2161
2156
2162
- auto *NewTypeRepr = CompoundIdentTypeRepr::create (C, Components );
2163
- return new (C) TypeExpr (NewTypeRepr );
2157
+ auto *TR = CompoundIdentTypeRepr::create (C, ParentComp, {MemberComp} );
2158
+ return new (C) TypeExpr (TR );
2164
2159
}
2165
2160
2166
2161
TypeExpr *TypeExpr::createForMemberDecl (IdentTypeRepr *ParentTR,
@@ -2170,43 +2165,32 @@ TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
2170
2165
2171
2166
// Create a new list of components.
2172
2167
SmallVector<ComponentIdentTypeRepr *, 2 > Components;
2173
- if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2174
- Components.push_back (Comp);
2175
- } else {
2176
- auto OldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents ();
2177
- Components.append (OldComps.begin (), OldComps.end ());
2168
+ if (auto *Compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2169
+ auto MemberComps = Compound->getMemberComponents ();
2170
+ Components.append (MemberComps.begin (), MemberComps.end ());
2178
2171
}
2179
2172
2180
- assert (!Components.empty ());
2181
-
2182
2173
// Add a new component for the member we just found.
2183
2174
auto *NewComp = new (C) SimpleIdentTypeRepr (NameLoc, Decl->createNameRef ());
2184
2175
NewComp->setValue (Decl, nullptr );
2185
2176
Components.push_back (NewComp);
2186
2177
2187
- auto *NewTypeRepr = CompoundIdentTypeRepr::create (C, Components);
2188
- return new (C) TypeExpr (NewTypeRepr);
2178
+ auto *TR = CompoundIdentTypeRepr::create (C, ParentTR->getBaseComponent (),
2179
+ Components);
2180
+ return new (C) TypeExpr (TR);
2189
2181
}
2190
2182
2191
2183
TypeExpr *TypeExpr::createForSpecializedDecl (IdentTypeRepr *ParentTR,
2192
2184
ArrayRef<TypeRepr*> Args,
2193
2185
SourceRange AngleLocs,
2194
2186
ASTContext &C) {
2195
- // Create a new list of components.
2196
- SmallVector<ComponentIdentTypeRepr *, 2 > components;
2197
- if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2198
- components.push_back (comp);
2199
- } else {
2200
- auto oldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents ();
2201
- components.append (oldComps.begin (), oldComps.end ());
2202
- }
2187
+ auto *lastComp = ParentTR->getLastComponent ();
2203
2188
2204
- auto *last = components. back ();
2205
- components. pop_back () ;
2189
+ if (!isa<SimpleIdentTypeRepr>(lastComp) || !lastComp-> getBoundDecl ())
2190
+ return nullptr ;
2206
2191
2207
- if (isa<SimpleIdentTypeRepr>(last) &&
2208
- last->getBoundDecl ()) {
2209
- if (isa<TypeAliasDecl>(last->getBoundDecl ())) {
2192
+ if (isa<TypeAliasDecl>(lastComp->getBoundDecl ())) {
2193
+ if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2210
2194
// If any of our parent types are unbound, bail out and let
2211
2195
// the constraint solver can infer generic parameters for them.
2212
2196
//
@@ -2219,33 +2203,50 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
2219
2203
//
2220
2204
// FIXME: Once we can model generic typealiases properly, rip
2221
2205
// this out.
2222
- for (auto *component : components) {
2223
- auto *componentDecl = dyn_cast_or_null<GenericTypeDecl>(
2224
- component->getBoundDecl ());
2206
+ auto isUnboundGenericComponent = [](ComponentIdentTypeRepr *TR) -> bool {
2207
+ if (isa<SimpleIdentTypeRepr>(TR)) {
2208
+ auto *decl = dyn_cast_or_null<GenericTypeDecl>(TR->getBoundDecl ());
2209
+ if (decl && decl->isGeneric ())
2210
+ return true ;
2211
+ }
2225
2212
2226
- if (isa<SimpleIdentTypeRepr>(component) &&
2227
- componentDecl &&
2228
- componentDecl->isGeneric ())
2213
+ return false ;
2214
+ };
2215
+
2216
+ for (auto *comp : compound->getMemberComponents ().drop_back ()) {
2217
+ if (isUnboundGenericComponent (comp))
2229
2218
return nullptr ;
2230
2219
}
2231
- }
2232
2220
2233
- auto *genericComp = GenericIdentTypeRepr::create (C,
2234
- last->getNameLoc (), last->getNameRef (),
2235
- Args, AngleLocs);
2236
- genericComp->setValue (last->getBoundDecl (), last->getDeclContext ());
2237
- components.push_back (genericComp);
2238
-
2239
- TypeRepr *genericRepr = nullptr ;
2240
- if (components.size () == 1 ) {
2241
- genericRepr = components.front ();
2242
- } else {
2243
- genericRepr = CompoundIdentTypeRepr::create (C, components);
2221
+ if (auto *identBase =
2222
+ dyn_cast<ComponentIdentTypeRepr>(compound->getBaseComponent ())) {
2223
+ if (isUnboundGenericComponent (identBase))
2224
+ return nullptr ;
2225
+ }
2244
2226
}
2245
- return new (C) TypeExpr (genericRepr);
2246
2227
}
2247
2228
2248
- return nullptr ;
2229
+ auto *genericComp = GenericIdentTypeRepr::create (
2230
+ C, lastComp->getNameLoc (), lastComp->getNameRef (), Args, AngleLocs);
2231
+ genericComp->setValue (lastComp->getBoundDecl (), lastComp->getDeclContext ());
2232
+
2233
+ TypeRepr *TR = nullptr ;
2234
+ if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2235
+ auto oldMemberComps = compound->getMemberComponents ().drop_back ();
2236
+
2237
+ // Create a new list of member components, replacing the last one with the
2238
+ // new specialized one.
2239
+ SmallVector<ComponentIdentTypeRepr *, 2 > newMemberComps;
2240
+ newMemberComps.append (oldMemberComps.begin (), oldMemberComps.end ());
2241
+ newMemberComps.push_back (genericComp);
2242
+
2243
+ TR = CompoundIdentTypeRepr::create (C, compound->getBaseComponent (),
2244
+ newMemberComps);
2245
+ } else {
2246
+ TR = genericComp;
2247
+ }
2248
+
2249
+ return new (C) TypeExpr (TR);
2249
2250
}
2250
2251
2251
2252
// Create an implicit TypeExpr, with location information even though it
0 commit comments