Skip to content

Commit eadfbf6

Browse files
committed
Swift AST: add some APIs to Type and CanonicalType
1 parent 48db890 commit eadfbf6

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,34 @@ public struct Type: CustomStringConvertible, NoReflectionChildren {
3333
public var description: String { String(taking: bridged.getDebugDescription()) }
3434

3535
public var hasTypeParameter: Bool { bridged.hasTypeParameter() }
36+
public var hasOpenedExistential: Bool { bridged.hasOpenedExistential() }
3637
public var isOpenedExistentialWithError: Bool { bridged.isOpenedExistentialWithError() }
3738
public var isEscapable: Bool { bridged.isEscapable() }
3839
public var isNoEscape: Bool { bridged.isNoEscape() }
3940
public var isInteger: Bool { bridged.isInteger() }
41+
public var isMetatypeType: Bool { bridged.isMetatypeType() }
42+
public var isExistentialMetatypeType: Bool { bridged.isExistentialMetatypeType() }
4043

44+
public var anyNominal: NominalTypeDecl? { bridged.getAnyNominal().getAs(NominalTypeDecl.self) }
45+
public var instanceTypeOfMetatype: Type { Type(bridged: bridged.getInstanceTypeOfMetatype()) }
46+
4147
public func subst(with substitutionMap: SubstitutionMap) -> Type {
4248
return Type(bridged: bridged.subst(substitutionMap.bridged))
4349
}
50+
51+
/// Performas a global conformance lookup for this type for `protocol`.
52+
/// It checks conditional requirements.
53+
///
54+
/// This type must be a contextualized type. It must not contain type parameters.
55+
///
56+
/// The resulting conformance reference does not include "missing" conformances, which are synthesized for
57+
/// some protocols as an error recovery mechanism.
58+
///
59+
/// Returns an invalid conformance if the search failed, otherwise an
60+
/// abstract, concrete or pack conformance, depending on the lookup type.
61+
public func checkConformance(to protocol: ProtocolDecl) -> Conformance {
62+
return Conformance(bridged: bridged.checkConformance(`protocol`.bridged))
63+
}
4464
}
4565

4666
/// A Type that is statically known to be canonical.
@@ -61,15 +81,26 @@ public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
6181
public var description: String { type.description }
6282

6383
public var hasTypeParameter: Bool { type.hasTypeParameter }
84+
public var hasOpenedExistential: Bool { type.hasOpenedExistential }
6485
public var isOpenedExistentialWithError: Bool { type.isOpenedExistentialWithError }
6586
public var isEscapable: Bool { type.isEscapable }
6687
public var isNoEscape: Bool { type.isNoEscape }
6788
public var isInteger: Bool { type.isInteger }
89+
public var isMetatypeType: Bool { type.isMetatypeType }
90+
public var isExistentialMetatypeType: Bool { type.isExistentialMetatypeType }
6891

92+
public var anyNominal: NominalTypeDecl? { type.anyNominal }
93+
public var instanceTypeOfMetatype: CanonicalType { type.instanceTypeOfMetatype.canonical }
94+
6995
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
7096
return type.subst(with: substitutionMap).canonical
7197
}
7298

99+
// See `type.checkConformance`
100+
public func checkConformance(to proto: ProtocolDecl) -> Conformance {
101+
return type.checkConformance(to: proto)
102+
}
103+
73104
public var canBeClass: TraitResult { bridged.canBeClass().result }
74105
}
75106

@@ -104,3 +135,15 @@ extension BridgedCanType.TraitResult {
104135
}
105136
}
106137
}
138+
139+
extension Type: Equatable {
140+
public static func ==(lhs: Type, rhs: Type) -> Bool {
141+
lhs.bridged.type == rhs.bridged.type
142+
}
143+
}
144+
145+
extension CanonicalType: Equatable {
146+
public static func ==(lhs: CanonicalType, rhs: CanonicalType) -> Bool {
147+
lhs.type == rhs.type
148+
}
149+
}

include/swift/AST/ASTBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct BridgedASTType;
7575
class BridgedCanType;
7676
class BridgedASTContext;
7777
struct BridgedSubstitutionMap;
78+
struct BridgedConformance;
7879
class BridgedParameterList;
7980
enum BridgedPlatformKind : size_t;
8081

@@ -2989,11 +2990,17 @@ struct BridgedASTType {
29892990
BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
29902991
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getCanonicalType() const;
29912992
BRIDGED_INLINE bool hasTypeParameter() const;
2993+
BRIDGED_INLINE bool hasOpenedExistential() const;
29922994
BRIDGED_INLINE bool isOpenedExistentialWithError() const;
29932995
BRIDGED_INLINE bool isEscapable() const;
29942996
BRIDGED_INLINE bool isNoEscape() const;
29952997
BRIDGED_INLINE bool isInteger() const;
2998+
BRIDGED_INLINE bool isMetatypeType() const;
2999+
BRIDGED_INLINE bool isExistentialMetatypeType() const;
3000+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getAnyNominal() const;
3001+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getInstanceTypeOfMetatype() const;
29963002
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
3003+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
29973004
};
29983005

29993006
class BridgedCanType {

include/swift/AST/ASTBridgingImpl.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/ASTContext.h"
1717
#include "swift/AST/ArgumentList.h"
1818
#include "swift/AST/AvailabilityDomain.h"
19+
#include "swift/AST/ConformanceLookup.h"
1920
#include "swift/AST/Decl.h"
2021
#include "swift/AST/Expr.h"
2122
#include "swift/AST/IfConfigClauseRangeInfo.h"
@@ -400,6 +401,10 @@ bool BridgedASTType::hasTypeParameter() const {
400401
return unbridged()->hasTypeParameter();
401402
}
402403

404+
bool BridgedASTType::hasOpenedExistential() const {
405+
return unbridged()->hasOpenedExistential();
406+
}
407+
403408
bool BridgedASTType::isOpenedExistentialWithError() const {
404409
return unbridged()->isOpenedExistentialWithError();
405410
}
@@ -416,10 +421,30 @@ bool BridgedASTType::isInteger() const {
416421
return unbridged()->is<swift::IntegerType>();
417422
}
418423

424+
bool BridgedASTType::isMetatypeType() const {
425+
return unbridged()->is<swift::AnyMetatypeType>();
426+
}
427+
428+
bool BridgedASTType::isExistentialMetatypeType() const {
429+
return unbridged()->is<swift::ExistentialMetatypeType>();
430+
}
431+
432+
OptionalBridgedDeclObj BridgedASTType::getAnyNominal() const {
433+
return {unbridged()->getAnyNominal()};
434+
}
435+
436+
BridgedASTType BridgedASTType::getInstanceTypeOfMetatype() const {
437+
return {unbridged()->getAs<swift::AnyMetatypeType>()->getInstanceType().getPointer()};
438+
}
439+
419440
BridgedASTType BridgedASTType::subst(BridgedSubstitutionMap substMap) const {
420441
return {unbridged().subst(substMap.unbridged()).getPointer()};
421442
}
422443

444+
BridgedConformance BridgedASTType::checkConformance(BridgedDeclObj proto) const {
445+
return swift::checkConformance(unbridged(), proto.getAs<swift::ProtocolDecl>(), /*allowMissing=*/ false);
446+
}
447+
423448
//===----------------------------------------------------------------------===//
424449
// MARK: BridgedCanType
425450
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)