Skip to content

Commit 5397dc7

Browse files
committed
[NFC] Remove some uses of CompoundIdentTypeRepr::getComponents
1 parent caca4f8 commit 5397dc7

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
@@ -426,26 +426,32 @@ class CompoundIdentTypeRepr final : public IdentTypeRepr,
426426
static CompoundIdentTypeRepr *create(const ASTContext &Ctx,
427427
ArrayRef<ComponentIdentTypeRepr*> Components);
428428

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

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

439449
private:
440450
SourceLoc getStartLocImpl() const {
441-
return getComponents().front()->getStartLoc();
442-
}
443-
SourceLoc getEndLocImpl() const {
444-
return getComponents().back()->getEndLoc();
445-
}
446-
SourceLoc getLocImpl() const {
447-
return getComponents().back()->getLoc();
451+
return getBaseComponent()->getStartLoc();
448452
}
453+
SourceLoc getEndLocImpl() const { return getLastComponent()->getEndLoc(); }
454+
SourceLoc getLocImpl() const { return getLastComponent()->getLoc(); }
449455

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

lib/AST/ASTDumper.cpp

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

3095-
ArrayRef<ComponentIdentTypeRepr *> components;
3095+
SmallVector<ComponentIdentTypeRepr *, 2> components;
30963096
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(T)) {
3097-
components = comp;
3097+
components.push_back(comp);
30983098
} else {
3099-
components = cast<CompoundIdentTypeRepr>(T)->getComponents();
3099+
auto memberComps = cast<CompoundIdentTypeRepr>(T)->getMemberComponents();
3100+
3101+
components.push_back(cast<ComponentIdentTypeRepr>(T->getBaseComponent()));
3102+
components.append(memberComps.begin(), memberComps.end());
31003103
}
31013104

31023105
for (auto comp : components) {

lib/AST/ASTWalker.cpp

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

19541954
bool Traversal::visitCompoundIdentTypeRepr(CompoundIdentTypeRepr *T) {
1955-
for (auto comp : T->getComponents()) {
1955+
if (doIt(T->getBaseComponent()))
1956+
return true;
1957+
1958+
for (auto comp : T->getMemberComponents()) {
19561959
if (doIt(comp))
19571960
return true;
19581961
}

lib/AST/NameLookup.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,11 +2268,14 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
22682268
// TypeRepr version: Builtin.AnyObject
22692269
if (auto typeRepr = typealias->getUnderlyingTypeRepr()) {
22702270
if (auto compound = dyn_cast<CompoundIdentTypeRepr>(typeRepr)) {
2271-
auto components = compound->getComponents();
2272-
if (components.size() == 2 &&
2273-
components[0]->getNameRef().isSimpleName("Builtin") &&
2274-
components[1]->getNameRef().isSimpleName("AnyObject")) {
2275-
anyObject = true;
2271+
if (auto identBase = dyn_cast<ComponentIdentTypeRepr>(
2272+
compound->getBaseComponent())) {
2273+
auto memberComps = compound->getMemberComponents();
2274+
if (memberComps.size() == 1 &&
2275+
identBase->getNameRef().isSimpleName("Builtin") &&
2276+
memberComps.front()->getNameRef().isSimpleName("AnyObject")) {
2277+
anyObject = true;
2278+
}
22762279
}
22772280
}
22782281
}
@@ -2436,33 +2439,34 @@ directReferencesForIdentTypeRepr(Evaluator &evaluator,
24362439
DeclContext *dc, bool allowUsableFromInline) {
24372440
DirectlyReferencedTypeDecls current;
24382441

2439-
ArrayRef<ComponentIdentTypeRepr *> components;
2440-
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(ident)) {
2441-
components = comp;
2442-
} else {
2443-
components = cast<CompoundIdentTypeRepr>(ident)->getComponents();
2444-
}
2445-
2446-
for (const auto &component : components) {
2442+
auto *baseComp = ident->getBaseComponent();
2443+
if (auto *identBase = dyn_cast<ComponentIdentTypeRepr>(baseComp)) {
24472444
// If we already set a declaration, use it.
2448-
if (auto typeDecl = component->getBoundDecl()) {
2445+
if (auto *typeDecl = identBase->getBoundDecl()) {
24492446
current = {1, typeDecl};
2450-
continue;
2447+
} else {
2448+
// For the base component, perform unqualified name lookup.
2449+
current = directReferencesForUnqualifiedTypeLookup(
2450+
identBase->getNameRef(), identBase->getLoc(), dc,
2451+
LookupOuterResults::Excluded, allowUsableFromInline);
24512452
}
2453+
} else {
2454+
current = directReferencesForTypeRepr(evaluator, ctx, baseComp, dc,
2455+
allowUsableFromInline);
2456+
}
24522457

2453-
// For the first component, perform unqualified name lookup.
2454-
if (current.empty()) {
2455-
current =
2456-
directReferencesForUnqualifiedTypeLookup(component->getNameRef(),
2457-
component->getLoc(),
2458-
dc,
2459-
LookupOuterResults::Excluded,
2460-
allowUsableFromInline);
2458+
auto *compound = dyn_cast<CompoundIdentTypeRepr>(ident);
2459+
if (!compound)
2460+
return current;
24612461

2462-
// If we didn't find anything, fail now.
2463-
if (current.empty())
2464-
return current;
2462+
// If we didn't find anything, fail now.
2463+
if (current.empty())
2464+
return current;
24652465

2466+
for (const auto &component : compound->getMemberComponents()) {
2467+
// If we already set a declaration, use it.
2468+
if (auto typeDecl = component->getBoundDecl()) {
2469+
current = {1, typeDecl};
24662470
continue;
24672471
}
24682472

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
@@ -3985,31 +3985,36 @@ class TypeReprAvailabilityWalker : public ASTWalker {
39853985
: where(where), flags(flags) {}
39863986

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

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

lib/Sema/TypeCheckType.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,34 +3810,35 @@ bool TypeResolver::resolveSILResults(TypeRepr *repr,
38103810
NeverNullType
38113811
TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
38123812
TypeResolutionOptions options) {
3813-
ArrayRef<ComponentIdentTypeRepr *> Components;
3814-
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(IdType)) {
3815-
Components = comp;
3813+
Type result;
3814+
3815+
auto *baseComp = IdType->getBaseComponent();
3816+
if (auto *identBase = dyn_cast<ComponentIdentTypeRepr>(baseComp)) {
3817+
// The base component uses unqualified lookup.
3818+
result = resolveTopLevelIdentTypeComponent(resolution.withOptions(options),
3819+
genericParams, identBase);
38163820
} else {
3817-
Components = cast<CompoundIdentTypeRepr>(IdType)->getComponents();
3821+
result = resolveType(baseComp, options);
38183822
}
38193823

3820-
// The first component uses unqualified lookup.
3821-
auto topLevelComp = Components.front();
3822-
auto result = resolveTopLevelIdentTypeComponent(
3823-
resolution.withOptions(options), genericParams, topLevelComp);
38243824
if (result->hasError())
38253825
return ErrorType::get(result->getASTContext());
38263826

38273827
// Remaining components are resolved via iterated qualified lookups.
3828-
SourceRange parentRange(topLevelComp->getStartLoc(),
3829-
topLevelComp->getEndLoc());
3830-
for (auto nestedComp : Components.drop_front()) {
3831-
result = resolveNestedIdentTypeComponent(resolution.withOptions(options),
3832-
genericParams, result, parentRange,
3833-
nestedComp);
3834-
if (result->hasError())
3835-
return ErrorType::get(result->getASTContext());
3828+
if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(IdType)) {
3829+
SourceRange parentRange = baseComp->getSourceRange();
3830+
for (auto *nestedComp : compound->getMemberComponents()) {
3831+
result = resolveNestedIdentTypeComponent(resolution.withOptions(options),
3832+
genericParams, result,
3833+
parentRange, nestedComp);
3834+
if (result->hasError())
3835+
return ErrorType::get(result->getASTContext());
38363836

3837-
parentRange.End = nestedComp->getEndLoc();
3837+
parentRange.End = nestedComp->getEndLoc();
3838+
}
38383839
}
38393840

3840-
auto lastComp = Components.back();
3841+
auto lastComp = IdType->getLastComponent();
38413842

38423843
// Diagnose an error if the last component's generic arguments are missing.
38433844
if (result->is<UnboundGenericType>() &&
@@ -4674,7 +4675,7 @@ class ExistentialTypeVisitor
46744675
if (auto compound = dyn_cast<CompoundIdentTypeRepr>(T)) {
46754676
// Only visit the last component to check, because nested typealiases in
46764677
// existentials are okay.
4677-
visit(compound->getComponents().back());
4678+
visit(compound->getLastComponent());
46784679
return Action::SkipChildren();
46794680
}
46804681
// Arbitrary protocol constraints are OK on opaque types.

0 commit comments

Comments
 (0)