Skip to content

Commit 09db813

Browse files
Merge pull request #31740 from AnthonyLatsis/genericsigimpl-constqual
[NFC] AST: Const-qualify GenericSignatureImpl
2 parents 554ecf6 + df9103e commit 09db813

File tree

8 files changed

+82
-91
lines changed

8 files changed

+82
-91
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class GenericTypeParamType;
101101
/// both the generic type parameters and the requirements placed on those
102102
/// generic parameters.
103103
class GenericSignature {
104-
GenericSignatureImpl *Ptr;
104+
const GenericSignatureImpl *Ptr;
105105

106106
public:
107107
/// Create a new generic signature with the given type parameters and
@@ -118,13 +118,13 @@ class GenericSignature {
118118
ArrayRef<Requirement> requirements);
119119

120120
public:
121-
/*implicit*/ GenericSignature(GenericSignatureImpl *P = 0) : Ptr(P) {}
121+
/*implicit*/ GenericSignature(const GenericSignatureImpl *P = 0) : Ptr(P) {}
122122

123-
GenericSignatureImpl *getPointer() const { return Ptr; }
123+
const GenericSignatureImpl *getPointer() const { return Ptr; }
124124

125125
bool isNull() const { return Ptr == 0; }
126126

127-
GenericSignatureImpl *operator->() const { return Ptr; }
127+
const GenericSignatureImpl *operator->() const { return Ptr; }
128128

129129
explicit operator bool() const { return Ptr != 0; }
130130

@@ -175,7 +175,7 @@ class CanGenericSignature : public GenericSignature {
175175
public:
176176
CanGenericSignature(std::nullptr_t) : GenericSignature(nullptr) {}
177177

178-
explicit CanGenericSignature(GenericSignatureImpl *P = 0)
178+
explicit CanGenericSignature(const GenericSignatureImpl *P = 0)
179179
: GenericSignature(P) {
180180
assert(isActuallyCanonicalOrNull() &&
181181
"Forming a CanGenericSignature out of a non-canonical signature!");
@@ -204,8 +204,8 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
204204
GenericSignatureImpl(const GenericSignatureImpl&) = delete;
205205
void operator=(const GenericSignatureImpl&) = delete;
206206

207-
unsigned NumGenericParams;
208-
unsigned NumRequirements;
207+
const unsigned NumGenericParams;
208+
const unsigned NumRequirements;
209209

210210
GenericEnvironment *GenericEnv = nullptr;
211211

@@ -220,39 +220,29 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
220220
return NumRequirements;
221221
}
222222

223-
/// Retrieve a mutable version of the generic parameters.
224-
MutableArrayRef<Type> getGenericParamsBuffer() {
225-
return {getTrailingObjects<Type>(), NumGenericParams};
226-
}
227-
228-
/// Retrieve a mutable version of the requirements.
229-
MutableArrayRef<Requirement> getRequirementsBuffer() {
230-
return {getTrailingObjects<Requirement>(), NumRequirements};
231-
}
232-
233223
GenericSignatureImpl(TypeArrayView<GenericTypeParamType> params,
234224
ArrayRef<Requirement> requirements,
235225
bool isKnownCanonical);
236226

237227
// FIXME: Making this a CanGenericSignature reveals callers are violating
238228
// the interface's invariants.
239-
mutable llvm::PointerUnion<GenericSignatureImpl *, ASTContext *>
229+
mutable llvm::PointerUnion<const GenericSignatureImpl *, ASTContext *>
240230
CanonicalSignatureOrASTContext;
241231

242232
void buildConformanceAccessPath(
243233
SmallVectorImpl<ConformanceAccessPath::Entry> &path,
244234
ArrayRef<Requirement> reqs,
245235
const void /*GenericSignatureBuilder::RequirementSource*/ *source,
246236
ProtocolDecl *conformingProto, Type rootType,
247-
ProtocolDecl *requirementSignatureProto);
237+
ProtocolDecl *requirementSignatureProto) const;
248238

249239
friend class ArchetypeType;
250240

251241
public:
252242
/// Retrieve the generic parameters.
253243
TypeArrayView<GenericTypeParamType> getGenericParams() const {
254-
auto temp = const_cast<GenericSignatureImpl *>(this);
255-
return TypeArrayView<GenericTypeParamType>(temp->getGenericParamsBuffer());
244+
return TypeArrayView<GenericTypeParamType>(
245+
{getTrailingObjects<Type>(), NumGenericParams});
256246
}
257247

258248
/// Retrieve the innermost generic parameters.
@@ -263,7 +253,7 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
263253

264254
/// Retrieve the requirements.
265255
ArrayRef<Requirement> getRequirements() const {
266-
return const_cast<GenericSignatureImpl *>(this)->getRequirementsBuffer();
256+
return {getTrailingObjects<Requirement>(), NumRequirements};
267257
}
268258

269259
/// Only allow allocation by doing a placement new.
@@ -299,7 +289,7 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
299289
}
300290

301291
/// Return true if these two generic signatures are equal.
302-
bool isEqual(GenericSignature Other);
292+
bool isEqual(GenericSignature Other) const;
303293

304294
/// Determines whether this GenericSignature is canonical.
305295
bool isCanonical() const;
@@ -310,55 +300,55 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
310300
CanGenericSignature getCanonicalSignature() const;
311301

312302
/// Retrieve the generic signature builder for the given generic signature.
313-
GenericSignatureBuilder *getGenericSignatureBuilder();
303+
GenericSignatureBuilder *getGenericSignatureBuilder() const;
314304

315305
/// Returns the generic environment that provides fresh contextual types
316306
/// (archetypes) that correspond to the interface types in this generic
317307
/// signature.
318-
GenericEnvironment *getGenericEnvironment();
308+
GenericEnvironment *getGenericEnvironment() const;
319309

320310
/// Uniquing for the ASTContext.
321-
void Profile(llvm::FoldingSetNodeID &ID) {
311+
void Profile(llvm::FoldingSetNodeID &ID) const {
322312
Profile(ID, getGenericParams(), getRequirements());
323313
}
324314

325315
/// Determine whether the given dependent type is required to be a class.
326-
bool requiresClass(Type type);
316+
bool requiresClass(Type type) const;
327317

328318
/// Determine the superclass bound on the given dependent type.
329-
Type getSuperclassBound(Type type);
319+
Type getSuperclassBound(Type type) const;
330320

331321
/// Determine the set of protocols to which the given dependent type
332322
/// must conform.
333-
GenericSignature::ConformsToArray getConformsTo(Type type);
323+
GenericSignature::ConformsToArray getConformsTo(Type type) const;
334324

335325
/// Determine whether the given dependent type conforms to this protocol.
336-
bool conformsToProtocol(Type type, ProtocolDecl *proto);
326+
bool conformsToProtocol(Type type, ProtocolDecl *proto) const;
337327

338328
/// Determine whether the given dependent type is equal to a concrete type.
339-
bool isConcreteType(Type type);
329+
bool isConcreteType(Type type) const;
340330

341331
/// Return the concrete type that the given dependent type is constrained to,
342332
/// or the null Type if it is not the subject of a concrete same-type
343333
/// constraint.
344-
Type getConcreteType(Type type);
334+
Type getConcreteType(Type type) const;
345335

346336
/// Return the layout constraint that the given dependent type is constrained
347337
/// to, or the null LayoutConstraint if it is not the subject of layout
348338
/// constraint.
349-
LayoutConstraint getLayoutConstraint(Type type);
339+
LayoutConstraint getLayoutConstraint(Type type) const;
350340

351341
/// Return whether two type parameters represent the same type under this
352342
/// generic signature.
353343
///
354344
/// The type parameters must be known to not be concrete within the context.
355-
bool areSameTypeParameterInContext(Type type1, Type type2);
345+
bool areSameTypeParameterInContext(Type type1, Type type2) const;
356346

357347
/// Determine if \c sig can prove \c requirement, meaning that it can deduce
358348
/// T: Foo or T == U (etc.) with the information it knows. This includes
359349
/// checking against global state, if any/all of the types in the requirement
360350
/// are concrete, not type parameters.
361-
bool isRequirementSatisfied(Requirement requirement);
351+
bool isRequirementSatisfied(Requirement requirement) const;
362352

363353
/// Return the requirements of this generic signature that are not also
364354
/// satisfied by \c otherSig.
@@ -370,14 +360,15 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
370360

371361
/// Return the canonical version of the given type under this generic
372362
/// signature.
373-
CanType getCanonicalTypeInContext(Type type);
374-
bool isCanonicalTypeInContext(Type type);
363+
CanType getCanonicalTypeInContext(Type type) const;
364+
bool isCanonicalTypeInContext(Type type) const;
375365

376366
/// Return the canonical version of the given type under this generic
377367
/// signature.
378368
CanType getCanonicalTypeInContext(Type type,
379-
GenericSignatureBuilder &builder);
380-
bool isCanonicalTypeInContext(Type type, GenericSignatureBuilder &builder);
369+
GenericSignatureBuilder &builder) const;
370+
bool isCanonicalTypeInContext(Type type,
371+
GenericSignatureBuilder &builder) const;
381372

382373
/// Retrieve the conformance access path used to extract the conformance of
383374
/// interface \c type to the given \c protocol.
@@ -392,14 +383,14 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
392383
///
393384
/// \seealso ConformanceAccessPath
394385
ConformanceAccessPath getConformanceAccessPath(Type type,
395-
ProtocolDecl *protocol);
386+
ProtocolDecl *protocol) const;
396387

397388
/// Get the ordinal of a generic parameter in this generic signature.
398389
///
399390
/// For example, if you have a generic signature for a nested context like:
400391
/// <t_0_0, t_0_1, t_1_0>
401392
/// then this will return 0 for t_0_0, 1 for t_0_1, and 2 for t_1_0.
402-
unsigned getGenericParamOrdinal(GenericTypeParamType *param);
393+
unsigned getGenericParamOrdinal(GenericTypeParamType *param) const;
403394

404395
/// Get a substitution map that maps all of the generic signature's
405396
/// generic parameters to themselves.
@@ -440,7 +431,7 @@ static inline raw_ostream &operator<<(raw_ostream &OS,
440431

441432
// A GenericSignature casts like a GenericSignatureImpl*.
442433
template <> struct simplify_type<const ::swift::GenericSignature> {
443-
typedef ::swift::GenericSignatureImpl *SimpleType;
434+
typedef const ::swift::GenericSignatureImpl *SimpleType;
444435
static SimpleType getSimplifiedValue(const ::swift::GenericSignature &Val) {
445436
return Val.getPointer();
446437
}

include/swift/AST/Type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ SourceLoc extractNearestSourceLoc(Type ty);
384384
class CanType : public Type {
385385
bool isActuallyCanonicalOrNull() const;
386386

387-
static bool isReferenceTypeImpl(CanType type, GenericSignatureImpl *sig,
387+
static bool isReferenceTypeImpl(CanType type, const GenericSignatureImpl *sig,
388388
bool functionsCount);
389389
static bool isExistentialTypeImpl(CanType type);
390390
static bool isAnyExistentialTypeImpl(CanType type);
@@ -436,7 +436,7 @@ class CanType : public Type {
436436
/// - existentials with class or class protocol bounds
437437
/// But not:
438438
/// - function types
439-
bool allowsOwnership(GenericSignatureImpl *sig) const {
439+
bool allowsOwnership(const GenericSignatureImpl *sig) const {
440440
return isReferenceTypeImpl(*this, sig,
441441
/*functions count*/ false);
442442
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ void simple_display(llvm::raw_ostream &out, AncestryFlags value);
11141114

11151115
class AbstractGenericSignatureRequest :
11161116
public SimpleRequest<AbstractGenericSignatureRequest,
1117-
GenericSignature (GenericSignatureImpl *,
1117+
GenericSignature (const GenericSignatureImpl *,
11181118
SmallVector<GenericTypeParamType *, 2>,
11191119
SmallVector<Requirement, 2>),
11201120
RequestFlags::Cached> {
@@ -1127,7 +1127,7 @@ class AbstractGenericSignatureRequest :
11271127
// Evaluation.
11281128
GenericSignature
11291129
evaluate(Evaluator &evaluator,
1130-
GenericSignatureImpl *baseSignature,
1130+
const GenericSignatureImpl *baseSignature,
11311131
SmallVector<GenericTypeParamType *, 2> addedParameters,
11321132
SmallVector<Requirement, 2> addedRequirements) const;
11331133

@@ -1144,7 +1144,7 @@ class AbstractGenericSignatureRequest :
11441144
class InferredGenericSignatureRequest :
11451145
public SimpleRequest<InferredGenericSignatureRequest,
11461146
GenericSignature (ModuleDecl *,
1147-
GenericSignatureImpl *,
1147+
const GenericSignatureImpl *,
11481148
GenericParamSource,
11491149
SmallVector<Requirement, 2>,
11501150
SmallVector<TypeLoc, 2>,
@@ -1160,7 +1160,7 @@ class InferredGenericSignatureRequest :
11601160
GenericSignature
11611161
evaluate(Evaluator &evaluator,
11621162
ModuleDecl *module,
1163-
GenericSignatureImpl *baseSignature,
1163+
const GenericSignatureImpl *baseSignature,
11641164
GenericParamSource paramSource,
11651165
SmallVector<Requirement, 2> addedRequirements,
11661166
SmallVector<TypeLoc, 2> inferenceSources,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
SWIFT_REQUEST(TypeChecker, AbstractGenericSignatureRequest,
19-
GenericSignature (GenericSignatureImpl *,
19+
GenericSignature (const GenericSignatureImpl *,
2020
SmallVector<GenericTypeParamType *, 2>,
2121
SmallVector<Requirement, 2>),
2222
Cached, NoLocationInfo)
@@ -92,7 +92,7 @@ SWIFT_REQUEST(TypeChecker, HasDynamicCallableAttributeRequest,
9292
SWIFT_REQUEST(TypeChecker, HasImplementationOnlyImportsRequest,
9393
bool(SourceFile *), Cached, NoLocationInfo)
9494
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
95-
GenericSignature (ModuleDecl *, GenericSignatureImpl *,
95+
GenericSignature (ModuleDecl *, const GenericSignatureImpl *,
9696
GenericParamSource,
9797
SmallVector<Requirement, 2>,
9898
SmallVector<TypeLoc, 2>, bool),

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
558558

559559
/// allowsOwnership() - Are variables of this type permitted to have
560560
/// ownership attributes?
561-
bool allowsOwnership(GenericSignatureImpl *sig = nullptr);
561+
bool allowsOwnership(const GenericSignatureImpl *sig = nullptr);
562562

563563
/// Determine whether this type involves a type variable.
564564
bool hasTypeVariable() const {

0 commit comments

Comments
 (0)