]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Language-level data type conversion for GNU C++. |
d353bf18 | 2 | Copyright (C) 1987-2015 Free Software Foundation, Inc. |
471086d6 | 3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
6f0d25a6 | 5 | This file is part of GCC. |
471086d6 | 6 | |
6f0d25a6 | 7 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 8 | it under the terms of the GNU General Public License as published by |
aa139c3f | 9 | the Free Software Foundation; either version 3, or (at your option) |
471086d6 | 10 | any later version. |
11 | ||
6f0d25a6 | 12 | GCC is distributed in the hope that it will be useful, |
471086d6 | 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 | |
aa139c3f | 18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
471086d6 | 20 | |
21 | ||
af86cc06 | 22 | /* This file contains the functions for converting C++ expressions |
471086d6 | 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" | |
b3ef7553 | 28 | #include "system.h" |
805e22b2 | 29 | #include "coretypes.h" |
30 | #include "tm.h" | |
b20a8bb4 | 31 | #include "alias.h" |
471086d6 | 32 | #include "tree.h" |
9ed99284 | 33 | #include "stor-layout.h" |
471086d6 | 34 | #include "flags.h" |
35 | #include "cp-tree.h" | |
ca82e026 | 36 | #include "intl.h" |
471086d6 | 37 | #include "convert.h" |
2e8226f7 | 38 | #include "decl.h" |
7a979707 | 39 | #include "target.h" |
96624a9e | 40 | |
c4698a21 | 41 | static tree cp_convert_to_pointer (tree, tree, tsubst_flags_t); |
42 | static tree convert_to_pointer_force (tree, tree, tsubst_flags_t); | |
ef9571e5 | 43 | static tree build_type_conversion (tree, tree); |
c4698a21 | 44 | static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t); |
19b271a9 | 45 | static void diagnose_ref_binding (location_t, tree, tree, tree); |
71ccdfff | 46 | |
471086d6 | 47 | /* Change of width--truncation and extension of integers or reals-- |
48 | is represented with NOP_EXPR. Proper functioning of many things | |
49 | assumes that no other conversions can be NOP_EXPRs. | |
50 | ||
51 | Conversion between integer and pointer is represented with CONVERT_EXPR. | |
52 | Converting integer to real uses FLOAT_EXPR | |
53 | and real to integer uses FIX_TRUNC_EXPR. | |
54 | ||
55 | Here is a list of all the functions that assume that widening and | |
56 | narrowing is always done with a NOP_EXPR: | |
57 | In convert.c, convert_to_integer. | |
58 | In c-typeck.c, build_binary_op_nodefault (boolean ops), | |
653e5405 | 59 | and c_common_truthvalue_conversion. |
471086d6 | 60 | In expr.c: expand_expr, for operands of a MULT_EXPR. |
61 | In fold-const.c: fold. | |
62 | In tree.c: get_narrower and get_unwidened. | |
63 | ||
64 | C++: in multiple-inheritance, converting between pointers may involve | |
65 | adjusting them by a delta stored within the class definition. */ | |
66 | \f | |
67 | /* Subroutines of `convert'. */ | |
68 | ||
471086d6 | 69 | /* if converting pointer to pointer |
70 | if dealing with classes, check for derived->base or vice versa | |
71 | else if dealing with method pointers, delegate | |
72 | else convert blindly | |
73 | else if converting class, pass off to build_type_conversion | |
e5ead12a | 74 | else try C-style pointer conversion. */ |
96624a9e | 75 | |
471086d6 | 76 | static tree |
c4698a21 | 77 | cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain) |
471086d6 | 78 | { |
cd16867a | 79 | tree intype = TREE_TYPE (expr); |
80 | enum tree_code form; | |
f82eeb87 | 81 | tree rval; |
3df42822 | 82 | location_t loc = EXPR_LOC_OR_LOC (expr, input_location); |
61f69bc9 | 83 | |
8bf328bf | 84 | if (intype == error_mark_node) |
85 | return error_mark_node; | |
74002e1d | 86 | |
95397ff9 | 87 | if (MAYBE_CLASS_TYPE_P (intype)) |
96624a9e | 88 | { |
96624a9e | 89 | intype = complete_type (intype); |
4b72716d | 90 | if (!COMPLETE_TYPE_P (intype)) |
96624a9e | 91 | { |
c4698a21 | 92 | if (complain & tf_error) |
93 | error_at (loc, "can%'t convert from incomplete type %qT to %qT", | |
94 | intype, type); | |
96624a9e | 95 | return error_mark_node; |
96 | } | |
97 | ||
8999978b | 98 | rval = build_type_conversion (type, expr); |
96624a9e | 99 | if (rval) |
100 | { | |
c4698a21 | 101 | if ((complain & tf_error) |
102 | && rval == error_mark_node) | |
61f69bc9 | 103 | error_at (loc, "conversion of %qE from %qT to %qT is ambiguous", |
104 | expr, intype, type); | |
96624a9e | 105 | return rval; |
106 | } | |
107 | } | |
108 | ||
860740a7 | 109 | /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */ |
c21c015b | 110 | if (TYPE_PTR_P (type) |
38281c46 | 111 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE |
e3cfe3ce | 112 | || VOID_TYPE_P (TREE_TYPE (type)))) |
38281c46 | 113 | { |
6ab399e8 | 114 | if (TYPE_PTRMEMFUNC_P (intype) |
115 | || TREE_CODE (intype) == METHOD_TYPE) | |
c4698a21 | 116 | return convert_member_func_to_ptr (type, expr, complain); |
c21c015b | 117 | if (TYPE_PTR_P (TREE_TYPE (expr))) |
a63bc44c | 118 | return build_nop (type, expr); |
38281c46 | 119 | intype = TREE_TYPE (expr); |
120 | } | |
121 | ||
311e42bc | 122 | if (expr == error_mark_node) |
123 | return error_mark_node; | |
124 | ||
74002e1d | 125 | form = TREE_CODE (intype); |
126 | ||
2b77484d | 127 | if (POINTER_TYPE_P (intype)) |
471086d6 | 128 | { |
129 | intype = TYPE_MAIN_VARIANT (intype); | |
130 | ||
131 | if (TYPE_MAIN_VARIANT (type) != intype | |
c21c015b | 132 | && TYPE_PTR_P (type) |
471086d6 | 133 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE |
95397ff9 | 134 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (type)) |
135 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype)) | |
4a2680fc | 136 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
471086d6 | 137 | { |
138 | enum tree_code code = PLUS_EXPR; | |
4a2680fc | 139 | tree binfo; |
8999978b | 140 | tree intype_class; |
141 | tree type_class; | |
142 | bool same_p; | |
4a2680fc | 143 | |
8999978b | 144 | intype_class = TREE_TYPE (intype); |
145 | type_class = TREE_TYPE (type); | |
146 | ||
9031d10b | 147 | same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class), |
8999978b | 148 | TYPE_MAIN_VARIANT (type_class)); |
149 | binfo = NULL_TREE; | |
c0af329c | 150 | /* Try derived to base conversion. */ |
8999978b | 151 | if (!same_p) |
ae260dcc | 152 | binfo = lookup_base (intype_class, type_class, ba_check, |
153 | NULL, complain); | |
8999978b | 154 | if (!same_p && !binfo) |
471086d6 | 155 | { |
c0af329c | 156 | /* Try base to derived conversion. */ |
ae260dcc | 157 | binfo = lookup_base (type_class, intype_class, ba_check, |
158 | NULL, complain); | |
471086d6 | 159 | code = MINUS_EXPR; |
160 | } | |
4a2680fc | 161 | if (binfo == error_mark_node) |
162 | return error_mark_node; | |
8999978b | 163 | if (binfo || same_p) |
471086d6 | 164 | { |
8999978b | 165 | if (binfo) |
c4698a21 | 166 | expr = build_base_path (code, expr, binfo, 0, complain); |
c0af329c | 167 | /* Add any qualifier conversions. */ |
8999978b | 168 | return build_nop (type, expr); |
471086d6 | 169 | } |
170 | } | |
471086d6 | 171 | |
1bc16cab | 172 | if (TYPE_PTRMEMFUNC_P (type)) |
bea7d742 | 173 | { |
c4698a21 | 174 | if (complain & tf_error) |
175 | error_at (loc, "cannot convert %qE from type %qT to type %qT", | |
176 | expr, intype, type); | |
1bc16cab | 177 | return error_mark_node; |
178 | } | |
a17aefa2 | 179 | |
1bc16cab | 180 | return build_nop (type, expr); |
181 | } | |
05765a91 | 182 | else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype)) |
e5ead12a | 183 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) |
184 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/false, | |
c4698a21 | 185 | /*c_cast_p=*/false, complain); |
2b77484d | 186 | else if (TYPE_PTRMEMFUNC_P (intype)) |
187 | { | |
a63bc44c | 188 | if (!warn_pmf2ptr) |
189 | { | |
190 | if (TREE_CODE (expr) == PTRMEM_CST) | |
c4698a21 | 191 | return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr), |
192 | complain); | |
a63bc44c | 193 | else if (TREE_CODE (expr) == OFFSET_REF) |
194 | { | |
195 | tree object = TREE_OPERAND (expr, 0); | |
196 | return get_member_function_from_ptrfunc (&object, | |
4405c1ad | 197 | TREE_OPERAND (expr, 1), |
c4698a21 | 198 | complain); |
a63bc44c | 199 | } |
200 | } | |
d3f0af4b | 201 | if (complain & tf_error) |
202 | error_at (loc, "cannot convert %qE from type %qT to type %qT", | |
203 | expr, intype, type); | |
2b77484d | 204 | return error_mark_node; |
205 | } | |
471086d6 | 206 | |
ef434176 | 207 | if (null_ptr_cst_p (expr)) |
471086d6 | 208 | { |
2b77484d | 209 | if (TYPE_PTRMEMFUNC_P (type)) |
cb02169c | 210 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, |
c4698a21 | 211 | /*c_cast_p=*/false, complain); |
ad63a0fc | 212 | |
106c3d67 | 213 | if (complain & tf_warning) |
214 | maybe_warn_zero_as_null_pointer_constant (expr, loc); | |
215 | ||
7ba2f86f | 216 | /* A NULL pointer-to-data-member is represented by -1, not by |
217 | zero. */ | |
218 | tree val = (TYPE_PTRDATAMEM_P (type) | |
219 | ? build_int_cst_type (type, -1) | |
220 | : build_int_cst (type, 0)); | |
221 | ||
222 | return (TREE_SIDE_EFFECTS (expr) | |
223 | ? build2 (COMPOUND_EXPR, type, expr, val) : val); | |
471086d6 | 224 | } |
05765a91 | 225 | else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form)) |
de1f960a | 226 | { |
c4698a21 | 227 | if (complain & tf_error) |
228 | error_at (loc, "invalid conversion from %qT to %qT", intype, type); | |
de1f960a | 229 | return error_mark_node; |
230 | } | |
471086d6 | 231 | |
bb0726a1 | 232 | if (INTEGRAL_CODE_P (form)) |
471086d6 | 233 | { |
c5aa1e92 | 234 | if (TYPE_PRECISION (intype) == POINTER_SIZE) |
471086d6 | 235 | return build1 (CONVERT_EXPR, type, expr); |
c4698a21 | 236 | expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr, |
237 | complain); | |
092b1d6f | 238 | /* Modes may be different but sizes should be the same. There |
239 | is supposed to be some integral type that is the same width | |
240 | as a pointer. */ | |
241 | gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))) | |
242 | == GET_MODE_SIZE (TYPE_MODE (type))); | |
9031d10b | 243 | |
471086d6 | 244 | return convert_to_pointer (type, expr); |
245 | } | |
246 | ||
cc4d0855 | 247 | if (type_unknown_p (expr)) |
c4698a21 | 248 | return instantiate_type (type, expr, complain); |
cc4d0855 | 249 | |
c4698a21 | 250 | if (complain & tf_error) |
251 | error_at (loc, "cannot convert %qE from type %qT to type %qT", | |
252 | expr, intype, type); | |
471086d6 | 253 | return error_mark_node; |
254 | } | |
255 | ||
256 | /* Like convert, except permit conversions to take place which | |
257 | are not normally allowed due to access restrictions | |
258 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 259 | |
471086d6 | 260 | static tree |
c4698a21 | 261 | convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain) |
471086d6 | 262 | { |
cd16867a | 263 | tree intype = TREE_TYPE (expr); |
264 | enum tree_code form = TREE_CODE (intype); | |
9031d10b | 265 | |
471086d6 | 266 | if (form == POINTER_TYPE) |
267 | { | |
268 | intype = TYPE_MAIN_VARIANT (intype); | |
269 | ||
270 | if (TYPE_MAIN_VARIANT (type) != intype | |
271 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
95397ff9 | 272 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (type)) |
273 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype)) | |
471086d6 | 274 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
275 | { | |
276 | enum tree_code code = PLUS_EXPR; | |
4a2680fc | 277 | tree binfo; |
278 | ||
279 | binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type), | |
ae260dcc | 280 | ba_unique, NULL, complain); |
4a2680fc | 281 | if (!binfo) |
471086d6 | 282 | { |
4a2680fc | 283 | binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
ae260dcc | 284 | ba_unique, NULL, complain); |
4a2680fc | 285 | code = MINUS_EXPR; |
471086d6 | 286 | } |
4a2680fc | 287 | if (binfo == error_mark_node) |
288 | return error_mark_node; | |
289 | if (binfo) | |
471086d6 | 290 | { |
c4698a21 | 291 | expr = build_base_path (code, expr, binfo, 0, complain); |
653e5405 | 292 | if (expr == error_mark_node) |
293 | return error_mark_node; | |
c0af329c | 294 | /* Add any qualifier conversions. */ |
4a2680fc | 295 | if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)), |
296 | TREE_TYPE (type))) | |
8999978b | 297 | expr = build_nop (type, expr); |
4a2680fc | 298 | return expr; |
471086d6 | 299 | } |
471086d6 | 300 | } |
471086d6 | 301 | } |
302 | ||
c4698a21 | 303 | return cp_convert_to_pointer (type, expr, complain); |
471086d6 | 304 | } |
305 | ||
306 | /* We are passing something to a function which requires a reference. | |
307 | The type we are interested in is in TYPE. The initial | |
308 | value we have to begin with is in ARG. | |
309 | ||
310 | FLAGS controls how we manage access checking. | |
ca106ab1 | 311 | DIRECT_BIND in FLAGS controls how any temporaries are generated. |
312 | If DIRECT_BIND is set, DECL is the reference we're binding to. */ | |
96624a9e | 313 | |
471086d6 | 314 | static tree |
c4698a21 | 315 | build_up_reference (tree type, tree arg, int flags, tree decl, |
316 | tsubst_flags_t complain) | |
471086d6 | 317 | { |
c76251c1 | 318 | tree rval; |
0543e7a9 | 319 | tree argtype = TREE_TYPE (arg); |
471086d6 | 320 | tree target_type = TREE_TYPE (type); |
471086d6 | 321 | |
b4df430b | 322 | gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); |
471086d6 | 323 | |
c76251c1 | 324 | if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg)) |
471086d6 | 325 | { |
ca106ab1 | 326 | /* Create a new temporary variable. We can't just use a TARGET_EXPR |
327 | here because it needs to live as long as DECL. */ | |
c76251c1 | 328 | tree targ = arg; |
ca106ab1 | 329 | |
5ab1aaca | 330 | arg = make_temporary_var_for_ref_to_temp (decl, target_type); |
c23ebfdd | 331 | |
332 | /* Process the initializer for the declaration. */ | |
38281c46 | 333 | DECL_INITIAL (arg) = targ; |
d91303a6 | 334 | cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE, |
011310f7 | 335 | LOOKUP_ONLYCONVERTING|DIRECT_BIND); |
1ad432f2 | 336 | } |
c76251c1 | 337 | else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg)) |
44189c16 | 338 | return get_target_expr_sfinae (arg, complain); |
1ad432f2 | 339 | |
ca106ab1 | 340 | /* If we had a way to wrap this up, and say, if we ever needed its |
b0df6589 | 341 | address, transform all occurrences of the register, into a memory |
342 | reference we could win better. */ | |
44189c16 | 343 | rval = cp_build_addr_expr (arg, complain); |
15e55420 | 344 | if (rval == error_mark_node) |
345 | return error_mark_node; | |
346 | ||
6686e84d | 347 | if ((flags & LOOKUP_PROTECT) |
348 | && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type) | |
95397ff9 | 349 | && MAYBE_CLASS_TYPE_P (argtype) |
350 | && MAYBE_CLASS_TYPE_P (target_type)) | |
6686e84d | 351 | { |
7e6960e0 | 352 | /* We go through lookup_base for the access control. */ |
ae260dcc | 353 | tree binfo = lookup_base (argtype, target_type, ba_check, |
354 | NULL, complain); | |
6686e84d | 355 | if (binfo == error_mark_node) |
356 | return error_mark_node; | |
357 | if (binfo == NULL_TREE) | |
358 | return error_not_base_type (target_type, argtype); | |
c4698a21 | 359 | rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain); |
6686e84d | 360 | } |
c76251c1 | 361 | else |
362 | rval | |
c4698a21 | 363 | = convert_to_pointer_force (build_pointer_type (target_type), |
364 | rval, complain); | |
8999978b | 365 | return build_nop (type, rval); |
471086d6 | 366 | } |
367 | ||
519ba62c | 368 | /* Subroutine of convert_to_reference. REFTYPE is the target reference type. |
369 | INTYPE is the original rvalue type and DECL is an optional _DECL node | |
370 | for diagnostics. | |
9031d10b | 371 | |
519ba62c | 372 | [dcl.init.ref] says that if an rvalue is used to |
373 | initialize a reference, then the reference must be to a | |
374 | non-volatile const type. */ | |
375 | ||
376 | static void | |
19b271a9 | 377 | diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl) |
519ba62c | 378 | { |
379 | tree ttl = TREE_TYPE (reftype); | |
9031d10b | 380 | |
519ba62c | 381 | if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl)) |
382 | { | |
383 | const char *msg; | |
384 | ||
385 | if (CP_TYPE_VOLATILE_P (ttl) && decl) | |
ca82e026 | 386 | msg = G_("initialization of volatile reference type %q#T from " |
387 | "rvalue of type %qT"); | |
519ba62c | 388 | else if (CP_TYPE_VOLATILE_P (ttl)) |
ca82e026 | 389 | msg = G_("conversion to volatile reference type %q#T " |
390 | "from rvalue of type %qT"); | |
519ba62c | 391 | else if (decl) |
ca82e026 | 392 | msg = G_("initialization of non-const reference type %q#T from " |
393 | "rvalue of type %qT"); | |
519ba62c | 394 | else |
ca82e026 | 395 | msg = G_("conversion to non-const reference type %q#T from " |
396 | "rvalue of type %qT"); | |
519ba62c | 397 | |
61f69bc9 | 398 | permerror (loc, msg, reftype, intype); |
519ba62c | 399 | } |
400 | } | |
401 | ||
471086d6 | 402 | /* For C++: Only need to do one-level references, but cannot |
403 | get tripped up on signed/unsigned differences. | |
404 | ||
d81e00a4 | 405 | DECL is either NULL_TREE or the _DECL node for a reference that is being |
406 | initialized. It can be error_mark_node if we don't know the _DECL but | |
407 | we know it's an initialization. */ | |
471086d6 | 408 | |
471086d6 | 409 | tree |
35771a9a | 410 | convert_to_reference (tree reftype, tree expr, int convtype, |
c4698a21 | 411 | int flags, tree decl, tsubst_flags_t complain) |
471086d6 | 412 | { |
cd16867a | 413 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); |
414 | tree intype; | |
471086d6 | 415 | tree rval = NULL_TREE; |
1a3f833b | 416 | tree rval_as_conversion = NULL_TREE; |
1bc16cab | 417 | bool can_convert_intype_to_type; |
3df42822 | 418 | location_t loc = EXPR_LOC_OR_LOC (expr, input_location); |
1a3f833b | 419 | |
9031d10b | 420 | if (TREE_CODE (type) == FUNCTION_TYPE |
d32df6a6 | 421 | && TREE_TYPE (expr) == unknown_type_node) |
66bbeb85 | 422 | expr = instantiate_type (type, expr, complain); |
0a3b29ad | 423 | |
424 | if (expr == error_mark_node) | |
425 | return error_mark_node; | |
426 | ||
427 | intype = TREE_TYPE (expr); | |
cc4d0855 | 428 | |
b4df430b | 429 | gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE); |
ab12ed55 | 430 | gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE); |
471086d6 | 431 | |
471086d6 | 432 | intype = TYPE_MAIN_VARIANT (intype); |
433 | ||
b657f346 | 434 | can_convert_intype_to_type = can_convert_standard (type, intype, complain); |
66bbeb85 | 435 | |
1bc16cab | 436 | if (!can_convert_intype_to_type |
95397ff9 | 437 | && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype) |
1a3f833b | 438 | && ! (flags & LOOKUP_NO_CONVERSION)) |
439 | { | |
440 | /* Look for a user-defined conversion to lvalue that we can use. */ | |
441 | ||
0a4be248 | 442 | rval_as_conversion |
8999978b | 443 | = build_type_conversion (reftype, expr); |
1a3f833b | 444 | |
445 | if (rval_as_conversion && rval_as_conversion != error_mark_node | |
446 | && real_lvalue_p (rval_as_conversion)) | |
447 | { | |
448 | expr = rval_as_conversion; | |
449 | rval_as_conversion = NULL_TREE; | |
450 | intype = type; | |
1bc16cab | 451 | can_convert_intype_to_type = 1; |
1a3f833b | 452 | } |
453 | } | |
454 | ||
b657f346 | 455 | if (((convtype & CONV_STATIC) |
456 | && can_convert_standard (intype, type, complain)) | |
1bc16cab | 457 | || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type)) |
471086d6 | 458 | { |
c4698a21 | 459 | { |
460 | tree ttl = TREE_TYPE (reftype); | |
461 | tree ttr = lvalue_type (expr); | |
471086d6 | 462 | |
19b271a9 | 463 | if ((complain & tf_error) |
c4698a21 | 464 | && ! real_lvalue_p (expr)) |
19b271a9 | 465 | diagnose_ref_binding (loc, reftype, intype, decl); |
9031d10b | 466 | |
c4698a21 | 467 | if (! (convtype & CONV_CONST) |
468 | && !at_least_as_qualified_p (ttl, ttr)) | |
469 | { | |
470 | if (complain & tf_error) | |
471 | permerror (loc, "conversion from %qT to %qT discards qualifiers", | |
472 | ttr, reftype); | |
473 | else | |
474 | return error_mark_node; | |
475 | } | |
476 | } | |
0543e7a9 | 477 | |
c4698a21 | 478 | return build_up_reference (reftype, expr, flags, decl, complain); |
471086d6 | 479 | } |
c07b1ad1 | 480 | else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) |
0543e7a9 | 481 | { |
482 | /* When casting an lvalue to a reference type, just convert into | |
483 | a pointer to the new type and deference it. This is allowed | |
e581f478 | 484 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
0543e7a9 | 485 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
e581f478 | 486 | |
3748625f | 487 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
653e5405 | 488 | meant. */ |
c4698a21 | 489 | if ((complain & tf_warning) |
c21c015b | 490 | && TYPE_PTR_P (intype) |
a09db423 | 491 | && (comptypes (TREE_TYPE (intype), type, |
492 | COMPARE_BASE | COMPARE_DERIVED))) | |
61f69bc9 | 493 | warning_at (loc, 0, "casting %qT to %qT does not dereference pointer", |
494 | intype, reftype); | |
9031d10b | 495 | |
c4698a21 | 496 | rval = cp_build_addr_expr (expr, complain); |
0543e7a9 | 497 | if (rval != error_mark_node) |
985e9ee0 | 498 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), |
c4698a21 | 499 | rval, 0, complain); |
0543e7a9 | 500 | if (rval != error_mark_node) |
b0722fac | 501 | rval = build1 (NOP_EXPR, reftype, rval); |
0543e7a9 | 502 | } |
0a4be248 | 503 | else |
860740a7 | 504 | { |
505 | rval = convert_for_initialization (NULL_TREE, type, expr, flags, | |
c4698a21 | 506 | ICR_CONVERTING, 0, 0, complain); |
a66fe4cb | 507 | if (rval == NULL_TREE || rval == error_mark_node) |
508 | return rval; | |
19b271a9 | 509 | if (complain & tf_error) |
510 | diagnose_ref_binding (loc, reftype, intype, decl); | |
c4698a21 | 511 | rval = build_up_reference (reftype, rval, flags, decl, complain); |
860740a7 | 512 | } |
471086d6 | 513 | |
514 | if (rval) | |
515 | { | |
96624a9e | 516 | /* If we found a way to convert earlier, then use it. */ |
471086d6 | 517 | return rval; |
518 | } | |
519 | ||
c4698a21 | 520 | if (complain & tf_error) |
61f69bc9 | 521 | error_at (loc, "cannot convert type %qT to type %qT", intype, reftype); |
b248d3f7 | 522 | |
471086d6 | 523 | return error_mark_node; |
524 | } | |
525 | ||
526 | /* We are using a reference VAL for its value. Bash that reference all the | |
96624a9e | 527 | way down to its lowest form. */ |
528 | ||
471086d6 | 529 | tree |
35771a9a | 530 | convert_from_reference (tree val) |
471086d6 | 531 | { |
73bdcca2 | 532 | if (TREE_TYPE (val) |
533 | && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE) | |
729f89ff | 534 | { |
2fb61329 | 535 | tree t = TREE_TYPE (TREE_TYPE (val)); |
729f89ff | 536 | tree ref = build1 (INDIRECT_REF, t, val); |
9031d10b | 537 | |
3d37a176 | 538 | mark_exp_read (val); |
729f89ff | 539 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
540 | so that we get the proper error message if the result is used | |
541 | to assign to. Also, &* is supposed to be a no-op. */ | |
542 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); | |
543 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
544 | TREE_SIDE_EFFECTS (ref) | |
545 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val)); | |
729f89ff | 546 | val = ref; |
547 | } | |
9031d10b | 548 | |
471086d6 | 549 | return val; |
550 | } | |
e5dab226 | 551 | |
552 | /* Really perform an lvalue-to-rvalue conversion, including copying an | |
553 | argument of class type into a temporary. */ | |
554 | ||
555 | tree | |
9e505437 | 556 | force_rvalue (tree expr, tsubst_flags_t complain) |
e5dab226 | 557 | { |
9e505437 | 558 | tree type = TREE_TYPE (expr); |
559 | if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR) | |
560 | { | |
f1f41a6c | 561 | vec<tree, va_gc> *args = make_tree_vector_single (expr); |
9e505437 | 562 | expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
563 | &args, type, LOOKUP_NORMAL, complain); | |
564 | release_tree_vector (args); | |
565 | expr = build_cplus_new (type, expr, complain); | |
566 | } | |
e5dab226 | 567 | else |
4405c1ad | 568 | expr = decay_conversion (expr, complain); |
e5dab226 | 569 | |
570 | return expr; | |
571 | } | |
f5220369 | 572 | |
471086d6 | 573 | \f |
a1f05651 | 574 | /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has |
575 | TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR | |
576 | unchanged. */ | |
577 | ||
578 | static tree | |
579 | ignore_overflows (tree expr, tree orig) | |
580 | { | |
581 | if (TREE_CODE (expr) == INTEGER_CST | |
582 | && TREE_CODE (orig) == INTEGER_CST | |
583 | && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig)) | |
584 | { | |
585 | gcc_assert (!TREE_OVERFLOW (orig)); | |
586 | /* Ensure constant sharing. */ | |
e913b5cd | 587 | expr = wide_int_to_tree (TREE_TYPE (expr), expr); |
a1f05651 | 588 | } |
589 | return expr; | |
590 | } | |
591 | ||
592 | /* Fold away simple conversions, but make sure TREE_OVERFLOW is set | |
593 | properly. */ | |
f5220369 | 594 | |
595 | tree | |
596 | cp_fold_convert (tree type, tree expr) | |
597 | { | |
fe565588 | 598 | tree conv; |
599 | if (TREE_TYPE (expr) == type) | |
600 | conv = expr; | |
601 | else if (TREE_CODE (expr) == PTRMEM_CST) | |
602 | { | |
603 | /* Avoid wrapping a PTRMEM_CST in NOP_EXPR. */ | |
604 | conv = copy_node (expr); | |
605 | TREE_TYPE (conv) = type; | |
606 | } | |
607 | else | |
608 | { | |
609 | conv = fold_convert (type, expr); | |
610 | conv = ignore_overflows (conv, expr); | |
611 | } | |
a1f05651 | 612 | return conv; |
f5220369 | 613 | } |
614 | ||
c4a8ac95 | 615 | /* C++ conversions, preference to static cast conversions. */ |
616 | ||
617 | tree | |
c4698a21 | 618 | cp_convert (tree type, tree expr, tsubst_flags_t complain) |
c4a8ac95 | 619 | { |
c4698a21 | 620 | return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain); |
c4a8ac95 | 621 | } |
622 | ||
59dd8856 | 623 | /* C++ equivalent of convert_and_check but using cp_convert as the |
624 | conversion function. | |
625 | ||
626 | Convert EXPR to TYPE, warning about conversion problems with constants. | |
627 | Invoke this function on every expression that is converted implicitly, | |
628 | i.e. because of language rules and not because of an explicit cast. */ | |
629 | ||
630 | tree | |
c4698a21 | 631 | cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain) |
59dd8856 | 632 | { |
633 | tree result; | |
634 | ||
635 | if (TREE_TYPE (expr) == type) | |
636 | return expr; | |
637 | ||
c4698a21 | 638 | result = cp_convert (type, expr, complain); |
59dd8856 | 639 | |
c4698a21 | 640 | if ((complain & tf_warning) |
65615944 | 641 | && c_inhibit_evaluation_warnings == 0) |
642 | { | |
643 | tree folded = maybe_constant_value (expr); | |
644 | tree stripped = folded; | |
b65fb3db | 645 | tree folded_result |
646 | = folded != expr ? cp_convert (type, folded, complain) : result; | |
65615944 | 647 | |
648 | /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a | |
649 | NOP_EXPR so that it isn't TREE_CONSTANT anymore. */ | |
650 | STRIP_NOPS (stripped); | |
651 | ||
652 | if (!TREE_OVERFLOW_P (stripped) | |
653 | && folded_result != error_mark_node) | |
22a75734 | 654 | warnings_for_convert_and_check (input_location, type, folded, |
655 | folded_result); | |
65615944 | 656 | } |
59dd8856 | 657 | |
658 | return result; | |
659 | } | |
660 | ||
b248d3f7 | 661 | /* Conversion... |
662 | ||
663 | FLAGS indicates how we should behave. */ | |
664 | ||
471086d6 | 665 | tree |
c4698a21 | 666 | ocp_convert (tree type, tree expr, int convtype, int flags, |
667 | tsubst_flags_t complain) | |
471086d6 | 668 | { |
cd16867a | 669 | tree e = expr; |
670 | enum tree_code code = TREE_CODE (type); | |
7a979707 | 671 | const char *invalid_conv_diag; |
2b3c93a3 | 672 | tree e1; |
3df42822 | 673 | location_t loc = EXPR_LOC_OR_LOC (expr, input_location); |
471086d6 | 674 | |
1bc16cab | 675 | if (error_operand_p (e) || type == error_mark_node) |
471086d6 | 676 | return error_mark_node; |
d81e00a4 | 677 | |
5b592939 | 678 | complete_type (type); |
679 | complete_type (TREE_TYPE (expr)); | |
680 | ||
7a979707 | 681 | if ((invalid_conv_diag |
682 | = targetm.invalid_conversion (TREE_TYPE (expr), type))) | |
683 | { | |
c4698a21 | 684 | if (complain & tf_error) |
685 | error (invalid_conv_diag); | |
7a979707 | 686 | return error_mark_node; |
687 | } | |
688 | ||
ce984e5e | 689 | /* FIXME remove when moving to c_fully_fold model. */ |
2055d27a | 690 | e = scalar_constant_value (e); |
7a00f939 | 691 | if (error_operand_p (e)) |
692 | return error_mark_node; | |
7745052b | 693 | |
95397ff9 | 694 | if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP)) |
1a3f833b | 695 | /* We need a new temporary; don't take this shortcut. */; |
930e8175 | 696 | else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e))) |
ca23ec64 | 697 | { |
daf9ff67 | 698 | if (same_type_p (type, TREE_TYPE (e))) |
ca23ec64 | 699 | /* The call to fold will not always remove the NOP_EXPR as |
700 | might be expected, since if one of the types is a typedef; | |
755edffd | 701 | the comparison in fold is just equality of pointers, not a |
a09db423 | 702 | call to comptypes. We don't call fold in this case because |
f699c174 | 703 | that can result in infinite recursion; fold will call |
704 | convert, which will call ocp_convert, etc. */ | |
705 | return e; | |
372e0e29 | 706 | /* For complex data types, we need to perform componentwise |
653e5405 | 707 | conversion. */ |
372e0e29 | 708 | else if (TREE_CODE (type) == COMPLEX_TYPE) |
653e5405 | 709 | return fold_if_not_in_template (convert_to_complex (type, e)); |
76249021 | 710 | else if (VECTOR_TYPE_P (type)) |
31631c53 | 711 | return fold_if_not_in_template (convert_to_vector (type, e)); |
bdb2219e | 712 | else if (TREE_CODE (e) == TARGET_EXPR) |
713 | { | |
714 | /* Don't build a NOP_EXPR of class type. Instead, change the | |
930e8175 | 715 | type of the temporary. */ |
bdb2219e | 716 | TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type; |
717 | return e; | |
718 | } | |
ca23ec64 | 719 | else |
092b1d6f | 720 | { |
721 | /* We shouldn't be treating objects of ADDRESSABLE type as | |
722 | rvalues. */ | |
723 | gcc_assert (!TREE_ADDRESSABLE (type)); | |
5d7ed6c7 | 724 | return fold_if_not_in_template (build_nop (type, e)); |
092b1d6f | 725 | } |
ca23ec64 | 726 | } |
727 | ||
2b3c93a3 | 728 | e1 = targetm.convert_to_type (type, e); |
729 | if (e1) | |
730 | return e1; | |
731 | ||
d81e00a4 | 732 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) |
95b2ac55 | 733 | { |
c4698a21 | 734 | e = convert_to_void (e, ICV_CAST, complain); |
aeef2be5 | 735 | return e; |
95b2ac55 | 736 | } |
d81e00a4 | 737 | |
bb0726a1 | 738 | if (INTEGRAL_CODE_P (code)) |
471086d6 | 739 | { |
617abf06 | 740 | tree intype = TREE_TYPE (e); |
a1f05651 | 741 | tree converted; |
384590d4 | 742 | |
743 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
744 | { | |
745 | /* enum = enum, enum = int, enum = float, (enum)pointer are all | |
746 | errors. */ | |
747 | if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype) | |
653e5405 | 748 | || TREE_CODE (intype) == REAL_TYPE) |
b312a686 | 749 | && ! (convtype & CONV_STATIC)) |
c21c015b | 750 | || TYPE_PTR_P (intype)) |
384590d4 | 751 | { |
c4698a21 | 752 | if (complain & tf_error) |
61f69bc9 | 753 | permerror (loc, "conversion from %q#T to %q#T", intype, type); |
c4698a21 | 754 | else |
384590d4 | 755 | return error_mark_node; |
756 | } | |
757 | ||
758 | /* [expr.static.cast] | |
759 | ||
760 | 8. A value of integral or enumeration type can be explicitly | |
761 | converted to an enumeration type. The value is unchanged if | |
762 | the original value is within the range of the enumeration | |
763 | values. Otherwise, the resulting enumeration value is | |
764 | unspecified. */ | |
c4698a21 | 765 | if ((complain & tf_warning) |
c28ddc97 | 766 | && TREE_CODE (e) == INTEGER_CST |
0116c9f8 | 767 | && ENUM_UNDERLYING_TYPE (type) |
c28ddc97 | 768 | && !int_fits_type_p (e, ENUM_UNDERLYING_TYPE (type))) |
61f69bc9 | 769 | warning_at (loc, OPT_Wconversion, |
770 | "the result of the conversion is unspecified because " | |
771 | "%qE is outside the range of type %qT", | |
772 | expr, type); | |
471086d6 | 773 | } |
95397ff9 | 774 | if (MAYBE_CLASS_TYPE_P (intype)) |
471086d6 | 775 | { |
776 | tree rval; | |
8999978b | 777 | rval = build_type_conversion (type, e); |
6495357a | 778 | if (rval) |
779 | return rval; | |
c4698a21 | 780 | if (complain & tf_error) |
61f69bc9 | 781 | error_at (loc, "%q#T used where a %qT was expected", intype, type); |
471086d6 | 782 | return error_mark_node; |
783 | } | |
bb0726a1 | 784 | if (code == BOOLEAN_TYPE) |
f8913d47 | 785 | { |
c21c015b | 786 | if (VOID_TYPE_P (intype)) |
61f69bc9 | 787 | { |
c4698a21 | 788 | if (complain & tf_error) |
789 | error_at (loc, | |
790 | "could not convert %qE from %<void%> to %<bool%>", | |
791 | expr); | |
61f69bc9 | 792 | return error_mark_node; |
793 | } | |
794 | ||
f8913d47 | 795 | /* We can't implicitly convert a scoped enum to bool, so convert |
796 | to the underlying type first. */ | |
797 | if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC)) | |
172fff7b | 798 | e = build_nop (ENUM_UNDERLYING_TYPE (intype), e); |
f8913d47 | 799 | return cp_truthvalue_conversion (e); |
800 | } | |
ce871053 | 801 | |
a1f05651 | 802 | converted = fold_if_not_in_template (convert_to_integer (type, e)); |
803 | ||
804 | /* Ignore any integer overflow caused by the conversion. */ | |
805 | return ignore_overflows (converted, e); | |
471086d6 | 806 | } |
c7ca48ea | 807 | if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e)) |
49858f84 | 808 | { |
809 | if (complain & tf_warning) | |
810 | maybe_warn_zero_as_null_pointer_constant (e, loc); | |
811 | return nullptr_node; | |
812 | } | |
05765a91 | 813 | if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type)) |
c4698a21 | 814 | return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain)); |
684b9d79 | 815 | if (code == VECTOR_TYPE) |
c37ff371 | 816 | { |
817 | tree in_vtype = TREE_TYPE (e); | |
95397ff9 | 818 | if (MAYBE_CLASS_TYPE_P (in_vtype)) |
c37ff371 | 819 | { |
820 | tree ret_val; | |
821 | ret_val = build_type_conversion (type, e); | |
653e5405 | 822 | if (ret_val) |
823 | return ret_val; | |
c4698a21 | 824 | if (complain & tf_error) |
825 | error_at (loc, "%q#T used where a %qT was expected", | |
826 | in_vtype, type); | |
653e5405 | 827 | return error_mark_node; |
c37ff371 | 828 | } |
5d7ed6c7 | 829 | return fold_if_not_in_template (convert_to_vector (type, e)); |
c37ff371 | 830 | } |
c4a8ac95 | 831 | if (code == REAL_TYPE || code == COMPLEX_TYPE) |
471086d6 | 832 | { |
95397ff9 | 833 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e))) |
471086d6 | 834 | { |
835 | tree rval; | |
8999978b | 836 | rval = build_type_conversion (type, e); |
471086d6 | 837 | if (rval) |
838 | return rval; | |
c4698a21 | 839 | else if (complain & tf_error) |
840 | error_at (loc, | |
841 | "%q#T used where a floating point value was expected", | |
842 | TREE_TYPE (e)); | |
471086d6 | 843 | } |
c4a8ac95 | 844 | if (code == REAL_TYPE) |
5d7ed6c7 | 845 | return fold_if_not_in_template (convert_to_real (type, e)); |
c4a8ac95 | 846 | else if (code == COMPLEX_TYPE) |
5d7ed6c7 | 847 | return fold_if_not_in_template (convert_to_complex (type, e)); |
471086d6 | 848 | } |
849 | ||
850 | /* New C++ semantics: since assignment is now based on | |
851 | memberwise copying, if the rhs type is derived from the | |
852 | lhs type, then we may still do a conversion. */ | |
95397ff9 | 853 | if (RECORD_OR_UNION_CODE_P (code)) |
471086d6 | 854 | { |
855 | tree dtype = TREE_TYPE (e); | |
c25194fd | 856 | tree ctor = NULL_TREE; |
471086d6 | 857 | |
471086d6 | 858 | dtype = TYPE_MAIN_VARIANT (dtype); |
859 | ||
471086d6 | 860 | /* Conversion between aggregate types. New C++ semantics allow |
861 | objects of derived type to be cast to objects of base type. | |
862 | Old semantics only allowed this between pointers. | |
863 | ||
864 | There may be some ambiguity between using a constructor | |
865 | vs. using a type conversion operator when both apply. */ | |
866 | ||
0a4be248 | 867 | ctor = e; |
860740a7 | 868 | |
44189c16 | 869 | if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain)) |
8c18e707 | 870 | return error_mark_node; |
6cbb4197 | 871 | |
f82f1250 | 872 | if (BRACE_ENCLOSED_INITIALIZER_P (ctor)) |
c4698a21 | 873 | ctor = perform_implicit_conversion (type, ctor, complain); |
f82f1250 | 874 | else if ((flags & LOOKUP_ONLYCONVERTING) |
875 | && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype))) | |
fce73460 | 876 | /* For copy-initialization, first we create a temp of the proper type |
877 | with a user-defined conversion sequence, then we direct-initialize | |
878 | the target with the temp (see [dcl.init]). */ | |
c4698a21 | 879 | ctor = build_user_type_conversion (type, ctor, flags, complain); |
3eb89cd8 | 880 | else |
f352a3fb | 881 | { |
f1f41a6c | 882 | vec<tree, va_gc> *ctor_vec = make_tree_vector_single (ctor); |
f352a3fb | 883 | ctor = build_special_member_call (NULL_TREE, |
884 | complete_ctor_identifier, | |
885 | &ctor_vec, | |
c4698a21 | 886 | type, flags, complain); |
f352a3fb | 887 | release_tree_vector (ctor_vec); |
888 | } | |
0a4be248 | 889 | if (ctor) |
c4698a21 | 890 | return build_cplus_new (type, ctor, complain); |
471086d6 | 891 | } |
892 | ||
c4698a21 | 893 | if (complain & tf_error) |
a7cc1549 | 894 | { |
895 | /* If the conversion failed and expr was an invalid use of pointer to | |
896 | member function, try to report a meaningful error. */ | |
5c874cd3 | 897 | if (invalid_nonstatic_memfn_p (loc, expr, complain)) |
a7cc1549 | 898 | /* We displayed the error message. */; |
899 | else | |
61f69bc9 | 900 | error_at (loc, "conversion from %qT to non-scalar type %qT requested", |
901 | TREE_TYPE (expr), type); | |
a7cc1549 | 902 | } |
471086d6 | 903 | return error_mark_node; |
904 | } | |
905 | ||
b1a8d4ce | 906 | /* When an expression is used in a void context, its value is discarded and |
907 | no lvalue-rvalue and similar conversions happen [expr.static.cast/4, | |
908 | stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type | |
909 | in a void context. The C++ standard does not define what an `access' to an | |
63eff20d | 910 | object is, but there is reason to believe that it is the lvalue to rvalue |
b1a8d4ce | 911 | conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it |
912 | accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8 | |
913 | indicates that volatile semantics should be the same between C and C++ | |
914 | where ever possible. C leaves it implementation defined as to what | |
915 | constitutes an access to a volatile. So, we interpret `*vp' as a read of | |
916 | the volatile object `vp' points to, unless that is an incomplete type. For | |
917 | volatile references we do not do this interpretation, because that would | |
918 | make it impossible to ignore the reference return value from functions. We | |
919 | issue warnings in the confusing cases. | |
9031d10b | 920 | |
dab3247a | 921 | The IMPLICIT is ICV_CAST when the user is explicitly converting an expression |
922 | to void via a cast. If an expression is being implicitly converted, IMPLICIT | |
923 | indicates the context of the implicit conversion. */ | |
b1a8d4ce | 924 | |
925 | tree | |
dab3247a | 926 | convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain) |
b1a8d4ce | 927 | { |
3df42822 | 928 | location_t loc = EXPR_LOC_OR_LOC (expr, input_location); |
61f69bc9 | 929 | |
9031d10b | 930 | if (expr == error_mark_node |
3d411d73 | 931 | || TREE_TYPE (expr) == error_mark_node) |
932 | return error_mark_node; | |
b2af2a93 | 933 | |
dab3247a | 934 | if (implicit == ICV_CAST) |
f94341bb | 935 | mark_exp_read (expr); |
936 | else | |
937 | { | |
938 | tree exprv = expr; | |
939 | ||
940 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
941 | exprv = TREE_OPERAND (exprv, 1); | |
0388391d | 942 | if (DECL_P (exprv) |
943 | || handled_component_p (exprv) | |
86ccc124 | 944 | || INDIRECT_REF_P (exprv)) |
f94341bb | 945 | /* Expr is not being 'used' here, otherwise we whould have |
946 | called mark_{rl}value_use use here, which would have in turn | |
947 | called mark_exp_read. Rather, we call mark_exp_read directly | |
948 | to avoid some warnings when | |
949 | -Wunused-but-set-{variable,parameter} is in effect. */ | |
950 | mark_exp_read (exprv); | |
951 | } | |
b2af2a93 | 952 | |
b1a8d4ce | 953 | if (!TREE_TYPE (expr)) |
954 | return expr; | |
5c874cd3 | 955 | if (invalid_nonstatic_memfn_p (loc, expr, complain)) |
334f6ce1 | 956 | return error_mark_node; |
ed36f1cf | 957 | if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) |
958 | { | |
ebd21de4 | 959 | if (complain & tf_error) |
61f69bc9 | 960 | error_at (loc, "pseudo-destructor is not called"); |
ed36f1cf | 961 | return error_mark_node; |
962 | } | |
e3cfe3ce | 963 | if (VOID_TYPE_P (TREE_TYPE (expr))) |
b1a8d4ce | 964 | return expr; |
965 | switch (TREE_CODE (expr)) | |
966 | { | |
967 | case COND_EXPR: | |
968 | { | |
653e5405 | 969 | /* The two parts of a cond expr might be separate lvalues. */ |
970 | tree op1 = TREE_OPERAND (expr,1); | |
971 | tree op2 = TREE_OPERAND (expr,2); | |
527facbb | 972 | bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1)) |
973 | || TREE_SIDE_EFFECTS (op2)); | |
dab3247a | 974 | tree new_op1, new_op2; |
527facbb | 975 | new_op1 = NULL_TREE; |
dab3247a | 976 | if (implicit != ICV_CAST && !side_effects) |
977 | { | |
527facbb | 978 | if (op1) |
979 | new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain); | |
dab3247a | 980 | new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain); |
981 | } | |
982 | else | |
983 | { | |
527facbb | 984 | if (op1) |
985 | new_op1 = convert_to_void (op1, ICV_CAST, complain); | |
dab3247a | 986 | new_op2 = convert_to_void (op2, ICV_CAST, complain); |
987 | } | |
9031d10b | 988 | |
527facbb | 989 | expr = build3 (COND_EXPR, TREE_TYPE (new_op2), |
831d52a2 | 990 | TREE_OPERAND (expr, 0), new_op1, new_op2); |
653e5405 | 991 | break; |
b1a8d4ce | 992 | } |
9031d10b | 993 | |
b1a8d4ce | 994 | case COMPOUND_EXPR: |
995 | { | |
653e5405 | 996 | /* The second part of a compound expr contains the value. */ |
997 | tree op1 = TREE_OPERAND (expr,1); | |
dab3247a | 998 | tree new_op1; |
999 | if (implicit != ICV_CAST && !TREE_NO_WARNING (expr)) | |
1000 | new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain); | |
1001 | else | |
1002 | new_op1 = convert_to_void (op1, ICV_CAST, complain); | |
9031d10b | 1003 | |
653e5405 | 1004 | if (new_op1 != op1) |
e907a2e9 | 1005 | { |
831d52a2 | 1006 | tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1), |
1007 | TREE_OPERAND (expr, 0), new_op1); | |
e907a2e9 | 1008 | expr = t; |
1009 | } | |
1010 | ||
653e5405 | 1011 | break; |
b1a8d4ce | 1012 | } |
9031d10b | 1013 | |
b1a8d4ce | 1014 | case NON_LVALUE_EXPR: |
1015 | case NOP_EXPR: | |
c0af329c | 1016 | /* These have already decayed to rvalue. */ |
b1a8d4ce | 1017 | break; |
9031d10b | 1018 | |
331bc0ad | 1019 | case CALL_EXPR: /* We have a special meaning for volatile void fn(). */ |
b1a8d4ce | 1020 | break; |
9031d10b | 1021 | |
b1a8d4ce | 1022 | case INDIRECT_REF: |
1023 | { | |
653e5405 | 1024 | tree type = TREE_TYPE (expr); |
1025 | int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) | |
1026 | == REFERENCE_TYPE; | |
1027 | int is_volatile = TYPE_VOLATILE (type); | |
1028 | int is_complete = COMPLETE_TYPE_P (complete_type (type)); | |
1029 | ||
dc02da7f | 1030 | /* Can't load the value if we don't know the type. */ |
653e5405 | 1031 | if (is_volatile && !is_complete) |
ebd21de4 | 1032 | { |
1033 | if (complain & tf_warning) | |
dab3247a | 1034 | switch (implicit) |
1035 | { | |
1036 | case ICV_CAST: | |
61f69bc9 | 1037 | warning_at (loc, 0, "conversion to void will not access " |
dab3247a | 1038 | "object of incomplete type %qT", type); |
1039 | break; | |
1040 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1041 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1042 | "incomplete type %qT in second operand " |
1043 | "of conditional expression", type); | |
1044 | break; | |
1045 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1046 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1047 | "incomplete type %qT in third operand " |
1048 | "of conditional expression", type); | |
1049 | break; | |
1050 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1051 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1052 | "incomplete type %qT in right operand of " |
1053 | "comma operator", type); | |
1054 | break; | |
1055 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1056 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1057 | "incomplete type %qT in left operand of " |
1058 | "comma operator", type); | |
1059 | break; | |
1060 | case ICV_STATEMENT: | |
61f69bc9 | 1061 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1062 | "incomplete type %qT in statement", type); |
1063 | break; | |
1064 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1065 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1066 | "incomplete type %qT in for increment " |
1067 | "expression", type); | |
1068 | break; | |
1069 | default: | |
1070 | gcc_unreachable (); | |
1071 | } | |
ebd21de4 | 1072 | } |
dc02da7f | 1073 | /* Don't load the value if this is an implicit dereference, or if |
1074 | the type needs to be handled by ctors/dtors. */ | |
dab3247a | 1075 | else if (is_volatile && is_reference) |
ebd21de4 | 1076 | { |
1077 | if (complain & tf_warning) | |
dab3247a | 1078 | switch (implicit) |
1079 | { | |
1080 | case ICV_CAST: | |
61f69bc9 | 1081 | warning_at (loc, 0, "conversion to void will not access " |
dab3247a | 1082 | "object of type %qT", type); |
1083 | break; | |
1084 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1085 | warning_at (loc, 0, "implicit dereference will not access " |
1086 | "object of type %qT in second operand of " | |
dab3247a | 1087 | "conditional expression", type); |
1088 | break; | |
1089 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1090 | warning_at (loc, 0, "implicit dereference will not access " |
1091 | "object of type %qT in third operand of " | |
dab3247a | 1092 | "conditional expression", type); |
1093 | break; | |
1094 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1095 | warning_at (loc, 0, "implicit dereference will not access " |
1096 | "object of type %qT in right operand of " | |
dab3247a | 1097 | "comma operator", type); |
1098 | break; | |
1099 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1100 | warning_at (loc, 0, "implicit dereference will not access " |
1101 | "object of type %qT in left operand of comma " | |
1102 | "operator", type); | |
dab3247a | 1103 | break; |
1104 | case ICV_STATEMENT: | |
61f69bc9 | 1105 | warning_at (loc, 0, "implicit dereference will not access " |
1106 | "object of type %qT in statement", type); | |
dab3247a | 1107 | break; |
1108 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1109 | warning_at (loc, 0, "implicit dereference will not access " |
1110 | "object of type %qT in for increment expression", | |
1111 | type); | |
dab3247a | 1112 | break; |
1113 | default: | |
1114 | gcc_unreachable (); | |
1115 | } | |
ebd21de4 | 1116 | } |
dab3247a | 1117 | else if (is_volatile && TREE_ADDRESSABLE (type)) |
1118 | { | |
1119 | if (complain & tf_warning) | |
1120 | switch (implicit) | |
1121 | { | |
1122 | case ICV_CAST: | |
61f69bc9 | 1123 | warning_at (loc, 0, "conversion to void will not access " |
dab3247a | 1124 | "object of non-trivially-copyable type %qT", |
61f69bc9 | 1125 | type); |
dab3247a | 1126 | break; |
1127 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1128 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1129 | "non-trivially-copyable type %qT in second " |
1130 | "operand of conditional expression", type); | |
1131 | break; | |
1132 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1133 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1134 | "non-trivially-copyable type %qT in third " |
1135 | "operand of conditional expression", type); | |
1136 | break; | |
1137 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1138 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1139 | "non-trivially-copyable type %qT in right " |
1140 | "operand of comma operator", type); | |
1141 | break; | |
1142 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1143 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1144 | "non-trivially-copyable type %qT in left " |
1145 | "operand of comma operator", type); | |
1146 | break; | |
1147 | case ICV_STATEMENT: | |
61f69bc9 | 1148 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1149 | "non-trivially-copyable type %qT in statement", |
61f69bc9 | 1150 | type); |
dab3247a | 1151 | break; |
1152 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1153 | warning_at (loc, 0, "indirection will not access object of " |
dab3247a | 1154 | "non-trivially-copyable type %qT in for " |
1155 | "increment expression", type); | |
1156 | break; | |
1157 | default: | |
1158 | gcc_unreachable (); | |
1159 | } | |
1160 | } | |
dc02da7f | 1161 | if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type)) |
a4d4030b | 1162 | { |
1163 | /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF | |
1164 | operation is stripped off. Note that we don't warn about | |
1165 | - an expression with TREE_NO_WARNING set. (For an example of | |
1166 | such expressions, see build_over_call in call.c.) | |
1167 | - automatic dereferencing of references, since the user cannot | |
3d177e8c | 1168 | control it. (See also warn_if_unused_value() in c-common.c.) */ |
a4d4030b | 1169 | if (warn_unused_value |
dab3247a | 1170 | && implicit != ICV_CAST |
a4d4030b | 1171 | && (complain & tf_warning) |
1172 | && !TREE_NO_WARNING (expr) | |
1173 | && !is_reference) | |
61f69bc9 | 1174 | warning_at (loc, OPT_Wunused_value, "value computed is not used"); |
a4d4030b | 1175 | expr = TREE_OPERAND (expr, 0); |
1176 | } | |
653e5405 | 1177 | |
1178 | break; | |
b1a8d4ce | 1179 | } |
9031d10b | 1180 | |
b1a8d4ce | 1181 | case VAR_DECL: |
1182 | { | |
653e5405 | 1183 | /* External variables might be incomplete. */ |
1184 | tree type = TREE_TYPE (expr); | |
1185 | int is_complete = COMPLETE_TYPE_P (complete_type (type)); | |
1186 | ||
ebd21de4 | 1187 | if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning)) |
dab3247a | 1188 | switch (implicit) |
1189 | { | |
1190 | case ICV_CAST: | |
61f69bc9 | 1191 | warning_at (loc, 0, "conversion to void will not access " |
dab3247a | 1192 | "object %qE of incomplete type %qT", expr, type); |
1193 | break; | |
1194 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1195 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1196 | "not be accessed in second operand of " | |
dab3247a | 1197 | "conditional expression", expr, type); |
1198 | break; | |
1199 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1200 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1201 | "not be accessed in third operand of " | |
dab3247a | 1202 | "conditional expression", expr, type); |
1203 | break; | |
1204 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1205 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1206 | "not be accessed in right operand of comma operator", | |
1207 | expr, type); | |
dab3247a | 1208 | break; |
1209 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1210 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1211 | "not be accessed in left operand of comma operator", | |
1212 | expr, type); | |
dab3247a | 1213 | break; |
1214 | case ICV_STATEMENT: | |
61f69bc9 | 1215 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1216 | "not be accessed in statement", expr, type); | |
dab3247a | 1217 | break; |
1218 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1219 | warning_at (loc, 0, "variable %qE of incomplete type %qT will " |
1220 | "not be accessed in for increment expression", | |
1221 | expr, type); | |
dab3247a | 1222 | break; |
1223 | default: | |
1224 | gcc_unreachable (); | |
1225 | } | |
1226 | ||
653e5405 | 1227 | break; |
b1a8d4ce | 1228 | } |
d9f88785 | 1229 | |
25b3017b | 1230 | case TARGET_EXPR: |
1231 | /* Don't bother with the temporary object returned from a function if | |
1232 | we don't use it and don't need to destroy it. We'll still | |
1233 | allocate space for it in expand_call or declare_return_variable, | |
1234 | but we don't need to track it through all the tree phases. */ | |
67bb1301 | 1235 | if (TARGET_EXPR_IMPLICIT_P (expr) |
25b3017b | 1236 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr))) |
1237 | { | |
1238 | tree init = TARGET_EXPR_INITIAL (expr); | |
1239 | if (TREE_CODE (init) == AGGR_INIT_EXPR | |
1240 | && !AGGR_INIT_VIA_CTOR_P (init)) | |
1241 | { | |
c2f47e15 | 1242 | tree fn = AGGR_INIT_EXPR_FN (init); |
389dd41b | 1243 | expr = build_call_array_loc (input_location, |
1244 | TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), | |
1245 | fn, | |
1246 | aggr_init_expr_nargs (init), | |
1247 | AGGR_INIT_EXPR_ARGP (init)); | |
25b3017b | 1248 | } |
1249 | } | |
1250 | break; | |
1251 | ||
b1a8d4ce | 1252 | default:; |
1253 | } | |
eebd67e7 | 1254 | expr = resolve_nondeduced_context (expr); |
b1a8d4ce | 1255 | { |
1256 | tree probe = expr; | |
9031d10b | 1257 | |
b1a8d4ce | 1258 | if (TREE_CODE (probe) == ADDR_EXPR) |
1259 | probe = TREE_OPERAND (expr, 0); | |
2e0e6579 | 1260 | if (type_unknown_p (probe)) |
1261 | { | |
1262 | /* [over.over] enumerates the places where we can take the address | |
1263 | of an overloaded function, and this is not one of them. */ | |
ebd21de4 | 1264 | if (complain & tf_error) |
dab3247a | 1265 | switch (implicit) |
1266 | { | |
1267 | case ICV_CAST: | |
61f69bc9 | 1268 | error_at (loc, "conversion to void " |
1269 | "cannot resolve address of overloaded function"); | |
dab3247a | 1270 | break; |
1271 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1272 | error_at (loc, "second operand of conditional expression " |
1273 | "cannot resolve address of overloaded function"); | |
dab3247a | 1274 | break; |
1275 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1276 | error_at (loc, "third operand of conditional expression " |
1277 | "cannot resolve address of overloaded function"); | |
dab3247a | 1278 | break; |
1279 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1280 | error_at (loc, "right operand of comma operator " |
1281 | "cannot resolve address of overloaded function"); | |
dab3247a | 1282 | break; |
1283 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1284 | error_at (loc, "left operand of comma operator " |
1285 | "cannot resolve address of overloaded function"); | |
dab3247a | 1286 | break; |
1287 | case ICV_STATEMENT: | |
61f69bc9 | 1288 | error_at (loc, "statement " |
1289 | "cannot resolve address of overloaded function"); | |
dab3247a | 1290 | break; |
1291 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1292 | error_at (loc, "for increment expression " |
1293 | "cannot resolve address of overloaded function"); | |
dab3247a | 1294 | break; |
1295 | } | |
ebd21de4 | 1296 | else |
1297 | return error_mark_node; | |
3ab4693e | 1298 | expr = void_node; |
2e0e6579 | 1299 | } |
dab3247a | 1300 | else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe)) |
2056769e | 1301 | { |
1302 | /* Only warn when there is no &. */ | |
ebd21de4 | 1303 | if (complain & tf_warning) |
dab3247a | 1304 | switch (implicit) |
1305 | { | |
1306 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1307 | warning_at (loc, OPT_Waddress, |
1308 | "second operand of conditional expression " | |
1309 | "is a reference, not call, to function %qE", expr); | |
dab3247a | 1310 | break; |
1311 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1312 | warning_at (loc, OPT_Waddress, |
1313 | "third operand of conditional expression " | |
1314 | "is a reference, not call, to function %qE", expr); | |
dab3247a | 1315 | break; |
1316 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1317 | warning_at (loc, OPT_Waddress, |
1318 | "right operand of comma operator " | |
1319 | "is a reference, not call, to function %qE", expr); | |
dab3247a | 1320 | break; |
1321 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1322 | warning_at (loc, OPT_Waddress, |
1323 | "left operand of comma operator " | |
1324 | "is a reference, not call, to function %qE", expr); | |
dab3247a | 1325 | break; |
1326 | case ICV_STATEMENT: | |
61f69bc9 | 1327 | warning_at (loc, OPT_Waddress, |
1328 | "statement is a reference, not call, to function %qE", | |
1329 | expr); | |
dab3247a | 1330 | break; |
1331 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1332 | warning_at (loc, OPT_Waddress, |
1333 | "for increment expression " | |
1334 | "is a reference, not call, to function %qE", expr); | |
dab3247a | 1335 | break; |
1336 | default: | |
1337 | gcc_unreachable (); | |
1338 | } | |
1339 | ||
2056769e | 1340 | if (TREE_CODE (expr) == COMPONENT_REF) |
1341 | expr = TREE_OPERAND (expr, 0); | |
1342 | } | |
b1a8d4ce | 1343 | } |
9031d10b | 1344 | |
e3cfe3ce | 1345 | if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr))) |
b1a8d4ce | 1346 | { |
dab3247a | 1347 | if (implicit != ICV_CAST |
5e2a4133 | 1348 | && warn_unused_value |
1349 | && !TREE_NO_WARNING (expr) | |
1350 | && !processing_template_decl) | |
1fe46df1 | 1351 | { |
1352 | /* The middle end does not warn about expressions that have | |
1353 | been explicitly cast to void, so we must do so here. */ | |
ebd21de4 | 1354 | if (!TREE_SIDE_EFFECTS (expr)) { |
1355 | if (complain & tf_warning) | |
dab3247a | 1356 | switch (implicit) |
1357 | { | |
1358 | case ICV_SECOND_OF_COND: | |
61f69bc9 | 1359 | warning_at (loc, OPT_Wunused_value, |
1360 | "second operand of conditional expression " | |
1361 | "has no effect"); | |
dab3247a | 1362 | break; |
1363 | case ICV_THIRD_OF_COND: | |
61f69bc9 | 1364 | warning_at (loc, OPT_Wunused_value, |
1365 | "third operand of conditional expression " | |
1366 | "has no effect"); | |
dab3247a | 1367 | break; |
1368 | case ICV_RIGHT_OF_COMMA: | |
61f69bc9 | 1369 | warning_at (loc, OPT_Wunused_value, |
1370 | "right operand of comma operator has no effect"); | |
dab3247a | 1371 | break; |
1372 | case ICV_LEFT_OF_COMMA: | |
61f69bc9 | 1373 | warning_at (loc, OPT_Wunused_value, |
1374 | "left operand of comma operator has no effect"); | |
dab3247a | 1375 | break; |
1376 | case ICV_STATEMENT: | |
61f69bc9 | 1377 | warning_at (loc, OPT_Wunused_value, |
1378 | "statement has no effect"); | |
dab3247a | 1379 | break; |
1380 | case ICV_THIRD_IN_FOR: | |
61f69bc9 | 1381 | warning_at (loc, OPT_Wunused_value, |
1382 | "for increment expression has no effect"); | |
dab3247a | 1383 | break; |
1384 | default: | |
1385 | gcc_unreachable (); | |
1386 | } | |
ebd21de4 | 1387 | } |
9031d10b | 1388 | else |
1389 | { | |
1fe46df1 | 1390 | tree e; |
1391 | enum tree_code code; | |
607a5d68 | 1392 | enum tree_code_class tclass; |
9031d10b | 1393 | |
1fe46df1 | 1394 | e = expr; |
1395 | /* We might like to warn about (say) "(int) f()", as the | |
1396 | cast has no effect, but the compiler itself will | |
1397 | generate implicit conversions under some | |
324d8f2d | 1398 | circumstances. (For example a block copy will be |
1fe46df1 | 1399 | turned into a call to "__builtin_memcpy", with a |
1400 | conversion of the return value to an appropriate | |
1401 | type.) So, to avoid false positives, we strip | |
416ce344 | 1402 | conversions. Do not use STRIP_NOPs because it will |
1403 | not strip conversions to "void", as that is not a | |
1404 | mode-preserving conversion. */ | |
1405 | while (TREE_CODE (e) == NOP_EXPR) | |
1406 | e = TREE_OPERAND (e, 0); | |
1fe46df1 | 1407 | |
1408 | code = TREE_CODE (e); | |
607a5d68 | 1409 | tclass = TREE_CODE_CLASS (code); |
1410 | if ((tclass == tcc_comparison | |
1411 | || tclass == tcc_unary | |
1412 | || (tclass == tcc_binary | |
1fe46df1 | 1413 | && !(code == MODIFY_EXPR |
1414 | || code == INIT_EXPR | |
1415 | || code == PREDECREMENT_EXPR | |
1416 | || code == PREINCREMENT_EXPR | |
1417 | || code == POSTDECREMENT_EXPR | |
9372a89d | 1418 | || code == POSTINCREMENT_EXPR)) |
1419 | || code == VEC_PERM_EXPR | |
1420 | || code == VEC_COND_EXPR) | |
ebd21de4 | 1421 | && (complain & tf_warning)) |
61f69bc9 | 1422 | warning_at (loc, OPT_Wunused_value, "value computed is not used"); |
1fe46df1 | 1423 | } |
1424 | } | |
00fa9079 | 1425 | expr = build1 (CONVERT_EXPR, void_type_node, expr); |
b1a8d4ce | 1426 | } |
de5ad4cb | 1427 | if (! TREE_SIDE_EFFECTS (expr)) |
3ab4693e | 1428 | expr = void_node; |
b1a8d4ce | 1429 | return expr; |
1430 | } | |
1431 | ||
d81e00a4 | 1432 | /* Create an expression whose value is that of EXPR, |
1433 | converted to type TYPE. The TREE_TYPE of the value | |
1434 | is always TYPE. This function implements all reasonable | |
1435 | conversions; callers should filter out those that are | |
c4a8ac95 | 1436 | not permitted by the language being compiled. |
1437 | ||
1438 | Most of this routine is from build_reinterpret_cast. | |
1439 | ||
a17c2a3a | 1440 | The back end cannot call cp_convert (what was convert) because |
c4a8ac95 | 1441 | conversions to/from basetypes may involve memory references |
1442 | (vbases) and adding or subtracting small values (multiple | |
1443 | inheritance), but it calls convert from the constant folding code | |
e0aa5007 | 1444 | on subtrees of already built trees after it has ripped them apart. |
c4a8ac95 | 1445 | |
1446 | Also, if we ever support range variables, we'll probably also have to | |
1447 | do a little bit more work. */ | |
d81e00a4 | 1448 | |
1449 | tree | |
35771a9a | 1450 | convert (tree type, tree expr) |
d81e00a4 | 1451 | { |
c4a8ac95 | 1452 | tree intype; |
1453 | ||
1454 | if (type == error_mark_node || expr == error_mark_node) | |
1455 | return error_mark_node; | |
1456 | ||
c4a8ac95 | 1457 | intype = TREE_TYPE (expr); |
1458 | ||
6686e84d | 1459 | if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype)) |
13f0eb20 | 1460 | return fold_if_not_in_template (build_nop (type, expr)); |
c4a8ac95 | 1461 | |
1462 | return ocp_convert (type, expr, CONV_OLD_CONVERT, | |
c4698a21 | 1463 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION, |
1464 | tf_warning_or_error); | |
d81e00a4 | 1465 | } |
1466 | ||
c4a8ac95 | 1467 | /* Like cp_convert, except permit conversions to take place which |
471086d6 | 1468 | are not normally allowed due to access restrictions |
1469 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 1470 | |
471086d6 | 1471 | tree |
c4698a21 | 1472 | convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain) |
471086d6 | 1473 | { |
cd16867a | 1474 | tree e = expr; |
1475 | enum tree_code code = TREE_CODE (type); | |
471086d6 | 1476 | |
1477 | if (code == REFERENCE_TYPE) | |
9031d10b | 1478 | return (fold_if_not_in_template |
c4698a21 | 1479 | (convert_to_reference (type, e, CONV_C_CAST, 0, |
1480 | NULL_TREE, complain))); | |
471086d6 | 1481 | |
1482 | if (code == POINTER_TYPE) | |
c4698a21 | 1483 | return fold_if_not_in_template (convert_to_pointer_force (type, e, |
1484 | complain)); | |
471086d6 | 1485 | |
ac9386a0 | 1486 | /* From typeck.c convert_for_assignment */ |
c21c015b | 1487 | if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR |
471086d6 | 1488 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) |
ac9386a0 | 1489 | || integer_zerop (e) |
1490 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
471086d6 | 1491 | && TYPE_PTRMEMFUNC_P (type)) |
cb02169c | 1492 | /* compatible pointer to member functions. */ |
1493 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1, | |
c4698a21 | 1494 | /*c_cast_p=*/1, complain); |
a74e8896 | 1495 | |
c4698a21 | 1496 | return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain); |
471086d6 | 1497 | } |
1498 | ||
471086d6 | 1499 | /* Convert an aggregate EXPR to type XTYPE. If a conversion |
1500 | exists, return the attempted conversion. This may | |
1501 | return ERROR_MARK_NODE if the conversion is not | |
1502 | allowed (references private members, etc). | |
1503 | If no conversion exists, NULL_TREE is returned. | |
1504 | ||
ce28ee2e | 1505 | FIXME: Ambiguity checking is wrong. Should choose one by the implicit |
1506 | object parameter, or by the second standard conversion sequence if | |
1507 | that doesn't do it. This will probably wait for an overloading rewrite. | |
1508 | (jason 8/9/95) */ | |
471086d6 | 1509 | |
ef9571e5 | 1510 | static tree |
8999978b | 1511 | build_type_conversion (tree xtype, tree expr) |
471086d6 | 1512 | { |
1513 | /* C++: check to see if we can convert this aggregate type | |
bcf789d7 | 1514 | into the required type. */ |
66bbeb85 | 1515 | return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL, |
1516 | tf_warning_or_error); | |
471086d6 | 1517 | } |
1518 | ||
1e66592c | 1519 | /* Convert the given EXPR to one of a group of types suitable for use in an |
1520 | expression. DESIRES is a combination of various WANT_* flags (q.v.) | |
35771a9a | 1521 | which indicates which types are suitable. If COMPLAIN is true, complain |
1e66592c | 1522 | about ambiguity; otherwise, the caller will deal with it. */ |
471086d6 | 1523 | |
1e66592c | 1524 | tree |
35771a9a | 1525 | build_expr_type_conversion (int desires, tree expr, bool complain) |
471086d6 | 1526 | { |
1e66592c | 1527 | tree basetype = TREE_TYPE (expr); |
6a44e72e | 1528 | tree conv = NULL_TREE; |
bdd152ce | 1529 | tree winner = NULL_TREE; |
471086d6 | 1530 | |
9031d10b | 1531 | if (expr == null_node |
1532 | && (desires & WANT_INT) | |
954885ed | 1533 | && !(desires & WANT_NULL)) |
db30b351 | 1534 | { |
1535 | source_location loc = | |
1536 | expansion_point_location_if_in_system_header (input_location); | |
1537 | ||
1538 | warning_at (loc, OPT_Wconversion_null, | |
1539 | "converting NULL to non-pointer type"); | |
1540 | } | |
9031d10b | 1541 | |
3d411d73 | 1542 | if (basetype == error_mark_node) |
1543 | return error_mark_node; | |
1544 | ||
95397ff9 | 1545 | if (! MAYBE_CLASS_TYPE_P (basetype)) |
bdd152ce | 1546 | switch (TREE_CODE (basetype)) |
1547 | { | |
1548 | case INTEGER_TYPE: | |
954885ed | 1549 | if ((desires & WANT_NULL) && null_ptr_cst_p (expr)) |
bdd152ce | 1550 | return expr; |
1551 | /* else fall through... */ | |
1552 | ||
1553 | case BOOLEAN_TYPE: | |
1554 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1555 | case ENUMERAL_TYPE: | |
1556 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1557 | case REAL_TYPE: | |
1558 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1559 | case POINTER_TYPE: | |
1560 | return (desires & WANT_POINTER) ? expr : NULL_TREE; | |
9031d10b | 1561 | |
bdd152ce | 1562 | case FUNCTION_TYPE: |
1563 | case ARRAY_TYPE: | |
4405c1ad | 1564 | return (desires & WANT_POINTER) ? decay_conversion (expr, |
1565 | tf_warning_or_error) | |
653e5405 | 1566 | : NULL_TREE; |
96ec9e30 | 1567 | |
64ec1ad6 | 1568 | case COMPLEX_TYPE: |
96ec9e30 | 1569 | case VECTOR_TYPE: |
64ec1ad6 | 1570 | if ((desires & WANT_VECTOR_OR_COMPLEX) == 0) |
96ec9e30 | 1571 | return NULL_TREE; |
1572 | switch (TREE_CODE (TREE_TYPE (basetype))) | |
1573 | { | |
1574 | case INTEGER_TYPE: | |
1575 | case BOOLEAN_TYPE: | |
1576 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1577 | case ENUMERAL_TYPE: | |
1578 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1579 | case REAL_TYPE: | |
1580 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1581 | default: | |
1582 | return NULL_TREE; | |
1583 | } | |
1584 | ||
bdd152ce | 1585 | default: |
1586 | return NULL_TREE; | |
1587 | } | |
1588 | ||
1589 | /* The code for conversions from class type is currently only used for | |
1590 | delete expressions. Other expressions are handled by build_new_op. */ | |
a5f2d620 | 1591 | if (!complete_type_or_maybe_complain (basetype, expr, complain)) |
f8182e08 | 1592 | return error_mark_node; |
1593 | if (!TYPE_HAS_CONVERSION (basetype)) | |
bdd152ce | 1594 | return NULL_TREE; |
1595 | ||
9960d752 | 1596 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) |
bdd152ce | 1597 | { |
1598 | int win = 0; | |
1599 | tree candidate; | |
1600 | tree cand = TREE_VALUE (conv); | |
c1ca2f12 | 1601 | cand = OVL_CURRENT (cand); |
bdd152ce | 1602 | |
1603 | if (winner && winner == cand) | |
1604 | continue; | |
1605 | ||
cf7aa2e5 | 1606 | if (DECL_NONCONVERTING_P (cand)) |
1607 | continue; | |
1608 | ||
ef4534a3 | 1609 | candidate = non_reference (TREE_TYPE (TREE_TYPE (cand))); |
bdd152ce | 1610 | |
1611 | switch (TREE_CODE (candidate)) | |
1612 | { | |
1613 | case BOOLEAN_TYPE: | |
1614 | case INTEGER_TYPE: | |
1615 | win = (desires & WANT_INT); break; | |
1616 | case ENUMERAL_TYPE: | |
1617 | win = (desires & WANT_ENUM); break; | |
1618 | case REAL_TYPE: | |
1619 | win = (desires & WANT_FLOAT); break; | |
1620 | case POINTER_TYPE: | |
1621 | win = (desires & WANT_POINTER); break; | |
1622 | ||
64ec1ad6 | 1623 | case COMPLEX_TYPE: |
96ec9e30 | 1624 | case VECTOR_TYPE: |
64ec1ad6 | 1625 | if ((desires & WANT_VECTOR_OR_COMPLEX) == 0) |
96ec9e30 | 1626 | break; |
1627 | switch (TREE_CODE (TREE_TYPE (candidate))) | |
1628 | { | |
1629 | case BOOLEAN_TYPE: | |
1630 | case INTEGER_TYPE: | |
1631 | win = (desires & WANT_INT); break; | |
1632 | case ENUMERAL_TYPE: | |
1633 | win = (desires & WANT_ENUM); break; | |
1634 | case REAL_TYPE: | |
1635 | win = (desires & WANT_FLOAT); break; | |
1636 | default: | |
1637 | break; | |
1638 | } | |
1639 | break; | |
1640 | ||
bdd152ce | 1641 | default: |
9c4c1195 | 1642 | /* A wildcard could be instantiated to match any desired |
1643 | type, but we can't deduce the template argument. */ | |
1644 | if (WILDCARD_TYPE_P (candidate)) | |
1645 | win = true; | |
bdd152ce | 1646 | break; |
1647 | } | |
1648 | ||
1649 | if (win) | |
1650 | { | |
9c4c1195 | 1651 | if (TREE_CODE (cand) == TEMPLATE_DECL) |
1652 | { | |
1653 | if (complain) | |
1654 | error ("default type conversion can't deduce template" | |
1655 | " argument for %qD", cand); | |
1656 | return error_mark_node; | |
1657 | } | |
1658 | ||
bdd152ce | 1659 | if (winner) |
1660 | { | |
609c541f | 1661 | tree winner_type |
1662 | = non_reference (TREE_TYPE (TREE_TYPE (winner))); | |
1663 | ||
1664 | if (!same_type_ignoring_top_level_qualifiers_p (winner_type, | |
1665 | candidate)) | |
bdd152ce | 1666 | { |
609c541f | 1667 | if (complain) |
1668 | { | |
1669 | error ("ambiguous default type conversion from %qT", | |
1670 | basetype); | |
392214f6 | 1671 | inform (input_location, |
1672 | " candidate conversions include %qD and %qD", | |
1673 | winner, cand); | |
609c541f | 1674 | } |
1675 | return error_mark_node; | |
bdd152ce | 1676 | } |
bdd152ce | 1677 | } |
609c541f | 1678 | |
1679 | winner = cand; | |
bdd152ce | 1680 | } |
1681 | } | |
1e66592c | 1682 | |
bdd152ce | 1683 | if (winner) |
1684 | { | |
ef4534a3 | 1685 | tree type = non_reference (TREE_TYPE (TREE_TYPE (winner))); |
66bbeb85 | 1686 | return build_user_type_conversion (type, expr, LOOKUP_NORMAL, |
1687 | tf_warning_or_error); | |
471086d6 | 1688 | } |
1e66592c | 1689 | |
985e9ee0 | 1690 | return NULL_TREE; |
471086d6 | 1691 | } |
bc3887cf | 1692 | |
96624a9e | 1693 | /* Implements integral promotion (4.1) and float->double promotion. */ |
1694 | ||
bc3887cf | 1695 | tree |
35771a9a | 1696 | type_promotes_to (tree type) |
bc3887cf | 1697 | { |
2b3c93a3 | 1698 | tree promoted_type; |
1699 | ||
ce28ee2e | 1700 | if (type == error_mark_node) |
1701 | return error_mark_node; | |
1702 | ||
bc3887cf | 1703 | type = TYPE_MAIN_VARIANT (type); |
bb0726a1 | 1704 | |
2b3c93a3 | 1705 | /* Check for promotions of target-defined types first. */ |
1706 | promoted_type = targetm.promoted_type (type); | |
1707 | if (promoted_type) | |
1708 | return promoted_type; | |
1709 | ||
bb0726a1 | 1710 | /* bool always promotes to int (not unsigned), even if it's the same |
1711 | size. */ | |
9e1a8234 | 1712 | if (TREE_CODE (type) == BOOLEAN_TYPE) |
bb0726a1 | 1713 | type = integer_type_node; |
1714 | ||
1715 | /* Normally convert enums to int, but convert wide enums to something | |
74b777e5 | 1716 | wider. Scoped enums don't promote, but pretend they do for backward |
1717 | ABI bug compatibility wrt varargs. */ | |
bb0726a1 | 1718 | else if (TREE_CODE (type) == ENUMERAL_TYPE |
924bbf02 | 1719 | || type == char16_type_node |
1720 | || type == char32_type_node | |
bb0726a1 | 1721 | || type == wchar_type_node) |
6c9497b1 | 1722 | { |
1723 | int precision = MAX (TYPE_PRECISION (type), | |
1724 | TYPE_PRECISION (integer_type_node)); | |
771d21fa | 1725 | tree totype = c_common_type_for_size (precision, 0); |
74b777e5 | 1726 | tree prom = type; |
1727 | if (TREE_CODE (prom) == ENUMERAL_TYPE) | |
1728 | prom = ENUM_UNDERLYING_TYPE (prom); | |
1729 | if (TYPE_UNSIGNED (prom) | |
1730 | && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype)) | |
1731 | prom = c_common_type_for_size (precision, 1); | |
1732 | else | |
1733 | prom = totype; | |
72fc0f64 | 1734 | if (SCOPED_ENUM_P (type)) |
74b777e5 | 1735 | { |
1736 | if (abi_version_crosses (6) | |
1737 | && TYPE_MODE (prom) != TYPE_MODE (type)) | |
1738 | warning (OPT_Wabi, "scoped enum %qT passed through ... as " | |
1739 | "%qT before -fabi-version=6, %qT after", | |
1740 | type, prom, ENUM_UNDERLYING_TYPE (type)); | |
1741 | if (!abi_version_at_least (6)) | |
1742 | type = prom; | |
1743 | } | |
6c9497b1 | 1744 | else |
74b777e5 | 1745 | type = prom; |
6c9497b1 | 1746 | } |
d7aeef06 | 1747 | else if (c_promoting_integer_type_p (type)) |
bc3887cf | 1748 | { |
d0acef9e | 1749 | /* Retain unsignedness if really not getting bigger. */ |
78a8ed03 | 1750 | if (TYPE_UNSIGNED (type) |
d0acef9e | 1751 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
bc3887cf | 1752 | type = unsigned_type_node; |
1753 | else | |
1754 | type = integer_type_node; | |
1755 | } | |
1756 | else if (type == float_type_node) | |
1757 | type = double_type_node; | |
9031d10b | 1758 | |
a681799d | 1759 | return type; |
bc3887cf | 1760 | } |
668ae905 | 1761 | |
668ae905 | 1762 | /* The routines below this point are carefully written to conform to |
1763 | the standard. They use the same terminology, and follow the rules | |
1764 | closely. Although they are used only in pt.c at the moment, they | |
1765 | should presumably be used everywhere in the future. */ | |
1766 | ||
1146f179 | 1767 | /* Attempt to perform qualification conversions on EXPR to convert it |
1768 | to TYPE. Return the resulting expression, or error_mark_node if | |
1769 | the conversion was impossible. */ | |
1770 | ||
9031d10b | 1771 | tree |
35771a9a | 1772 | perform_qualification_conversions (tree type, tree expr) |
668ae905 | 1773 | { |
1bc16cab | 1774 | tree expr_type; |
1775 | ||
1776 | expr_type = TREE_TYPE (expr); | |
1777 | ||
3b087710 | 1778 | if (same_type_p (type, expr_type)) |
1779 | return expr; | |
1780 | else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type) | |
1781 | && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type))) | |
1bc16cab | 1782 | return build_nop (type, expr); |
05765a91 | 1783 | else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type) |
1bc16cab | 1784 | && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), |
1785 | TYPE_PTRMEM_CLASS_TYPE (expr_type)) | |
1786 | && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type), | |
1787 | TYPE_PTRMEM_POINTED_TO_TYPE (expr_type))) | |
1788 | return build_nop (type, expr); | |
1146f179 | 1789 | else |
1790 | return error_mark_node; | |
668ae905 | 1791 | } |