Skip to content

Commit 813dc12

Browse files
committed
[Macros] Use the external module/type name in lieu of macro registration.
A macro declaration contains the external module and type name of the macro's implementation. Use that information to find the macro type (via its type metadata accessor) in a loaded plugin, so we no longer require the "allMacros" array. Instead, each macro implementation type must be a public struct. Since we are now fully dependent on the macro declaration for everything about a macro except its kind, remove most of the query infrastructure for compiler plugins. Replace the macro registration scheme based on the allMacros array with
1 parent b29fcb4 commit 813dc12

File tree

11 files changed

+125
-620
lines changed

11 files changed

+125
-620
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,12 +1439,6 @@ class ASTContext final {
14391439
/// The declared interface type of Builtin.TheTupleType.
14401440
BuiltinTupleType *getBuiltinTupleType();
14411441

1442-
/// Finds the loaded compiler plugins with the given name.
1443-
TinyPtrVector<CompilerPlugin *> getLoadedPlugins(StringRef name);
1444-
1445-
/// Add a loaded plugin with the given name.
1446-
void addLoadedPlugin(StringRef name, CompilerPlugin *plugin);
1447-
14481442
/// Finds the address of the given symbol. If `libraryHandleHint` is non-null,
14491443
/// search within the library.
14501444
void *getAddressOfSymbol(const char *name, void *libraryHandleHint = nullptr);

include/swift/AST/CompilerPlugin.h

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,10 @@ class CompilerPlugin {
5454
// stdlib/toolchain/CompilerPluginSupport.swift.
5555
enum class WitnessTableEntry: unsigned {
5656
ConformanceDescriptor = 0,
57-
// static func _name() -> (UnsafePointer<UInt8>, count: Int8)
58-
Name = 1,
5957
// static func _kind() -> _CompilerPluginKind
60-
Kind = 2,
58+
Kind = 1,
6159
// static func _rewrite(...) -> (UnsafePointer<UInt8>?, count: Int)
62-
Rewrite = 3,
63-
// static func _genericSignature(...) -> (UnsafePointer<UInt8>?, count: Int)
64-
GenericSignature = 4,
65-
// static func _typeSignature(...) -> (UnsafePointer<UInt8>, count: Int)
66-
TypeSignature = 5,
67-
// static func _owningModule(...) -> (UnsafePointer<UInt8>, count: Int)
68-
OwningModule = 6,
69-
// static func _supplementalSignatureModules(...)
70-
// -> (UnsafePointer<UInt8>, count: Int)
71-
SupplementalSignatureModules = 7,
60+
Rewrite = 2,
7261
};
7362

7463
/// The plugin type metadata.
@@ -77,8 +66,6 @@ class CompilerPlugin {
7766
void *parentLibrary;
7867
/// The witness table proving that the plugin conforms to `_CompilerPlugin`.
7968
const void *witnessTable;
80-
/// The plugin name, aka. result of the `_name()` method.
81-
StringRef name;
8269
/// The plugin's kind, aka. result of the `_kind()` method.
8370
Kind kind;
8471

@@ -87,14 +74,10 @@ class CompilerPlugin {
8774
return reinterpret_cast<const Func *const *>(witnessTable)[(unsigned)entry];
8875
}
8976

90-
protected:
77+
public:
9178
CompilerPlugin(const void *metadata, void *parentLibrary, ASTContext &ctx);
9279

9380
private:
94-
/// Invoke the `_name` method. The caller assumes ownership of the result
95-
/// string buffer.
96-
StringRef invokeName() const;
97-
9881
/// Invoke the `_kind` method.
9982
Kind invokeKind() const;
10083

@@ -110,26 +93,6 @@ class CompilerPlugin {
11093
CharSourceRange range, ASTContext &ctx,
11194
SmallVectorImpl<Diagnostic> &diagnostics) const;
11295

113-
/// Invoke the `_genericSignature` method. The caller assumes ownership of the
114-
/// result string buffer.
115-
Optional<StringRef> invokeGenericSignature() const;
116-
117-
/// Invoke the `_typeSignature` method. The caller assumes ownership of the
118-
/// result string buffer.
119-
StringRef invokeTypeSignature() const;
120-
121-
/// Invoke the `_owningModule` method. The caller assumes ownership of the
122-
/// result string buffer.
123-
StringRef invokeOwningModule() const;
124-
125-
/// Invoke the `_supplementalSignatureModules` method. The caller assumes
126-
/// ownership of the result string buffer.
127-
StringRef invokeSupplementalSignatureModules() const;
128-
129-
StringRef getName() const {
130-
return name;
131-
}
132-
13396
Kind getKind() const {
13497
return kind;
13598
}

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,6 @@ REMARK(warning_in_access_notes_file,none,
478478

479479
WARNING(compiler_plugin_not_loaded,none,
480480
"compiler plugin not loaded: %0; loader error: %1", (StringRef, StringRef))
481-
WARNING(compiler_plugin_missing_macro_declaration,none,
482-
"compiler plugin module '%0' (in %1) is missing a top-level computed "
483-
"property 'public var %2: [Any.Type]' to declare all macros; "
484-
"undeclared macros will be ignored", (StringRef, StringRef, StringRef))
485481

486482
#define UNDEFINE_DIAGNOSTIC_MACROS
487483
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6742,12 +6742,16 @@ ERROR(expected_macro_expansion_expr,PointsToFirstBadToken,
67426742
ERROR(macro_undefined,PointsToFirstBadToken,
67436743
"macro %0 is undefined; use `-load-plugin-library` to specify dynamic "
67446744
"libraries that contain this macro", (Identifier))
6745+
ERROR(external_macro_not_found,none,
6746+
"external implementation struct '%0.%1' could not be found in for "
6747+
"macro %2; the type must be public and provided via "
6748+
"'-load-plugin-library'", (StringRef, StringRef, DeclName))
67456749
NOTE(macro_note, none,
6746-
"macro %0: %1", (Identifier, StringRef))
6750+
"%1 (in macro %0)", (DeclName, StringRef))
67476751
WARNING(macro_warning, none,
6748-
"macro %0: %1", (Identifier, StringRef))
6752+
"%1 (in macro %0)", (DeclName, StringRef))
67496753
ERROR(macro_error, none,
6750-
"macro %0: %1", (Identifier, StringRef))
6754+
"%1 (in macro %0)", (DeclName, StringRef))
67516755

67526756
//------------------------------------------------------------------------------
67536757
// MARK: Move Only Errors

lib/AST/ASTContext.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/CompilerPlugin.h"
2323
#include "swift/AST/ConcreteDeclRef.h"
2424
#include "swift/AST/DiagnosticEngine.h"
25+
#include "swift/AST/DiagnosticsFrontend.h"
2526
#include "swift/AST/DiagnosticsSema.h"
2627
#include "swift/AST/DistributedDecl.h"
2728
#include "swift/AST/ExistentialLayout.h"
@@ -514,11 +515,8 @@ struct ASTContext::Implementation {
514515

515516
llvm::StringMap<OptionSet<SearchPathKind>> SearchPathsSet;
516517

517-
/// Cache of compiler plugins keyed by their name.
518-
///
519-
/// Names can be overloaded, so there can be multiple plugins with the same
520-
/// name.
521-
llvm::StringMap<TinyPtrVector<CompilerPlugin*>> LoadedPlugins;
518+
/// Record of loaded plugin modules.
519+
std::vector<std::pair<std::string, void *>> LoadedPluginPaths;
522520

523521
/// Cache of loaded symbols.
524522
llvm::StringMap<void *> LoadedSymbols;
@@ -584,12 +582,6 @@ ASTContext::Implementation::Implementation()
584582
ASTContext::Implementation::~Implementation() {
585583
for (auto &cleanup : Cleanups)
586584
cleanup();
587-
588-
for (const auto &pluginsByName : LoadedPlugins) {
589-
for (auto plugin : pluginsByName.second) {
590-
delete plugin;
591-
}
592-
}
593585
}
594586

595587
ConstraintCheckerArenaRAII::
@@ -6103,16 +6095,24 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() {
61036095
return result;
61046096
}
61056097

6106-
TinyPtrVector<CompilerPlugin *> ASTContext::getLoadedPlugins(StringRef name) {
6107-
auto &loadedPlugins = getImpl().LoadedPlugins;
6108-
auto lookup = loadedPlugins.find(name);
6109-
if (lookup == loadedPlugins.end())
6110-
return { };
6111-
return lookup->second;
6112-
}
6098+
void ASTContext::loadCompilerPlugins() {
6099+
for (auto &path : SearchPathOpts.getCompilerPluginLibraryPaths()) {
6100+
void *lib = nullptr;
6101+
#if !defined(_WIN32)
6102+
lib = dlopen(path.c_str(), RTLD_LAZY|RTLD_LOCAL);
6103+
#endif
6104+
if (!lib) {
6105+
const char *errorMsg = "Unsupported platform";
6106+
#if !defined(_WIN32)
6107+
errorMsg = dlerror();
6108+
#endif
6109+
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
6110+
errorMsg);
6111+
continue;
6112+
}
61136113

6114-
void ASTContext::addLoadedPlugin(StringRef name, CompilerPlugin *plugin) {
6115-
getImpl().LoadedPlugins[name].push_back(plugin);
6114+
getImpl().LoadedPluginPaths.push_back({path, lib});
6115+
}
61166116
}
61176117

61186118
void *ASTContext::getAddressOfSymbol(const char *name,
@@ -6123,6 +6123,15 @@ void *ASTContext::getAddressOfSymbol(const char *name,
61236123
if (lookup.second) {
61246124
auto *handle = libraryHandleHint ? libraryHandleHint : RTLD_DEFAULT;
61256125
address = dlsym(handle, name);
6126+
6127+
// If we didn't know where to look, look specifically in each plugin.
6128+
if (!address && !libraryHandleHint) {
6129+
for (const auto &plugin: getImpl().LoadedPluginPaths) {
6130+
address = dlsym(plugin.second, name);
6131+
if (address)
6132+
break;
6133+
}
6134+
}
61266135
}
61276136
#endif
61286137
return address;

0 commit comments

Comments
 (0)