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