Debian Codegraph @

gcc/rust/backend/rust-compile-implitem.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-implitem.h"
20
21 namespace Rust {
22 namespace Compile {
23
24 void
25 CompileTraitItem::visit (HIR::TraitItemConst &constant)
26 {
27 rust_assert (concrete != nullptr);
28 TyTy::BaseType *resolved_type = concrete;
29
30 tl::optional<Resolver::CanonicalPath> canonical_path;
31 if (flag_name_resolution_2_0)
32 {
33 auto &nr_ctx
34 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
35
36 canonical_path = nr_ctx.values.to_canonical_path (
37 constant.get_mappings ().get_nodeid ());
38 }
39 else
40 {
41 canonical_path = ctx->get_mappings ().lookup_canonical_path (
42 constant.get_mappings ().get_nodeid ());
43 }
44
45 rust_assert (canonical_path);
46
47 HIR::Expr &const_value_expr = constant.get_expr ();
48 TyTy::BaseType *expr_type = nullptr;
49 bool ok = ctx->get_tyctx ()->lookup_type (
50 const_value_expr.get_mappings ().get_hirid (), &expr_type);
51 rust_assert (ok);
52
53 tree const_expr
54 = compile_constant_item (constant.get_mappings ().get_hirid (), expr_type,
55 resolved_type, *canonical_path, const_value_expr,
56 constant.get_locus (),
57 const_value_expr.get_locus ());
58 ctx->push_const (const_expr);
59 ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
60
61 reference = const_expr;
62 }
63
64 void
65 CompileTraitItem::visit (HIR::TraitItemFunc &func)
66 {
67 rust_assert (func.has_definition ());
68
69 rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
70 TyTy::FnType *fntype = static_cast<TyTy::FnType *> (concrete);
71 fntype->monomorphize ();
72
73 // items can be forward compiled which means we may not need to invoke this
74 // code. We might also have already compiled this generic function as well.
75 tree lookup = NULL_TREE;
76 if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
77 fntype->get_id (), fntype))
78 {
79 // has this been added to the list then it must be finished
80 if (ctx->function_completed (lookup))
81 {
82 tree dummy = NULL_TREE;
83 if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
84 {
85 ctx->insert_function_decl (fntype, lookup);
86 }
87
88 reference = address_expression (lookup, ref_locus);
89 return;
90 }
91 }
92
93 if (fntype->has_substitutions_defined ())
94 {
95 // override the Hir Lookups for the substituions in this context
96 fntype->override_context ();
97 }
98
99 tl::optional<Resolver::CanonicalPath> canonical_path;
100 if (flag_name_resolution_2_0)
101 {
102 auto &nr_ctx
103 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
104
105 canonical_path
106 = nr_ctx.values.to_canonical_path (func.get_mappings ().get_nodeid ());
107 }
108 else
109 {
110 canonical_path = ctx->get_mappings ().lookup_canonical_path (
111 func.get_mappings ().get_nodeid ());
112 }
113
114 rust_assert (canonical_path);
115
116 // FIXME: How do we get the proper visibility here?
117 auto vis = HIR::Visibility (HIR::Visibility::VisType::PUBLIC);
118 HIR::TraitFunctionDecl &function = func.get_decl ();
119 tree fndecl
120 = compile_function (false, function.get_function_name ().as_string (),
121 function.get_self (), function.get_function_params (),
122 function.get_qualifiers (), vis,
123 func.get_outer_attrs (), func.get_locus (),
124 &func.get_block_expr (), *canonical_path, fntype);
125 reference = address_expression (fndecl, ref_locus);
126 }
127
128 } // namespace Compile
129 } // namespace Rust