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