Skip to content

Commit 115244f

Browse files
authored
Merge pull request swiftlang#33091 from CodaFi/const-ellation
2 parents 8edb812 + ec885b0 commit 115244f

14 files changed

+106
-108
lines changed

include/swift/AST/Initializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class PatternBindingInitializer : public Initializer {
104104

105105
/// If this initializes a single @lazy variable, lazily create a self
106106
/// declaration for it to refer to.
107-
ParamDecl *getImplicitSelfDecl();
107+
ParamDecl *getImplicitSelfDecl() const;
108108

109109
static bool classof(const DeclContext *DC) {
110110
if (auto init = dyn_cast<Initializer>(DC))

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/Decl.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ PatternBindingDecl *PatternBindingDecl::createDeserialized(
15431543
return PBD;
15441544
}
15451545

1546-
ParamDecl *PatternBindingInitializer::getImplicitSelfDecl() {
1546+
ParamDecl *PatternBindingInitializer::getImplicitSelfDecl() const {
15471547
if (SelfParam)
15481548
return SelfParam;
15491549

@@ -1555,12 +1555,14 @@ ParamDecl *PatternBindingInitializer::getImplicitSelfDecl() {
15551555
: ParamSpecifier::InOut);
15561556

15571557
ASTContext &C = DC->getASTContext();
1558-
SelfParam = new (C) ParamDecl(SourceLoc(), SourceLoc(),
1558+
auto *mutableThis = const_cast<PatternBindingInitializer *>(this);
1559+
auto *LazySelfParam = new (C) ParamDecl(SourceLoc(), SourceLoc(),
15591560
Identifier(), singleVar->getLoc(),
1560-
C.Id_self, this);
1561-
SelfParam->setImplicit();
1562-
SelfParam->setSpecifier(specifier);
1563-
SelfParam->setInterfaceType(DC->getSelfInterfaceType());
1561+
C.Id_self, mutableThis);
1562+
LazySelfParam->setImplicit();
1563+
LazySelfParam->setSpecifier(specifier);
1564+
LazySelfParam->setInterfaceType(DC->getSelfInterfaceType());
1565+
mutableThis->SelfParam = LazySelfParam;
15641566
}
15651567
}
15661568

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;

0 commit comments

Comments
 (0)