Skip to content

Commit ec885b0

Browse files
committed
[NFC] const-qualify Inheritance-Clause-Bearing PointerUnion
1 parent d8092f4 commit ec885b0

12 files changed

+97
-101
lines changed

include/swift/AST/NameLookup.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -519,36 +519,34 @@ void recordLookupOfTopLevelName(DeclContext *topLevelContext, DeclName name,
519519
/// Add anything we find to the \c result vector. If we come across the
520520
/// AnyObject type, set \c anyObject true.
521521
void getDirectlyInheritedNominalTypeDecls(
522-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
523-
unsigned i,
524-
llvm::SmallVectorImpl<Located<NominalTypeDecl *>> &result,
522+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
523+
unsigned i, llvm::SmallVectorImpl<Located<NominalTypeDecl *>> &result,
525524
bool &anyObject);
526525

527526
/// Retrieve the set of nominal type declarations that are directly
528527
/// "inherited" by the given declaration, looking through typealiases
529528
/// and splitting out the components of compositions.
530529
///
531530
/// If we come across the AnyObject type, set \c anyObject true.
532-
SmallVector<Located<NominalTypeDecl *>, 4>
533-
getDirectlyInheritedNominalTypeDecls(
534-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
535-
bool &anyObject);
531+
SmallVector<Located<NominalTypeDecl *>, 4> getDirectlyInheritedNominalTypeDecls(
532+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
533+
bool &anyObject);
536534

537535
/// Retrieve the set of nominal type declarations that appear as the
538536
/// constraint type of any "Self" constraints in the where clause of the
539537
/// given protocol or protocol extension.
540538
SelfBounds getSelfBoundsFromWhereClause(
541-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl);
539+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl);
542540

543541
/// Retrieve the TypeLoc at the given \c index from among the set of
544542
/// type declarations that are directly "inherited" by the given declaration.
545-
inline TypeLoc &
546-
getInheritedTypeLocAtIndex(llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
547-
unsigned index) {
548-
if (auto typeDecl = decl.dyn_cast<TypeDecl *>())
543+
inline const TypeLoc &getInheritedTypeLocAtIndex(
544+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
545+
unsigned index) {
546+
if (auto typeDecl = decl.dyn_cast<const TypeDecl *>())
549547
return typeDecl->getInherited()[index];
550548

551-
return decl.get<ExtensionDecl *>()->getInherited()[index];
549+
return decl.get<const ExtensionDecl *>()->getInherited()[index];
552550
}
553551

554552
namespace namelookup {

include/swift/AST/NameLookupRequests.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ enum class ResolutionKind;
5151

5252
/// Display a nominal type or extension thereof.
5353
void simple_display(
54-
llvm::raw_ostream &out,
55-
const llvm::PointerUnion<TypeDecl *, ExtensionDecl *> &value);
54+
llvm::raw_ostream &out,
55+
const llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> &value);
5656

5757
/// Describes a set of type declarations that are "direct" referenced by
5858
/// a particular type in the AST.
@@ -76,12 +76,13 @@ using DirectlyReferencedTypeDecls = llvm::TinyPtrVector<TypeDecl *>;
7676
///
7777
/// The inherited declaration of \c D at index 0 is the class declaration C.
7878
/// The inherited declaration of \c D at index 1 is the typealias Alias.
79-
class InheritedDeclsReferencedRequest :
80-
public SimpleRequest<InheritedDeclsReferencedRequest,
81-
DirectlyReferencedTypeDecls(
82-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>,
83-
unsigned),
84-
RequestFlags::Uncached> // FIXME: Cache these
79+
class InheritedDeclsReferencedRequest
80+
: public SimpleRequest<
81+
InheritedDeclsReferencedRequest,
82+
DirectlyReferencedTypeDecls(
83+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
84+
unsigned),
85+
RequestFlags::Uncached> // FIXME: Cache these
8586
{
8687
public:
8788
using SimpleRequest::SimpleRequest;
@@ -90,10 +91,10 @@ class InheritedDeclsReferencedRequest :
9091
friend SimpleRequest;
9192

9293
// Evaluation.
93-
DirectlyReferencedTypeDecls evaluate(
94-
Evaluator &evaluator,
95-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
96-
unsigned index) const;
94+
DirectlyReferencedTypeDecls
95+
evaluate(Evaluator &evaluator,
96+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
97+
unsigned index) const;
9798

9899
public:
99100
// Caching
@@ -251,23 +252,23 @@ struct SelfBounds {
251252

252253
/// Request the nominal types that occur as the right-hand side of "Self: Foo"
253254
/// constraints in the "where" clause of a protocol extension.
254-
class SelfBoundsFromWhereClauseRequest :
255-
public SimpleRequest<SelfBoundsFromWhereClauseRequest,
256-
SelfBounds(llvm::PointerUnion<TypeDecl *,
257-
ExtensionDecl *>),
258-
RequestFlags::Uncached> {
255+
class SelfBoundsFromWhereClauseRequest
256+
: public SimpleRequest<SelfBoundsFromWhereClauseRequest,
257+
SelfBounds(llvm::PointerUnion<
258+
const TypeDecl *, const ExtensionDecl *>),
259+
RequestFlags::Uncached> {
259260
public:
260261
using SimpleRequest::SimpleRequest;
261262

262263
private:
263264
friend SimpleRequest;
264265

265266
// Evaluation.
266-
SelfBounds evaluate(Evaluator &evaluator,
267-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>) const;
267+
SelfBounds
268+
evaluate(Evaluator &evaluator,
269+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>) const;
268270
};
269271

270-
271272
/// Request all type aliases and nominal types that appear in the "where"
272273
/// clause of an extension.
273274
class TypeDeclsFromWhereClauseRequest :

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ SWIFT_REQUEST(NameLookup, GetDestructorRequest, DestructorDecl *(ClassDecl *),
4545
SeparatelyCached, NoLocationInfo)
4646
SWIFT_REQUEST(NameLookup, InheritedDeclsReferencedRequest,
4747
DirectlyReferencedTypeDecls(
48-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>, unsigned),
48+
llvm::PointerUnion<const TypeDecl *,
49+
const ExtensionDecl *>, unsigned),
4950
Uncached, HasNearestLocation)
5051
SWIFT_REQUEST(NameLookup, InheritedProtocolsRequest,
5152
ArrayRef<ProtocolDecl *>(ProtocolDecl *), SeparatelyCached,
@@ -68,7 +69,8 @@ SWIFT_REQUEST(NameLookup, QualifiedLookupRequest,
6869
DeclNameRef, NLOptions),
6970
Uncached, NoLocationInfo)
7071
SWIFT_REQUEST(NameLookup, SelfBoundsFromWhereClauseRequest,
71-
SelfBounds(llvm::PointerUnion<TypeDecl *, ExtensionDecl *>),
72+
SelfBounds(llvm::PointerUnion<const TypeDecl *,
73+
const ExtensionDecl *>),
7274
Uncached, NoLocationInfo)
7375
SWIFT_REQUEST(NameLookup, SuperclassDeclRequest, ClassDecl *(NominalTypeDecl *),
7476
SeparatelyCached, NoLocationInfo)

include/swift/AST/TypeCheckRequests.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,17 @@ class StorageImplInfo;
5858

5959
/// Display a nominal type or extension thereof.
6060
void simple_display(
61-
llvm::raw_ostream &out,
62-
const llvm::PointerUnion<TypeDecl *, ExtensionDecl *> &value);
61+
llvm::raw_ostream &out,
62+
const llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> &value);
6363

6464
/// Request the type from the ith entry in the inheritance clause for the
6565
/// given declaration.
66-
class InheritedTypeRequest :
67-
public SimpleRequest<InheritedTypeRequest,
68-
Type(llvm::PointerUnion<TypeDecl *, ExtensionDecl *>,
69-
unsigned,
70-
TypeResolutionStage),
71-
RequestFlags::SeparatelyCached>
72-
{
66+
class InheritedTypeRequest
67+
: public SimpleRequest<
68+
InheritedTypeRequest,
69+
Type(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
70+
unsigned, TypeResolutionStage),
71+
RequestFlags::SeparatelyCached> {
7372
public:
7473
using SimpleRequest::SimpleRequest;
7574

@@ -79,9 +78,8 @@ class InheritedTypeRequest :
7978
// Evaluation.
8079
Type
8180
evaluate(Evaluator &evaluator,
82-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
83-
unsigned index,
84-
TypeResolutionStage stage) const;
81+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
82+
unsigned index, TypeResolutionStage stage) const;
8583

8684
public:
8785
// Source location

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
105105
SmallVector<TypeLoc, 2>, bool),
106106
Cached, NoLocationInfo)
107107
SWIFT_REQUEST(TypeChecker, InheritedTypeRequest,
108-
Type(llvm::PointerUnion<TypeDecl *, ExtensionDecl *>, unsigned,
109-
TypeResolutionStage),
108+
Type(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
109+
unsigned, TypeResolutionStage),
110110
SeparatelyCached, HasNearestLocation)
111111
SWIFT_REQUEST(TypeChecker, InheritsSuperclassInitializersRequest,
112112
bool(ClassDecl *), SeparatelyCached, NoLocationInfo)

lib/AST/ConformanceLookupTable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ bool ConformanceLookupTable::addProtocol(ProtocolDecl *protocol, SourceLoc loc,
464464
}
465465

466466
void ConformanceLookupTable::addInheritedProtocols(
467-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
468-
ConformanceSource source) {
467+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
468+
ConformanceSource source) {
469469
// Find all of the protocols in the inheritance list.
470470
bool anyObject = false;
471471
for (const auto &found :

lib/AST/ConformanceLookupTable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ class ConformanceLookupTable {
327327

328328
/// Add the protocols from the given list.
329329
void addInheritedProtocols(
330-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
331-
ConformanceSource source);
330+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
331+
ConformanceSource source);
332332

333333
/// Expand the implied conformances for the given DeclContext.
334334
void expandImpliedConformances(NominalTypeDecl *nominal, DeclContext *dc);

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,8 +3768,8 @@ void GenericSignatureBuilder::addGenericParameter(GenericTypeParamType *GenericP
37683768
/// Visit all of the types that show up in the list of inherited
37693769
/// types.
37703770
static ConstraintResult visitInherited(
3771-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
3772-
llvm::function_ref<ConstraintResult(Type, const TypeRepr *)> visitType) {
3771+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
3772+
llvm::function_ref<ConstraintResult(Type, const TypeRepr *)> visitType) {
37733773
// Local function that (recursively) adds inherited types.
37743774
ConstraintResult result = ConstraintResult::Resolved;
37753775
std::function<void(Type, const TypeRepr *)> visitInherited;
@@ -3795,8 +3795,8 @@ static ConstraintResult visitInherited(
37953795
};
37963796

37973797
// Visit all of the inherited types.
3798-
auto typeDecl = decl.dyn_cast<TypeDecl *>();
3799-
auto extDecl = decl.dyn_cast<ExtensionDecl *>();
3798+
auto typeDecl = decl.dyn_cast<const TypeDecl *>();
3799+
auto extDecl = decl.dyn_cast<const ExtensionDecl *>();
38003800
ASTContext &ctx = typeDecl ? typeDecl->getASTContext()
38013801
: extDecl->getASTContext();
38023802
auto &evaluator = ctx.evaluator;

lib/AST/NameLookup.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,12 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
819819
SmallVectorImpl<ModuleDecl *> &modulesFound,
820820
bool &anyObject);
821821

822-
SelfBounds
823-
SelfBoundsFromWhereClauseRequest::evaluate(
822+
SelfBounds SelfBoundsFromWhereClauseRequest::evaluate(
824823
Evaluator &evaluator,
825-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl) const {
826-
auto *typeDecl = decl.dyn_cast<TypeDecl *>();
827-
auto *protoDecl = dyn_cast_or_null<ProtocolDecl>(typeDecl);
828-
auto *extDecl = decl.dyn_cast<ExtensionDecl *>();
824+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl) const {
825+
auto *typeDecl = decl.dyn_cast<const TypeDecl *>();
826+
auto *protoDecl = dyn_cast_or_null<const ProtocolDecl>(typeDecl);
827+
auto *extDecl = decl.dyn_cast<const ExtensionDecl *>();
829828

830829
DeclContext *dc = protoDecl ? (DeclContext *)protoDecl : (DeclContext *)extDecl;
831830

@@ -880,9 +879,9 @@ SelfBoundsFromWhereClauseRequest::evaluate(
880879
}
881880

882881
SelfBounds swift::getSelfBoundsFromWhereClause(
883-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl) {
884-
auto *typeDecl = decl.dyn_cast<TypeDecl *>();
885-
auto *extDecl = decl.dyn_cast<ExtensionDecl *>();
882+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl) {
883+
auto *typeDecl = decl.dyn_cast<const TypeDecl *>();
884+
auto *extDecl = decl.dyn_cast<const ExtensionDecl *>();
886885
auto &ctx = typeDecl ? typeDecl->getASTContext()
887886
: extDecl->getASTContext();
888887
return evaluateOrDefault(ctx.evaluator,
@@ -2139,18 +2138,18 @@ static DirectlyReferencedTypeDecls directReferencesForType(Type type) {
21392138

21402139
DirectlyReferencedTypeDecls InheritedDeclsReferencedRequest::evaluate(
21412140
Evaluator &evaluator,
2142-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
2141+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
21432142
unsigned index) const {
21442143

21452144
// Prefer syntactic information when we have it.
2146-
TypeLoc &typeLoc = getInheritedTypeLocAtIndex(decl, index);
2145+
const TypeLoc &typeLoc = getInheritedTypeLocAtIndex(decl, index);
21472146
if (auto typeRepr = typeLoc.getTypeRepr()) {
21482147
// Figure out the context in which name lookup will occur.
21492148
DeclContext *dc;
2150-
if (auto typeDecl = decl.dyn_cast<TypeDecl *>())
2149+
if (auto typeDecl = decl.dyn_cast<const TypeDecl *>())
21512150
dc = typeDecl->getInnermostDeclContext();
21522151
else
2153-
dc = decl.get<ExtensionDecl *>();
2152+
dc = (DeclContext *)decl.get<const ExtensionDecl *>();
21542153

21552154
return directReferencesForTypeRepr(evaluator, dc->getASTContext(), typeRepr,
21562155
dc);
@@ -2356,12 +2355,11 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
23562355
}
23572356

23582357
void swift::getDirectlyInheritedNominalTypeDecls(
2359-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
2360-
unsigned i,
2361-
llvm::SmallVectorImpl<Located<NominalTypeDecl *>> &result,
2358+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
2359+
unsigned i, llvm::SmallVectorImpl<Located<NominalTypeDecl *>> &result,
23622360
bool &anyObject) {
2363-
auto typeDecl = decl.dyn_cast<TypeDecl *>();
2364-
auto extDecl = decl.dyn_cast<ExtensionDecl *>();
2361+
auto typeDecl = decl.dyn_cast<const TypeDecl *>();
2362+
auto extDecl = decl.dyn_cast<const ExtensionDecl *>();
23652363

23662364
ASTContext &ctx = typeDecl ? typeDecl->getASTContext()
23672365
: extDecl->getASTContext();
@@ -2393,10 +2391,10 @@ void swift::getDirectlyInheritedNominalTypeDecls(
23932391

23942392
SmallVector<Located<NominalTypeDecl *>, 4>
23952393
swift::getDirectlyInheritedNominalTypeDecls(
2396-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
2397-
bool &anyObject) {
2398-
auto typeDecl = decl.dyn_cast<TypeDecl *>();
2399-
auto extDecl = decl.dyn_cast<ExtensionDecl *>();
2394+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> decl,
2395+
bool &anyObject) {
2396+
auto typeDecl = decl.dyn_cast<const TypeDecl *>();
2397+
auto extDecl = decl.dyn_cast<const ExtensionDecl *>();
24002398

24012399
// Gather results from all of the inherited types.
24022400
unsigned numInherited = typeDecl ? typeDecl->getInherited().size()

lib/AST/TypeCheckRequests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ namespace swift {
3737
}
3838

3939
void swift::simple_display(
40-
llvm::raw_ostream &out,
41-
const llvm::PointerUnion<TypeDecl *, ExtensionDecl *> &value) {
42-
if (auto type = value.dyn_cast<TypeDecl *>()) {
40+
llvm::raw_ostream &out,
41+
const llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> &value) {
42+
if (auto type = value.dyn_cast<const TypeDecl *>()) {
4343
type->dumpRef(out);
4444
return;
4545
}
4646

47-
auto ext = value.get<ExtensionDecl *>();
47+
auto ext = value.get<const ExtensionDecl *>();
4848
simple_display(out, ext);
4949
}
5050

@@ -116,7 +116,7 @@ void InheritedTypeRequest::cacheResult(Type value) const {
116116
const auto &storage = getStorage();
117117
auto &typeLoc = getInheritedTypeLocAtIndex(std::get<0>(storage),
118118
std::get<1>(storage));
119-
typeLoc.setType(value);
119+
const_cast<TypeLoc &>(typeLoc).setType(value);
120120
}
121121

122122
//----------------------------------------------------------------------------//

0 commit comments

Comments
 (0)