Commit 6ce99c70 authored by jbroman's avatar jbroman Committed by Commit bot

GN: Replace vector<ParseNode*> with vector<unique_ptr<ParseNode>> in parse_tree.h.

Chromium can now use std::unique_ptr.

BUG=602726

Review URL: https://codereview.chromium.org/1884503003

Cr-Commit-Position: refs/heads/master@{#387958}
parent f354ca2b
......@@ -181,7 +181,7 @@ class Printer {
// bracket.
template <class PARSENODE> // Just for const covariance.
void Sequence(SequenceStyle style,
const std::vector<PARSENODE*>& list,
const std::vector<std::unique_ptr<PARSENODE>>& list,
const ParseNode* end,
bool force_multiline);
......@@ -194,7 +194,7 @@ class Printer {
void InitializeSub(Printer* sub);
template <class PARSENODE>
bool ListWillBeMultiline(const std::vector<PARSENODE*>& list,
bool ListWillBeMultiline(const std::vector<std::unique_ptr<PARSENODE>>& list,
const ParseNode* end);
std::string output_; // Output buffer.
......@@ -385,7 +385,7 @@ void Printer::Block(const ParseNode* root) {
size_t i = 0;
for (const auto& stmt : block->statements()) {
Expr(stmt, kPrecedenceLowest, std::string());
Expr(stmt.get(), kPrecedenceLowest, std::string());
Newline();
if (stmt->comments()) {
// Why are before() not printed here too? before() are handled inside
......@@ -399,8 +399,8 @@ void Printer::Block(const ParseNode* root) {
}
}
if (i < block->statements().size() - 1 &&
(ShouldAddBlankLineInBetween(block->statements()[i],
block->statements()[i + 1]))) {
(ShouldAddBlankLineInBetween(block->statements()[i].get(),
block->statements()[i + 1].get()))) {
Newline();
}
++i;
......@@ -648,7 +648,7 @@ int Printer::Expr(const ParseNode* root,
template <class PARSENODE>
void Printer::Sequence(SequenceStyle style,
const std::vector<PARSENODE*>& list,
const std::vector<std::unique_ptr<PARSENODE>>& list,
const ParseNode* end,
bool force_multiline) {
if (style == kSequenceStyleList)
......@@ -665,7 +665,7 @@ void Printer::Sequence(SequenceStyle style,
// No elements, and not forcing newlines, print nothing.
} else if (list.size() == 1 && !force_multiline) {
Print(" ");
Expr(list[0], kPrecedenceLowest, std::string());
Expr(list[0].get(), kPrecedenceLowest, std::string());
CHECK(!list[0]->comments() || list[0]->comments()->after().empty());
Print(" ");
} else {
......@@ -687,11 +687,11 @@ void Printer::Sequence(SequenceStyle style,
bool body_of_list = i < list.size() - 1 || style == kSequenceStyleList;
bool want_comma =
body_of_list && (style == kSequenceStyleList && !x->AsBlockComment());
Expr(x, kPrecedenceLowest, want_comma ? "," : std::string());
Expr(x.get(), kPrecedenceLowest, want_comma ? "," : std::string());
CHECK(!x->comments() || x->comments()->after().empty());
if (body_of_list) {
if (i < list.size() - 1 &&
ShouldAddBlankLineInBetween(list[i], list[i + 1]))
ShouldAddBlankLineInBetween(list[i].get(), list[i + 1].get()))
Newline();
}
++i;
......@@ -734,7 +734,7 @@ int Printer::FunctionCall(const FunctionCallNode* func_call,
bool have_block = func_call->block() != nullptr;
bool force_multiline = false;
const std::vector<const ParseNode*>& list = func_call->args()->contents();
const auto& list = func_call->args()->contents();
const ParseNode* end = func_call->args()->End();
if (end && end->comments() && !end->comments()->before().empty())
......@@ -767,7 +767,7 @@ int Printer::FunctionCall(const FunctionCallNode* func_call,
IndentState(CurrentColumn(), continuation_requires_indent, false));
int penalty_one_line = 0;
for (size_t i = 0; i < list.size(); ++i) {
penalty_one_line += sub1.Expr(list[i], kPrecedenceLowest,
penalty_one_line += sub1.Expr(list[i].get(), kPrecedenceLowest,
i < list.size() - 1 ? ", " : std::string());
}
sub1.Print(terminator);
......@@ -785,8 +785,9 @@ int Printer::FunctionCall(const FunctionCallNode* func_call,
IndentState(CurrentColumn(), continuation_requires_indent, false));
int penalty_multiline_start_same_line = 0;
for (size_t i = 0; i < list.size(); ++i) {
penalty_multiline_start_same_line += sub2.Expr(
list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string());
penalty_multiline_start_same_line +=
sub2.Expr(list[i].get(), kPrecedenceLowest,
i < list.size() - 1 ? "," : std::string());
if (i < list.size() - 1) {
sub2.Newline();
}
......@@ -807,8 +808,9 @@ int Printer::FunctionCall(const FunctionCallNode* func_call,
std::abs(sub3.CurrentColumn() - start_column) *
kPenaltyHorizontalSeparation;
}
penalty_multiline_start_next_line += sub3.Expr(
list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string());
penalty_multiline_start_next_line +=
sub3.Expr(list[i].get(), kPrecedenceLowest,
i < list.size() - 1 ? "," : std::string());
if (i < list.size() - 1) {
sub3.Newline();
}
......@@ -852,7 +854,7 @@ int Printer::FunctionCall(const FunctionCallNode* func_call,
Newline();
}
bool want_comma = i < list.size() - 1 && !x->AsBlockComment();
Expr(x, kPrecedenceLowest, want_comma ? "," : std::string());
Expr(x.get(), kPrecedenceLowest, want_comma ? "," : std::string());
CHECK(!x->comments() || x->comments()->after().empty());
if (i < list.size() - 1) {
if (!want_comma)
......@@ -900,8 +902,9 @@ void Printer::InitializeSub(Printer* sub) {
}
template <class PARSENODE>
bool Printer::ListWillBeMultiline(const std::vector<PARSENODE*>& list,
const ParseNode* end) {
bool Printer::ListWillBeMultiline(
const std::vector<std::unique_ptr<PARSENODE>>& list,
const ParseNode* end) {
if (list.size() > 1)
return true;
......
......@@ -47,7 +47,7 @@ Value RunForEach(Scope* scope,
const FunctionCallNode* function,
const ListNode* args_list,
Err* err) {
const std::vector<const ParseNode*>& args_vector = args_list->contents();
const auto& args_vector = args_list->contents();
if (args_vector.size() != 2) {
*err = Err(function, "Wrong number of arguments to foreach().",
"Expecting exactly two.");
......@@ -57,7 +57,8 @@ Value RunForEach(Scope* scope,
// Extract the loop variable.
const IdentifierNode* identifier = args_vector[0]->AsIdentifier();
if (!identifier) {
*err = Err(args_vector[0], "Expected an identifier for the loop var.");
*err =
Err(args_vector[0].get(), "Expected an identifier for the loop var.");
return Value();
}
base::StringPiece loop_var(identifier->value().value());
......@@ -69,7 +70,7 @@ Value RunForEach(Scope* scope,
if (list_identifier) {
list_value = scope->GetValue(list_identifier->value().value(), true);
if (!list_value) {
*err = Err(args_vector[1], "Undefined identifier.");
*err = Err(args_vector[1].get(), "Undefined identifier.");
return Value();
}
} else {
......
......@@ -153,7 +153,7 @@ Value RunForwardVariablesFrom(Scope* scope,
const FunctionCallNode* function,
const ListNode* args_list,
Err* err) {
const std::vector<const ParseNode*>& args_vector = args_list->contents();
const auto& args_vector = args_list->contents();
if (args_vector.size() != 2 && args_vector.size() != 3) {
*err = Err(function, "Wrong number of arguments.",
"Expecting two or three arguments.");
......@@ -166,7 +166,7 @@ Value RunForwardVariablesFrom(Scope* scope,
// to execute the ParseNode and get the value out if it's not an identifer.
const IdentifierNode* identifier = args_vector[0]->AsIdentifier();
if (!identifier) {
*err = Err(args_vector[0], "Expected an identifier for the scope.");
*err = Err(args_vector[0].get(), "Expected an identifier for the scope.");
return Value();
}
......
......@@ -464,7 +464,7 @@ Value RunDefined(Scope* scope,
const FunctionCallNode* function,
const ListNode* args_list,
Err* err) {
const std::vector<const ParseNode*>& args_vector = args_list->contents();
const auto& args_vector = args_list->contents();
if (args_vector.size() != 1) {
*err = Err(function, "Wrong number of arguments to defined().",
"Expecting exactly one.");
......
......@@ -297,7 +297,6 @@ BlockNode::BlockNode() {
}
BlockNode::~BlockNode() {
STLDeleteContainerPointers(statements_.begin(), statements_.end());
}
const BlockNode* BlockNode::AsBlock() const {
......@@ -307,7 +306,7 @@ const BlockNode* BlockNode::AsBlock() const {
Value BlockNode::Execute(Scope* scope, Err* err) const {
for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) {
// Check for trying to execute things with no side effects in a block.
const ParseNode* cur = statements_[i];
const ParseNode* cur = statements_[i].get();
if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() ||
cur->AsIdentifier()) {
*err = cur->MakeErrorDescribing(
......@@ -492,7 +491,6 @@ ListNode::ListNode() : prefer_multiline_(false) {
}
ListNode::~ListNode() {
STLDeleteContainerPointers(contents_.begin(), contents_.end());
}
const ListNode* ListNode::AsList() const {
......@@ -547,7 +545,7 @@ void ListNode::SortList(Comparator comparator) {
bool skip = false;
for (size_t i = sr.begin; i != sr.end; ++i) {
// Bails out if any of the nodes are unsupported.
const ParseNode* node = contents_[i];
const ParseNode* node = contents_[i].get();
if (!node->AsLiteral() && !node->AsIdentifier() && !node->AsAccessor()) {
skip = true;
continue;
......@@ -561,15 +559,19 @@ void ListNode::SortList(Comparator comparator) {
// to determine whether two nodes were initially separated by a blank line
// or not.
int start_line = contents_[sr.begin]->GetRange().begin().line_number();
const ParseNode* original_first = contents_[sr.begin];
const ParseNode* original_first = contents_[sr.begin].get();
std::sort(contents_.begin() + sr.begin, contents_.begin() + sr.end,
comparator);
[&comparator](const std::unique_ptr<const ParseNode>& a,
const std::unique_ptr<const ParseNode>& b) {
return comparator(a.get(), b.get());
});
// If the beginning of the range had before comments, and the first node
// moved during the sort, then move its comments to the new head of the
// range.
if (original_first->comments() && contents_[sr.begin] != original_first) {
if (original_first->comments() &&
contents_[sr.begin].get() != original_first) {
for (const auto& hc : original_first->comments()->before()) {
const_cast<ParseNode*>(contents_[sr.begin])
const_cast<ParseNode*>(contents_[sr.begin].get())
->comments_mutable()
->append_before(hc);
}
......@@ -579,7 +581,7 @@ void ListNode::SortList(Comparator comparator) {
}
const ParseNode* prev = nullptr;
for (size_t i = sr.begin; i != sr.end; ++i) {
const ParseNode* node = contents_[i];
const ParseNode* node = contents_[i].get();
DCHECK(node->AsLiteral() || node->AsIdentifier() || node->AsAccessor());
int line_number =
prev ? prev->GetRange().end().line_number() + 1 : start_line;
......@@ -627,8 +629,8 @@ std::vector<ListNode::SortRange> ListNode::GetSortRanges() const {
std::vector<SortRange> ranges;
const ParseNode* prev = nullptr;
size_t begin = 0;
for (size_t i = begin; i < contents_.size(); prev = contents_[i++]) {
if (IsSortRangeSeparator(contents_[i], prev)) {
for (size_t i = begin; i < contents_.size(); prev = contents_[i++].get()) {
if (IsSortRangeSeparator(contents_[i].get(), prev)) {
if (i > begin) {
ranges.push_back(SortRange(begin, i));
// If |i| is an item with an attached comment, then we start the next
......
......@@ -227,9 +227,11 @@ class BlockNode : public ParseNode {
void set_end(std::unique_ptr<EndNode> e) { end_ = std::move(e); }
const EndNode* End() const { return end_.get(); }
const std::vector<ParseNode*>& statements() const { return statements_; }
const std::vector<std::unique_ptr<ParseNode>>& statements() const {
return statements_;
}
void append_statement(std::unique_ptr<ParseNode> s) {
statements_.push_back(s.release());
statements_.push_back(std::move(s));
}
private:
......@@ -238,8 +240,7 @@ class BlockNode : public ParseNode {
Token begin_token_;
std::unique_ptr<EndNode> end_;
// Owning pointers, use unique_ptr when we can use C++11.
std::vector<ParseNode*> statements_;
std::vector<std::unique_ptr<ParseNode>> statements_;
DISALLOW_COPY_AND_ASSIGN(BlockNode);
};
......@@ -364,9 +365,11 @@ class ListNode : public ParseNode {
const EndNode* End() const { return end_.get(); }
void append_item(std::unique_ptr<ParseNode> s) {
contents_.push_back(s.release());
contents_.push_back(std::move(s));
}
const std::vector<std::unique_ptr<const ParseNode>>& contents() const {
return contents_;
}
const std::vector<const ParseNode*>& contents() const { return contents_; }
void SortAsStringsList();
void SortAsDepsList();
......@@ -397,8 +400,7 @@ class ListNode : public ParseNode {
std::unique_ptr<EndNode> end_;
bool prefer_multiline_;
// Owning pointers, use unique_ptr when we can use C++11.
std::vector<const ParseNode*> contents_;
std::vector<std::unique_ptr<const ParseNode>> contents_;
DISALLOW_COPY_AND_ASSIGN(ListNode);
};
......
......@@ -671,7 +671,7 @@ void Parser::TraverseOrder(const ParseNode* root,
TraverseOrder(binop->right(), pre, post);
} else if (const BlockNode* block = root->AsBlock()) {
for (const auto& statement : block->statements())
TraverseOrder(statement, pre, post);
TraverseOrder(statement.get(), pre, post);
TraverseOrder(block->End(), pre, post);
} else if (const ConditionNode* condition = root->AsConditionNode()) {
TraverseOrder(condition->condition(), pre, post);
......@@ -684,7 +684,7 @@ void Parser::TraverseOrder(const ParseNode* root,
// Nothing.
} else if (const ListNode* list = root->AsList()) {
for (const auto& node : list->contents())
TraverseOrder(node, pre, post);
TraverseOrder(node.get(), pre, post);
TraverseOrder(list->End(), pre, post);
} else if (root->AsLiteral()) {
// Nothing.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment