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

Generated by: LCOV version 2.4-beta

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