Skip to content

Commit 9e00582

Browse files
authored
Merge pull request #66724 from rintaro/5.9-macros-serialization-pluginopts
[5.9][Macros] Update plugin search options serialization
2 parents f679381 + 586a746 commit 9e00582

File tree

12 files changed

+305
-223
lines changed

12 files changed

+305
-223
lines changed

include/swift/AST/PluginLoader.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class ASTContext;
2828
/// * Load plugins resolving VFS paths
2929
/// * Track plugin dependencies
3030
class PluginLoader {
31+
public:
32+
struct PluginEntry {
33+
StringRef libraryPath;
34+
StringRef executablePath;
35+
};
36+
37+
private:
3138
/// Plugin registry. Lazily populated by get/setRegistry().
3239
/// NOTE: Do not reference this directly. Use getRegistry().
3340
PluginRegistry *Registry = nullptr;
@@ -38,16 +45,15 @@ class PluginLoader {
3845
ASTContext &Ctx;
3946
DependencyTracker *DepTracker;
4047

41-
/// Map a module name to an executable plugin path that provides the module.
42-
llvm::DenseMap<swift::Identifier, llvm::StringRef> ExecutablePluginPaths;
48+
/// Map a module name to an plugin entry that provides the module.
49+
llvm::Optional<llvm::DenseMap<swift::Identifier, PluginEntry>> PluginMap;
4350

44-
void createModuleToExecutablePluginMap();
51+
/// Get or lazily create and populate 'PluginMap'.
52+
llvm::DenseMap<swift::Identifier, PluginEntry> &getPluginMap();
4553

4654
public:
4755
PluginLoader(ASTContext &Ctx, DependencyTracker *DepTracker)
48-
: Ctx(Ctx), DepTracker(DepTracker) {
49-
createModuleToExecutablePluginMap();
50-
}
56+
: Ctx(Ctx), DepTracker(DepTracker) {}
5157

5258
void setRegistry(PluginRegistry *newValue);
5359
PluginRegistry *getRegistry();
@@ -63,8 +69,7 @@ class PluginLoader {
6369
/// 'loadExecutablePlugin()'.
6470
/// * (libPath: some, execPath: some) - load the executable path by
6571
/// 'loadExecutablePlugin()' and let the plugin load the libPath via IPC.
66-
std::pair<std::string, std::string>
67-
lookupPluginByModuleName(Identifier moduleName);
72+
const PluginEntry &lookupPluginByModuleName(Identifier moduleName);
6873

6974
/// Load the specified dylib plugin path resolving the path with the
7075
/// current VFS. If it fails to load the plugin, a diagnostic is emitted, and

include/swift/AST/SearchPathOptions.h

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#define SWIFT_AST_SEARCHPATHOPTIONS_H
1515

1616
#include "swift/Basic/ArrayRefView.h"
17+
#include "swift/Basic/ExternalUnion.h"
1718
#include "swift/Basic/PathRemapper.h"
18-
#include "swift/Basic/TaggedUnion.h"
1919
#include "llvm/ADT/Hashing.h"
2020
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2121
#include "llvm/ADT/StringMap.h"
@@ -187,25 +187,81 @@ struct ExternalPluginSearchPathAndServerPath {
187187
std::string ServerPath;
188188
};
189189

190-
namespace PluginSearchOption {
191-
struct LoadPluginLibrary {
192-
std::string LibraryPath;
193-
};
194-
struct LoadPluginExecutable {
195-
std::string ExecutablePath;
196-
std::vector<std::string> ModuleNames;
197-
};
198-
struct PluginPath {
199-
std::string SearchPath;
200-
};
201-
struct ExternalPluginPath {
202-
std::string SearchPath;
203-
std::string ServerPath;
204-
};
190+
class PluginSearchOption {
191+
public:
192+
struct LoadPluginLibrary {
193+
std::string LibraryPath;
194+
};
195+
struct LoadPluginExecutable {
196+
std::string ExecutablePath;
197+
std::vector<std::string> ModuleNames;
198+
};
199+
struct PluginPath {
200+
std::string SearchPath;
201+
};
202+
struct ExternalPluginPath {
203+
std::string SearchPath;
204+
std::string ServerPath;
205+
};
206+
207+
enum class Kind : uint8_t {
208+
LoadPluginLibrary,
209+
LoadPluginExecutable,
210+
PluginPath,
211+
ExternalPluginPath,
212+
};
205213

206-
using Value = TaggedUnion<LoadPluginLibrary, LoadPluginExecutable, PluginPath,
207-
ExternalPluginPath>;
208-
} // namespace PluginSearchOption
214+
private:
215+
using Members = ExternalUnionMembers<LoadPluginLibrary, LoadPluginExecutable,
216+
PluginPath, ExternalPluginPath>;
217+
static Members::Index getIndexForKind(Kind kind) {
218+
switch (kind) {
219+
case Kind::LoadPluginLibrary:
220+
return Members::indexOf<LoadPluginLibrary>();
221+
case Kind::LoadPluginExecutable:
222+
return Members::indexOf<LoadPluginExecutable>();
223+
case Kind::PluginPath:
224+
return Members::indexOf<PluginPath>();
225+
case Kind::ExternalPluginPath:
226+
return Members::indexOf<ExternalPluginPath>();
227+
}
228+
};
229+
using Storage = ExternalUnion<Kind, Members, getIndexForKind>;
230+
231+
Kind kind;
232+
Storage storage;
233+
234+
public:
235+
PluginSearchOption(const LoadPluginLibrary &v)
236+
: kind(Kind::LoadPluginLibrary) {
237+
storage.emplace<LoadPluginLibrary>(kind, v);
238+
}
239+
PluginSearchOption(const LoadPluginExecutable &v)
240+
: kind(Kind::LoadPluginExecutable) {
241+
storage.emplace<LoadPluginExecutable>(kind, v);
242+
}
243+
PluginSearchOption(const PluginPath &v) : kind(Kind::PluginPath) {
244+
storage.emplace<PluginPath>(kind, v);
245+
}
246+
PluginSearchOption(const ExternalPluginPath &v)
247+
: kind(Kind::ExternalPluginPath) {
248+
storage.emplace<ExternalPluginPath>(kind, v);
249+
}
250+
251+
Kind getKind() const { return kind; }
252+
253+
template <typename T>
254+
const T *dyn_cast() const {
255+
if (Members::indexOf<T>() != getIndexForKind(kind))
256+
return nullptr;
257+
return &storage.get<T>(kind);
258+
}
259+
260+
template <typename T>
261+
const T &get() const {
262+
return storage.get<T>(kind);
263+
}
264+
};
209265

210266
/// Options for controlling search path behavior.
211267
class SearchPathOptions {
@@ -383,7 +439,7 @@ class SearchPathOptions {
383439
std::vector<std::string> RuntimeLibraryPaths;
384440

385441
/// Plugin search path options.
386-
std::vector<PluginSearchOption::Value> PluginSearchOpts;
442+
std::vector<PluginSearchOption> PluginSearchOpts;
387443

388444
/// Don't look in for compiler-provided modules.
389445
bool SkipRuntimeLibraryImportPaths = false;

include/swift/Serialization/SerializationOptions.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H
1414
#define SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H
1515

16+
#include "swift/AST/SearchPathOptions.h"
1617
#include "swift/Basic/LLVM.h"
1718
#include "swift/Basic/PathRemapper.h"
1819
#include "llvm/Support/VersionTuple.h"
@@ -43,10 +44,7 @@ namespace swift {
4344
StringRef ModuleLinkName;
4445
StringRef ModuleInterface;
4546
std::vector<std::string> ExtraClangOptions;
46-
std::vector<std::string> PluginSearchPaths;
47-
std::vector<std::string> ExternalPluginSearchPaths;
48-
std::vector<std::string> CompilerPluginLibraryPaths;
49-
std::vector<std::string> CompilerPluginExecutablePaths;
47+
std::vector<swift::PluginSearchOption> PluginSearchOptions;
5048

5149
/// Path prefixes that should be rewritten in debug info.
5250
PathRemapper DebuggingOptionsPrefixMap;

include/swift/Serialization/Validation.h

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ struct ValidationInfo {
111111
class ExtendedValidationInfo {
112112
SmallVector<StringRef, 4> ExtraClangImporterOpts;
113113

114-
SmallVector<StringRef, 1> PluginSearchPaths;
115-
SmallVector<StringRef, 1> ExternalPluginSearchPaths;
116-
SmallVector<StringRef, 1> CompilerPluginLibraryPaths;
117-
SmallVector<StringRef, 1> CompilerPluginExecutablePaths;
114+
SmallVector<std::pair<PluginSearchOption::Kind, StringRef>, 2>
115+
PluginSearchOptions;
118116

119117
std::string SDKPath;
120118
StringRef ModuleABIName;
@@ -149,32 +147,13 @@ class ExtendedValidationInfo {
149147
ExtraClangImporterOpts.push_back(option);
150148
}
151149

152-
ArrayRef<StringRef> getPluginSearchPaths() const {
153-
return PluginSearchPaths;
150+
ArrayRef<std::pair<PluginSearchOption::Kind, StringRef>>
151+
getPluginSearchOptions() const {
152+
return PluginSearchOptions;
154153
}
155-
void addPluginSearchPath(StringRef path) {
156-
PluginSearchPaths.push_back(path);
157-
}
158-
159-
ArrayRef<StringRef> getExternalPluginSearchPaths() const {
160-
return ExternalPluginSearchPaths;
161-
}
162-
void addExternalPluginSearchPath(StringRef path) {
163-
ExternalPluginSearchPaths.push_back(path);
164-
}
165-
166-
ArrayRef<StringRef> getCompilerPluginLibraryPaths() const {
167-
return CompilerPluginLibraryPaths;
168-
}
169-
void addCompilerPluginLibraryPath(StringRef path) {
170-
CompilerPluginLibraryPaths.push_back(path);
171-
}
172-
173-
ArrayRef<StringRef> getCompilerPluginExecutablePaths() const {
174-
return CompilerPluginExecutablePaths;
175-
}
176-
void addCompilerPluginExecutablePath(StringRef path) {
177-
CompilerPluginExecutablePaths.push_back(path);
154+
void addPluginSearchOption(
155+
const std::pair<PluginSearchOption::Kind, StringRef> &opt) {
156+
PluginSearchOptions.push_back(opt);
178157
}
179158

180159
bool isSIB() const { return Bits.IsSIB; }

0 commit comments

Comments
 (0)