]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | // Copyright (C) 2020-2024 Free Software Foundation, Inc. |
06688fe4 PH |
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-tyty-call.h" | |
20 | #include "rust-hir-type-check-expr.h" | |
385a03ae | 21 | #include "rust-type-util.h" |
06688fe4 PH |
22 | |
23 | namespace Rust { | |
24 | namespace TyTy { | |
25 | ||
8ab098a7 | 26 | void |
9cc353d7 | 27 | emit_unexpected_argument_error (location_t loc, |
8ab098a7 MM |
28 | unsigned long unexpected_arg_count, |
29 | unsigned long expected_arg_count) | |
30 | { | |
31 | // https://doc.rust-lang.org/error_codes/E0061.html | |
32 | // rustc treats 1 as singular and others as plural | |
33 | std::string err_msg = "this function takes %lu "; | |
34 | if (expected_arg_count == 1) | |
35 | { | |
36 | err_msg += "argument"; | |
37 | } | |
38 | else | |
39 | { | |
40 | err_msg += "arguments"; | |
41 | } | |
42 | ||
43 | if (unexpected_arg_count == 1) | |
44 | { | |
45 | err_msg += " but %lu argument was supplied"; | |
46 | } | |
47 | else | |
48 | { | |
49 | err_msg += " but %lu arguments were supplied"; | |
50 | } | |
55f8b63a | 51 | rust_error_at (loc, ErrorCode::E0061, err_msg.c_str (), expected_arg_count, |
8ab098a7 MM |
52 | unexpected_arg_count); |
53 | } | |
54 | ||
06688fe4 PH |
55 | void |
56 | TypeCheckCallExpr::visit (ADTType &type) | |
57 | { | |
58 | rust_assert (!variant.is_error ()); | |
59 | if (variant.get_variant_type () != TyTy::VariantDef::VariantType::TUPLE) | |
60 | { | |
61 | rust_error_at ( | |
55f8b63a | 62 | call.get_locus (), ErrorCode::E0423, |
06688fe4 PH |
63 | "expected function, tuple struct or tuple variant, found struct %<%s%>", |
64 | type.get_name ().c_str ()); | |
65 | return; | |
66 | } | |
67 | ||
68 | if (call.num_params () != variant.num_fields ()) | |
69 | { | |
8ab098a7 MM |
70 | emit_unexpected_argument_error (call.get_locus (), |
71 | (unsigned long) call.num_params (), | |
72 | (unsigned long) variant.num_fields ()); | |
06688fe4 PH |
73 | return; |
74 | } | |
75 | ||
76 | size_t i = 0; | |
77 | for (auto &argument : call.get_arguments ()) | |
78 | { | |
79 | StructFieldType *field = variant.get_field_at_index (i); | |
80 | BaseType *field_tyty = field->get_field_type (); | |
9cc353d7 | 81 | location_t arg_locus = argument->get_locus (); |
06688fe4 PH |
82 | |
83 | BaseType *arg = Resolver::TypeCheckExpr::Resolve (argument.get ()); | |
84 | if (arg->get_kind () == TyTy::TypeKind::ERROR) | |
85 | { | |
86 | rust_error_at (argument->get_locus (), | |
87 | "failed to resolve argument type"); | |
88 | return; | |
89 | } | |
90 | ||
af22b54a | 91 | HirId coercion_side_id = argument->get_mappings ().get_hirid (); |
f8770d18 PEP |
92 | auto res = Resolver::coercion_site (coercion_side_id, |
93 | TyWithLocation (field_tyty), | |
94 | TyWithLocation (arg, arg_locus), | |
95 | argument->get_locus ()); | |
06688fe4 PH |
96 | if (res->get_kind () == TyTy::TypeKind::ERROR) |
97 | { | |
98 | return; | |
99 | } | |
100 | ||
06688fe4 PH |
101 | i++; |
102 | } | |
103 | ||
104 | if (i != call.num_params ()) | |
105 | { | |
8ab098a7 MM |
106 | emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, |
107 | (unsigned long) call.num_params ()); | |
06688fe4 PH |
108 | return; |
109 | } | |
110 | ||
111 | resolved = type.clone (); | |
112 | } | |
113 | ||
114 | void | |
115 | TypeCheckCallExpr::visit (FnType &type) | |
116 | { | |
06688fe4 PH |
117 | if (call.num_params () != type.num_params ()) |
118 | { | |
0a99bfe1 | 119 | if (type.is_variadic ()) |
06688fe4 PH |
120 | { |
121 | if (call.num_params () < type.num_params ()) | |
122 | { | |
8ab098a7 MM |
123 | emit_unexpected_argument_error ( |
124 | call.get_locus (), (unsigned long) call.num_params (), | |
125 | (unsigned long) type.num_params ()); | |
06688fe4 PH |
126 | return; |
127 | } | |
128 | } | |
129 | else | |
130 | { | |
8ab098a7 MM |
131 | emit_unexpected_argument_error (call.get_locus (), |
132 | (unsigned long) call.num_params (), | |
133 | (unsigned long) type.num_params ()); | |
06688fe4 PH |
134 | return; |
135 | } | |
136 | } | |
137 | ||
138 | size_t i = 0; | |
139 | for (auto &argument : call.get_arguments ()) | |
140 | { | |
9cc353d7 | 141 | location_t arg_locus = argument->get_locus (); |
06688fe4 PH |
142 | auto argument_expr_tyty |
143 | = Resolver::TypeCheckExpr::Resolve (argument.get ()); | |
144 | if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) | |
145 | { | |
146 | rust_error_at ( | |
147 | argument->get_locus (), | |
148 | "failed to resolve type for argument expr in CallExpr"); | |
149 | return; | |
150 | } | |
151 | ||
a9d72ef2 | 152 | // it might be a variadic function |
06688fe4 PH |
153 | if (i < type.num_params ()) |
154 | { | |
155 | auto fnparam = type.param_at (i); | |
af22b54a PH |
156 | HIR::Pattern *fn_param_pattern = fnparam.first; |
157 | BaseType *param_ty = fnparam.second; | |
9cc353d7 | 158 | location_t param_locus |
af22b54a PH |
159 | = fn_param_pattern == nullptr |
160 | ? mappings->lookup_location (param_ty->get_ref ()) | |
161 | : fn_param_pattern->get_locus (); | |
162 | ||
163 | HirId coercion_side_id = argument->get_mappings ().get_hirid (); | |
f8770d18 PEP |
164 | auto resolved_argument_type |
165 | = Resolver::coercion_site (coercion_side_id, | |
166 | TyWithLocation (param_ty, param_locus), | |
167 | TyWithLocation (argument_expr_tyty, | |
168 | arg_locus), | |
169 | argument->get_locus ()); | |
06688fe4 PH |
170 | if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |
171 | { | |
06688fe4 PH |
172 | return; |
173 | } | |
174 | } | |
a9d72ef2 OA |
175 | else |
176 | { | |
177 | switch (argument_expr_tyty->get_kind ()) | |
178 | { | |
179 | case TyTy::TypeKind::ERROR: | |
180 | return; | |
181 | case TyTy::TypeKind::INT: { | |
182 | auto &int_ty | |
183 | = static_cast<TyTy::IntType &> (*argument_expr_tyty); | |
184 | if ((int_ty.get_int_kind () == TyTy::IntType::IntKind::I8) | |
185 | || (int_ty.get_int_kind () == TyTy::IntType::IntKind::I16)) | |
186 | { | |
718b65a9 MM |
187 | rich_location richloc (line_table, arg_locus); |
188 | richloc.add_fixit_replace ( | |
189 | "cast the value to c_int: as c_int"); | |
190 | rust_error_at (richloc, ErrorCode::E0617, | |
a9d72ef2 OA |
191 | "expected %<c_int%> variadic argument"); |
192 | return; | |
193 | } | |
194 | break; | |
195 | } | |
196 | case TyTy::TypeKind::UINT: { | |
197 | auto &uint_ty | |
198 | = static_cast<TyTy::UintType &> (*argument_expr_tyty); | |
199 | if ((uint_ty.get_uint_kind () == TyTy::UintType::UintKind::U8) | |
200 | || (uint_ty.get_uint_kind () | |
201 | == TyTy::UintType::UintKind::U16)) | |
202 | { | |
718b65a9 MM |
203 | rich_location richloc (line_table, arg_locus); |
204 | richloc.add_fixit_replace ( | |
205 | "cast the value to c_uint: as c_uint"); | |
206 | rust_error_at (richloc, ErrorCode::E0617, | |
a9d72ef2 OA |
207 | "expected %<c_uint%> variadic argument"); |
208 | return; | |
209 | } | |
210 | break; | |
211 | } | |
212 | case TyTy::TypeKind::FLOAT: { | |
213 | if (static_cast<TyTy::FloatType &> (*argument_expr_tyty) | |
214 | .get_float_kind () | |
215 | == TyTy::FloatType::FloatKind::F32) | |
216 | { | |
718b65a9 MM |
217 | rich_location richloc (line_table, arg_locus); |
218 | richloc.add_fixit_replace ( | |
219 | "cast the value to c_double: as c_double"); | |
220 | rust_error_at (richloc, ErrorCode::E0617, | |
a9d72ef2 OA |
221 | "expected %<c_double%> variadic argument"); |
222 | return; | |
223 | } | |
224 | break; | |
225 | } | |
718b65a9 MM |
226 | case TyTy::TypeKind::BOOL: { |
227 | rich_location richloc (line_table, arg_locus); | |
228 | richloc.add_fixit_replace ("cast the value to c_int: as c_int"); | |
229 | rust_error_at (arg_locus, ErrorCode::E0617, | |
230 | "expected %<c_int%> variadic argument"); | |
231 | return; | |
232 | } | |
233 | case TyTy::TypeKind::FNDEF: { | |
234 | rust_error_at ( | |
235 | arg_locus, ErrorCode::E0617, | |
236 | "unexpected function definition type as variadic " | |
237 | "argument - cast to function pointer"); | |
238 | } | |
a9d72ef2 OA |
239 | return; |
240 | default: | |
241 | break; | |
242 | } | |
243 | } | |
06688fe4 PH |
244 | |
245 | i++; | |
246 | } | |
247 | ||
248 | if (i < call.num_params ()) | |
249 | { | |
8ab098a7 MM |
250 | emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, |
251 | (unsigned long) call.num_params ()); | |
06688fe4 PH |
252 | return; |
253 | } | |
254 | ||
255 | type.monomorphize (); | |
256 | resolved = type.get_return_type ()->clone (); | |
257 | } | |
258 | ||
259 | void | |
260 | TypeCheckCallExpr::visit (FnPtr &type) | |
261 | { | |
262 | if (call.num_params () != type.num_params ()) | |
263 | { | |
8ab098a7 MM |
264 | emit_unexpected_argument_error (call.get_locus (), |
265 | (unsigned long) call.num_params (), | |
266 | (unsigned long) type.num_params ()); | |
06688fe4 PH |
267 | return; |
268 | } | |
269 | ||
270 | size_t i = 0; | |
271 | for (auto &argument : call.get_arguments ()) | |
272 | { | |
9cc353d7 | 273 | location_t arg_locus = argument->get_locus (); |
14025f73 | 274 | BaseType *fnparam = type.get_param_type_at (i); |
06688fe4 PH |
275 | auto argument_expr_tyty |
276 | = Resolver::TypeCheckExpr::Resolve (argument.get ()); | |
277 | if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) | |
278 | { | |
279 | rust_error_at ( | |
280 | argument->get_locus (), | |
281 | "failed to resolve type for argument expr in CallExpr"); | |
282 | return; | |
283 | } | |
284 | ||
f8770d18 | 285 | auto resolved_argument_type = Resolver::coercion_site ( |
af22b54a PH |
286 | argument->get_mappings ().get_hirid (), TyWithLocation (fnparam), |
287 | TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ()); | |
06688fe4 PH |
288 | if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |
289 | { | |
06688fe4 PH |
290 | return; |
291 | } | |
292 | ||
293 | i++; | |
294 | } | |
295 | ||
296 | if (i != call.num_params ()) | |
297 | { | |
8ab098a7 MM |
298 | emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, |
299 | (unsigned long) call.num_params ()); | |
06688fe4 PH |
300 | return; |
301 | } | |
302 | ||
303 | resolved = type.get_return_type ()->monomorphized_clone (); | |
304 | } | |
305 | ||
306 | // method call checker | |
307 | ||
4d021d9e PH |
308 | TypeCheckMethodCallExpr::TypeCheckMethodCallExpr ( |
309 | Analysis::NodeMapping call_mappings, std::vector<Argument> &args, | |
9cc353d7 OA |
310 | location_t call_locus, location_t receiver_locus, |
311 | TyTy::BaseType *adjusted_self, Resolver::TypeCheckContext *context) | |
4d021d9e PH |
312 | : call_mappings (call_mappings), arguments (args), call_locus (call_locus), |
313 | receiver_locus (receiver_locus), adjusted_self (adjusted_self), | |
314 | context (context), mappings (Analysis::Mappings::get ()) | |
315 | {} | |
316 | ||
317 | BaseType * | |
318 | TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, | |
319 | TyTy::BaseType *adjusted_self, | |
320 | Resolver::TypeCheckContext *context) | |
321 | { | |
322 | std::vector<Argument> args; | |
323 | for (auto &arg : call.get_arguments ()) | |
324 | { | |
325 | BaseType *argument_expr_tyty | |
326 | = Resolver::TypeCheckExpr::Resolve (arg.get ()); | |
327 | if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) | |
328 | { | |
329 | rust_error_at (arg->get_locus (), | |
330 | "failed to resolve type for argument"); | |
331 | return new ErrorType (ref->get_ref ()); | |
332 | } | |
333 | ||
334 | Argument a (arg->get_mappings (), argument_expr_tyty, arg->get_locus ()); | |
335 | args.push_back (std::move (a)); | |
336 | } | |
337 | ||
338 | TypeCheckMethodCallExpr checker (call.get_mappings (), args, | |
339 | call.get_locus (), | |
340 | call.get_receiver ()->get_locus (), | |
341 | adjusted_self, context); | |
342 | return checker.check (*ref); | |
343 | } | |
344 | ||
345 | BaseType * | |
346 | TypeCheckMethodCallExpr::go (FnType *ref, Analysis::NodeMapping call_mappings, | |
9cc353d7 OA |
347 | std::vector<Argument> &args, location_t call_locus, |
348 | location_t receiver_locus, | |
4d021d9e PH |
349 | TyTy::BaseType *adjusted_self, |
350 | Resolver::TypeCheckContext *context) | |
351 | { | |
352 | TypeCheckMethodCallExpr checker (call_mappings, args, call_locus, | |
353 | receiver_locus, adjusted_self, context); | |
354 | return checker.check (*ref); | |
355 | } | |
356 | ||
357 | BaseType * | |
358 | TypeCheckMethodCallExpr::check (FnType &type) | |
06688fe4 | 359 | { |
f97c3809 PEP |
360 | Resolver::unify_site (call_mappings.get_hirid (), |
361 | TyWithLocation (type.get_self_type ()), | |
362 | TyWithLocation (adjusted_self, receiver_locus), | |
363 | call_locus); | |
06688fe4 PH |
364 | |
365 | // +1 for the receiver self | |
4d021d9e | 366 | size_t num_args_to_call = arguments.size () + 1; |
06688fe4 PH |
367 | if (num_args_to_call != type.num_params ()) |
368 | { | |
8ab098a7 MM |
369 | emit_unexpected_argument_error (call_locus, |
370 | (unsigned long) num_args_to_call, | |
371 | (unsigned long) type.num_params ()); | |
4d021d9e | 372 | return new ErrorType (type.get_ref ()); |
06688fe4 PH |
373 | } |
374 | ||
375 | size_t i = 1; | |
4d021d9e | 376 | for (auto &argument : arguments) |
06688fe4 | 377 | { |
9cc353d7 | 378 | location_t arg_locus = argument.get_locus (); |
af22b54a | 379 | |
06688fe4 | 380 | auto fnparam = type.param_at (i); |
af22b54a PH |
381 | HIR::Pattern *fn_param_pattern = fnparam.first; |
382 | BaseType *param_ty = fnparam.second; | |
9cc353d7 | 383 | location_t param_locus |
af22b54a PH |
384 | = fn_param_pattern == nullptr |
385 | ? mappings->lookup_location (param_ty->get_ref ()) | |
386 | : fn_param_pattern->get_locus (); | |
387 | ||
4d021d9e PH |
388 | auto argument_expr_tyty = argument.get_argument_type (); |
389 | HirId coercion_side_id = argument.get_mappings ().get_hirid (); | |
f8770d18 | 390 | auto resolved_argument_type = Resolver::coercion_site ( |
af22b54a | 391 | coercion_side_id, TyWithLocation (param_ty, param_locus), |
4d021d9e | 392 | TyWithLocation (argument_expr_tyty, arg_locus), arg_locus); |
06688fe4 PH |
393 | if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |
394 | { | |
4d021d9e | 395 | return new ErrorType (type.get_ref ()); |
06688fe4 PH |
396 | } |
397 | ||
398 | i++; | |
399 | } | |
400 | ||
401 | if (i != num_args_to_call) | |
402 | { | |
8ab098a7 MM |
403 | emit_unexpected_argument_error (call_locus, (unsigned long) i, |
404 | (unsigned long) arguments.size ()); | |
4d021d9e | 405 | return new ErrorType (type.get_ref ()); |
06688fe4 PH |
406 | } |
407 | ||
408 | type.monomorphize (); | |
409 | ||
4d021d9e | 410 | return type.get_return_type ()->monomorphized_clone (); |
06688fe4 PH |
411 | } |
412 | ||
413 | } // namespace TyTy | |
414 | } // namespace Rust |