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