Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
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.
9 : :
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.
14 : :
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/>.
18 : :
19 : : #include "rust-ast-fragment.h"
20 : :
21 : : namespace Rust {
22 : : namespace AST {
23 : :
24 : 2338754 : Fragment::Fragment (FragmentKind kind, std::vector<SingleASTNode> nodes,
25 : 2338754 : std::vector<std::unique_ptr<AST::Token>> tokens)
26 : 2338754 : : kind (kind), nodes (std::move (nodes)), tokens (std::move (tokens))
27 : 2338754 : {}
28 : :
29 : 2333649 : Fragment::Fragment (Fragment const &other) : kind (other.get_kind ())
30 : : {
31 : 2333649 : *this = other;
32 : 2333649 : }
33 : :
34 : : Fragment &
35 : 4670816 : Fragment::operator= (Fragment const &other)
36 : : {
37 : 4670816 : kind = other.get_kind ();
38 : :
39 : 4670816 : nodes.clear ();
40 : 4670816 : nodes.reserve (other.nodes.size ());
41 : 4697023 : for (auto &n : other.nodes)
42 : 26207 : nodes.push_back (n);
43 : :
44 : 4670816 : tokens.clear ();
45 : 4670816 : tokens.reserve (other.tokens.size ());
46 : 5245252 : for (auto &t : other.tokens)
47 : 574436 : tokens.emplace_back (t->clone_token ());
48 : :
49 : 4670816 : return *this;
50 : : }
51 : :
52 : : Fragment
53 : 2338301 : Fragment::create_error ()
54 : : {
55 : 2338301 : return Fragment (FragmentKind::Error, {}, {});
56 : : }
57 : :
58 : : Fragment
59 : 453 : Fragment::create_empty ()
60 : : {
61 : 453 : return Fragment (FragmentKind::Complete, {}, {});
62 : : }
63 : :
64 : 3624 : Fragment::Fragment (std::vector<AST::SingleASTNode> nodes,
65 : 3624 : std::vector<std::unique_ptr<AST::Token>> tokens)
66 : 3624 : : kind (FragmentKind::Complete), nodes (std::move (nodes)),
67 : 3624 : tokens (std::move (tokens))
68 : 3624 : {}
69 : :
70 : 197 : Fragment::Fragment (std::vector<AST::SingleASTNode> nodes,
71 : 197 : std::unique_ptr<AST::Token> token)
72 : 197 : : kind (FragmentKind::Complete), nodes (std::move (nodes))
73 : : {
74 : 197 : tokens.emplace_back (std::move (token));
75 : 197 : }
76 : :
77 : : std::vector<SingleASTNode> &
78 : 912 : Fragment::get_nodes ()
79 : : {
80 : 912 : return nodes;
81 : : }
82 : :
83 : : std::vector<std::unique_ptr<AST::Token>> &
84 : 55 : Fragment::get_tokens ()
85 : : {
86 : 55 : return tokens;
87 : : }
88 : :
89 : : FragmentKind
90 : 9337642 : Fragment::get_kind () const
91 : : {
92 : 9337642 : return kind;
93 : : }
94 : :
95 : : bool
96 : 2333177 : Fragment::is_error () const
97 : : {
98 : 2333177 : return get_kind () == FragmentKind::Error;
99 : : }
100 : :
101 : : bool
102 : 2329242 : Fragment::should_expand () const
103 : : {
104 : 2329242 : return !is_error ();
105 : : }
106 : :
107 : : bool
108 : 2839 : Fragment::is_expression_fragment () const
109 : : {
110 : 2839 : return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION);
111 : : }
112 : :
113 : : bool
114 : 33 : Fragment::is_type_fragment () const
115 : : {
116 : 33 : return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE);
117 : : }
118 : :
119 : : std::unique_ptr<Expr>
120 : 2839 : Fragment::take_expression_fragment ()
121 : : {
122 : 2839 : assert_single_fragment (SingleASTNode::NodeType::EXPRESSION);
123 : 2839 : return nodes[0].take_expr ();
124 : : }
125 : :
126 : : std::unique_ptr<Type>
127 : 33 : Fragment::take_type_fragment ()
128 : : {
129 : 33 : assert_single_fragment (SingleASTNode::NodeType::TYPE);
130 : 33 : return nodes[0].take_type ();
131 : : }
132 : :
133 : : void
134 : 0 : Fragment::accept_vis (ASTVisitor &vis)
135 : : {
136 : 0 : for (auto &node : nodes)
137 : 0 : node.accept_vis (vis);
138 : 0 : }
139 : :
140 : : bool
141 : 5744 : Fragment::is_single_fragment () const
142 : : {
143 : 5744 : return nodes.size () == 1;
144 : : }
145 : :
146 : : bool
147 : 2872 : Fragment::is_single_fragment_of_kind (SingleASTNode::NodeType expected) const
148 : : {
149 : 2872 : return is_single_fragment () && nodes[0].get_kind () == expected;
150 : : }
151 : :
152 : : void
153 : 2872 : Fragment::assert_single_fragment (SingleASTNode::NodeType expected) const
154 : : {
155 : 2872 : 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"},
162 : 2872 : };
163 : :
164 : 2872 : auto actual = nodes[0].get_kind ();
165 : 2872 : auto fail = false;
166 : :
167 : 2872 : if (!is_single_fragment ())
168 : : {
169 : 0 : rust_error_at (UNDEF_LOCATION, "fragment is not single");
170 : 0 : fail = true;
171 : : }
172 : :
173 : 2872 : if (actual != expected)
174 : : {
175 : 0 : rust_error_at (
176 : : UNDEF_LOCATION,
177 : : "invalid fragment operation: expected %qs node, got %qs node",
178 : 0 : str_map.find (expected)->second,
179 : 0 : str_map.find (nodes[0].get_kind ())->second);
180 : 0 : fail = true;
181 : : }
182 : :
183 : 2872 : rust_assert (!fail);
184 : 2872 : }
185 : :
186 : : } // namespace AST
187 : : } // namespace Rust
|