Skip to content

Commit 70d2964

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

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
#ifndef SWIFT_PLUGIN_REGISTRY_H
13+
#define SWIFT_PLUGIN_REGISTRY_H
1214

1315
#include "llvm/ADT/ArrayRef.h"
1416
#include "llvm/ADT/StringMap.h"
@@ -170,3 +172,5 @@ class PluginRegistry {
170172
};
171173

172174
} // namespace swift
175+
176+
#endif // SWIFT_PLUGIN_REGISTRY_H

include/swift/AST/TypeCheckRequests.h

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/Type.h"
2727
#include "swift/AST/Evaluator.h"
2828
#include "swift/AST/Pattern.h"
29+
#include "swift/AST/PluginRegistry.h"
2930
#include "swift/AST/ProtocolConformance.h"
3031
#include "swift/AST/SimpleRequest.h"
3132
#include "swift/AST/SourceFile.h"
@@ -49,8 +50,6 @@ struct ExternalMacroDefinition;
4950
class ClosureExpr;
5051
class GenericParamList;
5152
class LabeledStmt;
52-
class LoadedExecutablePlugin;
53-
class LoadedLibraryPlugin;
5453
class MacroDefinition;
5554
class PrecedenceGroupDecl;
5655
class PropertyWrapperInitializerInfo;
@@ -3999,41 +3998,20 @@ class ExpandSynthesizedMemberMacroRequest
39993998
bool isCached() const { return true; }
40003999
};
40014000

4002-
/// Load a plugin module with the given name.
4003-
///
4004-
///
4001+
/// Represent a loaded plugin either an in-process library or an executable.
40054002
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-
}
4003+
llvm::PointerUnion<LoadedLibraryPlugin *, LoadedExecutablePlugin *> ptr;
40174004

40184005
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-
}
4006+
LoadedCompilerPlugin(std::nullptr_t) : ptr(nullptr) {}
4007+
LoadedCompilerPlugin(LoadedLibraryPlugin *ptr) : ptr(ptr){};
4008+
LoadedCompilerPlugin(LoadedExecutablePlugin *ptr) : ptr(ptr){};
40274009

4028-
LoadedLibraryPlugin *getAsInProcessPlugin() const {
4029-
return kind == PluginKind::InProcess
4030-
? static_cast<LoadedLibraryPlugin *>(ptr)
4031-
: nullptr;
4010+
LoadedLibraryPlugin *getAsLibraryPlugin() const {
4011+
return ptr.dyn_cast<LoadedLibraryPlugin *>();
40324012
}
40334013
LoadedExecutablePlugin *getAsExecutablePlugin() const {
4034-
return kind == PluginKind::Executable
4035-
? static_cast<LoadedExecutablePlugin *>(ptr)
4036-
: nullptr;
4014+
return ptr.dyn_cast<LoadedExecutablePlugin *>();
40374015
}
40384016
};
40394017

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)