Skip to content

Commit 4e50e29

Browse files
authored
Merge pull request swiftlang#63233 from DougGregor/macros-mangle-buffer-names
[Macros] Mangle the names of macro expansion buffers
2 parents 119ea05 + 1f8e53e commit 4e50e29

29 files changed

+500
-74
lines changed

docs/ABI/Mangling.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Entities
365365
entity-spec ::= decl-name type 'fp' // generic type parameter
366366
entity-spec ::= decl-name type 'fo' // enum element (currently not used)
367367
entity-spec ::= decl-name label-list? type generic-signature? 'fm' // macro
368+
entity-spec ::= context macro-discriminator-list // macro expansion
368369
entity-spec ::= identifier 'Qa' // associated type declaration
369370

370371
ACCESSOR ::= 'm' // materializeForSet
@@ -392,6 +393,10 @@ Entities
392393
RELATED-DISCRIMINATOR ::= [a-j]
393394
RELATED-DISCRIMINATOR ::= [A-J]
394395

396+
macro-discriminator-list ::= macro-discriminator-list? 'fM' macro-expansion-operator INDEX
397+
398+
macro-expansion-operator ::= 'f' // freestanding macro
399+
395400
file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration
396401

397402
The identifier in a ``<file-discriminator>`` and the second identifier in a

include/swift/AST/ASTContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ namespace swift {
8787
class LazyContextData;
8888
class LazyIterableDeclContextData;
8989
class LazyMemberLoader;
90+
struct MacroDiscriminatorContext;
9091
class ModuleDependencyInfo;
9192
class PatternBindingDecl;
9293
class PatternBindingInitializer;
@@ -1070,6 +1071,11 @@ class ASTContext final {
10701071
AbstractFunctionDecl *originalAFD, unsigned previousGeneration,
10711072
llvm::SetVector<AutoDiffConfig> &results);
10721073

1074+
/// Retrieve the next macro expansion discriminator within the given
1075+
/// name and context.
1076+
unsigned getNextMacroDiscriminator(MacroDiscriminatorContext context,
1077+
DeclBaseName baseName);
1078+
10731079
/// Retrieve the Clang module loader for this ASTContext.
10741080
///
10751081
/// If there is no Clang module loader, returns a null pointer.

include/swift/AST/ASTMangler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace swift {
2626

2727
class AbstractClosureExpr;
2828
class ConformancePath;
29+
class MacroExpansionExpr;
2930
class RootProtocolConformance;
3031

3132
namespace Mangle {
@@ -365,6 +366,12 @@ class ASTMangler : public Mangler {
365366
mangleRuntimeAttributeGeneratorEntity(const ValueDecl *decl, CustomAttr *attr,
366367
SymbolKind SKind = SymbolKind::Default);
367368

369+
std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
370+
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
371+
void appendMacroExpansionContext(SourceLoc loc, DeclContext *origDC);
372+
void appendMacroExpansionOperator(
373+
StringRef macroName, unsigned discriminator);
374+
368375
enum SpecialContext {
369376
ObjCContext,
370377
ClangImporterContext,

include/swift/AST/Decl.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
725725
NumberOfVTableEntries : 2
726726
);
727727

728+
SWIFT_INLINE_BITFIELD(MacroExpansionDecl, Decl, 16,
729+
Discriminator : 16
730+
);
731+
728732
} Bits;
729733

730734
// Storage for the declaration attributes.
@@ -8454,6 +8458,8 @@ class MacroExpansionDecl : public Decl {
84548458
ConcreteDeclRef macroRef;
84558459

84568460
public:
8461+
enum : unsigned { InvalidDiscriminator = 0xFFFF };
8462+
84578463
MacroExpansionDecl(DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
84588464
DeclNameLoc macroLoc,
84598465
SourceLoc leftAngleLoc,
@@ -8463,7 +8469,9 @@ class MacroExpansionDecl : public Decl {
84638469
: Decl(DeclKind::MacroExpansion, dc), PoundLoc(poundLoc),
84648470
Macro(macro), MacroLoc(macroLoc),
84658471
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
8466-
GenericArgs(genericArgs), ArgList(args), Rewritten({}) {}
8472+
GenericArgs(genericArgs), ArgList(args), Rewritten({}) {
8473+
Bits.MacroExpansionDecl.Discriminator = InvalidDiscriminator;
8474+
}
84678475

84688476
ArrayRef<TypeRepr *> getGenericArgs() const { return GenericArgs; }
84698477

@@ -8482,6 +8490,24 @@ class MacroExpansionDecl : public Decl {
84828490
ConcreteDeclRef getMacroRef() const { return macroRef; }
84838491
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
84848492

8493+
/// Returns a discriminator which determines this macro expansion's index
8494+
/// in the sequence of macro expansions within the current function.
8495+
unsigned getDiscriminator() const;
8496+
8497+
/// Retrieve the raw discriminator, which may not have been computed yet.
8498+
///
8499+
/// Only use this for queries that are checking for (e.g.) reentrancy or
8500+
/// intentionally do not want to initiate verification.
8501+
unsigned getRawDiscriminator() const {
8502+
return Bits.MacroExpansionDecl.Discriminator;
8503+
}
8504+
8505+
void setDiscriminator(unsigned discriminator) {
8506+
assert(getRawDiscriminator() == InvalidDiscriminator);
8507+
assert(discriminator != InvalidDiscriminator);
8508+
Bits.MacroExpansionDecl.Discriminator = discriminator;
8509+
}
8510+
84858511
static bool classof(const Decl *D) {
84868512
return D->getKind() == DeclKind::MacroExpansion;
84878513
}

include/swift/AST/Expr.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
363363
: NumPadBits,
364364
NumElements : 32
365365
);
366+
367+
SWIFT_INLINE_BITFIELD(MacroExpansionExpr, Expr, (16-NumExprBits)+16,
368+
: 16 - NumExprBits, // Align and leave room for subclasses
369+
Discriminator : 16
370+
);
371+
366372
} Bits;
367373

368374
private:
@@ -6047,6 +6053,7 @@ class TypeJoinExpr final : public Expr,
60476053

60486054
class MacroExpansionExpr final : public Expr {
60496055
private:
6056+
DeclContext *DC;
60506057
SourceLoc PoundLoc;
60516058
DeclNameRef MacroName;
60526059
DeclNameLoc MacroNameLoc;
@@ -6059,19 +6066,26 @@ class MacroExpansionExpr final : public Expr {
60596066
ConcreteDeclRef macroRef;
60606067

60616068
public:
6062-
explicit MacroExpansionExpr(SourceLoc poundLoc, DeclNameRef macroName,
6069+
enum : unsigned { InvalidDiscriminator = 0xFFFF };
6070+
6071+
explicit MacroExpansionExpr(DeclContext *dc,
6072+
SourceLoc poundLoc, DeclNameRef macroName,
60636073
DeclNameLoc macroNameLoc,
60646074
SourceLoc leftAngleLoc,
60656075
ArrayRef<TypeRepr *> genericArgs,
60666076
SourceLoc rightAngleLoc,
6067-
ArgumentList *argList, bool isImplicit = false,
6077+
ArgumentList *argList,
6078+
bool isImplicit = false,
60686079
Type ty = Type())
6069-
: Expr(ExprKind::MacroExpansion, isImplicit, ty), PoundLoc(poundLoc),
6080+
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
6081+
DC(dc), PoundLoc(poundLoc),
60706082
MacroName(macroName), MacroNameLoc(macroNameLoc),
60716083
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
60726084
GenericArgs(genericArgs),
60736085
ArgList(argList),
6074-
Rewritten(nullptr) { }
6086+
Rewritten(nullptr) {
6087+
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
6088+
}
60756089

60766090
DeclNameRef getMacroName() const { return MacroName; }
60776091
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
@@ -6093,6 +6107,27 @@ class MacroExpansionExpr final : public Expr {
60936107
ConcreteDeclRef getMacroRef() const { return macroRef; }
60946108
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
60956109

6110+
DeclContext *getDeclContext() const { return DC; }
6111+
void setDeclContext(DeclContext *dc) { DC = dc; }
6112+
6113+
/// Returns a discriminator which determines this macro expansion's index
6114+
/// in the sequence of macro expansions within the current function.
6115+
unsigned getDiscriminator() const;
6116+
6117+
/// Retrieve the raw discriminator, which may not have been computed yet.
6118+
///
6119+
/// Only use this for queries that are checking for (e.g.) reentrancy or
6120+
/// intentionally do not want to initiate verification.
6121+
unsigned getRawDiscriminator() const {
6122+
return Bits.MacroExpansionExpr.Discriminator;
6123+
}
6124+
6125+
void setDiscriminator(unsigned discriminator) {
6126+
assert(getRawDiscriminator() == InvalidDiscriminator);
6127+
assert(discriminator != InvalidDiscriminator);
6128+
Bits.MacroExpansionExpr.Discriminator = discriminator;
6129+
}
6130+
60966131
SourceRange getSourceRange() const;
60976132

60986133
static bool classof(const Expr *E) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- MacroDiscriminatorContext.h - Macro Discriminators -----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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_MACRO_DISCRIMINATOR_CONTEXT_H
14+
#define SWIFT_AST_MACRO_DISCRIMINATOR_CONTEXT_H
15+
16+
#include "swift/AST/Decl.h"
17+
#include "swift/AST/Expr.h"
18+
#include "llvm/ADT/PointerUnion.h"
19+
20+
namespace swift {
21+
22+
/// Describes the context of a macro expansion for the purpose of
23+
/// computing macro expansion discriminators.
24+
struct MacroDiscriminatorContext
25+
: public llvm::PointerUnion<DeclContext *, MacroExpansionExpr *,
26+
MacroExpansionDecl *> {
27+
using PointerUnion::PointerUnion;
28+
29+
static MacroDiscriminatorContext getParentOf(MacroExpansionExpr *expansion);
30+
static MacroDiscriminatorContext getParentOf(MacroExpansionDecl *expansion);
31+
static MacroDiscriminatorContext getParentOf(
32+
SourceLoc loc, DeclContext *origDC
33+
);
34+
};
35+
36+
}
37+
38+
#endif // SWIFT_AST_MACRO_DISCRIMINATOR_CONTEXT_H

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ NODE(LazyProtocolWitnessTableAccessor)
148148
NODE(LazyProtocolWitnessTableCacheVariable)
149149
NODE(LocalDeclName)
150150
NODE(Macro)
151+
NODE(MacroExpansion)
151152
CONTEXT_NODE(MaterializeForSet)
152153
NODE(MergedFunction)
153154
NODE(Metatype)

include/swift/Demangling/Demangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ class Demangler : public NodeFactory {
589589
NodePointer demangleGenericRequirement();
590590
NodePointer demangleGenericType();
591591
NodePointer demangleValueWitness();
592+
NodePointer demangleMacroExpansion();
592593

593594
NodePointer demangleTypeMangling();
594595
NodePointer demangleSymbolicReference(unsigned char rawKind);

lib/AST/ASTContext.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/AST/IndexSubset.h"
3636
#include "swift/AST/KnownProtocols.h"
3737
#include "swift/AST/LazyResolver.h"
38+
#include "swift/AST/MacroDiscriminatorContext.h"
3839
#include "swift/AST/ModuleDependencies.h"
3940
#include "swift/AST/ModuleLoader.h"
4041
#include "swift/AST/NameLookup.h"
@@ -400,6 +401,10 @@ struct ASTContext::Implementation {
400401
/// is populated if the body is reparsed from other source buffers.
401402
llvm::DenseMap<const AbstractFunctionDecl *, SourceRange> OriginalBodySourceRanges;
402403

404+
/// Macro discriminators per context.
405+
llvm::DenseMap<std::pair<const void *, Identifier>, unsigned>
406+
NextMacroDiscriminator;
407+
403408
/// Structure that captures data that is segregated into different
404409
/// arenas.
405410
struct Arena {
@@ -2154,6 +2159,15 @@ void ASTContext::loadDerivativeFunctionConfigurations(
21542159
}
21552160
}
21562161

2162+
unsigned ASTContext::getNextMacroDiscriminator(
2163+
MacroDiscriminatorContext context,
2164+
DeclBaseName baseName
2165+
) {
2166+
std::pair<const void *, Identifier> key(
2167+
context.getOpaqueValue(), baseName.getIdentifier());
2168+
return getImpl().NextMacroDiscriminator[key]++;
2169+
}
2170+
21572171
void ASTContext::verifyAllLoadedModules() const {
21582172
#ifndef NDEBUG
21592173
FrontendStatsTracer tracer(Stats, "verify-all-loaded-modules");

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,8 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
30213021
void visitMacroExpansionExpr(MacroExpansionExpr *E) {
30223022
printCommon(E, "macro_expansion_expr");
30233023
PrintWithColorRAII(OS, IdentifierColor) << " name=" << E->getMacroName();
3024+
PrintWithColorRAII(OS, DiscriminatorColor)
3025+
<< " discriminator=" << E->getRawDiscriminator();
30243026
if (E->getArgs()) {
30253027
OS << '\n';
30263028
printArgumentList(E->getArgs());

0 commit comments

Comments
 (0)