]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Language-level data type conversion for GNU C++. |
107c7f39 | 2 | Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
3eccc2db | 3 | 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
471086d6 | 4 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
5 | ||
6f0d25a6 | 6 | This file is part of GCC. |
471086d6 | 7 | |
6f0d25a6 | 8 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
6f0d25a6 | 13 | GCC is distributed in the hope that it will be useful, |
471086d6 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
6f0d25a6 | 19 | along with GCC; see the file COPYING. If not, write to |
c58d4270 | 20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
471086d6 | 22 | |
23 | ||
af86cc06 | 24 | /* This file contains the functions for converting C++ expressions |
471086d6 | 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" | |
b3ef7553 | 30 | #include "system.h" |
805e22b2 | 31 | #include "coretypes.h" |
32 | #include "tm.h" | |
471086d6 | 33 | #include "tree.h" |
34 | #include "flags.h" | |
35 | #include "cp-tree.h" | |
471086d6 | 36 | #include "convert.h" |
f0eaeecd | 37 | #include "toplev.h" |
2e8226f7 | 38 | #include "decl.h" |
96624a9e | 39 | |
35771a9a | 40 | static tree cp_convert_to_pointer (tree, tree, bool); |
41 | static tree convert_to_pointer_force (tree, tree); | |
42 | static tree build_up_reference (tree, tree, int, tree); | |
43 | static void warn_ref_binding (tree, tree, tree); | |
71ccdfff | 44 | |
471086d6 | 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), | |
aff9e656 | 57 | and c_common_truthvalue_conversion. |
471086d6 | 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 | ||
471086d6 | 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 | |
045ed8f8 | 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). */ | |
96624a9e | 75 | |
471086d6 | 76 | static tree |
35771a9a | 77 | cp_convert_to_pointer (tree type, tree expr, bool force) |
471086d6 | 78 | { |
cd16867a | 79 | tree intype = TREE_TYPE (expr); |
80 | enum tree_code form; | |
f82eeb87 | 81 | tree rval; |
8bf328bf | 82 | if (intype == error_mark_node) |
83 | return error_mark_node; | |
74002e1d | 84 | |
96624a9e | 85 | if (IS_AGGR_TYPE (intype)) |
86 | { | |
96624a9e | 87 | intype = complete_type (intype); |
4b72716d | 88 | if (!COMPLETE_TYPE_P (intype)) |
96624a9e | 89 | { |
0a25aad6 | 90 | error ("can't convert from incomplete type %qT to %qT", |
91 | intype, type); | |
96624a9e | 92 | return error_mark_node; |
93 | } | |
94 | ||
8999978b | 95 | rval = build_type_conversion (type, expr); |
96624a9e | 96 | if (rval) |
97 | { | |
98 | if (rval == error_mark_node) | |
0a25aad6 | 99 | error ("conversion of %qE from %qT to %qT is ambiguous", |
100 | expr, intype, type); | |
96624a9e | 101 | return rval; |
102 | } | |
103 | } | |
104 | ||
860740a7 | 105 | /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */ |
38281c46 | 106 | if (TREE_CODE (type) == POINTER_TYPE |
107 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
e3cfe3ce | 108 | || VOID_TYPE_P (TREE_TYPE (type)))) |
38281c46 | 109 | { |
6ab399e8 | 110 | if (TYPE_PTRMEMFUNC_P (intype) |
111 | || TREE_CODE (intype) == METHOD_TYPE) | |
112 | return convert_member_func_to_ptr (type, expr); | |
a63bc44c | 113 | if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE) |
114 | return build_nop (type, expr); | |
38281c46 | 115 | intype = TREE_TYPE (expr); |
116 | } | |
117 | ||
311e42bc | 118 | if (expr == error_mark_node) |
119 | return error_mark_node; | |
120 | ||
74002e1d | 121 | form = TREE_CODE (intype); |
122 | ||
2b77484d | 123 | if (POINTER_TYPE_P (intype)) |
471086d6 | 124 | { |
125 | intype = TYPE_MAIN_VARIANT (intype); | |
126 | ||
127 | if (TYPE_MAIN_VARIANT (type) != intype | |
2b77484d | 128 | && TREE_CODE (type) == POINTER_TYPE |
471086d6 | 129 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE |
bccffb3a | 130 | && IS_AGGR_TYPE (TREE_TYPE (type)) |
131 | && IS_AGGR_TYPE (TREE_TYPE (intype)) | |
4a2680fc | 132 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
471086d6 | 133 | { |
134 | enum tree_code code = PLUS_EXPR; | |
4a2680fc | 135 | tree binfo; |
8999978b | 136 | tree intype_class; |
137 | tree type_class; | |
138 | bool same_p; | |
4a2680fc | 139 | |
8999978b | 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; | |
c0af329c | 146 | /* Try derived to base conversion. */ |
8999978b | 147 | if (!same_p) |
148 | binfo = lookup_base (intype_class, type_class, ba_check, NULL); | |
149 | if (!same_p && !binfo) | |
471086d6 | 150 | { |
c0af329c | 151 | /* Try base to derived conversion. */ |
8999978b | 152 | binfo = lookup_base (type_class, intype_class, ba_check, NULL); |
471086d6 | 153 | code = MINUS_EXPR; |
154 | } | |
4a2680fc | 155 | if (binfo == error_mark_node) |
156 | return error_mark_node; | |
8999978b | 157 | if (binfo || same_p) |
471086d6 | 158 | { |
8999978b | 159 | if (binfo) |
160 | expr = build_base_path (code, expr, binfo, 0); | |
c0af329c | 161 | /* Add any qualifier conversions. */ |
8999978b | 162 | return build_nop (type, expr); |
471086d6 | 163 | } |
164 | } | |
471086d6 | 165 | |
1bc16cab | 166 | if (TYPE_PTRMEMFUNC_P (type)) |
bea7d742 | 167 | { |
0a25aad6 | 168 | error ("cannot convert %qE from type %qT to type %qT", |
169 | expr, intype, type); | |
1bc16cab | 170 | return error_mark_node; |
171 | } | |
a17aefa2 | 172 | |
1bc16cab | 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; | |
a17aefa2 | 193 | |
1bc16cab | 194 | if (bk == bk_via_virtual) |
195 | { | |
196 | if (force) | |
0a25aad6 | 197 | warning ("pointer to member cast from %qT to %qT is via" |
198 | " virtual base", intype, type); | |
1bc16cab | 199 | else |
7045d67a | 200 | { |
0a25aad6 | 201 | error ("pointer to member cast from %qT to %qT is" |
202 | " via virtual base", intype, type); | |
1bc16cab | 203 | return error_mark_node; |
7045d67a | 204 | } |
1bc16cab | 205 | /* This is a reinterpret cast, whose result is unspecified. |
206 | We choose to do nothing. */ | |
207 | return build1 (NOP_EXPR, type, expr); | |
ce28ee2e | 208 | } |
209 | ||
1bc16cab | 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)); | |
8999978b | 217 | return build_nop (type, expr); |
471086d6 | 218 | } |
2b77484d | 219 | else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)) |
cb02169c | 220 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, |
221 | /*c_cast_p=*/false); | |
2b77484d | 222 | else if (TYPE_PTRMEMFUNC_P (intype)) |
223 | { | |
a63bc44c | 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 | } | |
0a25aad6 | 237 | error ("cannot convert %qE from type %qT to type %qT", |
238 | expr, intype, type); | |
2b77484d | 239 | return error_mark_node; |
240 | } | |
471086d6 | 241 | |
471086d6 | 242 | if (integer_zerop (expr)) |
243 | { | |
2b77484d | 244 | if (TYPE_PTRMEMFUNC_P (type)) |
cb02169c | 245 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, |
246 | /*c_cast_p=*/false); | |
ad63a0fc | 247 | |
606b494c | 248 | if (TYPE_PTRMEM_P (type)) |
7c446c95 | 249 | { |
250 | /* A NULL pointer-to-member is represented by -1, not by | |
251 | zero. */ | |
7016c612 | 252 | expr = build_int_cst (type, -1); |
7c446c95 | 253 | /* Fix up the representation of -1 if appropriate. */ |
254 | expr = force_fit_type (expr, 0, false, false); | |
255 | } | |
ad63a0fc | 256 | else |
7016c612 | 257 | expr = build_int_cst (type, 0); |
4d28c5d1 | 258 | |
471086d6 | 259 | return expr; |
260 | } | |
1bc16cab | 261 | else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form)) |
de1f960a | 262 | { |
0a25aad6 | 263 | error ("invalid conversion from %qT to %qT", intype, type); |
de1f960a | 264 | return error_mark_node; |
265 | } | |
471086d6 | 266 | |
bb0726a1 | 267 | if (INTEGRAL_CODE_P (form)) |
471086d6 | 268 | { |
c5aa1e92 | 269 | if (TYPE_PRECISION (intype) == POINTER_SIZE) |
471086d6 | 270 | return build1 (CONVERT_EXPR, type, expr); |
771d21fa | 271 | expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr); |
092b1d6f | 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 | ||
471086d6 | 278 | return convert_to_pointer (type, expr); |
279 | } | |
280 | ||
cc4d0855 | 281 | if (type_unknown_p (expr)) |
4b471722 | 282 | return instantiate_type (type, expr, tf_error | tf_warning); |
cc4d0855 | 283 | |
0a25aad6 | 284 | error ("cannot convert %qE from type %qT to type %qT", |
285 | expr, intype, type); | |
471086d6 | 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). */ | |
96624a9e | 292 | |
471086d6 | 293 | static tree |
35771a9a | 294 | convert_to_pointer_force (tree type, tree expr) |
471086d6 | 295 | { |
cd16867a | 296 | tree intype = TREE_TYPE (expr); |
297 | enum tree_code form = TREE_CODE (intype); | |
471086d6 | 298 | |
471086d6 | 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 | |
bccffb3a | 305 | && IS_AGGR_TYPE (TREE_TYPE (type)) |
306 | && IS_AGGR_TYPE (TREE_TYPE (intype)) | |
471086d6 | 307 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
308 | { | |
309 | enum tree_code code = PLUS_EXPR; | |
4a2680fc | 310 | tree binfo; |
311 | ||
312 | binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type), | |
ada40935 | 313 | ba_unique, NULL); |
4a2680fc | 314 | if (!binfo) |
471086d6 | 315 | { |
4a2680fc | 316 | binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
ada40935 | 317 | ba_unique, NULL); |
4a2680fc | 318 | code = MINUS_EXPR; |
471086d6 | 319 | } |
4a2680fc | 320 | if (binfo == error_mark_node) |
321 | return error_mark_node; | |
322 | if (binfo) | |
471086d6 | 323 | { |
4a2680fc | 324 | expr = build_base_path (code, expr, binfo, 0); |
de922179 | 325 | if (expr == error_mark_node) |
326 | return error_mark_node; | |
c0af329c | 327 | /* Add any qualifier conversions. */ |
4a2680fc | 328 | if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)), |
329 | TREE_TYPE (type))) | |
8999978b | 330 | expr = build_nop (type, expr); |
4a2680fc | 331 | return expr; |
471086d6 | 332 | } |
471086d6 | 333 | } |
471086d6 | 334 | } |
335 | ||
35771a9a | 336 | return cp_convert_to_pointer (type, expr, true); |
471086d6 | 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. | |
ca106ab1 | 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. */ | |
96624a9e | 346 | |
471086d6 | 347 | static tree |
35771a9a | 348 | build_up_reference (tree type, tree arg, int flags, tree decl) |
471086d6 | 349 | { |
c76251c1 | 350 | tree rval; |
0543e7a9 | 351 | tree argtype = TREE_TYPE (arg); |
471086d6 | 352 | tree target_type = TREE_TYPE (type); |
471086d6 | 353 | |
b4df430b | 354 | gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); |
471086d6 | 355 | |
c76251c1 | 356 | if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg)) |
471086d6 | 357 | { |
ca106ab1 | 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. */ | |
c76251c1 | 360 | tree targ = arg; |
ca106ab1 | 361 | |
7c09476d | 362 | arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg)); |
c23ebfdd | 363 | |
364 | /* Process the initializer for the declaration. */ | |
38281c46 | 365 | DECL_INITIAL (arg) = targ; |
ce23987e | 366 | cp_finish_decl (arg, targ, NULL_TREE, |
011310f7 | 367 | LOOKUP_ONLYCONVERTING|DIRECT_BIND); |
1ad432f2 | 368 | } |
c76251c1 | 369 | else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg)) |
1d8e4310 | 370 | return get_target_expr (arg); |
1ad432f2 | 371 | |
ca106ab1 | 372 | /* If we had a way to wrap this up, and say, if we ever needed its |
b0df6589 | 373 | address, transform all occurrences of the register, into a memory |
374 | reference we could win better. */ | |
c76251c1 | 375 | rval = build_unary_op (ADDR_EXPR, arg, 1); |
15e55420 | 376 | if (rval == error_mark_node) |
377 | return error_mark_node; | |
378 | ||
6686e84d | 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 | { | |
7e6960e0 | 384 | /* We go through lookup_base for the access control. */ |
4a2680fc | 385 | tree binfo = lookup_base (argtype, target_type, ba_check, NULL); |
6686e84d | 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); | |
4a2680fc | 390 | rval = build_base_path (PLUS_EXPR, rval, binfo, 1); |
6686e84d | 391 | } |
c76251c1 | 392 | else |
393 | rval | |
394 | = convert_to_pointer_force (build_pointer_type (target_type), rval); | |
8999978b | 395 | return build_nop (type, rval); |
471086d6 | 396 | } |
397 | ||
519ba62c | 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 | ||
406 | static void | |
35771a9a | 407 | warn_ref_binding (tree reftype, tree intype, tree decl) |
519ba62c | 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) | |
0a25aad6 | 416 | msg = "initialization of volatile reference type %q#T from" |
417 | " rvalue of type %qT"; | |
519ba62c | 418 | else if (CP_TYPE_VOLATILE_P (ttl)) |
0a25aad6 | 419 | msg = "conversion to volatile reference type %q#T " |
420 | " from rvalue of type %qT"; | |
519ba62c | 421 | else if (decl) |
0a25aad6 | 422 | msg = "initialization of non-const reference type %q#T from" |
423 | " rvalue of type %qT"; | |
519ba62c | 424 | else |
0a25aad6 | 425 | msg = "conversion to non-const reference type %q#T from" |
426 | " rvalue of type %qT"; | |
519ba62c | 427 | |
cf103c6c | 428 | pedwarn (msg, reftype, intype); |
519ba62c | 429 | } |
430 | } | |
431 | ||
471086d6 | 432 | /* For C++: Only need to do one-level references, but cannot |
433 | get tripped up on signed/unsigned differences. | |
434 | ||
d81e00a4 | 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. */ | |
471086d6 | 438 | |
471086d6 | 439 | tree |
35771a9a | 440 | convert_to_reference (tree reftype, tree expr, int convtype, |
441 | int flags, tree decl) | |
471086d6 | 442 | { |
cd16867a | 443 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); |
444 | tree intype; | |
471086d6 | 445 | tree rval = NULL_TREE; |
1a3f833b | 446 | tree rval_as_conversion = NULL_TREE; |
1bc16cab | 447 | bool can_convert_intype_to_type; |
1a3f833b | 448 | |
d32df6a6 | 449 | if (TREE_CODE (type) == FUNCTION_TYPE |
450 | && TREE_TYPE (expr) == unknown_type_node) | |
0a3b29ad | 451 | expr = instantiate_type (type, expr, |
452 | (flags & LOOKUP_COMPLAIN) | |
453 | ? tf_error | tf_warning : tf_none); | |
d32df6a6 | 454 | else |
0a3b29ad | 455 | expr = convert_from_reference (expr); |
456 | ||
457 | if (expr == error_mark_node) | |
458 | return error_mark_node; | |
459 | ||
460 | intype = TREE_TYPE (expr); | |
cc4d0855 | 461 | |
b4df430b | 462 | gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE); |
471086d6 | 463 | |
471086d6 | 464 | intype = TYPE_MAIN_VARIANT (intype); |
465 | ||
1bc16cab | 466 | can_convert_intype_to_type = can_convert (type, intype); |
467 | if (!can_convert_intype_to_type | |
468 | && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype) | |
1a3f833b | 469 | && ! (flags & LOOKUP_NO_CONVERSION)) |
470 | { | |
471 | /* Look for a user-defined conversion to lvalue that we can use. */ | |
472 | ||
0a4be248 | 473 | rval_as_conversion |
8999978b | 474 | = build_type_conversion (reftype, expr); |
1a3f833b | 475 | |
476 | if (rval_as_conversion && rval_as_conversion != error_mark_node | |
477 | && real_lvalue_p (rval_as_conversion)) | |
478 | { | |
479 | expr = rval_as_conversion; | |
480 | rval_as_conversion = NULL_TREE; | |
481 | intype = type; | |
1bc16cab | 482 | can_convert_intype_to_type = 1; |
1a3f833b | 483 | } |
484 | } | |
485 | ||
1bc16cab | 486 | if (((convtype & CONV_STATIC) && can_convert (intype, type)) |
487 | || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type)) | |
471086d6 | 488 | { |
471086d6 | 489 | if (flags & LOOKUP_COMPLAIN) |
490 | { | |
bc3887cf | 491 | tree ttl = TREE_TYPE (reftype); |
cb0ba4ec | 492 | tree ttr = lvalue_type (expr); |
471086d6 | 493 | |
519ba62c | 494 | if (! real_lvalue_p (expr)) |
495 | warn_ref_binding (reftype, intype, decl); | |
496 | ||
497 | if (! (convtype & CONV_CONST) | |
3e04bd45 | 498 | && !at_least_as_qualified_p (ttl, ttr)) |
0a25aad6 | 499 | pedwarn ("conversion from %qT to %qT discards qualifiers", |
500 | ttr, reftype); | |
0543e7a9 | 501 | } |
502 | ||
ca106ab1 | 503 | return build_up_reference (reftype, expr, flags, decl); |
471086d6 | 504 | } |
c07b1ad1 | 505 | else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) |
0543e7a9 | 506 | { |
507 | /* When casting an lvalue to a reference type, just convert into | |
508 | a pointer to the new type and deference it. This is allowed | |
e581f478 | 509 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
0543e7a9 | 510 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
e581f478 | 511 | |
3748625f | 512 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
e581f478 | 513 | meant. */ |
1a3f833b | 514 | if (TREE_CODE (intype) == POINTER_TYPE |
00952d10 | 515 | && (comptypes (TREE_TYPE (intype), type, |
516 | COMPARE_BASE | COMPARE_DERIVED))) | |
0a25aad6 | 517 | warning ("casting %qT to %qT does not dereference pointer", |
00952d10 | 518 | intype, reftype); |
e581f478 | 519 | |
0543e7a9 | 520 | rval = build_unary_op (ADDR_EXPR, expr, 0); |
521 | if (rval != error_mark_node) | |
985e9ee0 | 522 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), |
523 | rval, 0); | |
0543e7a9 | 524 | if (rval != error_mark_node) |
b0722fac | 525 | rval = build1 (NOP_EXPR, reftype, rval); |
0543e7a9 | 526 | } |
0a4be248 | 527 | else |
860740a7 | 528 | { |
529 | rval = convert_for_initialization (NULL_TREE, type, expr, flags, | |
530 | "converting", 0, 0); | |
a66fe4cb | 531 | if (rval == NULL_TREE || rval == error_mark_node) |
532 | return rval; | |
519ba62c | 533 | warn_ref_binding (reftype, intype, decl); |
ca106ab1 | 534 | rval = build_up_reference (reftype, rval, flags, decl); |
860740a7 | 535 | } |
471086d6 | 536 | |
537 | if (rval) | |
538 | { | |
96624a9e | 539 | /* If we found a way to convert earlier, then use it. */ |
471086d6 | 540 | return rval; |
541 | } | |
542 | ||
b248d3f7 | 543 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 544 | error ("cannot convert type %qT to type %qT", intype, reftype); |
b248d3f7 | 545 | |
471086d6 | 546 | return error_mark_node; |
547 | } | |
548 | ||
549 | /* We are using a reference VAL for its value. Bash that reference all the | |
96624a9e | 550 | way down to its lowest form. */ |
551 | ||
471086d6 | 552 | tree |
35771a9a | 553 | convert_from_reference (tree val) |
471086d6 | 554 | { |
120c0017 | 555 | if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE) |
4d698345 | 556 | return build_indirect_ref (val, NULL); |
471086d6 | 557 | return val; |
558 | } | |
dbacd3bd | 559 | |
560 | /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE, | |
561 | preserving cv-qualification. */ | |
562 | ||
563 | tree | |
35771a9a | 564 | convert_lvalue (tree totype, tree expr) |
dbacd3bd | 565 | { |
566 | totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr))); | |
567 | totype = build_reference_type (totype); | |
568 | expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL, | |
569 | NULL_TREE); | |
570 | return convert_from_reference (expr); | |
571 | } | |
e5dab226 | 572 | |
573 | /* Really perform an lvalue-to-rvalue conversion, including copying an | |
574 | argument of class type into a temporary. */ | |
575 | ||
576 | tree | |
577 | force_rvalue (tree expr) | |
578 | { | |
579 | if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR) | |
580 | expr = ocp_convert (TREE_TYPE (expr), expr, | |
581 | CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL); | |
582 | else | |
583 | expr = decay_conversion (expr); | |
584 | ||
585 | return expr; | |
586 | } | |
471086d6 | 587 | \f |
c4a8ac95 | 588 | /* C++ conversions, preference to static cast conversions. */ |
589 | ||
590 | tree | |
35771a9a | 591 | cp_convert (tree type, tree expr) |
c4a8ac95 | 592 | { |
593 | return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL); | |
594 | } | |
595 | ||
b248d3f7 | 596 | /* Conversion... |
597 | ||
598 | FLAGS indicates how we should behave. */ | |
599 | ||
471086d6 | 600 | tree |
35771a9a | 601 | ocp_convert (tree type, tree expr, int convtype, int flags) |
471086d6 | 602 | { |
cd16867a | 603 | tree e = expr; |
604 | enum tree_code code = TREE_CODE (type); | |
471086d6 | 605 | |
1bc16cab | 606 | if (error_operand_p (e) || type == error_mark_node) |
471086d6 | 607 | return error_mark_node; |
d81e00a4 | 608 | |
5b592939 | 609 | complete_type (type); |
610 | complete_type (TREE_TYPE (expr)); | |
611 | ||
1202e392 | 612 | e = decl_constant_value (e); |
7745052b | 613 | |
2133b374 | 614 | if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP) |
615 | /* Some internal structures (vtable_entry_type, sigtbl_ptr_type) | |
616 | don't go through finish_struct, so they don't have the synthesized | |
617 | constructors. So don't force a temporary. */ | |
618 | && TYPE_HAS_CONSTRUCTOR (type)) | |
1a3f833b | 619 | /* We need a new temporary; don't take this shortcut. */; |
620 | else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e))) | |
ca23ec64 | 621 | { |
daf9ff67 | 622 | if (same_type_p (type, TREE_TYPE (e))) |
ca23ec64 | 623 | /* The call to fold will not always remove the NOP_EXPR as |
624 | might be expected, since if one of the types is a typedef; | |
755edffd | 625 | the comparison in fold is just equality of pointers, not a |
f699c174 | 626 | call to comptypes. We don't call fold in this case because |
627 | that can result in infinite recursion; fold will call | |
628 | convert, which will call ocp_convert, etc. */ | |
629 | return e; | |
372e0e29 | 630 | /* For complex data types, we need to perform componentwise |
631 | conversion. */ | |
632 | else if (TREE_CODE (type) == COMPLEX_TYPE) | |
5d7ed6c7 | 633 | return fold_if_not_in_template (convert_to_complex (type, e)); |
bdb2219e | 634 | else if (TREE_CODE (e) == TARGET_EXPR) |
635 | { | |
636 | /* Don't build a NOP_EXPR of class type. Instead, change the | |
637 | type of the temporary. Only allow this for cv-qual changes, | |
638 | though. */ | |
092b1d6f | 639 | gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)), |
640 | TYPE_MAIN_VARIANT (type))); | |
bdb2219e | 641 | TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type; |
642 | return e; | |
643 | } | |
ca23ec64 | 644 | else |
092b1d6f | 645 | { |
646 | /* We shouldn't be treating objects of ADDRESSABLE type as | |
647 | rvalues. */ | |
648 | gcc_assert (!TREE_ADDRESSABLE (type)); | |
5d7ed6c7 | 649 | return fold_if_not_in_template (build_nop (type, e)); |
092b1d6f | 650 | } |
ca23ec64 | 651 | } |
652 | ||
d81e00a4 | 653 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) |
95b2ac55 | 654 | { |
b1a8d4ce | 655 | e = convert_to_void (e, /*implicit=*/NULL); |
aeef2be5 | 656 | return e; |
95b2ac55 | 657 | } |
d81e00a4 | 658 | |
bb0726a1 | 659 | if (INTEGRAL_CODE_P (code)) |
471086d6 | 660 | { |
617abf06 | 661 | tree intype = TREE_TYPE (e); |
02d7f858 | 662 | /* enum = enum, enum = int, enum = float, (enum)pointer are all |
663 | errors. */ | |
723d9ad3 | 664 | if (TREE_CODE (type) == ENUMERAL_TYPE |
02d7f858 | 665 | && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC)) |
666 | || (TREE_CODE (intype) == POINTER_TYPE))) | |
471086d6 | 667 | { |
0a25aad6 | 668 | pedwarn ("conversion from %q#T to %q#T", intype, type); |
471086d6 | 669 | |
670 | if (flag_pedantic_errors) | |
671 | return error_mark_node; | |
672 | } | |
f3ba5c6a | 673 | if (IS_AGGR_TYPE (intype)) |
471086d6 | 674 | { |
675 | tree rval; | |
8999978b | 676 | rval = build_type_conversion (type, e); |
6495357a | 677 | if (rval) |
678 | return rval; | |
b248d3f7 | 679 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 680 | error ("%q#T used where a %qT was expected", intype, type); |
471086d6 | 681 | return error_mark_node; |
682 | } | |
bb0726a1 | 683 | if (code == BOOLEAN_TYPE) |
ce871053 | 684 | return cp_truthvalue_conversion (e); |
685 | ||
5d7ed6c7 | 686 | return fold_if_not_in_template (convert_to_integer (type, e)); |
471086d6 | 687 | } |
1bc16cab | 688 | if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type)) |
5d7ed6c7 | 689 | return fold_if_not_in_template (cp_convert_to_pointer (type, e, false)); |
684b9d79 | 690 | if (code == VECTOR_TYPE) |
c37ff371 | 691 | { |
692 | tree in_vtype = TREE_TYPE (e); | |
693 | if (IS_AGGR_TYPE (in_vtype)) | |
694 | { | |
695 | tree ret_val; | |
696 | ret_val = build_type_conversion (type, e); | |
697 | if (ret_val) | |
698 | return ret_val; | |
699 | if (flags & LOOKUP_COMPLAIN) | |
0a25aad6 | 700 | error ("%q#T used where a %qT was expected", in_vtype, type); |
c37ff371 | 701 | return error_mark_node; |
702 | } | |
5d7ed6c7 | 703 | return fold_if_not_in_template (convert_to_vector (type, e)); |
c37ff371 | 704 | } |
c4a8ac95 | 705 | if (code == REAL_TYPE || code == COMPLEX_TYPE) |
471086d6 | 706 | { |
707 | if (IS_AGGR_TYPE (TREE_TYPE (e))) | |
708 | { | |
709 | tree rval; | |
8999978b | 710 | rval = build_type_conversion (type, e); |
471086d6 | 711 | if (rval) |
712 | return rval; | |
713 | else | |
b248d3f7 | 714 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 715 | error ("%q#T used where a floating point value was expected", |
b248d3f7 | 716 | TREE_TYPE (e)); |
471086d6 | 717 | } |
c4a8ac95 | 718 | if (code == REAL_TYPE) |
5d7ed6c7 | 719 | return fold_if_not_in_template (convert_to_real (type, e)); |
c4a8ac95 | 720 | else if (code == COMPLEX_TYPE) |
5d7ed6c7 | 721 | return fold_if_not_in_template (convert_to_complex (type, e)); |
471086d6 | 722 | } |
723 | ||
724 | /* New C++ semantics: since assignment is now based on | |
725 | memberwise copying, if the rhs type is derived from the | |
726 | lhs type, then we may still do a conversion. */ | |
727 | if (IS_AGGR_TYPE_CODE (code)) | |
728 | { | |
729 | tree dtype = TREE_TYPE (e); | |
c25194fd | 730 | tree ctor = NULL_TREE; |
471086d6 | 731 | |
471086d6 | 732 | dtype = TYPE_MAIN_VARIANT (dtype); |
733 | ||
471086d6 | 734 | /* Conversion between aggregate types. New C++ semantics allow |
735 | objects of derived type to be cast to objects of base type. | |
736 | Old semantics only allowed this between pointers. | |
737 | ||
738 | There may be some ambiguity between using a constructor | |
739 | vs. using a type conversion operator when both apply. */ | |
740 | ||
0a4be248 | 741 | ctor = e; |
860740a7 | 742 | |
8c18e707 | 743 | if (abstract_virtuals_error (NULL_TREE, type)) |
744 | return error_mark_node; | |
6cbb4197 | 745 | |
0a4be248 | 746 | if ((flags & LOOKUP_ONLYCONVERTING) |
747 | && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype))) | |
fce73460 | 748 | /* For copy-initialization, first we create a temp of the proper type |
749 | with a user-defined conversion sequence, then we direct-initialize | |
750 | the target with the temp (see [dcl.init]). */ | |
751 | ctor = build_user_type_conversion (type, ctor, flags); | |
3eb89cd8 | 752 | else |
f70cb9e6 | 753 | ctor = build_special_member_call (NULL_TREE, |
754 | complete_ctor_identifier, | |
755 | build_tree_list (NULL_TREE, ctor), | |
a6460bf1 | 756 | type, flags); |
0a4be248 | 757 | if (ctor) |
758 | return build_cplus_new (type, ctor); | |
471086d6 | 759 | } |
760 | ||
b248d3f7 | 761 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 762 | error ("conversion from %qT to non-scalar type %qT requested", |
763 | TREE_TYPE (expr), type); | |
471086d6 | 764 | return error_mark_node; |
765 | } | |
766 | ||
b1a8d4ce | 767 | /* When an expression is used in a void context, its value is discarded and |
768 | no lvalue-rvalue and similar conversions happen [expr.static.cast/4, | |
769 | stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type | |
770 | in a void context. The C++ standard does not define what an `access' to an | |
63eff20d | 771 | object is, but there is reason to believe that it is the lvalue to rvalue |
b1a8d4ce | 772 | conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it |
773 | accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8 | |
774 | indicates that volatile semantics should be the same between C and C++ | |
775 | where ever possible. C leaves it implementation defined as to what | |
776 | constitutes an access to a volatile. So, we interpret `*vp' as a read of | |
777 | the volatile object `vp' points to, unless that is an incomplete type. For | |
778 | volatile references we do not do this interpretation, because that would | |
779 | make it impossible to ignore the reference return value from functions. We | |
780 | issue warnings in the confusing cases. | |
781 | ||
782 | IMPLICIT is tells us the context of an implicit void conversion. */ | |
783 | ||
784 | tree | |
35771a9a | 785 | convert_to_void (tree expr, const char *implicit) |
b1a8d4ce | 786 | { |
3d411d73 | 787 | if (expr == error_mark_node |
788 | || TREE_TYPE (expr) == error_mark_node) | |
789 | return error_mark_node; | |
b1a8d4ce | 790 | if (!TREE_TYPE (expr)) |
791 | return expr; | |
334f6ce1 | 792 | if (invalid_nonstatic_memfn_p (expr)) |
793 | return error_mark_node; | |
e3cfe3ce | 794 | if (VOID_TYPE_P (TREE_TYPE (expr))) |
b1a8d4ce | 795 | return expr; |
796 | switch (TREE_CODE (expr)) | |
797 | { | |
798 | case COND_EXPR: | |
799 | { | |
800 | /* The two parts of a cond expr might be separate lvalues. */ | |
801 | tree op1 = TREE_OPERAND (expr,1); | |
802 | tree op2 = TREE_OPERAND (expr,2); | |
00fa9079 | 803 | tree new_op1 = convert_to_void |
804 | (op1, (implicit && !TREE_SIDE_EFFECTS (op2) | |
805 | ? "second operand of conditional" : NULL)); | |
806 | tree new_op2 = convert_to_void | |
807 | (op2, (implicit && !TREE_SIDE_EFFECTS (op1) | |
808 | ? "third operand of conditional" : NULL)); | |
b1a8d4ce | 809 | |
831d52a2 | 810 | expr = build3 (COND_EXPR, TREE_TYPE (new_op1), |
811 | TREE_OPERAND (expr, 0), new_op1, new_op2); | |
b1a8d4ce | 812 | break; |
813 | } | |
814 | ||
815 | case COMPOUND_EXPR: | |
816 | { | |
817 | /* The second part of a compound expr contains the value. */ | |
818 | tree op1 = TREE_OPERAND (expr,1); | |
00fa9079 | 819 | tree new_op1 = convert_to_void |
4ee9c684 | 820 | (op1, (implicit && !TREE_NO_WARNING (expr) |
23ec9bd9 | 821 | ? "right-hand operand of comma" : NULL)); |
b1a8d4ce | 822 | |
823 | if (new_op1 != op1) | |
e907a2e9 | 824 | { |
831d52a2 | 825 | tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1), |
826 | TREE_OPERAND (expr, 0), new_op1); | |
e907a2e9 | 827 | expr = t; |
828 | } | |
829 | ||
b1a8d4ce | 830 | break; |
831 | } | |
832 | ||
833 | case NON_LVALUE_EXPR: | |
834 | case NOP_EXPR: | |
c0af329c | 835 | /* These have already decayed to rvalue. */ |
b1a8d4ce | 836 | break; |
837 | ||
331bc0ad | 838 | case CALL_EXPR: /* We have a special meaning for volatile void fn(). */ |
b1a8d4ce | 839 | break; |
840 | ||
841 | case INDIRECT_REF: | |
842 | { | |
843 | tree type = TREE_TYPE (expr); | |
844 | int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) | |
845 | == REFERENCE_TYPE; | |
846 | int is_volatile = TYPE_VOLATILE (type); | |
4b72716d | 847 | int is_complete = COMPLETE_TYPE_P (complete_type (type)); |
b1a8d4ce | 848 | |
849 | if (is_volatile && !is_complete) | |
0a25aad6 | 850 | warning ("object of incomplete type %qT will not be accessed in %s", |
851 | type, implicit ? implicit : "void context"); | |
b1a8d4ce | 852 | else if (is_reference && is_volatile) |
0a25aad6 | 853 | warning ("object of type %qT will not be accessed in %s", |
854 | TREE_TYPE (TREE_OPERAND (expr, 0)), | |
855 | implicit ? implicit : "void context"); | |
b1a8d4ce | 856 | if (is_reference || !is_volatile || !is_complete) |
857 | expr = TREE_OPERAND (expr, 0); | |
858 | ||
859 | break; | |
860 | } | |
861 | ||
862 | case VAR_DECL: | |
863 | { | |
864 | /* External variables might be incomplete. */ | |
865 | tree type = TREE_TYPE (expr); | |
4b72716d | 866 | int is_complete = COMPLETE_TYPE_P (complete_type (type)); |
b1a8d4ce | 867 | |
868 | if (TYPE_VOLATILE (type) && !is_complete) | |
0a25aad6 | 869 | warning ("object %qE of incomplete type %qT will not be accessed in %s", |
870 | expr, type, implicit ? implicit : "void context"); | |
b1a8d4ce | 871 | break; |
872 | } | |
d9f88785 | 873 | |
b1a8d4ce | 874 | default:; |
875 | } | |
876 | { | |
877 | tree probe = expr; | |
878 | ||
879 | if (TREE_CODE (probe) == ADDR_EXPR) | |
880 | probe = TREE_OPERAND (expr, 0); | |
2e0e6579 | 881 | if (type_unknown_p (probe)) |
882 | { | |
883 | /* [over.over] enumerates the places where we can take the address | |
884 | of an overloaded function, and this is not one of them. */ | |
cf103c6c | 885 | pedwarn ("%s cannot resolve address of overloaded function", |
2e0e6579 | 886 | implicit ? implicit : "void cast"); |
ea720917 | 887 | expr = void_zero_node; |
2e0e6579 | 888 | } |
889 | else if (implicit && probe == expr && is_overloaded_fn (probe)) | |
b1a8d4ce | 890 | /* Only warn when there is no &. */ |
0a25aad6 | 891 | warning ("%s is a reference, not call, to function %qE", |
2e0e6579 | 892 | implicit, expr); |
b1a8d4ce | 893 | } |
894 | ||
e3cfe3ce | 895 | if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr))) |
b1a8d4ce | 896 | { |
4ee9c684 | 897 | if (implicit && warn_unused_value |
898 | && !TREE_SIDE_EFFECTS (expr) && !TREE_NO_WARNING (expr)) | |
00fa9079 | 899 | warning ("%s has no effect", implicit); |
900 | expr = build1 (CONVERT_EXPR, void_type_node, expr); | |
b1a8d4ce | 901 | } |
902 | return expr; | |
903 | } | |
904 | ||
d81e00a4 | 905 | /* Create an expression whose value is that of EXPR, |
906 | converted to type TYPE. The TREE_TYPE of the value | |
907 | is always TYPE. This function implements all reasonable | |
908 | conversions; callers should filter out those that are | |
c4a8ac95 | 909 | not permitted by the language being compiled. |
910 | ||
911 | Most of this routine is from build_reinterpret_cast. | |
912 | ||
913 | The backend cannot call cp_convert (what was convert) because | |
914 | conversions to/from basetypes may involve memory references | |
915 | (vbases) and adding or subtracting small values (multiple | |
916 | inheritance), but it calls convert from the constant folding code | |
e0aa5007 | 917 | on subtrees of already built trees after it has ripped them apart. |
c4a8ac95 | 918 | |
919 | Also, if we ever support range variables, we'll probably also have to | |
920 | do a little bit more work. */ | |
d81e00a4 | 921 | |
922 | tree | |
35771a9a | 923 | convert (tree type, tree expr) |
d81e00a4 | 924 | { |
c4a8ac95 | 925 | tree intype; |
926 | ||
927 | if (type == error_mark_node || expr == error_mark_node) | |
928 | return error_mark_node; | |
929 | ||
c4a8ac95 | 930 | intype = TREE_TYPE (expr); |
931 | ||
6686e84d | 932 | if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype)) |
c4a8ac95 | 933 | { |
1202e392 | 934 | expr = decl_constant_value (expr); |
5d7ed6c7 | 935 | return fold_if_not_in_template (build_nop (type, expr)); |
c4a8ac95 | 936 | } |
937 | ||
938 | return ocp_convert (type, expr, CONV_OLD_CONVERT, | |
939 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); | |
d81e00a4 | 940 | } |
941 | ||
c4a8ac95 | 942 | /* Like cp_convert, except permit conversions to take place which |
471086d6 | 943 | are not normally allowed due to access restrictions |
944 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 945 | |
471086d6 | 946 | tree |
35771a9a | 947 | convert_force (tree type, tree expr, int convtype) |
471086d6 | 948 | { |
cd16867a | 949 | tree e = expr; |
950 | enum tree_code code = TREE_CODE (type); | |
471086d6 | 951 | |
952 | if (code == REFERENCE_TYPE) | |
5d7ed6c7 | 953 | return (fold_if_not_in_template |
954 | (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN, | |
955 | NULL_TREE))); | |
471086d6 | 956 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) |
957 | e = convert_from_reference (e); | |
958 | ||
959 | if (code == POINTER_TYPE) | |
5d7ed6c7 | 960 | return fold_if_not_in_template (convert_to_pointer_force (type, e)); |
471086d6 | 961 | |
ac9386a0 | 962 | /* From typeck.c convert_for_assignment */ |
471086d6 | 963 | if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR |
964 | && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE | |
965 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) | |
ac9386a0 | 966 | || integer_zerop (e) |
967 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
471086d6 | 968 | && TYPE_PTRMEMFUNC_P (type)) |
cb02169c | 969 | /* compatible pointer to member functions. */ |
970 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1, | |
971 | /*c_cast_p=*/1); | |
a74e8896 | 972 | |
c4a8ac95 | 973 | return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL); |
471086d6 | 974 | } |
975 | ||
471086d6 | 976 | /* Convert an aggregate EXPR to type XTYPE. If a conversion |
977 | exists, return the attempted conversion. This may | |
978 | return ERROR_MARK_NODE if the conversion is not | |
979 | allowed (references private members, etc). | |
980 | If no conversion exists, NULL_TREE is returned. | |
981 | ||
ce28ee2e | 982 | FIXME: Ambiguity checking is wrong. Should choose one by the implicit |
983 | object parameter, or by the second standard conversion sequence if | |
984 | that doesn't do it. This will probably wait for an overloading rewrite. | |
985 | (jason 8/9/95) */ | |
471086d6 | 986 | |
471086d6 | 987 | tree |
8999978b | 988 | build_type_conversion (tree xtype, tree expr) |
471086d6 | 989 | { |
990 | /* C++: check to see if we can convert this aggregate type | |
bcf789d7 | 991 | into the required type. */ |
8999978b | 992 | return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL); |
471086d6 | 993 | } |
994 | ||
1e66592c | 995 | /* Convert the given EXPR to one of a group of types suitable for use in an |
996 | expression. DESIRES is a combination of various WANT_* flags (q.v.) | |
35771a9a | 997 | which indicates which types are suitable. If COMPLAIN is true, complain |
1e66592c | 998 | about ambiguity; otherwise, the caller will deal with it. */ |
471086d6 | 999 | |
1e66592c | 1000 | tree |
35771a9a | 1001 | build_expr_type_conversion (int desires, tree expr, bool complain) |
471086d6 | 1002 | { |
1e66592c | 1003 | tree basetype = TREE_TYPE (expr); |
6a44e72e | 1004 | tree conv = NULL_TREE; |
bdd152ce | 1005 | tree winner = NULL_TREE; |
471086d6 | 1006 | |
954885ed | 1007 | if (expr == null_node |
1008 | && (desires & WANT_INT) | |
1009 | && !(desires & WANT_NULL)) | |
cf103c6c | 1010 | warning ("converting NULL to non-pointer type"); |
954885ed | 1011 | |
ce28ee2e | 1012 | expr = convert_from_reference (expr); |
1013 | basetype = TREE_TYPE (expr); | |
471086d6 | 1014 | |
3d411d73 | 1015 | if (basetype == error_mark_node) |
1016 | return error_mark_node; | |
1017 | ||
bdd152ce | 1018 | if (! IS_AGGR_TYPE (basetype)) |
1019 | switch (TREE_CODE (basetype)) | |
1020 | { | |
1021 | case INTEGER_TYPE: | |
954885ed | 1022 | if ((desires & WANT_NULL) && null_ptr_cst_p (expr)) |
bdd152ce | 1023 | return expr; |
1024 | /* else fall through... */ | |
1025 | ||
1026 | case BOOLEAN_TYPE: | |
1027 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1028 | case ENUMERAL_TYPE: | |
1029 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1030 | case REAL_TYPE: | |
1031 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1032 | case POINTER_TYPE: | |
1033 | return (desires & WANT_POINTER) ? expr : NULL_TREE; | |
985e9ee0 | 1034 | |
bdd152ce | 1035 | case FUNCTION_TYPE: |
1036 | case ARRAY_TYPE: | |
a681799d | 1037 | return (desires & WANT_POINTER) ? decay_conversion (expr) |
bdd152ce | 1038 | : NULL_TREE; |
1039 | default: | |
1040 | return NULL_TREE; | |
1041 | } | |
1042 | ||
1043 | /* The code for conversions from class type is currently only used for | |
1044 | delete expressions. Other expressions are handled by build_new_op. */ | |
f8182e08 | 1045 | if (!complete_type_or_else (basetype, expr)) |
1046 | return error_mark_node; | |
1047 | if (!TYPE_HAS_CONVERSION (basetype)) | |
bdd152ce | 1048 | return NULL_TREE; |
1049 | ||
1050 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) | |
1051 | { | |
1052 | int win = 0; | |
1053 | tree candidate; | |
1054 | tree cand = TREE_VALUE (conv); | |
1055 | ||
1056 | if (winner && winner == cand) | |
1057 | continue; | |
1058 | ||
ef4534a3 | 1059 | candidate = non_reference (TREE_TYPE (TREE_TYPE (cand))); |
bdd152ce | 1060 | |
1061 | switch (TREE_CODE (candidate)) | |
1062 | { | |
1063 | case BOOLEAN_TYPE: | |
1064 | case INTEGER_TYPE: | |
1065 | win = (desires & WANT_INT); break; | |
1066 | case ENUMERAL_TYPE: | |
1067 | win = (desires & WANT_ENUM); break; | |
1068 | case REAL_TYPE: | |
1069 | win = (desires & WANT_FLOAT); break; | |
1070 | case POINTER_TYPE: | |
1071 | win = (desires & WANT_POINTER); break; | |
1072 | ||
1073 | default: | |
1074 | break; | |
1075 | } | |
1076 | ||
1077 | if (win) | |
1078 | { | |
1079 | if (winner) | |
1080 | { | |
1081 | if (complain) | |
1082 | { | |
0a25aad6 | 1083 | error ("ambiguous default type conversion from %qT", |
1084 | basetype); | |
1085 | error (" candidate conversions include %qD and %qD", | |
1086 | winner, cand); | |
bdd152ce | 1087 | } |
1088 | return error_mark_node; | |
1089 | } | |
1090 | else | |
1091 | winner = cand; | |
1092 | } | |
1093 | } | |
1e66592c | 1094 | |
bdd152ce | 1095 | if (winner) |
1096 | { | |
ef4534a3 | 1097 | tree type = non_reference (TREE_TYPE (TREE_TYPE (winner))); |
bdd152ce | 1098 | return build_user_type_conversion (type, expr, LOOKUP_NORMAL); |
471086d6 | 1099 | } |
1e66592c | 1100 | |
985e9ee0 | 1101 | return NULL_TREE; |
471086d6 | 1102 | } |
bc3887cf | 1103 | |
96624a9e | 1104 | /* Implements integral promotion (4.1) and float->double promotion. */ |
1105 | ||
bc3887cf | 1106 | tree |
35771a9a | 1107 | type_promotes_to (tree type) |
bc3887cf | 1108 | { |
ce28ee2e | 1109 | if (type == error_mark_node) |
1110 | return error_mark_node; | |
1111 | ||
bc3887cf | 1112 | type = TYPE_MAIN_VARIANT (type); |
bb0726a1 | 1113 | |
1114 | /* bool always promotes to int (not unsigned), even if it's the same | |
1115 | size. */ | |
028ea8b4 | 1116 | if (type == boolean_type_node) |
bb0726a1 | 1117 | type = integer_type_node; |
1118 | ||
1119 | /* Normally convert enums to int, but convert wide enums to something | |
1120 | wider. */ | |
1121 | else if (TREE_CODE (type) == ENUMERAL_TYPE | |
1122 | || type == wchar_type_node) | |
6c9497b1 | 1123 | { |
1124 | int precision = MAX (TYPE_PRECISION (type), | |
1125 | TYPE_PRECISION (integer_type_node)); | |
771d21fa | 1126 | tree totype = c_common_type_for_size (precision, 0); |
78a8ed03 | 1127 | if (TYPE_UNSIGNED (type) |
6c9497b1 | 1128 | && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) |
771d21fa | 1129 | type = c_common_type_for_size (precision, 1); |
6c9497b1 | 1130 | else |
1131 | type = totype; | |
1132 | } | |
d7aeef06 | 1133 | else if (c_promoting_integer_type_p (type)) |
bc3887cf | 1134 | { |
d0acef9e | 1135 | /* Retain unsignedness if really not getting bigger. */ |
78a8ed03 | 1136 | if (TYPE_UNSIGNED (type) |
d0acef9e | 1137 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
bc3887cf | 1138 | type = unsigned_type_node; |
1139 | else | |
1140 | type = integer_type_node; | |
1141 | } | |
1142 | else if (type == float_type_node) | |
1143 | type = double_type_node; | |
a681799d | 1144 | |
1145 | return type; | |
bc3887cf | 1146 | } |
668ae905 | 1147 | |
668ae905 | 1148 | /* The routines below this point are carefully written to conform to |
1149 | the standard. They use the same terminology, and follow the rules | |
1150 | closely. Although they are used only in pt.c at the moment, they | |
1151 | should presumably be used everywhere in the future. */ | |
1152 | ||
1146f179 | 1153 | /* Attempt to perform qualification conversions on EXPR to convert it |
1154 | to TYPE. Return the resulting expression, or error_mark_node if | |
1155 | the conversion was impossible. */ | |
1156 | ||
668ae905 | 1157 | tree |
35771a9a | 1158 | perform_qualification_conversions (tree type, tree expr) |
668ae905 | 1159 | { |
1bc16cab | 1160 | tree expr_type; |
1161 | ||
1162 | expr_type = TREE_TYPE (expr); | |
1163 | ||
1164 | if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type) | |
1165 | && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type))) | |
1166 | return build_nop (type, expr); | |
1167 | else if (TYPE_PTR_TO_MEMBER_P (type) | |
1168 | && TYPE_PTR_TO_MEMBER_P (expr_type) | |
1169 | && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), | |
1170 | TYPE_PTRMEM_CLASS_TYPE (expr_type)) | |
1171 | && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type), | |
1172 | TYPE_PTRMEM_POINTED_TO_TYPE (expr_type))) | |
1173 | return build_nop (type, expr); | |
1146f179 | 1174 | else |
1175 | return error_mark_node; | |
668ae905 | 1176 | } |