]>
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, |
95397ff9 | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
aa139c3f | 4 | Free Software Foundation, Inc. |
471086d6 | 5 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
6 | ||
6f0d25a6 | 7 | This file is part of GCC. |
471086d6 | 8 | |
6f0d25a6 | 9 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 10 | it under the terms of the GNU General Public License as published by |
aa139c3f | 11 | the Free Software Foundation; either version 3, or (at your option) |
471086d6 | 12 | any later version. |
13 | ||
6f0d25a6 | 14 | GCC is distributed in the hope that it will be useful, |
471086d6 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
aa139c3f | 20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
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" |
7a979707 | 39 | #include "target.h" |
96624a9e | 40 | |
e5ead12a | 41 | static tree cp_convert_to_pointer (tree, tree); |
35771a9a | 42 | static tree convert_to_pointer_force (tree, tree); |
ef9571e5 | 43 | static tree build_type_conversion (tree, tree); |
35771a9a | 44 | static tree build_up_reference (tree, tree, int, tree); |
45 | static void warn_ref_binding (tree, tree, tree); | |
71ccdfff | 46 | |
471086d6 | 47 | /* Change of width--truncation and extension of integers or reals-- |
48 | is represented with NOP_EXPR. Proper functioning of many things | |
49 | assumes that no other conversions can be NOP_EXPRs. | |
50 | ||
51 | Conversion between integer and pointer is represented with CONVERT_EXPR. | |
52 | Converting integer to real uses FLOAT_EXPR | |
53 | and real to integer uses FIX_TRUNC_EXPR. | |
54 | ||
55 | Here is a list of all the functions that assume that widening and | |
56 | narrowing is always done with a NOP_EXPR: | |
57 | In convert.c, convert_to_integer. | |
58 | In c-typeck.c, build_binary_op_nodefault (boolean ops), | |
653e5405 | 59 | and c_common_truthvalue_conversion. |
471086d6 | 60 | In expr.c: expand_expr, for operands of a MULT_EXPR. |
61 | In fold-const.c: fold. | |
62 | In tree.c: get_narrower and get_unwidened. | |
63 | ||
64 | C++: in multiple-inheritance, converting between pointers may involve | |
65 | adjusting them by a delta stored within the class definition. */ | |
66 | \f | |
67 | /* Subroutines of `convert'. */ | |
68 | ||
471086d6 | 69 | /* if converting pointer to pointer |
70 | if dealing with classes, check for derived->base or vice versa | |
71 | else if dealing with method pointers, delegate | |
72 | else convert blindly | |
73 | else if converting class, pass off to build_type_conversion | |
e5ead12a | 74 | else try C-style pointer conversion. */ |
96624a9e | 75 | |
471086d6 | 76 | static tree |
e5ead12a | 77 | cp_convert_to_pointer (tree type, tree expr) |
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 | |
95397ff9 | 85 | if (MAYBE_CLASS_TYPE_P (intype)) |
96624a9e | 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", |
653e5405 | 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", |
653e5405 | 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 |
95397ff9 | 130 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (type)) |
131 | && MAYBE_CLASS_TYPE_P (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 | ||
9031d10b | 143 | same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class), |
8999978b | 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", |
653e5405 | 169 | expr, intype, type); |
1bc16cab | 170 | return error_mark_node; |
171 | } | |
a17aefa2 | 172 | |
1bc16cab | 173 | return build_nop (type, expr); |
174 | } | |
e5ead12a | 175 | else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)) |
176 | || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))) | |
177 | return convert_ptrmem (type, expr, /*allow_inverse_p=*/false, | |
178 | /*c_cast_p=*/false); | |
2b77484d | 179 | else if (TYPE_PTRMEMFUNC_P (intype)) |
180 | { | |
a63bc44c | 181 | if (!warn_pmf2ptr) |
182 | { | |
183 | if (TREE_CODE (expr) == PTRMEM_CST) | |
184 | return cp_convert_to_pointer (type, | |
e5ead12a | 185 | PTRMEM_CST_MEMBER (expr)); |
a63bc44c | 186 | else if (TREE_CODE (expr) == OFFSET_REF) |
187 | { | |
188 | tree object = TREE_OPERAND (expr, 0); | |
189 | return get_member_function_from_ptrfunc (&object, | |
190 | TREE_OPERAND (expr, 1)); | |
191 | } | |
192 | } | |
0a25aad6 | 193 | error ("cannot convert %qE from type %qT to type %qT", |
653e5405 | 194 | expr, intype, type); |
2b77484d | 195 | return error_mark_node; |
196 | } | |
471086d6 | 197 | |
471086d6 | 198 | if (integer_zerop (expr)) |
199 | { | |
2b77484d | 200 | if (TYPE_PTRMEMFUNC_P (type)) |
cb02169c | 201 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, |
202 | /*c_cast_p=*/false); | |
ad63a0fc | 203 | |
606b494c | 204 | if (TYPE_PTRMEM_P (type)) |
7c446c95 | 205 | { |
206 | /* A NULL pointer-to-member is represented by -1, not by | |
207 | zero. */ | |
697bbc3f | 208 | expr = build_int_cst_type (type, -1); |
7c446c95 | 209 | } |
ad63a0fc | 210 | else |
7016c612 | 211 | expr = build_int_cst (type, 0); |
9031d10b | 212 | |
471086d6 | 213 | return expr; |
214 | } | |
1bc16cab | 215 | else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form)) |
de1f960a | 216 | { |
0a25aad6 | 217 | error ("invalid conversion from %qT to %qT", intype, type); |
de1f960a | 218 | return error_mark_node; |
219 | } | |
471086d6 | 220 | |
bb0726a1 | 221 | if (INTEGRAL_CODE_P (form)) |
471086d6 | 222 | { |
c5aa1e92 | 223 | if (TYPE_PRECISION (intype) == POINTER_SIZE) |
471086d6 | 224 | return build1 (CONVERT_EXPR, type, expr); |
771d21fa | 225 | expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr); |
092b1d6f | 226 | /* Modes may be different but sizes should be the same. There |
227 | is supposed to be some integral type that is the same width | |
228 | as a pointer. */ | |
229 | gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))) | |
230 | == GET_MODE_SIZE (TYPE_MODE (type))); | |
9031d10b | 231 | |
471086d6 | 232 | return convert_to_pointer (type, expr); |
233 | } | |
234 | ||
cc4d0855 | 235 | if (type_unknown_p (expr)) |
0fbca5e8 | 236 | return instantiate_type (type, expr, tf_warning_or_error); |
cc4d0855 | 237 | |
0a25aad6 | 238 | error ("cannot convert %qE from type %qT to type %qT", |
653e5405 | 239 | expr, intype, type); |
471086d6 | 240 | return error_mark_node; |
241 | } | |
242 | ||
243 | /* Like convert, except permit conversions to take place which | |
244 | are not normally allowed due to access restrictions | |
245 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 246 | |
471086d6 | 247 | static tree |
35771a9a | 248 | convert_to_pointer_force (tree type, tree expr) |
471086d6 | 249 | { |
cd16867a | 250 | tree intype = TREE_TYPE (expr); |
251 | enum tree_code form = TREE_CODE (intype); | |
9031d10b | 252 | |
471086d6 | 253 | if (form == POINTER_TYPE) |
254 | { | |
255 | intype = TYPE_MAIN_VARIANT (intype); | |
256 | ||
257 | if (TYPE_MAIN_VARIANT (type) != intype | |
258 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
95397ff9 | 259 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (type)) |
260 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype)) | |
471086d6 | 261 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
262 | { | |
263 | enum tree_code code = PLUS_EXPR; | |
4a2680fc | 264 | tree binfo; |
265 | ||
266 | binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type), | |
ada40935 | 267 | ba_unique, NULL); |
4a2680fc | 268 | if (!binfo) |
471086d6 | 269 | { |
4a2680fc | 270 | binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype), |
ada40935 | 271 | ba_unique, NULL); |
4a2680fc | 272 | code = MINUS_EXPR; |
471086d6 | 273 | } |
4a2680fc | 274 | if (binfo == error_mark_node) |
275 | return error_mark_node; | |
276 | if (binfo) | |
471086d6 | 277 | { |
4a2680fc | 278 | expr = build_base_path (code, expr, binfo, 0); |
653e5405 | 279 | if (expr == error_mark_node) |
280 | return error_mark_node; | |
c0af329c | 281 | /* Add any qualifier conversions. */ |
4a2680fc | 282 | if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)), |
283 | TREE_TYPE (type))) | |
8999978b | 284 | expr = build_nop (type, expr); |
4a2680fc | 285 | return expr; |
471086d6 | 286 | } |
471086d6 | 287 | } |
471086d6 | 288 | } |
289 | ||
e5ead12a | 290 | return cp_convert_to_pointer (type, expr); |
471086d6 | 291 | } |
292 | ||
293 | /* We are passing something to a function which requires a reference. | |
294 | The type we are interested in is in TYPE. The initial | |
295 | value we have to begin with is in ARG. | |
296 | ||
297 | FLAGS controls how we manage access checking. | |
ca106ab1 | 298 | DIRECT_BIND in FLAGS controls how any temporaries are generated. |
299 | If DIRECT_BIND is set, DECL is the reference we're binding to. */ | |
96624a9e | 300 | |
471086d6 | 301 | static tree |
35771a9a | 302 | build_up_reference (tree type, tree arg, int flags, tree decl) |
471086d6 | 303 | { |
c76251c1 | 304 | tree rval; |
0543e7a9 | 305 | tree argtype = TREE_TYPE (arg); |
471086d6 | 306 | tree target_type = TREE_TYPE (type); |
471086d6 | 307 | |
b4df430b | 308 | gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); |
471086d6 | 309 | |
c76251c1 | 310 | if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg)) |
471086d6 | 311 | { |
ca106ab1 | 312 | /* Create a new temporary variable. We can't just use a TARGET_EXPR |
313 | here because it needs to live as long as DECL. */ | |
c76251c1 | 314 | tree targ = arg; |
ca106ab1 | 315 | |
7c09476d | 316 | arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg)); |
c23ebfdd | 317 | |
318 | /* Process the initializer for the declaration. */ | |
38281c46 | 319 | DECL_INITIAL (arg) = targ; |
d91303a6 | 320 | cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE, |
011310f7 | 321 | LOOKUP_ONLYCONVERTING|DIRECT_BIND); |
1ad432f2 | 322 | } |
c76251c1 | 323 | else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg)) |
1d8e4310 | 324 | return get_target_expr (arg); |
1ad432f2 | 325 | |
ca106ab1 | 326 | /* If we had a way to wrap this up, and say, if we ever needed its |
b0df6589 | 327 | address, transform all occurrences of the register, into a memory |
328 | reference we could win better. */ | |
ebd21de4 | 329 | rval = cp_build_unary_op (ADDR_EXPR, arg, 1, tf_warning_or_error); |
15e55420 | 330 | if (rval == error_mark_node) |
331 | return error_mark_node; | |
332 | ||
6686e84d | 333 | if ((flags & LOOKUP_PROTECT) |
334 | && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type) | |
95397ff9 | 335 | && MAYBE_CLASS_TYPE_P (argtype) |
336 | && MAYBE_CLASS_TYPE_P (target_type)) | |
6686e84d | 337 | { |
7e6960e0 | 338 | /* We go through lookup_base for the access control. */ |
4a2680fc | 339 | tree binfo = lookup_base (argtype, target_type, ba_check, NULL); |
6686e84d | 340 | if (binfo == error_mark_node) |
341 | return error_mark_node; | |
342 | if (binfo == NULL_TREE) | |
343 | return error_not_base_type (target_type, argtype); | |
4a2680fc | 344 | rval = build_base_path (PLUS_EXPR, rval, binfo, 1); |
6686e84d | 345 | } |
c76251c1 | 346 | else |
347 | rval | |
348 | = convert_to_pointer_force (build_pointer_type (target_type), rval); | |
8999978b | 349 | return build_nop (type, rval); |
471086d6 | 350 | } |
351 | ||
519ba62c | 352 | /* Subroutine of convert_to_reference. REFTYPE is the target reference type. |
353 | INTYPE is the original rvalue type and DECL is an optional _DECL node | |
354 | for diagnostics. | |
9031d10b | 355 | |
519ba62c | 356 | [dcl.init.ref] says that if an rvalue is used to |
357 | initialize a reference, then the reference must be to a | |
358 | non-volatile const type. */ | |
359 | ||
360 | static void | |
35771a9a | 361 | warn_ref_binding (tree reftype, tree intype, tree decl) |
519ba62c | 362 | { |
363 | tree ttl = TREE_TYPE (reftype); | |
9031d10b | 364 | |
519ba62c | 365 | if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl)) |
366 | { | |
367 | const char *msg; | |
368 | ||
369 | if (CP_TYPE_VOLATILE_P (ttl) && decl) | |
0a25aad6 | 370 | msg = "initialization of volatile reference type %q#T from" |
653e5405 | 371 | " rvalue of type %qT"; |
519ba62c | 372 | else if (CP_TYPE_VOLATILE_P (ttl)) |
0a25aad6 | 373 | msg = "conversion to volatile reference type %q#T " |
653e5405 | 374 | " from rvalue of type %qT"; |
519ba62c | 375 | else if (decl) |
0a25aad6 | 376 | msg = "initialization of non-const reference type %q#T from" |
653e5405 | 377 | " rvalue of type %qT"; |
519ba62c | 378 | else |
0a25aad6 | 379 | msg = "conversion to non-const reference type %q#T from" |
653e5405 | 380 | " rvalue of type %qT"; |
519ba62c | 381 | |
2b9e3597 | 382 | permerror (input_location, msg, reftype, intype); |
519ba62c | 383 | } |
384 | } | |
385 | ||
471086d6 | 386 | /* For C++: Only need to do one-level references, but cannot |
387 | get tripped up on signed/unsigned differences. | |
388 | ||
d81e00a4 | 389 | DECL is either NULL_TREE or the _DECL node for a reference that is being |
390 | initialized. It can be error_mark_node if we don't know the _DECL but | |
391 | we know it's an initialization. */ | |
471086d6 | 392 | |
471086d6 | 393 | tree |
35771a9a | 394 | convert_to_reference (tree reftype, tree expr, int convtype, |
653e5405 | 395 | int flags, tree decl) |
471086d6 | 396 | { |
cd16867a | 397 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); |
398 | tree intype; | |
471086d6 | 399 | tree rval = NULL_TREE; |
1a3f833b | 400 | tree rval_as_conversion = NULL_TREE; |
1bc16cab | 401 | bool can_convert_intype_to_type; |
1a3f833b | 402 | |
9031d10b | 403 | if (TREE_CODE (type) == FUNCTION_TYPE |
d32df6a6 | 404 | && TREE_TYPE (expr) == unknown_type_node) |
9031d10b | 405 | expr = instantiate_type (type, expr, |
0a3b29ad | 406 | (flags & LOOKUP_COMPLAIN) |
0fbca5e8 | 407 | ? tf_warning_or_error : tf_none); |
0a3b29ad | 408 | |
409 | if (expr == error_mark_node) | |
410 | return error_mark_node; | |
411 | ||
412 | intype = TREE_TYPE (expr); | |
cc4d0855 | 413 | |
b4df430b | 414 | gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE); |
ab12ed55 | 415 | gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE); |
471086d6 | 416 | |
471086d6 | 417 | intype = TYPE_MAIN_VARIANT (intype); |
418 | ||
1bc16cab | 419 | can_convert_intype_to_type = can_convert (type, intype); |
420 | if (!can_convert_intype_to_type | |
95397ff9 | 421 | && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype) |
1a3f833b | 422 | && ! (flags & LOOKUP_NO_CONVERSION)) |
423 | { | |
424 | /* Look for a user-defined conversion to lvalue that we can use. */ | |
425 | ||
0a4be248 | 426 | rval_as_conversion |
8999978b | 427 | = build_type_conversion (reftype, expr); |
1a3f833b | 428 | |
429 | if (rval_as_conversion && rval_as_conversion != error_mark_node | |
430 | && real_lvalue_p (rval_as_conversion)) | |
431 | { | |
432 | expr = rval_as_conversion; | |
433 | rval_as_conversion = NULL_TREE; | |
434 | intype = type; | |
1bc16cab | 435 | can_convert_intype_to_type = 1; |
1a3f833b | 436 | } |
437 | } | |
438 | ||
1bc16cab | 439 | if (((convtype & CONV_STATIC) && can_convert (intype, type)) |
440 | || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type)) | |
471086d6 | 441 | { |
471086d6 | 442 | if (flags & LOOKUP_COMPLAIN) |
443 | { | |
bc3887cf | 444 | tree ttl = TREE_TYPE (reftype); |
cb0ba4ec | 445 | tree ttr = lvalue_type (expr); |
471086d6 | 446 | |
519ba62c | 447 | if (! real_lvalue_p (expr)) |
448 | warn_ref_binding (reftype, intype, decl); | |
9031d10b | 449 | |
519ba62c | 450 | if (! (convtype & CONV_CONST) |
3e04bd45 | 451 | && !at_least_as_qualified_p (ttl, ttr)) |
2b9e3597 | 452 | permerror (input_location, "conversion from %qT to %qT discards qualifiers", |
07317e69 | 453 | ttr, reftype); |
0543e7a9 | 454 | } |
455 | ||
ca106ab1 | 456 | return build_up_reference (reftype, expr, flags, decl); |
471086d6 | 457 | } |
c07b1ad1 | 458 | else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) |
0543e7a9 | 459 | { |
460 | /* When casting an lvalue to a reference type, just convert into | |
461 | a pointer to the new type and deference it. This is allowed | |
e581f478 | 462 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
0543e7a9 | 463 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
e581f478 | 464 | |
3748625f | 465 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
653e5405 | 466 | meant. */ |
1a3f833b | 467 | if (TREE_CODE (intype) == POINTER_TYPE |
a09db423 | 468 | && (comptypes (TREE_TYPE (intype), type, |
469 | COMPARE_BASE | COMPARE_DERIVED))) | |
c3ceba8e | 470 | warning (0, "casting %qT to %qT does not dereference pointer", |
00952d10 | 471 | intype, reftype); |
9031d10b | 472 | |
ebd21de4 | 473 | rval = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error); |
0543e7a9 | 474 | if (rval != error_mark_node) |
985e9ee0 | 475 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), |
476 | rval, 0); | |
0543e7a9 | 477 | if (rval != error_mark_node) |
b0722fac | 478 | rval = build1 (NOP_EXPR, reftype, rval); |
0543e7a9 | 479 | } |
0a4be248 | 480 | else |
860740a7 | 481 | { |
482 | rval = convert_for_initialization (NULL_TREE, type, expr, flags, | |
ebd21de4 | 483 | "converting", 0, 0, |
484 | tf_warning_or_error); | |
a66fe4cb | 485 | if (rval == NULL_TREE || rval == error_mark_node) |
486 | return rval; | |
519ba62c | 487 | warn_ref_binding (reftype, intype, decl); |
ca106ab1 | 488 | rval = build_up_reference (reftype, rval, flags, decl); |
860740a7 | 489 | } |
471086d6 | 490 | |
491 | if (rval) | |
492 | { | |
96624a9e | 493 | /* If we found a way to convert earlier, then use it. */ |
471086d6 | 494 | return rval; |
495 | } | |
496 | ||
b248d3f7 | 497 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 498 | error ("cannot convert type %qT to type %qT", intype, reftype); |
b248d3f7 | 499 | |
471086d6 | 500 | return error_mark_node; |
501 | } | |
502 | ||
503 | /* We are using a reference VAL for its value. Bash that reference all the | |
96624a9e | 504 | way down to its lowest form. */ |
505 | ||
471086d6 | 506 | tree |
35771a9a | 507 | convert_from_reference (tree val) |
471086d6 | 508 | { |
120c0017 | 509 | if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE) |
729f89ff | 510 | { |
511 | tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val))); | |
512 | tree ref = build1 (INDIRECT_REF, t, val); | |
9031d10b | 513 | |
729f89ff | 514 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, |
515 | so that we get the proper error message if the result is used | |
516 | to assign to. Also, &* is supposed to be a no-op. */ | |
517 | TREE_READONLY (ref) = CP_TYPE_CONST_P (t); | |
518 | TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t); | |
519 | TREE_SIDE_EFFECTS (ref) | |
520 | = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val)); | |
521 | REFERENCE_REF_P (ref) = 1; | |
522 | val = ref; | |
523 | } | |
9031d10b | 524 | |
471086d6 | 525 | return val; |
526 | } | |
e5dab226 | 527 | |
528 | /* Really perform an lvalue-to-rvalue conversion, including copying an | |
529 | argument of class type into a temporary. */ | |
530 | ||
531 | tree | |
532 | force_rvalue (tree expr) | |
533 | { | |
95397ff9 | 534 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR) |
e5dab226 | 535 | expr = ocp_convert (TREE_TYPE (expr), expr, |
536 | CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL); | |
537 | else | |
538 | expr = decay_conversion (expr); | |
539 | ||
540 | return expr; | |
541 | } | |
471086d6 | 542 | \f |
c4a8ac95 | 543 | /* C++ conversions, preference to static cast conversions. */ |
544 | ||
545 | tree | |
35771a9a | 546 | cp_convert (tree type, tree expr) |
c4a8ac95 | 547 | { |
548 | return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL); | |
549 | } | |
550 | ||
59dd8856 | 551 | /* C++ equivalent of convert_and_check but using cp_convert as the |
552 | conversion function. | |
553 | ||
554 | Convert EXPR to TYPE, warning about conversion problems with constants. | |
555 | Invoke this function on every expression that is converted implicitly, | |
556 | i.e. because of language rules and not because of an explicit cast. */ | |
557 | ||
558 | tree | |
559 | cp_convert_and_check (tree type, tree expr) | |
560 | { | |
561 | tree result; | |
562 | ||
563 | if (TREE_TYPE (expr) == type) | |
564 | return expr; | |
565 | ||
566 | result = cp_convert (type, expr); | |
567 | ||
7ee0d227 | 568 | if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node) |
59dd8856 | 569 | warnings_for_convert_and_check (type, expr, result); |
570 | ||
571 | return result; | |
572 | } | |
573 | ||
b248d3f7 | 574 | /* Conversion... |
575 | ||
576 | FLAGS indicates how we should behave. */ | |
577 | ||
471086d6 | 578 | tree |
35771a9a | 579 | ocp_convert (tree type, tree expr, int convtype, int flags) |
471086d6 | 580 | { |
cd16867a | 581 | tree e = expr; |
582 | enum tree_code code = TREE_CODE (type); | |
7a979707 | 583 | const char *invalid_conv_diag; |
471086d6 | 584 | |
1bc16cab | 585 | if (error_operand_p (e) || type == error_mark_node) |
471086d6 | 586 | return error_mark_node; |
d81e00a4 | 587 | |
5b592939 | 588 | complete_type (type); |
589 | complete_type (TREE_TYPE (expr)); | |
590 | ||
7a979707 | 591 | if ((invalid_conv_diag |
592 | = targetm.invalid_conversion (TREE_TYPE (expr), type))) | |
593 | { | |
594 | error (invalid_conv_diag); | |
595 | return error_mark_node; | |
596 | } | |
597 | ||
13f0eb20 | 598 | e = integral_constant_value (e); |
7745052b | 599 | |
95397ff9 | 600 | if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP)) |
1a3f833b | 601 | /* We need a new temporary; don't take this shortcut. */; |
930e8175 | 602 | else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e))) |
ca23ec64 | 603 | { |
daf9ff67 | 604 | if (same_type_p (type, TREE_TYPE (e))) |
ca23ec64 | 605 | /* The call to fold will not always remove the NOP_EXPR as |
606 | might be expected, since if one of the types is a typedef; | |
755edffd | 607 | the comparison in fold is just equality of pointers, not a |
a09db423 | 608 | call to comptypes. We don't call fold in this case because |
f699c174 | 609 | that can result in infinite recursion; fold will call |
610 | convert, which will call ocp_convert, etc. */ | |
611 | return e; | |
372e0e29 | 612 | /* For complex data types, we need to perform componentwise |
653e5405 | 613 | conversion. */ |
372e0e29 | 614 | else if (TREE_CODE (type) == COMPLEX_TYPE) |
653e5405 | 615 | return fold_if_not_in_template (convert_to_complex (type, e)); |
bdb2219e | 616 | else if (TREE_CODE (e) == TARGET_EXPR) |
617 | { | |
618 | /* Don't build a NOP_EXPR of class type. Instead, change the | |
930e8175 | 619 | type of the temporary. */ |
bdb2219e | 620 | TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type; |
621 | return e; | |
622 | } | |
ca23ec64 | 623 | else |
092b1d6f | 624 | { |
625 | /* We shouldn't be treating objects of ADDRESSABLE type as | |
626 | rvalues. */ | |
627 | gcc_assert (!TREE_ADDRESSABLE (type)); | |
5d7ed6c7 | 628 | return fold_if_not_in_template (build_nop (type, e)); |
092b1d6f | 629 | } |
ca23ec64 | 630 | } |
631 | ||
d81e00a4 | 632 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) |
95b2ac55 | 633 | { |
ebd21de4 | 634 | e = convert_to_void (e, /*implicit=*/NULL, tf_warning_or_error); |
aeef2be5 | 635 | return e; |
95b2ac55 | 636 | } |
d81e00a4 | 637 | |
bb0726a1 | 638 | if (INTEGRAL_CODE_P (code)) |
471086d6 | 639 | { |
617abf06 | 640 | tree intype = TREE_TYPE (e); |
384590d4 | 641 | |
642 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
643 | { | |
644 | /* enum = enum, enum = int, enum = float, (enum)pointer are all | |
645 | errors. */ | |
646 | if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype) | |
653e5405 | 647 | || TREE_CODE (intype) == REAL_TYPE) |
b312a686 | 648 | && ! (convtype & CONV_STATIC)) |
384590d4 | 649 | || TREE_CODE (intype) == POINTER_TYPE) |
650 | { | |
651 | if (flags & LOOKUP_COMPLAIN) | |
2b9e3597 | 652 | permerror (input_location, "conversion from %q#T to %q#T", intype, type); |
471086d6 | 653 | |
384590d4 | 654 | if (!flag_permissive) |
655 | return error_mark_node; | |
656 | } | |
657 | ||
658 | /* [expr.static.cast] | |
659 | ||
660 | 8. A value of integral or enumeration type can be explicitly | |
661 | converted to an enumeration type. The value is unchanged if | |
662 | the original value is within the range of the enumeration | |
663 | values. Otherwise, the resulting enumeration value is | |
664 | unspecified. */ | |
665 | if (TREE_CODE (expr) == INTEGER_CST && !int_fits_type_p (expr, type)) | |
666 | warning (OPT_Wconversion, | |
667 | "the result of the conversion is unspecified because " | |
668 | "%qE is outside the range of type %qT", | |
669 | expr, type); | |
471086d6 | 670 | } |
95397ff9 | 671 | if (MAYBE_CLASS_TYPE_P (intype)) |
471086d6 | 672 | { |
673 | tree rval; | |
8999978b | 674 | rval = build_type_conversion (type, e); |
6495357a | 675 | if (rval) |
676 | return rval; | |
b248d3f7 | 677 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 678 | error ("%q#T used where a %qT was expected", intype, type); |
471086d6 | 679 | return error_mark_node; |
680 | } | |
bb0726a1 | 681 | if (code == BOOLEAN_TYPE) |
ce871053 | 682 | return cp_truthvalue_conversion (e); |
683 | ||
5d7ed6c7 | 684 | return fold_if_not_in_template (convert_to_integer (type, e)); |
471086d6 | 685 | } |
1bc16cab | 686 | if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type)) |
e5ead12a | 687 | return fold_if_not_in_template (cp_convert_to_pointer (type, e)); |
684b9d79 | 688 | if (code == VECTOR_TYPE) |
c37ff371 | 689 | { |
690 | tree in_vtype = TREE_TYPE (e); | |
95397ff9 | 691 | if (MAYBE_CLASS_TYPE_P (in_vtype)) |
c37ff371 | 692 | { |
693 | tree ret_val; | |
694 | ret_val = build_type_conversion (type, e); | |
653e5405 | 695 | if (ret_val) |
696 | return ret_val; | |
697 | if (flags & LOOKUP_COMPLAIN) | |
698 | error ("%q#T used where a %qT was expected", in_vtype, type); | |
699 | return error_mark_node; | |
c37ff371 | 700 | } |
5d7ed6c7 | 701 | return fold_if_not_in_template (convert_to_vector (type, e)); |
c37ff371 | 702 | } |
c4a8ac95 | 703 | if (code == REAL_TYPE || code == COMPLEX_TYPE) |
471086d6 | 704 | { |
95397ff9 | 705 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e))) |
471086d6 | 706 | { |
707 | tree rval; | |
8999978b | 708 | rval = build_type_conversion (type, e); |
471086d6 | 709 | if (rval) |
710 | return rval; | |
711 | else | |
b248d3f7 | 712 | if (flags & LOOKUP_COMPLAIN) |
0a25aad6 | 713 | error ("%q#T used where a floating point value was expected", |
b248d3f7 | 714 | TREE_TYPE (e)); |
471086d6 | 715 | } |
c4a8ac95 | 716 | if (code == REAL_TYPE) |
5d7ed6c7 | 717 | return fold_if_not_in_template (convert_to_real (type, e)); |
c4a8ac95 | 718 | else if (code == COMPLEX_TYPE) |
5d7ed6c7 | 719 | return fold_if_not_in_template (convert_to_complex (type, e)); |
471086d6 | 720 | } |
721 | ||
722 | /* New C++ semantics: since assignment is now based on | |
723 | memberwise copying, if the rhs type is derived from the | |
724 | lhs type, then we may still do a conversion. */ | |
95397ff9 | 725 | if (RECORD_OR_UNION_CODE_P (code)) |
471086d6 | 726 | { |
727 | tree dtype = TREE_TYPE (e); | |
c25194fd | 728 | tree ctor = NULL_TREE; |
471086d6 | 729 | |
471086d6 | 730 | dtype = TYPE_MAIN_VARIANT (dtype); |
731 | ||
471086d6 | 732 | /* Conversion between aggregate types. New C++ semantics allow |
733 | objects of derived type to be cast to objects of base type. | |
734 | Old semantics only allowed this between pointers. | |
735 | ||
736 | There may be some ambiguity between using a constructor | |
737 | vs. using a type conversion operator when both apply. */ | |
738 | ||
0a4be248 | 739 | ctor = e; |
860740a7 | 740 | |
8c18e707 | 741 | if (abstract_virtuals_error (NULL_TREE, type)) |
742 | return error_mark_node; | |
6cbb4197 | 743 | |
f82f1250 | 744 | if (BRACE_ENCLOSED_INITIALIZER_P (ctor)) |
745 | ctor = perform_implicit_conversion (type, ctor, tf_warning_or_error); | |
746 | else if ((flags & LOOKUP_ONLYCONVERTING) | |
747 | && ! (CLASS_TYPE_P (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 |
9031d10b | 753 | ctor = build_special_member_call (NULL_TREE, |
f70cb9e6 | 754 | complete_ctor_identifier, |
755 | build_tree_list (NULL_TREE, ctor), | |
ebd21de4 | 756 | type, flags, |
757 | tf_warning_or_error); | |
0a4be248 | 758 | if (ctor) |
759 | return build_cplus_new (type, ctor); | |
471086d6 | 760 | } |
761 | ||
b248d3f7 | 762 | if (flags & LOOKUP_COMPLAIN) |
a7cc1549 | 763 | { |
764 | /* If the conversion failed and expr was an invalid use of pointer to | |
765 | member function, try to report a meaningful error. */ | |
766 | if (invalid_nonstatic_memfn_p (expr, tf_warning_or_error)) | |
767 | /* We displayed the error message. */; | |
768 | else | |
769 | error ("conversion from %qT to non-scalar type %qT requested", | |
770 | TREE_TYPE (expr), type); | |
771 | } | |
471086d6 | 772 | return error_mark_node; |
773 | } | |
774 | ||
b1a8d4ce | 775 | /* When an expression is used in a void context, its value is discarded and |
776 | no lvalue-rvalue and similar conversions happen [expr.static.cast/4, | |
777 | stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type | |
778 | in a void context. The C++ standard does not define what an `access' to an | |
63eff20d | 779 | object is, but there is reason to believe that it is the lvalue to rvalue |
b1a8d4ce | 780 | conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it |
781 | accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8 | |
782 | indicates that volatile semantics should be the same between C and C++ | |
783 | where ever possible. C leaves it implementation defined as to what | |
784 | constitutes an access to a volatile. So, we interpret `*vp' as a read of | |
785 | the volatile object `vp' points to, unless that is an incomplete type. For | |
786 | volatile references we do not do this interpretation, because that would | |
787 | make it impossible to ignore the reference return value from functions. We | |
788 | issue warnings in the confusing cases. | |
9031d10b | 789 | |
f8646185 | 790 | IMPLICIT is non-NULL iff an expression is being implicitly converted; it |
791 | is NULL when the user is explicitly converting an expression to void via | |
792 | a cast. When non-NULL, IMPLICIT is a string indicating the context of | |
793 | the implicit conversion. */ | |
b1a8d4ce | 794 | |
795 | tree | |
ebd21de4 | 796 | convert_to_void (tree expr, const char *implicit, tsubst_flags_t complain) |
b1a8d4ce | 797 | { |
9031d10b | 798 | if (expr == error_mark_node |
3d411d73 | 799 | || TREE_TYPE (expr) == error_mark_node) |
800 | return error_mark_node; | |
b1a8d4ce | 801 | if (!TREE_TYPE (expr)) |
802 | return expr; | |
ebd21de4 | 803 | if (invalid_nonstatic_memfn_p (expr, complain)) |
334f6ce1 | 804 | return error_mark_node; |
ed36f1cf | 805 | if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) |
806 | { | |
ebd21de4 | 807 | if (complain & tf_error) |
808 | error ("pseudo-destructor is not called"); | |
ed36f1cf | 809 | return error_mark_node; |
810 | } | |
e3cfe3ce | 811 | if (VOID_TYPE_P (TREE_TYPE (expr))) |
b1a8d4ce | 812 | return expr; |
813 | switch (TREE_CODE (expr)) | |
814 | { | |
815 | case COND_EXPR: | |
816 | { | |
653e5405 | 817 | /* The two parts of a cond expr might be separate lvalues. */ |
818 | tree op1 = TREE_OPERAND (expr,1); | |
819 | tree op2 = TREE_OPERAND (expr,2); | |
820 | tree new_op1 = convert_to_void | |
00fa9079 | 821 | (op1, (implicit && !TREE_SIDE_EFFECTS (op2) |
ebd21de4 | 822 | ? "second operand of conditional" : NULL), complain); |
653e5405 | 823 | tree new_op2 = convert_to_void |
00fa9079 | 824 | (op2, (implicit && !TREE_SIDE_EFFECTS (op1) |
ebd21de4 | 825 | ? "third operand of conditional" : NULL), complain); |
9031d10b | 826 | |
831d52a2 | 827 | expr = build3 (COND_EXPR, TREE_TYPE (new_op1), |
828 | TREE_OPERAND (expr, 0), new_op1, new_op2); | |
653e5405 | 829 | break; |
b1a8d4ce | 830 | } |
9031d10b | 831 | |
b1a8d4ce | 832 | case COMPOUND_EXPR: |
833 | { | |
653e5405 | 834 | /* The second part of a compound expr contains the value. */ |
835 | tree op1 = TREE_OPERAND (expr,1); | |
836 | tree new_op1 = convert_to_void | |
4ee9c684 | 837 | (op1, (implicit && !TREE_NO_WARNING (expr) |
ebd21de4 | 838 | ? "right-hand operand of comma" : NULL), complain); |
9031d10b | 839 | |
653e5405 | 840 | if (new_op1 != op1) |
e907a2e9 | 841 | { |
831d52a2 | 842 | tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1), |
843 | TREE_OPERAND (expr, 0), new_op1); | |
e907a2e9 | 844 | expr = t; |
845 | } | |
846 | ||
653e5405 | 847 | break; |
b1a8d4ce | 848 | } |
9031d10b | 849 | |
b1a8d4ce | 850 | case NON_LVALUE_EXPR: |
851 | case NOP_EXPR: | |
c0af329c | 852 | /* These have already decayed to rvalue. */ |
b1a8d4ce | 853 | break; |
9031d10b | 854 | |
331bc0ad | 855 | case CALL_EXPR: /* We have a special meaning for volatile void fn(). */ |
b1a8d4ce | 856 | break; |
9031d10b | 857 | |
b1a8d4ce | 858 | case INDIRECT_REF: |
859 | { | |
653e5405 | 860 | tree type = TREE_TYPE (expr); |
861 | int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) | |
862 | == REFERENCE_TYPE; | |
863 | int is_volatile = TYPE_VOLATILE (type); | |
864 | int is_complete = COMPLETE_TYPE_P (complete_type (type)); | |
865 | ||
dc02da7f | 866 | /* Can't load the value if we don't know the type. */ |
653e5405 | 867 | if (is_volatile && !is_complete) |
ebd21de4 | 868 | { |
869 | if (complain & tf_warning) | |
870 | warning (0, "object of incomplete type %qT will not be accessed in %s", | |
871 | type, implicit ? implicit : "void context"); | |
872 | } | |
dc02da7f | 873 | /* Don't load the value if this is an implicit dereference, or if |
874 | the type needs to be handled by ctors/dtors. */ | |
875 | else if (is_volatile && (is_reference || TREE_ADDRESSABLE (type))) | |
ebd21de4 | 876 | { |
877 | if (complain & tf_warning) | |
878 | warning (0, "object of type %qT will not be accessed in %s", | |
879 | TREE_TYPE (TREE_OPERAND (expr, 0)), | |
880 | implicit ? implicit : "void context"); | |
881 | } | |
dc02da7f | 882 | if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type)) |
a4d4030b | 883 | { |
884 | /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF | |
885 | operation is stripped off. Note that we don't warn about | |
886 | - an expression with TREE_NO_WARNING set. (For an example of | |
887 | such expressions, see build_over_call in call.c.) | |
888 | - automatic dereferencing of references, since the user cannot | |
889 | control it. (See also warn_if_unused_value() in stmt.c.) */ | |
890 | if (warn_unused_value | |
f8646185 | 891 | && implicit |
a4d4030b | 892 | && (complain & tf_warning) |
893 | && !TREE_NO_WARNING (expr) | |
894 | && !is_reference) | |
895 | warning (OPT_Wunused_value, "value computed is not used"); | |
896 | expr = TREE_OPERAND (expr, 0); | |
897 | } | |
653e5405 | 898 | |
899 | break; | |
b1a8d4ce | 900 | } |
9031d10b | 901 | |
b1a8d4ce | 902 | case VAR_DECL: |
903 | { | |
653e5405 | 904 | /* External variables might be incomplete. */ |
905 | tree type = TREE_TYPE (expr); | |
906 | int is_complete = COMPLETE_TYPE_P (complete_type (type)); | |
907 | ||
ebd21de4 | 908 | if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning)) |
653e5405 | 909 | warning (0, "object %qE of incomplete type %qT will not be accessed in %s", |
910 | expr, type, implicit ? implicit : "void context"); | |
911 | break; | |
b1a8d4ce | 912 | } |
d9f88785 | 913 | |
25b3017b | 914 | case TARGET_EXPR: |
915 | /* Don't bother with the temporary object returned from a function if | |
916 | we don't use it and don't need to destroy it. We'll still | |
917 | allocate space for it in expand_call or declare_return_variable, | |
918 | but we don't need to track it through all the tree phases. */ | |
67bb1301 | 919 | if (TARGET_EXPR_IMPLICIT_P (expr) |
25b3017b | 920 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr))) |
921 | { | |
922 | tree init = TARGET_EXPR_INITIAL (expr); | |
923 | if (TREE_CODE (init) == AGGR_INIT_EXPR | |
924 | && !AGGR_INIT_VIA_CTOR_P (init)) | |
925 | { | |
c2f47e15 | 926 | tree fn = AGGR_INIT_EXPR_FN (init); |
927 | expr = build_call_array (TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), | |
928 | fn, | |
929 | aggr_init_expr_nargs (init), | |
930 | AGGR_INIT_EXPR_ARGP (init)); | |
25b3017b | 931 | } |
932 | } | |
933 | break; | |
934 | ||
b1a8d4ce | 935 | default:; |
936 | } | |
937 | { | |
938 | tree probe = expr; | |
9031d10b | 939 | |
b1a8d4ce | 940 | if (TREE_CODE (probe) == ADDR_EXPR) |
941 | probe = TREE_OPERAND (expr, 0); | |
2e0e6579 | 942 | if (type_unknown_p (probe)) |
943 | { | |
944 | /* [over.over] enumerates the places where we can take the address | |
945 | of an overloaded function, and this is not one of them. */ | |
ebd21de4 | 946 | if (complain & tf_error) |
947 | error ("%s cannot resolve address of overloaded function", | |
948 | implicit ? implicit : "void cast"); | |
949 | else | |
950 | return error_mark_node; | |
ea720917 | 951 | expr = void_zero_node; |
2e0e6579 | 952 | } |
953 | else if (implicit && probe == expr && is_overloaded_fn (probe)) | |
2056769e | 954 | { |
955 | /* Only warn when there is no &. */ | |
ebd21de4 | 956 | if (complain & tf_warning) |
957 | warning (OPT_Waddress, "%s is a reference, not call, to function %qE", | |
958 | implicit, expr); | |
2056769e | 959 | if (TREE_CODE (expr) == COMPONENT_REF) |
960 | expr = TREE_OPERAND (expr, 0); | |
961 | } | |
b1a8d4ce | 962 | } |
9031d10b | 963 | |
e3cfe3ce | 964 | if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr))) |
b1a8d4ce | 965 | { |
5e2a4133 | 966 | if (implicit |
967 | && warn_unused_value | |
968 | && !TREE_NO_WARNING (expr) | |
969 | && !processing_template_decl) | |
1fe46df1 | 970 | { |
971 | /* The middle end does not warn about expressions that have | |
972 | been explicitly cast to void, so we must do so here. */ | |
ebd21de4 | 973 | if (!TREE_SIDE_EFFECTS (expr)) { |
974 | if (complain & tf_warning) | |
975 | warning (OPT_Wunused_value, "%s has no effect", implicit); | |
976 | } | |
9031d10b | 977 | else |
978 | { | |
1fe46df1 | 979 | tree e; |
980 | enum tree_code code; | |
607a5d68 | 981 | enum tree_code_class tclass; |
9031d10b | 982 | |
1fe46df1 | 983 | e = expr; |
984 | /* We might like to warn about (say) "(int) f()", as the | |
985 | cast has no effect, but the compiler itself will | |
986 | generate implicit conversions under some | |
324d8f2d | 987 | circumstances. (For example a block copy will be |
1fe46df1 | 988 | turned into a call to "__builtin_memcpy", with a |
989 | conversion of the return value to an appropriate | |
990 | type.) So, to avoid false positives, we strip | |
416ce344 | 991 | conversions. Do not use STRIP_NOPs because it will |
992 | not strip conversions to "void", as that is not a | |
993 | mode-preserving conversion. */ | |
994 | while (TREE_CODE (e) == NOP_EXPR) | |
995 | e = TREE_OPERAND (e, 0); | |
1fe46df1 | 996 | |
997 | code = TREE_CODE (e); | |
607a5d68 | 998 | tclass = TREE_CODE_CLASS (code); |
999 | if ((tclass == tcc_comparison | |
1000 | || tclass == tcc_unary | |
1001 | || (tclass == tcc_binary | |
1fe46df1 | 1002 | && !(code == MODIFY_EXPR |
1003 | || code == INIT_EXPR | |
1004 | || code == PREDECREMENT_EXPR | |
1005 | || code == PREINCREMENT_EXPR | |
1006 | || code == POSTDECREMENT_EXPR | |
1007 | || code == POSTINCREMENT_EXPR))) | |
ebd21de4 | 1008 | && (complain & tf_warning)) |
ced7c954 | 1009 | warning (OPT_Wunused_value, "value computed is not used"); |
1fe46df1 | 1010 | } |
1011 | } | |
00fa9079 | 1012 | expr = build1 (CONVERT_EXPR, void_type_node, expr); |
b1a8d4ce | 1013 | } |
de5ad4cb | 1014 | if (! TREE_SIDE_EFFECTS (expr)) |
1015 | expr = void_zero_node; | |
b1a8d4ce | 1016 | return expr; |
1017 | } | |
1018 | ||
d81e00a4 | 1019 | /* Create an expression whose value is that of EXPR, |
1020 | converted to type TYPE. The TREE_TYPE of the value | |
1021 | is always TYPE. This function implements all reasonable | |
1022 | conversions; callers should filter out those that are | |
c4a8ac95 | 1023 | not permitted by the language being compiled. |
1024 | ||
1025 | Most of this routine is from build_reinterpret_cast. | |
1026 | ||
a17c2a3a | 1027 | The back end cannot call cp_convert (what was convert) because |
c4a8ac95 | 1028 | conversions to/from basetypes may involve memory references |
1029 | (vbases) and adding or subtracting small values (multiple | |
1030 | inheritance), but it calls convert from the constant folding code | |
e0aa5007 | 1031 | on subtrees of already built trees after it has ripped them apart. |
c4a8ac95 | 1032 | |
1033 | Also, if we ever support range variables, we'll probably also have to | |
1034 | do a little bit more work. */ | |
d81e00a4 | 1035 | |
1036 | tree | |
35771a9a | 1037 | convert (tree type, tree expr) |
d81e00a4 | 1038 | { |
c4a8ac95 | 1039 | tree intype; |
1040 | ||
1041 | if (type == error_mark_node || expr == error_mark_node) | |
1042 | return error_mark_node; | |
1043 | ||
c4a8ac95 | 1044 | intype = TREE_TYPE (expr); |
1045 | ||
6686e84d | 1046 | if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype)) |
13f0eb20 | 1047 | return fold_if_not_in_template (build_nop (type, expr)); |
c4a8ac95 | 1048 | |
1049 | return ocp_convert (type, expr, CONV_OLD_CONVERT, | |
1050 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); | |
d81e00a4 | 1051 | } |
1052 | ||
c4a8ac95 | 1053 | /* Like cp_convert, except permit conversions to take place which |
471086d6 | 1054 | are not normally allowed due to access restrictions |
1055 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 1056 | |
471086d6 | 1057 | tree |
35771a9a | 1058 | convert_force (tree type, tree expr, int convtype) |
471086d6 | 1059 | { |
cd16867a | 1060 | tree e = expr; |
1061 | enum tree_code code = TREE_CODE (type); | |
471086d6 | 1062 | |
1063 | if (code == REFERENCE_TYPE) | |
9031d10b | 1064 | return (fold_if_not_in_template |
5d7ed6c7 | 1065 | (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN, |
1066 | NULL_TREE))); | |
471086d6 | 1067 | |
1068 | if (code == POINTER_TYPE) | |
5d7ed6c7 | 1069 | return fold_if_not_in_template (convert_to_pointer_force (type, e)); |
471086d6 | 1070 | |
ac9386a0 | 1071 | /* From typeck.c convert_for_assignment */ |
471086d6 | 1072 | if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR |
1073 | && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE | |
1074 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) | |
ac9386a0 | 1075 | || integer_zerop (e) |
1076 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
471086d6 | 1077 | && TYPE_PTRMEMFUNC_P (type)) |
cb02169c | 1078 | /* compatible pointer to member functions. */ |
1079 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1, | |
1080 | /*c_cast_p=*/1); | |
a74e8896 | 1081 | |
c4a8ac95 | 1082 | return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL); |
471086d6 | 1083 | } |
1084 | ||
471086d6 | 1085 | /* Convert an aggregate EXPR to type XTYPE. If a conversion |
1086 | exists, return the attempted conversion. This may | |
1087 | return ERROR_MARK_NODE if the conversion is not | |
1088 | allowed (references private members, etc). | |
1089 | If no conversion exists, NULL_TREE is returned. | |
1090 | ||
ce28ee2e | 1091 | FIXME: Ambiguity checking is wrong. Should choose one by the implicit |
1092 | object parameter, or by the second standard conversion sequence if | |
1093 | that doesn't do it. This will probably wait for an overloading rewrite. | |
1094 | (jason 8/9/95) */ | |
471086d6 | 1095 | |
ef9571e5 | 1096 | static tree |
8999978b | 1097 | build_type_conversion (tree xtype, tree expr) |
471086d6 | 1098 | { |
1099 | /* C++: check to see if we can convert this aggregate type | |
bcf789d7 | 1100 | into the required type. */ |
8999978b | 1101 | return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL); |
471086d6 | 1102 | } |
1103 | ||
1e66592c | 1104 | /* Convert the given EXPR to one of a group of types suitable for use in an |
1105 | expression. DESIRES is a combination of various WANT_* flags (q.v.) | |
35771a9a | 1106 | which indicates which types are suitable. If COMPLAIN is true, complain |
1e66592c | 1107 | about ambiguity; otherwise, the caller will deal with it. */ |
471086d6 | 1108 | |
1e66592c | 1109 | tree |
35771a9a | 1110 | build_expr_type_conversion (int desires, tree expr, bool complain) |
471086d6 | 1111 | { |
1e66592c | 1112 | tree basetype = TREE_TYPE (expr); |
6a44e72e | 1113 | tree conv = NULL_TREE; |
bdd152ce | 1114 | tree winner = NULL_TREE; |
471086d6 | 1115 | |
9031d10b | 1116 | if (expr == null_node |
1117 | && (desires & WANT_INT) | |
954885ed | 1118 | && !(desires & WANT_NULL)) |
308d6af4 | 1119 | warning (OPT_Wconversion, "converting NULL to non-pointer type"); |
9031d10b | 1120 | |
ce28ee2e | 1121 | basetype = TREE_TYPE (expr); |
471086d6 | 1122 | |
3d411d73 | 1123 | if (basetype == error_mark_node) |
1124 | return error_mark_node; | |
1125 | ||
95397ff9 | 1126 | if (! MAYBE_CLASS_TYPE_P (basetype)) |
bdd152ce | 1127 | switch (TREE_CODE (basetype)) |
1128 | { | |
1129 | case INTEGER_TYPE: | |
954885ed | 1130 | if ((desires & WANT_NULL) && null_ptr_cst_p (expr)) |
bdd152ce | 1131 | return expr; |
1132 | /* else fall through... */ | |
1133 | ||
1134 | case BOOLEAN_TYPE: | |
1135 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1136 | case ENUMERAL_TYPE: | |
1137 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1138 | case REAL_TYPE: | |
1139 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1140 | case POINTER_TYPE: | |
1141 | return (desires & WANT_POINTER) ? expr : NULL_TREE; | |
9031d10b | 1142 | |
bdd152ce | 1143 | case FUNCTION_TYPE: |
1144 | case ARRAY_TYPE: | |
a681799d | 1145 | return (desires & WANT_POINTER) ? decay_conversion (expr) |
653e5405 | 1146 | : NULL_TREE; |
96ec9e30 | 1147 | |
1148 | case VECTOR_TYPE: | |
1149 | if ((desires & WANT_VECTOR) == 0) | |
1150 | return NULL_TREE; | |
1151 | switch (TREE_CODE (TREE_TYPE (basetype))) | |
1152 | { | |
1153 | case INTEGER_TYPE: | |
1154 | case BOOLEAN_TYPE: | |
1155 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1156 | case ENUMERAL_TYPE: | |
1157 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1158 | case REAL_TYPE: | |
1159 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1160 | default: | |
1161 | return NULL_TREE; | |
1162 | } | |
1163 | ||
bdd152ce | 1164 | default: |
1165 | return NULL_TREE; | |
1166 | } | |
1167 | ||
1168 | /* The code for conversions from class type is currently only used for | |
1169 | delete expressions. Other expressions are handled by build_new_op. */ | |
f8182e08 | 1170 | if (!complete_type_or_else (basetype, expr)) |
1171 | return error_mark_node; | |
1172 | if (!TYPE_HAS_CONVERSION (basetype)) | |
bdd152ce | 1173 | return NULL_TREE; |
1174 | ||
1175 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) | |
1176 | { | |
1177 | int win = 0; | |
1178 | tree candidate; | |
1179 | tree cand = TREE_VALUE (conv); | |
1180 | ||
1181 | if (winner && winner == cand) | |
1182 | continue; | |
1183 | ||
ef4534a3 | 1184 | candidate = non_reference (TREE_TYPE (TREE_TYPE (cand))); |
bdd152ce | 1185 | |
1186 | switch (TREE_CODE (candidate)) | |
1187 | { | |
1188 | case BOOLEAN_TYPE: | |
1189 | case INTEGER_TYPE: | |
1190 | win = (desires & WANT_INT); break; | |
1191 | case ENUMERAL_TYPE: | |
1192 | win = (desires & WANT_ENUM); break; | |
1193 | case REAL_TYPE: | |
1194 | win = (desires & WANT_FLOAT); break; | |
1195 | case POINTER_TYPE: | |
1196 | win = (desires & WANT_POINTER); break; | |
1197 | ||
96ec9e30 | 1198 | case VECTOR_TYPE: |
1199 | if ((desires & WANT_VECTOR) == 0) | |
1200 | break; | |
1201 | switch (TREE_CODE (TREE_TYPE (candidate))) | |
1202 | { | |
1203 | case BOOLEAN_TYPE: | |
1204 | case INTEGER_TYPE: | |
1205 | win = (desires & WANT_INT); break; | |
1206 | case ENUMERAL_TYPE: | |
1207 | win = (desires & WANT_ENUM); break; | |
1208 | case REAL_TYPE: | |
1209 | win = (desires & WANT_FLOAT); break; | |
1210 | default: | |
1211 | break; | |
1212 | } | |
1213 | break; | |
1214 | ||
bdd152ce | 1215 | default: |
1216 | break; | |
1217 | } | |
1218 | ||
1219 | if (win) | |
1220 | { | |
1221 | if (winner) | |
1222 | { | |
1223 | if (complain) | |
1224 | { | |
0a25aad6 | 1225 | error ("ambiguous default type conversion from %qT", |
653e5405 | 1226 | basetype); |
0a25aad6 | 1227 | error (" candidate conversions include %qD and %qD", |
653e5405 | 1228 | winner, cand); |
bdd152ce | 1229 | } |
1230 | return error_mark_node; | |
1231 | } | |
1232 | else | |
1233 | winner = cand; | |
1234 | } | |
1235 | } | |
1e66592c | 1236 | |
bdd152ce | 1237 | if (winner) |
1238 | { | |
ef4534a3 | 1239 | tree type = non_reference (TREE_TYPE (TREE_TYPE (winner))); |
bdd152ce | 1240 | return build_user_type_conversion (type, expr, LOOKUP_NORMAL); |
471086d6 | 1241 | } |
1e66592c | 1242 | |
985e9ee0 | 1243 | return NULL_TREE; |
471086d6 | 1244 | } |
bc3887cf | 1245 | |
96624a9e | 1246 | /* Implements integral promotion (4.1) and float->double promotion. */ |
1247 | ||
bc3887cf | 1248 | tree |
35771a9a | 1249 | type_promotes_to (tree type) |
bc3887cf | 1250 | { |
ce28ee2e | 1251 | if (type == error_mark_node) |
1252 | return error_mark_node; | |
1253 | ||
bc3887cf | 1254 | type = TYPE_MAIN_VARIANT (type); |
bb0726a1 | 1255 | |
1256 | /* bool always promotes to int (not unsigned), even if it's the same | |
1257 | size. */ | |
028ea8b4 | 1258 | if (type == boolean_type_node) |
bb0726a1 | 1259 | type = integer_type_node; |
1260 | ||
1261 | /* Normally convert enums to int, but convert wide enums to something | |
1262 | wider. */ | |
1263 | else if (TREE_CODE (type) == ENUMERAL_TYPE | |
924bbf02 | 1264 | || type == char16_type_node |
1265 | || type == char32_type_node | |
bb0726a1 | 1266 | || type == wchar_type_node) |
6c9497b1 | 1267 | { |
1268 | int precision = MAX (TYPE_PRECISION (type), | |
1269 | TYPE_PRECISION (integer_type_node)); | |
771d21fa | 1270 | tree totype = c_common_type_for_size (precision, 0); |
78a8ed03 | 1271 | if (TYPE_UNSIGNED (type) |
6c9497b1 | 1272 | && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) |
771d21fa | 1273 | type = c_common_type_for_size (precision, 1); |
6c9497b1 | 1274 | else |
1275 | type = totype; | |
1276 | } | |
d7aeef06 | 1277 | else if (c_promoting_integer_type_p (type)) |
bc3887cf | 1278 | { |
d0acef9e | 1279 | /* Retain unsignedness if really not getting bigger. */ |
78a8ed03 | 1280 | if (TYPE_UNSIGNED (type) |
d0acef9e | 1281 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
bc3887cf | 1282 | type = unsigned_type_node; |
1283 | else | |
1284 | type = integer_type_node; | |
1285 | } | |
1286 | else if (type == float_type_node) | |
1287 | type = double_type_node; | |
9031d10b | 1288 | |
a681799d | 1289 | return type; |
bc3887cf | 1290 | } |
668ae905 | 1291 | |
668ae905 | 1292 | /* The routines below this point are carefully written to conform to |
1293 | the standard. They use the same terminology, and follow the rules | |
1294 | closely. Although they are used only in pt.c at the moment, they | |
1295 | should presumably be used everywhere in the future. */ | |
1296 | ||
1146f179 | 1297 | /* Attempt to perform qualification conversions on EXPR to convert it |
1298 | to TYPE. Return the resulting expression, or error_mark_node if | |
1299 | the conversion was impossible. */ | |
1300 | ||
9031d10b | 1301 | tree |
35771a9a | 1302 | perform_qualification_conversions (tree type, tree expr) |
668ae905 | 1303 | { |
1bc16cab | 1304 | tree expr_type; |
1305 | ||
1306 | expr_type = TREE_TYPE (expr); | |
1307 | ||
3b087710 | 1308 | if (same_type_p (type, expr_type)) |
1309 | return expr; | |
1310 | else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type) | |
1311 | && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type))) | |
1bc16cab | 1312 | return build_nop (type, expr); |
1313 | else if (TYPE_PTR_TO_MEMBER_P (type) | |
1314 | && TYPE_PTR_TO_MEMBER_P (expr_type) | |
1315 | && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), | |
1316 | TYPE_PTRMEM_CLASS_TYPE (expr_type)) | |
1317 | && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type), | |
1318 | TYPE_PTRMEM_POINTED_TO_TYPE (expr_type))) | |
1319 | return build_nop (type, expr); | |
1146f179 | 1320 | else |
1321 | return error_mark_node; | |
668ae905 | 1322 | } |