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