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