]>
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 | ||
116 | if (form == POINTER_TYPE) | |
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); | |
635 | register enum tree_code form = TREE_CODE (intype); | |
636 | tree rval = NULL_TREE; | |
637 | ||
8d08fdba MS |
638 | if (form == REFERENCE_TYPE) |
639 | intype = TREE_TYPE (intype); | |
640 | intype = TYPE_MAIN_VARIANT (intype); | |
641 | ||
a4443a08 MS |
642 | if (((convtype & CONV_STATIC) && comptypes (type, intype, -1)) |
643 | || ((convtype & CONV_IMPLICIT) && comptypes (type, intype, 0))) | |
8d08fdba | 644 | { |
8d08fdba MS |
645 | if (flags & LOOKUP_COMPLAIN) |
646 | { | |
39211cd5 MS |
647 | tree ttl = TREE_TYPE (reftype); |
648 | tree ttr; | |
649 | ||
8926095f | 650 | if (form == REFERENCE_TYPE) |
39211cd5 MS |
651 | ttr = TREE_TYPE (TREE_TYPE (expr)); |
652 | else | |
a3203465 MS |
653 | { |
654 | int r = TREE_READONLY (expr); | |
655 | int v = TREE_THIS_VOLATILE (expr); | |
f376e137 | 656 | ttr = cp_build_type_variant (TREE_TYPE (expr), r, v); |
a3203465 | 657 | } |
8d08fdba | 658 | |
a4443a08 MS |
659 | if (! lvalue_p (expr) && |
660 | (decl == NULL_TREE || ! TYPE_READONLY (ttl))) | |
39211cd5 | 661 | { |
a4443a08 MS |
662 | if (decl) |
663 | /* Ensure semantics of [dcl.init.ref] */ | |
664 | cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'", | |
665 | reftype, intype); | |
666 | else | |
667 | cp_pedwarn ("conversion to `%T' from rvalue `%T'", | |
668 | reftype, intype); | |
39211cd5 | 669 | } |
a4443a08 | 670 | else if (! (convtype & CONV_CONST)) |
8d08fdba | 671 | { |
a4443a08 MS |
672 | if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) |
673 | cp_pedwarn ("conversion from `%T' to `%T' discards const", | |
a3203465 | 674 | ttr, reftype); |
a4443a08 MS |
675 | else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) |
676 | cp_pedwarn ("conversion from `%T' to `%T' discards volatile", | |
a3203465 | 677 | ttr, reftype); |
8d08fdba | 678 | } |
8926095f MS |
679 | } |
680 | ||
a3203465 | 681 | if (form == REFERENCE_TYPE) |
8926095f | 682 | { |
db5ae43f | 683 | tree type = TREE_TYPE (expr); |
e1cd6e56 MS |
684 | TREE_TYPE (expr) = build_pointer_type (TREE_TYPE (type)); |
685 | rval = cp_convert (build_pointer_type (TREE_TYPE (reftype)), expr, | |
a4443a08 | 686 | convtype, flags); |
e1cd6e56 | 687 | TREE_TYPE (expr) = type; |
a3203465 | 688 | TREE_TYPE (rval) = reftype; |
59be85d7 JM |
689 | if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR) |
690 | TREE_TYPE (TREE_OPERAND (rval, 0)) | |
691 | = TREE_TYPE (TREE_OPERAND (rval, 1)) = reftype; | |
8926095f | 692 | return rval; |
8d08fdba | 693 | } |
8926095f | 694 | |
a4443a08 MS |
695 | return build_up_reference (reftype, expr, flags, |
696 | ! (convtype & CONV_CONST)); | |
8d08fdba MS |
697 | } |
698 | ||
21474714 MS |
699 | if ((convtype & CONV_IMPLICIT) |
700 | && IS_AGGR_TYPE (intype) | |
701 | && ! (flags & LOOKUP_NO_CONVERSION) | |
702 | && (rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1))) | |
703 | { | |
704 | if (rval == error_mark_node) | |
705 | cp_error ("conversion from `%T' to `%T' is ambiguous", | |
706 | intype, reftype); | |
707 | return rval; | |
708 | } | |
709 | else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr)) | |
8926095f MS |
710 | { |
711 | /* When casting an lvalue to a reference type, just convert into | |
712 | a pointer to the new type and deference it. This is allowed | |
a28e3c7f | 713 | by San Diego WP section 5.2.9 paragraph 12, though perhaps it |
8926095f | 714 | should be done directly (jason). (int &)ri ---> *(int*)&ri */ |
a28e3c7f | 715 | |
59be0cdd | 716 | /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they |
a28e3c7f MS |
717 | meant. */ |
718 | if (form == POINTER_TYPE | |
a4443a08 | 719 | && (comptypes (TREE_TYPE (intype), type, -1))) |
a28e3c7f MS |
720 | cp_warning ("casting `%T' to `%T' does not dereference pointer", |
721 | intype, reftype); | |
722 | ||
8926095f MS |
723 | rval = build_unary_op (ADDR_EXPR, expr, 0); |
724 | if (rval != error_mark_node) | |
6060a796 | 725 | rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0); |
8926095f | 726 | if (rval != error_mark_node) |
7177d104 | 727 | rval = build1 (NOP_EXPR, reftype, rval); |
8926095f | 728 | } |
a4443a08 | 729 | else if (decl) |
8d08fdba MS |
730 | { |
731 | tree rval_as_conversion = NULL_TREE; | |
732 | tree rval_as_ctor = NULL_TREE; | |
733 | ||
734 | if (IS_AGGR_TYPE (intype) | |
735 | && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1))) | |
736 | { | |
737 | if (rval == error_mark_node) | |
738 | return rval; | |
739 | ||
740 | rval_as_conversion = build_up_reference (reftype, rval, flags, 1); | |
741 | } | |
742 | ||
743 | /* Definitely need to go through a constructor here. */ | |
744 | if (TYPE_HAS_CONSTRUCTOR (type) | |
a28e3c7f | 745 | && ! CLASSTYPE_ABSTRACT_VIRTUALS (type) |
8d08fdba MS |
746 | && (rval = build_method_call |
747 | (NULL_TREE, constructor_name_full (type), | |
748 | build_tree_list (NULL_TREE, expr), TYPE_BINFO (type), | |
db5ae43f MS |
749 | LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY |
750 | | LOOKUP_ONLYCONVERTING))) | |
8d08fdba MS |
751 | { |
752 | tree init; | |
753 | ||
754 | if (global_bindings_p ()) | |
755 | { | |
756 | extern tree static_aggregates; | |
a4443a08 MS |
757 | tree t = get_temp_name (type, global_bindings_p ()); |
758 | init = build_method_call (t, constructor_name_full (type), | |
8d08fdba | 759 | build_tree_list (NULL_TREE, expr), |
a4443a08 | 760 | TYPE_BINFO (type), |
db5ae43f MS |
761 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION |
762 | | LOOKUP_ONLYCONVERTING); | |
8d08fdba MS |
763 | |
764 | if (init == error_mark_node) | |
765 | return error_mark_node; | |
766 | ||
a4443a08 MS |
767 | make_decl_rtl (t, NULL_PTR, 1); |
768 | static_aggregates = perm_tree_cons (expr, t, static_aggregates); | |
769 | rval = build_unary_op (ADDR_EXPR, t, 0); | |
8d08fdba MS |
770 | } |
771 | else | |
772 | { | |
773 | init = build_method_call (NULL_TREE, constructor_name_full (type), | |
774 | build_tree_list (NULL_TREE, expr), | |
a4443a08 | 775 | TYPE_BINFO (type), |
db5ae43f MS |
776 | LOOKUP_NORMAL|LOOKUP_NO_CONVERSION |
777 | |LOOKUP_ONLYCONVERTING); | |
8d08fdba MS |
778 | |
779 | if (init == error_mark_node) | |
780 | return error_mark_node; | |
781 | ||
782 | rval = build_cplus_new (type, init, 1); | |
783 | rval = build_up_reference (reftype, rval, flags, 1); | |
784 | } | |
785 | rval_as_ctor = rval; | |
786 | } | |
787 | ||
788 | if (rval_as_ctor && rval_as_conversion) | |
789 | { | |
790 | cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply", | |
791 | intype, reftype); | |
792 | return error_mark_node; | |
793 | } | |
794 | else if (rval_as_ctor) | |
795 | rval = rval_as_ctor; | |
796 | else if (rval_as_conversion) | |
797 | rval = rval_as_conversion; | |
798 | else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype)) | |
799 | { | |
800 | rval = convert (type, expr); | |
801 | if (rval == error_mark_node) | |
802 | return error_mark_node; | |
803 | ||
804 | rval = build_up_reference (reftype, rval, flags, 1); | |
805 | } | |
806 | ||
807 | if (rval && ! TYPE_READONLY (TREE_TYPE (reftype))) | |
a4443a08 MS |
808 | cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary", |
809 | reftype, intype); | |
8d08fdba MS |
810 | } |
811 | ||
812 | if (rval) | |
813 | { | |
814 | /* If we found a way to convert earlier, then use it. */ | |
815 | return rval; | |
816 | } | |
817 | ||
818 | my_friendly_assert (form != OFFSET_TYPE, 189); | |
819 | ||
8d08fdba MS |
820 | if (flags & LOOKUP_SPECULATIVELY) |
821 | return NULL_TREE; | |
822 | ||
8926095f MS |
823 | else if (flags & LOOKUP_COMPLAIN) |
824 | cp_error ("cannot convert type `%T' to type `%T'", intype, reftype); | |
825 | ||
8d08fdba MS |
826 | return error_mark_node; |
827 | } | |
828 | ||
829 | /* We are using a reference VAL for its value. Bash that reference all the | |
830 | way down to its lowest form. */ | |
831 | tree | |
832 | convert_from_reference (val) | |
833 | tree val; | |
834 | { | |
835 | tree type = TREE_TYPE (val); | |
836 | ||
837 | if (TREE_CODE (type) == OFFSET_TYPE) | |
838 | type = TREE_TYPE (type); | |
839 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
840 | { | |
841 | tree target_type = TREE_TYPE (type); | |
842 | tree nval; | |
843 | ||
844 | /* This can happen if we cast to a reference type. */ | |
845 | if (TREE_CODE (val) == ADDR_EXPR) | |
846 | { | |
847 | nval = build1 (NOP_EXPR, build_pointer_type (target_type), val); | |
848 | nval = build_indirect_ref (nval, NULL_PTR); | |
849 | /* The below was missing, are other important flags missing too? */ | |
850 | TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val); | |
851 | return nval; | |
852 | } | |
853 | ||
39211cd5 | 854 | nval = build1 (INDIRECT_REF, target_type, val); |
8d08fdba MS |
855 | |
856 | TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type); | |
857 | TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type); | |
858 | TREE_READONLY (nval) = TYPE_READONLY (target_type); | |
859 | /* The below was missing, are other important flags missing too? */ | |
860 | TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val); | |
861 | return nval; | |
862 | } | |
863 | return val; | |
864 | } | |
865 | \f | |
866 | /* See if there is a constructor of type TYPE which will convert | |
867 | EXPR. The reference manual seems to suggest (8.5.6) that we need | |
868 | not worry about finding constructors for base classes, then converting | |
869 | to the derived class. | |
870 | ||
871 | MSGP is a pointer to a message that would be an appropriate error | |
872 | string. If MSGP is NULL, then we are not interested in reporting | |
873 | errors. */ | |
874 | tree | |
875 | convert_to_aggr (type, expr, msgp, protect) | |
876 | tree type, expr; | |
877 | char **msgp; | |
878 | int protect; | |
879 | { | |
880 | tree basetype = type; | |
881 | tree name = TYPE_IDENTIFIER (basetype); | |
882 | tree function, fndecl, fntype, parmtypes, parmlist, result; | |
883 | tree method_name; | |
884 | enum access_type access; | |
885 | int can_be_private, can_be_protected; | |
886 | ||
887 | if (! TYPE_HAS_CONSTRUCTOR (basetype)) | |
888 | { | |
889 | if (msgp) | |
890 | *msgp = "type `%s' does not have a constructor"; | |
891 | return error_mark_node; | |
892 | } | |
893 | ||
894 | access = access_public; | |
895 | can_be_private = 0; | |
896 | can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name; | |
897 | ||
898 | parmlist = build_tree_list (NULL_TREE, expr); | |
899 | parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node); | |
900 | ||
901 | if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
902 | { | |
903 | parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes); | |
904 | parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist); | |
905 | } | |
906 | ||
907 | /* The type of the first argument will be filled in inside the loop. */ | |
908 | parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist); | |
909 | parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes); | |
910 | ||
911 | method_name = build_decl_overload (name, parmtypes, 1); | |
912 | ||
913 | /* constructors are up front. */ | |
914 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0); | |
915 | if (TYPE_HAS_DESTRUCTOR (basetype)) | |
916 | fndecl = DECL_CHAIN (fndecl); | |
917 | ||
918 | while (fndecl) | |
919 | { | |
920 | if (DECL_ASSEMBLER_NAME (fndecl) == method_name) | |
921 | { | |
922 | function = fndecl; | |
923 | if (protect) | |
924 | { | |
925 | if (TREE_PRIVATE (fndecl)) | |
926 | { | |
927 | can_be_private = | |
928 | (basetype == current_class_type | |
929 | || is_friend (basetype, current_function_decl) | |
930 | || purpose_member (basetype, DECL_ACCESS (fndecl))); | |
931 | if (! can_be_private) | |
932 | goto found; | |
933 | } | |
934 | else if (TREE_PROTECTED (fndecl)) | |
935 | { | |
936 | if (! can_be_protected) | |
937 | goto found; | |
938 | } | |
939 | } | |
940 | goto found_and_ok; | |
941 | } | |
942 | fndecl = DECL_CHAIN (fndecl); | |
943 | } | |
944 | ||
945 | /* No exact conversion was found. See if an approximate | |
946 | one will do. */ | |
947 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0); | |
948 | if (TYPE_HAS_DESTRUCTOR (basetype)) | |
949 | fndecl = DECL_CHAIN (fndecl); | |
950 | ||
951 | { | |
952 | int saw_private = 0; | |
953 | int saw_protected = 0; | |
954 | struct candidate *candidates = | |
955 | (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate)); | |
956 | struct candidate *cp = candidates; | |
957 | ||
958 | while (fndecl) | |
959 | { | |
960 | function = fndecl; | |
961 | cp->h_len = 2; | |
2986ae00 MS |
962 | cp->harshness = (struct harshness_code *) |
963 | alloca (3 * sizeof (struct harshness_code)); | |
8d08fdba MS |
964 | |
965 | compute_conversion_costs (fndecl, parmlist, cp, 2); | |
2986ae00 | 966 | if ((cp->h.code & EVIL_CODE) == 0) |
8d08fdba MS |
967 | { |
968 | cp->u.field = fndecl; | |
969 | if (protect) | |
970 | { | |
971 | if (TREE_PRIVATE (fndecl)) | |
972 | access = access_private; | |
973 | else if (TREE_PROTECTED (fndecl)) | |
974 | access = access_protected; | |
975 | else | |
976 | access = access_public; | |
977 | } | |
978 | else | |
979 | access = access_public; | |
980 | ||
981 | if (access == access_private | |
982 | ? (basetype == current_class_type | |
983 | || is_friend (basetype, cp->function) | |
984 | || purpose_member (basetype, DECL_ACCESS (fndecl))) | |
985 | : access == access_protected | |
986 | ? (can_be_protected | |
987 | || purpose_member (basetype, DECL_ACCESS (fndecl))) | |
988 | : 1) | |
989 | { | |
2986ae00 | 990 | if (cp->h.code <= TRIVIAL_CODE) |
8d08fdba MS |
991 | goto found_and_ok; |
992 | cp++; | |
993 | } | |
994 | else | |
995 | { | |
996 | if (access == access_private) | |
997 | saw_private = 1; | |
998 | else | |
999 | saw_protected = 1; | |
1000 | } | |
1001 | } | |
1002 | fndecl = DECL_CHAIN (fndecl); | |
1003 | } | |
1004 | if (cp - candidates) | |
1005 | { | |
1006 | /* Rank from worst to best. Then cp will point to best one. | |
1007 | Private fields have their bits flipped. For unsigned | |
1008 | numbers, this should make them look very large. | |
1009 | If the best alternate has a (signed) negative value, | |
1010 | then all we ever saw were private members. */ | |
1011 | if (cp - candidates > 1) | |
1012 | qsort (candidates, /* char *base */ | |
1013 | cp - candidates, /* int nel */ | |
1014 | sizeof (struct candidate), /* int width */ | |
1015 | rank_for_overload); /* int (*compar)() */ | |
1016 | ||
1017 | --cp; | |
2986ae00 | 1018 | if (cp->h.code & EVIL_CODE) |
8d08fdba MS |
1019 | { |
1020 | if (msgp) | |
1021 | *msgp = "ambiguous type conversion possible for `%s'"; | |
1022 | return error_mark_node; | |
1023 | } | |
1024 | ||
1025 | function = cp->function; | |
1026 | fndecl = cp->u.field; | |
1027 | goto found_and_ok; | |
1028 | } | |
1029 | else if (msgp) | |
1030 | { | |
1031 | if (saw_private) | |
1032 | if (saw_protected) | |
1033 | *msgp = "only private and protected conversions apply"; | |
1034 | else | |
1035 | *msgp = "only private conversions apply"; | |
1036 | else if (saw_protected) | |
1037 | *msgp = "only protected conversions apply"; | |
8926095f MS |
1038 | else |
1039 | *msgp = "no appropriate conversion to type `%s'"; | |
8d08fdba MS |
1040 | } |
1041 | return error_mark_node; | |
1042 | } | |
1043 | /* NOTREACHED */ | |
1044 | ||
8d08fdba MS |
1045 | found: |
1046 | if (access == access_private) | |
1047 | if (! can_be_private) | |
1048 | { | |
1049 | if (msgp) | |
1050 | *msgp = TREE_PRIVATE (fndecl) | |
1051 | ? "conversion to type `%s' is private" | |
1052 | : "conversion to type `%s' is from private base class"; | |
1053 | return error_mark_node; | |
1054 | } | |
1055 | if (access == access_protected) | |
1056 | if (! can_be_protected) | |
1057 | { | |
1058 | if (msgp) | |
1059 | *msgp = TREE_PRIVATE (fndecl) | |
1060 | ? "conversion to type `%s' is protected" | |
1061 | : "conversion to type `%s' is from protected base class"; | |
1062 | return error_mark_node; | |
1063 | } | |
1064 | function = fndecl; | |
1065 | found_and_ok: | |
1066 | ||
1067 | /* It will convert, but we don't do anything about it yet. */ | |
1068 | if (msgp == 0) | |
1069 | return NULL_TREE; | |
1070 | ||
1071 | fntype = TREE_TYPE (function); | |
1072 | if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL) | |
1073 | function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); | |
1074 | else | |
1075 | function = default_conversion (function); | |
1076 | ||
1077 | result = build_nt (CALL_EXPR, function, | |
1078 | convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype), | |
1079 | parmlist, NULL_TREE, LOOKUP_NORMAL), | |
1080 | NULL_TREE); | |
1081 | TREE_TYPE (result) = TREE_TYPE (fntype); | |
1082 | TREE_SIDE_EFFECTS (result) = 1; | |
8d08fdba MS |
1083 | return result; |
1084 | } | |
1085 | ||
1086 | /* Call this when we know (for any reason) that expr is not, in fact, | |
1087 | zero. This routine is like convert_pointer_to, but it pays | |
1088 | attention to which specific instance of what type we want to | |
1089 | convert to. This routine should eventually become | |
1090 | convert_to_pointer after all references to convert_to_pointer | |
1091 | are removed. */ | |
1092 | tree | |
1093 | convert_pointer_to_real (binfo, expr) | |
1094 | tree binfo, expr; | |
1095 | { | |
1096 | register tree intype = TREE_TYPE (expr); | |
1097 | tree ptr_type; | |
1098 | tree type, rval; | |
1099 | ||
1100 | if (TREE_CODE (binfo) == TREE_VEC) | |
1101 | type = BINFO_TYPE (binfo); | |
1102 | else if (IS_AGGR_TYPE (binfo)) | |
1103 | { | |
1104 | type = binfo; | |
1105 | } | |
1106 | else | |
1107 | { | |
1108 | type = binfo; | |
1109 | binfo = NULL_TREE; | |
1110 | } | |
1111 | ||
1112 | ptr_type = build_pointer_type (type); | |
1113 | if (ptr_type == TYPE_MAIN_VARIANT (intype)) | |
1114 | return expr; | |
1115 | ||
1116 | if (intype == error_mark_node) | |
1117 | return error_mark_node; | |
1118 | ||
1119 | my_friendly_assert (!integer_zerop (expr), 191); | |
1120 | ||
1121 | if (TREE_CODE (type) == RECORD_TYPE | |
1122 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE | |
1123 | && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype))) | |
1124 | { | |
1125 | tree path; | |
1126 | int distance | |
1127 | = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), | |
1128 | 0, &path); | |
1129 | ||
1130 | /* This function shouldn't be called with unqualified arguments | |
1131 | but if it is, give them an error message that they can read. */ | |
1132 | if (distance < 0) | |
1133 | { | |
7177d104 MS |
1134 | cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'", |
1135 | TREE_TYPE (intype), type); | |
8d08fdba MS |
1136 | |
1137 | if (distance == -2) | |
1138 | cp_error ("because `%T' is an ambiguous base class", type); | |
1139 | return error_mark_node; | |
1140 | } | |
1141 | ||
1142 | return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1); | |
1143 | } | |
1144 | rval = build1 (NOP_EXPR, ptr_type, | |
1145 | TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr); | |
1146 | TREE_CONSTANT (rval) = TREE_CONSTANT (expr); | |
1147 | return rval; | |
1148 | } | |
1149 | ||
1150 | /* Call this when we know (for any reason) that expr is | |
1151 | not, in fact, zero. This routine gets a type out of the first | |
1152 | argument and uses it to search for the type to convert to. If there | |
1153 | is more than one instance of that type in the expr, the conversion is | |
1154 | ambiguous. This routine should eventually go away, and all | |
1155 | callers should use convert_to_pointer_real. */ | |
1156 | tree | |
1157 | convert_pointer_to (binfo, expr) | |
1158 | tree binfo, expr; | |
1159 | { | |
1160 | tree type; | |
1161 | ||
1162 | if (TREE_CODE (binfo) == TREE_VEC) | |
1163 | type = BINFO_TYPE (binfo); | |
1164 | else if (IS_AGGR_TYPE (binfo)) | |
1165 | type = binfo; | |
1166 | else | |
1167 | type = binfo; | |
1168 | return convert_pointer_to_real (type, expr); | |
1169 | } | |
1170 | ||
1171 | /* Same as above, but don't abort if we get an "ambiguous" baseclass. | |
1172 | There's only one virtual baseclass we are looking for, and once | |
1173 | we find one such virtual baseclass, we have found them all. */ | |
1174 | ||
1175 | tree | |
1176 | convert_pointer_to_vbase (binfo, expr) | |
1177 | tree binfo; | |
1178 | tree expr; | |
1179 | { | |
1180 | tree intype = TREE_TYPE (TREE_TYPE (expr)); | |
1181 | tree binfos = TYPE_BINFO_BASETYPES (intype); | |
1182 | int i; | |
1183 | ||
1184 | for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--) | |
1185 | { | |
1186 | tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i)); | |
1187 | if (BINFO_TYPE (binfo) == basetype) | |
1188 | return convert_pointer_to (binfo, expr); | |
1189 | if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype))) | |
1190 | return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr)); | |
1191 | } | |
1192 | my_friendly_abort (6); | |
1193 | /* NOTREACHED */ | |
1194 | return NULL_TREE; | |
1195 | } | |
1196 | \f | |
8d08fdba | 1197 | tree |
a4443a08 | 1198 | cp_convert (type, expr, convtype, flags) |
8d08fdba | 1199 | tree type, expr; |
a4443a08 | 1200 | int convtype, flags; |
8d08fdba MS |
1201 | { |
1202 | register tree e = expr; | |
1203 | register enum tree_code code = TREE_CODE (type); | |
1204 | ||
6060a796 MS |
1205 | if (TREE_CODE (e) == ERROR_MARK |
1206 | || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK) | |
8d08fdba | 1207 | return error_mark_node; |
a4443a08 MS |
1208 | |
1209 | /* Trivial conversion: cv-qualifiers do not matter on rvalues. */ | |
f0e01782 MS |
1210 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e))) |
1211 | return fold (build1 (NOP_EXPR, type, e)); | |
a4443a08 MS |
1212 | |
1213 | if (code == VOID_TYPE && (convtype & CONV_STATIC)) | |
1214 | return build1 (CONVERT_EXPR, type, e); | |
1215 | ||
8d08fdba MS |
1216 | #if 0 |
1217 | /* This is incorrect. A truncation can't be stripped this way. | |
1218 | Extensions will be stripped by the use of get_unwidened. */ | |
f0e01782 MS |
1219 | if (TREE_CODE (e) == NOP_EXPR) |
1220 | return convert (type, TREE_OPERAND (e, 0)); | |
8d08fdba MS |
1221 | #endif |
1222 | ||
1223 | /* Just convert to the type of the member. */ | |
1224 | if (code == OFFSET_TYPE) | |
1225 | { | |
1226 | type = TREE_TYPE (type); | |
1227 | code = TREE_CODE (type); | |
1228 | } | |
1229 | ||
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); | |
1234 | ||
a292b002 MS |
1235 | if (TREE_CODE (e) == OFFSET_REF) |
1236 | e = resolve_offset_ref (e); | |
1237 | ||
a0a33927 MS |
1238 | if (TREE_READONLY_DECL_P (e)) |
1239 | e = decl_constant_value (e); | |
1240 | ||
2986ae00 | 1241 | if (INTEGRAL_CODE_P (code)) |
8d08fdba | 1242 | { |
f0e01782 | 1243 | tree intype = TREE_TYPE (e); |
8d08fdba MS |
1244 | enum tree_code form = TREE_CODE (intype); |
1245 | /* enum = enum, enum = int, enum = float are all errors. */ | |
1246 | if (flag_int_enum_equivalence == 0 | |
1247 | && TREE_CODE (type) == ENUMERAL_TYPE | |
6060a796 MS |
1248 | && ARITHMETIC_TYPE_P (intype) |
1249 | && ! (convtype & CONV_STATIC)) | |
8d08fdba MS |
1250 | { |
1251 | cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type); | |
1252 | ||
1253 | if (flag_pedantic_errors) | |
1254 | return error_mark_node; | |
1255 | } | |
a292b002 | 1256 | if (IS_AGGR_TYPE (intype)) |
8d08fdba MS |
1257 | { |
1258 | tree rval; | |
f0e01782 | 1259 | rval = build_type_conversion (CONVERT_EXPR, type, e, 1); |
00595019 MS |
1260 | if (rval) |
1261 | return rval; | |
1262 | cp_error ("`%#T' used where a `%T' was expected", intype, type); | |
8d08fdba MS |
1263 | return error_mark_node; |
1264 | } | |
2986ae00 | 1265 | if (code == BOOLEAN_TYPE) |
28ed4616 | 1266 | return truthvalue_conversion (e); |
8d08fdba MS |
1267 | return fold (convert_to_integer (type, e)); |
1268 | } | |
1269 | if (code == POINTER_TYPE) | |
1270 | return fold (cp_convert_to_pointer (type, e)); | |
1271 | if (code == REAL_TYPE) | |
1272 | { | |
1273 | if (IS_AGGR_TYPE (TREE_TYPE (e))) | |
1274 | { | |
1275 | tree rval; | |
1276 | rval = build_type_conversion (CONVERT_EXPR, type, e, 1); | |
1277 | if (rval) | |
1278 | return rval; | |
1279 | else | |
1280 | cp_error ("`%#T' used where a floating point value was expected", | |
1281 | TREE_TYPE (e)); | |
1282 | } | |
1283 | return fold (convert_to_real (type, e)); | |
1284 | } | |
1285 | ||
1286 | /* New C++ semantics: since assignment is now based on | |
1287 | memberwise copying, if the rhs type is derived from the | |
1288 | lhs type, then we may still do a conversion. */ | |
1289 | if (IS_AGGR_TYPE_CODE (code)) | |
1290 | { | |
1291 | tree dtype = TREE_TYPE (e); | |
db5ae43f MS |
1292 | tree ctor = NULL_TREE; |
1293 | tree conversion = NULL_TREE; | |
8d08fdba | 1294 | |
8d08fdba MS |
1295 | dtype = TYPE_MAIN_VARIANT (dtype); |
1296 | ||
1297 | /* Conversion of object pointers or signature pointers/references | |
1298 | to signature pointers/references. */ | |
1299 | ||
1300 | if (TYPE_LANG_SPECIFIC (type) | |
1301 | && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))) | |
1302 | { | |
1303 | tree constructor = build_signature_pointer_constructor (type, expr); | |
1304 | tree sig_ty = SIGNATURE_TYPE (type); | |
1305 | tree sig_ptr; | |
1306 | ||
1307 | if (constructor == error_mark_node) | |
1308 | return error_mark_node; | |
1309 | ||
1310 | sig_ptr = get_temp_name (type, 1); | |
1311 | DECL_INITIAL (sig_ptr) = constructor; | |
1312 | CLEAR_SIGNATURE (sig_ty); | |
6060a796 | 1313 | finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0); |
8d08fdba MS |
1314 | SET_SIGNATURE (sig_ty); |
1315 | TREE_READONLY (sig_ptr) = 1; | |
1316 | ||
1317 | return sig_ptr; | |
1318 | } | |
1319 | ||
1320 | /* Conversion between aggregate types. New C++ semantics allow | |
1321 | objects of derived type to be cast to objects of base type. | |
1322 | Old semantics only allowed this between pointers. | |
1323 | ||
1324 | There may be some ambiguity between using a constructor | |
1325 | vs. using a type conversion operator when both apply. */ | |
1326 | ||
db5ae43f MS |
1327 | if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype) |
1328 | && TYPE_HAS_CONVERSION (dtype)) | |
1329 | conversion = build_type_conversion (CONVERT_EXPR, type, e, 1); | |
8d08fdba | 1330 | |
db5ae43f MS |
1331 | if (conversion == error_mark_node) |
1332 | { | |
1333 | error ("ambiguous pointer conversion"); | |
1334 | return conversion; | |
1335 | } | |
8d08fdba | 1336 | |
db5ae43f MS |
1337 | if (TYPE_HAS_CONSTRUCTOR (type)) |
1338 | ctor = build_method_call (NULL_TREE, constructor_name_full (type), | |
1339 | build_tree_list (NULL_TREE, e), | |
1340 | TYPE_BINFO (type), | |
1341 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY | |
6060a796 | 1342 | | (convtype&CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING) |
db5ae43f | 1343 | | (conversion ? LOOKUP_NO_CONVERSION : 0)); |
8d08fdba | 1344 | |
db5ae43f MS |
1345 | if (ctor == error_mark_node) |
1346 | { | |
1347 | cp_error ("in conversion to type `%T'", type); | |
8d08fdba MS |
1348 | return error_mark_node; |
1349 | } | |
db5ae43f MS |
1350 | |
1351 | if (conversion && ctor) | |
8d08fdba | 1352 | { |
db5ae43f MS |
1353 | error ("both constructor and type conversion operator apply"); |
1354 | return error_mark_node; | |
1355 | } | |
1356 | else if (conversion) | |
1357 | return conversion; | |
1358 | else if (ctor) | |
1359 | { | |
1360 | if (current_function_decl) | |
1361 | /* We can't pass 1 to the with_cleanup_p arg here, because that | |
1362 | screws up passing classes by value. */ | |
1363 | ctor = build_cplus_new (type, ctor, 0); | |
1364 | else | |
8d08fdba | 1365 | { |
db5ae43f MS |
1366 | register tree parm = TREE_OPERAND (ctor, 1); |
1367 | ||
1368 | /* Initializers for static variables and parameters | |
1369 | have to handle doing the initialization and | |
1370 | cleanup themselves. */ | |
1371 | my_friendly_assert (TREE_CODE (ctor) == CALL_EXPR, 322); | |
1372 | #if 0 | |
1373 | /* The following assertion fails in cases where we | |
1374 | are initializing a static member variable of a | |
1375 | particular instance of a template class with a | |
1376 | call to a constructor of the given instance, as | |
1377 | in: | |
1378 | ||
1379 | TMPL<int> object = TMPL<int>(); | |
1380 | ||
1381 | Curiously, the assertion does not fail if we do | |
1382 | the same thing for a static member of a | |
1383 | non-template class, as in: | |
1384 | ||
1385 | T object = T(); | |
1386 | ||
1387 | I can't see why we should care here whether or not | |
1388 | the initializer expression involves a call to | |
1389 | `new', so for the time being, it seems best to | |
1390 | just avoid doing this assertion. */ | |
1391 | my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)), | |
1392 | 323); | |
1393 | #endif | |
1394 | TREE_VALUE (parm) = NULL_TREE; | |
1395 | ctor = build_indirect_ref (ctor, NULL_PTR); | |
1396 | TREE_HAS_CONSTRUCTOR (ctor) = 1; | |
8d08fdba | 1397 | } |
db5ae43f | 1398 | return ctor; |
8d08fdba MS |
1399 | } |
1400 | } | |
1401 | ||
f0e01782 | 1402 | /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack, |
8d08fdba MS |
1403 | then the it won't be hashed and hence compare as not equal, |
1404 | even when it is. */ | |
1405 | if (code == ARRAY_TYPE | |
f0e01782 MS |
1406 | && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type) |
1407 | && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type))) | |
1408 | return e; | |
8d08fdba MS |
1409 | |
1410 | cp_error ("conversion from `%T' to non-scalar type `%T' requested", | |
1411 | TREE_TYPE (expr), type); | |
1412 | return error_mark_node; | |
1413 | } | |
1414 | ||
a4443a08 MS |
1415 | /* Create an expression whose value is that of EXPR, |
1416 | converted to type TYPE. The TREE_TYPE of the value | |
1417 | is always TYPE. This function implements all reasonable | |
1418 | conversions; callers should filter out those that are | |
1419 | not permitted by the language being compiled. */ | |
1420 | ||
1421 | tree | |
1422 | convert (type, expr) | |
1423 | tree type, expr; | |
1424 | { | |
1425 | return cp_convert (type, expr, CONV_OLD_CONVERT, 0); | |
1426 | } | |
1427 | ||
8d08fdba MS |
1428 | /* Like convert, except permit conversions to take place which |
1429 | are not normally allowed due to access restrictions | |
1430 | (such as conversion from sub-type to private super-type). */ | |
1431 | tree | |
6060a796 | 1432 | convert_force (type, expr, convtype) |
8d08fdba MS |
1433 | tree type; |
1434 | tree expr; | |
6060a796 | 1435 | int convtype; |
8d08fdba MS |
1436 | { |
1437 | register tree e = expr; | |
1438 | register enum tree_code code = TREE_CODE (type); | |
1439 | ||
1440 | if (code == REFERENCE_TYPE) | |
a4443a08 MS |
1441 | return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN, |
1442 | NULL_TREE)); | |
8d08fdba MS |
1443 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) |
1444 | e = convert_from_reference (e); | |
1445 | ||
1446 | if (code == POINTER_TYPE) | |
1447 | return fold (convert_to_pointer_force (type, e)); | |
1448 | ||
51c184be | 1449 | /* From typeck.c convert_for_assignment */ |
8d08fdba MS |
1450 | if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR |
1451 | && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE | |
1452 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) | |
51c184be MS |
1453 | || integer_zerop (e) |
1454 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (e))) | |
8d08fdba MS |
1455 | && TYPE_PTRMEMFUNC_P (type)) |
1456 | { | |
1457 | /* compatible pointer to member functions. */ | |
51c184be | 1458 | return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1); |
8d08fdba | 1459 | } |
6060a796 MS |
1460 | |
1461 | return cp_convert (type, e, CONV_OLD_CONVERT|convtype, 0); | |
8d08fdba MS |
1462 | } |
1463 | ||
1464 | /* Subroutine of build_type_conversion. */ | |
1465 | static tree | |
1466 | build_type_conversion_1 (xtype, basetype, expr, typename, for_sure) | |
1467 | tree xtype, basetype; | |
1468 | tree expr; | |
1469 | tree typename; | |
1470 | int for_sure; | |
1471 | { | |
8d08fdba MS |
1472 | tree rval; |
1473 | int flags; | |
1474 | ||
1475 | if (for_sure == 0) | |
db5ae43f | 1476 | flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING; |
8d08fdba | 1477 | else |
db5ae43f | 1478 | flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING; |
8d08fdba | 1479 | |
21474714 | 1480 | rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags); |
8d08fdba MS |
1481 | if (rval == error_mark_node) |
1482 | { | |
1483 | if (for_sure == 0) | |
1484 | return NULL_TREE; | |
1485 | return error_mark_node; | |
1486 | } | |
8d08fdba MS |
1487 | if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE |
1488 | && TREE_CODE (xtype) != REFERENCE_TYPE) | |
1489 | rval = default_conversion (rval); | |
1490 | ||
1491 | if (warn_cast_qual | |
1492 | && TREE_TYPE (xtype) | |
1493 | && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval))) | |
1494 | > TREE_READONLY (TREE_TYPE (xtype)))) | |
1495 | warning ("user-defined conversion casting away `const'"); | |
a4443a08 | 1496 | return convert (xtype, rval); |
8d08fdba MS |
1497 | } |
1498 | ||
1499 | /* Convert an aggregate EXPR to type XTYPE. If a conversion | |
1500 | exists, return the attempted conversion. This may | |
1501 | return ERROR_MARK_NODE if the conversion is not | |
1502 | allowed (references private members, etc). | |
1503 | If no conversion exists, NULL_TREE is returned. | |
1504 | ||
1505 | If (FOR_SURE & 1) is non-zero, then we allow this type conversion | |
1506 | to take place immediately. Otherwise, we build a SAVE_EXPR | |
e1cd6e56 | 1507 | which can be evaluated if the results are ever needed. */ |
8d08fdba | 1508 | |
8d08fdba MS |
1509 | tree |
1510 | build_type_conversion (code, xtype, expr, for_sure) | |
1511 | enum tree_code code; | |
1512 | tree xtype, expr; | |
1513 | int for_sure; | |
1514 | { | |
1515 | /* C++: check to see if we can convert this aggregate type | |
e1cd6e56 MS |
1516 | into the required type. */ |
1517 | tree basetype; | |
1518 | tree conv; | |
1519 | tree winner = NULL_TREE; | |
8d08fdba MS |
1520 | |
1521 | if (expr == error_mark_node) | |
1522 | return error_mark_node; | |
1523 | ||
1524 | basetype = TREE_TYPE (expr); | |
1525 | if (TREE_CODE (basetype) == REFERENCE_TYPE) | |
1526 | basetype = TREE_TYPE (basetype); | |
1527 | ||
1528 | basetype = TYPE_MAIN_VARIANT (basetype); | |
1529 | if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype)) | |
1530 | return NULL_TREE; | |
1531 | ||
e1cd6e56 MS |
1532 | /* Do we have an exact match? */ |
1533 | { | |
1534 | tree typename = build_typename_overload (xtype); | |
1535 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1536 | return build_type_conversion_1 (xtype, basetype, expr, typename, | |
1537 | for_sure); | |
1538 | } | |
8d08fdba | 1539 | |
e1cd6e56 MS |
1540 | /* Nope; try looking for others. */ |
1541 | for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv)) | |
8d08fdba | 1542 | { |
e1cd6e56 MS |
1543 | if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv)) |
1544 | continue; | |
8d08fdba | 1545 | |
e1cd6e56 | 1546 | if (can_convert (xtype, TREE_VALUE (conv))) |
8d08fdba | 1547 | { |
e1cd6e56 | 1548 | if (winner) |
8d08fdba | 1549 | { |
e1cd6e56 MS |
1550 | cp_error ("ambiguous conversion from `%T' to `%T'", basetype, |
1551 | xtype); | |
1552 | cp_error (" candidate conversion functions include `%T' and `%T'", | |
1553 | TREE_VALUE (winner), TREE_VALUE (conv)); | |
1554 | return NULL_TREE; | |
8d08fdba MS |
1555 | } |
1556 | else | |
e1cd6e56 | 1557 | winner = conv; |
8d08fdba MS |
1558 | } |
1559 | } | |
1560 | ||
e1cd6e56 MS |
1561 | if (winner) |
1562 | return build_type_conversion_1 (xtype, basetype, expr, | |
1563 | TREE_PURPOSE (winner), for_sure); | |
8d08fdba MS |
1564 | |
1565 | return NULL_TREE; | |
1566 | } | |
1567 | ||
1568 | /* Must convert two aggregate types to non-aggregate type. | |
1569 | Attempts to find a non-ambiguous, "best" type conversion. | |
1570 | ||
1571 | Return 1 on success, 0 on failure. | |
1572 | ||
1573 | @@ What are the real semantics of this supposed to be??? */ | |
1574 | int | |
1575 | build_default_binary_type_conversion (code, arg1, arg2) | |
1576 | enum tree_code code; | |
1577 | tree *arg1, *arg2; | |
1578 | { | |
1579 | tree type1 = TREE_TYPE (*arg1); | |
1580 | tree type2 = TREE_TYPE (*arg2); | |
1581 | ||
1582 | if (TREE_CODE (type1) == REFERENCE_TYPE | |
1583 | || TREE_CODE (type1) == POINTER_TYPE) | |
1584 | type1 = TREE_TYPE (type1); | |
1585 | if (TREE_CODE (type2) == REFERENCE_TYPE | |
1586 | || TREE_CODE (type2) == POINTER_TYPE) | |
1587 | type2 = TREE_TYPE (type2); | |
1588 | ||
1589 | if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL) | |
1590 | { | |
1591 | tree decl = typedecl_for_tag (type1); | |
1592 | if (decl) | |
1593 | error ("type conversion nonexistent for type `%s'", | |
1594 | IDENTIFIER_POINTER (DECL_NAME (decl))); | |
1595 | else | |
1596 | error ("type conversion nonexistent for non-C++ type"); | |
1597 | return 0; | |
1598 | } | |
1599 | if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL) | |
1600 | { | |
1601 | tree decl = typedecl_for_tag (type2); | |
1602 | if (decl) | |
1603 | error ("type conversion nonexistent for type `%s'", | |
1604 | IDENTIFIER_POINTER (decl)); | |
1605 | else | |
1606 | error ("type conversion nonexistent for non-C++ type"); | |
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1)) | |
1611 | { | |
1612 | if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2)) | |
1613 | cp_error ("no conversion from `%T' and `%T' to types with default `%O' ", | |
1614 | type1, type2, code); | |
1615 | else | |
1616 | cp_error ("no conversion from `%T' to type with default `%O'", | |
1617 | type1, code); | |
1618 | return 0; | |
1619 | } | |
1620 | else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2)) | |
1621 | { | |
1622 | cp_error ("no conversion from `%T' to type with default `%O'", | |
1623 | type2, code); | |
1624 | return 0; | |
1625 | } | |
1626 | ||
2986ae00 MS |
1627 | if (code == TRUTH_ANDIF_EXPR |
1628 | || code == TRUTH_ORIF_EXPR) | |
8d08fdba | 1629 | { |
28ed4616 JM |
1630 | *arg1 = convert (boolean_type_node, *arg1); |
1631 | *arg2 = convert (boolean_type_node, *arg2); | |
2986ae00 MS |
1632 | } |
1633 | else if (TYPE_HAS_INT_CONVERSION (type1)) | |
1634 | { | |
1635 | if (TYPE_HAS_REAL_CONVERSION (type1)) | |
1636 | cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int", | |
1637 | type1); | |
8d08fdba MS |
1638 | *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1); |
1639 | *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1); | |
1640 | } | |
1641 | else if (TYPE_HAS_REAL_CONVERSION (type1)) | |
1642 | { | |
1643 | *arg1 = build_type_conversion (code, double_type_node, *arg1, 1); | |
1644 | *arg2 = build_type_conversion (code, double_type_node, *arg2, 1); | |
1645 | } | |
1646 | else | |
1647 | { | |
1648 | *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1); | |
1649 | if (*arg1 == error_mark_node) | |
1650 | error ("ambiguous pointer conversion"); | |
1651 | *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1); | |
1652 | if (*arg1 != error_mark_node && *arg2 == error_mark_node) | |
1653 | error ("ambiguous pointer conversion"); | |
1654 | } | |
1655 | if (*arg1 == 0) | |
1656 | { | |
1657 | if (*arg2 == 0 && type1 != type2) | |
1658 | cp_error ("default type conversion for types `%T' and `%T' failed", | |
1659 | type1, type2); | |
1660 | else | |
1661 | cp_error ("default type conversion for type `%T' failed", type1); | |
1662 | return 0; | |
1663 | } | |
1664 | else if (*arg2 == 0) | |
1665 | { | |
1666 | cp_error ("default type conversion for type `%T' failed", type2); | |
1667 | return 0; | |
1668 | } | |
1669 | return 1; | |
1670 | } | |
1671 | ||
2986ae00 | 1672 | /* Must convert an aggregate type to non-aggregate type. |
8d08fdba MS |
1673 | Attempts to find a non-ambiguous, "best" type conversion. |
1674 | ||
1675 | Return 1 on success, 0 on failure. | |
1676 | ||
1677 | The type of the argument is expected to be of aggregate type here. | |
1678 | ||
1679 | @@ What are the real semantics of this supposed to be??? */ | |
1680 | int | |
1681 | build_default_unary_type_conversion (code, arg) | |
1682 | enum tree_code code; | |
1683 | tree *arg; | |
1684 | { | |
1685 | tree type = TREE_TYPE (*arg); | |
8d08fdba MS |
1686 | |
1687 | if (! TYPE_HAS_CONVERSION (type)) | |
1688 | { | |
2986ae00 | 1689 | cp_error ("type conversion required for type `%T'", type); |
8d08fdba MS |
1690 | return 0; |
1691 | } | |
1692 | ||
2986ae00 | 1693 | if (code == TRUTH_NOT_EXPR) |
28ed4616 | 1694 | *arg = convert (boolean_type_node, *arg); |
2986ae00 MS |
1695 | else if (TYPE_HAS_INT_CONVERSION (type)) |
1696 | { | |
1697 | if (TYPE_HAS_REAL_CONVERSION (type)) | |
1698 | cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int", | |
1699 | type); | |
1700 | *arg = build_type_conversion (code, integer_type_node, *arg, 1); | |
1701 | } | |
8d08fdba MS |
1702 | else if (TYPE_HAS_REAL_CONVERSION (type)) |
1703 | *arg = build_type_conversion (code, double_type_node, *arg, 1); | |
1704 | else | |
1705 | { | |
1706 | *arg = build_type_conversion (code, ptr_type_node, *arg, 1); | |
1707 | if (*arg == error_mark_node) | |
1708 | error ("ambiguous pointer conversion"); | |
1709 | } | |
1710 | if (*arg == NULL_TREE) | |
1711 | { | |
2986ae00 | 1712 | cp_error ("default type conversion for type `%T' failed", type); |
8d08fdba MS |
1713 | return 0; |
1714 | } | |
1715 | return 1; | |
1716 | } | |
39211cd5 MS |
1717 | |
1718 | /* Implements integral promotion (4.1) and float->double promotion. */ | |
1719 | tree | |
1720 | type_promotes_to (type) | |
1721 | tree type; | |
1722 | { | |
1723 | int constp = TYPE_READONLY (type); | |
1724 | int volatilep = TYPE_VOLATILE (type); | |
1725 | type = TYPE_MAIN_VARIANT (type); | |
2986ae00 MS |
1726 | |
1727 | /* bool always promotes to int (not unsigned), even if it's the same | |
1728 | size. */ | |
28ed4616 | 1729 | if (type == boolean_type_node) |
2986ae00 MS |
1730 | type = integer_type_node; |
1731 | ||
1732 | /* Normally convert enums to int, but convert wide enums to something | |
1733 | wider. */ | |
1734 | else if (TREE_CODE (type) == ENUMERAL_TYPE | |
1735 | || type == wchar_type_node) | |
cf2ac46f JM |
1736 | { |
1737 | int precision = MAX (TYPE_PRECISION (type), | |
1738 | TYPE_PRECISION (integer_type_node)); | |
1739 | tree totype = type_for_size (precision, 0); | |
1740 | if (TREE_UNSIGNED (type) | |
1741 | && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) | |
1742 | type = type_for_size (precision, 1); | |
1743 | else | |
1744 | type = totype; | |
1745 | } | |
39211cd5 MS |
1746 | else if (C_PROMOTING_INTEGER_TYPE_P (type)) |
1747 | { | |
1748 | /* Traditionally, unsignedness is preserved in default promotions. | |
1749 | Otherwise, retain unsignedness if really not getting bigger. */ | |
1750 | if (TREE_UNSIGNED (type) | |
1751 | && (flag_traditional | |
1752 | || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
1753 | type = unsigned_type_node; | |
1754 | else | |
1755 | type = integer_type_node; | |
1756 | } | |
1757 | else if (type == float_type_node) | |
1758 | type = double_type_node; | |
1759 | ||
f376e137 | 1760 | return cp_build_type_variant (type, constp, volatilep); |
39211cd5 | 1761 | } |