]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Language-level data type conversion for GNU C++. |
59be0cdd | 2 | Copyright (C) 1987, 1988, 1992, 1993, 1995 Free Software Foundation, Inc. |
8d08fdba MS |
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 | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | ||
22 | /* This file contains the functions for converting C expressions | |
23 | to different data types. The only entry point is `convert'. | |
24 | Every language front end must have a `convert' function | |
25 | but what kind of conversions it does will depend on the language. */ | |
26 | ||
27 | #include "config.h" | |
28 | #include "tree.h" | |
29 | #include "flags.h" | |
30 | #include "cp-tree.h" | |
31 | #include "class.h" | |
32 | #include "convert.h" | |
33 | ||
34 | #undef NULL | |
35 | #define NULL (char *)0 | |
36 | ||
37 | /* Change of width--truncation and extension of integers or reals-- | |
38 | is represented with NOP_EXPR. Proper functioning of many things | |
39 | assumes that no other conversions can be NOP_EXPRs. | |
40 | ||
41 | Conversion between integer and pointer is represented with CONVERT_EXPR. | |
42 | Converting integer to real uses FLOAT_EXPR | |
43 | and real to integer uses FIX_TRUNC_EXPR. | |
44 | ||
45 | Here is a list of all the functions that assume that widening and | |
46 | narrowing is always done with a NOP_EXPR: | |
47 | In convert.c, convert_to_integer. | |
48 | In c-typeck.c, build_binary_op_nodefault (boolean ops), | |
49 | and truthvalue_conversion. | |
50 | In expr.c: expand_expr, for operands of a MULT_EXPR. | |
51 | In fold-const.c: fold. | |
52 | In tree.c: get_narrower and get_unwidened. | |
53 | ||
54 | C++: in multiple-inheritance, converting between pointers may involve | |
55 | adjusting them by a delta stored within the class definition. */ | |
56 | \f | |
57 | /* Subroutines of `convert'. */ | |
58 | ||
59 | /* Build a thunk. What it is, is an entry point that when called will | |
60 | adjust the this pointer (the first argument) by offset, and then | |
61 | goto the real address of the function given by REAL_ADDR that we | |
62 | would like called. What we return is the address of the thunk. */ | |
63 | static tree | |
64 | build_thunk (offset, real_addr) | |
65 | tree offset, real_addr; | |
66 | { | |
67 | if (TREE_CODE (real_addr) != ADDR_EXPR | |
68 | || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL) | |
69 | { | |
70 | sorry ("MI pointer to member conversion too complex"); | |
71 | return error_mark_node; | |
72 | } | |
73 | sorry ("MI pointer to member conversion too complex"); | |
74 | return error_mark_node; | |
75 | } | |
76 | ||
77 | /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into | |
78 | another `pointer to method'. This may involved the creation of | |
79 | a thunk to handle the this offset calculation. */ | |
80 | static tree | |
81 | convert_fn_ptr (type, expr) | |
82 | tree type, expr; | |
83 | { | |
84 | tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (expr))), | |
85 | TYPE_METHOD_BASETYPE (TREE_TYPE (type)), | |
86 | 1); | |
87 | if (binfo == error_mark_node) | |
88 | { | |
89 | error (" in pointer to member conversion"); | |
90 | return error_mark_node; | |
91 | } | |
92 | if (binfo == NULL_TREE) | |
93 | { | |
94 | /* ARM 4.8 restriction. */ | |
95 | error ("invalid pointer to member conversion"); | |
96 | return error_mark_node; | |
97 | } | |
98 | if (BINFO_OFFSET_ZEROP (binfo)) | |
99 | return build1 (NOP_EXPR, type, expr); | |
100 | return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr)); | |
101 | } | |
102 | ||
103 | /* if converting pointer to pointer | |
104 | if dealing with classes, check for derived->base or vice versa | |
105 | else if dealing with method pointers, delegate | |
106 | else convert blindly | |
107 | else if converting class, pass off to build_type_conversion | |
108 | else try C-style pointer conversion */ | |
109 | static tree | |
110 | cp_convert_to_pointer (type, expr) | |
111 | tree type, expr; | |
112 | { | |
113 | register tree intype = TREE_TYPE (expr); | |
114 | register enum tree_code form = TREE_CODE (intype); | |
115 | ||
8ccc31eb | 116 | if (form == POINTER_TYPE || form == REFERENCE_TYPE) |
8d08fdba MS |
117 | { |
118 | intype = TYPE_MAIN_VARIANT (intype); | |
119 | ||
120 | if (TYPE_MAIN_VARIANT (type) != intype | |
121 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
122 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) | |
123 | { | |
124 | enum tree_code code = PLUS_EXPR; | |
125 | tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1); | |
126 | if (binfo == error_mark_node) | |
127 | return error_mark_node; | |
128 | if (binfo == NULL_TREE) | |
129 | { | |
130 | binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1); | |
131 | if (binfo == error_mark_node) | |
132 | return error_mark_node; | |
133 | code = MINUS_EXPR; | |
134 | } | |
135 | if (binfo) | |
136 | { | |
137 | if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type)) | |
138 | || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype)) | |
139 | || ! BINFO_OFFSET_ZEROP (binfo)) | |
140 | { | |
141 | /* Need to get the path we took. */ | |
142 | tree path; | |
143 | ||
144 | if (code == PLUS_EXPR) | |
145 | get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path); | |
146 | else | |
147 | get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path); | |
148 | return build_vbase_path (code, type, expr, path, 0); | |
149 | } | |
150 | } | |
151 | } | |
152 | if (TYPE_MAIN_VARIANT (type) != intype | |
153 | && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE | |
154 | && TREE_CODE (type) == POINTER_TYPE | |
155 | && TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE) | |
156 | return convert_fn_ptr (type, expr); | |
157 | ||
158 | return build1 (NOP_EXPR, type, expr); | |
159 | } | |
160 | ||
161 | my_friendly_assert (form != OFFSET_TYPE, 186); | |
162 | ||
163 | if (TYPE_LANG_SPECIFIC (intype) | |
164 | && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype))) | |
165 | return convert_to_pointer (type, build_optr_ref (expr)); | |
166 | ||
167 | if (IS_AGGR_TYPE (intype)) | |
168 | { | |
169 | tree rval; | |
170 | rval = build_type_conversion (CONVERT_EXPR, type, expr, 1); | |
171 | if (rval) | |
172 | { | |
173 | if (rval == error_mark_node) | |
174 | cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous", | |
175 | expr, intype, type); | |
176 | return rval; | |
177 | } | |
178 | } | |
179 | ||
180 | if (integer_zerop (expr)) | |
181 | { | |
182 | if (type == TREE_TYPE (null_pointer_node)) | |
183 | return null_pointer_node; | |
184 | expr = build_int_2 (0, 0); | |
185 | TREE_TYPE (expr) = type; | |
186 | return expr; | |
187 | } | |
188 | ||
2986ae00 | 189 | if (INTEGRAL_CODE_P (form)) |
8d08fdba MS |
190 | { |
191 | if (type_precision (intype) == POINTER_SIZE) | |
192 | return build1 (CONVERT_EXPR, type, expr); | |
193 | expr = convert (type_for_size (POINTER_SIZE, 0), expr); | |
194 | /* Modes may be different but sizes should be the same. */ | |
195 | if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))) | |
196 | != GET_MODE_SIZE (TYPE_MODE (type))) | |
197 | /* There is supposed to be some integral type | |
198 | that is the same width as a pointer. */ | |
199 | abort (); | |
200 | return convert_to_pointer (type, expr); | |
201 | } | |
202 | ||
203 | cp_error ("cannot convert `%E' from type `%T' to type `%T'", | |
204 | expr, intype, type); | |
205 | return error_mark_node; | |
206 | } | |
207 | ||
208 | /* Like convert, except permit conversions to take place which | |
209 | are not normally allowed due to access restrictions | |
210 | (such as conversion from sub-type to private super-type). */ | |
211 | static tree | |
212 | convert_to_pointer_force (type, expr) | |
213 | tree type, expr; | |
214 | { | |
215 | register tree intype = TREE_TYPE (expr); | |
216 | register enum tree_code form = TREE_CODE (intype); | |
217 | ||
218 | if (integer_zerop (expr)) | |
219 | { | |
220 | if (type == TREE_TYPE (null_pointer_node)) | |
221 | return null_pointer_node; | |
222 | expr = build_int_2 (0, 0); | |
223 | TREE_TYPE (expr) = type; | |
224 | return expr; | |
225 | } | |
226 | ||
227 | /* Convert signature pointer/reference to `void *' first. */ | |
228 | if (form == RECORD_TYPE | |
229 | && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype))) | |
230 | { | |
231 | expr = build_optr_ref (expr); | |
232 | intype = TREE_TYPE (expr); | |
233 | form = TREE_CODE (intype); | |
234 | } | |
235 | ||
236 | if (form == POINTER_TYPE) | |
237 | { | |
238 | intype = TYPE_MAIN_VARIANT (intype); | |
239 | ||
240 | if (TYPE_MAIN_VARIANT (type) != intype | |
241 | && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE | |
242 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE) | |
243 | { | |
244 | enum tree_code code = PLUS_EXPR; | |
245 | tree path; | |
246 | int distance = get_base_distance (TREE_TYPE (type), | |
247 | TREE_TYPE (intype), 0, &path); | |
248 | if (distance == -2) | |
249 | { | |
250 | ambig: | |
251 | cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type), | |
252 | TYPE_NAME_STRING (TREE_TYPE (intype))); | |
253 | return error_mark_node; | |
254 | } | |
255 | if (distance == -1) | |
256 | { | |
257 | distance = get_base_distance (TREE_TYPE (intype), | |
258 | TREE_TYPE (type), 0, &path); | |
259 | if (distance == -2) | |
260 | goto ambig; | |
261 | if (distance < 0) | |
262 | /* Doesn't need any special help from us. */ | |
263 | return build1 (NOP_EXPR, type, expr); | |
264 | ||
265 | code = MINUS_EXPR; | |
266 | } | |
267 | return build_vbase_path (code, type, expr, path, 0); | |
268 | } | |
269 | return build1 (NOP_EXPR, type, expr); | |
270 | } | |
271 | ||
272 | return cp_convert_to_pointer (type, expr); | |
273 | } | |
274 | ||
275 | /* We are passing something to a function which requires a reference. | |
276 | The type we are interested in is in TYPE. The initial | |
277 | value we have to begin with is in ARG. | |
278 | ||
279 | FLAGS controls how we manage access checking. | |
280 | CHECKCONST controls if we report error messages on const subversion. */ | |
281 | static tree | |
282 | build_up_reference (type, arg, flags, checkconst) | |
283 | tree type, arg; | |
284 | int flags, checkconst; | |
285 | { | |
286 | tree rval, targ; | |
287 | int literal_flag = 0; | |
8926095f | 288 | tree argtype = TREE_TYPE (arg); |
8d08fdba MS |
289 | tree target_type = TREE_TYPE (type); |
290 | tree binfo = NULL_TREE; | |
291 | ||
292 | my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187); | |
8926095f | 293 | if ((flags & LOOKUP_PROTECT) |
8d08fdba MS |
294 | && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type) |
295 | && IS_AGGR_TYPE (argtype) | |
296 | && IS_AGGR_TYPE (target_type)) | |
297 | { | |
298 | binfo = get_binfo (target_type, argtype, 1); | |
8926095f | 299 | if (binfo == error_mark_node) |
8d08fdba MS |
300 | return error_mark_node; |
301 | if (binfo == NULL_TREE) | |
302 | return error_not_base_type (target_type, argtype); | |
8d08fdba MS |
303 | } |
304 | ||
305 | /* Pass along const and volatile down into the type. */ | |
306 | if (TYPE_READONLY (type) || TYPE_VOLATILE (type)) | |
f376e137 | 307 | target_type = cp_build_type_variant (target_type, TYPE_READONLY (type), |
21474714 | 308 | TYPE_VOLATILE (type)); |
8d08fdba MS |
309 | targ = arg; |
310 | if (TREE_CODE (targ) == SAVE_EXPR) | |
311 | targ = TREE_OPERAND (targ, 0); | |
6060a796 MS |
312 | while (TREE_CODE (targ) == NOP_EXPR |
313 | && (TYPE_MAIN_VARIANT (argtype) | |
314 | == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0))))) | |
315 | targ = TREE_OPERAND (targ, 0); | |
8d08fdba MS |
316 | |
317 | switch (TREE_CODE (targ)) | |
318 | { | |
319 | case INDIRECT_REF: | |
320 | /* This is a call to a constructor which did not know what it was | |
321 | initializing until now: it needs to initialize a temporary. */ | |
322 | if (TREE_HAS_CONSTRUCTOR (targ)) | |
323 | { | |
324 | tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1); | |
325 | TREE_HAS_CONSTRUCTOR (targ) = 0; | |
326 | return build_up_reference (type, temp, flags, 1); | |
327 | } | |
328 | /* Let &* cancel out to simplify resulting code. | |
329 | Also, throw away intervening NOP_EXPRs. */ | |
330 | arg = TREE_OPERAND (targ, 0); | |
331 | if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR | |
332 | || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg))) | |
333 | arg = TREE_OPERAND (arg, 0); | |
334 | ||
335 | /* in doing a &*, we have to get rid of the const'ness on the pointer | |
336 | value. Haven't thought about volatile here. Pointers come to mind | |
337 | here. */ | |
338 | if (TREE_READONLY (arg)) | |
339 | { | |
340 | arg = copy_node (arg); | |
341 | TREE_READONLY (arg) = 0; | |
342 | } | |
343 | ||
344 | rval = build1 (CONVERT_EXPR, type, arg); | |
345 | TREE_REFERENCE_EXPR (rval) = 1; | |
346 | ||
347 | /* propagate the const flag on something like: | |
348 | ||
349 | class Base { | |
350 | public: | |
351 | int foo; | |
352 | }; | |
353 | ||
354 | class Derived : public Base { | |
355 | public: | |
356 | int bar; | |
357 | }; | |
358 | ||
359 | void func(Base&); | |
360 | ||
361 | void func2(const Derived& d) { | |
362 | func(d); | |
363 | } | |
364 | ||
365 | on the d parameter. The below could have been avoided, if the flags | |
366 | were down in the tree, not sure why they are not. (mrs) */ | |
367 | /* The below code may have to be propagated to other parts of this | |
368 | switch. */ | |
369 | if (TREE_READONLY (targ) && !TREE_READONLY (arg) | |
370 | && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL) | |
371 | && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE | |
372 | && (TYPE_READONLY (target_type) && checkconst)) | |
373 | { | |
374 | arg = copy_node (arg); | |
375 | TREE_READONLY (arg) = TREE_READONLY (targ); | |
376 | } | |
377 | literal_flag = TREE_CONSTANT (arg); | |
378 | ||
a3203465 | 379 | goto done; |
8d08fdba MS |
380 | |
381 | /* Get this out of a register if we happened to be in one by accident. | |
382 | Also, build up references to non-lvalues it we must. */ | |
383 | /* For &x[y], return (&) x+y */ | |
384 | case ARRAY_REF: | |
385 | if (mark_addressable (TREE_OPERAND (targ, 0)) == 0) | |
386 | return error_mark_node; | |
387 | rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0), | |
388 | TREE_OPERAND (targ, 1), 1); | |
389 | TREE_TYPE (rval) = type; | |
390 | if (TREE_CONSTANT (TREE_OPERAND (targ, 1)) | |
391 | && staticp (TREE_OPERAND (targ, 0))) | |
392 | TREE_CONSTANT (rval) = 1; | |
393 | goto done; | |
394 | ||
395 | case SCOPE_REF: | |
396 | /* Could be a reference to a static member. */ | |
397 | { | |
398 | tree field = TREE_OPERAND (targ, 1); | |
399 | if (TREE_STATIC (field)) | |
400 | { | |
401 | rval = build1 (ADDR_EXPR, type, field); | |
402 | literal_flag = 1; | |
403 | goto done; | |
404 | } | |
405 | } | |
406 | ||
407 | /* We should have farmed out member pointers above. */ | |
408 | my_friendly_abort (188); | |
409 | ||
410 | case COMPONENT_REF: | |
411 | rval = build_component_addr (targ, build_pointer_type (argtype), | |
412 | "attempt to make a reference to bit-field structure member `%s'"); | |
413 | TREE_TYPE (rval) = type; | |
414 | literal_flag = staticp (TREE_OPERAND (targ, 0)); | |
415 | ||
a3203465 | 416 | goto done; |
8d08fdba MS |
417 | |
418 | /* Anything not already handled and not a true memory reference | |
419 | needs to have a reference built up. Do so silently for | |
420 | things like integers and return values from function, | |
421 | but complain if we need a reference to something declared | |
422 | as `register'. */ | |
423 | ||
424 | case RESULT_DECL: | |
425 | if (staticp (targ)) | |
426 | literal_flag = 1; | |
427 | TREE_ADDRESSABLE (targ) = 1; | |
428 | put_var_into_stack (targ); | |
429 | break; | |
430 | ||
431 | case PARM_DECL: | |
a292b002 | 432 | #if 0 |
8d08fdba MS |
433 | if (targ == current_class_decl) |
434 | { | |
435 | error ("address of `this' not available"); | |
a292b002 | 436 | /* #if 0 */ |
8d08fdba MS |
437 | /* This code makes the following core dump the compiler on a sun4, |
438 | if the code below is used. | |
439 | ||
440 | class e_decl; | |
441 | class a_decl; | |
442 | typedef a_decl* a_ref; | |
443 | ||
444 | class a_s { | |
445 | public: | |
446 | a_s(); | |
447 | void* append(a_ref& item); | |
448 | }; | |
449 | class a_decl { | |
450 | public: | |
451 | a_decl (e_decl *parent); | |
452 | a_s generic_s; | |
453 | a_s decls; | |
454 | e_decl* parent; | |
455 | }; | |
456 | ||
457 | class e_decl { | |
458 | public: | |
459 | e_decl(); | |
460 | a_s implementations; | |
461 | }; | |
462 | ||
463 | void foobar(void *); | |
464 | ||
465 | a_decl::a_decl(e_decl *parent) { | |
466 | parent->implementations.append(this); | |
467 | } | |
468 | */ | |
469 | ||
470 | TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */ | |
471 | put_var_into_stack (targ); | |
472 | break; | |
a292b002 | 473 | /* #else */ |
8d08fdba | 474 | return error_mark_node; |
a292b002 | 475 | /* #endif */ |
8d08fdba | 476 | } |
a292b002 | 477 | #endif |
8d08fdba MS |
478 | /* Fall through. */ |
479 | case VAR_DECL: | |
480 | case CONST_DECL: | |
a292b002 MS |
481 | if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ) |
482 | && !DECL_ARTIFICIAL (targ)) | |
483 | cp_warning ("address needed to build reference for `%D', which is declared `register'", | |
484 | targ); | |
8d08fdba MS |
485 | else if (staticp (targ)) |
486 | literal_flag = 1; | |
487 | ||
488 | TREE_ADDRESSABLE (targ) = 1; | |
489 | put_var_into_stack (targ); | |
490 | break; | |
491 | ||
492 | case COMPOUND_EXPR: | |
493 | { | |
494 | tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1), | |
495 | LOOKUP_PROTECT, checkconst); | |
496 | rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference); | |
497 | TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1)); | |
498 | return rval; | |
499 | } | |
500 | ||
f376e137 MS |
501 | case PREINCREMENT_EXPR: |
502 | case PREDECREMENT_EXPR: | |
8d08fdba MS |
503 | case MODIFY_EXPR: |
504 | case INIT_EXPR: | |
505 | { | |
506 | tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0), | |
507 | LOOKUP_PROTECT, checkconst); | |
508 | rval = build (COMPOUND_EXPR, type, arg, real_reference); | |
509 | TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0)); | |
510 | return rval; | |
511 | } | |
512 | ||
513 | case COND_EXPR: | |
514 | return build (COND_EXPR, type, | |
515 | TREE_OPERAND (targ, 0), | |
516 | build_up_reference (type, TREE_OPERAND (targ, 1), | |
517 | LOOKUP_PROTECT, checkconst), | |
518 | build_up_reference (type, TREE_OPERAND (targ, 2), | |
519 | LOOKUP_PROTECT, checkconst)); | |
520 | ||
e1cd6e56 MS |
521 | /* Undo the folding... */ |
522 | case MIN_EXPR: | |
523 | case MAX_EXPR: | |
524 | return build (COND_EXPR, type, | |
525 | build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR, | |
526 | boolean_type_node, TREE_OPERAND (targ, 0), | |
527 | TREE_OPERAND (targ, 1)), | |
528 | build_up_reference (type, TREE_OPERAND (targ, 0), | |
529 | LOOKUP_PROTECT, checkconst), | |
530 | build_up_reference (type, TREE_OPERAND (targ, 1), | |
531 | LOOKUP_PROTECT, checkconst)); | |
532 | ||
8d08fdba MS |
533 | case WITH_CLEANUP_EXPR: |
534 | return build (WITH_CLEANUP_EXPR, type, | |
535 | build_up_reference (type, TREE_OPERAND (targ, 0), | |
536 | LOOKUP_PROTECT, checkconst), | |
537 | 0, TREE_OPERAND (targ, 2)); | |
538 | ||
539 | case BIND_EXPR: | |
540 | arg = TREE_OPERAND (targ, 1); | |
541 | if (arg == NULL_TREE) | |
542 | { | |
543 | compiler_error ("({ ... }) expression not expanded when needed for reference"); | |
544 | return error_mark_node; | |
545 | } | |
546 | rval = build1 (ADDR_EXPR, type, arg); | |
547 | TREE_REFERENCE_EXPR (rval) = 1; | |
548 | return rval; | |
549 | ||
550 | default: | |
551 | break; | |
552 | } | |
553 | ||
554 | if (TREE_ADDRESSABLE (targ) == 0) | |
555 | { | |
556 | tree temp; | |
557 | ||
558 | if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype)) | |
559 | { | |
560 | temp = build_cplus_new (argtype, targ, 1); | |
a3203465 MS |
561 | if (TREE_CODE (temp) == WITH_CLEANUP_EXPR) |
562 | rval = build (WITH_CLEANUP_EXPR, type, | |
563 | build1 (ADDR_EXPR, type, TREE_OPERAND (temp, 0)), | |
564 | 0, TREE_OPERAND (temp, 2)); | |
565 | else | |
566 | rval = build1 (ADDR_EXPR, type, temp); | |
8d08fdba MS |
567 | goto done; |
568 | } | |
569 | else | |
570 | { | |
571 | temp = get_temp_name (argtype, 0); | |
572 | if (global_bindings_p ()) | |
573 | { | |
574 | /* Give this new temp some rtl and initialize it. */ | |
575 | DECL_INITIAL (temp) = targ; | |
576 | TREE_STATIC (temp) = 1; | |
6060a796 | 577 | finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING); |
8d08fdba MS |
578 | /* Do this after declaring it static. */ |
579 | rval = build_unary_op (ADDR_EXPR, temp, 0); | |
580 | TREE_TYPE (rval) = type; | |
581 | literal_flag = TREE_CONSTANT (rval); | |
582 | goto done; | |
583 | } | |
584 | else | |
585 | { | |
586 | rval = build_unary_op (ADDR_EXPR, temp, 0); | |
587 | if (binfo && !BINFO_OFFSET_ZEROP (binfo)) | |
588 | rval = convert_pointer_to (target_type, rval); | |
589 | else | |
590 | TREE_TYPE (rval) = type; | |
591 | ||
592 | temp = build (MODIFY_EXPR, argtype, temp, arg); | |
593 | TREE_SIDE_EFFECTS (temp) = 1; | |
594 | return build (COMPOUND_EXPR, type, temp, rval); | |
595 | } | |
596 | } | |
597 | } | |
598 | else | |
599 | rval = build1 (ADDR_EXPR, type, arg); | |
600 | ||
8d08fdba | 601 | done: |
21474714 MS |
602 | if (TYPE_USES_COMPLEX_INHERITANCE (argtype) |
603 | || TYPE_USES_COMPLEX_INHERITANCE (target_type)) | |
8d08fdba | 604 | { |
39211cd5 | 605 | TREE_TYPE (rval) = build_pointer_type (argtype); |
51c184be MS |
606 | if (flags & LOOKUP_PROTECT) |
607 | rval = convert_pointer_to (target_type, rval); | |
608 | else | |
609 | rval | |
610 | = convert_to_pointer_force (build_pointer_type (target_type), rval); | |
8d08fdba | 611 | TREE_TYPE (rval) = type; |
59be85d7 JM |
612 | if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR) |
613 | TREE_TYPE (TREE_OPERAND (rval, 0)) | |
614 | = TREE_TYPE (TREE_OPERAND (rval, 1)) = type; | |
8d08fdba MS |
615 | } |
616 | TREE_CONSTANT (rval) = literal_flag; | |
617 | return rval; | |
618 | } | |
619 | ||
620 | /* For C++: Only need to do one-level references, but cannot | |
621 | get tripped up on signed/unsigned differences. | |
622 | ||
a4443a08 MS |
623 | DECL is either NULL_TREE or the _DECL node for a reference that is being |
624 | initialized. It can be error_mark_node if we don't know the _DECL but | |
625 | we know it's an initialization. */ | |
8d08fdba | 626 | |
8d08fdba | 627 | tree |
a4443a08 | 628 | convert_to_reference (reftype, expr, convtype, flags, decl) |
8d08fdba | 629 | tree reftype, expr; |
a4443a08 MS |
630 | int convtype, flags; |
631 | tree decl; | |
8d08fdba MS |
632 | { |
633 | register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); | |
634 | register tree intype = TREE_TYPE (expr); | |
8d08fdba | 635 | tree rval = NULL_TREE; |
8ccc31eb MS |
636 | tree rval_as_conversion = NULL_TREE; |
637 | int i; | |
638 | ||
639 | if (TREE_CODE (intype) == REFERENCE_TYPE) | |
640 | my_friendly_abort (364); | |
8d08fdba | 641 | |
8d08fdba MS |
642 | intype = TYPE_MAIN_VARIANT (intype); |
643 | ||
8ccc31eb MS |
644 | i = comp_target_types (type, intype, 0); |
645 | ||
646 | if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype) | |
647 | && ! (flags & LOOKUP_NO_CONVERSION)) | |
648 | { | |
649 | /* Look for a user-defined conversion to lvalue that we can use. */ | |
650 | ||
651 | rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1); | |
652 | ||
653 | if (rval_as_conversion && rval_as_conversion != error_mark_node | |
654 | && real_lvalue_p (rval_as_conversion)) | |
655 | { | |
656 | expr = rval_as_conversion; | |
657 | rval_as_conversion = NULL_TREE; | |
658 | intype = type; | |
659 | i = 1; | |
660 | } | |
661 | } | |
662 | ||
663 | if (((convtype & CONV_STATIC) && i == -1) | |
664 | || ((convtype & CONV_IMPLICIT) && i == 1)) | |
8d08fdba | 665 | { |
8d08fdba MS |
666 | if (flags & LOOKUP_COMPLAIN) |
667 | { | |
39211cd5 MS |
668 | tree ttl = TREE_TYPE (reftype); |
669 | tree ttr; | |
670 | ||
8ccc31eb MS |
671 | { |
672 | int r = TREE_READONLY (expr); | |
673 | int v = TREE_THIS_VOLATILE (expr); | |
674 | ttr = cp_build_type_variant (TREE_TYPE (expr), r, v); | |
675 | } | |
8d08fdba | 676 | |
8ccc31eb | 677 | if (! real_lvalue_p (expr) && |
a4443a08 | 678 | (decl == NULL_TREE || ! TYPE_READONLY (ttl))) |
39211cd5 | 679 | { |
a4443a08 MS |
680 | if (decl) |
681 | /* Ensure semantics of [dcl.init.ref] */ | |
682 | cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'", | |
683 | reftype, intype); | |
684 | else | |
685 | cp_pedwarn ("conversion to `%T' from rvalue `%T'", | |
686 | reftype, intype); | |
39211cd5 | 687 | } |
a4443a08 | 688 | else if (! (convtype & CONV_CONST)) |
8d08fdba | 689 | { |
a4443a08 MS |
690 | if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) |
691 | cp_pedwarn ("conversion from `%T' to `%T' discards const", | |
a3203465 | 692 | ttr, reftype); |
a4443a08 MS |
693 | else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) |
694 | cp_pedwarn ("conversion from `%T' to `%T' discards volatile", | |
a3203465 | 695 | ttr, reftype); |
8d08fdba | 696 | } |
8926095f MS |
697 | } |
698 | ||
a4443a08 MS |
699 | return build_up_reference (reftype, expr, flags, |
700 | ! (convtype & CONV_CONST)); | |
8d08fdba | 701 | } |
21474714 | 702 | else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) |
8926095f MS |
703 | { |
704 | /* When casting an lvalue to a reference type, just convert into | |
705 | a pointer to the new type and deference it. This is allowed | |
a28e3c7f | 706 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
8926095f | 707 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
a28e3c7f | 708 | |
59be0cdd | 709 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
a28e3c7f | 710 | meant. */ |
8ccc31eb | 711 | if (TREE_CODE (intype) == POINTER_TYPE |
a4443a08 | 712 | && (comptypes (TREE_TYPE (intype), type, -1))) |
a28e3c7f MS |
713 | cp_warning ("casting `%T' to `%T' does not dereference pointer", |
714 | intype, reftype); | |
715 | ||
8926095f MS |
716 | rval = build_unary_op (ADDR_EXPR, expr, 0); |
717 | if (rval != error_mark_node) | |
6060a796 | 718 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0); |
8926095f | 719 | if (rval != error_mark_node) |
7177d104 | 720 | rval = build1 (NOP_EXPR, reftype, rval); |
8926095f | 721 | } |
a4443a08 | 722 | else if (decl) |
8d08fdba | 723 | { |
8d08fdba MS |
724 | tree rval_as_ctor = NULL_TREE; |
725 | ||
8ccc31eb | 726 | if (rval_as_conversion) |
8d08fdba | 727 | { |
8ccc31eb MS |
728 | if (rval_as_conversion == error_mark_node) |
729 | { | |
730 | cp_error ("conversion from `%T' to `%T' is ambiguous", | |
731 | intype, reftype); | |
732 | return error_mark_node; | |
733 | } | |
734 | rval_as_conversion = build_up_reference (reftype, rval_as_conversion, | |
735 | flags, 1); | |
8d08fdba MS |
736 | } |
737 | ||
738 | /* Definitely need to go through a constructor here. */ | |
739 | if (TYPE_HAS_CONSTRUCTOR (type) | |
a28e3c7f | 740 | && ! CLASSTYPE_ABSTRACT_VIRTUALS (type) |
8d08fdba MS |
741 | && (rval = build_method_call |
742 | (NULL_TREE, constructor_name_full (type), | |
743 | build_tree_list (NULL_TREE, expr), TYPE_BINFO (type), | |
db5ae43f MS |
744 | LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY |
745 | | LOOKUP_ONLYCONVERTING))) | |
8d08fdba MS |
746 | { |
747 | tree init; | |
748 | ||
749 | if (global_bindings_p ()) | |
750 | { | |
751 | extern tree static_aggregates; | |
a4443a08 MS |
752 | tree t = get_temp_name (type, global_bindings_p ()); |
753 | init = build_method_call (t, constructor_name_full (type), | |
8d08fdba | 754 | build_tree_list (NULL_TREE, expr), |
a4443a08 | 755 | TYPE_BINFO (type), |
db5ae43f MS |
756 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION |
757 | | LOOKUP_ONLYCONVERTING); | |
8d08fdba MS |
758 | |
759 | if (init == error_mark_node) | |
760 | return error_mark_node; | |
761 | ||
a4443a08 MS |
762 | make_decl_rtl (t, NULL_PTR, 1); |
763 | static_aggregates = perm_tree_cons (expr, t, static_aggregates); | |
764 | rval = build_unary_op (ADDR_EXPR, t, 0); | |
8d08fdba MS |
765 | } |
766 | else | |
767 | { | |
768 | init = build_method_call (NULL_TREE, constructor_name_full (type), | |
769 | build_tree_list (NULL_TREE, expr), | |
a4443a08 | 770 | TYPE_BINFO (type), |
db5ae43f MS |
771 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION |
772 | |LOOKUP_ONLYCONVERTING); | |
8d08fdba MS |
773 | |
774 | if (init == error_mark_node) | |
775 | return error_mark_node; | |
776 | ||
777 | rval = build_cplus_new (type, init, 1); | |
778 | rval = build_up_reference (reftype, rval, flags, 1); | |
779 | } | |
780 | rval_as_ctor = rval; | |
781 | } | |
782 | ||
783 | if (rval_as_ctor && rval_as_conversion) | |
784 | { | |
785 | cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply", | |
786 | intype, reftype); | |
787 | return error_mark_node; | |
788 | } | |
789 | else if (rval_as_ctor) | |
790 | rval = rval_as_ctor; | |
791 | else if (rval_as_conversion) | |
792 | rval = rval_as_conversion; | |
793 | else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype)) | |
794 | { | |
795 | rval = convert (type, expr); | |
796 | if (rval == error_mark_node) | |
797 | return error_mark_node; | |
798 | ||
799 | rval = build_up_reference (reftype, rval, flags, 1); | |
800 | } | |
801 | ||
802 | if (rval && ! TYPE_READONLY (TREE_TYPE (reftype))) | |
a4443a08 MS |
803 | cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary", |
804 | reftype, intype); | |
8d08fdba MS |
805 | } |
806 | ||
807 | if (rval) | |
808 | { | |
809 | /* If we found a way to convert earlier, then use it. */ | |
810 | return rval; | |
811 | } | |
812 | ||
8ccc31eb | 813 | my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189); |
8d08fdba | 814 | |
8d08fdba MS |
815 | if (flags & LOOKUP_SPECULATIVELY) |
816 | return NULL_TREE; | |
817 | ||
8926095f MS |
818 | else if (flags & LOOKUP_COMPLAIN) |
819 | cp_error ("cannot convert type `%T' to type `%T'", intype, reftype); | |
820 | ||
8d08fdba MS |
821 | return error_mark_node; |
822 | } | |
823 | ||
824 | /* We are using a reference VAL for its value. Bash that reference all the | |
825 | way down to its lowest form. */ | |
826 | tree | |
827 | convert_from_reference (val) | |
828 | tree val; | |
829 | { | |
830 | tree type = TREE_TYPE (val); | |
831 | ||
832 | if (TREE_CODE (type) == OFFSET_TYPE) | |
833 | type = TREE_TYPE (type); | |
834 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
835 | { | |
836 | tree target_type = TREE_TYPE (type); | |
837 | tree nval; | |
838 | ||
839 | /* This can happen if we cast to a reference type. */ | |
840 | if (TREE_CODE (val) == ADDR_EXPR) | |
841 | { | |
842 | nval = build1 (NOP_EXPR, build_pointer_type (target_type), val); | |
843 | nval = build_indirect_ref (nval, NULL_PTR); | |
844 | /* The below was missing, are other important flags missing too? */ | |
845 | TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val); | |
846 | return nval; | |
847 | } | |
848 | ||
39211cd5 | 849 | nval = build1 (INDIRECT_REF, target_type, val); |
8d08fdba MS |
850 | |
851 | TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type); | |
852 | TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type); | |
853 | TREE_READONLY (nval) = TYPE_READONLY (target_type); | |
854 | /* The below was missing, are other important flags missing too? */ | |
855 | TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val); | |
856 | return nval; | |
857 | } | |
858 | return val; | |
859 | } | |
860 | \f | |
861 | /* See if there is a constructor of type TYPE which will convert | |
862 | EXPR. The reference manual seems to suggest (8.5.6) that we need | |
863 | not worry about finding constructors for base classes, then converting | |
864 | to the derived class. | |
865 | ||
866 | MSGP is a pointer to a message that would be an appropriate error | |
867 | string. If MSGP is NULL, then we are not interested in reporting | |
868 | errors. */ | |
869 | tree | |
870 | convert_to_aggr (type, expr, msgp, protect) | |
871 | tree type, expr; | |
872 | char **msgp; | |
873 | int protect; | |
874 | { | |
875 | tree basetype = type; | |
876 | tree name = TYPE_IDENTIFIER (basetype); | |
877 | tree function, fndecl, fntype, parmtypes, parmlist, result; | |
878 | tree method_name; | |
879 | enum access_type access; | |
880 | int can_be_private, can_be_protected; | |
881 | ||
882 | if (! TYPE_HAS_CONSTRUCTOR (basetype)) | |
883 | { | |
884 | if (msgp) | |
885 | *msgp = "type `%s' does not have a constructor"; | |
886 | return error_mark_node; | |
887 | } | |
888 | ||
889 | access = access_public; | |
890 | can_be_private = 0; | |
891 | can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name; | |
892 | ||
893 | parmlist = build_tree_list (NULL_TREE, expr); | |
894 | parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node); | |
895 | ||
896 | if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
897 | { | |
898 | parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes); | |
899 | parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist); | |
900 | } | |
901 | ||
902 | /* The type of the first argument will be filled in inside the loop. */ | |
903 | parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist); | |
904 | parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes); | |
905 | ||
b7484fbe | 906 | #if 0 |
8d08fdba MS |
907 | method_name = build_decl_overload (name, parmtypes, 1); |
908 | ||
909 | /* constructors are up front. */ | |
910 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0); | |
911 | if (TYPE_HAS_DESTRUCTOR (basetype)) | |
912 | fndecl = DECL_CHAIN (fndecl); | |
913 | ||
914 | while (fndecl) | |
915 | { | |
916 | if (DECL_ASSEMBLER_NAME (fndecl) == method_name) | |
917 | { | |
918 | function = fndecl; | |
919 | if (protect) | |
920 | { | |
921 | if (TREE_PRIVATE (fndecl)) | |
922 | { | |
923 | can_be_private = | |
924 | (basetype == current_class_type | |
925 | || is_friend (basetype, current_function_decl) | |
926 | || purpose_member (basetype, DECL_ACCESS (fndecl))); | |
927 | if (! can_be_private) | |
928 | goto found; | |
929 | } | |
930 | else if (TREE_PROTECTED (fndecl)) | |
931 | { | |
932 | if (! can_be_protected) | |
933 | goto found; | |
934 | } | |
935 | } | |
936 | goto found_and_ok; | |
937 | } | |
938 | fndecl = DECL_CHAIN (fndecl); | |
939 | } | |
b7484fbe | 940 | #endif |
8d08fdba MS |
941 | |
942 | /* No exact conversion was found. See if an approximate | |
943 | one will do. */ | |
944 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0); | |
945 | if (TYPE_HAS_DESTRUCTOR (basetype)) | |
946 | fndecl = DECL_CHAIN (fndecl); | |
947 | ||
948 | { | |
949 | int saw_private = 0; | |
950 | int saw_protected = 0; | |
951 | struct candidate *candidates = | |
952 | (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate)); | |
953 | struct candidate *cp = candidates; | |
954 | ||
955 | while (fndecl) | |
956 | { | |
957 | function = fndecl; | |
958 | cp->h_len = 2; | |
2986ae00 MS |
959 | cp->harshness = (struct harshness_code *) |
960 | alloca (3 * sizeof (struct harshness_code)); | |
8d08fdba MS |
961 | |
962 | compute_conversion_costs (fndecl, parmlist, cp, 2); | |
2986ae00 | 963 | if ((cp->h.code & EVIL_CODE) == 0) |
8d08fdba MS |
964 | { |
965 | cp->u.field = fndecl; | |
966 | if (protect) | |
967 | { | |
968 | if (TREE_PRIVATE (fndecl)) | |
969 | access = access_private; | |
970 | else if (TREE_PROTECTED (fndecl)) | |
971 | access = access_protected; | |
972 | else | |
973 | access = access_public; | |
974 | } | |
975 | else | |
976 | access = access_public; | |
977 | ||
978 | if (access == access_private | |
979 | ? (basetype == current_class_type | |
980 | || is_friend (basetype, cp->function) | |
981 | || purpose_member (basetype, DECL_ACCESS (fndecl))) | |
982 | : access == access_protected | |
983 | ? (can_be_protected | |
984 | || purpose_member (basetype, DECL_ACCESS (fndecl))) | |
985 | : 1) | |
986 | { | |
2986ae00 | 987 | if (cp->h.code <= TRIVIAL_CODE) |
8d08fdba MS |
988 | goto found_and_ok; |
989 | cp++; | |
990 | } | |
991 | else | |
992 | { | |
993 | if (access == access_private) | |
994 | saw_private = 1; | |
995 | else | |
996 | saw_protected = 1; | |
997 | } | |
998 | } | |
999 | fndecl = DECL_CHAIN (fndecl); | |
1000 | } | |
1001 | if (cp - candidates) | |
1002 | { | |
1003 | /* Rank from worst to best. Then cp will point to best one. | |
1004 | Private fields have their bits flipped. For unsigned | |
1005 | numbers, this should make them look very large. | |
1006 | If the best alternate has a (signed) negative value, | |
1007 | then all we ever saw were private members. */ | |
1008 | if (cp - candidates > 1) | |
1009 | qsort (candidates, /* char *base */ | |
1010 | cp - candidates, /* int nel */ | |
1011 | sizeof (struct candidate), /* int width */ | |
1012 | rank_for_overload); /* int (*compar)() */ | |
1013 | ||
1014 | --cp; | |
2986ae00 | 1015 | if (cp->h.code & EVIL_CODE) |
8d08fdba MS |
1016 | { |
1017 | if (msgp) | |
1018 | *msgp = "ambiguous type conversion possible for `%s'"; | |
1019 | return error_mark_node; | |
1020 | } | |
1021 | ||
1022 | function = cp->function; | |
1023 | fndecl = cp->u.field; | |
1024 | goto found_and_ok; | |
1025 | } | |
1026 | else if (msgp) | |
1027 | { | |
1028 | if (saw_private) | |
1029 | if (saw_protected) | |
1030 | *msgp = "only private and protected conversions apply"; | |
1031 | else | |
1032 | *msgp = "only private conversions apply"; | |
1033 | else if (saw_protected) | |
1034 | *msgp = "only protected conversions apply"; | |
8926095f MS |
1035 | else |
1036 | *msgp = "no appropriate conversion to type `%s'"; | |
8d08fdba MS |
1037 | } |
1038 | return error_mark_node; | |
1039 | } | |
1040 | /* NOTREACHED */ | |
1041 | ||
8d08fdba MS |
1042 | found: |
1043 | if (access == access_private) | |
1044 | if (! can_be_private) | |
1045 | { | |
1046 | if (msgp) | |
1047 | *msgp = TREE_PRIVATE (fndecl) | |
1048 | ? "conversion to type `%s' is private" | |
1049 | : "conversion to type `%s' is from private base class"; | |
1050 | return error_mark_node; | |
1051 | } | |
1052 | if (access == access_protected) | |
1053 | if (! can_be_protected) | |
1054 | { | |
1055 | if (msgp) | |
1056 | *msgp = TREE_PRIVATE (fndecl) | |
1057 | ? "conversion to type `%s' is protected" | |
1058 | : "conversion to type `%s' is from protected base class"; | |
1059 | return error_mark_node; | |
1060 | } | |
1061 | function = fndecl; | |
1062 | found_and_ok: | |
1063 | ||
1064 | /* It will convert, but we don't do anything about it yet. */ | |
1065 | if (msgp == 0) | |
1066 | return NULL_TREE; | |
1067 | ||
1068 | fntype = TREE_TYPE (function); | |
1069 | if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL) | |
1070 | function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); | |
1071 | else | |
1072 | function = default_conversion (function); | |
1073 | ||
1074 | result = build_nt (CALL_EXPR, function, | |
1075 | convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype), | |
1076 | parmlist, NULL_TREE, LOOKUP_NORMAL), | |
1077 | NULL_TREE); | |
1078 | TREE_TYPE (result) = TREE_TYPE (fntype); | |
1079 | TREE_SIDE_EFFECTS (result) = 1; | |
8d08fdba MS |
1080 | return result; |
1081 | } | |
1082 | ||
1083 | /* Call this when we know (for any reason) that expr is not, in fact, | |
1084 | zero. This routine is like convert_pointer_to, but it pays | |
1085 | attention to which specific instance of what type we want to | |
1086 | convert to. This routine should eventually become | |
1087 | convert_to_pointer after all references to convert_to_pointer | |
1088 | are removed. */ | |
1089 | tree | |
1090 | convert_pointer_to_real (binfo, expr) | |
1091 | tree binfo, expr; | |
1092 | { | |
1093 | register tree intype = TREE_TYPE (expr); | |
1094 | tree ptr_type; | |
1095 | tree type, rval; | |
1096 | ||
1097 | if (TREE_CODE (binfo) == TREE_VEC) | |
1098 | type = BINFO_TYPE (binfo); | |
1099 | else if (IS_AGGR_TYPE (binfo)) | |
1100 | { | |
1101 | type = binfo; | |
1102 | } | |
1103 | else | |
1104 | { | |
1105 | type = binfo; | |
1106 | binfo = NULL_TREE; | |
1107 | } | |
1108 | ||
1109 | ptr_type = build_pointer_type (type); | |
1110 | if (ptr_type == TYPE_MAIN_VARIANT (intype)) | |
1111 | return expr; | |
1112 | ||
1113 | if (intype == error_mark_node) | |
1114 | return error_mark_node; | |
1115 | ||
1116 | my_friendly_assert (!integer_zerop (expr), 191); | |
1117 | ||
1118 | if (TREE_CODE (type) == RECORD_TYPE | |
1119 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE | |
1120 | && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype))) | |
1121 | { | |
1122 | tree path; | |
1123 | int distance | |
1124 | = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), | |
1125 | 0, &path); | |
1126 | ||
1127 | /* This function shouldn't be called with unqualified arguments | |
1128 | but if it is, give them an error message that they can read. */ | |
1129 | if (distance < 0) | |
1130 | { | |
7177d104 MS |
1131 | cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'", |
1132 | TREE_TYPE (intype), type); | |
8d08fdba MS |
1133 | |
1134 | if (distance == -2) | |
1135 | cp_error ("because `%T' is an ambiguous base class", type); | |
1136 | return error_mark_node; | |
1137 | } | |
1138 | ||
1139 | return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1); | |
1140 | } | |
1141 | rval = build1 (NOP_EXPR, ptr_type, | |
1142 | TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr); | |
1143 | TREE_CONSTANT (rval) = TREE_CONSTANT (expr); | |
1144 | return rval; | |
1145 | } | |
1146 | ||
1147 | /* Call this when we know (for any reason) that expr is | |
1148 | not, in fact, zero. This routine gets a type out of the first | |
1149 | argument and uses it to search for the type to convert to. If there | |
1150 | is more than one instance of that type in the expr, the conversion is | |
1151 | ambiguous. This routine should eventually go away, and all | |
1152 | callers should use convert_to_pointer_real. */ | |
1153 | tree | |
1154 | convert_pointer_to (binfo, expr) | |
1155 | tree binfo, expr; | |
1156 | { | |
1157 | tree type; | |
1158 | ||
1159 | if (TREE_CODE (binfo) == TREE_VEC) | |
1160 | type = BINFO_TYPE (binfo); | |
1161 | else if (IS_AGGR_TYPE (binfo)) | |
1162 | type = binfo; | |
1163 | else | |
1164 | type = binfo; | |
1165 | return convert_pointer_to_real (type, expr); | |
1166 | } | |
1167 | ||
1168 | /* Same as above, but don't abort if we get an "ambiguous" baseclass. | |
1169 | There's only one virtual baseclass we are looking for, and once | |
1170 | we find one such virtual baseclass, we have found them all. */ | |
1171 | ||
1172 | tree | |
1173 | convert_pointer_to_vbase (binfo, expr) | |
1174 | tree binfo; | |
1175 | tree expr; | |
1176 | { | |
1177 | tree intype = TREE_TYPE (TREE_TYPE (expr)); | |
1178 | tree binfos = TYPE_BINFO_BASETYPES (intype); | |
1179 | int i; | |
1180 | ||
1181 | for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--) | |
1182 | { | |
1183 | tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i)); | |
1184 | if (BINFO_TYPE (binfo) == basetype) | |
1185 | return convert_pointer_to (binfo, expr); | |
1186 | if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype))) | |
1187 | return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr)); | |
1188 | } | |
1189 | my_friendly_abort (6); | |
1190 | /* NOTREACHED */ | |
1191 | return NULL_TREE; | |
1192 | } | |
1193 | \f | |
8d08fdba | 1194 | tree |
a4443a08 | 1195 | cp_convert (type, expr, convtype, flags) |
8d08fdba | 1196 | tree type, expr; |
a4443a08 | 1197 | int convtype, flags; |
8d08fdba MS |
1198 | { |
1199 | register tree e = expr; | |
1200 | register enum tree_code code = TREE_CODE (type); | |
1201 | ||
6060a796 MS |
1202 | if (TREE_CODE (e) == ERROR_MARK |
1203 | || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK) | |
8d08fdba | 1204 | return error_mark_node; |
a4443a08 | 1205 | |
8ccc31eb MS |
1206 | if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)) |
1207 | /* We need a new temporary; don't take this shortcut. */; | |
1208 | else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e))) | |
1209 | /* Trivial conversion: cv-qualifiers do not matter on rvalues. */ | |
f0e01782 | 1210 | return fold (build1 (NOP_EXPR, type, e)); |
a4443a08 MS |
1211 | |
1212 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) | |
1213 | return build1 (CONVERT_EXPR, type, e); | |
1214 | ||
8d08fdba MS |
1215 | #if 0 |
1216 | /* This is incorrect. A truncation can't be stripped this way. | |
1217 | Extensions will be stripped by the use of get_unwidened. */ | |
f0e01782 MS |
1218 | if (TREE_CODE (e) == NOP_EXPR) |
1219 | return convert (type, TREE_OPERAND (e, 0)); | |
8d08fdba MS |
1220 | #endif |
1221 | ||
1222 | /* Just convert to the type of the member. */ | |
1223 | if (code == OFFSET_TYPE) | |
1224 | { | |
1225 | type = TREE_TYPE (type); | |
1226 | code = TREE_CODE (type); | |
1227 | } | |
1228 | ||
8ccc31eb | 1229 | #if 0 |
8d08fdba | 1230 | if (code == REFERENCE_TYPE) |
a4443a08 | 1231 | return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE)); |
8d08fdba MS |
1232 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) |
1233 | e = convert_from_reference (e); | |
8ccc31eb | 1234 | #endif |
8d08fdba | 1235 | |
a292b002 MS |
1236 | if (TREE_CODE (e) == OFFSET_REF) |
1237 | e = resolve_offset_ref (e); | |
1238 | ||
a0a33927 MS |
1239 | if (TREE_READONLY_DECL_P (e)) |
1240 | e = decl_constant_value (e); | |
1241 | ||
2986ae00 | 1242 | if (INTEGRAL_CODE_P (code)) |
8d08fdba | 1243 | { |
f0e01782 | 1244 | tree intype = TREE_TYPE (e); |
8d08fdba MS |
1245 | enum tree_code form = TREE_CODE (intype); |
1246 | /* enum = enum, enum = int, enum = float are all errors. */ | |
1247 | if (flag_int_enum_equivalence == 0 | |
1248 | && TREE_CODE (type) == ENUMERAL_TYPE | |
6060a796 MS |
1249 | && ARITHMETIC_TYPE_P (intype) |
1250 | && ! (convtype & CONV_STATIC)) | |
8d08fdba MS |
1251 | { |
1252 | cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type); | |
1253 | ||
1254 | if (flag_pedantic_errors) | |
1255 | return error_mark_node; | |
1256 | } | |
a292b002 | 1257 | if (IS_AGGR_TYPE (intype)) |
8d08fdba MS |
1258 | { |
1259 | tree rval; | |
f0e01782 | 1260 | rval = build_type_conversion (CONVERT_EXPR, type, e, 1); |
00595019 MS |
1261 | if (rval) |
1262 | return rval; | |
1263 | cp_error ("`%#T' used where a `%T' was expected", intype, type); | |
8d08fdba MS |
1264 | return error_mark_node; |
1265 | } | |
2986ae00 | 1266 | if (code == BOOLEAN_TYPE) |
28ed4616 | 1267 | return truthvalue_conversion (e); |
8d08fdba MS |
1268 | return fold (convert_to_integer (type, e)); |
1269 | } | |
8ccc31eb | 1270 | if (code == POINTER_TYPE || code == REFERENCE_TYPE) |
8d08fdba MS |
1271 | return fold (cp_convert_to_pointer (type, e)); |
1272 | if (code == REAL_TYPE) | |
1273 | { | |
1274 | if (IS_AGGR_TYPE (TREE_TYPE (e))) | |
1275 | { | |
1276 | tree rval; | |
1277 | rval = build_type_conversion (CONVERT_EXPR, type, e, 1); | |
1278 | if (rval) | |
1279 | return rval; | |
1280 | else | |
1281 | cp_error ("`%#T' used where a floating point value was expected", | |
1282 | TREE_TYPE (e)); | |
1283 | } | |
1284 | return fold (convert_to_real (type, e)); | |
1285 | } | |
1286 | ||
1287 | /* New C++ semantics: since assignment is now based on | |
1288 | memberwise copying, if the rhs type is derived from the | |
1289 | lhs type, then we may still do a conversion. */ | |
1290 | if (IS_AGGR_TYPE_CODE (code)) | |
1291 | { | |
1292 | tree dtype = TREE_TYPE (e); | |
db5ae43f MS |
1293 | tree ctor = NULL_TREE; |
1294 | tree conversion = NULL_TREE; | |
8d08fdba | 1295 | |
8d08fdba MS |
1296 | dtype = TYPE_MAIN_VARIANT (dtype); |
1297 | ||
1298 | /* Conversion of object pointers or signature pointers/references | |
1299 | to signature pointers/references. */ | |
1300 | ||
1301 | if (TYPE_LANG_SPECIFIC (type) | |
1302 | && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))) | |
1303 | { | |
1304 | tree constructor = build_signature_pointer_constructor (type, expr); | |
1305 | tree sig_ty = SIGNATURE_TYPE (type); | |
1306 | tree sig_ptr; | |
1307 | ||
1308 | if (constructor == error_mark_node) | |
1309 | return error_mark_node; | |
1310 | ||
1311 | sig_ptr = get_temp_name (type, 1); | |
1312 | DECL_INITIAL (sig_ptr) = constructor; | |
1313 | CLEAR_SIGNATURE (sig_ty); | |
6060a796 | 1314 | finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0); |
8d08fdba MS |
1315 | SET_SIGNATURE (sig_ty); |
1316 | TREE_READONLY (sig_ptr) = 1; | |
1317 | ||
1318 | return sig_ptr; | |
1319 | } | |
1320 | ||
1321 | /* Conversion between aggregate types. New C++ semantics allow | |
1322 | objects of derived type to be cast to objects of base type. | |
1323 | Old semantics only allowed this between pointers. | |
1324 | ||
1325 | There may be some ambiguity between using a constructor | |
1326 | vs. using a type conversion operator when both apply. */ | |
1327 | ||
db5ae43f MS |
1328 | if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype) |
1329 | && TYPE_HAS_CONVERSION (dtype)) | |
1330 | conversion = build_type_conversion (CONVERT_EXPR, type, e, 1); | |
8d08fdba | 1331 | |
db5ae43f MS |
1332 | if (conversion == error_mark_node) |
1333 | { | |
1334 | error ("ambiguous pointer conversion"); | |
1335 | return conversion; | |
1336 | } | |
8d08fdba | 1337 | |
db5ae43f MS |
1338 | if (TYPE_HAS_CONSTRUCTOR (type)) |
1339 | ctor = build_method_call (NULL_TREE, constructor_name_full (type), | |
1340 | build_tree_list (NULL_TREE, e), | |
1341 | TYPE_BINFO (type), | |
1342 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY | |
6060a796 | 1343 | | (convtype&CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING) |
db5ae43f | 1344 | | (conversion ? LOOKUP_NO_CONVERSION : 0)); |
8d08fdba | 1345 | |
db5ae43f MS |
1346 | if (ctor == error_mark_node) |
1347 | { | |
1348 | cp_error ("in conversion to type `%T'", type); | |
8d08fdba MS |
1349 | return error_mark_node; |
1350 | } | |
db5ae43f MS |
1351 | |
1352 | if (conversion && ctor) | |
8d08fdba | 1353 | { |
db5ae43f MS |
1354 | error ("both constructor and type conversion operator apply"); |
1355 | return error_mark_node; | |
1356 | } | |
1357 | else if (conversion) | |
1358 | return conversion; | |
1359 | else if (ctor) | |
1360 | { | |
1361 | if (current_function_decl) | |
1362 | /* We can't pass 1 to the with_cleanup_p arg here, because that | |
1363 | screws up passing classes by value. */ | |
1364 | ctor = build_cplus_new (type, ctor, 0); | |
1365 | else | |
8d08fdba | 1366 | { |
db5ae43f MS |
1367 | register tree parm = TREE_OPERAND (ctor, 1); |
1368 | ||
1369 | /* Initializers for static variables and parameters | |
1370 | have to handle doing the initialization and | |
1371 | cleanup themselves. */ | |
1372 | my_friendly_assert (TREE_CODE (ctor) == CALL_EXPR, 322); | |
1373 | #if 0 | |
1374 | /* The following assertion fails in cases where we | |
1375 | are initializing a static member variable of a | |
1376 | particular instance of a template class with a | |
1377 | call to a constructor of the given instance, as | |
1378 | in: | |
1379 | ||
1380 | TMPL<int> object = TMPL<int>(); | |
1381 | ||
1382 | Curiously, the assertion does not fail if we do | |
1383 | the same thing for a static member of a | |
1384 | non-template class, as in: | |
1385 | ||
1386 | T object = T(); | |
1387 | ||
1388 | I can't see why we should care here whether or not | |
1389 | the initializer expression involves a call to | |
1390 | `new', so for the time being, it seems best to | |
1391 | just avoid doing this assertion. */ | |
1392 | my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)), | |
1393 | 323); | |
1394 | #endif | |
1395 | TREE_VALUE (parm) = NULL_TREE; | |
1396 | ctor = build_indirect_ref (ctor, NULL_PTR); | |
1397 | TREE_HAS_CONSTRUCTOR (ctor) = 1; | |
8d08fdba | 1398 | } |
db5ae43f | 1399 | return ctor; |
8d08fdba MS |
1400 | } |
1401 | } | |
1402 | ||
f0e01782 | 1403 | /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack, |
8d08fdba MS |
1404 | then the it won't be hashed and hence compare as not equal, |
1405 | even when it is. */ | |
1406 | if (code == ARRAY_TYPE | |
f0e01782 MS |
1407 | && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type) |
1408 | && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type))) | |
1409 | return e; | |
8d08fdba MS |
1410 | |
1411 | cp_error ("conversion from `%T' to non-scalar type `%T' requested", | |
1412 | TREE_TYPE (expr), type); | |
1413 | return error_mark_node; | |
1414 | } | |
1415 | ||
a4443a08 MS |
1416 | /* Create an expression whose value is that of EXPR, |
1417 | converted to type TYPE. The TREE_TYPE of the value | |
1418 | is always TYPE. This function implements all reasonable | |
1419 | conversions; callers should filter out those that are | |
1420 | not permitted by the language being compiled. */ | |
1421 | ||
1422 | tree | |
1423 | convert (type, expr) | |
1424 | tree type, expr; | |
1425 | { | |
1426 | return cp_convert (type, expr, CONV_OLD_CONVERT, 0); | |
1427 | } | |
1428 | ||
8d08fdba MS |
1429 | /* Like convert, except permit conversions to take place which |
1430 | are not normally allowed due to access restrictions | |
1431 | (such as conversion from sub-type to private super-type). */ | |
1432 | tree | |
6060a796 | 1433 | convert_force (type, expr, convtype) |
8d08fdba MS |
1434 | tree type; |
1435 | tree expr; | |
6060a796 | 1436 | int convtype; |
8d08fdba MS |
1437 | { |
1438 | register tree e = expr; | |
1439 | register enum tree_code code = TREE_CODE (type); | |
1440 | ||
1441 | if (code == REFERENCE_TYPE) | |
a4443a08 MS |
1442 | return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN, |
1443 | NULL_TREE)); | |
8d08fdba MS |
1444 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) |
1445 | e = convert_from_reference (e); | |
1446 | ||
1447 | if (code == POINTER_TYPE) | |
1448 | return fold (convert_to_pointer_force (type, e)); | |
1449 | ||
51c184be | 1450 | /* From typeck.c convert_for_assignment */ |
8d08fdba MS |
1451 | if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR |
1452 | && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE | |
1453 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) | |
51c184be MS |
1454 | || integer_zerop (e) |
1455 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
8d08fdba MS |
1456 | && TYPE_PTRMEMFUNC_P (type)) |
1457 | { | |
1458 | /* compatible pointer to member functions. */ | |
51c184be | 1459 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1); |
8d08fdba | 1460 | } |
6060a796 | 1461 | |
8ccc31eb | 1462 | return cp_convert (type, e, CONV_C_CAST|convtype, 0); |
8d08fdba MS |
1463 | } |
1464 | ||
1465 | /* Subroutine of build_type_conversion. */ | |
1466 | static tree | |
1467 | build_type_conversion_1 (xtype, basetype, expr, typename, for_sure) | |
1468 | tree xtype, basetype; | |
1469 | tree expr; | |
1470 | tree typename; | |
1471 | int for_sure; | |
1472 | { | |
8d08fdba MS |
1473 | tree rval; |
1474 | int flags; | |
1475 | ||
1476 | if (for_sure == 0) | |
db5ae43f | 1477 | flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING; |
8d08fdba | 1478 | else |
db5ae43f | 1479 | flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING; |
8d08fdba | 1480 | |
21474714 | 1481 | rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags); |
8d08fdba MS |
1482 | if (rval == error_mark_node) |
1483 | { | |
1484 | if (for_sure == 0) | |
1485 | return NULL_TREE; | |
1486 | return error_mark_node; | |
1487 | } | |
8ccc31eb MS |
1488 | |
1489 | if (IS_AGGR_TYPE (TREE_TYPE (rval))) | |
1490 | return rval; | |
8d08fdba MS |
1491 | |
1492 | if (warn_cast_qual | |
1493 | && TREE_TYPE (xtype) | |
1494 | && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval))) | |
1495 | > TREE_READONLY (TREE_TYPE (xtype)))) | |
1496 | warning ("user-defined conversion casting away `const'"); | |
a4443a08 | 1497 | return convert (xtype, rval); |
8d08fdba MS |
1498 | } |
1499 | ||
1500 | /* Convert an aggregate EXPR to type XTYPE. If a conversion | |
1501 | exists, return the attempted conversion. This may | |
1502 | return ERROR_MARK_NODE if the conversion is not | |
1503 | allowed (references private members, etc). | |
1504 | If no conversion exists, NULL_TREE is returned. | |
1505 | ||
1506 | If (FOR_SURE & 1) is non-zero, then we allow this type conversion | |
1507 | to take place immediately. Otherwise, we build a SAVE_EXPR | |
e1cd6e56 | 1508 | which can be evaluated if the results are ever needed. */ |
8d08fdba | 1509 | |
8d08fdba MS |
1510 | tree |
1511 | build_type_conversion (code, xtype, expr, for_sure) | |
1512 | enum tree_code code; | |
1513 | tree xtype, expr; | |
1514 | int for_sure; | |
1515 | { | |
1516 | /* C++: check to see if we can convert this aggregate type | |
e1cd6e56 MS |
1517 | into the required type. */ |
1518 | tree basetype; | |
1519 | tree conv; | |
1520 | tree winner = NULL_TREE; | |
8d08fdba MS |
1521 | |
1522 | if (expr == error_mark_node) | |
1523 | return error_mark_node; | |
1524 | ||
1525 | basetype = TREE_TYPE (expr); | |
1526 | if (TREE_CODE (basetype) == REFERENCE_TYPE) | |
1527 | basetype = TREE_TYPE (basetype); | |
1528 | ||
1529 | basetype = TYPE_MAIN_VARIANT (basetype); | |
1530 | if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype)) | |
1531 | return NULL_TREE; | |
1532 | ||
e1cd6e56 MS |
1533 | /* Do we have an exact match? */ |
1534 | { | |
1535 | tree typename = build_typename_overload (xtype); | |
1536 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1537 | return build_type_conversion_1 (xtype, basetype, expr, typename, | |
1538 | for_sure); | |
1539 | } | |
8d08fdba | 1540 | |
e1cd6e56 MS |
1541 | /* Nope; try looking for others. */ |
1542 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) | |
8d08fdba | 1543 | { |
e1cd6e56 MS |
1544 | if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv)) |
1545 | continue; | |
8d08fdba | 1546 | |
e1cd6e56 | 1547 | if (can_convert (xtype, TREE_VALUE (conv))) |
8d08fdba | 1548 | { |
e1cd6e56 | 1549 | if (winner) |
8d08fdba | 1550 | { |
e1cd6e56 MS |
1551 | cp_error ("ambiguous conversion from `%T' to `%T'", basetype, |
1552 | xtype); | |
b7484fbe | 1553 | cp_error (" candidate conversions include `%T' and `%T'", |
e1cd6e56 MS |
1554 | TREE_VALUE (winner), TREE_VALUE (conv)); |
1555 | return NULL_TREE; | |
8d08fdba MS |
1556 | } |
1557 | else | |
e1cd6e56 | 1558 | winner = conv; |
8d08fdba MS |
1559 | } |
1560 | } | |
1561 | ||
e1cd6e56 MS |
1562 | if (winner) |
1563 | return build_type_conversion_1 (xtype, basetype, expr, | |
1564 | TREE_PURPOSE (winner), for_sure); | |
8d08fdba MS |
1565 | |
1566 | return NULL_TREE; | |
1567 | } | |
1568 | ||
b7484fbe MS |
1569 | /* Convert the given EXPR to one of a group of types suitable for use in an |
1570 | expression. DESIRES is a combination of various WANT_* flags (q.v.) | |
1571 | which indicates which types are suitable. If COMPLAIN is 1, complain | |
1572 | about ambiguity; otherwise, the caller will deal with it. */ | |
8d08fdba | 1573 | |
b7484fbe MS |
1574 | tree |
1575 | build_expr_type_conversion (desires, expr, complain) | |
1576 | int desires; | |
1577 | tree expr; | |
1578 | int complain; | |
8d08fdba | 1579 | { |
b7484fbe MS |
1580 | tree basetype = TREE_TYPE (expr); |
1581 | tree conv; | |
1582 | tree winner = NULL_TREE; | |
8d08fdba | 1583 | |
b7484fbe | 1584 | if (TREE_CODE (basetype) == OFFSET_TYPE) |
8d08fdba | 1585 | { |
b7484fbe MS |
1586 | expr = resolve_offset_ref (expr); |
1587 | basetype = TREE_TYPE (expr); | |
8d08fdba MS |
1588 | } |
1589 | ||
b7484fbe MS |
1590 | if (! IS_AGGR_TYPE (basetype)) |
1591 | switch (TREE_CODE (basetype)) | |
1592 | { | |
1593 | case INTEGER_TYPE: | |
1594 | if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST | |
1595 | && integer_zerop (expr)) | |
1596 | return expr; | |
1597 | /* else fall through... */ | |
1598 | ||
1599 | case BOOLEAN_TYPE: | |
1600 | return (desires & WANT_INT) ? expr : NULL_TREE; | |
1601 | case ENUMERAL_TYPE: | |
1602 | return (desires & WANT_ENUM) ? expr : NULL_TREE; | |
1603 | case REAL_TYPE: | |
1604 | return (desires & WANT_FLOAT) ? expr : NULL_TREE; | |
1605 | case POINTER_TYPE: | |
1606 | return (desires & WANT_POINTER) ? expr : NULL_TREE; | |
1607 | ||
1608 | case FUNCTION_TYPE: | |
1609 | case ARRAY_TYPE: | |
1610 | return (desires & WANT_POINTER) ? default_conversion (expr) | |
1611 | : NULL_TREE; | |
1612 | default: | |
1613 | return NULL_TREE; | |
1614 | } | |
8d08fdba | 1615 | |
b7484fbe MS |
1616 | if (! TYPE_HAS_CONVERSION (basetype)) |
1617 | return NULL_TREE; | |
1618 | ||
1619 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) | |
8d08fdba | 1620 | { |
b7484fbe MS |
1621 | int win = 0; |
1622 | ||
1623 | if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv)) | |
1624 | continue; | |
1625 | ||
1626 | switch (TREE_CODE (TREE_VALUE (conv))) | |
1627 | { | |
1628 | case BOOLEAN_TYPE: | |
1629 | case INTEGER_TYPE: | |
1630 | win = (desires & WANT_INT); break; | |
1631 | case ENUMERAL_TYPE: | |
1632 | win = (desires & WANT_ENUM); break; | |
1633 | case REAL_TYPE: | |
1634 | win = (desires & WANT_FLOAT); break; | |
1635 | case POINTER_TYPE: | |
1636 | win = (desires & WANT_POINTER); break; | |
1637 | } | |
1638 | ||
1639 | if (win) | |
1640 | { | |
1641 | if (winner) | |
1642 | { | |
1643 | if (complain) | |
1644 | { | |
1645 | cp_error ("ambiguous default type conversion from `%T'", | |
1646 | basetype); | |
1647 | cp_error (" candidate conversions include `%T' and `%T'", | |
1648 | TREE_VALUE (winner), TREE_VALUE (conv)); | |
1649 | } | |
1650 | return error_mark_node; | |
1651 | } | |
1652 | else | |
1653 | winner = conv; | |
1654 | } | |
8d08fdba | 1655 | } |
b7484fbe MS |
1656 | |
1657 | if (winner) | |
1658 | return build_type_conversion_1 (TREE_VALUE (winner), basetype, expr, | |
1659 | TREE_PURPOSE (winner), 1); | |
1660 | ||
1661 | return NULL_TREE; | |
8d08fdba MS |
1662 | } |
1663 | ||
b7484fbe | 1664 | /* Must convert two aggregate types to non-aggregate type. |
8d08fdba MS |
1665 | Attempts to find a non-ambiguous, "best" type conversion. |
1666 | ||
1667 | Return 1 on success, 0 on failure. | |
1668 | ||
8d08fdba MS |
1669 | @@ What are the real semantics of this supposed to be??? */ |
1670 | int | |
b7484fbe | 1671 | build_default_binary_type_conversion (code, arg1, arg2) |
8d08fdba | 1672 | enum tree_code code; |
b7484fbe | 1673 | tree *arg1, *arg2; |
8d08fdba | 1674 | { |
b7484fbe | 1675 | switch (code) |
8d08fdba | 1676 | { |
b7484fbe MS |
1677 | case MULT_EXPR: |
1678 | case TRUNC_DIV_EXPR: | |
1679 | case CEIL_DIV_EXPR: | |
1680 | case FLOOR_DIV_EXPR: | |
1681 | case ROUND_DIV_EXPR: | |
1682 | case EXACT_DIV_EXPR: | |
1683 | *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0); | |
1684 | *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0); | |
1685 | break; | |
8d08fdba | 1686 | |
b7484fbe MS |
1687 | case TRUNC_MOD_EXPR: |
1688 | case FLOOR_MOD_EXPR: | |
1689 | case LSHIFT_EXPR: | |
1690 | case RSHIFT_EXPR: | |
1691 | case BIT_AND_EXPR: | |
1692 | case BIT_XOR_EXPR: | |
1693 | case BIT_IOR_EXPR: | |
1694 | *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0); | |
1695 | *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0); | |
1696 | break; | |
1697 | ||
1698 | case PLUS_EXPR: | |
1699 | { | |
1700 | tree a1, a2, p1, p2; | |
1701 | int wins; | |
1702 | ||
1703 | a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0); | |
1704 | a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0); | |
1705 | p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0); | |
1706 | p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0); | |
1707 | ||
1708 | wins = (a1 && a2) + (a1 && p2) + (p1 && a2); | |
1709 | ||
1710 | if (wins > 1) | |
1711 | error ("ambiguous default type conversion for `operator +'"); | |
1712 | ||
1713 | if (a1 && a2) | |
1714 | *arg1 = a1, *arg2 = a2; | |
1715 | else if (a1 && p2) | |
1716 | *arg1 = a1, *arg2 = p2; | |
1717 | else | |
1718 | *arg1 = p1, *arg2 = a2; | |
1719 | break; | |
1720 | } | |
1721 | ||
1722 | case MINUS_EXPR: | |
1723 | { | |
1724 | tree a1, a2, p1, p2; | |
1725 | int wins; | |
1726 | ||
1727 | a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0); | |
1728 | a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0); | |
1729 | p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0); | |
1730 | p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0); | |
1731 | ||
1732 | wins = (a1 && a2) + (p1 && p2) + (p1 && a2); | |
1733 | ||
1734 | if (wins > 1) | |
1735 | error ("ambiguous default type conversion for `operator -'"); | |
1736 | ||
1737 | if (a1 && a2) | |
1738 | *arg1 = a1, *arg2 = a2; | |
1739 | else if (p1 && p2) | |
1740 | *arg1 = p1, *arg2 = p2; | |
1741 | else | |
1742 | *arg1 = p1, *arg2 = a2; | |
1743 | break; | |
1744 | } | |
1745 | ||
1746 | case GT_EXPR: | |
1747 | case LT_EXPR: | |
1748 | case GE_EXPR: | |
1749 | case LE_EXPR: | |
1750 | case EQ_EXPR: | |
1751 | case NE_EXPR: | |
1752 | { | |
1753 | tree a1, a2, p1, p2; | |
1754 | int wins; | |
1755 | ||
1756 | a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0); | |
1757 | a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0); | |
1758 | p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0); | |
1759 | p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0); | |
1760 | ||
1761 | wins = (a1 && a2) + (p1 && p2); | |
1762 | ||
1763 | if (wins > 1) | |
1764 | cp_error ("ambiguous default type conversion for `%O'", code); | |
1765 | ||
1766 | if (a1 && a2) | |
1767 | *arg1 = a1, *arg2 = a2; | |
1768 | else | |
1769 | *arg1 = p1, *arg2 = p2; | |
1770 | break; | |
1771 | } | |
1772 | ||
1773 | case TRUTH_ANDIF_EXPR: | |
1774 | case TRUTH_ORIF_EXPR: | |
1775 | *arg1 = convert (boolean_type_node, *arg1); | |
1776 | *arg2 = convert (boolean_type_node, *arg2); | |
1777 | break; | |
1778 | ||
1779 | default: | |
1780 | *arg1 = NULL_TREE; | |
1781 | *arg2 = NULL_TREE; | |
8d08fdba | 1782 | } |
b7484fbe MS |
1783 | |
1784 | if (*arg1 == error_mark_node || *arg2 == error_mark_node) | |
1785 | cp_error ("ambiguous default type conversion for `%O'", code); | |
1786 | ||
1787 | if (*arg1 && *arg2) | |
1788 | return 1; | |
1789 | ||
1790 | return 0; | |
8d08fdba | 1791 | } |
39211cd5 MS |
1792 | |
1793 | /* Implements integral promotion (4.1) and float->double promotion. */ | |
1794 | tree | |
1795 | type_promotes_to (type) | |
1796 | tree type; | |
1797 | { | |
1798 | int constp = TYPE_READONLY (type); | |
1799 | int volatilep = TYPE_VOLATILE (type); | |
1800 | type = TYPE_MAIN_VARIANT (type); | |
2986ae00 MS |
1801 | |
1802 | /* bool always promotes to int (not unsigned), even if it's the same | |
1803 | size. */ | |
28ed4616 | 1804 | if (type == boolean_type_node) |
2986ae00 MS |
1805 | type = integer_type_node; |
1806 | ||
1807 | /* Normally convert enums to int, but convert wide enums to something | |
1808 | wider. */ | |
1809 | else if (TREE_CODE (type) == ENUMERAL_TYPE | |
1810 | || type == wchar_type_node) | |
cf2ac46f JM |
1811 | { |
1812 | int precision = MAX (TYPE_PRECISION (type), | |
1813 | TYPE_PRECISION (integer_type_node)); | |
1814 | tree totype = type_for_size (precision, 0); | |
1815 | if (TREE_UNSIGNED (type) | |
1816 | && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) | |
1817 | type = type_for_size (precision, 1); | |
1818 | else | |
1819 | type = totype; | |
1820 | } | |
39211cd5 MS |
1821 | else if (C_PROMOTING_INTEGER_TYPE_P (type)) |
1822 | { | |
1823 | /* Traditionally, unsignedness is preserved in default promotions. | |
1824 | Otherwise, retain unsignedness if really not getting bigger. */ | |
1825 | if (TREE_UNSIGNED (type) | |
1826 | && (flag_traditional | |
1827 | || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
1828 | type = unsigned_type_node; | |
1829 | else | |
1830 | type = integer_type_node; | |
1831 | } | |
1832 | else if (type == float_type_node) | |
1833 | type = double_type_node; | |
1834 | ||
f376e137 | 1835 | return cp_build_type_variant (type, constp, volatilep); |
39211cd5 | 1836 | } |