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