Skip to content

Commit 4fdf16d

Browse files
committed
SIL: add CanonicalType.canBeClass
And move the implementation of `SIL.Type.canBeClass` to the AST Type. The SIL Type just calls the AST Type implementation. Also rename `SIL.Type.canonicalASTType` -> `SIL.Type.astType`.
1 parent cd7c533 commit 4fdf16d

File tree

8 files changed

+41
-27
lines changed

8 files changed

+41
-27
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public struct Type: CustomStringConvertible, NoReflectionChildren {
4646
/// A Type that is statically known to be canonical.
4747
/// For example, typealiases are resolved.
4848
public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
49+
public enum TraitResult {
50+
case isNot
51+
case canBe
52+
case `is`
53+
}
54+
4955
public let bridged: BridgedCanType
5056

5157
public init(bridged: BridgedCanType) { self.bridged = bridged }
@@ -63,6 +69,8 @@ public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
6369
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
6470
return type.subst(with: substitutionMap).canonical
6571
}
72+
73+
public var canBeClass: TraitResult { bridged.canBeClass().result }
6674
}
6775

6876
public struct TypeArray : RandomAccessCollection, CustomReflectable {
@@ -84,3 +92,15 @@ public struct TypeArray : RandomAccessCollection, CustomReflectable {
8492
return Mirror(self, children: c)
8593
}
8694
}
95+
96+
extension BridgedCanType.TraitResult {
97+
var result: CanonicalType.TraitResult {
98+
switch self {
99+
case .IsNot: return .isNot
100+
case .CanBe: return .canBe
101+
case .Is: return .is
102+
default:
103+
fatalError("wrong type TraitResult enum case")
104+
}
105+
}
106+
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,16 @@ private extension BuiltinInst {
129129
}
130130

131131
func optimizeCanBeClass(_ context: SimplifyContext) {
132-
guard let ty = substitutionMap.replacementTypes[0].canonical.silType else {
133-
return
134-
}
135132
let literal: IntegerLiteralInst
136-
switch ty.canBeClass {
137-
case .IsNot:
133+
switch substitutionMap.replacementType.canonical.canBeClass {
134+
case .isNot:
138135
let builder = Builder(before: self, context)
139136
literal = builder.createIntegerLiteral(0, type: type)
140-
case .Is:
137+
case .is:
141138
let builder = Builder(before: self, context)
142139
literal = builder.createIntegerLiteral(1, type: type)
143-
case .CanBe:
140+
case .canBe:
144141
return
145-
default:
146-
fatalError()
147142
}
148143
self.replace(with: literal, context)
149144
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
2727
public var addressType: Type { bridged.getAddressType().type }
2828
public var objectType: Type { bridged.getObjectType().type }
2929

30-
public var canonicalASTType: CanonicalType { CanonicalType(bridged: bridged.getCanType()) }
30+
public var astType: CanonicalType { CanonicalType(bridged: bridged.getCanType()) }
3131

3232
public func isTrivial(in function: Function) -> Bool {
3333
return bridged.isTrivial(function.bridged)
@@ -68,7 +68,7 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
6868
public var isThickFunction: Bool { bridged.isThickFunction() }
6969
public var isAsyncFunction: Bool { bridged.isAsyncFunction() }
7070

71-
public var canBeClass: BridgedType.TraitResult { bridged.canBeClass() }
71+
public var canBeClass: CanonicalType.TraitResult { astType.canBeClass }
7272

7373
public var isMoveOnly: Bool { bridged.isMoveOnly() }
7474

include/swift/AST/ASTBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,9 +2288,16 @@ class BridgedCanType {
22882288
swift::TypeBase * _Nullable type;
22892289

22902290
public:
2291+
enum class TraitResult {
2292+
IsNot,
2293+
CanBe,
2294+
Is
2295+
};
2296+
22912297
BRIDGED_INLINE BridgedCanType(swift::CanType ty);
22922298
BRIDGED_INLINE swift::CanType unbridged() const;
22932299
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getType() const;
2300+
BRIDGED_INLINE TraitResult canBeClass() const;
22942301
};
22952302

22962303
struct BridgedASTTypeArray {

include/swift/AST/ASTBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ BridgedASTType BridgedASTType::subst(BridgedSubstitutionMap substMap) const {
327327
// MARK: BridgedCanType
328328
//===----------------------------------------------------------------------===//
329329

330+
static_assert((int)BridgedCanType::TraitResult::IsNot == (int)swift::TypeTraitResult::IsNot);
331+
static_assert((int)BridgedCanType::TraitResult::CanBe == (int)swift::TypeTraitResult::CanBe);
332+
static_assert((int)BridgedCanType::TraitResult::Is == (int)swift::TypeTraitResult::Is);
333+
330334
BridgedCanType::BridgedCanType(swift::CanType ty) : type(ty.getPointer()) {
331335
}
332336

@@ -338,6 +342,10 @@ BridgedASTType BridgedCanType::getType() const {
338342
return {type};
339343
}
340344

345+
BridgedCanType::TraitResult BridgedCanType::canBeClass() const {
346+
return (TraitResult)unbridged()->canBeClass();
347+
}
348+
341349
//===----------------------------------------------------------------------===//
342350
// MARK: BridgedASTTypeArray
343351
//===----------------------------------------------------------------------===//

include/swift/SIL/SILBridging.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,6 @@ struct BridgedType {
228228
ObjC
229229
};
230230

231-
enum class TraitResult {
232-
IsNot,
233-
CanBe,
234-
Is
235-
};
236-
237231
struct EnumElementIterator {
238232
uint64_t storage[4];
239233

@@ -277,7 +271,6 @@ struct BridgedType {
277271
BRIDGED_INLINE bool isAsyncFunction() const;
278272
BRIDGED_INLINE bool isVoid() const;
279273
BRIDGED_INLINE bool isEmpty(BridgedFunction f) const;
280-
BRIDGED_INLINE TraitResult canBeClass() const;
281274
BRIDGED_INLINE bool isMoveOnly() const;
282275
BRIDGED_INLINE bool isEscapable(BridgedFunction f) const;
283276
BRIDGED_INLINE bool isOrContainsObjectiveCClass() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,6 @@ bool BridgedType::isEmpty(BridgedFunction f) const {
421421
return unbridged().isEmpty(*f.getFunction());
422422
}
423423

424-
BridgedType::TraitResult BridgedType::canBeClass() const {
425-
return (TraitResult)unbridged().canBeClass();
426-
}
427-
428424
bool BridgedType::isMoveOnly() const {
429425
return unbridged().isMoveOnly();
430426
}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ static_assert((int)BridgedType::MetatypeRepresentation::Thin == (int)swift::Meta
170170
static_assert((int)BridgedType::MetatypeRepresentation::Thick == (int)swift::MetatypeRepresentation::Thick);
171171
static_assert((int)BridgedType::MetatypeRepresentation::ObjC == (int)swift::MetatypeRepresentation::ObjC);
172172

173-
static_assert((int)BridgedType::TraitResult::IsNot == (int)swift::TypeTraitResult::IsNot);
174-
static_assert((int)BridgedType::TraitResult::CanBe == (int)swift::TypeTraitResult::CanBe);
175-
static_assert((int)BridgedType::TraitResult::Is == (int)swift::TypeTraitResult::Is);
176-
177-
178173
//===----------------------------------------------------------------------===//
179174
// SILFunction
180175
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)