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