Skip to content

Commit 7e04315

Browse files
committed
[CodeCompletion] Move ImportDepth to its own file
1 parent 48179ad commit 7e04315

File tree

5 files changed

+131
-88
lines changed

5 files changed

+131
-88
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/IDE/CodeCompletionResult.h"
2626
#include "swift/IDE/CodeCompletionResultSink.h"
2727
#include "swift/IDE/CodeCompletionString.h"
28+
#include "swift/IDE/ImportDepth.h"
2829
#include "llvm/ADT/ArrayRef.h"
2930
#include "llvm/ADT/StringMap.h"
3031
#include "llvm/ADT/StringRef.h"
@@ -78,26 +79,6 @@ ArrayRef<T> copyArray(llvm::BumpPtrAllocator &Allocator,
7879
return llvm::makeArrayRef(Buffer, Arr.size());
7980
}
8081

81-
/// A utility for calculating the import depth of a given module. Direct imports
82-
/// have depth 1, imports of those modules have depth 2, etc.
83-
///
84-
/// Special modules such as Playground auxiliary sources are considered depth
85-
/// 0.
86-
class ImportDepth {
87-
llvm::StringMap<uint8_t> depths;
88-
89-
public:
90-
ImportDepth() = default;
91-
ImportDepth(ASTContext &context, const FrontendOptions &frontendOptions);
92-
93-
Optional<uint8_t> lookup(StringRef module) {
94-
auto I = depths.find(module);
95-
if (I == depths.end())
96-
return None;
97-
return I->getValue();
98-
}
99-
};
100-
10182
struct SwiftCompletionInfo {
10283
swift::ASTContext *swiftASTContext = nullptr;
10384
const swift::CompilerInvocation *invocation = nullptr;

include/swift/IDE/ImportDepth.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- ImportDepth.h ----------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
#ifndef SWIFT_IDE_IMPORTDEPTH_H
14+
#define SWIFT_IDE_IMPORTDEPTH_H
15+
16+
#include "swift/AST/ASTContext.h"
17+
#include "swift/Basic/LLVM.h"
18+
#include "swift/Frontend/FrontendOptions.h"
19+
#include "llvm/ADT/StringMap.h"
20+
21+
namespace swift {
22+
namespace ide {
23+
24+
/// A utility for calculating the import depth of a given module. Direct imports
25+
/// have depth 1, imports of those modules have depth 2, etc.
26+
///
27+
/// Special modules such as Playground auxiliary sources are considered depth
28+
/// 0.
29+
class ImportDepth {
30+
llvm::StringMap<uint8_t> depths;
31+
32+
public:
33+
ImportDepth() = default;
34+
ImportDepth(ASTContext &context, const FrontendOptions &frontendOptions);
35+
36+
Optional<uint8_t> lookup(StringRef module) {
37+
auto I = depths.find(module);
38+
if (I == depths.end())
39+
return None;
40+
return I->getValue();
41+
}
42+
};
43+
44+
} // end namespace ide
45+
} // end namespace swift
46+
47+
#endif // SWIFT_IDE_IMPORTDEPTH_H

lib/IDE/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_swift_host_library(swiftIDE STATIC
2828
SyntaxModel.cpp
2929
Utils.cpp
3030
IDETypeChecking.cpp
31+
ImportDepth.cpp
3132
APIDigesterData.cpp
3233
SourceEntityWalker.cpp
3334
TypeContextInfo.cpp

lib/IDE/CodeCompletion.cpp

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,71 +2065,3 @@ void swift::ide::lookupCodeCompletionResultsFromModule(
20652065
CompletionLookup Lookup(targetSink, module->getASTContext(), SF);
20662066
Lookup.lookupExternalModuleDecls(module, accessPath, needLeadingDot);
20672067
}
2068-
2069-
//===----------------------------------------------------------------------===//
2070-
// ImportDepth
2071-
//===----------------------------------------------------------------------===//
2072-
2073-
ImportDepth::ImportDepth(ASTContext &context,
2074-
const FrontendOptions &frontendOptions) {
2075-
llvm::DenseSet<ModuleDecl *> seen;
2076-
std::deque<std::pair<ModuleDecl *, uint8_t>> worklist;
2077-
2078-
StringRef mainModule = frontendOptions.ModuleName;
2079-
auto *main = context.getLoadedModule(context.getIdentifier(mainModule));
2080-
assert(main && "missing main module");
2081-
worklist.emplace_back(main, uint8_t(0));
2082-
2083-
// Imports from -import-name such as Playground auxiliary sources are treated
2084-
// specially by applying import depth 0.
2085-
llvm::StringSet<> auxImports;
2086-
for (const auto &pair : frontendOptions.getImplicitImportModuleNames())
2087-
auxImports.insert(pair.first);
2088-
2089-
// Private imports from this module.
2090-
// FIXME: only the private imports from the current source file.
2091-
// FIXME: ImportFilterKind::ShadowedByCrossImportOverlay?
2092-
SmallVector<ImportedModule, 16> mainImports;
2093-
main->getImportedModules(mainImports,
2094-
{ModuleDecl::ImportFilterKind::Default,
2095-
ModuleDecl::ImportFilterKind::ImplementationOnly});
2096-
for (auto &import : mainImports) {
2097-
uint8_t depth = 1;
2098-
if (auxImports.count(import.importedModule->getName().str()))
2099-
depth = 0;
2100-
worklist.emplace_back(import.importedModule, depth);
2101-
}
2102-
2103-
// Fill depths with BFS over module imports.
2104-
while (!worklist.empty()) {
2105-
ModuleDecl *module;
2106-
uint8_t depth;
2107-
std::tie(module, depth) = worklist.front();
2108-
worklist.pop_front();
2109-
2110-
if (!seen.insert(module).second)
2111-
continue;
2112-
2113-
// Insert new module:depth mapping.
2114-
const clang::Module *CM = module->findUnderlyingClangModule();
2115-
if (CM) {
2116-
depths[CM->getFullModuleName()] = depth;
2117-
} else {
2118-
depths[module->getName().str()] = depth;
2119-
}
2120-
2121-
// Add imports to the worklist.
2122-
SmallVector<ImportedModule, 16> imports;
2123-
module->getImportedModules(imports);
2124-
for (auto &import : imports) {
2125-
uint8_t next = std::max(depth, uint8_t(depth + 1)); // unsigned wrap
2126-
2127-
// Implicitly imported sub-modules get the same depth as their parent.
2128-
if (const clang::Module *CMI =
2129-
import.importedModule->findUnderlyingClangModule())
2130-
if (CM && CMI->isSubModuleOf(CM))
2131-
next = depth;
2132-
worklist.emplace_back(import.importedModule, next);
2133-
}
2134-
}
2135-
}

lib/IDE/ImportDepth.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===--- ImportDepth.cpp --------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
#include "swift/IDE/ImportDepth.h"
14+
#include "swift/AST/Module.h"
15+
#include "clang/Basic/Module.h"
16+
17+
using namespace swift;
18+
using namespace swift::ide;
19+
20+
ImportDepth::ImportDepth(ASTContext &context,
21+
const FrontendOptions &frontendOptions) {
22+
llvm::DenseSet<ModuleDecl *> seen;
23+
std::deque<std::pair<ModuleDecl *, uint8_t>> worklist;
24+
25+
StringRef mainModule = frontendOptions.ModuleName;
26+
auto *main = context.getLoadedModule(context.getIdentifier(mainModule));
27+
assert(main && "missing main module");
28+
worklist.emplace_back(main, uint8_t(0));
29+
30+
// Imports from -import-name such as Playground auxiliary sources are treated
31+
// specially by applying import depth 0.
32+
llvm::StringSet<> auxImports;
33+
for (const auto &pair : frontendOptions.getImplicitImportModuleNames())
34+
auxImports.insert(pair.first);
35+
36+
// Private imports from this module.
37+
// FIXME: only the private imports from the current source file.
38+
// FIXME: ImportFilterKind::ShadowedByCrossImportOverlay?
39+
SmallVector<ImportedModule, 16> mainImports;
40+
main->getImportedModules(mainImports,
41+
{ModuleDecl::ImportFilterKind::Default,
42+
ModuleDecl::ImportFilterKind::ImplementationOnly});
43+
for (auto &import : mainImports) {
44+
uint8_t depth = 1;
45+
if (auxImports.count(import.importedModule->getName().str()))
46+
depth = 0;
47+
worklist.emplace_back(import.importedModule, depth);
48+
}
49+
50+
// Fill depths with BFS over module imports.
51+
while (!worklist.empty()) {
52+
ModuleDecl *module;
53+
uint8_t depth;
54+
std::tie(module, depth) = worklist.front();
55+
worklist.pop_front();
56+
57+
if (!seen.insert(module).second)
58+
continue;
59+
60+
// Insert new module:depth mapping.
61+
const clang::Module *CM = module->findUnderlyingClangModule();
62+
if (CM) {
63+
depths[CM->getFullModuleName()] = depth;
64+
} else {
65+
depths[module->getName().str()] = depth;
66+
}
67+
68+
// Add imports to the worklist.
69+
SmallVector<ImportedModule, 16> imports;
70+
module->getImportedModules(imports);
71+
for (auto &import : imports) {
72+
uint8_t next = std::max(depth, uint8_t(depth + 1)); // unsigned wrap
73+
74+
// Implicitly imported sub-modules get the same depth as their parent.
75+
if (const clang::Module *CMI =
76+
import.importedModule->findUnderlyingClangModule())
77+
if (CM && CMI->isSubModuleOf(CM))
78+
next = depth;
79+
worklist.emplace_back(import.importedModule, next);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)