aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-implitem.cc
blob: e9351d820b66f33200a831d5746c9c5cf6ac9dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2020-2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include "rust-compile-implitem.h"

namespace Rust {
namespace Compile {

void
CompileTraitItem::visit (HIR::TraitItemConst &constant)
{
  rust_assert (concrete != nullptr);
  TyTy::BaseType *resolved_type = concrete;

  const Resolver::CanonicalPath *canonical_path = nullptr;
  bool ok = ctx->get_mappings ().lookup_canonical_path (
    constant.get_mappings ().get_nodeid (), &canonical_path);
  rust_assert (ok);

  HIR::Expr *const_value_expr = constant.get_expr ().get ();
  tree const_expr
    = compile_constant_item (resolved_type, canonical_path, const_value_expr,
			     constant.get_locus ());
  ctx->push_const (const_expr);
  ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);

  reference = const_expr;
}

void
CompileTraitItem::visit (HIR::TraitItemFunc &func)
{
  rust_assert (func.has_block_defined ());

  rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
  TyTy::FnType *fntype = static_cast<TyTy::FnType *> (concrete);
  fntype->monomorphize ();

  // items can be forward compiled which means we may not need to invoke this
  // code. We might also have already compiled this generic function as well.
  tree lookup = NULL_TREE;
  if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
				 fntype->get_id (), fntype))
    {
      // has this been added to the list then it must be finished
      if (ctx->function_completed (lookup))
	{
	  tree dummy = NULL_TREE;
	  if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
	    {
	      ctx->insert_function_decl (fntype, lookup);
	    }

	  reference = address_expression (lookup, ref_locus);
	  return;
	}
    }

  if (fntype->has_substitutions_defined ())
    {
      // override the Hir Lookups for the substituions in this context
      fntype->override_context ();
    }

  const Resolver::CanonicalPath *canonical_path = nullptr;
  bool ok = ctx->get_mappings ().lookup_canonical_path (
    func.get_mappings ().get_nodeid (), &canonical_path);
  rust_assert (ok);

  // FIXME: How do we get the proper visibility here?
  auto vis = HIR::Visibility (HIR::Visibility::VisType::PUBLIC);
  HIR::TraitFunctionDecl &function = func.get_decl ();
  tree fndecl
    = compile_function (function.get_function_name ().as_string (),
			function.get_self (), function.get_function_params (),
			function.get_qualifiers (), vis,
			func.get_outer_attrs (), func.get_locus (),
			func.get_block_expr ().get (), canonical_path, fntype);
  reference = address_expression (fndecl, ref_locus);
}

} // namespace Compile
} // namespace Rust