]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Language-level data type conversion for GNU C++. |
2 | Copyright (C) 1987, 1988, 1992, 1993 Free Software Foundation, Inc. | |
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 | ||
189 | if (form == INTEGER_TYPE || form == ENUMERAL_TYPE) | |
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; | |
288 | tree argtype = TREE_TYPE (arg), basetype = argtype; | |
289 | tree target_type = TREE_TYPE (type); | |
290 | tree binfo = NULL_TREE; | |
291 | ||
292 | my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187); | |
293 | if (flags != 0 | |
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); | |
299 | if ((flags & LOOKUP_PROTECT) && binfo == error_mark_node) | |
300 | return error_mark_node; | |
301 | if (binfo == NULL_TREE) | |
302 | return error_not_base_type (target_type, argtype); | |
303 | basetype = BINFO_TYPE (binfo); | |
304 | } | |
305 | ||
306 | /* Pass along const and volatile down into the type. */ | |
307 | if (TYPE_READONLY (type) || TYPE_VOLATILE (type)) | |
308 | target_type = build_type_variant (target_type, TYPE_READONLY (type), | |
309 | TYPE_VOLATILE (type)); | |
310 | targ = arg; | |
311 | if (TREE_CODE (targ) == SAVE_EXPR) | |
312 | targ = TREE_OPERAND (targ, 0); | |
313 | ||
314 | switch (TREE_CODE (targ)) | |
315 | { | |
316 | case INDIRECT_REF: | |
317 | /* This is a call to a constructor which did not know what it was | |
318 | initializing until now: it needs to initialize a temporary. */ | |
319 | if (TREE_HAS_CONSTRUCTOR (targ)) | |
320 | { | |
321 | tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1); | |
322 | TREE_HAS_CONSTRUCTOR (targ) = 0; | |
323 | return build_up_reference (type, temp, flags, 1); | |
324 | } | |
325 | /* Let &* cancel out to simplify resulting code. | |
326 | Also, throw away intervening NOP_EXPRs. */ | |
327 | arg = TREE_OPERAND (targ, 0); | |
328 | if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR | |
329 | || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg))) | |
330 | arg = TREE_OPERAND (arg, 0); | |
331 | ||
332 | /* in doing a &*, we have to get rid of the const'ness on the pointer | |
333 | value. Haven't thought about volatile here. Pointers come to mind | |
334 | here. */ | |
335 | if (TREE_READONLY (arg)) | |
336 | { | |
337 | arg = copy_node (arg); | |
338 | TREE_READONLY (arg) = 0; | |
339 | } | |
340 | ||
341 | rval = build1 (CONVERT_EXPR, type, arg); | |
342 | TREE_REFERENCE_EXPR (rval) = 1; | |
343 | ||
344 | /* propagate the const flag on something like: | |
345 | ||
346 | class Base { | |
347 | public: | |
348 | int foo; | |
349 | }; | |
350 | ||
351 | class Derived : public Base { | |
352 | public: | |
353 | int bar; | |
354 | }; | |
355 | ||
356 | void func(Base&); | |
357 | ||
358 | void func2(const Derived& d) { | |
359 | func(d); | |
360 | } | |
361 | ||
362 | on the d parameter. The below could have been avoided, if the flags | |
363 | were down in the tree, not sure why they are not. (mrs) */ | |
364 | /* The below code may have to be propagated to other parts of this | |
365 | switch. */ | |
366 | if (TREE_READONLY (targ) && !TREE_READONLY (arg) | |
367 | && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL) | |
368 | && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE | |
369 | && (TYPE_READONLY (target_type) && checkconst)) | |
370 | { | |
371 | arg = copy_node (arg); | |
372 | TREE_READONLY (arg) = TREE_READONLY (targ); | |
373 | } | |
374 | literal_flag = TREE_CONSTANT (arg); | |
375 | ||
376 | goto done_but_maybe_warn; | |
377 | ||
378 | /* Get this out of a register if we happened to be in one by accident. | |
379 | Also, build up references to non-lvalues it we must. */ | |
380 | /* For &x[y], return (&) x+y */ | |
381 | case ARRAY_REF: | |
382 | if (mark_addressable (TREE_OPERAND (targ, 0)) == 0) | |
383 | return error_mark_node; | |
384 | rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0), | |
385 | TREE_OPERAND (targ, 1), 1); | |
386 | TREE_TYPE (rval) = type; | |
387 | if (TREE_CONSTANT (TREE_OPERAND (targ, 1)) | |
388 | && staticp (TREE_OPERAND (targ, 0))) | |
389 | TREE_CONSTANT (rval) = 1; | |
390 | goto done; | |
391 | ||
392 | case SCOPE_REF: | |
393 | /* Could be a reference to a static member. */ | |
394 | { | |
395 | tree field = TREE_OPERAND (targ, 1); | |
396 | if (TREE_STATIC (field)) | |
397 | { | |
398 | rval = build1 (ADDR_EXPR, type, field); | |
399 | literal_flag = 1; | |
400 | goto done; | |
401 | } | |
402 | } | |
403 | ||
404 | /* We should have farmed out member pointers above. */ | |
405 | my_friendly_abort (188); | |
406 | ||
407 | case COMPONENT_REF: | |
408 | rval = build_component_addr (targ, build_pointer_type (argtype), | |
409 | "attempt to make a reference to bit-field structure member `%s'"); | |
410 | TREE_TYPE (rval) = type; | |
411 | literal_flag = staticp (TREE_OPERAND (targ, 0)); | |
412 | ||
413 | goto done_but_maybe_warn; | |
414 | ||
415 | /* Anything not already handled and not a true memory reference | |
416 | needs to have a reference built up. Do so silently for | |
417 | things like integers and return values from function, | |
418 | but complain if we need a reference to something declared | |
419 | as `register'. */ | |
420 | ||
421 | case RESULT_DECL: | |
422 | if (staticp (targ)) | |
423 | literal_flag = 1; | |
424 | TREE_ADDRESSABLE (targ) = 1; | |
425 | put_var_into_stack (targ); | |
426 | break; | |
427 | ||
428 | case PARM_DECL: | |
429 | if (targ == current_class_decl) | |
430 | { | |
431 | error ("address of `this' not available"); | |
432 | #if 0 | |
433 | /* This code makes the following core dump the compiler on a sun4, | |
434 | if the code below is used. | |
435 | ||
436 | class e_decl; | |
437 | class a_decl; | |
438 | typedef a_decl* a_ref; | |
439 | ||
440 | class a_s { | |
441 | public: | |
442 | a_s(); | |
443 | void* append(a_ref& item); | |
444 | }; | |
445 | class a_decl { | |
446 | public: | |
447 | a_decl (e_decl *parent); | |
448 | a_s generic_s; | |
449 | a_s decls; | |
450 | e_decl* parent; | |
451 | }; | |
452 | ||
453 | class e_decl { | |
454 | public: | |
455 | e_decl(); | |
456 | a_s implementations; | |
457 | }; | |
458 | ||
459 | void foobar(void *); | |
460 | ||
461 | a_decl::a_decl(e_decl *parent) { | |
462 | parent->implementations.append(this); | |
463 | } | |
464 | */ | |
465 | ||
466 | TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */ | |
467 | put_var_into_stack (targ); | |
468 | break; | |
469 | #else | |
470 | return error_mark_node; | |
471 | #endif | |
472 | } | |
473 | /* Fall through. */ | |
474 | case VAR_DECL: | |
475 | case CONST_DECL: | |
476 | if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)) | |
477 | warning ("address needed to build reference for `%s', which is declared `register'", | |
478 | IDENTIFIER_POINTER (DECL_NAME (targ))); | |
479 | else if (staticp (targ)) | |
480 | literal_flag = 1; | |
481 | ||
482 | TREE_ADDRESSABLE (targ) = 1; | |
483 | put_var_into_stack (targ); | |
484 | break; | |
485 | ||
486 | case COMPOUND_EXPR: | |
487 | { | |
488 | tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1), | |
489 | LOOKUP_PROTECT, checkconst); | |
490 | rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference); | |
491 | TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1)); | |
492 | return rval; | |
493 | } | |
494 | ||
495 | case MODIFY_EXPR: | |
496 | case INIT_EXPR: | |
497 | { | |
498 | tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0), | |
499 | LOOKUP_PROTECT, checkconst); | |
500 | rval = build (COMPOUND_EXPR, type, arg, real_reference); | |
501 | TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0)); | |
502 | return rval; | |
503 | } | |
504 | ||
505 | case COND_EXPR: | |
506 | return build (COND_EXPR, type, | |
507 | TREE_OPERAND (targ, 0), | |
508 | build_up_reference (type, TREE_OPERAND (targ, 1), | |
509 | LOOKUP_PROTECT, checkconst), | |
510 | build_up_reference (type, TREE_OPERAND (targ, 2), | |
511 | LOOKUP_PROTECT, checkconst)); | |
512 | ||
513 | case WITH_CLEANUP_EXPR: | |
514 | return build (WITH_CLEANUP_EXPR, type, | |
515 | build_up_reference (type, TREE_OPERAND (targ, 0), | |
516 | LOOKUP_PROTECT, checkconst), | |
517 | 0, TREE_OPERAND (targ, 2)); | |
518 | ||
519 | case BIND_EXPR: | |
520 | arg = TREE_OPERAND (targ, 1); | |
521 | if (arg == NULL_TREE) | |
522 | { | |
523 | compiler_error ("({ ... }) expression not expanded when needed for reference"); | |
524 | return error_mark_node; | |
525 | } | |
526 | rval = build1 (ADDR_EXPR, type, arg); | |
527 | TREE_REFERENCE_EXPR (rval) = 1; | |
528 | return rval; | |
529 | ||
530 | default: | |
531 | break; | |
532 | } | |
533 | ||
534 | if (TREE_ADDRESSABLE (targ) == 0) | |
535 | { | |
536 | tree temp; | |
537 | ||
538 | if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype)) | |
539 | { | |
540 | temp = build_cplus_new (argtype, targ, 1); | |
541 | rval = build1 (ADDR_EXPR, type, temp); | |
542 | goto done; | |
543 | } | |
544 | else | |
545 | { | |
546 | temp = get_temp_name (argtype, 0); | |
547 | if (global_bindings_p ()) | |
548 | { | |
549 | /* Give this new temp some rtl and initialize it. */ | |
550 | DECL_INITIAL (temp) = targ; | |
551 | TREE_STATIC (temp) = 1; | |
552 | finish_decl (temp, targ, NULL_TREE, 0); | |
553 | /* Do this after declaring it static. */ | |
554 | rval = build_unary_op (ADDR_EXPR, temp, 0); | |
555 | TREE_TYPE (rval) = type; | |
556 | literal_flag = TREE_CONSTANT (rval); | |
557 | goto done; | |
558 | } | |
559 | else | |
560 | { | |
561 | rval = build_unary_op (ADDR_EXPR, temp, 0); | |
562 | if (binfo && !BINFO_OFFSET_ZEROP (binfo)) | |
563 | rval = convert_pointer_to (target_type, rval); | |
564 | else | |
565 | TREE_TYPE (rval) = type; | |
566 | ||
567 | temp = build (MODIFY_EXPR, argtype, temp, arg); | |
568 | TREE_SIDE_EFFECTS (temp) = 1; | |
569 | return build (COMPOUND_EXPR, type, temp, rval); | |
570 | } | |
571 | } | |
572 | } | |
573 | else | |
574 | rval = build1 (ADDR_EXPR, type, arg); | |
575 | ||
576 | done_but_maybe_warn: | |
577 | if (checkconst && TREE_READONLY (arg) && ! TYPE_READONLY (target_type)) | |
578 | readonly_error (arg, "conversion to reference", 1); | |
579 | ||
580 | done: | |
581 | if (TYPE_USES_COMPLEX_INHERITANCE (argtype)) | |
582 | { | |
583 | TREE_TYPE (rval) = TYPE_POINTER_TO (argtype); | |
584 | rval = convert_pointer_to (target_type, rval); | |
585 | TREE_TYPE (rval) = type; | |
586 | } | |
587 | TREE_CONSTANT (rval) = literal_flag; | |
588 | return rval; | |
589 | } | |
590 | ||
591 | /* For C++: Only need to do one-level references, but cannot | |
592 | get tripped up on signed/unsigned differences. | |
593 | ||
594 | If DECL is NULL_TREE it means convert as though casting (by force). | |
595 | If it is ERROR_MARK_NODE, it means the conversion is implicit, | |
596 | and that temporaries may be created. | |
597 | Make sure the use of user-defined conversion operators is un-ambiguous. | |
598 | Otherwise, DECL is a _DECL node which can be used in error reporting. | |
599 | ||
600 | FNDECL, PARMNUM, and ERRTYPE are only used when checking for use of | |
601 | volatile or const references where they aren't desired. */ | |
602 | ||
603 | tree | |
604 | convert_to_reference (decl, reftype, expr, fndecl, parmnum, | |
605 | errtype, strict, flags) | |
606 | tree decl; | |
607 | tree reftype, expr; | |
608 | tree fndecl; | |
609 | int parmnum; | |
610 | char *errtype; | |
611 | int strict, flags; | |
612 | { | |
613 | register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype)); | |
614 | register tree intype = TREE_TYPE (expr); | |
615 | register enum tree_code form = TREE_CODE (intype); | |
616 | tree rval = NULL_TREE; | |
617 | ||
618 | if (TREE_CODE(type) == ARRAY_TYPE) | |
619 | type = build_pointer_type (TREE_TYPE(type)); | |
620 | if (form == REFERENCE_TYPE) | |
621 | intype = TREE_TYPE (intype); | |
622 | intype = TYPE_MAIN_VARIANT (intype); | |
623 | ||
624 | if (IS_AGGR_TYPE (intype) | |
625 | && (rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1))) | |
626 | { | |
627 | if (rval == error_mark_node) | |
628 | cp_error ("conversion from `%T' to `%T' is ambiguous", | |
629 | intype, reftype); | |
630 | return rval; | |
631 | } | |
632 | ||
633 | if (comptypes (type, intype, strict)) | |
634 | { | |
635 | /* Section 13. */ | |
636 | if (flags & LOOKUP_COMPLAIN) | |
637 | { | |
638 | /* Since convert_for_initialization didn't call convert_for_assignment, | |
639 | we have to do this checking here. FIXME: We should have a common | |
640 | routine between here and convert_for_assignment. */ | |
641 | if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE) | |
642 | { | |
643 | register tree ttl = TREE_TYPE (reftype); | |
644 | register tree ttr = TREE_TYPE (TREE_TYPE (expr)); | |
645 | ||
646 | if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) | |
647 | { | |
648 | if (fndecl) | |
649 | cp_pedwarn ("passing `%T' as argument %P of `%D' discards const", | |
650 | TREE_TYPE (expr), parmnum, fndecl); | |
651 | else | |
652 | cp_pedwarn ("%s to `%T' from `%T' discards const", | |
653 | errtype, reftype, TREE_TYPE (expr)); | |
654 | } | |
655 | if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) | |
656 | { | |
657 | if (fndecl) | |
658 | cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile", | |
659 | TREE_TYPE (expr), parmnum, fndecl); | |
660 | else | |
661 | cp_pedwarn ("%s to `%T' from `%T' discards volatile", | |
662 | errtype, reftype, TREE_TYPE (expr)); | |
663 | } | |
664 | } else if (TREE_CODE (reftype) == REFERENCE_TYPE | |
665 | && ! TREE_READONLY (TREE_TYPE (reftype)) | |
666 | && ! lvalue_p (expr)) | |
667 | { | |
668 | /* Ensure semantics of 8.4.3 */ | |
669 | if (fndecl) | |
670 | cp_pedwarn ("ANSI C++ forbids passing non-lvalue `%T' as argument %P of `%D' into non-const &", | |
671 | TREE_TYPE (expr), parmnum, fndecl); | |
672 | else | |
673 | cp_pedwarn ("ANSI C++ forbids %s to `%T' from non-lvalue `%T'", | |
674 | errtype, reftype, TREE_TYPE (expr)); | |
675 | } | |
676 | } | |
677 | ||
678 | /* If EXPR is of aggregate type, and is really a CALL_EXPR, | |
679 | then we don't need to convert it to reference type if | |
680 | it is only being used to initialize DECL which is also | |
681 | of the same aggregate type. */ | |
682 | if (form == REFERENCE_TYPE | |
683 | || (decl != NULL_TREE && decl != error_mark_node | |
684 | && IS_AGGR_TYPE (type) | |
685 | && TREE_CODE (expr) == CALL_EXPR | |
686 | && TYPE_MAIN_VARIANT (type) == intype)) | |
687 | { | |
688 | if (decl && decl != error_mark_node) | |
689 | { | |
690 | tree e1 = build (INIT_EXPR, void_type_node, decl, expr); | |
691 | tree e2; | |
692 | ||
693 | TREE_SIDE_EFFECTS (e1) = 1; | |
694 | if (form == REFERENCE_TYPE) | |
695 | e2 = build1 (NOP_EXPR, reftype, decl); | |
696 | else | |
697 | { | |
698 | e2 = build_unary_op (ADDR_EXPR, decl, 0); | |
699 | TREE_TYPE (e2) = reftype; | |
700 | TREE_REFERENCE_EXPR (e2) = 1; | |
701 | } | |
702 | return build_compound_expr (tree_cons (NULL_TREE, e1, | |
703 | build_tree_list (NULL_TREE, e2))); | |
704 | } | |
705 | expr = copy_node (expr); | |
706 | TREE_TYPE (expr) = reftype; | |
707 | return expr; | |
708 | } | |
709 | return build_up_reference (reftype, expr, flags, decl!=NULL_TREE); | |
710 | } | |
711 | ||
712 | if (decl == error_mark_node) | |
713 | { | |
714 | tree rval_as_conversion = NULL_TREE; | |
715 | tree rval_as_ctor = NULL_TREE; | |
716 | ||
717 | if (IS_AGGR_TYPE (intype) | |
718 | && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1))) | |
719 | { | |
720 | if (rval == error_mark_node) | |
721 | return rval; | |
722 | ||
723 | rval_as_conversion = build_up_reference (reftype, rval, flags, 1); | |
724 | } | |
725 | ||
726 | /* Definitely need to go through a constructor here. */ | |
727 | if (TYPE_HAS_CONSTRUCTOR (type) | |
728 | && (rval = build_method_call | |
729 | (NULL_TREE, constructor_name_full (type), | |
730 | build_tree_list (NULL_TREE, expr), TYPE_BINFO (type), | |
731 | LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY))) | |
732 | { | |
733 | tree init; | |
734 | ||
735 | if (global_bindings_p ()) | |
736 | { | |
737 | extern tree static_aggregates; | |
738 | decl = get_temp_name (type, global_bindings_p ()); | |
739 | init = build_method_call (decl, constructor_name_full (type), | |
740 | build_tree_list (NULL_TREE, expr), | |
741 | TYPE_BINFO (type), LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); | |
742 | ||
743 | if (init == error_mark_node) | |
744 | return error_mark_node; | |
745 | ||
746 | make_decl_rtl (decl, NULL_PTR, 1); | |
747 | static_aggregates = perm_tree_cons (expr, decl, static_aggregates); | |
748 | rval = build_unary_op (ADDR_EXPR, decl, 0); | |
749 | } | |
750 | else | |
751 | { | |
752 | init = build_method_call (NULL_TREE, constructor_name_full (type), | |
753 | build_tree_list (NULL_TREE, expr), | |
754 | TYPE_BINFO (type), LOOKUP_NORMAL|LOOKUP_NO_CONVERSION); | |
755 | ||
756 | if (init == error_mark_node) | |
757 | return error_mark_node; | |
758 | ||
759 | rval = build_cplus_new (type, init, 1); | |
760 | rval = build_up_reference (reftype, rval, flags, 1); | |
761 | } | |
762 | rval_as_ctor = rval; | |
763 | } | |
764 | ||
765 | if (rval_as_ctor && rval_as_conversion) | |
766 | { | |
767 | cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply", | |
768 | intype, reftype); | |
769 | return error_mark_node; | |
770 | } | |
771 | else if (rval_as_ctor) | |
772 | rval = rval_as_ctor; | |
773 | else if (rval_as_conversion) | |
774 | rval = rval_as_conversion; | |
775 | else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype)) | |
776 | { | |
777 | rval = convert (type, expr); | |
778 | if (rval == error_mark_node) | |
779 | return error_mark_node; | |
780 | ||
781 | rval = build_up_reference (reftype, rval, flags, 1); | |
782 | } | |
783 | ||
784 | if (rval && ! TYPE_READONLY (TREE_TYPE (reftype))) | |
785 | cp_pedwarn ("converting `%T' to non-const `%T' will use a temporary", | |
786 | intype, reftype); | |
787 | } | |
788 | ||
789 | if (rval) | |
790 | { | |
791 | /* If we found a way to convert earlier, then use it. */ | |
792 | return rval; | |
793 | } | |
794 | ||
795 | my_friendly_assert (form != OFFSET_TYPE, 189); | |
796 | ||
797 | if ((flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY)) == LOOKUP_COMPLAIN) | |
798 | cp_error ("cannot convert type `%T' to type `%T'", intype, reftype); | |
799 | ||
800 | if (flags & LOOKUP_SPECULATIVELY) | |
801 | return NULL_TREE; | |
802 | ||
803 | return error_mark_node; | |
804 | } | |
805 | ||
806 | /* We are using a reference VAL for its value. Bash that reference all the | |
807 | way down to its lowest form. */ | |
808 | tree | |
809 | convert_from_reference (val) | |
810 | tree val; | |
811 | { | |
812 | tree type = TREE_TYPE (val); | |
813 | ||
814 | if (TREE_CODE (type) == OFFSET_TYPE) | |
815 | type = TREE_TYPE (type); | |
816 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
817 | { | |
818 | tree target_type = TREE_TYPE (type); | |
819 | tree nval; | |
820 | ||
821 | /* This can happen if we cast to a reference type. */ | |
822 | if (TREE_CODE (val) == ADDR_EXPR) | |
823 | { | |
824 | nval = build1 (NOP_EXPR, build_pointer_type (target_type), val); | |
825 | nval = build_indirect_ref (nval, NULL_PTR); | |
826 | /* The below was missing, are other important flags missing too? */ | |
827 | TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val); | |
828 | return nval; | |
829 | } | |
830 | ||
831 | nval = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (target_type), val); | |
832 | ||
833 | TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type); | |
834 | TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type); | |
835 | TREE_READONLY (nval) = TYPE_READONLY (target_type); | |
836 | /* The below was missing, are other important flags missing too? */ | |
837 | TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val); | |
838 | return nval; | |
839 | } | |
840 | return val; | |
841 | } | |
842 | \f | |
843 | /* See if there is a constructor of type TYPE which will convert | |
844 | EXPR. The reference manual seems to suggest (8.5.6) that we need | |
845 | not worry about finding constructors for base classes, then converting | |
846 | to the derived class. | |
847 | ||
848 | MSGP is a pointer to a message that would be an appropriate error | |
849 | string. If MSGP is NULL, then we are not interested in reporting | |
850 | errors. */ | |
851 | tree | |
852 | convert_to_aggr (type, expr, msgp, protect) | |
853 | tree type, expr; | |
854 | char **msgp; | |
855 | int protect; | |
856 | { | |
857 | tree basetype = type; | |
858 | tree name = TYPE_IDENTIFIER (basetype); | |
859 | tree function, fndecl, fntype, parmtypes, parmlist, result; | |
860 | tree method_name; | |
861 | enum access_type access; | |
862 | int can_be_private, can_be_protected; | |
863 | ||
864 | if (! TYPE_HAS_CONSTRUCTOR (basetype)) | |
865 | { | |
866 | if (msgp) | |
867 | *msgp = "type `%s' does not have a constructor"; | |
868 | return error_mark_node; | |
869 | } | |
870 | ||
871 | access = access_public; | |
872 | can_be_private = 0; | |
873 | can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name; | |
874 | ||
875 | parmlist = build_tree_list (NULL_TREE, expr); | |
876 | parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node); | |
877 | ||
878 | if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
879 | { | |
880 | parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes); | |
881 | parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist); | |
882 | } | |
883 | ||
884 | /* The type of the first argument will be filled in inside the loop. */ | |
885 | parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist); | |
886 | parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes); | |
887 | ||
888 | method_name = build_decl_overload (name, parmtypes, 1); | |
889 | ||
890 | /* constructors are up front. */ | |
891 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0); | |
892 | if (TYPE_HAS_DESTRUCTOR (basetype)) | |
893 | fndecl = DECL_CHAIN (fndecl); | |
894 | ||
895 | while (fndecl) | |
896 | { | |
897 | if (DECL_ASSEMBLER_NAME (fndecl) == method_name) | |
898 | { | |
899 | function = fndecl; | |
900 | if (protect) | |
901 | { | |
902 | if (TREE_PRIVATE (fndecl)) | |
903 | { | |
904 | can_be_private = | |
905 | (basetype == current_class_type | |
906 | || is_friend (basetype, current_function_decl) | |
907 | || purpose_member (basetype, DECL_ACCESS (fndecl))); | |
908 | if (! can_be_private) | |
909 | goto found; | |
910 | } | |
911 | else if (TREE_PROTECTED (fndecl)) | |
912 | { | |
913 | if (! can_be_protected) | |
914 | goto found; | |
915 | } | |
916 | } | |
917 | goto found_and_ok; | |
918 | } | |
919 | fndecl = DECL_CHAIN (fndecl); | |
920 | } | |
921 | ||
922 | /* No exact conversion was found. See if an approximate | |
923 | one will do. */ | |
924 | fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0); | |
925 | if (TYPE_HAS_DESTRUCTOR (basetype)) | |
926 | fndecl = DECL_CHAIN (fndecl); | |
927 | ||
928 | { | |
929 | int saw_private = 0; | |
930 | int saw_protected = 0; | |
931 | struct candidate *candidates = | |
932 | (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate)); | |
933 | struct candidate *cp = candidates; | |
934 | ||
935 | while (fndecl) | |
936 | { | |
937 | function = fndecl; | |
938 | cp->h_len = 2; | |
939 | if (flag_ansi_overloading) | |
940 | cp->v.ansi_harshness = (struct harshness_code *) | |
941 | alloca (3 * sizeof (struct harshness_code)); | |
942 | else | |
943 | cp->v.old_harshness = (unsigned short *) | |
944 | alloca (3 * sizeof (short)); | |
945 | ||
946 | compute_conversion_costs (fndecl, parmlist, cp, 2); | |
947 | if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE) == 0) | |
948 | || (!flag_ansi_overloading && cp->evil == 0)) | |
949 | { | |
950 | cp->u.field = fndecl; | |
951 | if (protect) | |
952 | { | |
953 | if (TREE_PRIVATE (fndecl)) | |
954 | access = access_private; | |
955 | else if (TREE_PROTECTED (fndecl)) | |
956 | access = access_protected; | |
957 | else | |
958 | access = access_public; | |
959 | } | |
960 | else | |
961 | access = access_public; | |
962 | ||
963 | if (access == access_private | |
964 | ? (basetype == current_class_type | |
965 | || is_friend (basetype, cp->function) | |
966 | || purpose_member (basetype, DECL_ACCESS (fndecl))) | |
967 | : access == access_protected | |
968 | ? (can_be_protected | |
969 | || purpose_member (basetype, DECL_ACCESS (fndecl))) | |
970 | : 1) | |
971 | { | |
972 | if ((flag_ansi_overloading && cp->h.code <= TRIVIAL_CODE) | |
973 | || (!flag_ansi_overloading | |
974 | && cp->user == 0 && cp->b_or_d == 0 | |
975 | && cp->easy <= 1)) | |
976 | goto found_and_ok; | |
977 | cp++; | |
978 | } | |
979 | else | |
980 | { | |
981 | if (access == access_private) | |
982 | saw_private = 1; | |
983 | else | |
984 | saw_protected = 1; | |
985 | } | |
986 | } | |
987 | fndecl = DECL_CHAIN (fndecl); | |
988 | } | |
989 | if (cp - candidates) | |
990 | { | |
991 | /* Rank from worst to best. Then cp will point to best one. | |
992 | Private fields have their bits flipped. For unsigned | |
993 | numbers, this should make them look very large. | |
994 | If the best alternate has a (signed) negative value, | |
995 | then all we ever saw were private members. */ | |
996 | if (cp - candidates > 1) | |
997 | qsort (candidates, /* char *base */ | |
998 | cp - candidates, /* int nel */ | |
999 | sizeof (struct candidate), /* int width */ | |
1000 | rank_for_overload); /* int (*compar)() */ | |
1001 | ||
1002 | --cp; | |
1003 | if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE)) | |
1004 | || (!flag_ansi_overloading && cp->evil > 1)) | |
1005 | { | |
1006 | if (msgp) | |
1007 | *msgp = "ambiguous type conversion possible for `%s'"; | |
1008 | return error_mark_node; | |
1009 | } | |
1010 | ||
1011 | function = cp->function; | |
1012 | fndecl = cp->u.field; | |
1013 | goto found_and_ok; | |
1014 | } | |
1015 | else if (msgp) | |
1016 | { | |
1017 | if (saw_private) | |
1018 | if (saw_protected) | |
1019 | *msgp = "only private and protected conversions apply"; | |
1020 | else | |
1021 | *msgp = "only private conversions apply"; | |
1022 | else if (saw_protected) | |
1023 | *msgp = "only protected conversions apply"; | |
1024 | } | |
1025 | return error_mark_node; | |
1026 | } | |
1027 | /* NOTREACHED */ | |
1028 | ||
1029 | not_found: | |
1030 | if (msgp) *msgp = "no appropriate conversion to type `%s'"; | |
1031 | return error_mark_node; | |
1032 | found: | |
1033 | if (access == access_private) | |
1034 | if (! can_be_private) | |
1035 | { | |
1036 | if (msgp) | |
1037 | *msgp = TREE_PRIVATE (fndecl) | |
1038 | ? "conversion to type `%s' is private" | |
1039 | : "conversion to type `%s' is from private base class"; | |
1040 | return error_mark_node; | |
1041 | } | |
1042 | if (access == access_protected) | |
1043 | if (! can_be_protected) | |
1044 | { | |
1045 | if (msgp) | |
1046 | *msgp = TREE_PRIVATE (fndecl) | |
1047 | ? "conversion to type `%s' is protected" | |
1048 | : "conversion to type `%s' is from protected base class"; | |
1049 | return error_mark_node; | |
1050 | } | |
1051 | function = fndecl; | |
1052 | found_and_ok: | |
1053 | ||
1054 | /* It will convert, but we don't do anything about it yet. */ | |
1055 | if (msgp == 0) | |
1056 | return NULL_TREE; | |
1057 | ||
1058 | fntype = TREE_TYPE (function); | |
1059 | if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL) | |
1060 | function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); | |
1061 | else | |
1062 | function = default_conversion (function); | |
1063 | ||
1064 | result = build_nt (CALL_EXPR, function, | |
1065 | convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype), | |
1066 | parmlist, NULL_TREE, LOOKUP_NORMAL), | |
1067 | NULL_TREE); | |
1068 | TREE_TYPE (result) = TREE_TYPE (fntype); | |
1069 | TREE_SIDE_EFFECTS (result) = 1; | |
1070 | TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype); | |
1071 | return result; | |
1072 | } | |
1073 | ||
1074 | /* Call this when we know (for any reason) that expr is not, in fact, | |
1075 | zero. This routine is like convert_pointer_to, but it pays | |
1076 | attention to which specific instance of what type we want to | |
1077 | convert to. This routine should eventually become | |
1078 | convert_to_pointer after all references to convert_to_pointer | |
1079 | are removed. */ | |
1080 | tree | |
1081 | convert_pointer_to_real (binfo, expr) | |
1082 | tree binfo, expr; | |
1083 | { | |
1084 | register tree intype = TREE_TYPE (expr); | |
1085 | tree ptr_type; | |
1086 | tree type, rval; | |
1087 | ||
1088 | if (TREE_CODE (binfo) == TREE_VEC) | |
1089 | type = BINFO_TYPE (binfo); | |
1090 | else if (IS_AGGR_TYPE (binfo)) | |
1091 | { | |
1092 | type = binfo; | |
1093 | } | |
1094 | else | |
1095 | { | |
1096 | type = binfo; | |
1097 | binfo = NULL_TREE; | |
1098 | } | |
1099 | ||
1100 | ptr_type = build_pointer_type (type); | |
1101 | if (ptr_type == TYPE_MAIN_VARIANT (intype)) | |
1102 | return expr; | |
1103 | ||
1104 | if (intype == error_mark_node) | |
1105 | return error_mark_node; | |
1106 | ||
1107 | my_friendly_assert (!integer_zerop (expr), 191); | |
1108 | ||
1109 | if (TREE_CODE (type) == RECORD_TYPE | |
1110 | && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE | |
1111 | && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype))) | |
1112 | { | |
1113 | tree path; | |
1114 | int distance | |
1115 | = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), | |
1116 | 0, &path); | |
1117 | ||
1118 | /* This function shouldn't be called with unqualified arguments | |
1119 | but if it is, give them an error message that they can read. */ | |
1120 | if (distance < 0) | |
1121 | { | |
1122 | cp_error ("cannot convert a pointer of type `%T'", | |
1123 | TREE_TYPE (intype)); | |
1124 | cp_error ("to a pointer of type `%T'", type); | |
1125 | ||
1126 | if (distance == -2) | |
1127 | cp_error ("because `%T' is an ambiguous base class", type); | |
1128 | return error_mark_node; | |
1129 | } | |
1130 | ||
1131 | return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1); | |
1132 | } | |
1133 | rval = build1 (NOP_EXPR, ptr_type, | |
1134 | TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr); | |
1135 | TREE_CONSTANT (rval) = TREE_CONSTANT (expr); | |
1136 | return rval; | |
1137 | } | |
1138 | ||
1139 | /* Call this when we know (for any reason) that expr is | |
1140 | not, in fact, zero. This routine gets a type out of the first | |
1141 | argument and uses it to search for the type to convert to. If there | |
1142 | is more than one instance of that type in the expr, the conversion is | |
1143 | ambiguous. This routine should eventually go away, and all | |
1144 | callers should use convert_to_pointer_real. */ | |
1145 | tree | |
1146 | convert_pointer_to (binfo, expr) | |
1147 | tree binfo, expr; | |
1148 | { | |
1149 | tree type; | |
1150 | ||
1151 | if (TREE_CODE (binfo) == TREE_VEC) | |
1152 | type = BINFO_TYPE (binfo); | |
1153 | else if (IS_AGGR_TYPE (binfo)) | |
1154 | type = binfo; | |
1155 | else | |
1156 | type = binfo; | |
1157 | return convert_pointer_to_real (type, expr); | |
1158 | } | |
1159 | ||
1160 | /* Same as above, but don't abort if we get an "ambiguous" baseclass. | |
1161 | There's only one virtual baseclass we are looking for, and once | |
1162 | we find one such virtual baseclass, we have found them all. */ | |
1163 | ||
1164 | tree | |
1165 | convert_pointer_to_vbase (binfo, expr) | |
1166 | tree binfo; | |
1167 | tree expr; | |
1168 | { | |
1169 | tree intype = TREE_TYPE (TREE_TYPE (expr)); | |
1170 | tree binfos = TYPE_BINFO_BASETYPES (intype); | |
1171 | int i; | |
1172 | ||
1173 | for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--) | |
1174 | { | |
1175 | tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i)); | |
1176 | if (BINFO_TYPE (binfo) == basetype) | |
1177 | return convert_pointer_to (binfo, expr); | |
1178 | if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype))) | |
1179 | return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr)); | |
1180 | } | |
1181 | my_friendly_abort (6); | |
1182 | /* NOTREACHED */ | |
1183 | return NULL_TREE; | |
1184 | } | |
1185 | \f | |
1186 | /* Create an expression whose value is that of EXPR, | |
1187 | converted to type TYPE. The TREE_TYPE of the value | |
1188 | is always TYPE. This function implements all reasonable | |
1189 | conversions; callers should filter out those that are | |
1190 | not permitted by the language being compiled. */ | |
1191 | ||
1192 | tree | |
1193 | convert (type, expr) | |
1194 | tree type, expr; | |
1195 | { | |
1196 | register tree e = expr; | |
1197 | register enum tree_code code = TREE_CODE (type); | |
1198 | ||
1199 | if (type == TREE_TYPE (expr) | |
1200 | || TREE_CODE (expr) == ERROR_MARK) | |
1201 | return expr; | |
1202 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr))) | |
1203 | return fold (build1 (NOP_EXPR, type, expr)); | |
1204 | if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK) | |
1205 | return error_mark_node; | |
1206 | if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE) | |
1207 | { | |
1208 | error ("void value not ignored as it ought to be"); | |
1209 | return error_mark_node; | |
1210 | } | |
1211 | if (code == VOID_TYPE) | |
1212 | { | |
1213 | /* We're converting to a void type; see if they have an | |
1214 | `operator void'. */ | |
1215 | tree rval = build_type_conversion (NOP_EXPR, type, e, 0); | |
1216 | /* If we can convert to void type via a type conversion, do so. */ | |
1217 | if (rval) | |
1218 | return rval; | |
1219 | return build1 (CONVERT_EXPR, type, e); | |
1220 | } | |
1221 | #if 0 | |
1222 | /* This is incorrect. A truncation can't be stripped this way. | |
1223 | Extensions will be stripped by the use of get_unwidened. */ | |
1224 | if (TREE_CODE (expr) == NOP_EXPR) | |
1225 | return convert (type, TREE_OPERAND (expr, 0)); | |
1226 | #endif | |
1227 | ||
1228 | /* Just convert to the type of the member. */ | |
1229 | if (code == OFFSET_TYPE) | |
1230 | { | |
1231 | type = TREE_TYPE (type); | |
1232 | code = TREE_CODE (type); | |
1233 | } | |
1234 | ||
1235 | /* C++ */ | |
1236 | if (code == REFERENCE_TYPE) | |
1237 | return fold (convert_to_reference (error_mark_node, type, e, NULL_TREE, | |
1238 | -1, "conversion", -1, LOOKUP_NORMAL)); | |
1239 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) | |
1240 | e = convert_from_reference (e); | |
1241 | ||
1242 | if (code == INTEGER_TYPE || code == ENUMERAL_TYPE) | |
1243 | { | |
1244 | tree intype = TREE_TYPE (expr); | |
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 | |
1249 | && (form == INTEGER_TYPE || form == REAL_TYPE | |
1250 | || form == ENUMERAL_TYPE)) | |
1251 | { | |
1252 | cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type); | |
1253 | ||
1254 | if (flag_pedantic_errors) | |
1255 | return error_mark_node; | |
1256 | } | |
1257 | if (form == OFFSET_TYPE) | |
1258 | cp_error_at ("pointer-to-member expression object not composed with type `%D' object", | |
1259 | TYPE_NAME (TYPE_OFFSET_BASETYPE (intype))); | |
1260 | else if (IS_AGGR_TYPE (intype)) | |
1261 | { | |
1262 | tree rval; | |
1263 | rval = build_type_conversion (CONVERT_EXPR, type, expr, 1); | |
1264 | if (rval) return rval; | |
1265 | cp_error ("`%#T' used where an `int' was expected", intype); | |
1266 | return error_mark_node; | |
1267 | } | |
1268 | return fold (convert_to_integer (type, e)); | |
1269 | } | |
1270 | if (code == POINTER_TYPE) | |
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); | |
1293 | ||
1294 | if (TREE_CODE (dtype) == REFERENCE_TYPE) | |
1295 | { | |
1296 | e = convert_from_reference (e); | |
1297 | dtype = TREE_TYPE (e); | |
1298 | } | |
1299 | dtype = TYPE_MAIN_VARIANT (dtype); | |
1300 | ||
1301 | /* Conversion of object pointers or signature pointers/references | |
1302 | to signature pointers/references. */ | |
1303 | ||
1304 | if (TYPE_LANG_SPECIFIC (type) | |
1305 | && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))) | |
1306 | { | |
1307 | tree constructor = build_signature_pointer_constructor (type, expr); | |
1308 | tree sig_ty = SIGNATURE_TYPE (type); | |
1309 | tree sig_ptr; | |
1310 | ||
1311 | if (constructor == error_mark_node) | |
1312 | return error_mark_node; | |
1313 | ||
1314 | sig_ptr = get_temp_name (type, 1); | |
1315 | DECL_INITIAL (sig_ptr) = constructor; | |
1316 | CLEAR_SIGNATURE (sig_ty); | |
1317 | finish_decl (sig_ptr, constructor, 0, 0); | |
1318 | SET_SIGNATURE (sig_ty); | |
1319 | TREE_READONLY (sig_ptr) = 1; | |
1320 | ||
1321 | return sig_ptr; | |
1322 | } | |
1323 | ||
1324 | /* Conversion between aggregate types. New C++ semantics allow | |
1325 | objects of derived type to be cast to objects of base type. | |
1326 | Old semantics only allowed this between pointers. | |
1327 | ||
1328 | There may be some ambiguity between using a constructor | |
1329 | vs. using a type conversion operator when both apply. */ | |
1330 | ||
1331 | else if (IS_AGGR_TYPE (dtype)) | |
1332 | { | |
1333 | tree binfo; | |
1334 | ||
1335 | tree conversion = TYPE_HAS_CONVERSION (dtype) | |
1336 | ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE; | |
1337 | ||
1338 | if (TYPE_HAS_CONSTRUCTOR (type)) | |
1339 | { | |
1340 | tree rval = build_method_call (NULL_TREE, constructor_name_full (type), | |
1341 | build_tree_list (NULL_TREE, e), | |
1342 | TYPE_BINFO (type), | |
1343 | conversion ? LOOKUP_NO_CONVERSION : 0); | |
1344 | ||
1345 | if (rval != error_mark_node) | |
1346 | { | |
1347 | if (conversion) | |
1348 | { | |
1349 | error ("both constructor and type conversion operator apply"); | |
1350 | return error_mark_node; | |
1351 | } | |
1352 | /* call to constructor successful. */ | |
1353 | rval = build_cplus_new (type, rval, 0); | |
1354 | return rval; | |
1355 | } | |
1356 | } | |
1357 | /* Type conversion successful/applies. */ | |
1358 | if (conversion) | |
1359 | { | |
1360 | if (conversion == error_mark_node) | |
1361 | error ("ambiguous pointer conversion"); | |
1362 | return conversion; | |
1363 | } | |
1364 | ||
1365 | /* now try normal C++ assignment semantics. */ | |
1366 | binfo = TYPE_BINFO (dtype); | |
1367 | if (BINFO_TYPE (binfo) == type | |
1368 | || (binfo = get_binfo (type, dtype, 1))) | |
1369 | { | |
1370 | if (binfo == error_mark_node) | |
1371 | return error_mark_node; | |
1372 | } | |
1373 | if (binfo != NULL_TREE) | |
1374 | { | |
1375 | if (lvalue_p (e)) | |
1376 | { | |
1377 | e = build_unary_op (ADDR_EXPR, e, 0); | |
1378 | ||
1379 | if (! BINFO_OFFSET_ZEROP (binfo)) | |
1380 | e = build (PLUS_EXPR, TYPE_POINTER_TO (type), | |
1381 | e, BINFO_OFFSET (binfo)); | |
1382 | return build1 (INDIRECT_REF, type, e); | |
1383 | } | |
1384 | ||
1385 | sorry ("addressable aggregates"); | |
1386 | return error_mark_node; | |
1387 | } | |
1388 | error ("conversion between incompatible aggregate types requested"); | |
1389 | return error_mark_node; | |
1390 | } | |
1391 | /* conversion from non-aggregate to aggregate type requires | |
1392 | constructor. */ | |
1393 | else if (TYPE_HAS_CONSTRUCTOR (type)) | |
1394 | { | |
1395 | tree rval; | |
1396 | tree init = build_method_call (NULL_TREE, constructor_name_full (type), | |
1397 | build_tree_list (NULL_TREE, e), | |
1398 | TYPE_BINFO (type), LOOKUP_NORMAL); | |
1399 | if (init == error_mark_node) | |
1400 | { | |
1401 | cp_error ("in conversion to type `%T'", type); | |
1402 | return error_mark_node; | |
1403 | } | |
1404 | rval = build_cplus_new (type, init, 0); | |
1405 | return rval; | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | /* If TYPE or TREE_TYPE (EXPR) is not on the permanent_obstack, | |
1410 | then the it won't be hashed and hence compare as not equal, | |
1411 | even when it is. */ | |
1412 | if (code == ARRAY_TYPE | |
1413 | && TREE_TYPE (TREE_TYPE (expr)) == TREE_TYPE (type) | |
1414 | && index_type_equal (TYPE_DOMAIN (TREE_TYPE (expr)), TYPE_DOMAIN (type))) | |
1415 | return expr; | |
1416 | ||
1417 | cp_error ("conversion from `%T' to non-scalar type `%T' requested", | |
1418 | TREE_TYPE (expr), type); | |
1419 | return error_mark_node; | |
1420 | } | |
1421 | ||
1422 | /* Like convert, except permit conversions to take place which | |
1423 | are not normally allowed due to access restrictions | |
1424 | (such as conversion from sub-type to private super-type). */ | |
1425 | tree | |
1426 | convert_force (type, expr) | |
1427 | tree type; | |
1428 | tree expr; | |
1429 | { | |
1430 | register tree e = expr; | |
1431 | register enum tree_code code = TREE_CODE (type); | |
1432 | ||
1433 | if (code == REFERENCE_TYPE) | |
1434 | return fold (convert_to_reference (0, type, e, NULL_TREE, -1, | |
1435 | NULL, -1, 0)); | |
1436 | else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE) | |
1437 | e = convert_from_reference (e); | |
1438 | ||
1439 | if (code == POINTER_TYPE) | |
1440 | return fold (convert_to_pointer_force (type, e)); | |
1441 | ||
1442 | /* From cp-typeck.c convert_for_assignment */ | |
1443 | if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR | |
1444 | && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE | |
1445 | && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE) | |
1446 | || integer_zerop (e)) | |
1447 | && TYPE_PTRMEMFUNC_P (type)) | |
1448 | { | |
1449 | /* compatible pointer to member functions. */ | |
1450 | e = build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1); | |
1451 | if (e == 0) | |
1452 | return error_mark_node; | |
1453 | return digest_init (type, e, (tree *)0); | |
1454 | } | |
1455 | { | |
1456 | int old_equiv = flag_int_enum_equivalence; | |
1457 | flag_int_enum_equivalence = 1; | |
1458 | e = convert (type, e); | |
1459 | flag_int_enum_equivalence = old_equiv; | |
1460 | } | |
1461 | return e; | |
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 | { | |
1472 | tree first_arg = expr; | |
1473 | tree rval; | |
1474 | int flags; | |
1475 | ||
1476 | if (for_sure == 0) | |
1477 | { | |
1478 | if (! lvalue_p (expr)) | |
1479 | first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node); | |
1480 | flags = LOOKUP_PROTECT; | |
1481 | } | |
1482 | else | |
1483 | flags = LOOKUP_NORMAL; | |
1484 | ||
1485 | rval = build_method_call (first_arg, constructor_name_full (typename), | |
1486 | NULL_TREE, NULL_TREE, flags); | |
1487 | if (rval == error_mark_node) | |
1488 | { | |
1489 | if (for_sure == 0) | |
1490 | return NULL_TREE; | |
1491 | return error_mark_node; | |
1492 | } | |
1493 | if (first_arg != expr) | |
1494 | { | |
1495 | expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr, | |
1496 | LOOKUP_COMPLAIN, 1); | |
1497 | TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0); | |
1498 | } | |
1499 | if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE | |
1500 | && TREE_CODE (xtype) != REFERENCE_TYPE) | |
1501 | rval = default_conversion (rval); | |
1502 | ||
1503 | if (warn_cast_qual | |
1504 | && TREE_TYPE (xtype) | |
1505 | && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval))) | |
1506 | > TREE_READONLY (TREE_TYPE (xtype)))) | |
1507 | warning ("user-defined conversion casting away `const'"); | |
1508 | return convert (xtype, rval); | |
1509 | } | |
1510 | ||
1511 | /* Convert an aggregate EXPR to type XTYPE. If a conversion | |
1512 | exists, return the attempted conversion. This may | |
1513 | return ERROR_MARK_NODE if the conversion is not | |
1514 | allowed (references private members, etc). | |
1515 | If no conversion exists, NULL_TREE is returned. | |
1516 | ||
1517 | If (FOR_SURE & 1) is non-zero, then we allow this type conversion | |
1518 | to take place immediately. Otherwise, we build a SAVE_EXPR | |
1519 | which can be evaluated if the results are ever needed. | |
1520 | ||
1521 | If FOR_SURE >= 2, then we only look for exact conversions. | |
1522 | ||
1523 | TYPE may be a reference type, in which case we first look | |
1524 | for something that will convert to a reference type. If | |
1525 | that fails, we will try to look for something of the | |
1526 | reference's target type, and then return a reference to that. */ | |
1527 | tree | |
1528 | build_type_conversion (code, xtype, expr, for_sure) | |
1529 | enum tree_code code; | |
1530 | tree xtype, expr; | |
1531 | int for_sure; | |
1532 | { | |
1533 | /* C++: check to see if we can convert this aggregate type | |
1534 | into the required scalar type. */ | |
1535 | tree type, type_default; | |
1536 | tree typename = build_typename_overload (xtype), *typenames; | |
1537 | int n_variants = 0; | |
1538 | tree basetype, save_basetype; | |
1539 | tree rval; | |
1540 | int exact_conversion = for_sure >= 2; | |
1541 | for_sure &= 1; | |
1542 | ||
1543 | if (expr == error_mark_node) | |
1544 | return error_mark_node; | |
1545 | ||
1546 | basetype = TREE_TYPE (expr); | |
1547 | if (TREE_CODE (basetype) == REFERENCE_TYPE) | |
1548 | basetype = TREE_TYPE (basetype); | |
1549 | ||
1550 | basetype = TYPE_MAIN_VARIANT (basetype); | |
1551 | if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype)) | |
1552 | return NULL_TREE; | |
1553 | ||
1554 | if (TREE_CODE (xtype) == POINTER_TYPE | |
1555 | || TREE_CODE (xtype) == REFERENCE_TYPE) | |
1556 | { | |
1557 | /* Prepare to match a variant of this type. */ | |
1558 | type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype)); | |
1559 | for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type)) | |
1560 | n_variants++; | |
1561 | typenames = (tree *)alloca (n_variants * sizeof (tree)); | |
1562 | for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype)); | |
1563 | type; n_variants++, type = TYPE_NEXT_VARIANT (type)) | |
1564 | { | |
1565 | if (type == TREE_TYPE (xtype)) | |
1566 | typenames[n_variants] = typename; | |
1567 | else if (TREE_CODE (xtype) == POINTER_TYPE) | |
1568 | typenames[n_variants] = build_typename_overload (build_pointer_type (type)); | |
1569 | else | |
1570 | typenames[n_variants] = build_typename_overload (build_reference_type (type)); | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | save_basetype = basetype; | |
1575 | type = xtype; | |
1576 | ||
1577 | while (TYPE_HAS_CONVERSION (basetype)) | |
1578 | { | |
1579 | int i; | |
1580 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1581 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1582 | for (i = 0; i < n_variants; i++) | |
1583 | if (typenames[i] != typename | |
1584 | && lookup_fnfields (TYPE_BINFO (basetype), typenames[i], 0)) | |
1585 | return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure); | |
1586 | ||
1587 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1588 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1589 | else | |
1590 | break; | |
1591 | } | |
1592 | ||
1593 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
1594 | { | |
1595 | tree first_arg = expr; | |
1596 | type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
1597 | basetype = save_basetype; | |
1598 | ||
1599 | /* May need to build a temporary for this. */ | |
1600 | while (TYPE_HAS_CONVERSION (basetype)) | |
1601 | { | |
1602 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1603 | { | |
1604 | int flags; | |
1605 | ||
1606 | if (for_sure == 0) | |
1607 | { | |
1608 | if (! lvalue_p (expr)) | |
1609 | first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node); | |
1610 | flags = LOOKUP_PROTECT; | |
1611 | } | |
1612 | else | |
1613 | flags = LOOKUP_NORMAL; | |
1614 | rval = build_method_call (first_arg, constructor_name_full (typename), | |
1615 | NULL_TREE, NULL_TREE, flags); | |
1616 | if (rval == error_mark_node) | |
1617 | { | |
1618 | if (for_sure == 0) | |
1619 | return NULL_TREE; | |
1620 | return error_mark_node; | |
1621 | } | |
1622 | TREE_VALUE (TREE_OPERAND (rval, 1)) = expr; | |
1623 | ||
1624 | if (IS_AGGR_TYPE (type)) | |
1625 | { | |
1626 | tree init = build_method_call (NULL_TREE, | |
1627 | constructor_name_full (type), | |
1628 | build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL); | |
1629 | tree temp = build_cplus_new (type, init, 1); | |
1630 | return build_up_reference (TYPE_REFERENCE_TO (type), temp, | |
1631 | LOOKUP_COMPLAIN, 1); | |
1632 | } | |
1633 | return convert (xtype, rval); | |
1634 | } | |
1635 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1636 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1637 | else | |
1638 | break; | |
1639 | } | |
1640 | /* No free conversions for reference types, right?. */ | |
1641 | return NULL_TREE; | |
1642 | } | |
1643 | ||
1644 | if (exact_conversion) | |
1645 | return NULL_TREE; | |
1646 | ||
1647 | /* No perfect match found, try default. */ | |
1648 | #if 0 /* This is wrong; there is no standard conversion from void* to | |
1649 | anything. -jason */ | |
1650 | if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE) | |
1651 | type_default = ptr_type_node; | |
1652 | else | |
1653 | #endif | |
1654 | if (type == void_type_node) | |
1655 | return NULL_TREE; | |
1656 | else | |
1657 | { | |
1658 | tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node)); | |
1659 | if (tmp == error_mark_node) | |
1660 | return NULL_TREE; | |
1661 | type_default = TREE_TYPE (tmp); | |
1662 | } | |
1663 | ||
1664 | basetype = save_basetype; | |
1665 | ||
1666 | if (type_default != type) | |
1667 | { | |
1668 | type = type_default; | |
1669 | typename = build_typename_overload (type); | |
1670 | ||
1671 | while (TYPE_HAS_CONVERSION (basetype)) | |
1672 | { | |
1673 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1674 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1675 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1676 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1677 | else | |
1678 | break; | |
1679 | } | |
1680 | } | |
1681 | ||
1682 | try_pointer: | |
1683 | ||
1684 | if (type == ptr_type_node) | |
1685 | { | |
1686 | /* Try converting to some other pointer type | |
1687 | with which void* is compatible, or in situations | |
1688 | in which void* is appropriate (such as &&,||, and !). */ | |
1689 | ||
1690 | while (TYPE_HAS_CONVERSION (basetype)) | |
1691 | { | |
1692 | if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0) | |
1693 | { | |
1694 | if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node) | |
1695 | return error_mark_node; | |
1696 | typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv)); | |
1697 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1698 | } | |
1699 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1700 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1701 | else | |
1702 | break; | |
1703 | } | |
1704 | } | |
1705 | if (TREE_CODE (type) == POINTER_TYPE | |
1706 | && TYPE_READONLY (TREE_TYPE (type)) | |
1707 | && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node) | |
1708 | { | |
1709 | /* Try converting to some other pointer type | |
1710 | with which const void* is compatible. */ | |
1711 | ||
1712 | while (TYPE_HAS_CONVERSION (basetype)) | |
1713 | { | |
1714 | if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0) | |
1715 | { | |
1716 | if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node) | |
1717 | return error_mark_node; | |
1718 | typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv)); | |
1719 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1720 | } | |
1721 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1722 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1723 | else | |
1724 | break; | |
1725 | } | |
1726 | } | |
1727 | /* Use the longer or shorter conversion that is appropriate. Have | |
1728 | to check against 0 because the conversion may come from a baseclass. */ | |
1729 | if (TREE_CODE (type) == INTEGER_TYPE | |
1730 | && TYPE_HAS_INT_CONVERSION (basetype) | |
1731 | && CLASSTYPE_CONVERSION (basetype, int_conv) != 0 | |
1732 | && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node) | |
1733 | { | |
1734 | typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv)); | |
1735 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1736 | } | |
1737 | ||
1738 | if (TREE_CODE (type) == REAL_TYPE | |
1739 | && TYPE_HAS_REAL_CONVERSION (basetype) | |
1740 | && CLASSTYPE_CONVERSION (basetype, real_conv) != 0 | |
1741 | && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node) | |
1742 | { | |
1743 | /* Only accept using an operator double() if there isn't a conflicting | |
1744 | operator int(). */ | |
1745 | if (flag_ansi_overloading && TYPE_HAS_INT_CONVERSION (basetype)) | |
1746 | { | |
1747 | error ("two possible conversions for type `%s'", | |
1748 | TYPE_NAME_STRING (type)); | |
1749 | return error_mark_node; | |
1750 | } | |
1751 | ||
1752 | typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv)); | |
1753 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1754 | } | |
1755 | ||
1756 | /* THIS IS A KLUDGE. */ | |
1757 | if (TREE_CODE (type) != POINTER_TYPE | |
1758 | && (code == TRUTH_ANDIF_EXPR | |
1759 | || code == TRUTH_ORIF_EXPR | |
1760 | || code == TRUTH_NOT_EXPR)) | |
1761 | { | |
1762 | /* Here's when we can convert to a pointer. */ | |
1763 | type = ptr_type_node; | |
1764 | goto try_pointer; | |
1765 | } | |
1766 | ||
1767 | /* THESE ARE TOTAL KLUDGES. */ | |
1768 | /* Default promotion yields no new alternatives, try | |
1769 | conversions which are anti-default, such as | |
1770 | ||
1771 | double -> float or int -> unsigned or unsigned -> long | |
1772 | ||
1773 | */ | |
1774 | if (type_default == type | |
1775 | && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == REAL_TYPE)) | |
1776 | { | |
1777 | int not_again = 0; | |
1778 | ||
1779 | if (type == double_type_node) | |
1780 | typename = build_typename_overload (float_type_node); | |
1781 | else if (type == integer_type_node) | |
1782 | typename = build_typename_overload (unsigned_type_node); | |
1783 | else if (type == unsigned_type_node) | |
1784 | typename = build_typename_overload (long_integer_type_node); | |
1785 | ||
1786 | again: | |
1787 | basetype = save_basetype; | |
1788 | while (TYPE_HAS_CONVERSION (basetype)) | |
1789 | { | |
1790 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1791 | return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1792 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1793 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1794 | else | |
1795 | break; | |
1796 | } | |
1797 | if (! not_again) | |
1798 | { | |
1799 | if (type == integer_type_node) | |
1800 | { | |
1801 | typename = build_typename_overload (long_integer_type_node); | |
1802 | not_again = 1; | |
1803 | goto again; | |
1804 | } | |
1805 | else | |
1806 | { | |
1807 | typename = build_typename_overload (integer_type_node); | |
1808 | not_again = 1; | |
1809 | goto again; | |
1810 | } | |
1811 | } | |
1812 | } | |
1813 | ||
1814 | /* Now, try C promotions... | |
1815 | ||
1816 | float -> int | |
1817 | int -> float, void * | |
1818 | void * -> int | |
1819 | ||
1820 | Truthvalue conversions let us try to convert | |
1821 | to pointer if we were going for int, and to int | |
1822 | if we were looking for pointer. */ | |
1823 | ||
1824 | basetype = save_basetype; | |
1825 | if (TREE_CODE (type) == REAL_TYPE | |
1826 | || (TREE_CODE (type) == POINTER_TYPE | |
1827 | && (code == TRUTH_ANDIF_EXPR | |
1828 | || code == TRUTH_ORIF_EXPR | |
1829 | || code == TRUTH_NOT_EXPR))) | |
1830 | type = integer_type_node; | |
1831 | else if (TREE_CODE (type) == INTEGER_TYPE) | |
1832 | if (TYPE_HAS_REAL_CONVERSION (basetype)) | |
1833 | type = double_type_node; | |
1834 | else | |
1835 | return NULL_TREE; | |
1836 | else | |
1837 | return NULL_TREE; | |
1838 | ||
1839 | typename = build_typename_overload (type); | |
1840 | while (TYPE_HAS_CONVERSION (basetype)) | |
1841 | { | |
1842 | if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0)) | |
1843 | { | |
1844 | rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure); | |
1845 | return rval; | |
1846 | } | |
1847 | if (TYPE_BINFO_BASETYPES (basetype)) | |
1848 | basetype = TYPE_BINFO_BASETYPE (basetype, 0); | |
1849 | else | |
1850 | break; | |
1851 | } | |
1852 | ||
1853 | return NULL_TREE; | |
1854 | } | |
1855 | ||
1856 | /* Must convert two aggregate types to non-aggregate type. | |
1857 | Attempts to find a non-ambiguous, "best" type conversion. | |
1858 | ||
1859 | Return 1 on success, 0 on failure. | |
1860 | ||
1861 | @@ What are the real semantics of this supposed to be??? */ | |
1862 | int | |
1863 | build_default_binary_type_conversion (code, arg1, arg2) | |
1864 | enum tree_code code; | |
1865 | tree *arg1, *arg2; | |
1866 | { | |
1867 | tree type1 = TREE_TYPE (*arg1); | |
1868 | tree type2 = TREE_TYPE (*arg2); | |
1869 | ||
1870 | if (TREE_CODE (type1) == REFERENCE_TYPE | |
1871 | || TREE_CODE (type1) == POINTER_TYPE) | |
1872 | type1 = TREE_TYPE (type1); | |
1873 | if (TREE_CODE (type2) == REFERENCE_TYPE | |
1874 | || TREE_CODE (type2) == POINTER_TYPE) | |
1875 | type2 = TREE_TYPE (type2); | |
1876 | ||
1877 | if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL) | |
1878 | { | |
1879 | tree decl = typedecl_for_tag (type1); | |
1880 | if (decl) | |
1881 | error ("type conversion nonexistent for type `%s'", | |
1882 | IDENTIFIER_POINTER (DECL_NAME (decl))); | |
1883 | else | |
1884 | error ("type conversion nonexistent for non-C++ type"); | |
1885 | return 0; | |
1886 | } | |
1887 | if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL) | |
1888 | { | |
1889 | tree decl = typedecl_for_tag (type2); | |
1890 | if (decl) | |
1891 | error ("type conversion nonexistent for type `%s'", | |
1892 | IDENTIFIER_POINTER (decl)); | |
1893 | else | |
1894 | error ("type conversion nonexistent for non-C++ type"); | |
1895 | return 0; | |
1896 | } | |
1897 | ||
1898 | if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1)) | |
1899 | { | |
1900 | if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2)) | |
1901 | cp_error ("no conversion from `%T' and `%T' to types with default `%O' ", | |
1902 | type1, type2, code); | |
1903 | else | |
1904 | cp_error ("no conversion from `%T' to type with default `%O'", | |
1905 | type1, code); | |
1906 | return 0; | |
1907 | } | |
1908 | else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2)) | |
1909 | { | |
1910 | cp_error ("no conversion from `%T' to type with default `%O'", | |
1911 | type2, code); | |
1912 | return 0; | |
1913 | } | |
1914 | ||
1915 | if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1)) | |
1916 | cp_warning ("ambiguous type conversion for type `%T', defaulting to int", | |
1917 | type1); | |
1918 | if (TYPE_HAS_INT_CONVERSION (type1)) | |
1919 | { | |
1920 | *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1); | |
1921 | *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1); | |
1922 | } | |
1923 | else if (TYPE_HAS_REAL_CONVERSION (type1)) | |
1924 | { | |
1925 | *arg1 = build_type_conversion (code, double_type_node, *arg1, 1); | |
1926 | *arg2 = build_type_conversion (code, double_type_node, *arg2, 1); | |
1927 | } | |
1928 | else | |
1929 | { | |
1930 | *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1); | |
1931 | if (*arg1 == error_mark_node) | |
1932 | error ("ambiguous pointer conversion"); | |
1933 | *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1); | |
1934 | if (*arg1 != error_mark_node && *arg2 == error_mark_node) | |
1935 | error ("ambiguous pointer conversion"); | |
1936 | } | |
1937 | if (*arg1 == 0) | |
1938 | { | |
1939 | if (*arg2 == 0 && type1 != type2) | |
1940 | cp_error ("default type conversion for types `%T' and `%T' failed", | |
1941 | type1, type2); | |
1942 | else | |
1943 | cp_error ("default type conversion for type `%T' failed", type1); | |
1944 | return 0; | |
1945 | } | |
1946 | else if (*arg2 == 0) | |
1947 | { | |
1948 | cp_error ("default type conversion for type `%T' failed", type2); | |
1949 | return 0; | |
1950 | } | |
1951 | return 1; | |
1952 | } | |
1953 | ||
1954 | /* Must convert two aggregate types to non-aggregate type. | |
1955 | Attempts to find a non-ambiguous, "best" type conversion. | |
1956 | ||
1957 | Return 1 on success, 0 on failure. | |
1958 | ||
1959 | The type of the argument is expected to be of aggregate type here. | |
1960 | ||
1961 | @@ What are the real semantics of this supposed to be??? */ | |
1962 | int | |
1963 | build_default_unary_type_conversion (code, arg) | |
1964 | enum tree_code code; | |
1965 | tree *arg; | |
1966 | { | |
1967 | tree type = TREE_TYPE (*arg); | |
1968 | tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
1969 | ? TYPE_IDENTIFIER (type) : TYPE_NAME (type); | |
1970 | char *name = IDENTIFIER_POINTER (id); | |
1971 | ||
1972 | if (! TYPE_HAS_CONVERSION (type)) | |
1973 | { | |
1974 | error ("type conversion required for type `%s'", name); | |
1975 | return 0; | |
1976 | } | |
1977 | ||
1978 | if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type)) | |
1979 | warning ("ambiguous type conversion for type `%s', defaulting to int", | |
1980 | name); | |
1981 | if (TYPE_HAS_INT_CONVERSION (type)) | |
1982 | *arg = build_type_conversion (code, integer_type_node, *arg, 1); | |
1983 | else if (TYPE_HAS_REAL_CONVERSION (type)) | |
1984 | *arg = build_type_conversion (code, double_type_node, *arg, 1); | |
1985 | else | |
1986 | { | |
1987 | *arg = build_type_conversion (code, ptr_type_node, *arg, 1); | |
1988 | if (*arg == error_mark_node) | |
1989 | error ("ambiguous pointer conversion"); | |
1990 | } | |
1991 | if (*arg == NULL_TREE) | |
1992 | { | |
1993 | error ("default type conversion for type `%s' failed", name); | |
1994 | return 0; | |
1995 | } | |
1996 | return 1; | |
1997 | } |