Skip to content

Commit b6e503d

Browse files
authored
Merge pull request #8044 from slavapestov/cantype-gardening
AST: Remove some unnecessary getCanonicalType() calls
2 parents 6902900 + 5036806 commit b6e503d

File tree

17 files changed

+94
-152
lines changed

17 files changed

+94
-152
lines changed

include/swift/AST/Type.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ class CanType : public Type {
348348
bool isActuallyCanonicalOrNull() const;
349349

350350
static bool isReferenceTypeImpl(CanType type, bool functionsCount);
351-
static bool isExistentialTypeImpl(CanType type);
352-
static bool isAnyExistentialTypeImpl(CanType type);
353351
static bool isExistentialTypeImpl(CanType type,
354352
SmallVectorImpl<ProtocolDecl*> &protocols);
355353
static bool isAnyExistentialTypeImpl(CanType type,
@@ -392,21 +390,11 @@ class CanType : public Type {
392390
return isReferenceTypeImpl(*this, /*functions count*/ false);
393391
}
394392

395-
/// Is this type existential?
396-
bool isExistentialType() const {
397-
return isExistentialTypeImpl(*this);
398-
}
399-
400393
/// Is this type existential?
401394
bool isExistentialType(SmallVectorImpl<ProtocolDecl *> &protocols) {
402395
return isExistentialTypeImpl(*this, protocols);
403396
}
404397

405-
/// Is this type an existential or an existential metatype?
406-
bool isAnyExistentialType() const {
407-
return isAnyExistentialTypeImpl(*this);
408-
}
409-
410398
/// Is this type an existential or an existential metatype?
411399
bool isAnyExistentialType(SmallVectorImpl<ProtocolDecl *> &protocols) {
412400
return isAnyExistentialTypeImpl(*this, protocols);
@@ -424,11 +412,7 @@ class CanType : public Type {
424412
return isObjCExistentialTypeImpl(*this);
425413
}
426414

427-
ClassDecl *getClassOrBoundGenericClass() const; // in Types.h
428-
StructDecl *getStructOrBoundGenericStruct() const; // in Types.h
429-
EnumDecl *getEnumOrBoundGenericEnum() const; // in Types.h
430415
NominalTypeDecl *getNominalOrBoundGenericNominal() const; // in Types.h
431-
CanType getNominalParent() const; // in Types.h
432416
NominalTypeDecl *getAnyNominal() const;
433417
GenericTypeDecl *getAnyGeneric() const;
434418

include/swift/AST/Types.h

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4383,46 +4383,29 @@ inline GenericTypeParamType *TypeBase::getRootGenericParam() {
43834383
}
43844384

43854385
inline bool TypeBase::isExistentialType() {
4386-
return getCanonicalType().isExistentialType();
4386+
return is<ProtocolType>() || is<ProtocolCompositionType>();
43874387
}
43884388

43894389
inline bool TypeBase::isAnyExistentialType() {
4390-
return getCanonicalType().isAnyExistentialType();
4391-
}
4392-
4393-
inline bool CanType::isExistentialTypeImpl(CanType type) {
4394-
return isa<ProtocolType>(type) || isa<ProtocolCompositionType>(type);
4395-
}
4396-
4397-
inline bool CanType::isAnyExistentialTypeImpl(CanType type) {
4398-
return isExistentialTypeImpl(type) || isa<ExistentialMetatypeType>(type);
4390+
return isExistentialType() || is<ExistentialMetatypeType>();
43994391
}
44004392

44014393
inline bool TypeBase::isClassExistentialType() {
4402-
CanType T = getCanonicalType();
4403-
if (auto pt = dyn_cast<ProtocolType>(T))
4394+
if (auto pt = getAs<ProtocolType>())
44044395
return pt->requiresClass();
4405-
if (auto pct = dyn_cast<ProtocolCompositionType>(T))
4396+
if (auto pct = getAs<ProtocolCompositionType>())
44064397
return pct->requiresClass();
44074398
return false;
44084399
}
44094400

44104401
inline bool TypeBase::isOpenedExistential() {
4411-
if (!hasOpenedExistential())
4412-
return false;
4413-
4414-
CanType T = getCanonicalType();
4415-
if (auto archetype = dyn_cast<ArchetypeType>(T))
4402+
if (auto archetype = getAs<ArchetypeType>())
44164403
return !archetype->getOpenedExistentialType().isNull();
44174404
return false;
44184405
}
44194406

44204407
inline bool TypeBase::isOpenedExistentialWithError() {
4421-
if (!hasOpenedExistential())
4422-
return false;
4423-
4424-
CanType T = getCanonicalType();
4425-
if (auto archetype = dyn_cast<ArchetypeType>(T)) {
4408+
if (auto archetype = getAs<ArchetypeType>()) {
44264409
auto openedExistentialType = archetype->getOpenedExistentialType();
44274410
return (!openedExistentialType.isNull() &&
44284411
openedExistentialType->isExistentialWithError());
@@ -4431,42 +4414,30 @@ inline bool TypeBase::isOpenedExistentialWithError() {
44314414
}
44324415

44334416
inline ClassDecl *TypeBase::getClassOrBoundGenericClass() {
4434-
return getCanonicalType().getClassOrBoundGenericClass();
4435-
}
4436-
4437-
inline ClassDecl *CanType::getClassOrBoundGenericClass() const {
4438-
if (auto classTy = dyn_cast<ClassType>(*this))
4417+
if (auto classTy = getAs<ClassType>())
44394418
return classTy->getDecl();
44404419

4441-
if (auto boundTy = dyn_cast<BoundGenericClassType>(*this))
4420+
if (auto boundTy = getAs<BoundGenericClassType>())
44424421
return boundTy->getDecl();
44434422

44444423
return nullptr;
44454424
}
44464425

44474426
inline StructDecl *TypeBase::getStructOrBoundGenericStruct() {
4448-
return getCanonicalType().getStructOrBoundGenericStruct();
4449-
}
4450-
4451-
inline StructDecl *CanType::getStructOrBoundGenericStruct() const {
4452-
if (auto structTy = dyn_cast<StructType>(*this))
4427+
if (auto structTy = getAs<StructType>())
44534428
return structTy->getDecl();
44544429

4455-
if (auto boundTy = dyn_cast<BoundGenericStructType>(*this))
4430+
if (auto boundTy = getAs<BoundGenericStructType>())
44564431
return boundTy->getDecl();
44574432

44584433
return nullptr;
44594434
}
44604435

44614436
inline EnumDecl *TypeBase::getEnumOrBoundGenericEnum() {
4462-
return getCanonicalType().getEnumOrBoundGenericEnum();
4463-
}
4464-
4465-
inline EnumDecl *CanType::getEnumOrBoundGenericEnum() const {
4466-
if (auto enumTy = dyn_cast<EnumType>(*this))
4437+
if (auto enumTy = getAs<EnumType>())
44674438
return enumTy->getDecl();
44684439

4469-
if (auto boundTy = dyn_cast<BoundGenericEnumType>(*this))
4440+
if (auto boundTy = getAs<BoundGenericEnumType>())
44704441
return boundTy->getDecl();
44714442

44724443
return nullptr;
@@ -4504,10 +4475,8 @@ inline GenericTypeDecl *TypeBase::getAnyGeneric() {
45044475
return getCanonicalType().getAnyGeneric();
45054476
}
45064477

4507-
4508-
45094478
inline bool TypeBase::isBuiltinIntegerType(unsigned n) {
4510-
if (auto intTy = dyn_cast<BuiltinIntegerType>(getCanonicalType()))
4479+
if (auto intTy = getAs<BuiltinIntegerType>())
45114480
return intTy->getWidth().isFixedWidth()
45124481
&& intTy->getWidth().getFixedWidth() == n;
45134482
return false;
@@ -4564,20 +4533,12 @@ inline CanType CanType::getLValueOrInOutObjectTypeImpl(CanType type) {
45644533
return type;
45654534
}
45664535

4567-
inline CanType CanType::getNominalParent() const {
4568-
if (auto classType = dyn_cast<NominalType>(*this)) {
4569-
return classType.getParent();
4570-
} else {
4571-
return cast<BoundGenericType>(*this).getParent();
4572-
}
4573-
}
4574-
45754536
inline bool TypeBase::mayHaveSuperclass() {
45764537
if (getClassOrBoundGenericClass())
45774538
return true;
45784539

45794540
if (auto archetype = getAs<ArchetypeType>())
4580-
return (bool)archetype->requiresClass();
4541+
return archetype->requiresClass();
45814542

45824543
return is<DynamicSelfType>();
45834544
}

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4158,7 +4158,7 @@ class InitExistentialMetatypeInst final
41584158
exType = exMetatype.getInstanceType();
41594159
concreteType = cast<MetatypeType>(concreteType).getInstanceType();
41604160
}
4161-
assert(exType.isExistentialType());
4161+
assert(exType->isExistentialType());
41624162
return concreteType;
41634163
}
41644164

include/swift/SIL/SILType.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,22 @@ class SILType {
193193
/// Retrieve the ClassDecl for a type that maps to a Swift class or
194194
/// bound generic class type.
195195
ClassDecl *getClassOrBoundGenericClass() const {
196-
return getSwiftRValueType().getClassOrBoundGenericClass();
196+
return getSwiftRValueType()->getClassOrBoundGenericClass();
197197
}
198198
/// Retrieve the StructDecl for a type that maps to a Swift struct or
199199
/// bound generic struct type.
200200
StructDecl *getStructOrBoundGenericStruct() const {
201-
return getSwiftRValueType().getStructOrBoundGenericStruct();
201+
return getSwiftRValueType()->getStructOrBoundGenericStruct();
202202
}
203203
/// Retrieve the EnumDecl for a type that maps to a Swift enum or
204204
/// bound generic enum type.
205205
EnumDecl *getEnumOrBoundGenericEnum() const {
206-
return getSwiftRValueType().getEnumOrBoundGenericEnum();
206+
return getSwiftRValueType()->getEnumOrBoundGenericEnum();
207207
}
208208
/// Retrieve the NominalTypeDecl for a type that maps to a Swift
209209
/// nominal or bound generic nominal type.
210210
NominalTypeDecl *getNominalOrBoundGenericNominal() const {
211-
return getSwiftRValueType().getNominalOrBoundGenericNominal();
211+
return getSwiftRValueType()->getNominalOrBoundGenericNominal();
212212
}
213213

214214
/// True if the type is an address type.
@@ -284,11 +284,11 @@ class SILType {
284284
}
285285
/// Returns true if the referenced type is an existential type.
286286
bool isExistentialType() const {
287-
return getSwiftRValueType().isExistentialType();
287+
return getSwiftRValueType()->isExistentialType();
288288
}
289289
/// Returns true if the referenced type is any kind of existential type.
290290
bool isAnyExistentialType() const {
291-
return getSwiftRValueType().isAnyExistentialType();
291+
return getSwiftRValueType()->isAnyExistentialType();
292292
}
293293
/// Returns true if the referenced type is a class existential type.
294294
bool isClassExistentialType() const {

lib/AST/Type.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ bool CanType::isExistentialTypeImpl(CanType type,
236236
return true;
237237
}
238238

239-
assert(!type.isExistentialType());
240239
return false;
241240
}
242241

@@ -263,7 +262,7 @@ bool TypeBase::isObjCExistentialType() {
263262
}
264263

265264
bool CanType::isObjCExistentialTypeImpl(CanType type) {
266-
if (!type.isExistentialType()) return false;
265+
if (!type->isExistentialType()) return false;
267266

268267
SmallVector<ProtocolDecl *, 4> protocols;
269268
type.getAnyExistentialTypeProtocols(protocols);

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class CategoryInitializerVisitor
129129
classMetadata = llvm::ConstantExpr::getBitCast(classMetadata,
130130
IGM.ObjCClassPtrTy);
131131
metaclassMetadata = IGM.getAddrOfMetaclassObject(
132-
origTy.getClassOrBoundGenericClass(),
132+
origTy->getClassOrBoundGenericClass(),
133133
NotForDefinition);
134134
metaclassMetadata = llvm::ConstantExpr::getBitCast(metaclassMetadata,
135135
IGM.ObjCClassPtrTy);

lib/IRGen/GenMeta.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static llvm::Value *emitNominalMetadataRef(IRGenFunction &IGF,
407407

408408

409409
bool irgen::hasKnownSwiftMetadata(IRGenModule &IGM, CanType type) {
410-
if (ClassDecl *theClass = type.getClassOrBoundGenericClass()) {
410+
if (ClassDecl *theClass = type->getClassOrBoundGenericClass()) {
411411
return hasKnownSwiftMetadata(IGM, theClass);
412412
}
413413

@@ -3667,7 +3667,7 @@ namespace {
36673667
}
36683668

36693669
void addParentMetadataRef(ClassDecl *forClass, Type classType) {
3670-
CanType parentType = classType->getCanonicalType().getNominalParent();
3670+
CanType parentType = classType->getNominalParent()->getCanonicalType();
36713671

36723672
if (auto metadata =
36733673
tryEmitConstantTypeMetadataRef(IGM, parentType,
@@ -3781,7 +3781,7 @@ namespace {
37813781
}
37823782

37833783
void addParentMetadataRef(ClassDecl *forClass, Type classType) {
3784-
CanType parentType = classType->getCanonicalType().getNominalParent();
3784+
CanType parentType = classType->getNominalParent()->getCanonicalType();
37853785
this->addFillOp(parentType, None, /*relative*/ false);
37863786
B.addNullPointer(IGM.TypeMetadataPtrTy);
37873787
}
@@ -4487,7 +4487,7 @@ llvm::Value *irgen::emitHeapMetadataRefForHeapObject(IRGenFunction &IGF,
44874487
llvm::Value *object,
44884488
CanType objectType,
44894489
bool suppressCast) {
4490-
ClassDecl *theClass = objectType.getClassOrBoundGenericClass();
4490+
ClassDecl *theClass = objectType->getClassOrBoundGenericClass();
44914491
if (theClass && isKnownNotTaggedPointer(IGF.IGM, theClass))
44924492
return emitLoadOfHeapMetadataRef(IGF, object,
44934493
getIsaEncodingForType(IGF.IGM, objectType),
@@ -5226,8 +5226,8 @@ namespace {
52265226
Size AddressPoint = Size::invalid();
52275227

52285228
bool computeUnfilledParent() {
5229-
if (auto parentType = asImpl().getTargetType().getNominalParent()) {
5230-
return !tryEmitConstantTypeMetadataRef(IGM, parentType,
5229+
if (auto parentType = asImpl().getTargetType()->getNominalParent()) {
5230+
return !tryEmitConstantTypeMetadataRef(IGM, parentType->getCanonicalType(),
52315231
SymbolReferenceKind::Absolute);
52325232
}
52335233
return false;
@@ -5364,7 +5364,7 @@ namespace {
53645364
}
53655365
void emitInitialization(IRGenFunction &IGF, llvm::Value *metadata) {
53665366
if (HasUnfilledParent) {
5367-
auto parentType = getTargetType().getNominalParent();
5367+
auto parentType = getTargetType()->getNominalParent()->getCanonicalType();
53685368
auto parentMetadata = IGF.emitTypeMetadataRef(parentType);
53695369

53705370
int index = ValueTypeParentIndex;
@@ -5410,7 +5410,7 @@ namespace {
54105410
}
54115411
void emitInitialization(IRGenFunction &IGF, llvm::Value *metadata) {
54125412
if (HasUnfilledParent) {
5413-
auto parentType = getTargetType().getNominalParent();
5413+
auto parentType = getTargetType()->getNominalParent()->getCanonicalType();
54145414
auto parentMetadata = IGF.emitTypeMetadataRef(parentType);
54155415

54165416
int index = ValueTypeParentIndex;

lib/IRGen/GenObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ static void emitSuperArgument(IRGenFunction &IGF, bool isInstanceMethod,
602602
} else {
603603
ClassDecl *searchClassDecl =
604604
searchClass.castTo<MetatypeType>().getInstanceType()
605-
.getClassOrBoundGenericClass();
605+
->getClassOrBoundGenericClass();
606606
searchValue = IGF.IGM.getAddrOfMetaclassObject(searchClassDecl,
607607
NotForDefinition);
608608
}

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ void EmitPolymorphicParameters::bindExtraSource(const MetadataSource &source,
496496
auto selfTy = FnType->getSelfInstanceType();
497497
CanType argTy = getTypeInContext(selfTy);
498498
setTypeMetadataName(IGF.IGM, metadata, argTy);
499-
auto *CD = selfTy.getClassOrBoundGenericClass();
499+
auto *CD = selfTy->getClassOrBoundGenericClass();
500500
// The self metadata here corresponds to the conforming type.
501501
// For an inheritable conformance, that may be a subclass of the static
502502
// type, and so the self metadata will be inexact. Currently, all

0 commit comments

Comments
 (0)