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