Skip to content

Commit 72ddbeb

Browse files
committed
[Macros] Assign macro expansion discriminators more lazily.
This is a punt. They'll be unique, and they'll be per-context, but they'll be lazy and therefore not stable.
1 parent 6e3289d commit 72ddbeb

File tree

9 files changed

+173
-35
lines changed

9 files changed

+173
-35
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 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,10 @@ 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+
/// context.
1076+
unsigned getNextMacroDiscriminator(MacroDiscriminatorContext context);
1077+
10731078
/// Retrieve the Clang module loader for this ASTContext.
10741079
///
10751080
/// If there is no Clang module loader, returns a null pointer.
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

lib/AST/ASTContext.cpp

Lines changed: 9 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,9 @@ 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<const void *, unsigned> NextMacroDiscriminator;
406+
403407
/// Structure that captures data that is segregated into different
404408
/// arenas.
405409
struct Arena {
@@ -2154,6 +2158,11 @@ void ASTContext::loadDerivativeFunctionConfigurations(
21542158
}
21552159
}
21562160

2161+
unsigned ASTContext::getNextMacroDiscriminator(
2162+
MacroDiscriminatorContext context) {
2163+
return getImpl().NextMacroDiscriminator[context.getOpaqueValue()]++;
2164+
}
2165+
21572166
void ASTContext::verifyAllLoadedModules() const {
21582167
#ifndef NDEBUG
21592168
FrontendStatsTracer tracer(Stats, "verify-all-loaded-modules");

lib/AST/ASTMangler.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/GenericSignature.h"
2424
#include "swift/AST/Initializer.h"
2525
#include "swift/AST/LazyResolver.h"
26+
#include "swift/AST/MacroDiscriminatorContext.h"
2627
#include "swift/AST/Module.h"
2728
#include "swift/AST/Ownership.h"
2829
#include "swift/AST/ParameterList.h"
@@ -3677,6 +3678,53 @@ std::string ASTMangler::mangleRuntimeAttributeGeneratorEntity(
36773678
return finalize();
36783679
}
36793680

3681+
void ASTMangler::appendMacroExpansionContext(
3682+
SourceLoc loc, DeclContext *origDC
3683+
) {
3684+
if (loc.isInvalid())
3685+
return appendContext(origDC, StringRef());
3686+
3687+
ASTContext &ctx = origDC->getASTContext();
3688+
SourceManager &sourceMgr = ctx.SourceMgr;
3689+
3690+
auto bufferID = sourceMgr.findBufferContainingLoc(loc);
3691+
auto generatedSourceInfo = sourceMgr.getGeneratedSourceInfo(bufferID);
3692+
if (!generatedSourceInfo)
3693+
return appendContext(origDC, StringRef());
3694+
3695+
auto parent = MacroDiscriminatorContext::getParentOf(loc, origDC);
3696+
if (auto dc = parent.dyn_cast<DeclContext *>())
3697+
return appendContext(dc, StringRef());
3698+
3699+
SourceLoc outerExpansionLoc;
3700+
unsigned discriminator;
3701+
3702+
if (auto expr = parent.dyn_cast<MacroExpansionExpr *>()) {
3703+
outerExpansionLoc = expr->getLoc();
3704+
discriminator = expr->getDiscriminator();
3705+
} else {
3706+
auto decl = parent.get<MacroExpansionDecl *>();
3707+
outerExpansionLoc = decl->getLoc();
3708+
discriminator = decl->getDiscriminator();
3709+
}
3710+
3711+
// Append our own context and discriminator.
3712+
appendMacroExpansionContext(outerExpansionLoc, origDC);
3713+
appendMacroExpansionOperator(discriminator);
3714+
}
3715+
3716+
void ASTMangler::appendMacroExpansionOperator(unsigned discriminator) {
3717+
appendOperator("fM", Index(discriminator));
3718+
}
3719+
3720+
std::string ASTMangler::mangleMacroExpansion(
3721+
const MacroExpansionExpr *expansion) {
3722+
beginMangling();
3723+
appendMacroExpansionContext(expansion->getLoc(), expansion->getDeclContext());
3724+
appendMacroExpansionOperator(expansion->getDiscriminator());
3725+
return finalize();
3726+
}
3727+
36803728
static void gatherExistentialRequirements(SmallVectorImpl<Requirement> &reqs,
36813729
ParameterizedProtocolType *PPT) {
36823730
auto protoTy = PPT->getBaseType();

lib/AST/Decl.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/AST/Initializer.h"
3434
#include "swift/AST/LazyResolver.h"
3535
#include "swift/AST/MacroDefinition.h"
36+
#include "swift/AST/MacroDiscriminatorContext.h"
3637
#include "swift/AST/Module.h"
3738
#include "swift/AST/NameLookup.h"
3839
#include "swift/AST/NameLookupRequests.h"
@@ -9864,9 +9865,13 @@ unsigned MacroExpansionDecl::getDiscriminator() const {
98649865
if (getRawDiscriminator() != InvalidDiscriminator)
98659866
return getRawDiscriminator();
98669867

9868+
auto mutableThis = const_cast<MacroExpansionDecl *>(this);
98679869
auto dc = getDeclContext();
98689870
ASTContext &ctx = dc->getASTContext();
9869-
evaluateOrDefault(ctx.evaluator, LocalDiscriminatorsRequest{dc}, 0);
9871+
auto discriminatorContext =
9872+
MacroDiscriminatorContext::getParentOf(mutableThis);
9873+
mutableThis->setDiscriminator(
9874+
ctx.getNextMacroDiscriminator(discriminatorContext));
98709875

98719876
assert(getRawDiscriminator() != InvalidDiscriminator);
98729877
return getRawDiscriminator();
@@ -9888,3 +9893,58 @@ ArrayRef<CustomAttr *> ValueDecl::getRuntimeDiscoverableAttrs() const {
98889893
GetRuntimeDiscoverableAttributes{mutableSelf},
98899894
nullptr);
98909895
}
9896+
9897+
/// Retrieve the parent discriminator context for the given macro.
9898+
MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
9899+
SourceLoc loc, DeclContext *origDC) {
9900+
if (loc.isInvalid())
9901+
return origDC;
9902+
9903+
ASTContext &ctx = origDC->getASTContext();
9904+
SourceManager &sourceMgr = ctx.SourceMgr;
9905+
9906+
auto bufferID = sourceMgr.findBufferContainingLoc(loc);
9907+
auto generatedSourceInfo = sourceMgr.getGeneratedSourceInfo(bufferID);
9908+
if (!generatedSourceInfo)
9909+
return origDC;
9910+
9911+
switch (generatedSourceInfo->kind) {
9912+
case GeneratedSourceInfo::ExpressionMacroExpansion: {
9913+
auto expansion =
9914+
cast<MacroExpansionExpr>(
9915+
ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode)
9916+
.get<Expr *>());
9917+
if (!origDC->isChildContextOf(expansion->getDeclContext()))
9918+
return MacroDiscriminatorContext(expansion);
9919+
return origDC;
9920+
}
9921+
9922+
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
9923+
auto expansion =
9924+
cast<MacroExpansionDecl>(
9925+
ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode)
9926+
.get<Decl *>());
9927+
if (!origDC->isChildContextOf(expansion->getDeclContext()))
9928+
return MacroDiscriminatorContext(expansion);
9929+
return origDC;
9930+
}
9931+
9932+
case GeneratedSourceInfo::AccessorMacroExpansion:
9933+
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
9934+
case GeneratedSourceInfo::PrettyPrinted:
9935+
case GeneratedSourceInfo::ReplacedFunctionBody:
9936+
return origDC;
9937+
}
9938+
}
9939+
9940+
MacroDiscriminatorContext
9941+
MacroDiscriminatorContext::getParentOf(MacroExpansionExpr *expansion) {
9942+
return getParentOf(
9943+
expansion->getLoc(), expansion->getDeclContext());
9944+
}
9945+
9946+
MacroDiscriminatorContext
9947+
MacroDiscriminatorContext::getParentOf(MacroExpansionDecl *expansion) {
9948+
return getParentOf(
9949+
expansion->getLoc(), expansion->getDeclContext());
9950+
}

lib/AST/Expr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/ASTVisitor.h"
2222
#include "swift/AST/Decl.h" // FIXME: Bad dependency
23+
#include "swift/AST/MacroDiscriminatorContext.h"
2324
#include "swift/AST/ParameterList.h"
2425
#include "swift/AST/Stmt.h"
2526
#include "swift/AST/ASTWalker.h"
@@ -2563,9 +2564,13 @@ unsigned MacroExpansionExpr::getDiscriminator() const {
25632564
if (getRawDiscriminator() != InvalidDiscriminator)
25642565
return getRawDiscriminator();
25652566

2567+
auto mutableThis = const_cast<MacroExpansionExpr *>(this);
25662568
auto dc = getDeclContext();
25672569
ASTContext &ctx = dc->getASTContext();
2568-
evaluateOrDefault(ctx.evaluator, LocalDiscriminatorsRequest{dc}, 0);
2570+
auto discriminatorContext =
2571+
MacroDiscriminatorContext::getParentOf(mutableThis);
2572+
mutableThis->setDiscriminator(
2573+
ctx.getNextMacroDiscriminator(discriminatorContext));
25692574

25702575
assert(getRawDiscriminator() != InvalidDiscriminator);
25712576
return getRawDiscriminator();

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3782,6 +3782,9 @@ namespace {
37823782
}
37833783

37843784
Type visitMacroExpansionExpr(MacroExpansionExpr *expr) {
3785+
// Assign a discriminator.
3786+
(void)expr->getDiscriminator();
3787+
37853788
auto &ctx = CS.getASTContext();
37863789
auto locator = CS.getConstraintLocator(expr);
37873790

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20492049
}
20502050

20512051
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {
2052+
// Assign a discriminator.
2053+
(void)MED->getDiscriminator();
2054+
20522055
(void)evaluateOrDefault(
20532056
Ctx.evaluator, ExpandMacroExpansionDeclRequest{MED}, {});
20542057
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ namespace {
231231
/// Local declaration discriminators.
232232
llvm::SmallDenseMap<Identifier, unsigned> DeclDiscriminators;
233233

234-
/// Macro expansion discriminator.
235-
unsigned NextMacroExpansionDiscriminator = 0;
236-
237234
public:
238235
SetLocalDiscriminators(
239236
unsigned initialDiscriminator = 0
@@ -290,28 +287,6 @@ namespace {
290287
return Action::SkipChildren(E);
291288
}
292289

293-
// Macro expansion expressions get a discriminator.
294-
if (auto macroExpansion = dyn_cast<MacroExpansionExpr>(E)) {
295-
if (macroExpansion->getRawDiscriminator() ==
296-
MacroExpansionExpr::InvalidDiscriminator) {
297-
macroExpansion->setDiscriminator(NextMacroExpansionDiscriminator++);
298-
}
299-
300-
// Walk the arguments.
301-
if (auto args = macroExpansion->getArgs())
302-
args->walk(*this);
303-
304-
// If there is a rewritten expression, walk it with a fresh macro
305-
// discriminator.
306-
if (auto rewritten = macroExpansion->getRewritten()) {
307-
llvm::SaveAndRestore<unsigned> savedMacroDiscriminator(
308-
NextMacroExpansionDiscriminator, 0u);
309-
rewritten->walk(*this);
310-
}
311-
312-
return Action::SkipChildren(E);
313-
}
314-
315290
// Caller-side default arguments need their @autoclosures checked.
316291
if (auto *DAE = dyn_cast<DefaultArgumentExpr>(E))
317292
if (DAE->isCallerSide() && DAE->getParamDecl()->isAutoClosure())
@@ -342,14 +317,6 @@ namespace {
342317
setLocalDiscriminator(valueDecl);
343318
}
344319

345-
// If we have a macro expansion declaration, assign a discriminator to it.
346-
if (auto macroExpansion = dyn_cast<MacroExpansionDecl>(D)) {
347-
if (macroExpansion->getRawDiscriminator() ==
348-
MacroExpansionDecl::InvalidDiscriminator) {
349-
macroExpansion->setDiscriminator(NextMacroExpansionDiscriminator++);
350-
}
351-
}
352-
353320
// But we do want to walk into the initializers of local
354321
// variables.
355322
return Action::VisitChildrenIf(isa<PatternBindingDecl>(D));

0 commit comments

Comments
 (0)