Debian Codegraph @

gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h

raw
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 #ifndef RUST_BIR_BUILDER_STRUCT_H
20 #define RUST_BIR_BUILDER_STRUCT_H
21
22 #include "rust-bir-builder-internal.h"
23 #include "rust-bir-builder-expr-stmt.h"
24
25 namespace Rust {
26 namespace BIR {
27
28 // Separated because it needs the struct type as a context.
29 class StructBuilder : public AbstractBuilder, public HIR::HIRFullVisitor
30 {
31 TyTy::VariantDef *struct_ty;
32 /** Output of the builder. */
33 std::vector<PlaceId> init_values;
34
35 public:
36 StructBuilder (BuilderContext &ctx, TyTy::VariantDef *struct_ty)
37 : AbstractBuilder (ctx), struct_ty (struct_ty)
38 {
39 init_values.reserve (struct_ty->get_fields ().size ());
40 }
41
42 std::vector<PlaceId> &&build (HIR::StructExprStructFields &struct_expr)
43 {
44 for (auto &field : struct_expr.get_fields ())
45 field->accept_vis (*this);
46 return std::move (init_values);
47 }
48
49 void visit (HIR::StructExprFieldIdentifier &field) override
50 {
51 handle_named_field (field, resolve_variable (field));
52 }
53 void visit (HIR::StructExprFieldIdentifierValue &field) override
54 {
55 auto value = ExprStmtBuilder (ctx).build (field.get_value ());
56 handle_named_field (field, value);
57 }
58 void visit (HIR::StructExprFieldIndexValue &field) override
59 {
60 auto value = ExprStmtBuilder (ctx).build (field.get_value ());
61 coercion_site (value,
62 struct_ty->get_field_at_index (field.get_tuple_index ())
63 ->get_field_type ());
64 init_values.push_back (value);
65 }
66
67 private:
68 template <typename T> void handle_named_field (T &field, PlaceId value)
69 {
70 size_t field_index;
71 TyTy::StructFieldType *field_type;
72 bool ok = struct_ty->lookup_field (field.get_field_name ().as_string (),
73 &field_type, &field_index);
74 rust_assert (ok);
75 rust_assert (value != INVALID_PLACE);
76 coercion_site (value, field_type->get_field_type ());
77 init_values.push_back (value);
78 }
79
80 protected:
81 void visit (HIR::Lifetime &lifetime) override { rust_unreachable (); }
82 void visit (HIR::LifetimeParam &lifetime_param) override
83 {
84 rust_unreachable ();
85 }
86 void visit (HIR::PathInExpression &path) override { rust_unreachable (); }
87 void visit (HIR::TypePathSegment &segment) override { rust_unreachable (); }
88 void visit (HIR::TypePathSegmentGeneric &segment) override
89 {
90 rust_unreachable ();
91 }
92 void visit (HIR::TypePathSegmentFunction &segment) override
93 {
94 rust_unreachable ();
95 }
96 void visit (HIR::TypePath &path) override { rust_unreachable (); }
97 void visit (HIR::QualifiedPathInExpression &path) override
98 {
99 rust_unreachable ();
100 }
101 void visit (HIR::QualifiedPathInType &path) override { rust_unreachable (); }
102 void visit (HIR::LiteralExpr &expr) override { rust_unreachable (); }
103 void visit (HIR::BorrowExpr &expr) override { rust_unreachable (); }
104 void visit (HIR::DereferenceExpr &expr) override { rust_unreachable (); }
105 void visit (HIR::ErrorPropagationExpr &expr) override { rust_unreachable (); }
106 void visit (HIR::NegationExpr &expr) override { rust_unreachable (); }
107 void visit (HIR::ArithmeticOrLogicalExpr &expr) override
108 {
109 rust_unreachable ();
110 }
111 void visit (HIR::ComparisonExpr &expr) override { rust_unreachable (); }
112 void visit (HIR::LazyBooleanExpr &expr) override { rust_unreachable (); }
113 void visit (HIR::TypeCastExpr &expr) override { rust_unreachable (); }
114 void visit (HIR::AssignmentExpr &expr) override { rust_unreachable (); }
115 void visit (HIR::CompoundAssignmentExpr &expr) override
116 {
117 rust_unreachable ();
118 }
119 void visit (HIR::GroupedExpr &expr) override { rust_unreachable (); }
120 void visit (HIR::ArrayElemsValues &elems) override { rust_unreachable (); }
121 void visit (HIR::ArrayElemsCopied &elems) override { rust_unreachable (); }
122 void visit (HIR::ArrayExpr &expr) override { rust_unreachable (); }
123 void visit (HIR::ArrayIndexExpr &expr) override { rust_unreachable (); }
124 void visit (HIR::TupleExpr &expr) override { rust_unreachable (); }
125 void visit (HIR::TupleIndexExpr &expr) override { rust_unreachable (); }
126 void visit (HIR::StructExprStruct &expr) override { rust_unreachable (); }
127 void visit (HIR::StructExprStructFields &expr) override
128 {
129 rust_unreachable ();
130 }
131 void visit (HIR::StructExprStructBase &expr) override { rust_unreachable (); }
132 void visit (HIR::CallExpr &expr) override { rust_unreachable (); }
133 void visit (HIR::MethodCallExpr &expr) override { rust_unreachable (); }
134 void visit (HIR::FieldAccessExpr &expr) override { rust_unreachable (); }
135 void visit (HIR::BlockExpr &expr) override { rust_unreachable (); }
136 void visit (HIR::ClosureExpr &expr) override { rust_unreachable (); }
137 void visit (HIR::ContinueExpr &expr) override { rust_unreachable (); }
138 void visit (HIR::BreakExpr &expr) override { rust_unreachable (); }
139 void visit (HIR::RangeFromToExpr &expr) override { rust_unreachable (); }
140 void visit (HIR::RangeFromExpr &expr) override { rust_unreachable (); }
141 void visit (HIR::RangeToExpr &expr) override { rust_unreachable (); }
142 void visit (HIR::RangeFullExpr &expr) override { rust_unreachable (); }
143 void visit (HIR::RangeFromToInclExpr &expr) override { rust_unreachable (); }
144 void visit (HIR::RangeToInclExpr &expr) override { rust_unreachable (); }
145 void visit (HIR::ReturnExpr &expr) override { rust_unreachable (); }
146 void visit (HIR::UnsafeBlockExpr &expr) override { rust_unreachable (); }
147 void visit (HIR::LoopExpr &expr) override { rust_unreachable (); }
148 void visit (HIR::WhileLoopExpr &expr) override { rust_unreachable (); }
149 void visit (HIR::WhileLetLoopExpr &expr) override { rust_unreachable (); }
150 void visit (HIR::IfExpr &expr) override { rust_unreachable (); }
151 void visit (HIR::IfExprConseqElse &expr) override { rust_unreachable (); }
152 void visit (HIR::MatchExpr &expr) override { rust_unreachable (); }
153 void visit (HIR::AwaitExpr &expr) override { rust_unreachable (); }
154 void visit (HIR::AsyncBlockExpr &expr) override { rust_unreachable (); }
155 void visit (HIR::InlineAsm &expr) override { rust_unreachable (); }
156 void visit (HIR::TypeParam &param) override { rust_unreachable (); }
157 void visit (HIR::ConstGenericParam &param) override { rust_unreachable (); }
158 void visit (HIR::LifetimeWhereClauseItem &item) override
159 {
160 rust_unreachable ();
161 }
162 void visit (HIR::TypeBoundWhereClauseItem &item) override
163 {
164 rust_unreachable ();
165 }
166 void visit (HIR::Module &module) override { rust_unreachable (); }
167 void visit (HIR::ExternCrate &crate) override { rust_unreachable (); }
168 void visit (HIR::UseTreeGlob &use_tree) override { rust_unreachable (); }
169 void visit (HIR::UseTreeList &use_tree) override { rust_unreachable (); }
170 void visit (HIR::UseTreeRebind &use_tree) override { rust_unreachable (); }
171 void visit (HIR::UseDeclaration &use_decl) override { rust_unreachable (); }
172 void visit (HIR::Function &function) override { rust_unreachable (); }
173 void visit (HIR::TypeAlias &type_alias) override { rust_unreachable (); }
174 void visit (HIR::StructStruct &struct_item) override { rust_unreachable (); }
175 void visit (HIR::TupleStruct &tuple_struct) override { rust_unreachable (); }
176 void visit (HIR::EnumItem &item) override { rust_unreachable (); }
177 void visit (HIR::EnumItemTuple &item) override { rust_unreachable (); }
178 void visit (HIR::EnumItemStruct &item) override { rust_unreachable (); }
179 void visit (HIR::EnumItemDiscriminant &item) override { rust_unreachable (); }
180 void visit (HIR::Enum &enum_item) override { rust_unreachable (); }
181 void visit (HIR::Union &union_item) override { rust_unreachable (); }
182 void visit (HIR::ConstantItem &const_item) override { rust_unreachable (); }
183 void visit (HIR::StaticItem &static_item) override { rust_unreachable (); }
184 void visit (HIR::TraitItemFunc &item) override { rust_unreachable (); }
185 void visit (HIR::TraitItemConst &item) override { rust_unreachable (); }
186 void visit (HIR::TraitItemType &item) override { rust_unreachable (); }
187 void visit (HIR::Trait &trait) override { rust_unreachable (); }
188 void visit (HIR::ImplBlock &impl) override { rust_unreachable (); }
189 void visit (HIR::ExternalStaticItem &item) override { rust_unreachable (); }
190 void visit (HIR::ExternalFunctionItem &item) override { rust_unreachable (); }
191 void visit (HIR::ExternalTypeItem &item) override { rust_unreachable (); }
192 void visit (HIR::ExternBlock &block) override { rust_unreachable (); }
193 void visit (HIR::LiteralPattern &pattern) override { rust_unreachable (); }
194 void visit (HIR::IdentifierPattern &pattern) override { rust_unreachable (); }
195 void visit (HIR::WildcardPattern &pattern) override { rust_unreachable (); }
196 void visit (HIR::RangePatternBoundLiteral &bound) override
197 {
198 rust_unreachable ();
199 }
200 void visit (HIR::RangePatternBoundPath &bound) override
201 {
202 rust_unreachable ();
203 }
204 void visit (HIR::RangePatternBoundQualPath &bound) override
205 {
206 rust_unreachable ();
207 }
208 void visit (HIR::RangePattern &pattern) override { rust_unreachable (); }
209 void visit (HIR::ReferencePattern &pattern) override { rust_unreachable (); }
210 void visit (HIR::StructPatternFieldTuplePat &field) override
211 {
212 rust_unreachable ();
213 }
214 void visit (HIR::StructPatternFieldIdentPat &field) override
215 {
216 rust_unreachable ();
217 }
218 void visit (HIR::StructPatternFieldIdent &field) override
219 {
220 rust_unreachable ();
221 }
222 void visit (HIR::StructPattern &pattern) override { rust_unreachable (); }
223 void visit (HIR::TupleStructItemsNoRange &tuple_items) override
224 {
225 rust_unreachable ();
226 }
227 void visit (HIR::TupleStructItemsRange &tuple_items) override
228 {
229 rust_unreachable ();
230 }
231 void visit (HIR::TupleStructPattern &pattern) override
232 {
233 rust_unreachable ();
234 }
235 void visit (HIR::TuplePatternItemsMultiple &tuple_items) override
236 {
237 rust_unreachable ();
238 }
239 void visit (HIR::TuplePatternItemsRanged &tuple_items) override
240 {
241 rust_unreachable ();
242 }
243 void visit (HIR::TuplePattern &pattern) override { rust_unreachable (); }
244 void visit (HIR::SlicePattern &pattern) override { rust_unreachable (); }
245 void visit (HIR::AltPattern &pattern) override { rust_unreachable (); }
246 void visit (HIR::EmptyStmt &stmt) override { rust_unreachable (); }
247 void visit (HIR::LetStmt &stmt) override { rust_unreachable (); }
248 void visit (HIR::ExprStmt &stmt) override { rust_unreachable (); }
249 void visit (HIR::TraitBound &bound) override { rust_unreachable (); }
250 void visit (HIR::ImplTraitType &type) override { rust_unreachable (); }
251 void visit (HIR::TraitObjectType &type) override { rust_unreachable (); }
252 void visit (HIR::ParenthesisedType &type) override { rust_unreachable (); }
253 void visit (HIR::TupleType &type) override { rust_unreachable (); }
254 void visit (HIR::NeverType &type) override { rust_unreachable (); }
255 void visit (HIR::RawPointerType &type) override { rust_unreachable (); }
256 void visit (HIR::ReferenceType &type) override { rust_unreachable (); }
257 void visit (HIR::ArrayType &type) override { rust_unreachable (); }
258 void visit (HIR::SliceType &type) override { rust_unreachable (); }
259 void visit (HIR::InferredType &type) override { rust_unreachable (); }
260 void visit (HIR::BareFunctionType &type) override { rust_unreachable (); }
261 };
262
263 } // namespace BIR
264 } // namespace Rust
265
266 #endif // RUST_BIR_BUILDER_STRUCT_H