Refactor parse nodes to support non-const access. Change-Id: I1dd35ea155a6cd727e45a01453346dbb6a6a6964 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/25420 Commit-Queue: Matt Stark <msta@google.com> Reviewed-by: Takuto Ikuta <tikuta@google.com>
diff --git a/src/gn/build_file_editor.h b/src/gn/build_file_editor.h index a003816..c4a2813 100644 --- a/src/gn/build_file_editor.h +++ b/src/gn/build_file_editor.h
@@ -78,20 +78,17 @@ results->push_back(std::move(*mapped)); } - if (auto* block = node->AsBlock()) { + if (auto* block = node->AsBlockMut()) { for (const auto& stmt : block->statements()) { FindStatementRecursive(stmt.get(), stack, transform, results); } - } else if (auto* condition = node->AsCondition()) { - FindStatementRecursive(const_cast<BlockNode*>(condition->if_true()), stack, - transform, results); + } else if (auto* condition = node->AsConditionMut()) { + FindStatementRecursive(condition->if_true(), stack, transform, results); if (condition->if_false()) { - FindStatementRecursive(const_cast<ParseNode*>(condition->if_false()), - stack, transform, results); + FindStatementRecursive(condition->if_false(), stack, transform, results); } - } else if (auto* func = node->AsFunctionCall(); func && func->block()) { - FindStatementRecursive(const_cast<BlockNode*>(func->block()), stack, - transform, results); + } else if (auto* func = node->AsFunctionCallMut(); func && func->block()) { + FindStatementRecursive(func->block(), stack, transform, results); } stack.pop_back();
diff --git a/src/gn/parse_tree.cc b/src/gn/parse_tree.cc index 8173c90..03fec79 100644 --- a/src/gn/parse_tree.cc +++ b/src/gn/parse_tree.cc
@@ -995,8 +995,8 @@ int start_line = contents_[sr.begin]->GetRange().begin().line_number(); const ParseNode* original_first = contents_[sr.begin].get(); std::sort(contents_.begin() + sr.begin, contents_.begin() + sr.end, - [&comparator](const std::unique_ptr<const ParseNode>& a, - const std::unique_ptr<const ParseNode>& b) { + [&comparator](const std::unique_ptr<ParseNode>& a, + const std::unique_ptr<ParseNode>& b) { return comparator(a.get(), b.get()); }); // If the beginning of the range had before comments, and the first node @@ -1005,9 +1005,7 @@ if (original_first->comments() && contents_[sr.begin].get() != original_first) { for (const auto& hc : original_first->comments()->before()) { - const_cast<ParseNode*>(contents_[sr.begin].get()) - ->comments_mutable() - ->append_before(hc); + contents_[sr.begin]->comments_mutable()->append_before(hc); } const_cast<ParseNode*>(original_first) ->comments_mutable()
diff --git a/src/gn/parse_tree.h b/src/gn/parse_tree.h index 08b5dbc..f30c59e 100644 --- a/src/gn/parse_tree.h +++ b/src/gn/parse_tree.h
@@ -272,9 +272,11 @@ void set_op(const Token& t) { op_ = t; } const ParseNode* left() const { return left_.get(); } + ParseNode* left() { return left_.get(); } void set_left(std::unique_ptr<ParseNode> left) { left_ = std::move(left); } const ParseNode* right() const { return right_.get(); } + ParseNode* right() { return right_.get(); } void set_right(std::unique_ptr<ParseNode> right) { right_ = std::move(right); } @@ -371,16 +373,19 @@ void set_if_token(const Token& token) { if_token_ = token; } const ParseNode* condition() const { return condition_.get(); } + ParseNode* condition() { return condition_.get(); } void set_condition(std::unique_ptr<ParseNode> c) { condition_ = std::move(c); } const BlockNode* if_true() const { return if_true_.get(); } + BlockNode* if_true() { return if_true_.get(); } void set_if_true(std::unique_ptr<BlockNode> t) { if_true_ = std::move(t); } // This is either empty, a block (for the else clause), or another // condition. const ParseNode* if_false() const { return if_false_.get(); } + ParseNode* if_false() { return if_false_.get(); } void set_if_false(std::unique_ptr<ParseNode> f) { if_false_ = std::move(f); } static constexpr const char* kDumpNodeName = "CONDITION"; @@ -418,6 +423,7 @@ void set_function(Token t) { function_ = t; } const ListNode* args() const { return args_.get(); } + ListNode* args() { return args_.get(); } void set_args(std::unique_ptr<ListNode> a); const BlockNode* block() const { return block_.get(); } @@ -492,12 +498,10 @@ void append_item(std::unique_ptr<ParseNode> s) { contents_.push_back(std::move(s)); } - const std::vector<std::unique_ptr<const ParseNode>>& contents() const { + const std::vector<std::unique_ptr<ParseNode>>& contents() const { return contents_; } - std::vector<std::unique_ptr<const ParseNode>>& contents() { - return contents_; - } + std::vector<std::unique_ptr<ParseNode>>& contents() { return contents_; } void ShortenTargets(); void SortAsStringsList(); @@ -523,7 +527,7 @@ Token begin_token_; std::unique_ptr<EndNode> end_; - std::vector<std::unique_ptr<const ParseNode>> contents_; + std::vector<std::unique_ptr<ParseNode>> contents_; ListNode(const ListNode&) = delete; ListNode& operator=(const ListNode&) = delete;
diff --git a/src/gn/parse_tree_unittest.cc b/src/gn/parse_tree_unittest.cc index 49a3fd1..e5f41ff 100644 --- a/src/gn/parse_tree_unittest.cc +++ b/src/gn/parse_tree_unittest.cc
@@ -196,11 +196,11 @@ ASSERT_EQ(7u, contents.size()); auto all_elements_are_literal_nodes = - [](base::span<const std::unique_ptr<const ParseNode>> container) -> bool { - return std::ranges::all_of( - container, [](const std::unique_ptr<const ParseNode>& element) { - return element->AsLiteral(); - }); + [](base::span<const std::unique_ptr<ParseNode>> container) -> bool { + return std::ranges::all_of(container, + [](const std::unique_ptr<ParseNode>& element) { + return element->AsLiteral(); + }); }; auto get_literal_value = [](const ParseNode& node) {