Skip to content

Commit b14341d

Browse files
committed
[Type checker] Check “bridged functions” as part of type checking setup.
The “check bridged functions” hack makes sure that certain functions provided by the Darwin and ObjectiveC overlays and used by SILGen get interface types. Perform “check bridged functions” as part of the general “type check a source file” routine. This eliminates some state from TypeChecker (the “have we checked yet?” bit) so we can take TypeChecker out of more of @objc checking.
1 parent e22a84b commit b14341d

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

lib/Sema/TypeCheckDeclObjC.cpp

+5-13
Original file line numberDiff line numberDiff line change
@@ -928,24 +928,19 @@ static void checkObjCBridgingFunctions(ModuleDecl *mod,
928928
}
929929
}
930930

931-
static void checkBridgedFunctions(TypeChecker &TC) {
932-
if (TC.HasCheckedBridgeFunctions)
933-
return;
934-
935-
TC.HasCheckedBridgeFunctions = true;
936-
931+
void swift::checkBridgedFunctions(ASTContext &ctx) {
937932
#define BRIDGE_TYPE(BRIDGED_MOD, BRIDGED_TYPE, _, NATIVE_TYPE, OPT) \
938-
Identifier ID_##BRIDGED_MOD = TC.Context.getIdentifier(#BRIDGED_MOD);\
939-
if (ModuleDecl *module = TC.Context.getLoadedModule(ID_##BRIDGED_MOD)) {\
933+
Identifier ID_##BRIDGED_MOD = ctx.getIdentifier(#BRIDGED_MOD);\
934+
if (ModuleDecl *module = ctx.getLoadedModule(ID_##BRIDGED_MOD)) {\
940935
checkObjCBridgingFunctions(module, #BRIDGED_TYPE, \
941936
"_convert" #BRIDGED_TYPE "To" #NATIVE_TYPE, \
942937
"_convert" #NATIVE_TYPE "To" #BRIDGED_TYPE); \
943938
}
944939
#include "swift/SIL/BridgedTypes.def"
945940

946-
if (ModuleDecl *module = TC.Context.getLoadedModule(TC.Context.Id_Foundation)) {
941+
if (ModuleDecl *module = ctx.getLoadedModule(ctx.Id_Foundation)) {
947942
checkObjCBridgingFunctions(module,
948-
TC.Context.getSwiftName(
943+
ctx.getSwiftName(
949944
KnownFoundationEntity::NSError),
950945
"_convertNSErrorToError",
951946
"_convertErrorToNSError");
@@ -1434,9 +1429,6 @@ void swift::markAsObjC(TypeChecker &TC, ValueDecl *D,
14341429
attr->setInvalid();
14351430
}
14361431

1437-
// Make sure we have the appropriate bridging operations.
1438-
if (!isa<DestructorDecl>(D))
1439-
checkBridgedFunctions(TC);
14401432
TC.useObjectiveCBridgeableConformances(D->getInnermostDeclContext(),
14411433
D->getInterfaceType());
14421434

lib/Sema/TypeCheckObjC.h

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ bool isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason);
137137
/// Check whether the given declaration can be represented in Objective-C.
138138
bool canBeRepresentedInObjC(const ValueDecl *decl);
139139

140+
/// Check that specific, known bridging functions are fully type-checked.
141+
///
142+
/// NOTE: This is only here to support the --enable-source-import hack.
143+
void checkBridgedFunctions(ASTContext &ctx);
144+
140145
} // end namespace swift
141146

142147
#endif // SWIFT_SEMA_TYPE_CHECK_OBJC_H

lib/Sema/TypeChecker.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "swift/Subsystems.h"
1919
#include "TypeChecker.h"
20+
#include "TypeCheckObjC.h"
2021
#include "CodeSynthesis.h"
2122
#include "MiscDiagnostics.h"
2223
#include "GenericTypeResolver.h"
@@ -670,6 +671,10 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
670671
}
671672
});
672673

674+
// Look for bridging functions. This only matters when
675+
// -enable-source-import is provided.
676+
checkBridgedFunctions(TC.Context);
677+
673678
// Type check the top-level elements of the source file.
674679
bool hasTopLevelCode = false;
675680
for (auto D : llvm::makeArrayRef(SF.Decls).slice(StartElem)) {

lib/Sema/TypeChecker.h

-5
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,6 @@ class TypeChecker final : public LazyResolver {
678678
llvm::DenseMap<std::pair<ValueDecl*, ValueDecl*>, bool>
679679
specializedOverloadComparisonCache;
680680

681-
// We delay validation of C and Objective-C type-bridging functions in the
682-
// standard library until we encounter a declaration that requires one. This
683-
// flag is set to 'true' once the bridge functions have been checked.
684-
bool HasCheckedBridgeFunctions = false;
685-
686681
/// A list of closures for the most recently type-checked function, which we
687682
/// will need to compute captures for.
688683
std::vector<AnyFunctionRef> ClosuresWithUncomputedCaptures;

0 commit comments

Comments
 (0)