Skip to content

Commit 0c54362

Browse files
authored
Merge pull request #62768 from DougGregor/macro-omnibus
2 parents d5eca6c + 5936b7b commit 0c54362

File tree

72 files changed

+1206
-486
lines changed

Some content is hidden

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

72 files changed

+1206
-486
lines changed

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,13 @@ class SDKNodeDeclVar : public SDKNodeDecl {
674674
void addAccessor(SDKNode* AC);
675675
};
676676

677+
class SDKNodeDeclMacro : public SDKNodeDecl {
678+
public:
679+
SDKNodeDeclMacro(SDKNodeInitInfo Info);
680+
static bool classof(const SDKNode *N);
681+
SDKNodeType *getType() const;
682+
};
683+
677684
class SDKNodeDeclAbstractFunc : public SDKNodeDecl {
678685
bool IsThrowing;
679686
bool ReqNewWitnessTableEntry;
@@ -796,6 +803,7 @@ class SwiftDeclCollector: public VisibleDeclConsumer {
796803
SDKNode *constructSubscriptDeclNode(SubscriptDecl *SD);
797804
SDKNode *constructAssociatedTypeNode(AssociatedTypeDecl *ATD);
798805
SDKNode *constructTypeAliasNode(TypeAliasDecl *TAD);
806+
SDKNode *constructMacroNode(MacroDecl *MAD);
799807
SDKNode *constructVarNode(ValueDecl *VD);
800808
SDKNode *constructExternalExtensionNode(NominalTypeDecl *NTD,
801809
ArrayRef<ExtensionDecl*> AllExts);

include/swift/AST/Decl.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace swift {
6060
struct ASTNode;
6161
class ASTPrinter;
6262
class ASTWalker;
63+
enum class BuiltinMacroKind: uint8_t;
6364
class ConstructorDecl;
6465
class DestructorDecl;
6566
class DiagnosticEngine;
@@ -79,6 +80,7 @@ namespace swift {
7980
class GenericSignature;
8081
class GenericTypeParamDecl;
8182
class GenericTypeParamType;
83+
class MacroDefinition;
8284
class ModuleDecl;
8385
class NamedPattern;
8486
class EnumCaseDecl;
@@ -255,6 +257,9 @@ struct OverloadSignature {
255257
/// Whether this is a type alias.
256258
unsigned IsTypeAlias : 1;
257259

260+
/// Whether this is a macro.
261+
unsigned IsMacro : 1;
262+
258263
/// Whether this signature is part of a protocol extension.
259264
unsigned InProtocolExtension : 1;
260265

@@ -8345,27 +8350,16 @@ class MacroDecl : public GenericContext, public ValueDecl {
83458350
/// The result type.
83468351
TypeLoc resultType;
83478352

8348-
/// The module name for the external macro definition.
8349-
Identifier externalModuleName;
8350-
8351-
/// The location of the module name for the external macro definition.
8352-
SourceLoc externalModuleNameLoc;
8353-
8354-
/// The type name for the external macro definition.
8355-
Identifier externalMacroTypeName;
8356-
8357-
/// The location of the type name for the external macro definition.
8358-
SourceLoc externalMacroTypeNameLoc;
8353+
/// The macro definition, which should always be a
8354+
/// \c MacroExpansionExpr in well-formed code.
8355+
Expr *definition;
83598356

83608357
MacroDecl(SourceLoc macroLoc, DeclName name, SourceLoc nameLoc,
83618358
GenericParamList *genericParams,
83628359
ParameterList *parameterList,
83638360
SourceLoc arrowOrColonLoc,
83648361
TypeRepr *resultType,
8365-
Identifier externalModuleName,
8366-
SourceLoc externalModuleNameLoc,
8367-
Identifier externalMacroTypeName,
8368-
SourceLoc externalMacroTypeNameLoc,
8362+
Expr *definition,
83698363
DeclContext *parent);
83708364

83718365
SourceRange getSourceRange() const;
@@ -8376,6 +8370,13 @@ class MacroDecl : public GenericContext, public ValueDecl {
83768370
/// Determine the contexts in which this macro can be applied.
83778371
MacroContexts getMacroContexts() const;
83788372

8373+
/// Retrieve the definition of this macro.
8374+
MacroDefinition getDefinition() const;
8375+
8376+
/// Retrieve the builtin macro kind for this macro, or \c None if it is a
8377+
/// user-defined macro with no special semantics.
8378+
Optional<BuiltinMacroKind> getBuiltinKind() const;
8379+
83798380
static bool classof(const DeclContext *C) {
83808381
if (auto D = C->getAsDecl())
83818382
return classof(D);

include/swift/AST/DiagnosticsCommon.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ ERROR(unknown_attribute,none,
219219
//------------------------------------------------------------------------------
220220
NOTE(in_macro_expansion,none,
221221
"in expansion of macro %0 here", (DeclName))
222+
ERROR(macro_experimental,none,
223+
"macros are an experimental feature that is not enabled", ())
222224

223225
//------------------------------------------------------------------------------
224226
// MARK: bridged diagnostics

include/swift/AST/DiagnosticsParse.def

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,9 +1329,6 @@ ERROR(expected_rsquare_array_expr,PointsToFirstBadToken,
13291329
// Object literal expressions
13301330
ERROR(expected_arg_list_in_object_literal,PointsToFirstBadToken,
13311331
"expected argument list in object literal", ())
1332-
ERROR(legacy_object_literal,none,
1333-
"'%select{|[}0#%1(...)%select{|#]}0' has been renamed to '#%2(...)'",
1334-
(bool, StringRef, StringRef))
13351332

13361333
// Unknown pound expression.
13371334
ERROR(unknown_pound_expr,none,
@@ -1999,18 +1996,12 @@ ERROR(expected_lparen_macro,PointsToFirstBadToken,
19991996
"expected '(' for macro parameters or ':' for a value-like macro", ())
20001997
ERROR(expected_type_macro_result,PointsToFirstBadToken,
20011998
"expected macro result type", ())
2002-
ERROR(macro_decl_expected_equal,PointsToFirstBadToken,
2003-
"expected '=' to introduce external macro name", ())
2004-
ERROR(macro_decl_expected_macro_module,PointsToFirstBadToken,
2005-
"expected external macro name", ())
2006-
ERROR(macro_decl_expected_period,PointsToFirstBadToken,
2007-
"expected '.' between external macro module and type name", ())
2008-
ERROR(macro_decl_expected_macro_type,PointsToFirstBadToken,
2009-
"expected external macro type name", ())
2010-
2011-
ERROR(macro_expansion_expr_expected_macro_identifier,none,
1999+
ERROR(macro_decl_expected_macro_definition,PointsToFirstBadToken,
2000+
"expected macro definition following '=''", ())
2001+
2002+
ERROR(macro_expansion_expr_expected_macro_identifier,PointsToFirstBadToken,
20122003
"expected a macro identifier for a pound literal expression", ())
2013-
ERROR(macro_expansion_decl_expected_macro_identifier,none,
2004+
ERROR(macro_expansion_decl_expected_macro_identifier,PointsToFirstBadToken,
20142005
"expected a macro identifier for a pound literal declaration", ())
20152006

20162007
ERROR(parser_round_trip_error,none,

include/swift/AST/DiagnosticsSema.def

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,15 +1271,18 @@ ERROR(missing_init_on_metatype_initialization,none,
12711271
"initializing from a metatype value must reference 'init' explicitly",
12721272
())
12731273
ERROR(extra_argument_labels,none,
1274-
"extraneous argument label%select{|s}0 '%1' in %select{call|subscript}2",
1275-
(bool, StringRef, bool))
1274+
"extraneous argument label%select{|s}0 '%1' in "
1275+
"%select{call|subscript|macro expansion}2",
1276+
(bool, StringRef, unsigned))
12761277
ERROR(missing_argument_labels,none,
1277-
"missing argument label%select{|s}0 '%1' in %select{call|subscript}2",
1278-
(bool, StringRef, bool))
1278+
"missing argument label%select{|s}0 '%1' in "
1279+
"%select{call|subscript|macro expansion}2",
1280+
(bool, StringRef, unsigned))
12791281
ERROR(wrong_argument_labels,none,
1280-
"incorrect argument label%select{|s}0 in %select{call|subscript}3 "
1282+
"incorrect argument label%select{|s}0 in "
1283+
"%select{call|subscript|macro expansion}3 "
12811284
"(have '%1', expected '%2')",
1282-
(bool, StringRef, StringRef, bool))
1285+
(bool, StringRef, StringRef, unsigned))
12831286
ERROR(argument_out_of_order_named_named,none,
12841287
"argument %0 must precede argument %1", (Identifier, Identifier))
12851288
ERROR(argument_out_of_order_named_unnamed,none,
@@ -1313,22 +1316,29 @@ ERROR(instance_member_in_default_parameter,none,
13131316
"cannot use instance member %0 as a default parameter", (DeclNameRef))
13141317

13151318
ERROR(missing_argument_named,none,
1316-
"missing argument for parameter %0 in call", (Identifier))
1319+
"missing argument for parameter %0 in "
1320+
"%select{call|subscript|macro expansion}1", (Identifier, unsigned))
13171321
ERROR(missing_argument_positional,none,
1318-
"missing argument for parameter #%0 in call", (unsigned))
1322+
"missing argument for parameter #%0 in "
1323+
"%select{call|subscript|macro expansion}1", (unsigned, unsigned))
13191324
ERROR(missing_arguments_in_call,none,
1320-
"missing arguments for parameters %0 in call", (StringRef))
1325+
"missing arguments for parameters %0 in "
1326+
"%select{call|subscript|macro expansion}1", (StringRef, unsigned))
13211327
ERROR(extra_argument_named,none,
1322-
"extra argument %0 in call", (Identifier))
1328+
"extra argument %0 in %select{call|subscript|macro expansion}1",
1329+
(Identifier, unsigned))
13231330
ERROR(extra_argument_positional,none,
1324-
"extra argument in call", ())
1331+
"extra argument in %select{call|subscript|macro expansion}0",
1332+
(unsigned))
13251333
ERROR(extra_arguments_in_call,none,
13261334
"extra %select{arguments|trailing closures}0 at positions %1 in call",
13271335
(bool, StringRef))
13281336
ERROR(extra_argument_to_nullary_call,none,
1329-
"argument passed to call that takes no arguments", ())
1337+
"argument passed to %select{call|subscript|macro expansion}0 "
1338+
"that takes no arguments", (unsigned))
13301339
ERROR(extra_trailing_closure_in_call,none,
1331-
"extra trailing closure passed in call", ())
1340+
"extra trailing closure passed in "
1341+
"%select{call|subscript|macro expansion}0", (unsigned))
13321342
ERROR(trailing_closure_bad_param,none,
13331343
"trailing closure passed to parameter of type %0 that does not "
13341344
"accept a closure", (Type))
@@ -6758,8 +6768,7 @@ ERROR(experimental_no_metadata_feature_can_only_be_used_when_enabled,
67586768
ERROR(expected_macro_expansion_expr,PointsToFirstBadToken,
67596769
"expected macro expansion to produce an expression", ())
67606770
ERROR(macro_undefined,PointsToFirstBadToken,
6761-
"macro %0 is undefined; use `-load-plugin-library` to specify dynamic "
6762-
"libraries that contain this macro", (Identifier))
6771+
"no macro named %0", (Identifier))
67636772
ERROR(external_macro_not_found,none,
67646773
"external macro implementation type '%0.%1' could not be found for "
67656774
"macro %2; the type must be public and provided via "
@@ -6790,6 +6799,26 @@ ERROR(macro_in_nested,none,
67906799
ERROR(macro_without_context,none,
67916800
"macro %0 must declare its applicable contexts (e.g., '@expression')",
67926801
(DeclName))
6802+
ERROR(macro_expansion_missing_pound,none,
6803+
"expansion of macro %0 requires leading '#'", (DeclName))
6804+
ERROR(macro_expansion_missing_arguments,none,
6805+
"expansion of macro %0 requires arguments", (DeclName))
6806+
ERROR(macro_unsupported,none,
6807+
"macros are not supported in this compiler", ())
6808+
ERROR(macro_recursive,none,
6809+
"recursive expansion of macro %0", (DeclName))
6810+
WARNING(macro_definition_old_style,none,
6811+
"external macro definitions are now written using #externalMacro", ())
6812+
WARNING(macro_definition_unknown_builtin,none,
6813+
"ignoring definition of unknown builtin macro %0", (Identifier))
6814+
ERROR(macro_definition_not_expansion,none,
6815+
"macro must itself be defined by a macro expansion such as "
6816+
"'#externalMacro(...)'", ())
6817+
ERROR(macro_definition_unsupported,none,
6818+
"macro definitions other than '#externalMacro(...)' are unsupported", ())
6819+
ERROR(external_macro_arg_not_type_name,none,
6820+
"argument to `#externalMacro` must be a string literal naming "
6821+
"the external macro's %select{module|type}", (unsigned))
67936822

67946823
//------------------------------------------------------------------------------
67956824
// MARK: Move Only Errors

include/swift/AST/MacroDefinition.h

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,64 +22,97 @@
2222

2323
namespace swift {
2424

25+
/// A reference to an external macro definition that is understood by ASTGen.
26+
struct ExternalMacroDefinition {
27+
/// ASTGen's notion of an macro definition, which is opaque to the C++ part
28+
/// of the compiler.
29+
void *opaqueHandle = nullptr;
30+
};
31+
32+
/// A reference to an external macro.
33+
struct ExternalMacroReference {
34+
Identifier moduleName;
35+
Identifier macroTypeName;
36+
};
37+
38+
/// Describes the known kinds of builtin macros.
39+
enum class BuiltinMacroKind: uint8_t {
40+
/// #externalMacro, which references an external macro.
41+
ExternalMacro,
42+
};
43+
2544
/// Provides the definition of a macro.
2645
class MacroDefinition {
2746
public:
28-
/// Describes a missing macro definition.
29-
struct MissingDefinition {
30-
Identifier externalModuleName;
31-
Identifier externalMacroTypeName;
32-
};
33-
3447
/// Describes how the macro is implemented.
35-
enum class ImplementationKind: uint8_t {
48+
enum class Kind: uint8_t {
49+
/// The macro has a definition, but it is invalid, so the macro cannot be
50+
/// expanded.
51+
Invalid,
52+
3653
/// The macro has no definition.
3754
Undefined,
3855

39-
/// The macro has a definition, but it could not be found.
40-
Missing,
56+
/// An externally-provided macro definition.
57+
External,
4158

42-
/// The macro is in the same process as the compiler, whether built-in or
43-
/// loaded via a compiler plugin.
44-
InProcess,
59+
/// A builtin macro definition, which has a separate builtin kind.
60+
Builtin,
4561
};
4662

47-
ImplementationKind implKind;
63+
Kind kind;
4864

4965
private:
50-
void *opaqueHandle;
66+
union Data {
67+
ExternalMacroReference external;
68+
BuiltinMacroKind builtin;
5169

52-
MacroDefinition(ImplementationKind implKind, void *opaqueHandle)
53-
: implKind(implKind), opaqueHandle(opaqueHandle) { }
70+
Data() : builtin(BuiltinMacroKind::ExternalMacro) { }
71+
} data;
72+
73+
MacroDefinition(Kind kind) : kind(kind) { }
74+
75+
MacroDefinition(ExternalMacroReference external) : kind(Kind::External) {
76+
data.external = external;
77+
}
78+
79+
MacroDefinition(BuiltinMacroKind builtinKind) : kind(Kind::Builtin) {
80+
data.builtin = builtinKind;
81+
}
5482

5583
public:
84+
static MacroDefinition forInvalid() {
85+
return MacroDefinition(Kind::Invalid);
86+
}
87+
5688
static MacroDefinition forUndefined() {
57-
return MacroDefinition{
58-
ImplementationKind::Undefined, nullptr
59-
};
89+
return MacroDefinition(Kind::Undefined);
6090
}
6191

62-
static MacroDefinition forMissing(
63-
ASTContext &ctx,
64-
Identifier externalModuleName,
65-
Identifier externalMacroTypeName
66-
);
92+
static MacroDefinition forExternal(
93+
Identifier moduleName,
94+
Identifier macroTypeName
95+
) {
96+
return MacroDefinition(ExternalMacroReference{moduleName, macroTypeName});
97+
}
6798

68-
static MacroDefinition forInProcess(void *opaqueHandle) {
69-
return MacroDefinition{ImplementationKind::InProcess, opaqueHandle};
99+
static MacroDefinition forBuiltin(BuiltinMacroKind builtinKind) {
100+
return MacroDefinition(builtinKind);
70101
}
71102

72-
/// Return the opaque handle for an in-process macro definition.
73-
void *getInProcessOpaqueHandle() const {
74-
assert(implKind == ImplementationKind::InProcess);
75-
return opaqueHandle;
103+
/// Retrieve the external macro being referenced.
104+
ExternalMacroReference getExternalMacro() const {
105+
assert(kind == Kind::External);
106+
return data.external;
76107
}
77108

78-
/// Return more information about a missing macro definition.
79-
MissingDefinition *getMissingDefinition() const {
80-
assert(implKind == ImplementationKind::Missing);
81-
return static_cast<MissingDefinition *>(opaqueHandle);
109+
/// Retrieve the builtin kind.
110+
BuiltinMacroKind getBuiltinKind() const {
111+
assert(kind == Kind::Builtin);
112+
return data.builtin;
82113
}
114+
115+
explicit operator bool() const { return kind != Kind::Invalid; }
83116
};
84117

85118
}

0 commit comments

Comments
 (0)