Skip to content

Commit cb338dd

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 3407d35 commit cb338dd

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
@@ -6736,12 +6736,16 @@ ERROR(expected_macro_expansion_expr,PointsToFirstBadToken,
67366736
ERROR(macro_undefined,PointsToFirstBadToken,
67376737
"macro %0 is undefined; use `-load-plugin-library` to specify dynamic "
67386738
"libraries that contain this macro", (Identifier))
6739+
ERROR(external_macro_not_found,none,
6740+
"external implementation struct '%0.%1' could not be found in for "
6741+
"macro %2; the type must be public and provided via "
6742+
"'-load-plugin-library'", (StringRef, StringRef, DeclName))
67396743
NOTE(macro_note, none,
6740-
"macro %0: %1", (Identifier, StringRef))
6744+
"%1 (in macro %0)", (DeclName, StringRef))
67416745
WARNING(macro_warning, none,
6742-
"macro %0: %1", (Identifier, StringRef))
6746+
"%1 (in macro %0)", (DeclName, StringRef))
67436747
ERROR(macro_error, none,
6744-
"macro %0: %1", (Identifier, StringRef))
6748+
"%1 (in macro %0)", (DeclName, StringRef))
67456749

67466750
//------------------------------------------------------------------------------
67476751
// 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::
@@ -6054,16 +6046,24 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() {
60546046
return result;
60556047
}
60566048

6057-
TinyPtrVector<CompilerPlugin *> ASTContext::getLoadedPlugins(StringRef name) {
6058-
auto &loadedPlugins = getImpl().LoadedPlugins;
6059-
auto lookup = loadedPlugins.find(name);
6060-
if (lookup == loadedPlugins.end())
6061-
return { };
6062-
return lookup->second;
6063-
}
6049+
void ASTContext::loadCompilerPlugins() {
6050+
for (auto &path : SearchPathOpts.getCompilerPluginLibraryPaths()) {
6051+
void *lib = nullptr;
6052+
#if !defined(_WIN32)
6053+
lib = dlopen(path.c_str(), RTLD_LAZY|RTLD_LOCAL);
6054+
#endif
6055+
if (!lib) {
6056+
const char *errorMsg = "Unsupported platform";
6057+
#if !defined(_WIN32)
6058+
errorMsg = dlerror();
6059+
#endif
6060+
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
6061+
errorMsg);
6062+
continue;
6063+
}
60646064

6065-
void ASTContext::addLoadedPlugin(StringRef name, CompilerPlugin *plugin) {
6066-
getImpl().LoadedPlugins[name].push_back(plugin);
6065+
getImpl().LoadedPluginPaths.push_back({path, lib});
6066+
}
60676067
}
60686068

60696069
void *ASTContext::getAddressOfSymbol(const char *name,
@@ -6074,6 +6074,15 @@ void *ASTContext::getAddressOfSymbol(const char *name,
60746074
if (lookup.second) {
60756075
auto *handle = libraryHandleHint ? libraryHandleHint : RTLD_DEFAULT;
60766076
address = dlsym(handle, name);
6077+
6078+
// If we didn't know where to look, look specifically in each plugin.
6079+
if (!address && !libraryHandleHint) {
6080+
for (const auto &plugin: getImpl().LoadedPluginPaths) {
6081+
address = dlsym(plugin.second, name);
6082+
if (address)
6083+
break;
6084+
}
6085+
}
60776086
}
60786087
#endif
60796088
return address;

0 commit comments

Comments
 (0)