1/* Data type conversion
2   Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3.  If not see
18<http://www.gnu.org/licenses/>.  */
19
20
21/* This file contains the functions for converting expressions to
22   different data types for the translation of the gfortran internal
23   representation to GIMPLE.  The only entry point is `convert'.  */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
28#include "hash-set.h"
29#include "machmode.h"
30#include "vec.h"
31#include "double-int.h"
32#include "input.h"
33#include "alias.h"
34#include "symtab.h"
35#include "options.h"
36#include "wide-int.h"
37#include "inchash.h"
38#include "tree.h"
39#include "fold-const.h"
40#include "convert.h"
41
42/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
43   or validate its data type for a GIMPLE `if' or `while' statement.
44
45   The resulting type should always be `boolean_type_node'.  */
46
47static tree
48truthvalue_conversion (tree expr)
49{
50  switch (TREE_CODE (TREE_TYPE (expr)))
51    {
52    case BOOLEAN_TYPE:
53      if (TREE_TYPE (expr) == boolean_type_node)
54	return expr;
55      else if (COMPARISON_CLASS_P (expr))
56	{
57	  TREE_TYPE (expr) = boolean_type_node;
58	  return expr;
59	}
60      else if (TREE_CODE (expr) == NOP_EXPR)
61        return fold_build1_loc (input_location, NOP_EXPR,
62			    boolean_type_node, TREE_OPERAND (expr, 0));
63      else
64        return fold_build1_loc (input_location, NOP_EXPR, boolean_type_node,
65				expr);
66
67    case INTEGER_TYPE:
68      if (TREE_CODE (expr) == INTEGER_CST)
69	return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
70      else
71        return fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
72				expr, build_int_cst (TREE_TYPE (expr), 0));
73
74    default:
75      gcc_unreachable ();
76    }
77}
78
79/* Create an expression whose value is that of EXPR,
80   converted to type TYPE.  The TREE_TYPE of the value
81   is always TYPE.  This function implements all reasonable
82   conversions; callers should filter out those that are
83   not permitted by the language being compiled.  */
84
85tree
86convert (tree type, tree expr)
87{
88  tree e = expr;
89  enum tree_code code;
90
91  if (type == TREE_TYPE (expr))
92    return expr;
93
94  if (TREE_CODE (type) == ERROR_MARK
95      || TREE_CODE (expr) == ERROR_MARK
96      || TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
97    return expr;
98
99  gcc_checking_assert (TREE_CODE (TREE_TYPE (expr)) != VOID_TYPE);
100
101  if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
102    return fold_build1_loc (input_location, NOP_EXPR, type, expr);
103
104  code = TREE_CODE (type);
105  if (code == VOID_TYPE)
106    return fold_build1_loc (input_location, CONVERT_EXPR, type, e);
107  if (code == BOOLEAN_TYPE)
108    return fold_build1_loc (input_location, NOP_EXPR, type,
109			    truthvalue_conversion (e));
110  if (code == INTEGER_TYPE)
111    return fold (convert_to_integer (type, e));
112  if (code == POINTER_TYPE || code == REFERENCE_TYPE)
113    return fold (convert_to_pointer (type, e));
114  if (code == REAL_TYPE)
115    return fold (convert_to_real (type, e));
116  if (code == COMPLEX_TYPE)
117    return fold (convert_to_complex (type, e));
118  if (code == VECTOR_TYPE)
119    return fold (convert_to_vector (type, e));
120
121  gcc_unreachable ();
122}
123
124