aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-item.cc
blob: 7f96694b759fbceffd64418730ed00b4a26cbffd (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// 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-item.h"
#include "rust-compile-implitem.h"
#include "rust-compile-extern.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
namespace Compile {

void
CompileItem::visit (HIR::StaticItem &var)
{
  // have we already compiled this?
  Bvariable *static_decl_ref = nullptr;
  if (ctx->lookup_var_decl (var.get_mappings ().get_hirid (), &static_decl_ref))
    {
      reference = Backend::var_expression (static_decl_ref, ref_locus);
      return;
    }

  TyTy::BaseType *resolved_type = nullptr;
  bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
					    &resolved_type);
  rust_assert (ok);

  tree type = TyTyResolveCompile::compile (ctx, resolved_type);

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

  HIR::Expr *const_value_expr = var.get_expr ().get ();
  ctx->push_const_context ();
  tree value = compile_constant_item (resolved_type, canonical_path,
				      const_value_expr, var.get_locus ());
  ctx->pop_const_context ();

  std::string name = canonical_path->get ();
  std::string asm_name = ctx->mangle_item (resolved_type, *canonical_path);

  bool is_external = false;
  bool is_hidden = false;
  bool in_unique_section = true;

  Bvariable *static_global
    = Backend::global_variable (name, asm_name, type, is_external, is_hidden,
				in_unique_section, var.get_locus ());

  tree init = value == error_mark_node ? error_mark_node : DECL_INITIAL (value);
  Backend::global_variable_set_init (static_global, init);

  ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global);
  ctx->push_var (static_global);

  reference = Backend::var_expression (static_global, ref_locus);
}

void
CompileItem::visit (HIR::ConstantItem &constant)
{
  auto &mappings = constant.get_mappings ();

  if (ctx->lookup_const_decl (mappings.get_hirid (), &reference))
    return;

  // resolve the type
  TyTy::BaseType *resolved_type = nullptr;

  bool ok
    = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &resolved_type);
  rust_assert (ok);

  // canonical path
  Resolver::CanonicalPath canonical_path
    = Resolver::CanonicalPath::create_empty ();

  if (flag_name_resolution_2_0)
    {
      auto nr_ctx
	= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

      canonical_path
	= nr_ctx.values.to_canonical_path (mappings.get_nodeid ()).value ();
    }
  else
    {
      const Resolver::CanonicalPath *canonical_path_ptr = nullptr;
      ok = ctx->get_mappings ().lookup_canonical_path (mappings.get_nodeid (),
						       &canonical_path_ptr);
      rust_assert (ok);
      canonical_path = *canonical_path_ptr;
    }

  HIR::Expr *const_value_expr = constant.get_expr ().get ();
  ctx->push_const_context ();
  tree const_expr
    = compile_constant_item (resolved_type, &canonical_path, const_value_expr,
			     constant.get_locus ());
  ctx->pop_const_context ();

  ctx->push_const (const_expr);
  ctx->insert_const_decl (mappings.get_hirid (), const_expr);
  reference = const_expr;
}

void
CompileItem::visit (HIR::Function &function)
{
  TyTy::BaseType *fntype_tyty;
  if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
				       &fntype_tyty))
    {
      rust_fatal_error (function.get_locus (),
			"failed to lookup function type");
      return;
    }

  rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
  TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
  if (fntype->has_substitutions_defined ())
    {
      // we cant do anything for this only when it is used and a concrete type
      // is given
      if (concrete == nullptr)
	return;
      else
	{
	  rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
	  fntype = static_cast<TyTy::FnType *> (concrete);
	  fntype->monomorphize ();
	}
    }
  else
    {
      // if this is part of a trait impl block which is not generic we need to
      // ensure associated types are setup
      HirId parent_impl_block = UNKNOWN_HIRID;
      HirId id = function.get_mappings ().get_hirid ();
      HIR::ImplItem *impl_item
	= ctx->get_mappings ().lookup_hir_implitem (id, &parent_impl_block);
      if (impl_item != nullptr)
	{
	  Resolver::AssociatedImplTrait *impl = nullptr;
	  bool found = ctx->get_tyctx ()->lookup_associated_trait_impl (
	    parent_impl_block, &impl);
	  if (found)
	    impl->setup_raw_associated_types ();
	}
    }

  Resolver::CanonicalPath canonical_path
    = Resolver::CanonicalPath::create_empty ();

  if (flag_name_resolution_2_0)
    {
      auto nr_ctx
	= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

      auto path = nr_ctx.values.to_canonical_path (
	function.get_mappings ().get_nodeid ());

      canonical_path = path.value ();
    }
  else
    {
      const Resolver::CanonicalPath *path = nullptr;
      bool ok = ctx->get_mappings ().lookup_canonical_path (
	function.get_mappings ().get_nodeid (), &path);
      rust_assert (ok);

      canonical_path = *path;
    }

  const std::string asm_name = ctx->mangle_item (fntype, canonical_path);

  // 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, asm_name))
    {
      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 ();
    }

  if (function.get_qualifiers ().is_const ())
    ctx->push_const_context ();

  tree fndecl
    = compile_function (function.get_function_name ().as_string (),
			function.get_self_param (),
			function.get_function_params (),
			function.get_qualifiers (), function.get_visibility (),
			function.get_outer_attrs (), function.get_locus (),
			function.get_definition ().get (), &canonical_path,
			fntype);
  reference = address_expression (fndecl, ref_locus);

  if (function.get_qualifiers ().is_const ())
    ctx->pop_const_context ();
}

void
CompileItem::visit (HIR::ImplBlock &impl_block)
{
  TyTy::BaseType *self_lookup = nullptr;
  if (!ctx->get_tyctx ()->lookup_type (
	impl_block.get_type ()->get_mappings ().get_hirid (), &self_lookup))
    {
      rust_error_at (impl_block.get_locus (), "failed to resolve type of impl");
      return;
    }

  for (auto &impl_item : impl_block.get_impl_items ())
    CompileInherentImplItem::Compile (impl_item.get (), ctx);
}

void
CompileItem::visit (HIR::ExternBlock &extern_block)
{
  for (auto &item : extern_block.get_extern_items ())
    {
      CompileExternItem::compile (item.get (), ctx, concrete);
    }
}

void
CompileItem::visit (HIR::Module &module)
{
  for (auto &item : module.get_items ())
    CompileItem::compile (item.get (), ctx);
}

} // namespace Compile
} // namespace Rust