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