Skip to content

Commit 99e1531

Browse files
committed
[Macros] Make 'LoadedCompilerPlugin' a wrapper of PointerUnion
(cherry picked from commit ace080c)
1 parent 9dbe6b0 commit 99e1531

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;
@@ -4018,41 +4017,20 @@ class ExpandSynthesizedMemberMacroRequest
40184017
void noteCycleStep(DiagnosticEngine &diags) const;
40194018
};
40204019

4021-
/// Load a plugin module with the given name.
4022-
///
4023-
///
4020+
/// Represent a loaded plugin either an in-process library or an executable.
40244021
class LoadedCompilerPlugin {
4025-
enum class PluginKind : uint8_t {
4026-
None,
4027-
InProcess,
4028-
Executable,
4029-
};
4030-
PluginKind kind;
4031-
void *ptr;
4032-
4033-
LoadedCompilerPlugin(PluginKind kind, void *ptr) : kind(kind), ptr(ptr) {
4034-
assert(ptr != nullptr || kind == PluginKind::None);
4035-
}
4022+
llvm::PointerUnion<LoadedLibraryPlugin *, LoadedExecutablePlugin *> ptr;
40364023

40374024
public:
4038-
LoadedCompilerPlugin(std::nullptr_t) : kind(PluginKind::None), ptr(nullptr) {}
4039-
4040-
static LoadedCompilerPlugin inProcess(LoadedLibraryPlugin *ptr) {
4041-
return {PluginKind::InProcess, ptr};
4042-
}
4043-
static LoadedCompilerPlugin executable(LoadedExecutablePlugin *ptr) {
4044-
return {PluginKind::Executable, ptr};
4045-
}
4025+
LoadedCompilerPlugin(std::nullptr_t) : ptr(nullptr) {}
4026+
LoadedCompilerPlugin(LoadedLibraryPlugin *ptr) : ptr(ptr){};
4027+
LoadedCompilerPlugin(LoadedExecutablePlugin *ptr) : ptr(ptr){};
40464028

4047-
LoadedLibraryPlugin *getAsInProcessPlugin() const {
4048-
return kind == PluginKind::InProcess
4049-
? static_cast<LoadedLibraryPlugin *>(ptr)
4050-
: nullptr;
4029+
LoadedLibraryPlugin *getAsLibraryPlugin() const {
4030+
return ptr.dyn_cast<LoadedLibraryPlugin *>();
40514031
}
40524032
LoadedExecutablePlugin *getAsExecutablePlugin() const {
4053-
return kind == PluginKind::Executable
4054-
? static_cast<LoadedExecutablePlugin *>(ptr)
4055-
: nullptr;
4033+
return ptr.dyn_cast<LoadedExecutablePlugin *>();
40564034
}
40574035
};
40584036

lib/Sema/TypeCheckMacros.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,14 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
358358
Identifier moduleName) const {
359359
// Check dynamic link library plugins.
360360
// i.e. '-plugin-path', and '-load-plugin-library'.
361-
if (auto found = loadLibraryPluginByName(*ctx, moduleName))
362-
return LoadedCompilerPlugin::inProcess(found);
361+
if (auto found = loadLibraryPluginByName(*ctx, moduleName)) {
362+
return found;
363+
}
363364

364365
// Fall back to executable plugins.
365366
// i.e. '-external-plugin-path', and '-load-plugin-executable'.
366367
if (auto *found = loadExecutablePluginByName(*ctx, moduleName)) {
367-
return LoadedCompilerPlugin::executable(found);
368+
return found;
368369
}
369370

370371
return nullptr;
@@ -421,7 +422,7 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
421422
LoadedCompilerPlugin loaded =
422423
evaluateOrDefault(evaluator, loadRequest, nullptr);
423424

424-
if (auto loadedLibrary = loaded.getAsInProcessPlugin()) {
425+
if (auto loadedLibrary = loaded.getAsLibraryPlugin()) {
425426
if (auto inProcess = resolveInProcessMacro(
426427
*ctx, moduleName, typeName, loadedLibrary))
427428
return *inProcess;

0 commit comments

Comments
 (0)