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