]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Language-level data type conversion for GNU C++. |
a945c346 | 2 | Copyright (C) 1987-2024 Free Software Foundation, Inc. |
8d08fdba MS |
3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
f5adbb8d | 5 | This file is part of GCC. |
8d08fdba | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
10 | any later version. |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
20 | |
21 | ||
3fd5abcf | 22 | /* This file contains the functions for converting C++ expressions |
8d08fdba MS |
23 | to different data types. The only entry point is `convert'. |
24 | Every language front end must have a `convert' function | |
25 | but what kind of conversions it does will depend on the language. */ | |
26 | ||
27 | #include "config.h" | |
8d052bc7 | 28 | #include "system.h" |
4977bab6 | 29 | #include "coretypes.h" |
2adfab87 | 30 | #include "target.h" |
2adfab87 | 31 | #include "cp-tree.h" |
d8a2d370 | 32 | #include "stor-layout.h" |
8d08fdba | 33 | #include "flags.h" |
f25a2b52 | 34 | #include "intl.h" |
8d08fdba | 35 | #include "convert.h" |
314e6352 ML |
36 | #include "stringpool.h" |
37 | #include "attribs.h" | |
8ad0c477 | 38 | #include "escaped_string.h" |
e92cc029 | 39 | |
4b978f96 | 40 | static tree convert_to_pointer_force (tree, tree, tsubst_flags_t); |
ae00383b | 41 | static tree build_type_conversion (tree, tree); |
4b978f96 | 42 | static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t); |
76545796 | 43 | static void diagnose_ref_binding (location_t, tree, tree, tree); |
49c249e1 | 44 | |
8d08fdba MS |
45 | /* Change of width--truncation and extension of integers or reals-- |
46 | is represented with NOP_EXPR. Proper functioning of many things | |
47 | assumes that no other conversions can be NOP_EXPRs. | |
48 | ||
49 | Conversion between integer and pointer is represented with CONVERT_EXPR. | |
50 | Converting integer to real uses FLOAT_EXPR | |
51 | and real to integer uses FIX_TRUNC_EXPR. | |
52 | ||
53 | Here is a list of all the functions that assume that widening and | |
54 | narrowing is always done with a NOP_EXPR: | |
e53b6e56 ML |
55 | In convert.cc, convert_to_integer[_maybe_fold]. |
56 | In c-typeck.cc, build_binary_op_nodefault (boolean ops), | |
0cbd7506 | 57 | and c_common_truthvalue_conversion. |
e53b6e56 ML |
58 | In expr.cc: expand_expr, for operands of a MULT_EXPR. |
59 | In fold-const.cc: fold. | |
60 | In tree.cc: get_narrower and get_unwidened. | |
8d08fdba MS |
61 | |
62 | C++: in multiple-inheritance, converting between pointers may involve | |
63 | adjusting them by a delta stored within the class definition. */ | |
64 | \f | |
65 | /* Subroutines of `convert'. */ | |
66 | ||
8d08fdba MS |
67 | /* if converting pointer to pointer |
68 | if dealing with classes, check for derived->base or vice versa | |
69 | else if dealing with method pointers, delegate | |
70 | else convert blindly | |
71 | else if converting class, pass off to build_type_conversion | |
6e03b280 | 72 | else try C-style pointer conversion. */ |
e92cc029 | 73 | |
8d08fdba | 74 | static tree |
415594bb JM |
75 | cp_convert_to_pointer (tree type, tree expr, bool dofold, |
76 | tsubst_flags_t complain) | |
8d08fdba | 77 | { |
926ce8bd KH |
78 | tree intype = TREE_TYPE (expr); |
79 | enum tree_code form; | |
31bcaa20 | 80 | tree rval; |
f9d0ca40 | 81 | location_t loc = cp_expr_loc_or_input_loc (expr); |
5a3c9cf2 | 82 | |
0c482362 AP |
83 | if (intype == error_mark_node) |
84 | return error_mark_node; | |
71851aaa | 85 | |
9e1e64ec | 86 | if (MAYBE_CLASS_TYPE_P (intype)) |
e92cc029 | 87 | { |
e92cc029 | 88 | intype = complete_type (intype); |
d0f062fb | 89 | if (!COMPLETE_TYPE_P (intype)) |
e92cc029 | 90 | { |
4b978f96 | 91 | if (complain & tf_error) |
a9c697b8 | 92 | error_at (loc, "cannot convert from incomplete type %qH to %qI", |
4b978f96 | 93 | intype, type); |
e92cc029 MS |
94 | return error_mark_node; |
95 | } | |
96 | ||
7993382e | 97 | rval = build_type_conversion (type, expr); |
e92cc029 MS |
98 | if (rval) |
99 | { | |
4b978f96 PC |
100 | if ((complain & tf_error) |
101 | && rval == error_mark_node) | |
f012c8ef | 102 | error_at (loc, "conversion of %qE from %qH to %qI is ambiguous", |
5a3c9cf2 | 103 | expr, intype, type); |
e92cc029 MS |
104 | return rval; |
105 | } | |
106 | } | |
107 | ||
faf5394a | 108 | /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */ |
50e10fa8 | 109 | if (TYPE_PTR_P (type) |
9a3b49ac | 110 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE |
b72801e2 | 111 | || VOID_TYPE_P (TREE_TYPE (type)))) |
9a3b49ac | 112 | { |
33c25e5c MM |
113 | if (TYPE_PTRMEMFUNC_P (intype) |
114 | || TREE_CODE (intype) == METHOD_TYPE) | |
4b978f96 | 115 | return convert_member_func_to_ptr (type, expr, complain); |
50e10fa8 | 116 | if (TYPE_PTR_P (TREE_TYPE (expr))) |
d6b4ea85 | 117 | return build_nop (type, expr); |
9a3b49ac MS |
118 | intype = TREE_TYPE (expr); |
119 | } | |
120 | ||
c87978aa JM |
121 | if (expr == error_mark_node) |
122 | return error_mark_node; | |
123 | ||
71851aaa MS |
124 | form = TREE_CODE (intype); |
125 | ||
71a93b08 | 126 | if (INDIRECT_TYPE_P (intype)) |
8d08fdba MS |
127 | { |
128 | intype = TYPE_MAIN_VARIANT (intype); | |
129 | ||
130 | if (TYPE_MAIN_VARIANT (type) != intype | |
50e10fa8 | 131 | && TYPE_PTR_P (type) |
8d08fdba | 132 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE |
9e1e64ec PC |
133 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (type)) |
134 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype)) | |
338d90b8 | 135 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
8d08fdba MS |
136 | { |
137 | enum tree_code code = PLUS_EXPR; | |
338d90b8 | 138 | tree binfo; |
7993382e MM |
139 | tree intype_class; |
140 | tree type_class; | |
141 | bool same_p; | |
338d90b8 | 142 | |
7993382e MM |
143 | intype_class = TREE_TYPE (intype); |
144 | type_class = TREE_TYPE (type); | |
145 | ||
c8094d83 | 146 | same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class), |
7993382e MM |
147 | TYPE_MAIN_VARIANT (type_class)); |
148 | binfo = NULL_TREE; | |
00a17e31 | 149 | /* Try derived to base conversion. */ |
7993382e | 150 | if (!same_p) |
22854930 PC |
151 | binfo = lookup_base (intype_class, type_class, ba_check, |
152 | NULL, complain); | |
7993382e | 153 | if (!same_p && !binfo) |
8d08fdba | 154 | { |
00a17e31 | 155 | /* Try base to derived conversion. */ |
22854930 PC |
156 | binfo = lookup_base (type_class, intype_class, ba_check, |
157 | NULL, complain); | |
8d08fdba MS |
158 | code = MINUS_EXPR; |
159 | } | |
338d90b8 NS |
160 | if (binfo == error_mark_node) |
161 | return error_mark_node; | |
7993382e | 162 | if (binfo || same_p) |
8d08fdba | 163 | { |
7993382e | 164 | if (binfo) |
4b978f96 | 165 | expr = build_base_path (code, expr, binfo, 0, complain); |
00a17e31 | 166 | /* Add any qualifier conversions. */ |
7993382e | 167 | return build_nop (type, expr); |
8d08fdba MS |
168 | } |
169 | } | |
8d08fdba | 170 | |
a5ac359a | 171 | if (TYPE_PTRMEMFUNC_P (type)) |
28cbf42c | 172 | { |
4b978f96 | 173 | if (complain & tf_error) |
f012c8ef | 174 | error_at (loc, "cannot convert %qE from type %qH to type %qI", |
4b978f96 | 175 | expr, intype, type); |
a5ac359a MM |
176 | return error_mark_node; |
177 | } | |
b928a651 | 178 | |
a5ac359a MM |
179 | return build_nop (type, expr); |
180 | } | |
66b1156a | 181 | else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
6e03b280 OW |
182 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) |
183 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/false, | |
4b978f96 | 184 | /*c_cast_p=*/false, complain); |
d8f8dca1 MM |
185 | else if (TYPE_PTRMEMFUNC_P (intype)) |
186 | { | |
d6b4ea85 MM |
187 | if (!warn_pmf2ptr) |
188 | { | |
189 | if (TREE_CODE (expr) == PTRMEM_CST) | |
4b978f96 | 190 | return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr), |
415594bb | 191 | dofold, complain); |
d6b4ea85 MM |
192 | else if (TREE_CODE (expr) == OFFSET_REF) |
193 | { | |
194 | tree object = TREE_OPERAND (expr, 0); | |
195 | return get_member_function_from_ptrfunc (&object, | |
89fcabaf | 196 | TREE_OPERAND (expr, 1), |
4b978f96 | 197 | complain); |
d6b4ea85 MM |
198 | } |
199 | } | |
bc51de9c | 200 | if (complain & tf_error) |
f012c8ef | 201 | error_at (loc, "cannot convert %qE from type %qH to type %qI", |
bc51de9c | 202 | expr, intype, type); |
d8f8dca1 MM |
203 | return error_mark_node; |
204 | } | |
8d08fdba | 205 | |
33c2474d | 206 | if (null_ptr_cst_p (expr)) |
8d08fdba | 207 | { |
d8f8dca1 | 208 | if (TYPE_PTRMEMFUNC_P (type)) |
08e17d9d | 209 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, |
4b978f96 | 210 | /*c_cast_p=*/false, complain); |
cd8ed629 | 211 | |
f61af651 PC |
212 | if (complain & tf_warning) |
213 | maybe_warn_zero_as_null_pointer_constant (expr, loc); | |
214 | ||
e3692e02 PC |
215 | /* A NULL pointer-to-data-member is represented by -1, not by |
216 | zero. */ | |
217 | tree val = (TYPE_PTRDATAMEM_P (type) | |
218 | ? build_int_cst_type (type, -1) | |
219 | : build_int_cst (type, 0)); | |
220 | ||
221 | return (TREE_SIDE_EFFECTS (expr) | |
222 | ? build2 (COMPOUND_EXPR, type, expr, val) : val); | |
8d08fdba | 223 | } |
66b1156a | 224 | else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form)) |
201fbb7f | 225 | { |
4b978f96 | 226 | if (complain & tf_error) |
f012c8ef | 227 | error_at (loc, "invalid conversion from %qH to %qI", intype, type); |
201fbb7f GDR |
228 | return error_mark_node; |
229 | } | |
8d08fdba | 230 | |
2986ae00 | 231 | if (INTEGRAL_CODE_P (form)) |
8d08fdba | 232 | { |
f5963e61 | 233 | if (TYPE_PRECISION (intype) == POINTER_SIZE) |
8d08fdba | 234 | return build1 (CONVERT_EXPR, type, expr); |
720dff97 | 235 | expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr, |
4b978f96 | 236 | complain); |
8dc2b103 NS |
237 | /* Modes may be different but sizes should be the same. There |
238 | is supposed to be some integral type that is the same width | |
239 | as a pointer. */ | |
7a504f33 RS |
240 | gcc_assert (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (expr))) |
241 | == GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type))); | |
c8094d83 | 242 | |
dcbb27dd JM |
243 | /* FIXME needed because convert_to_pointer_maybe_fold still folds |
244 | conversion of constants. */ | |
245 | if (!dofold) | |
246 | return build1 (CONVERT_EXPR, type, expr); | |
247 | ||
415594bb | 248 | return convert_to_pointer_maybe_fold (type, expr, dofold); |
8d08fdba MS |
249 | } |
250 | ||
e6e174e5 | 251 | if (type_unknown_p (expr)) |
4b978f96 | 252 | return instantiate_type (type, expr, complain); |
e6e174e5 | 253 | |
4b978f96 | 254 | if (complain & tf_error) |
f012c8ef | 255 | error_at (loc, "cannot convert %qE from type %qH to type %qI", |
4b978f96 | 256 | expr, intype, type); |
8d08fdba MS |
257 | return error_mark_node; |
258 | } | |
259 | ||
260 | /* Like convert, except permit conversions to take place which | |
261 | are not normally allowed due to access restrictions | |
262 | (such as conversion from sub-type to private super-type). */ | |
e92cc029 | 263 | |
8d08fdba | 264 | static tree |
4b978f96 | 265 | convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain) |
8d08fdba | 266 | { |
926ce8bd KH |
267 | tree intype = TREE_TYPE (expr); |
268 | enum tree_code form = TREE_CODE (intype); | |
c8094d83 | 269 | |
8d08fdba MS |
270 | if (form == POINTER_TYPE) |
271 | { | |
272 | intype = TYPE_MAIN_VARIANT (intype); | |
273 | ||
274 | if (TYPE_MAIN_VARIANT (type) != intype | |
275 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
9e1e64ec PC |
276 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (type)) |
277 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype)) | |
8d08fdba MS |
278 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
279 | { | |
280 | enum tree_code code = PLUS_EXPR; | |
338d90b8 NS |
281 | tree binfo; |
282 | ||
283 | binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type), | |
22854930 | 284 | ba_unique, NULL, complain); |
338d90b8 | 285 | if (!binfo) |
8d08fdba | 286 | { |
338d90b8 | 287 | binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
22854930 | 288 | ba_unique, NULL, complain); |
338d90b8 | 289 | code = MINUS_EXPR; |
8d08fdba | 290 | } |
338d90b8 NS |
291 | if (binfo == error_mark_node) |
292 | return error_mark_node; | |
293 | if (binfo) | |
8d08fdba | 294 | { |
4b978f96 | 295 | expr = build_base_path (code, expr, binfo, 0, complain); |
0cbd7506 MS |
296 | if (expr == error_mark_node) |
297 | return error_mark_node; | |
00a17e31 | 298 | /* Add any qualifier conversions. */ |
338d90b8 NS |
299 | if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)), |
300 | TREE_TYPE (type))) | |
7993382e | 301 | expr = build_nop (type, expr); |
338d90b8 | 302 | return expr; |
8d08fdba | 303 | } |
8d08fdba | 304 | } |
8d08fdba MS |
305 | } |
306 | ||
415594bb | 307 | return cp_convert_to_pointer (type, expr, /*fold*/false, complain); |
8d08fdba MS |
308 | } |
309 | ||
310 | /* We are passing something to a function which requires a reference. | |
311 | The type we are interested in is in TYPE. The initial | |
312 | value we have to begin with is in ARG. | |
313 | ||
314 | FLAGS controls how we manage access checking. | |
08ac397c JM |
315 | DIRECT_BIND in FLAGS controls how any temporaries are generated. |
316 | If DIRECT_BIND is set, DECL is the reference we're binding to. */ | |
e92cc029 | 317 | |
8d08fdba | 318 | static tree |
4b978f96 PC |
319 | build_up_reference (tree type, tree arg, int flags, tree decl, |
320 | tsubst_flags_t complain) | |
8d08fdba | 321 | { |
eb66be0e | 322 | tree rval; |
8926095f | 323 | tree argtype = TREE_TYPE (arg); |
8d08fdba | 324 | tree target_type = TREE_TYPE (type); |
8d08fdba | 325 | |
9f613f06 | 326 | gcc_assert (TYPE_REF_P (type)); |
8d08fdba | 327 | |
72b3e203 | 328 | if ((flags & DIRECT_BIND) && ! lvalue_p (arg)) |
8d08fdba | 329 | { |
08ac397c JM |
330 | /* Create a new temporary variable. We can't just use a TARGET_EXPR |
331 | here because it needs to live as long as DECL. */ | |
eb66be0e | 332 | tree targ = arg; |
08ac397c | 333 | |
efd7ad5c | 334 | arg = make_temporary_var_for_ref_to_temp (decl, target_type); |
8d1e67c6 MM |
335 | |
336 | /* Process the initializer for the declaration. */ | |
9a3b49ac | 337 | DECL_INITIAL (arg) = targ; |
d174af6c | 338 | cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE, |
c37dc68e | 339 | LOOKUP_ONLYCONVERTING|DIRECT_BIND); |
e349ee73 | 340 | } |
bb19d4af | 341 | else if (!(flags & DIRECT_BIND) && ! obvalue_p (arg)) |
c17fa0f2 | 342 | return get_target_expr (arg, complain); |
e349ee73 | 343 | |
08ac397c | 344 | /* If we had a way to wrap this up, and say, if we ever needed its |
d2e5ee5c MS |
345 | address, transform all occurrences of the register, into a memory |
346 | reference we could win better. */ | |
81bd268c | 347 | rval = cp_build_addr_expr (arg, complain); |
162bc98d JM |
348 | if (rval == error_mark_node) |
349 | return error_mark_node; | |
350 | ||
6633d636 MS |
351 | if ((flags & LOOKUP_PROTECT) |
352 | && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type) | |
9e1e64ec PC |
353 | && MAYBE_CLASS_TYPE_P (argtype) |
354 | && MAYBE_CLASS_TYPE_P (target_type)) | |
6633d636 | 355 | { |
2db1ab2d | 356 | /* We go through lookup_base for the access control. */ |
22854930 PC |
357 | tree binfo = lookup_base (argtype, target_type, ba_check, |
358 | NULL, complain); | |
6633d636 MS |
359 | if (binfo == error_mark_node) |
360 | return error_mark_node; | |
361 | if (binfo == NULL_TREE) | |
362 | return error_not_base_type (target_type, argtype); | |
4b978f96 | 363 | rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain); |
6633d636 | 364 | } |
eb66be0e MS |
365 | else |
366 | rval | |
4b978f96 PC |
367 | = convert_to_pointer_force (build_pointer_type (target_type), |
368 | rval, complain); | |
7993382e | 369 | return build_nop (type, rval); |
8d08fdba MS |
370 | } |
371 | ||
08aead78 NS |
372 | /* Subroutine of convert_to_reference. REFTYPE is the target reference type. |
373 | INTYPE is the original rvalue type and DECL is an optional _DECL node | |
374 | for diagnostics. | |
c8094d83 | 375 | |
08aead78 NS |
376 | [dcl.init.ref] says that if an rvalue is used to |
377 | initialize a reference, then the reference must be to a | |
378 | non-volatile const type. */ | |
379 | ||
380 | static void | |
76545796 | 381 | diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl) |
08aead78 NS |
382 | { |
383 | tree ttl = TREE_TYPE (reftype); | |
c8094d83 | 384 | |
a347241b JM |
385 | if (!TYPE_REF_IS_RVALUE (reftype) |
386 | && !CP_TYPE_CONST_NON_VOLATILE_P (ttl)) | |
08aead78 NS |
387 | { |
388 | const char *msg; | |
389 | ||
390 | if (CP_TYPE_VOLATILE_P (ttl) && decl) | |
f25a2b52 SZ |
391 | msg = G_("initialization of volatile reference type %q#T from " |
392 | "rvalue of type %qT"); | |
08aead78 | 393 | else if (CP_TYPE_VOLATILE_P (ttl)) |
f25a2b52 SZ |
394 | msg = G_("conversion to volatile reference type %q#T " |
395 | "from rvalue of type %qT"); | |
08aead78 | 396 | else if (decl) |
f25a2b52 SZ |
397 | msg = G_("initialization of non-const reference type %q#T from " |
398 | "rvalue of type %qT"); | |
08aead78 | 399 | else |
f25a2b52 SZ |
400 | msg = G_("conversion to non-const reference type %q#T from " |
401 | "rvalue of type %qT"); | |
08aead78 | 402 | |
5a3c9cf2 | 403 | permerror (loc, msg, reftype, intype); |
08aead78 NS |
404 | } |
405 | } | |
406 | ||
8d08fdba MS |
407 | /* For C++: Only need to do one-level references, but cannot |
408 | get tripped up on signed/unsigned differences. | |
409 | ||
a4443a08 MS |
410 | DECL is either NULL_TREE or the _DECL node for a reference that is being |
411 | initialized. It can be error_mark_node if we don't know the _DECL but | |
412 | we know it's an initialization. */ | |
8d08fdba | 413 | |
8d08fdba | 414 | tree |
b746c5dc | 415 | convert_to_reference (tree reftype, tree expr, int convtype, |
4b978f96 | 416 | int flags, tree decl, tsubst_flags_t complain) |
8d08fdba | 417 | { |
926ce8bd KH |
418 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); |
419 | tree intype; | |
8d08fdba | 420 | tree rval = NULL_TREE; |
8ccc31eb | 421 | tree rval_as_conversion = NULL_TREE; |
a5ac359a | 422 | bool can_convert_intype_to_type; |
f9d0ca40 | 423 | location_t loc = cp_expr_loc_or_input_loc (expr); |
8ccc31eb | 424 | |
c8094d83 | 425 | if (TREE_CODE (type) == FUNCTION_TYPE |
2303a079 | 426 | && TREE_TYPE (expr) == unknown_type_node) |
b40e334f | 427 | expr = instantiate_type (type, expr, complain); |
a723baf1 MM |
428 | |
429 | if (expr == error_mark_node) | |
430 | return error_mark_node; | |
431 | ||
432 | intype = TREE_TYPE (expr); | |
e6e174e5 | 433 | |
9f613f06 PC |
434 | gcc_assert (!TYPE_REF_P (intype)); |
435 | gcc_assert (TYPE_REF_P (reftype)); | |
8d08fdba | 436 | |
8d08fdba MS |
437 | intype = TYPE_MAIN_VARIANT (intype); |
438 | ||
53db1bc0 | 439 | can_convert_intype_to_type = can_convert_standard (type, intype, complain); |
b40e334f | 440 | |
a5ac359a | 441 | if (!can_convert_intype_to_type |
9e1e64ec | 442 | && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype) |
8ccc31eb MS |
443 | && ! (flags & LOOKUP_NO_CONVERSION)) |
444 | { | |
445 | /* Look for a user-defined conversion to lvalue that we can use. */ | |
446 | ||
277294d7 | 447 | rval_as_conversion |
7993382e | 448 | = build_type_conversion (reftype, expr); |
8ccc31eb MS |
449 | |
450 | if (rval_as_conversion && rval_as_conversion != error_mark_node | |
72b3e203 | 451 | && lvalue_p (rval_as_conversion)) |
8ccc31eb MS |
452 | { |
453 | expr = rval_as_conversion; | |
454 | rval_as_conversion = NULL_TREE; | |
455 | intype = type; | |
a5ac359a | 456 | can_convert_intype_to_type = 1; |
8ccc31eb MS |
457 | } |
458 | } | |
459 | ||
53db1bc0 JM |
460 | if (((convtype & CONV_STATIC) |
461 | && can_convert_standard (intype, type, complain)) | |
a5ac359a | 462 | || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type)) |
8d08fdba | 463 | { |
4b978f96 PC |
464 | { |
465 | tree ttl = TREE_TYPE (reftype); | |
466 | tree ttr = lvalue_type (expr); | |
8d08fdba | 467 | |
76545796 | 468 | if ((complain & tf_error) |
72b3e203 | 469 | && ! lvalue_p (expr)) |
76545796 | 470 | diagnose_ref_binding (loc, reftype, intype, decl); |
c8094d83 | 471 | |
4b978f96 PC |
472 | if (! (convtype & CONV_CONST) |
473 | && !at_least_as_qualified_p (ttl, ttr)) | |
474 | { | |
475 | if (complain & tf_error) | |
f012c8ef | 476 | permerror (loc, "conversion from %qH to %qI discards qualifiers", |
4b978f96 PC |
477 | ttr, reftype); |
478 | else | |
479 | return error_mark_node; | |
480 | } | |
481 | } | |
8926095f | 482 | |
4b978f96 | 483 | return build_up_reference (reftype, expr, flags, decl, complain); |
8d08fdba | 484 | } |
bb19d4af | 485 | else if ((convtype & CONV_REINTERPRET) && obvalue_p (expr)) |
8926095f MS |
486 | { |
487 | /* When casting an lvalue to a reference type, just convert into | |
488 | a pointer to the new type and deference it. This is allowed | |
a28e3c7f | 489 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
8926095f | 490 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
a28e3c7f | 491 | |
59be0cdd | 492 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
0cbd7506 | 493 | meant. */ |
4b978f96 | 494 | if ((complain & tf_warning) |
50e10fa8 | 495 | && TYPE_PTR_P (intype) |
96d84882 PB |
496 | && (comptypes (TREE_TYPE (intype), type, |
497 | COMPARE_BASE | COMPARE_DERIVED))) | |
5a3c9cf2 PC |
498 | warning_at (loc, 0, "casting %qT to %qT does not dereference pointer", |
499 | intype, reftype); | |
c8094d83 | 500 | |
4b978f96 | 501 | rval = cp_build_addr_expr (expr, complain); |
8926095f | 502 | if (rval != error_mark_node) |
3c215895 | 503 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), |
4b978f96 | 504 | rval, 0, complain); |
8926095f | 505 | if (rval != error_mark_node) |
7177d104 | 506 | rval = build1 (NOP_EXPR, reftype, rval); |
8926095f | 507 | } |
277294d7 | 508 | else |
faf5394a MS |
509 | { |
510 | rval = convert_for_initialization (NULL_TREE, type, expr, flags, | |
4b978f96 | 511 | ICR_CONVERTING, 0, 0, complain); |
09ad2917 DD |
512 | if (rval == NULL_TREE || rval == error_mark_node) |
513 | return rval; | |
76545796 PC |
514 | if (complain & tf_error) |
515 | diagnose_ref_binding (loc, reftype, intype, decl); | |
4b978f96 | 516 | rval = build_up_reference (reftype, rval, flags, decl, complain); |
faf5394a | 517 | } |
8d08fdba MS |
518 | |
519 | if (rval) | |
520 | { | |
e92cc029 | 521 | /* If we found a way to convert earlier, then use it. */ |
8d08fdba MS |
522 | return rval; |
523 | } | |
524 | ||
4b978f96 | 525 | if (complain & tf_error) |
f012c8ef | 526 | error_at (loc, "cannot convert type %qH to type %qI", intype, reftype); |
878cd289 | 527 | |
8d08fdba MS |
528 | return error_mark_node; |
529 | } | |
530 | ||
531 | /* We are using a reference VAL for its value. Bash that reference all the | |
e92cc029 MS |
532 | way down to its lowest form. */ |
533 | ||
8d08fdba | 534 | tree |
b746c5dc | 535 | convert_from_reference (tree val) |
8d08fdba | 536 | { |
4e3f6d85 | 537 | if (TREE_TYPE (val) |
9f613f06 | 538 | && TYPE_REF_P (TREE_TYPE (val))) |
db24eb1f | 539 | { |
cd41d410 | 540 | tree t = TREE_TYPE (TREE_TYPE (val)); |
db24eb1f | 541 | tree ref = build1 (INDIRECT_REF, t, val); |
c8094d83 | 542 | |
e3ae330d | 543 | mark_exp_read (val); |
db24eb1f NS |
544 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
545 | so that we get the proper error message if the result is used | |
546 | to assign to. Also, &* is supposed to be a no-op. */ | |
547 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); | |
548 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
549 | TREE_SIDE_EFFECTS (ref) | |
550 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val)); | |
db24eb1f NS |
551 | val = ref; |
552 | } | |
c8094d83 | 553 | |
8d08fdba MS |
554 | return val; |
555 | } | |
f7b9026e JM |
556 | |
557 | /* Really perform an lvalue-to-rvalue conversion, including copying an | |
558 | argument of class type into a temporary. */ | |
559 | ||
560 | tree | |
574cfaa4 | 561 | force_rvalue (tree expr, tsubst_flags_t complain) |
f7b9026e | 562 | { |
574cfaa4 JM |
563 | tree type = TREE_TYPE (expr); |
564 | if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR) | |
565 | { | |
cd9cf97b | 566 | releasing_vec args (make_tree_vector_single (expr)); |
574cfaa4 JM |
567 | expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
568 | &args, type, LOOKUP_NORMAL, complain); | |
574cfaa4 JM |
569 | expr = build_cplus_new (type, expr, complain); |
570 | } | |
f7b9026e | 571 | else |
89fcabaf | 572 | expr = decay_conversion (expr, complain); |
f7b9026e JM |
573 | |
574 | return expr; | |
575 | } | |
9771799c | 576 | |
8d08fdba | 577 | \f |
9e115cec JM |
578 | /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has |
579 | TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR | |
580 | unchanged. */ | |
581 | ||
582 | static tree | |
583 | ignore_overflows (tree expr, tree orig) | |
584 | { | |
dfd7fdca DM |
585 | tree stripped_expr = tree_strip_any_location_wrapper (expr); |
586 | tree stripped_orig = tree_strip_any_location_wrapper (orig); | |
587 | ||
588 | if (TREE_CODE (stripped_expr) == INTEGER_CST | |
589 | && TREE_CODE (stripped_orig) == INTEGER_CST | |
590 | && TREE_OVERFLOW (stripped_expr) != TREE_OVERFLOW (stripped_orig)) | |
9e115cec | 591 | { |
dfd7fdca | 592 | gcc_assert (!TREE_OVERFLOW (stripped_orig)); |
9e115cec | 593 | /* Ensure constant sharing. */ |
dfd7fdca DM |
594 | stripped_expr = wide_int_to_tree (TREE_TYPE (stripped_expr), |
595 | wi::to_wide (stripped_expr)); | |
9e115cec | 596 | } |
dfd7fdca DM |
597 | |
598 | return preserve_any_location_wrapper (stripped_expr, expr); | |
9e115cec JM |
599 | } |
600 | ||
601 | /* Fold away simple conversions, but make sure TREE_OVERFLOW is set | |
d6f1cf64 MS |
602 | properly and propagate TREE_NO_WARNING if folding EXPR results |
603 | in the same expression code. */ | |
9771799c JM |
604 | |
605 | tree | |
606 | cp_fold_convert (tree type, tree expr) | |
607 | { | |
fb899e32 JM |
608 | tree conv; |
609 | if (TREE_TYPE (expr) == type) | |
610 | conv = expr; | |
d760b068 JM |
611 | else if (TREE_CODE (expr) == PTRMEM_CST |
612 | && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), | |
613 | PTRMEM_CST_CLASS (expr))) | |
fb899e32 JM |
614 | { |
615 | /* Avoid wrapping a PTRMEM_CST in NOP_EXPR. */ | |
616 | conv = copy_node (expr); | |
617 | TREE_TYPE (conv) = type; | |
618 | } | |
d760b068 JM |
619 | else if (TYPE_PTRMEM_P (type)) |
620 | { | |
621 | conv = convert_ptrmem (type, expr, true, false, | |
622 | tf_warning_or_error); | |
623 | conv = cp_fully_fold (conv); | |
624 | } | |
fb899e32 JM |
625 | else |
626 | { | |
627 | conv = fold_convert (type, expr); | |
628 | conv = ignore_overflows (conv, expr); | |
629 | } | |
d6f1cf64 | 630 | |
65870e75 MS |
631 | if (TREE_CODE (expr) == TREE_CODE (conv)) |
632 | copy_warning (conv, expr); | |
d6f1cf64 | 633 | |
9e115cec | 634 | return conv; |
9771799c JM |
635 | } |
636 | ||
37c46b43 MS |
637 | /* C++ conversions, preference to static cast conversions. */ |
638 | ||
639 | tree | |
4b978f96 | 640 | cp_convert (tree type, tree expr, tsubst_flags_t complain) |
37c46b43 | 641 | { |
4b978f96 | 642 | return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain); |
37c46b43 MS |
643 | } |
644 | ||
07231d4f MLI |
645 | /* C++ equivalent of convert_and_check but using cp_convert as the |
646 | conversion function. | |
647 | ||
648 | Convert EXPR to TYPE, warning about conversion problems with constants. | |
649 | Invoke this function on every expression that is converted implicitly, | |
650 | i.e. because of language rules and not because of an explicit cast. */ | |
651 | ||
652 | tree | |
4b978f96 | 653 | cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain) |
07231d4f | 654 | { |
3456db4d | 655 | tree result, expr_for_warning = expr; |
07231d4f | 656 | |
3456db4d JJ |
657 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
658 | expr = TREE_OPERAND (expr, 0); | |
07231d4f MLI |
659 | if (TREE_TYPE (expr) == type) |
660 | return expr; | |
cda0a029 JM |
661 | if (expr == error_mark_node) |
662 | return expr; | |
4b978f96 | 663 | result = cp_convert (type, expr, complain); |
07231d4f | 664 | |
4b978f96 | 665 | if ((complain & tf_warning) |
da834cfc JM |
666 | && c_inhibit_evaluation_warnings == 0) |
667 | { | |
3456db4d | 668 | tree folded = cp_fully_fold (expr_for_warning); |
cda0a029 JM |
669 | tree folded_result; |
670 | if (folded == expr) | |
671 | folded_result = result; | |
672 | else | |
673 | { | |
674 | /* Avoid bogus -Wparentheses warnings. */ | |
3212c3c8 | 675 | warning_sentinel w (warn_parentheses); |
144a96e4 | 676 | warning_sentinel c (warn_int_in_bool_context); |
cda0a029 JM |
677 | folded_result = cp_convert (type, folded, tf_none); |
678 | } | |
679 | folded_result = fold_simple (folded_result); | |
680 | if (!TREE_OVERFLOW_P (folded) | |
da834cfc | 681 | && folded_result != error_mark_node) |
f9d0ca40 | 682 | warnings_for_convert_and_check (cp_expr_loc_or_input_loc (expr), |
e87eed2a | 683 | type, folded, folded_result); |
da834cfc | 684 | } |
07231d4f MLI |
685 | |
686 | return result; | |
687 | } | |
688 | ||
878cd289 MS |
689 | /* Conversion... |
690 | ||
691 | FLAGS indicates how we should behave. */ | |
692 | ||
8d08fdba | 693 | tree |
4b978f96 PC |
694 | ocp_convert (tree type, tree expr, int convtype, int flags, |
695 | tsubst_flags_t complain) | |
8d08fdba | 696 | { |
926ce8bd KH |
697 | tree e = expr; |
698 | enum tree_code code = TREE_CODE (type); | |
4de67c26 | 699 | const char *invalid_conv_diag; |
40449a90 | 700 | tree e1; |
f9d0ca40 | 701 | location_t loc = cp_expr_loc_or_input_loc (expr); |
415594bb | 702 | bool dofold = (convtype & CONV_FOLD); |
8d08fdba | 703 | |
a5ac359a | 704 | if (error_operand_p (e) || type == error_mark_node) |
8d08fdba | 705 | return error_mark_node; |
a4443a08 | 706 | |
72809d6f JJ |
707 | if (TREE_CODE (e) == COMPOUND_EXPR) |
708 | { | |
709 | e = ocp_convert (type, TREE_OPERAND (e, 1), convtype, flags, complain); | |
710 | if (e == error_mark_node) | |
711 | return error_mark_node; | |
712 | if (e == TREE_OPERAND (expr, 1)) | |
713 | return expr; | |
e3585e6a MP |
714 | e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e), |
715 | TREE_OPERAND (expr, 0), e); | |
716 | copy_warning (e, expr); | |
717 | return e; | |
72809d6f JJ |
718 | } |
719 | ||
5d73aa63 MM |
720 | complete_type (type); |
721 | complete_type (TREE_TYPE (expr)); | |
722 | ||
4de67c26 JM |
723 | if ((invalid_conv_diag |
724 | = targetm.invalid_conversion (TREE_TYPE (expr), type))) | |
725 | { | |
4b978f96 PC |
726 | if (complain & tf_error) |
727 | error (invalid_conv_diag); | |
4de67c26 JM |
728 | return error_mark_node; |
729 | } | |
730 | ||
fa2200cb | 731 | /* FIXME remove when moving to c_fully_fold model. */ |
9783ae5a | 732 | if (!CLASS_TYPE_P (type)) |
82be290b JM |
733 | { |
734 | e = mark_rvalue_use (e); | |
effcb418 PP |
735 | tree v = scalar_constant_value (e); |
736 | if (!error_operand_p (v)) | |
737 | e = v; | |
82be290b | 738 | } |
88274c4d JM |
739 | if (error_operand_p (e)) |
740 | return error_mark_node; | |
9c0d0367 | 741 | |
39d8c7d2 PC |
742 | if (NULLPTR_TYPE_P (type) && null_ptr_cst_p (e)) |
743 | { | |
744 | if (complain & tf_warning) | |
745 | maybe_warn_zero_as_null_pointer_constant (e, loc); | |
746 | ||
747 | if (!TREE_SIDE_EFFECTS (e)) | |
748 | return nullptr_node; | |
749 | } | |
750 | ||
36cbfdb0 | 751 | if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP)) |
b7558a2c | 752 | /* We need a new temporary; don't take this shortcut. */; |
0fcedd9c | 753 | else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e))) |
01240200 | 754 | { |
5dab8b11 JM |
755 | tree etype = TREE_TYPE (e); |
756 | if (same_type_p (type, etype)) | |
01240200 MM |
757 | /* The call to fold will not always remove the NOP_EXPR as |
758 | might be expected, since if one of the types is a typedef; | |
34cd5ae7 | 759 | the comparison in fold is just equality of pointers, not a |
96d84882 | 760 | call to comptypes. We don't call fold in this case because |
953360c8 MM |
761 | that can result in infinite recursion; fold will call |
762 | convert, which will call ocp_convert, etc. */ | |
763 | return e; | |
a04678ca | 764 | /* For complex data types, we need to perform componentwise |
0cbd7506 | 765 | conversion. */ |
a04678ca | 766 | else if (TREE_CODE (type) == COMPLEX_TYPE) |
415594bb | 767 | return convert_to_complex_maybe_fold (type, e, dofold); |
b55b02ea | 768 | else if (VECTOR_TYPE_P (type)) |
96bea935 | 769 | return convert_to_vector (type, rvalue (e)); |
4e8dca1c JM |
770 | else if (TREE_CODE (e) == TARGET_EXPR) |
771 | { | |
772 | /* Don't build a NOP_EXPR of class type. Instead, change the | |
0fcedd9c | 773 | type of the temporary. */ |
5dab8b11 | 774 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype)); |
4e8dca1c JM |
775 | TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type; |
776 | return e; | |
777 | } | |
5dab8b11 JM |
778 | else if (TREE_CODE (e) == CONSTRUCTOR) |
779 | { | |
780 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype)); | |
781 | TREE_TYPE (e) = type; | |
782 | return e; | |
783 | } | |
01240200 | 784 | else |
8dc2b103 NS |
785 | { |
786 | /* We shouldn't be treating objects of ADDRESSABLE type as | |
787 | rvalues. */ | |
788 | gcc_assert (!TREE_ADDRESSABLE (type)); | |
cda0a029 | 789 | return build_nop (type, e); |
8dc2b103 | 790 | } |
01240200 MM |
791 | } |
792 | ||
40449a90 SL |
793 | e1 = targetm.convert_to_type (type, e); |
794 | if (e1) | |
795 | return e1; | |
796 | ||
a4443a08 | 797 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) |
848b92e1 | 798 | { |
4b978f96 | 799 | e = convert_to_void (e, ICV_CAST, complain); |
66543169 | 800 | return e; |
848b92e1 | 801 | } |
a4443a08 | 802 | |
2986ae00 | 803 | if (INTEGRAL_CODE_P (code)) |
8d08fdba | 804 | { |
f0e01782 | 805 | tree intype = TREE_TYPE (e); |
9e115cec | 806 | tree converted; |
b13e752f MLI |
807 | |
808 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
809 | { | |
810 | /* enum = enum, enum = int, enum = float, (enum)pointer are all | |
811 | errors. */ | |
812 | if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype) | |
8861c807 | 813 | || SCALAR_FLOAT_TYPE_P (intype)) |
b6ab6892 | 814 | && ! (convtype & CONV_STATIC)) |
50e10fa8 | 815 | || TYPE_PTR_P (intype)) |
b13e752f | 816 | { |
4b978f96 | 817 | if (complain & tf_error) |
5a3c9cf2 | 818 | permerror (loc, "conversion from %q#T to %q#T", intype, type); |
4b978f96 | 819 | else |
b13e752f MLI |
820 | return error_mark_node; |
821 | } | |
822 | ||
823 | /* [expr.static.cast] | |
824 | ||
825 | 8. A value of integral or enumeration type can be explicitly | |
826 | converted to an enumeration type. The value is unchanged if | |
827 | the original value is within the range of the enumeration | |
828 | values. Otherwise, the resulting enumeration value is | |
829 | unspecified. */ | |
dfd7fdca | 830 | tree val = fold_for_warn (e); |
4b978f96 | 831 | if ((complain & tf_warning) |
dfd7fdca | 832 | && TREE_CODE (val) == INTEGER_CST |
0ffc4683 | 833 | && ENUM_UNDERLYING_TYPE (type) |
dfd7fdca | 834 | && !int_fits_type_p (val, ENUM_UNDERLYING_TYPE (type))) |
5a3c9cf2 PC |
835 | warning_at (loc, OPT_Wconversion, |
836 | "the result of the conversion is unspecified because " | |
837 | "%qE is outside the range of type %qT", | |
838 | expr, type); | |
8d08fdba | 839 | } |
9e1e64ec | 840 | if (MAYBE_CLASS_TYPE_P (intype)) |
8d08fdba MS |
841 | { |
842 | tree rval; | |
7993382e | 843 | rval = build_type_conversion (type, e); |
00595019 MS |
844 | if (rval) |
845 | return rval; | |
4b978f96 | 846 | if (complain & tf_error) |
5a3c9cf2 | 847 | error_at (loc, "%q#T used where a %qT was expected", intype, type); |
8d08fdba MS |
848 | return error_mark_node; |
849 | } | |
2986ae00 | 850 | if (code == BOOLEAN_TYPE) |
1ee44b26 | 851 | { |
50e10fa8 | 852 | if (VOID_TYPE_P (intype)) |
5a3c9cf2 | 853 | { |
4b978f96 PC |
854 | if (complain & tf_error) |
855 | error_at (loc, | |
856 | "could not convert %qE from %<void%> to %<bool%>", | |
857 | expr); | |
5a3c9cf2 PC |
858 | return error_mark_node; |
859 | } | |
860 | ||
6f1e9668 RS |
861 | if (VECTOR_TYPE_P (intype) && !gnu_vector_type_p (intype)) |
862 | { | |
863 | if (complain & tf_error) | |
864 | error_at (loc, "could not convert %qE from %qH to %qI", expr, | |
865 | TREE_TYPE (expr), type); | |
866 | return error_mark_node; | |
867 | } | |
868 | ||
1ee44b26 JM |
869 | /* We can't implicitly convert a scoped enum to bool, so convert |
870 | to the underlying type first. */ | |
871 | if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC)) | |
55b13820 | 872 | e = build_nop (ENUM_UNDERLYING_TYPE (intype), e); |
3799a5b8 | 873 | if (complain & tf_warning) |
2ab340fe | 874 | return cp_truthvalue_conversion (e, complain); |
3799a5b8 MP |
875 | else |
876 | { | |
877 | /* Prevent bogus -Wint-in-bool-context warnings coming | |
878 | from c_common_truthvalue_conversion down the line. */ | |
879 | warning_sentinel w (warn_int_in_bool_context); | |
54bf2539 | 880 | warning_sentinel c (warn_sign_compare); |
2ab340fe | 881 | return cp_truthvalue_conversion (e, complain); |
3799a5b8 | 882 | } |
1ee44b26 | 883 | } |
1998463c | 884 | |
415594bb | 885 | converted = convert_to_integer_maybe_fold (type, e, dofold); |
9e115cec JM |
886 | |
887 | /* Ignore any integer overflow caused by the conversion. */ | |
888 | return ignore_overflows (converted, e); | |
8d08fdba | 889 | } |
71a93b08 | 890 | if (INDIRECT_TYPE_P (type) || TYPE_PTRMEM_P (type)) |
415594bb | 891 | return cp_convert_to_pointer (type, e, dofold, complain); |
bb37c4a5 | 892 | if (code == VECTOR_TYPE) |
037cc9c5 FJ |
893 | { |
894 | tree in_vtype = TREE_TYPE (e); | |
9e1e64ec | 895 | if (MAYBE_CLASS_TYPE_P (in_vtype)) |
037cc9c5 FJ |
896 | { |
897 | tree ret_val; | |
898 | ret_val = build_type_conversion (type, e); | |
0cbd7506 MS |
899 | if (ret_val) |
900 | return ret_val; | |
4b978f96 PC |
901 | if (complain & tf_error) |
902 | error_at (loc, "%q#T used where a %qT was expected", | |
903 | in_vtype, type); | |
0cbd7506 | 904 | return error_mark_node; |
037cc9c5 | 905 | } |
96bea935 | 906 | return convert_to_vector (type, rvalue (e)); |
037cc9c5 | 907 | } |
37c46b43 | 908 | if (code == REAL_TYPE || code == COMPLEX_TYPE) |
8d08fdba | 909 | { |
9e1e64ec | 910 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e))) |
8d08fdba MS |
911 | { |
912 | tree rval; | |
7993382e | 913 | rval = build_type_conversion (type, e); |
8d08fdba MS |
914 | if (rval) |
915 | return rval; | |
4b978f96 PC |
916 | else if (complain & tf_error) |
917 | error_at (loc, | |
0ecf545c | 918 | "%q#T used where a floating-point value was expected", |
4b978f96 | 919 | TREE_TYPE (e)); |
8d08fdba | 920 | } |
37c46b43 | 921 | if (code == REAL_TYPE) |
415594bb | 922 | return convert_to_real_maybe_fold (type, e, dofold); |
37c46b43 | 923 | else if (code == COMPLEX_TYPE) |
415594bb | 924 | return convert_to_complex_maybe_fold (type, e, dofold); |
8d08fdba MS |
925 | } |
926 | ||
927 | /* New C++ semantics: since assignment is now based on | |
928 | memberwise copying, if the rhs type is derived from the | |
929 | lhs type, then we may still do a conversion. */ | |
9e1e64ec | 930 | if (RECORD_OR_UNION_CODE_P (code)) |
8d08fdba MS |
931 | { |
932 | tree dtype = TREE_TYPE (e); | |
db5ae43f | 933 | tree ctor = NULL_TREE; |
8d08fdba | 934 | |
8d08fdba MS |
935 | dtype = TYPE_MAIN_VARIANT (dtype); |
936 | ||
8d08fdba MS |
937 | /* Conversion between aggregate types. New C++ semantics allow |
938 | objects of derived type to be cast to objects of base type. | |
939 | Old semantics only allowed this between pointers. | |
940 | ||
941 | There may be some ambiguity between using a constructor | |
942 | vs. using a type conversion operator when both apply. */ | |
943 | ||
277294d7 | 944 | ctor = e; |
faf5394a | 945 | |
c17fa0f2 | 946 | if (abstract_virtuals_error (NULL_TREE, type, complain)) |
a7a64a77 | 947 | return error_mark_node; |
59e76fc6 | 948 | |
09357846 | 949 | if (BRACE_ENCLOSED_INITIALIZER_P (ctor)) |
4b978f96 | 950 | ctor = perform_implicit_conversion (type, ctor, complain); |
09357846 JM |
951 | else if ((flags & LOOKUP_ONLYCONVERTING) |
952 | && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype))) | |
62c154ed JM |
953 | /* For copy-initialization, first we create a temp of the proper type |
954 | with a user-defined conversion sequence, then we direct-initialize | |
955 | the target with the temp (see [dcl.init]). */ | |
4b978f96 | 956 | ctor = build_user_type_conversion (type, ctor, flags, complain); |
5e818b93 | 957 | else |
c166b898 | 958 | { |
cd9cf97b | 959 | releasing_vec ctor_vec (make_tree_vector_single (ctor)); |
c166b898 ILT |
960 | ctor = build_special_member_call (NULL_TREE, |
961 | complete_ctor_identifier, | |
962 | &ctor_vec, | |
4b978f96 | 963 | type, flags, complain); |
c166b898 | 964 | } |
277294d7 | 965 | if (ctor) |
4b978f96 | 966 | return build_cplus_new (type, ctor, complain); |
8d08fdba MS |
967 | } |
968 | ||
4b978f96 | 969 | if (complain & tf_error) |
111a28c2 DS |
970 | { |
971 | /* If the conversion failed and expr was an invalid use of pointer to | |
972 | member function, try to report a meaningful error. */ | |
d3ea4c06 | 973 | if (invalid_nonstatic_memfn_p (loc, expr, complain)) |
111a28c2 DS |
974 | /* We displayed the error message. */; |
975 | else | |
f012c8ef | 976 | error_at (loc, "conversion from %qH to non-scalar type %qI requested", |
5a3c9cf2 | 977 | TREE_TYPE (expr), type); |
111a28c2 | 978 | } |
8d08fdba MS |
979 | return error_mark_node; |
980 | } | |
981 | ||
babaa9df JM |
982 | /* If CALL is a call, return the callee; otherwise null. */ |
983 | ||
984 | tree | |
985 | cp_get_callee (tree call) | |
986 | { | |
987 | if (call == NULL_TREE) | |
988 | return call; | |
989 | else if (TREE_CODE (call) == CALL_EXPR) | |
990 | return CALL_EXPR_FN (call); | |
991 | else if (TREE_CODE (call) == AGGR_INIT_EXPR) | |
992 | return AGGR_INIT_EXPR_FN (call); | |
993 | return NULL_TREE; | |
994 | } | |
995 | ||
b632761d JM |
996 | /* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL |
997 | if we can. */ | |
998 | ||
999 | tree | |
f5f035a3 | 1000 | cp_get_fndecl_from_callee (tree fn, bool fold /* = true */) |
b632761d JM |
1001 | { |
1002 | if (fn == NULL_TREE) | |
1003 | return fn; | |
8c906382 MP |
1004 | |
1005 | /* We evaluate constexpr functions on the original, pre-genericization | |
1006 | bodies. So block-scope extern declarations have not been mapped to | |
1007 | declarations in outer scopes. Use the namespace-scope declaration, | |
1008 | if any, so that retrieve_constexpr_fundef can find it (PR111132). */ | |
1009 | auto fn_or_local_alias = [] (tree f) | |
1010 | { | |
1011 | if (DECL_LOCAL_DECL_P (f)) | |
1012 | if (tree alias = DECL_LOCAL_DECL_ALIAS (f)) | |
1013 | if (alias != error_mark_node) | |
1014 | return alias; | |
1015 | return f; | |
1016 | }; | |
1017 | ||
b632761d | 1018 | if (TREE_CODE (fn) == FUNCTION_DECL) |
8c906382 | 1019 | return fn_or_local_alias (fn); |
b632761d | 1020 | tree type = TREE_TYPE (fn); |
bf732686 | 1021 | if (type == NULL_TREE || !INDIRECT_TYPE_P (type)) |
b632761d | 1022 | return NULL_TREE; |
f5f035a3 JM |
1023 | if (fold) |
1024 | fn = maybe_constant_init (fn); | |
b632761d | 1025 | STRIP_NOPS (fn); |
e6321c45 JM |
1026 | if (TREE_CODE (fn) == ADDR_EXPR |
1027 | || TREE_CODE (fn) == FDESC_EXPR) | |
1028 | fn = TREE_OPERAND (fn, 0); | |
1029 | if (TREE_CODE (fn) == FUNCTION_DECL) | |
8c906382 | 1030 | return fn_or_local_alias (fn); |
b632761d JM |
1031 | return NULL_TREE; |
1032 | } | |
1033 | ||
1034 | /* Like get_callee_fndecl, but handles AGGR_INIT_EXPR as well and uses the | |
1035 | constexpr machinery. */ | |
1036 | ||
1037 | tree | |
1038 | cp_get_callee_fndecl (tree call) | |
1039 | { | |
1040 | return cp_get_fndecl_from_callee (cp_get_callee (call)); | |
1041 | } | |
1042 | ||
f5f035a3 JM |
1043 | /* As above, but not using the constexpr machinery. */ |
1044 | ||
1045 | tree | |
1046 | cp_get_callee_fndecl_nofold (tree call) | |
1047 | { | |
1048 | return cp_get_fndecl_from_callee (cp_get_callee (call), false); | |
1049 | } | |
1050 | ||
b632761d JM |
1051 | /* Subroutine of convert_to_void. Warn if we're discarding something with |
1052 | attribute [[nodiscard]]. */ | |
1053 | ||
1054 | static void | |
1055 | maybe_warn_nodiscard (tree expr, impl_conv_void implicit) | |
1056 | { | |
c3a63fb3 JM |
1057 | if (!warn_unused_result || c_inhibit_evaluation_warnings) |
1058 | return; | |
1059 | ||
b632761d JM |
1060 | tree call = expr; |
1061 | if (TREE_CODE (expr) == TARGET_EXPR) | |
1062 | call = TARGET_EXPR_INITIAL (expr); | |
f9d0ca40 | 1063 | location_t loc = cp_expr_loc_or_input_loc (call); |
b632761d | 1064 | tree callee = cp_get_callee (call); |
dad31187 | 1065 | if (!callee || !TREE_TYPE (callee)) |
b632761d JM |
1066 | return; |
1067 | ||
1068 | tree type = TREE_TYPE (callee); | |
1069 | if (TYPE_PTRMEMFUNC_P (type)) | |
1070 | type = TYPE_PTRMEMFUNC_FN_TYPE (type); | |
71a93b08 | 1071 | if (INDIRECT_TYPE_P (type)) |
b632761d | 1072 | type = TREE_TYPE (type); |
dad31187 PP |
1073 | if (!FUNC_OR_METHOD_TYPE_P (type)) |
1074 | return; | |
b632761d JM |
1075 | |
1076 | tree rettype = TREE_TYPE (type); | |
1077 | tree fn = cp_get_fndecl_from_callee (callee); | |
8ad0c477 | 1078 | tree attr; |
b632761d | 1079 | if (implicit != ICV_CAST && fn |
8ad0c477 | 1080 | && (attr = lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn)))) |
b632761d | 1081 | { |
8ad0c477 JM |
1082 | escaped_string msg; |
1083 | tree args = TREE_VALUE (attr); | |
1084 | if (args) | |
1085 | msg.escape (TREE_STRING_POINTER (TREE_VALUE (args))); | |
5544dbeb JJ |
1086 | const char *format |
1087 | = (msg | |
1088 | ? G_("ignoring return value of %qD, " | |
1089 | "declared with attribute %<nodiscard%>: %<%s%>") | |
1090 | : G_("ignoring return value of %qD, " | |
1091 | "declared with attribute %<nodiscard%>%s")); | |
1092 | const char *raw_msg = msg ? (const char *) msg : ""; | |
097f82ec | 1093 | auto_diagnostic_group d; |
8ad0c477 | 1094 | if (warning_at (loc, OPT_Wunused_result, format, fn, raw_msg)) |
b632761d JM |
1095 | inform (DECL_SOURCE_LOCATION (fn), "declared here"); |
1096 | } | |
1097 | else if (implicit != ICV_CAST | |
8ad0c477 | 1098 | && (attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype)))) |
b632761d | 1099 | { |
8ad0c477 JM |
1100 | escaped_string msg; |
1101 | tree args = TREE_VALUE (attr); | |
1102 | if (args) | |
1103 | msg.escape (TREE_STRING_POINTER (TREE_VALUE (args))); | |
5544dbeb JJ |
1104 | const char *format |
1105 | = (msg | |
1106 | ? G_("ignoring returned value of type %qT, " | |
1107 | "declared with attribute %<nodiscard%>: %<%s%>") | |
1108 | : G_("ignoring returned value of type %qT, " | |
1109 | "declared with attribute %<nodiscard%>%s")); | |
1110 | const char *raw_msg = msg ? (const char *) msg : ""; | |
097f82ec | 1111 | auto_diagnostic_group d; |
8ad0c477 | 1112 | if (warning_at (loc, OPT_Wunused_result, format, rettype, raw_msg)) |
b632761d JM |
1113 | { |
1114 | if (fn) | |
1115 | inform (DECL_SOURCE_LOCATION (fn), | |
1116 | "in call to %qD, declared here", fn); | |
1117 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)), | |
1118 | "%qT declared here", rettype); | |
1119 | } | |
1120 | } | |
1121 | else if (TREE_CODE (expr) == TARGET_EXPR | |
1122 | && lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (type))) | |
1123 | { | |
1124 | /* The TARGET_EXPR confuses do_warn_unused_result into thinking that the | |
1125 | result is used, so handle that case here. */ | |
1126 | if (fn) | |
1127 | { | |
097f82ec | 1128 | auto_diagnostic_group d; |
b632761d JM |
1129 | if (warning_at (loc, OPT_Wunused_result, |
1130 | "ignoring return value of %qD, " | |
a9c697b8 | 1131 | "declared with attribute %<warn_unused_result%>", |
b632761d JM |
1132 | fn)) |
1133 | inform (DECL_SOURCE_LOCATION (fn), "declared here"); | |
1134 | } | |
1135 | else | |
1136 | warning_at (loc, OPT_Wunused_result, | |
1137 | "ignoring return value of function " | |
a9c697b8 | 1138 | "declared with attribute %<warn_unused_result%>"); |
b632761d JM |
1139 | } |
1140 | } | |
1141 | ||
02cac427 NS |
1142 | /* When an expression is used in a void context, its value is discarded and |
1143 | no lvalue-rvalue and similar conversions happen [expr.static.cast/4, | |
1144 | stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type | |
1145 | in a void context. The C++ standard does not define what an `access' to an | |
cd0be382 | 1146 | object is, but there is reason to believe that it is the lvalue to rvalue |
02cac427 NS |
1147 | conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it |
1148 | accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8 | |
1149 | indicates that volatile semantics should be the same between C and C++ | |
1150 | where ever possible. C leaves it implementation defined as to what | |
1151 | constitutes an access to a volatile. So, we interpret `*vp' as a read of | |
1152 | the volatile object `vp' points to, unless that is an incomplete type. For | |
1153 | volatile references we do not do this interpretation, because that would | |
1154 | make it impossible to ignore the reference return value from functions. We | |
1155 | issue warnings in the confusing cases. | |
c8094d83 | 1156 | |
ebeb2c24 SZ |
1157 | The IMPLICIT is ICV_CAST when the user is explicitly converting an expression |
1158 | to void via a cast. If an expression is being implicitly converted, IMPLICIT | |
1159 | indicates the context of the implicit conversion. */ | |
02cac427 NS |
1160 | |
1161 | tree | |
ebeb2c24 | 1162 | convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain) |
02cac427 | 1163 | { |
f9d0ca40 | 1164 | location_t loc = cp_expr_loc_or_input_loc (expr); |
5a3c9cf2 | 1165 | |
c8094d83 | 1166 | if (expr == error_mark_node |
07c88314 MM |
1167 | || TREE_TYPE (expr) == error_mark_node) |
1168 | return error_mark_node; | |
056928b2 | 1169 | |
38946ea1 JM |
1170 | expr = maybe_undo_parenthesized_ref (expr); |
1171 | ||
84dd815f | 1172 | expr = mark_discarded_use (expr); |
ebeb2c24 | 1173 | if (implicit == ICV_CAST) |
84dd815f | 1174 | /* An explicit cast to void avoids all -Wunused-but-set* warnings. */ |
9fc8dacc | 1175 | mark_exp_read (expr); |
056928b2 | 1176 | |
02cac427 NS |
1177 | if (!TREE_TYPE (expr)) |
1178 | return expr; | |
d3ea4c06 | 1179 | if (invalid_nonstatic_memfn_p (loc, expr, complain)) |
c8b2e872 | 1180 | return error_mark_node; |
9f4faeae MM |
1181 | if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) |
1182 | { | |
5ade1ed2 | 1183 | if (complain & tf_error) |
5a3c9cf2 | 1184 | error_at (loc, "pseudo-destructor is not called"); |
9f4faeae MM |
1185 | return error_mark_node; |
1186 | } | |
861d4af8 AS |
1187 | |
1188 | /* Explicitly evaluate void-converted concept checks since their | |
1189 | satisfaction may produce ill-formed programs. */ | |
1190 | if (concept_check_p (expr)) | |
662ef5b5 | 1191 | expr = evaluate_concept_check (expr); |
861d4af8 | 1192 | |
b72801e2 | 1193 | if (VOID_TYPE_P (TREE_TYPE (expr))) |
02cac427 NS |
1194 | return expr; |
1195 | switch (TREE_CODE (expr)) | |
1196 | { | |
1197 | case COND_EXPR: | |
1198 | { | |
0cbd7506 MS |
1199 | /* The two parts of a cond expr might be separate lvalues. */ |
1200 | tree op1 = TREE_OPERAND (expr,1); | |
1201 | tree op2 = TREE_OPERAND (expr,2); | |
cb8384a3 JM |
1202 | bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1)) |
1203 | || TREE_SIDE_EFFECTS (op2)); | |
ebeb2c24 | 1204 | tree new_op1, new_op2; |
cb8384a3 | 1205 | new_op1 = NULL_TREE; |
ebeb2c24 SZ |
1206 | if (implicit != ICV_CAST && !side_effects) |
1207 | { | |
cb8384a3 JM |
1208 | if (op1) |
1209 | new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain); | |
ebeb2c24 SZ |
1210 | new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain); |
1211 | } | |
1212 | else | |
1213 | { | |
cb8384a3 JM |
1214 | if (op1) |
1215 | new_op1 = convert_to_void (op1, ICV_CAST, complain); | |
ebeb2c24 SZ |
1216 | new_op2 = convert_to_void (op2, ICV_CAST, complain); |
1217 | } | |
c8094d83 | 1218 | |
660eb7e9 JJ |
1219 | expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2), |
1220 | TREE_OPERAND (expr, 0), new_op1, new_op2); | |
0cbd7506 | 1221 | break; |
02cac427 | 1222 | } |
c8094d83 | 1223 | |
02cac427 NS |
1224 | case COMPOUND_EXPR: |
1225 | { | |
0cbd7506 MS |
1226 | /* The second part of a compound expr contains the value. */ |
1227 | tree op1 = TREE_OPERAND (expr,1); | |
ebeb2c24 | 1228 | tree new_op1; |
65870e75 | 1229 | if (implicit != ICV_CAST && !warning_suppressed_p (expr /* What warning? */)) |
ebeb2c24 SZ |
1230 | new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain); |
1231 | else | |
1232 | new_op1 = convert_to_void (op1, ICV_CAST, complain); | |
c8094d83 | 1233 | |
0cbd7506 | 1234 | if (new_op1 != op1) |
9a52d09b | 1235 | { |
660eb7e9 JJ |
1236 | tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1), |
1237 | TREE_OPERAND (expr, 0), new_op1); | |
9a52d09b MM |
1238 | expr = t; |
1239 | } | |
1240 | ||
0cbd7506 | 1241 | break; |
02cac427 | 1242 | } |
c8094d83 | 1243 | |
02cac427 NS |
1244 | case NON_LVALUE_EXPR: |
1245 | case NOP_EXPR: | |
00a17e31 | 1246 | /* These have already decayed to rvalue. */ |
02cac427 | 1247 | break; |
c8094d83 | 1248 | |
f4f206f4 | 1249 | case CALL_EXPR: /* We have a special meaning for volatile void fn(). */ |
b25a3756 AO |
1250 | /* cdtors may return this or void, depending on |
1251 | targetm.cxx.cdtor_returns_this, but this shouldn't affect our | |
c9816196 JJ |
1252 | decisions here: neither nodiscard warnings (nodiscard dtors |
1253 | are nonsensical and ctors have a different behavior with that | |
1254 | attribute that is handled in the TARGET_EXPR case), nor should | |
1255 | any constexpr or template instantiations be affected by an ABI | |
1256 | property that is, or at least ought to be transparent to the | |
1257 | language. */ | |
b25a3756 | 1258 | if (tree fn = cp_get_callee_fndecl_nofold (expr)) |
c9816196 | 1259 | if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn)) |
b25a3756 AO |
1260 | return expr; |
1261 | ||
7b521fbd MP |
1262 | if (complain & tf_warning) |
1263 | maybe_warn_nodiscard (expr, implicit); | |
02cac427 | 1264 | break; |
c8094d83 | 1265 | |
02cac427 NS |
1266 | case INDIRECT_REF: |
1267 | { | |
0cbd7506 | 1268 | tree type = TREE_TYPE (expr); |
9f613f06 | 1269 | int is_reference = TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))); |
0cbd7506 | 1270 | int is_volatile = TYPE_VOLATILE (type); |
b510b83a PP |
1271 | if (is_volatile) |
1272 | complete_type (type); | |
1273 | int is_complete = COMPLETE_TYPE_P (type); | |
0cbd7506 | 1274 | |
bed02d89 | 1275 | /* Can't load the value if we don't know the type. */ |
0cbd7506 | 1276 | if (is_volatile && !is_complete) |
5ade1ed2 DG |
1277 | { |
1278 | if (complain & tf_warning) | |
ebeb2c24 SZ |
1279 | switch (implicit) |
1280 | { | |
1281 | case ICV_CAST: | |
5a3c9cf2 | 1282 | warning_at (loc, 0, "conversion to void will not access " |
ebeb2c24 SZ |
1283 | "object of incomplete type %qT", type); |
1284 | break; | |
1285 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 | 1286 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1287 | "incomplete type %qT in second operand " |
1288 | "of conditional expression", type); | |
1289 | break; | |
1290 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 | 1291 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1292 | "incomplete type %qT in third operand " |
1293 | "of conditional expression", type); | |
1294 | break; | |
1295 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 | 1296 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1297 | "incomplete type %qT in right operand of " |
1298 | "comma operator", type); | |
1299 | break; | |
1300 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 | 1301 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1302 | "incomplete type %qT in left operand of " |
1303 | "comma operator", type); | |
1304 | break; | |
1305 | case ICV_STATEMENT: | |
5a3c9cf2 | 1306 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1307 | "incomplete type %qT in statement", type); |
1308 | break; | |
1309 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 | 1310 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1311 | "incomplete type %qT in for increment " |
1312 | "expression", type); | |
1313 | break; | |
1314 | default: | |
1315 | gcc_unreachable (); | |
1316 | } | |
5ade1ed2 | 1317 | } |
bed02d89 JM |
1318 | /* Don't load the value if this is an implicit dereference, or if |
1319 | the type needs to be handled by ctors/dtors. */ | |
ebeb2c24 | 1320 | else if (is_volatile && is_reference) |
5ade1ed2 DG |
1321 | { |
1322 | if (complain & tf_warning) | |
ebeb2c24 SZ |
1323 | switch (implicit) |
1324 | { | |
1325 | case ICV_CAST: | |
5a3c9cf2 | 1326 | warning_at (loc, 0, "conversion to void will not access " |
ebeb2c24 SZ |
1327 | "object of type %qT", type); |
1328 | break; | |
1329 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 PC |
1330 | warning_at (loc, 0, "implicit dereference will not access " |
1331 | "object of type %qT in second operand of " | |
ebeb2c24 SZ |
1332 | "conditional expression", type); |
1333 | break; | |
1334 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 PC |
1335 | warning_at (loc, 0, "implicit dereference will not access " |
1336 | "object of type %qT in third operand of " | |
ebeb2c24 SZ |
1337 | "conditional expression", type); |
1338 | break; | |
1339 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 PC |
1340 | warning_at (loc, 0, "implicit dereference will not access " |
1341 | "object of type %qT in right operand of " | |
ebeb2c24 SZ |
1342 | "comma operator", type); |
1343 | break; | |
1344 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 PC |
1345 | warning_at (loc, 0, "implicit dereference will not access " |
1346 | "object of type %qT in left operand of comma " | |
1347 | "operator", type); | |
ebeb2c24 SZ |
1348 | break; |
1349 | case ICV_STATEMENT: | |
5a3c9cf2 PC |
1350 | warning_at (loc, 0, "implicit dereference will not access " |
1351 | "object of type %qT in statement", type); | |
ebeb2c24 SZ |
1352 | break; |
1353 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 PC |
1354 | warning_at (loc, 0, "implicit dereference will not access " |
1355 | "object of type %qT in for increment expression", | |
1356 | type); | |
ebeb2c24 SZ |
1357 | break; |
1358 | default: | |
1359 | gcc_unreachable (); | |
1360 | } | |
5ade1ed2 | 1361 | } |
ebeb2c24 SZ |
1362 | else if (is_volatile && TREE_ADDRESSABLE (type)) |
1363 | { | |
1364 | if (complain & tf_warning) | |
1365 | switch (implicit) | |
1366 | { | |
1367 | case ICV_CAST: | |
5a3c9cf2 | 1368 | warning_at (loc, 0, "conversion to void will not access " |
ebeb2c24 | 1369 | "object of non-trivially-copyable type %qT", |
5a3c9cf2 | 1370 | type); |
ebeb2c24 SZ |
1371 | break; |
1372 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 | 1373 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1374 | "non-trivially-copyable type %qT in second " |
1375 | "operand of conditional expression", type); | |
1376 | break; | |
1377 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 | 1378 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1379 | "non-trivially-copyable type %qT in third " |
1380 | "operand of conditional expression", type); | |
1381 | break; | |
1382 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 | 1383 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1384 | "non-trivially-copyable type %qT in right " |
1385 | "operand of comma operator", type); | |
1386 | break; | |
1387 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 | 1388 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1389 | "non-trivially-copyable type %qT in left " |
1390 | "operand of comma operator", type); | |
1391 | break; | |
1392 | case ICV_STATEMENT: | |
5a3c9cf2 | 1393 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 | 1394 | "non-trivially-copyable type %qT in statement", |
5a3c9cf2 | 1395 | type); |
ebeb2c24 SZ |
1396 | break; |
1397 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 | 1398 | warning_at (loc, 0, "indirection will not access object of " |
ebeb2c24 SZ |
1399 | "non-trivially-copyable type %qT in for " |
1400 | "increment expression", type); | |
1401 | break; | |
1402 | default: | |
1403 | gcc_unreachable (); | |
1404 | } | |
1405 | } | |
bed02d89 | 1406 | if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type)) |
041d7a27 LCW |
1407 | { |
1408 | /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF | |
1409 | operation is stripped off. Note that we don't warn about | |
1410 | - an expression with TREE_NO_WARNING set. (For an example of | |
e53b6e56 | 1411 | such expressions, see build_over_call in call.cc.) |
041d7a27 | 1412 | - automatic dereferencing of references, since the user cannot |
e53b6e56 | 1413 | control it. (See also warn_if_unused_value() in c-common.cc.) */ |
041d7a27 | 1414 | if (warn_unused_value |
ebeb2c24 | 1415 | && implicit != ICV_CAST |
041d7a27 | 1416 | && (complain & tf_warning) |
65870e75 | 1417 | && !warning_suppressed_p (expr, OPT_Wunused_value) |
041d7a27 | 1418 | && !is_reference) |
5a3c9cf2 | 1419 | warning_at (loc, OPT_Wunused_value, "value computed is not used"); |
041d7a27 | 1420 | expr = TREE_OPERAND (expr, 0); |
7b521fbd MP |
1421 | if (TREE_CODE (expr) == CALL_EXPR |
1422 | && (complain & tf_warning)) | |
ac853c90 | 1423 | maybe_warn_nodiscard (expr, implicit); |
041d7a27 | 1424 | } |
0cbd7506 MS |
1425 | |
1426 | break; | |
02cac427 | 1427 | } |
c8094d83 | 1428 | |
02cac427 NS |
1429 | case VAR_DECL: |
1430 | { | |
0cbd7506 MS |
1431 | /* External variables might be incomplete. */ |
1432 | tree type = TREE_TYPE (expr); | |
0cbd7506 | 1433 | |
b510b83a PP |
1434 | if (TYPE_VOLATILE (type) |
1435 | && !COMPLETE_TYPE_P (complete_type (type)) | |
1436 | && (complain & tf_warning)) | |
ebeb2c24 SZ |
1437 | switch (implicit) |
1438 | { | |
1439 | case ICV_CAST: | |
5a3c9cf2 | 1440 | warning_at (loc, 0, "conversion to void will not access " |
ebeb2c24 SZ |
1441 | "object %qE of incomplete type %qT", expr, type); |
1442 | break; | |
1443 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 PC |
1444 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1445 | "not be accessed in second operand of " | |
ebeb2c24 SZ |
1446 | "conditional expression", expr, type); |
1447 | break; | |
1448 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 PC |
1449 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1450 | "not be accessed in third operand of " | |
ebeb2c24 SZ |
1451 | "conditional expression", expr, type); |
1452 | break; | |
1453 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 PC |
1454 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1455 | "not be accessed in right operand of comma operator", | |
1456 | expr, type); | |
ebeb2c24 SZ |
1457 | break; |
1458 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 PC |
1459 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1460 | "not be accessed in left operand of comma operator", | |
1461 | expr, type); | |
ebeb2c24 SZ |
1462 | break; |
1463 | case ICV_STATEMENT: | |
5a3c9cf2 PC |
1464 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1465 | "not be accessed in statement", expr, type); | |
ebeb2c24 SZ |
1466 | break; |
1467 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 PC |
1468 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1469 | "not be accessed in for increment expression", | |
1470 | expr, type); | |
ebeb2c24 SZ |
1471 | break; |
1472 | default: | |
1473 | gcc_unreachable (); | |
1474 | } | |
1475 | ||
0cbd7506 | 1476 | break; |
02cac427 | 1477 | } |
2bdb0643 | 1478 | |
c08cd4c1 JM |
1479 | case TARGET_EXPR: |
1480 | /* Don't bother with the temporary object returned from a function if | |
45177337 JM |
1481 | we don't use it, don't need to destroy it, and won't abort in |
1482 | assign_temp. We'll still | |
c08cd4c1 JM |
1483 | allocate space for it in expand_call or declare_return_variable, |
1484 | but we don't need to track it through all the tree phases. */ | |
5de1a1eb | 1485 | if (TARGET_EXPR_IMPLICIT_P (expr) |
45177337 | 1486 | && !TREE_ADDRESSABLE (TREE_TYPE (expr))) |
c08cd4c1 JM |
1487 | { |
1488 | tree init = TARGET_EXPR_INITIAL (expr); | |
1489 | if (TREE_CODE (init) == AGGR_INIT_EXPR | |
1490 | && !AGGR_INIT_VIA_CTOR_P (init)) | |
1491 | { | |
5039610b | 1492 | tree fn = AGGR_INIT_EXPR_FN (init); |
db3927fb | 1493 | expr = build_call_array_loc (input_location, |
b632761d JM |
1494 | TREE_TYPE (TREE_TYPE |
1495 | (TREE_TYPE (fn))), | |
db3927fb AH |
1496 | fn, |
1497 | aggr_init_expr_nargs (init), | |
1498 | AGGR_INIT_EXPR_ARGP (init)); | |
c08cd4c1 JM |
1499 | } |
1500 | } | |
7b521fbd MP |
1501 | if (complain & tf_warning) |
1502 | maybe_warn_nodiscard (expr, implicit); | |
c08cd4c1 JM |
1503 | break; |
1504 | ||
2664c1bf AA |
1505 | case CO_AWAIT_EXPR: |
1506 | { | |
1507 | auto awr = co_await_get_resume_call (expr); | |
1508 | if (awr && *awr) | |
1509 | *awr = convert_to_void (*awr, implicit, complain); | |
1510 | break; | |
1511 | } | |
1512 | ||
02cac427 NS |
1513 | default:; |
1514 | } | |
46f0d909 | 1515 | expr = resolve_nondeduced_context (expr, complain); |
c352ef0e JM |
1516 | if (!mark_single_function (expr, complain)) |
1517 | return error_mark_node; | |
1518 | ||
02cac427 NS |
1519 | { |
1520 | tree probe = expr; | |
c8094d83 | 1521 | |
02cac427 NS |
1522 | if (TREE_CODE (probe) == ADDR_EXPR) |
1523 | probe = TREE_OPERAND (expr, 0); | |
96d6c610 JM |
1524 | if (type_unknown_p (probe)) |
1525 | { | |
1526 | /* [over.over] enumerates the places where we can take the address | |
1527 | of an overloaded function, and this is not one of them. */ | |
5ade1ed2 | 1528 | if (complain & tf_error) |
ebeb2c24 SZ |
1529 | switch (implicit) |
1530 | { | |
1531 | case ICV_CAST: | |
5a3c9cf2 PC |
1532 | error_at (loc, "conversion to void " |
1533 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1534 | break; |
1535 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 PC |
1536 | error_at (loc, "second operand of conditional expression " |
1537 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1538 | break; |
1539 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 PC |
1540 | error_at (loc, "third operand of conditional expression " |
1541 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1542 | break; |
1543 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 PC |
1544 | error_at (loc, "right operand of comma operator " |
1545 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1546 | break; |
1547 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 PC |
1548 | error_at (loc, "left operand of comma operator " |
1549 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1550 | break; |
1551 | case ICV_STATEMENT: | |
5a3c9cf2 PC |
1552 | error_at (loc, "statement " |
1553 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1554 | break; |
1555 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 PC |
1556 | error_at (loc, "for increment expression " |
1557 | "cannot resolve address of overloaded function"); | |
ebeb2c24 SZ |
1558 | break; |
1559 | } | |
5ade1ed2 DG |
1560 | else |
1561 | return error_mark_node; | |
632f2871 | 1562 | expr = void_node; |
96d6c610 | 1563 | } |
ebeb2c24 | 1564 | else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe)) |
05f8c2d5 JM |
1565 | { |
1566 | /* Only warn when there is no &. */ | |
5ade1ed2 | 1567 | if (complain & tf_warning) |
ebeb2c24 SZ |
1568 | switch (implicit) |
1569 | { | |
1570 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 PC |
1571 | warning_at (loc, OPT_Waddress, |
1572 | "second operand of conditional expression " | |
1573 | "is a reference, not call, to function %qE", expr); | |
ebeb2c24 SZ |
1574 | break; |
1575 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 PC |
1576 | warning_at (loc, OPT_Waddress, |
1577 | "third operand of conditional expression " | |
1578 | "is a reference, not call, to function %qE", expr); | |
ebeb2c24 SZ |
1579 | break; |
1580 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 PC |
1581 | warning_at (loc, OPT_Waddress, |
1582 | "right operand of comma operator " | |
1583 | "is a reference, not call, to function %qE", expr); | |
ebeb2c24 SZ |
1584 | break; |
1585 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 PC |
1586 | warning_at (loc, OPT_Waddress, |
1587 | "left operand of comma operator " | |
1588 | "is a reference, not call, to function %qE", expr); | |
ebeb2c24 SZ |
1589 | break; |
1590 | case ICV_STATEMENT: | |
5a3c9cf2 PC |
1591 | warning_at (loc, OPT_Waddress, |
1592 | "statement is a reference, not call, to function %qE", | |
1593 | expr); | |
ebeb2c24 SZ |
1594 | break; |
1595 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 PC |
1596 | warning_at (loc, OPT_Waddress, |
1597 | "for increment expression " | |
1598 | "is a reference, not call, to function %qE", expr); | |
ebeb2c24 SZ |
1599 | break; |
1600 | default: | |
1601 | gcc_unreachable (); | |
1602 | } | |
1603 | ||
05f8c2d5 JM |
1604 | if (TREE_CODE (expr) == COMPONENT_REF) |
1605 | expr = TREE_OPERAND (expr, 0); | |
1606 | } | |
02cac427 | 1607 | } |
c8094d83 | 1608 | |
b72801e2 | 1609 | if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr))) |
02cac427 | 1610 | { |
ebeb2c24 | 1611 | if (implicit != ICV_CAST |
55792875 | 1612 | && warn_unused_value |
65870e75 | 1613 | && !warning_suppressed_p (expr, OPT_Wunused_value) |
7f3db013 | 1614 | && !processing_template_decl |
0000ea4f JJ |
1615 | && !cp_unevaluated_operand |
1616 | && (complain & tf_warning)) | |
dfb5c523 MM |
1617 | { |
1618 | /* The middle end does not warn about expressions that have | |
1619 | been explicitly cast to void, so we must do so here. */ | |
0000ea4f JJ |
1620 | if (!TREE_SIDE_EFFECTS (expr)) |
1621 | { | |
ebeb2c24 SZ |
1622 | switch (implicit) |
1623 | { | |
1624 | case ICV_SECOND_OF_COND: | |
5a3c9cf2 PC |
1625 | warning_at (loc, OPT_Wunused_value, |
1626 | "second operand of conditional expression " | |
1627 | "has no effect"); | |
ebeb2c24 SZ |
1628 | break; |
1629 | case ICV_THIRD_OF_COND: | |
5a3c9cf2 PC |
1630 | warning_at (loc, OPT_Wunused_value, |
1631 | "third operand of conditional expression " | |
1632 | "has no effect"); | |
ebeb2c24 SZ |
1633 | break; |
1634 | case ICV_RIGHT_OF_COMMA: | |
5a3c9cf2 PC |
1635 | warning_at (loc, OPT_Wunused_value, |
1636 | "right operand of comma operator has no effect"); | |
ebeb2c24 SZ |
1637 | break; |
1638 | case ICV_LEFT_OF_COMMA: | |
5a3c9cf2 PC |
1639 | warning_at (loc, OPT_Wunused_value, |
1640 | "left operand of comma operator has no effect"); | |
ebeb2c24 SZ |
1641 | break; |
1642 | case ICV_STATEMENT: | |
5a3c9cf2 PC |
1643 | warning_at (loc, OPT_Wunused_value, |
1644 | "statement has no effect"); | |
ebeb2c24 SZ |
1645 | break; |
1646 | case ICV_THIRD_IN_FOR: | |
5a3c9cf2 PC |
1647 | warning_at (loc, OPT_Wunused_value, |
1648 | "for increment expression has no effect"); | |
ebeb2c24 SZ |
1649 | break; |
1650 | default: | |
1651 | gcc_unreachable (); | |
1652 | } | |
0000ea4f | 1653 | } |
c8094d83 MS |
1654 | else |
1655 | { | |
0000ea4f | 1656 | tree e = expr; |
dfb5c523 MM |
1657 | /* We might like to warn about (say) "(int) f()", as the |
1658 | cast has no effect, but the compiler itself will | |
1659 | generate implicit conversions under some | |
cc29fd59 | 1660 | circumstances. (For example a block copy will be |
dfb5c523 MM |
1661 | turned into a call to "__builtin_memcpy", with a |
1662 | conversion of the return value to an appropriate | |
1663 | type.) So, to avoid false positives, we strip | |
d9fa1233 MM |
1664 | conversions. Do not use STRIP_NOPs because it will |
1665 | not strip conversions to "void", as that is not a | |
1666 | mode-preserving conversion. */ | |
1667 | while (TREE_CODE (e) == NOP_EXPR) | |
1668 | e = TREE_OPERAND (e, 0); | |
dfb5c523 | 1669 | |
0000ea4f JJ |
1670 | enum tree_code code = TREE_CODE (e); |
1671 | enum tree_code_class tclass = TREE_CODE_CLASS (code); | |
1672 | if (tclass == tcc_comparison | |
1673 | || tclass == tcc_unary | |
1674 | || tclass == tcc_binary | |
144b6099 PP |
1675 | || code == TRUTH_NOT_EXPR |
1676 | || code == ADDR_EXPR | |
0000ea4f JJ |
1677 | || code == VEC_PERM_EXPR |
1678 | || code == VEC_COND_EXPR) | |
1679 | warn_if_unused_value (e, loc); | |
dfb5c523 MM |
1680 | } |
1681 | } | |
e895113a | 1682 | expr = build1 (CONVERT_EXPR, void_type_node, expr); |
02cac427 | 1683 | } |
ccbe00a4 | 1684 | if (! TREE_SIDE_EFFECTS (expr)) |
632f2871 | 1685 | expr = void_node; |
02cac427 NS |
1686 | return expr; |
1687 | } | |
1688 | ||
a4443a08 MS |
1689 | /* Create an expression whose value is that of EXPR, |
1690 | converted to type TYPE. The TREE_TYPE of the value | |
1691 | is always TYPE. This function implements all reasonable | |
1692 | conversions; callers should filter out those that are | |
37c46b43 MS |
1693 | not permitted by the language being compiled. |
1694 | ||
1695 | Most of this routine is from build_reinterpret_cast. | |
1696 | ||
3b426391 | 1697 | The back end cannot call cp_convert (what was convert) because |
37c46b43 MS |
1698 | conversions to/from basetypes may involve memory references |
1699 | (vbases) and adding or subtracting small values (multiple | |
1700 | inheritance), but it calls convert from the constant folding code | |
7dfa399d | 1701 | on subtrees of already built trees after it has ripped them apart. |
37c46b43 MS |
1702 | |
1703 | Also, if we ever support range variables, we'll probably also have to | |
1704 | do a little bit more work. */ | |
a4443a08 MS |
1705 | |
1706 | tree | |
b746c5dc | 1707 | convert (tree type, tree expr) |
a4443a08 | 1708 | { |
37c46b43 MS |
1709 | tree intype; |
1710 | ||
1711 | if (type == error_mark_node || expr == error_mark_node) | |
1712 | return error_mark_node; | |
1713 | ||
37c46b43 MS |
1714 | intype = TREE_TYPE (expr); |
1715 | ||
71a93b08 | 1716 | if (INDIRECT_TYPE_P (type) && INDIRECT_TYPE_P (intype)) |
cda0a029 | 1717 | return build_nop (type, expr); |
37c46b43 | 1718 | |
415594bb | 1719 | return ocp_convert (type, expr, CONV_BACKEND_CONVERT, |
4b978f96 PC |
1720 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION, |
1721 | tf_warning_or_error); | |
a4443a08 MS |
1722 | } |
1723 | ||
600dcd74 JM |
1724 | /* Like convert, but in a static initializer (called from |
1725 | convert_and_check). */ | |
1726 | ||
1727 | tree | |
1728 | convert_init (tree type, tree expr) | |
1729 | { | |
1730 | return convert (type, expr); | |
1731 | } | |
1732 | ||
37c46b43 | 1733 | /* Like cp_convert, except permit conversions to take place which |
8d08fdba MS |
1734 | are not normally allowed due to access restrictions |
1735 | (such as conversion from sub-type to private super-type). */ | |
e92cc029 | 1736 | |
8d08fdba | 1737 | tree |
4b978f96 | 1738 | convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain) |
8d08fdba | 1739 | { |
926ce8bd KH |
1740 | tree e = expr; |
1741 | enum tree_code code = TREE_CODE (type); | |
8d08fdba MS |
1742 | |
1743 | if (code == REFERENCE_TYPE) | |
cda0a029 JM |
1744 | return convert_to_reference (type, e, CONV_C_CAST, 0, |
1745 | NULL_TREE, complain); | |
8d08fdba MS |
1746 | |
1747 | if (code == POINTER_TYPE) | |
cda0a029 | 1748 | return convert_to_pointer_force (type, e, complain); |
8d08fdba | 1749 | |
e53b6e56 | 1750 | /* From typeck.cc convert_for_assignment */ |
50e10fa8 | 1751 | if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR |
8d08fdba | 1752 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) |
51c184be MS |
1753 | || integer_zerop (e) |
1754 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
8d08fdba | 1755 | && TYPE_PTRMEMFUNC_P (type)) |
08e17d9d MM |
1756 | /* compatible pointer to member functions. */ |
1757 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1, | |
4b978f96 | 1758 | /*c_cast_p=*/1, complain); |
6060a796 | 1759 | |
4b978f96 | 1760 | return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain); |
8d08fdba MS |
1761 | } |
1762 | ||
8d08fdba MS |
1763 | /* Convert an aggregate EXPR to type XTYPE. If a conversion |
1764 | exists, return the attempted conversion. This may | |
1765 | return ERROR_MARK_NODE if the conversion is not | |
1766 | allowed (references private members, etc). | |
1767 | If no conversion exists, NULL_TREE is returned. | |
1768 | ||
f30432d7 MS |
1769 | FIXME: Ambiguity checking is wrong. Should choose one by the implicit |
1770 | object parameter, or by the second standard conversion sequence if | |
1771 | that doesn't do it. This will probably wait for an overloading rewrite. | |
1772 | (jason 8/9/95) */ | |
8d08fdba | 1773 | |
ae00383b | 1774 | static tree |
7993382e | 1775 | build_type_conversion (tree xtype, tree expr) |
8d08fdba MS |
1776 | { |
1777 | /* C++: check to see if we can convert this aggregate type | |
e1cd6e56 | 1778 | into the required type. */ |
b40e334f PC |
1779 | return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL, |
1780 | tf_warning_or_error); | |
8d08fdba MS |
1781 | } |
1782 | ||
b7484fbe MS |
1783 | /* Convert the given EXPR to one of a group of types suitable for use in an |
1784 | expression. DESIRES is a combination of various WANT_* flags (q.v.) | |
b746c5dc | 1785 | which indicates which types are suitable. If COMPLAIN is true, complain |
b7484fbe | 1786 | about ambiguity; otherwise, the caller will deal with it. */ |
8d08fdba | 1787 | |
b7484fbe | 1788 | tree |
b746c5dc | 1789 | build_expr_type_conversion (int desires, tree expr, bool complain) |
8d08fdba | 1790 | { |
b7484fbe | 1791 | tree basetype = TREE_TYPE (expr); |
b370501f | 1792 | tree conv = NULL_TREE; |
02020185 | 1793 | tree winner = NULL_TREE; |
8d08fdba | 1794 | |
9a004410 | 1795 | if (null_node_p (expr) |
c8094d83 | 1796 | && (desires & WANT_INT) |
03d0f4af | 1797 | && !(desires & WANT_NULL)) |
70dc395a | 1798 | { |
620e594b | 1799 | location_t loc = |
70dc395a DS |
1800 | expansion_point_location_if_in_system_header (input_location); |
1801 | ||
1802 | warning_at (loc, OPT_Wconversion_null, | |
1803 | "converting NULL to non-pointer type"); | |
1804 | } | |
c8094d83 | 1805 | |
07c88314 MM |
1806 | if (basetype == error_mark_node) |
1807 | return error_mark_node; | |
1808 | ||
9e1e64ec | 1809 | if (! MAYBE_CLASS_TYPE_P (basetype)) |
02020185 JM |
1810 | switch (TREE_CODE (basetype)) |
1811 | { | |
1812 | case INTEGER_TYPE: | |
03d0f4af | 1813 | if ((desires & WANT_NULL) && null_ptr_cst_p (expr)) |
02020185 | 1814 | return expr; |
191816a3 | 1815 | /* fall through. */ |
02020185 JM |
1816 | |
1817 | case BOOLEAN_TYPE: | |
1818 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1819 | case ENUMERAL_TYPE: | |
1820 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1821 | case REAL_TYPE: | |
1822 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1823 | case POINTER_TYPE: | |
1824 | return (desires & WANT_POINTER) ? expr : NULL_TREE; | |
c8094d83 | 1825 | |
02020185 JM |
1826 | case FUNCTION_TYPE: |
1827 | case ARRAY_TYPE: | |
89fcabaf PC |
1828 | return (desires & WANT_POINTER) ? decay_conversion (expr, |
1829 | tf_warning_or_error) | |
0cbd7506 | 1830 | : NULL_TREE; |
4576ceaf JJ |
1831 | |
1832 | case VECTOR_TYPE: | |
6f1e9668 RS |
1833 | if (!gnu_vector_type_p (basetype)) |
1834 | return NULL_TREE; | |
1835 | /* FALLTHROUGH */ | |
1836 | case COMPLEX_TYPE: | |
1ff6b2c8 | 1837 | if ((desires & WANT_VECTOR_OR_COMPLEX) == 0) |
4576ceaf JJ |
1838 | return NULL_TREE; |
1839 | switch (TREE_CODE (TREE_TYPE (basetype))) | |
1840 | { | |
1841 | case INTEGER_TYPE: | |
1842 | case BOOLEAN_TYPE: | |
1843 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1844 | case ENUMERAL_TYPE: | |
1845 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1846 | case REAL_TYPE: | |
1847 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1848 | default: | |
1849 | return NULL_TREE; | |
1850 | } | |
1851 | ||
02020185 JM |
1852 | default: |
1853 | return NULL_TREE; | |
1854 | } | |
1855 | ||
1856 | /* The code for conversions from class type is currently only used for | |
1857 | delete expressions. Other expressions are handled by build_new_op. */ | |
309714d4 | 1858 | if (!complete_type_or_maybe_complain (basetype, expr, complain)) |
29f4ceab GB |
1859 | return error_mark_node; |
1860 | if (!TYPE_HAS_CONVERSION (basetype)) | |
02020185 JM |
1861 | return NULL_TREE; |
1862 | ||
9c7d5cae | 1863 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) |
02020185 JM |
1864 | { |
1865 | int win = 0; | |
1866 | tree candidate; | |
1867 | tree cand = TREE_VALUE (conv); | |
848bf88d | 1868 | cand = OVL_FIRST (cand); |
02020185 JM |
1869 | |
1870 | if (winner && winner == cand) | |
1871 | continue; | |
1872 | ||
e57d93c6 JM |
1873 | if (DECL_NONCONVERTING_P (cand)) |
1874 | continue; | |
1875 | ||
ee76b931 | 1876 | candidate = non_reference (TREE_TYPE (TREE_TYPE (cand))); |
02020185 JM |
1877 | |
1878 | switch (TREE_CODE (candidate)) | |
1879 | { | |
1880 | case BOOLEAN_TYPE: | |
1881 | case INTEGER_TYPE: | |
1882 | win = (desires & WANT_INT); break; | |
1883 | case ENUMERAL_TYPE: | |
1884 | win = (desires & WANT_ENUM); break; | |
1885 | case REAL_TYPE: | |
1886 | win = (desires & WANT_FLOAT); break; | |
1887 | case POINTER_TYPE: | |
1888 | win = (desires & WANT_POINTER); break; | |
1889 | ||
1ff6b2c8 | 1890 | case COMPLEX_TYPE: |
4576ceaf | 1891 | case VECTOR_TYPE: |
1ff6b2c8 | 1892 | if ((desires & WANT_VECTOR_OR_COMPLEX) == 0) |
4576ceaf JJ |
1893 | break; |
1894 | switch (TREE_CODE (TREE_TYPE (candidate))) | |
1895 | { | |
1896 | case BOOLEAN_TYPE: | |
1897 | case INTEGER_TYPE: | |
1898 | win = (desires & WANT_INT); break; | |
1899 | case ENUMERAL_TYPE: | |
1900 | win = (desires & WANT_ENUM); break; | |
1901 | case REAL_TYPE: | |
1902 | win = (desires & WANT_FLOAT); break; | |
1903 | default: | |
1904 | break; | |
1905 | } | |
1906 | break; | |
1907 | ||
02020185 | 1908 | default: |
c5918c21 JM |
1909 | /* A wildcard could be instantiated to match any desired |
1910 | type, but we can't deduce the template argument. */ | |
1911 | if (WILDCARD_TYPE_P (candidate)) | |
1912 | win = true; | |
02020185 JM |
1913 | break; |
1914 | } | |
1915 | ||
1916 | if (win) | |
1917 | { | |
c5918c21 JM |
1918 | if (TREE_CODE (cand) == TEMPLATE_DECL) |
1919 | { | |
1920 | if (complain) | |
a9c697b8 | 1921 | error ("default type conversion cannot deduce template" |
c5918c21 JM |
1922 | " argument for %qD", cand); |
1923 | return error_mark_node; | |
1924 | } | |
1925 | ||
02020185 JM |
1926 | if (winner) |
1927 | { | |
ca45eca1 JM |
1928 | tree winner_type |
1929 | = non_reference (TREE_TYPE (TREE_TYPE (winner))); | |
1930 | ||
1931 | if (!same_type_ignoring_top_level_qualifiers_p (winner_type, | |
1932 | candidate)) | |
02020185 | 1933 | { |
ca45eca1 JM |
1934 | if (complain) |
1935 | { | |
cb76fcf5 | 1936 | auto_diagnostic_group d; |
ca45eca1 JM |
1937 | error ("ambiguous default type conversion from %qT", |
1938 | basetype); | |
2d45625f PC |
1939 | inform (input_location, |
1940 | " candidate conversions include %qD and %qD", | |
1941 | winner, cand); | |
ca45eca1 JM |
1942 | } |
1943 | return error_mark_node; | |
02020185 | 1944 | } |
02020185 | 1945 | } |
ca45eca1 JM |
1946 | |
1947 | winner = cand; | |
02020185 JM |
1948 | } |
1949 | } | |
b7484fbe | 1950 | |
02020185 JM |
1951 | if (winner) |
1952 | { | |
ee76b931 | 1953 | tree type = non_reference (TREE_TYPE (TREE_TYPE (winner))); |
b40e334f PC |
1954 | return build_user_type_conversion (type, expr, LOOKUP_NORMAL, |
1955 | tf_warning_or_error); | |
8d08fdba | 1956 | } |
b7484fbe | 1957 | |
3c215895 | 1958 | return NULL_TREE; |
8d08fdba | 1959 | } |
39211cd5 | 1960 | |
e92cc029 MS |
1961 | /* Implements integral promotion (4.1) and float->double promotion. */ |
1962 | ||
39211cd5 | 1963 | tree |
b746c5dc | 1964 | type_promotes_to (tree type) |
39211cd5 | 1965 | { |
40449a90 SL |
1966 | tree promoted_type; |
1967 | ||
f30432d7 MS |
1968 | if (type == error_mark_node) |
1969 | return error_mark_node; | |
1970 | ||
39211cd5 | 1971 | type = TYPE_MAIN_VARIANT (type); |
2986ae00 | 1972 | |
40449a90 SL |
1973 | /* Check for promotions of target-defined types first. */ |
1974 | promoted_type = targetm.promoted_type (type); | |
1975 | if (promoted_type) | |
1976 | return promoted_type; | |
1977 | ||
2986ae00 MS |
1978 | /* bool always promotes to int (not unsigned), even if it's the same |
1979 | size. */ | |
66be89f0 | 1980 | if (TREE_CODE (type) == BOOLEAN_TYPE) |
2986ae00 MS |
1981 | type = integer_type_node; |
1982 | ||
1983 | /* Normally convert enums to int, but convert wide enums to something | |
62984918 JM |
1984 | wider. Scoped enums don't promote, but pretend they do for backward |
1985 | ABI bug compatibility wrt varargs. */ | |
2986ae00 | 1986 | else if (TREE_CODE (type) == ENUMERAL_TYPE |
2d91f79d | 1987 | || type == char8_type_node |
b6baa67d KVH |
1988 | || type == char16_type_node |
1989 | || type == char32_type_node | |
2986ae00 | 1990 | || type == wchar_type_node) |
cf2ac46f | 1991 | { |
eab01c18 MK |
1992 | tree prom = type; |
1993 | ||
1994 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1995 | { | |
1996 | prom = ENUM_UNDERLYING_TYPE (prom); | |
1997 | if (!ENUM_IS_SCOPED (type) | |
1998 | && ENUM_FIXED_UNDERLYING_TYPE_P (type)) | |
1999 | { | |
2000 | /* ISO C++17, 7.6/4. A prvalue of an unscoped enumeration type | |
2001 | whose underlying type is fixed (10.2) can be converted to a | |
2002 | prvalue of its underlying type. Moreover, if integral promotion | |
2003 | can be applied to its underlying type, a prvalue of an unscoped | |
2004 | enumeration type whose underlying type is fixed can also be | |
2005 | converted to a prvalue of the promoted underlying type. */ | |
2006 | return type_promotes_to (prom); | |
2007 | } | |
2008 | } | |
2009 | ||
cf2ac46f JM |
2010 | int precision = MAX (TYPE_PRECISION (type), |
2011 | TYPE_PRECISION (integer_type_node)); | |
b0c48229 | 2012 | tree totype = c_common_type_for_size (precision, 0); |
62984918 JM |
2013 | if (TYPE_UNSIGNED (prom) |
2014 | && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype)) | |
2015 | prom = c_common_type_for_size (precision, 1); | |
2016 | else | |
2017 | prom = totype; | |
967444bb | 2018 | if (SCOPED_ENUM_P (type)) |
62984918 JM |
2019 | { |
2020 | if (abi_version_crosses (6) | |
2021 | && TYPE_MODE (prom) != TYPE_MODE (type)) | |
a9c697b8 | 2022 | warning (OPT_Wabi, "scoped enum %qT passed through %<...%> as " |
a3f9f006 | 2023 | "%qT before %<-fabi-version=6%>, %qT after", |
62984918 JM |
2024 | type, prom, ENUM_UNDERLYING_TYPE (type)); |
2025 | if (!abi_version_at_least (6)) | |
2026 | type = prom; | |
2027 | } | |
cf2ac46f | 2028 | else |
62984918 | 2029 | type = prom; |
cf2ac46f | 2030 | } |
d72040f5 | 2031 | else if (c_promoting_integer_type_p (type)) |
39211cd5 | 2032 | { |
3d4683cb | 2033 | /* Retain unsignedness if really not getting bigger. */ |
8df83eae | 2034 | if (TYPE_UNSIGNED (type) |
3d4683cb | 2035 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
39211cd5 MS |
2036 | type = unsigned_type_node; |
2037 | else | |
2038 | type = integer_type_node; | |
2039 | } | |
2040 | else if (type == float_type_node) | |
2041 | type = double_type_node; | |
c8094d83 | 2042 | |
0a72704b | 2043 | return type; |
39211cd5 | 2044 | } |
75650646 | 2045 | |
75650646 MM |
2046 | /* The routines below this point are carefully written to conform to |
2047 | the standard. They use the same terminology, and follow the rules | |
e53b6e56 | 2048 | closely. Although they are used only in pt.cc at the moment, they |
75650646 MM |
2049 | should presumably be used everywhere in the future. */ |
2050 | ||
8b8b203a JM |
2051 | /* True iff EXPR can be converted to TYPE via a qualification conversion. |
2052 | Callers should check for identical types before calling this function. */ | |
2053 | ||
2054 | bool | |
2055 | can_convert_qual (tree type, tree expr) | |
2056 | { | |
2057 | tree expr_type = TREE_TYPE (expr); | |
2058 | gcc_assert (!same_type_p (type, expr_type)); | |
2059 | ||
7c3ba214 JM |
2060 | /* A function pointer conversion also counts as a Qualification Adjustment |
2061 | under [over.ics.scs]. */ | |
2062 | if (fnptr_conv_p (type, expr_type)) | |
2063 | return true; | |
2064 | ||
8b8b203a JM |
2065 | if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)) |
2066 | return comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)); | |
2067 | else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type)) | |
2068 | return (same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), | |
2069 | TYPE_PTRMEM_CLASS_TYPE (expr_type)) | |
2070 | && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type), | |
2071 | TYPE_PTRMEM_POINTED_TO_TYPE (expr_type))); | |
2072 | else | |
2073 | return false; | |
2074 | } | |
2075 | ||
e1467ff2 MM |
2076 | /* Attempt to perform qualification conversions on EXPR to convert it |
2077 | to TYPE. Return the resulting expression, or error_mark_node if | |
c5f50290 JM |
2078 | the conversion was impossible. Since this is only used by |
2079 | convert_nontype_argument, we fold the conversion. */ | |
e1467ff2 | 2080 | |
c8094d83 | 2081 | tree |
b746c5dc | 2082 | perform_qualification_conversions (tree type, tree expr) |
75650646 | 2083 | { |
a5ac359a MM |
2084 | tree expr_type; |
2085 | ||
2086 | expr_type = TREE_TYPE (expr); | |
2087 | ||
6f25cb35 MM |
2088 | if (same_type_p (type, expr_type)) |
2089 | return expr; | |
8b8b203a | 2090 | else if (can_convert_qual (type, expr)) |
c5f50290 | 2091 | return cp_fold_convert (type, expr); |
e1467ff2 MM |
2092 | else |
2093 | return error_mark_node; | |
75650646 | 2094 | } |
b8fd7909 JM |
2095 | |
2096 | /* True iff T is a transaction-safe function type. */ | |
2097 | ||
2098 | bool | |
2099 | tx_safe_fn_type_p (tree t) | |
2100 | { | |
7bdc7e06 | 2101 | if (!FUNC_OR_METHOD_TYPE_P (t)) |
b8fd7909 JM |
2102 | return false; |
2103 | return !!lookup_attribute ("transaction_safe", TYPE_ATTRIBUTES (t)); | |
2104 | } | |
2105 | ||
2106 | /* Return the transaction-unsafe variant of transaction-safe function type | |
2107 | T. */ | |
2108 | ||
2109 | tree | |
2110 | tx_unsafe_fn_variant (tree t) | |
2111 | { | |
2112 | gcc_assert (tx_safe_fn_type_p (t)); | |
2113 | tree attrs = remove_attribute ("transaction_safe", | |
2114 | TYPE_ATTRIBUTES (t)); | |
2115 | return cp_build_type_attribute_variant (t, attrs); | |
2116 | } | |
2117 | ||
2118 | /* Return true iff FROM can convert to TO by a transaction-safety | |
2119 | conversion. */ | |
2120 | ||
51dc6603 | 2121 | static bool |
b8fd7909 JM |
2122 | can_convert_tx_safety (tree to, tree from) |
2123 | { | |
2124 | return (flag_tm && tx_safe_fn_type_p (from) | |
2125 | && same_type_p (to, tx_unsafe_fn_variant (from))); | |
2126 | } | |
51dc6603 | 2127 | |
b4329e3d | 2128 | /* Return true iff FROM can convert to TO by dropping noexcept. |
027e3041 | 2129 | This is just a subroutine of fnptr_conv_p. */ |
51dc6603 JM |
2130 | |
2131 | static bool | |
2132 | noexcept_conv_p (tree to, tree from) | |
2133 | { | |
2134 | if (!flag_noexcept_type) | |
2135 | return false; | |
2136 | ||
b4329e3d | 2137 | if (TREE_CODE (to) != TREE_CODE (from)) |
51dc6603 | 2138 | return false; |
b4329e3d | 2139 | if (!FUNC_OR_METHOD_TYPE_P (from)) |
51dc6603 | 2140 | return false; |
b4329e3d PP |
2141 | if (!type_throw_all_p (to) |
2142 | || type_throw_all_p (from)) | |
51dc6603 | 2143 | return false; |
b4329e3d PP |
2144 | tree v = build_exception_variant (from, NULL_TREE); |
2145 | return same_type_p (to, v); | |
51dc6603 JM |
2146 | } |
2147 | ||
2148 | /* Return true iff FROM can convert to TO by a function pointer conversion. */ | |
2149 | ||
2150 | bool | |
2151 | fnptr_conv_p (tree to, tree from) | |
2152 | { | |
b4329e3d | 2153 | tree t = to; |
51dc6603 JM |
2154 | tree f = from; |
2155 | if (TYPE_PTRMEMFUNC_P (t) | |
2156 | && TYPE_PTRMEMFUNC_P (f)) | |
2157 | { | |
2158 | t = TYPE_PTRMEMFUNC_FN_TYPE (t); | |
2159 | f = TYPE_PTRMEMFUNC_FN_TYPE (f); | |
2160 | } | |
b4329e3d PP |
2161 | if (INDIRECT_TYPE_P (t) |
2162 | && INDIRECT_TYPE_P (f)) | |
51dc6603 JM |
2163 | { |
2164 | t = TREE_TYPE (t); | |
2165 | f = TREE_TYPE (f); | |
2166 | } | |
2167 | ||
2168 | return (noexcept_conv_p (t, f) | |
2169 | || can_convert_tx_safety (t, f)); | |
2170 | } | |
2171 | ||
e6d7d618 JM |
2172 | /* Return FN with any NOP_EXPRs stripped that represent function pointer |
2173 | conversions or conversions to the same type. */ | |
51dc6603 JM |
2174 | |
2175 | tree | |
2176 | strip_fnptr_conv (tree fn) | |
2177 | { | |
2178 | while (TREE_CODE (fn) == NOP_EXPR) | |
2179 | { | |
2180 | tree op = TREE_OPERAND (fn, 0); | |
e6d7d618 JM |
2181 | tree ft = TREE_TYPE (fn); |
2182 | tree ot = TREE_TYPE (op); | |
2183 | if (same_type_p (ft, ot) | |
2184 | || fnptr_conv_p (ft, ot)) | |
51dc6603 JM |
2185 | fn = op; |
2186 | else | |
2187 | break; | |
2188 | } | |
2189 | return fn; | |
2190 | } |