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