Skip to content

Commit 7be645e

Browse files
committed
[NFC] Remove some uses of CompoundIdentTypeRepr::getComponents
1 parent 24204c0 commit 7be645e

File tree

8 files changed

+104
-82
lines changed

8 files changed

+104
-82
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,26 +425,32 @@ class CompoundIdentTypeRepr final : public IdentTypeRepr,
425425
static CompoundIdentTypeRepr *create(const ASTContext &Ctx,
426426
ArrayRef<ComponentIdentTypeRepr*> Components);
427427

428+
TypeRepr *getBaseComponent() const { return getComponents().front(); }
429+
428430
ArrayRef<ComponentIdentTypeRepr*> getComponents() const {
429431
return {getTrailingObjects<ComponentIdentTypeRepr*>(),
430432
Bits.CompoundIdentTypeRepr.NumComponents};
431433
}
432434

435+
ArrayRef<ComponentIdentTypeRepr *> getMemberComponents() const {
436+
return getComponents().slice(1);
437+
}
438+
439+
ComponentIdentTypeRepr *getLastComponent() const {
440+
return getMemberComponents().back();
441+
}
442+
433443
static bool classof(const TypeRepr *T) {
434444
return T->getKind() == TypeReprKind::CompoundIdent;
435445
}
436446
static bool classof(const CompoundIdentTypeRepr *T) { return true; }
437447

438448
private:
439449
SourceLoc getStartLocImpl() const {
440-
return getComponents().front()->getStartLoc();
441-
}
442-
SourceLoc getEndLocImpl() const {
443-
return getComponents().back()->getEndLoc();
444-
}
445-
SourceLoc getLocImpl() const {
446-
return getComponents().back()->getLoc();
450+
return getBaseComponent()->getStartLoc();
447451
}
452+
SourceLoc getEndLocImpl() const { return getLastComponent()->getEndLoc(); }
453+
SourceLoc getLocImpl() const { return getLastComponent()->getLoc(); }
448454

449455
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
450456
friend class TypeRepr;

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,11 +3116,14 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
31163116
printCommon("type_ident");
31173117
Indent += 2;
31183118

3119-
ArrayRef<ComponentIdentTypeRepr *> components;
3119+
SmallVector<ComponentIdentTypeRepr *, 2> components;
31203120
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(T)) {
3121-
components = comp;
3121+
components.push_back(comp);
31223122
} else {
3123-
components = cast<CompoundIdentTypeRepr>(T)->getComponents();
3123+
auto memberComps = cast<CompoundIdentTypeRepr>(T)->getMemberComponents();
3124+
3125+
components.push_back(cast<ComponentIdentTypeRepr>(T->getBaseComponent()));
3126+
components.append(memberComps.begin(), memberComps.end());
31243127
}
31253128

31263129
for (auto comp : components) {

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,10 @@ bool Traversal::visitGenericIdentTypeRepr(GenericIdentTypeRepr *T) {
19731973
}
19741974

19751975
bool Traversal::visitCompoundIdentTypeRepr(CompoundIdentTypeRepr *T) {
1976-
for (auto comp : T->getComponents()) {
1976+
if (doIt(T->getBaseComponent()))
1977+
return true;
1978+
1979+
for (auto comp : T->getMemberComponents()) {
19771980
if (doIt(comp))
19781981
return true;
19791982
}

lib/AST/NameLookup.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,11 +2257,14 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
22572257
// TypeRepr version: Builtin.AnyObject
22582258
if (auto typeRepr = typealias->getUnderlyingTypeRepr()) {
22592259
if (auto compound = dyn_cast<CompoundIdentTypeRepr>(typeRepr)) {
2260-
auto components = compound->getComponents();
2261-
if (components.size() == 2 &&
2262-
components[0]->getNameRef().isSimpleName("Builtin") &&
2263-
components[1]->getNameRef().isSimpleName("AnyObject")) {
2264-
anyObject = true;
2260+
if (auto identBase = dyn_cast<ComponentIdentTypeRepr>(
2261+
compound->getBaseComponent())) {
2262+
auto memberComps = compound->getMemberComponents();
2263+
if (memberComps.size() == 1 &&
2264+
identBase->getNameRef().isSimpleName("Builtin") &&
2265+
memberComps.front()->getNameRef().isSimpleName("AnyObject")) {
2266+
anyObject = true;
2267+
}
22652268
}
22662269
}
22672270
}
@@ -2425,33 +2428,34 @@ directReferencesForIdentTypeRepr(Evaluator &evaluator,
24252428
DeclContext *dc, bool allowUsableFromInline) {
24262429
DirectlyReferencedTypeDecls current;
24272430

2428-
ArrayRef<ComponentIdentTypeRepr *> components;
2429-
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(ident)) {
2430-
components = comp;
2431-
} else {
2432-
components = cast<CompoundIdentTypeRepr>(ident)->getComponents();
2433-
}
2434-
2435-
for (const auto &component : components) {
2431+
auto *baseComp = ident->getBaseComponent();
2432+
if (auto *identBase = dyn_cast<ComponentIdentTypeRepr>(baseComp)) {
24362433
// If we already set a declaration, use it.
2437-
if (auto typeDecl = component->getBoundDecl()) {
2434+
if (auto *typeDecl = identBase->getBoundDecl()) {
24382435
current = {1, typeDecl};
2439-
continue;
2436+
} else {
2437+
// For the base component, perform unqualified name lookup.
2438+
current = directReferencesForUnqualifiedTypeLookup(
2439+
identBase->getNameRef(), identBase->getLoc(), dc,
2440+
LookupOuterResults::Excluded, allowUsableFromInline);
24402441
}
2442+
} else {
2443+
current = directReferencesForTypeRepr(evaluator, ctx, baseComp, dc,
2444+
allowUsableFromInline);
2445+
}
24412446

2442-
// For the first component, perform unqualified name lookup.
2443-
if (current.empty()) {
2444-
current =
2445-
directReferencesForUnqualifiedTypeLookup(component->getNameRef(),
2446-
component->getLoc(),
2447-
dc,
2448-
LookupOuterResults::Excluded,
2449-
allowUsableFromInline);
2447+
auto *compound = dyn_cast<CompoundIdentTypeRepr>(ident);
2448+
if (!compound)
2449+
return current;
24502450

2451-
// If we didn't find anything, fail now.
2452-
if (current.empty())
2453-
return current;
2451+
// If we didn't find anything, fail now.
2452+
if (current.empty())
2453+
return current;
24542454

2455+
for (const auto &component : compound->getMemberComponents()) {
2456+
// If we already set a declaration, use it.
2457+
if (auto typeDecl = component->getBoundDecl()) {
2458+
current = {1, typeDecl};
24552459
continue;
24562460
}
24572461

lib/AST/TypeRepr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ TypeRepr *IdentTypeRepr::getBaseComponent() {
139139
if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(this))
140140
return Comp;
141141

142-
return cast<CompoundIdentTypeRepr>(this)->getComponents().front();
142+
return cast<CompoundIdentTypeRepr>(this)->getBaseComponent();
143143
}
144144

145145
ComponentIdentTypeRepr *IdentTypeRepr::getLastComponent() {
146146
if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(this))
147147
return Comp;
148148

149-
return cast<CompoundIdentTypeRepr>(this)->getComponents().back();
149+
return cast<CompoundIdentTypeRepr>(this)->getLastComponent();
150150
}
151151

152152
DeclNameRef ComponentIdentTypeRepr::getNameRef() const {
@@ -295,8 +295,8 @@ void ComponentIdentTypeRepr::printImpl(ASTPrinter &Printer,
295295

296296
void CompoundIdentTypeRepr::printImpl(ASTPrinter &Printer,
297297
const PrintOptions &Opts) const {
298-
printTypeRepr(getComponents().front(), Printer, Opts);
299-
for (auto C : getComponents().slice(1)) {
298+
printTypeRepr(getBaseComponent(), Printer, Opts);
299+
for (auto C : getMemberComponents()) {
300300
Printer << ".";
301301
printTypeRepr(C, Printer, Opts);
302302
}

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
206206
}
207207

208208
FoundResult visitCompoundIdentTypeRepr(CompoundIdentTypeRepr *T) {
209-
return visit(T->getComponents().back());
209+
return visit(T->getLastComponent());
210210
}
211211

212212
FoundResult visitOptionalTypeRepr(OptionalTypeRepr *T) {

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,31 +3986,36 @@ class TypeReprAvailabilityWalker : public ASTWalker {
39863986
: where(where), flags(flags) {}
39873987

39883988
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
3989-
if (auto *ITR = dyn_cast<IdentTypeRepr>(T)) {
3990-
if (auto *CTR = dyn_cast<CompoundIdentTypeRepr>(ITR)) {
3991-
for (auto *comp : CTR->getComponents()) {
3992-
// If a parent type is unavailable, don't go on to diagnose
3993-
// the member since that will just produce a redundant
3994-
// diagnostic.
3995-
if (checkComponentIdentTypeRepr(comp)) {
3996-
foundAnyIssues = true;
3997-
break;
3998-
}
3999-
}
4000-
} else if (auto *GTR = dyn_cast<GenericIdentTypeRepr>(T)) {
4001-
if (checkComponentIdentTypeRepr(GTR))
4002-
foundAnyIssues = true;
4003-
} else if (auto *STR = dyn_cast<SimpleIdentTypeRepr>(T)) {
4004-
if (checkComponentIdentTypeRepr(STR))
4005-
foundAnyIssues = true;
3989+
auto *ITR = dyn_cast<IdentTypeRepr>(T);
3990+
if (!ITR)
3991+
return Action::Continue();
3992+
3993+
auto *baseComp = ITR->getBaseComponent();
3994+
if (auto *identBase = dyn_cast<ComponentIdentTypeRepr>(baseComp)) {
3995+
if (checkComponentIdentTypeRepr(identBase)) {
3996+
foundAnyIssues = true;
3997+
return Action::SkipChildren();
40063998
}
4007-
4008-
// We've already visited all the children above, so we don't
4009-
// need to recurse.
3999+
} else if (diagnoseTypeReprAvailability(baseComp, where, flags)) {
4000+
foundAnyIssues = true;
40104001
return Action::SkipChildren();
40114002
}
40124003

4013-
return Action::Continue();
4004+
if (auto *CTR = dyn_cast<CompoundIdentTypeRepr>(T)) {
4005+
for (auto *comp : CTR->getMemberComponents()) {
4006+
// If a parent type is unavailable, don't go on to diagnose
4007+
// the member since that will just produce a redundant
4008+
// diagnostic.
4009+
if (checkComponentIdentTypeRepr(comp)) {
4010+
foundAnyIssues = true;
4011+
break;
4012+
}
4013+
}
4014+
}
4015+
4016+
// We've already visited all the children above, so we don't
4017+
// need to recurse.
4018+
return Action::SkipChildren();
40144019
}
40154020
};
40164021

lib/Sema/TypeCheckType.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3805,34 +3805,35 @@ bool TypeResolver::resolveSILResults(TypeRepr *repr,
38053805
NeverNullType
38063806
TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
38073807
TypeResolutionOptions options) {
3808-
ArrayRef<ComponentIdentTypeRepr *> Components;
3809-
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(IdType)) {
3810-
Components = comp;
3808+
Type result;
3809+
3810+
auto *baseComp = IdType->getBaseComponent();
3811+
if (auto *identBase = dyn_cast<ComponentIdentTypeRepr>(baseComp)) {
3812+
// The base component uses unqualified lookup.
3813+
result = resolveTopLevelIdentTypeComponent(resolution.withOptions(options),
3814+
genericParams, identBase);
38113815
} else {
3812-
Components = cast<CompoundIdentTypeRepr>(IdType)->getComponents();
3816+
result = resolveType(baseComp, options);
38133817
}
38143818

3815-
// The first component uses unqualified lookup.
3816-
auto topLevelComp = Components.front();
3817-
auto result = resolveTopLevelIdentTypeComponent(
3818-
resolution.withOptions(options), genericParams, topLevelComp);
38193819
if (result->hasError())
38203820
return ErrorType::get(result->getASTContext());
38213821

38223822
// Remaining components are resolved via iterated qualified lookups.
3823-
SourceRange parentRange(topLevelComp->getStartLoc(),
3824-
topLevelComp->getEndLoc());
3825-
for (auto nestedComp : Components.drop_front()) {
3826-
result = resolveNestedIdentTypeComponent(resolution.withOptions(options),
3827-
genericParams, result, parentRange,
3828-
nestedComp);
3829-
if (result->hasError())
3830-
return ErrorType::get(result->getASTContext());
3823+
if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(IdType)) {
3824+
SourceRange parentRange = baseComp->getSourceRange();
3825+
for (auto *nestedComp : compound->getMemberComponents()) {
3826+
result = resolveNestedIdentTypeComponent(resolution.withOptions(options),
3827+
genericParams, result,
3828+
parentRange, nestedComp);
3829+
if (result->hasError())
3830+
return ErrorType::get(result->getASTContext());
38313831

3832-
parentRange.End = nestedComp->getEndLoc();
3832+
parentRange.End = nestedComp->getEndLoc();
3833+
}
38333834
}
38343835

3835-
auto lastComp = Components.back();
3836+
auto lastComp = IdType->getLastComponent();
38363837

38373838
// Diagnose an error if the last component's generic arguments are missing.
38383839
if (result->is<UnboundGenericType>() &&
@@ -4702,7 +4703,7 @@ class ExistentialTypeVisitor
47024703
if (auto compound = dyn_cast<CompoundIdentTypeRepr>(T)) {
47034704
// Only visit the last component to check, because nested typealiases in
47044705
// existentials are okay.
4705-
visit(compound->getComponents().back());
4706+
visit(compound->getLastComponent());
47064707
return Action::SkipChildren();
47074708
}
47084709
// Arbitrary protocol constraints are OK on opaque types.

0 commit comments

Comments
 (0)