Skip to content

Commit 86d405b

Browse files
committed
[Macros] Unify MacroExpansionDecl/MacroExpansionExpr expansion logic
'MacroExpansionDecl' and 'MacroExpansionExpr' have many common methods. Introduce a common base class 'FreestandingMacroExpansion' that holds 'MacroExpansionInfo'. Factor out common expansion logic to 'evaluateFreestandingMacro' function that resembles 'evaluateAttachedMacro'.
1 parent 8a6fddf commit 86d405b

18 files changed

+377
-369
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/Basic/Mangler.h"
1717
#include "swift/AST/Types.h"
1818
#include "swift/AST/Decl.h"
19+
#include "swift/AST/FreestandingMacroExpansion.h"
1920
#include "swift/Basic/TaggedUnion.h"
2021

2122
namespace clang {
@@ -367,8 +368,7 @@ class ASTMangler : public Mangler {
367368
mangleRuntimeAttributeGeneratorEntity(const ValueDecl *decl, CustomAttr *attr,
368369
SymbolKind SKind = SymbolKind::Default);
369370

370-
std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
371-
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
371+
std::string mangleMacroExpansion(const FreestandingMacroExpansion *expansion);
372372
std::string mangleAttachedMacroExpansion(
373373
const Decl *decl, CustomAttr *attr, MacroRole role);
374374

include/swift/AST/Decl.h

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/DefaultArgumentKind.h"
2727
#include "swift/AST/DiagnosticConsumer.h"
2828
#include "swift/AST/DiagnosticEngine.h"
29+
#include "swift/AST/FreestandingMacroExpansion.h"
2930
#include "swift/AST/GenericParamKey.h"
3031
#include "swift/AST/IfConfigClause.h"
3132
#include "swift/AST/LayoutConstraint.h"
@@ -8584,69 +8585,28 @@ class MacroDecl : public GenericContext, public ValueDecl {
85848585
using Decl::getASTContext;
85858586
};
85868587

8587-
/// Information about a macro expansion that is common between macro
8588-
/// expansion declarations and expressions.
8589-
///
8590-
/// Instances of these types will be shared among paired macro expansion
8591-
/// declaration/expression nodes.
8592-
struct MacroExpansionInfo : ASTAllocated<MacroExpansionInfo> {
8593-
SourceLoc SigilLoc;
8594-
DeclNameRef MacroName;
8595-
DeclNameLoc MacroNameLoc;
8596-
SourceLoc LeftAngleLoc, RightAngleLoc;
8597-
ArrayRef<TypeRepr *> GenericArgs;
8598-
ArgumentList *ArgList;
8599-
8600-
/// The referenced macro.
8601-
ConcreteDeclRef macroRef;
8602-
8603-
MacroExpansionInfo(SourceLoc sigilLoc,
8604-
DeclNameRef macroName,
8605-
DeclNameLoc macroNameLoc,
8606-
SourceLoc leftAngleLoc, SourceLoc rightAngleLoc,
8607-
ArrayRef<TypeRepr *> genericArgs,
8608-
ArgumentList *argList)
8609-
: SigilLoc(sigilLoc), MacroName(macroName), MacroNameLoc(macroNameLoc),
8610-
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
8611-
GenericArgs(genericArgs), ArgList(argList) { }
8612-
};
8613-
8614-
class MacroExpansionDecl : public Decl {
8615-
MacroExpansionInfo *info;
8588+
class MacroExpansionDecl : public Decl, public FreestandingMacroExpansion {
86168589

86178590
public:
86188591
enum : unsigned { InvalidDiscriminator = 0xFFFF };
86198592

86208593
MacroExpansionDecl(DeclContext *dc, MacroExpansionInfo *info);
86218594

8622-
MacroExpansionDecl(DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
8623-
DeclNameLoc macroLoc,
8624-
SourceLoc leftAngleLoc,
8625-
ArrayRef<TypeRepr *> genericArgs,
8626-
SourceLoc rightAngleLoc,
8627-
ArgumentList *args);
8595+
static MacroExpansionDecl *create(DeclContext *dc, SourceLoc poundLoc,
8596+
DeclNameRef macro, DeclNameLoc macroLoc,
8597+
SourceLoc leftAngleLoc,
8598+
ArrayRef<TypeRepr *> genericArgs,
8599+
SourceLoc rightAngleLoc,
8600+
ArgumentList *args);
86288601

8629-
ArrayRef<TypeRepr *> getGenericArgs() const {
8630-
return info->GenericArgs;
8631-
}
8602+
DeclContext *getDeclContext() const { return Decl::getDeclContext(); }
86328603

8633-
SourceRange getGenericArgsRange() const {
8634-
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
8604+
SourceRange getSourceRange() const {
8605+
return getExpansionInfo()->getSourceRange();
86358606
}
8636-
8637-
SourceRange getSourceRange() const;
8638-
SourceLoc getLocFromSource() const { return info->SigilLoc; }
8639-
SourceLoc getPoundLoc() const { return info->SigilLoc; }
8640-
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
8641-
DeclNameRef getMacroName() const { return info->MacroName; }
8642-
ArgumentList *getArgs() const { return info->ArgList; }
8643-
void setArgs(ArgumentList *args) { info->ArgList = args; }
8607+
SourceLoc getLocFromSource() const { return getExpansionInfo()->SigilLoc; }
86448608
using ExprOrStmtExpansionCallback = llvm::function_ref<void(ASTNode)>;
86458609
void forEachExpandedExprOrStmt(ExprOrStmtExpansionCallback) const;
8646-
ConcreteDeclRef getMacroRef() const { return info->macroRef; }
8647-
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }
8648-
8649-
MacroExpansionInfo *getExpansionInfo() const { return info; }
86508610

86518611
/// Returns a discriminator which determines this macro expansion's index
86528612
/// in the sequence of macro expansions within the current function.
@@ -8669,6 +8629,9 @@ class MacroExpansionDecl : public Decl {
86698629
static bool classof(const Decl *D) {
86708630
return D->getKind() == DeclKind::MacroExpansion;
86718631
}
8632+
static bool classof(const FreestandingMacroExpansion *expansion) {
8633+
return expansion->getFreestandingMacroKind() == FreestandingMacroKind::Decl;
8634+
}
86728635
};
86738636

86748637
/// Find references to the given generic parameter in the type signature of the

include/swift/AST/Expr.h

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/Decl.h"
2626
#include "swift/AST/DeclContext.h"
2727
#include "swift/AST/DeclNameLoc.h"
28+
#include "swift/AST/FreestandingMacroExpansion.h"
2829
#include "swift/AST/FunctionRefKind.h"
2930
#include "swift/AST/ProtocolConformanceRef.h"
3031
#include "swift/AST/TypeAlignments.h"
@@ -6214,10 +6215,10 @@ class TypeJoinExpr final : public Expr,
62146215

62156216
/// An invocation of a macro expansion, spelled with `#` for freestanding
62166217
/// macros or `@` for attached macros.
6217-
class MacroExpansionExpr final : public Expr {
6218+
class MacroExpansionExpr final : public Expr,
6219+
public FreestandingMacroExpansion {
62186220
private:
62196221
DeclContext *DC;
6220-
MacroExpansionInfo *info;
62216222
Expr *Rewritten;
62226223
MacroRoles Roles;
62236224
MacroExpansionDecl *SubstituteDecl;
@@ -6226,47 +6227,31 @@ class MacroExpansionExpr final : public Expr {
62266227
enum : unsigned { InvalidDiscriminator = 0xFFFF };
62276228

62286229
explicit MacroExpansionExpr(DeclContext *dc, MacroExpansionInfo *info,
6229-
MacroRoles roles,
6230-
bool isImplicit = false,
6230+
MacroRoles roles, bool isImplicit = false,
62316231
Type ty = Type())
62326232
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
6233-
DC(dc), info(info), Rewritten(nullptr), Roles(roles),
6234-
SubstituteDecl(nullptr) {
6233+
FreestandingMacroExpansion(FreestandingMacroKind::Expr, info), DC(dc),
6234+
Rewritten(nullptr), Roles(roles), SubstituteDecl(nullptr) {
62356235
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
62366236
}
62376237

6238-
explicit MacroExpansionExpr(DeclContext *dc,
6239-
SourceLoc sigilLoc, DeclNameRef macroName,
6240-
DeclNameLoc macroNameLoc,
6241-
SourceLoc leftAngleLoc,
6242-
ArrayRef<TypeRepr *> genericArgs,
6243-
SourceLoc rightAngleLoc,
6244-
ArgumentList *argList,
6245-
MacroRoles roles,
6246-
bool isImplicit = false,
6247-
Type ty = Type());
6248-
6249-
DeclNameRef getMacroName() const { return info->MacroName; }
6250-
DeclNameLoc getMacroNameLoc() const { return info->MacroNameLoc; }
6238+
static MacroExpansionExpr *
6239+
create(DeclContext *dc, SourceLoc sigilLoc, DeclNameRef macroName,
6240+
DeclNameLoc macroNameLoc, SourceLoc leftAngleLoc,
6241+
ArrayRef<TypeRepr *> genericArgs, SourceLoc rightAngleLoc,
6242+
ArgumentList *argList, MacroRoles roles, bool isImplicit = false,
6243+
Type ty = Type());
62516244

62526245
Expr *getRewritten() const { return Rewritten; }
62536246
void setRewritten(Expr *rewritten) { Rewritten = rewritten; }
62546247

6255-
ArrayRef<TypeRepr *> getGenericArgs() const { return info->GenericArgs; }
6256-
6257-
SourceRange getGenericArgsRange() const {
6258-
return SourceRange(info->LeftAngleLoc, info->RightAngleLoc);
6248+
ArgumentList *getArgs() const {
6249+
return FreestandingMacroExpansion::getArgs();
62596250
}
62606251

6261-
ArgumentList *getArgs() const { return info->ArgList; }
6262-
void setArgs(ArgumentList *newArgs) { info->ArgList = newArgs; }
6263-
62646252
MacroRoles getMacroRoles() const { return Roles; }
62656253

6266-
SourceLoc getLoc() const { return info->SigilLoc; }
6267-
6268-
ConcreteDeclRef getMacroRef() const { return info->macroRef; }
6269-
void setMacroRef(ConcreteDeclRef ref) { info->macroRef = ref; }
6254+
SourceLoc getLoc() const { return getPoundLoc(); }
62706255

62716256
DeclContext *getDeclContext() const { return DC; }
62726257
void setDeclContext(DeclContext *dc) { DC = dc; }
@@ -6289,16 +6274,19 @@ class MacroExpansionExpr final : public Expr {
62896274
Bits.MacroExpansionExpr.Discriminator = discriminator;
62906275
}
62916276

6292-
MacroExpansionInfo *getExpansionInfo() const { return info; }
6293-
6294-
SourceRange getSourceRange() const;
6277+
SourceRange getSourceRange() const {
6278+
return getExpansionInfo()->getSourceRange();
6279+
}
62956280

62966281
MacroExpansionDecl *createSubstituteDecl();
62976282
MacroExpansionDecl *getSubstituteDecl() const;
62986283

62996284
static bool classof(const Expr *E) {
63006285
return E->getKind() == ExprKind::MacroExpansion;
63016286
}
6287+
static bool classof(const FreestandingMacroExpansion *expansion) {
6288+
return expansion->getFreestandingMacroKind() == FreestandingMacroKind::Expr;
6289+
}
63026290
};
63036291

63046292
inline bool Expr::isInfixOperator() const {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//===--- FreestandingMacroExpansion.h ------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_AST_FREESTANDING_MACRO_EXPANSION_H
14+
#define SWIFT_AST_FREESTANDING_MACRO_EXPANSION_H
15+
16+
#include "swift/AST/ASTAllocated.h"
17+
#include "swift/AST/ASTNode.h"
18+
#include "swift/AST/ConcreteDeclRef.h"
19+
#include "swift/AST/DeclNameLoc.h"
20+
#include "swift/AST/Identifier.h"
21+
#include "swift/Basic/SourceLoc.h"
22+
23+
namespace swift {
24+
class MacroExpansionDecl;
25+
class MacroExpansionExpr;
26+
class Expr;
27+
class Decl;
28+
class ArgumentList;
29+
30+
/// Information about a macro expansion that is common between macro
31+
/// expansion declarations and expressions.
32+
///
33+
/// Instances of these types will be shared among paired macro expansion
34+
/// declaration/expression nodes.
35+
struct MacroExpansionInfo : ASTAllocated<MacroExpansionInfo> {
36+
SourceLoc SigilLoc;
37+
DeclNameRef MacroName;
38+
DeclNameLoc MacroNameLoc;
39+
SourceLoc LeftAngleLoc, RightAngleLoc;
40+
llvm::ArrayRef<TypeRepr *> GenericArgs;
41+
ArgumentList *ArgList;
42+
43+
/// The referenced macro.
44+
ConcreteDeclRef macroRef;
45+
46+
MacroExpansionInfo(SourceLoc sigilLoc, DeclNameRef macroName,
47+
DeclNameLoc macroNameLoc, SourceLoc leftAngleLoc,
48+
SourceLoc rightAngleLoc, ArrayRef<TypeRepr *> genericArgs,
49+
ArgumentList *argList)
50+
: SigilLoc(sigilLoc), MacroName(macroName), MacroNameLoc(macroNameLoc),
51+
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
52+
GenericArgs(genericArgs), ArgList(argList) {}
53+
54+
SourceLoc getLoc() const { return SigilLoc; }
55+
SourceRange getGenericArgsRange() const {
56+
return {LeftAngleLoc, RightAngleLoc};
57+
}
58+
SourceRange getSourceRange() const;
59+
};
60+
61+
enum class FreestandingMacroKind {
62+
Expr, // MacroExpansionExpr.
63+
Decl, // MacroExpansionDecl.
64+
};
65+
66+
/// A base class of either 'MacroExpansionExpr' or 'MacroExpansionDecl'.
67+
class FreestandingMacroExpansion {
68+
llvm::PointerIntPair<MacroExpansionInfo *, 1, FreestandingMacroKind>
69+
infoAndKind;
70+
71+
protected:
72+
FreestandingMacroExpansion(FreestandingMacroKind kind,
73+
MacroExpansionInfo *info)
74+
: infoAndKind(info, kind) {}
75+
76+
public:
77+
MacroExpansionInfo *getExpansionInfo() const {
78+
return infoAndKind.getPointer();
79+
}
80+
FreestandingMacroKind getFreestandingMacroKind() const {
81+
return infoAndKind.getInt();
82+
}
83+
84+
ASTNode getASTNode();
85+
86+
SourceLoc getPoundLoc() const { return getExpansionInfo()->SigilLoc; }
87+
88+
DeclNameLoc getMacroNameLoc() const {
89+
return getExpansionInfo()->MacroNameLoc;
90+
}
91+
DeclNameRef getMacroName() const { return getExpansionInfo()->MacroName; }
92+
93+
ArrayRef<TypeRepr *> getGenericArgs() const {
94+
return getExpansionInfo()->GenericArgs;
95+
}
96+
SourceRange getGenericArgsRange() const {
97+
return getExpansionInfo()->getGenericArgsRange();
98+
}
99+
100+
ArgumentList *getArgs() const { return getExpansionInfo()->ArgList; }
101+
void setArgs(ArgumentList *args) { getExpansionInfo()->ArgList = args; }
102+
103+
ConcreteDeclRef getMacroRef() const { return getExpansionInfo()->macroRef; }
104+
void setMacroRef(ConcreteDeclRef ref) { getExpansionInfo()->macroRef = ref; }
105+
106+
DeclContext *getDeclContext() const;
107+
SourceRange getSourceRange() const;
108+
unsigned getDiscriminator() const;
109+
};
110+
111+
} // namespace swift
112+
113+
#endif // SWIFT_AST_FREESTANDING_MACRO_EXPANSION_H

include/swift/AST/MacroDiscriminatorContext.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ namespace swift {
2222
/// Describes the context of a macro expansion for the purpose of
2323
/// computing macro expansion discriminators.
2424
struct MacroDiscriminatorContext
25-
: public llvm::PointerUnion<DeclContext *, MacroExpansionExpr *,
26-
MacroExpansionDecl *> {
25+
: public llvm::PointerUnion<DeclContext *, FreestandingMacroExpansion *> {
2726
using PointerUnion::PointerUnion;
2827

29-
static MacroDiscriminatorContext getParentOf(MacroExpansionExpr *expansion);
30-
static MacroDiscriminatorContext getParentOf(MacroExpansionDecl *expansion);
28+
static MacroDiscriminatorContext getParentOf(FreestandingMacroExpansion *expansion);
3129
static MacroDiscriminatorContext getParentOf(
3230
SourceLoc loc, DeclContext *origDC
3331
);

include/swift/AST/PrettyStackTrace.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
#ifndef SWIFT_PRETTYSTACKTRACE_H
1919
#define SWIFT_PRETTYSTACKTRACE_H
2020

21-
#include "llvm/Support/PrettyStackTrace.h"
22-
#include "swift/Basic/SourceLoc.h"
2321
#include "swift/AST/AnyFunctionRef.h"
22+
#include "swift/AST/FreestandingMacroExpansion.h"
2423
#include "swift/AST/Identifier.h"
2524
#include "swift/AST/Type.h"
25+
#include "swift/Basic/SourceLoc.h"
26+
#include "llvm/Support/PrettyStackTrace.h"
2627

2728
namespace clang {
2829
class Type;
@@ -93,7 +94,21 @@ class PrettyStackTraceAnyFunctionRef : public llvm::PrettyStackTraceEntry {
9394
virtual void print(llvm::raw_ostream &OS) const override;
9495
};
9596

96-
void printExprDescription(llvm::raw_ostream &out, Expr *E,
97+
/// PrettyStackTraceFreestandingMacroExpansion - Observe that we are
98+
/// processing a specific freestanding macro expansion.
99+
class PrettyStackTraceFreestandingMacroExpansion
100+
: public llvm::PrettyStackTraceEntry {
101+
const FreestandingMacroExpansion *Expansion;
102+
const char *Action;
103+
104+
public:
105+
PrettyStackTraceFreestandingMacroExpansion(
106+
const char *action, const FreestandingMacroExpansion *expansion)
107+
: Expansion(expansion), Action(action) {}
108+
virtual void print(llvm::raw_ostream &OS) const override;
109+
};
110+
111+
void printExprDescription(llvm::raw_ostream &out, const Expr *E,
97112
const ASTContext &Context, bool addNewline = true);
98113

99114
/// PrettyStackTraceExpr - Observe that we are processing a specific

0 commit comments

Comments
 (0)