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