1 /* Data type conversion
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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'. */
27 #include "coretypes.h"
31 #include "fold-const.h"
34 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
35 or validate its data type for a GIMPLE `if' or `while' statement.
37 The resulting type should always be `boolean_type_node'. */
40 truthvalue_conversion (tree expr
)
42 switch (TREE_CODE (TREE_TYPE (expr
)))
45 if (TREE_TYPE (expr
) == boolean_type_node
)
47 else if (COMPARISON_CLASS_P (expr
))
49 TREE_TYPE (expr
) = boolean_type_node
;
52 else if (TREE_CODE (expr
) == NOP_EXPR
)
53 return fold_build1_loc (input_location
, NOP_EXPR
,
54 boolean_type_node
, TREE_OPERAND (expr
, 0));
56 return fold_build1_loc (input_location
, NOP_EXPR
, boolean_type_node
,
60 if (TREE_CODE (expr
) == INTEGER_CST
)
61 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
63 return fold_build2_loc (input_location
, NE_EXPR
, boolean_type_node
,
64 expr
, build_int_cst (TREE_TYPE (expr
), 0));
71 /* Create an expression whose value is that of EXPR,
72 converted to type TYPE. The TREE_TYPE of the value
73 is always TYPE. This function implements all reasonable
74 conversions; callers should filter out those that are
75 not permitted by the language being compiled. */
78 convert (tree type
, tree expr
)
83 if (type
== TREE_TYPE (expr
))
86 if (TREE_CODE (type
) == ERROR_MARK
87 || TREE_CODE (expr
) == ERROR_MARK
88 || TREE_CODE (TREE_TYPE (expr
)) == ERROR_MARK
)
91 gcc_checking_assert (TREE_CODE (TREE_TYPE (expr
)) != VOID_TYPE
);
93 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
94 return fold_build1_loc (input_location
, NOP_EXPR
, type
, expr
);
96 code
= TREE_CODE (type
);
97 if (code
== VOID_TYPE
)
98 return fold_build1_loc (input_location
, CONVERT_EXPR
, type
, e
);
99 if (code
== BOOLEAN_TYPE
)
100 return fold_build1_loc (input_location
, NOP_EXPR
, type
,
101 truthvalue_conversion (e
));
102 if (code
== INTEGER_TYPE
)
103 return fold (convert_to_integer (type
, e
));
104 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
)
105 return fold (convert_to_pointer (type
, e
));
106 if (code
== REAL_TYPE
)
107 return fold (convert_to_real (type
, e
));
108 if (code
== COMPLEX_TYPE
)
109 return fold (convert_to_complex (type
, e
));
110 if (code
== VECTOR_TYPE
)
111 return fold (convert_to_vector (type
, e
));