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