PR ipa/pr67600
[platform/upstream/gcc.git] / gcc / fortran / trans-const.c
1 /* Translation of constants
2    Copyright (C) 2002-2015 Free Software Foundation, Inc.
3    Contributed by Paul Brook
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* trans-const.c -- convert constant values */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "gfortran.h"
27 #include "alias.h"
28 #include "tree.h"
29 #include "options.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "realmpfr.h"
33 #include "diagnostic-core.h"    /* For fatal_error.  */
34 #include "trans.h"
35 #include "trans-const.h"
36 #include "trans-types.h"
37 #include "target-memory.h"
38
39 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
40
41 /* Build a constant with given type from an int_cst.  */
42
43 tree
44 gfc_build_const (tree type, tree intval)
45 {
46   tree val;
47   tree zero;
48
49   switch (TREE_CODE (type))
50     {
51     case INTEGER_TYPE:
52       val = convert (type, intval);
53       break;
54
55     case REAL_TYPE:
56       val = build_real_from_int_cst (type, intval);
57       break;
58
59     case COMPLEX_TYPE:
60       val = build_real_from_int_cst (TREE_TYPE (type), intval);
61       zero = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
62       val = build_complex (type, val, zero);
63       break;
64
65     default:
66       gcc_unreachable ();
67     }
68   return val;
69 }
70
71 /* Build a string constant with C char type.  */
72
73 tree
74 gfc_build_string_const (int length, const char *s)
75 {
76   tree str;
77   tree len;
78
79   str = build_string (length, s);
80   len = size_int (length);
81   TREE_TYPE (str) =
82     build_array_type (gfc_character1_type_node,
83                       build_range_type (gfc_charlen_type_node,
84                                         size_one_node, len));
85   TYPE_STRING_FLAG (TREE_TYPE (str)) = 1;
86   return str;
87 }
88
89
90 /* Build a string constant with a type given by its kind; take care of
91    non-default character kinds.  */
92
93 tree
94 gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string)
95 {
96   int i;
97   tree str, len;
98   size_t size;
99   char *s;
100
101   i = gfc_validate_kind (BT_CHARACTER, kind, false);
102   size = length * gfc_character_kinds[i].bit_size / 8;
103
104   s = XCNEWVAR (char, size);
105   gfc_encode_character (kind, length, string, (unsigned char *) s, size);
106
107   str = build_string (size, s);
108   free (s);
109
110   len = size_int (length);
111   TREE_TYPE (str) =
112     build_array_type (gfc_get_char_type (kind),
113                       build_range_type (gfc_charlen_type_node,
114                                         size_one_node, len));
115   TYPE_STRING_FLAG (TREE_TYPE (str)) = 1;
116   return str;
117 }
118
119
120 /* Build a Fortran character constant from a zero-terminated string.
121    There a two version of this function, one that translates the string
122    and one that doesn't.  */
123 tree
124 gfc_build_cstring_const (const char *string)
125 {
126   return gfc_build_string_const (strlen (string) + 1, string);
127 }
128
129 tree
130 gfc_build_localized_cstring_const (const char *msgid)
131 {
132   const char *localized = _(msgid);
133   return gfc_build_string_const (strlen (localized) + 1, localized);
134 }
135
136
137 /* Return a string constant with the given length.  Used for static
138    initializers.  The constant will be padded or truncated to match 
139    length.  */
140
141 tree
142 gfc_conv_string_init (tree length, gfc_expr * expr)
143 {
144   gfc_char_t *s;
145   HOST_WIDE_INT len;
146   int slen;
147   tree str;
148   bool free_s = false;
149
150   gcc_assert (expr->expr_type == EXPR_CONSTANT);
151   gcc_assert (expr->ts.type == BT_CHARACTER);
152   gcc_assert (tree_fits_uhwi_p (length));
153
154   len = TREE_INT_CST_LOW (length);
155   slen = expr->value.character.length;
156
157   if (len > slen)
158     {
159       s = gfc_get_wide_string (len);
160       memcpy (s, expr->value.character.string, slen * sizeof (gfc_char_t));
161       gfc_wide_memset (&s[slen], ' ', len - slen);
162       free_s = true;
163     }
164   else
165     s = expr->value.character.string;
166
167   str = gfc_build_wide_string_const (expr->ts.kind, len, s);
168
169   if (free_s)
170     free (s);
171
172   return str;
173 }
174
175
176 /* Create a tree node for the string length if it is constant.  */
177
178 void
179 gfc_conv_const_charlen (gfc_charlen * cl)
180 {
181   if (!cl || cl->backend_decl)
182     return;
183
184   if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
185     {
186       cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
187                                                cl->length->ts.kind);
188       cl->backend_decl = fold_convert (gfc_charlen_type_node,
189                                         cl->backend_decl);
190     }
191 }
192
193 void
194 gfc_init_constants (void)
195 {
196   int n;
197
198   for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
199     gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
200 }
201
202 /* Converts a GMP integer into a backend tree node.  */
203
204 tree
205 gfc_conv_mpz_to_tree (mpz_t i, int kind)
206 {
207   wide_int val = wi::from_mpz (gfc_get_int_type (kind), i, true);
208   return wide_int_to_tree (gfc_get_int_type (kind), val);
209 }
210
211 /* Converts a backend tree into a GMP integer.  */
212
213 void
214 gfc_conv_tree_to_mpz (mpz_t i, tree source)
215 {
216   wi::to_mpz (source, i, TYPE_SIGN (TREE_TYPE (source)));
217 }
218
219 /* Converts a real constant into backend form.  */
220
221 tree
222 gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan)
223 {
224   tree type;
225   int n;
226   REAL_VALUE_TYPE real;
227
228   n = gfc_validate_kind (BT_REAL, kind, false);
229   gcc_assert (gfc_real_kinds[n].radix == 2);
230
231   type = gfc_get_real_type (kind);
232   if (mpfr_nan_p (f) && is_snan)
233      real_from_string (&real, "SNaN");
234   else
235     real_from_mpfr (&real, f, type, GFC_RND_MODE);
236
237   return build_real (type, real);
238 }
239
240 /* Returns a real constant that is +Infinity if the target
241    supports infinities for this floating-point mode, and
242    +HUGE_VAL otherwise (the largest representable number).  */
243
244 tree
245 gfc_build_inf_or_huge (tree type, int kind)
246 {
247   if (HONOR_INFINITIES (TYPE_MODE (type)))
248     {
249       REAL_VALUE_TYPE real;
250       real_inf (&real);
251       return build_real (type, real);
252     }
253   else
254     {
255       int k = gfc_validate_kind (BT_REAL, kind, false);
256       return gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge, kind, 0);
257     }
258 }
259
260 /* Returns a floating-point NaN of a given type.  */
261
262 tree
263 gfc_build_nan (tree type, const char *str)
264 {
265   REAL_VALUE_TYPE real;
266   real_nan (&real, str, 1, TYPE_MODE (type));
267   return build_real (type, real);
268 }
269
270 /* Converts a backend tree into a real constant.  */
271
272 void
273 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
274 {
275   mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
276 }
277
278 /* Translate any literal constant to a tree.  Constants never have
279    pre or post chains.  Character literal constants are special
280    special because they have a value and a length, so they cannot be
281    returned as a single tree.  It is up to the caller to set the
282    length somewhere if necessary.
283
284    Returns the translated constant, or aborts if it gets a type it
285    can't handle.  */
286
287 tree
288 gfc_conv_constant_to_tree (gfc_expr * expr)
289 {
290   tree res;
291
292   gcc_assert (expr->expr_type == EXPR_CONSTANT);
293
294   /* If it is has a prescribed memory representation, we build a string
295      constant and VIEW_CONVERT to its type.  */
296  
297   switch (expr->ts.type)
298     {
299     case BT_INTEGER:
300       if (expr->representation.string)
301         return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
302                          gfc_get_int_type (expr->ts.kind),
303                          gfc_build_string_const (expr->representation.length,
304                                                  expr->representation.string));
305       else
306         return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
307
308     case BT_REAL:
309       if (expr->representation.string)
310         return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
311                          gfc_get_real_type (expr->ts.kind),
312                          gfc_build_string_const (expr->representation.length,
313                                                  expr->representation.string));
314       else
315         return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan);
316
317     case BT_LOGICAL:
318       if (expr->representation.string)
319         {
320           tree tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
321                         gfc_get_int_type (expr->ts.kind),
322                         gfc_build_string_const (expr->representation.length,
323                                                 expr->representation.string));
324           if (!integer_zerop (tmp) && !integer_onep (tmp))
325             gfc_warning (0, "Assigning value other than 0 or 1 to LOGICAL"
326                          " has undefined result at %L", &expr->where);
327           return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
328         }
329       else
330         return build_int_cst (gfc_get_logical_type (expr->ts.kind),
331                               expr->value.logical);
332
333     case BT_COMPLEX:
334       if (expr->representation.string)
335         return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
336                          gfc_get_complex_type (expr->ts.kind),
337                          gfc_build_string_const (expr->representation.length,
338                                                  expr->representation.string));
339       else
340         {
341           tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex),
342                                           expr->ts.kind, expr->is_snan);
343           tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex),
344                                           expr->ts.kind, expr->is_snan);
345
346           return build_complex (gfc_typenode_for_spec (&expr->ts),
347                                 real, imag);
348         }
349
350     case BT_CHARACTER:
351       res = gfc_build_wide_string_const (expr->ts.kind,
352                                          expr->value.character.length,
353                                          expr->value.character.string);
354       return res;
355
356     case BT_HOLLERITH:
357       return gfc_build_string_const (expr->representation.length,
358                                      expr->representation.string);
359
360     default:
361       fatal_error (input_location,
362                    "gfc_conv_constant_to_tree(): invalid type: %s",
363                    gfc_typename (&expr->ts));
364     }
365 }
366
367
368 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
369    We can handle character literal constants here as well.  */
370
371 void
372 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
373 {
374   gfc_ss *ss;
375
376   /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR.  If
377      so, the expr_type will not yet be an EXPR_CONSTANT.  We need to make
378      it so here.  */
379   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
380       && expr->ts.u.derived->attr.is_iso_c)
381     {
382       if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR 
383           || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
384         {
385           /* Create a new EXPR_CONSTANT expression for our local uses.  */
386           expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
387         }
388     }
389
390   if (expr->expr_type != EXPR_CONSTANT)
391     {
392       gfc_expr *e = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
393       gfc_error ("non-constant initialization expression at %L", &expr->where);
394       se->expr = gfc_conv_constant_to_tree (e);
395       return;
396     }
397
398   ss = se->ss;
399   if (ss != NULL)
400     {
401       gfc_ss_info *ss_info;
402
403       ss_info = ss->info;
404       gcc_assert (ss != gfc_ss_terminator);
405       gcc_assert (ss_info->type == GFC_SS_SCALAR);
406       gcc_assert (ss_info->expr == expr);
407
408       se->expr = ss_info->data.scalar.value;
409       se->string_length = ss_info->string_length;
410       gfc_advance_se_ss_chain (se);
411       return;
412     }
413
414   /* Translate the constant and put it in the simplifier structure.  */
415   se->expr = gfc_conv_constant_to_tree (expr);
416
417   /* If this is a CHARACTER string, set its length in the simplifier
418      structure, too.  */
419   if (expr->ts.type == BT_CHARACTER)
420     se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
421 }