]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/trans-types.cc
cobol: Respect error in cobol.install-common.
[thirdparty/gcc.git] / gcc / fortran / trans-types.cc
CommitLineData
6de9cd9a 1/* Backend support for Fortran 95 basic types and derived types.
6441eb6d 2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
5
9fc4d79b 6This file is part of GCC.
6de9cd9a 7
9fc4d79b
TS
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
d234d788 10Software Foundation; either version 3, or (at your option) any later
9fc4d79b 11version.
6de9cd9a 12
9fc4d79b
TS
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6de9cd9a
DN
17
18You should have received a copy of the GNU General Public License
d234d788
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a 21
e53b6e56 22/* trans-types.cc -- gfortran backend types */
6de9cd9a
DN
23
24#include "config.h"
25#include "system.h"
26#include "coretypes.h"
2adfab87 27#include "target.h"
6de9cd9a 28#include "tree.h"
2adfab87
AM
29#include "gfortran.h"
30#include "trans.h"
31#include "stringpool.h"
40e23961 32#include "fold-const.h"
d8a2d370 33#include "stor-layout.h"
a48ba7e1 34#include "langhooks.h" /* For iso-c-bindings.def. */
c829d016 35#include "toplev.h" /* For rest_of_decl_compilation. */
6de9cd9a
DN
36#include "trans-types.h"
37#include "trans-const.h"
ff3598bc 38#include "trans-array.h"
a48ba7e1 39#include "dwarf2out.h" /* For struct array_descr_info. */
314e6352 40#include "attribs.h"
40cb3f8a 41#include "alias.h"
6de9cd9a
DN
42\f
43
44#if (GFC_MAX_DIMENSIONS < 10)
45#define GFC_RANK_DIGITS 1
46#define GFC_RANK_PRINTF_FORMAT "%01d"
47#elif (GFC_MAX_DIMENSIONS < 100)
48#define GFC_RANK_DIGITS 2
49#define GFC_RANK_PRINTF_FORMAT "%02d"
50#else
51#error If you really need >99 dimensions, continue the sequence above...
52#endif
53
a8b3b0b6
CR
54/* array of structs so we don't have to worry about xmalloc or free */
55CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
56
6de9cd9a 57tree gfc_array_index_type;
b4838d29 58tree gfc_array_range_type;
7e6de2a5 59tree gfc_character1_type_node;
6de9cd9a 60tree pvoid_type_node;
10174ddf 61tree prvoid_type_node;
6de9cd9a
DN
62tree ppvoid_type_node;
63tree pchar_type_node;
fd39c4bf 64static tree pfunc_type_node;
7e6de2a5 65
63ee5404
JB
66tree logical_type_node;
67tree logical_true_node;
68tree logical_false_node;
d7177ab2 69tree gfc_charlen_type_node;
6de9cd9a 70
c65699ef
JM
71tree gfc_float128_type_node = NULL_TREE;
72tree gfc_complex_float128_type_node = NULL_TREE;
a3c85b74
FXC
73
74bool gfc_real16_is_float128 = false;
133d0d42 75bool gfc_real16_use_iec_60559 = false;
a3c85b74 76
e2cad04b 77static GTY(()) tree gfc_desc_dim_type;
6de9cd9a 78static GTY(()) tree gfc_max_array_element_size;
c62c6622
TB
79static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
80static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
64f96237 81static GTY(()) tree gfc_cfi_descriptor_base[2 * (CFI_MAX_RANK + 2)];
6de9cd9a 82
5e8e542f
RH
83/* Arrays for all integral and real kinds. We'll fill this in at runtime
84 after the target has a chance to process command-line options. */
85
86#define MAX_INT_KINDS 5
87gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
88gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
113a6da9 89gfc_unsigned_info gfc_unsigned_kinds[MAX_INT_KINDS + 1];
e2cad04b
RH
90static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
91static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
113a6da9 92static GTY(()) tree gfc_unsigned_types[MAX_INT_KINDS + 1];
5e8e542f 93
e8f35d4d 94#define MAX_REAL_KINDS 5
5e8e542f 95gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
e2cad04b
RH
96static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
97static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
5e8e542f 98
374929b2
FXC
99#define MAX_CHARACTER_KINDS 2
100gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
101static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
102static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
103
35151cd5 104static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
66e4ab31 105
5e8e542f
RH
106/* The integer kind to use for array indices. This will be set to the
107 proper value based on target information from the backend. */
108
109int gfc_index_integer_kind;
110
111/* The default kinds of the various types. */
112
9d64df18 113int gfc_default_integer_kind;
113a6da9 114int gfc_default_unsigned_kind;
f4e7375a 115int gfc_max_integer_kind;
9d64df18
TS
116int gfc_default_real_kind;
117int gfc_default_double_kind;
118int gfc_default_character_kind;
119int gfc_default_logical_kind;
120int gfc_default_complex_kind;
e8525382 121int gfc_c_int_kind;
d09131ee 122int gfc_c_uint_kind;
a6d22fb2 123int gfc_c_intptr_kind;
da661a58
TB
124int gfc_atomic_int_kind;
125int gfc_atomic_logical_kind;
5e8e542f 126
4fec64b0
JD
127/* The kind size used for record offsets. If the target system supports
128 kind=8, this will be set to 8, otherwise it is set to 4. */
f04986a9 129int gfc_intio_kind;
4fec64b0 130
f1412ca5
FXC
131/* The integer kind used to store character lengths. */
132int gfc_charlen_int_kind;
133
f622221a
JB
134/* Kind of internal integer for storing object sizes. */
135int gfc_size_kind;
136
39f87c03
FXC
137/* The size of the numeric storage unit and character storage unit. */
138int gfc_numeric_storage_size;
139int gfc_character_storage_size;
140
fd39c4bf 141static tree dtype_type_node = NULL_TREE;
7fb43006
PT
142
143
144/* Build the dtype_type_node if necessary. */
145tree get_dtype_type_node (void)
146{
147 tree field;
148 tree dtype_node;
149 tree *dtype_chain = NULL;
150
151 if (dtype_type_node == NULL_TREE)
152 {
153 dtype_node = make_node (RECORD_TYPE);
154 TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
155 TYPE_NAMELESS (dtype_node) = 1;
156 field = gfc_add_field_to_struct_1 (dtype_node,
157 get_identifier ("elem_len"),
158 size_type_node, &dtype_chain);
d5e69948 159 suppress_warning (field);
7fb43006
PT
160 field = gfc_add_field_to_struct_1 (dtype_node,
161 get_identifier ("version"),
162 integer_type_node, &dtype_chain);
d5e69948 163 suppress_warning (field);
7fb43006
PT
164 field = gfc_add_field_to_struct_1 (dtype_node,
165 get_identifier ("rank"),
166 signed_char_type_node, &dtype_chain);
d5e69948 167 suppress_warning (field);
7fb43006
PT
168 field = gfc_add_field_to_struct_1 (dtype_node,
169 get_identifier ("type"),
170 signed_char_type_node, &dtype_chain);
d5e69948 171 suppress_warning (field);
7fb43006
PT
172 field = gfc_add_field_to_struct_1 (dtype_node,
173 get_identifier ("attribute"),
174 short_integer_type_node, &dtype_chain);
d5e69948 175 suppress_warning (field);
7fb43006
PT
176 gfc_finish_type (dtype_node);
177 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
178 dtype_type_node = dtype_node;
179 }
180 return dtype_type_node;
181}
a8b3b0b6 182
a8b3b0b6
CR
183static int
184get_real_kind_from_node (tree type)
185{
186 int i;
187
188 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
189 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
de6969fd
KL
190 {
191 /* On Power, we have three 128-bit scalar floating-point modes
192 and all of their types have 128 bit type precision, so we
193 should check underlying real format details further. */
194#if defined(HAVE_TFmode) && defined(HAVE_IFmode) && defined(HAVE_KFmode)
195 if (gfc_real_kinds[i].kind == 16)
196 {
197 machine_mode mode = TYPE_MODE (type);
198 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
199 if (fmt->p != gfc_real_kinds[i].digits)
200 continue;
201 }
202#endif
203 return gfc_real_kinds[i].kind;
204 }
a8b3b0b6
CR
205
206 return -4;
207}
208
209static int
210get_int_kind_from_node (tree type)
211{
212 int i;
213
214 if (!type)
215 return -2;
216
217 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
218 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
219 return gfc_integer_kinds[i].kind;
220
221 return -1;
222}
223
e0a6661b
FXC
224static int
225get_int_kind_from_name (const char *name)
226{
227 return get_int_kind_from_node (get_typenode_from_name (name));
228}
229
d09131ee
TK
230static int
231get_unsigned_kind_from_node (tree type)
232{
233 int i;
234
235 if (!type)
236 return -2;
237
238 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
239 if (gfc_unsigned_kinds[i].bit_size == TYPE_PRECISION (type))
240 return gfc_unsigned_kinds[i].kind;
241
242 return -1;
243}
244
245static int
246get_uint_kind_from_name (const char *name)
247{
248 return get_unsigned_kind_from_node (get_typenode_from_name (name));
249}
e0a6661b
FXC
250
251/* Get the kind number corresponding to an integer of given size,
252 following the required return values for ISO_FORTRAN_ENV INT* constants:
253 -2 is returned if we support a kind of larger size, -1 otherwise. */
254int
255gfc_get_int_kind_from_width_isofortranenv (int size)
256{
257 int i;
258
259 /* Look for a kind with matching storage size. */
260 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
261 if (gfc_integer_kinds[i].bit_size == size)
262 return gfc_integer_kinds[i].kind;
263
264 /* Look for a kind with larger storage size. */
265 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
266 if (gfc_integer_kinds[i].bit_size > size)
267 return -2;
268
269 return -1;
270}
271
d09131ee
TK
272/* Same, but for unsigned. */
273
274int
275gfc_get_uint_kind_from_width_isofortranenv (int size)
276{
277 int i;
278
279 /* Look for a kind with matching storage size. */
280 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
281 if (gfc_unsigned_kinds[i].bit_size == size)
282 return gfc_unsigned_kinds[i].kind;
283
284 /* Look for a kind with larger storage size. */
285 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
286 if (gfc_unsigned_kinds[i].bit_size > size)
287 return -2;
288
289 return -1;
290}
291
26349ecf
SK
292
293/* Get the kind number corresponding to a real of a given storage size.
294 If two real's have the same storage size, then choose the real with
295 the largest precision. If a kind type is unavailable and a real
296 exists with wider storage, then return -2; otherwise, return -1. */
297
e0a6661b
FXC
298int
299gfc_get_real_kind_from_width_isofortranenv (int size)
300{
26349ecf 301 int digits, i, kind;
e0a6661b
FXC
302
303 size /= 8;
304
26349ecf
SK
305 kind = -1;
306 digits = 0;
307
e0a6661b
FXC
308 /* Look for a kind with matching storage size. */
309 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
310 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
26349ecf
SK
311 {
312 if (gfc_real_kinds[i].digits > digits)
313 {
314 digits = gfc_real_kinds[i].digits;
315 kind = gfc_real_kinds[i].kind;
316 }
317 }
318
319 if (kind != -1)
320 return kind;
e0a6661b
FXC
321
322 /* Look for a kind with larger storage size. */
323 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
324 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
26349ecf 325 kind = -2;
e0a6661b 326
26349ecf 327 return kind;
e0a6661b
FXC
328}
329
330
331
a8b3b0b6
CR
332static int
333get_int_kind_from_width (int size)
334{
335 int i;
336
337 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
338 if (gfc_integer_kinds[i].bit_size == size)
339 return gfc_integer_kinds[i].kind;
340
341 return -2;
342}
343
344static int
345get_int_kind_from_minimal_width (int size)
346{
347 int i;
348
349 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
350 if (gfc_integer_kinds[i].bit_size >= size)
351 return gfc_integer_kinds[i].kind;
352
353 return -2;
354}
355
d09131ee
TK
356static int
357get_uint_kind_from_width (int size)
358{
359 int i;
360
361 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
362 if (gfc_integer_kinds[i].bit_size == size)
363 return gfc_integer_kinds[i].kind;
364
365 return -2;
366}
367
a8b3b0b6
CR
368
369/* Generate the CInteropKind_t objects for the C interoperable
370 kinds. */
371
28d0b595
TB
372void
373gfc_init_c_interop_kinds (void)
a8b3b0b6
CR
374{
375 int i;
a8b3b0b6
CR
376
377 /* init all pointers in the list to NULL */
378 for (i = 0; i < ISOCBINDING_NUMBER; i++)
379 {
380 /* Initialize the name and value fields. */
381 c_interop_kinds_table[i].name[0] = '\0';
382 c_interop_kinds_table[i].value = -100;
383 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
384 }
385
21684705 386#define NAMED_INTCST(a,b,c,d) \
a8b3b0b6
CR
387 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
388 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
389 c_interop_kinds_table[a].value = c;
d09131ee
TK
390#define NAMED_UINTCST(a,b,c,d) \
391 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
392 c_interop_kinds_table[a].f90_type = BT_UNSIGNED; \
393 c_interop_kinds_table[a].value = c;
28d0b595 394#define NAMED_REALCST(a,b,c,d) \
a8b3b0b6
CR
395 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
396 c_interop_kinds_table[a].f90_type = BT_REAL; \
397 c_interop_kinds_table[a].value = c;
28d0b595 398#define NAMED_CMPXCST(a,b,c,d) \
a8b3b0b6
CR
399 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
400 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
401 c_interop_kinds_table[a].value = c;
402#define NAMED_LOGCST(a,b,c) \
403 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
404 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
405 c_interop_kinds_table[a].value = c;
406#define NAMED_CHARKNDCST(a,b,c) \
407 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
408 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
409 c_interop_kinds_table[a].value = c;
410#define NAMED_CHARCST(a,b,c) \
411 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
412 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
413 c_interop_kinds_table[a].value = c;
414#define DERIVED_TYPE(a,b,c) \
415 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
416 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
417 c_interop_kinds_table[a].value = c;
cadddfdd 418#define NAMED_FUNCTION(a,b,c,d) \
a8b3b0b6
CR
419 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
420 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
cadddfdd
TB
421 c_interop_kinds_table[a].value = c;
422#define NAMED_SUBROUTINE(a,b,c,d) \
d000aa67
TB
423 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
424 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
425 c_interop_kinds_table[a].value = c;
426#include "iso-c-binding.def"
a8b3b0b6
CR
427}
428
429
5e8e542f
RH
430/* Query the target to determine which machine modes are available for
431 computation. Choose KIND numbers for them. */
432
433void
434gfc_init_kinds (void)
435{
59b51186 436 opt_scalar_int_mode int_mode_iter;
5c20c4af 437 opt_scalar_float_mode float_mode_iter;
374929b2 438 int i_index, r_index, kind;
5e8e542f 439 bool saw_i4 = false, saw_i8 = false;
f4347334 440 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
90d6f0c7
JJ
441 scalar_mode r16_mode = QImode;
442 scalar_mode composite_mode = QImode;
5e8e542f 443
59b51186
RS
444 i_index = 0;
445 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
5e8e542f 446 {
59b51186 447 scalar_int_mode mode = int_mode_iter.require ();
5e8e542f
RH
448 int kind, bitsize;
449
b77d1a17 450 if (!targetm.scalar_mode_supported_p (mode))
5e8e542f
RH
451 continue;
452
04204c2f
RH
453 /* The middle end doesn't support constants larger than 2*HWI.
454 Perhaps the target hook shouldn't have accepted these either,
455 but just to be safe... */
b77d1a17 456 bitsize = GET_MODE_BITSIZE (mode);
04204c2f
RH
457 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
458 continue;
459
6e45f57b 460 gcc_assert (i_index != MAX_INT_KINDS);
5e8e542f
RH
461
462 /* Let the kind equal the bit size divided by 8. This insulates the
463 programmer from the underlying byte size. */
5e8e542f
RH
464 kind = bitsize / 8;
465
466 if (kind == 4)
467 saw_i4 = true;
468 if (kind == 8)
469 saw_i8 = true;
470
471 gfc_integer_kinds[i_index].kind = kind;
472 gfc_integer_kinds[i_index].radix = 2;
473 gfc_integer_kinds[i_index].digits = bitsize - 1;
474 gfc_integer_kinds[i_index].bit_size = bitsize;
475
113a6da9
TK
476 if (flag_unsigned)
477 {
478 gfc_unsigned_kinds[i_index].kind = kind;
479 gfc_unsigned_kinds[i_index].radix = 2;
480 gfc_unsigned_kinds[i_index].digits = bitsize;
481 gfc_unsigned_kinds[i_index].bit_size = bitsize;
482 }
483
5e8e542f
RH
484 gfc_logical_kinds[i_index].kind = kind;
485 gfc_logical_kinds[i_index].bit_size = bitsize;
486
487 i_index += 1;
488 }
489
f04986a9 490 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
4fec64b0
JD
491 used for large file access. */
492
493 if (saw_i8)
014ec6ee 494 gfc_intio_kind = 8;
4fec64b0 495 else
014ec6ee 496 gfc_intio_kind = 4;
4fec64b0 497
f04986a9
PT
498 /* If we do not at least have kind = 4, everything is pointless. */
499 gcc_assert(saw_i4);
4fec64b0 500
f4e7375a
SK
501 /* Set the maximum integer kind. Used with at least BOZ constants. */
502 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
503
5c20c4af
RS
504 r_index = 0;
505 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
5e8e542f 506 {
5c20c4af
RS
507 scalar_float_mode mode = float_mode_iter.require ();
508 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
5e8e542f
RH
509 int kind;
510
511 if (fmt == NULL)
512 continue;
b77d1a17 513 if (!targetm.scalar_mode_supported_p (mode))
5e8e542f
RH
514 continue;
515
90d6f0c7
JJ
516 if (MODE_COMPOSITE_P (mode)
517 && (GET_MODE_PRECISION (mode) + 7) / 8 == 16)
518 composite_mode = mode;
519
58340a7c 520 /* Only let float, double, long double and TFmode go through.
a3c85b74 521 Runtime support for others is not provided, so they would be
87e6d9dc 522 useless. */
5c20c4af 523 if (!targetm.libgcc_floating_mode_supported_p (mode))
21c0a521
DM
524 continue;
525 if (mode != TYPE_MODE (float_type_node)
1ec601bf
FXC
526 && (mode != TYPE_MODE (double_type_node))
527 && (mode != TYPE_MODE (long_double_type_node))
8cc4b7a2 528#if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
1ec601bf
FXC
529 && (mode != TFmode)
530#endif
531 )
216ac520
SE
532 continue;
533
5e8e542f
RH
534 /* Let the kind equal the precision divided by 8, rounding up. Again,
535 this insulates the programmer from the underlying byte size.
536
537 Also, it effectively deals with IEEE extended formats. There, the
538 total size of the type may equal 16, but it's got 6 bytes of padding
539 and the increased size can get in the way of a real IEEE quad format
540 which may also be supported by the target.
541
542 We round up so as to handle IA-64 __floatreg (RFmode), which is an
543 82 bit type. Not to be confused with __float80 (XFmode), which is
544 an 80 bit type also supported by IA-64. So XFmode should come out
58340a7c
SL
545 to be kind=10, and RFmode should come out to be kind=11. Egads.
546
547 TODO: The kind calculation has to be modified to support all
548 three 128-bit floating-point modes on PowerPC as IFmode, KFmode,
549 and TFmode since the following line would all map to kind=16.
550 However, currently only float, double, long double, and TFmode
551 reach this code.
552 */
5e8e542f
RH
553
554 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
555
556 if (kind == 4)
557 saw_r4 = true;
558 if (kind == 8)
559 saw_r8 = true;
f4347334
ZG
560 if (kind == 10)
561 saw_r10 = true;
5e8e542f 562 if (kind == 16)
90d6f0c7
JJ
563 {
564 saw_r16 = true;
565 r16_mode = mode;
566 }
5e8e542f 567
df2fba9e 568 /* Careful we don't stumble a weird internal mode. */
6e45f57b 569 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
5e8e542f 570 /* Or have too many modes for the allocated space. */
6e45f57b 571 gcc_assert (r_index != MAX_REAL_KINDS);
5e8e542f
RH
572
573 gfc_real_kinds[r_index].kind = kind;
90d6f0c7 574 gfc_real_kinds[r_index].abi_kind = kind;
5e8e542f
RH
575 gfc_real_kinds[r_index].radix = fmt->b;
576 gfc_real_kinds[r_index].digits = fmt->p;
577 gfc_real_kinds[r_index].min_exponent = fmt->emin;
578 gfc_real_kinds[r_index].max_exponent = fmt->emax;
c69cb92f
AM
579 if (fmt->pnan < fmt->p)
580 /* This is an IBM extended double format (or the MIPS variant)
581 made up of two IEEE doubles. The value of the long double is
582 the sum of the values of the two parts. The most significant
583 part is required to be the value of the long double rounded
584 to the nearest double. If we use emax of 1024 then we can't
585 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
586 rounding will make the most significant part overflow. */
587 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
e2cad04b 588 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
5e8e542f
RH
589 r_index += 1;
590 }
591
90d6f0c7
JJ
592 /* Detect the powerpc64le-linux case with -mabi=ieeelongdouble, where
593 the long double type is non-MODE_COMPOSITE_P TFmode but one can use
594 -mabi=ibmlongdouble too and get MODE_COMPOSITE_P TFmode with the same
595 precision. For libgfortran calls pretend the IEEE 754 quad TFmode has
596 kind 17 rather than 16 and use kind 16 for the IBM extended format
597 TFmode. */
598 if (composite_mode != QImode && saw_r16 && !MODE_COMPOSITE_P (r16_mode))
599 {
600 for (int i = 0; i < r_index; ++i)
601 if (gfc_real_kinds[i].kind == 16)
e79f6e61
JJ
602 {
603 gfc_real_kinds[i].abi_kind = 17;
604 if (flag_building_libgfortran
605 && (TARGET_GLIBC_MAJOR < 2
606 || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR < 32)))
607 {
133d0d42
JJ
608 if (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26)
609 {
610 gfc_real16_use_iec_60559 = true;
611 gfc_real_kinds[i].use_iec_60559 = 1;
612 }
e79f6e61
JJ
613 gfc_real16_is_float128 = true;
614 gfc_real_kinds[i].c_float128 = 1;
615 }
616 }
90d6f0c7 617 }
9acd5a01
JJ
618 else if ((flag_convert & (GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM)) != 0)
619 gfc_fatal_error ("%<-fconvert=r16_ieee%> or %<-fconvert=r16_ibm%> not "
620 "supported on this architecture");
90d6f0c7 621
f4347334
ZG
622 /* Choose the default integer kind. We choose 4 unless the user directs us
623 otherwise. Even if the user specified that the default integer kind is 8,
624 the numeric storage size is not 64 bits. In this case, a warning will be
625 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
626
627 gfc_numeric_storage_size = 4 * 8;
628
203c7ebf 629 if (flag_default_integer)
5e8e542f
RH
630 {
631 if (!saw_i8)
ddc05d11
TB
632 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
633 "%<-fdefault-integer-8%> option");
f4347334 634
9d64df18 635 gfc_default_integer_kind = 8;
39f87c03 636
f4347334 637 }
203c7ebf 638 else if (flag_integer4_kind == 8)
f4347334
ZG
639 {
640 if (!saw_i8)
ddc05d11
TB
641 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
642 "%<-finteger-4-integer-8%> option");
f4347334
ZG
643
644 gfc_default_integer_kind = 8;
5e8e542f
RH
645 }
646 else if (saw_i4)
39f87c03
FXC
647 {
648 gfc_default_integer_kind = 4;
39f87c03 649 }
5e8e542f 650 else
39f87c03
FXC
651 {
652 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
653 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
654 }
5e8e542f 655
113a6da9
TK
656 gfc_default_unsigned_kind = gfc_default_integer_kind;
657
5e8e542f 658 /* Choose the default real kind. Again, we choose 4 when possible. */
d16d81b5 659 if (flag_default_real_8)
5e8e542f
RH
660 {
661 if (!saw_r8)
ddc05d11
TB
662 gfc_fatal_error ("REAL(KIND=8) is not available for "
663 "%<-fdefault-real-8%> option");
f4347334 664
9d64df18 665 gfc_default_real_kind = 8;
5e8e542f 666 }
d16d81b5
JW
667 else if (flag_default_real_10)
668 {
669 if (!saw_r10)
670 gfc_fatal_error ("REAL(KIND=10) is not available for "
671 "%<-fdefault-real-10%> option");
672
673 gfc_default_real_kind = 10;
674 }
675 else if (flag_default_real_16)
676 {
677 if (!saw_r16)
678 gfc_fatal_error ("REAL(KIND=16) is not available for "
679 "%<-fdefault-real-16%> option");
680
681 gfc_default_real_kind = 16;
682 }
203c7ebf 683 else if (flag_real4_kind == 8)
f4347334
ZG
684 {
685 if (!saw_r8)
ddc05d11
TB
686 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
687 "option");
f4347334
ZG
688
689 gfc_default_real_kind = 8;
690 }
203c7ebf 691 else if (flag_real4_kind == 10)
f4347334
ZG
692 {
693 if (!saw_r10)
ddc05d11
TB
694 gfc_fatal_error ("REAL(KIND=10) is not available for "
695 "%<-freal-4-real-10%> option");
f4347334
ZG
696
697 gfc_default_real_kind = 10;
698 }
203c7ebf 699 else if (flag_real4_kind == 16)
f4347334
ZG
700 {
701 if (!saw_r16)
ddc05d11
TB
702 gfc_fatal_error ("REAL(KIND=16) is not available for "
703 "%<-freal-4-real-16%> option");
f4347334
ZG
704
705 gfc_default_real_kind = 16;
706 }
5e8e542f 707 else if (saw_r4)
9d64df18 708 gfc_default_real_kind = 4;
5e8e542f 709 else
9d64df18 710 gfc_default_real_kind = gfc_real_kinds[0].kind;
5e8e542f 711
f04986a9 712 /* Choose the default double kind. If -fdefault-real and -fdefault-double
3ae9eb27
SK
713 are specified, we use kind=8, if it's available. If -fdefault-real is
714 specified without -fdefault-double, we use kind=16, if it's available.
715 Otherwise we do not change anything. */
d16d81b5 716 if (flag_default_double && saw_r8)
3ae9eb27 717 gfc_default_double_kind = 8;
d16d81b5
JW
718 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
719 {
720 /* Use largest available kind. */
721 if (saw_r16)
722 gfc_default_double_kind = 16;
723 else if (saw_r10)
724 gfc_default_double_kind = 10;
725 else if (saw_r8)
726 gfc_default_double_kind = 8;
727 else
728 gfc_default_double_kind = gfc_default_real_kind;
729 }
203c7ebf 730 else if (flag_real8_kind == 4)
f4347334
ZG
731 {
732 if (!saw_r4)
ddc05d11
TB
733 gfc_fatal_error ("REAL(KIND=4) is not available for "
734 "%<-freal-8-real-4%> option");
f4347334 735
21c0a521 736 gfc_default_double_kind = 4;
f4347334 737 }
203c7ebf 738 else if (flag_real8_kind == 10 )
f4347334
ZG
739 {
740 if (!saw_r10)
ddc05d11
TB
741 gfc_fatal_error ("REAL(KIND=10) is not available for "
742 "%<-freal-8-real-10%> option");
f4347334 743
21c0a521 744 gfc_default_double_kind = 10;
f4347334 745 }
203c7ebf 746 else if (flag_real8_kind == 16 )
f4347334
ZG
747 {
748 if (!saw_r16)
ddc05d11
TB
749 gfc_fatal_error ("REAL(KIND=10) is not available for "
750 "%<-freal-8-real-16%> option");
f4347334 751
21c0a521 752 gfc_default_double_kind = 16;
f4347334 753 }
5e8e542f 754 else if (saw_r4 && saw_r8)
9d64df18 755 gfc_default_double_kind = 8;
5e8e542f
RH
756 else
757 {
758 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
759 real ... occupies two contiguous numeric storage units.
760
761 Therefore we must be supplied a kind twice as large as we chose
762 for single precision. There are loopholes, in that double
763 precision must *occupy* two storage units, though it doesn't have
764 to *use* two storage units. Which means that you can make this
765 kind artificially wide by padding it. But at present there are
766 no GCC targets for which a two-word type does not exist, so we
767 just let gfc_validate_kind abort and tell us if something breaks. */
768
9d64df18
TS
769 gfc_default_double_kind
770 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
5e8e542f
RH
771 }
772
773 /* The default logical kind is constrained to be the same as the
774 default integer kind. Similarly with complex and real. */
9d64df18
TS
775 gfc_default_logical_kind = gfc_default_integer_kind;
776 gfc_default_complex_kind = gfc_default_real_kind;
5e8e542f 777
374929b2
FXC
778 /* We only have two character kinds: ASCII and UCS-4.
779 ASCII corresponds to a 8-bit integer type, if one is available.
1cc0e193 780 UCS-4 corresponds to a 32-bit integer type, if one is available. */
374929b2
FXC
781 i_index = 0;
782 if ((kind = get_int_kind_from_width (8)) > 0)
783 {
784 gfc_character_kinds[i_index].kind = kind;
785 gfc_character_kinds[i_index].bit_size = 8;
786 gfc_character_kinds[i_index].name = "ascii";
787 i_index++;
788 }
789 if ((kind = get_int_kind_from_width (32)) > 0)
790 {
791 gfc_character_kinds[i_index].kind = kind;
792 gfc_character_kinds[i_index].bit_size = 32;
793 gfc_character_kinds[i_index].name = "iso_10646";
794 i_index++;
795 }
796
5e8e542f 797 /* Choose the smallest integer kind for our default character. */
374929b2 798 gfc_default_character_kind = gfc_character_kinds[0].kind;
39f87c03 799 gfc_character_storage_size = gfc_default_character_kind * 8;
5e8e542f 800
8395e28b
TB
801 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
802
e8525382
SK
803 /* Pick a kind the same size as the C "int" type. */
804 gfc_c_int_kind = INT_TYPE_SIZE / 8;
a8b3b0b6 805
d09131ee
TK
806 /* UNSIGNED has the same as INT. */
807 gfc_c_uint_kind = gfc_c_int_kind;
808
da661a58
TB
809 /* Choose atomic kinds to match C's int. */
810 gfc_atomic_int_kind = gfc_c_int_kind;
811 gfc_atomic_logical_kind = gfc_c_int_kind;
a6d22fb2
KCY
812
813 gfc_c_intptr_kind = POINTER_SIZE / 8;
5e8e542f
RH
814}
815
28d0b595 816
5e8e542f
RH
817/* Make sure that a valid kind is present. Returns an index into the
818 associated kinds array, -1 if the kind is not present. */
819
820static int
821validate_integer (int kind)
822{
823 int i;
824
825 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
826 if (gfc_integer_kinds[i].kind == kind)
827 return i;
828
829 return -1;
830}
831
113a6da9
TK
832static int
833validate_unsigned (int kind)
834{
835 int i;
836
837 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
838 if (gfc_unsigned_kinds[i].kind == kind)
839 return i;
840
841 return -1;
842}
843
5e8e542f
RH
844static int
845validate_real (int kind)
846{
847 int i;
848
849 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
850 if (gfc_real_kinds[i].kind == kind)
851 return i;
852
853 return -1;
854}
855
856static int
857validate_logical (int kind)
858{
859 int i;
860
861 for (i = 0; gfc_logical_kinds[i].kind; i++)
862 if (gfc_logical_kinds[i].kind == kind)
863 return i;
864
865 return -1;
866}
867
868static int
869validate_character (int kind)
870{
374929b2
FXC
871 int i;
872
873 for (i = 0; gfc_character_kinds[i].kind; i++)
874 if (gfc_character_kinds[i].kind == kind)
875 return i;
876
877 return -1;
5e8e542f
RH
878}
879
880/* Validate a kind given a basic type. The return value is the same
881 for the child functions, with -1 indicating nonexistence of the
882 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
883
884int
885gfc_validate_kind (bt type, int kind, bool may_fail)
886{
887 int rc;
888
889 switch (type)
890 {
891 case BT_REAL: /* Fall through */
892 case BT_COMPLEX:
893 rc = validate_real (kind);
894 break;
895 case BT_INTEGER:
896 rc = validate_integer (kind);
897 break;
113a6da9
TK
898 case BT_UNSIGNED:
899 rc = validate_unsigned (kind);
900 break;
5e8e542f
RH
901 case BT_LOGICAL:
902 rc = validate_logical (kind);
903 break;
904 case BT_CHARACTER:
905 rc = validate_character (kind);
906 break;
907
908 default:
909 gfc_internal_error ("gfc_validate_kind(): Got bad type");
910 }
911
912 if (rc < 0 && !may_fail)
913 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
914
915 return rc;
916}
917
918
e2cad04b
RH
919/* Four subroutines of gfc_init_types. Create type nodes for the given kind.
920 Reuse common type nodes where possible. Recognize if the kind matches up
921 with a C type. This will be used later in determining which routines may
922 be scarfed from libm. */
923
924static tree
925gfc_build_int_type (gfc_integer_info *info)
926{
927 int mode_precision = info->bit_size;
928
929 if (mode_precision == CHAR_TYPE_SIZE)
930 info->c_char = 1;
931 if (mode_precision == SHORT_TYPE_SIZE)
932 info->c_short = 1;
933 if (mode_precision == INT_TYPE_SIZE)
934 info->c_int = 1;
935 if (mode_precision == LONG_TYPE_SIZE)
936 info->c_long = 1;
937 if (mode_precision == LONG_LONG_TYPE_SIZE)
938 info->c_long_long = 1;
939
940 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
941 return intQI_type_node;
942 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
943 return intHI_type_node;
944 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
945 return intSI_type_node;
946 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
947 return intDI_type_node;
948 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
949 return intTI_type_node;
950
951 return make_signed_type (mode_precision);
952}
953
0a05c536 954tree
374929b2
FXC
955gfc_build_uint_type (int size)
956{
957 if (size == CHAR_TYPE_SIZE)
958 return unsigned_char_type_node;
959 if (size == SHORT_TYPE_SIZE)
960 return short_unsigned_type_node;
961 if (size == INT_TYPE_SIZE)
962 return unsigned_type_node;
963 if (size == LONG_TYPE_SIZE)
964 return long_unsigned_type_node;
965 if (size == LONG_LONG_TYPE_SIZE)
966 return long_long_unsigned_type_node;
967
968 return make_unsigned_type (size);
969}
970
113a6da9
TK
971static tree
972gfc_build_unsigned_type (gfc_unsigned_info *info)
973{
974 int mode_precision = info->bit_size;
975
976 if (mode_precision == CHAR_TYPE_SIZE)
977 info->c_unsigned_char = 1;
978 if (mode_precision == SHORT_TYPE_SIZE)
979 info->c_unsigned_short = 1;
980 if (mode_precision == INT_TYPE_SIZE)
981 info->c_unsigned_int = 1;
982 if (mode_precision == LONG_TYPE_SIZE)
983 info->c_unsigned_long = 1;
984 if (mode_precision == LONG_LONG_TYPE_SIZE)
985 info->c_unsigned_long_long = 1;
986
987 return gfc_build_uint_type (mode_precision);
988}
374929b2 989
e2cad04b
RH
990static tree
991gfc_build_real_type (gfc_real_info *info)
992{
993 int mode_precision = info->mode_precision;
994 tree new_type;
995
37a48009 996 if (mode_precision == TYPE_PRECISION (float_type_node))
e2cad04b 997 info->c_float = 1;
37a48009 998 if (mode_precision == TYPE_PRECISION (double_type_node))
e2cad04b 999 info->c_double = 1;
37a48009
KL
1000 if (mode_precision == TYPE_PRECISION (long_double_type_node)
1001 && !info->c_float128)
e2cad04b 1002 info->c_long_double = 1;
37a48009
KL
1003 if (mode_precision != TYPE_PRECISION (long_double_type_node)
1004 && mode_precision == 128)
a3c85b74 1005 {
58340a7c 1006 /* TODO: see PR101835. */
a3c85b74
FXC
1007 info->c_float128 = 1;
1008 gfc_real16_is_float128 = true;
133d0d42
JJ
1009 if (TARGET_GLIBC_MAJOR > 2
1010 || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26))
1011 {
1012 info->use_iec_60559 = 1;
1013 gfc_real16_use_iec_60559 = true;
1014 }
a3c85b74 1015 }
e2cad04b
RH
1016
1017 if (TYPE_PRECISION (float_type_node) == mode_precision)
1018 return float_type_node;
1019 if (TYPE_PRECISION (double_type_node) == mode_precision)
1020 return double_type_node;
1021 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
1022 return long_double_type_node;
1023
1024 new_type = make_node (REAL_TYPE);
1025 TYPE_PRECISION (new_type) = mode_precision;
1026 layout_type (new_type);
1027 return new_type;
1028}
1029
1030static tree
1031gfc_build_complex_type (tree scalar_type)
1032{
1033 tree new_type;
1034
1035 if (scalar_type == NULL)
1036 return NULL;
1037 if (scalar_type == float_type_node)
1038 return complex_float_type_node;
1039 if (scalar_type == double_type_node)
1040 return complex_double_type_node;
1041 if (scalar_type == long_double_type_node)
1042 return complex_long_double_type_node;
1043
1044 new_type = make_node (COMPLEX_TYPE);
1045 TREE_TYPE (new_type) = scalar_type;
1046 layout_type (new_type);
1047 return new_type;
1048}
1049
1050static tree
1051gfc_build_logical_type (gfc_logical_info *info)
1052{
1053 int bit_size = info->bit_size;
1054 tree new_type;
1055
1056 if (bit_size == BOOL_TYPE_SIZE)
1057 {
1058 info->c_bool = 1;
1059 return boolean_type_node;
1060 }
1061
1062 new_type = make_unsigned_type (bit_size);
1063 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
1064 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
1065 TYPE_PRECISION (new_type) = 1;
1066
1067 return new_type;
1068}
1069
e0a6661b 1070
6de9cd9a
DN
1071/* Create the backend type nodes. We map them to their
1072 equivalent C type, at least for now. We also give
1073 names to the types here, and we push them in the
1074 global binding level context.*/
c3e8c6b8 1075
6de9cd9a
DN
1076void
1077gfc_init_types (void)
1078{
efcc8d38 1079 char name_buf[26];
e2cad04b
RH
1080 int index;
1081 tree type;
6de9cd9a 1082 unsigned n;
6de9cd9a 1083
e2cad04b 1084 /* Create and name the types. */
6de9cd9a 1085#define PUSH_TYPE(name, node) \
c2255bc4
AH
1086 pushdecl (build_decl (input_location, \
1087 TYPE_DECL, get_identifier (name), node))
6de9cd9a 1088
e2cad04b
RH
1089 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
1090 {
1091 type = gfc_build_int_type (&gfc_integer_kinds[index]);
dead0bae
JJ
1092 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
1093 if (TYPE_STRING_FLAG (type))
1094 type = make_signed_type (gfc_integer_kinds[index].bit_size);
e2cad04b 1095 gfc_integer_types[index] = type;
40373aa6 1096 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
e2cad04b
RH
1097 gfc_integer_kinds[index].kind);
1098 PUSH_TYPE (name_buf, type);
1099 }
6de9cd9a 1100
e2cad04b
RH
1101 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
1102 {
1103 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
1104 gfc_logical_types[index] = type;
40373aa6 1105 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
e2cad04b
RH
1106 gfc_logical_kinds[index].kind);
1107 PUSH_TYPE (name_buf, type);
1108 }
6de9cd9a 1109
e2cad04b
RH
1110 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
1111 {
1112 type = gfc_build_real_type (&gfc_real_kinds[index]);
1113 gfc_real_types[index] = type;
40373aa6 1114 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
e2cad04b
RH
1115 gfc_real_kinds[index].kind);
1116 PUSH_TYPE (name_buf, type);
1117
a3c85b74 1118 if (gfc_real_kinds[index].c_float128)
c65699ef 1119 gfc_float128_type_node = type;
a3c85b74 1120
e2cad04b
RH
1121 type = gfc_build_complex_type (type);
1122 gfc_complex_types[index] = type;
40373aa6 1123 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
e2cad04b
RH
1124 gfc_real_kinds[index].kind);
1125 PUSH_TYPE (name_buf, type);
a3c85b74
FXC
1126
1127 if (gfc_real_kinds[index].c_float128)
c65699ef 1128 gfc_complex_float128_type_node = type;
e2cad04b 1129 }
6de9cd9a 1130
374929b2
FXC
1131 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
1132 {
1133 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
1134 type = build_qualified_type (type, TYPE_UNQUALIFIED);
1135 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
1136 gfc_character_kinds[index].kind);
1137 PUSH_TYPE (name_buf, type);
1138 gfc_character_types[index] = type;
1139 gfc_pcharacter_types[index] = build_pointer_type (type);
1140 }
1141 gfc_character1_type_node = gfc_character_types[0];
6de9cd9a 1142
113a6da9
TK
1143 if (flag_unsigned)
1144 {
1145 for (index = 0; gfc_unsigned_kinds[index].kind != 0;++index)
1146 {
1147 int index_char = -1;
1148 for (int i=0; gfc_character_kinds[i].kind != 0; i++)
1149 {
1150 if (gfc_character_kinds[i].bit_size
1151 == gfc_unsigned_kinds[index].bit_size)
1152 {
1153 index_char = i;
1154 break;
1155 }
1156 }
512371d7 1157 if (index_char > -1)
113a6da9 1158 {
512371d7
JJ
1159 type = gfc_character_types[index_char];
1160 if (TYPE_STRING_FLAG (type))
1161 {
1162 type = build_distinct_type_copy (type);
1163 TYPE_CANONICAL (type)
1164 = TYPE_CANONICAL (gfc_character_types[index_char]);
1165 }
1166 else
1167 type = build_variant_type_copy (type);
1168 TYPE_NAME (type) = NULL_TREE;
1169 TYPE_STRING_FLAG (type) = 0;
113a6da9
TK
1170 }
1171 else
512371d7
JJ
1172 type = gfc_build_unsigned_type (&gfc_unsigned_kinds[index]);
1173 gfc_unsigned_types[index] = type;
1174 snprintf (name_buf, sizeof(name_buf), "unsigned(kind=%d)",
1175 gfc_integer_kinds[index].kind);
1176 PUSH_TYPE (name_buf, type);
113a6da9
TK
1177 }
1178 }
1179
6de9cd9a
DN
1180 PUSH_TYPE ("byte", unsigned_char_type_node);
1181 PUSH_TYPE ("void", void_type_node);
1182
1183 /* DBX debugging output gets upset if these aren't set. */
1184 if (!TYPE_NAME (integer_type_node))
1185 PUSH_TYPE ("c_integer", integer_type_node);
1186 if (!TYPE_NAME (char_type_node))
1187 PUSH_TYPE ("c_char", char_type_node);
e2cad04b 1188
6de9cd9a
DN
1189#undef PUSH_TYPE
1190
1191 pvoid_type_node = build_pointer_type (void_type_node);
10174ddf 1192 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
6de9cd9a
DN
1193 ppvoid_type_node = build_pointer_type (pvoid_type_node);
1194 pchar_type_node = build_pointer_type (gfc_character1_type_node);
089db47d 1195 pfunc_type_node
b64fca63 1196 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
6de9cd9a 1197
6de9cd9a 1198 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
b4838d29
ZD
1199 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1200 since this function is called before gfc_init_constants. */
1201 gfc_array_range_type
1202 = build_range_type (gfc_array_index_type,
1203 build_int_cst (gfc_array_index_type, 0),
1204 NULL_TREE);
6de9cd9a
DN
1205
1206 /* The maximum array element size that can be handled is determined
1207 by the number of bits available to store this field in the array
1208 descriptor. */
1209
7fb43006 1210 n = TYPE_PRECISION (size_type_node);
7d60be94 1211 gfc_max_array_element_size
fae3018d 1212 = wide_int_to_tree (size_type_node,
807e902e 1213 wi::mask (n, UNSIGNED,
fae3018d 1214 TYPE_PRECISION (size_type_node)));
6de9cd9a 1215
63ee5404
JB
1216 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1217 logical_true_node = build_int_cst (logical_type_node, 1);
1218 logical_false_node = build_int_cst (logical_type_node, 0);
1219
f622221a
JB
1220 /* Character lengths are of type size_t, except signed. */
1221 gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
f1412ca5 1222 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
f622221a
JB
1223
1224 /* Fortran kind number of size_type_node (size_t). This is used for
1225 the _size member in vtables. */
1226 gfc_size_kind = get_int_kind_from_node (size_type_node);
6de9cd9a
DN
1227}
1228
e2cad04b 1229/* Get the type node for the given type and kind. */
c3e8c6b8 1230
6de9cd9a
DN
1231tree
1232gfc_get_int_type (int kind)
1233{
644cb69f
FXC
1234 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1235 return index < 0 ? 0 : gfc_integer_types[index];
6de9cd9a
DN
1236}
1237
113a6da9
TK
1238tree
1239gfc_get_unsigned_type (int kind)
1240{
1241 int index = gfc_validate_kind (BT_UNSIGNED, kind, true);
1242 return index < 0 ? 0 : gfc_unsigned_types[index];
1243}
1244
6de9cd9a
DN
1245tree
1246gfc_get_real_type (int kind)
1247{
644cb69f
FXC
1248 int index = gfc_validate_kind (BT_REAL, kind, true);
1249 return index < 0 ? 0 : gfc_real_types[index];
6de9cd9a
DN
1250}
1251
6de9cd9a
DN
1252tree
1253gfc_get_complex_type (int kind)
1254{
644cb69f
FXC
1255 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1256 return index < 0 ? 0 : gfc_complex_types[index];
6de9cd9a
DN
1257}
1258
6de9cd9a
DN
1259tree
1260gfc_get_logical_type (int kind)
1261{
644cb69f
FXC
1262 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1263 return index < 0 ? 0 : gfc_logical_types[index];
6de9cd9a 1264}
374929b2
FXC
1265
1266tree
1267gfc_get_char_type (int kind)
1268{
1269 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1270 return index < 0 ? 0 : gfc_character_types[index];
1271}
1272
1273tree
1274gfc_get_pchar_type (int kind)
1275{
1276 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1277 return index < 0 ? 0 : gfc_pcharacter_types[index];
1278}
1279
6de9cd9a 1280\f
40f20186 1281/* Create a character type with the given kind and length. */
c3e8c6b8 1282
6de9cd9a 1283tree
d393bbd7 1284gfc_get_character_type_len_for_eltype (tree eltype, tree len)
6de9cd9a 1285{
e2cad04b 1286 tree bounds, type;
6de9cd9a 1287
5e3b8727 1288 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
d393bbd7 1289 type = build_array_type (eltype, bounds);
6de9cd9a
DN
1290 TYPE_STRING_FLAG (type) = 1;
1291
1292 return type;
1293}
40f20186 1294
d393bbd7
FXC
1295tree
1296gfc_get_character_type_len (int kind, tree len)
1297{
1298 gfc_validate_kind (BT_CHARACTER, kind, false);
1299 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1300}
1301
40f20186
PB
1302
1303/* Get a type node for a character kind. */
1304
1305tree
1306gfc_get_character_type (int kind, gfc_charlen * cl)
1307{
1308 tree len;
1309
1310 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
afbc5ae8
PT
1311 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1312 len = build_fold_indirect_ref (len);
40f20186
PB
1313
1314 return gfc_get_character_type_len (kind, len);
1315}
6de9cd9a 1316\f
5c8fbcad 1317/* Convert a basic type. This will be an array for character types. */
c3e8c6b8 1318
6de9cd9a 1319tree
de91486c 1320gfc_typenode_for_spec (gfc_typespec * spec, int codim)
6de9cd9a
DN
1321{
1322 tree basetype;
1323
1324 switch (spec->type)
1325 {
1326 case BT_UNKNOWN:
6e45f57b 1327 gcc_unreachable ();
6de9cd9a
DN
1328
1329 case BT_INTEGER:
a8b3b0b6
CR
1330 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1331 has been resolved. This is done so we can convert C_PTR and
1332 C_FUNPTR to simple variables that get translated to (void *). */
1333 if (spec->f90_type == BT_VOID)
089db47d 1334 {
bc21d315
JW
1335 if (spec->u.derived
1336 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
089db47d
CR
1337 basetype = ptr_type_node;
1338 else
1339 basetype = pfunc_type_node;
1340 }
a8b3b0b6
CR
1341 else
1342 basetype = gfc_get_int_type (spec->kind);
6de9cd9a
DN
1343 break;
1344
113a6da9
TK
1345 case BT_UNSIGNED:
1346 basetype = gfc_get_unsigned_type (spec->kind);
1347 break;
1348
6de9cd9a
DN
1349 case BT_REAL:
1350 basetype = gfc_get_real_type (spec->kind);
1351 break;
1352
1353 case BT_COMPLEX:
1354 basetype = gfc_get_complex_type (spec->kind);
1355 break;
1356
1357 case BT_LOGICAL:
1358 basetype = gfc_get_logical_type (spec->kind);
1359 break;
1360
1361 case BT_CHARACTER:
9b548517 1362 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
6de9cd9a
DN
1363 break;
1364
25812571
PT
1365 case BT_HOLLERITH:
1366 /* Since this cannot be used, return a length one character. */
1367 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1368 gfc_index_one_node);
1369 break;
1370
f6288c24
FR
1371 case BT_UNION:
1372 basetype = gfc_get_union_type (spec->u.derived);
1373 break;
1374
6de9cd9a 1375 case BT_DERIVED:
cf2b3c22 1376 case BT_CLASS:
de91486c 1377 basetype = gfc_get_derived_type (spec->u.derived, codim);
6de9cd9a 1378
8f75db9f
PT
1379 if (spec->type == BT_CLASS)
1380 GFC_CLASS_TYPE_P (basetype) = 1;
1381
a8b3b0b6
CR
1382 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1383 type and kind to fit a (void *) and the basetype returned was a
1384 ptr_type_node. We need to pass up this new information to the
1385 symbol that was declared of type C_PTR or C_FUNPTR. */
cadddfdd 1386 if (spec->u.derived->ts.f90_type == BT_VOID)
a8b3b0b6 1387 {
cadddfdd
TB
1388 spec->type = BT_INTEGER;
1389 spec->kind = gfc_index_integer_kind;
4ea60a39
TK
1390 spec->f90_type = BT_VOID;
1391 spec->is_c_interop = 1; /* Mark as escaping later. */
a8b3b0b6
CR
1392 }
1393 break;
1394 case BT_VOID:
45a69325 1395 case BT_ASSUMED:
089db47d
CR
1396 /* This is for the second arg to c_f_pointer and c_f_procpointer
1397 of the iso_c_binding module, to accept any ptr type. */
1398 basetype = ptr_type_node;
1399 if (spec->f90_type == BT_VOID)
1400 {
bc21d315
JW
1401 if (spec->u.derived
1402 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
089db47d
CR
1403 basetype = ptr_type_node;
1404 else
1405 basetype = pfunc_type_node;
1406 }
a8b3b0b6 1407 break;
93755deb
HA
1408 case BT_PROCEDURE:
1409 basetype = pfunc_type_node;
1410 break;
6de9cd9a 1411 default:
6e45f57b 1412 gcc_unreachable ();
6de9cd9a
DN
1413 }
1414 return basetype;
1415}
1416\f
1417/* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
c3e8c6b8 1418
6de9cd9a
DN
1419static tree
1420gfc_conv_array_bound (gfc_expr * expr)
1421{
1422 /* If expr is an integer constant, return that. */
1423 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1424 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1425
1426 /* Otherwise return NULL. */
1427 return NULL_TREE;
1428}
1429\f
710700ab
TB
1430/* Return the type of an element of the array. Note that scalar coarrays
1431 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1432 (with POINTER_TYPE stripped) is returned. */
1433
6de9cd9a
DN
1434tree
1435gfc_get_element_type (tree type)
1436{
1437 tree element;
1438
1439 if (GFC_ARRAY_TYPE_P (type))
1440 {
1441 if (TREE_CODE (type) == POINTER_TYPE)
1442 type = TREE_TYPE (type);
4409de24
TB
1443 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1444 {
1445 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1446 element = type;
1447 }
1448 else
1449 {
1450 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1451 element = TREE_TYPE (type);
1452 }
6de9cd9a
DN
1453 }
1454 else
1455 {
6e45f57b 1456 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
4c73896d 1457 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
6de9cd9a 1458
6e45f57b 1459 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
6de9cd9a
DN
1460 element = TREE_TYPE (element);
1461
badd9e69 1462 /* For arrays, which are not scalar coarrays. */
5e67a369 1463 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
badd9e69 1464 element = TREE_TYPE (element);
6de9cd9a
DN
1465 }
1466
1467 return element;
1468}
1469\f
eb28fb7d 1470/* Build an array. This function is called from gfc_sym_type().
c3e8c6b8 1471 Actually returns array descriptor type.
6de9cd9a
DN
1472
1473 Format of array descriptors is as follows:
1474
1475 struct gfc_array_descriptor
1476 {
7fb43006 1477 array *data;
6de9cd9a 1478 index offset;
7fb43006 1479 struct dtype_type dtype;
6de9cd9a
DN
1480 struct descriptor_dimension dimension[N_DIM];
1481 }
1482
7fb43006
PT
1483 struct dtype_type
1484 {
1485 size_t elem_len;
1486 int version;
1487 signed char rank;
1488 signed char type;
1489 signed short attribute;
1490 }
1491
6de9cd9a
DN
1492 struct descriptor_dimension
1493 {
1494 index stride;
1495 index lbound;
1496 index ubound;
1497 }
1498
eb28fb7d
TS
1499 Translation code should use gfc_conv_descriptor_* rather than
1500 accessing the descriptor directly. Any changes to the array
1501 descriptor type will require changes in gfc_conv_descriptor_* and
1502 gfc_build_array_initializer.
6de9cd9a 1503
eb28fb7d
TS
1504 This is represented internally as a RECORD_TYPE. The index nodes
1505 are gfc_array_index_type and the data node is a pointer to the
1506 data. See below for the handling of character types.
6de9cd9a 1507
eb28fb7d
TS
1508 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1509 this generated poor code for assumed/deferred size arrays. These
1510 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1511 of the GENERIC grammar. Also, there is no way to explicitly set
1512 the array stride, so all data must be packed(1). I've tried to
1513 mark all the functions which would require modification with a GCC
1514 ARRAYS comment.
6de9cd9a 1515
eb28fb7d 1516 The data component points to the first element in the array. The
df2fba9e
RW
1517 offset field is the position of the origin of the array (i.e. element
1518 (0, 0 ...)). This may be outside the bounds of the array.
6de9cd9a
DN
1519
1520 An element is accessed by
eb28fb7d 1521 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
c3e8c6b8 1522 This gives good performance as the computation does not involve the
eb28fb7d
TS
1523 bounds of the array. For packed arrays, this is optimized further
1524 by substituting the known strides.
6de9cd9a 1525
eb28fb7d
TS
1526 This system has one problem: all array bounds must be within 2^31
1527 elements of the origin (2^63 on 64-bit machines). For example
1528 integer, dimension (80000:90000, 80000:90000, 2) :: array
1529 may not work properly on 32-bit machines because 80000*80000 >
df2fba9e 1530 2^31, so the calculation for stride2 would overflow. This may
eb28fb7d
TS
1531 still work, but I haven't checked, and it relies on the overflow
1532 doing the right thing.
6de9cd9a 1533
1f2959f0 1534 The way to fix this problem is to access elements as follows:
eb28fb7d
TS
1535 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1536 Obviously this is much slower. I will make this a compile time
1537 option, something like -fsmall-array-offsets. Mixing code compiled
1538 with and without this switch will work.
1539
1540 (1) This can be worked around by modifying the upper bound of the
1541 previous dimension. This requires extra fields in the descriptor
1542 (both real_ubound and fake_ubound). */
6de9cd9a
DN
1543
1544
1545/* Returns true if the array sym does not require a descriptor. */
1546
c072df1a 1547bool
6de9cd9a
DN
1548gfc_is_nodesc_array (gfc_symbol * sym)
1549{
f3b0bb7a
AV
1550 symbol_attribute *array_attr;
1551 gfc_array_spec *as;
18f3b223 1552 bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
f3b0bb7a
AV
1553
1554 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1555 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1556
1557 gcc_assert (array_attr->dimension || array_attr->codimension);
6de9cd9a
DN
1558
1559 /* We only want local arrays. */
f3b0bb7a
AV
1560 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1561 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1562 || array_attr->allocatable)
6de9cd9a
DN
1563 return 0;
1564
571d54de 1565 /* We want a descriptor for associate-name arrays that do not have an
f3b0bb7a
AV
1566 explicitly known shape already. */
1567 if (sym->assoc && as->type != AS_EXPLICIT)
571d54de
DK
1568 return 0;
1569
f3b0bb7a 1570 /* The dummy is stored in sym and not in the component. */
6de9cd9a 1571 if (sym->attr.dummy)
f3b0bb7a
AV
1572 return as->type != AS_ASSUMED_SHAPE
1573 && as->type != AS_ASSUMED_RANK;
6de9cd9a
DN
1574
1575 if (sym->attr.result || sym->attr.function)
1576 return 0;
1577
f3b0bb7a 1578 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
6de9cd9a
DN
1579
1580 return 1;
1581}
1582
40f20186
PB
1583
1584/* Create an array descriptor type. */
1585
6de9cd9a 1586static tree
fad0afd7 1587gfc_build_array_type (tree type, gfc_array_spec * as,
fe4e525c 1588 enum gfc_array_kind akind, bool restricted,
de91486c 1589 bool contiguous, int codim)
6de9cd9a
DN
1590{
1591 tree lbound[GFC_MAX_DIMENSIONS];
1592 tree ubound[GFC_MAX_DIMENSIONS];
a8a5f4a9
TB
1593 int n, corank;
1594
1595 /* Assumed-shape arrays do not have codimension information stored in the
1596 descriptor. */
de91486c 1597 corank = MAX (as->corank, codim);
a8a5f4a9
TB
1598 if (as->type == AS_ASSUMED_SHAPE ||
1599 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
de91486c 1600 corank = codim;
6de9cd9a 1601
c62c6622
TB
1602 if (as->type == AS_ASSUMED_RANK)
1603 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1604 {
1605 lbound[n] = NULL_TREE;
1606 ubound[n] = NULL_TREE;
1607 }
1608
6de9cd9a
DN
1609 for (n = 0; n < as->rank; n++)
1610 {
1611 /* Create expressions for the known bounds of the array. */
1612 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
7ab92584 1613 lbound[n] = gfc_index_one_node;
6de9cd9a
DN
1614 else
1615 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1616 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1617 }
1618
a8a5f4a9 1619 for (n = as->rank; n < as->rank + corank; n++)
a3935ffc 1620 {
49b804d0 1621 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
a3935ffc
TB
1622 lbound[n] = gfc_index_one_node;
1623 else
1624 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1625
a8a5f4a9 1626 if (n < as->rank + corank - 1)
a3935ffc
TB
1627 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1628 }
1629
fad0afd7 1630 if (as->type == AS_ASSUMED_SHAPE)
fe4e525c
TB
1631 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1632 : GFC_ARRAY_ASSUMED_SHAPE;
c62c6622 1633 else if (as->type == AS_ASSUMED_RANK)
8293b9e4
TB
1634 {
1635 if (akind == GFC_ARRAY_ALLOCATABLE)
1636 akind = GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE;
1637 else if (akind == GFC_ARRAY_POINTER || akind == GFC_ARRAY_POINTER_CONT)
1638 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_POINTER_CONT
1639 : GFC_ARRAY_ASSUMED_RANK_POINTER;
1640 else
1641 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1642 : GFC_ARRAY_ASSUMED_RANK;
1643 }
c62c6622
TB
1644 return gfc_get_array_type_bounds (type, as->rank == -1
1645 ? GFC_MAX_DIMENSIONS : as->rank,
de91486c
AV
1646 corank, lbound, ubound, 0, akind,
1647 restricted);
6de9cd9a
DN
1648}
1649\f
1650/* Returns the struct descriptor_dimension type. */
c3e8c6b8 1651
6de9cd9a
DN
1652static tree
1653gfc_get_desc_dim_type (void)
1654{
1655 tree type;
35151cd5 1656 tree decl, *chain = NULL;
6de9cd9a
DN
1657
1658 if (gfc_desc_dim_type)
1659 return gfc_desc_dim_type;
1660
1661 /* Build the type node. */
1662 type = make_node (RECORD_TYPE);
1663
1664 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1665 TYPE_PACKED (type) = 1;
1666
1667 /* Consists of the stride, lbound and ubound members. */
35151cd5 1668 decl = gfc_add_field_to_struct_1 (type,
dfd6ece2
NF
1669 get_identifier ("stride"),
1670 gfc_array_index_type, &chain);
d5e69948 1671 suppress_warning (decl);
6de9cd9a 1672
35151cd5 1673 decl = gfc_add_field_to_struct_1 (type,
dfd6ece2
NF
1674 get_identifier ("lbound"),
1675 gfc_array_index_type, &chain);
d5e69948 1676 suppress_warning (decl);
6de9cd9a 1677
35151cd5 1678 decl = gfc_add_field_to_struct_1 (type,
dfd6ece2
NF
1679 get_identifier ("ubound"),
1680 gfc_array_index_type, &chain);
d5e69948 1681 suppress_warning (decl);
6de9cd9a
DN
1682
1683 /* Finish off the type. */
6de9cd9a 1684 gfc_finish_type (type);
dfcf0b12 1685 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
6de9cd9a
DN
1686
1687 gfc_desc_dim_type = type;
1688 return type;
1689}
1690
40b026d8 1691
43a5ef69 1692/* Return the DTYPE for an array. This describes the type and type parameters
40b026d8
PB
1693 of the array. */
1694/* TODO: Only call this when the value is actually used, and make all the
1695 unknown cases abort. */
1696
1697tree
d7463e5b 1698gfc_get_dtype_rank_type (int rank, tree etype)
6de9cd9a 1699{
d514626e 1700 tree ptype;
6de9cd9a
DN
1701 tree size;
1702 int n;
6de9cd9a
DN
1703 tree tmp;
1704 tree dtype;
7fb43006
PT
1705 tree field;
1706 vec<constructor_elt, va_gc> *v = NULL;
6de9cd9a 1707
d514626e
JRFS
1708 ptype = etype;
1709 while (TREE_CODE (etype) == POINTER_TYPE
1710 || TREE_CODE (etype) == ARRAY_TYPE)
1711 {
1712 ptype = etype;
1713 etype = TREE_TYPE (etype);
1714 }
1715
1716 gcc_assert (etype);
f094a21f 1717
40b026d8 1718 switch (TREE_CODE (etype))
6de9cd9a
DN
1719 {
1720 case INTEGER_TYPE:
d514626e
JRFS
1721 if (TREE_CODE (ptype) == ARRAY_TYPE
1722 && TYPE_STRING_FLAG (ptype))
1723 n = BT_CHARACTER;
1724 else
291e20e8
TK
1725 {
1726 if (TYPE_UNSIGNED (etype))
1727 n = BT_UNSIGNED;
1728 else
1729 n = BT_INTEGER;
1730 }
6de9cd9a
DN
1731 break;
1732
1733 case BOOLEAN_TYPE:
a11930ba 1734 n = BT_LOGICAL;
6de9cd9a
DN
1735 break;
1736
1737 case REAL_TYPE:
a11930ba 1738 n = BT_REAL;
6de9cd9a
DN
1739 break;
1740
1741 case COMPLEX_TYPE:
a11930ba 1742 n = BT_COMPLEX;
6de9cd9a
DN
1743 break;
1744
6de9cd9a 1745 case RECORD_TYPE:
7fb43006
PT
1746 if (GFC_CLASS_TYPE_P (etype))
1747 n = BT_CLASS;
1748 else
1749 n = BT_DERIVED;
6de9cd9a
DN
1750 break;
1751
d514626e
JRFS
1752 case FUNCTION_TYPE:
1753 case VOID_TYPE:
1754 n = BT_VOID;
6de9cd9a
DN
1755 break;
1756
1757 default:
40f20186 1758 /* TODO: Don't do dtype for temporary descriptorless arrays. */
f094a21f 1759 /* We can encounter strange array types for temporary arrays. */
d514626e
JRFS
1760 gcc_unreachable ();
1761 }
1762
1763 switch (n)
1764 {
1765 case BT_CHARACTER:
1766 gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1767 size = gfc_get_character_len_in_bytes (ptype);
1768 break;
1769 case BT_VOID:
1770 gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1771 size = size_in_bytes (ptype);
1772 break;
1773 default:
1774 size = size_in_bytes (etype);
1775 break;
6de9cd9a 1776 }
2ce90517 1777
d514626e 1778 gcc_assert (size);
6de9cd9a 1779
d514626e
JRFS
1780 STRIP_NOPS (size);
1781 size = fold_convert (size_type_node, size);
7fb43006
PT
1782 tmp = get_dtype_type_node ();
1783 field = gfc_advance_chain (TYPE_FIELDS (tmp),
1784 GFC_DTYPE_ELEM_LEN);
1785 CONSTRUCTOR_APPEND_ELT (v, field,
1786 fold_convert (TREE_TYPE (field), size));
d4b6d147
TB
1787 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1788 GFC_DTYPE_VERSION);
1789 CONSTRUCTOR_APPEND_ELT (v, field,
1790 build_zero_cst (TREE_TYPE (field)));
6de9cd9a 1791
7fb43006
PT
1792 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1793 GFC_DTYPE_RANK);
64f96237
TB
1794 if (rank >= 0)
1795 CONSTRUCTOR_APPEND_ELT (v, field,
1796 build_int_cst (TREE_TYPE (field), rank));
6de9cd9a 1797
7fb43006
PT
1798 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1799 GFC_DTYPE_TYPE);
1800 CONSTRUCTOR_APPEND_ELT (v, field,
1801 build_int_cst (TREE_TYPE (field), n));
1802
1803 dtype = build_constructor (tmp, v);
6de9cd9a 1804
d7463e5b
TB
1805 return dtype;
1806}
1807
1808
1809tree
d514626e 1810gfc_get_dtype (tree type, int * rank)
d7463e5b
TB
1811{
1812 tree dtype;
1813 tree etype;
d514626e 1814 int irnk;
d7463e5b
TB
1815
1816 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1817
d514626e 1818 irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
d7463e5b 1819 etype = gfc_get_element_type (type);
d514626e 1820 dtype = gfc_get_dtype_rank_type (irnk, etype);
d7463e5b 1821
40b026d8 1822 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
6de9cd9a
DN
1823 return dtype;
1824}
1825
1826
dcfef7d4
TS
1827/* Build an array type for use without a descriptor, packed according
1828 to the value of PACKED. */
6de9cd9a
DN
1829
1830tree
10174ddf
MM
1831gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1832 bool restricted)
6de9cd9a
DN
1833{
1834 tree range;
1835 tree type;
1836 tree tmp;
1837 int n;
1838 int known_stride;
1839 int known_offset;
1840 mpz_t offset;
1841 mpz_t stride;
1842 mpz_t delta;
1843 gfc_expr *expr;
1844
1845 mpz_init_set_ui (offset, 0);
1846 mpz_init_set_ui (stride, 1);
1847 mpz_init (delta);
1848
d5029d45 1849 /* We don't use build_array_type because this does not include
13795658 1850 lang-specific information (i.e. the bounds of the array) when checking
6de9cd9a 1851 for duplicates. */
4409de24
TB
1852 if (as->rank)
1853 type = make_node (ARRAY_TYPE);
1854 else
8a5c4899 1855 type = build_variant_type_copy (etype);
6de9cd9a
DN
1856
1857 GFC_ARRAY_TYPE_P (type) = 1;
766090c2 1858 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
6de9cd9a 1859
dcfef7d4 1860 known_stride = (packed != PACKED_NO);
6de9cd9a
DN
1861 known_offset = 1;
1862 for (n = 0; n < as->rank; n++)
1863 {
1864 /* Fill in the stride and bound components of the type. */
1865 if (known_stride)
08789087 1866 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
6de9cd9a
DN
1867 else
1868 tmp = NULL_TREE;
1869 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1870
1871 expr = as->lower[n];
c2b15fe2 1872 if (expr && expr->expr_type == EXPR_CONSTANT)
6de9cd9a
DN
1873 {
1874 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
fad0afd7 1875 gfc_index_integer_kind);
6de9cd9a
DN
1876 }
1877 else
1878 {
1879 known_stride = 0;
1880 tmp = NULL_TREE;
1881 }
1882 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1883
1884 if (known_stride)
1885 {
1886 /* Calculate the offset. */
1887 mpz_mul (delta, stride, as->lower[n]->value.integer);
1888 mpz_sub (offset, offset, delta);
1889 }
1890 else
1891 known_offset = 0;
1892
1893 expr = as->upper[n];
1894 if (expr && expr->expr_type == EXPR_CONSTANT)
1895 {
1896 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1897 gfc_index_integer_kind);
1898 }
1899 else
1900 {
1901 tmp = NULL_TREE;
1902 known_stride = 0;
1903 }
1904 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1905
1906 if (known_stride)
1907 {
1908 /* Calculate the stride. */
1909 mpz_sub (delta, as->upper[n]->value.integer,
1910 as->lower[n]->value.integer);
1911 mpz_add_ui (delta, delta, 1);
1912 mpz_mul (stride, stride, delta);
1913 }
1914
1915 /* Only the first stride is known for partial packed arrays. */
dcfef7d4 1916 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
6de9cd9a
DN
1917 known_stride = 0;
1918 }
a3935ffc
TB
1919 for (n = as->rank; n < as->rank + as->corank; n++)
1920 {
1921 expr = as->lower[n];
c2b15fe2 1922 if (expr && expr->expr_type == EXPR_CONSTANT)
a3935ffc
TB
1923 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1924 gfc_index_integer_kind);
1925 else
2ce90517 1926 tmp = NULL_TREE;
a3935ffc
TB
1927 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1928
1929 expr = as->upper[n];
1930 if (expr && expr->expr_type == EXPR_CONSTANT)
1931 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1932 gfc_index_integer_kind);
1933 else
1934 tmp = NULL_TREE;
1935 if (n < as->rank + as->corank - 1)
18f3b223 1936 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
a3935ffc 1937 }
6de9cd9a
DN
1938
1939 if (known_offset)
1940 {
1941 GFC_TYPE_ARRAY_OFFSET (type) =
1942 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1943 }
1944 else
1945 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1946
1947 if (known_stride)
1948 {
1949 GFC_TYPE_ARRAY_SIZE (type) =
1950 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1951 }
1952 else
1953 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1954
6de9cd9a 1955 GFC_TYPE_ARRAY_RANK (type) = as->rank;
a3935ffc 1956 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
40b026d8 1957 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
7ab92584 1958 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
6de9cd9a
DN
1959 NULL_TREE);
1960 /* TODO: use main type if it is unbounded. */
1961 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1962 build_pointer_type (build_array_type (etype, range));
10174ddf
MM
1963 if (restricted)
1964 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1965 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1966 TYPE_QUAL_RESTRICT);
6de9cd9a 1967
4409de24
TB
1968 if (as->rank == 0)
1969 {
f19626cf 1970 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
5aacb11e
TB
1971 {
1972 type = build_pointer_type (type);
4409de24 1973
5aacb11e 1974 if (restricted)
f04986a9 1975 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
4409de24 1976
4409de24 1977 GFC_ARRAY_TYPE_P (type) = 1;
f04986a9 1978 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
4409de24
TB
1979 }
1980
93c96e3a 1981 goto array_type_done;
4409de24
TB
1982 }
1983
6de9cd9a
DN
1984 if (known_stride)
1985 {
1986 mpz_sub_ui (stride, stride, 1);
1987 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1988 }
1989 else
1990 range = NULL_TREE;
1991
7ab92584 1992 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
6de9cd9a
DN
1993 TYPE_DOMAIN (type) = range;
1994
1995 build_pointer_type (etype);
1996 TREE_TYPE (type) = etype;
1997
1998 layout_type (type);
1999
09775c40
AO
2000 /* Represent packed arrays as multi-dimensional if they have rank >
2001 1 and with proper bounds, instead of flat arrays. This makes for
2002 better debug info. */
2003 if (known_offset)
08789087
JJ
2004 {
2005 tree gtype = etype, rtype, type_decl;
2006
2007 for (n = as->rank - 1; n >= 0; n--)
2008 {
2009 rtype = build_range_type (gfc_array_index_type,
2010 GFC_TYPE_ARRAY_LBOUND (type, n),
2011 GFC_TYPE_ARRAY_UBOUND (type, n));
2012 gtype = build_array_type (gtype, rtype);
2013 }
c2255bc4
AH
2014 TYPE_NAME (type) = type_decl = build_decl (input_location,
2015 TYPE_DECL, NULL, gtype);
08789087
JJ
2016 DECL_ORIGINAL_TYPE (type_decl) = gtype;
2017 }
2018
b8ff4e88 2019 if (packed != PACKED_STATIC || !known_stride
f19626cf 2020 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
6de9cd9a 2021 {
841b0c1f
PB
2022 /* For dummy arrays and automatic (heap allocated) arrays we
2023 want a pointer to the array. */
6de9cd9a 2024 type = build_pointer_type (type);
10174ddf
MM
2025 if (restricted)
2026 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
6de9cd9a
DN
2027 GFC_ARRAY_TYPE_P (type) = 1;
2028 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
2029 }
93c96e3a
HA
2030
2031array_type_done:
2032 mpz_clear (offset);
2033 mpz_clear (stride);
2034 mpz_clear (delta);
2035
6de9cd9a
DN
2036 return type;
2037}
2038
aa13dc3c 2039
4c73896d
RH
2040/* Return or create the base type for an array descriptor. */
2041
2042static tree
de91486c 2043gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
4c73896d 2044{
35151cd5 2045 tree fat_type, decl, arraytype, *chain = NULL;
f33beee9 2046 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
c62c6622
TB
2047 int idx;
2048
2049 /* Assumed-rank array. */
2050 if (dimen == -1)
2051 dimen = GFC_MAX_DIMENSIONS;
2052
2053 idx = 2 * (codimen + dimen) + restricted;
4c73896d 2054
c62c6622 2055 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
af232d48 2056
f19626cf 2057 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
af232d48
TB
2058 {
2059 if (gfc_array_descriptor_base_caf[idx])
2060 return gfc_array_descriptor_base_caf[idx];
2061 }
2062 else if (gfc_array_descriptor_base[idx])
10174ddf 2063 return gfc_array_descriptor_base[idx];
4c73896d
RH
2064
2065 /* Build the type node. */
2066 fat_type = make_node (RECORD_TYPE);
2067
bf65e4b1 2068 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
4c73896d 2069 TYPE_NAME (fat_type) = get_identifier (name);
cd3f04c8 2070 TYPE_NAMELESS (fat_type) = 1;
4c73896d
RH
2071
2072 /* Add the data member as the first element of the descriptor. */
8ba6ea87
ML
2073 gfc_add_field_to_struct_1 (fat_type,
2074 get_identifier ("data"),
2075 (restricted
2076 ? prvoid_type_node
2077 : ptr_type_node), &chain);
4c73896d
RH
2078
2079 /* Add the base component. */
35151cd5 2080 decl = gfc_add_field_to_struct_1 (fat_type,
dfd6ece2
NF
2081 get_identifier ("offset"),
2082 gfc_array_index_type, &chain);
d5e69948 2083 suppress_warning (decl);
4c73896d
RH
2084
2085 /* Add the dtype component. */
35151cd5 2086 decl = gfc_add_field_to_struct_1 (fat_type,
dfd6ece2 2087 get_identifier ("dtype"),
7fb43006 2088 get_dtype_type_node (), &chain);
d5e69948 2089 suppress_warning (decl);
4c73896d 2090
ff3598bc
PT
2091 /* Add the span component. */
2092 decl = gfc_add_field_to_struct_1 (fat_type,
2093 get_identifier ("span"),
2094 gfc_array_index_type, &chain);
d5e69948 2095 suppress_warning (decl);
ff3598bc 2096
4c73896d 2097 /* Build the array type for the stride and bound components. */
c62c6622
TB
2098 if (dimen + codimen > 0)
2099 {
2100 arraytype =
2101 build_array_type (gfc_get_desc_dim_type (),
2102 build_range_type (gfc_array_index_type,
2103 gfc_index_zero_node,
2104 gfc_rank_cst[codimen + dimen - 1]));
2105
2106 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
2107 arraytype, &chain);
d5e69948 2108 suppress_warning (decl);
c62c6622 2109 }
4c73896d 2110
f549bfb3 2111 if (flag_coarray == GFC_FCOARRAY_LIB)
af232d48
TB
2112 {
2113 decl = gfc_add_field_to_struct_1 (fat_type,
2114 get_identifier ("token"),
2115 prvoid_type_node, &chain);
d5e69948 2116 suppress_warning (decl);
af232d48
TB
2117 }
2118
4c73896d 2119 /* Finish off the type. */
4c73896d 2120 gfc_finish_type (fat_type);
dfcf0b12 2121 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
4c73896d 2122
de91486c 2123 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
af232d48
TB
2124 gfc_array_descriptor_base_caf[idx] = fat_type;
2125 else
2126 gfc_array_descriptor_base[idx] = fat_type;
2127
4c73896d
RH
2128 return fat_type;
2129}
6de9cd9a 2130
aa13dc3c 2131
6de9cd9a
DN
2132/* Build an array (descriptor) type with given bounds. */
2133
2134tree
f33beee9 2135gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
fad0afd7 2136 tree * ubound, int packed,
de91486c 2137 enum gfc_array_kind akind, bool restricted)
6de9cd9a 2138{
f33beee9 2139 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
416a8af4 2140 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
9aa433c2 2141 const char *type_name;
4c73896d 2142 int n;
6de9cd9a 2143
de91486c 2144 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
9618fb3c 2145 fat_type = build_distinct_type_copy (base_type);
5d4e9f7b
RB
2146 /* Unshare TYPE_FIELDs. */
2147 for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
2148 {
2149 tree next = DECL_CHAIN (*tp);
2150 *tp = copy_node (*tp);
2151 DECL_CONTEXT (*tp) = fat_type;
2152 DECL_CHAIN (*tp) = next;
2153 }
10174ddf
MM
2154 /* Make sure that nontarget and target array type have the same canonical
2155 type (and same stub decl for debug info). */
de91486c 2156 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
9618fb3c
RG
2157 TYPE_CANONICAL (fat_type) = base_type;
2158 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
40cb3f8a
JH
2159 /* Arrays of unknown type must alias with all array descriptors. */
2160 TYPE_TYPELESS_STORAGE (base_type) = 1;
2161 TYPE_TYPELESS_STORAGE (fat_type) = 1;
2162 gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
6de9cd9a 2163
d514626e
JRFS
2164 tmp = etype;
2165 if (TREE_CODE (tmp) == ARRAY_TYPE
2166 && TYPE_STRING_FLAG (tmp))
2167 tmp = TREE_TYPE (etype);
2168 tmp = TYPE_NAME (tmp);
6de9cd9a
DN
2169 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
2170 tmp = DECL_NAME (tmp);
2171 if (tmp)
9aa433c2 2172 type_name = IDENTIFIER_POINTER (tmp);
6de9cd9a 2173 else
9aa433c2 2174 type_name = "unknown";
bf65e4b1 2175 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
9aa433c2 2176 GFC_MAX_SYMBOL_LEN, type_name);
6de9cd9a 2177 TYPE_NAME (fat_type) = get_identifier (name);
cd3f04c8 2178 TYPE_NAMELESS (fat_type) = 1;
6de9cd9a 2179
4c73896d 2180 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
766090c2 2181 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
4c73896d
RH
2182
2183 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
a3935ffc 2184 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
4c73896d 2185 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
fad0afd7 2186 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
6de9cd9a
DN
2187
2188 /* Build an array descriptor record type. */
2189 if (packed != 0)
7ab92584 2190 stride = gfc_index_one_node;
6de9cd9a
DN
2191 else
2192 stride = NULL_TREE;
4ca9939b 2193 for (n = 0; n < dimen + codimen; n++)
6de9cd9a 2194 {
4ca9939b
TB
2195 if (n < dimen)
2196 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
6de9cd9a
DN
2197
2198 if (lbound)
2199 lower = lbound[n];
2200 else
2201 lower = NULL_TREE;
2202
2203 if (lower != NULL_TREE)
2204 {
2205 if (INTEGER_CST_P (lower))
2206 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
2207 else
2208 lower = NULL_TREE;
2209 }
2210
4ca9939b
TB
2211 if (codimen && n == dimen + codimen - 1)
2212 break;
2213
6de9cd9a
DN
2214 upper = ubound[n];
2215 if (upper != NULL_TREE)
2216 {
2217 if (INTEGER_CST_P (upper))
2218 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
2219 else
2220 upper = NULL_TREE;
2221 }
2222
4ca9939b
TB
2223 if (n >= dimen)
2224 continue;
2225
6de9cd9a
DN
2226 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
2227 {
bc98ed60
TB
2228 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2229 gfc_array_index_type, upper, lower);
2230 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2231 gfc_array_index_type, tmp,
2232 gfc_index_one_node);
2233 stride = fold_build2_loc (input_location, MULT_EXPR,
2234 gfc_array_index_type, tmp, stride);
6de9cd9a 2235 /* Check the folding worked. */
6e45f57b 2236 gcc_assert (INTEGER_CST_P (stride));
6de9cd9a
DN
2237 }
2238 else
2239 stride = NULL_TREE;
2240 }
2241 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
4c73896d 2242
6de9cd9a
DN
2243 /* TODO: known offsets for descriptors. */
2244 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2245
badd9e69
TB
2246 if (dimen == 0)
2247 {
2248 arraytype = build_pointer_type (etype);
2249 if (restricted)
2250 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2251
2252 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2253 return fat_type;
2254 }
2255
416a8af4
RG
2256 /* We define data as an array with the correct size if possible.
2257 Much better than doing pointer arithmetic. */
2258 if (stride)
2259 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2260 int_const_binop (MINUS_EXPR, stride,
807e902e 2261 build_int_cst (TREE_TYPE (stride), 1)));
416a8af4
RG
2262 else
2263 rtype = gfc_array_range_type;
2264 arraytype = build_array_type (etype, rtype);
6de9cd9a 2265 arraytype = build_pointer_type (arraytype);
10174ddf
MM
2266 if (restricted)
2267 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
6de9cd9a
DN
2268 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2269
d560566a
AO
2270 /* This will generate the base declarations we need to emit debug
2271 information for this type. FIXME: there must be a better way to
2272 avoid divergence between compilations with and without debug
2273 information. */
2274 {
2275 struct array_descr_info info;
2276 gfc_get_array_descr_info (fat_type, &info);
2277 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2278 }
2279
6de9cd9a
DN
2280 return fat_type;
2281}
2282\f
2283/* Build a pointer type. This function is called from gfc_sym_type(). */
c3e8c6b8 2284
6de9cd9a
DN
2285static tree
2286gfc_build_pointer_type (gfc_symbol * sym, tree type)
2287{
436529ea 2288 /* Array pointer types aren't actually pointers. */
6de9cd9a
DN
2289 if (sym->attr.dimension)
2290 return type;
2291 else
2292 return build_pointer_type (type);
2293}
b3c1b8a1
MM
2294
2295static tree gfc_nonrestricted_type (tree t);
2296/* Given two record or union type nodes TO and FROM, ensure
2297 that all fields in FROM have a corresponding field in TO,
2298 their type being nonrestrict variants. This accepts a TO
2299 node that already has a prefix of the fields in FROM. */
2300static void
2301mirror_fields (tree to, tree from)
2302{
2303 tree fto, ffrom;
2304 tree *chain;
2305
2306 /* Forward to the end of TOs fields. */
2307 fto = TYPE_FIELDS (to);
2308 ffrom = TYPE_FIELDS (from);
2309 chain = &TYPE_FIELDS (to);
2310 while (fto)
2311 {
2312 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2313 chain = &DECL_CHAIN (fto);
2314 fto = DECL_CHAIN (fto);
2315 ffrom = DECL_CHAIN (ffrom);
2316 }
2317
2318 /* Now add all fields remaining in FROM (starting with ffrom). */
2319 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2320 {
2321 tree newfield = copy_node (ffrom);
2322 DECL_CONTEXT (newfield) = to;
2323 /* The store to DECL_CHAIN might seem redundant with the
2324 stores to *chain, but not clearing it here would mean
2325 leaving a chain into the old fields. If ever
2326 our called functions would look at them confusion
2327 will arise. */
2328 DECL_CHAIN (newfield) = NULL_TREE;
2329 *chain = newfield;
2330 chain = &DECL_CHAIN (newfield);
2331
2332 if (TREE_CODE (ffrom) == FIELD_DECL)
2333 {
2334 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2335 TREE_TYPE (newfield) = elemtype;
2336 }
2337 }
2338 *chain = NULL_TREE;
2339}
2340
2341/* Given a type T, returns a different type of the same structure,
2342 except that all types it refers to (recursively) are always
2343 non-restrict qualified types. */
2344static tree
2345gfc_nonrestricted_type (tree t)
2346{
2347 tree ret = t;
2348
eea58adb 2349 /* If the type isn't laid out yet, don't copy it. If something
b3c1b8a1
MM
2350 needs it for real it should wait until the type got finished. */
2351 if (!TYPE_SIZE (t))
2352 return t;
2353
2354 if (!TYPE_LANG_SPECIFIC (t))
766090c2 2355 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
b3c1b8a1
MM
2356 /* If we're dealing with this very node already further up
2357 the call chain (recursion via pointers and struct members)
2358 we haven't yet determined if we really need a new type node.
2359 Assume we don't, return T itself. */
2360 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2361 return t;
2362
2363 /* If we have calculated this all already, just return it. */
2364 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2365 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2366
2367 /* Mark this type. */
2368 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2369
2370 switch (TREE_CODE (t))
2371 {
2372 default:
2373 break;
2374
2375 case POINTER_TYPE:
2376 case REFERENCE_TYPE:
2377 {
2378 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2379 if (totype == TREE_TYPE (t))
2380 ret = t;
2381 else if (TREE_CODE (t) == POINTER_TYPE)
2382 ret = build_pointer_type (totype);
2383 else
2384 ret = build_reference_type (totype);
2385 ret = build_qualified_type (ret,
2386 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2387 }
2388 break;
2389
2390 case ARRAY_TYPE:
2391 {
2392 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2393 if (elemtype == TREE_TYPE (t))
2394 ret = t;
2395 else
2396 {
2397 ret = build_variant_type_copy (t);
2398 TREE_TYPE (ret) = elemtype;
2399 if (TYPE_LANG_SPECIFIC (t)
2400 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2401 {
2402 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2403 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2404 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2405 {
2406 TYPE_LANG_SPECIFIC (ret)
766090c2 2407 = ggc_cleared_alloc<struct lang_type> ();
b3c1b8a1
MM
2408 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2409 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2410 }
2411 }
2412 }
2413 }
2414 break;
2415
2416 case RECORD_TYPE:
2417 case UNION_TYPE:
2418 case QUAL_UNION_TYPE:
2419 {
2420 tree field;
2421 /* First determine if we need a new type at all.
2422 Careful, the two calls to gfc_nonrestricted_type per field
2423 might return different values. That happens exactly when
2424 one of the fields reaches back to this very record type
2425 (via pointers). The first calls will assume that we don't
2426 need to copy T (see the error_mark_node marking). If there
2427 are any reasons for copying T apart from having to copy T,
2428 we'll indeed copy it, and the second calls to
2429 gfc_nonrestricted_type will use that new node if they
2430 reach back to T. */
2431 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2432 if (TREE_CODE (field) == FIELD_DECL)
2433 {
2434 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2435 if (elemtype != TREE_TYPE (field))
2436 break;
2437 }
2438 if (!field)
2439 break;
2440 ret = build_variant_type_copy (t);
2441 TYPE_FIELDS (ret) = NULL_TREE;
2442
2443 /* Here we make sure that as soon as we know we have to copy
2444 T, that also fields reaching back to us will use the new
2445 copy. It's okay if that copy still contains the old fields,
2446 we won't look at them. */
2447 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2448 mirror_fields (ret, t);
2449 }
2450 break;
2451 }
2452
2453 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2454 return ret;
2455}
2456
6de9cd9a
DN
2457\f
2458/* Return the type for a symbol. Special handling is required for character
2459 types to get the correct level of indirection.
2460 For functions return the return type.
ad6e2a18
TS
2461 For subroutines return void_type_node.
2462 Calling this multiple times for the same symbol should be avoided,
2463 especially for character and array types. */
c3e8c6b8 2464
6de9cd9a 2465tree
64f96237 2466gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
6de9cd9a
DN
2467{
2468 tree type;
2469 int byref;
10174ddf 2470 bool restricted;
6de9cd9a 2471
3070bab4
JW
2472 /* Procedure Pointers inside COMMON blocks. */
2473 if (sym->attr.proc_pointer && sym->attr.in_common)
00625fae
JW
2474 {
2475 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2476 sym->attr.proc_pointer = 0;
2477 type = build_pointer_type (gfc_get_function_type (sym));
2478 sym->attr.proc_pointer = 1;
2479 return type;
2480 }
2481
6de9cd9a
DN
2482 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2483 return void_type_node;
2484
da4c6ed8
TS
2485 /* In the case of a function the fake result variable may have a
2486 type different from the function type, so don't return early in
2487 that case. */
2488 if (sym->backend_decl && !sym->attr.function)
2489 return TREE_TYPE (sym->backend_decl);
6de9cd9a 2490
f36a7f04
PT
2491 if (sym->attr.result
2492 && sym->ts.type == BT_CHARACTER
2493 && sym->ts.u.cl->backend_decl == NULL_TREE
1996c75e 2494 && sym->ns->proc_name
482768bd 2495 && sym->ns->proc_name->ts.u.cl
f36a7f04
PT
2496 && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2497 sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2498
665733c1
JJ
2499 if (sym->ts.type == BT_CHARACTER
2500 && ((sym->attr.function && sym->attr.is_bind_c)
906b4e15 2501 || ((sym->attr.result || sym->attr.value)
665733c1 2502 && sym->ns->proc_name
9b548517 2503 && sym->ns->proc_name->attr.is_bind_c)
0231b076
AV
2504 || (sym->ts.deferred
2505 && (!sym->ts.u.cl
2506 || !sym->ts.u.cl->backend_decl
2507 || sym->attr.save))
564b637f
MM
2508 || (sym->attr.dummy
2509 && sym->attr.value
2510 && gfc_length_one_character_type_p (&sym->ts))))
89502883 2511 type = gfc_get_char_type (sym->ts.kind);
06a54338 2512 else
3c9f5092 2513 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
6de9cd9a 2514
47d13acb
TK
2515 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
2516 && !sym->pass_as_value)
6de9cd9a
DN
2517 byref = 1;
2518 else
2519 byref = 0;
2520
9c3a880f
HA
2521 restricted = (!sym->attr.target && !IS_POINTER (sym)
2522 && !IS_PROC_POINTER (sym) && !sym->attr.cray_pointee);
b3c1b8a1
MM
2523 if (!restricted)
2524 type = gfc_nonrestricted_type (type);
2525
64f96237
TB
2526 /* Dummy argument to a bind(C) procedure. */
2527 if (is_bind_c && is_CFI_desc (sym, NULL))
2528 type = gfc_get_cfi_type (sym->attr.dimension ? sym->as->rank : 0,
2529 /* restricted = */ false);
2530 else if (sym->attr.dimension || sym->attr.codimension)
6de9cd9a
DN
2531 {
2532 if (gfc_is_nodesc_array (sym))
2533 {
2534 /* If this is a character argument of unknown length, just use the
2535 base type. */
2536 if (sym->ts.type != BT_CHARACTER
b49a3de7 2537 || !(sym->attr.dummy || sym->attr.function)
bc21d315 2538 || sym->ts.u.cl->backend_decl)
6de9cd9a
DN
2539 {
2540 type = gfc_get_nodesc_array_type (type, sym->as,
dcfef7d4 2541 byref ? PACKED_FULL
10174ddf
MM
2542 : PACKED_STATIC,
2543 restricted);
6de9cd9a
DN
2544 byref = 0;
2545 }
2546 }
2547 else
fad0afd7
JJ
2548 {
2549 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2550 if (sym->attr.pointer)
fe4e525c
TB
2551 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2552 : GFC_ARRAY_POINTER;
fad0afd7
JJ
2553 else if (sym->attr.allocatable)
2554 akind = GFC_ARRAY_ALLOCATABLE;
fe4e525c 2555 type = gfc_build_array_type (type, sym->as, akind, restricted,
723b30be 2556 sym->attr.contiguous, sym->as->corank);
fad0afd7 2557 }
a8b3b0b6 2558 }
6de9cd9a
DN
2559 else
2560 {
571d54de
DK
2561 if (sym->attr.allocatable || sym->attr.pointer
2562 || gfc_is_associate_pointer (sym))
6de9cd9a
DN
2563 type = gfc_build_pointer_type (sym, type);
2564 }
2565
2566 /* We currently pass all parameters by reference.
2567 See f95_get_function_decl. For dummy function parameters return the
2568 function type. */
2569 if (byref)
1619aa6f
PB
2570 {
2571 /* We must use pointer types for potentially absent variables. The
2572 optimizers assume a reference type argument is never NULL. */
eb92cd57
TB
2573 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2574 || sym->attr.optional
30ff79fa 2575 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
1619aa6f
PB
2576 type = build_pointer_type (type);
2577 else
9c3a880f
HA
2578 type = build_reference_type (type);
2579
2580 if (restricted)
2581 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1619aa6f 2582 }
6de9cd9a
DN
2583
2584 return (type);
2585}
2586\f
2587/* Layout and output debug info for a record type. */
c3e8c6b8 2588
6de9cd9a
DN
2589void
2590gfc_finish_type (tree type)
2591{
2592 tree decl;
2593
c2255bc4
AH
2594 decl = build_decl (input_location,
2595 TYPE_DECL, NULL_TREE, type);
6de9cd9a
DN
2596 TYPE_STUB_DECL (type) = decl;
2597 layout_type (type);
2598 rest_of_type_compilation (type, 1);
0e6df31e 2599 rest_of_decl_compilation (decl, 1, 0);
6de9cd9a
DN
2600}
2601\f
2602/* Add a field of given NAME and TYPE to the context of a UNION_TYPE
dfd6ece2 2603 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
35151cd5 2604 to the end of the field list pointed to by *CHAIN.
6de9cd9a
DN
2605
2606 Returns a pointer to the new field. */
c3e8c6b8 2607
dfd6ece2 2608static tree
35151cd5 2609gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
dfd6ece2
NF
2610{
2611 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2612
2613 DECL_CONTEXT (decl) = context;
910ad8de 2614 DECL_CHAIN (decl) = NULL_TREE;
35151cd5
MM
2615 if (TYPE_FIELDS (context) == NULL_TREE)
2616 TYPE_FIELDS (context) = decl;
dfd6ece2
NF
2617 if (chain != NULL)
2618 {
2619 if (*chain != NULL)
2620 **chain = decl;
910ad8de 2621 *chain = &DECL_CHAIN (decl);
dfd6ece2
NF
2622 }
2623
2624 return decl;
2625}
2626
2627/* Like `gfc_add_field_to_struct_1', but adds alignment
2628 information. */
2629
6de9cd9a 2630tree
35151cd5 2631gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
6de9cd9a 2632{
35151cd5 2633 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
6de9cd9a 2634
6de9cd9a 2635 DECL_INITIAL (decl) = 0;
fe37c7af 2636 SET_DECL_ALIGN (decl, 0);
6de9cd9a 2637 DECL_USER_ALIGN (decl) = 0;
6de9cd9a
DN
2638
2639 return decl;
2640}
2641
2642
6b887797
PT
2643/* Copy the backend_decl and component backend_decls if
2644 the two derived type symbols are "equal", as described
2645 in 4.4.2 and resolved by gfc_compare_derived_types. */
2646
c072df1a 2647bool
43afc047 2648gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
0101807c 2649 bool from_gsym)
6b887797
PT
2650{
2651 gfc_component *to_cm;
2652 gfc_component *from_cm;
2653
f29bda83
TB
2654 if (from == to)
2655 return 1;
2656
6b887797
PT
2657 if (from->backend_decl == NULL
2658 || !gfc_compare_derived_types (from, to))
2659 return 0;
2660
2661 to->backend_decl = from->backend_decl;
2662
2663 to_cm = to->components;
2664 from_cm = from->components;
2665
2666 /* Copy the component declarations. If a component is itself
2667 a derived type, we need a copy of its component declarations.
2668 This is done by recursing into gfc_get_derived_type and
2669 ensures that the component's component declarations have
f04986a9 2670 been built. If it is a character, we need the character
6b887797
PT
2671 length, as well. */
2672 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2673 {
2674 to_cm->backend_decl = from_cm->backend_decl;
f549bfb3 2675 to_cm->caf_token = from_cm->caf_token;
f6288c24
FR
2676 if (from_cm->ts.type == BT_UNION)
2677 gfc_get_union_type (to_cm->ts.u.derived);
2678 else if (from_cm->ts.type == BT_DERIVED
78a1d149
JW
2679 && (!from_cm->attr.pointer || from_gsym))
2680 gfc_get_derived_type (to_cm->ts.u.derived);
2681 else if (from_cm->ts.type == BT_CLASS
2682 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
bc21d315 2683 gfc_get_derived_type (to_cm->ts.u.derived);
6b887797 2684 else if (from_cm->ts.type == BT_CHARACTER)
bc21d315 2685 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
6b887797
PT
2686 }
2687
2688 return 1;
2689}
2690
2691
713485cc
JW
2692/* Build a tree node for a procedure pointer component. */
2693
fd39c4bf 2694static tree
713485cc
JW
2695gfc_get_ppc_type (gfc_component* c)
2696{
2697 tree t;
37513ce9
JW
2698
2699 /* Explicit interface. */
2700 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2701 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2702
2703 /* Implicit interface (only return value may be known). */
2704 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2705 t = gfc_typenode_for_spec (&c->ts);
713485cc
JW
2706 else
2707 t = void_type_node;
37513ce9 2708
23f8b90c
FXC
2709 /* FIXME: it would be better to provide explicit interfaces in all
2710 cases, since they should be known by the compiler. */
7c72651a 2711 return build_pointer_type (build_function_type (t, NULL_TREE));
713485cc
JW
2712}
2713
2714
f6288c24
FR
2715/* Build a tree node for a union type. Requires building each map
2716 structure which is an element of the union. */
2717
2718tree
2719gfc_get_union_type (gfc_symbol *un)
2720{
2721 gfc_component *map = NULL;
2722 tree typenode = NULL, map_type = NULL, map_field = NULL;
2723 tree *chain = NULL;
2724
2725 if (un->backend_decl)
2726 {
2727 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2728 return un->backend_decl;
2729 else
2730 typenode = un->backend_decl;
2731 }
2732 else
2733 {
2734 typenode = make_node (UNION_TYPE);
2735 TYPE_NAME (typenode) = get_identifier (un->name);
2736 }
2737
2738 /* Add each contained MAP as a field. */
2739 for (map = un->components; map; map = map->next)
2740 {
2741 gcc_assert (map->ts.type == BT_DERIVED);
2742
2743 /* The map's type node, which is defined within this union's context. */
2744 map_type = gfc_get_derived_type (map->ts.u.derived);
2745 TYPE_CONTEXT (map_type) = typenode;
2746
2747 /* The map field's declaration. */
2748 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2749 map_type, &chain);
ffdfc5b0
TB
2750 if (GFC_LOCUS_IS_SET (map->loc))
2751 gfc_set_decl_location (map_field, &map->loc);
2752 else if (GFC_LOCUS_IS_SET (un->declared_at))
2753 gfc_set_decl_location (map_field, &un->declared_at);
f6288c24
FR
2754
2755 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2756 DECL_NAMELESS(map_field) = true;
2757
2758 /* We should never clobber another backend declaration for this map,
2759 because each map component is unique. */
2760 if (!map->backend_decl)
2761 map->backend_decl = map_field;
2762 }
2763
2764 un->backend_decl = typenode;
2765 gfc_finish_type (typenode);
2766
2767 return typenode;
2768}
2769
18f3b223
AV
2770bool
2771cobounds_match_decl (const gfc_symbol *derived)
2772{
2773 tree arrtype, tmp;
2774 gfc_array_spec *as;
2775
2776 if (!derived->backend_decl)
2777 return false;
2778 /* Care only about coarray declarations. Everything else is ok with us. */
2779 if (!derived->components || strcmp (derived->components->name, "_data") != 0)
2780 return true;
2781 if (!derived->components->attr.codimension)
2782 return true;
2783
2784 arrtype = TREE_TYPE (TYPE_FIELDS (derived->backend_decl));
2785 as = derived->components->as;
2786 if (GFC_TYPE_ARRAY_CORANK (arrtype) != as->corank)
2787 return false;
2788
2789 for (int dim = as->rank; dim < as->rank + as->corank; ++dim)
2790 {
2791 /* Check lower bound. */
2792 tmp = TYPE_LANG_SPECIFIC (arrtype)->lbound[dim];
2793 if (!tmp || !INTEGER_CST_P (tmp))
2794 return false;
2795 if (as->lower[dim]->expr_type != EXPR_CONSTANT
2796 || as->lower[dim]->ts.type != BT_INTEGER)
2797 return false;
2798 if (*tmp->int_cst.val != mpz_get_si (as->lower[dim]->value.integer))
2799 return false;
2800
2801 /* Check upper bound. */
2802 tmp = TYPE_LANG_SPECIFIC (arrtype)->ubound[dim];
2803 if (!tmp && !as->upper[dim])
2804 continue;
2805
2806 if (!tmp || !INTEGER_CST_P (tmp))
2807 return false;
2808 if (as->upper[dim]->expr_type != EXPR_CONSTANT
2809 || as->upper[dim]->ts.type != BT_INTEGER)
2810 return false;
2811 if (*tmp->int_cst.val != mpz_get_si (as->upper[dim]->value.integer))
2812 return false;
2813 }
2814
2815 return true;
2816}
f6288c24 2817
6b887797
PT
2818/* Build a tree node for a derived type. If there are equal
2819 derived types, with different local names, these are built
2820 at the same time. If an equal derived type has been built
2821 in a parent namespace, this is used. */
c3e8c6b8 2822
7c1dab0d 2823tree
de91486c 2824gfc_get_derived_type (gfc_symbol * derived, int codimen)
6de9cd9a 2825{
35151cd5 2826 tree typenode = NULL, field = NULL, field_type = NULL;
3af8d8cb 2827 tree canonical = NULL_TREE;
dfd6ece2 2828 tree *chain = NULL;
3af8d8cb 2829 bool got_canonical = false;
8b704316 2830 bool unlimited_entity = false;
91d52f87 2831 gfc_component *c;
3af8d8cb 2832 gfc_namespace *ns;
57905c2b 2833 tree tmp;
18f3b223 2834 bool coarray_flag, class_coarray_flag;
f549bfb3
PT
2835
2836 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2837 && derived->module && !derived->attr.vtype;
18f3b223
AV
2838 class_coarray_flag = derived->components
2839 && derived->components->ts.type == BT_DERIVED
2840 && strcmp (derived->components->name, "_data") == 0
2841 && derived->components->attr.codimension
2842 && derived->components->as->cotype == AS_EXPLICIT;
6de9cd9a 2843
5bab4c96
PT
2844 gcc_assert (!derived->attr.pdt_template);
2845
9f3880d1
TB
2846 if (derived->attr.unlimited_polymorphic
2847 || (flag_coarray == GFC_FCOARRAY_LIB
2848 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
83aa73e7 2849 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
f8862a1b
DR
2850 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2851 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
8b704316
PT
2852 return ptr_type_node;
2853
5df445a2
TB
2854 if (flag_coarray != GFC_FCOARRAY_LIB
2855 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
f8862a1b
DR
2856 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2857 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
5df445a2
TB
2858 return gfc_get_int_type (gfc_default_integer_kind);
2859
c3f34952
TB
2860 if (derived && derived->attr.flavor == FL_PROCEDURE
2861 && derived->attr.generic)
2862 derived = gfc_find_dt_in_generic (derived);
2863
a8b3b0b6 2864 /* See if it's one of the iso_c_binding derived types. */
cadddfdd 2865 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
a8b3b0b6 2866 {
9dc35956
CR
2867 if (derived->backend_decl)
2868 return derived->backend_decl;
2869
089db47d
CR
2870 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2871 derived->backend_decl = ptr_type_node;
2872 else
2873 derived->backend_decl = pfunc_type_node;
9dc35956 2874
a8b3b0b6
CR
2875 derived->ts.kind = gfc_index_integer_kind;
2876 derived->ts.type = BT_INTEGER;
2877 /* Set the f90_type to BT_VOID as a way to recognize something of type
2878 BT_INTEGER that needs to fit a void * for the purpose of the
2879 iso_c_binding derived types. */
2880 derived->ts.f90_type = BT_VOID;
f04986a9 2881
a8b3b0b6
CR
2882 return derived->backend_decl;
2883 }
3af8d8cb 2884
0101807c 2885 /* If use associated, use the module type for this one. */
9fa52231 2886 if (derived->backend_decl == NULL
c4565031 2887 && (derived->attr.use_assoc || derived->attr.used_in_submodule)
9fa52231
TB
2888 && derived->module
2889 && gfc_get_module_backend_decl (derived))
0101807c 2890 goto copy_derived_types;
3af8d8cb 2891
9fa52231
TB
2892 /* The derived types from an earlier namespace can be used as the
2893 canonical type. */
c4565031
PT
2894 if (derived->backend_decl == NULL
2895 && !derived->attr.use_assoc
2896 && !derived->attr.used_in_submodule
9fa52231 2897 && gfc_global_ns_list)
3af8d8cb
PT
2898 {
2899 for (ns = gfc_global_ns_list;
2900 ns->translated && !got_canonical;
2901 ns = ns->sibling)
2902 {
20e8ceae 2903 if (ns->derived_types)
3af8d8cb 2904 {
20e8ceae
AB
2905 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2906 dt = dt->dt_next)
2907 {
2908 gfc_copy_dt_decls_ifequal (dt, derived, true);
2909 if (derived->backend_decl)
2910 got_canonical = true;
2911 if (dt->dt_next == ns->derived_types)
2912 break;
2913 }
2914 }
2915 }
3af8d8cb
PT
2916 }
2917
2918 /* Store up the canonical type to be added to this one. */
2919 if (got_canonical)
2920 {
2921 if (TYPE_CANONICAL (derived->backend_decl))
2922 canonical = TYPE_CANONICAL (derived->backend_decl);
2923 else
2924 canonical = derived->backend_decl;
2925
2926 derived->backend_decl = NULL_TREE;
2927 }
2928
6de9cd9a 2929 /* derived->backend_decl != 0 means we saw it before, but its
436529ea 2930 components' backend_decl may have not been built. */
18f3b223
AV
2931 if (derived->backend_decl
2932 && (!class_coarray_flag || cobounds_match_decl (derived)))
3e6d828d 2933 {
37513ce9
JW
2934 /* Its components' backend_decl have been built or we are
2935 seeing recursion through the formal arglist of a procedure
2936 pointer component. */
ebd63afa 2937 if (TYPE_FIELDS (derived->backend_decl))
18f3b223 2938 return derived->backend_decl;
ebd63afa
PT
2939 else if (derived->attr.abstract
2940 && derived->attr.proc_pointer_comp)
2941 {
2942 /* If an abstract derived type with procedure pointer
2943 components has no other type of component, return the
2944 backend_decl. Otherwise build the components if any of the
2945 non-procedure pointer components have no backend_decl. */
2946 for (c = derived->components; c; c = c->next)
2947 {
bf9f15ee
PT
2948 bool same_alloc_type = c->attr.allocatable
2949 && derived == c->ts.u.derived;
2950 if (!c->attr.proc_pointer
2951 && !same_alloc_type
2952 && c->backend_decl == NULL)
ebd63afa
PT
2953 break;
2954 else if (c->next == NULL)
2955 return derived->backend_decl;
2956 }
2957 typenode = derived->backend_decl;
2958 }
3e6d828d
JW
2959 else
2960 typenode = derived->backend_decl;
2961 }
6de9cd9a
DN
2962 else
2963 {
2964 /* We see this derived type first time, so build the type node. */
2965 typenode = make_node (RECORD_TYPE);
2966 TYPE_NAME (typenode) = get_identifier (derived->name);
c61819ff 2967 TYPE_PACKED (typenode) = flag_pack_derived;
6de9cd9a
DN
2968 derived->backend_decl = typenode;
2969 }
2970
8b704316 2971 if (derived->components
8293b9e4
TB
2972 && derived->components->ts.type == BT_DERIVED
2973 && startswith (derived->name, "__class")
2974 && strcmp (derived->components->name, "_data") == 0
2975 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
8b704316
PT
2976 unlimited_entity = true;
2977
bce71376
PT
2978 /* Go through the derived type components, building them as
2979 necessary. The reason for doing this now is that it is
2980 possible to recurse back to this derived type through a
2981 pointer component (PR24092). If this happens, the fields
2982 will be built and so we can return the type. */
2983 for (c = derived->components; c; c = c->next)
2984 {
f6288c24
FR
2985 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2986 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2987
cf2b3c22 2988 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
bce71376
PT
2989 continue;
2990
de915fbe
AV
2991 const bool incomplete_type
2992 = c->ts.u.derived->backend_decl
2993 && TREE_CODE (c->ts.u.derived->backend_decl) == RECORD_TYPE
2994 && !(TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)
2995 && TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)->size);
2996 const bool pointer_component
2997 = c->attr.pointer || c->attr.allocatable || c->attr.proc_pointer;
2998
2999 /* Prevent endless recursion on recursive types (i.e. types that reference
3000 themself in a component. Break the recursion by not building pointers
3001 to incomplete types again, aka types that are already in the build. */
3002 if (c->ts.u.derived->backend_decl == NULL
3003 || (c->attr.codimension && c->as->corank != codimen)
3004 || !(incomplete_type && pointer_component))
de91486c
AV
3005 {
3006 int local_codim = c->attr.codimension ? c->as->corank: codimen;
3007 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
3008 local_codim);
3009 }
a8b3b0b6 3010
efb63364 3011 if (c->ts.u.derived->attr.is_iso_c)
a8b3b0b6
CR
3012 {
3013 /* Need to copy the modified ts from the derived type. The
3014 typespec was modified because C_PTR/C_FUNPTR are translated
3015 into (void *) from derived types. */
bc21d315
JW
3016 c->ts.type = c->ts.u.derived->ts.type;
3017 c->ts.kind = c->ts.u.derived->ts.kind;
3018 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
9dc35956
CR
3019 if (c->initializer)
3020 {
3021 c->initializer->ts.type = c->ts.type;
3022 c->initializer->ts.kind = c->ts.kind;
3023 c->initializer->ts.f90_type = c->ts.f90_type;
3024 c->initializer->expr_type = EXPR_NULL;
3025 }
a8b3b0b6 3026 }
bce71376
PT
3027 }
3028
18f3b223 3029 if (!class_coarray_flag && TYPE_FIELDS (derived->backend_decl))
bce71376
PT
3030 return derived->backend_decl;
3031
6de9cd9a 3032 /* Build the type member list. Install the newly created RECORD_TYPE
f6288c24
FR
3033 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
3034 through only the top-level linked list of components so we correctly
3035 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
3036 types are built as part of gfc_get_union_type. */
6de9cd9a
DN
3037 for (c = derived->components; c; c = c->next)
3038 {
bf9f15ee
PT
3039 bool same_alloc_type = c->attr.allocatable
3040 && derived == c->ts.u.derived;
57905c2b
PT
3041 /* Prevent infinite recursion, when the procedure pointer type is
3042 the same as derived, by forcing the procedure pointer component to
3043 be built as if the explicit interface does not exist. */
3044 if (c->attr.proc_pointer
fc23d35a
JW
3045 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
3046 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
3047 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
3048 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
713485cc 3049 field_type = gfc_get_ppc_type (c);
57905c2b
PT
3050 else if (c->attr.proc_pointer && derived->backend_decl)
3051 {
d30869a8 3052 tmp = build_function_type (derived->backend_decl, NULL_TREE);
57905c2b
PT
3053 field_type = build_pointer_type (tmp);
3054 }
cf2b3c22 3055 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
f549bfb3
PT
3056 field_type = c->ts.u.derived->backend_decl;
3057 else if (c->attr.caf_token)
3058 field_type = pvoid_type_node;
6de9cd9a
DN
3059 else
3060 {
5bab4c96
PT
3061 if (c->ts.type == BT_CHARACTER
3062 && !c->ts.deferred && !c->attr.pdt_string)
6de9cd9a
DN
3063 {
3064 /* Evaluate the string length. */
bc21d315
JW
3065 gfc_conv_const_charlen (c->ts.u.cl);
3066 gcc_assert (c->ts.u.cl->backend_decl);
6de9cd9a 3067 }
2b3dc0db
PT
3068 else if (c->ts.type == BT_CHARACTER)
3069 c->ts.u.cl->backend_decl
3070 = build_int_cst (gfc_charlen_type_node, 0);
6de9cd9a 3071
de91486c 3072 field_type = gfc_typenode_for_spec (&c->ts, codimen);
6de9cd9a
DN
3073 }
3074
1f2959f0 3075 /* This returns an array descriptor type. Initialization may be
6de9cd9a 3076 required. */
241e79cf 3077 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
6de9cd9a 3078 {
5bab4c96 3079 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
6de9cd9a 3080 {
fad0afd7 3081 enum gfc_array_kind akind;
8293b9e4
TB
3082 bool is_ptr = ((c == derived->components
3083 && derived->components->ts.type == BT_DERIVED
3084 && startswith (derived->name, "__class")
3085 && (strcmp (derived->components->name, "_data")
3086 == 0))
3087 ? c->attr.class_pointer : c->attr.pointer);
3088 if (is_ptr)
fe4e525c
TB
3089 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
3090 : GFC_ARRAY_POINTER;
8293b9e4 3091 else if (c->attr.allocatable)
fad0afd7 3092 akind = GFC_ARRAY_ALLOCATABLE;
8293b9e4
TB
3093 else if (c->as->type == AS_ASSUMED_RANK)
3094 akind = GFC_ARRAY_ASSUMED_RANK;
3095 else
3096 /* FIXME – see PR fortran/104651. Additionally, the following
3097 gfc_build_array_type should use !is_ptr instead of
3098 c->attr.pointer and codim unconditionally without '? :'. */
3099 akind = GFC_ARRAY_ASSUMED_SHAPE;
1f2959f0 3100 /* Pointers to arrays aren't actually pointer types. The
723b30be
AV
3101 descriptors are separate, but the data is common. Every
3102 array pointer in a coarray derived type needs to provide space
3103 for the coarray management, too. Therefore treat coarrays
3104 and pointers to coarrays in derived types the same. */
3105 field_type = gfc_build_array_type
3106 (
3107 field_type, c->as, akind, !c->attr.target && !c->attr.pointer,
3108 c->attr.contiguous,
3109 c->attr.codimension || c->attr.pointer ? codimen : 0
3110 );
6de9cd9a
DN
3111 }
3112 else
dcfef7d4 3113 field_type = gfc_get_nodesc_array_type (field_type, c->as,
10174ddf
MM
3114 PACKED_STATIC,
3115 !c->attr.target);
6de9cd9a 3116 }
5bab4c96 3117 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
8b704316
PT
3118 && !c->attr.proc_pointer
3119 && !(unlimited_entity && c == derived->components))
6de9cd9a
DN
3120 field_type = build_pointer_type (field_type);
3121
bf9f15ee 3122 if (c->attr.pointer || same_alloc_type)
4bfd470b
TB
3123 field_type = gfc_nonrestricted_type (field_type);
3124
c9dfc414 3125 /* vtype fields can point to different types to the base type. */
8b704316
PT
3126 if (c->ts.type == BT_DERIVED
3127 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
0a8c9a13
PT
3128 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
3129 ptr_mode, true);
c9dfc414 3130
f04986a9
PT
3131 /* Ensure that the CLASS language specific flag is set. */
3132 if (c->ts.type == BT_CLASS)
3133 {
3134 if (POINTER_TYPE_P (field_type))
3135 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
3136 else
3137 GFC_CLASS_TYPE_P (field_type) = 1;
3138 }
3139
35151cd5 3140 field = gfc_add_field_to_struct (typenode,
dfd6ece2
NF
3141 get_identifier (c->name),
3142 field_type, &chain);
ffdfc5b0 3143 if (GFC_LOCUS_IS_SET (c->loc))
dfcf0b12 3144 gfc_set_decl_location (field, &c->loc);
ffdfc5b0 3145 else if (GFC_LOCUS_IS_SET (derived->declared_at))
dfcf0b12 3146 gfc_set_decl_location (field, &derived->declared_at);
6de9cd9a 3147
92d28cbb
JJ
3148 gfc_finish_decl_attrs (field, &c->attr);
3149
6de9cd9a
DN
3150 DECL_PACKED (field) |= TYPE_PACKED (typenode);
3151
bce71376 3152 gcc_assert (field);
18f3b223
AV
3153 /* Overwrite for class array to supply different bounds for different
3154 types. */
91d52f87 3155 if (class_coarray_flag || !c->backend_decl || c->attr.caf_token)
bce71376 3156 c->backend_decl = field;
3c9f5092 3157
18f3b223
AV
3158 if (c->attr.pointer && (c->attr.dimension || c->attr.codimension)
3159 && !(c->ts.type == BT_DERIVED && strcmp (c->name, "_data") == 0))
ff3598bc 3160 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
6de9cd9a
DN
3161 }
3162
35151cd5 3163 /* Now lay out the derived type, including the fields. */
64754ed5
RG
3164 if (canonical)
3165 TYPE_CANONICAL (typenode) = canonical;
6de9cd9a
DN
3166
3167 gfc_finish_type (typenode);
dfcf0b12 3168 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
96ffc6cd
TK
3169 if (derived->module && derived->ns->proc_name
3170 && derived->ns->proc_name->attr.flavor == FL_MODULE)
a64f5186
JJ
3171 {
3172 if (derived->ns->proc_name->backend_decl
3173 && TREE_CODE (derived->ns->proc_name->backend_decl)
3174 == NAMESPACE_DECL)
3175 {
3176 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
3177 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
3178 = derived->ns->proc_name->backend_decl;
3179 }
3180 }
6de9cd9a
DN
3181
3182 derived->backend_decl = typenode;
3183
3af8d8cb
PT
3184copy_derived_types:
3185
91d52f87
AV
3186 if (!derived->attr.vtype)
3187 for (c = derived->components; c; c = c->next)
3188 {
3189 /* Do not add a caf_token field for class container components. */
84947575 3190 if (codimen && coarray_flag && !c->attr.dimension
91d52f87
AV
3191 && !c->attr.codimension && (c->attr.allocatable || c->attr.pointer)
3192 && !derived->attr.is_class)
3193 {
3194 /* Provide sufficient space to hold "_caf_symbol". */
3195 char caf_name[GFC_MAX_SYMBOL_LEN + 6];
3196 gfc_component *token;
3197 snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
3198 token = gfc_find_component (derived, caf_name, true, true, NULL);
3199 gcc_assert (token);
3200 gfc_comp_caf_token (c) = token->backend_decl;
3201 suppress_warning (gfc_comp_caf_token (c));
3202 }
3203 }
f549bfb3 3204
20e8ceae
AB
3205 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
3206 {
3207 gfc_copy_dt_decls_ifequal (derived, dt, false);
3208 if (dt->dt_next == gfc_derived_types)
3209 break;
3210 }
6b887797 3211
e0e85e06 3212 return derived->backend_decl;
6de9cd9a 3213}
e0e85e06
PT
3214
3215
c072df1a 3216bool
6de9cd9a
DN
3217gfc_return_by_reference (gfc_symbol * sym)
3218{
3219 if (!sym->attr.function)
3220 return 0;
3221
b49a3de7 3222 if (sym->attr.dimension)
6de9cd9a
DN
3223 return 1;
3224
665733c1
JJ
3225 if (sym->ts.type == BT_CHARACTER
3226 && !sym->attr.is_bind_c
3227 && (!sym->attr.result
3228 || !sym->ns->proc_name
3229 || !sym->ns->proc_name->attr.is_bind_c))
6de9cd9a
DN
3230 return 1;
3231
973ff4c0
TS
3232 /* Possibly return complex numbers by reference for g77 compatibility.
3233 We don't do this for calls to intrinsics (as the library uses the
bf98b735
HA
3234 -fno-f2c calling convention) except for calls to specific wrappers
3235 (_gfortran_f2c_specific_*), nor for calls to functions which always
973ff4c0
TS
3236 require an explicit interface, as no compatibility problems can
3237 arise there. */
c61819ff 3238 if (flag_f2c && sym->ts.type == BT_COMPLEX
2273fd5a
HA
3239 && !sym->attr.pointer
3240 && !sym->attr.allocatable
bf98b735 3241 && !sym->attr.always_explicit)
973ff4c0 3242 return 1;
66e4ab31 3243
6de9cd9a
DN
3244 return 0;
3245}
3246\f
d198b59a
JJ
3247static tree
3248gfc_get_mixed_entry_union (gfc_namespace *ns)
3249{
3250 tree type;
dfd6ece2 3251 tree *chain = NULL;
d198b59a
JJ
3252 char name[GFC_MAX_SYMBOL_LEN + 1];
3253 gfc_entry_list *el, *el2;
3254
3255 gcc_assert (ns->proc_name->attr.mixed_entry_master);
3256 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
3257
3258 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
3259
3260 /* Build the type node. */
3261 type = make_node (UNION_TYPE);
3262
3263 TYPE_NAME (type) = get_identifier (name);
d198b59a
JJ
3264
3265 for (el = ns->entries; el; el = el->next)
3266 {
3267 /* Search for duplicates. */
3268 for (el2 = ns->entries; el2 != el; el2 = el2->next)
3269 if (el2->sym->result == el->sym->result)
3270 break;
3271
3272 if (el == el2)
35151cd5 3273 gfc_add_field_to_struct_1 (type,
dfd6ece2
NF
3274 get_identifier (el->sym->result->name),
3275 gfc_sym_type (el->sym->result), &chain);
d198b59a
JJ
3276 }
3277
3278 /* Finish off the type. */
d198b59a 3279 gfc_finish_type (type);
dfcf0b12 3280 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
d198b59a
JJ
3281 return type;
3282}
3283\f
6dfd24f7
TB
3284/* Create a "fn spec" based on the formal arguments;
3285 cf. create_function_arglist. */
3286
3287static tree
3288create_fn_spec (gfc_symbol *sym, tree fntype)
3289{
3290 char spec[150];
3291 size_t spec_len;
3292 gfc_formal_arglist *f;
3293 tree tmp;
3294
3295 memset (&spec, 0, sizeof (spec));
3296 spec[0] = '.';
762cca00
JH
3297 spec[1] = ' ';
3298 spec_len = 2;
6dfd24f7
TB
3299
3300 if (sym->attr.entry_master)
762cca00
JH
3301 {
3302 spec[spec_len++] = 'R';
3303 spec[spec_len++] = ' ';
3304 }
6dfd24f7
TB
3305 if (gfc_return_by_reference (sym))
3306 {
3307 gfc_symbol *result = sym->result ? sym->result : sym;
3308
3309 if (result->attr.pointer || sym->attr.proc_pointer)
762cca00
JH
3310 {
3311 spec[spec_len++] = '.';
3312 spec[spec_len++] = ' ';
3313 }
6dfd24f7 3314 else
762cca00
JH
3315 {
3316 spec[spec_len++] = 'w';
3317 spec[spec_len++] = ' ';
3318 }
6dfd24f7 3319 if (sym->ts.type == BT_CHARACTER)
762cca00 3320 {
ff0eec94
TB
3321 if (!sym->ts.u.cl->length
3322 && (sym->attr.allocatable || sym->attr.pointer))
3323 spec[spec_len++] = 'w';
3324 else
3325 spec[spec_len++] = 'R';
762cca00
JH
3326 spec[spec_len++] = ' ';
3327 }
6dfd24f7
TB
3328 }
3329
4cbc9039 3330 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
6dfd24f7
TB
3331 if (spec_len < sizeof (spec))
3332 {
be60aa5b
JRFS
3333 bool is_class = false;
3334 bool is_pointer = false;
3335
3336 if (f->sym)
3337 {
3338 is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
3339 && f->sym->attr.class_ok;
3340 is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
3341 : f->sym->attr.pointer;
3342 }
3343
3344 if (f->sym == NULL || is_pointer || f->sym->attr.target
39752c6b
TB
3345 || f->sym->attr.external || f->sym->attr.cray_pointer
3346 || (f->sym->ts.type == BT_DERIVED
3347 && (f->sym->ts.u.derived->attr.proc_pointer_comp
3348 || f->sym->ts.u.derived->attr.pointer_comp))
be60aa5b 3349 || (is_class
39752c6b 3350 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
4ea60a39
TK
3351 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3352 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
762cca00
JH
3353 {
3354 spec[spec_len++] = '.';
3355 spec[spec_len++] = ' ';
3356 }
6dfd24f7 3357 else if (f->sym->attr.intent == INTENT_IN)
762cca00
JH
3358 {
3359 spec[spec_len++] = 'r';
3360 spec[spec_len++] = ' ';
3361 }
6dfd24f7 3362 else if (f->sym)
762cca00
JH
3363 {
3364 spec[spec_len++] = 'w';
3365 spec[spec_len++] = ' ';
3366 }
6dfd24f7
TB
3367 }
3368
3369 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3370 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3371 return build_type_attribute_variant (fntype, tmp);
3372}
3373
212f4988
TB
3374
3375/* NOTE: The returned function type must match the argument list created by
3376 create_function_arglist. */
3377
6de9cd9a 3378tree
36ec54aa
TK
3379gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3380 const char *fnspec)
6de9cd9a
DN
3381{
3382 tree type;
57ef133b 3383 vec<tree, va_gc> *typelist = NULL;
e805adaa 3384 vec<tree, va_gc> *hidden_typelist = NULL;
6de9cd9a
DN
3385 gfc_formal_arglist *f;
3386 gfc_symbol *arg;
57ef133b
BE
3387 int alternate_return = 0;
3388 bool is_varargs = true;
6de9cd9a 3389
ecf24057
FXC
3390 /* Make sure this symbol is a function, a subroutine or the main
3391 program. */
3392 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3393 || sym->attr.flavor == FL_PROGRAM);
6de9cd9a 3394
3434c119
MM
3395 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3396 so that they can be detected here and handled further down. */
3397 if (sym->backend_decl == NULL)
3398 sym->backend_decl = error_mark_node;
3399 else if (sym->backend_decl == error_mark_node)
57ef133b 3400 goto arg_type_list_done;
3434c119
MM
3401 else if (sym->attr.proc_pointer)
3402 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3403 else
3404 return TREE_TYPE (sym->backend_decl);
6de9cd9a 3405
3d79abbd 3406 if (sym->attr.entry_master)
6c32445b 3407 /* Additional parameter for selecting an entry point. */
9771b263 3408 vec_safe_push (typelist, gfc_array_index_type);
3d79abbd 3409
bfd61955
TB
3410 if (sym->result)
3411 arg = sym->result;
3412 else
3413 arg = sym;
3414
3415 if (arg->ts.type == BT_CHARACTER)
bc21d315 3416 gfc_conv_const_charlen (arg->ts.u.cl);
bfd61955 3417
6de9cd9a
DN
3418 /* Some functions we use an extra parameter for the return value. */
3419 if (gfc_return_by_reference (sym))
3420 {
6de9cd9a 3421 type = gfc_sym_type (arg);
973ff4c0 3422 if (arg->ts.type == BT_COMPLEX
6de9cd9a
DN
3423 || arg->attr.dimension
3424 || arg->ts.type == BT_CHARACTER)
3425 type = build_reference_type (type);
3426
9771b263 3427 vec_safe_push (typelist, type);
6de9cd9a 3428 if (arg->ts.type == BT_CHARACTER)
8d51f26f
PT
3429 {
3430 if (!arg->ts.deferred)
3431 /* Transfer by value. */
9771b263 3432 vec_safe_push (typelist, gfc_charlen_type_node);
8d51f26f
PT
3433 else
3434 /* Deferred character lengths are transferred by reference
3435 so that the value can be returned. */
9771b263 3436 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
8d51f26f 3437 }
6de9cd9a 3438 }
378f53c7
TK
3439 if (sym->backend_decl == error_mark_node && actual_args != NULL
3440 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3441 || sym->attr.proc == PROC_UNKNOWN))
e68a35ae 3442 gfc_get_formal_from_actual_arglist (sym, actual_args);
6de9cd9a 3443
436529ea 3444 /* Build the argument types for the function. */
4cbc9039 3445 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
6de9cd9a
DN
3446 {
3447 arg = f->sym;
3448 if (arg)
3449 {
3450 /* Evaluate constant character lengths here so that they can be
3451 included in the type. */
3452 if (arg->ts.type == BT_CHARACTER)
bc21d315 3453 gfc_conv_const_charlen (arg->ts.u.cl);
6de9cd9a
DN
3454
3455 if (arg->attr.flavor == FL_PROCEDURE)
3456 {
3457 type = gfc_get_function_type (arg);
3458 type = build_pointer_type (type);
3459 }
3460 else
64f96237 3461 type = gfc_sym_type (arg, sym->attr.is_bind_c);
6de9cd9a
DN
3462
3463 /* Parameter Passing Convention
3464
3465 We currently pass all parameters by reference.
3466 Parameters with INTENT(IN) could be passed by value.
3467 The problem arises if a function is called via an implicit
3468 prototype. In this situation the INTENT is not known.
3469 For this reason all parameters to global functions must be
aa9c57ec 3470 passed by reference. Passing by value would potentially
6de9cd9a 3471 generate bad code. Worse there would be no way of telling that
c3e8c6b8 3472 this code was bad, except that it would give incorrect results.
6de9cd9a
DN
3473
3474 Contained procedures could pass by value as these are never
e2ae1407 3475 used without an explicit interface, and cannot be passed as
c3e8c6b8 3476 actual parameters for a dummy procedure. */
8d51f26f 3477
9771b263 3478 vec_safe_push (typelist, type);
6de9cd9a
DN
3479 }
3480 else
3481 {
3482 if (sym->attr.subroutine)
3483 alternate_return = 1;
3484 }
3485 }
3486
212f4988 3487 /* Add hidden arguments. */
4cbc9039 3488 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
8d51f26f
PT
3489 {
3490 arg = f->sym;
212f4988 3491 /* Add hidden string length parameters. */
8d51f26f
PT
3492 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3493 {
3494 if (!arg->ts.deferred)
3495 /* Transfer by value. */
3496 type = gfc_charlen_type_node;
3497 else
3498 /* Deferred character lengths are transferred by reference
3499 so that the value can be returned. */
3500 type = build_pointer_type (gfc_charlen_type_node);
3501
e805adaa 3502 vec_safe_push (hidden_typelist, type);
8d51f26f 3503 }
8859dce9 3504 /* For scalar intrinsic types or derived types, VALUE passes the value,
add31061
JJ
3505 hence, the optional status cannot be transferred via a NULL pointer.
3506 Thus, we will use a hidden argument in that case. */
59a63247
HA
3507 if (arg
3508 && arg->attr.optional
3509 && arg->attr.value
3510 && !arg->attr.dimension
8859dce9 3511 && arg->ts.type != BT_CLASS)
add31061 3512 vec_safe_push (typelist, boolean_type_node);
212f4988
TB
3513 /* Coarrays which are descriptorless or assumed-shape pass with
3514 -fcoarray=lib the token and the offset as hidden arguments. */
d31f485d
TB
3515 if (arg
3516 && flag_coarray == GFC_FCOARRAY_LIB
3517 && ((arg->ts.type != BT_CLASS
3518 && arg->attr.codimension
3519 && !arg->attr.allocatable)
3520 || (arg->ts.type == BT_CLASS
3521 && CLASS_DATA (arg)->attr.codimension
3522 && !CLASS_DATA (arg)->attr.allocatable)))
212f4988 3523 {
e805adaa
HA
3524 vec_safe_push (hidden_typelist, pvoid_type_node); /* caf_token. */
3525 vec_safe_push (hidden_typelist, gfc_array_index_type); /* caf_offset. */
212f4988 3526 }
8d51f26f 3527 }
6de9cd9a 3528
e805adaa
HA
3529 /* Put hidden character length, caf_token, caf_offset at the end. */
3530 vec_safe_reserve (typelist, vec_safe_length (hidden_typelist));
3531 vec_safe_splice (typelist, hidden_typelist);
3532
9771b263 3533 if (!vec_safe_is_empty (typelist)
6c32445b
NF
3534 || sym->attr.is_main_program
3535 || sym->attr.if_source != IFSRC_UNKNOWN)
3536 is_varargs = false;
6de9cd9a 3537
57ef133b 3538 if (sym->backend_decl == error_mark_node)
3434c119
MM
3539 sym->backend_decl = NULL_TREE;
3540
57ef133b
BE
3541arg_type_list_done:
3542
6de9cd9a
DN
3543 if (alternate_return)
3544 type = integer_type_node;
3545 else if (!sym->attr.function || gfc_return_by_reference (sym))
3546 type = void_type_node;
d198b59a
JJ
3547 else if (sym->attr.mixed_entry_master)
3548 type = gfc_get_mixed_entry_union (sym->ns);
c61819ff 3549 else if (flag_f2c && sym->ts.type == BT_REAL
da4c6ed8 3550 && sym->ts.kind == gfc_default_real_kind
2273fd5a
HA
3551 && !sym->attr.pointer
3552 && !sym->attr.allocatable
da4c6ed8
TS
3553 && !sym->attr.always_explicit)
3554 {
f04986a9 3555 /* Special case: f2c calling conventions require that (scalar)
da4c6ed8
TS
3556 default REAL functions return the C type double instead. f2c
3557 compatibility is only an issue with functions that don't
3558 require an explicit interface, as only these could be
3559 implemented in Fortran 77. */
3560 sym->ts.kind = gfc_default_double_kind;
3561 type = gfc_typenode_for_spec (&sym->ts);
3562 sym->ts.kind = gfc_default_real_kind;
3563 }
726d8566
JW
3564 else if (sym->result && sym->result->attr.proc_pointer)
3565 /* Procedure pointer return values. */
3070bab4
JW
3566 {
3567 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3568 {
3569 /* Unset proc_pointer as gfc_get_function_type
3570 is called recursively. */
3571 sym->result->attr.proc_pointer = 0;
3572 type = build_pointer_type (gfc_get_function_type (sym->result));
3573 sym->result->attr.proc_pointer = 1;
3574 }
3575 else
3576 type = gfc_sym_type (sym->result);
3577 }
6de9cd9a
DN
3578 else
3579 type = gfc_sym_type (sym);
3580
57ef133b 3581 if (is_varargs)
4fe34cdc
JM
3582 /* This should be represented as an unprototyped type, not a type
3583 with (...) prototype. */
3584 type = build_function_type (type, NULL_TREE);
6c32445b
NF
3585 else
3586 type = build_function_type_vec (type, typelist);
36ec54aa
TK
3587
3588 /* If we were passed an fn spec, add it here, otherwise determine it from
3589 the formal arguments. */
3590 if (fnspec)
3591 {
3592 tree tmp;
3593 int spec_len = strlen (fnspec);
3594 tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3595 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3596 type = build_type_attribute_variant (type, tmp);
3597 }
3598 else
3599 type = create_fn_spec (sym, type);
6de9cd9a
DN
3600
3601 return type;
3602}
3603\f
e2cad04b 3604/* Language hooks for middle-end access to type nodes. */
6de9cd9a
DN
3605
3606/* Return an integer type with BITS bits of precision,
3607 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3608
3609tree
3610gfc_type_for_size (unsigned bits, int unsignedp)
3611{
e2cad04b
RH
3612 if (!unsignedp)
3613 {
3614 int i;
3615 for (i = 0; i <= MAX_INT_KINDS; ++i)
3616 {
3617 tree type = gfc_integer_types[i];
3618 if (type && bits == TYPE_PRECISION (type))
3619 return type;
3620 }
5218394a
PB
3621
3622 /* Handle TImode as a special case because it is used by some backends
df2fba9e 3623 (e.g. ARM) even though it is not available for normal use. */
5218394a
PB
3624#if HOST_BITS_PER_WIDE_INT >= 64
3625 if (bits == TYPE_PRECISION (intTI_type_node))
3626 return intTI_type_node;
3627#endif
82aa8722
JJ
3628
3629 if (bits <= TYPE_PRECISION (intQI_type_node))
3630 return intQI_type_node;
3631 if (bits <= TYPE_PRECISION (intHI_type_node))
3632 return intHI_type_node;
3633 if (bits <= TYPE_PRECISION (intSI_type_node))
3634 return intSI_type_node;
3635 if (bits <= TYPE_PRECISION (intDI_type_node))
3636 return intDI_type_node;
3637 if (bits <= TYPE_PRECISION (intTI_type_node))
3638 return intTI_type_node;
e2cad04b
RH
3639 }
3640 else
3641 {
82aa8722 3642 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
e2cad04b 3643 return unsigned_intQI_type_node;
82aa8722 3644 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
e2cad04b 3645 return unsigned_intHI_type_node;
82aa8722 3646 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
e2cad04b 3647 return unsigned_intSI_type_node;
82aa8722 3648 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
e2cad04b 3649 return unsigned_intDI_type_node;
82aa8722 3650 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
e2cad04b
RH
3651 return unsigned_intTI_type_node;
3652 }
6de9cd9a 3653
e2cad04b 3654 return NULL_TREE;
6de9cd9a
DN
3655}
3656
e2cad04b
RH
3657/* Return a data type that has machine mode MODE. If the mode is an
3658 integer, then UNSIGNEDP selects between signed and unsigned types. */
6de9cd9a
DN
3659
3660tree
ef4bddc2 3661gfc_type_for_mode (machine_mode mode, int unsignedp)
6de9cd9a 3662{
e2cad04b
RH
3663 int i;
3664 tree *base;
b0567726 3665 scalar_int_mode int_mode;
e2cad04b
RH
3666
3667 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3668 base = gfc_real_types;
3669 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3670 base = gfc_complex_types;
b0567726 3671 else if (is_a <scalar_int_mode> (mode, &int_mode))
82aa8722 3672 {
b0567726 3673 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
82aa8722
JJ
3674 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3675 }
5c0caeb3
RS
3676 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3677 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3678 {
f4a2ae23 3679 unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
5c0caeb3
RS
3680 GET_MODE_NUNITS (mode));
3681 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3682 return build_vector_type_for_mode (bool_type, mode);
3683 }
928686b1
RS
3684 else if (VECTOR_MODE_P (mode)
3685 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
6de9cd9a 3686 {
ef4bddc2 3687 machine_mode inner_mode = GET_MODE_INNER (mode);
f676971a
EC
3688 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3689 if (inner_type != NULL_TREE)
3690 return build_vector_type_for_mode (inner_type, mode);
e2cad04b 3691 return NULL_TREE;
6de9cd9a 3692 }
e2cad04b 3693 else
1a5ffec4 3694 return NULL_TREE;
6de9cd9a 3695
e2cad04b
RH
3696 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3697 {
3698 tree type = base[i];
3699 if (type && mode == TYPE_MODE (type))
3700 return type;
3701 }
3702
3703 return NULL_TREE;
3704}
3705
fad0afd7
JJ
3706/* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3707 in that case. */
3708
3709bool
3710gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3711{
3712 int rank, dim;
3713 bool indirect = false;
ff3598bc 3714 tree etype, ptype, t, base_decl;
2297a38e 3715 tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
fad0afd7 3716 tree lower_suboff, upper_suboff, stride_suboff;
7fb43006 3717 tree dtype, field, rank_off;
fad0afd7
JJ
3718
3719 if (! GFC_DESCRIPTOR_TYPE_P (type))
3720 {
3721 if (! POINTER_TYPE_P (type))
3722 return false;
3723 type = TREE_TYPE (type);
3724 if (! GFC_DESCRIPTOR_TYPE_P (type))
3725 return false;
3726 indirect = true;
3727 }
3728
3729 rank = GFC_TYPE_ARRAY_RANK (type);
ca32b29e 3730 if (rank >= (int) (ARRAY_SIZE (info->dimen)))
fad0afd7
JJ
3731 return false;
3732
3733 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3734 gcc_assert (POINTER_TYPE_P (etype));
3735 etype = TREE_TYPE (etype);
badd9e69
TB
3736
3737 /* If the type is not a scalar coarray. */
3738 if (TREE_CODE (etype) == ARRAY_TYPE)
3739 etype = TREE_TYPE (etype);
3740
fad0afd7
JJ
3741 /* Can't handle variable sized elements yet. */
3742 if (int_size_in_bytes (etype) <= 0)
3743 return false;
3744 /* Nor non-constant lower bounds in assumed shape arrays. */
fe4e525c
TB
3745 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3746 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
fad0afd7
JJ
3747 {
3748 for (dim = 0; dim < rank; dim++)
3749 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3750 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3751 return false;
3752 }
3753
3754 memset (info, '\0', sizeof (*info));
3755 info->ndimensions = rank;
616743a8 3756 info->ordering = array_descr_ordering_column_major;
fad0afd7
JJ
3757 info->element_type = etype;
3758 ptype = build_pointer_type (gfc_array_index_type);
d560566a
AO
3759 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3760 if (!base_decl)
fad0afd7 3761 {
847f587d
MJ
3762 base_decl = build_debug_expr_decl (indirect
3763 ? build_pointer_type (ptype) : ptype);
d560566a 3764 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
fad0afd7 3765 }
d560566a
AO
3766 info->base_decl = base_decl;
3767 if (indirect)
3768 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
fad0afd7 3769
2297a38e
JJ
3770 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3771 &dim_off, &dim_size, &stride_suboff,
ff3598bc 3772 &lower_suboff, &upper_suboff);
fad0afd7 3773
2297a38e
JJ
3774 t = fold_build_pointer_plus (base_decl, span_off);
3775 elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3776
fad0afd7
JJ
3777 t = base_decl;
3778 if (!integer_zerop (data_off))
5d49b6a7 3779 t = fold_build_pointer_plus (t, data_off);
fad0afd7
JJ
3780 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3781 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
8293b9e4
TB
3782 enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
3783 if (akind == GFC_ARRAY_ALLOCATABLE
3784 || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE)
63ee5404 3785 info->allocated = build2 (NE_EXPR, logical_type_node,
fad0afd7 3786 info->data_location, null_pointer_node);
8293b9e4
TB
3787 else if (akind == GFC_ARRAY_POINTER
3788 || akind == GFC_ARRAY_POINTER_CONT
3789 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
3790 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
63ee5404 3791 info->associated = build2 (NE_EXPR, logical_type_node,
fad0afd7 3792 info->data_location, null_pointer_node);
8293b9e4
TB
3793 if ((akind == GFC_ARRAY_ASSUMED_RANK
3794 || akind == GFC_ARRAY_ASSUMED_RANK_CONT
3795 || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
3796 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
3797 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
5cea3ad6
JJ
3798 && dwarf_version >= 5)
3799 {
3800 rank = 1;
3801 info->ndimensions = 1;
90fe402a 3802 t = fold_build_pointer_plus (base_decl, dtype_off);
7fb43006
PT
3803 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3804 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3805 rank_off = byte_position (field);
90fe402a 3806 t = fold_build_pointer_plus (t, rank_off);
7fb43006 3807
da17c304
JJ
3808 t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
3809 t = build1 (INDIRECT_REF, TREE_TYPE (field), t);
7fb43006 3810 info->rank = t;
5cea3ad6
JJ
3811 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3812 t = size_binop (MULT_EXPR, t, dim_size);
3813 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3814 }
fad0afd7
JJ
3815
3816 for (dim = 0; dim < rank; dim++)
3817 {
5d49b6a7
RG
3818 t = fold_build_pointer_plus (base_decl,
3819 size_binop (PLUS_EXPR,
3820 dim_off, lower_suboff));
fad0afd7
JJ
3821 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3822 info->dimen[dim].lower_bound = t;
5d49b6a7
RG
3823 t = fold_build_pointer_plus (base_decl,
3824 size_binop (PLUS_EXPR,
3825 dim_off, upper_suboff));
fad0afd7
JJ
3826 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3827 info->dimen[dim].upper_bound = t;
8293b9e4
TB
3828 if (akind == GFC_ARRAY_ASSUMED_SHAPE
3829 || akind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
fad0afd7
JJ
3830 {
3831 /* Assumed shape arrays have known lower bounds. */
3832 info->dimen[dim].upper_bound
3833 = build2 (MINUS_EXPR, gfc_array_index_type,
3834 info->dimen[dim].upper_bound,
3835 info->dimen[dim].lower_bound);
3836 info->dimen[dim].lower_bound
3837 = fold_convert (gfc_array_index_type,
3838 GFC_TYPE_ARRAY_LBOUND (type, dim));
3839 info->dimen[dim].upper_bound
3840 = build2 (PLUS_EXPR, gfc_array_index_type,
3841 info->dimen[dim].lower_bound,
3842 info->dimen[dim].upper_bound);
3843 }
5d49b6a7
RG
3844 t = fold_build_pointer_plus (base_decl,
3845 size_binop (PLUS_EXPR,
3846 dim_off, stride_suboff));
fad0afd7
JJ
3847 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3848 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3849 info->dimen[dim].stride = t;
5cea3ad6
JJ
3850 if (dim + 1 < rank)
3851 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
fad0afd7
JJ
3852 }
3853
3854 return true;
3855}
3856
b5116268
TB
3857
3858/* Create a type to handle vector subscripts for coarray library calls. It
3859 has the form:
3860 struct caf_vector_t {
3861 size_t nvec; // size of the vector
3862 union {
3863 struct {
3864 void *vector;
3865 int kind;
3866 } v;
3867 struct {
3868 ptrdiff_t lower_bound;
3869 ptrdiff_t upper_bound;
3870 ptrdiff_t stride;
3871 } triplet;
3872 } u;
3873 }
3874 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3875 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3876
3877tree
3878gfc_get_caf_vector_type (int dim)
3879{
3880 static tree vector_types[GFC_MAX_DIMENSIONS];
3881 static tree vec_type = NULL_TREE;
3882 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3883
3884 if (vector_types[dim-1] != NULL_TREE)
3885 return vector_types[dim-1];
3886
3887 if (vec_type == NULL_TREE)
3888 {
3889 chain = 0;
3890 vect_struct_type = make_node (RECORD_TYPE);
3891 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3892 get_identifier ("vector"),
3893 pvoid_type_node, &chain);
d5e69948 3894 suppress_warning (tmp);
b5116268
TB
3895 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3896 get_identifier ("kind"),
3897 integer_type_node, &chain);
d5e69948 3898 suppress_warning (tmp);
b5116268
TB
3899 gfc_finish_type (vect_struct_type);
3900
3901 chain = 0;
3902 triplet_struct_type = make_node (RECORD_TYPE);
3903 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3904 get_identifier ("lower_bound"),
3905 gfc_array_index_type, &chain);
d5e69948 3906 suppress_warning (tmp);
b5116268
TB
3907 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3908 get_identifier ("upper_bound"),
3909 gfc_array_index_type, &chain);
d5e69948 3910 suppress_warning (tmp);
b5116268
TB
3911 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3912 gfc_array_index_type, &chain);
d5e69948 3913 suppress_warning (tmp);
b5116268
TB
3914 gfc_finish_type (triplet_struct_type);
3915
3916 chain = 0;
3917 union_type = make_node (UNION_TYPE);
3918 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3919 vect_struct_type, &chain);
d5e69948 3920 suppress_warning (tmp);
b5116268
TB
3921 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3922 triplet_struct_type, &chain);
d5e69948 3923 suppress_warning (tmp);
b5116268
TB
3924 gfc_finish_type (union_type);
3925
3926 chain = 0;
3927 vec_type = make_node (RECORD_TYPE);
3928 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3929 size_type_node, &chain);
d5e69948 3930 suppress_warning (tmp);
b5116268
TB
3931 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3932 union_type, &chain);
d5e69948 3933 suppress_warning (tmp);
b5116268
TB
3934 gfc_finish_type (vec_type);
3935 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3936 }
3937
3938 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3939 gfc_rank_cst[dim-1]);
3940 vector_types[dim-1] = build_array_type (vec_type, tmp);
3941 return vector_types[dim-1];
3942}
3943
3c9f5092
AV
3944
3945tree
3946gfc_get_caf_reference_type ()
3947{
3948 static tree reference_type = NULL_TREE;
3949 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3950 a_struct_type, u_union_type, tmp, *chain;
3951
3952 if (reference_type != NULL_TREE)
3953 return reference_type;
3954
3955 chain = 0;
3956 c_struct_type = make_node (RECORD_TYPE);
3957 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3958 get_identifier ("offset"),
3959 gfc_array_index_type, &chain);
d5e69948 3960 suppress_warning (tmp);
3c9f5092
AV
3961 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3962 get_identifier ("caf_token_offset"),
3963 gfc_array_index_type, &chain);
d5e69948 3964 suppress_warning (tmp);
3c9f5092
AV
3965 gfc_finish_type (c_struct_type);
3966
3967 chain = 0;
3968 s_struct_type = make_node (RECORD_TYPE);
3969 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3970 get_identifier ("start"),
3971 gfc_array_index_type, &chain);
d5e69948 3972 suppress_warning (tmp);
3c9f5092
AV
3973 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3974 get_identifier ("end"),
3975 gfc_array_index_type, &chain);
d5e69948 3976 suppress_warning (tmp);
3c9f5092
AV
3977 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3978 get_identifier ("stride"),
3979 gfc_array_index_type, &chain);
d5e69948 3980 suppress_warning (tmp);
3c9f5092
AV
3981 gfc_finish_type (s_struct_type);
3982
3983 chain = 0;
3984 v_struct_type = make_node (RECORD_TYPE);
3985 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3986 get_identifier ("vector"),
3987 pvoid_type_node, &chain);
d5e69948 3988 suppress_warning (tmp);
3c9f5092
AV
3989 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3990 get_identifier ("nvec"),
3991 size_type_node, &chain);
d5e69948 3992 suppress_warning (tmp);
3c9f5092
AV
3993 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3994 get_identifier ("kind"),
3995 integer_type_node, &chain);
d5e69948 3996 suppress_warning (tmp);
3c9f5092
AV
3997 gfc_finish_type (v_struct_type);
3998
3999 chain = 0;
4000 union_type = make_node (UNION_TYPE);
4001 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
4002 s_struct_type, &chain);
d5e69948 4003 suppress_warning (tmp);
3c9f5092
AV
4004 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
4005 v_struct_type, &chain);
d5e69948 4006 suppress_warning (tmp);
3c9f5092
AV
4007 gfc_finish_type (union_type);
4008
4009 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4010 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
4011 dim_union_type = build_array_type (union_type, tmp);
4012
4013 chain = 0;
4014 a_struct_type = make_node (RECORD_TYPE);
4015 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
4016 build_array_type (unsigned_char_type_node,
4017 build_range_type (gfc_array_index_type,
4018 gfc_index_zero_node,
4019 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
4020 &chain);
d5e69948 4021 suppress_warning (tmp);
3c9f5092
AV
4022 tmp = gfc_add_field_to_struct_1 (a_struct_type,
4023 get_identifier ("static_array_type"),
4024 integer_type_node, &chain);
d5e69948 4025 suppress_warning (tmp);
3c9f5092
AV
4026 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
4027 dim_union_type, &chain);
d5e69948 4028 suppress_warning (tmp);
3c9f5092
AV
4029 gfc_finish_type (a_struct_type);
4030
4031 chain = 0;
4032 u_union_type = make_node (UNION_TYPE);
4033 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
4034 c_struct_type, &chain);
d5e69948 4035 suppress_warning (tmp);
3c9f5092
AV
4036 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
4037 a_struct_type, &chain);
d5e69948 4038 suppress_warning (tmp);
3c9f5092
AV
4039 gfc_finish_type (u_union_type);
4040
4041 chain = 0;
4042 reference_type = make_node (RECORD_TYPE);
4043 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
4044 build_pointer_type (reference_type), &chain);
d5e69948 4045 suppress_warning (tmp);
3c9f5092
AV
4046 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
4047 integer_type_node, &chain);
d5e69948 4048 suppress_warning (tmp);
3c9f5092
AV
4049 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
4050 size_type_node, &chain);
d5e69948 4051 suppress_warning (tmp);
3c9f5092
AV
4052 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
4053 u_union_type, &chain);
d5e69948 4054 suppress_warning (tmp);
3c9f5092
AV
4055 gfc_finish_type (reference_type);
4056 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
4057
4058 return reference_type;
4059}
4060
64f96237
TB
4061static tree
4062gfc_get_cfi_dim_type ()
4063{
4064 static tree CFI_dim_t = NULL;
4065
4066 if (CFI_dim_t)
4067 return CFI_dim_t;
4068
4069 CFI_dim_t = make_node (RECORD_TYPE);
4070 TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
4071 TYPE_NAMELESS (CFI_dim_t) = 1;
4072 tree field;
4073 tree *chain = NULL;
4074 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
4075 gfc_array_index_type, &chain);
4076 suppress_warning (field);
4077 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
4078 gfc_array_index_type, &chain);
4079 suppress_warning (field);
4080 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
4081 gfc_array_index_type, &chain);
4082 suppress_warning (field);
4083 gfc_finish_type (CFI_dim_t);
4084 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
4085 return CFI_dim_t;
4086}
4087
4088
4089/* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
4090 otherwise dim[dimen] is used. */
4091
4092tree
4093gfc_get_cfi_type (int dimen, bool restricted)
4094{
4095 gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
4096
4097 int idx = 2*(dimen + 1) + restricted;
4098
4099 if (gfc_cfi_descriptor_base[idx])
4100 return gfc_cfi_descriptor_base[idx];
4101
4102 /* Build the type node. */
4103 tree CFI_cdesc_t = make_node (RECORD_TYPE);
4104 char name[GFC_MAX_SYMBOL_LEN + 1];
4105 if (dimen != -1)
4106 sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
4107 TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
4108 TYPE_NAMELESS (CFI_cdesc_t) = 1;
4109
4110 tree field;
4111 tree *chain = NULL;
4112 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
4113 (restricted ? prvoid_type_node
4114 : ptr_type_node), &chain);
4115 suppress_warning (field);
4116 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
4117 size_type_node, &chain);
4118 suppress_warning (field);
4119 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
4120 integer_type_node, &chain);
4121 suppress_warning (field);
4122 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
4123 signed_char_type_node, &chain);
4124 suppress_warning (field);
4125 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
4126 signed_char_type_node, &chain);
4127 suppress_warning (field);
4128 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
4129 get_typenode_from_name (INT16_TYPE),
4130 &chain);
4131 suppress_warning (field);
4132
4133 if (dimen != 0)
4134 {
4135 tree range = NULL_TREE;
4136 if (dimen > 0)
4137 range = gfc_rank_cst[dimen - 1];
4138 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4139 range);
4140 tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
4141 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
4142 CFI_dim_t, &chain);
4143 suppress_warning (field);
4144 }
4145
4146 TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
4147 gfc_finish_type (CFI_cdesc_t);
4148 gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
4149 return CFI_cdesc_t;
4150}
4151
6de9cd9a 4152#include "gt-fortran-trans-types.h"