Skip to content

Commit 51fcde7

Browse files
authored
Merge pull request #62086 from DougGregor/macro-decl
2 parents 21b6c08 + 3a172fb commit 51fcde7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+627
-238
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,6 @@ class ASTContext final {
352352
llvm::SmallPtrSet<DerivativeAttr *, 1>>
353353
DerivativeAttrs;
354354

355-
/// Cache of compiler plugins keyed by their name.
356-
llvm::StringMap<CompilerPlugin> LoadedPlugins;
357-
358-
/// Cache of loaded symbols.
359-
llvm::StringMap<void *> LoadedSymbols;
360-
361355
private:
362356
/// The current generation number, which reflects the number of
363357
/// times that external modules have been loaded.
@@ -1452,8 +1446,11 @@ class ASTContext final {
14521446
/// The declared interface type of Builtin.TheTupleType.
14531447
BuiltinTupleType *getBuiltinTupleType();
14541448

1455-
/// Finds the loaded compiler plugin given its name.
1456-
CompilerPlugin *getLoadedPlugin(StringRef name);
1449+
/// Finds the loaded compiler plugins with the given name.
1450+
TinyPtrVector<CompilerPlugin *> getLoadedPlugins(StringRef name);
1451+
1452+
/// Add a loaded plugin with the given name.
1453+
void addLoadedPlugin(StringRef name, CompilerPlugin *plugin);
14571454

14581455
/// Finds the address of the given symbol. If `libraryHandleHint` is non-null,
14591456
/// search within the library.

include/swift/AST/Decl.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ enum class DescriptiveDeclKind : uint8_t {
192192
Requirement,
193193
OpaqueResultType,
194194
OpaqueVarType,
195+
Macro,
195196
MacroExpansion
196197
};
197198

@@ -8270,6 +8271,68 @@ class MissingMemberDecl : public Decl {
82708271
}
82718272
};
82728273

8274+
/// Provides a declaration of a macro.
8275+
///
8276+
/// Macros are defined externally via conformances to the 'Macro' type
8277+
/// that is part of swift-syntax, and are introduced into the compiler via
8278+
/// various mechanisms (built-in macros are linked in directly, plugin macros
8279+
/// are introduced via compiler plugins, and so on). They have no explicit
8280+
/// representation in the source code, but are still declarations.
8281+
class MacroDecl : public GenericContext, public ValueDecl {
8282+
public:
8283+
/// The kind of macro, which determines how it can be used in source code.
8284+
enum Kind: uint8_t {
8285+
/// An expression macro.
8286+
Expression,
8287+
};
8288+
8289+
/// Describes how the macro is implemented.
8290+
enum class ImplementationKind: uint8_t {
8291+
/// The macro is built-in to the compiler, linked against the same
8292+
/// underlying syntax tree libraries.
8293+
Builtin,
8294+
8295+
/// The macro was defined in a compiler plugin.
8296+
Plugin,
8297+
};
8298+
8299+
/// The kind of macro.
8300+
const Kind kind;
8301+
8302+
/// How the macro is implemented.
8303+
const ImplementationKind implementationKind;
8304+
8305+
/// Supplemental modules that should be imported when
8306+
const ArrayRef<ModuleDecl *> supplementalSignatureModules;
8307+
8308+
/// An opaque handle to the representation of the macro.
8309+
void * const opaqueHandle;
8310+
8311+
public:
8312+
MacroDecl(
8313+
Kind kind, ImplementationKind implementationKind, Identifier name,
8314+
ModuleDecl *owningModule,
8315+
ArrayRef<ModuleDecl *> supplementalSignatureModules,
8316+
void *opaqueHandle
8317+
);
8318+
8319+
SourceRange getSourceRange() const;
8320+
8321+
static bool classof(const DeclContext *C) {
8322+
if (auto D = C->getAsDecl())
8323+
return classof(D);
8324+
return false;
8325+
}
8326+
8327+
static bool classof(const Decl *D) {
8328+
return D->getKind() == DeclKind::Macro;
8329+
}
8330+
8331+
using DeclContext::operator new;
8332+
using DeclContext::operator delete;
8333+
using Decl::getASTContext;
8334+
};
8335+
82738336
class MacroExpansionDecl : public Decl {
82748337
SourceLoc PoundLoc;
82758338
DeclNameRef Macro;

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ enum class DeclContextKind : unsigned {
100100
FileUnit,
101101
GenericTypeDecl,
102102
ExtensionDecl,
103-
Last_DeclContextKind = ExtensionDecl
103+
MacroDecl,
104+
Last_DeclContextKind = MacroDecl
104105
};
105106

106107
/// Kinds of DeclContexts after deserialization.
@@ -277,6 +278,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
277278
case DeclContextKind::EnumElementDecl:
278279
case DeclContextKind::GenericTypeDecl:
279280
case DeclContextKind::ExtensionDecl:
281+
case DeclContextKind::MacroDecl:
280282
return ASTHierarchy::Decl;
281283
}
282284
llvm_unreachable("Unhandled DeclContextKind");

include/swift/AST/DeclNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ ABSTRACT_DECL(Value, Decl)
171171
ABSTRACT_FUNCTION_DECL(Func, AbstractFunctionDecl)
172172
ABSTRACT_FUNCTION_DECL(Accessor, FuncDecl)
173173
DECL_RANGE(AbstractFunction, Constructor, Accessor)
174+
GENERIC_VALUE_DECL(Macro, ValueDecl)
174175
CONTEXT_VALUE_DECL(EnumElement, ValueDecl)
175176
DECL_RANGE(Value, Enum, EnumElement)
176177

include/swift/AST/Macro.h

Lines changed: 0 additions & 86 deletions
This file was deleted.

include/swift/AST/TypeCheckRequests.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,16 +3738,16 @@ class SynthesizeHasSymbolQueryRequest
37383738
/// module.
37393739
class MacroLookupRequest
37403740
: public SimpleRequest<MacroLookupRequest,
3741-
ArrayRef<Macro *>(Identifier, ModuleDecl *),
3741+
ArrayRef<MacroDecl *>(Identifier, ModuleDecl *),
37423742
RequestFlags::Cached> {
37433743
public:
37443744
using SimpleRequest::SimpleRequest;
37453745

37463746
private:
37473747
friend SimpleRequest;
37483748

3749-
ArrayRef<Macro *> evaluate(Evaluator &evaluator,
3750-
Identifier macroName, ModuleDecl *mod) const;
3749+
ArrayRef<MacroDecl *> evaluate(Evaluator &evaluator,
3750+
Identifier macroName, ModuleDecl *mod) const;
37513751

37523752
public:
37533753
bool isCached() const { return true; }
@@ -3758,7 +3758,6 @@ void simple_display(llvm::raw_ostream &out, Type value);
37583758
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);
37593759
void simple_display(llvm::raw_ostream &out, ImplicitMemberAction action);
37603760
void simple_display(llvm::raw_ostream &out, ResultBuilderBodyPreCheck pck);
3761-
void simple_display(llvm::raw_ostream &out, const Macro *macro);
37623761

37633762
#define SWIFT_TYPEID_ZONE TypeChecker
37643763
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"

include/swift/AST/TypeMemberVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
4141
BAD_MEMBER(TopLevelCode)
4242
BAD_MEMBER(Operator)
4343
BAD_MEMBER(PrecedenceGroup)
44+
BAD_MEMBER(Macro)
4445

4546
// The children of these are automatically inserted into the
4647
// surrounding context.

include/swift/IDE/CodeCompletionResult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ enum class CodeCompletionDeclKind : uint8_t {
117117
LocalVar,
118118
GlobalVar,
119119
PrecedenceGroup,
120+
Macro,
120121
};
121122

122123
enum class CodeCompletionLiteralKind : uint8_t {

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4906,14 +4906,6 @@ class ConstraintSystem {
49064906
ConstraintLocator *locator,
49074907
OpenedTypeMap *replacements = nullptr);
49084908

4909-
#if SWIFT_SWIFT_PARSER
4910-
/// Retrieve the opened type of a macro with the given name.
4911-
///
4912-
/// \returns The opened type of the macro with this name, or the null \c Type
4913-
/// if no such macro exists.
4914-
Type getTypeOfMacroReference(Identifier macroName, Expr *anchor);
4915-
#endif
4916-
49174909
/// Retrieve a list of generic parameter types solver has "opened" (replaced
49184910
/// with a type variable) at the given location.
49194911
ArrayRef<OpenedType> getOpenedTypes(ConstraintLocator *locator) const {

lib/AST/ASTContext.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,15 @@ struct ASTContext::Implementation {
515515

516516
llvm::StringMap<OptionSet<SearchPathKind>> SearchPathsSet;
517517

518+
/// Cache of compiler plugins keyed by their name.
519+
///
520+
/// Names can be overloaded, so there can be multiple plugins with the same
521+
/// name.
522+
llvm::StringMap<TinyPtrVector<CompilerPlugin*>> LoadedPlugins;
523+
524+
/// Cache of loaded symbols.
525+
llvm::StringMap<void *> LoadedSymbols;
526+
518527
/// The permanent arena.
519528
Arena Permanent;
520529

@@ -579,6 +588,12 @@ ASTContext::Implementation::Implementation()
579588
ASTContext::Implementation::~Implementation() {
580589
for (auto &cleanup : Cleanups)
581590
cleanup();
591+
592+
for (const auto &pluginsByName : LoadedPlugins) {
593+
for (auto plugin : pluginsByName.second) {
594+
delete plugin;
595+
}
596+
}
582597
}
583598

584599
ConstraintCheckerArenaRAII::
@@ -6047,16 +6062,21 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() {
60476062
return result;
60486063
}
60496064

6050-
CompilerPlugin *ASTContext::getLoadedPlugin(StringRef name) {
6051-
auto lookup = LoadedPlugins.find(name);
6052-
if (lookup == LoadedPlugins.end())
6053-
return nullptr;
6054-
return &lookup->second;
6065+
TinyPtrVector<CompilerPlugin *> ASTContext::getLoadedPlugins(StringRef name) {
6066+
auto &loadedPlugins = getImpl().LoadedPlugins;
6067+
auto lookup = loadedPlugins.find(name);
6068+
if (lookup == loadedPlugins.end())
6069+
return { };
6070+
return lookup->second;
6071+
}
6072+
6073+
void ASTContext::addLoadedPlugin(StringRef name, CompilerPlugin *plugin) {
6074+
getImpl().LoadedPlugins[name].push_back(plugin);
60556075
}
60566076

60576077
void *ASTContext::getAddressOfSymbol(const char *name,
60586078
void *libraryHandleHint) {
6059-
auto lookup = LoadedSymbols.try_emplace(name, nullptr);
6079+
auto lookup = getImpl().LoadedSymbols.try_emplace(name, nullptr);
60606080
void *&address = lookup.first->getValue();
60616081
#if !defined(_WIN32)
60626082
if (lookup.second) {

0 commit comments

Comments
 (0)