Skip to content

Commit 2113e17

Browse files
committed
[NFC] AST: Remove IdentTypeRepr::ComponentRange
1 parent ea76627 commit 2113e17

File tree

10 files changed

+85
-69
lines changed

10 files changed

+85
-69
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,14 @@ class IdentTypeRepr : public TypeRepr {
256256
explicit IdentTypeRepr(TypeReprKind K) : TypeRepr(K) {}
257257

258258
public:
259-
class ComponentRange;
260-
inline ComponentRange getComponentRange();
259+
TypeRepr *getBaseComponent();
260+
ComponentIdentTypeRepr *getLastComponent();
261+
262+
/// The type declaration the last component is bound to.
263+
TypeDecl *getBoundDecl() const;
264+
265+
/// The identifier that describes the last component.
266+
DeclNameRef getNameRef() const;
261267

262268
static bool classof(const TypeRepr *T) {
263269
return T->getKind() == TypeReprKind::SimpleIdent ||
@@ -444,38 +450,6 @@ class CompoundIdentTypeRepr final : public IdentTypeRepr,
444450
friend class TypeRepr;
445451
};
446452

447-
/// This wraps an IdentTypeRepr and provides an iterator interface for the
448-
/// components (or the single component) it represents.
449-
class IdentTypeRepr::ComponentRange {
450-
IdentTypeRepr *IdT;
451-
452-
public:
453-
explicit ComponentRange(IdentTypeRepr *T) : IdT(T) {}
454-
455-
typedef ComponentIdentTypeRepr * const* iterator;
456-
457-
iterator begin() const {
458-
if (isa<ComponentIdentTypeRepr>(IdT))
459-
return reinterpret_cast<iterator>(&IdT);
460-
return cast<CompoundIdentTypeRepr>(IdT)->getComponents().begin();
461-
}
462-
463-
iterator end() const {
464-
if (isa<ComponentIdentTypeRepr>(IdT))
465-
return reinterpret_cast<iterator>(&IdT) + 1;
466-
return cast<CompoundIdentTypeRepr>(IdT)->getComponents().end();
467-
}
468-
469-
bool empty() const { return begin() == end(); }
470-
471-
ComponentIdentTypeRepr *front() const { return *begin(); }
472-
ComponentIdentTypeRepr *back() const { return *(end()-1); }
473-
};
474-
475-
inline IdentTypeRepr::ComponentRange IdentTypeRepr::getComponentRange() {
476-
return ComponentRange(this);
477-
}
478-
479453
/// A function type.
480454
/// \code
481455
/// (Foo) -> Bar

lib/AST/ASTDumper.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,15 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
31153115
void visitIdentTypeRepr(IdentTypeRepr *T) {
31163116
printCommon("type_ident");
31173117
Indent += 2;
3118-
for (auto comp : T->getComponentRange()) {
3118+
3119+
ArrayRef<ComponentIdentTypeRepr *> components;
3120+
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(T)) {
3121+
components = comp;
3122+
} else {
3123+
components = cast<CompoundIdentTypeRepr>(T)->getComponents();
3124+
}
3125+
3126+
for (auto comp : components) {
31193127
OS << '\n';
31203128
printCommon("component");
31213129
PrintWithColorRAII(OS, IdentifierColor)

lib/AST/Expr.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,8 +2236,12 @@ TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
22362236

22372237
// Create a new list of components.
22382238
SmallVector<ComponentIdentTypeRepr *, 2> Components;
2239-
for (auto *Component : ParentTR->getComponentRange())
2240-
Components.push_back(Component);
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());
2244+
}
22412245

22422246
assert(!Components.empty());
22432247

@@ -2256,8 +2260,11 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
22562260
ASTContext &C) {
22572261
// Create a new list of components.
22582262
SmallVector<ComponentIdentTypeRepr *, 2> components;
2259-
for (auto *component : ParentTR->getComponentRange()) {
2260-
components.push_back(component);
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());
22612268
}
22622269

22632270
auto *last = components.back();

lib/AST/NameLookup.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,14 @@ directReferencesForIdentTypeRepr(Evaluator &evaluator,
24252425
DeclContext *dc, bool allowUsableFromInline) {
24262426
DirectlyReferencedTypeDecls current;
24272427

2428-
for (const auto &component : ident->getComponentRange()) {
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) {
24292436
// If we already set a declaration, use it.
24302437
if (auto typeDecl = component->getBoundDecl()) {
24312438
current = {1, typeDecl};

lib/AST/TypeRepr.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ SourceLoc TypeRepr::findUncheckedAttrLoc() const {
127127
return SourceLoc();
128128
}
129129

130+
TypeDecl *IdentTypeRepr::getBoundDecl() const {
131+
return const_cast<IdentTypeRepr *>(this)->getLastComponent()->getBoundDecl();
132+
}
133+
134+
DeclNameRef IdentTypeRepr::getNameRef() const {
135+
return const_cast<IdentTypeRepr *>(this)->getLastComponent()->getNameRef();
136+
}
137+
138+
TypeRepr *IdentTypeRepr::getBaseComponent() {
139+
if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(this))
140+
return Comp;
141+
142+
return cast<CompoundIdentTypeRepr>(this)->getComponents().front();
143+
}
144+
145+
ComponentIdentTypeRepr *IdentTypeRepr::getLastComponent() {
146+
if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(this))
147+
return Comp;
148+
149+
return cast<CompoundIdentTypeRepr>(this)->getComponents().back();
150+
}
151+
130152
DeclNameRef ComponentIdentTypeRepr::getNameRef() const {
131153
if (IdOrDecl.is<DeclNameRef>())
132154
return IdOrDecl.get<DeclNameRef>();

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,11 @@ class CodeCompletionCallbacksImpl : public IDEInspectionCallbacks {
215215

216216
// It doesn't type check as a type, so see if it's a qualifying module name.
217217
if (auto *ITR = dyn_cast<IdentTypeRepr>(ParsedTypeLoc.getTypeRepr())) {
218-
const auto &componentRange = ITR->getComponentRange();
219218
// If it has more than one component, it can't be a module name.
220-
if (std::distance(componentRange.begin(), componentRange.end()) != 1)
219+
if (isa<CompoundIdentTypeRepr>(ITR))
221220
return false;
222221

223-
const auto &component = componentRange.front();
222+
const auto *component = cast<ComponentIdentTypeRepr>(ITR);
224223
ImportPath::Module::Builder builder(
225224
component->getNameRef().getBaseIdentifier(),
226225
component->getLoc());

lib/Index/Index.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,11 +1326,10 @@ bool IndexSwiftASTWalker::reportInheritedTypeRefs(ArrayRef<InheritedEntry> Inher
13261326
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related) {
13271327

13281328
if (auto *T = dyn_cast_or_null<IdentTypeRepr>(Ty.getTypeRepr())) {
1329-
auto Comps = T->getComponentRange();
1330-
SourceLoc IdLoc = Comps.back()->getLoc();
1329+
SourceLoc IdLoc = T->getLoc();
13311330
NominalTypeDecl *NTD = nullptr;
13321331
bool isImplicit = false;
1333-
if (auto *VD = Comps.back()->getBoundDecl()) {
1332+
if (auto *VD = T->getBoundDecl()) {
13341333
if (auto *TAD = dyn_cast<TypeAliasDecl>(VD)) {
13351334
IndexSymbol Info;
13361335
if (!reportRef(TAD, IdLoc, Info, None))
@@ -1426,8 +1425,7 @@ IndexSwiftASTWalker::getTypeLocAsNominalTypeDecl(const TypeLoc &Ty) {
14261425
if (Type T = Ty.getType())
14271426
return T->getAnyNominal();
14281427
if (auto *T = dyn_cast_or_null<IdentTypeRepr>(Ty.getTypeRepr())) {
1429-
auto Comp = T->getComponentRange().back();
1430-
if (auto NTD = dyn_cast_or_null<NominalTypeDecl>(Comp->getBoundDecl()))
1428+
if (auto NTD = dyn_cast_or_null<NominalTypeDecl>(T->getBoundDecl()))
14311429
return NTD;
14321430
}
14331431
return nullptr;

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,12 +1933,8 @@ static bool isMainDispatchQueueMember(ConstraintLocator *locator) {
19331933
if (!identTypeRepr)
19341934
return false;
19351935

1936-
auto components = identTypeRepr->getComponentRange();
1937-
if (components.empty())
1938-
return false;
1939-
1940-
if (components.back()->getNameRef().getBaseName().userFacingName() !=
1941-
"DispatchQueue")
1936+
if (identTypeRepr->getNameRef().getBaseName().userFacingName() !=
1937+
"DispatchQueue")
19421938
return false;
19431939

19441940
return true;

lib/Sema/TypeCheckType.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,8 +2375,7 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
23752375
// be proper name lookup, OF COURSE.
23762376
if (auto simpleIdent = dyn_cast<SimpleIdentTypeRepr>(
23772377
opaqueRepr->getBase())) {
2378-
Identifier name = simpleIdent->getComponentRange().front()
2379-
->getNameRef().getBaseIdentifier();
2378+
Identifier name = simpleIdent->getNameRef().getBaseIdentifier();
23802379
if (auto gpDecl = opaqueRepr->getGenericParams()
23812380
->lookUpGenericParam(name)) {
23822381
auto outerGenericSignature = opaqueDecl->getNamingDecl()
@@ -3860,9 +3859,13 @@ bool TypeResolver::resolveSILResults(TypeRepr *repr,
38603859
NeverNullType
38613860
TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
38623861
TypeResolutionOptions options) {
3863-
auto ComponentRange = IdType->getComponentRange();
3864-
auto Components = llvm::makeArrayRef(ComponentRange.begin(),
3865-
ComponentRange.end());
3862+
ArrayRef<ComponentIdentTypeRepr *> Components;
3863+
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(IdType)) {
3864+
Components = comp;
3865+
} else {
3866+
Components = cast<CompoundIdentTypeRepr>(IdType)->getComponents();
3867+
}
3868+
38663869
Type result = resolveIdentTypeComponent(resolution.withOptions(options),
38673870
genericParams, Components);
38683871
if (!result || result->hasError()) {
@@ -4714,7 +4717,7 @@ class ExistentialTypeVisitor
47144717
if (auto compound = dyn_cast<CompoundIdentTypeRepr>(T)) {
47154718
// Only visit the last component to check, because nested typealiases in
47164719
// existentials are okay.
4717-
visit(compound->getComponentRange().back());
4720+
visit(compound->getComponents().back());
47184721
return Action::SkipChildren();
47194722
}
47204723
// Arbitrary protocol constraints are OK on opaque types.
@@ -4795,7 +4798,7 @@ class ExistentialTypeVisitor
47954798
}
47964799
}
47974800

4798-
void visitIdentTypeRepr(IdentTypeRepr *T) {
4801+
void visitComponentIdentTypeRepr(ComponentIdentTypeRepr *T) {
47994802
if (T->isInvalid())
48004803
return;
48014804

@@ -4830,17 +4833,17 @@ class ExistentialTypeVisitor
48304833
if (needsParens)
48314834
OS << ")";
48324835

4833-
auto comp = T->getComponentRange().back();
4834-
if (auto *proto = dyn_cast_or_null<ProtocolDecl>(comp->getBoundDecl())) {
4836+
if (auto *proto = dyn_cast_or_null<ProtocolDecl>(T->getBoundDecl())) {
48354837
if (proto->existentialRequiresAny() && !Ctx.LangOpts.hasFeature(Feature::ImplicitSome)) {
4836-
Ctx.Diags.diagnose(comp->getNameLoc(),
4838+
Ctx.Diags.diagnose(T->getNameLoc(),
48374839
diag::existential_requires_any,
48384840
proto->getDeclaredInterfaceType(),
48394841
proto->getDeclaredExistentialType(),
48404842
/*isAlias=*/false)
48414843
.fixItReplace(replaceRepr->getSourceRange(), fix);
48424844
}
4843-
} else if (auto *alias = dyn_cast_or_null<TypeAliasDecl>(comp->getBoundDecl())) {
4845+
} else if (auto *alias =
4846+
dyn_cast_or_null<TypeAliasDecl>(T->getBoundDecl())) {
48444847
auto type = Type(alias->getDeclaredInterfaceType()->getDesugaredType());
48454848

48464849
// If this is a type alias to a constraint type, the type
@@ -4852,7 +4855,7 @@ class ExistentialTypeVisitor
48524855
if (!protoDecl->existentialRequiresAny() || Ctx.LangOpts.hasFeature(Feature::ImplicitSome))
48534856
continue;
48544857

4855-
Ctx.Diags.diagnose(comp->getNameLoc(),
4858+
Ctx.Diags.diagnose(T->getNameLoc(),
48564859
diag::existential_requires_any,
48574860
alias->getDeclaredInterfaceType(),
48584861
ExistentialType::get(alias->getDeclaredInterfaceType()),

lib/Sema/TypeChecker.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,12 @@ namespace {
475475

476476
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
477477
if (auto *ident = dyn_cast<IdentTypeRepr>(T)) {
478-
auto firstComponent = ident->getComponentRange().front();
479-
auto name = firstComponent->getNameRef().getBaseIdentifier();
478+
if (auto *base =
479+
dyn_cast<ComponentIdentTypeRepr>(ident->getBaseComponent())) {
480+
auto name = base->getNameRef().getBaseIdentifier();
480481
if (auto *paramDecl = params->lookUpGenericParam(name))
481-
firstComponent->setValue(paramDecl, dc);
482+
base->setValue(paramDecl, dc);
483+
}
482484
}
483485

484486
return Action::Continue();

0 commit comments

Comments
 (0)