Skip to content

Commit 8bea09d

Browse files
committed
[NFC] AST: Refactor for recursive MemberTypeRepr representation
1 parent d9777a0 commit 8bea09d

File tree

6 files changed

+100
-110
lines changed

6 files changed

+100
-110
lines changed

include/swift/AST/Attr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ASTContext;
5454
struct PrintOptions;
5555
class CustomAttr;
5656
class Decl;
57+
class DeclRefTypeRepr;
5758
class AbstractFunctionDecl;
5859
class FuncDecl;
5960
class ClassDecl;
@@ -1766,7 +1767,7 @@ class CustomAttr final : public DeclAttribute {
17661767
///
17671768
/// For an identifier type repr, return a pair of `nullptr` and the
17681769
/// identifier.
1769-
std::pair<IdentTypeRepr *, IdentTypeRepr *> destructureMacroRef();
1770+
std::pair<IdentTypeRepr *, DeclRefTypeRepr *> destructureMacroRef();
17701771

17711772
/// Whether the attribute has any arguments.
17721773
bool hasArgs() const { return argList != nullptr; }

include/swift/AST/TypeRepr.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,19 @@ class DeclRefTypeRepr : public TypeRepr {
356356
/// is a \c MemberTypeRepr, and the method returns its last member component.
357357
IdentTypeRepr *getLastComponent();
358358

359+
DeclNameLoc getNameLoc() const;
360+
DeclNameRef getNameRef() const;
361+
359362
/// Returns whether this instance has been bound to a type declaration. This
360363
/// happens during type resolution.
361364
bool isBound() const;
362365

363366
/// Returns the type declaration this instance has been bound to.
364367
TypeDecl *getBoundDecl() const;
365368

366-
/// The identifier that describes the last component.
367-
DeclNameRef getNameRef() const;
369+
DeclContext *getDeclContext() const;
370+
371+
void setValue(TypeDecl *TD, DeclContext *DC);
368372

369373
/// Returns whether this instance has a generic argument list. That is, either
370374
/// a valid angle bracket source range, or a positive number of generic
@@ -373,12 +377,19 @@ class DeclRefTypeRepr : public TypeRepr {
373377

374378
ArrayRef<TypeRepr *> getGenericArgs() const;
375379

380+
SourceRange getAngleBrackets() const;
381+
376382
static bool classof(const TypeRepr *T) {
377383
return T->getKind() == TypeReprKind::SimpleIdent ||
378384
T->getKind() == TypeReprKind::GenericIdent ||
379385
T->getKind() == TypeReprKind::Member;
380386
}
381387
static bool classof(const DeclRefTypeRepr *T) { return true; }
388+
389+
protected:
390+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
391+
392+
friend class TypeRepr;
382393
};
383394

384395
/// An identifier type with an optional set of generic arguments.
@@ -435,9 +446,8 @@ class IdentTypeRepr : public DeclRefTypeRepr {
435446
static bool classof(const IdentTypeRepr *T) { return true; }
436447

437448
protected:
438-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
439-
440449
SourceLoc getLocImpl() const { return Loc.getBaseNameLoc(); }
450+
441451
friend class TypeRepr;
442452
};
443453

@@ -583,7 +593,6 @@ class MemberTypeRepr final : public DeclRefTypeRepr,
583593
SourceLoc getEndLocImpl() const { return getLastComponent()->getEndLoc(); }
584594
SourceLoc getLocImpl() const { return getLastComponent()->getLoc(); }
585595

586-
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
587596
friend class TypeRepr;
588597
};
589598

lib/AST/Attr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,18 +2737,18 @@ CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
27372737
CustomAttr(atLoc, range, type, initContext, argList, implicit);
27382738
}
27392739

2740-
std::pair<IdentTypeRepr *, IdentTypeRepr *> CustomAttr::destructureMacroRef() {
2740+
std::pair<IdentTypeRepr *, DeclRefTypeRepr *>
2741+
CustomAttr::destructureMacroRef() {
27412742
TypeRepr *typeRepr = getTypeRepr();
27422743
if (!typeRepr)
27432744
return {nullptr, nullptr};
27442745
if (auto *identType = dyn_cast<IdentTypeRepr>(typeRepr))
27452746
return {nullptr, identType};
2746-
if (auto *memType = dyn_cast<MemberTypeRepr>(typeRepr))
2747-
if (auto *base = dyn_cast<IdentTypeRepr>(memType->getRoot()))
2748-
if (memType->getMemberComponents().size() == 1)
2749-
if (auto first =
2750-
dyn_cast<IdentTypeRepr>(memType->getMemberComponents().front()))
2751-
return {base, first};
2747+
if (auto *memType = dyn_cast<MemberTypeRepr>(typeRepr)) {
2748+
if (auto *base = dyn_cast<SimpleIdentTypeRepr>(memType->getBase())) {
2749+
return {base, memType};
2750+
}
2751+
}
27522752
return {nullptr, nullptr};
27532753
}
27542754

lib/AST/Expr.cpp

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,57 +2236,47 @@ TypeExpr *TypeExpr::createForMemberDecl(DeclNameLoc ParentNameLoc,
22362236
assert(ParentNameLoc.isValid());
22372237
assert(NameLoc.isValid());
22382238

2239-
// The base component is the parent type.
2240-
auto *ParentComp = new (C) SimpleIdentTypeRepr(ParentNameLoc,
2241-
Parent->createNameRef());
2242-
ParentComp->setValue(Parent, nullptr);
2239+
// The base is the parent type.
2240+
auto *BaseTR =
2241+
new (C) SimpleIdentTypeRepr(ParentNameLoc, Parent->createNameRef());
2242+
BaseTR->setValue(Parent, nullptr);
22432243

2244-
// The member component is the member we just found.
2245-
auto *MemberComp =
2246-
new (C) SimpleIdentTypeRepr(NameLoc, Decl->createNameRef());
2247-
MemberComp->setValue(Decl, nullptr);
2244+
auto *MemberTR =
2245+
MemberTypeRepr::create(C, BaseTR, NameLoc, Decl->createNameRef());
2246+
MemberTR->setValue(Decl, nullptr);
22482247

2249-
auto *TR = MemberTypeRepr::create(C, ParentComp, {MemberComp});
2250-
return new (C) TypeExpr(TR);
2248+
return new (C) TypeExpr(MemberTR);
22512249
}
22522250

22532251
TypeExpr *TypeExpr::createForMemberDecl(TypeRepr *ParentTR, DeclNameLoc NameLoc,
22542252
TypeDecl *Decl) {
22552253
ASTContext &C = Decl->getASTContext();
22562254

2257-
// Add a new component for the member we just found.
2258-
auto *NewComp = new (C) SimpleIdentTypeRepr(NameLoc, Decl->createNameRef());
2259-
NewComp->setValue(Decl, nullptr);
2255+
auto *MemberTR =
2256+
MemberTypeRepr::create(C, ParentTR, NameLoc, Decl->createNameRef());
2257+
MemberTR->setValue(Decl, nullptr);
22602258

2261-
TypeRepr *TR = nullptr;
2262-
if (auto *DeclRefTR = dyn_cast<DeclRefTypeRepr>(ParentTR)) {
2263-
// Create a new list of components.
2264-
SmallVector<IdentTypeRepr *, 4> Components;
2265-
if (auto *MemberTR = dyn_cast<MemberTypeRepr>(ParentTR)) {
2266-
auto MemberComps = MemberTR->getMemberComponents();
2267-
Components.append(MemberComps.begin(), MemberComps.end());
2268-
}
2269-
2270-
Components.push_back(NewComp);
2271-
TR = MemberTypeRepr::create(C, DeclRefTR->getRoot(), Components);
2272-
} else {
2273-
TR = MemberTypeRepr::create(C, ParentTR, NewComp);
2274-
}
2275-
2276-
return new (C) TypeExpr(TR);
2259+
return new (C) TypeExpr(MemberTR);
22772260
}
22782261

22792262
TypeExpr *TypeExpr::createForSpecializedDecl(DeclRefTypeRepr *ParentTR,
22802263
ArrayRef<TypeRepr *> Args,
22812264
SourceRange AngleLocs,
22822265
ASTContext &C) {
2283-
auto *lastComp = ParentTR->getLastComponent();
2266+
DeclRefTypeRepr *specializedTR = nullptr;
22842267

2285-
if (!isa<SimpleIdentTypeRepr>(lastComp) || !lastComp->getBoundDecl())
2268+
auto *boundDecl = ParentTR->getBoundDecl();
2269+
if (!boundDecl || ParentTR->hasGenericArgList()) {
22862270
return nullptr;
2271+
}
22872272

2288-
if (isa<TypeAliasDecl>(lastComp->getBoundDecl())) {
2289-
if (auto *memberTR = dyn_cast<MemberTypeRepr>(ParentTR)) {
2273+
if (isa<IdentTypeRepr>(ParentTR)) {
2274+
specializedTR = GenericIdentTypeRepr::create(
2275+
C, ParentTR->getNameLoc(), ParentTR->getNameRef(), Args, AngleLocs);
2276+
specializedTR->setValue(boundDecl, ParentTR->getDeclContext());
2277+
} else {
2278+
auto *const memberTR = cast<MemberTypeRepr>(ParentTR);
2279+
if (isa<TypeAliasDecl>(boundDecl)) {
22902280
// If any of our parent types are unbound, bail out and let
22912281
// the constraint solver can infer generic parameters for them.
22922282
//
@@ -2299,48 +2289,30 @@ TypeExpr *TypeExpr::createForSpecializedDecl(DeclRefTypeRepr *ParentTR,
22992289
//
23002290
// FIXME: Once we can model generic typealiases properly, rip
23012291
// this out.
2302-
auto isUnboundGenericComponent = [](IdentTypeRepr *ITR) -> bool {
2303-
if (isa<SimpleIdentTypeRepr>(ITR)) {
2304-
auto *decl = dyn_cast_or_null<GenericTypeDecl>(ITR->getBoundDecl());
2292+
MemberTypeRepr *currTR = memberTR;
2293+
while (auto *declRefBaseTR =
2294+
dyn_cast<DeclRefTypeRepr>(currTR->getBase())) {
2295+
if (!declRefBaseTR->hasGenericArgList()) {
2296+
auto *decl =
2297+
dyn_cast_or_null<GenericTypeDecl>(declRefBaseTR->getBoundDecl());
23052298
if (decl && decl->isGeneric())
2306-
return true;
2299+
return nullptr;
23072300
}
23082301

2309-
return false;
2310-
};
2311-
2312-
for (auto *comp : memberTR->getMemberComponents().drop_back()) {
2313-
if (isUnboundGenericComponent(comp))
2314-
return nullptr;
2315-
}
2316-
2317-
if (auto *identRoot = dyn_cast<IdentTypeRepr>(memberTR->getRoot())) {
2318-
if (isUnboundGenericComponent(identRoot))
2319-
return nullptr;
2302+
currTR = dyn_cast<MemberTypeRepr>(declRefBaseTR);
2303+
if (!currTR) {
2304+
break;
2305+
}
23202306
}
23212307
}
2322-
}
23232308

2324-
auto *genericComp = GenericIdentTypeRepr::create(
2325-
C, lastComp->getNameLoc(), lastComp->getNameRef(), Args, AngleLocs);
2326-
genericComp->setValue(lastComp->getBoundDecl(), lastComp->getDeclContext());
2327-
2328-
TypeRepr *TR = nullptr;
2329-
if (auto *memberTR = dyn_cast<MemberTypeRepr>(ParentTR)) {
2330-
auto oldMemberComps = memberTR->getMemberComponents().drop_back();
2331-
2332-
// Create a new list of member components, replacing the last one with the
2333-
// new specialized one.
2334-
SmallVector<IdentTypeRepr *, 2> newMemberComps;
2335-
newMemberComps.append(oldMemberComps.begin(), oldMemberComps.end());
2336-
newMemberComps.push_back(genericComp);
2337-
2338-
TR = MemberTypeRepr::create(C, memberTR->getRoot(), newMemberComps);
2339-
} else {
2340-
TR = genericComp;
2309+
specializedTR =
2310+
MemberTypeRepr::create(C, memberTR->getBase(), ParentTR->getNameLoc(),
2311+
ParentTR->getNameRef(), Args, AngleLocs);
2312+
specializedTR->setValue(boundDecl, ParentTR->getDeclContext());
23412313
}
23422314

2343-
return new (C) TypeExpr(TR);
2315+
return new (C) TypeExpr(specializedTR);
23442316
}
23452317

23462318
// Create an implicit TypeExpr, with location information even though it

lib/AST/TypeCheckRequests.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,11 +1822,8 @@ SourceRange UnresolvedMacroReference::getGenericArgsRange() const {
18221822
auto [_, macro] = attr->destructureMacroRef();
18231823
if (!macro)
18241824
return SourceRange();
1825-
auto *genericTypeRepr = dyn_cast_or_null<GenericIdentTypeRepr>(macro);
1826-
if (!genericTypeRepr)
1827-
return SourceRange();
18281825

1829-
return genericTypeRepr->getAngleBrackets();
1826+
return macro->getAngleBrackets();
18301827
}
18311828

18321829
llvm_unreachable("Unhandled case");
@@ -1840,11 +1837,8 @@ ArrayRef<TypeRepr *> UnresolvedMacroReference::getGenericArgs() const {
18401837
auto [_, macro] = attr->destructureMacroRef();
18411838
if (!macro)
18421839
return {};
1843-
auto *genericTypeRepr = dyn_cast_or_null<GenericIdentTypeRepr>(macro);
1844-
if (!genericTypeRepr)
1845-
return {};
18461840

1847-
return genericTypeRepr->getGenericArgs();
1841+
return macro->getGenericArgs();
18481842
}
18491843

18501844
llvm_unreachable("Unhandled case");

lib/AST/TypeRepr.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ const TypeRepr *DeclRefTypeRepr::getRoot() const {
203203
return cast<MemberTypeRepr>(this)->getRoot();
204204
}
205205

206+
DeclNameLoc DeclRefTypeRepr::getNameLoc() const {
207+
return const_cast<DeclRefTypeRepr *>(this)->getLastComponent()->getNameLoc();
208+
}
209+
206210
bool DeclRefTypeRepr::isBound() const {
207211
return const_cast<DeclRefTypeRepr *>(this)->getLastComponent()->isBound();
208212
}
@@ -213,6 +217,16 @@ TypeDecl *DeclRefTypeRepr::getBoundDecl() const {
213217
->getBoundDecl();
214218
}
215219

220+
DeclContext *DeclRefTypeRepr::getDeclContext() const {
221+
return const_cast<DeclRefTypeRepr *>(this)
222+
->getLastComponent()
223+
->getDeclContext();
224+
}
225+
226+
void DeclRefTypeRepr::setValue(TypeDecl *TD, DeclContext *DC) {
227+
getLastComponent()->setValue(TD, DC);
228+
}
229+
216230
bool DeclRefTypeRepr::hasGenericArgList() const {
217231
return isa<GenericIdentTypeRepr>(
218232
const_cast<DeclRefTypeRepr *>(this)->getLastComponent());
@@ -227,6 +241,15 @@ ArrayRef<TypeRepr *> DeclRefTypeRepr::getGenericArgs() const {
227241
return {};
228242
}
229243

244+
SourceRange DeclRefTypeRepr::getAngleBrackets() const {
245+
auto *lastComp = const_cast<DeclRefTypeRepr *>(this)->getLastComponent();
246+
if (auto *genericTR = dyn_cast<GenericIdentTypeRepr>(lastComp)) {
247+
return genericTR->getAngleBrackets();
248+
}
249+
250+
return SourceRange();
251+
}
252+
230253
DeclNameRef DeclRefTypeRepr::getNameRef() const {
231254
return const_cast<DeclRefTypeRepr *>(this)->getLastComponent()->getNameRef();
232255
}
@@ -341,19 +364,13 @@ void AttributedTypeRepr::printAttrs(ASTPrinter &Printer,
341364
}
342365
}
343366

344-
static void printGenericArgs(ASTPrinter &Printer, const PrintOptions &Opts,
345-
ArrayRef<TypeRepr *> Args) {
346-
if (Args.empty())
347-
return;
348-
349-
Printer << "<";
350-
interleave(Args, [&](TypeRepr *Arg) { printTypeRepr(Arg, Printer, Opts); },
351-
[&] { Printer << ", "; });
352-
Printer << ">";
353-
}
367+
void DeclRefTypeRepr::printImpl(ASTPrinter &Printer,
368+
const PrintOptions &Opts) const {
369+
if (auto *memberTR = dyn_cast<MemberTypeRepr>(this)) {
370+
printTypeRepr(memberTR->getBase(), Printer, Opts);
371+
Printer << ".";
372+
}
354373

355-
void IdentTypeRepr::printImpl(ASTPrinter &Printer,
356-
const PrintOptions &Opts) const {
357374
if (auto *TD = dyn_cast_or_null<TypeDecl>(getBoundDecl())) {
358375
if (auto MD = dyn_cast<ModuleDecl>(TD))
359376
Printer.printModuleRef(MD, getNameRef().getBaseIdentifier());
@@ -363,16 +380,13 @@ void IdentTypeRepr::printImpl(ASTPrinter &Printer,
363380
Printer.printName(getNameRef().getBaseIdentifier());
364381
}
365382

366-
if (auto GenIdT = dyn_cast<GenericIdentTypeRepr>(this))
367-
printGenericArgs(Printer, Opts, GenIdT->getGenericArgs());
368-
}
369-
370-
void MemberTypeRepr::printImpl(ASTPrinter &Printer,
371-
const PrintOptions &Opts) const {
372-
printTypeRepr(getRoot(), Printer, Opts);
373-
for (auto C : getMemberComponents()) {
374-
Printer << ".";
375-
printTypeRepr(C, Printer, Opts);
383+
if (hasGenericArgList()) {
384+
Printer << "<";
385+
interleave(
386+
getGenericArgs(),
387+
[&](TypeRepr *Arg) { printTypeRepr(Arg, Printer, Opts); },
388+
[&] { Printer << ", "; });
389+
Printer << ">";
376390
}
377391
}
378392

0 commit comments

Comments
 (0)