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