]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/convert.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / fortran / convert.c
1 /* Data type conversion
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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/>. */
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 "vec.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "convert.h"
38
39 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
40 or validate its data type for a GIMPLE `if' or `while' statement.
41
42 The resulting type should always be `boolean_type_node'. */
43
44 static tree
45 truthvalue_conversion (tree expr)
46 {
47 switch (TREE_CODE (TREE_TYPE (expr)))
48 {
49 case BOOLEAN_TYPE:
50 if (TREE_TYPE (expr) == boolean_type_node)
51 return expr;
52 else if (COMPARISON_CLASS_P (expr))
53 {
54 TREE_TYPE (expr) = boolean_type_node;
55 return expr;
56 }
57 else if (TREE_CODE (expr) == NOP_EXPR)
58 return fold_build1_loc (input_location, NOP_EXPR,
59 boolean_type_node, TREE_OPERAND (expr, 0));
60 else
61 return fold_build1_loc (input_location, NOP_EXPR, boolean_type_node,
62 expr);
63
64 case INTEGER_TYPE:
65 if (TREE_CODE (expr) == INTEGER_CST)
66 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
67 else
68 return fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
69 expr, build_int_cst (TREE_TYPE (expr), 0));
70
71 default:
72 gcc_unreachable ();
73 }
74 }
75
76 /* Create an expression whose value is that of EXPR,
77 converted to type TYPE. The TREE_TYPE of the value
78 is always TYPE. This function implements all reasonable
79 conversions; callers should filter out those that are
80 not permitted by the language being compiled. */
81
82 tree
83 convert (tree type, tree expr)
84 {
85 tree e = expr;
86 enum tree_code code;
87
88 if (type == TREE_TYPE (expr))
89 return expr;
90
91 if (TREE_CODE (type) == ERROR_MARK
92 || TREE_CODE (expr) == ERROR_MARK
93 || TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
94 return expr;
95
96 gcc_checking_assert (TREE_CODE (TREE_TYPE (expr)) != VOID_TYPE);
97
98 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
99 return fold_build1_loc (input_location, NOP_EXPR, type, expr);
100
101 code = TREE_CODE (type);
102 if (code == VOID_TYPE)
103 return fold_build1_loc (input_location, CONVERT_EXPR, type, e);
104 if (code == BOOLEAN_TYPE)
105 return fold_build1_loc (input_location, NOP_EXPR, type,
106 truthvalue_conversion (e));
107 if (code == INTEGER_TYPE)
108 return fold (convert_to_integer (type, e));
109 if (code == POINTER_TYPE || code == REFERENCE_TYPE)
110 return fold (convert_to_pointer (type, e));
111 if (code == REAL_TYPE)
112 return fold (convert_to_real (type, e));
113 if (code == COMPLEX_TYPE)
114 return fold (convert_to_complex (type, e));
115 if (code == VECTOR_TYPE)
116 return fold (convert_to_vector (type, e));
117
118 gcc_unreachable ();
119 }
120