Skip to content

Commit e1102cc

Browse files
authored
Merge pull request #62204 from DougGregor/macro-declarations
Macro declarations
2 parents df81263 + 64f2c8e commit e1102cc

File tree

82 files changed

+1505
-1057
lines changed

Some content is hidden

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

82 files changed

+1505
-1057
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ Entities
362362
entity-spec ::= decl-name label-list? type 'v' ACCESSOR // variable
363363
entity-spec ::= decl-name type 'fp' // generic type parameter
364364
entity-spec ::= decl-name type 'fo' // enum element (currently not used)
365+
entity-spec ::= decl-name label-list? type generic-signature? 'fm' // macro
365366
entity-spec ::= identifier 'Qa' // associated type declaration
366367

367368
ACCESSOR ::= 'm' // materializeForSet

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/ASTScope.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,35 @@ class EnumElementScope : public ASTScopeImpl {
11871187
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
11881188
};
11891189

1190+
class MacroDeclScope final : public ASTScopeImpl {
1191+
public:
1192+
MacroDecl *const decl;
1193+
1194+
MacroDeclScope(MacroDecl *e) : decl(e) {}
1195+
virtual ~MacroDeclScope() {}
1196+
1197+
protected:
1198+
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1199+
1200+
private:
1201+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
1202+
1203+
public:
1204+
std::string getClassName() const override;
1205+
SourceRange
1206+
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1207+
1208+
protected:
1209+
void printSpecifics(llvm::raw_ostream &out) const override;
1210+
1211+
public:
1212+
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
1213+
Decl *getDecl() const { return decl; }
1214+
1215+
protected:
1216+
NullablePtr<const GenericParamList> genericParams() const override;
1217+
};
1218+
11901219
class AbstractStmtScope : public ASTScopeImpl {
11911220
public:
11921221
SourceRange

include/swift/AST/CompilerPlugin.h

Lines changed: 7 additions & 41 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:
91-
CompilerPlugin(const void *metadata, void *parentLibrary, ASTContext &ctx);
77+
CompilerPlugin(
78+
const void *metadata, void *parentLibrary, const void *witnessTable);
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

@@ -103,33 +86,16 @@ class CompilerPlugin {
10386
CompilerPlugin(const CompilerPlugin &) = delete;
10487
CompilerPlugin(CompilerPlugin &&) = default;
10588

89+
/// Try to resolve a compiler plugin given the metadata point.
90+
static CompilerPlugin *fromMetatype(const void *metadata, ASTContext &ctx);
91+
10692
/// Invoke the `_rewrite` method. The caller assumes ownership of the result
10793
/// string buffer and diagnostic buffers.
10894
Optional<NullTerminatedStringRef> invokeRewrite(
10995
StringRef targetModuleName, StringRef filePath, StringRef sourceFileText,
11096
CharSourceRange range, ASTContext &ctx,
11197
SmallVectorImpl<Diagnostic> &diagnostics) const;
11298

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-
13399
Kind getKind() const {
134100
return kind;
135101
}

include/swift/AST/Decl.h

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8280,44 +8280,47 @@ class MissingMemberDecl : public Decl {
82808280
/// representation in the source code, but are still declarations.
82818281
class MacroDecl : public GenericContext, public ValueDecl {
82828282
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-
};
8283+
/// The location of the 'macro' keyword.
8284+
SourceLoc macroLoc;
82888285

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,
8286+
/// The parameter list for a function-like macro.
8287+
ParameterList *parameterList;
82948288

8295-
/// The macro was defined in a compiler plugin.
8296-
Plugin,
8297-
};
8289+
/// Where the '->' or ':' is located, for a function- or value-like macro,
8290+
/// respectively.
8291+
SourceLoc arrowOrColonLoc;
82988292

8299-
/// The kind of macro.
8300-
const Kind kind;
8293+
/// The result type.
8294+
TypeLoc resultType;
83018295

8302-
/// How the macro is implemented.
8303-
const ImplementationKind implementationKind;
8296+
/// The module name for the external macro definition.
8297+
Identifier externalModuleName;
83048298

8305-
/// Supplemental modules that should be imported when
8306-
const ArrayRef<ModuleDecl *> supplementalSignatureModules;
8299+
/// The location of the module name for the external macro definition.
8300+
SourceLoc externalModuleNameLoc;
83078301

8308-
/// An opaque handle to the representation of the macro.
8309-
void * const opaqueHandle;
8302+
/// The type name for the external macro definition.
8303+
Identifier externalMacroTypeName;
83108304

8311-
public:
8312-
MacroDecl(
8313-
Kind kind, ImplementationKind implementationKind, Identifier name,
8314-
ModuleDecl *owningModule,
8315-
ArrayRef<ModuleDecl *> supplementalSignatureModules,
8316-
void *opaqueHandle
8317-
);
8305+
/// The location of the type name for the external macro definition.
8306+
SourceLoc externalMacroTypeNameLoc;
8307+
8308+
MacroDecl(SourceLoc macroLoc, DeclName name, SourceLoc nameLoc,
8309+
GenericParamList *genericParams,
8310+
ParameterList *parameterList,
8311+
SourceLoc arrowOrColonLoc,
8312+
TypeRepr *resultType,
8313+
Identifier externalModuleName,
8314+
SourceLoc externalModuleNameLoc,
8315+
Identifier externalMacroTypeName,
8316+
SourceLoc externalMacroTypeNameLoc,
8317+
DeclContext *parent);
83188318

83198319
SourceRange getSourceRange() const;
83208320

8321+
/// Retrieve the interface type produced when expanding this macro.
8322+
Type getResultInterfaceType() const;
8323+
83218324
static bool classof(const DeclContext *C) {
83228325
if (auto D = C->getAsDecl())
83238326
return classof(D);
@@ -8481,6 +8484,8 @@ inline bool ValueDecl::hasCurriedSelf() const {
84818484
inline bool ValueDecl::hasParameterList() const {
84828485
if (auto *eed = dyn_cast<EnumElementDecl>(this))
84838486
return eed->hasAssociatedValues();
8487+
if (auto *macro = dyn_cast<MacroDecl>(this))
8488+
return macro->parameterList != nullptr;
84848489
return isa<AbstractFunctionDecl>(this) || isa<SubscriptDecl>(this);
84858490
}
84868491

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ enum class DeclContextKind : unsigned {
9494
EnumElementDecl,
9595
AbstractFunctionDecl,
9696
SerializedLocal,
97-
Last_LocalDeclContextKind = SerializedLocal,
97+
MacroDecl,
98+
Last_LocalDeclContextKind = MacroDecl,
9899

99100
Module,
100101
FileUnit,
101102
GenericTypeDecl,
102103
ExtensionDecl,
103-
MacroDecl,
104-
Last_DeclContextKind = MacroDecl
104+
Last_DeclContextKind = ExtensionDecl
105105
};
106106

107107
/// Kinds of DeclContexts after deserialization.

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/DiagnosticsParse.def

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,8 +1999,24 @@ ERROR(foreign_diagnostic,none,
19991999
"%0", (StringRef))
20002000

20012001
//------------------------------------------------------------------------------
2002-
// MARK: macro expansion
2002+
// MARK: macros
20032003
//------------------------------------------------------------------------------
2004+
ERROR(expected_macro_value_type,PointsToFirstBadToken,
2005+
"expected macro value type following ':'", ())
2006+
ERROR(no_default_arg_macro,none,
2007+
"default arguments are not allowed in macros", ())
2008+
ERROR(expected_lparen_macro,PointsToFirstBadToken,
2009+
"expected '(' for macro parameters or ':' for a value-like macro", ())
2010+
ERROR(expected_type_macro_result,PointsToFirstBadToken,
2011+
"expected macro result type", ())
2012+
ERROR(macro_decl_expected_equal,PointsToFirstBadToken,
2013+
"expected '=' to introduce external macro name", ())
2014+
ERROR(macro_decl_expected_macro_module,PointsToFirstBadToken,
2015+
"expected external macro name", ())
2016+
ERROR(macro_decl_expected_period,PointsToFirstBadToken,
2017+
"expected '.' between external macro module and type name", ())
2018+
ERROR(macro_decl_expected_macro_type,PointsToFirstBadToken,
2019+
"expected external macro type name", ())
20042020

20052021
ERROR(macro_expansion_expr_expected_macro_identifier,none,
20062022
"expected a macro identifier for a pound literal expression", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6742,12 +6742,33 @@ 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))
6755+
ERROR(macro_type_access,none,
6756+
"macro %select{must be declared "
6757+
"%select{private|fileprivate|internal|%error|%error}1"
6758+
"|cannot be declared "
6759+
"%select{in this context|fileprivate|internal|public|open}1}0 "
6760+
"because its %select{parameter|result type}3 uses "
6761+
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
6762+
(bool, AccessLevel, AccessLevel, bool))
6763+
WARNING(macro_type_access_warn,none,
6764+
"macro %select{should be declared "
6765+
"%select{private|fileprivate|internal|%error|%error}1"
6766+
"|should not be declared %select{in this context|fileprivate|internal|public|open}1}0 "
6767+
"because its %select{parameter|result type}3 uses "
6768+
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
6769+
(bool, AccessLevel, AccessLevel, bool))
6770+
ERROR(macro_in_nested,none,
6771+
"macro %0 can only be declared at file scope", (DeclName))
67516772

67526773
//------------------------------------------------------------------------------
67536774
// MARK: Move Only Errors

include/swift/AST/Expr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6021,6 +6021,9 @@ class MacroExpansionExpr final : public Expr {
60216021
ArgumentList *ArgList;
60226022
Expr *Rewritten;
60236023

6024+
/// The referenced macro.
6025+
ConcreteDeclRef macroRef;
6026+
60246027
public:
60256028
explicit MacroExpansionExpr(SourceLoc poundLoc, DeclNameRef macroName,
60266029
DeclNameLoc macroNameLoc,
@@ -6053,6 +6056,9 @@ class MacroExpansionExpr final : public Expr {
60536056

60546057
SourceLoc getLoc() const { return PoundLoc; }
60556058

6059+
ConcreteDeclRef getMacroRef() const { return macroRef; }
6060+
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
6061+
60566062
SourceRange getSourceRange() const;
60576063

60586064
static bool classof(const Expr *E) {

0 commit comments

Comments
 (0)