@@ -2210,23 +2210,18 @@ TypeExpr *TypeExpr::createForMemberDecl(DeclNameLoc ParentNameLoc,
2210
2210
assert (ParentNameLoc.isValid ());
2211
2211
assert (NameLoc.isValid ());
2212
2212
2213
- // Create a new list of components.
2214
- SmallVector<ComponentIdentTypeRepr *, 2 > Components;
2215
-
2216
- // The first component is the parent type.
2213
+ // The base component is the parent type.
2217
2214
auto *ParentComp = new (C) SimpleIdentTypeRepr (ParentNameLoc,
2218
2215
Parent->createNameRef ());
2219
2216
ParentComp->setValue (Parent, nullptr );
2220
- Components.push_back (ParentComp);
2221
2217
2222
- // The second component is the member we just found.
2223
- auto *NewComp = new (C) SimpleIdentTypeRepr (NameLoc,
2224
- Decl->createNameRef ());
2225
- NewComp->setValue (Decl, nullptr );
2226
- Components.push_back (NewComp);
2218
+ // The member component is the member we just found.
2219
+ auto *MemberComp =
2220
+ new (C) SimpleIdentTypeRepr (NameLoc, Decl->createNameRef ());
2221
+ MemberComp->setValue (Decl, nullptr );
2227
2222
2228
- auto *NewTypeRepr = CompoundIdentTypeRepr::create (C, Components );
2229
- return new (C) TypeExpr (NewTypeRepr );
2223
+ auto *TR = CompoundIdentTypeRepr::create (C, ParentComp, {MemberComp} );
2224
+ return new (C) TypeExpr (TR );
2230
2225
}
2231
2226
2232
2227
TypeExpr *TypeExpr::createForMemberDecl (IdentTypeRepr *ParentTR,
@@ -2236,43 +2231,32 @@ TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
2236
2231
2237
2232
// Create a new list of components.
2238
2233
SmallVector<ComponentIdentTypeRepr *, 2 > Components;
2239
- if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2240
- Components.push_back (Comp);
2241
- } else {
2242
- auto OldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents ();
2243
- Components.append (OldComps.begin (), OldComps.end ());
2234
+ if (auto *Compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2235
+ auto MemberComps = Compound->getMemberComponents ();
2236
+ Components.append (MemberComps.begin (), MemberComps.end ());
2244
2237
}
2245
2238
2246
- assert (!Components.empty ());
2247
-
2248
2239
// Add a new component for the member we just found.
2249
2240
auto *NewComp = new (C) SimpleIdentTypeRepr (NameLoc, Decl->createNameRef ());
2250
2241
NewComp->setValue (Decl, nullptr );
2251
2242
Components.push_back (NewComp);
2252
2243
2253
- auto *NewTypeRepr = CompoundIdentTypeRepr::create (C, Components);
2254
- return new (C) TypeExpr (NewTypeRepr);
2244
+ auto *TR = CompoundIdentTypeRepr::create (C, ParentTR->getBaseComponent (),
2245
+ Components);
2246
+ return new (C) TypeExpr (TR);
2255
2247
}
2256
2248
2257
2249
TypeExpr *TypeExpr::createForSpecializedDecl (IdentTypeRepr *ParentTR,
2258
2250
ArrayRef<TypeRepr*> Args,
2259
2251
SourceRange AngleLocs,
2260
2252
ASTContext &C) {
2261
- // Create a new list of components.
2262
- SmallVector<ComponentIdentTypeRepr *, 2 > components;
2263
- if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2264
- components.push_back (comp);
2265
- } else {
2266
- auto oldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents ();
2267
- components.append (oldComps.begin (), oldComps.end ());
2268
- }
2253
+ auto *lastComp = ParentTR->getLastComponent ();
2269
2254
2270
- auto *last = components. back ();
2271
- components. pop_back () ;
2255
+ if (!isa<SimpleIdentTypeRepr>(lastComp) || !lastComp-> getBoundDecl ())
2256
+ return nullptr ;
2272
2257
2273
- if (isa<SimpleIdentTypeRepr>(last) &&
2274
- last->getBoundDecl ()) {
2275
- if (isa<TypeAliasDecl>(last->getBoundDecl ())) {
2258
+ if (isa<TypeAliasDecl>(lastComp->getBoundDecl ())) {
2259
+ if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2276
2260
// If any of our parent types are unbound, bail out and let
2277
2261
// the constraint solver can infer generic parameters for them.
2278
2262
//
@@ -2285,33 +2269,50 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
2285
2269
//
2286
2270
// FIXME: Once we can model generic typealiases properly, rip
2287
2271
// this out.
2288
- for (auto *component : components) {
2289
- auto *componentDecl = dyn_cast_or_null<GenericTypeDecl>(
2290
- component->getBoundDecl ());
2272
+ auto isUnboundGenericComponent = [](ComponentIdentTypeRepr *TR) -> bool {
2273
+ if (isa<SimpleIdentTypeRepr>(TR)) {
2274
+ auto *decl = dyn_cast_or_null<GenericTypeDecl>(TR->getBoundDecl ());
2275
+ if (decl && decl->isGeneric ())
2276
+ return true ;
2277
+ }
2291
2278
2292
- if (isa<SimpleIdentTypeRepr>(component) &&
2293
- componentDecl &&
2294
- componentDecl->isGeneric ())
2279
+ return false ;
2280
+ };
2281
+
2282
+ for (auto *comp : compound->getMemberComponents ().drop_back ()) {
2283
+ if (isUnboundGenericComponent (comp))
2295
2284
return nullptr ;
2296
2285
}
2297
- }
2298
2286
2299
- auto *genericComp = GenericIdentTypeRepr::create (C,
2300
- last->getNameLoc (), last->getNameRef (),
2301
- Args, AngleLocs);
2302
- genericComp->setValue (last->getBoundDecl (), last->getDeclContext ());
2303
- components.push_back (genericComp);
2304
-
2305
- TypeRepr *genericRepr = nullptr ;
2306
- if (components.size () == 1 ) {
2307
- genericRepr = components.front ();
2308
- } else {
2309
- genericRepr = CompoundIdentTypeRepr::create (C, components);
2287
+ if (auto *identBase =
2288
+ dyn_cast<ComponentIdentTypeRepr>(compound->getBaseComponent ())) {
2289
+ if (isUnboundGenericComponent (identBase))
2290
+ return nullptr ;
2291
+ }
2310
2292
}
2311
- return new (C) TypeExpr (genericRepr);
2312
2293
}
2313
2294
2314
- return nullptr ;
2295
+ auto *genericComp = GenericIdentTypeRepr::create (
2296
+ C, lastComp->getNameLoc (), lastComp->getNameRef (), Args, AngleLocs);
2297
+ genericComp->setValue (lastComp->getBoundDecl (), lastComp->getDeclContext ());
2298
+
2299
+ TypeRepr *TR = nullptr ;
2300
+ if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2301
+ auto oldMemberComps = compound->getMemberComponents ().drop_back ();
2302
+
2303
+ // Create a new list of member components, replacing the last one with the
2304
+ // new specialized one.
2305
+ SmallVector<ComponentIdentTypeRepr *, 2 > newMemberComps;
2306
+ newMemberComps.append (oldMemberComps.begin (), oldMemberComps.end ());
2307
+ newMemberComps.push_back (genericComp);
2308
+
2309
+ TR = CompoundIdentTypeRepr::create (C, compound->getBaseComponent (),
2310
+ newMemberComps);
2311
+ } else {
2312
+ TR = genericComp;
2313
+ }
2314
+
2315
+ return new (C) TypeExpr (TR);
2315
2316
}
2316
2317
2317
2318
// Create an implicit TypeExpr, with location information even though it
0 commit comments