1/* Utility routines for data type conversion for GNU C.
2   Copyright (C) 1987, 88, 91-95, 97, 1998 Free Software Foundation, Inc.
3
4This file is part of GNU C.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING.  If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.  */
20
21
22/* These routines are somewhat language-independent utility function
23   intended to be called by the language-specific convert () functions.  */
24
25#include "config.h"
26#include "tree.h"
27#include "flags.h"
28#include "convert.h"
29#include "toplev.h"
30
31/* Convert EXPR to some pointer or reference type TYPE.
32
33   EXPR must be pointer, reference, integer, enumeral, or literal zero;
34   in other cases error is called.  */
35
36tree
37convert_to_pointer (type, expr)
38     tree type, expr;
39{
40  if (integer_zerop (expr))
41    {
42      expr = build_int_2 (0, 0);
43      TREE_TYPE (expr) = type;
44      return expr;
45    }
46
47  switch (TREE_CODE (TREE_TYPE (expr)))
48    {
49    case POINTER_TYPE:
50    case REFERENCE_TYPE:
51      return build1 (NOP_EXPR, type, expr);
52
53    case INTEGER_TYPE:
54    case ENUMERAL_TYPE:
55    case BOOLEAN_TYPE:
56    case CHAR_TYPE:
57      if (TYPE_PRECISION (TREE_TYPE (expr)) == POINTER_SIZE)
58	return build1 (CONVERT_EXPR, type, expr);
59
60      return
61	convert_to_pointer (type,
62			    convert (type_for_size (POINTER_SIZE, 0), expr));
63
64    default:
65      error ("cannot convert to a pointer type");
66      return convert_to_pointer (type, integer_zero_node);
67    }
68}
69
70/* Convert EXPR to some floating-point type TYPE.
71
72   EXPR must be float, integer, or enumeral;
73   in other cases error is called.  */
74
75tree
76convert_to_real (type, expr)
77     tree type, expr;
78{
79  switch (TREE_CODE (TREE_TYPE (expr)))
80    {
81    case REAL_TYPE:
82      return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
83		     type, expr);
84
85    case INTEGER_TYPE:
86    case ENUMERAL_TYPE:
87    case BOOLEAN_TYPE:
88    case CHAR_TYPE:
89      return build1 (FLOAT_EXPR, type, expr);
90
91    case COMPLEX_TYPE:
92      return convert (type,
93		      fold (build1 (REALPART_EXPR,
94				    TREE_TYPE (TREE_TYPE (expr)), expr)));
95
96    case POINTER_TYPE:
97    case REFERENCE_TYPE:
98      error ("pointer value used where a floating point value was expected");
99      return convert_to_real (type, integer_zero_node);
100
101    default:
102      error ("aggregate value used where a float was expected");
103      return convert_to_real (type, integer_zero_node);
104    }
105}
106
107/* Convert EXPR to some integer (or enum) type TYPE.
108
109   EXPR must be pointer, integer, discrete (enum, char, or bool), or float;
110   in other cases error is called.
111
112   The result of this is always supposed to be a newly created tree node
113   not in use in any existing structure.  */
114
115tree
116convert_to_integer (type, expr)
117     tree type, expr;
118{
119  enum tree_code ex_form = TREE_CODE (expr);
120  tree intype = TREE_TYPE (expr);
121  int inprec = TYPE_PRECISION (intype);
122  int outprec = TYPE_PRECISION (type);
123
124  /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
125     be.  Consider `enum E = { a, b = (enum E) 3 };'.  */
126  if (!TYPE_SIZE (type))
127    {
128      error ("conversion to incomplete type");
129      return error_mark_node;
130    }
131
132  switch (TREE_CODE (intype))
133    {
134    case POINTER_TYPE:
135    case REFERENCE_TYPE:
136      if (integer_zerop (expr))
137	expr = integer_zero_node;
138      else
139	expr = fold (build1 (CONVERT_EXPR,
140			     type_for_size (POINTER_SIZE, 0), expr));
141
142      return convert_to_integer (type, expr);
143
144    case INTEGER_TYPE:
145    case ENUMERAL_TYPE:
146    case BOOLEAN_TYPE:
147    case CHAR_TYPE:
148      /* If this is a logical operation, which just returns 0 or 1, we can
149	 change the type of the expression.  For some logical operations,
150	 we must also change the types of the operands to maintain type
151	 correctness.  */
152
153      if (TREE_CODE_CLASS (ex_form) == '<')
154	{
155	  TREE_TYPE (expr) = type;
156	  return expr;
157	}
158
159      else if (ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
160	       || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
161	       || ex_form == TRUTH_XOR_EXPR)
162	{
163	  TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
164	  TREE_OPERAND (expr, 1) = convert (type, TREE_OPERAND (expr, 1));
165	  TREE_TYPE (expr) = type;
166	  return expr;
167	}
168
169      else if (ex_form == TRUTH_NOT_EXPR)
170	{
171	  TREE_OPERAND (expr, 0) = convert (type, TREE_OPERAND (expr, 0));
172	  TREE_TYPE (expr) = type;
173	  return expr;
174	}
175
176      /* If we are widening the type, put in an explicit conversion.
177	 Similarly if we are not changing the width.  After this, we know
178	 we are truncating EXPR.  */
179
180      else if (outprec >= inprec)
181	return build1 (NOP_EXPR, type, expr);
182
183      /* If TYPE is an enumeral type or a type with a precision less
184	 than the number of bits in its mode, do the conversion to the
185	 type corresponding to its mode, then do a nop conversion
186	 to TYPE.  */
187      else if (TREE_CODE (type) == ENUMERAL_TYPE
188	       || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
189	return build1 (NOP_EXPR, type,
190		       convert (type_for_mode (TYPE_MODE (type),
191					       TREE_UNSIGNED (type)),
192				expr));
193
194      /* Here detect when we can distribute the truncation down past some
195	 arithmetic.  For example, if adding two longs and converting to an
196	 int, we can equally well convert both to ints and then add.
197	 For the operations handled here, such truncation distribution
198	 is always safe.
199	 It is desirable in these cases:
200	 1) when truncating down to full-word from a larger size
201	 2) when truncating takes no work.
202	 3) when at least one operand of the arithmetic has been extended
203	 (as by C's default conversions).  In this case we need two conversions
204	 if we do the arithmetic as already requested, so we might as well
205	 truncate both and then combine.  Perhaps that way we need only one.
206
207	 Note that in general we cannot do the arithmetic in a type
208	 shorter than the desired result of conversion, even if the operands
209	 are both extended from a shorter type, because they might overflow
210	 if combined in that type.  The exceptions to this--the times when
211	 two narrow values can be combined in their narrow type even to
212	 make a wider result--are handled by "shorten" in build_binary_op.  */
213
214      switch (ex_form)
215	{
216	case RSHIFT_EXPR:
217	  /* We can pass truncation down through right shifting
218	     when the shift count is a nonpositive constant.  */
219	  if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
220	      && tree_int_cst_lt (TREE_OPERAND (expr, 1),
221				  convert (TREE_TYPE (TREE_OPERAND (expr, 1)),
222					   integer_one_node)))
223	    goto trunc1;
224	  break;
225
226	case LSHIFT_EXPR:
227	  /* We can pass truncation down through left shifting
228	     when the shift count is a nonnegative constant.  */
229	  if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
230	      && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
231	      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
232	    {
233	      /* If shift count is less than the width of the truncated type,
234		 really shift.  */
235	      if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
236		/* In this case, shifting is like multiplication.  */
237		goto trunc1;
238	      else
239		{
240		  /* If it is >= that width, result is zero.
241		     Handling this with trunc1 would give the wrong result:
242		     (int) ((long long) a << 32) is well defined (as 0)
243		     but (int) a << 32 is undefined and would get a
244		     warning.  */
245
246		  tree t = convert_to_integer (type, integer_zero_node);
247
248		  /* If the original expression had side-effects, we must
249		     preserve it.  */
250		  if (TREE_SIDE_EFFECTS (expr))
251		    return build (COMPOUND_EXPR, type, expr, t);
252		  else
253		    return t;
254		}
255	    }
256	  break;
257
258	case MAX_EXPR:
259	case MIN_EXPR:
260	case MULT_EXPR:
261	  {
262	    tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
263	    tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
264
265	    /* Don't distribute unless the output precision is at least as big
266	       as the actual inputs.  Otherwise, the comparison of the
267	       truncated values will be wrong.  */
268	    if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
269		&& outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
270		/* If signedness of arg0 and arg1 don't match,
271		   we can't necessarily find a type to compare them in.  */
272		&& (TREE_UNSIGNED (TREE_TYPE (arg0))
273		    == TREE_UNSIGNED (TREE_TYPE (arg1))))
274	      goto trunc1;
275	    break;
276	  }
277
278	case PLUS_EXPR:
279	case MINUS_EXPR:
280	case BIT_AND_EXPR:
281	case BIT_IOR_EXPR:
282	case BIT_XOR_EXPR:
283	case BIT_ANDTC_EXPR:
284	trunc1:
285	  {
286	    tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
287	    tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
288
289	    if (outprec >= BITS_PER_WORD
290		|| TRULY_NOOP_TRUNCATION (outprec, inprec)
291		|| inprec > TYPE_PRECISION (TREE_TYPE (arg0))
292		|| inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
293	      {
294		/* Do the arithmetic in type TYPEX,
295		   then convert result to TYPE.  */
296		register tree typex = type;
297
298		/* Can't do arithmetic in enumeral types
299		   so use an integer type that will hold the values.  */
300		if (TREE_CODE (typex) == ENUMERAL_TYPE)
301		  typex = type_for_size (TYPE_PRECISION (typex),
302					 TREE_UNSIGNED (typex));
303
304		/* But now perhaps TYPEX is as wide as INPREC.
305		   In that case, do nothing special here.
306		   (Otherwise would recurse infinitely in convert.  */
307		if (TYPE_PRECISION (typex) != inprec)
308		  {
309		    /* Don't do unsigned arithmetic where signed was wanted,
310		       or vice versa.
311		       Exception: if either of the original operands were
312		       unsigned then can safely do the work as unsigned.
313		       And we may need to do it as unsigned
314		       if we truncate to the original size.  */
315		    typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
316			      || TREE_UNSIGNED (TREE_TYPE (arg0))
317			      || TREE_UNSIGNED (TREE_TYPE (arg1)))
318			     ? unsigned_type (typex) : signed_type (typex));
319		    return convert (type,
320				    fold (build (ex_form, typex,
321						 convert (typex, arg0),
322						 convert (typex, arg1),
323						 0)));
324		  }
325	      }
326	  }
327	  break;
328
329	case NEGATE_EXPR:
330	case BIT_NOT_EXPR:
331	  /* This is not correct for ABS_EXPR,
332	     since we must test the sign before truncation.  */
333	  {
334	    register tree typex = type;
335
336	    /* Can't do arithmetic in enumeral types
337	       so use an integer type that will hold the values.  */
338	    if (TREE_CODE (typex) == ENUMERAL_TYPE)
339	      typex = type_for_size (TYPE_PRECISION (typex),
340				     TREE_UNSIGNED (typex));
341
342	    /* But now perhaps TYPEX is as wide as INPREC.
343	       In that case, do nothing special here.
344	       (Otherwise would recurse infinitely in convert.  */
345	    if (TYPE_PRECISION (typex) != inprec)
346	      {
347		/* Don't do unsigned arithmetic where signed was wanted,
348		   or vice versa.  */
349		typex = (TREE_UNSIGNED (TREE_TYPE (expr))
350			 ? unsigned_type (typex) : signed_type (typex));
351		return convert (type,
352				fold (build1 (ex_form, typex,
353					      convert (typex,
354						       TREE_OPERAND (expr, 0)))));
355	      }
356	  }
357
358	case NOP_EXPR:
359	  /* If truncating after truncating, might as well do all at once.
360	     If truncating after extending, we may get rid of wasted work.  */
361	  return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
362
363	case COND_EXPR:
364	  /* It is sometimes worthwhile to push the narrowing down through
365	     the conditional and never loses.  */
366	  return fold (build (COND_EXPR, type, TREE_OPERAND (expr, 0),
367			      convert (type, TREE_OPERAND (expr, 1)),
368			      convert (type, TREE_OPERAND (expr, 2))));
369
370	default:
371	  break;
372	}
373
374      return build1 (NOP_EXPR, type, expr);
375
376    case REAL_TYPE:
377      return build1 (FIX_TRUNC_EXPR, type, expr);
378
379    case COMPLEX_TYPE:
380      return convert (type,
381		      fold (build1 (REALPART_EXPR,
382				    TREE_TYPE (TREE_TYPE (expr)), expr)));
383
384    default:
385      error ("aggregate value used where an integer was expected");
386      return convert (type, integer_zero_node);
387    }
388}
389
390/* Convert EXPR to the complex type TYPE in the usual ways.  */
391
392tree
393convert_to_complex (type, expr)
394     tree type, expr;
395{
396  tree subtype = TREE_TYPE (type);
397
398  switch (TREE_CODE (TREE_TYPE (expr)))
399    {
400    case REAL_TYPE:
401    case INTEGER_TYPE:
402    case ENUMERAL_TYPE:
403    case BOOLEAN_TYPE:
404    case CHAR_TYPE:
405      return build (COMPLEX_EXPR, type, convert (subtype, expr),
406		    convert (subtype, integer_zero_node));
407
408    case COMPLEX_TYPE:
409      {
410	tree elt_type = TREE_TYPE (TREE_TYPE (expr));
411
412	if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
413	  return expr;
414	else if (TREE_CODE (expr) == COMPLEX_EXPR)
415	  return fold (build (COMPLEX_EXPR,
416			      type,
417			      convert (subtype, TREE_OPERAND (expr, 0)),
418			      convert (subtype, TREE_OPERAND (expr, 1))));
419	else
420	  {
421	    expr = save_expr (expr);
422	    return
423	      fold (build (COMPLEX_EXPR,
424			   type, convert (subtype,
425					  fold (build1 (REALPART_EXPR,
426							TREE_TYPE (TREE_TYPE (expr)),
427							expr))),
428			   convert (subtype,
429				    fold (build1 (IMAGPART_EXPR,
430						  TREE_TYPE (TREE_TYPE (expr)),
431						  expr)))));
432	  }
433      }
434
435    case POINTER_TYPE:
436    case REFERENCE_TYPE:
437      error ("pointer value used where a complex was expected");
438      return convert_to_complex (type, integer_zero_node);
439
440    default:
441      error ("aggregate value used where a complex was expected");
442      return convert_to_complex (type, integer_zero_node);
443    }
444}
445