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