Skip to content

[ClangImporter] Retire the term "adapter" in favor of "overlay" #24427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ class LoadedFile : public FileUnit {
}

/// Returns the Swift module that overlays a Clang module.
virtual ModuleDecl *getAdapterModule() const { return nullptr; }
virtual ModuleDecl *getOverlayModule() const { return nullptr; }

virtual bool isSystemModule() const { return false; }

Expand Down
6 changes: 3 additions & 3 deletions include/swift/ClangImporter/ClangImporterOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class ClangImporterOptions {
/// and this is not set, clang will rebuild the module.
bool DisableModulesValidateSystemHeaders = false;

/// When set, don't look for or load adapter modules.
bool DisableAdapterModules = false;
/// When set, don't look for or load overlays.
bool DisableOverlayModules = false;

/// When set, don't enforce warnings with -Werror.
bool DebuggerSupport = false;
Expand All @@ -118,7 +118,7 @@ class ClangImporterOptions {
Code = hash_combine(Code, InferImportAsMember);
Code = hash_combine(Code, DisableSwiftBridgeAttr);
Code = hash_combine(Code, DisableModulesValidateSystemHeaders);
Code = hash_combine(Code, DisableAdapterModules);
Code = hash_combine(Code, DisableOverlayModules);
return Code;
}
};
Expand Down
4 changes: 2 additions & 2 deletions include/swift/ClangImporter/ClangModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ModuleLoader;
class ClangModuleUnit final : public LoadedFile {
ClangImporter::Implementation &owner;
const clang::Module *clangModule;
llvm::PointerIntPair<ModuleDecl *, 1, bool> adapterModule;
llvm::PointerIntPair<ModuleDecl *, 1, bool> overlayModule;
mutable ArrayRef<ModuleDecl::ImportedModule> importedModulesForLookup;
/// The metadata of the underlying Clang module.
clang::ExternalASTSource::ASTSourceDescriptor ASTSourceDescriptor;
Expand All @@ -57,7 +57,7 @@ class ClangModuleUnit final : public LoadedFile {
bool isTopLevel() const;

/// Returns the Swift module that overlays this Clang module.
ModuleDecl *getAdapterModule() const override;
ModuleDecl *getOverlayModule() const override;

/// Retrieve the "exported" name of the module, which is usually the module
/// name, but might be the name of the public module through which this
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ bool NormalProtocolConformance::isRetroactive() const {
// defined in a Clang module.
if (auto nominalLoadedModule =
dyn_cast<LoadedFile>(nominal->getModuleScopeContext())) {
if (auto overlayModule = nominalLoadedModule->getAdapterModule())
if (auto overlayModule = nominalLoadedModule->getOverlayModule())
nominalModule = overlayModule;
}

Expand Down
63 changes: 31 additions & 32 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,13 +1627,12 @@ ModuleDecl *ClangImporter::loadModule(
if (!clangModule)
return nullptr;

return Impl.finishLoadingClangModule(clangModule,
/*preferAdapter=*/false);
return Impl.finishLoadingClangModule(clangModule, /*preferOverlay=*/false);
}

ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
const clang::Module *clangModule,
bool findAdapter) {
bool findOverlay) {
assert(clangModule);

// Bump the generation count.
Expand All @@ -1645,7 +1644,7 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
if ((wrapperUnit = cacheEntry.getPointer())) {
result = wrapperUnit->getParentModule();
if (!cacheEntry.getInt()) {
// Force load adapter modules for all imported modules.
// Force load overlays for all imported modules.
// FIXME: This forces the creation of wrapper modules for all imports as
// well, and may do unnecessary work.
cacheEntry.setInt(true);
Expand All @@ -1666,7 +1665,7 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
result->addFile(*wrapperUnit);
cacheEntry.setPointerAndInt(wrapperUnit, true);

// Force load adapter modules for all imported modules.
// Force load overlays for all imported modules.
// FIXME: This forces the creation of wrapper modules for all imports as
// well, and may do unnecessary work.
result->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {});
Expand All @@ -1680,9 +1679,9 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
loaded = result;
}

if (findAdapter)
if (ModuleDecl *adapter = wrapperUnit->getAdapterModule())
result = adapter;
if (findOverlay)
if (ModuleDecl *overlay = wrapperUnit->getOverlayModule())
result = overlay;

return result;
}
Expand All @@ -1702,7 +1701,7 @@ void ClangImporter::Implementation::handleDeferredImports()
}
PCHImportedSubmodules.clear();
for (const clang::Module *M : ImportedHeaderExports)
(void)finishLoadingClangModule(M, /*preferAdapter=*/true);
(void)finishLoadingClangModule(M, /*preferOverlay=*/true);
}

ModuleDecl *ClangImporter::getImportedHeaderModule() const {
Expand Down Expand Up @@ -1801,7 +1800,7 @@ ClangImporter::Implementation::Implementation(ASTContext &ctx,
InferImportAsMember(opts.InferImportAsMember),
DisableSwiftBridgeAttr(opts.DisableSwiftBridgeAttr),
BridgingHeaderExplicitlyRequested(!opts.BridgingHeader.empty()),
DisableAdapterModules(opts.DisableAdapterModules),
DisableOverlayModules(opts.DisableOverlayModules),
IsReadingBridgingPCH(false),
CurrentVersion(ImportNameVersion::fromOptions(ctx.LangOpts)),
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
Expand Down Expand Up @@ -3164,40 +3163,40 @@ StringRef ClangModuleUnit::getExportedModuleName() const {
return getParentModule()->getName().str();
}

ModuleDecl *ClangModuleUnit::getAdapterModule() const {
ModuleDecl *ClangModuleUnit::getOverlayModule() const {
if (!clangModule)
return nullptr;

if (owner.DisableAdapterModules)
if (owner.DisableOverlayModules)
return nullptr;

if (!isTopLevel()) {
// FIXME: Is this correct for submodules?
auto topLevel = clangModule->getTopLevelModule();
auto wrapper = owner.getWrapperForModule(topLevel);
return wrapper->getAdapterModule();
return wrapper->getOverlayModule();
}

if (!adapterModule.getInt()) {
if (!overlayModule.getInt()) {
// FIXME: Include proper source location.
ModuleDecl *M = getParentModule();
ASTContext &Ctx = M->getASTContext();
auto adapter = Ctx.getModule(ModuleDecl::AccessPathTy({M->getName(),
SourceLoc()}));
if (adapter == M) {
adapter = nullptr;
auto overlay = Ctx.getModule(ModuleDecl::AccessPathTy({M->getName(),
SourceLoc()}));
if (overlay == M) {
overlay = nullptr;
} else {
auto &sharedModuleRef = Ctx.LoadedModules[M->getName()];
assert(!sharedModuleRef || sharedModuleRef == adapter ||
assert(!sharedModuleRef || sharedModuleRef == overlay ||
sharedModuleRef == M);
sharedModuleRef = adapter;
sharedModuleRef = overlay;
}

auto mutableThis = const_cast<ClangModuleUnit *>(this);
mutableThis->adapterModule.setPointerAndInt(adapter, true);
mutableThis->overlayModule.setPointerAndInt(overlay, true);
}

return adapterModule.getPointer();
return overlayModule.getPointer();
}

void ClangModuleUnit::getImportedModules(
Expand Down Expand Up @@ -3243,11 +3242,11 @@ void ClangModuleUnit::getImportedModules(
}
}

auto topLevelAdapter = getAdapterModule();
auto topLevelOverlay = getOverlayModule();
for (auto importMod : imported) {
auto wrapper = owner.getWrapperForModule(importMod);

auto actualMod = wrapper->getAdapterModule();
auto actualMod = wrapper->getOverlayModule();
if (!actualMod) {
// HACK: Deal with imports of submodules by importing the top-level module
// as well.
Expand All @@ -3260,11 +3259,11 @@ void ClangModuleUnit::getImportedModules(
}
}
actualMod = wrapper->getParentModule();
} else if (actualMod == topLevelAdapter) {
} else if (actualMod == topLevelOverlay) {
actualMod = wrapper->getParentModule();
}

assert(actualMod && "Missing imported adapter module");
assert(actualMod && "Missing imported overlay");
imports.push_back({ModuleDecl::AccessPathTy(), actualMod});
}
}
Expand All @@ -3283,7 +3282,7 @@ void ClangModuleUnit::getImportedModulesForLookup(

SmallVector<clang::Module *, 8> imported;
const clang::Module *topLevel;
ModuleDecl *topLevelAdapter = getAdapterModule();
ModuleDecl *topLevelOverlay = getOverlayModule();
if (!clangModule) {
// This is the special "imported headers" module.
imported.append(owner.ImportedHeaderExports.begin(),
Expand Down Expand Up @@ -3315,7 +3314,7 @@ void ClangModuleUnit::getImportedModulesForLookup(
// Don't continue looking through submodules of modules that have
// overlays. The overlay might shadow things.
auto wrapper = owner.getWrapperForModule(nextTopLevel);
if (wrapper->getAdapterModule())
if (wrapper->getOverlayModule())
continue;
}

Expand All @@ -3334,11 +3333,11 @@ void ClangModuleUnit::getImportedModulesForLookup(
for (auto importMod : topLevelImported) {
auto wrapper = owner.getWrapperForModule(importMod);

auto actualMod = wrapper->getAdapterModule();
if (!actualMod || actualMod == topLevelAdapter)
auto actualMod = wrapper->getOverlayModule();
if (!actualMod || actualMod == topLevelOverlay)
actualMod = wrapper->getParentModule();

assert(actualMod && "Missing imported adapter module");
assert(actualMod && "Missing imported overlay");
imports.push_back({ModuleDecl::AccessPathTy(), actualMod});
}

Expand Down Expand Up @@ -3725,7 +3724,7 @@ bool ClangImporter::isInOverlayModuleForImportedModule(
return false;

auto overlayModule = overlayDC->getParentModule();
if (overlayModule == importedClangModuleUnit->getAdapterModule())
if (overlayModule == importedClangModuleUnit->getOverlayModule())
return true;

// Is this a private module that's re-exported to the public (overlay) name?
Expand Down
20 changes: 10 additions & 10 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4330,12 +4330,12 @@ namespace {
}

template <typename T, typename U>
T *resolveSwiftDeclImpl(const U *decl, Identifier name, ModuleDecl *adapter) {
T *resolveSwiftDeclImpl(const U *decl, Identifier name, ModuleDecl *overlay) {
const auto &languageVersion =
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;

SmallVector<ValueDecl *, 4> results;
adapter->lookupValue({}, name, NLKind::QualifiedLookup, results);
overlay->lookupValue({}, name, NLKind::QualifiedLookup, results);
T *found = nullptr;
for (auto result : results) {
if (auto singleResult = dyn_cast<T>(result)) {
Expand All @@ -4361,8 +4361,8 @@ namespace {
template <typename T, typename U>
T *resolveSwiftDecl(const U *decl, Identifier name,
ClangModuleUnit *clangModule) {
if (auto adapter = clangModule->getAdapterModule())
return resolveSwiftDeclImpl<T>(decl, name, adapter);
if (auto overlay = clangModule->getOverlayModule())
return resolveSwiftDeclImpl<T>(decl, name, overlay);
if (clangModule == Impl.ImportedHeaderUnit) {
// Use an index-based loop because new owners can come in as we're
// iterating.
Expand Down Expand Up @@ -4414,7 +4414,7 @@ namespace {
// FIXME: Figure out how to deal with incomplete protocols, since that
// notion doesn't exist in Swift.
if (!decl->hasDefinition()) {
// Check if this protocol is implemented in its adapter.
// Check if this protocol is implemented in its overlay.
if (auto clangModule = Impl.getClangModuleForDecl(decl, true))
if (auto native = resolveSwiftDecl<ProtocolDecl>(decl, name,
clangModule))
Expand Down Expand Up @@ -4545,7 +4545,7 @@ namespace {
auto name = importedName.getDeclName().getBaseIdentifier();

if (!decl->hasDefinition()) {
// Check if this class is implemented in its adapter.
// Check if this class is implemented in its overlay.
if (auto clangModule = Impl.getClangModuleForDecl(decl, true)) {
if (auto native = resolveSwiftDecl<ClassDecl>(decl, name,
clangModule)) {
Expand Down Expand Up @@ -5199,13 +5199,13 @@ static bool conformsToProtocolInOriginalModule(NominalTypeDecl *nominal,
const DeclContext *containingFile = nominal->getModuleScopeContext();
ModuleDecl *originalModule = containingFile->getParentModule();

ModuleDecl *adapterModule = nullptr;
ModuleDecl *overlayModule = nullptr;
if (auto *clangUnit = dyn_cast<ClangModuleUnit>(containingFile))
adapterModule = clangUnit->getAdapterModule();
overlayModule = clangUnit->getOverlayModule();

for (ExtensionDecl *extension : nominal->getExtensions()) {
ModuleDecl *extensionModule = extension->getParentModule();
if (extensionModule != originalModule && extensionModule != adapterModule &&
if (extensionModule != originalModule && extensionModule != overlayModule &&
extensionModule != foundationModule) {
continue;
}
Expand Down Expand Up @@ -5302,7 +5302,7 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
return false;

// Break circularity by only looking for declared conformances in the
// original module, or possibly its adapter.
// original module, or possibly its overlay.
if (conformsToProtocolInOriginalModule(computedNominal, proto,
Impl.tryLoadFoundationModule(),
Impl.getTypeResolver())) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ Type ClangImporter::Implementation::getNamedSwiftType(ModuleDecl *module,
if (auto clangUnit = dyn_cast<ClangModuleUnit>(file)) {
// If we have an overlay, look in the overlay. Otherwise, skip
// the lookup to avoid infinite recursion.
if (auto module = clangUnit->getAdapterModule())
if (auto module = clangUnit->getOverlayModule())
module->lookupValue({ }, identifier,
NLKind::UnqualifiedLookup, results);
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
const bool InferImportAsMember;
const bool DisableSwiftBridgeAttr;
const bool BridgingHeaderExplicitlyRequested;
const bool DisableAdapterModules;
const bool DisableOverlayModules;

bool IsReadingBridgingPCH;
llvm::SmallVector<clang::serialization::SubmoduleID, 2> PCHImportedSubmodules;
Expand Down Expand Up @@ -520,7 +520,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
Type NSObjectTy;

/// A pair containing a ClangModuleUnit,
/// and whether the adapters of its re-exported modules have all been forced
/// and whether the overlays of its re-exported modules have all been forced
/// to load already.
using ModuleInitPair = llvm::PointerIntPair<ClangModuleUnit *, 1, bool>;

Expand Down Expand Up @@ -894,7 +894,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation

/// Constructs a Swift module for the given Clang module.
ModuleDecl *finishLoadingClangModule(const clang::Module *clangModule,
bool preferAdapter);
bool preferOverlay);

/// Call finishLoadingClangModule on each deferred import collected
/// while scanning a bridging header or PCH.
Expand Down
2 changes: 1 addition & 1 deletion lib/DWARFImporter/DWARFImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class DWARFImporter::Implementation {
ModuleWrappers.insert({name, wrapperUnit});
decl->addFile(*wrapperUnit);

// Force load adapter modules for all imported modules.
// Force load overlay modules for all imported modules.
decl->forAllVisibleModules({}, [](ModuleDecl::ImportedModule import) {});

return decl;
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,

Opts.DisableModulesValidateSystemHeaders |= Args.hasArg(OPT_disable_modules_validate_system_headers);

Opts.DisableAdapterModules |= Args.hasArg(OPT_emit_imported_modules);
Opts.DisableOverlayModules |= Args.hasArg(OPT_emit_imported_modules);

if (const Arg *A = Args.getLastArg(OPT_pch_output_dir)) {
Opts.PrecompiledHeaderOutputDir = A->getValue();
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ bool WitnessChecker::findBestWitness(
if (!clangModule)
continue;

DeclContext *overlay = clangModule->getAdapterModule();
DeclContext *overlay = clangModule->getOverlayModule();
if (!overlay)
continue;

Expand Down
3 changes: 0 additions & 3 deletions test/ClangImporter/Inputs/adapter.swift

This file was deleted.

2 changes: 1 addition & 1 deletion test/ClangImporter/Inputs/custom-modules/ClangModuleUser.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@import ClangModuleWithAdapter;
@import ClangModuleWithOverlay;
2 changes: 1 addition & 1 deletion test/ClangImporter/Inputs/custom-modules/module.map
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module ClangModuleUser {
export *
}

module ClangModuleWithAdapter {
module ClangModuleWithOverlay {
link "UnderlyingClangLibrary"
}

Expand Down
3 changes: 3 additions & 0 deletions test/ClangImporter/Inputs/overlay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@_exported import ClangModuleWithOverlay

public func fromSwiftOverlay() {}
Loading