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