Skip to content

Commit 18088b0

Browse files
committed
[AST] Consolidate Obj-C types on ASTContext
This commit moves the getNSObjectType and getObjCSelectorType methods from TypeChecker onto ASTContext. In addition, it moves the FOR_KNOWN_FOUNDATION_TYPES macro into a separate file to define each of the Obj-C type decls we want to have access to.
1 parent e2193e1 commit 18088b0

File tree

9 files changed

+73
-112
lines changed

9 files changed

+73
-112
lines changed

include/swift/AST/ASTContext.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,13 @@ class ASTContext final {
447447
/// Retrieve the type Swift.Never.
448448
CanType getNeverType() const;
449449

450-
/// Retrieve the declaration of ObjectiveC.ObjCBool.
451-
StructDecl *getObjCBoolDecl() const;
452-
453-
/// Retrieve the declaration of Foundation.NSCopying.
454-
ProtocolDecl *getNSCopyingDecl() const;
455-
/// Retrieve the declaration of Foundation.NSError.
456-
ClassDecl *getNSErrorDecl() const;
457-
/// Retrieve the declaration of Foundation.NSNumber.
458-
ClassDecl *getNSNumberDecl() const;
459-
/// Retrieve the declaration of Foundation.NSValue.
460-
ClassDecl *getNSValueDecl() const;
450+
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS) \
451+
/** Retrieve the declaration of MODULE.NAME. */ \
452+
DECL_CLASS *get##NAME##Decl() const; \
453+
\
454+
/** Retrieve the type of MODULE.NAME. */ \
455+
Type get##NAME##Type() const;
456+
#include "swift/AST/KnownObjCTypes.def"
461457

462458
// Declare accessors for the known declarations.
463459
#define FUNC_DECL(Name, Id) \

include/swift/AST/KnownObjCTypes.def

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- KnownObjCTypes.def - Common Objective-C types --------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This xmacro generates code for common imported Objective-C types the
14+
// compiler has special knowledge of.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef KNOWN_OBJC_TYPE_DECL
19+
/// KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS)
20+
///
21+
/// The macro is expanded for each known imported Objective-C type. MODULE is
22+
/// bound to the name of the module the type comes from. NAME is bound to the
23+
/// unqualified name of the type. DECL_CLASS is bound to the Decl subclass it
24+
/// is expected to be an instance of.
25+
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS)
26+
#endif
27+
28+
KNOWN_OBJC_TYPE_DECL(Foundation, NSCopying, ProtocolDecl)
29+
KNOWN_OBJC_TYPE_DECL(Foundation, NSError, ClassDecl)
30+
KNOWN_OBJC_TYPE_DECL(Foundation, NSNumber, ClassDecl)
31+
KNOWN_OBJC_TYPE_DECL(Foundation, NSValue, ClassDecl)
32+
33+
KNOWN_OBJC_TYPE_DECL(ObjectiveC, NSObject, ClassDecl)
34+
KNOWN_OBJC_TYPE_DECL(ObjectiveC, Selector, StructDecl)
35+
KNOWN_OBJC_TYPE_DECL(ObjectiveC, ObjCBool, StructDecl)
36+
37+
#undef KNOWN_OBJC_TYPE_DECL

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ using AssociativityCacheType =
9898
llvm::DenseMap<std::pair<PrecedenceGroupDecl *, PrecedenceGroupDecl *>,
9999
Associativity>;
100100

101-
#define FOR_KNOWN_FOUNDATION_TYPES(MACRO) \
102-
MACRO(NSCopying, ProtocolDecl) \
103-
MACRO(NSError, ClassDecl) \
104-
MACRO(NSNumber, ClassDecl) \
105-
MACRO(NSValue, ClassDecl)
106-
107101
struct OverrideSignatureKey {
108102
GenericSignature baseMethodSig;
109103
GenericSignature derivedClassSig;
@@ -190,6 +184,11 @@ struct ASTContext::Implementation {
190184
DECL_CLASS *NAME##Decl = nullptr;
191185
#include "swift/AST/KnownStdlibTypes.def"
192186

187+
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS) \
188+
/** The declaration of MODULE.NAME. */ \
189+
DECL_CLASS *NAME##Decl = nullptr;
190+
#include "swift/AST/KnownObjCTypes.def"
191+
193192
/// The declaration of '+' function for two RangeReplaceableCollection.
194193
FuncDecl *PlusFunctionOnRangeReplaceableCollection = nullptr;
195194

@@ -216,15 +215,6 @@ struct ASTContext::Implementation {
216215

217216
/// The declaration of Swift.AutoreleasingUnsafeMutablePointer<T>.memory.
218217
VarDecl *AutoreleasingUnsafeMutablePointerMemoryDecl = nullptr;
219-
220-
/// The declaration of ObjectiveC.ObjCBool.
221-
StructDecl *ObjCBoolDecl = nullptr;
222-
223-
#define CACHE_FOUNDATION_DECL(NAME, DECLTYPE) \
224-
/** The declaration of Foundation.NAME. */ \
225-
DECLTYPE *NAME##Decl = nullptr;
226-
FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
227-
#undef CACHE_FOUNDATION_DECL
228218

229219
// Declare cached declarations for each of the known declarations.
230220
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
@@ -827,33 +817,12 @@ CanType ASTContext::getNeverType() const {
827817
return neverDecl->getDeclaredType()->getCanonicalType();
828818
}
829819

830-
StructDecl *ASTContext::getObjCBoolDecl() const {
831-
if (!getImpl().ObjCBoolDecl) {
832-
SmallVector<ValueDecl *, 1> results;
833-
auto *Context = const_cast<ASTContext *>(this);
834-
if (ModuleDecl *M = Context->getModuleByName(Id_ObjectiveC.str())) {
835-
M->lookupValue(getIdentifier("ObjCBool"), NLKind::UnqualifiedLookup,
836-
results);
837-
for (auto result : results) {
838-
if (auto structDecl = dyn_cast<StructDecl>(result)) {
839-
if (structDecl->getGenericParams() == nullptr) {
840-
getImpl().ObjCBoolDecl = structDecl;
841-
break;
842-
}
843-
}
844-
}
845-
}
846-
}
847-
848-
return getImpl().ObjCBoolDecl;
849-
}
850-
851-
#define GET_FOUNDATION_DECL(NAME, DECLTYPE) \
820+
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECLTYPE) \
852821
DECLTYPE *ASTContext::get##NAME##Decl() const { \
853822
if (!getImpl().NAME##Decl) { \
854-
if (ModuleDecl *M = getLoadedModule(Id_Foundation)) { \
855-
/* Note: lookupQualified() will search both the Foundation module \
856-
* and the Clang Foundation module it imports. */ \
823+
if (ModuleDecl *M = getLoadedModule(Id_##MODULE)) { \
824+
/* Note: lookupQualified() will search both the Swift overlay \
825+
* and the Clang module it imports. */ \
857826
SmallVector<ValueDecl *, 1> decls; \
858827
M->lookupQualified(M, getIdentifier(#NAME), NL_OnlyTypes, decls); \
859828
if (decls.size() == 1 && isa<DECLTYPE>(decls[0])) { \
@@ -866,11 +835,16 @@ DECLTYPE *ASTContext::get##NAME##Decl() const { \
866835
} \
867836
\
868837
return getImpl().NAME##Decl; \
838+
} \
839+
\
840+
Type ASTContext::get##NAME##Type() const { \
841+
auto *decl = get##NAME##Decl(); \
842+
if (!decl) \
843+
return Type(); \
844+
return decl->getDeclaredInterfaceType(); \
869845
}
870846

871-
FOR_KNOWN_FOUNDATION_TYPES(GET_FOUNDATION_DECL)
872-
#undef GET_FOUNDATION_DECL
873-
#undef FOR_KNOWN_FOUNDATION_TYPES
847+
#include "swift/AST/KnownObjCTypes.def"
874848

875849
ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
876850
// Check whether we've already looked for and cached this protocol.

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2981,7 +2981,7 @@ namespace {
29812981

29822982
// Make sure we can reference ObjectiveC.Selector.
29832983
// FIXME: Fix-It to add the import?
2984-
auto type = CS.getTypeChecker().getObjCSelectorType(CS.DC);
2984+
auto type = CS.getASTContext().getSelectorType();
29852985
if (!type) {
29862986
ctx.Diags.diagnose(E->getLoc(), diag::expr_selector_module_missing);
29872987
return nullptr;

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6420,6 +6420,7 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64206420
// If the bridged value type is generic, the generic arguments
64216421
// must either match or be bridged.
64226422
// FIXME: This should be an associated type of the protocol.
6423+
auto &ctx = getASTContext();
64236424
if (auto fromBGT = unwrappedToType->getAs<BoundGenericType>()) {
64246425
if (fromBGT->getDecl() == TC.Context.getArrayDecl()) {
64256426
// [AnyObject]
@@ -6429,14 +6430,14 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64296430
LocatorPathElt::GenericArgument(0))));
64306431
} else if (fromBGT->getDecl() == TC.Context.getDictionaryDecl()) {
64316432
// [NSObject : AnyObject]
6432-
auto NSObjectType = TC.getNSObjectType(DC);
6433-
if (!NSObjectType) {
6433+
auto nsObjectType = ctx.getNSObjectType();
6434+
if (!nsObjectType) {
64346435
// Not a bridging case. Should we detect this earlier?
64356436
return SolutionKind::Error;
64366437
}
64376438

64386439
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[0],
6439-
NSObjectType,
6440+
nsObjectType,
64406441
getConstraintLocator(
64416442
locator.withPathElement(
64426443
LocatorPathElt::GenericArgument(0))));
@@ -6447,13 +6448,13 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
64476448
locator.withPathElement(
64486449
LocatorPathElt::GenericArgument(1))));
64496450
} else if (fromBGT->getDecl() == TC.Context.getSetDecl()) {
6450-
auto NSObjectType = TC.getNSObjectType(DC);
6451-
if (!NSObjectType) {
6451+
auto nsObjectType = ctx.getNSObjectType();
6452+
if (!nsObjectType) {
64526453
// Not a bridging case. Should we detect this earlier?
64536454
return SolutionKind::Error;
64546455
}
64556456
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[0],
6456-
NSObjectType,
6457+
nsObjectType,
64576458
getConstraintLocator(
64586459
locator.withPathElement(
64596460
LocatorPathElt::GenericArgument(0))));

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3474,7 +3474,7 @@ class ObjCSelectorWalker : public ASTWalker {
34743474

34753475
static void diagDeprecatedObjCSelectors(TypeChecker &tc, const DeclContext *dc,
34763476
const Expr *expr) {
3477-
auto selectorTy = tc.getObjCSelectorType(const_cast<DeclContext *>(dc));
3477+
auto selectorTy = dc->getASTContext().getSelectorType();
34783478
if (!selectorTy) return;
34793479

34803480
const_cast<Expr *>(expr)->walk(ObjCSelectorWalker(tc, dc, selectorTy));

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19941994
return nullptr;
19951995

19961996
// Don't bother to convert deprecated selector syntax.
1997-
if (auto selectorTy = TC.getObjCSelectorType(DC)) {
1997+
if (auto selectorTy = TC.Context.getSelectorType()) {
19981998
if (type->isEqual(selectorTy))
19991999
return nullptr;
20002000
}
@@ -4407,7 +4407,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
44074407

44084408
// Objective-C metaclasses are subclasses of NSObject in the ObjC runtime,
44094409
// so casts from NSObject to potentially-class metatypes may succeed.
4410-
if (auto nsObject = getNSObjectType(dc)) {
4410+
if (auto nsObject = Context.getNSObjectType()) {
44114411
if (fromType->isEqual(nsObject)) {
44124412
if (auto toMeta = toType->getAs<MetatypeType>()) {
44134413
if (toMeta->getInstanceType()->mayHaveSuperclass()

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -939,48 +939,6 @@ bool swift::canBeRepresentedInObjC(const ValueDecl *decl) {
939939
return false;
940940
}
941941

942-
static Type getObjectiveCNominalType(Type &cache,
943-
Identifier ModuleName,
944-
Identifier TypeName,
945-
DeclContext *dc) {
946-
if (cache)
947-
return cache;
948-
949-
// FIXME: Does not respect visibility of the module.
950-
ASTContext &ctx = dc->getASTContext();
951-
ModuleDecl *module = ctx.getLoadedModule(ModuleName);
952-
if (!module)
953-
return nullptr;
954-
955-
SmallVector<ValueDecl *, 4> decls;
956-
NLOptions options = NL_QualifiedDefault | NL_OnlyTypes;
957-
dc->lookupQualified(module, TypeName, options, decls);
958-
for (auto decl : decls) {
959-
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
960-
cache = nominal->getDeclaredType();
961-
return cache;
962-
}
963-
}
964-
965-
return nullptr;
966-
}
967-
968-
#pragma mark Objective-C-specific types
969-
970-
Type TypeChecker::getNSObjectType(DeclContext *dc) {
971-
return getObjectiveCNominalType(NSObjectType, Context.Id_ObjectiveC,
972-
Context.getSwiftId(
973-
KnownFoundationEntity::NSObject),
974-
dc);
975-
}
976-
977-
Type TypeChecker::getObjCSelectorType(DeclContext *dc) {
978-
return getObjectiveCNominalType(ObjCSelectorType,
979-
Context.Id_ObjectiveC,
980-
Context.Id_Selector,
981-
dc);
982-
}
983-
984942
#pragma mark "@objc declaration handling"
985943

986944
/// Whether this declaration is a member of a class extension marked @objc.

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,6 @@ class TypeChecker final {
556556
std::vector<AbstractClosureExpr *> ClosuresWithUncomputedCaptures;
557557

558558
private:
559-
Type NSObjectType;
560-
Type ObjCSelectorType;
561-
562559
/// The set of expressions currently being analyzed for failures.
563560
llvm::DenseMap<Expr*, Expr*> DiagnosedExprs;
564561

@@ -731,9 +728,7 @@ class TypeChecker final {
731728
Type getIntType(DeclContext *dc);
732729
Type getInt8Type(DeclContext *dc);
733730
Type getUInt8Type(DeclContext *dc);
734-
Type getNSObjectType(DeclContext *dc);
735-
Type getObjCSelectorType(DeclContext *dc);
736-
731+
737732
/// Try to resolve an IdentTypeRepr, returning either the referenced
738733
/// Type or an ErrorType in case of error.
739734
static Type resolveIdentifierType(TypeResolution resolution,

0 commit comments

Comments
 (0)