Debian Codegraph @

gcc/rust/backend/rust-compile.cc

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 #include "rust-compile.h"
20 #include "rust-compile-item.h"
21 #include "rust-compile-implitem.h"
22 #include "rust-hir-type-bounds.h"
23 #include "rust-compile-type.h"
24 #include "rust-substitution-mapper.h"
25 #include "rust-type-util.h"
26 #include "rust-session-manager.h"
27
28 namespace Rust {
29 namespace Compile {
30
31 CompileCrate::CompileCrate (HIR::Crate &crate, Context *ctx)
32 : crate (crate), ctx (ctx)
33 {}
34
35 CompileCrate::~CompileCrate () {}
36
37 void
38 CompileCrate::Compile (HIR::Crate &crate, Context *ctx)
39 {
40 CompileCrate c (crate, ctx);
41 c.go ();
42 }
43
44 void
45 CompileCrate::go ()
46 {
47 for (auto &item : crate.get_items ())
48 CompileItem::compile (item.get (), ctx);
49 auto crate_type
50 = Rust::Session::get_instance ().options.target_data.get_crate_type ();
51 if (crate_type == TargetOptions::CrateType::PROC_MACRO)
52 add_proc_macro_symbols ();
53 }
54
55 // Shared methods in compilation
56
57 tree
58 HIRCompileBase::coercion_site (HirId id, tree rvalue, TyTy::BaseType *rval,
59 TyTy::BaseType *lval, location_t lvalue_locus,
60 location_t rvalue_locus)
61 {
62 std::vector<Resolver::Adjustment> *adjustments = nullptr;
63 bool ok = ctx->get_tyctx ()->lookup_autoderef_mappings (id, &adjustments);
64 if (ok)
65 {
66 rvalue = resolve_adjustements (*adjustments, rvalue, rvalue_locus);
67 }
68
69 return coercion_site1 (rvalue, rval, lval, lvalue_locus, rvalue_locus);
70 }
71
72 tree
73 HIRCompileBase::coercion_site1 (tree rvalue, TyTy::BaseType *rval,
74 TyTy::BaseType *lval, location_t lvalue_locus,
75 location_t rvalue_locus)
76 {
77 if (rvalue == error_mark_node)
78 return error_mark_node;
79
80 TyTy::BaseType *actual = rval->destructure ();
81 TyTy::BaseType *expected = lval->destructure ();
82
83 if (expected->get_kind () == TyTy::TypeKind::REF)
84 {
85 // this is a dyn object
86 if (RS_DST_FLAG_P (TREE_TYPE (rvalue)))
87 {
88 return rvalue;
89 }
90
91 // bad coercion... of something to a reference
92 if (actual->get_kind () != TyTy::TypeKind::REF)
93 return error_mark_node;
94
95 const TyTy::ReferenceType *exp
96 = static_cast<const TyTy::ReferenceType *> (expected);
97 const TyTy::ReferenceType *act
98 = static_cast<const TyTy::ReferenceType *> (actual);
99
100 tree deref_rvalue = indirect_expression (rvalue, rvalue_locus);
101 tree coerced
102 = coercion_site1 (deref_rvalue, act->get_base (), exp->get_base (),
103 lvalue_locus, rvalue_locus);
104 if (exp->is_dyn_object () && RS_DST_FLAG_P (TREE_TYPE (coerced)))
105 return coerced;
106
107 return address_expression (coerced, rvalue_locus);
108 }
109 else if (expected->get_kind () == TyTy::TypeKind::POINTER)
110 {
111 // this is a dyn object
112 if (RS_DST_FLAG_P (TREE_TYPE (rvalue)))
113 {
114 return rvalue;
115 }
116
117 // bad coercion... of something to a reference
118 bool valid_coercion = actual->get_kind () == TyTy::TypeKind::REF
119 || actual->get_kind () == TyTy::TypeKind::POINTER;
120 if (!valid_coercion)
121 return error_mark_node;
122
123 const TyTy::PointerType *exp
124 = static_cast<const TyTy::PointerType *> (expected);
125
126 TyTy::BaseType *actual_base = nullptr;
127 if (actual->get_kind () == TyTy::TypeKind::REF)
128 {
129 const TyTy::ReferenceType *act
130 = static_cast<const TyTy::ReferenceType *> (actual);
131
132 actual_base = act->get_base ();
133 }
134 else if (actual->get_kind () == TyTy::TypeKind::POINTER)
135 {
136 const TyTy::PointerType *act
137 = static_cast<const TyTy::PointerType *> (actual);
138
139 actual_base = act->get_base ();
140 }
141 rust_assert (actual_base != nullptr);
142
143 tree deref_rvalue = indirect_expression (rvalue, rvalue_locus);
144 tree coerced
145 = coercion_site1 (deref_rvalue, actual_base, exp->get_base (),
146 lvalue_locus, rvalue_locus);
147
148 if (exp->is_dyn_object () && RS_DST_FLAG_P (TREE_TYPE (coerced)))
149 return coerced;
150
151 return address_expression (coerced, rvalue_locus);
152 }
153 else if (expected->get_kind () == TyTy::TypeKind::ARRAY)
154 {
155 if (actual->get_kind () != TyTy::TypeKind::ARRAY)
156 return error_mark_node;
157
158 tree tree_rval_type = TyTyResolveCompile::compile (ctx, actual);
159 tree tree_lval_type = TyTyResolveCompile::compile (ctx, expected);
160 if (!verify_array_capacities (tree_lval_type, tree_rval_type,
161 lvalue_locus, rvalue_locus))
162 return error_mark_node;
163 }
164 else if (expected->get_kind () == TyTy::TypeKind::SLICE)
165 {
166 // bad coercion
167 bool valid_coercion = actual->get_kind () == TyTy::TypeKind::SLICE
168 || actual->get_kind () == TyTy::TypeKind::ARRAY;
169 if (!valid_coercion)
170 return error_mark_node;
171
172 // nothing to do here
173 if (actual->get_kind () == TyTy::TypeKind::SLICE)
174 return rvalue;
175
176 // return an unsized coercion
177 Resolver::Adjustment unsize_adj (
178 Resolver::Adjustment::AdjustmentType::UNSIZE, actual, expected);
179 return resolve_unsized_adjustment (unsize_adj, rvalue, rvalue_locus);
180 }
181
182 return rvalue;
183 }
184
185 tree
186 HIRCompileBase::coerce_to_dyn_object (tree compiled_ref,
187 const TyTy::BaseType *actual,
188 const TyTy::DynamicObjectType *ty,
189 location_t locus)
190 {
191 // DST's get wrapped in a pseudo reference that doesnt exist...
192 const TyTy::ReferenceType r (ctx->get_mappings ().get_next_hir_id (),
193 TyTy::TyVar (ty->get_ref ()), Mutability::Imm);
194
195 tree dynamic_object = TyTyResolveCompile::compile (ctx, &r);
196 tree dynamic_object_fields = TYPE_FIELDS (dynamic_object);
197 tree vtable_field = DECL_CHAIN (dynamic_object_fields);
198 rust_assert (TREE_CODE (TREE_TYPE (vtable_field)) == ARRAY_TYPE);
199
200 //' this assumes ordering and current the structure is
201 // __trait_object_ptr
202 // [list of function ptrs]
203
204 std::vector<std::pair<Resolver::TraitReference *, HIR::ImplBlock *>>
205 probed_bounds_for_receiver = Resolver::TypeBoundsProbe::Probe (actual);
206
207 tree address_of_compiled_ref = null_pointer_node;
208 if (!actual->is_unit ())
209 address_of_compiled_ref = address_expression (compiled_ref, locus);
210
211 std::vector<tree> vtable_ctor_elems;
212 std::vector<unsigned long> vtable_ctor_idx;
213 unsigned long i = 0;
214 for (auto &bound : ty->get_object_items ())
215 {
216 const Resolver::TraitItemReference *item = bound.first;
217 const TyTy::TypeBoundPredicate *predicate = bound.second;
218
219 auto address = compute_address_for_trait_item (item, predicate,
220 probed_bounds_for_receiver,
221 actual, actual, locus);
222 vtable_ctor_elems.push_back (address);
223 vtable_ctor_idx.push_back (i++);
224 }
225
226 tree vtable_ctor
227 = Backend::array_constructor_expression (TREE_TYPE (vtable_field),
228 vtable_ctor_idx, vtable_ctor_elems,
229 locus);
230
231 std::vector<tree> dyn_ctor = {address_of_compiled_ref, vtable_ctor};
232 return Backend::constructor_expression (dynamic_object, false, dyn_ctor, -1,
233 locus);
234 }
235
236 tree
237 HIRCompileBase::compute_address_for_trait_item (
238 const Resolver::TraitItemReference *ref,
239 const TyTy::TypeBoundPredicate *predicate,
240 std::vector<std::pair<Resolver::TraitReference *, HIR::ImplBlock *>>
241 &receiver_bounds,
242 const TyTy::BaseType *receiver, const TyTy::BaseType *root, location_t locus)
243 {
244 TyTy::TypeBoundPredicateItem predicate_item
245 = predicate->lookup_associated_item (ref->get_identifier ());
246 rust_assert (!predicate_item.is_error ());
247
248 // This is the expected end type
249 TyTy::BaseType *trait_item_type = predicate_item.get_tyty_for_receiver (root);
250 rust_assert (trait_item_type->get_kind () == TyTy::TypeKind::FNDEF);
251 TyTy::FnType *trait_item_fntype
252 = static_cast<TyTy::FnType *> (trait_item_type);
253
254 // Loop through the list of trait references and impls that we satisfy.
255 // We are looking for one that has an implementation for "ref", a trait
256 // item.
257 for (auto &item : receiver_bounds)
258 {
259 HIR::ImplBlock *impl_block = item.second;
260 rust_assert (impl_block != nullptr);
261
262 // Checks for empty impl blocks, triggered by Sized trait.
263 if (!impl_block->has_type ())
264 continue;
265
266 // Lookup type for potentially associated impl.
267 HIR::Type &self_type_path = impl_block->get_type ();
268
269 // Convert HIR::Type to TyTy::BaseType
270 TyTy::BaseType *self = nullptr;
271 bool ok = ctx->get_tyctx ()->lookup_type (
272 self_type_path.get_mappings ().get_hirid (), &self);
273
274 rust_assert (ok);
275
276 // Look through the relevant bounds on our type, and find which one our
277 // impl block satisfies
278 TyTy::TypeBoundPredicate *self_bound = nullptr;
279 for (auto &bound : self->get_specified_bounds ())
280 {
281 const Resolver::TraitReference *bound_ref = bound.get ();
282 const Resolver::TraitReference *specified_ref = predicate->get ();
283 // If this impl is for one of our types or supertypes
284 if (specified_ref->satisfies_bound (*bound_ref))
285 {
286 self_bound = &bound;
287 break;
288 }
289 }
290
291 // This impl block doesn't help us
292 if (self_bound == nullptr)
293 continue;
294
295 // Find the specific function in the impl block that matches "ref".
296 // This is the one we want to compute the address for.
297 HIR::Function *associated_function = nullptr;
298 for (auto &impl_item : impl_block->get_impl_items ())
299 {
300 bool is_function = impl_item->get_impl_item_type ()
301 == HIR::ImplItem::ImplItemType::FUNCTION;
302 if (!is_function)
303 continue;
304
305 HIR::Function *fn = static_cast<HIR::Function *> (impl_item.get ());
306 bool found_associated_item
307 = fn->get_function_name ().as_string ().compare (
308 ref->get_identifier ())
309 == 0;
310 if (found_associated_item)
311 associated_function = fn;
312 }
313
314 // This impl block satisfies the bound, but doesn't contain the relevant
315 // function. This could happen because of supertraits.
316 if (associated_function == nullptr)
317 continue;
318
319 // lookup the associated type for this item
320 TyTy::BaseType *lookup = nullptr;
321 ok = ctx->get_tyctx ()->lookup_type (
322 associated_function->get_mappings ().get_hirid (), &lookup);
323 rust_assert (ok);
324 rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
325 TyTy::FnType *lookup_fntype = static_cast<TyTy::FnType *> (lookup);
326
327 if (lookup_fntype->needs_substitution ())
328 {
329 TyTy::BaseType *infer
330 = Resolver::SubstMapper::InferSubst (lookup_fntype, UNDEF_LOCATION);
331 infer
332 = Resolver::unify_site (infer->get_ref (),
333 TyTy::TyWithLocation (trait_item_fntype),
334 TyTy::TyWithLocation (infer),
335 UNDEF_LOCATION);
336 rust_assert (infer->get_kind () == TyTy::TypeKind::FNDEF);
337 lookup_fntype = static_cast<TyTy::FnType *> (infer);
338 }
339
340 return CompileInherentImplItem::Compile (associated_function, ctx,
341 lookup_fntype, locus);
342 }
343
344 // we can only compile trait-items with a body
345 bool trait_item_has_definition = ref->is_optional ();
346 rust_assert (trait_item_has_definition);
347
348 HIR::TraitItem *trait_item = ref->get_hir_trait_item ();
349 return CompileTraitItem::Compile (trait_item, ctx, trait_item_fntype, true,
350 locus);
351 }
352
353 bool
354 HIRCompileBase::verify_array_capacities (tree ltype, tree rtype,
355 location_t lvalue_locus,
356 location_t rvalue_locus)
357 {
358 rust_assert (ltype != NULL_TREE);
359 rust_assert (rtype != NULL_TREE);
360
361 // lets just return ok as other errors have already occurred
362 if (ltype == error_mark_node || rtype == error_mark_node)
363 return true;
364
365 tree ltype_domain = TYPE_DOMAIN (ltype);
366 if (!ltype_domain)
367 return false;
368
369 if (!TREE_CONSTANT (TYPE_MAX_VALUE (ltype_domain)))
370 return false;
371
372 unsigned HOST_WIDE_INT ltype_length
373 = wi::ext (wi::to_offset (TYPE_MAX_VALUE (ltype_domain))
374 - wi::to_offset (TYPE_MIN_VALUE (ltype_domain)) + 1,
375 TYPE_PRECISION (TREE_TYPE (ltype_domain)),
376 TYPE_SIGN (TREE_TYPE (ltype_domain)))
377 .to_uhwi ();
378
379 tree rtype_domain = TYPE_DOMAIN (rtype);
380 if (!rtype_domain)
381 return false;
382
383 if (!TREE_CONSTANT (TYPE_MAX_VALUE (rtype_domain)))
384 return false;
385
386 unsigned HOST_WIDE_INT rtype_length
387 = wi::ext (wi::to_offset (TYPE_MAX_VALUE (rtype_domain))
388 - wi::to_offset (TYPE_MIN_VALUE (rtype_domain)) + 1,
389 TYPE_PRECISION (TREE_TYPE (rtype_domain)),
390 TYPE_SIGN (TREE_TYPE (rtype_domain)))
391 .to_uhwi ();
392
393 if (ltype_length != rtype_length)
394 {
395 rust_error_at (rvalue_locus, ErrorCode::E0308,
396 "mismatched types, expected an array with a fixed size "
397 "of " HOST_WIDE_INT_PRINT_UNSIGNED
398 " elements, found one with " HOST_WIDE_INT_PRINT_UNSIGNED
399 " elements",
400 ltype_length, rtype_length);
401 return false;
402 }
403
404 return true;
405 }
406
407 } // namespace Compile
408 } // namespace Rust