Skip to content

Commit 5ab6b72

Browse files
committed
[Macros] Turn Macro into a declaration node.
Although the declaration of macros doesn't appear in Swift source code that uses macros, they still operate as declarations within the language. Rework `Macro` as `MacroDecl`, a generic value declaration, which appropriate models its place in the language. The vast majority of this change is in extending all of the various switches on declaration kinds to account for macros.
1 parent 4b87cb7 commit 5ab6b72

39 files changed

+226
-106
lines changed

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 {

lib/AST/ASTDumper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ namespace {
698698
printGenericParameters(OS, AFD->getParsedGenericParams());
699699
if (auto *GTD = dyn_cast<GenericTypeDecl>(VD))
700700
printGenericParameters(OS, GTD->getParsedGenericParams());
701+
if (auto *MD = dyn_cast<MacroDecl>(VD))
702+
printGenericParameters(OS, MD->getParsedGenericParams());
701703

702704
if (auto *var = dyn_cast<VarDecl>(VD)) {
703705
PrintWithColorRAII(OS, TypeColor) << " type='";
@@ -1307,6 +1309,11 @@ namespace {
13071309
PrintWithColorRAII(OS, ParenthesisColor) << ')';
13081310
}
13091311

1312+
void visitMacroDecl(MacroDecl *MD) {
1313+
printCommon(MD, "macro_decl");
1314+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
1315+
}
1316+
13101317
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {
13111318
printCommon(MED, "macro_expansion_decl ");
13121319
OS << MED->getMacro();
@@ -1425,6 +1432,10 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
14251432
case DeclContextKind::EnumElementDecl:
14261433
printName(os, cast<EnumElementDecl>(dc)->getName());
14271434
break;
1435+
1436+
case DeclContextKind::MacroDecl:
1437+
printName(os, cast<MacroDecl>(dc)->getName());
1438+
break;
14281439
}
14291440
}
14301441

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,9 @@ void ASTMangler::appendContext(const DeclContext *ctx, StringRef useModuleName)
23792379
case DeclContextKind::TopLevelCodeDecl:
23802380
// Mangle the containing module context.
23812381
return appendContext(ctx->getParent(), useModuleName);
2382+
2383+
case DeclContextKind::MacroDecl:
2384+
llvm_unreachable("macro declarations are never mangled");
23822385
}
23832386

23842387
llvm_unreachable("bad decl context");

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,10 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
18951895
return true;
18961896
}
18971897

1898+
// Skip macros, which don't have an in-source representation.
1899+
if (isa<MacroDecl>(D))
1900+
return false;
1901+
18981902
// Skip declarations that are not accessible.
18991903
if (auto *VD = dyn_cast<ValueDecl>(D)) {
19001904
if (Options.AccessFilter > AccessLevel::Private &&
@@ -4433,6 +4437,10 @@ void PrintAST::visitMissingMemberDecl(MissingMemberDecl *decl) {
44334437
Printer << " */";
44344438
}
44354439

4440+
void PrintAST::visitMacroDecl(MacroDecl *decl) {
4441+
// No in-source representation of macros.
4442+
}
4443+
44364444
void PrintAST::visitMacroExpansionDecl(MacroExpansionDecl *decl) {
44374445
Printer << '#' << decl->getMacro();
44384446
if (decl->getArgs()) {

0 commit comments

Comments
 (0)