Skip to content

Commit 990d527

Browse files
committed
[Macros] Make 'LoadedCompilerPlugin' a wrapper of PointerUnion
1 parent 6abe93e commit 990d527

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3999,41 +3999,20 @@ class ExpandSynthesizedMemberMacroRequest
39993999
bool isCached() const { return true; }
40004000
};
40014001

4002-
/// Load a plugin module with the given name.
4003-
///
4004-
///
4002+
/// Represent a loaded plugin either an in-process library or an executable.
40054003
class LoadedCompilerPlugin {
4006-
enum class PluginKind : uint8_t {
4007-
None,
4008-
InProcess,
4009-
Executable,
4010-
};
4011-
PluginKind kind;
4012-
void *ptr;
4013-
4014-
LoadedCompilerPlugin(PluginKind kind, void *ptr) : kind(kind), ptr(ptr) {
4015-
assert(ptr != nullptr || kind == PluginKind::None);
4016-
}
4004+
llvm::PointerUnion<LoadedLibraryPlugin *, LoadedExecutablePlugin *> ptr;
40174005

40184006
public:
4019-
LoadedCompilerPlugin(std::nullptr_t) : kind(PluginKind::None), ptr(nullptr) {}
4020-
4021-
static LoadedCompilerPlugin inProcess(LoadedLibraryPlugin *ptr) {
4022-
return {PluginKind::InProcess, ptr};
4023-
}
4024-
static LoadedCompilerPlugin executable(LoadedExecutablePlugin *ptr) {
4025-
return {PluginKind::Executable, ptr};
4026-
}
4007+
LoadedCompilerPlugin(std::nullptr_t) : ptr(nullptr) {}
4008+
LoadedCompilerPlugin(LoadedLibraryPlugin *ptr) : ptr(ptr){};
4009+
LoadedCompilerPlugin(LoadedExecutablePlugin *ptr) : ptr(ptr){};
40274010

4028-
LoadedLibraryPlugin *getAsInProcessPlugin() const {
4029-
return kind == PluginKind::InProcess
4030-
? static_cast<LoadedLibraryPlugin *>(ptr)
4031-
: nullptr;
4011+
LoadedLibraryPlugin *getAsLibraryPlugin() const {
4012+
return ptr.dyn_cast<LoadedLibraryPlugin *>();
40324013
}
40334014
LoadedExecutablePlugin *getAsExecutablePlugin() const {
4034-
return kind == PluginKind::Executable
4035-
? static_cast<LoadedExecutablePlugin *>(ptr)
4036-
: nullptr;
4015+
return ptr.dyn_cast<LoadedExecutablePlugin *>();
40374016
}
40384017
};
40394018

lib/Sema/TypeCheckMacros.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,14 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
366366
Identifier moduleName) const {
367367
// Check dynamic link library plugins.
368368
// i.e. '-plugin-path', and '-load-plugin-library'.
369-
if (auto found = loadLibraryPluginByName(*ctx, moduleName))
370-
return LoadedCompilerPlugin::inProcess(found);
369+
if (auto found = loadLibraryPluginByName(*ctx, moduleName)) {
370+
return found;
371+
}
371372

372373
// Fall back to executable plugins.
373374
// i.e. '-external-plugin-path', and '-load-plugin-executable'.
374375
if (auto *found = loadExecutablePluginByName(*ctx, moduleName)) {
375-
return LoadedCompilerPlugin::executable(found);
376+
return found;
376377
}
377378

378379
return nullptr;
@@ -429,7 +430,7 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
429430
LoadedCompilerPlugin loaded =
430431
evaluateOrDefault(evaluator, loadRequest, nullptr);
431432

432-
if (auto loadedLibrary = loaded.getAsInProcessPlugin()) {
433+
if (auto loadedLibrary = loaded.getAsLibraryPlugin()) {
433434
if (auto inProcess = resolveInProcessMacro(
434435
*ctx, moduleName, typeName, loadedLibrary))
435436
return *inProcess;

0 commit comments

Comments
 (0)