[PR testsuite/116860] Testsuite adjustment for recently added tests
[official-gcc.git] / gcc / rust / ast / rust-ast-fragment.cc
blob076cab3234ee56b690f29e62ebdfb3276275b332
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #include "rust-ast-fragment.h"
21 namespace Rust {
22 namespace AST {
24 Fragment::Fragment (FragmentKind kind, std::vector<SingleASTNode> nodes,
25 std::vector<std::unique_ptr<AST::Token>> tokens)
26 : kind (kind), nodes (std::move (nodes)), tokens (std::move (tokens))
29 Fragment::Fragment (Fragment const &other) : kind (other.get_kind ())
31 *this = other;
34 Fragment &
35 Fragment::operator= (Fragment const &other)
37 kind = other.get_kind ();
39 nodes.clear ();
40 nodes.reserve (other.nodes.size ());
41 for (auto &n : other.nodes)
42 nodes.push_back (n);
44 tokens.clear ();
45 tokens.reserve (other.tokens.size ());
46 for (auto &t : other.tokens)
47 tokens.emplace_back (t->clone_token ());
49 return *this;
52 Fragment
53 Fragment::create_error ()
55 return Fragment (FragmentKind::Error, {}, {});
58 Fragment
59 Fragment::create_empty ()
61 return Fragment (FragmentKind::Complete, {}, {});
64 Fragment::Fragment (std::vector<AST::SingleASTNode> nodes,
65 std::vector<std::unique_ptr<AST::Token>> tokens)
66 : kind (FragmentKind::Complete), nodes (std::move (nodes)),
67 tokens (std::move (tokens))
70 Fragment::Fragment (std::vector<AST::SingleASTNode> nodes,
71 std::unique_ptr<AST::Token> token)
72 : kind (FragmentKind::Complete), nodes (std::move (nodes))
74 tokens.emplace_back (std::move (token));
77 std::vector<SingleASTNode> &
78 Fragment::get_nodes ()
80 return nodes;
83 std::vector<std::unique_ptr<AST::Token>> &
84 Fragment::get_tokens ()
86 return tokens;
89 FragmentKind
90 Fragment::get_kind () const
92 return kind;
95 bool
96 Fragment::is_error () const
98 return get_kind () == FragmentKind::Error;
101 bool
102 Fragment::should_expand () const
104 return !is_error ();
107 bool
108 Fragment::is_expression_fragment () const
110 return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION);
113 bool
114 Fragment::is_type_fragment () const
116 return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE);
119 std::unique_ptr<Expr>
120 Fragment::take_expression_fragment ()
122 assert_single_fragment (SingleASTNode::NodeType::EXPRESSION);
123 return nodes[0].take_expr ();
126 std::unique_ptr<Type>
127 Fragment::take_type_fragment ()
129 assert_single_fragment (SingleASTNode::NodeType::TYPE);
130 return nodes[0].take_type ();
133 void
134 Fragment::accept_vis (ASTVisitor &vis)
136 for (auto &node : nodes)
137 node.accept_vis (vis);
140 bool
141 Fragment::is_single_fragment () const
143 return nodes.size () == 1;
146 bool
147 Fragment::is_single_fragment_of_kind (SingleASTNode::NodeType expected) const
149 return is_single_fragment () && nodes[0].get_kind () == expected;
152 void
153 Fragment::assert_single_fragment (SingleASTNode::NodeType expected) const
155 static const std::map<SingleASTNode::NodeType, const char *> str_map = {
156 {SingleASTNode::NodeType::ASSOC_ITEM, "associated item"},
157 {SingleASTNode::NodeType::ITEM, "item"},
158 {SingleASTNode::NodeType::TYPE, "type"},
159 {SingleASTNode::NodeType::EXPRESSION, "expr"},
160 {SingleASTNode::NodeType::STMT, "stmt"},
161 {SingleASTNode::NodeType::EXTERN, "extern"},
164 auto actual = nodes[0].get_kind ();
165 auto fail = false;
167 if (!is_single_fragment ())
169 rust_error_at (UNDEF_LOCATION, "fragment is not single");
170 fail = true;
173 if (actual != expected)
175 rust_error_at (
176 UNDEF_LOCATION,
177 "invalid fragment operation: expected %qs node, got %qs node",
178 str_map.find (expected)->second,
179 str_map.find (nodes[0].get_kind ())->second);
180 fail = true;
183 rust_assert (!fail);
186 } // namespace AST
187 } // namespace Rust