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