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