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