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