]>
Commit | Line | Data |
---|---|---|
6441eb6d | 1 | // Copyright (C) 2020-2025 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, |
9f06b910 | 63 | "expected function, tuple struct or tuple variant, found struct %qs", |
06688fe4 PH |
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 | 82 | |
c29a3bc9 | 83 | BaseType *arg = Resolver::TypeCheckExpr::Resolve (*argument); |
06688fe4 PH |
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 (); |
c29a3bc9 | 142 | auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument); |
a362f8a7 PH |
143 | if (argument_expr_tyty->is<TyTy::ErrorType> ()) |
144 | return; | |
06688fe4 | 145 | |
a9d72ef2 | 146 | // it might be a variadic function |
06688fe4 PH |
147 | if (i < type.num_params ()) |
148 | { | |
c29a3bc9 | 149 | auto &fnparam = type.param_at (i); |
d123f43e | 150 | BaseType *param_ty = fnparam.get_type (); |
9cc353d7 | 151 | location_t param_locus |
d123f43e | 152 | = fnparam.has_pattern () |
b3246d3f PEP |
153 | ? fnparam.get_pattern ().get_locus () |
154 | : mappings.lookup_location (param_ty->get_ref ()); | |
af22b54a PH |
155 | |
156 | HirId coercion_side_id = argument->get_mappings ().get_hirid (); | |
f8770d18 PEP |
157 | auto resolved_argument_type |
158 | = Resolver::coercion_site (coercion_side_id, | |
159 | TyWithLocation (param_ty, param_locus), | |
160 | TyWithLocation (argument_expr_tyty, | |
161 | arg_locus), | |
162 | argument->get_locus ()); | |
06688fe4 PH |
163 | if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |
164 | { | |
06688fe4 PH |
165 | return; |
166 | } | |
167 | } | |
a9d72ef2 OA |
168 | else |
169 | { | |
170 | switch (argument_expr_tyty->get_kind ()) | |
171 | { | |
172 | case TyTy::TypeKind::ERROR: | |
173 | return; | |
174 | case TyTy::TypeKind::INT: { | |
175 | auto &int_ty | |
176 | = static_cast<TyTy::IntType &> (*argument_expr_tyty); | |
177 | if ((int_ty.get_int_kind () == TyTy::IntType::IntKind::I8) | |
178 | || (int_ty.get_int_kind () == TyTy::IntType::IntKind::I16)) | |
179 | { | |
718b65a9 MM |
180 | rich_location richloc (line_table, arg_locus); |
181 | richloc.add_fixit_replace ( | |
182 | "cast the value to c_int: as c_int"); | |
183 | rust_error_at (richloc, ErrorCode::E0617, | |
a9d72ef2 OA |
184 | "expected %<c_int%> variadic argument"); |
185 | return; | |
186 | } | |
187 | break; | |
188 | } | |
189 | case TyTy::TypeKind::UINT: { | |
190 | auto &uint_ty | |
191 | = static_cast<TyTy::UintType &> (*argument_expr_tyty); | |
192 | if ((uint_ty.get_uint_kind () == TyTy::UintType::UintKind::U8) | |
193 | || (uint_ty.get_uint_kind () | |
194 | == TyTy::UintType::UintKind::U16)) | |
195 | { | |
718b65a9 MM |
196 | rich_location richloc (line_table, arg_locus); |
197 | richloc.add_fixit_replace ( | |
198 | "cast the value to c_uint: as c_uint"); | |
199 | rust_error_at (richloc, ErrorCode::E0617, | |
a9d72ef2 OA |
200 | "expected %<c_uint%> variadic argument"); |
201 | return; | |
202 | } | |
203 | break; | |
204 | } | |
205 | case TyTy::TypeKind::FLOAT: { | |
206 | if (static_cast<TyTy::FloatType &> (*argument_expr_tyty) | |
207 | .get_float_kind () | |
208 | == TyTy::FloatType::FloatKind::F32) | |
209 | { | |
718b65a9 MM |
210 | rich_location richloc (line_table, arg_locus); |
211 | richloc.add_fixit_replace ( | |
212 | "cast the value to c_double: as c_double"); | |
213 | rust_error_at (richloc, ErrorCode::E0617, | |
a9d72ef2 OA |
214 | "expected %<c_double%> variadic argument"); |
215 | return; | |
216 | } | |
217 | break; | |
218 | } | |
718b65a9 MM |
219 | case TyTy::TypeKind::BOOL: { |
220 | rich_location richloc (line_table, arg_locus); | |
221 | richloc.add_fixit_replace ("cast the value to c_int: as c_int"); | |
222 | rust_error_at (arg_locus, ErrorCode::E0617, | |
223 | "expected %<c_int%> variadic argument"); | |
224 | return; | |
225 | } | |
226 | case TyTy::TypeKind::FNDEF: { | |
227 | rust_error_at ( | |
228 | arg_locus, ErrorCode::E0617, | |
229 | "unexpected function definition type as variadic " | |
230 | "argument - cast to function pointer"); | |
231 | } | |
a9d72ef2 OA |
232 | return; |
233 | default: | |
234 | break; | |
235 | } | |
236 | } | |
06688fe4 PH |
237 | |
238 | i++; | |
239 | } | |
240 | ||
241 | if (i < call.num_params ()) | |
242 | { | |
8ab098a7 MM |
243 | emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, |
244 | (unsigned long) call.num_params ()); | |
06688fe4 PH |
245 | return; |
246 | } | |
247 | ||
248 | type.monomorphize (); | |
249 | resolved = type.get_return_type ()->clone (); | |
250 | } | |
251 | ||
252 | void | |
253 | TypeCheckCallExpr::visit (FnPtr &type) | |
254 | { | |
255 | if (call.num_params () != type.num_params ()) | |
256 | { | |
8ab098a7 MM |
257 | emit_unexpected_argument_error (call.get_locus (), |
258 | (unsigned long) call.num_params (), | |
259 | (unsigned long) type.num_params ()); | |
06688fe4 PH |
260 | return; |
261 | } | |
262 | ||
263 | size_t i = 0; | |
264 | for (auto &argument : call.get_arguments ()) | |
265 | { | |
9cc353d7 | 266 | location_t arg_locus = argument->get_locus (); |
14025f73 | 267 | BaseType *fnparam = type.get_param_type_at (i); |
c29a3bc9 | 268 | auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument); |
06688fe4 PH |
269 | if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) |
270 | { | |
271 | rust_error_at ( | |
272 | argument->get_locus (), | |
273 | "failed to resolve type for argument expr in CallExpr"); | |
274 | return; | |
275 | } | |
276 | ||
f8770d18 | 277 | auto resolved_argument_type = Resolver::coercion_site ( |
af22b54a PH |
278 | argument->get_mappings ().get_hirid (), TyWithLocation (fnparam), |
279 | TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ()); | |
06688fe4 PH |
280 | if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |
281 | { | |
06688fe4 PH |
282 | return; |
283 | } | |
284 | ||
285 | i++; | |
286 | } | |
287 | ||
288 | if (i != call.num_params ()) | |
289 | { | |
8ab098a7 MM |
290 | emit_unexpected_argument_error (call.get_locus (), (unsigned long) i, |
291 | (unsigned long) call.num_params ()); | |
06688fe4 PH |
292 | return; |
293 | } | |
294 | ||
295 | resolved = type.get_return_type ()->monomorphized_clone (); | |
296 | } | |
297 | ||
298 | // method call checker | |
299 | ||
4d021d9e PH |
300 | TypeCheckMethodCallExpr::TypeCheckMethodCallExpr ( |
301 | Analysis::NodeMapping call_mappings, std::vector<Argument> &args, | |
9cc353d7 OA |
302 | location_t call_locus, location_t receiver_locus, |
303 | TyTy::BaseType *adjusted_self, Resolver::TypeCheckContext *context) | |
4d021d9e PH |
304 | : call_mappings (call_mappings), arguments (args), call_locus (call_locus), |
305 | receiver_locus (receiver_locus), adjusted_self (adjusted_self), | |
306 | context (context), mappings (Analysis::Mappings::get ()) | |
307 | {} | |
308 | ||
309 | BaseType * | |
310 | TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, | |
311 | TyTy::BaseType *adjusted_self, | |
312 | Resolver::TypeCheckContext *context) | |
313 | { | |
314 | std::vector<Argument> args; | |
315 | for (auto &arg : call.get_arguments ()) | |
316 | { | |
c29a3bc9 | 317 | BaseType *argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*arg); |
4d021d9e PH |
318 | if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) |
319 | { | |
320 | rust_error_at (arg->get_locus (), | |
321 | "failed to resolve type for argument"); | |
322 | return new ErrorType (ref->get_ref ()); | |
323 | } | |
324 | ||
325 | Argument a (arg->get_mappings (), argument_expr_tyty, arg->get_locus ()); | |
326 | args.push_back (std::move (a)); | |
327 | } | |
328 | ||
329 | TypeCheckMethodCallExpr checker (call.get_mappings (), args, | |
330 | call.get_locus (), | |
c29a3bc9 | 331 | call.get_receiver ().get_locus (), |
4d021d9e PH |
332 | adjusted_self, context); |
333 | return checker.check (*ref); | |
334 | } | |
335 | ||
336 | BaseType * | |
337 | TypeCheckMethodCallExpr::go (FnType *ref, Analysis::NodeMapping call_mappings, | |
9cc353d7 OA |
338 | std::vector<Argument> &args, location_t call_locus, |
339 | location_t receiver_locus, | |
4d021d9e PH |
340 | TyTy::BaseType *adjusted_self, |
341 | Resolver::TypeCheckContext *context) | |
342 | { | |
343 | TypeCheckMethodCallExpr checker (call_mappings, args, call_locus, | |
344 | receiver_locus, adjusted_self, context); | |
345 | return checker.check (*ref); | |
346 | } | |
347 | ||
348 | BaseType * | |
349 | TypeCheckMethodCallExpr::check (FnType &type) | |
06688fe4 | 350 | { |
f97c3809 PEP |
351 | Resolver::unify_site (call_mappings.get_hirid (), |
352 | TyWithLocation (type.get_self_type ()), | |
353 | TyWithLocation (adjusted_self, receiver_locus), | |
354 | call_locus); | |
06688fe4 PH |
355 | |
356 | // +1 for the receiver self | |
4d021d9e | 357 | size_t num_args_to_call = arguments.size () + 1; |
06688fe4 PH |
358 | if (num_args_to_call != type.num_params ()) |
359 | { | |
8ab098a7 MM |
360 | emit_unexpected_argument_error (call_locus, |
361 | (unsigned long) num_args_to_call, | |
362 | (unsigned long) type.num_params ()); | |
4d021d9e | 363 | return new ErrorType (type.get_ref ()); |
06688fe4 PH |
364 | } |
365 | ||
366 | size_t i = 1; | |
4d021d9e | 367 | for (auto &argument : arguments) |
06688fe4 | 368 | { |
9cc353d7 | 369 | location_t arg_locus = argument.get_locus (); |
af22b54a | 370 | |
c29a3bc9 | 371 | auto &fnparam = type.param_at (i); |
d123f43e | 372 | BaseType *param_ty = fnparam.get_type (); |
9cc353d7 | 373 | location_t param_locus |
d123f43e | 374 | = fnparam.has_pattern () |
b3246d3f PEP |
375 | ? fnparam.get_pattern ().get_locus () |
376 | : mappings.lookup_location (param_ty->get_ref ()); | |
af22b54a | 377 | |
4d021d9e PH |
378 | auto argument_expr_tyty = argument.get_argument_type (); |
379 | HirId coercion_side_id = argument.get_mappings ().get_hirid (); | |
f8770d18 | 380 | auto resolved_argument_type = Resolver::coercion_site ( |
af22b54a | 381 | coercion_side_id, TyWithLocation (param_ty, param_locus), |
4d021d9e | 382 | TyWithLocation (argument_expr_tyty, arg_locus), arg_locus); |
06688fe4 PH |
383 | if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |
384 | { | |
4d021d9e | 385 | return new ErrorType (type.get_ref ()); |
06688fe4 PH |
386 | } |
387 | ||
388 | i++; | |
389 | } | |
390 | ||
391 | if (i != num_args_to_call) | |
392 | { | |
8ab098a7 MM |
393 | emit_unexpected_argument_error (call_locus, (unsigned long) i, |
394 | (unsigned long) arguments.size ()); | |
4d021d9e | 395 | return new ErrorType (type.get_ref ()); |
06688fe4 PH |
396 | } |
397 | ||
398 | type.monomorphize (); | |
399 | ||
4d021d9e | 400 | return type.get_return_type ()->monomorphized_clone (); |
06688fe4 PH |
401 | } |
402 | ||
403 | } // namespace TyTy | |
404 | } // namespace Rust |