LCOV - code coverage report
Current view: top level - gcc/fortran - trans-types.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.4 % 1881 1682
Test Date: 2026-10-03 16:17:38 Functions: 97.3 % 73 71
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.