Skip to content

Commit 5c0d395

Browse files
xedinrudkx
authored andcommitted
[Type Checker] replace std::{stack, queue} with llvm::SmallVector
(cherry picked from commit 99ac807)
1 parent fae33c1 commit 5c0d395

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#include "llvm/Support/SaveAndRestore.h"
2121
#include <memory>
2222
#include <tuple>
23-
#include <stack>
24-
#include <queue>
2523
using namespace swift;
2624
using namespace constraints;
2725

@@ -1475,23 +1473,21 @@ void ConstraintSystem::shrink(Expr *expr) {
14751473
// The primary constraint system.
14761474
ConstraintSystem &CS;
14771475

1478-
// All of the sub-expressions of certain type (binary/unary/calls) in
1479-
// depth-first order.
1480-
std::queue<Candidate> &SubExprs;
1476+
// All of the sub-expressions which are suitable to be solved
1477+
// separately from the main system e.g. binary expressions, collections,
1478+
// function calls, coercions etc.
1479+
llvm::SmallVector<Candidate, 4> Candidates;
14811480

14821481
// Counts the number of overload sets present in the tree so far.
14831482
// Note that the traversal is depth-first.
1484-
std::stack<std::pair<ApplyExpr *, unsigned>,
1485-
llvm::SmallVector<std::pair<ApplyExpr *, unsigned>, 4>>
1486-
ApplyExprs;
1483+
llvm::SmallVector<std::pair<ApplyExpr *, unsigned>, 4> ApplyExprs;
14871484

14881485
// A collection of original domains of all of the expressions,
14891486
// so they can be restored in case of failure.
14901487
DomainMap &Domains;
14911488

1492-
ExprCollector(Expr *expr, ConstraintSystem &cs,
1493-
std::queue<Candidate> &container, DomainMap &domains)
1494-
: PrimaryExpr(expr), CS(cs), SubExprs(container), Domains(domains) {}
1489+
ExprCollector(Expr *expr, ConstraintSystem &cs, DomainMap &domains)
1490+
: PrimaryExpr(expr), CS(cs), Domains(domains) {}
14951491

14961492
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
14971493
// A dictionary expression is just a set of tuples; try to solve ones
@@ -1516,7 +1512,7 @@ void ConstraintSystem::shrink(Expr *expr) {
15161512

15171513
// Make each of the dictionary elements an independent dictionary,
15181514
// such makes it easy to type-check everything separately.
1519-
SubExprs.push(Candidate(CS, dict, isPrimaryExpr));
1515+
Candidates.push_back(Candidate(CS, dict, isPrimaryExpr));
15201516
}
15211517

15221518
// Don't try to walk into the dictionary.
@@ -1526,7 +1522,7 @@ void ConstraintSystem::shrink(Expr *expr) {
15261522
// Consider all of the collections to be candidates,
15271523
// FIXME: try to split collections into parts for simplified solving.
15281524
if (isa<CollectionExpr>(expr)) {
1529-
SubExprs.push(Candidate(CS, expr, false));
1525+
Candidates.push_back(Candidate(CS, expr, false));
15301526
return {false, expr};
15311527
}
15321528

@@ -1552,7 +1548,7 @@ void ConstraintSystem::shrink(Expr *expr) {
15521548
auto func = applyExpr->getFn();
15531549
// Let's record this function application for post-processing
15541550
// as well as if it contains overload set, see walkToExprPost.
1555-
ApplyExprs.push({ applyExpr, isa<OverloadSetRefExpr>(func) });
1551+
ApplyExprs.push_back({applyExpr, isa<OverloadSetRefExpr>(func)});
15561552
}
15571553

15581554
return { true, expr };
@@ -1562,9 +1558,9 @@ void ConstraintSystem::shrink(Expr *expr) {
15621558
// If there are sub-expressions to consider and
15631559
// contextual type is involved, let's add top-most expression
15641560
// to the queue just to make sure that we didn't miss any solutions.
1565-
if (expr == PrimaryExpr && !SubExprs.empty()) {
1561+
if (expr == PrimaryExpr && !Candidates.empty()) {
15661562
if (!CS.getContextualType().isNull()) {
1567-
SubExprs.push(Candidate(CS, expr, true));
1563+
Candidates.push_back(Candidate(CS, expr, true));
15681564
return expr;
15691565
}
15701566
}
@@ -1575,17 +1571,17 @@ void ConstraintSystem::shrink(Expr *expr) {
15751571
unsigned numOverloadSets = 0;
15761572
// Let's count how many overload sets do we have.
15771573
while (!ApplyExprs.empty()) {
1578-
auto application = ApplyExprs.top();
1574+
auto &application = ApplyExprs.back();
15791575
auto applyExpr = application.first;
15801576

15811577
// Add overload sets tracked by current expression.
15821578
numOverloadSets += application.second;
1583-
ApplyExprs.pop();
1579+
ApplyExprs.pop_back();
15841580

15851581
// We've found the current expression, so record the number of
15861582
// overloads.
15871583
if (expr == applyExpr) {
1588-
ApplyExprs.push({ applyExpr, numOverloadSets });
1584+
ApplyExprs.push_back({applyExpr, numOverloadSets});
15891585
break;
15901586
}
15911587
}
@@ -1594,22 +1590,19 @@ void ConstraintSystem::shrink(Expr *expr) {
15941590
// there is no point of solving this expression,
15951591
// because we won't be able to reduce its domain.
15961592
if (numOverloadSets > 1)
1597-
SubExprs.push(Candidate(CS, expr, expr == PrimaryExpr));
1593+
Candidates.push_back(Candidate(CS, expr, expr == PrimaryExpr));
15981594

15991595
return expr;
16001596
}
16011597
};
16021598

1603-
std::queue<Candidate> expressions;
1604-
ExprCollector collector(expr, *this, expressions, domains);
1599+
ExprCollector collector(expr, *this, domains);
16051600

16061601
// Collect all of the binary/unary and call sub-expressions
16071602
// so we can start solving them separately.
16081603
expr->walk(collector);
16091604

1610-
while (!expressions.empty()) {
1611-
auto &candidate = expressions.front();
1612-
1605+
for (auto &candidate : collector.Candidates) {
16131606
// If there are no results, let's forget everything we know about the
16141607
// system so far. This actually is ok, because some of the expressions
16151608
// might require manual salvaging.
@@ -1629,8 +1622,6 @@ void ConstraintSystem::shrink(Expr *expr) {
16291622
return childExpr;
16301623
});
16311624
}
1632-
1633-
expressions.pop();
16341625
}
16351626
}
16361627

0 commit comments

Comments
 (0)