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