Skip to content

Commit e1ce0a9

Browse files
committed
[AST/ClangImporter] Eliminate layering violation with isInOverlayModuleForImportedModule.
#16951 introduced a layering violation between the AST and ClangImporter libraries; break the layering violation by moving the function isInOverlayModuleForImportedModule() to ClangModuleLoader.
1 parent 6585c92 commit e1ce0a9

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Sema;
2424

2525
namespace swift {
2626

27+
class DeclContext;
28+
2729
class ClangModuleLoader : public ModuleLoader {
2830
private:
2931
virtual void anchor();
@@ -46,6 +48,16 @@ class ClangModuleLoader : public ModuleLoader {
4648
/// \returns true if there was an error adding the search path.
4749
virtual bool addSearchPath(StringRef newSearchPath, bool isFramework,
4850
bool isSystem) = 0;
51+
52+
/// Determine whether \c overlayDC is within an overlay module for the
53+
/// imported context enclosing \c importedDC.
54+
///
55+
/// This routine is used for various hacks that are only permitted within
56+
/// overlays of imported modules, e.g., Objective-C bridging conformances.
57+
virtual bool isInOverlayModuleForImportedModule(
58+
const DeclContext *overlayDC,
59+
const DeclContext *importedDC) = 0;
60+
4961
};
5062

5163
} // namespace swift

include/swift/ClangImporter/ClangImporter.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ class ClangImporter final : public ClangModuleLoader {
143143
ArrayRef<std::pair<Identifier, SourceLoc>> path)
144144
override;
145145

146+
/// Determine whether \c overlayDC is within an overlay module for the
147+
/// imported context enclosing \c importedDC.
148+
///
149+
/// This routine is used for various hacks that are only permitted within
150+
/// overlays of imported modules, e.g., Objective-C bridging conformances.
151+
bool isInOverlayModuleForImportedModule(
152+
const DeclContext *overlayDC,
153+
const DeclContext *importedDC) override;
154+
146155
/// \brief Look for declarations associated with the given name.
147156
///
148157
/// \param name The name we're searching for.
@@ -350,14 +359,6 @@ class ClangImporter final : public ClangModuleLoader {
350359
ImportDecl *createImportDecl(ASTContext &Ctx, DeclContext *DC, ClangNode ClangN,
351360
ArrayRef<clang::Module *> Exported);
352361

353-
/// Determine whether \c overlayDC is within an overlay module for the
354-
/// imported context enclosing \c importedDC.
355-
///
356-
/// This routine is used for various hacks that are only permitted within
357-
/// overlays of imported modules, e.g., Objective-C bridging conformances.
358-
bool isInOverlayModuleForImportedModule(const DeclContext *overlayDC,
359-
const DeclContext *importedDC);
360-
361362
} // end namespace swift
362363

363364
#endif

lib/AST/NameLookup.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "NameLookupImpl.h"
18-
#include "swift/ClangImporter/ClangImporter.h"
1918
#include "swift/AST/NameLookup.h"
2019
#include "swift/AST/ASTContext.h"
2120
#include "swift/AST/ASTScope.h"
2221
#include "swift/AST/ASTVisitor.h"
22+
#include "swift/AST/ClangModuleLoader.h"
2323
#include "swift/AST/DebuggerClient.h"
2424
#include "swift/AST/ExistentialLayout.h"
2525
#include "swift/AST/LazyResolver.h"
@@ -299,13 +299,18 @@ bool swift::removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
299299
// Prefer declarations in an overlay to similar declarations in
300300
// the Clang module it customizes.
301301
if (firstDecl->hasClangNode() != secondDecl->hasClangNode()) {
302-
if (isInOverlayModuleForImportedModule(firstDecl->getDeclContext(),
303-
secondDecl->getDeclContext())){
302+
auto clangLoader = ctx.getClangModuleLoader();
303+
if (!clangLoader) continue;
304+
305+
if (clangLoader->isInOverlayModuleForImportedModule(
306+
firstDecl->getDeclContext(),
307+
secondDecl->getDeclContext())) {
304308
shadowed.insert(secondDecl);
305309
continue;
306310
}
307311

308-
if (isInOverlayModuleForImportedModule(secondDecl->getDeclContext(),
312+
if (clangLoader->isInOverlayModuleForImportedModule(
313+
secondDecl->getDeclContext(),
309314
firstDecl->getDeclContext())) {
310315
shadowed.insert(firstDecl);
311316
break;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,7 +3598,8 @@ importName(const clang::NamedDecl *D,
35983598
getDeclName();
35993599
}
36003600

3601-
bool swift::isInOverlayModuleForImportedModule(const DeclContext *overlayDC,
3601+
bool ClangImporter::isInOverlayModuleForImportedModule(
3602+
const DeclContext *overlayDC,
36023603
const DeclContext *importedDC) {
36033604
overlayDC = overlayDC->getModuleScopeContext();
36043605
importedDC = importedDC->getModuleScopeContext();
@@ -3613,7 +3614,7 @@ bool swift::isInOverlayModuleForImportedModule(const DeclContext *overlayDC,
36133614

36143615
// Is this a private module that's re-exported to the public (overlay) name?
36153616
auto clangModule =
3616-
importedClangModuleUnit->getClangModule()->getTopLevelModule();
3617+
importedClangModuleUnit->getClangModule()->getTopLevelModule();
36173618
return !clangModule->ExportAsModule.empty() &&
3618-
clangModule->ExportAsModule == overlayModule->getName().str();
3619+
clangModule->ExportAsModule == overlayModule->getName().str();
36193620
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
#include "MiscDiagnostics.h"
1919
#include "swift/AST/GenericSignatureBuilder.h"
2020
#include "swift/AST/ASTVisitor.h"
21+
#include "swift/AST/ClangModuleLoader.h"
2122
#include "swift/AST/GenericEnvironment.h"
2223
#include "swift/AST/NameLookup.h"
2324
#include "swift/AST/ParameterList.h"
2425
#include "swift/AST/Types.h"
25-
#include "swift/ClangImporter/ClangImporter.h"
2626
#include "swift/Parse/Lexer.h"
2727
#include "llvm/Support/Debug.h"
2828

@@ -1413,7 +1413,9 @@ static bool isObjCClassExtensionInOverlay(DeclContext *dc) {
14131413
if (!classDecl)
14141414
return false;
14151415

1416-
return isInOverlayModuleForImportedModule(ext, classDecl);
1416+
auto clangLoader = dc->getASTContext().getClangModuleLoader();
1417+
if (!clangLoader) return false;
1418+
return clangLoader->isInOverlayModuleForImportedModule(ext, classDecl);
14171419
}
14181420

14191421
void AttributeChecker::visitRequiredAttr(RequiredAttr *attr) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/ASTContext.h"
2828
#include "swift/AST/ASTMangler.h"
2929
#include "swift/AST/ASTPrinter.h"
30+
#include "swift/AST/ClangModuleLoader.h"
3031
#include "swift/AST/Decl.h"
3132
#include "swift/AST/ExistentialLayout.h"
3233
#include "swift/AST/GenericEnvironment.h"
@@ -3628,8 +3629,10 @@ void ConformanceChecker::checkConformance(MissingWitnessDiagnosisKind Kind) {
36283629
if (Proto->isSpecificProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
36293630
auto nominal = Adoptee->getAnyNominal();
36303631
if (!TC.Context.isTypeBridgedInExternalModule(nominal)) {
3632+
auto clangLoader = TC.Context.getClangModuleLoader();
36313633
if (nominal->getParentModule() != DC->getParentModule() &&
3632-
!isInOverlayModuleForImportedModule(DC, nominal)) {
3634+
!(clangLoader &&
3635+
clangLoader->isInOverlayModuleForImportedModule(DC, nominal))) {
36333636
auto nominalModule = nominal->getParentModule();
36343637
TC.diagnose(Loc, diag::nonlocal_bridged_to_objc, nominal->getName(),
36353638
Proto->getName(), nominalModule->getName());

0 commit comments

Comments
 (0)