Skip to content

Commit 8c8a385

Browse files
committed
Sema: Record SyntacticElementTargetKeys in the trail
1 parent 516277f commit 8c8a385

File tree

7 files changed

+194
-84
lines changed

7 files changed

+194
-84
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ CHANGE(FavoredConstraint)
6262
CHANGE(RecordedResultBuilderTransform)
6363
CHANGE(RecordedClosureType)
6464
CHANGE(RecordedContextualInfo)
65+
CHANGE(RecordedTarget)
6566

66-
LAST_CHANGE(RecordedContextualInfo)
67+
LAST_CHANGE(RecordedTarget)
6768

6869
#undef LOCATOR_CHANGE
6970
#undef EXPR_CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TypeVariableType;
3434
namespace constraints {
3535

3636
class Constraint;
37+
struct SyntacticElementTargetKey;
3738

3839
class SolverTrail {
3940
public:
@@ -102,7 +103,6 @@ class SolverTrail {
102103
Type DstType;
103104
} Restriction;
104105

105-
ConstraintFix *Fix;
106106

107107
struct {
108108
GenericTypeParamType *GP;
@@ -119,12 +119,19 @@ class SolverTrail {
119119
Type OldType;
120120
} KeyPath;
121121

122-
ConstraintLocator *Locator;
123-
PackExpansionType *ExpansionTy;
124-
PackElementExpr *ElementExpr;
122+
ConstraintFix *TheFix;
123+
ConstraintLocator *TheLocator;
124+
PackExpansionType *TheExpansion;
125+
PackElementExpr *TheElement;
125126
Expr *TheExpr;
126-
AnyFunctionRef AFR;
127-
const ClosureExpr *Closure;
127+
Stmt *TheStmt;
128+
StmtConditionElement *TheCondElt;
129+
Pattern *ThePattern;
130+
PatternBindingDecl *ThePatternBinding;
131+
VarDecl *TheVar;
132+
AnyFunctionRef TheRef;
133+
ClosureExpr *TheClosure;
134+
DeclContext *TheDeclContext;
128135
};
129136

130137
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -205,11 +212,14 @@ class SolverTrail {
205212
static Change RecordedResultBuilderTransform(AnyFunctionRef fn);
206213

207214
/// Create a change that recorded a closure type.
208-
static Change RecordedClosureType(const ClosureExpr *closure);
215+
static Change RecordedClosureType(ClosureExpr *closure);
209216

210217
/// Create a change that recorded the contextual type of an AST node.
211218
static Change RecordedContextualInfo(ASTNode node);
212219

220+
/// Create a change that recorded a SyntacticElementTarget.
221+
static Change RecordedTarget(SyntacticElementTargetKey key);
222+
213223
/// Undo this change, reverting the constraint graph to the state it
214224
/// had prior to this change.
215225
///
@@ -218,6 +228,9 @@ class SolverTrail {
218228

219229
void dump(llvm::raw_ostream &out, ConstraintSystem &cs,
220230
unsigned indent = 0) const;
231+
232+
private:
233+
SyntacticElementTargetKey getSyntacticElementTargetKey() const;
221234
};
222235

223236
SolverTrail(ConstraintSystem &cs);

include/swift/Sema/ConstraintSystem.h

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,7 @@ struct CaseLabelItemInfo {
12031203

12041204
/// Key to the constraint solver's mapping from AST nodes to their corresponding
12051205
/// target.
1206-
class SyntacticElementTargetKey {
1207-
public:
1206+
struct SyntacticElementTargetKey {
12081207
enum class Kind {
12091208
empty,
12101209
tombstone,
@@ -1218,72 +1217,75 @@ class SyntacticElementTargetKey {
12181217
functionRef,
12191218
};
12201219

1221-
private:
12221220
Kind kind;
12231221

12241222
union {
1225-
const StmtConditionElement *stmtCondElement;
1223+
StmtConditionElement *stmtCondElement;
12261224

1227-
const Expr *expr;
1225+
Expr *expr;
12281226

1229-
const Stmt *stmt;
1227+
Stmt *stmt;
12301228

1231-
const Pattern *pattern;
1229+
Pattern *pattern;
12321230

12331231
struct PatternBindingEntry {
1234-
const PatternBindingDecl *patternBinding;
1232+
PatternBindingDecl *patternBinding;
12351233
unsigned index;
12361234
} patternBindingEntry;
12371235

1238-
const VarDecl *varDecl;
1236+
VarDecl *varDecl;
12391237

1240-
const DeclContext *functionRef;
1238+
DeclContext *functionRef;
12411239
} storage;
12421240

1243-
public:
12441241
SyntacticElementTargetKey(Kind kind) {
12451242
assert(kind == Kind::empty || kind == Kind::tombstone);
12461243
this->kind = kind;
12471244
}
12481245

1249-
SyntacticElementTargetKey(const StmtConditionElement *stmtCondElement) {
1246+
SyntacticElementTargetKey(StmtConditionElement *stmtCondElement) {
12501247
kind = Kind::stmtCondElement;
12511248
storage.stmtCondElement = stmtCondElement;
12521249
}
12531250

1254-
SyntacticElementTargetKey(const Expr *expr) {
1251+
SyntacticElementTargetKey(Expr *expr) {
12551252
kind = Kind::expr;
12561253
storage.expr = expr;
12571254
}
12581255

1259-
SyntacticElementTargetKey(const ClosureExpr *closure) {
1256+
SyntacticElementTargetKey(ClosureExpr *closure) {
12601257
kind = Kind::closure;
12611258
storage.expr = closure;
12621259
}
12631260

1264-
SyntacticElementTargetKey(const Stmt *stmt) {
1261+
SyntacticElementTargetKey(Stmt *stmt) {
12651262
kind = Kind::stmt;
12661263
storage.stmt = stmt;
12671264
}
12681265

1269-
SyntacticElementTargetKey(const Pattern *pattern) {
1266+
SyntacticElementTargetKey(Pattern *pattern) {
12701267
kind = Kind::pattern;
12711268
storage.pattern = pattern;
12721269
}
12731270

1274-
SyntacticElementTargetKey(const PatternBindingDecl *patternBinding,
1271+
SyntacticElementTargetKey(PatternBindingDecl *patternBinding,
12751272
unsigned index) {
12761273
kind = Kind::patternBindingEntry;
12771274
storage.patternBindingEntry.patternBinding = patternBinding;
12781275
storage.patternBindingEntry.index = index;
12791276
}
12801277

1281-
SyntacticElementTargetKey(const VarDecl *varDecl) {
1278+
SyntacticElementTargetKey(VarDecl *varDecl) {
12821279
kind = Kind::varDecl;
12831280
storage.varDecl = varDecl;
12841281
}
12851282

1286-
SyntacticElementTargetKey(const AnyFunctionRef functionRef) {
1283+
SyntacticElementTargetKey(DeclContext *dc) {
1284+
kind = Kind::functionRef;
1285+
storage.functionRef = dc;
1286+
}
1287+
1288+
SyntacticElementTargetKey(AnyFunctionRef functionRef) {
12871289
kind = Kind::functionRef;
12881290
storage.functionRef = functionRef.getAsDeclContext();
12891291
}
@@ -1574,7 +1576,7 @@ class Solution {
15741576
std::vector<std::pair<ASTNode, ContextualTypeInfo>> contextualTypes;
15751577

15761578
/// Maps AST nodes to their target.
1577-
llvm::MapVector<SyntacticElementTargetKey, SyntacticElementTarget> targets;
1579+
llvm::DenseMap<SyntacticElementTargetKey, SyntacticElementTarget> targets;
15781580

15791581
/// Maps case label items to information tracked about them as they are
15801582
/// being solved.
@@ -1727,12 +1729,7 @@ class Solution {
17271729
}
17281730

17291731
std::optional<SyntacticElementTarget>
1730-
getTargetFor(SyntacticElementTargetKey key) const {
1731-
auto known = targets.find(key);
1732-
if (known == targets.end())
1733-
return std::nullopt;
1734-
return known->second;
1735-
}
1732+
getTargetFor(SyntacticElementTargetKey key) const;
17361733

17371734
ConstraintLocator *getCalleeLocator(ConstraintLocator *locator,
17381735
bool lookThroughApply = true) const;
@@ -2275,7 +2272,7 @@ class ConstraintSystem {
22752272
KeyPaths;
22762273

22772274
/// Maps AST entries to their targets.
2278-
llvm::MapVector<SyntacticElementTargetKey, SyntacticElementTarget> targets;
2275+
llvm::DenseMap<SyntacticElementTargetKey, SyntacticElementTarget> targets;
22792276

22802277
/// Contextual type information for expressions that are part of this
22812278
/// constraint system. The second type, if valid, contains the type as it
@@ -2860,9 +2857,6 @@ class ConstraintSystem {
28602857
/// FIXME: Remove this.
28612858
unsigned numFixes;
28622859

2863-
/// The length of \c targets.
2864-
unsigned numTargets;
2865-
28662860
/// The length of \c caseLabelItems.
28672861
unsigned numCaseLabelItems;
28682862

@@ -3090,8 +3084,10 @@ class ConstraintSystem {
30903084
bool inserted = ClosureTypes.insert({closure, type}).second;
30913085
ASSERT(inserted);
30923086

3093-
if (solverState)
3094-
recordChange(SolverTrail::Change::RecordedClosureType(closure));
3087+
if (solverState) {
3088+
recordChange(SolverTrail::Change::RecordedClosureType(
3089+
const_cast<ClosureExpr *>(closure)));
3090+
}
30953091
}
30963092

30973093
void removeClosureType(const ClosureExpr *closure) {
@@ -3359,18 +3355,12 @@ class ConstraintSystem {
33593355
}
33603356

33613357
void setTargetFor(SyntacticElementTargetKey key,
3362-
SyntacticElementTarget target) {
3363-
assert(targets.count(key) == 0 && "Already set this target");
3364-
targets.insert({key, target});
3365-
}
3358+
SyntacticElementTarget target);
3359+
3360+
void removeTargetFor(SyntacticElementTargetKey key);
33663361

33673362
std::optional<SyntacticElementTarget>
3368-
getTargetFor(SyntacticElementTargetKey key) const {
3369-
auto known = targets.find(key);
3370-
if (known == targets.end())
3371-
return std::nullopt;
3372-
return known->second;
3373-
}
3363+
getTargetFor(SyntacticElementTargetKey key) const;
33743364

33753365
std::optional<AppliedBuilderTransform>
33763366
getAppliedResultBuilderTransform(AnyFunctionRef fn) const {

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4995,7 +4995,7 @@ bool ConstraintSystem::generateConstraints(StmtCondition condition,
49954995
}
49964996

49974997
Type boolTy = boolDecl->getDeclaredInterfaceType();
4998-
for (const auto &condElement : condition) {
4998+
for (auto &condElement : condition) {
49994999
switch (condElement.getKind()) {
50005000
case StmtConditionElement::CK_Availability:
50015001
// Nothing to do here.

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ Solution ConstraintSystem::finalize() {
218218
solution.contextualTypes.push_back({entry.first, entry.second.first});
219219
}
220220

221-
solution.targets = targets;
221+
for (auto &target : targets)
222+
solution.targets.insert(target);
222223

223224
for (const auto &item : caseLabelItems)
224225
solution.caseLabelItems.insert(item);
@@ -692,7 +693,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
692693
numTypeVariables = cs.TypeVariables.size();
693694
numFixes = cs.Fixes.size();
694695
numKeyPaths = cs.KeyPaths.size();
695-
numTargets = cs.targets.size();
696696
numCaseLabelItems = cs.caseLabelItems.size();
697697
numPotentialThrowSites = cs.potentialThrowSites.size();
698698
numExprPatterns = cs.exprPatterns.size();
@@ -737,9 +737,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
737737
/// Remove any key path expressions.
738738
truncate(cs.KeyPaths, numKeyPaths);
739739

740-
// Remove any targets.
741-
truncate(cs.targets, numTargets);
742-
743740
// Remove any case label item infos.
744741
truncate(cs.caseLabelItems, numCaseLabelItems);
745742

lib/Sema/CSSyntacticElement.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@
2626
using namespace swift;
2727
using namespace swift::constraints;
2828

29+
void ConstraintSystem::setTargetFor(SyntacticElementTargetKey key,
30+
SyntacticElementTarget target) {
31+
bool inserted = targets.insert({key, target}).second;
32+
ASSERT(inserted);
33+
34+
if (solverState)
35+
recordChange(SolverTrail::Change::RecordedTarget(key));
36+
}
37+
38+
void ConstraintSystem::removeTargetFor(SyntacticElementTargetKey key) {
39+
bool erased = targets.erase(key);
40+
ASSERT(erased);
41+
}
42+
43+
std::optional<SyntacticElementTarget>
44+
ConstraintSystem::getTargetFor(SyntacticElementTargetKey key) const {
45+
auto known = targets.find(key);
46+
if (known == targets.end())
47+
return std::nullopt;
48+
return known->second;
49+
}
50+
51+
std::optional<SyntacticElementTarget>
52+
Solution::getTargetFor(SyntacticElementTargetKey key) const {
53+
auto known = targets.find(key);
54+
if (known == targets.end())
55+
return std::nullopt;
56+
return known->second;
57+
}
58+
2959
namespace {
3060

3161
// Produce an implicit empty tuple expression.

0 commit comments

Comments
 (0)