aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/typecheck/rust-tyty-call.cc
blob: a28780bcac37855848c932b4b2525057eb2adbb1 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// 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-tyty-call.h"
#include "rust-hir-type-check-expr.h"
#include "rust-type-util.h"

namespace Rust {
namespace TyTy {

void
emit_unexpected_argument_error (location_t loc,
				unsigned long unexpected_arg_count,
				unsigned long expected_arg_count)
{
  // https://doc.rust-lang.org/error_codes/E0061.html
  // rustc treats 1 as singular and others as plural
  std::string err_msg = "this function takes %lu ";
  if (expected_arg_count == 1)
    {
      err_msg += "argument";
    }
  else
    {
      err_msg += "arguments";
    }

  if (unexpected_arg_count == 1)
    {
      err_msg += " but %lu argument was supplied";
    }
  else
    {
      err_msg += " but %lu arguments were supplied";
    }
  rust_error_at (loc, ErrorCode::E0061, err_msg.c_str (), expected_arg_count,
		 unexpected_arg_count);
}

void
TypeCheckCallExpr::visit (ADTType &type)
{
  rust_assert (!variant.is_error ());
  if (variant.get_variant_type () != TyTy::VariantDef::VariantType::TUPLE)
    {
      rust_error_at (
	call.get_locus (), ErrorCode::E0423,
	"expected function, tuple struct or tuple variant, found struct %<%s%>",
	type.get_name ().c_str ());
      return;
    }

  if (call.num_params () != variant.num_fields ())
    {
      emit_unexpected_argument_error (call.get_locus (),
				      (unsigned long) call.num_params (),
				      (unsigned long) variant.num_fields ());
      return;
    }

  size_t i = 0;
  for (auto &argument : call.get_arguments ())
    {
      StructFieldType *field = variant.get_field_at_index (i);
      BaseType *field_tyty = field->get_field_type ();
      location_t arg_locus = argument->get_locus ();

      BaseType *arg = Resolver::TypeCheckExpr::Resolve (argument.get ());
      if (arg->get_kind () == TyTy::TypeKind::ERROR)
	{
	  rust_error_at (argument->get_locus (),
			 "failed to resolve argument type");
	  return;
	}

      HirId coercion_side_id = argument->get_mappings ().get_hirid ();
      auto res = Resolver::coercion_site (coercion_side_id,
					  TyWithLocation (field_tyty),
					  TyWithLocation (arg, arg_locus),
					  argument->get_locus ());
      if (res->get_kind () == TyTy::TypeKind::ERROR)
	{
	  return;
	}

      i++;
    }

  if (i != call.num_params ())
    {
      emit_unexpected_argument_error (call.get_locus (), (unsigned long) i,
				      (unsigned long) call.num_params ());
      return;
    }

  resolved = type.clone ();
}

void
TypeCheckCallExpr::visit (FnType &type)
{
  if (call.num_params () != type.num_params ())
    {
      if (type.is_variadic ())
	{
	  if (call.num_params () < type.num_params ())
	    {
	      emit_unexpected_argument_error (
		call.get_locus (), (unsigned long) call.num_params (),
		(unsigned long) type.num_params ());
	      return;
	    }
	}
      else
	{
	  emit_unexpected_argument_error (call.get_locus (),
					  (unsigned long) call.num_params (),
					  (unsigned long) type.num_params ());
	  return;
	}
    }

  size_t i = 0;
  for (auto &argument : call.get_arguments ())
    {
      location_t arg_locus = argument->get_locus ();
      auto argument_expr_tyty
	= Resolver::TypeCheckExpr::Resolve (argument.get ());
      if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
	{
	  rust_error_at (
	    argument->get_locus (),
	    "failed to resolve type for argument expr in CallExpr");
	  return;
	}

      // it might be a variadic function
      if (i < type.num_params ())
	{
	  auto fnparam = type.param_at (i);
	  HIR::Pattern *fn_param_pattern = fnparam.first;
	  BaseType *param_ty = fnparam.second;
	  location_t param_locus
	    = fn_param_pattern == nullptr
		? mappings->lookup_location (param_ty->get_ref ())
		: fn_param_pattern->get_locus ();

	  HirId coercion_side_id = argument->get_mappings ().get_hirid ();
	  auto resolved_argument_type
	    = Resolver::coercion_site (coercion_side_id,
				       TyWithLocation (param_ty, param_locus),
				       TyWithLocation (argument_expr_tyty,
						       arg_locus),
				       argument->get_locus ());
	  if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
	    {
	      return;
	    }
	}
      else
	{
	  switch (argument_expr_tyty->get_kind ())
	    {
	    case TyTy::TypeKind::ERROR:
	      return;
	      case TyTy::TypeKind::INT: {
		auto &int_ty
		  = static_cast<TyTy::IntType &> (*argument_expr_tyty);
		if ((int_ty.get_int_kind () == TyTy::IntType::IntKind::I8)
		    || (int_ty.get_int_kind () == TyTy::IntType::IntKind::I16))
		  {
		    rich_location richloc (line_table, arg_locus);
		    richloc.add_fixit_replace (
		      "cast the value to c_int: as c_int");
		    rust_error_at (richloc, ErrorCode::E0617,
				   "expected %<c_int%> variadic argument");
		    return;
		  }
		break;
	      }
	      case TyTy::TypeKind::UINT: {
		auto &uint_ty
		  = static_cast<TyTy::UintType &> (*argument_expr_tyty);
		if ((uint_ty.get_uint_kind () == TyTy::UintType::UintKind::U8)
		    || (uint_ty.get_uint_kind ()
			== TyTy::UintType::UintKind::U16))
		  {
		    rich_location richloc (line_table, arg_locus);
		    richloc.add_fixit_replace (
		      "cast the value to c_uint: as c_uint");
		    rust_error_at (richloc, ErrorCode::E0617,
				   "expected %<c_uint%> variadic argument");
		    return;
		  }
		break;
	      }
	      case TyTy::TypeKind::FLOAT: {
		if (static_cast<TyTy::FloatType &> (*argument_expr_tyty)
		      .get_float_kind ()
		    == TyTy::FloatType::FloatKind::F32)
		  {
		    rich_location richloc (line_table, arg_locus);
		    richloc.add_fixit_replace (
		      "cast the value to c_double: as c_double");
		    rust_error_at (richloc, ErrorCode::E0617,
				   "expected %<c_double%> variadic argument");
		    return;
		  }
		break;
	      }
	      case TyTy::TypeKind::BOOL: {
		rich_location richloc (line_table, arg_locus);
		richloc.add_fixit_replace ("cast the value to c_int: as c_int");
		rust_error_at (arg_locus, ErrorCode::E0617,
			       "expected %<c_int%> variadic argument");
		return;
	      }
	      case TyTy::TypeKind::FNDEF: {
		rust_error_at (
		  arg_locus, ErrorCode::E0617,
		  "unexpected function definition type as variadic "
		  "argument - cast to function pointer");
	      }
	      return;
	    default:
	      break;
	    }
	}

      i++;
    }

  if (i < call.num_params ())
    {
      emit_unexpected_argument_error (call.get_locus (), (unsigned long) i,
				      (unsigned long) call.num_params ());
      return;
    }

  type.monomorphize ();
  resolved = type.get_return_type ()->clone ();
}

void
TypeCheckCallExpr::visit (FnPtr &type)
{
  if (call.num_params () != type.num_params ())
    {
      emit_unexpected_argument_error (call.get_locus (),
				      (unsigned long) call.num_params (),
				      (unsigned long) type.num_params ());
      return;
    }

  size_t i = 0;
  for (auto &argument : call.get_arguments ())
    {
      location_t arg_locus = argument->get_locus ();
      BaseType *fnparam = type.get_param_type_at (i);
      auto argument_expr_tyty
	= Resolver::TypeCheckExpr::Resolve (argument.get ());
      if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
	{
	  rust_error_at (
	    argument->get_locus (),
	    "failed to resolve type for argument expr in CallExpr");
	  return;
	}

      auto resolved_argument_type = Resolver::coercion_site (
	argument->get_mappings ().get_hirid (), TyWithLocation (fnparam),
	TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ());
      if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
	{
	  return;
	}

      i++;
    }

  if (i != call.num_params ())
    {
      emit_unexpected_argument_error (call.get_locus (), (unsigned long) i,
				      (unsigned long) call.num_params ());
      return;
    }

  resolved = type.get_return_type ()->monomorphized_clone ();
}

// method call checker

TypeCheckMethodCallExpr::TypeCheckMethodCallExpr (
  Analysis::NodeMapping call_mappings, std::vector<Argument> &args,
  location_t call_locus, location_t receiver_locus,
  TyTy::BaseType *adjusted_self, Resolver::TypeCheckContext *context)
  : call_mappings (call_mappings), arguments (args), call_locus (call_locus),
    receiver_locus (receiver_locus), adjusted_self (adjusted_self),
    context (context), mappings (Analysis::Mappings::get ())
{}

BaseType *
TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call,
			     TyTy::BaseType *adjusted_self,
			     Resolver::TypeCheckContext *context)
{
  std::vector<Argument> args;
  for (auto &arg : call.get_arguments ())
    {
      BaseType *argument_expr_tyty
	= Resolver::TypeCheckExpr::Resolve (arg.get ());
      if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
	{
	  rust_error_at (arg->get_locus (),
			 "failed to resolve type for argument");
	  return new ErrorType (ref->get_ref ());
	}

      Argument a (arg->get_mappings (), argument_expr_tyty, arg->get_locus ());
      args.push_back (std::move (a));
    }

  TypeCheckMethodCallExpr checker (call.get_mappings (), args,
				   call.get_locus (),
				   call.get_receiver ()->get_locus (),
				   adjusted_self, context);
  return checker.check (*ref);
}

BaseType *
TypeCheckMethodCallExpr::go (FnType *ref, Analysis::NodeMapping call_mappings,
			     std::vector<Argument> &args, location_t call_locus,
			     location_t receiver_locus,
			     TyTy::BaseType *adjusted_self,
			     Resolver::TypeCheckContext *context)
{
  TypeCheckMethodCallExpr checker (call_mappings, args, call_locus,
				   receiver_locus, adjusted_self, context);
  return checker.check (*ref);
}

BaseType *
TypeCheckMethodCallExpr::check (FnType &type)
{
  Resolver::unify_site (call_mappings.get_hirid (),
			TyWithLocation (type.get_self_type ()),
			TyWithLocation (adjusted_self, receiver_locus),
			call_locus);

  // +1 for the receiver self
  size_t num_args_to_call = arguments.size () + 1;
  if (num_args_to_call != type.num_params ())
    {
      emit_unexpected_argument_error (call_locus,
				      (unsigned long) num_args_to_call,
				      (unsigned long) type.num_params ());
      return new ErrorType (type.get_ref ());
    }

  size_t i = 1;
  for (auto &argument : arguments)
    {
      location_t arg_locus = argument.get_locus ();

      auto fnparam = type.param_at (i);
      HIR::Pattern *fn_param_pattern = fnparam.first;
      BaseType *param_ty = fnparam.second;
      location_t param_locus
	= fn_param_pattern == nullptr
	    ? mappings->lookup_location (param_ty->get_ref ())
	    : fn_param_pattern->get_locus ();

      auto argument_expr_tyty = argument.get_argument_type ();
      HirId coercion_side_id = argument.get_mappings ().get_hirid ();
      auto resolved_argument_type = Resolver::coercion_site (
	coercion_side_id, TyWithLocation (param_ty, param_locus),
	TyWithLocation (argument_expr_tyty, arg_locus), arg_locus);
      if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
	{
	  return new ErrorType (type.get_ref ());
	}

      i++;
    }

  if (i != num_args_to_call)
    {
      emit_unexpected_argument_error (call_locus, (unsigned long) i,
				      (unsigned long) arguments.size ());
      return new ErrorType (type.get_ref ());
    }

  type.monomorphize ();

  return type.get_return_type ()->monomorphized_clone ();
}

} // namespace TyTy
} // namespace Rust