]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Language-level data type conversion for GNU C++. |
c5aa1e92 | 2 | Copyright (C) 1987, 88, 92-96, 1998 Free Software Foundation, Inc. |
471086d6 | 3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
c58d4270 | 19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
471086d6 | 21 | |
22 | ||
23 | /* This file contains the functions for converting C expressions | |
24 | to different data types. The only entry point is `convert'. | |
25 | Every language front end must have a `convert' function | |
26 | but what kind of conversions it does will depend on the language. */ | |
27 | ||
28 | #include "config.h" | |
b3ef7553 | 29 | #include "system.h" |
471086d6 | 30 | #include "tree.h" |
31 | #include "flags.h" | |
32 | #include "cp-tree.h" | |
471086d6 | 33 | #include "convert.h" |
34 | ||
860740a7 | 35 | extern tree static_aggregates; |
96624a9e | 36 | |
71ccdfff | 37 | static tree cp_convert_to_pointer PROTO((tree, tree)); |
38 | static tree convert_to_pointer_force PROTO((tree, tree)); | |
6a44e72e | 39 | static tree build_up_reference PROTO((tree, tree, int)); |
71ccdfff | 40 | |
471086d6 | 41 | /* Change of width--truncation and extension of integers or reals-- |
42 | is represented with NOP_EXPR. Proper functioning of many things | |
43 | assumes that no other conversions can be NOP_EXPRs. | |
44 | ||
45 | Conversion between integer and pointer is represented with CONVERT_EXPR. | |
46 | Converting integer to real uses FLOAT_EXPR | |
47 | and real to integer uses FIX_TRUNC_EXPR. | |
48 | ||
49 | Here is a list of all the functions that assume that widening and | |
50 | narrowing is always done with a NOP_EXPR: | |
51 | In convert.c, convert_to_integer. | |
52 | In c-typeck.c, build_binary_op_nodefault (boolean ops), | |
53 | and truthvalue_conversion. | |
54 | In expr.c: expand_expr, for operands of a MULT_EXPR. | |
55 | In fold-const.c: fold. | |
56 | In tree.c: get_narrower and get_unwidened. | |
57 | ||
58 | C++: in multiple-inheritance, converting between pointers may involve | |
59 | adjusting them by a delta stored within the class definition. */ | |
60 | \f | |
61 | /* Subroutines of `convert'. */ | |
62 | ||
471086d6 | 63 | /* if converting pointer to pointer |
64 | if dealing with classes, check for derived->base or vice versa | |
65 | else if dealing with method pointers, delegate | |
66 | else convert blindly | |
67 | else if converting class, pass off to build_type_conversion | |
68 | else try C-style pointer conversion */ | |
96624a9e | 69 | |
471086d6 | 70 | static tree |
71 | cp_convert_to_pointer (type, expr) | |
72 | tree type, expr; | |
73 | { | |
74 | register tree intype = TREE_TYPE (expr); | |
74002e1d | 75 | register enum tree_code form; |
f82eeb87 | 76 | tree rval; |
74002e1d | 77 | |
96624a9e | 78 | if (IS_AGGR_TYPE (intype)) |
79 | { | |
96624a9e | 80 | intype = complete_type (intype); |
81 | if (TYPE_SIZE (intype) == NULL_TREE) | |
82 | { | |
905d4035 | 83 | cp_error ("can't convert from incomplete type `%T' to `%T'", |
96624a9e | 84 | intype, type); |
85 | return error_mark_node; | |
86 | } | |
87 | ||
88 | rval = build_type_conversion (CONVERT_EXPR, type, expr, 1); | |
89 | if (rval) | |
90 | { | |
91 | if (rval == error_mark_node) | |
905d4035 | 92 | cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous", |
96624a9e | 93 | expr, intype, type); |
94 | return rval; | |
95 | } | |
96 | } | |
97 | ||
74002e1d | 98 | if (TYPE_PTRMEMFUNC_P (type)) |
99 | type = TYPE_PTRMEMFUNC_FN_TYPE (type); | |
74002e1d | 100 | |
860740a7 | 101 | /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */ |
38281c46 | 102 | if (TREE_CODE (type) == POINTER_TYPE |
103 | && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
860740a7 | 104 | || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)) |
38281c46 | 105 | { |
106 | /* Allow an implicit this pointer for pointer to member | |
107 | functions. */ | |
860740a7 | 108 | if (TYPE_PTRMEMFUNC_P (intype)) |
38281c46 | 109 | { |
110 | tree decl, basebinfo; | |
860740a7 | 111 | tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype)); |
38281c46 | 112 | tree t = TYPE_METHOD_BASETYPE (fntype); |
113 | ||
114 | if (current_class_type == 0 | |
115 | || get_base_distance (t, current_class_type, 0, &basebinfo) | |
116 | == -1) | |
117 | { | |
118 | decl = build1 (NOP_EXPR, t, error_mark_node); | |
119 | } | |
120 | else if (current_class_ptr == 0) | |
121 | decl = build1 (NOP_EXPR, t, error_mark_node); | |
122 | else | |
123 | decl = current_class_ref; | |
124 | ||
125 | expr = build (OFFSET_REF, fntype, decl, expr); | |
126 | } | |
127 | ||
128 | if (TREE_CODE (expr) == OFFSET_REF | |
129 | && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE) | |
130 | expr = resolve_offset_ref (expr); | |
131 | if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE) | |
132 | expr = build_addr_func (expr); | |
133 | if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE) | |
134 | { | |
135 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE) | |
136 | if (pedantic || warn_pmf2ptr) | |
905d4035 | 137 | cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr), |
38281c46 | 138 | type); |
139 | return build1 (NOP_EXPR, type, expr); | |
140 | } | |
141 | intype = TREE_TYPE (expr); | |
142 | } | |
143 | ||
860740a7 | 144 | if (TYPE_PTRMEMFUNC_P (intype)) |
145 | intype = TYPE_PTRMEMFUNC_FN_TYPE (intype); | |
146 | ||
74002e1d | 147 | form = TREE_CODE (intype); |
148 | ||
1a3f833b | 149 | if (form == POINTER_TYPE || form == REFERENCE_TYPE) |
471086d6 | 150 | { |
151 | intype = TYPE_MAIN_VARIANT (intype); | |
152 | ||
153 | if (TYPE_MAIN_VARIANT (type) != intype | |
154 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
bccffb3a | 155 | && IS_AGGR_TYPE (TREE_TYPE (type)) |
156 | && IS_AGGR_TYPE (TREE_TYPE (intype)) | |
2edf445e | 157 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE |
158 | /* If EXPR is NULL, then we don't need to do any arithmetic | |
159 | to convert it: | |
160 | ||
161 | [conv.ptr] | |
162 | ||
163 | The null pointer value is converted to the null pointer | |
164 | value of the destination type. */ | |
165 | && !integer_zerop (expr)) | |
471086d6 | 166 | { |
167 | enum tree_code code = PLUS_EXPR; | |
168 | tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1); | |
169 | if (binfo == error_mark_node) | |
170 | return error_mark_node; | |
171 | if (binfo == NULL_TREE) | |
172 | { | |
173 | binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1); | |
174 | if (binfo == error_mark_node) | |
175 | return error_mark_node; | |
176 | code = MINUS_EXPR; | |
177 | } | |
178 | if (binfo) | |
179 | { | |
180 | if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type)) | |
181 | || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype)) | |
182 | || ! BINFO_OFFSET_ZEROP (binfo)) | |
183 | { | |
184 | /* Need to get the path we took. */ | |
185 | tree path; | |
186 | ||
187 | if (code == PLUS_EXPR) | |
985e9ee0 | 188 | get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), |
189 | 0, &path); | |
471086d6 | 190 | else |
985e9ee0 | 191 | get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), |
192 | 0, &path); | |
471086d6 | 193 | return build_vbase_path (code, type, expr, path, 0); |
194 | } | |
195 | } | |
196 | } | |
74002e1d | 197 | if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE |
471086d6 | 198 | && TREE_CODE (type) == POINTER_TYPE |
74002e1d | 199 | && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE) |
985e9ee0 | 200 | return build_ptrmemfunc (type, expr, 1); |
471086d6 | 201 | |
bea7d742 | 202 | if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE |
203 | && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE) | |
204 | { | |
205 | tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type)); | |
206 | tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)); | |
fc9fc8d8 | 207 | tree binfo = get_binfo (b2, b1, 1); |
208 | enum tree_code code = PLUS_EXPR; | |
209 | ||
bea7d742 | 210 | if (binfo == NULL_TREE) |
fc9fc8d8 | 211 | { |
212 | binfo = get_binfo (b1, b2, 1); | |
213 | code = MINUS_EXPR; | |
214 | } | |
215 | ||
bea7d742 | 216 | if (binfo == error_mark_node) |
217 | return error_mark_node; | |
fc9fc8d8 | 218 | if (binfo && ! TREE_VIA_VIRTUAL (binfo)) |
219 | expr = size_binop (code, expr, BINFO_OFFSET (binfo)); | |
bea7d742 | 220 | } |
221 | ||
ce28ee2e | 222 | if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE |
223 | || (TREE_CODE (type) == POINTER_TYPE | |
224 | && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)) | |
225 | { | |
905d4035 | 226 | cp_error ("cannot convert `%E' from type `%T' to type `%T'", |
ce28ee2e | 227 | expr, intype, type); |
228 | return error_mark_node; | |
229 | } | |
230 | ||
f82eeb87 | 231 | rval = build1 (NOP_EXPR, type, expr); |
232 | TREE_CONSTANT (rval) = TREE_CONSTANT (expr); | |
233 | return rval; | |
471086d6 | 234 | } |
235 | ||
236 | my_friendly_assert (form != OFFSET_TYPE, 186); | |
237 | ||
238 | if (TYPE_LANG_SPECIFIC (intype) | |
239 | && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype))) | |
240 | return convert_to_pointer (type, build_optr_ref (expr)); | |
241 | ||
471086d6 | 242 | if (integer_zerop (expr)) |
243 | { | |
11066dd5 | 244 | if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE) |
245 | return build_ptrmemfunc (type, expr, 0); | |
471086d6 | 246 | expr = build_int_2 (0, 0); |
247 | TREE_TYPE (expr) = type; | |
248 | return expr; | |
249 | } | |
250 | ||
bb0726a1 | 251 | if (INTEGRAL_CODE_P (form)) |
471086d6 | 252 | { |
c5aa1e92 | 253 | if (TYPE_PRECISION (intype) == POINTER_SIZE) |
471086d6 | 254 | return build1 (CONVERT_EXPR, type, expr); |
c4a8ac95 | 255 | expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr); |
471086d6 | 256 | /* Modes may be different but sizes should be the same. */ |
257 | if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))) | |
258 | != GET_MODE_SIZE (TYPE_MODE (type))) | |
259 | /* There is supposed to be some integral type | |
260 | that is the same width as a pointer. */ | |
261 | abort (); | |
262 | return convert_to_pointer (type, expr); | |
263 | } | |
264 | ||
905d4035 | 265 | cp_error ("cannot convert `%E' from type `%T' to type `%T'", |
471086d6 | 266 | expr, intype, type); |
267 | return error_mark_node; | |
268 | } | |
269 | ||
270 | /* Like convert, except permit conversions to take place which | |
271 | are not normally allowed due to access restrictions | |
272 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 273 | |
471086d6 | 274 | static tree |
275 | convert_to_pointer_force (type, expr) | |
276 | tree type, expr; | |
277 | { | |
278 | register tree intype = TREE_TYPE (expr); | |
279 | register enum tree_code form = TREE_CODE (intype); | |
280 | ||
281 | if (integer_zerop (expr)) | |
282 | { | |
471086d6 | 283 | expr = build_int_2 (0, 0); |
284 | TREE_TYPE (expr) = type; | |
285 | return expr; | |
286 | } | |
287 | ||
288 | /* Convert signature pointer/reference to `void *' first. */ | |
289 | if (form == RECORD_TYPE | |
290 | && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype))) | |
291 | { | |
292 | expr = build_optr_ref (expr); | |
293 | intype = TREE_TYPE (expr); | |
294 | form = TREE_CODE (intype); | |
295 | } | |
296 | ||
297 | if (form == POINTER_TYPE) | |
298 | { | |
299 | intype = TYPE_MAIN_VARIANT (intype); | |
300 | ||
301 | if (TYPE_MAIN_VARIANT (type) != intype | |
302 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
bccffb3a | 303 | && IS_AGGR_TYPE (TREE_TYPE (type)) |
304 | && IS_AGGR_TYPE (TREE_TYPE (intype)) | |
471086d6 | 305 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) |
306 | { | |
307 | enum tree_code code = PLUS_EXPR; | |
308 | tree path; | |
309 | int distance = get_base_distance (TREE_TYPE (type), | |
310 | TREE_TYPE (intype), 0, &path); | |
311 | if (distance == -2) | |
312 | { | |
313 | ambig: | |
905d4035 | 314 | cp_error ("type `%T' is ambiguous baseclass of `%s'", |
b0df6589 | 315 | TREE_TYPE (type), |
316 | TYPE_NAME_STRING (TREE_TYPE (intype))); | |
471086d6 | 317 | return error_mark_node; |
318 | } | |
319 | if (distance == -1) | |
320 | { | |
321 | distance = get_base_distance (TREE_TYPE (intype), | |
322 | TREE_TYPE (type), 0, &path); | |
323 | if (distance == -2) | |
324 | goto ambig; | |
325 | if (distance < 0) | |
326 | /* Doesn't need any special help from us. */ | |
327 | return build1 (NOP_EXPR, type, expr); | |
328 | ||
329 | code = MINUS_EXPR; | |
330 | } | |
331 | return build_vbase_path (code, type, expr, path, 0); | |
332 | } | |
471086d6 | 333 | } |
334 | ||
335 | return cp_convert_to_pointer (type, expr); | |
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. | |
c76251c1 | 343 | DIRECT_BIND in FLAGS controls how any temporaries are generated. */ |
96624a9e | 344 | |
471086d6 | 345 | static tree |
6a44e72e | 346 | build_up_reference (type, arg, flags) |
471086d6 | 347 | tree type, arg; |
6a44e72e | 348 | int flags; |
471086d6 | 349 | { |
c76251c1 | 350 | tree rval; |
0543e7a9 | 351 | tree argtype = TREE_TYPE (arg); |
471086d6 | 352 | tree target_type = TREE_TYPE (type); |
471086d6 | 353 | |
354 | my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187); | |
471086d6 | 355 | |
c76251c1 | 356 | if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg)) |
471086d6 | 357 | { |
c76251c1 | 358 | tree targ = arg; |
38281c46 | 359 | if (toplevel_bindings_p ()) |
c76251c1 | 360 | arg = get_temp_name (argtype, 1); |
471086d6 | 361 | else |
362 | { | |
38281c46 | 363 | arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype)); |
364 | DECL_ARTIFICIAL (arg) = 1; | |
1ad432f2 | 365 | } |
38281c46 | 366 | DECL_INITIAL (arg) = targ; |
367 | cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING); | |
1ad432f2 | 368 | } |
c76251c1 | 369 | else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg)) |
1ad432f2 | 370 | { |
38281c46 | 371 | tree slot = build_decl (VAR_DECL, NULL_TREE, argtype); |
372 | arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE); | |
0f76a66d | 373 | TREE_SIDE_EFFECTS (arg) = 1; |
471086d6 | 374 | } |
1ad432f2 | 375 | |
b0df6589 | 376 | /* If we had a way to wrap this up, and say, if we ever needed it's |
377 | address, transform all occurrences of the register, into a memory | |
378 | reference we could win better. */ | |
c76251c1 | 379 | rval = build_unary_op (ADDR_EXPR, arg, 1); |
6686e84d | 380 | if ((flags & LOOKUP_PROTECT) |
381 | && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type) | |
382 | && IS_AGGR_TYPE (argtype) | |
383 | && IS_AGGR_TYPE (target_type)) | |
384 | { | |
385 | /* We go through get_binfo for the access control. */ | |
386 | tree binfo = get_binfo (target_type, argtype, 1); | |
387 | if (binfo == error_mark_node) | |
388 | return error_mark_node; | |
389 | if (binfo == NULL_TREE) | |
390 | return error_not_base_type (target_type, argtype); | |
391 | rval = convert_pointer_to_real (binfo, rval); | |
392 | } | |
c76251c1 | 393 | else |
394 | rval | |
395 | = convert_to_pointer_force (build_pointer_type (target_type), rval); | |
6686e84d | 396 | rval = build1 (NOP_EXPR, type, rval); |
c76251c1 | 397 | TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0)); |
471086d6 | 398 | return rval; |
399 | } | |
400 | ||
401 | /* For C++: Only need to do one-level references, but cannot | |
402 | get tripped up on signed/unsigned differences. | |
403 | ||
d81e00a4 | 404 | DECL is either NULL_TREE or the _DECL node for a reference that is being |
405 | initialized. It can be error_mark_node if we don't know the _DECL but | |
406 | we know it's an initialization. */ | |
471086d6 | 407 | |
471086d6 | 408 | tree |
d81e00a4 | 409 | convert_to_reference (reftype, expr, convtype, flags, decl) |
471086d6 | 410 | tree reftype, expr; |
d81e00a4 | 411 | int convtype, flags; |
412 | tree decl; | |
471086d6 | 413 | { |
414 | register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); | |
415 | register tree intype = TREE_TYPE (expr); | |
471086d6 | 416 | tree rval = NULL_TREE; |
1a3f833b | 417 | tree rval_as_conversion = NULL_TREE; |
418 | int i; | |
419 | ||
420 | if (TREE_CODE (intype) == REFERENCE_TYPE) | |
421 | my_friendly_abort (364); | |
471086d6 | 422 | |
471086d6 | 423 | intype = TYPE_MAIN_VARIANT (intype); |
424 | ||
1a3f833b | 425 | i = comp_target_types (type, intype, 0); |
426 | ||
427 | if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype) | |
428 | && ! (flags & LOOKUP_NO_CONVERSION)) | |
429 | { | |
430 | /* Look for a user-defined conversion to lvalue that we can use. */ | |
431 | ||
0a4be248 | 432 | rval_as_conversion |
433 | = build_type_conversion (CONVERT_EXPR, reftype, expr, 1); | |
1a3f833b | 434 | |
435 | if (rval_as_conversion && rval_as_conversion != error_mark_node | |
436 | && real_lvalue_p (rval_as_conversion)) | |
437 | { | |
438 | expr = rval_as_conversion; | |
439 | rval_as_conversion = NULL_TREE; | |
440 | intype = type; | |
441 | i = 1; | |
442 | } | |
443 | } | |
444 | ||
445 | if (((convtype & CONV_STATIC) && i == -1) | |
446 | || ((convtype & CONV_IMPLICIT) && i == 1)) | |
471086d6 | 447 | { |
471086d6 | 448 | if (flags & LOOKUP_COMPLAIN) |
449 | { | |
bc3887cf | 450 | tree ttl = TREE_TYPE (reftype); |
cb0ba4ec | 451 | tree ttr = lvalue_type (expr); |
471086d6 | 452 | |
f9670f72 | 453 | if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl)) |
bc3887cf | 454 | { |
d81e00a4 | 455 | if (decl) |
456 | /* Ensure semantics of [dcl.init.ref] */ | |
905d4035 | 457 | cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'", |
d81e00a4 | 458 | reftype, intype); |
459 | else | |
905d4035 | 460 | cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'", |
d81e00a4 | 461 | reftype, intype); |
bc3887cf | 462 | } |
d81e00a4 | 463 | else if (! (convtype & CONV_CONST)) |
471086d6 | 464 | { |
d81e00a4 | 465 | if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) |
905d4035 | 466 | cp_pedwarn ("conversion from `%T' to `%T' discards const", |
ad91f3ed | 467 | ttr, reftype); |
d81e00a4 | 468 | else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) |
905d4035 | 469 | cp_pedwarn ("conversion from `%T' to `%T' discards volatile", |
ad91f3ed | 470 | ttr, reftype); |
471086d6 | 471 | } |
0543e7a9 | 472 | } |
473 | ||
6a44e72e | 474 | return build_up_reference (reftype, expr, flags); |
471086d6 | 475 | } |
c07b1ad1 | 476 | else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) |
0543e7a9 | 477 | { |
478 | /* When casting an lvalue to a reference type, just convert into | |
479 | a pointer to the new type and deference it. This is allowed | |
e581f478 | 480 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
0543e7a9 | 481 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
e581f478 | 482 | |
3748625f | 483 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
e581f478 | 484 | meant. */ |
1a3f833b | 485 | if (TREE_CODE (intype) == POINTER_TYPE |
d81e00a4 | 486 | && (comptypes (TREE_TYPE (intype), type, -1))) |
905d4035 | 487 | cp_warning ("casting `%T' to `%T' does not dereference pointer", |
e581f478 | 488 | intype, reftype); |
489 | ||
0543e7a9 | 490 | rval = build_unary_op (ADDR_EXPR, expr, 0); |
491 | if (rval != error_mark_node) | |
985e9ee0 | 492 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), |
493 | rval, 0); | |
0543e7a9 | 494 | if (rval != error_mark_node) |
b0722fac | 495 | rval = build1 (NOP_EXPR, reftype, rval); |
0543e7a9 | 496 | } |
0a4be248 | 497 | else |
860740a7 | 498 | { |
499 | rval = convert_for_initialization (NULL_TREE, type, expr, flags, | |
500 | "converting", 0, 0); | |
501 | if (rval == error_mark_node) | |
502 | return error_mark_node; | |
6a44e72e | 503 | rval = build_up_reference (reftype, rval, flags); |
860740a7 | 504 | |
505 | if (rval && ! TYPE_READONLY (TREE_TYPE (reftype))) | |
905d4035 | 506 | cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary", |
860740a7 | 507 | reftype, intype); |
508 | } | |
471086d6 | 509 | |
510 | if (rval) | |
511 | { | |
96624a9e | 512 | /* If we found a way to convert earlier, then use it. */ |
471086d6 | 513 | return rval; |
514 | } | |
515 | ||
1a3f833b | 516 | my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189); |
471086d6 | 517 | |
b248d3f7 | 518 | if (flags & LOOKUP_COMPLAIN) |
905d4035 | 519 | cp_error ("cannot convert type `%T' to type `%T'", intype, reftype); |
b248d3f7 | 520 | |
471086d6 | 521 | if (flags & LOOKUP_SPECULATIVELY) |
522 | return NULL_TREE; | |
523 | ||
524 | return error_mark_node; | |
525 | } | |
526 | ||
527 | /* We are using a reference VAL for its value. Bash that reference all the | |
96624a9e | 528 | way down to its lowest form. */ |
529 | ||
471086d6 | 530 | tree |
531 | convert_from_reference (val) | |
532 | tree val; | |
533 | { | |
534 | tree type = TREE_TYPE (val); | |
535 | ||
536 | if (TREE_CODE (type) == OFFSET_TYPE) | |
537 | type = TREE_TYPE (type); | |
8c5c575d | 538 | if (TREE_CODE (type) == REFERENCE_TYPE) |
539 | return build_indirect_ref (val, NULL_PTR); | |
471086d6 | 540 | return val; |
541 | } | |
542 | \f | |
471086d6 | 543 | /* Call this when we know (for any reason) that expr is not, in fact, |
544 | zero. This routine is like convert_pointer_to, but it pays | |
545 | attention to which specific instance of what type we want to | |
546 | convert to. This routine should eventually become | |
547 | convert_to_pointer after all references to convert_to_pointer | |
548 | are removed. */ | |
96624a9e | 549 | |
471086d6 | 550 | tree |
551 | convert_pointer_to_real (binfo, expr) | |
552 | tree binfo, expr; | |
553 | { | |
554 | register tree intype = TREE_TYPE (expr); | |
555 | tree ptr_type; | |
556 | tree type, rval; | |
557 | ||
00d2b1b9 | 558 | if (intype == error_mark_node) |
559 | return error_mark_node; | |
560 | ||
471086d6 | 561 | if (TREE_CODE (binfo) == TREE_VEC) |
562 | type = BINFO_TYPE (binfo); | |
563 | else if (IS_AGGR_TYPE (binfo)) | |
564 | { | |
565 | type = binfo; | |
566 | } | |
567 | else | |
568 | { | |
569 | type = binfo; | |
570 | binfo = NULL_TREE; | |
571 | } | |
572 | ||
d2a15a12 | 573 | ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)), |
574 | TYPE_VOLATILE (TREE_TYPE (intype))); | |
575 | ptr_type = build_pointer_type (ptr_type); | |
471086d6 | 576 | if (ptr_type == TYPE_MAIN_VARIANT (intype)) |
577 | return expr; | |
578 | ||
471086d6 | 579 | my_friendly_assert (!integer_zerop (expr), 191); |
580 | ||
0df40fee | 581 | intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype)); |
471086d6 | 582 | if (TREE_CODE (type) == RECORD_TYPE |
0df40fee | 583 | && TREE_CODE (intype) == RECORD_TYPE |
584 | && type != intype) | |
471086d6 | 585 | { |
586 | tree path; | |
587 | int distance | |
0df40fee | 588 | = get_base_distance (binfo, intype, 0, &path); |
471086d6 | 589 | |
590 | /* This function shouldn't be called with unqualified arguments | |
591 | but if it is, give them an error message that they can read. */ | |
592 | if (distance < 0) | |
593 | { | |
905d4035 | 594 | cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'", |
0df40fee | 595 | intype, type); |
471086d6 | 596 | |
597 | if (distance == -2) | |
905d4035 | 598 | cp_error ("because `%T' is an ambiguous base class", type); |
471086d6 | 599 | return error_mark_node; |
600 | } | |
601 | ||
602 | return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1); | |
603 | } | |
604 | rval = build1 (NOP_EXPR, ptr_type, | |
605 | TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr); | |
606 | TREE_CONSTANT (rval) = TREE_CONSTANT (expr); | |
607 | return rval; | |
608 | } | |
609 | ||
610 | /* Call this when we know (for any reason) that expr is | |
611 | not, in fact, zero. This routine gets a type out of the first | |
612 | argument and uses it to search for the type to convert to. If there | |
613 | is more than one instance of that type in the expr, the conversion is | |
614 | ambiguous. This routine should eventually go away, and all | |
615 | callers should use convert_to_pointer_real. */ | |
96624a9e | 616 | |
471086d6 | 617 | tree |
618 | convert_pointer_to (binfo, expr) | |
619 | tree binfo, expr; | |
620 | { | |
621 | tree type; | |
622 | ||
623 | if (TREE_CODE (binfo) == TREE_VEC) | |
624 | type = BINFO_TYPE (binfo); | |
625 | else if (IS_AGGR_TYPE (binfo)) | |
626 | type = binfo; | |
627 | else | |
628 | type = binfo; | |
629 | return convert_pointer_to_real (type, expr); | |
630 | } | |
471086d6 | 631 | \f |
c4a8ac95 | 632 | /* C++ conversions, preference to static cast conversions. */ |
633 | ||
634 | tree | |
635 | cp_convert (type, expr) | |
636 | tree type, expr; | |
637 | { | |
638 | return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL); | |
639 | } | |
640 | ||
b248d3f7 | 641 | /* Conversion... |
642 | ||
643 | FLAGS indicates how we should behave. */ | |
644 | ||
471086d6 | 645 | tree |
c4a8ac95 | 646 | ocp_convert (type, expr, convtype, flags) |
471086d6 | 647 | tree type, expr; |
d81e00a4 | 648 | int convtype, flags; |
471086d6 | 649 | { |
650 | register tree e = expr; | |
651 | register enum tree_code code = TREE_CODE (type); | |
652 | ||
b465397d | 653 | if (e == error_mark_node |
654 | || TREE_TYPE (e) == error_mark_node) | |
471086d6 | 655 | return error_mark_node; |
d81e00a4 | 656 | |
7745052b | 657 | if (TREE_READONLY_DECL_P (e)) |
658 | e = decl_constant_value (e); | |
659 | ||
2133b374 | 660 | if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP) |
661 | /* Some internal structures (vtable_entry_type, sigtbl_ptr_type) | |
662 | don't go through finish_struct, so they don't have the synthesized | |
663 | constructors. So don't force a temporary. */ | |
664 | && TYPE_HAS_CONSTRUCTOR (type)) | |
1a3f833b | 665 | /* We need a new temporary; don't take this shortcut. */; |
666 | else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e))) | |
667 | /* Trivial conversion: cv-qualifiers do not matter on rvalues. */ | |
617abf06 | 668 | return fold (build1 (NOP_EXPR, type, e)); |
d81e00a4 | 669 | |
670 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) | |
671 | return build1 (CONVERT_EXPR, type, e); | |
672 | ||
471086d6 | 673 | #if 0 |
674 | /* This is incorrect. A truncation can't be stripped this way. | |
675 | Extensions will be stripped by the use of get_unwidened. */ | |
617abf06 | 676 | if (TREE_CODE (e) == NOP_EXPR) |
c4a8ac95 | 677 | return cp_convert (type, TREE_OPERAND (e, 0)); |
471086d6 | 678 | #endif |
679 | ||
680 | /* Just convert to the type of the member. */ | |
681 | if (code == OFFSET_TYPE) | |
682 | { | |
683 | type = TREE_TYPE (type); | |
684 | code = TREE_CODE (type); | |
685 | } | |
686 | ||
1a3f833b | 687 | #if 0 |
471086d6 | 688 | if (code == REFERENCE_TYPE) |
d81e00a4 | 689 | return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE)); |
471086d6 | 690 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) |
691 | e = convert_from_reference (e); | |
1a3f833b | 692 | #endif |
471086d6 | 693 | |
f3ba5c6a | 694 | if (TREE_CODE (e) == OFFSET_REF) |
695 | e = resolve_offset_ref (e); | |
696 | ||
bb0726a1 | 697 | if (INTEGRAL_CODE_P (code)) |
471086d6 | 698 | { |
617abf06 | 699 | tree intype = TREE_TYPE (e); |
02d7f858 | 700 | /* enum = enum, enum = int, enum = float, (enum)pointer are all |
701 | errors. */ | |
723d9ad3 | 702 | if (TREE_CODE (type) == ENUMERAL_TYPE |
02d7f858 | 703 | && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC)) |
704 | || (TREE_CODE (intype) == POINTER_TYPE))) | |
471086d6 | 705 | { |
905d4035 | 706 | cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type); |
471086d6 | 707 | |
708 | if (flag_pedantic_errors) | |
709 | return error_mark_node; | |
710 | } | |
f3ba5c6a | 711 | if (IS_AGGR_TYPE (intype)) |
471086d6 | 712 | { |
713 | tree rval; | |
617abf06 | 714 | rval = build_type_conversion (CONVERT_EXPR, type, e, 1); |
6495357a | 715 | if (rval) |
716 | return rval; | |
b248d3f7 | 717 | if (flags & LOOKUP_COMPLAIN) |
905d4035 | 718 | cp_error ("`%#T' used where a `%T' was expected", intype, type); |
b248d3f7 | 719 | if (flags & LOOKUP_SPECULATIVELY) |
720 | return NULL_TREE; | |
471086d6 | 721 | return error_mark_node; |
722 | } | |
bb0726a1 | 723 | if (code == BOOLEAN_TYPE) |
e4ce2dc4 | 724 | { |
725 | /* Common Ada/Pascal programmer's mistake. We always warn | |
726 | about this since it is so bad. */ | |
727 | if (TREE_CODE (expr) == FUNCTION_DECL) | |
905d4035 | 728 | cp_warning ("the address of `%D', will always be `true'", expr); |
e4ce2dc4 | 729 | return truthvalue_conversion (e); |
730 | } | |
471086d6 | 731 | return fold (convert_to_integer (type, e)); |
732 | } | |
74002e1d | 733 | if (code == POINTER_TYPE || code == REFERENCE_TYPE |
734 | || TYPE_PTRMEMFUNC_P (type)) | |
471086d6 | 735 | return fold (cp_convert_to_pointer (type, e)); |
c4a8ac95 | 736 | if (code == REAL_TYPE || code == COMPLEX_TYPE) |
471086d6 | 737 | { |
738 | if (IS_AGGR_TYPE (TREE_TYPE (e))) | |
739 | { | |
740 | tree rval; | |
741 | rval = build_type_conversion (CONVERT_EXPR, type, e, 1); | |
742 | if (rval) | |
743 | return rval; | |
744 | else | |
b248d3f7 | 745 | if (flags & LOOKUP_COMPLAIN) |
905d4035 | 746 | cp_error ("`%#T' used where a floating point value was expected", |
b248d3f7 | 747 | TREE_TYPE (e)); |
471086d6 | 748 | } |
c4a8ac95 | 749 | if (code == REAL_TYPE) |
750 | return fold (convert_to_real (type, e)); | |
751 | else if (code == COMPLEX_TYPE) | |
752 | return fold (convert_to_complex (type, e)); | |
471086d6 | 753 | } |
754 | ||
755 | /* New C++ semantics: since assignment is now based on | |
756 | memberwise copying, if the rhs type is derived from the | |
757 | lhs type, then we may still do a conversion. */ | |
758 | if (IS_AGGR_TYPE_CODE (code)) | |
759 | { | |
760 | tree dtype = TREE_TYPE (e); | |
c25194fd | 761 | tree ctor = NULL_TREE; |
471086d6 | 762 | |
471086d6 | 763 | dtype = TYPE_MAIN_VARIANT (dtype); |
764 | ||
765 | /* Conversion of object pointers or signature pointers/references | |
766 | to signature pointers/references. */ | |
767 | ||
768 | if (TYPE_LANG_SPECIFIC (type) | |
769 | && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))) | |
770 | { | |
771 | tree constructor = build_signature_pointer_constructor (type, expr); | |
772 | tree sig_ty = SIGNATURE_TYPE (type); | |
773 | tree sig_ptr; | |
774 | ||
775 | if (constructor == error_mark_node) | |
776 | return error_mark_node; | |
777 | ||
778 | sig_ptr = get_temp_name (type, 1); | |
779 | DECL_INITIAL (sig_ptr) = constructor; | |
780 | CLEAR_SIGNATURE (sig_ty); | |
6f553a8d | 781 | cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0); |
471086d6 | 782 | SET_SIGNATURE (sig_ty); |
783 | TREE_READONLY (sig_ptr) = 1; | |
784 | ||
785 | return sig_ptr; | |
786 | } | |
787 | ||
788 | /* Conversion between aggregate types. New C++ semantics allow | |
789 | objects of derived type to be cast to objects of base type. | |
790 | Old semantics only allowed this between pointers. | |
791 | ||
792 | There may be some ambiguity between using a constructor | |
793 | vs. using a type conversion operator when both apply. */ | |
794 | ||
0a4be248 | 795 | ctor = e; |
860740a7 | 796 | |
0a4be248 | 797 | if ((flags & LOOKUP_ONLYCONVERTING) |
798 | && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype))) | |
fce73460 | 799 | /* For copy-initialization, first we create a temp of the proper type |
800 | with a user-defined conversion sequence, then we direct-initialize | |
801 | the target with the temp (see [dcl.init]). */ | |
802 | ctor = build_user_type_conversion (type, ctor, flags); | |
0a4be248 | 803 | if (ctor) |
804 | ctor = build_method_call (NULL_TREE, ctor_identifier, | |
805 | build_expr_list (NULL_TREE, ctor), | |
806 | TYPE_BINFO (type), flags); | |
807 | if (ctor) | |
808 | return build_cplus_new (type, ctor); | |
471086d6 | 809 | } |
810 | ||
617abf06 | 811 | /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack, |
7745052b | 812 | then it won't be hashed and hence compare as not equal, |
471086d6 | 813 | even when it is. */ |
814 | if (code == ARRAY_TYPE | |
617abf06 | 815 | && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type) |
816 | && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type))) | |
817 | return e; | |
471086d6 | 818 | |
b248d3f7 | 819 | if (flags & LOOKUP_COMPLAIN) |
905d4035 | 820 | cp_error ("conversion from `%T' to non-scalar type `%T' requested", |
b248d3f7 | 821 | TREE_TYPE (expr), type); |
822 | if (flags & LOOKUP_SPECULATIVELY) | |
823 | return NULL_TREE; | |
471086d6 | 824 | return error_mark_node; |
825 | } | |
826 | ||
d81e00a4 | 827 | /* Create an expression whose value is that of EXPR, |
828 | converted to type TYPE. The TREE_TYPE of the value | |
829 | is always TYPE. This function implements all reasonable | |
830 | conversions; callers should filter out those that are | |
c4a8ac95 | 831 | not permitted by the language being compiled. |
832 | ||
833 | Most of this routine is from build_reinterpret_cast. | |
834 | ||
835 | The backend cannot call cp_convert (what was convert) because | |
836 | conversions to/from basetypes may involve memory references | |
837 | (vbases) and adding or subtracting small values (multiple | |
838 | inheritance), but it calls convert from the constant folding code | |
839 | on subtrees of already build trees after it has ripped them apart. | |
840 | ||
841 | Also, if we ever support range variables, we'll probably also have to | |
842 | do a little bit more work. */ | |
d81e00a4 | 843 | |
844 | tree | |
845 | convert (type, expr) | |
846 | tree type, expr; | |
847 | { | |
c4a8ac95 | 848 | tree intype; |
849 | ||
850 | if (type == error_mark_node || expr == error_mark_node) | |
851 | return error_mark_node; | |
852 | ||
c4a8ac95 | 853 | intype = TREE_TYPE (expr); |
854 | ||
6686e84d | 855 | if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype)) |
c4a8ac95 | 856 | { |
857 | if (TREE_READONLY_DECL_P (expr)) | |
858 | expr = decl_constant_value (expr); | |
859 | return fold (build1 (NOP_EXPR, type, expr)); | |
860 | } | |
861 | ||
862 | return ocp_convert (type, expr, CONV_OLD_CONVERT, | |
863 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); | |
d81e00a4 | 864 | } |
865 | ||
c4a8ac95 | 866 | /* Like cp_convert, except permit conversions to take place which |
471086d6 | 867 | are not normally allowed due to access restrictions |
868 | (such as conversion from sub-type to private super-type). */ | |
96624a9e | 869 | |
471086d6 | 870 | tree |
a74e8896 | 871 | convert_force (type, expr, convtype) |
471086d6 | 872 | tree type; |
873 | tree expr; | |
a74e8896 | 874 | int convtype; |
471086d6 | 875 | { |
876 | register tree e = expr; | |
877 | register enum tree_code code = TREE_CODE (type); | |
878 | ||
879 | if (code == REFERENCE_TYPE) | |
d81e00a4 | 880 | return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN, |
881 | NULL_TREE)); | |
471086d6 | 882 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) |
883 | e = convert_from_reference (e); | |
884 | ||
885 | if (code == POINTER_TYPE) | |
886 | return fold (convert_to_pointer_force (type, e)); | |
887 | ||
ac9386a0 | 888 | /* From typeck.c convert_for_assignment */ |
471086d6 | 889 | if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR |
890 | && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE | |
891 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) | |
ac9386a0 | 892 | || integer_zerop (e) |
893 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
471086d6 | 894 | && TYPE_PTRMEMFUNC_P (type)) |
895 | { | |
96624a9e | 896 | /* compatible pointer to member functions. */ |
ac9386a0 | 897 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1); |
471086d6 | 898 | } |
a74e8896 | 899 | |
c4a8ac95 | 900 | return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL); |
471086d6 | 901 | } |
902 | ||
471086d6 | 903 | /* Convert an aggregate EXPR to type XTYPE. If a conversion |
904 | exists, return the attempted conversion. This may | |
905 | return ERROR_MARK_NODE if the conversion is not | |
906 | allowed (references private members, etc). | |
907 | If no conversion exists, NULL_TREE is returned. | |
908 | ||
909 | If (FOR_SURE & 1) is non-zero, then we allow this type conversion | |
910 | to take place immediately. Otherwise, we build a SAVE_EXPR | |
ce28ee2e | 911 | which can be evaluated if the results are ever needed. |
912 | ||
913 | Changes to this functions should be mirrored in user_harshness. | |
914 | ||
915 | FIXME: Ambiguity checking is wrong. Should choose one by the implicit | |
916 | object parameter, or by the second standard conversion sequence if | |
917 | that doesn't do it. This will probably wait for an overloading rewrite. | |
918 | (jason 8/9/95) */ | |
471086d6 | 919 | |
471086d6 | 920 | tree |
921 | build_type_conversion (code, xtype, expr, for_sure) | |
6a44e72e | 922 | enum tree_code code ATTRIBUTE_UNUSED; |
471086d6 | 923 | tree xtype, expr; |
924 | int for_sure; | |
925 | { | |
926 | /* C++: check to see if we can convert this aggregate type | |
bcf789d7 | 927 | into the required type. */ |
0a4be248 | 928 | return build_user_type_conversion |
929 | (xtype, expr, for_sure ? LOOKUP_NORMAL : 0); | |
471086d6 | 930 | } |
931 | ||
1e66592c | 932 | /* Convert the given EXPR to one of a group of types suitable for use in an |
933 | expression. DESIRES is a combination of various WANT_* flags (q.v.) | |
934 | which indicates which types are suitable. If COMPLAIN is 1, complain | |
935 | about ambiguity; otherwise, the caller will deal with it. */ | |
471086d6 | 936 | |
1e66592c | 937 | tree |
938 | build_expr_type_conversion (desires, expr, complain) | |
939 | int desires; | |
940 | tree expr; | |
941 | int complain; | |
471086d6 | 942 | { |
1e66592c | 943 | tree basetype = TREE_TYPE (expr); |
6a44e72e | 944 | tree conv = NULL_TREE; |
bdd152ce | 945 | tree winner = NULL_TREE; |
471086d6 | 946 | |
954885ed | 947 | if (expr == null_node |
948 | && (desires & WANT_INT) | |
949 | && !(desires & WANT_NULL)) | |
905d4035 | 950 | cp_warning ("converting NULL to non-pointer type"); |
954885ed | 951 | |
1e66592c | 952 | if (TREE_CODE (basetype) == OFFSET_TYPE) |
ce28ee2e | 953 | expr = resolve_offset_ref (expr); |
954 | expr = convert_from_reference (expr); | |
955 | basetype = TREE_TYPE (expr); | |
471086d6 | 956 | |
bdd152ce | 957 | if (! IS_AGGR_TYPE (basetype)) |
958 | switch (TREE_CODE (basetype)) | |
959 | { | |
960 | case INTEGER_TYPE: | |
954885ed | 961 | if ((desires & WANT_NULL) && null_ptr_cst_p (expr)) |
bdd152ce | 962 | return expr; |
963 | /* else fall through... */ | |
964 | ||
965 | case BOOLEAN_TYPE: | |
966 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
967 | case ENUMERAL_TYPE: | |
968 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
969 | case REAL_TYPE: | |
970 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
971 | case POINTER_TYPE: | |
972 | return (desires & WANT_POINTER) ? expr : NULL_TREE; | |
985e9ee0 | 973 | |
bdd152ce | 974 | case FUNCTION_TYPE: |
975 | case ARRAY_TYPE: | |
976 | return (desires & WANT_POINTER) ? default_conversion (expr) | |
977 | : NULL_TREE; | |
978 | default: | |
979 | return NULL_TREE; | |
980 | } | |
981 | ||
982 | /* The code for conversions from class type is currently only used for | |
983 | delete expressions. Other expressions are handled by build_new_op. */ | |
984 | ||
985 | if (! TYPE_HAS_CONVERSION (basetype)) | |
986 | return NULL_TREE; | |
987 | ||
988 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) | |
989 | { | |
990 | int win = 0; | |
991 | tree candidate; | |
992 | tree cand = TREE_VALUE (conv); | |
993 | ||
994 | if (winner && winner == cand) | |
995 | continue; | |
996 | ||
997 | candidate = TREE_TYPE (TREE_TYPE (cand)); | |
998 | if (TREE_CODE (candidate) == REFERENCE_TYPE) | |
999 | candidate = TREE_TYPE (candidate); | |
1000 | ||
1001 | switch (TREE_CODE (candidate)) | |
1002 | { | |
1003 | case BOOLEAN_TYPE: | |
1004 | case INTEGER_TYPE: | |
1005 | win = (desires & WANT_INT); break; | |
1006 | case ENUMERAL_TYPE: | |
1007 | win = (desires & WANT_ENUM); break; | |
1008 | case REAL_TYPE: | |
1009 | win = (desires & WANT_FLOAT); break; | |
1010 | case POINTER_TYPE: | |
1011 | win = (desires & WANT_POINTER); break; | |
1012 | ||
1013 | default: | |
1014 | break; | |
1015 | } | |
1016 | ||
1017 | if (win) | |
1018 | { | |
1019 | if (winner) | |
1020 | { | |
1021 | if (complain) | |
1022 | { | |
905d4035 | 1023 | cp_error ("ambiguous default type conversion from `%T'", |
bdd152ce | 1024 | basetype); |
905d4035 | 1025 | cp_error (" candidate conversions include `%D' and `%D'", |
bdd152ce | 1026 | winner, cand); |
1027 | } | |
1028 | return error_mark_node; | |
1029 | } | |
1030 | else | |
1031 | winner = cand; | |
1032 | } | |
1033 | } | |
1e66592c | 1034 | |
bdd152ce | 1035 | if (winner) |
1036 | { | |
1037 | tree type = TREE_TYPE (TREE_TYPE (winner)); | |
1038 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
1039 | type = TREE_TYPE (type); | |
1040 | return build_user_type_conversion (type, expr, LOOKUP_NORMAL); | |
471086d6 | 1041 | } |
1e66592c | 1042 | |
985e9ee0 | 1043 | return NULL_TREE; |
471086d6 | 1044 | } |
bc3887cf | 1045 | |
96624a9e | 1046 | /* Implements integral promotion (4.1) and float->double promotion. */ |
1047 | ||
bc3887cf | 1048 | tree |
1049 | type_promotes_to (type) | |
1050 | tree type; | |
1051 | { | |
ce28ee2e | 1052 | int constp, volatilep; |
1053 | ||
1054 | if (type == error_mark_node) | |
1055 | return error_mark_node; | |
1056 | ||
1057 | constp = TYPE_READONLY (type); | |
1058 | volatilep = TYPE_VOLATILE (type); | |
bc3887cf | 1059 | type = TYPE_MAIN_VARIANT (type); |
bb0726a1 | 1060 | |
1061 | /* bool always promotes to int (not unsigned), even if it's the same | |
1062 | size. */ | |
028ea8b4 | 1063 | if (type == boolean_type_node) |
bb0726a1 | 1064 | type = integer_type_node; |
1065 | ||
1066 | /* Normally convert enums to int, but convert wide enums to something | |
1067 | wider. */ | |
1068 | else if (TREE_CODE (type) == ENUMERAL_TYPE | |
1069 | || type == wchar_type_node) | |
6c9497b1 | 1070 | { |
1071 | int precision = MAX (TYPE_PRECISION (type), | |
1072 | TYPE_PRECISION (integer_type_node)); | |
1073 | tree totype = type_for_size (precision, 0); | |
1074 | if (TREE_UNSIGNED (type) | |
1075 | && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) | |
1076 | type = type_for_size (precision, 1); | |
1077 | else | |
1078 | type = totype; | |
1079 | } | |
bc3887cf | 1080 | else if (C_PROMOTING_INTEGER_TYPE_P (type)) |
1081 | { | |
d0acef9e | 1082 | /* Retain unsignedness if really not getting bigger. */ |
bc3887cf | 1083 | if (TREE_UNSIGNED (type) |
d0acef9e | 1084 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
bc3887cf | 1085 | type = unsigned_type_node; |
1086 | else | |
1087 | type = integer_type_node; | |
1088 | } | |
1089 | else if (type == float_type_node) | |
1090 | type = double_type_node; | |
1091 | ||
c38086bd | 1092 | return cp_build_type_variant (type, constp, volatilep); |
bc3887cf | 1093 | } |
668ae905 | 1094 | |
668ae905 | 1095 | /* The routines below this point are carefully written to conform to |
1096 | the standard. They use the same terminology, and follow the rules | |
1097 | closely. Although they are used only in pt.c at the moment, they | |
1098 | should presumably be used everywhere in the future. */ | |
1099 | ||
1146f179 | 1100 | /* Attempt to perform qualification conversions on EXPR to convert it |
1101 | to TYPE. Return the resulting expression, or error_mark_node if | |
1102 | the conversion was impossible. */ | |
1103 | ||
668ae905 | 1104 | tree |
1105 | perform_qualification_conversions (type, expr) | |
1106 | tree type; | |
1107 | tree expr; | |
1108 | { | |
213d2076 | 1109 | if (TREE_CODE (type) == POINTER_TYPE |
1110 | && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE | |
1111 | && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr)))) | |
1146f179 | 1112 | return build1 (NOP_EXPR, type, expr); |
1113 | else | |
1114 | return error_mark_node; | |
668ae905 | 1115 | } |