Skip to content

Commit e5413c9

Browse files
committed
[NFC] AST/NameLookup: Refactor for recursive MemberTypeRepr representation
1 parent e118026 commit e5413c9

File tree

3 files changed

+74
-56
lines changed

3 files changed

+74
-56
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ class DeclRefTypeRepr : public TypeRepr {
362362
/// The identifier that describes the last component.
363363
DeclNameRef getNameRef() const;
364364

365+
/// Returns whether this instance has a generic argument list. That is, either
366+
/// a valid angle bracket source range, or a positive number of generic
367+
/// arguments.
368+
bool hasGenericArgList() const;
369+
365370
ArrayRef<TypeRepr *> getGenericArgs() const;
366371

367372
static bool classof(const TypeRepr *T) {
@@ -518,11 +523,14 @@ class MemberTypeRepr final : public DeclRefTypeRepr,
518523
private llvm::TrailingObjects<MemberTypeRepr, IdentTypeRepr *> {
519524
friend TrailingObjects;
520525

526+
const ASTContext *C;
527+
521528
/// The root component, which is not necessarily an identifier type.
522529
TypeRepr *Root;
523530

524-
MemberTypeRepr(TypeRepr *Root, ArrayRef<IdentTypeRepr *> MemberComponents)
525-
: DeclRefTypeRepr(TypeReprKind::Member), Root(Root) {
531+
MemberTypeRepr(TypeRepr *Root, ArrayRef<IdentTypeRepr *> MemberComponents,
532+
const ASTContext &C)
533+
: DeclRefTypeRepr(TypeReprKind::Member), C(&C), Root(Root) {
526534
Bits.MemberTypeRepr.NumMemberComponents = MemberComponents.size();
527535
assert(MemberComponents.size() > 0 &&
528536
"MemberTypeRepr requires at least 1 member component");
@@ -531,12 +539,24 @@ class MemberTypeRepr final : public DeclRefTypeRepr,
531539
}
532540

533541
public:
542+
static MemberTypeRepr *create(const ASTContext &C, TypeRepr *Base,
543+
DeclNameLoc NameLoc, DeclNameRef Name);
544+
545+
static MemberTypeRepr *create(const ASTContext &C, TypeRepr *Base,
546+
DeclNameLoc NameLoc, DeclNameRef Name,
547+
ArrayRef<TypeRepr *> GenericArgs,
548+
SourceRange AngleBrackets);
549+
534550
static TypeRepr *create(const ASTContext &Ctx, TypeRepr *Root,
535551
ArrayRef<IdentTypeRepr *> MemberComponents);
536552

537553
static DeclRefTypeRepr *create(const ASTContext &Ctx,
538554
ArrayRef<IdentTypeRepr *> Components);
539555

556+
/// Returns the qualifier or base type representation. For example, `A.B`
557+
/// for `A.B.C`.
558+
TypeRepr *getBase() const;
559+
540560
/// Returns the root qualifier. For example, `A` for `A.B.C`.
541561
TypeRepr *getRoot() const { return Root; }
542562

lib/AST/NameLookup.cpp

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,24 +2745,21 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
27452745

27462746
// Recognize Swift.AnyObject directly.
27472747
if (typealias->getName().is("AnyObject")) {
2748-
// TypeRepr version: Builtin.AnyObject
2749-
if (auto typeRepr = typealias->getUnderlyingTypeRepr()) {
2750-
if (auto memberTR = dyn_cast<MemberTypeRepr>(typeRepr)) {
2751-
auto memberComps = memberTR->getMemberComponents();
2752-
if (memberComps.size() == 1 &&
2753-
memberComps.front()->getNameRef().isSimpleName("AnyObject") &&
2754-
memberTR->getRoot()->isSimpleUnqualifiedIdentifier("Builtin")) {
2755-
anyObject = true;
2756-
}
2757-
}
2758-
}
2759-
27602748
// Type version: an empty class-bound existential.
27612749
if (typealias->hasInterfaceType()) {
27622750
if (auto type = typealias->getUnderlyingType())
27632751
if (type->isAnyObject())
27642752
anyObject = true;
27652753
}
2754+
// TypeRepr version: Builtin.AnyObject
2755+
else if (auto *memberTR = dyn_cast_or_null<MemberTypeRepr>(
2756+
typealias->getUnderlyingTypeRepr())) {
2757+
if (!memberTR->hasGenericArgList() &&
2758+
memberTR->getNameRef().isSimpleName("AnyObject") &&
2759+
memberTR->getBase()->isSimpleUnqualifiedIdentifier("Builtin")) {
2760+
anyObject = true;
2761+
}
2762+
}
27662763
}
27672764

27682765
continue;
@@ -2922,50 +2919,25 @@ static DirectlyReferencedTypeDecls
29222919
directReferencesForDeclRefTypeRepr(Evaluator &evaluator, ASTContext &ctx,
29232920
DeclRefTypeRepr *repr, DeclContext *dc,
29242921
bool allowUsableFromInline) {
2925-
DirectlyReferencedTypeDecls current;
2926-
2927-
auto *rootComp = repr->getRoot();
2928-
if (auto *identBase = dyn_cast<IdentTypeRepr>(rootComp)) {
2929-
// If we already set a declaration, use it.
2930-
if (auto *typeDecl = identBase->getBoundDecl()) {
2931-
current = {1, typeDecl};
2932-
} else {
2933-
// For the base component, perform unqualified name lookup.
2934-
current = directReferencesForUnqualifiedTypeLookup(
2935-
identBase->getNameRef(), identBase->getLoc(), dc,
2936-
LookupOuterResults::Excluded, allowUsableFromInline);
2937-
}
2938-
} else {
2939-
current = directReferencesForTypeRepr(evaluator, ctx, rootComp, dc,
2940-
allowUsableFromInline);
2922+
// If we already set a declaration, use it.
2923+
if (auto *typeDecl = repr->getBoundDecl()) {
2924+
return {typeDecl};
29412925
}
29422926

2943-
auto *memberTR = dyn_cast<MemberTypeRepr>(repr);
2944-
if (!memberTR)
2945-
return current;
2946-
2947-
// If we didn't find anything, fail now.
2948-
if (current.empty())
2949-
return current;
2950-
2951-
for (const auto &component : memberTR->getMemberComponents()) {
2952-
// If we already set a declaration, use it.
2953-
if (auto typeDecl = component->getBoundDecl()) {
2954-
current = {1, typeDecl};
2955-
continue;
2956-
}
2927+
if (auto *memberTR = dyn_cast<MemberTypeRepr>(repr)) {
2928+
DirectlyReferencedTypeDecls baseTypeDecls = directReferencesForTypeRepr(
2929+
evaluator, ctx, memberTR->getBase(), dc, allowUsableFromInline);
29572930

2958-
// For subsequent components, perform qualified name lookup.
2959-
current =
2960-
directReferencesForQualifiedTypeLookup(evaluator, ctx, current,
2961-
component->getNameRef(), dc,
2962-
component->getLoc(),
2963-
allowUsableFromInline);
2964-
if (current.empty())
2965-
return current;
2931+
// For a qualified identifier, perform qualified name lookup.
2932+
return directReferencesForQualifiedTypeLookup(
2933+
evaluator, ctx, baseTypeDecls, repr->getNameRef(), dc, repr->getLoc(),
2934+
allowUsableFromInline);
29662935
}
29672936

2968-
return current;
2937+
// For an unqualified identifier, perform unqualified name lookup.
2938+
return directReferencesForUnqualifiedTypeLookup(
2939+
repr->getNameRef(), repr->getLoc(), dc, LookupOuterResults::Excluded,
2940+
allowUsableFromInline);
29692941
}
29702942

29712943
static DirectlyReferencedTypeDecls
@@ -3654,11 +3626,12 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
36543626
assocType->getDescriptiveKind(),
36553627
assocType->getName());
36563628

3657-
auto *baseComp = new (ctx) SimpleIdentTypeRepr(
3629+
auto *baseTR = new (ctx) SimpleIdentTypeRepr(
36583630
identTypeRepr->getNameLoc(), DeclNameRef(moduleName));
36593631

36603632
auto *newTE = new (ctx) TypeExpr(
3661-
MemberTypeRepr::create(ctx, baseComp, {identTypeRepr}));
3633+
MemberTypeRepr::create(ctx, baseTR, identTypeRepr->getNameLoc(),
3634+
identTypeRepr->getNameRef()));
36623635
attr->resetTypeInformation(newTE);
36633636
return nominal;
36643637
}

lib/AST/TypeRepr.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ TypeDecl *DeclRefTypeRepr::getBoundDecl() const {
209209
->getBoundDecl();
210210
}
211211

212+
bool DeclRefTypeRepr::hasGenericArgList() const {
213+
return isa<GenericIdentTypeRepr>(
214+
const_cast<DeclRefTypeRepr *>(this)->getLastComponent());
215+
}
216+
212217
ArrayRef<TypeRepr *> DeclRefTypeRepr::getGenericArgs() const {
213218
auto *lastComp = const_cast<DeclRefTypeRepr *>(this)->getLastComponent();
214219
if (auto *genericTR = dyn_cast<GenericIdentTypeRepr>(lastComp)) {
@@ -454,14 +459,29 @@ GenericIdentTypeRepr *GenericIdentTypeRepr::create(const ASTContext &C,
454459
return new (mem) GenericIdentTypeRepr(Loc, Id, GenericArgs, AngleBrackets);
455460
}
456461

462+
MemberTypeRepr *MemberTypeRepr::create(const ASTContext &C, TypeRepr *Base,
463+
DeclNameLoc NameLoc, DeclNameRef Name) {
464+
assert(Base);
465+
return cast<MemberTypeRepr>(DeclRefTypeRepr::create(C, Base, NameLoc, Name));
466+
}
467+
468+
MemberTypeRepr *MemberTypeRepr::create(const ASTContext &C, TypeRepr *Base,
469+
DeclNameLoc NameLoc, DeclNameRef Name,
470+
ArrayRef<TypeRepr *> GenericArgs,
471+
SourceRange AngleBrackets) {
472+
assert(Base);
473+
return cast<MemberTypeRepr>(DeclRefTypeRepr::create(
474+
C, Base, NameLoc, Name, GenericArgs, AngleBrackets));
475+
}
476+
457477
TypeRepr *MemberTypeRepr::create(const ASTContext &C, TypeRepr *Root,
458478
ArrayRef<IdentTypeRepr *> MemberComponents) {
459479
if (MemberComponents.empty())
460480
return Root;
461481

462482
auto size = totalSizeToAlloc<IdentTypeRepr *>(MemberComponents.size());
463483
auto mem = C.Allocate(size, alignof(MemberTypeRepr));
464-
return new (mem) MemberTypeRepr(Root, MemberComponents);
484+
return new (mem) MemberTypeRepr(Root, MemberComponents, C);
465485
}
466486

467487
DeclRefTypeRepr *MemberTypeRepr::create(const ASTContext &Ctx,
@@ -470,6 +490,11 @@ DeclRefTypeRepr *MemberTypeRepr::create(const ASTContext &Ctx,
470490
create(Ctx, Components.front(), Components.drop_front()));
471491
}
472492

493+
TypeRepr *MemberTypeRepr::getBase() const {
494+
return MemberTypeRepr::create(*C, getRoot(),
495+
getMemberComponents().drop_back());
496+
}
497+
473498
PackTypeRepr::PackTypeRepr(SourceLoc keywordLoc, SourceRange braceLocs,
474499
ArrayRef<TypeRepr*> elements)
475500
: TypeRepr(TypeReprKind::Pack),

0 commit comments

Comments
 (0)