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 : : #include "rust-compile-implitem.h"
20 : :
21 : : namespace Rust {
22 : : namespace Compile {
23 : :
24 : : void
25 : 0 : CompileTraitItem::visit (HIR::TraitItemConst &constant)
26 : : {
27 : 0 : rust_assert (concrete != nullptr);
28 : 0 : TyTy::BaseType *resolved_type = concrete;
29 : :
30 : 0 : tl::optional<Resolver::CanonicalPath> canonical_path;
31 : 0 : if (flag_name_resolution_2_0)
32 : : {
33 : 0 : auto &nr_ctx
34 : 0 : = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
35 : :
36 : 0 : canonical_path = nr_ctx.values.to_canonical_path (
37 : 0 : constant.get_mappings ().get_nodeid ());
38 : : }
39 : : else
40 : : {
41 : 0 : canonical_path = ctx->get_mappings ().lookup_canonical_path (
42 : 0 : constant.get_mappings ().get_nodeid ());
43 : : }
44 : :
45 : 0 : rust_assert (canonical_path);
46 : :
47 : 0 : HIR::Expr &const_value_expr = constant.get_expr ();
48 : 0 : TyTy::BaseType *expr_type = nullptr;
49 : 0 : bool ok = ctx->get_tyctx ()->lookup_type (
50 : 0 : const_value_expr.get_mappings ().get_hirid (), &expr_type);
51 : 0 : rust_assert (ok);
52 : :
53 : 0 : tree const_expr
54 : 0 : = compile_constant_item (constant.get_mappings ().get_hirid (), expr_type,
55 : 0 : resolved_type, *canonical_path, const_value_expr,
56 : : constant.get_locus (),
57 : 0 : const_value_expr.get_locus ());
58 : 0 : ctx->push_const (const_expr);
59 : 0 : ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
60 : :
61 : 0 : reference = const_expr;
62 : 0 : }
63 : :
64 : : void
65 : 172 : CompileTraitItem::visit (HIR::TraitItemFunc &func)
66 : : {
67 : 172 : rust_assert (func.has_definition ());
68 : :
69 : 172 : rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
70 : 172 : TyTy::FnType *fntype = static_cast<TyTy::FnType *> (concrete);
71 : 172 : 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 : 172 : tree lookup = NULL_TREE;
76 : 172 : 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 : 0 : if (ctx->function_completed (lookup))
81 : : {
82 : 0 : tree dummy = NULL_TREE;
83 : 0 : if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
84 : : {
85 : 0 : ctx->insert_function_decl (fntype, lookup);
86 : : }
87 : :
88 : 0 : reference = address_expression (lookup, ref_locus);
89 : 0 : return;
90 : : }
91 : : }
92 : :
93 : 172 : if (fntype->has_substitutions_defined ())
94 : : {
95 : : // override the Hir Lookups for the substituions in this context
96 : 172 : fntype->override_context ();
97 : : }
98 : :
99 : 172 : tl::optional<Resolver::CanonicalPath> canonical_path;
100 : 172 : if (flag_name_resolution_2_0)
101 : : {
102 : 22 : auto &nr_ctx
103 : 22 : = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
104 : :
105 : 22 : canonical_path
106 : 44 : = nr_ctx.values.to_canonical_path (func.get_mappings ().get_nodeid ());
107 : : }
108 : : else
109 : : {
110 : 300 : canonical_path = ctx->get_mappings ().lookup_canonical_path (
111 : 150 : func.get_mappings ().get_nodeid ());
112 : : }
113 : :
114 : 172 : rust_assert (canonical_path);
115 : :
116 : : // FIXME: How do we get the proper visibility here?
117 : 172 : auto vis = HIR::Visibility (HIR::Visibility::VisType::PUBLIC);
118 : 172 : HIR::TraitFunctionDecl &function = func.get_decl ();
119 : 172 : tree fndecl
120 : 172 : = 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 : 172 : &func.get_block_expr (), *canonical_path, fntype);
125 : 172 : reference = address_expression (fndecl, ref_locus);
126 : 344 : }
127 : :
128 : : } // namespace Compile
129 : : } // namespace Rust
|