1*e4b17023SJohn Marino /* Language-level data type conversion for GNU C.
2*e4b17023SJohn Marino Copyright (C) 1987, 1988, 1991, 1998, 2002, 2003, 2004, 2005, 2007, 2008,
3*e4b17023SJohn Marino 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino /* This file contains the functions for converting C expressions
23*e4b17023SJohn Marino to different data types. The only entry point is `convert'.
24*e4b17023SJohn Marino Every language front end must have a `convert' function
25*e4b17023SJohn Marino but what kind of conversions it does will depend on the language. */
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino #include "config.h"
28*e4b17023SJohn Marino #include "system.h"
29*e4b17023SJohn Marino #include "coretypes.h"
30*e4b17023SJohn Marino #include "tm.h"
31*e4b17023SJohn Marino #include "tree.h"
32*e4b17023SJohn Marino #include "flags.h"
33*e4b17023SJohn Marino #include "convert.h"
34*e4b17023SJohn Marino #include "c-family/c-common.h"
35*e4b17023SJohn Marino #include "c-tree.h"
36*e4b17023SJohn Marino #include "langhooks.h"
37*e4b17023SJohn Marino #include "target.h"
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino /* Change of width--truncation and extension of integers or reals--
40*e4b17023SJohn Marino is represented with NOP_EXPR. Proper functioning of many things
41*e4b17023SJohn Marino assumes that no other conversions can be NOP_EXPRs.
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino Conversion between integer and pointer is represented with CONVERT_EXPR.
44*e4b17023SJohn Marino Converting integer to real uses FLOAT_EXPR
45*e4b17023SJohn Marino and real to integer uses FIX_TRUNC_EXPR.
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino Here is a list of all the functions that assume that widening and
48*e4b17023SJohn Marino narrowing is always done with a NOP_EXPR:
49*e4b17023SJohn Marino In convert.c, convert_to_integer.
50*e4b17023SJohn Marino In c-typeck.c, build_binary_op (boolean ops), and
51*e4b17023SJohn Marino c_common_truthvalue_conversion.
52*e4b17023SJohn Marino In expr.c: expand_expr, for operands of a MULT_EXPR.
53*e4b17023SJohn Marino In fold-const.c: fold.
54*e4b17023SJohn Marino In tree.c: get_narrower and get_unwidened. */
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino /* Subroutines of `convert'. */
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino /* Create an expression whose value is that of EXPR,
61*e4b17023SJohn Marino converted to type TYPE. The TREE_TYPE of the value
62*e4b17023SJohn Marino is always TYPE. This function implements all reasonable
63*e4b17023SJohn Marino conversions; callers should filter out those that are
64*e4b17023SJohn Marino not permitted by the language being compiled. */
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino tree
convert(tree type,tree expr)67*e4b17023SJohn Marino convert (tree type, tree expr)
68*e4b17023SJohn Marino {
69*e4b17023SJohn Marino tree e = expr;
70*e4b17023SJohn Marino enum tree_code code = TREE_CODE (type);
71*e4b17023SJohn Marino const char *invalid_conv_diag;
72*e4b17023SJohn Marino tree ret;
73*e4b17023SJohn Marino location_t loc = EXPR_LOCATION (expr);
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino if (type == error_mark_node
76*e4b17023SJohn Marino || expr == error_mark_node
77*e4b17023SJohn Marino || TREE_TYPE (expr) == error_mark_node)
78*e4b17023SJohn Marino return error_mark_node;
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino if ((invalid_conv_diag
81*e4b17023SJohn Marino = targetm.invalid_conversion (TREE_TYPE (expr), type)))
82*e4b17023SJohn Marino {
83*e4b17023SJohn Marino error (invalid_conv_diag);
84*e4b17023SJohn Marino return error_mark_node;
85*e4b17023SJohn Marino }
86*e4b17023SJohn Marino
87*e4b17023SJohn Marino if (type == TREE_TYPE (expr))
88*e4b17023SJohn Marino return expr;
89*e4b17023SJohn Marino ret = targetm.convert_to_type (type, expr);
90*e4b17023SJohn Marino if (ret)
91*e4b17023SJohn Marino return ret;
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino STRIP_TYPE_NOPS (e);
94*e4b17023SJohn Marino
95*e4b17023SJohn Marino if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr))
96*e4b17023SJohn Marino && (TREE_CODE (TREE_TYPE (expr)) != COMPLEX_TYPE
97*e4b17023SJohn Marino || TREE_CODE (e) == COMPLEX_EXPR))
98*e4b17023SJohn Marino return fold_convert_loc (loc, type, expr);
99*e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
100*e4b17023SJohn Marino return error_mark_node;
101*e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
102*e4b17023SJohn Marino {
103*e4b17023SJohn Marino error ("void value not ignored as it ought to be");
104*e4b17023SJohn Marino return error_mark_node;
105*e4b17023SJohn Marino }
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino switch (code)
108*e4b17023SJohn Marino {
109*e4b17023SJohn Marino case VOID_TYPE:
110*e4b17023SJohn Marino return fold_convert_loc (loc, type, e);
111*e4b17023SJohn Marino
112*e4b17023SJohn Marino case INTEGER_TYPE:
113*e4b17023SJohn Marino case ENUMERAL_TYPE:
114*e4b17023SJohn Marino ret = convert_to_integer (type, e);
115*e4b17023SJohn Marino goto maybe_fold;
116*e4b17023SJohn Marino
117*e4b17023SJohn Marino case BOOLEAN_TYPE:
118*e4b17023SJohn Marino return fold_convert_loc
119*e4b17023SJohn Marino (loc, type, c_objc_common_truthvalue_conversion (input_location, expr));
120*e4b17023SJohn Marino
121*e4b17023SJohn Marino case POINTER_TYPE:
122*e4b17023SJohn Marino case REFERENCE_TYPE:
123*e4b17023SJohn Marino ret = convert_to_pointer (type, e);
124*e4b17023SJohn Marino goto maybe_fold;
125*e4b17023SJohn Marino
126*e4b17023SJohn Marino case REAL_TYPE:
127*e4b17023SJohn Marino ret = convert_to_real (type, e);
128*e4b17023SJohn Marino goto maybe_fold;
129*e4b17023SJohn Marino
130*e4b17023SJohn Marino case FIXED_POINT_TYPE:
131*e4b17023SJohn Marino ret = convert_to_fixed (type, e);
132*e4b17023SJohn Marino goto maybe_fold;
133*e4b17023SJohn Marino
134*e4b17023SJohn Marino case COMPLEX_TYPE:
135*e4b17023SJohn Marino /* If converting from COMPLEX_TYPE to a different COMPLEX_TYPE
136*e4b17023SJohn Marino and e is not COMPLEX_EXPR, convert_to_complex uses save_expr,
137*e4b17023SJohn Marino but for the C FE c_save_expr needs to be called instead. */
138*e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (e)) == COMPLEX_TYPE)
139*e4b17023SJohn Marino {
140*e4b17023SJohn Marino if (TREE_CODE (e) != COMPLEX_EXPR)
141*e4b17023SJohn Marino {
142*e4b17023SJohn Marino tree subtype = TREE_TYPE (type);
143*e4b17023SJohn Marino tree elt_type = TREE_TYPE (TREE_TYPE (e));
144*e4b17023SJohn Marino
145*e4b17023SJohn Marino if (in_late_binary_op)
146*e4b17023SJohn Marino e = save_expr (e);
147*e4b17023SJohn Marino else
148*e4b17023SJohn Marino e = c_save_expr (e);
149*e4b17023SJohn Marino ret
150*e4b17023SJohn Marino = fold_build2_loc (loc, COMPLEX_EXPR, type,
151*e4b17023SJohn Marino convert (subtype,
152*e4b17023SJohn Marino fold_build1 (REALPART_EXPR,
153*e4b17023SJohn Marino elt_type, e)),
154*e4b17023SJohn Marino convert (subtype,
155*e4b17023SJohn Marino fold_build1 (IMAGPART_EXPR,
156*e4b17023SJohn Marino elt_type, e)));
157*e4b17023SJohn Marino goto maybe_fold;
158*e4b17023SJohn Marino }
159*e4b17023SJohn Marino }
160*e4b17023SJohn Marino ret = convert_to_complex (type, e);
161*e4b17023SJohn Marino goto maybe_fold;
162*e4b17023SJohn Marino
163*e4b17023SJohn Marino case VECTOR_TYPE:
164*e4b17023SJohn Marino ret = convert_to_vector (type, e);
165*e4b17023SJohn Marino goto maybe_fold;
166*e4b17023SJohn Marino
167*e4b17023SJohn Marino case RECORD_TYPE:
168*e4b17023SJohn Marino case UNION_TYPE:
169*e4b17023SJohn Marino if (lang_hooks.types_compatible_p (type, TREE_TYPE (expr)))
170*e4b17023SJohn Marino return e;
171*e4b17023SJohn Marino break;
172*e4b17023SJohn Marino
173*e4b17023SJohn Marino default:
174*e4b17023SJohn Marino break;
175*e4b17023SJohn Marino
176*e4b17023SJohn Marino maybe_fold:
177*e4b17023SJohn Marino if (TREE_CODE (ret) != C_MAYBE_CONST_EXPR)
178*e4b17023SJohn Marino ret = fold (ret);
179*e4b17023SJohn Marino return ret;
180*e4b17023SJohn Marino }
181*e4b17023SJohn Marino
182*e4b17023SJohn Marino error ("conversion to non-scalar type requested");
183*e4b17023SJohn Marino return error_mark_node;
184*e4b17023SJohn Marino }
185