Skip to content

Commit 5867e48

Browse files
committed
[Macros] Start recording expanded macro definitions and replacements
Handle a trivial macro defined in terms of another macro.
1 parent 1cbd320 commit 5867e48

File tree

10 files changed

+182
-25
lines changed

10 files changed

+182
-25
lines changed

include/swift/AST/MacroDefinition.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace swift {
2424

25+
class ASTContext;
26+
2527
/// A reference to an external macro definition that is understood by ASTGen.
2628
struct ExternalMacroDefinition {
2729
enum class PluginKind {
@@ -46,6 +48,35 @@ enum class BuiltinMacroKind: uint8_t {
4648
ExternalMacro,
4749
};
4850

51+
/// A single replacement
52+
struct ExpandedMacroReplacement {
53+
unsigned startOffset, endOffset;
54+
unsigned parameterIndex;
55+
};
56+
57+
/// An expansion of another macro.
58+
class ExpandedMacroDefinition {
59+
friend class MacroDefinition;
60+
61+
/// The expansion text, ASTContext-allocated.
62+
StringRef expansionText;
63+
64+
/// The macro replacements, ASTContext-allocated.
65+
ArrayRef<ExpandedMacroReplacement> replacements;
66+
67+
ExpandedMacroDefinition(
68+
StringRef expansionText,
69+
ArrayRef<ExpandedMacroReplacement> replacements
70+
) : expansionText(expansionText), replacements(replacements) { }
71+
72+
public:
73+
StringRef getExpansionText() const { return expansionText; }
74+
75+
ArrayRef<ExpandedMacroReplacement> getReplacements() const {
76+
return replacements;
77+
}
78+
};
79+
4980
/// Provides the definition of a macro.
5081
class MacroDefinition {
5182
public:
@@ -63,6 +94,9 @@ class MacroDefinition {
6394

6495
/// A builtin macro definition, which has a separate builtin kind.
6596
Builtin,
97+
98+
/// A macro that is defined as an expansion of another macro.
99+
Expanded,
66100
};
67101

68102
Kind kind;
@@ -71,6 +105,7 @@ class MacroDefinition {
71105
union Data {
72106
ExternalMacroReference external;
73107
BuiltinMacroKind builtin;
108+
ExpandedMacroDefinition expanded;
74109

75110
Data() : builtin(BuiltinMacroKind::ExternalMacro) { }
76111
} data;
@@ -85,6 +120,10 @@ class MacroDefinition {
85120
data.builtin = builtinKind;
86121
}
87122

123+
MacroDefinition(ExpandedMacroDefinition expanded) : kind(Kind::Expanded) {
124+
data.expanded = expanded;
125+
}
126+
88127
public:
89128
static MacroDefinition forInvalid() {
90129
return MacroDefinition(Kind::Invalid);
@@ -105,6 +144,13 @@ class MacroDefinition {
105144
return MacroDefinition(builtinKind);
106145
}
107146

147+
/// Create a representation of an expanded macro definition.
148+
static MacroDefinition forExpanded(
149+
ASTContext &ctx,
150+
StringRef expansionText,
151+
ArrayRef<ExpandedMacroReplacement> replacements
152+
);
153+
108154
/// Retrieve the external macro being referenced.
109155
ExternalMacroReference getExternalMacro() const {
110156
assert(kind == Kind::External);
@@ -117,6 +163,11 @@ class MacroDefinition {
117163
return data.builtin;
118164
}
119165

166+
ExpandedMacroDefinition getExpanded() const {
167+
assert(kind == Kind::Expanded);
168+
return data.expanded;
169+
}
170+
120171
operator Kind() const { return kind; }
121172
};
122173

include/swift/Basic/StringExtras.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class NullTerminatedStringRef {
490490

491491
/// Create a null-terminated string, copying \p Str into \p A .
492492
template <typename Allocator>
493-
NullTerminatedStringRef(StringRef Str, Allocator &A) : Ref("") {
493+
NullTerminatedStringRef(StringRef Str, Allocator &A) : Ref() {
494494
if (Str.empty())
495495
return;
496496

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,12 +3869,14 @@ void ASTMangler::appendMacroExpansionContext(
38693869
baseName = expr->getMacroName().getBaseName();
38703870
discriminator = expr->getDiscriminator();
38713871
role = MacroRole::Expression;
3872+
outerExpansionDC = expr->getDeclContext();
38723873
} else {
38733874
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
38743875
outerExpansionLoc = decl->getLoc();
38753876
baseName = decl->getMacroName().getBaseName();
38763877
discriminator = decl->getDiscriminator();
38773878
role = MacroRole::Declaration;
3879+
outerExpansionDC = decl->getDeclContext();
38783880
}
38793881
break;
38803882
}

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,6 +4711,10 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
47114711
break;
47124712
}
47134713
break;
4714+
4715+
case MacroDefinition::Kind::Expanded:
4716+
Printer << " = " << def.getExpanded().getExpansionText();
4717+
break;
47144718
}
47154719
}
47164720

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10315,6 +10315,15 @@ Optional<BuiltinMacroKind> MacroDecl::getBuiltinKind() const {
1031510315
return def.getBuiltinKind();
1031610316
}
1031710317

10318+
MacroDefinition MacroDefinition::forExpanded(
10319+
ASTContext &ctx,
10320+
StringRef expansionText,
10321+
ArrayRef<ExpandedMacroReplacement> replacements
10322+
) {
10323+
return ExpandedMacroDefinition{ctx.AllocateCopy(expansionText),
10324+
ctx.AllocateCopy(replacements)};
10325+
}
10326+
1031810327
MacroExpansionDecl::MacroExpansionDecl(
1031910328
DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
1032010329
DeclNameLoc macroLoc, SourceLoc leftAngleLoc,

lib/AST/DistributedDecl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include "clang/AST/Attr.h"
5757
#include "clang/AST/DeclObjC.h"
5858

59-
#include "InlinableText.h"
6059
#include <algorithm>
6160

6261
using namespace swift;

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func checkMacroDefinition(
314314
)
315315
return BridgedMacroDefinitionKind.externalMacro.rawValue
316316

317-
case let .expansion(expansionSyntax, replacements: replacements)
317+
case let .expansion(expansionSyntax, replacements: _)
318318
where expansionSyntax.macro.text == "externalMacro":
319319
// Extract the identifier from the "module" argument.
320320
guard let firstArg = expansionSyntax.argumentList.first,
@@ -354,6 +354,12 @@ func checkMacroDefinition(
354354
return BridgedMacroDefinitionKind.externalMacro.rawValue
355355

356356
case let .expansion(expansionSyntax, replacements: replacements):
357+
// Provide the expansion syntax.
358+
(externalMacroPointer.pointee, externalMacroLength.pointee) =
359+
allocateUTF8String(expansionSyntax.trimmedDescription,
360+
nullTerminated: true)
361+
362+
357363
// If there are no replacements, we're done.
358364
if replacements.isEmpty {
359365
return BridgedMacroDefinitionKind.expandedMacro.rawValue

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20532053

20542054
case MacroDefinition::Kind::Invalid:
20552055
case MacroDefinition::Kind::Builtin:
2056+
case MacroDefinition::Kind::Expanded:
20562057
// Nothing else to check here.
20572058
break;
20582059

0 commit comments

Comments
 (0)