Debian Codegraph @

gcc/rust/backend/rust-compile-resolve-path.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-resolve-path.h"
20 #include "options.h"
21 #include "rust-compile-intrinsic.h"
22 #include "rust-compile-item.h"
23 #include "rust-compile-implitem.h"
24 #include "rust-compile-expr.h"
25 #include "rust-hir-map.h"
26 #include "rust-hir-trait-resolve.h"
27 #include "rust-hir-path-probe.h"
28 #include "rust-compile-extern.h"
29 #include "rust-constexpr.h"
30 #include "rust-tyty.h"
31
32 namespace Rust {
33 namespace Compile {
34
35 tree
36 ResolvePathRef::Compile (HIR::QualifiedPathInExpression &expr, Context *ctx)
37 {
38 ResolvePathRef resolver (ctx);
39 return resolver.resolve_path_like (expr);
40 }
41
42 tree
43 ResolvePathRef::Compile (HIR::PathInExpression &expr, Context *ctx)
44 {
45 ResolvePathRef resolver (ctx);
46 return resolver.resolve_path_like (expr);
47 }
48
49 ResolvePathRef::ResolvePathRef (Context *ctx) : HIRCompileBase (ctx) {}
50
51 template <typename T>
52 tree
53 ResolvePathRef::resolve_path_like (T &expr)
54 {
55 if (expr.is_lang_item ())
56 {
57 auto lang_item
58 = Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ());
59
60 // FIXME: Is that correct? :/
61 auto final_segment
62 = HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
63
64 return resolve_with_node_id (final_segment, expr.get_mappings (),
65 expr.get_locus (), true, lang_item);
66 }
67
68 return resolve (expr.get_final_segment ().get_segment (),
69 expr.get_mappings (), expr.get_locus (), true);
70 }
71
72 tree
73 ResolvePathRef::attempt_constructor_expression_lookup (
74 TyTy::BaseType *lookup, Context *ctx, const Analysis::NodeMapping &mappings,
75 location_t expr_locus)
76 {
77 // it might be an enum data-less enum variant
78 if (lookup->get_kind () != TyTy::TypeKind::ADT)
79 return error_mark_node;
80
81 TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
82 if (adt->is_unit ())
83 return unit_expression (expr_locus);
84
85 if (!adt->is_enum ())
86 return error_mark_node;
87
88 HirId variant_id;
89 if (!ctx->get_tyctx ()->lookup_variant_definition (mappings.get_hirid (),
90 &variant_id))
91 return error_mark_node;
92
93 int union_disriminator = -1;
94 TyTy::VariantDef *variant = nullptr;
95 if (!adt->lookup_variant_by_id (variant_id, &variant, &union_disriminator))
96 return error_mark_node;
97
98 // this can only be for discriminant variants the others are built up
99 // using call-expr or struct-init
100 rust_assert (variant->get_variant_type ()
101 == TyTy::VariantDef::VariantType::NUM);
102
103 // we need the actual gcc type
104 tree compiled_adt_type = TyTyResolveCompile::compile (ctx, adt);
105
106 // make the ctor for the union
107 HIR::Expr &discrim_expr = variant->get_discriminant ();
108 ctx->push_const_context ();
109 tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
110 ctx->pop_const_context ();
111 tree folded_discrim_expr = fold_expr (discrim_expr_node);
112 tree qualifier = folded_discrim_expr;
113
114 // false for is enum but this is an enum but we have a new layout
115 return Backend::constructor_expression (compiled_adt_type, false, {qualifier},
116 -1, expr_locus);
117 }
118
119 tree
120 ResolvePathRef::resolve_with_node_id (
121 const HIR::PathIdentSegment &final_segment,
122 const Analysis::NodeMapping &mappings, location_t expr_locus,
123 bool is_qualified_path, NodeId resolved_node_id)
124 {
125 TyTy::BaseType *lookup = nullptr;
126 bool ok = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &lookup);
127 rust_assert (ok);
128
129 tl::optional<HirId> hid
130 = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
131 if (!hid.has_value ())
132 {
133 rust_error_at (expr_locus, "reverse call path lookup failure");
134 return error_mark_node;
135 }
136 auto ref = hid.value ();
137
138 // might be a constant
139 tree constant_expr;
140 if (ctx->lookup_const_decl (ref, &constant_expr))
141 {
142 TREE_USED (constant_expr) = 1;
143 return constant_expr;
144 }
145
146 // maybe closure binding
147 tree closure_binding = error_mark_node;
148 if (ctx->lookup_closure_binding (ref, &closure_binding))
149 {
150 TREE_USED (closure_binding) = 1;
151 return closure_binding;
152 }
153
154 // this might be a variable reference or a function reference
155 Bvariable *var = nullptr;
156 if (ctx->lookup_var_decl (ref, &var))
157 {
158 // TREE_USED is setup in the gcc abstraction here
159 return Backend::var_expression (var, expr_locus);
160 }
161
162 // might be a match pattern binding
163 tree binding = error_mark_node;
164 if (ctx->lookup_pattern_binding (ref, &binding))
165 {
166 TREE_USED (binding) = 1;
167 return binding;
168 }
169
170 // it might be a function call
171 if (lookup->get_kind () == TyTy::TypeKind::FNDEF)
172 {
173 TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup);
174 tree fn = NULL_TREE;
175 if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
176 {
177 TREE_USED (fn) = 1;
178 return address_expression (fn, expr_locus);
179 }
180 else if (fntype->get_abi () == ABI::INTRINSIC)
181 {
182 Intrinsics compile (ctx);
183 fn = compile.compile (fntype);
184 TREE_USED (fn) = 1;
185 return address_expression (fn, expr_locus);
186 }
187 }
188
189 // Handle unit struct
190 if (lookup->get_kind () == TyTy::TypeKind::ADT)
191 return attempt_constructor_expression_lookup (lookup, ctx, mappings,
192 expr_locus);
193
194 // let the query system figure it out
195 tree resolved_item = query_compile (ref, lookup, final_segment, mappings,
196 expr_locus, is_qualified_path);
197 if (resolved_item != error_mark_node)
198 {
199 TREE_USED (resolved_item) = 1;
200 }
201
202 return resolved_item;
203 }
204
205 tree
206 ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
207 const Analysis::NodeMapping &mappings,
208 location_t expr_locus, bool is_qualified_path)
209 {
210 TyTy::BaseType *lookup = nullptr;
211 bool ok = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &lookup);
212 rust_assert (ok);
213
214 // need to look up the reference for this identifier
215
216 // this can fail because it might be a Constructor for something
217 // in that case the caller should attempt ResolvePathType::Compile
218 NodeId ref_node_id = UNKNOWN_NODEID;
219 if (flag_name_resolution_2_0)
220 {
221 auto &nr_ctx
222 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
223
224 auto resolved = nr_ctx.lookup (mappings.get_nodeid ());
225
226 if (!resolved)
227 return attempt_constructor_expression_lookup (lookup, ctx, mappings,
228 expr_locus);
229
230 ref_node_id = *resolved;
231 }
232 else
233 {
234 if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
235 &ref_node_id))
236 return attempt_constructor_expression_lookup (lookup, ctx, mappings,
237 expr_locus);
238 }
239
240 return resolve_with_node_id (final_segment, mappings, expr_locus,
241 is_qualified_path, ref_node_id);
242 }
243
244 tree
245 HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
246 const HIR::PathIdentSegment &final_segment,
247 const Analysis::NodeMapping &mappings,
248 location_t expr_locus, bool is_qualified_path)
249 {
250 bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
251 if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
252 {
253 if (!lookup->has_substitutions_defined ())
254 return CompileItem::compile (*resolved_item, ctx, nullptr, expr_locus);
255 else
256 return CompileItem::compile (*resolved_item, ctx, lookup, expr_locus);
257 }
258 else if (auto hir_extern_item
259 = ctx->get_mappings ().lookup_hir_extern_item (ref))
260 {
261 HIR::ExternalItem *resolved_extern_item = hir_extern_item->first;
262 if (!lookup->has_substitutions_defined ())
263 return CompileExternItem::compile (resolved_extern_item, ctx, nullptr,
264 expr_locus);
265 else
266 return CompileExternItem::compile (resolved_extern_item, ctx, lookup,
267 expr_locus);
268 }
269 else
270 {
271 if (is_fn)
272 {
273 TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup);
274 TyTy::BaseType *receiver = nullptr;
275
276 if (fn->is_method ())
277 {
278 receiver = fn->get_self_type ();
279 receiver = receiver->destructure ();
280
281 return resolve_method_address (fn, receiver, expr_locus);
282 }
283 }
284
285 if (auto resolved_item = ctx->get_mappings ().lookup_hir_implitem (ref))
286 {
287 if (!lookup->has_substitutions_defined ())
288 return CompileInherentImplItem::Compile (resolved_item->first, ctx,
289 nullptr, expr_locus);
290 else
291 return CompileInherentImplItem::Compile (resolved_item->first, ctx,
292 lookup, expr_locus);
293 }
294 else if (auto trait_item
295 = ctx->get_mappings ().lookup_hir_trait_item (ref))
296 {
297 HIR::Trait *trait = ctx->get_mappings ().lookup_trait_item_mapping (
298 trait_item.value ()->get_mappings ().get_hirid ());
299
300 Resolver::TraitReference *trait_ref
301 = &Resolver::TraitReference::error_node ();
302 bool ok = ctx->get_tyctx ()->lookup_trait_reference (
303 trait->get_mappings ().get_defid (), &trait_ref);
304 rust_assert (ok);
305
306 if (trait_item.value ()->get_item_kind ()
307 == HIR::TraitItem::TraitItemKind::CONST)
308 {
309 auto &c
310 = *static_cast<HIR::TraitItemConst *> (trait_item.value ());
311 if (!c.has_expr ())
312 {
313 rich_location r (line_table, expr_locus);
314 r.add_range (trait->get_locus ());
315 r.add_range (c.get_locus ());
316 rust_error_at (r, "no default expression on trait constant");
317 return error_mark_node;
318 }
319
320 return CompileExpr::Compile (c.get_expr (), ctx);
321 }
322
323 if (trait_item.value ()->get_item_kind ()
324 != HIR::TraitItem::TraitItemKind::FUNC)
325 return error_mark_node;
326
327 // the type resolver can only resolve type bounds to their trait
328 // item so its up to us to figure out if this path should resolve
329 // to an trait-impl-block-item or if it can be defaulted to the
330 // trait-impl-item's definition
331 //
332 // because we know this is resolved to a trait item we can actually
333 // just grab the Self type parameter here for the receiver to match
334 // the appropriate impl block
335
336 rust_assert (lookup->is<TyTy::FnType> ());
337 auto fn = lookup->as<TyTy::FnType> ();
338 rust_assert (fn->get_num_type_params () > 0);
339 auto &self = fn->get_substs ().at (0);
340 auto receiver = self.get_param_ty ();
341 auto candidates
342 = Resolver::PathProbeImplTrait::Probe (receiver, final_segment,
343 trait_ref);
344 if (candidates.size () == 0)
345 {
346 // this means we are defaulting back to the trait_item if
347 // possible
348 Resolver::TraitItemReference *trait_item_ref = nullptr;
349 bool ok = trait_ref->lookup_hir_trait_item (*trait_item.value (),
350 &trait_item_ref);
351 rust_assert (ok); // found
352 rust_assert (trait_item_ref->is_optional ()); // has definition
353
354 return CompileTraitItem::Compile (
355 trait_item_ref->get_hir_trait_item (), ctx, lookup, true,
356 expr_locus);
357 }
358 else
359 {
360 rust_assert (candidates.size () == 1);
361
362 auto candidate = *candidates.begin ();
363 rust_assert (candidate.is_impl_candidate ());
364
365 HIR::ImplBlock *impl = candidate.item.impl.parent;
366 HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
367
368 TyTy::BaseType *self = nullptr;
369 bool ok = ctx->get_tyctx ()->lookup_type (
370 impl->get_type ().get_mappings ().get_hirid (), &self);
371 rust_assert (ok);
372
373 if (!lookup->has_substitutions_defined ())
374 return CompileInherentImplItem::Compile (impl_item, ctx,
375 nullptr, expr_locus);
376 else
377 return CompileInherentImplItem::Compile (impl_item, ctx, lookup,
378 expr_locus);
379 }
380 }
381 }
382
383 return error_mark_node;
384 }
385
386 } // namespace Compile
387 } // namespace Rust