]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/cvt.c
re PR c++/18803 (rejects access to operator() in template)
[thirdparty/gcc.git] / gcc / cp / cvt.c
CommitLineData
8d08fdba 1/* Language-level data type conversion for GNU C++.
d6a8bdff 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
aa335b76 3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
8d08fdba
MS
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
f5adbb8d 6This file is part of GCC.
8d08fdba 7
f5adbb8d 8GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
f5adbb8d 13GCC is distributed in the hope that it will be useful,
8d08fdba
MS
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
f5adbb8d 19along with GCC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
22
23
3fd5abcf 24/* This file contains the functions for converting C++ expressions
8d08fdba
MS
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"
8d052bc7 30#include "system.h"
4977bab6
ZW
31#include "coretypes.h"
32#include "tm.h"
8d08fdba
MS
33#include "tree.h"
34#include "flags.h"
35#include "cp-tree.h"
8d08fdba 36#include "convert.h"
e833cb11 37#include "toplev.h"
ddaed37e 38#include "decl.h"
e92cc029 39
b746c5dc
GDR
40static tree cp_convert_to_pointer (tree, tree, bool);
41static tree convert_to_pointer_force (tree, tree);
42static tree build_up_reference (tree, tree, int, tree);
43static void warn_ref_binding (tree, tree, tree);
49c249e1 44
8d08fdba
MS
45/* Change of width--truncation and extension of integers or reals--
46 is represented with NOP_EXPR. Proper functioning of many things
47 assumes that no other conversions can be NOP_EXPRs.
48
49 Conversion between integer and pointer is represented with CONVERT_EXPR.
50 Converting integer to real uses FLOAT_EXPR
51 and real to integer uses FIX_TRUNC_EXPR.
52
53 Here is a list of all the functions that assume that widening and
54 narrowing is always done with a NOP_EXPR:
55 In convert.c, convert_to_integer.
56 In c-typeck.c, build_binary_op_nodefault (boolean ops),
78ef5b89 57 and c_common_truthvalue_conversion.
8d08fdba
MS
58 In expr.c: expand_expr, for operands of a MULT_EXPR.
59 In fold-const.c: fold.
60 In tree.c: get_narrower and get_unwidened.
61
62 C++: in multiple-inheritance, converting between pointers may involve
63 adjusting them by a delta stored within the class definition. */
64\f
65/* Subroutines of `convert'. */
66
8d08fdba
MS
67/* if converting pointer to pointer
68 if dealing with classes, check for derived->base or vice versa
69 else if dealing with method pointers, delegate
70 else convert blindly
71 else if converting class, pass off to build_type_conversion
f9825168
NS
72 else try C-style pointer conversion. If FORCE is true then allow
73 conversions via virtual bases (these are permitted by reinterpret_cast,
74 but not static_cast). */
e92cc029 75
8d08fdba 76static tree
b746c5dc 77cp_convert_to_pointer (tree type, tree expr, bool force)
8d08fdba 78{
926ce8bd
KH
79 tree intype = TREE_TYPE (expr);
80 enum tree_code form;
31bcaa20 81 tree rval;
0c482362
AP
82 if (intype == error_mark_node)
83 return error_mark_node;
71851aaa 84
e92cc029
MS
85 if (IS_AGGR_TYPE (intype))
86 {
e92cc029 87 intype = complete_type (intype);
d0f062fb 88 if (!COMPLETE_TYPE_P (intype))
e92cc029 89 {
939add70
GDR
90 error ("can't convert from incomplete type %qT to %qT",
91 intype, type);
e92cc029
MS
92 return error_mark_node;
93 }
94
7993382e 95 rval = build_type_conversion (type, expr);
e92cc029
MS
96 if (rval)
97 {
98 if (rval == error_mark_node)
939add70
GDR
99 error ("conversion of %qE from %qT to %qT is ambiguous",
100 expr, intype, type);
e92cc029
MS
101 return rval;
102 }
103 }
104
faf5394a 105 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
9a3b49ac
MS
106 if (TREE_CODE (type) == POINTER_TYPE
107 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
b72801e2 108 || VOID_TYPE_P (TREE_TYPE (type))))
9a3b49ac 109 {
33c25e5c
MM
110 if (TYPE_PTRMEMFUNC_P (intype)
111 || TREE_CODE (intype) == METHOD_TYPE)
112 return convert_member_func_to_ptr (type, expr);
d6b4ea85
MM
113 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
114 return build_nop (type, expr);
9a3b49ac
MS
115 intype = TREE_TYPE (expr);
116 }
117
c87978aa
JM
118 if (expr == error_mark_node)
119 return error_mark_node;
120
71851aaa
MS
121 form = TREE_CODE (intype);
122
d8f8dca1 123 if (POINTER_TYPE_P (intype))
8d08fdba
MS
124 {
125 intype = TYPE_MAIN_VARIANT (intype);
126
127 if (TYPE_MAIN_VARIANT (type) != intype
d8f8dca1 128 && TREE_CODE (type) == POINTER_TYPE
8d08fdba 129 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
a80e4195
MS
130 && IS_AGGR_TYPE (TREE_TYPE (type))
131 && IS_AGGR_TYPE (TREE_TYPE (intype))
338d90b8 132 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
8d08fdba
MS
133 {
134 enum tree_code code = PLUS_EXPR;
338d90b8 135 tree binfo;
7993382e
MM
136 tree intype_class;
137 tree type_class;
138 bool same_p;
338d90b8 139
7993382e
MM
140 intype_class = TREE_TYPE (intype);
141 type_class = TREE_TYPE (type);
142
143 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
144 TYPE_MAIN_VARIANT (type_class));
145 binfo = NULL_TREE;
00a17e31 146 /* Try derived to base conversion. */
7993382e
MM
147 if (!same_p)
148 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
149 if (!same_p && !binfo)
8d08fdba 150 {
00a17e31 151 /* Try base to derived conversion. */
7993382e 152 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
8d08fdba
MS
153 code = MINUS_EXPR;
154 }
338d90b8
NS
155 if (binfo == error_mark_node)
156 return error_mark_node;
7993382e 157 if (binfo || same_p)
8d08fdba 158 {
7993382e
MM
159 if (binfo)
160 expr = build_base_path (code, expr, binfo, 0);
00a17e31 161 /* Add any qualifier conversions. */
7993382e 162 return build_nop (type, expr);
8d08fdba
MS
163 }
164 }
8d08fdba 165
a5ac359a 166 if (TYPE_PTRMEMFUNC_P (type))
28cbf42c 167 {
939add70
GDR
168 error ("cannot convert %qE from type %qT to type %qT",
169 expr, intype, type);
a5ac359a
MM
170 return error_mark_node;
171 }
b928a651 172
a5ac359a
MM
173 return build_nop (type, expr);
174 }
175 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
176 {
177 tree b1;
178 tree b2;
179 tree binfo;
180 enum tree_code code = PLUS_EXPR;
181 base_kind bk;
182
183 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
184 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
185 binfo = lookup_base (b1, b2, ba_check, &bk);
186 if (!binfo)
187 {
188 binfo = lookup_base (b2, b1, ba_check, &bk);
189 code = MINUS_EXPR;
190 }
191 if (binfo == error_mark_node)
192 return error_mark_node;
b928a651 193
a5ac359a
MM
194 if (bk == bk_via_virtual)
195 {
196 if (force)
939add70
GDR
197 warning ("pointer to member cast from %qT to %qT is via"
198 " virtual base", intype, type);
a5ac359a 199 else
6ad07332 200 {
939add70
GDR
201 error ("pointer to member cast from %qT to %qT is"
202 " via virtual base", intype, type);
a5ac359a 203 return error_mark_node;
6ad07332 204 }
a5ac359a
MM
205 /* This is a reinterpret cast, whose result is unspecified.
206 We choose to do nothing. */
207 return build1 (NOP_EXPR, type, expr);
f30432d7
MS
208 }
209
a5ac359a
MM
210 if (TREE_CODE (expr) == PTRMEM_CST)
211 expr = cplus_expand_constant (expr);
212
213 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
214 expr = size_binop (code,
215 build_nop (sizetype, expr),
216 BINFO_OFFSET (binfo));
7993382e 217 return build_nop (type, expr);
8d08fdba 218 }
d8f8dca1 219 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
08e17d9d
MM
220 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
221 /*c_cast_p=*/false);
d8f8dca1
MM
222 else if (TYPE_PTRMEMFUNC_P (intype))
223 {
d6b4ea85
MM
224 if (!warn_pmf2ptr)
225 {
226 if (TREE_CODE (expr) == PTRMEM_CST)
227 return cp_convert_to_pointer (type,
228 PTRMEM_CST_MEMBER (expr),
229 force);
230 else if (TREE_CODE (expr) == OFFSET_REF)
231 {
232 tree object = TREE_OPERAND (expr, 0);
233 return get_member_function_from_ptrfunc (&object,
234 TREE_OPERAND (expr, 1));
235 }
236 }
939add70
GDR
237 error ("cannot convert %qE from type %qT to type %qT",
238 expr, intype, type);
d8f8dca1
MM
239 return error_mark_node;
240 }
8d08fdba 241
8d08fdba
MS
242 if (integer_zerop (expr))
243 {
d8f8dca1 244 if (TYPE_PTRMEMFUNC_P (type))
08e17d9d
MM
245 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
246 /*c_cast_p=*/false);
cd8ed629 247
1f84ec23 248 if (TYPE_PTRMEM_P (type))
4a90aeeb
NS
249 {
250 /* A NULL pointer-to-member is represented by -1, not by
251 zero. */
7d60be94 252 expr = build_int_cst (type, -1);
4a90aeeb
NS
253 /* Fix up the representation of -1 if appropriate. */
254 expr = force_fit_type (expr, 0, false, false);
255 }
cd8ed629 256 else
7d60be94 257 expr = build_int_cst (type, 0);
ca7a3bd7 258
8d08fdba
MS
259 return expr;
260 }
a5ac359a 261 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
201fbb7f 262 {
939add70 263 error ("invalid conversion from %qT to %qT", intype, type);
201fbb7f
GDR
264 return error_mark_node;
265 }
8d08fdba 266
2986ae00 267 if (INTEGRAL_CODE_P (form))
8d08fdba 268 {
f5963e61 269 if (TYPE_PRECISION (intype) == POINTER_SIZE)
8d08fdba 270 return build1 (CONVERT_EXPR, type, expr);
b0c48229 271 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
8dc2b103
NS
272 /* Modes may be different but sizes should be the same. There
273 is supposed to be some integral type that is the same width
274 as a pointer. */
275 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
276 == GET_MODE_SIZE (TYPE_MODE (type)));
277
8d08fdba
MS
278 return convert_to_pointer (type, expr);
279 }
280
e6e174e5 281 if (type_unknown_p (expr))
c2ea3a40 282 return instantiate_type (type, expr, tf_error | tf_warning);
e6e174e5 283
939add70
GDR
284 error ("cannot convert %qE from type %qT to type %qT",
285 expr, intype, type);
8d08fdba
MS
286 return error_mark_node;
287}
288
289/* Like convert, except permit conversions to take place which
290 are not normally allowed due to access restrictions
291 (such as conversion from sub-type to private super-type). */
e92cc029 292
8d08fdba 293static tree
b746c5dc 294convert_to_pointer_force (tree type, tree expr)
8d08fdba 295{
926ce8bd
KH
296 tree intype = TREE_TYPE (expr);
297 enum tree_code form = TREE_CODE (intype);
8d08fdba 298
8d08fdba
MS
299 if (form == POINTER_TYPE)
300 {
301 intype = TYPE_MAIN_VARIANT (intype);
302
303 if (TYPE_MAIN_VARIANT (type) != intype
304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
a80e4195
MS
305 && IS_AGGR_TYPE (TREE_TYPE (type))
306 && IS_AGGR_TYPE (TREE_TYPE (intype))
8d08fdba
MS
307 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
308 {
309 enum tree_code code = PLUS_EXPR;
338d90b8
NS
310 tree binfo;
311
312 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
18e4be85 313 ba_unique, NULL);
338d90b8 314 if (!binfo)
8d08fdba 315 {
338d90b8 316 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
18e4be85 317 ba_unique, NULL);
338d90b8 318 code = MINUS_EXPR;
8d08fdba 319 }
338d90b8
NS
320 if (binfo == error_mark_node)
321 return error_mark_node;
322 if (binfo)
8d08fdba 323 {
338d90b8 324 expr = build_base_path (code, expr, binfo, 0);
75c525d7
GDR
325 if (expr == error_mark_node)
326 return error_mark_node;
00a17e31 327 /* Add any qualifier conversions. */
338d90b8
NS
328 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
329 TREE_TYPE (type)))
7993382e 330 expr = build_nop (type, expr);
338d90b8 331 return expr;
8d08fdba 332 }
8d08fdba 333 }
8d08fdba
MS
334 }
335
b746c5dc 336 return cp_convert_to_pointer (type, expr, true);
8d08fdba
MS
337}
338
339/* We are passing something to a function which requires a reference.
340 The type we are interested in is in TYPE. The initial
341 value we have to begin with is in ARG.
342
343 FLAGS controls how we manage access checking.
08ac397c
JM
344 DIRECT_BIND in FLAGS controls how any temporaries are generated.
345 If DIRECT_BIND is set, DECL is the reference we're binding to. */
e92cc029 346
8d08fdba 347static tree
b746c5dc 348build_up_reference (tree type, tree arg, int flags, tree decl)
8d08fdba 349{
eb66be0e 350 tree rval;
8926095f 351 tree argtype = TREE_TYPE (arg);
8d08fdba 352 tree target_type = TREE_TYPE (type);
8d08fdba 353
50bc768d 354 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8d08fdba 355
eb66be0e 356 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
8d08fdba 357 {
08ac397c
JM
358 /* Create a new temporary variable. We can't just use a TARGET_EXPR
359 here because it needs to live as long as DECL. */
eb66be0e 360 tree targ = arg;
08ac397c 361
aa6e8ed3 362 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
8d1e67c6
MM
363
364 /* Process the initializer for the declaration. */
9a3b49ac 365 DECL_INITIAL (arg) = targ;
cd9f6678 366 cp_finish_decl (arg, targ, NULL_TREE,
c37dc68e 367 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
e349ee73 368 }
eb66be0e 369 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
c506ca22 370 return get_target_expr (arg);
e349ee73 371
08ac397c 372 /* If we had a way to wrap this up, and say, if we ever needed its
d2e5ee5c
MS
373 address, transform all occurrences of the register, into a memory
374 reference we could win better. */
eb66be0e 375 rval = build_unary_op (ADDR_EXPR, arg, 1);
162bc98d
JM
376 if (rval == error_mark_node)
377 return error_mark_node;
378
6633d636
MS
379 if ((flags & LOOKUP_PROTECT)
380 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
381 && IS_AGGR_TYPE (argtype)
382 && IS_AGGR_TYPE (target_type))
383 {
2db1ab2d 384 /* We go through lookup_base for the access control. */
338d90b8 385 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
6633d636
MS
386 if (binfo == error_mark_node)
387 return error_mark_node;
388 if (binfo == NULL_TREE)
389 return error_not_base_type (target_type, argtype);
338d90b8 390 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
6633d636 391 }
eb66be0e
MS
392 else
393 rval
394 = convert_to_pointer_force (build_pointer_type (target_type), rval);
7993382e 395 return build_nop (type, rval);
8d08fdba
MS
396}
397
08aead78
NS
398/* Subroutine of convert_to_reference. REFTYPE is the target reference type.
399 INTYPE is the original rvalue type and DECL is an optional _DECL node
400 for diagnostics.
401
402 [dcl.init.ref] says that if an rvalue is used to
403 initialize a reference, then the reference must be to a
404 non-volatile const type. */
405
406static void
b746c5dc 407warn_ref_binding (tree reftype, tree intype, tree decl)
08aead78
NS
408{
409 tree ttl = TREE_TYPE (reftype);
410
411 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
412 {
413 const char *msg;
414
415 if (CP_TYPE_VOLATILE_P (ttl) && decl)
939add70
GDR
416 msg = "initialization of volatile reference type %q#T from"
417 " rvalue of type %qT";
08aead78 418 else if (CP_TYPE_VOLATILE_P (ttl))
939add70
GDR
419 msg = "conversion to volatile reference type %q#T "
420 " from rvalue of type %qT";
08aead78 421 else if (decl)
939add70
GDR
422 msg = "initialization of non-const reference type %q#T from"
423 " rvalue of type %qT";
08aead78 424 else
939add70
GDR
425 msg = "conversion to non-const reference type %q#T from"
426 " rvalue of type %qT";
08aead78 427
33bd39a2 428 pedwarn (msg, reftype, intype);
08aead78
NS
429 }
430}
431
8d08fdba
MS
432/* For C++: Only need to do one-level references, but cannot
433 get tripped up on signed/unsigned differences.
434
a4443a08
MS
435 DECL is either NULL_TREE or the _DECL node for a reference that is being
436 initialized. It can be error_mark_node if we don't know the _DECL but
437 we know it's an initialization. */
8d08fdba 438
8d08fdba 439tree
b746c5dc
GDR
440convert_to_reference (tree reftype, tree expr, int convtype,
441 int flags, tree decl)
8d08fdba 442{
926ce8bd
KH
443 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
444 tree intype;
8d08fdba 445 tree rval = NULL_TREE;
8ccc31eb 446 tree rval_as_conversion = NULL_TREE;
a5ac359a 447 bool can_convert_intype_to_type;
8ccc31eb 448
2303a079
MM
449 if (TREE_CODE (type) == FUNCTION_TYPE
450 && TREE_TYPE (expr) == unknown_type_node)
a723baf1
MM
451 expr = instantiate_type (type, expr,
452 (flags & LOOKUP_COMPLAIN)
453 ? tf_error | tf_warning : tf_none);
a723baf1
MM
454
455 if (expr == error_mark_node)
456 return error_mark_node;
457
458 intype = TREE_TYPE (expr);
e6e174e5 459
50bc768d 460 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
8d08fdba 461
8d08fdba
MS
462 intype = TYPE_MAIN_VARIANT (intype);
463
a5ac359a
MM
464 can_convert_intype_to_type = can_convert (type, intype);
465 if (!can_convert_intype_to_type
466 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
8ccc31eb
MS
467 && ! (flags & LOOKUP_NO_CONVERSION))
468 {
469 /* Look for a user-defined conversion to lvalue that we can use. */
470
277294d7 471 rval_as_conversion
7993382e 472 = build_type_conversion (reftype, expr);
8ccc31eb
MS
473
474 if (rval_as_conversion && rval_as_conversion != error_mark_node
475 && real_lvalue_p (rval_as_conversion))
476 {
477 expr = rval_as_conversion;
478 rval_as_conversion = NULL_TREE;
479 intype = type;
a5ac359a 480 can_convert_intype_to_type = 1;
8ccc31eb
MS
481 }
482 }
483
a5ac359a
MM
484 if (((convtype & CONV_STATIC) && can_convert (intype, type))
485 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
8d08fdba 486 {
8d08fdba
MS
487 if (flags & LOOKUP_COMPLAIN)
488 {
39211cd5 489 tree ttl = TREE_TYPE (reftype);
8cd4c175 490 tree ttr = lvalue_type (expr);
8d08fdba 491
08aead78
NS
492 if (! real_lvalue_p (expr))
493 warn_ref_binding (reftype, intype, decl);
494
495 if (! (convtype & CONV_CONST)
91063b51 496 && !at_least_as_qualified_p (ttl, ttr))
939add70
GDR
497 pedwarn ("conversion from %qT to %qT discards qualifiers",
498 ttr, reftype);
8926095f
MS
499 }
500
08ac397c 501 return build_up_reference (reftype, expr, flags, decl);
8d08fdba 502 }
21474714 503 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
8926095f
MS
504 {
505 /* When casting an lvalue to a reference type, just convert into
506 a pointer to the new type and deference it. This is allowed
a28e3c7f 507 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
8926095f 508 should be done directly (jason). (int &)ri ---> *(int*)&ri */
a28e3c7f 509
59be0cdd 510 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
a28e3c7f 511 meant. */
8ccc31eb 512 if (TREE_CODE (intype) == POINTER_TYPE
c8a209ca
NS
513 && (comptypes (TREE_TYPE (intype), type,
514 COMPARE_BASE | COMPARE_DERIVED)))
939add70 515 warning ("casting %qT to %qT does not dereference pointer",
c8a209ca 516 intype, reftype);
a28e3c7f 517
8926095f
MS
518 rval = build_unary_op (ADDR_EXPR, expr, 0);
519 if (rval != error_mark_node)
3c215895
JM
520 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
521 rval, 0);
8926095f 522 if (rval != error_mark_node)
7177d104 523 rval = build1 (NOP_EXPR, reftype, rval);
8926095f 524 }
277294d7 525 else
faf5394a
MS
526 {
527 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
528 "converting", 0, 0);
09ad2917
DD
529 if (rval == NULL_TREE || rval == error_mark_node)
530 return rval;
08aead78 531 warn_ref_binding (reftype, intype, decl);
08ac397c 532 rval = build_up_reference (reftype, rval, flags, decl);
faf5394a 533 }
8d08fdba
MS
534
535 if (rval)
536 {
e92cc029 537 /* If we found a way to convert earlier, then use it. */
8d08fdba
MS
538 return rval;
539 }
540
878cd289 541 if (flags & LOOKUP_COMPLAIN)
939add70 542 error ("cannot convert type %qT to type %qT", intype, reftype);
878cd289 543
8d08fdba
MS
544 return error_mark_node;
545}
546
547/* We are using a reference VAL for its value. Bash that reference all the
e92cc029
MS
548 way down to its lowest form. */
549
8d08fdba 550tree
b746c5dc 551convert_from_reference (tree val)
8d08fdba 552{
8d245821 553 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
db24eb1f
NS
554 {
555 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
556 tree ref = build1 (INDIRECT_REF, t, val);
557
558 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
559 so that we get the proper error message if the result is used
560 to assign to. Also, &* is supposed to be a no-op. */
561 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
562 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
563 TREE_SIDE_EFFECTS (ref)
564 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
565 REFERENCE_REF_P (ref) = 1;
566 val = ref;
567 }
568
8d08fdba
MS
569 return val;
570}
01f9e964
JM
571
572/* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
573 preserving cv-qualification. */
574
575tree
b746c5dc 576convert_lvalue (tree totype, tree expr)
01f9e964
JM
577{
578 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
579 totype = build_reference_type (totype);
580 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
581 NULL_TREE);
582 return convert_from_reference (expr);
583}
f7b9026e
JM
584
585/* Really perform an lvalue-to-rvalue conversion, including copying an
586 argument of class type into a temporary. */
587
588tree
589force_rvalue (tree expr)
590{
591 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
592 expr = ocp_convert (TREE_TYPE (expr), expr,
593 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
594 else
595 expr = decay_conversion (expr);
596
597 return expr;
598}
8d08fdba 599\f
37c46b43
MS
600/* C++ conversions, preference to static cast conversions. */
601
602tree
b746c5dc 603cp_convert (tree type, tree expr)
37c46b43
MS
604{
605 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
606}
607
878cd289
MS
608/* Conversion...
609
610 FLAGS indicates how we should behave. */
611
8d08fdba 612tree
b746c5dc 613ocp_convert (tree type, tree expr, int convtype, int flags)
8d08fdba 614{
926ce8bd
KH
615 tree e = expr;
616 enum tree_code code = TREE_CODE (type);
8d08fdba 617
a5ac359a 618 if (error_operand_p (e) || type == error_mark_node)
8d08fdba 619 return error_mark_node;
a4443a08 620
5d73aa63
MM
621 complete_type (type);
622 complete_type (TREE_TYPE (expr));
623
fc611ce0 624 e = decl_constant_value (e);
9c0d0367 625
dc26f471
JM
626 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
627 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
628 don't go through finish_struct, so they don't have the synthesized
629 constructors. So don't force a temporary. */
630 && TYPE_HAS_CONSTRUCTOR (type))
8ccc31eb
MS
631 /* We need a new temporary; don't take this shortcut. */;
632 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
01240200 633 {
3bfdc719 634 if (same_type_p (type, TREE_TYPE (e)))
01240200
MM
635 /* The call to fold will not always remove the NOP_EXPR as
636 might be expected, since if one of the types is a typedef;
34cd5ae7 637 the comparison in fold is just equality of pointers, not a
953360c8
MM
638 call to comptypes. We don't call fold in this case because
639 that can result in infinite recursion; fold will call
640 convert, which will call ocp_convert, etc. */
641 return e;
a04678ca
GDR
642 /* For complex data types, we need to perform componentwise
643 conversion. */
644 else if (TREE_CODE (type) == COMPLEX_TYPE)
455f19cb 645 return fold_if_not_in_template (convert_to_complex (type, e));
4e8dca1c
JM
646 else if (TREE_CODE (e) == TARGET_EXPR)
647 {
648 /* Don't build a NOP_EXPR of class type. Instead, change the
649 type of the temporary. Only allow this for cv-qual changes,
650 though. */
8dc2b103
NS
651 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
652 TYPE_MAIN_VARIANT (type)));
4e8dca1c
JM
653 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
654 return e;
655 }
01240200 656 else
8dc2b103
NS
657 {
658 /* We shouldn't be treating objects of ADDRESSABLE type as
659 rvalues. */
660 gcc_assert (!TREE_ADDRESSABLE (type));
455f19cb 661 return fold_if_not_in_template (build_nop (type, e));
8dc2b103 662 }
01240200
MM
663 }
664
a4443a08 665 if (code == VOID_TYPE && (convtype & CONV_STATIC))
848b92e1 666 {
02cac427 667 e = convert_to_void (e, /*implicit=*/NULL);
66543169 668 return e;
848b92e1 669 }
a4443a08 670
2986ae00 671 if (INTEGRAL_CODE_P (code))
8d08fdba 672 {
f0e01782 673 tree intype = TREE_TYPE (e);
824b9a4c
MS
674 /* enum = enum, enum = int, enum = float, (enum)pointer are all
675 errors. */
84663f74 676 if (TREE_CODE (type) == ENUMERAL_TYPE
b6ab6892
GB
677 && (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
678 || TREE_CODE (intype) == REAL_TYPE)
679 && ! (convtype & CONV_STATIC))
680 || TREE_CODE (intype) == POINTER_TYPE))
8d08fdba 681 {
b6ab6892
GB
682 if (flags & LOOKUP_COMPLAIN)
683 pedwarn ("conversion from %q#T to %q#T", intype, type);
8d08fdba
MS
684
685 if (flag_pedantic_errors)
686 return error_mark_node;
687 }
a292b002 688 if (IS_AGGR_TYPE (intype))
8d08fdba
MS
689 {
690 tree rval;
7993382e 691 rval = build_type_conversion (type, e);
00595019
MS
692 if (rval)
693 return rval;
878cd289 694 if (flags & LOOKUP_COMPLAIN)
939add70 695 error ("%q#T used where a %qT was expected", intype, type);
8d08fdba
MS
696 return error_mark_node;
697 }
2986ae00 698 if (code == BOOLEAN_TYPE)
1998463c
SB
699 return cp_truthvalue_conversion (e);
700
455f19cb 701 return fold_if_not_in_template (convert_to_integer (type, e));
8d08fdba 702 }
a5ac359a 703 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
455f19cb 704 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
bb37c4a5 705 if (code == VECTOR_TYPE)
037cc9c5
FJ
706 {
707 tree in_vtype = TREE_TYPE (e);
708 if (IS_AGGR_TYPE (in_vtype))
709 {
710 tree ret_val;
711 ret_val = build_type_conversion (type, e);
712 if (ret_val)
713 return ret_val;
714 if (flags & LOOKUP_COMPLAIN)
939add70 715 error ("%q#T used where a %qT was expected", in_vtype, type);
037cc9c5
FJ
716 return error_mark_node;
717 }
455f19cb 718 return fold_if_not_in_template (convert_to_vector (type, e));
037cc9c5 719 }
37c46b43 720 if (code == REAL_TYPE || code == COMPLEX_TYPE)
8d08fdba
MS
721 {
722 if (IS_AGGR_TYPE (TREE_TYPE (e)))
723 {
724 tree rval;
7993382e 725 rval = build_type_conversion (type, e);
8d08fdba
MS
726 if (rval)
727 return rval;
728 else
878cd289 729 if (flags & LOOKUP_COMPLAIN)
939add70 730 error ("%q#T used where a floating point value was expected",
878cd289 731 TREE_TYPE (e));
8d08fdba 732 }
37c46b43 733 if (code == REAL_TYPE)
455f19cb 734 return fold_if_not_in_template (convert_to_real (type, e));
37c46b43 735 else if (code == COMPLEX_TYPE)
455f19cb 736 return fold_if_not_in_template (convert_to_complex (type, e));
8d08fdba
MS
737 }
738
739 /* New C++ semantics: since assignment is now based on
740 memberwise copying, if the rhs type is derived from the
741 lhs type, then we may still do a conversion. */
742 if (IS_AGGR_TYPE_CODE (code))
743 {
744 tree dtype = TREE_TYPE (e);
db5ae43f 745 tree ctor = NULL_TREE;
8d08fdba 746
8d08fdba
MS
747 dtype = TYPE_MAIN_VARIANT (dtype);
748
8d08fdba
MS
749 /* Conversion between aggregate types. New C++ semantics allow
750 objects of derived type to be cast to objects of base type.
751 Old semantics only allowed this between pointers.
752
753 There may be some ambiguity between using a constructor
754 vs. using a type conversion operator when both apply. */
755
277294d7 756 ctor = e;
faf5394a 757
a7a64a77
MM
758 if (abstract_virtuals_error (NULL_TREE, type))
759 return error_mark_node;
59e76fc6 760
277294d7
JM
761 if ((flags & LOOKUP_ONLYCONVERTING)
762 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
62c154ed
JM
763 /* For copy-initialization, first we create a temp of the proper type
764 with a user-defined conversion sequence, then we direct-initialize
765 the target with the temp (see [dcl.init]). */
766 ctor = build_user_type_conversion (type, ctor, flags);
5e818b93 767 else
4ba126e4
MM
768 ctor = build_special_member_call (NULL_TREE,
769 complete_ctor_identifier,
770 build_tree_list (NULL_TREE, ctor),
cad7e87b 771 type, flags);
277294d7
JM
772 if (ctor)
773 return build_cplus_new (type, ctor);
8d08fdba
MS
774 }
775
878cd289 776 if (flags & LOOKUP_COMPLAIN)
939add70
GDR
777 error ("conversion from %qT to non-scalar type %qT requested",
778 TREE_TYPE (expr), type);
8d08fdba
MS
779 return error_mark_node;
780}
781
02cac427
NS
782/* When an expression is used in a void context, its value is discarded and
783 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
784 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
785 in a void context. The C++ standard does not define what an `access' to an
cd0be382 786 object is, but there is reason to believe that it is the lvalue to rvalue
02cac427
NS
787 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
788 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
789 indicates that volatile semantics should be the same between C and C++
790 where ever possible. C leaves it implementation defined as to what
791 constitutes an access to a volatile. So, we interpret `*vp' as a read of
792 the volatile object `vp' points to, unless that is an incomplete type. For
793 volatile references we do not do this interpretation, because that would
794 make it impossible to ignore the reference return value from functions. We
795 issue warnings in the confusing cases.
796
797 IMPLICIT is tells us the context of an implicit void conversion. */
798
799tree
b746c5dc 800convert_to_void (tree expr, const char *implicit)
02cac427 801{
07c88314
MM
802 if (expr == error_mark_node
803 || TREE_TYPE (expr) == error_mark_node)
804 return error_mark_node;
02cac427
NS
805 if (!TREE_TYPE (expr))
806 return expr;
c8b2e872
MM
807 if (invalid_nonstatic_memfn_p (expr))
808 return error_mark_node;
b72801e2 809 if (VOID_TYPE_P (TREE_TYPE (expr)))
02cac427
NS
810 return expr;
811 switch (TREE_CODE (expr))
812 {
813 case COND_EXPR:
814 {
815 /* The two parts of a cond expr might be separate lvalues. */
816 tree op1 = TREE_OPERAND (expr,1);
817 tree op2 = TREE_OPERAND (expr,2);
e895113a
NS
818 tree new_op1 = convert_to_void
819 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
820 ? "second operand of conditional" : NULL));
821 tree new_op2 = convert_to_void
822 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
823 ? "third operand of conditional" : NULL));
02cac427 824
f293ce4b
RS
825 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
826 TREE_OPERAND (expr, 0), new_op1, new_op2);
02cac427
NS
827 break;
828 }
829
830 case COMPOUND_EXPR:
831 {
832 /* The second part of a compound expr contains the value. */
833 tree op1 = TREE_OPERAND (expr,1);
e895113a 834 tree new_op1 = convert_to_void
6de9cd9a 835 (op1, (implicit && !TREE_NO_WARNING (expr)
f3da0ead 836 ? "right-hand operand of comma" : NULL));
02cac427
NS
837
838 if (new_op1 != op1)
9a52d09b 839 {
f293ce4b
RS
840 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
841 TREE_OPERAND (expr, 0), new_op1);
9a52d09b
MM
842 expr = t;
843 }
844
02cac427
NS
845 break;
846 }
847
848 case NON_LVALUE_EXPR:
849 case NOP_EXPR:
00a17e31 850 /* These have already decayed to rvalue. */
02cac427
NS
851 break;
852
f4f206f4 853 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
02cac427
NS
854 break;
855
856 case INDIRECT_REF:
857 {
858 tree type = TREE_TYPE (expr);
859 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
860 == REFERENCE_TYPE;
861 int is_volatile = TYPE_VOLATILE (type);
d0f062fb 862 int is_complete = COMPLETE_TYPE_P (complete_type (type));
02cac427
NS
863
864 if (is_volatile && !is_complete)
939add70
GDR
865 warning ("object of incomplete type %qT will not be accessed in %s",
866 type, implicit ? implicit : "void context");
02cac427 867 else if (is_reference && is_volatile)
939add70
GDR
868 warning ("object of type %qT will not be accessed in %s",
869 TREE_TYPE (TREE_OPERAND (expr, 0)),
870 implicit ? implicit : "void context");
02cac427
NS
871 if (is_reference || !is_volatile || !is_complete)
872 expr = TREE_OPERAND (expr, 0);
873
874 break;
875 }
876
877 case VAR_DECL:
878 {
879 /* External variables might be incomplete. */
880 tree type = TREE_TYPE (expr);
d0f062fb 881 int is_complete = COMPLETE_TYPE_P (complete_type (type));
02cac427
NS
882
883 if (TYPE_VOLATILE (type) && !is_complete)
939add70
GDR
884 warning ("object %qE of incomplete type %qT will not be accessed in %s",
885 expr, type, implicit ? implicit : "void context");
02cac427
NS
886 break;
887 }
2bdb0643 888
02cac427
NS
889 default:;
890 }
891 {
892 tree probe = expr;
893
894 if (TREE_CODE (probe) == ADDR_EXPR)
895 probe = TREE_OPERAND (expr, 0);
96d6c610
JM
896 if (type_unknown_p (probe))
897 {
898 /* [over.over] enumerates the places where we can take the address
899 of an overloaded function, and this is not one of them. */
33bd39a2 900 pedwarn ("%s cannot resolve address of overloaded function",
96d6c610 901 implicit ? implicit : "void cast");
394b9d99 902 expr = void_zero_node;
96d6c610
JM
903 }
904 else if (implicit && probe == expr && is_overloaded_fn (probe))
02cac427 905 /* Only warn when there is no &. */
939add70 906 warning ("%s is a reference, not call, to function %qE",
96d6c610 907 implicit, expr);
02cac427
NS
908 }
909
b72801e2 910 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
02cac427 911 {
6de9cd9a
DN
912 if (implicit && warn_unused_value
913 && !TREE_SIDE_EFFECTS (expr) && !TREE_NO_WARNING (expr))
e895113a
NS
914 warning ("%s has no effect", implicit);
915 expr = build1 (CONVERT_EXPR, void_type_node, expr);
02cac427
NS
916 }
917 return expr;
918}
919
a4443a08
MS
920/* Create an expression whose value is that of EXPR,
921 converted to type TYPE. The TREE_TYPE of the value
922 is always TYPE. This function implements all reasonable
923 conversions; callers should filter out those that are
37c46b43
MS
924 not permitted by the language being compiled.
925
926 Most of this routine is from build_reinterpret_cast.
927
928 The backend cannot call cp_convert (what was convert) because
929 conversions to/from basetypes may involve memory references
930 (vbases) and adding or subtracting small values (multiple
931 inheritance), but it calls convert from the constant folding code
7dfa399d 932 on subtrees of already built trees after it has ripped them apart.
37c46b43
MS
933
934 Also, if we ever support range variables, we'll probably also have to
935 do a little bit more work. */
a4443a08
MS
936
937tree
b746c5dc 938convert (tree type, tree expr)
a4443a08 939{
37c46b43
MS
940 tree intype;
941
942 if (type == error_mark_node || expr == error_mark_node)
943 return error_mark_node;
944
37c46b43
MS
945 intype = TREE_TYPE (expr);
946
6633d636 947 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
37c46b43 948 {
fc611ce0 949 expr = decl_constant_value (expr);
455f19cb 950 return fold_if_not_in_template (build_nop (type, expr));
37c46b43
MS
951 }
952
953 return ocp_convert (type, expr, CONV_OLD_CONVERT,
954 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
a4443a08
MS
955}
956
37c46b43 957/* Like cp_convert, except permit conversions to take place which
8d08fdba
MS
958 are not normally allowed due to access restrictions
959 (such as conversion from sub-type to private super-type). */
e92cc029 960
8d08fdba 961tree
b746c5dc 962convert_force (tree type, tree expr, int convtype)
8d08fdba 963{
926ce8bd
KH
964 tree e = expr;
965 enum tree_code code = TREE_CODE (type);
8d08fdba
MS
966
967 if (code == REFERENCE_TYPE)
455f19cb
MM
968 return (fold_if_not_in_template
969 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
970 NULL_TREE)));
8d08fdba
MS
971
972 if (code == POINTER_TYPE)
455f19cb 973 return fold_if_not_in_template (convert_to_pointer_force (type, e));
8d08fdba 974
51c184be 975 /* From typeck.c convert_for_assignment */
8d08fdba
MS
976 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
977 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
978 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
51c184be
MS
979 || integer_zerop (e)
980 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
8d08fdba 981 && TYPE_PTRMEMFUNC_P (type))
08e17d9d
MM
982 /* compatible pointer to member functions. */
983 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
984 /*c_cast_p=*/1);
6060a796 985
37c46b43 986 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
8d08fdba
MS
987}
988
8d08fdba
MS
989/* Convert an aggregate EXPR to type XTYPE. If a conversion
990 exists, return the attempted conversion. This may
991 return ERROR_MARK_NODE if the conversion is not
992 allowed (references private members, etc).
993 If no conversion exists, NULL_TREE is returned.
994
f30432d7
MS
995 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
996 object parameter, or by the second standard conversion sequence if
997 that doesn't do it. This will probably wait for an overloading rewrite.
998 (jason 8/9/95) */
8d08fdba 999
8d08fdba 1000tree
7993382e 1001build_type_conversion (tree xtype, tree expr)
8d08fdba
MS
1002{
1003 /* C++: check to see if we can convert this aggregate type
e1cd6e56 1004 into the required type. */
7993382e 1005 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
8d08fdba
MS
1006}
1007
b7484fbe
MS
1008/* Convert the given EXPR to one of a group of types suitable for use in an
1009 expression. DESIRES is a combination of various WANT_* flags (q.v.)
b746c5dc 1010 which indicates which types are suitable. If COMPLAIN is true, complain
b7484fbe 1011 about ambiguity; otherwise, the caller will deal with it. */
8d08fdba 1012
b7484fbe 1013tree
b746c5dc 1014build_expr_type_conversion (int desires, tree expr, bool complain)
8d08fdba 1015{
b7484fbe 1016 tree basetype = TREE_TYPE (expr);
b370501f 1017 tree conv = NULL_TREE;
02020185 1018 tree winner = NULL_TREE;
8d08fdba 1019
03d0f4af
MM
1020 if (expr == null_node
1021 && (desires & WANT_INT)
1022 && !(desires & WANT_NULL))
33bd39a2 1023 warning ("converting NULL to non-pointer type");
03d0f4af 1024
f30432d7 1025 basetype = TREE_TYPE (expr);
8d08fdba 1026
07c88314
MM
1027 if (basetype == error_mark_node)
1028 return error_mark_node;
1029
02020185
JM
1030 if (! IS_AGGR_TYPE (basetype))
1031 switch (TREE_CODE (basetype))
1032 {
1033 case INTEGER_TYPE:
03d0f4af 1034 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
02020185
JM
1035 return expr;
1036 /* else fall through... */
1037
1038 case BOOLEAN_TYPE:
1039 return (desires & WANT_INT) ? expr : NULL_TREE;
1040 case ENUMERAL_TYPE:
1041 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1042 case REAL_TYPE:
1043 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1044 case POINTER_TYPE:
1045 return (desires & WANT_POINTER) ? expr : NULL_TREE;
3c215895 1046
02020185
JM
1047 case FUNCTION_TYPE:
1048 case ARRAY_TYPE:
0a72704b 1049 return (desires & WANT_POINTER) ? decay_conversion (expr)
02020185
JM
1050 : NULL_TREE;
1051 default:
1052 return NULL_TREE;
1053 }
1054
1055 /* The code for conversions from class type is currently only used for
1056 delete expressions. Other expressions are handled by build_new_op. */
29f4ceab
GB
1057 if (!complete_type_or_else (basetype, expr))
1058 return error_mark_node;
1059 if (!TYPE_HAS_CONVERSION (basetype))
02020185
JM
1060 return NULL_TREE;
1061
1062 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1063 {
1064 int win = 0;
1065 tree candidate;
1066 tree cand = TREE_VALUE (conv);
1067
1068 if (winner && winner == cand)
1069 continue;
1070
ee76b931 1071 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
02020185
JM
1072
1073 switch (TREE_CODE (candidate))
1074 {
1075 case BOOLEAN_TYPE:
1076 case INTEGER_TYPE:
1077 win = (desires & WANT_INT); break;
1078 case ENUMERAL_TYPE:
1079 win = (desires & WANT_ENUM); break;
1080 case REAL_TYPE:
1081 win = (desires & WANT_FLOAT); break;
1082 case POINTER_TYPE:
1083 win = (desires & WANT_POINTER); break;
1084
1085 default:
1086 break;
1087 }
1088
1089 if (win)
1090 {
1091 if (winner)
1092 {
1093 if (complain)
1094 {
939add70
GDR
1095 error ("ambiguous default type conversion from %qT",
1096 basetype);
1097 error (" candidate conversions include %qD and %qD",
1098 winner, cand);
02020185
JM
1099 }
1100 return error_mark_node;
1101 }
1102 else
1103 winner = cand;
1104 }
1105 }
b7484fbe 1106
02020185
JM
1107 if (winner)
1108 {
ee76b931 1109 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
02020185 1110 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
8d08fdba 1111 }
b7484fbe 1112
3c215895 1113 return NULL_TREE;
8d08fdba 1114}
39211cd5 1115
e92cc029
MS
1116/* Implements integral promotion (4.1) and float->double promotion. */
1117
39211cd5 1118tree
b746c5dc 1119type_promotes_to (tree type)
39211cd5 1120{
f30432d7
MS
1121 if (type == error_mark_node)
1122 return error_mark_node;
1123
39211cd5 1124 type = TYPE_MAIN_VARIANT (type);
2986ae00
MS
1125
1126 /* bool always promotes to int (not unsigned), even if it's the same
1127 size. */
28ed4616 1128 if (type == boolean_type_node)
2986ae00
MS
1129 type = integer_type_node;
1130
1131 /* Normally convert enums to int, but convert wide enums to something
1132 wider. */
1133 else if (TREE_CODE (type) == ENUMERAL_TYPE
1134 || type == wchar_type_node)
cf2ac46f
JM
1135 {
1136 int precision = MAX (TYPE_PRECISION (type),
1137 TYPE_PRECISION (integer_type_node));
b0c48229 1138 tree totype = c_common_type_for_size (precision, 0);
8df83eae 1139 if (TYPE_UNSIGNED (type)
cf2ac46f 1140 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
b0c48229 1141 type = c_common_type_for_size (precision, 1);
cf2ac46f
JM
1142 else
1143 type = totype;
1144 }
d72040f5 1145 else if (c_promoting_integer_type_p (type))
39211cd5 1146 {
3d4683cb 1147 /* Retain unsignedness if really not getting bigger. */
8df83eae 1148 if (TYPE_UNSIGNED (type)
3d4683cb 1149 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
39211cd5
MS
1150 type = unsigned_type_node;
1151 else
1152 type = integer_type_node;
1153 }
1154 else if (type == float_type_node)
1155 type = double_type_node;
0a72704b
MM
1156
1157 return type;
39211cd5 1158}
75650646 1159
75650646
MM
1160/* The routines below this point are carefully written to conform to
1161 the standard. They use the same terminology, and follow the rules
1162 closely. Although they are used only in pt.c at the moment, they
1163 should presumably be used everywhere in the future. */
1164
e1467ff2
MM
1165/* Attempt to perform qualification conversions on EXPR to convert it
1166 to TYPE. Return the resulting expression, or error_mark_node if
1167 the conversion was impossible. */
1168
75650646 1169tree
b746c5dc 1170perform_qualification_conversions (tree type, tree expr)
75650646 1171{
a5ac359a
MM
1172 tree expr_type;
1173
1174 expr_type = TREE_TYPE (expr);
1175
1176 if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1177 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1178 return build_nop (type, expr);
1179 else if (TYPE_PTR_TO_MEMBER_P (type)
1180 && TYPE_PTR_TO_MEMBER_P (expr_type)
1181 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1182 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1183 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1184 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1185 return build_nop (type, expr);
e1467ff2
MM
1186 else
1187 return error_mark_node;
75650646 1188}