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-24 16:24:59 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       431882 : tree get_dtype_type_node (void)
     148              : {
     149       431882 :   tree field;
     150       431882 :   tree dtype_node;
     151       431882 :   tree *dtype_chain = NULL;
     152              : 
     153       431882 :   if (dtype_type_node == NULL_TREE)
     154              :     {
     155        32537 :       dtype_node = make_node (RECORD_TYPE);
     156        32537 :       TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
     157        32537 :       TYPE_NAMELESS (dtype_node) = 1;
     158        32537 :       field = gfc_add_field_to_struct_1 (dtype_node,
     159              :                                          get_identifier ("elem_len"),
     160              :                                          size_type_node, &dtype_chain);
     161        32537 :       suppress_warning (field);
     162        32537 :       field = gfc_add_field_to_struct_1 (dtype_node,
     163              :                                          get_identifier ("version"),
     164              :                                          integer_type_node, &dtype_chain);
     165        32537 :       suppress_warning (field);
     166        32537 :       field = gfc_add_field_to_struct_1 (dtype_node,
     167              :                                          get_identifier ("rank"),
     168              :                                          gfc_array_dim_rank_type, &dtype_chain);
     169        32537 :       suppress_warning (field);
     170        32537 :       field = gfc_add_field_to_struct_1 (dtype_node,
     171              :                                          get_identifier ("type"),
     172              :                                          signed_char_type_node, &dtype_chain);
     173        32537 :       suppress_warning (field);
     174        32537 :       field = gfc_add_field_to_struct_1 (dtype_node,
     175              :                                          get_identifier ("attribute"),
     176              :                                          short_integer_type_node, &dtype_chain);
     177        32537 :       suppress_warning (field);
     178        32537 :       gfc_finish_type (dtype_node);
     179        32537 :       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
     180        32537 :       dtype_type_node = dtype_node;
     181              :     }
     182       431882 :   return dtype_type_node;
     183              : }
     184              : 
     185              : static int
     186       260432 : get_real_kind_from_node (tree type)
     187              : {
     188       260432 :   int i;
     189              : 
     190       651080 :   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
     191       651080 :     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       846404 : get_int_kind_from_node (tree type)
     213              : {
     214       846404 :   int i;
     215              : 
     216       846404 :   if (!type)
     217              :     return -2;
     218              : 
     219      2729628 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     220      2729628 :     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       520864 : get_int_kind_from_name (const char *name)
     228              : {
     229       520864 :   return get_int_kind_from_node (get_typenode_from_name (name));
     230              : }
     231              : 
     232              : static int
     233       553418 : get_unsigned_kind_from_node (tree type)
     234              : {
     235       553418 :   int i;
     236              : 
     237       553418 :   if (!type)
     238              :     return -2;
     239              : 
     240       561044 :   for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
     241        11808 :     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       423202 : get_uint_kind_from_name (const char *name)
     249              : {
     250       423202 :   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         5624 : gfc_get_int_kind_from_width_isofortranenv (int size)
     258              : {
     259         5624 :   int i;
     260              : 
     261              :   /* Look for a kind with matching storage size.  */
     262        14060 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     263        14060 :     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         2812 : gfc_get_uint_kind_from_width_isofortranenv (int size)
     278              : {
     279         2812 :   int i;
     280              : 
     281              :   /* Look for a kind with matching storage size.  */
     282         2962 :   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         2712 :   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         2812 : gfc_get_real_kind_from_width_isofortranenv (int size)
     302              : {
     303         2812 :   int digits, i, kind;
     304              : 
     305         2812 :   size /= 8;
     306              : 
     307         2812 :   kind = -1;
     308         2812 :   digits = 0;
     309              : 
     310              :   /* Look for a kind with matching storage size.  */
     311        14060 :   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
     312        11248 :     if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
     313              :       {
     314         2808 :         if (gfc_real_kinds[i].digits > digits)
     315              :           {
     316         2808 :             digits = gfc_real_kinds[i].digits;
     317         2808 :             kind = gfc_real_kinds[i].kind;
     318              :           }
     319              :       }
     320              : 
     321         2812 :   if (kind != -1)
     322              :     return kind;
     323              : 
     324              :   /* Look for a kind with larger storage size.  */
     325         3515 :   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
     326         2812 :     if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
     327         2812 :       kind = -2;
     328              : 
     329              :   return kind;
     330              : }
     331              : 
     332              : 
     333              : 
     334              : static int
     335       162770 : get_int_kind_from_width (int size)
     336              : {
     337       162770 :   int i;
     338              : 
     339       488310 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     340       487492 :     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        32554 : get_int_kind_from_minimal_width (int size)
     348              : {
     349        32554 :   int i;
     350              : 
     351       162770 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     352       162361 :     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        97662 : get_uint_kind_from_width (int size)
     360              : {
     361        97662 :   int i;
     362              : 
     363       100614 :   for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
     364         3690 :     if (gfc_integer_kinds[i].bit_size == size)
     365          738 :       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        32554 : gfc_init_c_interop_kinds (void)
     376              : {
     377        32554 :   int i;
     378              : 
     379              :   /* init all pointers in the list to NULL */
     380      2474104 :   for (i = 0; i < ISOCBINDING_NUMBER; i++)
     381              :     {
     382              :       /* Initialize the name and value fields.  */
     383      2441550 :       c_interop_kinds_table[i].name[0] = '\0';
     384      2441550 :       c_interop_kinds_table[i].value = -100;
     385      2441550 :       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        32554 : }
     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        32554 : gfc_init_kinds (void)
     437              : {
     438        32554 :   opt_scalar_int_mode int_mode_iter;
     439        32554 :   opt_scalar_float_mode float_mode_iter;
     440        32554 :   int i_index, r_index, kind;
     441        32554 :   bool saw_i4 = false, saw_i8 = false;
     442        32554 :   bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
     443        32554 :   scalar_mode r16_mode = QImode;
     444        32554 :   scalar_mode composite_mode = QImode;
     445              : 
     446        32554 :   i_index = 0;
     447       260432 :   FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
     448              :     {
     449       227878 :       scalar_int_mode mode = int_mode_iter.require ();
     450       227878 :       int kind, bitsize;
     451              : 
     452       227878 :       if (!targetm.scalar_mode_supported_p (mode))
     453       227878 :         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       162361 :       bitsize = GET_MODE_BITSIZE (mode);
     459       162361 :       if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
     460            0 :         continue;
     461              : 
     462       162361 :       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       162361 :       kind = bitsize / 8;
     467              : 
     468       162361 :       if (kind == 4)
     469              :         saw_i4 = true;
     470       129807 :       if (kind == 8)
     471        32554 :         saw_i8 = true;
     472              : 
     473       162361 :       gfc_integer_kinds[i_index].kind = kind;
     474       162361 :       gfc_integer_kinds[i_index].radix = 2;
     475       162361 :       gfc_integer_kinds[i_index].digits = bitsize - 1;
     476       162361 :       gfc_integer_kinds[i_index].bit_size = bitsize;
     477              : 
     478       162361 :       if (flag_unsigned)
     479              :         {
     480         1230 :           gfc_unsigned_kinds[i_index].kind = kind;
     481         1230 :           gfc_unsigned_kinds[i_index].radix = 2;
     482         1230 :           gfc_unsigned_kinds[i_index].digits = bitsize;
     483         1230 :           gfc_unsigned_kinds[i_index].bit_size = bitsize;
     484              :         }
     485              : 
     486       162361 :       gfc_logical_kinds[i_index].kind = kind;
     487       162361 :       gfc_logical_kinds[i_index].bit_size = bitsize;
     488              : 
     489       162361 :       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        32554 :   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        32554 :   gcc_assert(saw_i4);
     502              : 
     503              :   /* Set the maximum integer kind.  Used with at least BOZ constants.  */
     504        32554 :   gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
     505              : 
     506        32554 :   r_index = 0;
     507       227878 :   FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
     508              :     {
     509       195324 :       scalar_float_mode mode = float_mode_iter.require ();
     510       195324 :       const struct real_format *fmt = REAL_MODE_FORMAT (mode);
     511       195324 :       int kind;
     512              : 
     513       195324 :       if (fmt == NULL)
     514       195324 :         continue;
     515       195324 :       if (!targetm.scalar_mode_supported_p (mode))
     516            0 :         continue;
     517              : 
     518      1367268 :       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       195324 :       if (!targetm.libgcc_floating_mode_supported_p (mode))
     526            0 :         continue;
     527       195324 :       if (mode != TYPE_MODE (float_type_node)
     528       162770 :             && (mode != TYPE_MODE (double_type_node))
     529       130216 :             && (mode != TYPE_MODE (long_double_type_node))
     530              : #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
     531       292986 :             && (mode != TFmode)
     532              : #endif
     533              :            )
     534        65108 :         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       130216 :       kind = (GET_MODE_PRECISION (mode) + 7) / 8;
     557              : 
     558       130216 :       if (kind == 4)
     559              :         saw_r4 = true;
     560        97662 :       if (kind == 8)
     561              :         saw_r8 = true;
     562        97662 :       if (kind == 10)
     563              :         saw_r10 = true;
     564        97662 :       if (kind == 16)
     565              :         {
     566        32554 :           saw_r16 = true;
     567        32554 :           r16_mode = mode;
     568              :         }
     569              : 
     570              :       /* Careful we don't stumble a weird internal mode.  */
     571       130216 :       gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
     572              :       /* Or have too many modes for the allocated space.  */
     573        97662 :       gcc_assert (r_index != MAX_REAL_KINDS);
     574              : 
     575       130216 :       gfc_real_kinds[r_index].kind = kind;
     576       130216 :       gfc_real_kinds[r_index].abi_kind = kind;
     577       130216 :       gfc_real_kinds[r_index].radix = fmt->b;
     578       130216 :       gfc_real_kinds[r_index].digits = fmt->p;
     579       130216 :       gfc_real_kinds[r_index].min_exponent = fmt->emin;
     580       130216 :       gfc_real_kinds[r_index].max_exponent = fmt->emax;
     581       130216 :       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       130216 :       gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
     591       130216 :       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        32554 :   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        32554 :   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        32554 :   gfc_numeric_storage_size = 4 * 8;
     630              : 
     631        32554 :   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        32463 :   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        32463 :   else if (saw_i4)
     649              :     {
     650        32463 :       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        32554 :   gfc_default_unsigned_kind = gfc_default_integer_kind;
     659              : 
     660              :   /* Choose the default real kind.  Again, we choose 4 when possible.  */
     661        32554 :   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        32552 :   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        32546 :   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        32540 :   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        32516 :   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        32492 :   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        32468 :   else if (saw_r4)
     710        32468 :     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        32554 :   if (flag_default_double && saw_r8)
     719            0 :     gfc_default_double_kind = 8;
     720        32554 :   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        32540 :   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        32516 :   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        32492 :   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        32468 :   else if (saw_r4 && saw_r8)
     757        32468 :     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        32554 :   gfc_default_logical_kind = gfc_default_integer_kind;
     778        32554 :   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        32554 :   i_index = 0;
     784        65108 :   if ((kind = get_int_kind_from_width (8)) > 0)
     785              :     {
     786        32554 :       gfc_character_kinds[i_index].kind = kind;
     787        32554 :       gfc_character_kinds[i_index].bit_size = 8;
     788        32554 :       gfc_character_kinds[i_index].name = "ascii";
     789        32554 :       i_index++;
     790              :     }
     791        65108 :   if ((kind = get_int_kind_from_width (32)) > 0)
     792              :     {
     793        32554 :       gfc_character_kinds[i_index].kind = kind;
     794        32554 :       gfc_character_kinds[i_index].bit_size = 32;
     795        32554 :       gfc_character_kinds[i_index].name = "iso_10646";
     796        32554 :       i_index++;
     797              :     }
     798              : 
     799              :   /* Choose the smallest integer kind for our default character.  */
     800        32554 :   gfc_default_character_kind = gfc_character_kinds[0].kind;
     801        32554 :   gfc_character_storage_size = gfc_default_character_kind * 8;
     802              : 
     803        32963 :   gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
     804              : 
     805        32554 :   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        32554 :   gfc_c_int_kind = INT_TYPE_SIZE / 8;
     810              : 
     811              :   /* UNSIGNED has the same as INT.  */
     812        32554 :   gfc_c_uint_kind = gfc_c_int_kind;
     813              : 
     814              :   /* Choose atomic kinds to match C's int.  */
     815        32554 :   gfc_atomic_int_kind = gfc_c_int_kind;
     816        32554 :   gfc_atomic_logical_kind = gfc_c_int_kind;
     817              : 
     818        32554 :   gfc_c_intptr_kind = POINTER_SIZE / 8;
     819        32554 : }
     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     83107047 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     831     83104980 :     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      4542510 :   for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
     843      1636320 :     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      5439678 :   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
     855      5439667 :     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      1994541 :   for (i = 0; gfc_logical_kinds[i].kind; i++)
     867      1994533 :     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      1425111 :   for (i = 0; gfc_character_kinds[i].kind; i++)
     879      1425097 :     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     34960364 : gfc_validate_kind (bt type, int kind, bool may_fail)
     891              : {
     892     34960364 :   int rc;
     893              : 
     894     34960364 :   switch (type)
     895              :     {
     896              :     case BT_REAL:               /* Fall through */
     897              :     case BT_COMPLEX:
     898     34960364 :       rc = validate_real (kind);
     899              :       break;
     900              :     case BT_INTEGER:
     901     34960364 :       rc = validate_integer (kind);
     902              :       break;
     903              :     case BT_UNSIGNED:
     904     34960364 :       rc = validate_unsigned (kind);
     905              :       break;
     906              :     case BT_LOGICAL:
     907     34960364 :       rc = validate_logical (kind);
     908              :       break;
     909              :     case BT_CHARACTER:
     910     34960364 :       rc = validate_character (kind);
     911              :       break;
     912              : 
     913            0 :     default:
     914            0 :       gfc_internal_error ("gfc_validate_kind(): Got bad type");
     915              :     }
     916              : 
     917     34960364 :   if (rc < 0 && !may_fail)
     918            0 :     gfc_internal_error ("gfc_validate_kind(): Got bad kind");
     919              : 
     920     34960364 :   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       162361 : gfc_build_int_type (gfc_integer_info *info)
     931              : {
     932       162361 :   int mode_precision = info->bit_size;
     933              : 
     934       162361 :   if (mode_precision == CHAR_TYPE_SIZE)
     935        32554 :     info->c_char = 1;
     936       162361 :   if (mode_precision == SHORT_TYPE_SIZE)
     937        32554 :     info->c_short = 1;
     938       162361 :   if (mode_precision == INT_TYPE_SIZE)
     939        32554 :     info->c_int = 1;
     940       163997 :   if (mode_precision == LONG_TYPE_SIZE)
     941        32554 :     info->c_long = 1;
     942       162361 :   if (mode_precision == LONG_LONG_TYPE_SIZE)
     943        32554 :     info->c_long_long = 1;
     944              : 
     945       162361 :   if (TYPE_PRECISION (intQI_type_node) == mode_precision)
     946              :     return intQI_type_node;
     947       129807 :   if (TYPE_PRECISION (intHI_type_node) == mode_precision)
     948              :     return intHI_type_node;
     949        97253 :   if (TYPE_PRECISION (intSI_type_node) == mode_precision)
     950              :     return intSI_type_node;
     951        64699 :   if (TYPE_PRECISION (intDI_type_node) == mode_precision)
     952              :     return intDI_type_node;
     953        32145 :   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        66592 : gfc_build_uint_type (int size)
     961              : {
     962        66592 :   if (size == CHAR_TYPE_SIZE)
     963        32680 :     return unsigned_char_type_node;
     964        33912 :   if (size == SHORT_TYPE_SIZE)
     965          372 :     return short_unsigned_type_node;
     966        33540 :   if (size == INT_TYPE_SIZE)
     967        32788 :     return unsigned_type_node;
     968          752 :   if (size == LONG_TYPE_SIZE)
     969          372 :     return long_unsigned_type_node;
     970          380 :   if (size == LONG_LONG_TYPE_SIZE)
     971            0 :     return long_long_unsigned_type_node;
     972              : 
     973          380 :   return make_unsigned_type (size);
     974              : }
     975              : 
     976              : static tree
     977          738 : gfc_build_unsigned_type (gfc_unsigned_info *info)
     978              : {
     979          738 :   int mode_precision = info->bit_size;
     980              : 
     981          738 :   if (mode_precision == CHAR_TYPE_SIZE)
     982            0 :     info->c_unsigned_char = 1;
     983          738 :   if (mode_precision == SHORT_TYPE_SIZE)
     984          246 :     info->c_unsigned_short = 1;
     985          738 :   if (mode_precision == INT_TYPE_SIZE)
     986            0 :     info->c_unsigned_int = 1;
     987          738 :   if (mode_precision == LONG_TYPE_SIZE)
     988          246 :     info->c_unsigned_long = 1;
     989          738 :   if (mode_precision == LONG_LONG_TYPE_SIZE)
     990          246 :     info->c_unsigned_long_long = 1;
     991              : 
     992          738 :   return gfc_build_uint_type (mode_precision);
     993              : }
     994              : 
     995              : static tree
     996       130216 : gfc_build_real_type (gfc_real_info *info)
     997              : {
     998       130216 :   int mode_precision = info->mode_precision;
     999       130216 :   tree new_type;
    1000              : 
    1001       130216 :   if (mode_precision == TYPE_PRECISION (float_type_node))
    1002        32554 :     info->c_float = 1;
    1003       130216 :   if (mode_precision == TYPE_PRECISION (double_type_node))
    1004        32554 :     info->c_double = 1;
    1005       130216 :   if (mode_precision == TYPE_PRECISION (long_double_type_node)
    1006       130216 :       && !info->c_float128)
    1007        32554 :     info->c_long_double = 1;
    1008       130216 :   if (mode_precision != TYPE_PRECISION (long_double_type_node)
    1009       130216 :       && mode_precision == 128)
    1010              :     {
    1011              :       /* TODO: see PR101835.  */
    1012        32554 :       info->c_float128 = 1;
    1013        32554 :       gfc_real16_is_float128 = true;
    1014        32554 :       if (TARGET_GLIBC_MAJOR > 2
    1015              :           || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26))
    1016              :         {
    1017        32554 :           info->use_iec_60559 = 1;
    1018        32554 :           gfc_real16_use_iec_60559 = true;
    1019              :         }
    1020              :     }
    1021              : 
    1022       130216 :   if (TYPE_PRECISION (float_type_node) == mode_precision)
    1023              :     return float_type_node;
    1024        97662 :   if (TYPE_PRECISION (double_type_node) == mode_precision)
    1025              :     return double_type_node;
    1026        65108 :   if (TYPE_PRECISION (long_double_type_node) == mode_precision)
    1027              :     return long_double_type_node;
    1028              : 
    1029        32554 :   new_type = make_node (REAL_TYPE);
    1030        32554 :   TYPE_PRECISION (new_type) = mode_precision;
    1031        32554 :   layout_type (new_type);
    1032        32554 :   return new_type;
    1033              : }
    1034              : 
    1035              : static tree
    1036       130216 : gfc_build_complex_type (tree scalar_type)
    1037              : {
    1038       130216 :   tree new_type;
    1039              : 
    1040       130216 :   if (scalar_type == NULL)
    1041              :     return NULL;
    1042       130216 :   if (scalar_type == float_type_node)
    1043        32554 :     return complex_float_type_node;
    1044        97662 :   if (scalar_type == double_type_node)
    1045        32554 :     return complex_double_type_node;
    1046        65108 :   if (scalar_type == long_double_type_node)
    1047        32554 :     return complex_long_double_type_node;
    1048              : 
    1049        32554 :   new_type = make_node (COMPLEX_TYPE);
    1050        32554 :   TREE_TYPE (new_type) = scalar_type;
    1051        32554 :   layout_type (new_type);
    1052        32554 :   return new_type;
    1053              : }
    1054              : 
    1055              : static tree
    1056       162361 : gfc_build_logical_type (gfc_logical_info *info)
    1057              : {
    1058       162361 :   int bit_size = info->bit_size;
    1059       162361 :   tree new_type;
    1060              : 
    1061       162361 :   if (bit_size == BOOL_TYPE_SIZE)
    1062              :     {
    1063        32554 :       info->c_bool = 1;
    1064        32554 :       return boolean_type_node;
    1065              :     }
    1066              : 
    1067       129807 :   new_type = make_unsigned_type (bit_size);
    1068       129807 :   TREE_SET_CODE (new_type, BOOLEAN_TYPE);
    1069       129807 :   TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
    1070       129807 :   TYPE_PRECISION (new_type) = 1;
    1071              : 
    1072       129807 :   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        32554 : gfc_init_types (void)
    1083              : {
    1084        32554 :   char name_buf[26];
    1085        32554 :   int index;
    1086        32554 :   tree type;
    1087        32554 :   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       194915 :   for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
    1095              :     {
    1096       162361 :       type = gfc_build_int_type (&gfc_integer_kinds[index]);
    1097              :       /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set.  */
    1098       162361 :       if (TYPE_STRING_FLAG (type))
    1099        32554 :         type = make_signed_type (gfc_integer_kinds[index].bit_size);
    1100       162361 :       gfc_integer_types[index] = type;
    1101       162361 :       snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
    1102              :                 gfc_integer_kinds[index].kind);
    1103       162361 :       PUSH_TYPE (name_buf, type);
    1104              :     }
    1105              : 
    1106       194915 :   for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
    1107              :     {
    1108       162361 :       type = gfc_build_logical_type (&gfc_logical_kinds[index]);
    1109       162361 :       gfc_logical_types[index] = type;
    1110       162361 :       snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
    1111              :                 gfc_logical_kinds[index].kind);
    1112       162361 :       PUSH_TYPE (name_buf, type);
    1113              :     }
    1114              : 
    1115       162770 :   for (index = 0; gfc_real_kinds[index].kind != 0; index++)
    1116              :     {
    1117       130216 :       type = gfc_build_real_type (&gfc_real_kinds[index]);
    1118       130216 :       gfc_real_types[index] = type;
    1119       130216 :       snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
    1120              :                 gfc_real_kinds[index].kind);
    1121       130216 :       PUSH_TYPE (name_buf, type);
    1122              : 
    1123       130216 :       if (gfc_real_kinds[index].c_float128)
    1124        32554 :         gfc_float128_type_node = type;
    1125              : 
    1126       130216 :       type = gfc_build_complex_type (type);
    1127       130216 :       gfc_complex_types[index] = type;
    1128       130216 :       snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
    1129              :                 gfc_real_kinds[index].kind);
    1130       130216 :       PUSH_TYPE (name_buf, type);
    1131              : 
    1132       130216 :       if (gfc_real_kinds[index].c_float128)
    1133        32554 :         gfc_complex_float128_type_node = type;
    1134              :     }
    1135              : 
    1136        97662 :   for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
    1137              :     {
    1138        65108 :       type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
    1139        65108 :       type = build_qualified_type (type, TYPE_UNQUALIFIED);
    1140        65108 :       TYPE_STRING_FLAG (type) = 1;
    1141        65108 :       snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
    1142              :                 gfc_character_kinds[index].kind);
    1143        65108 :       PUSH_TYPE (name_buf, type);
    1144        65108 :       gfc_character_types[index] = type;
    1145        65108 :       gfc_pcharacter_types[index] = build_pointer_type (type);
    1146              :     }
    1147        32554 :   gfc_character1_type_node = gfc_character_types[0];
    1148              : 
    1149        32554 :   if (flag_unsigned)
    1150              :     {
    1151         1476 :       for (index = 0; gfc_unsigned_kinds[index].kind != 0;++index)
    1152              :         {
    1153         2952 :           int index_char = -1;
    1154         2952 :           for (int i=0; gfc_character_kinds[i].kind != 0; i++)
    1155              :             {
    1156         2214 :               if (gfc_character_kinds[i].bit_size
    1157         2214 :                   == gfc_unsigned_kinds[index].bit_size)
    1158              :                 {
    1159              :                   index_char = i;
    1160              :                   break;
    1161              :                 }
    1162              :             }
    1163         1230 :           if (index_char > -1)
    1164              :             {
    1165          492 :               type = gfc_character_types[index_char];
    1166          492 :               if (TYPE_STRING_FLAG (type))
    1167              :                 {
    1168          492 :                   type = build_distinct_type_copy (type);
    1169          984 :                   TYPE_CANONICAL (type)
    1170          492 :                     = TYPE_CANONICAL (gfc_character_types[index_char]);
    1171              :                 }
    1172              :               else
    1173            0 :                 type = build_variant_type_copy (type);
    1174          492 :               TYPE_NAME (type) = NULL_TREE;
    1175          492 :               TYPE_STRING_FLAG (type) = 0;
    1176              :             }
    1177              :           else
    1178          738 :             type = gfc_build_unsigned_type (&gfc_unsigned_kinds[index]);
    1179         1230 :           gfc_unsigned_types[index] = type;
    1180         1230 :           snprintf (name_buf, sizeof(name_buf), "unsigned(kind=%d)",
    1181              :                     gfc_integer_kinds[index].kind);
    1182         1230 :           PUSH_TYPE (name_buf, type);
    1183              :         }
    1184              :     }
    1185              : 
    1186        32554 :   PUSH_TYPE ("byte", unsigned_char_type_node);
    1187        32554 :   PUSH_TYPE ("void", void_type_node);
    1188              : 
    1189              :   /* DBX debugging output gets upset if these aren't set.  */
    1190        32554 :   if (!TYPE_NAME (integer_type_node))
    1191            0 :     PUSH_TYPE ("c_integer", integer_type_node);
    1192        32554 :   if (!TYPE_NAME (char_type_node))
    1193        32554 :     PUSH_TYPE ("c_char", char_type_node);
    1194              : 
    1195              : #undef PUSH_TYPE
    1196              : 
    1197        32554 :   pvoid_type_node = build_pointer_type (void_type_node);
    1198        32554 :   prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
    1199        32554 :   ppvoid_type_node = build_pointer_type (pvoid_type_node);
    1200        32554 :   pchar_type_node = build_pointer_type (gfc_character1_type_node);
    1201        32554 :   pfunc_type_node
    1202        32554 :     = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
    1203              : 
    1204        32554 :   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        32554 :   gfc_array_range_type
    1208        32554 :           = 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        32554 :   n = TYPE_PRECISION (size_type_node);
    1217        32554 :   gfc_max_array_element_size
    1218        32554 :     = wide_int_to_tree (size_type_node,
    1219        32554 :                         wi::mask (n, UNSIGNED,
    1220        32554 :                                   TYPE_PRECISION (size_type_node)));
    1221              : 
    1222        32554 :   logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
    1223        32554 :   logical_true_node = build_int_cst (logical_type_node, 1);
    1224        32554 :   logical_false_node = build_int_cst (logical_type_node, 0);
    1225              : 
    1226              :   /* Character lengths are of type size_t, except signed.  */
    1227        32554 :   gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
    1228        32554 :   gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
    1229              : 
    1230        32554 :   gfc_array_dim_rank_type
    1231        32554 :                 = 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        32554 :   gfc_size_kind = get_int_kind_from_node (size_type_node);
    1239        32554 : }
    1240              : 
    1241              : /* Get the type node for the given type and kind.  */
    1242              : 
    1243              : tree
    1244      5914163 : gfc_get_int_type (int kind)
    1245              : {
    1246      5914163 :   int index = gfc_validate_kind (BT_INTEGER, kind, true);
    1247      5914163 :   return index < 0 ? 0 : gfc_integer_types[index];
    1248              : }
    1249              : 
    1250              : tree
    1251      3088775 : gfc_get_unsigned_type (int kind)
    1252              : {
    1253      3088775 :   int index = gfc_validate_kind (BT_UNSIGNED, kind, true);
    1254      3088775 :   return index < 0 ? 0 : gfc_unsigned_types[index];
    1255              : }
    1256              : 
    1257              : tree
    1258       795807 : gfc_get_real_type (int kind)
    1259              : {
    1260       795807 :   int index = gfc_validate_kind (BT_REAL, kind, true);
    1261       795807 :   return index < 0 ? 0 : gfc_real_types[index];
    1262              : }
    1263              : 
    1264              : tree
    1265       482241 : gfc_get_complex_type (int kind)
    1266              : {
    1267       482241 :   int index = gfc_validate_kind (BT_COMPLEX, kind, true);
    1268       482241 :   return index < 0 ? 0 : gfc_complex_types[index];
    1269              : }
    1270              : 
    1271              : tree
    1272       598234 : gfc_get_logical_type (int kind)
    1273              : {
    1274       598234 :   int index = gfc_validate_kind (BT_LOGICAL, kind, true);
    1275       598234 :   return index < 0 ? 0 : gfc_logical_types[index];
    1276              : }
    1277              : 
    1278              : tree
    1279       439988 : gfc_get_char_type (int kind)
    1280              : {
    1281       439988 :   int index = gfc_validate_kind (BT_CHARACTER, kind, true);
    1282       439988 :   return index < 0 ? 0 : gfc_character_types[index];
    1283              : }
    1284              : 
    1285              : tree
    1286       168463 : gfc_get_pchar_type (int kind)
    1287              : {
    1288       168463 :   int index = gfc_validate_kind (BT_CHARACTER, kind, true);
    1289       168463 :   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       104136 : gfc_get_character_type_len_for_eltype (tree eltype, tree len)
    1297              : {
    1298       104136 :   tree bounds, type;
    1299              : 
    1300       104136 :   bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
    1301       104136 :   type = build_array_type (eltype, bounds);
    1302       104136 :   TYPE_STRING_FLAG (type) = 1;
    1303              : 
    1304       104136 :   return type;
    1305              : }
    1306              : 
    1307              : tree
    1308        86288 : gfc_get_character_type_len (int kind, tree len)
    1309              : {
    1310        86288 :   gfc_validate_kind (BT_CHARACTER, kind, false);
    1311        86288 :   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        75385 : gfc_get_character_type (int kind, gfc_charlen * cl)
    1319              : {
    1320        75385 :   tree len;
    1321              : 
    1322        75385 :   len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
    1323        74194 :   if (len && POINTER_TYPE_P (TREE_TYPE (len)))
    1324            0 :     len = build_fold_indirect_ref (len);
    1325              : 
    1326        75385 :   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      1310633 : gfc_typenode_for_spec (gfc_typespec * spec, int codim)
    1333              : {
    1334      1310633 :   tree basetype;
    1335              : 
    1336      1310633 :   switch (spec->type)
    1337              :     {
    1338            0 :     case BT_UNKNOWN:
    1339            0 :       gcc_unreachable ();
    1340              : 
    1341       479574 :     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       479574 :       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       479230 :         basetype = gfc_get_int_type (spec->kind);
    1355              :       break;
    1356              : 
    1357         2779 :     case BT_UNSIGNED:
    1358         2779 :       basetype = gfc_get_unsigned_type (spec->kind);
    1359         2779 :       break;
    1360              : 
    1361       144898 :     case BT_REAL:
    1362       144898 :       basetype = gfc_get_real_type (spec->kind);
    1363       144898 :       break;
    1364              : 
    1365        26175 :     case BT_COMPLEX:
    1366        26175 :       basetype = gfc_get_complex_type (spec->kind);
    1367        26175 :       break;
    1368              : 
    1369       427643 :     case BT_LOGICAL:
    1370       427643 :       basetype = gfc_get_logical_type (spec->kind);
    1371       427643 :       break;
    1372              : 
    1373        62926 :     case BT_CHARACTER:
    1374        62926 :       basetype = gfc_get_character_type (spec->kind, spec->u.cl);
    1375        62926 :       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       162738 :     case BT_DERIVED:
    1388       162738 :     case BT_CLASS:
    1389       162738 :       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       162738 :       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         3734 :     case BT_VOID:
    1404         3734 :     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         3734 :       basetype = ptr_type_node;
    1408         3734 :       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      1310633 :   return basetype;
    1424              : }
    1425              : 
    1426              : /* Build an INT_CST for constant expressions, otherwise return NULL_TREE.  */
    1427              : 
    1428              : static tree
    1429       122624 : gfc_conv_array_bound (gfc_expr * expr)
    1430              : {
    1431              :   /* If expr is an integer constant, return that.  */
    1432       122624 :   if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
    1433        15621 :     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       343299 : gfc_get_element_type (tree type)
    1445              : {
    1446       343299 :   tree element;
    1447              : 
    1448       343299 :   if (GFC_ARRAY_TYPE_P (type))
    1449              :     {
    1450       130071 :       if (TREE_CODE (type) == POINTER_TYPE)
    1451        21608 :         type = TREE_TYPE (type);
    1452       130071 :       if (GFC_TYPE_ARRAY_RANK (type) == 0)
    1453              :         {
    1454          630 :           gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
    1455              :           element = type;
    1456              :         }
    1457              :       else
    1458              :         {
    1459       129441 :           gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    1460       129441 :           element = TREE_TYPE (type);
    1461              :         }
    1462              :     }
    1463              :   else
    1464              :     {
    1465       213228 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
    1466       213228 :       element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
    1467              : 
    1468       213228 :       gcc_assert (TREE_CODE (element) == POINTER_TYPE);
    1469       213228 :       element = TREE_TYPE (element);
    1470              : 
    1471              :       /* For arrays, which are not scalar coarrays.  */
    1472       213228 :       if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
    1473       211544 :         element = TREE_TYPE (element);
    1474              :     }
    1475              : 
    1476       343299 :   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       113371 : gfc_is_nodesc_array (gfc_symbol * sym)
    1558              : {
    1559       113371 :   symbol_attribute *array_attr;
    1560       113371 :   gfc_array_spec *as;
    1561       113371 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    1562              : 
    1563       113371 :   array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
    1564       113371 :   as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
    1565              : 
    1566       113371 :   gcc_assert (array_attr->dimension || array_attr->codimension);
    1567              : 
    1568              :   /* We only want local arrays.  */
    1569       113371 :   if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    1570       105921 :       || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
    1571       105921 :       || 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        93438 :   if (sym->assoc && as->type != AS_EXPLICIT)
    1577              :     return 0;
    1578              : 
    1579              :   /* The dummy is stored in sym and not in the component.  */
    1580        91602 :   if (sym->attr.dummy)
    1581        41161 :     return as->type != AS_ASSUMED_SHAPE
    1582        41161 :         && as->type != AS_ASSUMED_RANK;
    1583              : 
    1584        50441 :   if (sym->attr.result || sym->attr.function)
    1585              :     return 0;
    1586              : 
    1587        40471 :   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        54271 : 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        54271 :   tree lbound[GFC_MAX_DIMENSIONS];
    1601        54271 :   tree ubound[GFC_MAX_DIMENSIONS];
    1602        54271 :   int n, corank;
    1603              : 
    1604              :   /* Assumed-shape arrays do not have codimension information stored in the
    1605              :      descriptor.  */
    1606        54271 :   corank = MAX (as->corank, codim);
    1607        54271 :   if (as->type == AS_ASSUMED_SHAPE ||
    1608         8077 :       (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
    1609        54271 :     corank = codim;
    1610              : 
    1611        54271 :   if (as->type == AS_ASSUMED_RANK)
    1612       129232 :     for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    1613              :       {
    1614       121155 :         lbound[n] = NULL_TREE;
    1615       121155 :         ubound[n] = NULL_TREE;
    1616              :       }
    1617              : 
    1618       123302 :   for (n = 0; n < as->rank; n++)
    1619              :     {
    1620              :       /* Create expressions for the known bounds of the array.  */
    1621        69031 :       if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
    1622        16741 :         lbound[n] = gfc_index_one_node;
    1623              :       else
    1624        52290 :         lbound[n] = gfc_conv_array_bound (as->lower[n]);
    1625        69031 :       ubound[n] = gfc_conv_array_bound (as->upper[n]);
    1626              :     }
    1627              : 
    1628        55363 :   for (n = as->rank; n < as->rank + corank; n++)
    1629              :     {
    1630         1092 :       if (as->type != AS_DEFERRED && as->lower[n] == NULL)
    1631           18 :         lbound[n] = gfc_index_one_node;
    1632              :       else
    1633         1074 :         lbound[n] = gfc_conv_array_bound (as->lower[n]);
    1634              : 
    1635         1092 :       if (n < as->rank + corank - 1)
    1636          229 :         ubound[n] = gfc_conv_array_bound (as->upper[n]);
    1637              :     }
    1638              : 
    1639        54271 :   if (as->type == AS_ASSUMED_SHAPE)
    1640        17283 :     akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
    1641              :                        : GFC_ARRAY_ASSUMED_SHAPE;
    1642        36988 :   else if (as->type == AS_ASSUMED_RANK)
    1643              :     {
    1644         8077 :       if (akind == GFC_ARRAY_ALLOCATABLE)
    1645              :         akind = GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE;
    1646         7696 :       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         7270 :         akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
    1651              :                            : GFC_ARRAY_ASSUMED_RANK;
    1652              :     }
    1653       100465 :   return gfc_get_array_type_bounds (type, as->rank == -1
    1654              :                                           ? GFC_MAX_DIMENSIONS : as->rank,
    1655              :                                     corank, lbound, ubound, 0, akind,
    1656        54271 :                                     restricted);
    1657              : }
    1658              : 
    1659              : /* Returns the struct descriptor_dimension type.  */
    1660              : 
    1661              : static tree
    1662        33088 : gfc_get_desc_dim_type (void)
    1663              : {
    1664        33088 :   tree type;
    1665        33088 :   tree decl, *chain = NULL;
    1666              : 
    1667        33088 :   if (gfc_desc_dim_type)
    1668              :     return gfc_desc_dim_type;
    1669              : 
    1670              :   /* Build the type node.  */
    1671        12524 :   type = make_node (RECORD_TYPE);
    1672              : 
    1673        12524 :   TYPE_NAME (type) = get_identifier ("descriptor_dimension");
    1674        12524 :   TYPE_PACKED (type) = 1;
    1675              : 
    1676              :   /* Consists of the stride, lbound and ubound members.  */
    1677        12524 :   decl = gfc_add_field_to_struct_1 (type,
    1678              :                                     get_identifier ("stride"),
    1679              :                                     gfc_array_index_type, &chain);
    1680        12524 :   suppress_warning (decl);
    1681              : 
    1682        12524 :   decl = gfc_add_field_to_struct_1 (type,
    1683              :                                     get_identifier ("lbound"),
    1684              :                                     gfc_array_index_type, &chain);
    1685        12524 :   suppress_warning (decl);
    1686              : 
    1687        12524 :   decl = gfc_add_field_to_struct_1 (type,
    1688              :                                     get_identifier ("ubound"),
    1689              :                                     gfc_array_index_type, &chain);
    1690        12524 :   suppress_warning (decl);
    1691              : 
    1692              :   /* Finish off the type.  */
    1693        12524 :   gfc_finish_type (type);
    1694        12524 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
    1695              : 
    1696        12524 :   gfc_desc_dim_type = type;
    1697        12524 :   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       147326 : gfc_get_dtype_rank_type (int rank, tree etype)
    1708              : {
    1709       147326 :   tree ptype;
    1710       147326 :   tree size;
    1711       147326 :   int n;
    1712              : 
    1713       147326 :   ptype = etype;
    1714       147326 :   while (TREE_CODE (etype) == POINTER_TYPE
    1715       179153 :          || TREE_CODE (etype) == ARRAY_TYPE)
    1716              :     {
    1717        31827 :       ptype = etype;
    1718        31827 :       etype = TREE_TYPE (etype);
    1719              :     }
    1720              : 
    1721       147326 :   gcc_assert (etype);
    1722              : 
    1723       147326 :   switch (TREE_CODE (etype))
    1724              :     {
    1725        86379 :     case INTEGER_TYPE:
    1726        86379 :       if (TREE_CODE (ptype) == ARRAY_TYPE
    1727        86379 :           && TYPE_STRING_FLAG (ptype))
    1728              :         n = BT_CHARACTER;
    1729              :       else
    1730              :         {
    1731        62596 :           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        22720 :     case RECORD_TYPE:
    1751        22720 :       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        23783 :   switch (n)
    1769              :     {
    1770        23783 :     case BT_CHARACTER:
    1771        23783 :       gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
    1772        23783 :       size = gfc_get_character_len_in_bytes (ptype);
    1773        23783 :       break;
    1774         2203 :     case BT_VOID:
    1775         2203 :       gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
    1776         2203 :       size = size_in_bytes (ptype);
    1777         2203 :       break;
    1778       121340 :     default:
    1779       121340 :       size = size_in_bytes (etype);
    1780       121340 :       break;
    1781              :     }
    1782              : 
    1783       147326 :   return gfc_build_dtype_constructor (size, n, rank);
    1784              : }
    1785              : 
    1786              : 
    1787              : tree
    1788       118840 : gfc_get_dtype (tree type, int * rank)
    1789              : {
    1790       118840 :   tree dtype;
    1791       118840 :   tree etype;
    1792       118840 :   int irnk;
    1793              : 
    1794       118840 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
    1795              : 
    1796       118840 :   irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
    1797       118840 :   etype = gfc_get_element_type (type);
    1798       118840 :   dtype = gfc_get_dtype_rank_type (irnk, etype);
    1799              : 
    1800       118840 :   GFC_TYPE_ARRAY_DTYPE (type) = dtype;
    1801       118840 :   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       119144 : gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
    1810              :                            bool restricted)
    1811              : {
    1812       119144 :   tree range;
    1813       119144 :   tree type;
    1814       119144 :   tree tmp;
    1815       119144 :   int n;
    1816       119144 :   int known_stride;
    1817       119144 :   int known_offset;
    1818       119144 :   mpz_t offset;
    1819       119144 :   mpz_t stride;
    1820       119144 :   mpz_t delta;
    1821       119144 :   gfc_expr *expr;
    1822              : 
    1823       119144 :   mpz_init_set_ui (offset, 0);
    1824       119144 :   mpz_init_set_ui (stride, 1);
    1825       119144 :   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       119144 :   if (as->rank)
    1831       117166 :     type = make_node (ARRAY_TYPE);
    1832              :   else
    1833         1978 :     type = build_variant_type_copy (etype);
    1834              : 
    1835       119144 :   GFC_ARRAY_TYPE_P (type) = 1;
    1836       119144 :   TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
    1837              : 
    1838       119144 :   known_stride = (packed != PACKED_NO);
    1839       119144 :   known_offset = 1;
    1840       258413 :   for (n = 0; n < as->rank; n++)
    1841              :     {
    1842              :       /* Fill in the stride and bound components of the type.  */
    1843       139269 :       if (known_stride)
    1844       125262 :         tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
    1845              :       else
    1846              :         tmp = NULL_TREE;
    1847       139269 :       GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
    1848              : 
    1849       139269 :       expr = as->lower[n];
    1850       139269 :       if (expr && expr->expr_type == EXPR_CONSTANT)
    1851              :         {
    1852       138479 :           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       139269 :       GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
    1861              : 
    1862       139269 :       if (known_stride)
    1863              :         {
    1864              :           /* Calculate the offset.  */
    1865       124808 :           mpz_mul (delta, stride, as->lower[n]->value.integer);
    1866       124808 :           mpz_sub (offset, offset, delta);
    1867              :         }
    1868              :       else
    1869              :         known_offset = 0;
    1870              : 
    1871       139269 :       expr = as->upper[n];
    1872       139269 :       if (expr && expr->expr_type == EXPR_CONSTANT)
    1873              :         {
    1874       111076 :           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       139269 :       GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    1883              : 
    1884       139269 :       if (known_stride)
    1885              :         {
    1886              :           /* Calculate the stride.  */
    1887       110120 :           mpz_sub (delta, as->upper[n]->value.integer,
    1888       110120 :                    as->lower[n]->value.integer);
    1889       110120 :           mpz_add_ui (delta, delta, 1);
    1890       110120 :           mpz_mul (stride, stride, delta);
    1891              :         }
    1892              : 
    1893              :       /* Only the first stride is known for partial packed arrays.  */
    1894       139269 :       if (packed == PACKED_NO || packed == PACKED_PARTIAL)
    1895        11050 :         known_stride = 0;
    1896              :     }
    1897       121771 :   for (n = as->rank; n < as->rank + as->corank; n++)
    1898              :     {
    1899         2627 :       expr = as->lower[n];
    1900         2627 :       if (expr && expr->expr_type == EXPR_CONSTANT)
    1901         2513 :         tmp = gfc_conv_mpz_to_tree (expr->value.integer,
    1902              :                                     gfc_index_integer_kind);
    1903              :       else
    1904              :         tmp = NULL_TREE;
    1905         2627 :       GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
    1906              : 
    1907         2627 :       expr = as->upper[n];
    1908         2627 :       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         2627 :       if (n < as->rank + as->corank - 1)
    1914          277 :         GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    1915              :     }
    1916              : 
    1917       119144 :   if (known_offset)
    1918              :     {
    1919       108195 :       GFC_TYPE_ARRAY_OFFSET (type) =
    1920       108195 :         gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
    1921              :     }
    1922              :   else
    1923        10949 :     GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
    1924              : 
    1925       119144 :   if (known_stride)
    1926              :     {
    1927        88326 :       GFC_TYPE_ARRAY_SIZE (type) =
    1928        88326 :         gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
    1929              :     }
    1930              :   else
    1931        30818 :     GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
    1932              : 
    1933       119144 :   GFC_TYPE_ARRAY_RANK (type) = as->rank;
    1934       119144 :   GFC_TYPE_ARRAY_CORANK (type) = as->corank;
    1935       119144 :   GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
    1936       119144 :   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       119144 :   GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
    1940       119144 :     build_pointer_type (build_array_type (etype, range));
    1941       119144 :   if (restricted)
    1942       115746 :     GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
    1943       115746 :       build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
    1944              :                             TYPE_QUAL_RESTRICT);
    1945              : 
    1946       119144 :   if (as->rank == 0)
    1947              :     {
    1948         1978 :       if (packed != PACKED_STATIC  || flag_coarray == GFC_FCOARRAY_LIB)
    1949              :         {
    1950         1895 :           type = build_pointer_type (type);
    1951              : 
    1952         1895 :           if (restricted)
    1953         1895 :             type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
    1954              : 
    1955         1895 :           GFC_ARRAY_TYPE_P (type) = 1;
    1956         1895 :           TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
    1957              :         }
    1958              : 
    1959         1978 :       goto array_type_done;
    1960              :     }
    1961              : 
    1962       117166 :   if (known_stride)
    1963              :     {
    1964        86389 :       mpz_sub_ui (stride, stride, 1);
    1965        86389 :       range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
    1966              :     }
    1967              :   else
    1968              :     range = NULL_TREE;
    1969              : 
    1970       117166 :   range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
    1971       117166 :   TYPE_DOMAIN (type) = range;
    1972              : 
    1973       117166 :   build_pointer_type (etype);
    1974       117166 :   TREE_TYPE (type) = etype;
    1975              : 
    1976       117166 :   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       117166 :   if (known_offset)
    1982              :     {
    1983       106217 :       tree gtype = etype, rtype, type_decl;
    1984              : 
    1985       228487 :       for (n = as->rank - 1; n >= 0; n--)
    1986              :         {
    1987       489080 :           rtype = build_range_type (gfc_array_index_type,
    1988       122270 :                                     GFC_TYPE_ARRAY_LBOUND (type, n),
    1989       122270 :                                     GFC_TYPE_ARRAY_UBOUND (type, n));
    1990       122270 :           gtype = build_array_type (gtype, rtype);
    1991              :         }
    1992       106217 :       TYPE_NAME (type) = type_decl = build_decl (input_location,
    1993              :                                                  TYPE_DECL, NULL, gtype);
    1994       106217 :       DECL_ORIGINAL_TYPE (type_decl) = gtype;
    1995              :     }
    1996              : 
    1997       117166 :   if (packed != PACKED_STATIC || !known_stride
    1998        82060 :       || (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        35222 :       type = build_pointer_type (type);
    2003        35222 :       if (restricted)
    2004        33874 :         type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
    2005        35222 :       GFC_ARRAY_TYPE_P (type) = 1;
    2006        35222 :       TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
    2007              :     }
    2008              : 
    2009        81944 : array_type_done:
    2010       119144 :   mpz_clear (offset);
    2011       119144 :   mpz_clear (stride);
    2012       119144 :   mpz_clear (delta);
    2013              : 
    2014       119144 :   return type;
    2015              : }
    2016              : 
    2017              : 
    2018              : /* Return or create the base type for an array descriptor.  */
    2019              : 
    2020              : static tree
    2021       310618 : gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
    2022              : {
    2023       310618 :   tree fat_type, decl, arraytype, *chain = NULL;
    2024       310618 :   char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
    2025       310618 :   int idx;
    2026              : 
    2027              :   /* Assumed-rank array.  */
    2028       310618 :   if (dimen == -1)
    2029            0 :     dimen = GFC_MAX_DIMENSIONS;
    2030              : 
    2031       310618 :   idx = 2 * (codimen + dimen) + restricted;
    2032              : 
    2033       310618 :   gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
    2034              : 
    2035       310618 :   if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
    2036              :     {
    2037         2372 :       if (gfc_array_descriptor_base_caf[idx])
    2038              :         return gfc_array_descriptor_base_caf[idx];
    2039              :     }
    2040       308246 :   else if (gfc_array_descriptor_base[idx])
    2041              :     return gfc_array_descriptor_base[idx];
    2042              : 
    2043              :   /* Build the type node.  */
    2044        36084 :   fat_type = make_node (RECORD_TYPE);
    2045              : 
    2046        36084 :   sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
    2047        36084 :   TYPE_NAME (fat_type) = get_identifier (name);
    2048        36084 :   TYPE_NAMELESS (fat_type) = 1;
    2049              : 
    2050              :   /* Add the data member as the first element of the descriptor.  */
    2051        36084 :   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        36084 :   decl = gfc_add_field_to_struct_1 (fat_type,
    2059              :                                     get_identifier ("offset"),
    2060              :                                     gfc_array_index_type, &chain);
    2061        36084 :   suppress_warning (decl);
    2062              : 
    2063              :   /* Add the dtype component.  */
    2064        36084 :   decl = gfc_add_field_to_struct_1 (fat_type,
    2065              :                                     get_identifier ("dtype"),
    2066              :                                     get_dtype_type_node (), &chain);
    2067        36084 :   suppress_warning (decl);
    2068              : 
    2069              :   /* Add the span component.  */
    2070        36084 :   decl = gfc_add_field_to_struct_1 (fat_type,
    2071              :                                     get_identifier ("span"),
    2072              :                                     gfc_array_index_type, &chain);
    2073        36084 :   suppress_warning (decl);
    2074              : 
    2075              :   /* Build the array type for the stride and bound components.  */
    2076        36084 :   if (dimen + codimen > 0)
    2077              :     {
    2078        33088 :       arraytype =
    2079        33088 :         build_array_type (gfc_get_desc_dim_type (),
    2080              :                           build_range_type (gfc_array_index_type,
    2081              :                                             gfc_index_zero_node,
    2082        33088 :                                             gfc_rank_cst[codimen + dimen - 1]));
    2083              : 
    2084        33088 :       decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
    2085              :                                         arraytype, &chain);
    2086        33088 :       suppress_warning (decl);
    2087              :     }
    2088              : 
    2089        36084 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    2090              :     {
    2091         1772 :       decl = gfc_add_field_to_struct_1 (fat_type,
    2092              :                                         get_identifier ("token"),
    2093              :                                         prvoid_type_node, &chain);
    2094         1772 :       suppress_warning (decl);
    2095              :     }
    2096              : 
    2097              :   /* Finish off the type.  */
    2098        36084 :   gfc_finish_type (fat_type);
    2099        36084 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
    2100              : 
    2101        36084 :   if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
    2102          962 :     gfc_array_descriptor_base_caf[idx] = fat_type;
    2103              :   else
    2104        35122 :     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       155309 : 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       155309 :   char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
    2118       155309 :   tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
    2119       155309 :   const char *type_name;
    2120       155309 :   int n;
    2121              : 
    2122       155309 :   base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
    2123       155309 :   fat_type = build_distinct_type_copy (base_type);
    2124              :   /* Unshare TYPE_FIELDs.  */
    2125       930170 :   for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
    2126              :     {
    2127       774861 :       tree next = DECL_CHAIN (*tp);
    2128       774861 :       *tp = copy_node (*tp);
    2129       774861 :       DECL_CONTEXT (*tp) = fat_type;
    2130       774861 :       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       155309 :   base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
    2135       155309 :   TYPE_CANONICAL (fat_type) = base_type;
    2136       155309 :   TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
    2137              :   /* Arrays of unknown type must alias with all array descriptors.  */
    2138       155309 :   TYPE_TYPELESS_STORAGE (base_type) = 1;
    2139       155309 :   TYPE_TYPELESS_STORAGE (fat_type) = 1;
    2140       155309 :   gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
    2141              : 
    2142       155309 :   tmp = etype;
    2143       155309 :   if (TREE_CODE (tmp) == ARRAY_TYPE
    2144       155309 :       && TYPE_STRING_FLAG (tmp))
    2145        24335 :     tmp = TREE_TYPE (etype);
    2146       155309 :   tmp = TYPE_NAME (tmp);
    2147       155309 :   if (tmp && TREE_CODE (tmp) == TYPE_DECL)
    2148       127634 :     tmp = DECL_NAME (tmp);
    2149       127634 :   if (tmp)
    2150       151589 :     type_name = IDENTIFIER_POINTER (tmp);
    2151              :   else
    2152              :     type_name = "unknown";
    2153       155309 :   sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
    2154              :            GFC_MAX_SYMBOL_LEN, type_name);
    2155       155309 :   TYPE_NAME (fat_type) = get_identifier (name);
    2156       155309 :   TYPE_NAMELESS (fat_type) = 1;
    2157              : 
    2158       155309 :   GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
    2159       155309 :   TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
    2160              : 
    2161       155309 :   GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
    2162       155309 :   GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
    2163       155309 :   GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
    2164       155309 :   GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
    2165              : 
    2166              :   /* Build an array descriptor record type.  */
    2167       155309 :   if (packed != 0)
    2168        37281 :     stride = gfc_index_one_node;
    2169              :   else
    2170              :     stride = NULL_TREE;
    2171       485135 :   for (n = 0; n < dimen + codimen; n++)
    2172              :     {
    2173       331820 :       if (n < dimen)
    2174       328664 :         GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
    2175              : 
    2176       331820 :       if (lbound)
    2177       331820 :         lower = lbound[n];
    2178              :       else
    2179              :         lower = NULL_TREE;
    2180              : 
    2181       331820 :       if (lower != NULL_TREE)
    2182              :         {
    2183       171135 :           if (INTEGER_CST_P (lower))
    2184       170018 :             GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
    2185              :           else
    2186              :             lower = NULL_TREE;
    2187              :         }
    2188              : 
    2189       331820 :       if (codimen && n == dimen + codimen - 1)
    2190              :         break;
    2191              : 
    2192       329826 :       upper = ubound[n];
    2193       329826 :       if (upper != NULL_TREE)
    2194              :         {
    2195       137159 :           if (INTEGER_CST_P (upper))
    2196       103280 :             GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
    2197              :           else
    2198              :             upper = NULL_TREE;
    2199              :         }
    2200              : 
    2201       329826 :       if (n >= dimen)
    2202         1162 :         continue;
    2203              : 
    2204       328664 :       if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
    2205              :         {
    2206        29242 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2207              :                                  gfc_array_index_type, upper, lower);
    2208        29242 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2209              :                                  gfc_array_index_type, tmp,
    2210              :                                  gfc_index_one_node);
    2211        29242 :           stride = fold_build2_loc (input_location, MULT_EXPR,
    2212              :                                     gfc_array_index_type, tmp, stride);
    2213              :           /* Check the folding worked.  */
    2214        29242 :           gcc_assert (INTEGER_CST_P (stride));
    2215              :         }
    2216              :       else
    2217              :         stride = NULL_TREE;
    2218              :     }
    2219       155309 :   GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
    2220              : 
    2221              :   /* TODO: known offsets for descriptors.  */
    2222       155309 :   GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
    2223              : 
    2224       155309 :   if (dimen == 0)
    2225              :     {
    2226         7866 :       arraytype =  build_pointer_type (etype);
    2227         7866 :       if (restricted)
    2228         7122 :         arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
    2229              : 
    2230         7866 :       GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
    2231         7866 :       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       147443 :   if (stride)
    2237        22872 :     rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
    2238              :                               int_const_binop (MINUS_EXPR, stride,
    2239        45744 :                                                build_int_cst (TREE_TYPE (stride), 1)));
    2240              :   else
    2241       124571 :     rtype = gfc_array_range_type;
    2242       147443 :   arraytype = build_array_type (etype, rtype);
    2243       147443 :   arraytype = build_pointer_type (arraytype);
    2244       147443 :   if (restricted)
    2245        70778 :     arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
    2246       147443 :   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       147443 :   {
    2253       147443 :     struct array_descr_info info;
    2254       147443 :     gfc_get_array_descr_info (fat_type, &info);
    2255       147443 :     gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
    2256              :   }
    2257              : 
    2258       147443 :   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         7131 : gfc_get_scalar_to_descriptor_type (tree scalar_type, symbol_attribute attr)
    2270              : {
    2271         7131 :   enum gfc_array_kind akind;
    2272              : 
    2273         7131 :   if (attr.pointer)
    2274              :     akind = GFC_ARRAY_POINTER_CONT;
    2275         6869 :   else if (attr.allocatable)
    2276              :     akind = GFC_ARRAY_ALLOCATABLE;
    2277              :   else
    2278         5384 :     akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
    2279              : 
    2280         7131 :   if (POINTER_TYPE_P (scalar_type))
    2281         6152 :     scalar_type = TREE_TYPE (scalar_type);
    2282              : 
    2283         7131 :   tree *lbound = NULL, *ubound = NULL;
    2284         7131 :   int codim = 0;
    2285         7131 :   if (TYPE_LANG_SPECIFIC (scalar_type))
    2286              :     {
    2287         5827 :       struct lang_type *lang_specific = TYPE_LANG_SPECIFIC (scalar_type);
    2288         5827 :       codim = lang_specific->corank;
    2289         5827 :       lbound = lang_specific->lbound;
    2290         5827 :       ubound = lang_specific->ubound;
    2291              :     }
    2292         7488 :   return gfc_get_array_type_bounds (scalar_type, 0, codim, lbound, ubound, 1,
    2293         7131 :                                     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        17445 : 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        17445 :     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         4654 : mirror_fields (tree to, tree from)
    2316              : {
    2317         4654 :   tree fto, ffrom;
    2318         4654 :   tree *chain;
    2319              : 
    2320              :   /* Forward to the end of TOs fields.  */
    2321         4654 :   fto = TYPE_FIELDS (to);
    2322         4654 :   ffrom = TYPE_FIELDS (from);
    2323         4654 :   chain = &TYPE_FIELDS (to);
    2324         4654 :   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        22192 :   for (; ffrom; ffrom = DECL_CHAIN (ffrom))
    2334              :     {
    2335        17538 :       tree newfield = copy_node (ffrom);
    2336        17538 :       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        17538 :       DECL_CHAIN (newfield) = NULL_TREE;
    2343        17538 :       *chain = newfield;
    2344        17538 :       chain = &DECL_CHAIN (newfield);
    2345              : 
    2346        17538 :       if (TREE_CODE (ffrom) == FIELD_DECL)
    2347              :         {
    2348        17538 :           tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
    2349        17538 :           TREE_TYPE (newfield) = elemtype;
    2350              :         }
    2351              :     }
    2352         4654 :   *chain = NULL_TREE;
    2353         4654 : }
    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       278059 : gfc_nonrestricted_type (tree t)
    2360              : {
    2361       278059 :   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       278059 :   if (!TYPE_SIZE (t))
    2366              :     return t;
    2367              : 
    2368       265525 :   if (!TYPE_LANG_SPECIFIC (t))
    2369       107374 :     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       265525 :   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       257740 :   if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
    2379       144879 :     return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
    2380              : 
    2381              :   /* Mark this type.  */
    2382       112861 :   TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
    2383              : 
    2384       112861 :   switch (TREE_CODE (t))
    2385              :     {
    2386              :       default:
    2387              :         break;
    2388              : 
    2389        40770 :       case POINTER_TYPE:
    2390        40770 :       case REFERENCE_TYPE:
    2391        40770 :         {
    2392        40770 :           tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
    2393        40770 :           if (totype == TREE_TYPE (t))
    2394              :             ret = t;
    2395         1667 :           else if (TREE_CODE (t) == POINTER_TYPE)
    2396         1667 :             ret = build_pointer_type (totype);
    2397              :           else
    2398            0 :             ret = build_reference_type (totype);
    2399        81540 :           ret = build_qualified_type (ret,
    2400        40770 :                                       TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
    2401              :         }
    2402        40770 :         break;
    2403              : 
    2404         6737 :       case ARRAY_TYPE:
    2405         6737 :         {
    2406         6737 :           tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
    2407         6737 :           if (elemtype == TREE_TYPE (t))
    2408              :             ret = t;
    2409              :           else
    2410              :             {
    2411           35 :               ret = build_variant_type_copy (t);
    2412           35 :               TREE_TYPE (ret) = elemtype;
    2413           35 :               if (TYPE_LANG_SPECIFIC (t)
    2414           35 :                   && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
    2415              :                 {
    2416           35 :                   tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
    2417           35 :                   dataptr_type = gfc_nonrestricted_type (dataptr_type);
    2418           35 :                   if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
    2419              :                     {
    2420           35 :                       TYPE_LANG_SPECIFIC (ret)
    2421           35 :                         = ggc_cleared_alloc<struct lang_type> ();
    2422           35 :                       *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
    2423           35 :                       GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
    2424              :                     }
    2425              :                 }
    2426              :             }
    2427              :         }
    2428              :         break;
    2429              : 
    2430        31132 :       case RECORD_TYPE:
    2431        31132 :       case UNION_TYPE:
    2432        31132 :       case QUAL_UNION_TYPE:
    2433        31132 :         {
    2434        31132 :           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       153539 :           for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
    2446       127061 :             if (TREE_CODE (field) == FIELD_DECL)
    2447              :               {
    2448       127061 :                 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
    2449       127061 :                 if (elemtype != TREE_TYPE (field))
    2450              :                   break;
    2451              :               }
    2452        31132 :           if (!field)
    2453              :             break;
    2454         4654 :           ret = build_variant_type_copy (t);
    2455         4654 :           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         4654 :           TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
    2462         4654 :           mirror_fields (ret, t);
    2463              :         }
    2464         4654 :         break;
    2465              :     }
    2466              : 
    2467       112861 :   TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
    2468       112861 :   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       430679 : gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
    2481              : {
    2482       430679 :   tree type;
    2483       430679 :   int byref;
    2484       430679 :   bool restricted;
    2485              : 
    2486              :   /* Procedure Pointers inside COMMON blocks.  */
    2487       430679 :   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       430649 :   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       430649 :   if (sym->backend_decl && !sym->attr.function)
    2503          493 :     return TREE_TYPE (sym->backend_decl);
    2504              : 
    2505       430156 :   if (sym->attr.result
    2506         8808 :       && sym->ts.type == BT_CHARACTER
    2507         1240 :       && sym->ts.u.cl->backend_decl == NULL_TREE
    2508          526 :       && sym->ns->proc_name
    2509          520 :       && sym->ns->proc_name->ts.u.cl
    2510          518 :       && 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       430156 :   if (sym->ts.type == BT_CHARACTER
    2514       430156 :       && ((sym->attr.function && sym->attr.is_bind_c)
    2515        43156 :           || ((sym->attr.result || sym->attr.value)
    2516         1788 :               && sym->ns->proc_name
    2517         1782 :               && sym->ns->proc_name->attr.is_bind_c)
    2518        42894 :           || (sym->ts.deferred
    2519         4725 :               && (!sym->ts.u.cl
    2520         4725 :                   || !sym->ts.u.cl->backend_decl
    2521         3474 :                   || sym->attr.save))
    2522        41442 :           || (sym->attr.dummy
    2523        19304 :               && sym->attr.value
    2524          288 :               && gfc_length_one_character_type_p (&sym->ts))))
    2525         1931 :     type = gfc_get_char_type (sym->ts.kind);
    2526              :   else
    2527       428225 :     type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
    2528              : 
    2529       430156 :   if (sym->attr.dummy && !sym->attr.function
    2530       168016 :       && (!sym->attr.value
    2531        11843 :           || sym->attr.dimension
    2532        11699 :           || (sym->ts.type == BT_CHARACTER
    2533          500 :               && (!sym->ts.u.cl || !sym->ts.u.cl->length
    2534          452 :                   || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)))
    2535       156389 :       && !sym->pass_as_value)
    2536              :     byref = 1;
    2537              :   else
    2538       275083 :     byref = 0;
    2539              : 
    2540       399672 :   restricted = (!sym->attr.target && !IS_POINTER (sym)
    2541       811086 :                 && !IS_PROC_POINTER (sym) && !sym->attr.cray_pointee);
    2542        49975 :   if (!restricted)
    2543        49975 :     type = gfc_nonrestricted_type (type);
    2544              : 
    2545              :   /* Dummy argument to a bind(C) procedure.  */
    2546       430156 :   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       426515 :   else if (sym->attr.dimension || sym->attr.codimension)
    2550              :     {
    2551       101826 :       if (gfc_is_nodesc_array (sym))
    2552              :         {
    2553              :           /* If this is a character argument of unknown length, just use the
    2554              :              base type.  */
    2555        55230 :           if (sym->ts.type != BT_CHARACTER
    2556         5975 :               || !(sym->attr.dummy || sym->attr.function)
    2557         1960 :               || sym->ts.u.cl->backend_decl)
    2558              :             {
    2559        54802 :               type = gfc_get_nodesc_array_type (type, sym->as,
    2560              :                                                 byref ? PACKED_FULL
    2561              :                                                       : PACKED_STATIC,
    2562              :                                                 restricted);
    2563        54802 :               byref = 0;
    2564              :             }
    2565              :         }
    2566              :       else
    2567              :         {
    2568        46596 :           enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
    2569        46596 :           if (sym->attr.pointer)
    2570         7450 :             akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
    2571              :                                          : GFC_ARRAY_POINTER;
    2572        39146 :           else if (sym->attr.allocatable)
    2573        12399 :             akind = GFC_ARRAY_ALLOCATABLE;
    2574        46596 :           type = gfc_build_array_type (type, sym->as, akind, restricted,
    2575        46596 :                                        sym->attr.contiguous, sym->as->corank);
    2576              :         }
    2577              :     }
    2578              :   else
    2579              :     {
    2580       320234 :       if (sym->attr.allocatable || sym->attr.pointer
    2581       635227 :           || gfc_is_associate_pointer (sym))
    2582        17445 :         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       430156 :   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       140777 :       if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
    2593       140777 :           || sym->attr.optional
    2594       121940 :           || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
    2595        20505 :         type = build_pointer_type (type);
    2596              :       else
    2597       120272 :         type = build_reference_type (type);
    2598              : 
    2599       140777 :       if (restricted)
    2600       133109 :         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       350506 : gfc_finish_type (tree type)
    2610              : {
    2611       350506 :   tree decl;
    2612              : 
    2613       350506 :   decl = build_decl (input_location,
    2614              :                      TYPE_DECL, NULL_TREE, type);
    2615       350506 :   TYPE_STUB_DECL (type) = decl;
    2616       350506 :   layout_type (type);
    2617       350506 :   rest_of_type_compilation (type, 1);
    2618       350506 :   rest_of_decl_compilation (decl, 1, 0);
    2619       350506 : }
    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      5162441 : gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
    2629              : {
    2630      5162441 :   tree decl = build_decl (input_location, FIELD_DECL, name, type);
    2631              : 
    2632      5162441 :   DECL_CONTEXT (decl) = context;
    2633      5162441 :   DECL_CHAIN (decl) = NULL_TREE;
    2634      5162441 :   if (TYPE_FIELDS (context) == NULL_TREE)
    2635       343063 :     TYPE_FIELDS (context) = decl;
    2636      5162441 :   if (chain != NULL)
    2637              :     {
    2638      5162441 :       if (*chain != NULL)
    2639      4819378 :         **chain = decl;
    2640      5162441 :       *chain = &DECL_CHAIN (decl);
    2641              :     }
    2642              : 
    2643      5162441 :   return decl;
    2644              : }
    2645              : 
    2646              : /* Like `gfc_add_field_to_struct_1', but adds alignment
    2647              :    information.  */
    2648              : 
    2649              : tree
    2650      4770421 : gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
    2651              : {
    2652      4770421 :   tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
    2653              : 
    2654      4770421 :   DECL_INITIAL (decl) = 0;
    2655      4770421 :   SET_DECL_ALIGN (decl, 0);
    2656      4770421 :   DECL_USER_ALIGN (decl) = 0;
    2657              : 
    2658      4770421 :   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       364765 : gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
    2668              :                            bool from_gsym)
    2669              : {
    2670       364765 :   gfc_component *to_cm;
    2671       364765 :   gfc_component *from_cm;
    2672              : 
    2673       364765 :   if (from == to)
    2674              :     return 1;
    2675              : 
    2676       319236 :   if (from->backend_decl == NULL
    2677       319236 :         || !gfc_compare_derived_types (from, to))
    2678              :     return 0;
    2679              : 
    2680        15785 :   to->backend_decl = from->backend_decl;
    2681              : 
    2682        15785 :   to_cm = to->components;
    2683        15785 :   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        59574 :   for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
    2692              :     {
    2693        43789 :       to_cm->backend_decl = from_cm->backend_decl;
    2694        43789 :       to_cm->caf_token = from_cm->caf_token;
    2695        43789 :       if (from_cm->ts.type == BT_UNION)
    2696           28 :         gfc_get_union_type (to_cm->ts.u.derived);
    2697        43761 :       else if (from_cm->ts.type == BT_DERIVED
    2698        15171 :           && (!from_cm->attr.pointer || from_gsym))
    2699        13805 :         gfc_get_derived_type (to_cm->ts.u.derived);
    2700        29956 :       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        29169 :       else if (from_cm->ts.type == BT_CHARACTER)
    2704          900 :         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        33442 : gfc_get_ppc_type (gfc_component* c)
    2715              : {
    2716        33442 :   tree t;
    2717              : 
    2718              :   /* Explicit interface.  */
    2719        33442 :   if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
    2720         3620 :     return build_pointer_type (gfc_get_function_type (c->ts.interface));
    2721              : 
    2722              :   /* Implicit interface (only return value may be known).  */
    2723        29822 :   if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
    2724            9 :     t = gfc_typenode_for_spec (&c->ts);
    2725              :   else
    2726        29813 :     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        29822 :   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       199703 : gfc_get_derived_type (gfc_symbol * derived, int codimen)
    2844              : {
    2845       199703 :   tree typenode = NULL, field = NULL, field_type = NULL;
    2846       199703 :   tree canonical = NULL_TREE;
    2847       199703 :   tree *chain = NULL;
    2848       199703 :   bool got_canonical = false;
    2849       199703 :   bool unlimited_entity = false;
    2850       199703 :   gfc_component *c;
    2851       199703 :   gfc_namespace *ns;
    2852       199703 :   tree tmp;
    2853       199703 :   bool coarray_flag, class_coarray_flag;
    2854              : 
    2855       399406 :   coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
    2856       199703 :                  && derived->module && !derived->attr.vtype;
    2857       399406 :   class_coarray_flag = derived->components
    2858       186922 :                        && derived->components->ts.type == BT_DERIVED
    2859        62388 :                        && strcmp (derived->components->name, "_data") == 0
    2860        35947 :                        && derived->components->attr.codimension
    2861       200382 :                        && derived->components->as->cotype == AS_EXPLICIT;
    2862              : 
    2863       199703 :   gcc_assert (!derived->attr.pdt_template);
    2864              : 
    2865       199703 :   if (derived->attr.unlimited_polymorphic
    2866       195754 :       || (flag_coarray == GFC_FCOARRAY_LIB
    2867         4110 :           && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    2868          189 :           && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
    2869              :               || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
    2870          189 :               || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
    2871         4138 :     return ptr_type_node;
    2872              : 
    2873       195565 :   if (flag_coarray != GFC_FCOARRAY_LIB
    2874       191644 :       && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    2875          448 :       && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
    2876          448 :           || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
    2877          331 :     return gfc_get_int_type (gfc_default_integer_kind);
    2878              : 
    2879       195234 :   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       195234 :   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       182651 :   if (derived->backend_decl == NULL
    2906        45538 :       && (derived->attr.use_assoc || derived->attr.used_in_submodule)
    2907        11968 :       && derived->module
    2908       194619 :       && gfc_get_module_backend_decl (derived))
    2909        11460 :     goto copy_derived_types;
    2910              : 
    2911              :   /* The derived types from an earlier namespace can be used as the
    2912              :      canonical type.  */
    2913       171191 :   if (derived->backend_decl == NULL
    2914        34078 :       && !derived->attr.use_assoc
    2915        33591 :       && !derived->attr.used_in_submodule
    2916        33570 :       && gfc_global_ns_list)
    2917              :     {
    2918         8869 :       for (ns = gfc_global_ns_list;
    2919        42433 :            ns->translated && !got_canonical;
    2920         8869 :            ns = ns->sibling)
    2921              :         {
    2922         8869 :           if (ns->derived_types)
    2923              :             {
    2924        32001 :               for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
    2925              :                    dt = dt->dt_next)
    2926              :                 {
    2927        31744 :                   gfc_copy_dt_decls_ifequal (dt, derived, true);
    2928        31744 :                   if (derived->backend_decl)
    2929          376 :                     got_canonical = true;
    2930        31744 :                   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        33564 :   if (got_canonical)
    2939              :     {
    2940          376 :       if (TYPE_CANONICAL (derived->backend_decl))
    2941          376 :         canonical = TYPE_CANONICAL (derived->backend_decl);
    2942              :       else
    2943              :         canonical = derived->backend_decl;
    2944              : 
    2945          376 :       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       171191 :   if (derived->backend_decl
    2951       171191 :       && (!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       137045 :       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        34146 :       typenode = make_node (RECORD_TYPE);
    2985        34146 :       TYPE_NAME (typenode) = get_identifier (derived->name);
    2986        34146 :       TYPE_PACKED (typenode) = flag_pack_derived;
    2987        34146 :       derived->backend_decl = typenode;
    2988        34146 :       if (derived->attr.is_class)
    2989         7978 :         GFC_CLASS_TYPE_P (typenode) = 1;
    2990              :     }
    2991              : 
    2992        39246 :   if (derived->components
    2993        31808 :       && derived->components->ts.type == BT_DERIVED
    2994        11448 :       && startswith (derived->name, "__class")
    2995         8030 :       && strcmp (derived->components->name, "_data") == 0
    2996        47276 :       && 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       156512 :   for (c = derived->components; c; c = c->next)
    3005              :     {
    3006       117266 :       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       117266 :       if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
    3010        76582 :         continue;
    3011              : 
    3012        40684 :       const bool incomplete_type
    3013        40684 :         = c->ts.u.derived->backend_decl
    3014        33695 :           && TREE_CODE (c->ts.u.derived->backend_decl) == RECORD_TYPE
    3015        72977 :           && !(TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)
    3016        19098 :                && TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)->size);
    3017        81368 :       const bool pointer_component
    3018        40684 :         = 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        40684 :       if (c->ts.u.derived->backend_decl == NULL
    3024        33695 :           || (c->attr.codimension && c->as->corank != codimen)
    3025        33398 :           || !(incomplete_type && pointer_component))
    3026              :         {
    3027         9744 :           int local_codim = c->attr.codimension ? c->as->corank: codimen;
    3028         9744 :           c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
    3029              :                                                                 local_codim);
    3030              :         }
    3031              : 
    3032        40684 :       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        39246 :   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       156298 :   for (c = derived->components; c; c = c->next)
    3059              :     {
    3060       234254 :       bool same_alloc_type = c->attr.allocatable
    3061       117127 :                              && 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       117127 :       if (c->attr.proc_pointer
    3066        33486 :           && (c->ts.type != BT_DERIVED || (c->ts.u.derived
    3067          215 :                     && !gfc_compare_derived_types (derived, c->ts.u.derived)))
    3068       150589 :           && (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        33442 :         field_type = gfc_get_ppc_type (c);
    3071        83685 :       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        83641 :       else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    3077        39997 :         field_type = c->ts.u.derived->backend_decl;
    3078        43644 :       else if (c->attr.caf_token)
    3079          661 :         field_type = pvoid_type_node;
    3080              :       else
    3081              :         {
    3082        42983 :           if (c->ts.type == BT_CHARACTER
    3083         2023 :               && !c->ts.deferred && !c->attr.pdt_string)
    3084              :             {
    3085              :               /* Evaluate the string length.  */
    3086         1482 :               gfc_conv_const_charlen (c->ts.u.cl);
    3087         1482 :               gcc_assert (c->ts.u.cl->backend_decl);
    3088              :             }
    3089        41501 :           else if (c->ts.type == BT_CHARACTER)
    3090          541 :             c->ts.u.cl->backend_decl
    3091          541 :                         = build_int_cst (gfc_charlen_type_node, 0);
    3092              : 
    3093        42983 :           field_type = gfc_typenode_for_spec (&c->ts, codimen);
    3094              :         }
    3095              : 
    3096              :       /* This returns an array descriptor type.  Initialization may be
    3097              :          required.  */
    3098       117127 :       if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
    3099              :         {
    3100         9522 :           if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
    3101              :             {
    3102         7675 :               enum gfc_array_kind akind;
    3103         7675 :               bool is_ptr = ((c == derived->components
    3104         5052 :                               && derived->components->ts.type == BT_DERIVED
    3105         3725 :                               && startswith (derived->name, "__class")
    3106         3112 :                               && (strcmp (derived->components->name, "_data")
    3107              :                                   == 0))
    3108        12727 :                              ? c->attr.class_pointer : c->attr.pointer);
    3109         7675 :               if (is_ptr)
    3110         1940 :                 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
    3111              :                                            : GFC_ARRAY_POINTER;
    3112         5735 :               else if (c->attr.allocatable)
    3113              :                 akind = GFC_ARRAY_ALLOCATABLE;
    3114         1457 :               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         1301 :                 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         7675 :               field_type = gfc_build_array_type
    3127        10697 :                 (
    3128              :                   field_type, c->as, akind, !c->attr.target && !c->attr.pointer,
    3129              :                   c->attr.contiguous,
    3130         7675 :                   c->attr.codimension || c->attr.pointer ? codimen : 0
    3131              :                 );
    3132         7675 :             }
    3133              :           else
    3134         1847 :             field_type = gfc_get_nodesc_array_type (field_type, c->as,
    3135              :                                                     PACKED_STATIC,
    3136              :                                                     !c->attr.target);
    3137              :         }
    3138       107605 :       else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
    3139        35681 :                && !c->attr.proc_pointer
    3140        35515 :                && !(unlimited_entity && c == derived->components))
    3141        34944 :         field_type = build_pointer_type (field_type);
    3142              : 
    3143       117127 :       if (c->attr.pointer || same_alloc_type)
    3144        35943 :         field_type = gfc_nonrestricted_type (field_type);
    3145              : 
    3146              :       /* vtype fields can point to different types to the base type.  */
    3147       117127 :       if (c->ts.type == BT_DERIVED
    3148        39348 :             && c->ts.u.derived && c->ts.u.derived->attr.vtype)
    3149        17256 :           field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
    3150              :                                                     ptr_mode, true);
    3151              : 
    3152       117127 :       field = gfc_add_field_to_struct (typenode,
    3153              :                                        get_identifier (c->name),
    3154              :                                        field_type, &chain);
    3155       117127 :       if (GFC_LOCUS_IS_SET (c->loc))
    3156       117127 :         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       117127 :       gfc_finish_decl_attrs (field, &c->attr);
    3161              : 
    3162       117127 :       DECL_PACKED (field) |= TYPE_PACKED (typenode);
    3163              : 
    3164       117127 :       gcc_assert (field);
    3165              :       /* Overwrite for class array to supply different bounds for different
    3166              :          types.  */
    3167       117127 :       if (class_coarray_flag || !c->backend_decl || c->attr.caf_token)
    3168       116045 :         c->backend_decl = field;
    3169              : 
    3170       117127 :       if ((c->attr.dimension || c->attr.codimension)
    3171         9634 :           && ((derived->attr.is_class && c->attr.class_pointer)
    3172         8927 :               || (!derived->attr.is_class && c->attr.pointer)))
    3173         1950 :         GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
    3174              :     }
    3175              : 
    3176              :   /* Now lay out the derived type, including the fields.  */
    3177        39171 :   if (canonical)
    3178          376 :     TYPE_CANONICAL (typenode) = canonical;
    3179              : 
    3180        39171 :   gfc_finish_type (typenode);
    3181        39171 :   gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
    3182        39171 :   if (derived->module && derived->ns->proc_name
    3183        21357 :       && derived->ns->proc_name->attr.flavor == FL_MODULE)
    3184              :     {
    3185        20016 :       if (derived->ns->proc_name->backend_decl
    3186        20001 :           && TREE_CODE (derived->ns->proc_name->backend_decl)
    3187              :              == NAMESPACE_DECL)
    3188              :         {
    3189        20001 :           TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
    3190        20001 :           DECL_CONTEXT (TYPE_STUB_DECL (typenode))
    3191        40002 :             = derived->ns->proc_name->backend_decl;
    3192              :         }
    3193              :     }
    3194              : 
    3195        39171 :   derived->backend_decl = typenode;
    3196              : 
    3197        50631 : copy_derived_types:
    3198              : 
    3199        50631 :   if (!derived->attr.vtype)
    3200        95771 :     for (c = derived->components; c; c = c->next)
    3201              :       {
    3202              :         /* Do not add a caf_token field for class container components.  */
    3203        57701 :         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       320775 :   for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
    3219              :     {
    3220       319752 :       gfc_copy_dt_decls_ifequal (derived, dt, false);
    3221       319752 :       if (dt->dt_next == gfc_derived_types)
    3222              :         break;
    3223              :     }
    3224              : 
    3225        50631 :   if (derived->attr.is_class)
    3226        10519 :     GFC_CLASS_TYPE_P (derived->backend_decl) = 1;
    3227              : 
    3228        50631 :   return derived->backend_decl;
    3229              : }
    3230              : 
    3231              : 
    3232              : bool
    3233       986883 : gfc_return_by_reference (gfc_symbol * sym)
    3234              : {
    3235       986883 :   if (!sym->attr.function)
    3236              :     return 0;
    3237              : 
    3238       497542 :   if (sym->attr.dimension)
    3239              :     return 1;
    3240              : 
    3241       422002 :   if (sym->ts.type == BT_CHARACTER
    3242        23643 :       && !sym->attr.is_bind_c
    3243        22941 :       && (!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       399061 :   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       113113 : create_fn_spec (gfc_symbol *sym, tree fntype)
    3327              : {
    3328       113113 :   char spec[150];
    3329       113113 :   size_t spec_len;
    3330       113113 :   gfc_formal_arglist *f;
    3331       113113 :   tree tmp;
    3332              : 
    3333       113113 :   memset (&spec, 0, sizeof (spec));
    3334       113113 :   spec[0] = '.';
    3335       113113 :   spec[1] = ' ';
    3336       113113 :   spec_len = 2;
    3337              : 
    3338       113113 :   if (sym->attr.entry_master)
    3339              :     {
    3340          667 :       spec[spec_len++] = 'R';
    3341          667 :       spec[spec_len++] = ' ';
    3342              :     }
    3343       113113 :   if (gfc_return_by_reference (sym))
    3344              :     {
    3345        10786 :       gfc_symbol *result = sym->result ? sym->result : sym;
    3346              : 
    3347        10786 :       if (result->attr.pointer || sym->attr.proc_pointer)
    3348              :         {
    3349          424 :           spec[spec_len++] = '.';
    3350          424 :           spec[spec_len++] = ' ';
    3351              :         }
    3352              :       else
    3353              :         {
    3354        10362 :           spec[spec_len++] = 'w';
    3355        10362 :           spec[spec_len++] = ' ';
    3356              :         }
    3357        10786 :       if (sym->ts.type == BT_CHARACTER)
    3358              :         {
    3359         2960 :           if (!sym->ts.u.cl->length
    3360         1573 :               && (sym->attr.allocatable || sym->attr.pointer))
    3361          318 :             spec[spec_len++] = 'w';
    3362              :           else
    3363         2642 :             spec[spec_len++] = 'R';
    3364         2960 :           spec[spec_len++] = ' ';
    3365              :         }
    3366              :     }
    3367              : 
    3368       265876 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    3369       152763 :     if (spec_len < sizeof (spec))
    3370              :       {
    3371       152763 :         bool is_class = false;
    3372       152763 :         bool is_pointer = false;
    3373              : 
    3374       152763 :         if (f->sym)
    3375              :           {
    3376        10186 :             is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
    3377       162845 :               && f->sym->attr.class_ok;
    3378       152659 :             is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
    3379       142473 :                                   : f->sym->attr.pointer;
    3380              :           }
    3381              : 
    3382       152763 :         if (f->sym == NULL || is_pointer || f->sym->attr.target
    3383       145316 :             || f->sym->attr.external || f->sym->attr.cray_pointer
    3384       144803 :             || (f->sym->ts.type == BT_DERIVED
    3385        28315 :                 && (f->sym->ts.u.derived->attr.proc_pointer_comp
    3386        27640 :                     || f->sym->ts.u.derived->attr.pointer_comp))
    3387       141778 :             || (is_class
    3388         9200 :                 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
    3389         8539 :                     || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
    3390       140385 :             || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
    3391              :           {
    3392        20534 :             spec[spec_len++] = '.';
    3393        20534 :             spec[spec_len++] = ' ';
    3394              :           }
    3395       132229 :         else if (f->sym->attr.intent == INTENT_IN)
    3396              :           {
    3397        63164 :             spec[spec_len++] = 'r';
    3398        63164 :             spec[spec_len++] = ' ';
    3399              :           }
    3400        69065 :         else if (f->sym)
    3401              :           {
    3402        69065 :             spec[spec_len++] = 'w';
    3403        69065 :             spec[spec_len++] = ' ';
    3404              :           }
    3405              :       }
    3406              : 
    3407       113113 :   tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
    3408       113113 :   tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
    3409       113113 :   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       115266 : gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
    3418              :                        const char *fnspec)
    3419              : {
    3420       115266 :   tree type;
    3421       115266 :   vec<tree, va_gc> *typelist = NULL;
    3422       115266 :   vec<tree, va_gc> *hidden_typelist = NULL;
    3423       115266 :   gfc_formal_arglist *f;
    3424       115266 :   gfc_symbol *arg;
    3425       115266 :   int alternate_return = 0;
    3426       115266 :   bool is_varargs = true;
    3427              : 
    3428              :   /* Make sure this symbol is a function, a subroutine or the main
    3429              :      program.  */
    3430       115266 :   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       115266 :   if (sym->backend_decl == NULL)
    3436       115009 :     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       115009 :   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       115009 :   if (sym->result)
    3449        34497 :     arg = sym->result;
    3450              :   else
    3451              :     arg = sym;
    3452              : 
    3453       115009 :   if (arg->ts.type == BT_CHARACTER)
    3454         3287 :     gfc_conv_const_charlen (arg->ts.u.cl);
    3455              : 
    3456              :   /* Some functions we use an extra parameter for the return value.  */
    3457       115009 :   if (gfc_return_by_reference (sym))
    3458              :     {
    3459        12580 :       type = gfc_sym_type (arg);
    3460        12580 :       if (arg->ts.type == BT_COMPLEX
    3461        12137 :           || arg->attr.dimension
    3462         1697 :           || arg->ts.type == BT_CHARACTER)
    3463        12580 :         type = build_reference_type (type);
    3464              : 
    3465        12580 :       vec_safe_push (typelist, type);
    3466        12580 :       if (arg->ts.type == BT_CHARACTER)
    3467              :         {
    3468         3200 :           if (!arg->ts.deferred)
    3469              :             /* Transfer by value.  */
    3470         2834 :             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          366 :             vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
    3475              :         }
    3476              :     }
    3477       115009 :   if (sym->backend_decl == error_mark_node && actual_args != NULL
    3478        16227 :       && sym->ts.interface == NULL
    3479        16221 :       && 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       273462 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    3485              :     {
    3486       158453 :       arg = f->sym;
    3487       158453 :       if (arg)
    3488              :         {
    3489              :           /* Evaluate constant character lengths here so that they can be
    3490              :              included in the type.  */
    3491       158349 :           if (arg->ts.type == BT_CHARACTER)
    3492        12962 :             gfc_conv_const_charlen (arg->ts.u.cl);
    3493              : 
    3494       158349 :           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       157309 :             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       158349 :           vec_safe_push (typelist, type);
    3518              :         }
    3519              :       else
    3520              :         {
    3521          104 :           if (sym->attr.subroutine)
    3522       158453 :             alternate_return = 1;
    3523              :         }
    3524              :     }
    3525              : 
    3526              :   /* Add hidden arguments.  */
    3527       273462 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    3528              :     {
    3529       158453 :       arg = f->sym;
    3530              :       /* Add hidden string length parameters.  */
    3531       158453 :       if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
    3532              :         {
    3533        10869 :           if (!arg->ts.deferred)
    3534              :             /* Transfer by value.  */
    3535         9995 :             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        10869 :           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       158349 :           && arg->attr.optional
    3548        20479 :           && 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       158349 :           && flag_coarray == GFC_FCOARRAY_LIB
    3556         7617 :           && ((arg->ts.type != BT_CLASS
    3557         7586 :                && arg->attr.codimension
    3558         1643 :                && !arg->attr.allocatable)
    3559         6002 :               || (arg->ts.type == BT_CLASS
    3560           31 :                   && CLASS_DATA (arg)->attr.codimension
    3561           24 :                   && !CLASS_DATA (arg)->attr.allocatable)))
    3562              :         {
    3563         1635 :           vec_safe_push (hidden_typelist, pvoid_type_node);  /* caf_token.  */
    3564         1635 :           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       124009 :   vec_safe_reserve (typelist, vec_safe_length (hidden_typelist));
    3570       115009 :   vec_safe_splice (typelist, hidden_typelist);
    3571              : 
    3572       115009 :   if (!vec_safe_is_empty (typelist)
    3573        44333 :       || sym->attr.is_main_program
    3574        17227 :       || sym->attr.if_source != IFSRC_UNKNOWN)
    3575              :     is_varargs = false;
    3576              : 
    3577       115009 :   if (sym->backend_decl == error_mark_node)
    3578       115009 :     sym->backend_decl = NULL_TREE;
    3579              : 
    3580       115062 : arg_type_list_done:
    3581              : 
    3582       115062 :   if (alternate_return)
    3583           74 :     type = integer_type_node;
    3584       114988 :   else if (!sym->attr.function || gfc_return_by_reference (sym))
    3585        92638 :     type = void_type_node;
    3586        22350 :   else if (sym->attr.mixed_entry_master)
    3587           98 :     type = gfc_get_mixed_entry_union (sym->ns);
    3588        22252 :   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        22080 :   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        21583 :     type = gfc_sym_type (sym);
    3619              : 
    3620       115062 :   if (is_varargs)
    3621              :     /* This should be represented as an unprototyped type, not a type
    3622              :        with (...) prototype.  */
    3623         1980 :     type = build_function_type (type, NULL_TREE);
    3624              :   else
    3625       254434 :     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       115062 :   if (fnspec)
    3630              :     {
    3631         1949 :       tree tmp;
    3632         1949 :       int spec_len = strlen (fnspec);
    3633         1949 :       tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
    3634         1949 :       tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
    3635         1949 :       type = build_type_attribute_variant (type, tmp);
    3636              :     }
    3637              :   else
    3638       113113 :     type = create_fn_spec (sym, type);
    3639              : 
    3640       115062 :   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       696210 : gfc_type_for_size (unsigned bits, int unsignedp)
    3650              : {
    3651       696210 :   if (!unsignedp)
    3652              :     {
    3653              :       int i;
    3654       471903 :       for (i = 0; i <= MAX_INT_KINDS; ++i)
    3655              :         {
    3656       471873 :           tree type = gfc_integer_types[i];
    3657       471873 :           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       558546 :       if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
    3682              :         return unsigned_intQI_type_node;
    3683       525288 :       if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
    3684              :         return unsigned_intHI_type_node;
    3685       492434 :       if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
    3686              :         return unsigned_intSI_type_node;
    3687       451602 :       if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
    3688              :         return unsigned_intDI_type_node;
    3689        32609 :       if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
    3690        32609 :         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       712714 : gfc_type_for_mode (machine_mode mode, int unsignedp)
    3701              : {
    3702       712714 :   int i;
    3703       712714 :   tree *base;
    3704       712714 :   scalar_int_mode int_mode;
    3705              : 
    3706       712714 :   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
    3707              :     base = gfc_real_types;
    3708       704438 :   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
    3709              :     base = gfc_complex_types;
    3710       509196 :   else if (is_a <scalar_int_mode> (mode, &int_mode))
    3711              :     {
    3712       508704 :       tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
    3713       508704 :       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        66059 :            && 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       797091 :   for (i = 0; i <= MAX_REAL_KINDS; ++i)
    3736              :     {
    3737       732017 :       tree type = base[i];
    3738       732017 :       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       430632 : gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
    3750              : {
    3751       430632 :   int rank, dim;
    3752       430632 :   bool indirect = false;
    3753       430632 :   tree etype, ptype, t, base_decl;
    3754       430632 :   tree data_off, span_off, dim_off, rank_off, dim_size, elem_size;
    3755       430632 :   tree lower_suboff, upper_suboff, stride_suboff;
    3756              : 
    3757       430632 :   if (! GFC_DESCRIPTOR_TYPE_P (type))
    3758              :     {
    3759       274738 :       if (! POINTER_TYPE_P (type))
    3760              :         return false;
    3761       171874 :       type = TREE_TYPE (type);
    3762       171874 :       if (! GFC_DESCRIPTOR_TYPE_P (type))
    3763              :         return false;
    3764              :       indirect = true;
    3765              :     }
    3766              : 
    3767       305157 :   rank = GFC_TYPE_ARRAY_RANK (type);
    3768       305157 :   if (rank >= (int) (ARRAY_SIZE (info->dimen)))
    3769              :     return false;
    3770              : 
    3771       305157 :   etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
    3772       305157 :   gcc_assert (POINTER_TYPE_P (etype));
    3773       305157 :   etype = TREE_TYPE (etype);
    3774              : 
    3775              :   /* If the type is not a scalar coarray.  */
    3776       305157 :   if (TREE_CODE (etype) == ARRAY_TYPE)
    3777       305131 :     etype = TREE_TYPE (etype);
    3778              : 
    3779              :   /* Can't handle variable sized elements yet.  */
    3780       305157 :   if (int_size_in_bytes (etype) <= 0)
    3781              :     return false;
    3782              :   /* Nor non-constant lower bounds in assumed shape arrays.  */
    3783       283519 :   if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
    3784       283519 :       || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
    3785              :     {
    3786        86791 :       for (dim = 0; dim < rank; dim++)
    3787        53585 :         if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
    3788        53585 :             || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
    3789              :           return false;
    3790              :     }
    3791              : 
    3792       283263 :   memset (info, '\0', sizeof (*info));
    3793       283263 :   info->ndimensions = rank;
    3794       283263 :   info->ordering = array_descr_ordering_column_major;
    3795       283263 :   info->element_type = etype;
    3796       283263 :   ptype = build_pointer_type (gfc_array_index_type);
    3797       283263 :   base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
    3798       283263 :   if (!base_decl)
    3799              :     {
    3800       411915 :       base_decl = build_debug_expr_decl (indirect
    3801       137297 :                                          ? build_pointer_type (ptype) : ptype);
    3802       274618 :       GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
    3803              :     }
    3804       283263 :   info->base_decl = base_decl;
    3805       283263 :   if (indirect)
    3806       138904 :     base_decl = build1 (INDIRECT_REF, ptype, base_decl);
    3807              : 
    3808       283263 :   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       283263 :   t = fold_build_pointer_plus (base_decl, span_off);
    3813       283263 :   elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3814              : 
    3815       283263 :   t = base_decl;
    3816       283263 :   if (!integer_zerop (data_off))
    3817            0 :     t = fold_build_pointer_plus (t, data_off);
    3818       283263 :   t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
    3819       283263 :   info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
    3820       283263 :   enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
    3821       283263 :   if (akind == GFC_ARRAY_ALLOCATABLE
    3822       283263 :       || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE)
    3823        34999 :     info->allocated = build2 (NE_EXPR, logical_type_node,
    3824              :                               info->data_location, null_pointer_node);
    3825       248264 :   else if (akind == GFC_ARRAY_POINTER
    3826       248264 :            || akind == GFC_ARRAY_POINTER_CONT
    3827       248264 :            || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
    3828       230264 :            || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
    3829        18000 :     info->associated = build2 (NE_EXPR, logical_type_node,
    3830              :                                info->data_location, null_pointer_node);
    3831       283263 :   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       283263 :        || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
    3836        15224 :       && dwarf_version >= 5)
    3837              :     {
    3838        15224 :       rank = 1;
    3839        15224 :       info->ndimensions = 1;
    3840        15224 :       t = fold_build_pointer_plus (base_decl, rank_off);
    3841        15224 :       t = build1 (NOP_EXPR, build_pointer_type (signed_char_type_node), t);
    3842        15224 :       t = build1 (INDIRECT_REF, signed_char_type_node, t);
    3843        15224 :       info->rank = t;
    3844        15224 :       t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
    3845        15224 :       t = size_binop (MULT_EXPR, t, dim_size);
    3846        15224 :       dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
    3847              :     }
    3848              : 
    3849       700930 :   for (dim = 0; dim < rank; dim++)
    3850              :     {
    3851       417667 :       t = fold_build_pointer_plus (base_decl,
    3852              :                                    size_binop (PLUS_EXPR,
    3853              :                                                dim_off, lower_suboff));
    3854       417667 :       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3855       417667 :       info->dimen[dim].lower_bound = t;
    3856       417667 :       t = fold_build_pointer_plus (base_decl,
    3857              :                                    size_binop (PLUS_EXPR,
    3858              :                                                dim_off, upper_suboff));
    3859       417667 :       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3860       417667 :       info->dimen[dim].upper_bound = t;
    3861       417667 :       if (akind == GFC_ARRAY_ASSUMED_SHAPE
    3862       417667 :           || akind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
    3863              :         {
    3864              :           /* Assumed shape arrays have known lower bounds.  */
    3865        53329 :           info->dimen[dim].upper_bound
    3866        53329 :             = build2 (MINUS_EXPR, gfc_array_index_type,
    3867              :                       info->dimen[dim].upper_bound,
    3868              :                       info->dimen[dim].lower_bound);
    3869        53329 :           info->dimen[dim].lower_bound
    3870        53329 :             = fold_convert (gfc_array_index_type,
    3871              :                             GFC_TYPE_ARRAY_LBOUND (type, dim));
    3872        53329 :           info->dimen[dim].upper_bound
    3873        53329 :             = build2 (PLUS_EXPR, gfc_array_index_type,
    3874              :                       info->dimen[dim].lower_bound,
    3875              :                       info->dimen[dim].upper_bound);
    3876              :         }
    3877       417667 :       t = fold_build_pointer_plus (base_decl,
    3878              :                                    size_binop (PLUS_EXPR,
    3879              :                                                dim_off, stride_suboff));
    3880       417667 :       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3881       417667 :       t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
    3882       417667 :       info->dimen[dim].stride = t;
    3883       417667 :       if (dim + 1 < rank)
    3884       134430 :         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.5.0-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.