Skip to content

Commit d929395

Browse files
authored
Requestify implicit imports (#31016)
Requestify implicit imports
2 parents 701ad83 + 92103a7 commit d929395

24 files changed

+380
-334
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SWIFT_TYPEID(AncestryFlags)
1919
SWIFT_TYPEID(CtorInitializerKind)
2020
SWIFT_TYPEID(FunctionBuilderBodyPreCheck)
2121
SWIFT_TYPEID(GenericSignature)
22+
SWIFT_TYPEID(ImplicitImport)
2223
SWIFT_TYPEID(ImplicitMemberAction)
2324
SWIFT_TYPEID(ParamSpecifier)
2425
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfo)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class GenericTypeParamType;
3636
class InfixOperatorDecl;
3737
class IterableDeclContext;
3838
class ModuleDecl;
39+
struct ImplicitImport;
3940
class NamedPattern;
4041
class NominalTypeDecl;
4142
class OperatorDecl;

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,10 @@ class TypeAliasDecl : public GenericTypeDecl {
30323032
Type getUnderlyingType() const;
30333033
void setUnderlyingType(Type type);
30343034

3035+
/// Returns the interface type of the underlying type if computed, null
3036+
/// otherwise. Should only be used for dumping.
3037+
Type getCachedUnderlyingType() const { return UnderlyingTy.getType(); }
3038+
30353039
/// For generic typealiases, return the unbound generic type.
30363040
UnboundGenericType *getUnboundGenericType() const;
30373041

include/swift/AST/DiagnosticsCommon.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ ERROR(attr_only_on_parameters, none,
9090
ERROR(function_type_no_parens,none,
9191
"single argument function types require parentheses", ())
9292

93+
// Used by both the Frontend and Sema.
94+
ERROR(error_underlying_module_not_found,none,
95+
"underlying Objective-C module %0 not found", (Identifier))
96+
9397
// Used by -verify-generic-signatures
9498
ERROR(generic_signature_not_minimal,none,
9599
"generic requirement '%0' is redundant in %1", (StringRef, StringRef))

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ ERROR(error_stdlib_module_name,none,
142142

143143
ERROR(error_stdlib_not_found,Fatal,
144144
"unable to load standard library for target '%0'", (StringRef))
145-
ERROR(error_underlying_module_not_found,none,
146-
"underlying Objective-C module %0 not found", (Identifier))
147145

148146
ERROR(error_unable_to_load_supplementary_output_file_map, none,
149147
"unable to load supplementary output file map '%0': %1",

include/swift/AST/Module.h

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace swift {
5959
class FuncDecl;
6060
class InfixOperatorDecl;
6161
class LinkLibrary;
62+
struct ImplicitImport;
6263
class ModuleLoader;
6364
class NominalTypeDecl;
6465
class EnumElementDecl;
@@ -158,6 +159,42 @@ enum class ResilienceStrategy : unsigned {
158159
Resilient
159160
};
160161

162+
/// The kind of stdlib that should be imported.
163+
enum class ImplicitStdlibKind {
164+
/// No standard library should be implicitly imported.
165+
None,
166+
167+
/// The Builtin module should be implicitly imported.
168+
Builtin,
169+
170+
/// The regular Swift standard library should be implicitly imported.
171+
Stdlib
172+
};
173+
174+
struct ImplicitImportInfo {
175+
/// The implicit stdlib to import.
176+
ImplicitStdlibKind StdlibKind;
177+
178+
/// Whether we should attempt to import an underlying Clang half of this
179+
/// module.
180+
bool ShouldImportUnderlyingModule;
181+
182+
/// The bridging header path for this module, empty if there is none.
183+
StringRef BridgingHeaderPath;
184+
185+
/// The names of additional modules to be implicitly imported.
186+
SmallVector<Identifier, 4> ModuleNames;
187+
188+
/// An additional list of already-loaded modules which should be implicitly
189+
/// imported.
190+
SmallVector<std::pair<ModuleDecl *, /*exported*/ bool>, 4>
191+
AdditionalModules;
192+
193+
ImplicitImportInfo()
194+
: StdlibKind(ImplicitStdlibKind::None),
195+
ShouldImportUnderlyingModule(false) {}
196+
};
197+
161198
class OverlayFile;
162199

163200
/// The minimum unit of compilation.
@@ -245,6 +282,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
245282
llvm::SmallDenseMap<Identifier, SmallVector<OverlayFile *, 1>>
246283
declaredCrossImports;
247284

285+
/// A description of what should be implicitly imported by each file of this
286+
/// module.
287+
const ImplicitImportInfo ImportInfo;
288+
248289
std::unique_ptr<SourceLookupCache> Cache;
249290
SourceLookupCache &getSourceLookupCache() const;
250291

@@ -274,15 +315,29 @@ class ModuleDecl : public DeclContext, public TypeDecl {
274315
/// \see EntryPointInfoTy
275316
EntryPointInfoTy EntryPointInfo;
276317

277-
ModuleDecl(Identifier name, ASTContext &ctx);
318+
ModuleDecl(Identifier name, ASTContext &ctx, ImplicitImportInfo importInfo);
278319

279320
public:
280-
static ModuleDecl *create(Identifier name, ASTContext &ctx) {
281-
return new (ctx) ModuleDecl(name, ctx);
321+
/// Creates a new module with a given \p name.
322+
///
323+
/// \param importInfo Information about which modules should be implicitly
324+
/// imported by each file of this module.
325+
static ModuleDecl *
326+
create(Identifier name, ASTContext &ctx,
327+
ImplicitImportInfo importInfo = ImplicitImportInfo()) {
328+
return new (ctx) ModuleDecl(name, ctx, importInfo);
282329
}
283330

284331
using Decl::getASTContext;
285332

333+
/// Retrieves information about which modules are implicitly imported by
334+
/// each file of this module.
335+
const ImplicitImportInfo &getImplicitImportInfo() const { return ImportInfo; }
336+
337+
/// Retrieve a list of modules that each file of this module implicitly
338+
/// imports.
339+
ArrayRef<ImplicitImport> getImplicitImports() const;
340+
286341
ArrayRef<FileUnit *> getFiles() {
287342
return Files;
288343
}

include/swift/AST/SourceFile.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ class SourceFile final : public FileUnit {
3333
class Impl;
3434
struct SourceFileSyntaxInfo;
3535

36-
/// The implicit module import that the SourceFile should get.
37-
enum class ImplicitModuleImportKind {
38-
None,
39-
Builtin,
40-
Stdlib
41-
};
42-
4336
/// Possible attributes for imports in source files.
4437
enum class ImportFlags {
4538
/// The imported module is exposed to anyone who imports the parent module.
@@ -128,8 +121,8 @@ class SourceFile final : public FileUnit {
128121

129122
/// This is the list of modules that are imported by this module.
130123
///
131-
/// This is filled in by the import resolution phase.
132-
ArrayRef<ImportedModuleDesc> Imports;
124+
/// This is \c None until it is filled in by the import resolution phase.
125+
Optional<ArrayRef<ImportedModuleDesc>> Imports;
133126

134127
/// A unique identifier representing this file; used to mark private decls
135128
/// within the file to keep them from conflicting with other files in the
@@ -335,12 +328,14 @@ class SourceFile final : public FileUnit {
335328
llvm::StringMap<SourceFilePathInfo> getInfoForUsedFilePaths() const;
336329

337330
SourceFile(ModuleDecl &M, SourceFileKind K, Optional<unsigned> bufferID,
338-
ImplicitModuleImportKind ModImpKind, bool KeepParsedTokens = false,
339-
bool KeepSyntaxTree = false, ParsingOptions parsingOpts = {});
331+
bool KeepParsedTokens = false, bool KeepSyntaxTree = false,
332+
ParsingOptions parsingOpts = {});
340333

341334
~SourceFile();
342335

343-
void addImports(ArrayRef<ImportedModuleDesc> IM);
336+
/// Set the imports for this source file. This gets called by import
337+
/// resolution.
338+
void setImports(ArrayRef<ImportedModuleDesc> imports);
344339

345340
enum ImportQueryKind {
346341
/// Return the results for testable or private imports.

include/swift/AST/TypeCheckRequests.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Evaluator.h"
2424
#include "swift/AST/Pattern.h"
2525
#include "swift/AST/SimpleRequest.h"
26+
#include "swift/AST/SourceFile.h"
2627
#include "swift/AST/TypeResolutionStage.h"
2728
#include "swift/Basic/AnyValue.h"
2829
#include "swift/Basic/Statistic.h"
@@ -2344,6 +2345,45 @@ class SimpleDidSetRequest
23442345
}
23452346
};
23462347

2348+
/// A module which has been implicitly imported.
2349+
struct ImplicitImport {
2350+
using ImportOptions = SourceFile::ImportOptions;
2351+
2352+
ModuleDecl *Module;
2353+
ImportOptions Options;
2354+
2355+
ImplicitImport(ModuleDecl *module, ImportOptions opts = {})
2356+
: Module(module), Options(opts) {}
2357+
2358+
friend bool operator==(const ImplicitImport &lhs,
2359+
const ImplicitImport &rhs) {
2360+
return lhs.Module == rhs.Module &&
2361+
lhs.Options.toRaw() == rhs.Options.toRaw();
2362+
}
2363+
};
2364+
2365+
void simple_display(llvm::raw_ostream &out, const ImplicitImport &import);
2366+
2367+
/// Computes the loaded modules that should be implicitly imported by each file
2368+
/// of a given module.
2369+
class ModuleImplicitImportsRequest
2370+
: public SimpleRequest<ModuleImplicitImportsRequest,
2371+
ArrayRef<ImplicitImport>(ModuleDecl *),
2372+
RequestFlags::Cached> {
2373+
public:
2374+
using SimpleRequest::SimpleRequest;
2375+
2376+
private:
2377+
friend SimpleRequest;
2378+
2379+
ArrayRef<ImplicitImport>
2380+
evaluate(Evaluator &evaluator, ModuleDecl *module) const;
2381+
2382+
public:
2383+
// Cached.
2384+
bool isCached() const { return true; }
2385+
};
2386+
23472387
// Allow AnyValue to compare two Type values, even though Type doesn't
23482388
// support ==.
23492389
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ SWIFT_REQUEST(TypeChecker, ValidatePrecedenceGroupRequest,
128128
Cached, NoLocationInfo)
129129
SWIFT_REQUEST(TypeChecker, MangleLocalTypeDeclRequest,
130130
std::string(const TypeDecl *), Cached, NoLocationInfo)
131+
SWIFT_REQUEST(TypeChecker, ModuleImplicitImportsRequest,
132+
ArrayRef<ImplicitImport>(ModuleDecl *), Cached, NoLocationInfo)
131133
SWIFT_REQUEST(TypeChecker, NamingPatternRequest,
132134
NamedPattern *(VarDecl *), SeparatelyCached, NoLocationInfo)
133135
SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,

include/swift/Frontend/Frontend.h

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,15 @@ class CompilerInvocation {
335335
/// in generating a cached PCH file for the bridging header.
336336
std::string getPCHHash() const;
337337

338-
SourceFile::ImplicitModuleImportKind getImplicitModuleImportKind() const {
338+
/// Retrieve the stdlib kind to implicitly import.
339+
ImplicitStdlibKind getImplicitStdlibKind() const {
339340
if (getInputKind() == InputFileKind::SIL) {
340-
return SourceFile::ImplicitModuleImportKind::None;
341+
return ImplicitStdlibKind::None;
341342
}
342343
if (getParseStdlib()) {
343-
return SourceFile::ImplicitModuleImportKind::Builtin;
344+
return ImplicitStdlibKind::Builtin;
344345
}
345-
return SourceFile::ImplicitModuleImportKind::Stdlib;
346+
return ImplicitStdlibKind::Stdlib;
346347
}
347348

348349
/// Performs input setup common to these tools:
@@ -654,7 +655,6 @@ class CompilerInstance {
654655
private:
655656
SourceFile *
656657
createSourceFileForMainModule(SourceFileKind FileKind,
657-
SourceFile::ImplicitModuleImportKind ImportKind,
658658
Optional<unsigned> BufferID,
659659
SourceFile::ParsingOptions options = {});
660660

@@ -667,38 +667,17 @@ class CompilerInstance {
667667
private:
668668
/// Load stdlib & return true if should continue, i.e. no error
669669
bool loadStdlib();
670-
ModuleDecl *importUnderlyingModule();
671-
ModuleDecl *importBridgingHeader();
672670

673-
void
674-
getImplicitlyImportedModules(SmallVectorImpl<ModuleDecl *> &importModules);
675-
676-
public: // for static functions in Frontend.cpp
677-
struct ImplicitImports {
678-
SourceFile::ImplicitModuleImportKind kind;
679-
ModuleDecl *objCModuleUnderlyingMixedFramework;
680-
ModuleDecl *headerModule;
681-
SmallVector<ModuleDecl *, 4> modules;
682-
683-
explicit ImplicitImports(CompilerInstance &compiler);
684-
};
685-
686-
static void addAdditionalInitialImportsTo(
687-
SourceFile *SF, const ImplicitImports &implicitImports);
688-
689-
private:
690-
void addMainFileToModule(const ImplicitImports &implicitImports);
671+
/// Retrieve a description of which modules should be implicitly imported.
672+
ImplicitImportInfo getImplicitImportInfo() const;
691673

692674
void performSemaUpTo(SourceFile::ASTStage_t LimitStage);
693-
void parseAndCheckTypesUpTo(const ImplicitImports &implicitImports,
694-
SourceFile::ASTStage_t LimitStage);
675+
void parseAndCheckTypesUpTo(SourceFile::ASTStage_t LimitStage);
695676

696-
void parseLibraryFile(unsigned BufferID,
697-
const ImplicitImports &implicitImports);
677+
void parseLibraryFile(unsigned BufferID);
698678

699679
/// Return true if had load error
700-
bool
701-
parsePartialModulesAndLibraryFiles(const ImplicitImports &implicitImports);
680+
bool parsePartialModulesAndLibraryFiles();
702681

703682
void forEachFileToTypeCheck(llvm::function_ref<void(SourceFile &)> fn);
704683

include/swift/Frontend/FrontendOptions.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ namespace swift {
3333
class FrontendOptions {
3434
friend class ArgsToFrontendOptionsConverter;
3535

36+
/// A list of arbitrary modules to import and make implicitly visible.
37+
std::vector<std::string> ImplicitImportModuleNames;
38+
3639
public:
3740
FrontendInputsAndOutputs InputsAndOutputs;
3841

@@ -44,9 +47,6 @@ class FrontendOptions {
4447

4548
bool isOutputFileDirectory() const;
4649

47-
/// A list of arbitrary modules to import and make implicitly visible.
48-
std::vector<std::string> ImplicitImportModuleNames;
49-
5050
/// An Objective-C header to import and make implicitly visible.
5151
std::string ImplicitObjCHeaderPath;
5252

@@ -322,6 +322,12 @@ class FrontendOptions {
322322
const PrimarySpecificPaths &
323323
getPrimarySpecificPathsForPrimary(StringRef) const;
324324

325+
/// Retrieves the list of arbitrary modules to import and make implicitly
326+
/// visible.
327+
ArrayRef<std::string> getImplicitImportModuleNames() const {
328+
return ImplicitImportModuleNames;
329+
}
330+
325331
private:
326332
static bool canActionEmitDependencies(ActionType);
327333
static bool canActionEmitReferenceDependencies(ActionType);

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ namespace {
631631
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
632632
printCommon(TAD, "typealias");
633633
PrintWithColorRAII(OS, TypeColor) << " type='";
634-
if (auto underlying = TAD->getUnderlyingType()) {
634+
if (auto underlying = TAD->getCachedUnderlyingType()) {
635635
PrintWithColorRAII(OS, TypeColor)
636636
<< underlying.getString();
637637
} else {

0 commit comments

Comments
 (0)