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