Fortran: Emit correct types for CHARACTER(C_CHAR), VALUE arguments
[official-gcc.git] / gcc / fortran / trans-types.c
blob6262d52657fa571ea7f77ace6860ac333a1415a2
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* trans-types.c -- gfortran backend types */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "tree.h"
29 #include "gfortran.h"
30 #include "trans.h"
31 #include "stringpool.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "langhooks.h" /* For iso-c-bindings.def. */
35 #include "toplev.h" /* For rest_of_decl_compilation. */
36 #include "trans-types.h"
37 #include "trans-const.h"
38 #include "trans-array.h"
39 #include "dwarf2out.h" /* For struct array_descr_info. */
40 #include "attribs.h"
41 #include "alias.h"
44 #if (GFC_MAX_DIMENSIONS < 10)
45 #define GFC_RANK_DIGITS 1
46 #define GFC_RANK_PRINTF_FORMAT "%01d"
47 #elif (GFC_MAX_DIMENSIONS < 100)
48 #define GFC_RANK_DIGITS 2
49 #define GFC_RANK_PRINTF_FORMAT "%02d"
50 #else
51 #error If you really need >99 dimensions, continue the sequence above...
52 #endif
54 /* array of structs so we don't have to worry about xmalloc or free */
55 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
57 tree gfc_array_index_type;
58 tree gfc_array_range_type;
59 tree gfc_character1_type_node;
60 tree pvoid_type_node;
61 tree prvoid_type_node;
62 tree ppvoid_type_node;
63 tree pchar_type_node;
64 static tree pfunc_type_node;
66 tree logical_type_node;
67 tree logical_true_node;
68 tree logical_false_node;
69 tree gfc_charlen_type_node;
71 tree gfc_float128_type_node = NULL_TREE;
72 tree gfc_complex_float128_type_node = NULL_TREE;
74 bool gfc_real16_is_float128 = false;
76 static GTY(()) tree gfc_desc_dim_type;
77 static GTY(()) tree gfc_max_array_element_size;
78 static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
79 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
80 static GTY(()) tree gfc_cfi_descriptor_base[2 * (CFI_MAX_RANK + 2)];
82 /* Arrays for all integral and real kinds. We'll fill this in at runtime
83 after the target has a chance to process command-line options. */
85 #define MAX_INT_KINDS 5
86 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
87 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
88 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
89 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
91 #define MAX_REAL_KINDS 5
92 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
93 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
94 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
96 #define MAX_CHARACTER_KINDS 2
97 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
98 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
99 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
101 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
103 /* The integer kind to use for array indices. This will be set to the
104 proper value based on target information from the backend. */
106 int gfc_index_integer_kind;
108 /* The default kinds of the various types. */
110 int gfc_default_integer_kind;
111 int gfc_max_integer_kind;
112 int gfc_default_real_kind;
113 int gfc_default_double_kind;
114 int gfc_default_character_kind;
115 int gfc_default_logical_kind;
116 int gfc_default_complex_kind;
117 int gfc_c_int_kind;
118 int gfc_c_intptr_kind;
119 int gfc_atomic_int_kind;
120 int gfc_atomic_logical_kind;
122 /* The kind size used for record offsets. If the target system supports
123 kind=8, this will be set to 8, otherwise it is set to 4. */
124 int gfc_intio_kind;
126 /* The integer kind used to store character lengths. */
127 int gfc_charlen_int_kind;
129 /* Kind of internal integer for storing object sizes. */
130 int gfc_size_kind;
132 /* The size of the numeric storage unit and character storage unit. */
133 int gfc_numeric_storage_size;
134 int gfc_character_storage_size;
136 static tree dtype_type_node = NULL_TREE;
139 /* Build the dtype_type_node if necessary. */
140 tree get_dtype_type_node (void)
142 tree field;
143 tree dtype_node;
144 tree *dtype_chain = NULL;
146 if (dtype_type_node == NULL_TREE)
148 dtype_node = make_node (RECORD_TYPE);
149 TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
150 TYPE_NAMELESS (dtype_node) = 1;
151 field = gfc_add_field_to_struct_1 (dtype_node,
152 get_identifier ("elem_len"),
153 size_type_node, &dtype_chain);
154 suppress_warning (field);
155 field = gfc_add_field_to_struct_1 (dtype_node,
156 get_identifier ("version"),
157 integer_type_node, &dtype_chain);
158 suppress_warning (field);
159 field = gfc_add_field_to_struct_1 (dtype_node,
160 get_identifier ("rank"),
161 signed_char_type_node, &dtype_chain);
162 suppress_warning (field);
163 field = gfc_add_field_to_struct_1 (dtype_node,
164 get_identifier ("type"),
165 signed_char_type_node, &dtype_chain);
166 suppress_warning (field);
167 field = gfc_add_field_to_struct_1 (dtype_node,
168 get_identifier ("attribute"),
169 short_integer_type_node, &dtype_chain);
170 suppress_warning (field);
171 gfc_finish_type (dtype_node);
172 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
173 dtype_type_node = dtype_node;
175 return dtype_type_node;
178 static int
179 get_real_kind_from_node (tree type)
181 int i;
183 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
184 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
185 return gfc_real_kinds[i].kind;
187 return -4;
190 static int
191 get_int_kind_from_node (tree type)
193 int i;
195 if (!type)
196 return -2;
198 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
199 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
200 return gfc_integer_kinds[i].kind;
202 return -1;
205 static int
206 get_int_kind_from_name (const char *name)
208 return get_int_kind_from_node (get_typenode_from_name (name));
212 /* Get the kind number corresponding to an integer of given size,
213 following the required return values for ISO_FORTRAN_ENV INT* constants:
214 -2 is returned if we support a kind of larger size, -1 otherwise. */
216 gfc_get_int_kind_from_width_isofortranenv (int size)
218 int i;
220 /* Look for a kind with matching storage size. */
221 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
222 if (gfc_integer_kinds[i].bit_size == size)
223 return gfc_integer_kinds[i].kind;
225 /* Look for a kind with larger storage size. */
226 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
227 if (gfc_integer_kinds[i].bit_size > size)
228 return -2;
230 return -1;
234 /* Get the kind number corresponding to a real of a given storage size.
235 If two real's have the same storage size, then choose the real with
236 the largest precision. If a kind type is unavailable and a real
237 exists with wider storage, then return -2; otherwise, return -1. */
240 gfc_get_real_kind_from_width_isofortranenv (int size)
242 int digits, i, kind;
244 size /= 8;
246 kind = -1;
247 digits = 0;
249 /* Look for a kind with matching storage size. */
250 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
251 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
253 if (gfc_real_kinds[i].digits > digits)
255 digits = gfc_real_kinds[i].digits;
256 kind = gfc_real_kinds[i].kind;
260 if (kind != -1)
261 return kind;
263 /* Look for a kind with larger storage size. */
264 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
265 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
266 kind = -2;
268 return kind;
273 static int
274 get_int_kind_from_width (int size)
276 int i;
278 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
279 if (gfc_integer_kinds[i].bit_size == size)
280 return gfc_integer_kinds[i].kind;
282 return -2;
285 static int
286 get_int_kind_from_minimal_width (int size)
288 int i;
290 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
291 if (gfc_integer_kinds[i].bit_size >= size)
292 return gfc_integer_kinds[i].kind;
294 return -2;
298 /* Generate the CInteropKind_t objects for the C interoperable
299 kinds. */
301 void
302 gfc_init_c_interop_kinds (void)
304 int i;
306 /* init all pointers in the list to NULL */
307 for (i = 0; i < ISOCBINDING_NUMBER; i++)
309 /* Initialize the name and value fields. */
310 c_interop_kinds_table[i].name[0] = '\0';
311 c_interop_kinds_table[i].value = -100;
312 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
315 #define NAMED_INTCST(a,b,c,d) \
316 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
317 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
318 c_interop_kinds_table[a].value = c;
319 #define NAMED_REALCST(a,b,c,d) \
320 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
321 c_interop_kinds_table[a].f90_type = BT_REAL; \
322 c_interop_kinds_table[a].value = c;
323 #define NAMED_CMPXCST(a,b,c,d) \
324 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
325 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
326 c_interop_kinds_table[a].value = c;
327 #define NAMED_LOGCST(a,b,c) \
328 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
329 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
330 c_interop_kinds_table[a].value = c;
331 #define NAMED_CHARKNDCST(a,b,c) \
332 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
333 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
334 c_interop_kinds_table[a].value = c;
335 #define NAMED_CHARCST(a,b,c) \
336 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
337 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
338 c_interop_kinds_table[a].value = c;
339 #define DERIVED_TYPE(a,b,c) \
340 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
341 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
342 c_interop_kinds_table[a].value = c;
343 #define NAMED_FUNCTION(a,b,c,d) \
344 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
345 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
346 c_interop_kinds_table[a].value = c;
347 #define NAMED_SUBROUTINE(a,b,c,d) \
348 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
349 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
350 c_interop_kinds_table[a].value = c;
351 #include "iso-c-binding.def"
355 /* Query the target to determine which machine modes are available for
356 computation. Choose KIND numbers for them. */
358 void
359 gfc_init_kinds (void)
361 opt_scalar_int_mode int_mode_iter;
362 opt_scalar_float_mode float_mode_iter;
363 int i_index, r_index, kind;
364 bool saw_i4 = false, saw_i8 = false;
365 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
367 i_index = 0;
368 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
370 scalar_int_mode mode = int_mode_iter.require ();
371 int kind, bitsize;
373 if (!targetm.scalar_mode_supported_p (mode))
374 continue;
376 /* The middle end doesn't support constants larger than 2*HWI.
377 Perhaps the target hook shouldn't have accepted these either,
378 but just to be safe... */
379 bitsize = GET_MODE_BITSIZE (mode);
380 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
381 continue;
383 gcc_assert (i_index != MAX_INT_KINDS);
385 /* Let the kind equal the bit size divided by 8. This insulates the
386 programmer from the underlying byte size. */
387 kind = bitsize / 8;
389 if (kind == 4)
390 saw_i4 = true;
391 if (kind == 8)
392 saw_i8 = true;
394 gfc_integer_kinds[i_index].kind = kind;
395 gfc_integer_kinds[i_index].radix = 2;
396 gfc_integer_kinds[i_index].digits = bitsize - 1;
397 gfc_integer_kinds[i_index].bit_size = bitsize;
399 gfc_logical_kinds[i_index].kind = kind;
400 gfc_logical_kinds[i_index].bit_size = bitsize;
402 i_index += 1;
405 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
406 used for large file access. */
408 if (saw_i8)
409 gfc_intio_kind = 8;
410 else
411 gfc_intio_kind = 4;
413 /* If we do not at least have kind = 4, everything is pointless. */
414 gcc_assert(saw_i4);
416 /* Set the maximum integer kind. Used with at least BOZ constants. */
417 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
419 r_index = 0;
420 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
422 scalar_float_mode mode = float_mode_iter.require ();
423 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
424 int kind;
426 if (fmt == NULL)
427 continue;
428 if (!targetm.scalar_mode_supported_p (mode))
429 continue;
431 /* Only let float, double, long double and TFmode go through.
432 Runtime support for others is not provided, so they would be
433 useless. */
434 if (!targetm.libgcc_floating_mode_supported_p (mode))
435 continue;
436 if (mode != TYPE_MODE (float_type_node)
437 && (mode != TYPE_MODE (double_type_node))
438 && (mode != TYPE_MODE (long_double_type_node))
439 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
440 && (mode != TFmode)
441 #endif
443 continue;
445 /* Let the kind equal the precision divided by 8, rounding up. Again,
446 this insulates the programmer from the underlying byte size.
448 Also, it effectively deals with IEEE extended formats. There, the
449 total size of the type may equal 16, but it's got 6 bytes of padding
450 and the increased size can get in the way of a real IEEE quad format
451 which may also be supported by the target.
453 We round up so as to handle IA-64 __floatreg (RFmode), which is an
454 82 bit type. Not to be confused with __float80 (XFmode), which is
455 an 80 bit type also supported by IA-64. So XFmode should come out
456 to be kind=10, and RFmode should come out to be kind=11. Egads.
458 TODO: The kind calculation has to be modified to support all
459 three 128-bit floating-point modes on PowerPC as IFmode, KFmode,
460 and TFmode since the following line would all map to kind=16.
461 However, currently only float, double, long double, and TFmode
462 reach this code.
465 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
467 if (kind == 4)
468 saw_r4 = true;
469 if (kind == 8)
470 saw_r8 = true;
471 if (kind == 10)
472 saw_r10 = true;
473 if (kind == 16)
474 saw_r16 = true;
476 /* Careful we don't stumble a weird internal mode. */
477 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
478 /* Or have too many modes for the allocated space. */
479 gcc_assert (r_index != MAX_REAL_KINDS);
481 gfc_real_kinds[r_index].kind = kind;
482 gfc_real_kinds[r_index].radix = fmt->b;
483 gfc_real_kinds[r_index].digits = fmt->p;
484 gfc_real_kinds[r_index].min_exponent = fmt->emin;
485 gfc_real_kinds[r_index].max_exponent = fmt->emax;
486 if (fmt->pnan < fmt->p)
487 /* This is an IBM extended double format (or the MIPS variant)
488 made up of two IEEE doubles. The value of the long double is
489 the sum of the values of the two parts. The most significant
490 part is required to be the value of the long double rounded
491 to the nearest double. If we use emax of 1024 then we can't
492 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
493 rounding will make the most significant part overflow. */
494 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
495 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
496 r_index += 1;
499 /* Choose the default integer kind. We choose 4 unless the user directs us
500 otherwise. Even if the user specified that the default integer kind is 8,
501 the numeric storage size is not 64 bits. In this case, a warning will be
502 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
504 gfc_numeric_storage_size = 4 * 8;
506 if (flag_default_integer)
508 if (!saw_i8)
509 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
510 "%<-fdefault-integer-8%> option");
512 gfc_default_integer_kind = 8;
515 else if (flag_integer4_kind == 8)
517 if (!saw_i8)
518 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
519 "%<-finteger-4-integer-8%> option");
521 gfc_default_integer_kind = 8;
523 else if (saw_i4)
525 gfc_default_integer_kind = 4;
527 else
529 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
530 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
533 /* Choose the default real kind. Again, we choose 4 when possible. */
534 if (flag_default_real_8)
536 if (!saw_r8)
537 gfc_fatal_error ("REAL(KIND=8) is not available for "
538 "%<-fdefault-real-8%> option");
540 gfc_default_real_kind = 8;
542 else if (flag_default_real_10)
544 if (!saw_r10)
545 gfc_fatal_error ("REAL(KIND=10) is not available for "
546 "%<-fdefault-real-10%> option");
548 gfc_default_real_kind = 10;
550 else if (flag_default_real_16)
552 if (!saw_r16)
553 gfc_fatal_error ("REAL(KIND=16) is not available for "
554 "%<-fdefault-real-16%> option");
556 gfc_default_real_kind = 16;
558 else if (flag_real4_kind == 8)
560 if (!saw_r8)
561 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
562 "option");
564 gfc_default_real_kind = 8;
566 else if (flag_real4_kind == 10)
568 if (!saw_r10)
569 gfc_fatal_error ("REAL(KIND=10) is not available for "
570 "%<-freal-4-real-10%> option");
572 gfc_default_real_kind = 10;
574 else if (flag_real4_kind == 16)
576 if (!saw_r16)
577 gfc_fatal_error ("REAL(KIND=16) is not available for "
578 "%<-freal-4-real-16%> option");
580 gfc_default_real_kind = 16;
582 else if (saw_r4)
583 gfc_default_real_kind = 4;
584 else
585 gfc_default_real_kind = gfc_real_kinds[0].kind;
587 /* Choose the default double kind. If -fdefault-real and -fdefault-double
588 are specified, we use kind=8, if it's available. If -fdefault-real is
589 specified without -fdefault-double, we use kind=16, if it's available.
590 Otherwise we do not change anything. */
591 if (flag_default_double && saw_r8)
592 gfc_default_double_kind = 8;
593 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
595 /* Use largest available kind. */
596 if (saw_r16)
597 gfc_default_double_kind = 16;
598 else if (saw_r10)
599 gfc_default_double_kind = 10;
600 else if (saw_r8)
601 gfc_default_double_kind = 8;
602 else
603 gfc_default_double_kind = gfc_default_real_kind;
605 else if (flag_real8_kind == 4)
607 if (!saw_r4)
608 gfc_fatal_error ("REAL(KIND=4) is not available for "
609 "%<-freal-8-real-4%> option");
611 gfc_default_double_kind = 4;
613 else if (flag_real8_kind == 10 )
615 if (!saw_r10)
616 gfc_fatal_error ("REAL(KIND=10) is not available for "
617 "%<-freal-8-real-10%> option");
619 gfc_default_double_kind = 10;
621 else if (flag_real8_kind == 16 )
623 if (!saw_r16)
624 gfc_fatal_error ("REAL(KIND=10) is not available for "
625 "%<-freal-8-real-16%> option");
627 gfc_default_double_kind = 16;
629 else if (saw_r4 && saw_r8)
630 gfc_default_double_kind = 8;
631 else
633 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
634 real ... occupies two contiguous numeric storage units.
636 Therefore we must be supplied a kind twice as large as we chose
637 for single precision. There are loopholes, in that double
638 precision must *occupy* two storage units, though it doesn't have
639 to *use* two storage units. Which means that you can make this
640 kind artificially wide by padding it. But at present there are
641 no GCC targets for which a two-word type does not exist, so we
642 just let gfc_validate_kind abort and tell us if something breaks. */
644 gfc_default_double_kind
645 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
648 /* The default logical kind is constrained to be the same as the
649 default integer kind. Similarly with complex and real. */
650 gfc_default_logical_kind = gfc_default_integer_kind;
651 gfc_default_complex_kind = gfc_default_real_kind;
653 /* We only have two character kinds: ASCII and UCS-4.
654 ASCII corresponds to a 8-bit integer type, if one is available.
655 UCS-4 corresponds to a 32-bit integer type, if one is available. */
656 i_index = 0;
657 if ((kind = get_int_kind_from_width (8)) > 0)
659 gfc_character_kinds[i_index].kind = kind;
660 gfc_character_kinds[i_index].bit_size = 8;
661 gfc_character_kinds[i_index].name = "ascii";
662 i_index++;
664 if ((kind = get_int_kind_from_width (32)) > 0)
666 gfc_character_kinds[i_index].kind = kind;
667 gfc_character_kinds[i_index].bit_size = 32;
668 gfc_character_kinds[i_index].name = "iso_10646";
669 i_index++;
672 /* Choose the smallest integer kind for our default character. */
673 gfc_default_character_kind = gfc_character_kinds[0].kind;
674 gfc_character_storage_size = gfc_default_character_kind * 8;
676 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
678 /* Pick a kind the same size as the C "int" type. */
679 gfc_c_int_kind = INT_TYPE_SIZE / 8;
681 /* Choose atomic kinds to match C's int. */
682 gfc_atomic_int_kind = gfc_c_int_kind;
683 gfc_atomic_logical_kind = gfc_c_int_kind;
685 gfc_c_intptr_kind = POINTER_SIZE / 8;
689 /* Make sure that a valid kind is present. Returns an index into the
690 associated kinds array, -1 if the kind is not present. */
692 static int
693 validate_integer (int kind)
695 int i;
697 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
698 if (gfc_integer_kinds[i].kind == kind)
699 return i;
701 return -1;
704 static int
705 validate_real (int kind)
707 int i;
709 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
710 if (gfc_real_kinds[i].kind == kind)
711 return i;
713 return -1;
716 static int
717 validate_logical (int kind)
719 int i;
721 for (i = 0; gfc_logical_kinds[i].kind; i++)
722 if (gfc_logical_kinds[i].kind == kind)
723 return i;
725 return -1;
728 static int
729 validate_character (int kind)
731 int i;
733 for (i = 0; gfc_character_kinds[i].kind; i++)
734 if (gfc_character_kinds[i].kind == kind)
735 return i;
737 return -1;
740 /* Validate a kind given a basic type. The return value is the same
741 for the child functions, with -1 indicating nonexistence of the
742 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
745 gfc_validate_kind (bt type, int kind, bool may_fail)
747 int rc;
749 switch (type)
751 case BT_REAL: /* Fall through */
752 case BT_COMPLEX:
753 rc = validate_real (kind);
754 break;
755 case BT_INTEGER:
756 rc = validate_integer (kind);
757 break;
758 case BT_LOGICAL:
759 rc = validate_logical (kind);
760 break;
761 case BT_CHARACTER:
762 rc = validate_character (kind);
763 break;
765 default:
766 gfc_internal_error ("gfc_validate_kind(): Got bad type");
769 if (rc < 0 && !may_fail)
770 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
772 return rc;
776 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
777 Reuse common type nodes where possible. Recognize if the kind matches up
778 with a C type. This will be used later in determining which routines may
779 be scarfed from libm. */
781 static tree
782 gfc_build_int_type (gfc_integer_info *info)
784 int mode_precision = info->bit_size;
786 if (mode_precision == CHAR_TYPE_SIZE)
787 info->c_char = 1;
788 if (mode_precision == SHORT_TYPE_SIZE)
789 info->c_short = 1;
790 if (mode_precision == INT_TYPE_SIZE)
791 info->c_int = 1;
792 if (mode_precision == LONG_TYPE_SIZE)
793 info->c_long = 1;
794 if (mode_precision == LONG_LONG_TYPE_SIZE)
795 info->c_long_long = 1;
797 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
798 return intQI_type_node;
799 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
800 return intHI_type_node;
801 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
802 return intSI_type_node;
803 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
804 return intDI_type_node;
805 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
806 return intTI_type_node;
808 return make_signed_type (mode_precision);
811 tree
812 gfc_build_uint_type (int size)
814 if (size == CHAR_TYPE_SIZE)
815 return unsigned_char_type_node;
816 if (size == SHORT_TYPE_SIZE)
817 return short_unsigned_type_node;
818 if (size == INT_TYPE_SIZE)
819 return unsigned_type_node;
820 if (size == LONG_TYPE_SIZE)
821 return long_unsigned_type_node;
822 if (size == LONG_LONG_TYPE_SIZE)
823 return long_long_unsigned_type_node;
825 return make_unsigned_type (size);
829 static tree
830 gfc_build_real_type (gfc_real_info *info)
832 int mode_precision = info->mode_precision;
833 tree new_type;
835 if (mode_precision == FLOAT_TYPE_SIZE)
836 info->c_float = 1;
837 if (mode_precision == DOUBLE_TYPE_SIZE)
838 info->c_double = 1;
839 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
840 info->c_long_double = 1;
841 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
843 /* TODO: see PR101835. */
844 info->c_float128 = 1;
845 gfc_real16_is_float128 = true;
848 if (TYPE_PRECISION (float_type_node) == mode_precision)
849 return float_type_node;
850 if (TYPE_PRECISION (double_type_node) == mode_precision)
851 return double_type_node;
852 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
853 return long_double_type_node;
855 new_type = make_node (REAL_TYPE);
856 TYPE_PRECISION (new_type) = mode_precision;
857 layout_type (new_type);
858 return new_type;
861 static tree
862 gfc_build_complex_type (tree scalar_type)
864 tree new_type;
866 if (scalar_type == NULL)
867 return NULL;
868 if (scalar_type == float_type_node)
869 return complex_float_type_node;
870 if (scalar_type == double_type_node)
871 return complex_double_type_node;
872 if (scalar_type == long_double_type_node)
873 return complex_long_double_type_node;
875 new_type = make_node (COMPLEX_TYPE);
876 TREE_TYPE (new_type) = scalar_type;
877 layout_type (new_type);
878 return new_type;
881 static tree
882 gfc_build_logical_type (gfc_logical_info *info)
884 int bit_size = info->bit_size;
885 tree new_type;
887 if (bit_size == BOOL_TYPE_SIZE)
889 info->c_bool = 1;
890 return boolean_type_node;
893 new_type = make_unsigned_type (bit_size);
894 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
895 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
896 TYPE_PRECISION (new_type) = 1;
898 return new_type;
902 /* Create the backend type nodes. We map them to their
903 equivalent C type, at least for now. We also give
904 names to the types here, and we push them in the
905 global binding level context.*/
907 void
908 gfc_init_types (void)
910 char name_buf[26];
911 int index;
912 tree type;
913 unsigned n;
915 /* Create and name the types. */
916 #define PUSH_TYPE(name, node) \
917 pushdecl (build_decl (input_location, \
918 TYPE_DECL, get_identifier (name), node))
920 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
922 type = gfc_build_int_type (&gfc_integer_kinds[index]);
923 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
924 if (TYPE_STRING_FLAG (type))
925 type = make_signed_type (gfc_integer_kinds[index].bit_size);
926 gfc_integer_types[index] = type;
927 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
928 gfc_integer_kinds[index].kind);
929 PUSH_TYPE (name_buf, type);
932 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
934 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
935 gfc_logical_types[index] = type;
936 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
937 gfc_logical_kinds[index].kind);
938 PUSH_TYPE (name_buf, type);
941 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
943 type = gfc_build_real_type (&gfc_real_kinds[index]);
944 gfc_real_types[index] = type;
945 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
946 gfc_real_kinds[index].kind);
947 PUSH_TYPE (name_buf, type);
949 if (gfc_real_kinds[index].c_float128)
950 gfc_float128_type_node = type;
952 type = gfc_build_complex_type (type);
953 gfc_complex_types[index] = type;
954 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
955 gfc_real_kinds[index].kind);
956 PUSH_TYPE (name_buf, type);
958 if (gfc_real_kinds[index].c_float128)
959 gfc_complex_float128_type_node = type;
962 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
964 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
965 type = build_qualified_type (type, TYPE_UNQUALIFIED);
966 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
967 gfc_character_kinds[index].kind);
968 PUSH_TYPE (name_buf, type);
969 gfc_character_types[index] = type;
970 gfc_pcharacter_types[index] = build_pointer_type (type);
972 gfc_character1_type_node = gfc_character_types[0];
974 PUSH_TYPE ("byte", unsigned_char_type_node);
975 PUSH_TYPE ("void", void_type_node);
977 /* DBX debugging output gets upset if these aren't set. */
978 if (!TYPE_NAME (integer_type_node))
979 PUSH_TYPE ("c_integer", integer_type_node);
980 if (!TYPE_NAME (char_type_node))
981 PUSH_TYPE ("c_char", char_type_node);
983 #undef PUSH_TYPE
985 pvoid_type_node = build_pointer_type (void_type_node);
986 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
987 ppvoid_type_node = build_pointer_type (pvoid_type_node);
988 pchar_type_node = build_pointer_type (gfc_character1_type_node);
989 pfunc_type_node
990 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
992 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
993 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
994 since this function is called before gfc_init_constants. */
995 gfc_array_range_type
996 = build_range_type (gfc_array_index_type,
997 build_int_cst (gfc_array_index_type, 0),
998 NULL_TREE);
1000 /* The maximum array element size that can be handled is determined
1001 by the number of bits available to store this field in the array
1002 descriptor. */
1004 n = TYPE_PRECISION (size_type_node);
1005 gfc_max_array_element_size
1006 = wide_int_to_tree (size_type_node,
1007 wi::mask (n, UNSIGNED,
1008 TYPE_PRECISION (size_type_node)));
1010 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1011 logical_true_node = build_int_cst (logical_type_node, 1);
1012 logical_false_node = build_int_cst (logical_type_node, 0);
1014 /* Character lengths are of type size_t, except signed. */
1015 gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1016 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1018 /* Fortran kind number of size_type_node (size_t). This is used for
1019 the _size member in vtables. */
1020 gfc_size_kind = get_int_kind_from_node (size_type_node);
1023 /* Get the type node for the given type and kind. */
1025 tree
1026 gfc_get_int_type (int kind)
1028 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1029 return index < 0 ? 0 : gfc_integer_types[index];
1032 tree
1033 gfc_get_real_type (int kind)
1035 int index = gfc_validate_kind (BT_REAL, kind, true);
1036 return index < 0 ? 0 : gfc_real_types[index];
1039 tree
1040 gfc_get_complex_type (int kind)
1042 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1043 return index < 0 ? 0 : gfc_complex_types[index];
1046 tree
1047 gfc_get_logical_type (int kind)
1049 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1050 return index < 0 ? 0 : gfc_logical_types[index];
1053 tree
1054 gfc_get_char_type (int kind)
1056 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1057 return index < 0 ? 0 : gfc_character_types[index];
1060 tree
1061 gfc_get_pchar_type (int kind)
1063 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1064 return index < 0 ? 0 : gfc_pcharacter_types[index];
1068 /* Create a character type with the given kind and length. */
1070 tree
1071 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1073 tree bounds, type;
1075 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1076 type = build_array_type (eltype, bounds);
1077 TYPE_STRING_FLAG (type) = 1;
1079 return type;
1082 tree
1083 gfc_get_character_type_len (int kind, tree len)
1085 gfc_validate_kind (BT_CHARACTER, kind, false);
1086 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1090 /* Get a type node for a character kind. */
1092 tree
1093 gfc_get_character_type (int kind, gfc_charlen * cl)
1095 tree len;
1097 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1098 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1099 len = build_fold_indirect_ref (len);
1101 return gfc_get_character_type_len (kind, len);
1104 /* Convert a basic type. This will be an array for character types. */
1106 tree
1107 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1109 tree basetype;
1111 switch (spec->type)
1113 case BT_UNKNOWN:
1114 gcc_unreachable ();
1116 case BT_INTEGER:
1117 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1118 has been resolved. This is done so we can convert C_PTR and
1119 C_FUNPTR to simple variables that get translated to (void *). */
1120 if (spec->f90_type == BT_VOID)
1122 if (spec->u.derived
1123 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1124 basetype = ptr_type_node;
1125 else
1126 basetype = pfunc_type_node;
1128 else
1129 basetype = gfc_get_int_type (spec->kind);
1130 break;
1132 case BT_REAL:
1133 basetype = gfc_get_real_type (spec->kind);
1134 break;
1136 case BT_COMPLEX:
1137 basetype = gfc_get_complex_type (spec->kind);
1138 break;
1140 case BT_LOGICAL:
1141 basetype = gfc_get_logical_type (spec->kind);
1142 break;
1144 case BT_CHARACTER:
1145 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1146 break;
1148 case BT_HOLLERITH:
1149 /* Since this cannot be used, return a length one character. */
1150 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1151 gfc_index_one_node);
1152 break;
1154 case BT_UNION:
1155 basetype = gfc_get_union_type (spec->u.derived);
1156 break;
1158 case BT_DERIVED:
1159 case BT_CLASS:
1160 basetype = gfc_get_derived_type (spec->u.derived, codim);
1162 if (spec->type == BT_CLASS)
1163 GFC_CLASS_TYPE_P (basetype) = 1;
1165 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1166 type and kind to fit a (void *) and the basetype returned was a
1167 ptr_type_node. We need to pass up this new information to the
1168 symbol that was declared of type C_PTR or C_FUNPTR. */
1169 if (spec->u.derived->ts.f90_type == BT_VOID)
1171 spec->type = BT_INTEGER;
1172 spec->kind = gfc_index_integer_kind;
1173 spec->f90_type = BT_VOID;
1174 spec->is_c_interop = 1; /* Mark as escaping later. */
1176 break;
1177 case BT_VOID:
1178 case BT_ASSUMED:
1179 /* This is for the second arg to c_f_pointer and c_f_procpointer
1180 of the iso_c_binding module, to accept any ptr type. */
1181 basetype = ptr_type_node;
1182 if (spec->f90_type == BT_VOID)
1184 if (spec->u.derived
1185 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1186 basetype = ptr_type_node;
1187 else
1188 basetype = pfunc_type_node;
1190 break;
1191 case BT_PROCEDURE:
1192 basetype = pfunc_type_node;
1193 break;
1194 default:
1195 gcc_unreachable ();
1197 return basetype;
1200 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1202 static tree
1203 gfc_conv_array_bound (gfc_expr * expr)
1205 /* If expr is an integer constant, return that. */
1206 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1207 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1209 /* Otherwise return NULL. */
1210 return NULL_TREE;
1213 /* Return the type of an element of the array. Note that scalar coarrays
1214 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1215 (with POINTER_TYPE stripped) is returned. */
1217 tree
1218 gfc_get_element_type (tree type)
1220 tree element;
1222 if (GFC_ARRAY_TYPE_P (type))
1224 if (TREE_CODE (type) == POINTER_TYPE)
1225 type = TREE_TYPE (type);
1226 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1228 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1229 element = type;
1231 else
1233 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1234 element = TREE_TYPE (type);
1237 else
1239 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1240 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1242 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1243 element = TREE_TYPE (element);
1245 /* For arrays, which are not scalar coarrays. */
1246 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1247 element = TREE_TYPE (element);
1250 return element;
1253 /* Build an array. This function is called from gfc_sym_type().
1254 Actually returns array descriptor type.
1256 Format of array descriptors is as follows:
1258 struct gfc_array_descriptor
1260 array *data;
1261 index offset;
1262 struct dtype_type dtype;
1263 struct descriptor_dimension dimension[N_DIM];
1266 struct dtype_type
1268 size_t elem_len;
1269 int version;
1270 signed char rank;
1271 signed char type;
1272 signed short attribute;
1275 struct descriptor_dimension
1277 index stride;
1278 index lbound;
1279 index ubound;
1282 Translation code should use gfc_conv_descriptor_* rather than
1283 accessing the descriptor directly. Any changes to the array
1284 descriptor type will require changes in gfc_conv_descriptor_* and
1285 gfc_build_array_initializer.
1287 This is represented internally as a RECORD_TYPE. The index nodes
1288 are gfc_array_index_type and the data node is a pointer to the
1289 data. See below for the handling of character types.
1291 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1292 this generated poor code for assumed/deferred size arrays. These
1293 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1294 of the GENERIC grammar. Also, there is no way to explicitly set
1295 the array stride, so all data must be packed(1). I've tried to
1296 mark all the functions which would require modification with a GCC
1297 ARRAYS comment.
1299 The data component points to the first element in the array. The
1300 offset field is the position of the origin of the array (i.e. element
1301 (0, 0 ...)). This may be outside the bounds of the array.
1303 An element is accessed by
1304 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1305 This gives good performance as the computation does not involve the
1306 bounds of the array. For packed arrays, this is optimized further
1307 by substituting the known strides.
1309 This system has one problem: all array bounds must be within 2^31
1310 elements of the origin (2^63 on 64-bit machines). For example
1311 integer, dimension (80000:90000, 80000:90000, 2) :: array
1312 may not work properly on 32-bit machines because 80000*80000 >
1313 2^31, so the calculation for stride2 would overflow. This may
1314 still work, but I haven't checked, and it relies on the overflow
1315 doing the right thing.
1317 The way to fix this problem is to access elements as follows:
1318 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1319 Obviously this is much slower. I will make this a compile time
1320 option, something like -fsmall-array-offsets. Mixing code compiled
1321 with and without this switch will work.
1323 (1) This can be worked around by modifying the upper bound of the
1324 previous dimension. This requires extra fields in the descriptor
1325 (both real_ubound and fake_ubound). */
1328 /* Returns true if the array sym does not require a descriptor. */
1331 gfc_is_nodesc_array (gfc_symbol * sym)
1333 symbol_attribute *array_attr;
1334 gfc_array_spec *as;
1335 bool is_classarray = IS_CLASS_ARRAY (sym);
1337 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1338 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1340 gcc_assert (array_attr->dimension || array_attr->codimension);
1342 /* We only want local arrays. */
1343 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1344 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1345 || array_attr->allocatable)
1346 return 0;
1348 /* We want a descriptor for associate-name arrays that do not have an
1349 explicitly known shape already. */
1350 if (sym->assoc && as->type != AS_EXPLICIT)
1351 return 0;
1353 /* The dummy is stored in sym and not in the component. */
1354 if (sym->attr.dummy)
1355 return as->type != AS_ASSUMED_SHAPE
1356 && as->type != AS_ASSUMED_RANK;
1358 if (sym->attr.result || sym->attr.function)
1359 return 0;
1361 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1363 return 1;
1367 /* Create an array descriptor type. */
1369 static tree
1370 gfc_build_array_type (tree type, gfc_array_spec * as,
1371 enum gfc_array_kind akind, bool restricted,
1372 bool contiguous, int codim)
1374 tree lbound[GFC_MAX_DIMENSIONS];
1375 tree ubound[GFC_MAX_DIMENSIONS];
1376 int n, corank;
1378 /* Assumed-shape arrays do not have codimension information stored in the
1379 descriptor. */
1380 corank = MAX (as->corank, codim);
1381 if (as->type == AS_ASSUMED_SHAPE ||
1382 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1383 corank = codim;
1385 if (as->type == AS_ASSUMED_RANK)
1386 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1388 lbound[n] = NULL_TREE;
1389 ubound[n] = NULL_TREE;
1392 for (n = 0; n < as->rank; n++)
1394 /* Create expressions for the known bounds of the array. */
1395 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1396 lbound[n] = gfc_index_one_node;
1397 else
1398 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1399 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1402 for (n = as->rank; n < as->rank + corank; n++)
1404 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1405 lbound[n] = gfc_index_one_node;
1406 else
1407 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1409 if (n < as->rank + corank - 1)
1410 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1413 if (as->type == AS_ASSUMED_SHAPE)
1414 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1415 : GFC_ARRAY_ASSUMED_SHAPE;
1416 else if (as->type == AS_ASSUMED_RANK)
1417 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1418 : GFC_ARRAY_ASSUMED_RANK;
1419 return gfc_get_array_type_bounds (type, as->rank == -1
1420 ? GFC_MAX_DIMENSIONS : as->rank,
1421 corank, lbound, ubound, 0, akind,
1422 restricted);
1425 /* Returns the struct descriptor_dimension type. */
1427 static tree
1428 gfc_get_desc_dim_type (void)
1430 tree type;
1431 tree decl, *chain = NULL;
1433 if (gfc_desc_dim_type)
1434 return gfc_desc_dim_type;
1436 /* Build the type node. */
1437 type = make_node (RECORD_TYPE);
1439 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1440 TYPE_PACKED (type) = 1;
1442 /* Consists of the stride, lbound and ubound members. */
1443 decl = gfc_add_field_to_struct_1 (type,
1444 get_identifier ("stride"),
1445 gfc_array_index_type, &chain);
1446 suppress_warning (decl);
1448 decl = gfc_add_field_to_struct_1 (type,
1449 get_identifier ("lbound"),
1450 gfc_array_index_type, &chain);
1451 suppress_warning (decl);
1453 decl = gfc_add_field_to_struct_1 (type,
1454 get_identifier ("ubound"),
1455 gfc_array_index_type, &chain);
1456 suppress_warning (decl);
1458 /* Finish off the type. */
1459 gfc_finish_type (type);
1460 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1462 gfc_desc_dim_type = type;
1463 return type;
1467 /* Return the DTYPE for an array. This describes the type and type parameters
1468 of the array. */
1469 /* TODO: Only call this when the value is actually used, and make all the
1470 unknown cases abort. */
1472 tree
1473 gfc_get_dtype_rank_type (int rank, tree etype)
1475 tree ptype;
1476 tree size;
1477 int n;
1478 tree tmp;
1479 tree dtype;
1480 tree field;
1481 vec<constructor_elt, va_gc> *v = NULL;
1483 ptype = etype;
1484 while (TREE_CODE (etype) == POINTER_TYPE
1485 || TREE_CODE (etype) == ARRAY_TYPE)
1487 ptype = etype;
1488 etype = TREE_TYPE (etype);
1491 gcc_assert (etype);
1493 switch (TREE_CODE (etype))
1495 case INTEGER_TYPE:
1496 if (TREE_CODE (ptype) == ARRAY_TYPE
1497 && TYPE_STRING_FLAG (ptype))
1498 n = BT_CHARACTER;
1499 else
1500 n = BT_INTEGER;
1501 break;
1503 case BOOLEAN_TYPE:
1504 n = BT_LOGICAL;
1505 break;
1507 case REAL_TYPE:
1508 n = BT_REAL;
1509 break;
1511 case COMPLEX_TYPE:
1512 n = BT_COMPLEX;
1513 break;
1515 case RECORD_TYPE:
1516 if (GFC_CLASS_TYPE_P (etype))
1517 n = BT_CLASS;
1518 else
1519 n = BT_DERIVED;
1520 break;
1522 case FUNCTION_TYPE:
1523 case VOID_TYPE:
1524 n = BT_VOID;
1525 break;
1527 default:
1528 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1529 /* We can encounter strange array types for temporary arrays. */
1530 gcc_unreachable ();
1533 switch (n)
1535 case BT_CHARACTER:
1536 gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1537 size = gfc_get_character_len_in_bytes (ptype);
1538 break;
1539 case BT_VOID:
1540 gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1541 size = size_in_bytes (ptype);
1542 break;
1543 default:
1544 size = size_in_bytes (etype);
1545 break;
1548 gcc_assert (size);
1550 STRIP_NOPS (size);
1551 size = fold_convert (size_type_node, size);
1552 tmp = get_dtype_type_node ();
1553 field = gfc_advance_chain (TYPE_FIELDS (tmp),
1554 GFC_DTYPE_ELEM_LEN);
1555 CONSTRUCTOR_APPEND_ELT (v, field,
1556 fold_convert (TREE_TYPE (field), size));
1558 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1559 GFC_DTYPE_RANK);
1560 if (rank >= 0)
1561 CONSTRUCTOR_APPEND_ELT (v, field,
1562 build_int_cst (TREE_TYPE (field), rank));
1564 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1565 GFC_DTYPE_TYPE);
1566 CONSTRUCTOR_APPEND_ELT (v, field,
1567 build_int_cst (TREE_TYPE (field), n));
1569 dtype = build_constructor (tmp, v);
1571 return dtype;
1575 tree
1576 gfc_get_dtype (tree type, int * rank)
1578 tree dtype;
1579 tree etype;
1580 int irnk;
1582 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1584 irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
1585 etype = gfc_get_element_type (type);
1586 dtype = gfc_get_dtype_rank_type (irnk, etype);
1588 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1589 return dtype;
1593 /* Build an array type for use without a descriptor, packed according
1594 to the value of PACKED. */
1596 tree
1597 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1598 bool restricted)
1600 tree range;
1601 tree type;
1602 tree tmp;
1603 int n;
1604 int known_stride;
1605 int known_offset;
1606 mpz_t offset;
1607 mpz_t stride;
1608 mpz_t delta;
1609 gfc_expr *expr;
1611 mpz_init_set_ui (offset, 0);
1612 mpz_init_set_ui (stride, 1);
1613 mpz_init (delta);
1615 /* We don't use build_array_type because this does not include
1616 lang-specific information (i.e. the bounds of the array) when checking
1617 for duplicates. */
1618 if (as->rank)
1619 type = make_node (ARRAY_TYPE);
1620 else
1621 type = build_variant_type_copy (etype);
1623 GFC_ARRAY_TYPE_P (type) = 1;
1624 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1626 known_stride = (packed != PACKED_NO);
1627 known_offset = 1;
1628 for (n = 0; n < as->rank; n++)
1630 /* Fill in the stride and bound components of the type. */
1631 if (known_stride)
1632 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1633 else
1634 tmp = NULL_TREE;
1635 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1637 expr = as->lower[n];
1638 if (expr && expr->expr_type == EXPR_CONSTANT)
1640 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1641 gfc_index_integer_kind);
1643 else
1645 known_stride = 0;
1646 tmp = NULL_TREE;
1648 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1650 if (known_stride)
1652 /* Calculate the offset. */
1653 mpz_mul (delta, stride, as->lower[n]->value.integer);
1654 mpz_sub (offset, offset, delta);
1656 else
1657 known_offset = 0;
1659 expr = as->upper[n];
1660 if (expr && expr->expr_type == EXPR_CONSTANT)
1662 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1663 gfc_index_integer_kind);
1665 else
1667 tmp = NULL_TREE;
1668 known_stride = 0;
1670 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1672 if (known_stride)
1674 /* Calculate the stride. */
1675 mpz_sub (delta, as->upper[n]->value.integer,
1676 as->lower[n]->value.integer);
1677 mpz_add_ui (delta, delta, 1);
1678 mpz_mul (stride, stride, delta);
1681 /* Only the first stride is known for partial packed arrays. */
1682 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1683 known_stride = 0;
1685 for (n = as->rank; n < as->rank + as->corank; n++)
1687 expr = as->lower[n];
1688 if (expr && expr->expr_type == EXPR_CONSTANT)
1689 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1690 gfc_index_integer_kind);
1691 else
1692 tmp = NULL_TREE;
1693 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1695 expr = as->upper[n];
1696 if (expr && expr->expr_type == EXPR_CONSTANT)
1697 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1698 gfc_index_integer_kind);
1699 else
1700 tmp = NULL_TREE;
1701 if (n < as->rank + as->corank - 1)
1702 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1705 if (known_offset)
1707 GFC_TYPE_ARRAY_OFFSET (type) =
1708 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1710 else
1711 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1713 if (known_stride)
1715 GFC_TYPE_ARRAY_SIZE (type) =
1716 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1718 else
1719 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1721 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1722 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1723 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1724 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1725 NULL_TREE);
1726 /* TODO: use main type if it is unbounded. */
1727 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1728 build_pointer_type (build_array_type (etype, range));
1729 if (restricted)
1730 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1731 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1732 TYPE_QUAL_RESTRICT);
1734 if (as->rank == 0)
1736 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1738 type = build_pointer_type (type);
1740 if (restricted)
1741 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1743 GFC_ARRAY_TYPE_P (type) = 1;
1744 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1747 return type;
1750 if (known_stride)
1752 mpz_sub_ui (stride, stride, 1);
1753 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1755 else
1756 range = NULL_TREE;
1758 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1759 TYPE_DOMAIN (type) = range;
1761 build_pointer_type (etype);
1762 TREE_TYPE (type) = etype;
1764 layout_type (type);
1766 mpz_clear (offset);
1767 mpz_clear (stride);
1768 mpz_clear (delta);
1770 /* Represent packed arrays as multi-dimensional if they have rank >
1771 1 and with proper bounds, instead of flat arrays. This makes for
1772 better debug info. */
1773 if (known_offset)
1775 tree gtype = etype, rtype, type_decl;
1777 for (n = as->rank - 1; n >= 0; n--)
1779 rtype = build_range_type (gfc_array_index_type,
1780 GFC_TYPE_ARRAY_LBOUND (type, n),
1781 GFC_TYPE_ARRAY_UBOUND (type, n));
1782 gtype = build_array_type (gtype, rtype);
1784 TYPE_NAME (type) = type_decl = build_decl (input_location,
1785 TYPE_DECL, NULL, gtype);
1786 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1789 if (packed != PACKED_STATIC || !known_stride
1790 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1792 /* For dummy arrays and automatic (heap allocated) arrays we
1793 want a pointer to the array. */
1794 type = build_pointer_type (type);
1795 if (restricted)
1796 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1797 GFC_ARRAY_TYPE_P (type) = 1;
1798 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1800 return type;
1804 /* Return or create the base type for an array descriptor. */
1806 static tree
1807 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1809 tree fat_type, decl, arraytype, *chain = NULL;
1810 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1811 int idx;
1813 /* Assumed-rank array. */
1814 if (dimen == -1)
1815 dimen = GFC_MAX_DIMENSIONS;
1817 idx = 2 * (codimen + dimen) + restricted;
1819 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1821 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1823 if (gfc_array_descriptor_base_caf[idx])
1824 return gfc_array_descriptor_base_caf[idx];
1826 else if (gfc_array_descriptor_base[idx])
1827 return gfc_array_descriptor_base[idx];
1829 /* Build the type node. */
1830 fat_type = make_node (RECORD_TYPE);
1832 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1833 TYPE_NAME (fat_type) = get_identifier (name);
1834 TYPE_NAMELESS (fat_type) = 1;
1836 /* Add the data member as the first element of the descriptor. */
1837 gfc_add_field_to_struct_1 (fat_type,
1838 get_identifier ("data"),
1839 (restricted
1840 ? prvoid_type_node
1841 : ptr_type_node), &chain);
1843 /* Add the base component. */
1844 decl = gfc_add_field_to_struct_1 (fat_type,
1845 get_identifier ("offset"),
1846 gfc_array_index_type, &chain);
1847 suppress_warning (decl);
1849 /* Add the dtype component. */
1850 decl = gfc_add_field_to_struct_1 (fat_type,
1851 get_identifier ("dtype"),
1852 get_dtype_type_node (), &chain);
1853 suppress_warning (decl);
1855 /* Add the span component. */
1856 decl = gfc_add_field_to_struct_1 (fat_type,
1857 get_identifier ("span"),
1858 gfc_array_index_type, &chain);
1859 suppress_warning (decl);
1861 /* Build the array type for the stride and bound components. */
1862 if (dimen + codimen > 0)
1864 arraytype =
1865 build_array_type (gfc_get_desc_dim_type (),
1866 build_range_type (gfc_array_index_type,
1867 gfc_index_zero_node,
1868 gfc_rank_cst[codimen + dimen - 1]));
1870 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1871 arraytype, &chain);
1872 suppress_warning (decl);
1875 if (flag_coarray == GFC_FCOARRAY_LIB)
1877 decl = gfc_add_field_to_struct_1 (fat_type,
1878 get_identifier ("token"),
1879 prvoid_type_node, &chain);
1880 suppress_warning (decl);
1883 /* Finish off the type. */
1884 gfc_finish_type (fat_type);
1885 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1887 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1888 gfc_array_descriptor_base_caf[idx] = fat_type;
1889 else
1890 gfc_array_descriptor_base[idx] = fat_type;
1892 return fat_type;
1896 /* Build an array (descriptor) type with given bounds. */
1898 tree
1899 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1900 tree * ubound, int packed,
1901 enum gfc_array_kind akind, bool restricted)
1903 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1904 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1905 const char *type_name;
1906 int n;
1908 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1909 fat_type = build_distinct_type_copy (base_type);
1910 /* Unshare TYPE_FIELDs. */
1911 for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
1913 tree next = DECL_CHAIN (*tp);
1914 *tp = copy_node (*tp);
1915 DECL_CONTEXT (*tp) = fat_type;
1916 DECL_CHAIN (*tp) = next;
1918 /* Make sure that nontarget and target array type have the same canonical
1919 type (and same stub decl for debug info). */
1920 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1921 TYPE_CANONICAL (fat_type) = base_type;
1922 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1923 /* Arrays of unknown type must alias with all array descriptors. */
1924 TYPE_TYPELESS_STORAGE (base_type) = 1;
1925 TYPE_TYPELESS_STORAGE (fat_type) = 1;
1926 gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
1928 tmp = etype;
1929 if (TREE_CODE (tmp) == ARRAY_TYPE
1930 && TYPE_STRING_FLAG (tmp))
1931 tmp = TREE_TYPE (etype);
1932 tmp = TYPE_NAME (tmp);
1933 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1934 tmp = DECL_NAME (tmp);
1935 if (tmp)
1936 type_name = IDENTIFIER_POINTER (tmp);
1937 else
1938 type_name = "unknown";
1939 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1940 GFC_MAX_SYMBOL_LEN, type_name);
1941 TYPE_NAME (fat_type) = get_identifier (name);
1942 TYPE_NAMELESS (fat_type) = 1;
1944 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1945 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1947 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1948 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1949 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1950 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1952 /* Build an array descriptor record type. */
1953 if (packed != 0)
1954 stride = gfc_index_one_node;
1955 else
1956 stride = NULL_TREE;
1957 for (n = 0; n < dimen + codimen; n++)
1959 if (n < dimen)
1960 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1962 if (lbound)
1963 lower = lbound[n];
1964 else
1965 lower = NULL_TREE;
1967 if (lower != NULL_TREE)
1969 if (INTEGER_CST_P (lower))
1970 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1971 else
1972 lower = NULL_TREE;
1975 if (codimen && n == dimen + codimen - 1)
1976 break;
1978 upper = ubound[n];
1979 if (upper != NULL_TREE)
1981 if (INTEGER_CST_P (upper))
1982 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1983 else
1984 upper = NULL_TREE;
1987 if (n >= dimen)
1988 continue;
1990 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1992 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1993 gfc_array_index_type, upper, lower);
1994 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1995 gfc_array_index_type, tmp,
1996 gfc_index_one_node);
1997 stride = fold_build2_loc (input_location, MULT_EXPR,
1998 gfc_array_index_type, tmp, stride);
1999 /* Check the folding worked. */
2000 gcc_assert (INTEGER_CST_P (stride));
2002 else
2003 stride = NULL_TREE;
2005 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
2007 /* TODO: known offsets for descriptors. */
2008 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2010 if (dimen == 0)
2012 arraytype = build_pointer_type (etype);
2013 if (restricted)
2014 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2016 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2017 return fat_type;
2020 /* We define data as an array with the correct size if possible.
2021 Much better than doing pointer arithmetic. */
2022 if (stride)
2023 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2024 int_const_binop (MINUS_EXPR, stride,
2025 build_int_cst (TREE_TYPE (stride), 1)));
2026 else
2027 rtype = gfc_array_range_type;
2028 arraytype = build_array_type (etype, rtype);
2029 arraytype = build_pointer_type (arraytype);
2030 if (restricted)
2031 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2032 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2034 /* This will generate the base declarations we need to emit debug
2035 information for this type. FIXME: there must be a better way to
2036 avoid divergence between compilations with and without debug
2037 information. */
2039 struct array_descr_info info;
2040 gfc_get_array_descr_info (fat_type, &info);
2041 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2044 return fat_type;
2047 /* Build a pointer type. This function is called from gfc_sym_type(). */
2049 static tree
2050 gfc_build_pointer_type (gfc_symbol * sym, tree type)
2052 /* Array pointer types aren't actually pointers. */
2053 if (sym->attr.dimension)
2054 return type;
2055 else
2056 return build_pointer_type (type);
2059 static tree gfc_nonrestricted_type (tree t);
2060 /* Given two record or union type nodes TO and FROM, ensure
2061 that all fields in FROM have a corresponding field in TO,
2062 their type being nonrestrict variants. This accepts a TO
2063 node that already has a prefix of the fields in FROM. */
2064 static void
2065 mirror_fields (tree to, tree from)
2067 tree fto, ffrom;
2068 tree *chain;
2070 /* Forward to the end of TOs fields. */
2071 fto = TYPE_FIELDS (to);
2072 ffrom = TYPE_FIELDS (from);
2073 chain = &TYPE_FIELDS (to);
2074 while (fto)
2076 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2077 chain = &DECL_CHAIN (fto);
2078 fto = DECL_CHAIN (fto);
2079 ffrom = DECL_CHAIN (ffrom);
2082 /* Now add all fields remaining in FROM (starting with ffrom). */
2083 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2085 tree newfield = copy_node (ffrom);
2086 DECL_CONTEXT (newfield) = to;
2087 /* The store to DECL_CHAIN might seem redundant with the
2088 stores to *chain, but not clearing it here would mean
2089 leaving a chain into the old fields. If ever
2090 our called functions would look at them confusion
2091 will arise. */
2092 DECL_CHAIN (newfield) = NULL_TREE;
2093 *chain = newfield;
2094 chain = &DECL_CHAIN (newfield);
2096 if (TREE_CODE (ffrom) == FIELD_DECL)
2098 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2099 TREE_TYPE (newfield) = elemtype;
2102 *chain = NULL_TREE;
2105 /* Given a type T, returns a different type of the same structure,
2106 except that all types it refers to (recursively) are always
2107 non-restrict qualified types. */
2108 static tree
2109 gfc_nonrestricted_type (tree t)
2111 tree ret = t;
2113 /* If the type isn't laid out yet, don't copy it. If something
2114 needs it for real it should wait until the type got finished. */
2115 if (!TYPE_SIZE (t))
2116 return t;
2118 if (!TYPE_LANG_SPECIFIC (t))
2119 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2120 /* If we're dealing with this very node already further up
2121 the call chain (recursion via pointers and struct members)
2122 we haven't yet determined if we really need a new type node.
2123 Assume we don't, return T itself. */
2124 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2125 return t;
2127 /* If we have calculated this all already, just return it. */
2128 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2129 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2131 /* Mark this type. */
2132 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2134 switch (TREE_CODE (t))
2136 default:
2137 break;
2139 case POINTER_TYPE:
2140 case REFERENCE_TYPE:
2142 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2143 if (totype == TREE_TYPE (t))
2144 ret = t;
2145 else if (TREE_CODE (t) == POINTER_TYPE)
2146 ret = build_pointer_type (totype);
2147 else
2148 ret = build_reference_type (totype);
2149 ret = build_qualified_type (ret,
2150 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2152 break;
2154 case ARRAY_TYPE:
2156 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2157 if (elemtype == TREE_TYPE (t))
2158 ret = t;
2159 else
2161 ret = build_variant_type_copy (t);
2162 TREE_TYPE (ret) = elemtype;
2163 if (TYPE_LANG_SPECIFIC (t)
2164 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2166 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2167 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2168 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2170 TYPE_LANG_SPECIFIC (ret)
2171 = ggc_cleared_alloc<struct lang_type> ();
2172 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2173 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2178 break;
2180 case RECORD_TYPE:
2181 case UNION_TYPE:
2182 case QUAL_UNION_TYPE:
2184 tree field;
2185 /* First determine if we need a new type at all.
2186 Careful, the two calls to gfc_nonrestricted_type per field
2187 might return different values. That happens exactly when
2188 one of the fields reaches back to this very record type
2189 (via pointers). The first calls will assume that we don't
2190 need to copy T (see the error_mark_node marking). If there
2191 are any reasons for copying T apart from having to copy T,
2192 we'll indeed copy it, and the second calls to
2193 gfc_nonrestricted_type will use that new node if they
2194 reach back to T. */
2195 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2196 if (TREE_CODE (field) == FIELD_DECL)
2198 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2199 if (elemtype != TREE_TYPE (field))
2200 break;
2202 if (!field)
2203 break;
2204 ret = build_variant_type_copy (t);
2205 TYPE_FIELDS (ret) = NULL_TREE;
2207 /* Here we make sure that as soon as we know we have to copy
2208 T, that also fields reaching back to us will use the new
2209 copy. It's okay if that copy still contains the old fields,
2210 we won't look at them. */
2211 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2212 mirror_fields (ret, t);
2214 break;
2217 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2218 return ret;
2222 /* Return the type for a symbol. Special handling is required for character
2223 types to get the correct level of indirection.
2224 For functions return the return type.
2225 For subroutines return void_type_node.
2226 Calling this multiple times for the same symbol should be avoided,
2227 especially for character and array types. */
2229 tree
2230 gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
2232 tree type;
2233 int byref;
2234 bool restricted;
2236 /* Procedure Pointers inside COMMON blocks. */
2237 if (sym->attr.proc_pointer && sym->attr.in_common)
2239 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2240 sym->attr.proc_pointer = 0;
2241 type = build_pointer_type (gfc_get_function_type (sym));
2242 sym->attr.proc_pointer = 1;
2243 return type;
2246 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2247 return void_type_node;
2249 /* In the case of a function the fake result variable may have a
2250 type different from the function type, so don't return early in
2251 that case. */
2252 if (sym->backend_decl && !sym->attr.function)
2253 return TREE_TYPE (sym->backend_decl);
2255 if (sym->attr.result
2256 && sym->ts.type == BT_CHARACTER
2257 && sym->ts.u.cl->backend_decl == NULL_TREE
2258 && sym->ns->proc_name
2259 && sym->ns->proc_name->ts.u.cl
2260 && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2261 sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2263 if (sym->ts.type == BT_CHARACTER
2264 && ((sym->attr.function && sym->attr.is_bind_c)
2265 || ((sym->attr.result || sym->attr.value)
2266 && sym->ns->proc_name
2267 && sym->ns->proc_name->attr.is_bind_c)
2268 || (sym->ts.deferred && (!sym->ts.u.cl
2269 || !sym->ts.u.cl->backend_decl))))
2270 type = gfc_character1_type_node;
2271 else
2272 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2274 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
2275 && !sym->pass_as_value)
2276 byref = 1;
2277 else
2278 byref = 0;
2280 restricted = !sym->attr.target && !sym->attr.pointer
2281 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2282 if (!restricted)
2283 type = gfc_nonrestricted_type (type);
2285 /* Dummy argument to a bind(C) procedure. */
2286 if (is_bind_c && is_CFI_desc (sym, NULL))
2287 type = gfc_get_cfi_type (sym->attr.dimension ? sym->as->rank : 0,
2288 /* restricted = */ false);
2289 else if (sym->attr.dimension || sym->attr.codimension)
2291 if (gfc_is_nodesc_array (sym))
2293 /* If this is a character argument of unknown length, just use the
2294 base type. */
2295 if (sym->ts.type != BT_CHARACTER
2296 || !(sym->attr.dummy || sym->attr.function)
2297 || sym->ts.u.cl->backend_decl)
2299 type = gfc_get_nodesc_array_type (type, sym->as,
2300 byref ? PACKED_FULL
2301 : PACKED_STATIC,
2302 restricted);
2303 byref = 0;
2306 else
2308 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2309 if (sym->attr.pointer)
2310 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2311 : GFC_ARRAY_POINTER;
2312 else if (sym->attr.allocatable)
2313 akind = GFC_ARRAY_ALLOCATABLE;
2314 type = gfc_build_array_type (type, sym->as, akind, restricted,
2315 sym->attr.contiguous, false);
2318 else
2320 if (sym->attr.allocatable || sym->attr.pointer
2321 || gfc_is_associate_pointer (sym))
2322 type = gfc_build_pointer_type (sym, type);
2325 /* We currently pass all parameters by reference.
2326 See f95_get_function_decl. For dummy function parameters return the
2327 function type. */
2328 if (byref)
2330 /* We must use pointer types for potentially absent variables. The
2331 optimizers assume a reference type argument is never NULL. */
2332 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2333 || sym->attr.optional
2334 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2335 type = build_pointer_type (type);
2336 else
2338 type = build_reference_type (type);
2339 if (restricted)
2340 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2344 return (type);
2347 /* Layout and output debug info for a record type. */
2349 void
2350 gfc_finish_type (tree type)
2352 tree decl;
2354 decl = build_decl (input_location,
2355 TYPE_DECL, NULL_TREE, type);
2356 TYPE_STUB_DECL (type) = decl;
2357 layout_type (type);
2358 rest_of_type_compilation (type, 1);
2359 rest_of_decl_compilation (decl, 1, 0);
2362 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2363 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2364 to the end of the field list pointed to by *CHAIN.
2366 Returns a pointer to the new field. */
2368 static tree
2369 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2371 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2373 DECL_CONTEXT (decl) = context;
2374 DECL_CHAIN (decl) = NULL_TREE;
2375 if (TYPE_FIELDS (context) == NULL_TREE)
2376 TYPE_FIELDS (context) = decl;
2377 if (chain != NULL)
2379 if (*chain != NULL)
2380 **chain = decl;
2381 *chain = &DECL_CHAIN (decl);
2384 return decl;
2387 /* Like `gfc_add_field_to_struct_1', but adds alignment
2388 information. */
2390 tree
2391 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2393 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2395 DECL_INITIAL (decl) = 0;
2396 SET_DECL_ALIGN (decl, 0);
2397 DECL_USER_ALIGN (decl) = 0;
2399 return decl;
2403 /* Copy the backend_decl and component backend_decls if
2404 the two derived type symbols are "equal", as described
2405 in 4.4.2 and resolved by gfc_compare_derived_types. */
2408 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2409 bool from_gsym)
2411 gfc_component *to_cm;
2412 gfc_component *from_cm;
2414 if (from == to)
2415 return 1;
2417 if (from->backend_decl == NULL
2418 || !gfc_compare_derived_types (from, to))
2419 return 0;
2421 to->backend_decl = from->backend_decl;
2423 to_cm = to->components;
2424 from_cm = from->components;
2426 /* Copy the component declarations. If a component is itself
2427 a derived type, we need a copy of its component declarations.
2428 This is done by recursing into gfc_get_derived_type and
2429 ensures that the component's component declarations have
2430 been built. If it is a character, we need the character
2431 length, as well. */
2432 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2434 to_cm->backend_decl = from_cm->backend_decl;
2435 to_cm->caf_token = from_cm->caf_token;
2436 if (from_cm->ts.type == BT_UNION)
2437 gfc_get_union_type (to_cm->ts.u.derived);
2438 else if (from_cm->ts.type == BT_DERIVED
2439 && (!from_cm->attr.pointer || from_gsym))
2440 gfc_get_derived_type (to_cm->ts.u.derived);
2441 else if (from_cm->ts.type == BT_CLASS
2442 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2443 gfc_get_derived_type (to_cm->ts.u.derived);
2444 else if (from_cm->ts.type == BT_CHARACTER)
2445 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2448 return 1;
2452 /* Build a tree node for a procedure pointer component. */
2454 static tree
2455 gfc_get_ppc_type (gfc_component* c)
2457 tree t;
2459 /* Explicit interface. */
2460 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2461 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2463 /* Implicit interface (only return value may be known). */
2464 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2465 t = gfc_typenode_for_spec (&c->ts);
2466 else
2467 t = void_type_node;
2469 /* FIXME: it would be better to provide explicit interfaces in all
2470 cases, since they should be known by the compiler. */
2471 return build_pointer_type (build_function_type (t, NULL_TREE));
2475 /* Build a tree node for a union type. Requires building each map
2476 structure which is an element of the union. */
2478 tree
2479 gfc_get_union_type (gfc_symbol *un)
2481 gfc_component *map = NULL;
2482 tree typenode = NULL, map_type = NULL, map_field = NULL;
2483 tree *chain = NULL;
2485 if (un->backend_decl)
2487 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2488 return un->backend_decl;
2489 else
2490 typenode = un->backend_decl;
2492 else
2494 typenode = make_node (UNION_TYPE);
2495 TYPE_NAME (typenode) = get_identifier (un->name);
2498 /* Add each contained MAP as a field. */
2499 for (map = un->components; map; map = map->next)
2501 gcc_assert (map->ts.type == BT_DERIVED);
2503 /* The map's type node, which is defined within this union's context. */
2504 map_type = gfc_get_derived_type (map->ts.u.derived);
2505 TYPE_CONTEXT (map_type) = typenode;
2507 /* The map field's declaration. */
2508 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2509 map_type, &chain);
2510 if (map->loc.lb)
2511 gfc_set_decl_location (map_field, &map->loc);
2512 else if (un->declared_at.lb)
2513 gfc_set_decl_location (map_field, &un->declared_at);
2515 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2516 DECL_NAMELESS(map_field) = true;
2518 /* We should never clobber another backend declaration for this map,
2519 because each map component is unique. */
2520 if (!map->backend_decl)
2521 map->backend_decl = map_field;
2524 un->backend_decl = typenode;
2525 gfc_finish_type (typenode);
2527 return typenode;
2531 /* Build a tree node for a derived type. If there are equal
2532 derived types, with different local names, these are built
2533 at the same time. If an equal derived type has been built
2534 in a parent namespace, this is used. */
2536 tree
2537 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2539 tree typenode = NULL, field = NULL, field_type = NULL;
2540 tree canonical = NULL_TREE;
2541 tree *chain = NULL;
2542 bool got_canonical = false;
2543 bool unlimited_entity = false;
2544 gfc_component *c;
2545 gfc_namespace *ns;
2546 tree tmp;
2547 bool coarray_flag;
2549 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2550 && derived->module && !derived->attr.vtype;
2552 gcc_assert (!derived->attr.pdt_template);
2554 if (derived->attr.unlimited_polymorphic
2555 || (flag_coarray == GFC_FCOARRAY_LIB
2556 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2557 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2558 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2559 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2560 return ptr_type_node;
2562 if (flag_coarray != GFC_FCOARRAY_LIB
2563 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2564 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2565 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2566 return gfc_get_int_type (gfc_default_integer_kind);
2568 if (derived && derived->attr.flavor == FL_PROCEDURE
2569 && derived->attr.generic)
2570 derived = gfc_find_dt_in_generic (derived);
2572 /* See if it's one of the iso_c_binding derived types. */
2573 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2575 if (derived->backend_decl)
2576 return derived->backend_decl;
2578 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2579 derived->backend_decl = ptr_type_node;
2580 else
2581 derived->backend_decl = pfunc_type_node;
2583 derived->ts.kind = gfc_index_integer_kind;
2584 derived->ts.type = BT_INTEGER;
2585 /* Set the f90_type to BT_VOID as a way to recognize something of type
2586 BT_INTEGER that needs to fit a void * for the purpose of the
2587 iso_c_binding derived types. */
2588 derived->ts.f90_type = BT_VOID;
2590 return derived->backend_decl;
2593 /* If use associated, use the module type for this one. */
2594 if (derived->backend_decl == NULL
2595 && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2596 && derived->module
2597 && gfc_get_module_backend_decl (derived))
2598 goto copy_derived_types;
2600 /* The derived types from an earlier namespace can be used as the
2601 canonical type. */
2602 if (derived->backend_decl == NULL
2603 && !derived->attr.use_assoc
2604 && !derived->attr.used_in_submodule
2605 && gfc_global_ns_list)
2607 for (ns = gfc_global_ns_list;
2608 ns->translated && !got_canonical;
2609 ns = ns->sibling)
2611 if (ns->derived_types)
2613 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2614 dt = dt->dt_next)
2616 gfc_copy_dt_decls_ifequal (dt, derived, true);
2617 if (derived->backend_decl)
2618 got_canonical = true;
2619 if (dt->dt_next == ns->derived_types)
2620 break;
2626 /* Store up the canonical type to be added to this one. */
2627 if (got_canonical)
2629 if (TYPE_CANONICAL (derived->backend_decl))
2630 canonical = TYPE_CANONICAL (derived->backend_decl);
2631 else
2632 canonical = derived->backend_decl;
2634 derived->backend_decl = NULL_TREE;
2637 /* derived->backend_decl != 0 means we saw it before, but its
2638 components' backend_decl may have not been built. */
2639 if (derived->backend_decl)
2641 /* Its components' backend_decl have been built or we are
2642 seeing recursion through the formal arglist of a procedure
2643 pointer component. */
2644 if (TYPE_FIELDS (derived->backend_decl))
2645 return derived->backend_decl;
2646 else if (derived->attr.abstract
2647 && derived->attr.proc_pointer_comp)
2649 /* If an abstract derived type with procedure pointer
2650 components has no other type of component, return the
2651 backend_decl. Otherwise build the components if any of the
2652 non-procedure pointer components have no backend_decl. */
2653 for (c = derived->components; c; c = c->next)
2655 bool same_alloc_type = c->attr.allocatable
2656 && derived == c->ts.u.derived;
2657 if (!c->attr.proc_pointer
2658 && !same_alloc_type
2659 && c->backend_decl == NULL)
2660 break;
2661 else if (c->next == NULL)
2662 return derived->backend_decl;
2664 typenode = derived->backend_decl;
2666 else
2667 typenode = derived->backend_decl;
2669 else
2671 /* We see this derived type first time, so build the type node. */
2672 typenode = make_node (RECORD_TYPE);
2673 TYPE_NAME (typenode) = get_identifier (derived->name);
2674 TYPE_PACKED (typenode) = flag_pack_derived;
2675 derived->backend_decl = typenode;
2678 if (derived->components
2679 && derived->components->ts.type == BT_DERIVED
2680 && strcmp (derived->components->name, "_data") == 0
2681 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2682 unlimited_entity = true;
2684 /* Go through the derived type components, building them as
2685 necessary. The reason for doing this now is that it is
2686 possible to recurse back to this derived type through a
2687 pointer component (PR24092). If this happens, the fields
2688 will be built and so we can return the type. */
2689 for (c = derived->components; c; c = c->next)
2691 bool same_alloc_type = c->attr.allocatable
2692 && derived == c->ts.u.derived;
2694 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2695 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2697 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2698 continue;
2700 if ((!c->attr.pointer && !c->attr.proc_pointer
2701 && !same_alloc_type)
2702 || c->ts.u.derived->backend_decl == NULL)
2704 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2705 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2706 local_codim);
2709 if (c->ts.u.derived->attr.is_iso_c)
2711 /* Need to copy the modified ts from the derived type. The
2712 typespec was modified because C_PTR/C_FUNPTR are translated
2713 into (void *) from derived types. */
2714 c->ts.type = c->ts.u.derived->ts.type;
2715 c->ts.kind = c->ts.u.derived->ts.kind;
2716 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2717 if (c->initializer)
2719 c->initializer->ts.type = c->ts.type;
2720 c->initializer->ts.kind = c->ts.kind;
2721 c->initializer->ts.f90_type = c->ts.f90_type;
2722 c->initializer->expr_type = EXPR_NULL;
2727 if (TYPE_FIELDS (derived->backend_decl))
2728 return derived->backend_decl;
2730 /* Build the type member list. Install the newly created RECORD_TYPE
2731 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2732 through only the top-level linked list of components so we correctly
2733 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2734 types are built as part of gfc_get_union_type. */
2735 for (c = derived->components; c; c = c->next)
2737 bool same_alloc_type = c->attr.allocatable
2738 && derived == c->ts.u.derived;
2739 /* Prevent infinite recursion, when the procedure pointer type is
2740 the same as derived, by forcing the procedure pointer component to
2741 be built as if the explicit interface does not exist. */
2742 if (c->attr.proc_pointer
2743 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2744 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2745 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2746 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2747 field_type = gfc_get_ppc_type (c);
2748 else if (c->attr.proc_pointer && derived->backend_decl)
2750 tmp = build_function_type (derived->backend_decl, NULL_TREE);
2751 field_type = build_pointer_type (tmp);
2753 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2754 field_type = c->ts.u.derived->backend_decl;
2755 else if (c->attr.caf_token)
2756 field_type = pvoid_type_node;
2757 else
2759 if (c->ts.type == BT_CHARACTER
2760 && !c->ts.deferred && !c->attr.pdt_string)
2762 /* Evaluate the string length. */
2763 gfc_conv_const_charlen (c->ts.u.cl);
2764 gcc_assert (c->ts.u.cl->backend_decl);
2766 else if (c->ts.type == BT_CHARACTER)
2767 c->ts.u.cl->backend_decl
2768 = build_int_cst (gfc_charlen_type_node, 0);
2770 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2773 /* This returns an array descriptor type. Initialization may be
2774 required. */
2775 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2777 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2779 enum gfc_array_kind akind;
2780 if (c->attr.pointer)
2781 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2782 : GFC_ARRAY_POINTER;
2783 else
2784 akind = GFC_ARRAY_ALLOCATABLE;
2785 /* Pointers to arrays aren't actually pointer types. The
2786 descriptors are separate, but the data is common. */
2787 field_type = gfc_build_array_type (field_type, c->as, akind,
2788 !c->attr.target
2789 && !c->attr.pointer,
2790 c->attr.contiguous,
2791 codimen);
2793 else
2794 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2795 PACKED_STATIC,
2796 !c->attr.target);
2798 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2799 && !c->attr.proc_pointer
2800 && !(unlimited_entity && c == derived->components))
2801 field_type = build_pointer_type (field_type);
2803 if (c->attr.pointer || same_alloc_type)
2804 field_type = gfc_nonrestricted_type (field_type);
2806 /* vtype fields can point to different types to the base type. */
2807 if (c->ts.type == BT_DERIVED
2808 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2809 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2810 ptr_mode, true);
2812 /* Ensure that the CLASS language specific flag is set. */
2813 if (c->ts.type == BT_CLASS)
2815 if (POINTER_TYPE_P (field_type))
2816 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2817 else
2818 GFC_CLASS_TYPE_P (field_type) = 1;
2821 field = gfc_add_field_to_struct (typenode,
2822 get_identifier (c->name),
2823 field_type, &chain);
2824 if (c->loc.lb)
2825 gfc_set_decl_location (field, &c->loc);
2826 else if (derived->declared_at.lb)
2827 gfc_set_decl_location (field, &derived->declared_at);
2829 gfc_finish_decl_attrs (field, &c->attr);
2831 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2833 gcc_assert (field);
2834 if (!c->backend_decl)
2835 c->backend_decl = field;
2837 if (c->attr.pointer && c->attr.dimension
2838 && !(c->ts.type == BT_DERIVED
2839 && strcmp (c->name, "_data") == 0))
2840 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2843 /* Now lay out the derived type, including the fields. */
2844 if (canonical)
2845 TYPE_CANONICAL (typenode) = canonical;
2847 gfc_finish_type (typenode);
2848 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2849 if (derived->module && derived->ns->proc_name
2850 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2852 if (derived->ns->proc_name->backend_decl
2853 && TREE_CODE (derived->ns->proc_name->backend_decl)
2854 == NAMESPACE_DECL)
2856 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2857 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2858 = derived->ns->proc_name->backend_decl;
2862 derived->backend_decl = typenode;
2864 copy_derived_types:
2866 for (c = derived->components; c; c = c->next)
2868 /* Do not add a caf_token field for class container components. */
2869 if ((codimen || coarray_flag)
2870 && !c->attr.dimension && !c->attr.codimension
2871 && (c->attr.allocatable || c->attr.pointer)
2872 && !derived->attr.is_class)
2874 /* Provide sufficient space to hold "_caf_symbol". */
2875 char caf_name[GFC_MAX_SYMBOL_LEN + 6];
2876 gfc_component *token;
2877 snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
2878 token = gfc_find_component (derived, caf_name, true, true, NULL);
2879 gcc_assert (token);
2880 c->caf_token = token->backend_decl;
2881 suppress_warning (c->caf_token);
2885 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
2887 gfc_copy_dt_decls_ifequal (derived, dt, false);
2888 if (dt->dt_next == gfc_derived_types)
2889 break;
2892 return derived->backend_decl;
2897 gfc_return_by_reference (gfc_symbol * sym)
2899 if (!sym->attr.function)
2900 return 0;
2902 if (sym->attr.dimension)
2903 return 1;
2905 if (sym->ts.type == BT_CHARACTER
2906 && !sym->attr.is_bind_c
2907 && (!sym->attr.result
2908 || !sym->ns->proc_name
2909 || !sym->ns->proc_name->attr.is_bind_c))
2910 return 1;
2912 /* Possibly return complex numbers by reference for g77 compatibility.
2913 We don't do this for calls to intrinsics (as the library uses the
2914 -fno-f2c calling convention), nor for calls to functions which always
2915 require an explicit interface, as no compatibility problems can
2916 arise there. */
2917 if (flag_f2c && sym->ts.type == BT_COMPLEX
2918 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2919 return 1;
2921 return 0;
2924 static tree
2925 gfc_get_mixed_entry_union (gfc_namespace *ns)
2927 tree type;
2928 tree *chain = NULL;
2929 char name[GFC_MAX_SYMBOL_LEN + 1];
2930 gfc_entry_list *el, *el2;
2932 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2933 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2935 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2937 /* Build the type node. */
2938 type = make_node (UNION_TYPE);
2940 TYPE_NAME (type) = get_identifier (name);
2942 for (el = ns->entries; el; el = el->next)
2944 /* Search for duplicates. */
2945 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2946 if (el2->sym->result == el->sym->result)
2947 break;
2949 if (el == el2)
2950 gfc_add_field_to_struct_1 (type,
2951 get_identifier (el->sym->result->name),
2952 gfc_sym_type (el->sym->result), &chain);
2955 /* Finish off the type. */
2956 gfc_finish_type (type);
2957 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2958 return type;
2961 /* Create a "fn spec" based on the formal arguments;
2962 cf. create_function_arglist. */
2964 static tree
2965 create_fn_spec (gfc_symbol *sym, tree fntype)
2967 char spec[150];
2968 size_t spec_len;
2969 gfc_formal_arglist *f;
2970 tree tmp;
2972 memset (&spec, 0, sizeof (spec));
2973 spec[0] = '.';
2974 spec[1] = ' ';
2975 spec_len = 2;
2977 if (sym->attr.entry_master)
2979 spec[spec_len++] = 'R';
2980 spec[spec_len++] = ' ';
2982 if (gfc_return_by_reference (sym))
2984 gfc_symbol *result = sym->result ? sym->result : sym;
2986 if (result->attr.pointer || sym->attr.proc_pointer)
2988 spec[spec_len++] = '.';
2989 spec[spec_len++] = ' ';
2991 else
2993 spec[spec_len++] = 'w';
2994 spec[spec_len++] = ' ';
2996 if (sym->ts.type == BT_CHARACTER)
2998 if (!sym->ts.u.cl->length
2999 && (sym->attr.allocatable || sym->attr.pointer))
3000 spec[spec_len++] = 'w';
3001 else
3002 spec[spec_len++] = 'R';
3003 spec[spec_len++] = ' ';
3007 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3008 if (spec_len < sizeof (spec))
3010 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
3011 || f->sym->attr.external || f->sym->attr.cray_pointer
3012 || (f->sym->ts.type == BT_DERIVED
3013 && (f->sym->ts.u.derived->attr.proc_pointer_comp
3014 || f->sym->ts.u.derived->attr.pointer_comp))
3015 || (f->sym->ts.type == BT_CLASS
3016 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3017 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3018 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3020 spec[spec_len++] = '.';
3021 spec[spec_len++] = ' ';
3023 else if (f->sym->attr.intent == INTENT_IN)
3025 spec[spec_len++] = 'r';
3026 spec[spec_len++] = ' ';
3028 else if (f->sym)
3030 spec[spec_len++] = 'w';
3031 spec[spec_len++] = ' ';
3035 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3036 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3037 return build_type_attribute_variant (fntype, tmp);
3041 /* NOTE: The returned function type must match the argument list created by
3042 create_function_arglist. */
3044 tree
3045 gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3046 const char *fnspec)
3048 tree type;
3049 vec<tree, va_gc> *typelist = NULL;
3050 gfc_formal_arglist *f;
3051 gfc_symbol *arg;
3052 int alternate_return = 0;
3053 bool is_varargs = true;
3055 /* Make sure this symbol is a function, a subroutine or the main
3056 program. */
3057 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3058 || sym->attr.flavor == FL_PROGRAM);
3060 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3061 so that they can be detected here and handled further down. */
3062 if (sym->backend_decl == NULL)
3063 sym->backend_decl = error_mark_node;
3064 else if (sym->backend_decl == error_mark_node)
3065 goto arg_type_list_done;
3066 else if (sym->attr.proc_pointer)
3067 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3068 else
3069 return TREE_TYPE (sym->backend_decl);
3071 if (sym->attr.entry_master)
3072 /* Additional parameter for selecting an entry point. */
3073 vec_safe_push (typelist, gfc_array_index_type);
3075 if (sym->result)
3076 arg = sym->result;
3077 else
3078 arg = sym;
3080 if (arg->ts.type == BT_CHARACTER)
3081 gfc_conv_const_charlen (arg->ts.u.cl);
3083 /* Some functions we use an extra parameter for the return value. */
3084 if (gfc_return_by_reference (sym))
3086 type = gfc_sym_type (arg);
3087 if (arg->ts.type == BT_COMPLEX
3088 || arg->attr.dimension
3089 || arg->ts.type == BT_CHARACTER)
3090 type = build_reference_type (type);
3092 vec_safe_push (typelist, type);
3093 if (arg->ts.type == BT_CHARACTER)
3095 if (!arg->ts.deferred)
3096 /* Transfer by value. */
3097 vec_safe_push (typelist, gfc_charlen_type_node);
3098 else
3099 /* Deferred character lengths are transferred by reference
3100 so that the value can be returned. */
3101 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3104 if (sym->backend_decl == error_mark_node && actual_args != NULL
3105 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3106 || sym->attr.proc == PROC_UNKNOWN))
3107 gfc_get_formal_from_actual_arglist (sym, actual_args);
3109 /* Build the argument types for the function. */
3110 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3112 arg = f->sym;
3113 if (arg)
3115 /* Evaluate constant character lengths here so that they can be
3116 included in the type. */
3117 if (arg->ts.type == BT_CHARACTER)
3118 gfc_conv_const_charlen (arg->ts.u.cl);
3120 if (arg->attr.flavor == FL_PROCEDURE)
3122 type = gfc_get_function_type (arg);
3123 type = build_pointer_type (type);
3125 else
3126 type = gfc_sym_type (arg, sym->attr.is_bind_c);
3128 /* Parameter Passing Convention
3130 We currently pass all parameters by reference.
3131 Parameters with INTENT(IN) could be passed by value.
3132 The problem arises if a function is called via an implicit
3133 prototype. In this situation the INTENT is not known.
3134 For this reason all parameters to global functions must be
3135 passed by reference. Passing by value would potentially
3136 generate bad code. Worse there would be no way of telling that
3137 this code was bad, except that it would give incorrect results.
3139 Contained procedures could pass by value as these are never
3140 used without an explicit interface, and cannot be passed as
3141 actual parameters for a dummy procedure. */
3143 vec_safe_push (typelist, type);
3145 else
3147 if (sym->attr.subroutine)
3148 alternate_return = 1;
3152 /* Add hidden arguments. */
3153 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3155 arg = f->sym;
3156 /* Add hidden string length parameters. */
3157 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3159 if (!arg->ts.deferred)
3160 /* Transfer by value. */
3161 type = gfc_charlen_type_node;
3162 else
3163 /* Deferred character lengths are transferred by reference
3164 so that the value can be returned. */
3165 type = build_pointer_type (gfc_charlen_type_node);
3167 vec_safe_push (typelist, type);
3169 /* For noncharacter scalar intrinsic types, VALUE passes the value,
3170 hence, the optional status cannot be transferred via a NULL pointer.
3171 Thus, we will use a hidden argument in that case. */
3172 else if (arg
3173 && arg->attr.optional
3174 && arg->attr.value
3175 && !arg->attr.dimension
3176 && arg->ts.type != BT_CLASS
3177 && !gfc_bt_struct (arg->ts.type))
3178 vec_safe_push (typelist, boolean_type_node);
3179 /* Coarrays which are descriptorless or assumed-shape pass with
3180 -fcoarray=lib the token and the offset as hidden arguments. */
3181 if (arg
3182 && flag_coarray == GFC_FCOARRAY_LIB
3183 && ((arg->ts.type != BT_CLASS
3184 && arg->attr.codimension
3185 && !arg->attr.allocatable)
3186 || (arg->ts.type == BT_CLASS
3187 && CLASS_DATA (arg)->attr.codimension
3188 && !CLASS_DATA (arg)->attr.allocatable)))
3190 vec_safe_push (typelist, pvoid_type_node); /* caf_token. */
3191 vec_safe_push (typelist, gfc_array_index_type); /* caf_offset. */
3195 if (!vec_safe_is_empty (typelist)
3196 || sym->attr.is_main_program
3197 || sym->attr.if_source != IFSRC_UNKNOWN)
3198 is_varargs = false;
3200 if (sym->backend_decl == error_mark_node)
3201 sym->backend_decl = NULL_TREE;
3203 arg_type_list_done:
3205 if (alternate_return)
3206 type = integer_type_node;
3207 else if (!sym->attr.function || gfc_return_by_reference (sym))
3208 type = void_type_node;
3209 else if (sym->attr.mixed_entry_master)
3210 type = gfc_get_mixed_entry_union (sym->ns);
3211 else if (flag_f2c && sym->ts.type == BT_REAL
3212 && sym->ts.kind == gfc_default_real_kind
3213 && !sym->attr.always_explicit)
3215 /* Special case: f2c calling conventions require that (scalar)
3216 default REAL functions return the C type double instead. f2c
3217 compatibility is only an issue with functions that don't
3218 require an explicit interface, as only these could be
3219 implemented in Fortran 77. */
3220 sym->ts.kind = gfc_default_double_kind;
3221 type = gfc_typenode_for_spec (&sym->ts);
3222 sym->ts.kind = gfc_default_real_kind;
3224 else if (sym->result && sym->result->attr.proc_pointer)
3225 /* Procedure pointer return values. */
3227 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3229 /* Unset proc_pointer as gfc_get_function_type
3230 is called recursively. */
3231 sym->result->attr.proc_pointer = 0;
3232 type = build_pointer_type (gfc_get_function_type (sym->result));
3233 sym->result->attr.proc_pointer = 1;
3235 else
3236 type = gfc_sym_type (sym->result);
3238 else
3239 type = gfc_sym_type (sym);
3241 if (is_varargs)
3242 type = build_varargs_function_type_vec (type, typelist);
3243 else
3244 type = build_function_type_vec (type, typelist);
3246 /* If we were passed an fn spec, add it here, otherwise determine it from
3247 the formal arguments. */
3248 if (fnspec)
3250 tree tmp;
3251 int spec_len = strlen (fnspec);
3252 tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3253 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3254 type = build_type_attribute_variant (type, tmp);
3256 else
3257 type = create_fn_spec (sym, type);
3259 return type;
3262 /* Language hooks for middle-end access to type nodes. */
3264 /* Return an integer type with BITS bits of precision,
3265 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3267 tree
3268 gfc_type_for_size (unsigned bits, int unsignedp)
3270 if (!unsignedp)
3272 int i;
3273 for (i = 0; i <= MAX_INT_KINDS; ++i)
3275 tree type = gfc_integer_types[i];
3276 if (type && bits == TYPE_PRECISION (type))
3277 return type;
3280 /* Handle TImode as a special case because it is used by some backends
3281 (e.g. ARM) even though it is not available for normal use. */
3282 #if HOST_BITS_PER_WIDE_INT >= 64
3283 if (bits == TYPE_PRECISION (intTI_type_node))
3284 return intTI_type_node;
3285 #endif
3287 if (bits <= TYPE_PRECISION (intQI_type_node))
3288 return intQI_type_node;
3289 if (bits <= TYPE_PRECISION (intHI_type_node))
3290 return intHI_type_node;
3291 if (bits <= TYPE_PRECISION (intSI_type_node))
3292 return intSI_type_node;
3293 if (bits <= TYPE_PRECISION (intDI_type_node))
3294 return intDI_type_node;
3295 if (bits <= TYPE_PRECISION (intTI_type_node))
3296 return intTI_type_node;
3298 else
3300 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3301 return unsigned_intQI_type_node;
3302 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3303 return unsigned_intHI_type_node;
3304 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3305 return unsigned_intSI_type_node;
3306 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3307 return unsigned_intDI_type_node;
3308 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3309 return unsigned_intTI_type_node;
3312 return NULL_TREE;
3315 /* Return a data type that has machine mode MODE. If the mode is an
3316 integer, then UNSIGNEDP selects between signed and unsigned types. */
3318 tree
3319 gfc_type_for_mode (machine_mode mode, int unsignedp)
3321 int i;
3322 tree *base;
3323 scalar_int_mode int_mode;
3325 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3326 base = gfc_real_types;
3327 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3328 base = gfc_complex_types;
3329 else if (is_a <scalar_int_mode> (mode, &int_mode))
3331 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3332 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3334 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3335 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3337 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
3338 GET_MODE_NUNITS (mode));
3339 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3340 return build_vector_type_for_mode (bool_type, mode);
3342 else if (VECTOR_MODE_P (mode)
3343 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3345 machine_mode inner_mode = GET_MODE_INNER (mode);
3346 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3347 if (inner_type != NULL_TREE)
3348 return build_vector_type_for_mode (inner_type, mode);
3349 return NULL_TREE;
3351 else
3352 return NULL_TREE;
3354 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3356 tree type = base[i];
3357 if (type && mode == TYPE_MODE (type))
3358 return type;
3361 return NULL_TREE;
3364 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3365 in that case. */
3367 bool
3368 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3370 int rank, dim;
3371 bool indirect = false;
3372 tree etype, ptype, t, base_decl;
3373 tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
3374 tree lower_suboff, upper_suboff, stride_suboff;
3375 tree dtype, field, rank_off;
3377 if (! GFC_DESCRIPTOR_TYPE_P (type))
3379 if (! POINTER_TYPE_P (type))
3380 return false;
3381 type = TREE_TYPE (type);
3382 if (! GFC_DESCRIPTOR_TYPE_P (type))
3383 return false;
3384 indirect = true;
3387 rank = GFC_TYPE_ARRAY_RANK (type);
3388 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3389 return false;
3391 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3392 gcc_assert (POINTER_TYPE_P (etype));
3393 etype = TREE_TYPE (etype);
3395 /* If the type is not a scalar coarray. */
3396 if (TREE_CODE (etype) == ARRAY_TYPE)
3397 etype = TREE_TYPE (etype);
3399 /* Can't handle variable sized elements yet. */
3400 if (int_size_in_bytes (etype) <= 0)
3401 return false;
3402 /* Nor non-constant lower bounds in assumed shape arrays. */
3403 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3404 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3406 for (dim = 0; dim < rank; dim++)
3407 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3408 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3409 return false;
3412 memset (info, '\0', sizeof (*info));
3413 info->ndimensions = rank;
3414 info->ordering = array_descr_ordering_column_major;
3415 info->element_type = etype;
3416 ptype = build_pointer_type (gfc_array_index_type);
3417 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3418 if (!base_decl)
3420 base_decl = build_debug_expr_decl (indirect
3421 ? build_pointer_type (ptype) : ptype);
3422 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3424 info->base_decl = base_decl;
3425 if (indirect)
3426 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3428 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3429 &dim_off, &dim_size, &stride_suboff,
3430 &lower_suboff, &upper_suboff);
3432 t = fold_build_pointer_plus (base_decl, span_off);
3433 elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3435 t = base_decl;
3436 if (!integer_zerop (data_off))
3437 t = fold_build_pointer_plus (t, data_off);
3438 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3439 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3440 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3441 info->allocated = build2 (NE_EXPR, logical_type_node,
3442 info->data_location, null_pointer_node);
3443 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3444 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3445 info->associated = build2 (NE_EXPR, logical_type_node,
3446 info->data_location, null_pointer_node);
3447 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3448 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3449 && dwarf_version >= 5)
3451 rank = 1;
3452 info->ndimensions = 1;
3453 t = base_decl;
3454 if (!integer_zerop (dtype_off))
3455 t = fold_build_pointer_plus (t, dtype_off);
3456 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3457 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3458 rank_off = byte_position (field);
3459 if (!integer_zerop (dtype_off))
3460 t = fold_build_pointer_plus (t, rank_off);
3462 t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
3463 t = build1 (INDIRECT_REF, TREE_TYPE (field), t);
3464 info->rank = t;
3465 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3466 t = size_binop (MULT_EXPR, t, dim_size);
3467 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3470 for (dim = 0; dim < rank; dim++)
3472 t = fold_build_pointer_plus (base_decl,
3473 size_binop (PLUS_EXPR,
3474 dim_off, lower_suboff));
3475 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3476 info->dimen[dim].lower_bound = t;
3477 t = fold_build_pointer_plus (base_decl,
3478 size_binop (PLUS_EXPR,
3479 dim_off, upper_suboff));
3480 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3481 info->dimen[dim].upper_bound = t;
3482 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3483 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3485 /* Assumed shape arrays have known lower bounds. */
3486 info->dimen[dim].upper_bound
3487 = build2 (MINUS_EXPR, gfc_array_index_type,
3488 info->dimen[dim].upper_bound,
3489 info->dimen[dim].lower_bound);
3490 info->dimen[dim].lower_bound
3491 = fold_convert (gfc_array_index_type,
3492 GFC_TYPE_ARRAY_LBOUND (type, dim));
3493 info->dimen[dim].upper_bound
3494 = build2 (PLUS_EXPR, gfc_array_index_type,
3495 info->dimen[dim].lower_bound,
3496 info->dimen[dim].upper_bound);
3498 t = fold_build_pointer_plus (base_decl,
3499 size_binop (PLUS_EXPR,
3500 dim_off, stride_suboff));
3501 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3502 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3503 info->dimen[dim].stride = t;
3504 if (dim + 1 < rank)
3505 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3508 return true;
3512 /* Create a type to handle vector subscripts for coarray library calls. It
3513 has the form:
3514 struct caf_vector_t {
3515 size_t nvec; // size of the vector
3516 union {
3517 struct {
3518 void *vector;
3519 int kind;
3520 } v;
3521 struct {
3522 ptrdiff_t lower_bound;
3523 ptrdiff_t upper_bound;
3524 ptrdiff_t stride;
3525 } triplet;
3526 } u;
3528 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3529 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3531 tree
3532 gfc_get_caf_vector_type (int dim)
3534 static tree vector_types[GFC_MAX_DIMENSIONS];
3535 static tree vec_type = NULL_TREE;
3536 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3538 if (vector_types[dim-1] != NULL_TREE)
3539 return vector_types[dim-1];
3541 if (vec_type == NULL_TREE)
3543 chain = 0;
3544 vect_struct_type = make_node (RECORD_TYPE);
3545 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3546 get_identifier ("vector"),
3547 pvoid_type_node, &chain);
3548 suppress_warning (tmp);
3549 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3550 get_identifier ("kind"),
3551 integer_type_node, &chain);
3552 suppress_warning (tmp);
3553 gfc_finish_type (vect_struct_type);
3555 chain = 0;
3556 triplet_struct_type = make_node (RECORD_TYPE);
3557 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3558 get_identifier ("lower_bound"),
3559 gfc_array_index_type, &chain);
3560 suppress_warning (tmp);
3561 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3562 get_identifier ("upper_bound"),
3563 gfc_array_index_type, &chain);
3564 suppress_warning (tmp);
3565 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3566 gfc_array_index_type, &chain);
3567 suppress_warning (tmp);
3568 gfc_finish_type (triplet_struct_type);
3570 chain = 0;
3571 union_type = make_node (UNION_TYPE);
3572 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3573 vect_struct_type, &chain);
3574 suppress_warning (tmp);
3575 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3576 triplet_struct_type, &chain);
3577 suppress_warning (tmp);
3578 gfc_finish_type (union_type);
3580 chain = 0;
3581 vec_type = make_node (RECORD_TYPE);
3582 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3583 size_type_node, &chain);
3584 suppress_warning (tmp);
3585 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3586 union_type, &chain);
3587 suppress_warning (tmp);
3588 gfc_finish_type (vec_type);
3589 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3592 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3593 gfc_rank_cst[dim-1]);
3594 vector_types[dim-1] = build_array_type (vec_type, tmp);
3595 return vector_types[dim-1];
3599 tree
3600 gfc_get_caf_reference_type ()
3602 static tree reference_type = NULL_TREE;
3603 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3604 a_struct_type, u_union_type, tmp, *chain;
3606 if (reference_type != NULL_TREE)
3607 return reference_type;
3609 chain = 0;
3610 c_struct_type = make_node (RECORD_TYPE);
3611 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3612 get_identifier ("offset"),
3613 gfc_array_index_type, &chain);
3614 suppress_warning (tmp);
3615 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3616 get_identifier ("caf_token_offset"),
3617 gfc_array_index_type, &chain);
3618 suppress_warning (tmp);
3619 gfc_finish_type (c_struct_type);
3621 chain = 0;
3622 s_struct_type = make_node (RECORD_TYPE);
3623 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3624 get_identifier ("start"),
3625 gfc_array_index_type, &chain);
3626 suppress_warning (tmp);
3627 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3628 get_identifier ("end"),
3629 gfc_array_index_type, &chain);
3630 suppress_warning (tmp);
3631 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3632 get_identifier ("stride"),
3633 gfc_array_index_type, &chain);
3634 suppress_warning (tmp);
3635 gfc_finish_type (s_struct_type);
3637 chain = 0;
3638 v_struct_type = make_node (RECORD_TYPE);
3639 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3640 get_identifier ("vector"),
3641 pvoid_type_node, &chain);
3642 suppress_warning (tmp);
3643 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3644 get_identifier ("nvec"),
3645 size_type_node, &chain);
3646 suppress_warning (tmp);
3647 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3648 get_identifier ("kind"),
3649 integer_type_node, &chain);
3650 suppress_warning (tmp);
3651 gfc_finish_type (v_struct_type);
3653 chain = 0;
3654 union_type = make_node (UNION_TYPE);
3655 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3656 s_struct_type, &chain);
3657 suppress_warning (tmp);
3658 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3659 v_struct_type, &chain);
3660 suppress_warning (tmp);
3661 gfc_finish_type (union_type);
3663 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3664 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3665 dim_union_type = build_array_type (union_type, tmp);
3667 chain = 0;
3668 a_struct_type = make_node (RECORD_TYPE);
3669 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3670 build_array_type (unsigned_char_type_node,
3671 build_range_type (gfc_array_index_type,
3672 gfc_index_zero_node,
3673 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3674 &chain);
3675 suppress_warning (tmp);
3676 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3677 get_identifier ("static_array_type"),
3678 integer_type_node, &chain);
3679 suppress_warning (tmp);
3680 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3681 dim_union_type, &chain);
3682 suppress_warning (tmp);
3683 gfc_finish_type (a_struct_type);
3685 chain = 0;
3686 u_union_type = make_node (UNION_TYPE);
3687 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3688 c_struct_type, &chain);
3689 suppress_warning (tmp);
3690 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3691 a_struct_type, &chain);
3692 suppress_warning (tmp);
3693 gfc_finish_type (u_union_type);
3695 chain = 0;
3696 reference_type = make_node (RECORD_TYPE);
3697 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3698 build_pointer_type (reference_type), &chain);
3699 suppress_warning (tmp);
3700 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3701 integer_type_node, &chain);
3702 suppress_warning (tmp);
3703 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3704 size_type_node, &chain);
3705 suppress_warning (tmp);
3706 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3707 u_union_type, &chain);
3708 suppress_warning (tmp);
3709 gfc_finish_type (reference_type);
3710 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3712 return reference_type;
3715 static tree
3716 gfc_get_cfi_dim_type ()
3718 static tree CFI_dim_t = NULL;
3720 if (CFI_dim_t)
3721 return CFI_dim_t;
3723 CFI_dim_t = make_node (RECORD_TYPE);
3724 TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
3725 TYPE_NAMELESS (CFI_dim_t) = 1;
3726 tree field;
3727 tree *chain = NULL;
3728 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
3729 gfc_array_index_type, &chain);
3730 suppress_warning (field);
3731 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
3732 gfc_array_index_type, &chain);
3733 suppress_warning (field);
3734 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
3735 gfc_array_index_type, &chain);
3736 suppress_warning (field);
3737 gfc_finish_type (CFI_dim_t);
3738 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
3739 return CFI_dim_t;
3743 /* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
3744 otherwise dim[dimen] is used. */
3746 tree
3747 gfc_get_cfi_type (int dimen, bool restricted)
3749 gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
3751 int idx = 2*(dimen + 1) + restricted;
3753 if (gfc_cfi_descriptor_base[idx])
3754 return gfc_cfi_descriptor_base[idx];
3756 /* Build the type node. */
3757 tree CFI_cdesc_t = make_node (RECORD_TYPE);
3758 char name[GFC_MAX_SYMBOL_LEN + 1];
3759 if (dimen != -1)
3760 sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
3761 TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
3762 TYPE_NAMELESS (CFI_cdesc_t) = 1;
3764 tree field;
3765 tree *chain = NULL;
3766 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
3767 (restricted ? prvoid_type_node
3768 : ptr_type_node), &chain);
3769 suppress_warning (field);
3770 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
3771 size_type_node, &chain);
3772 suppress_warning (field);
3773 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
3774 integer_type_node, &chain);
3775 suppress_warning (field);
3776 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
3777 signed_char_type_node, &chain);
3778 suppress_warning (field);
3779 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
3780 signed_char_type_node, &chain);
3781 suppress_warning (field);
3782 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
3783 get_typenode_from_name (INT16_TYPE),
3784 &chain);
3785 suppress_warning (field);
3787 if (dimen != 0)
3789 tree range = NULL_TREE;
3790 if (dimen > 0)
3791 range = gfc_rank_cst[dimen - 1];
3792 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3793 range);
3794 tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
3795 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
3796 CFI_dim_t, &chain);
3797 suppress_warning (field);
3800 TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
3801 gfc_finish_type (CFI_cdesc_t);
3802 gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
3803 return CFI_cdesc_t;
3806 #include "gt-fortran-trans-types.h"