ast_fragment: Add take_type_fragment() method

Co-authored-by: philberty <philip.herron@embecosm.com>
This commit is contained in:
Arthur Cohen 2022-03-28 10:52:29 +02:00
parent b6bbf1fa72
commit 3413f632ec
1 changed files with 19 additions and 12 deletions

View File

@ -1827,6 +1827,18 @@ private:
std::vector<SingleASTNode> nodes;
bool fragment_is_error;
/**
* We need to make a special case for Expression and Type fragments as only
* one Node will be extracted from the `nodes` vector
*/
bool is_single_fragment () const { return nodes.size () == 1; }
bool is_single_fragment_kind (SingleASTNode::NodeType kind) const
{
return is_single_fragment () && nodes[0].get_kind () == kind;
}
public:
ASTFragment (std::vector<SingleASTNode> nodes, bool fragment_is_error = false)
: nodes (std::move (nodes)), fragment_is_error (fragment_is_error)
@ -1867,23 +1879,18 @@ public:
bool should_expand () const { return !is_error () && !nodes.empty (); }
/**
* We need to make a special case for Expression fragments as only one
* Node will be extracted from the `nodes` vector
*/
bool is_expression_fragment () const
{
return nodes.size () == 1
&& nodes[0].get_kind () == SingleASTNode::NodeType::EXPRESSION;
}
std::unique_ptr<Expr> take_expression_fragment ()
{
rust_assert (is_expression_fragment ());
rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION));
return nodes[0].take_expr ();
}
std::unique_ptr<Type> take_type_fragment ()
{
rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::TYPE));
return nodes[0].take_type ();
}
void accept_vis (ASTVisitor &vis)
{
for (auto &node : nodes)