aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-expr.cc
blob: c7941bc2014fc45b0a8656318f30fcb60b310217 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) 2020-2021 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.h"
#include "rust-compile-item.h"
#include "rust-compile-expr.h"
#include "rust-compile-struct-field-expr.h"
#include "rust-hir-trait-resolve.h"
#include "rust-hir-path-probe.h"
#include "rust-hir-type-bounds.h"
#include "rust-hir-dot-operator.h"

namespace Rust {
namespace Compile {

void
CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr)
{
  auto op = expr.get_expr_type ();
  auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx);
  auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx);

  // this might be an operator overload situation lets check
  TyTy::FnType *fntype;
  bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
    expr.get_mappings ().get_hirid (), &fntype);
  if (!is_op_overload)
    {
      translated = ctx->get_backend ()->arithmetic_or_logical_expression (
	op, lhs, rhs, expr.get_locus ());
      return;
    }

  // lookup the resolved name
  NodeId resolved_node_id = UNKNOWN_NODEID;
  if (!ctx->get_resolver ()->lookup_resolved_name (
	expr.get_mappings ().get_nodeid (), &resolved_node_id))
    {
      rust_error_at (expr.get_locus (), "failed to lookup resolved MethodCall");
      return;
    }

  // reverse lookup
  HirId ref;
  if (!ctx->get_mappings ()->lookup_node_to_hir (
	expr.get_mappings ().get_crate_num (), resolved_node_id, &ref))
    {
      rust_fatal_error (expr.get_locus (), "reverse lookup failure");
      return;
    }

  TyTy::BaseType *receiver = nullptr;
  bool ok
    = ctx->get_tyctx ()->lookup_receiver (expr.get_mappings ().get_hirid (),
					  &receiver);
  rust_assert (ok);

  bool is_dyn_dispatch
    = receiver->get_root ()->get_kind () == TyTy::TypeKind::DYNAMIC;
  bool is_generic_receiver = receiver->get_kind () == TyTy::TypeKind::PARAM;
  if (is_generic_receiver)
    {
      TyTy::ParamType *p = static_cast<TyTy::ParamType *> (receiver);
      receiver = p->resolve ();
    }

  if (is_dyn_dispatch)
    {
      const TyTy::DynamicObjectType *dyn
	= static_cast<const TyTy::DynamicObjectType *> (receiver->get_root ());

      std::vector<HIR::Expr *> arguments;
      arguments.push_back (expr.get_rhs ());

      translated = compile_dyn_dispatch_call (dyn, receiver, fntype, lhs,
					      arguments, expr.get_locus ());
      return;
    }

  // lookup compiled functions since it may have already been compiled
  HIR::PathIdentSegment segment_name ("add");
  Bexpression *fn_expr
    = resolve_method_address (fntype, ref, receiver, segment_name,
			      expr.get_mappings (), expr.get_locus ());

  // lookup the autoderef mappings
  std::vector<Resolver::Adjustment> *adjustments = nullptr;
  ok = ctx->get_tyctx ()->lookup_autoderef_mappings (
    expr.get_mappings ().get_hirid (), &adjustments);
  rust_assert (ok);

  Bexpression *self = lhs;
  for (auto &adjustment : *adjustments)
    {
      switch (adjustment.get_type ())
	{
	case Resolver::Adjustment::AdjustmentType::IMM_REF:
	case Resolver::Adjustment::AdjustmentType::MUT_REF:
	  self = ctx->get_backend ()->address_expression (
	    self, expr.get_lhs ()->get_locus ());
	  break;

	case Resolver::Adjustment::AdjustmentType::DEREF_REF:
	  Btype *expected_type
	    = TyTyResolveCompile::compile (ctx, adjustment.get_expected ());
	  self = ctx->get_backend ()->indirect_expression (
	    expected_type, self, true, /* known_valid*/
	    expr.get_lhs ()->get_locus ());
	  break;
	}
    }

  std::vector<Bexpression *> args;
  args.push_back (self); // adjusted self
  args.push_back (rhs);

  auto fncontext = ctx->peek_fn ();
  translated
    = ctx->get_backend ()->call_expression (fncontext.fndecl, fn_expr, args,
					    nullptr, expr.get_locus ());
}

Bexpression *
CompileExpr::compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn,
					TyTy::BaseType *receiver,
					TyTy::FnType *fntype,
					Bexpression *receiver_ref,
					std::vector<HIR::Expr *> &arguments,
					Location expr_locus)
{
  size_t offs = 0;
  const Resolver::TraitItemReference *ref = nullptr;
  for (auto &bound : dyn->get_object_items ())
    {
      const Resolver::TraitItemReference *item = bound.first;
      auto t = item->get_tyty ();
      rust_assert (t->get_kind () == TyTy::TypeKind::FNDEF);
      auto ft = static_cast<TyTy::FnType *> (t);

      if (ft->get_id () == fntype->get_id ())
	{
	  ref = item;
	  break;
	}
      offs++;
    }

  if (ref == nullptr)
    return ctx->get_backend ()->error_expression ();

  // get any indirection sorted out
  if (receiver->get_kind () == TyTy::TypeKind::REF)
    {
      TyTy::ReferenceType *r = static_cast<TyTy::ReferenceType *> (receiver);
      auto indirect_ty = r->get_base ();
      Btype *indrect_compiled_tyty
	= TyTyResolveCompile::compile (ctx, indirect_ty);

      Bexpression *indirect
	= ctx->get_backend ()->indirect_expression (indrect_compiled_tyty,
						    receiver_ref, true,
						    expr_locus);
      receiver_ref = indirect;
    }

  // access the offs + 1 for the fnptr and offs=0 for the reciever obj
  Bexpression *self_argument
    = ctx->get_backend ()->struct_field_expression (receiver_ref, 0,
						    expr_locus);

  // access the vtable for the fn
  Bexpression *fn_vtable_access
    = ctx->get_backend ()->struct_field_expression (receiver_ref, offs + 1,
						    expr_locus);

  // cast it to the correct fntype
  Btype *expected_fntype = TyTyResolveCompile::compile (ctx, fntype, true);
  Bexpression *fn_convert_expr
    = ctx->get_backend ()->convert_expression (expected_fntype,
					       fn_vtable_access, expr_locus);

  fncontext fnctx = ctx->peek_fn ();
  Bblock *enclosing_scope = ctx->peek_enclosing_scope ();
  bool is_address_taken = false;
  Bstatement *ret_var_stmt = nullptr;
  Bvariable *fn_convert_expr_tmp
    = ctx->get_backend ()->temporary_variable (fnctx.fndecl, enclosing_scope,
					       expected_fntype, fn_convert_expr,
					       is_address_taken, expr_locus,
					       &ret_var_stmt);
  ctx->add_statement (ret_var_stmt);

  std::vector<Bexpression *> args;
  args.push_back (self_argument);
  for (auto &argument : arguments)
    {
      Bexpression *compiled_expr = CompileExpr::Compile (argument, ctx);
      args.push_back (compiled_expr);
    }

  Bexpression *fn_expr
    = ctx->get_backend ()->var_expression (fn_convert_expr_tmp, expr_locus);

  return ctx->get_backend ()->call_expression (fnctx.fndecl, fn_expr, args,
					       nullptr, expr_locus);
}

Bexpression *
CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
				     TyTy::BaseType *receiver,
				     HIR::PathIdentSegment &segment,
				     Analysis::NodeMapping expr_mappings,
				     Location expr_locus)
{
  // lookup compiled functions since it may have already been compiled
  Bfunction *fn = nullptr;
  if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
    {
      return ctx->get_backend ()->function_code_expression (fn, expr_locus);
    }

  // Now we can try and resolve the address since this might be a forward
  // declared function, generic function which has not be compiled yet or
  // its an not yet trait bound function
  HIR::ImplItem *resolved_item
    = ctx->get_mappings ()->lookup_hir_implitem (expr_mappings.get_crate_num (),
						 ref, nullptr);
  if (resolved_item != nullptr)
    {
      if (!fntype->has_subsititions_defined ())
	return CompileInherentImplItem::Compile (receiver, resolved_item, ctx,
						 true);

      return CompileInherentImplItem::Compile (receiver, resolved_item, ctx,
					       true, fntype);
    }

  // it might be resolved to a trait item
  HIR::TraitItem *trait_item = ctx->get_mappings ()->lookup_hir_trait_item (
    expr_mappings.get_crate_num (), ref);
  HIR::Trait *trait = ctx->get_mappings ()->lookup_trait_item_mapping (
    trait_item->get_mappings ().get_hirid ());

  Resolver::TraitReference *trait_ref
    = &Resolver::TraitReference::error_node ();
  bool ok = ctx->get_tyctx ()->lookup_trait_reference (
    trait->get_mappings ().get_defid (), &trait_ref);
  rust_assert (ok);

  // the type resolver can only resolve type bounds to their trait
  // item so its up to us to figure out if this path should resolve
  // to an trait-impl-block-item or if it can be defaulted to the
  // trait-impl-item's definition

  auto root = receiver->get_root ();
  std::vector<Resolver::PathProbeCandidate> candidates
    = Resolver::PathProbeType::Probe (root, segment, true, false, true);

  if (candidates.size () == 0)
    {
      // this means we are defaulting back to the trait_item if
      // possible
      Resolver::TraitItemReference *trait_item_ref = nullptr;
      bool ok = trait_ref->lookup_hir_trait_item (*trait_item, &trait_item_ref);
      rust_assert (ok);				    // found
      rust_assert (trait_item_ref->is_optional ()); // has definition

      // FIXME Optional means it has a definition and an associated
      // block which can be a default implementation, if it does not
      // contain an implementation we should actually return
      // error_mark_node

      return CompileTraitItem::Compile (receiver,
					trait_item_ref->get_hir_trait_item (),
					ctx, fntype, true, expr_locus);
    }
  else
    {
      std::vector<Resolver::Adjustment> adjustments;
      Resolver::PathProbeCandidate *candidate
	= Resolver::MethodResolution::Select (candidates, root, adjustments);

      // FIXME this will be a case to return error_mark_node, there is
      // an error scenario where a Trait Foo has a method Bar, but this
      // receiver does not implement this trait or has an incompatible
      // implementation and we should just return error_mark_node
      rust_assert (candidate != nullptr);
      rust_assert (candidate->is_impl_candidate ());

      HIR::ImplItem *impl_item = candidate->item.impl.impl_item;
      if (!fntype->has_subsititions_defined ())
	return CompileInherentImplItem::Compile (receiver, impl_item, ctx,
						 true);

      return CompileInherentImplItem::Compile (receiver, impl_item, ctx, true,
					       fntype);
    }
}

} // namespace Compile
} // namespace Rust