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