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