Skip to content

Commit 9292231

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

File tree

10 files changed

+184
-27
lines changed

10 files changed

+184
-27
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,8 +4758,8 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
47584758

47594759
case MacroDefinition::Kind::External: {
47604760
auto external = def.getExternalMacro();
4761-
Printer << " = #externalMacro(module: \"" << external.moduleName << "\", "
4762-
<< "type: \"" << external.macroTypeName << "\")";
4761+
Printer << " = #externalMacro(module: \"" << external.moduleName
4762+
<< "\", " << "type: \"" << external.macroTypeName << "\")";
47634763
break;
47644764
}
47654765

@@ -4771,6 +4771,10 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
47714771
break;
47724772
}
47734773
break;
4774+
4775+
case MacroDefinition::Kind::Expanded:
4776+
Printer << " = " << def.getExpanded().getExpansionText();
4777+
break;
47744778
}
47754779
}
47764780
}

lib/AST/Decl.cpp

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

10329+
MacroDefinition MacroDefinition::forExpanded(
10330+
ASTContext &ctx,
10331+
StringRef expansionText,
10332+
ArrayRef<ExpandedMacroReplacement> replacements
10333+
) {
10334+
return ExpandedMacroDefinition{ctx.AllocateCopy(expansionText),
10335+
ctx.AllocateCopy(replacements)};
10336+
}
10337+
1032910338
MacroExpansionDecl::MacroExpansionDecl(
1033010339
DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
1033110340
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
@@ -2009,6 +2009,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20092009

20102010
case MacroDefinition::Kind::Invalid:
20112011
case MacroDefinition::Kind::Builtin:
2012+
case MacroDefinition::Kind::Expanded:
20122013
// Nothing else to check here.
20132014
break;
20142015

0 commit comments

Comments
 (0)