LCOV - code coverage report
Current view: top level - gcc/fortran - trans-types.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.7 % 1887 1693
Test Date: 2026-03-28 14:25:54 Functions: 97.2 % 72 70
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       424169 : tree get_dtype_type_node (void)
     146              : {
     147       424169 :   tree field;
     148       424169 :   tree dtype_node;
     149       424169 :   tree *dtype_chain = NULL;
     150              : 
     151       424169 :   if (dtype_type_node == NULL_TREE)
     152              :     {
     153        31361 :       dtype_node = make_node (RECORD_TYPE);
     154        31361 :       TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
     155        31361 :       TYPE_NAMELESS (dtype_node) = 1;
     156        31361 :       field = gfc_add_field_to_struct_1 (dtype_node,
     157              :                                          get_identifier ("elem_len"),
     158              :                                          size_type_node, &dtype_chain);
     159        31361 :       suppress_warning (field);
     160        31361 :       field = gfc_add_field_to_struct_1 (dtype_node,
     161              :                                          get_identifier ("version"),
     162              :                                          integer_type_node, &dtype_chain);
     163        31361 :       suppress_warning (field);
     164        31361 :       field = gfc_add_field_to_struct_1 (dtype_node,
     165              :                                          get_identifier ("rank"),
     166              :                                          signed_char_type_node, &dtype_chain);
     167        31361 :       suppress_warning (field);
     168        31361 :       field = gfc_add_field_to_struct_1 (dtype_node,
     169              :                                          get_identifier ("type"),
     170              :                                          signed_char_type_node, &dtype_chain);
     171        31361 :       suppress_warning (field);
     172        31361 :       field = gfc_add_field_to_struct_1 (dtype_node,
     173              :                                          get_identifier ("attribute"),
     174              :                                          short_integer_type_node, &dtype_chain);
     175        31361 :       suppress_warning (field);
     176        31361 :       gfc_finish_type (dtype_node);
     177        31361 :       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
     178        31361 :       dtype_type_node = dtype_node;
     179              :     }
     180       424169 :   return dtype_type_node;
     181              : }
     182              : 
     183              : static int
     184       251024 : get_real_kind_from_node (tree type)
     185              : {
     186       251024 :   int i;
     187              : 
     188       627560 :   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
     189       627560 :     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       815828 : get_int_kind_from_node (tree type)
     211              : {
     212       815828 :   int i;
     213              : 
     214       815828 :   if (!type)
     215              :     return -2;
     216              : 
     217      2630868 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     218      2630868 :     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       502048 : get_int_kind_from_name (const char *name)
     226              : {
     227       502048 :   return get_int_kind_from_node (get_typenode_from_name (name));
     228              : }
     229              : 
     230              : static int
     231       533426 : get_unsigned_kind_from_node (tree type)
     232              : {
     233       533426 :   int i;
     234              : 
     235       533426 :   if (!type)
     236              :     return -2;
     237              : 
     238       541021 :   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       407914 : get_uint_kind_from_name (const char *name)
     247              : {
     248       407914 :   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       156890 : get_int_kind_from_width (int size)
     334              : {
     335       156890 :   int i;
     336              : 
     337       470670 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     338       469856 :     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        31378 : get_int_kind_from_minimal_width (int size)
     346              : {
     347        31378 :   int i;
     348              : 
     349       156890 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     350       156483 :     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        94134 : get_uint_kind_from_width (int size)
     358              : {
     359        94134 :   int i;
     360              : 
     361        97074 :   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        31378 : gfc_init_c_interop_kinds (void)
     374              : {
     375        31378 :   int i;
     376              : 
     377              :   /* init all pointers in the list to NULL */
     378      2353350 :   for (i = 0; i < ISOCBINDING_NUMBER; i++)
     379              :     {
     380              :       /* Initialize the name and value fields.  */
     381      2321972 :       c_interop_kinds_table[i].name[0] = '\0';
     382      2321972 :       c_interop_kinds_table[i].value = -100;
     383      2321972 :       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        31378 : }
     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        31378 : gfc_init_kinds (void)
     435              : {
     436        31378 :   opt_scalar_int_mode int_mode_iter;
     437        31378 :   opt_scalar_float_mode float_mode_iter;
     438        31378 :   int i_index, r_index, kind;
     439        31378 :   bool saw_i4 = false, saw_i8 = false;
     440        31378 :   bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
     441        31378 :   scalar_mode r16_mode = QImode;
     442        31378 :   scalar_mode composite_mode = QImode;
     443              : 
     444        31378 :   i_index = 0;
     445       251024 :   FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
     446              :     {
     447       219646 :       scalar_int_mode mode = int_mode_iter.require ();
     448       219646 :       int kind, bitsize;
     449              : 
     450       219646 :       if (!targetm.scalar_mode_supported_p (mode))
     451       219646 :         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       156483 :       bitsize = GET_MODE_BITSIZE (mode);
     457       156483 :       if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
     458            0 :         continue;
     459              : 
     460       156483 :       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       156483 :       kind = bitsize / 8;
     465              : 
     466       156483 :       if (kind == 4)
     467              :         saw_i4 = true;
     468       125105 :       if (kind == 8)
     469        31378 :         saw_i8 = true;
     470              : 
     471       156483 :       gfc_integer_kinds[i_index].kind = kind;
     472       156483 :       gfc_integer_kinds[i_index].radix = 2;
     473       156483 :       gfc_integer_kinds[i_index].digits = bitsize - 1;
     474       156483 :       gfc_integer_kinds[i_index].bit_size = bitsize;
     475              : 
     476       156483 :       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       156483 :       gfc_logical_kinds[i_index].kind = kind;
     485       156483 :       gfc_logical_kinds[i_index].bit_size = bitsize;
     486              : 
     487       156483 :       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        31378 :   if (saw_i8)
     494        31378 :     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        31378 :   gcc_assert(saw_i4);
     500              : 
     501              :   /* Set the maximum integer kind.  Used with at least BOZ constants.  */
     502        31378 :   gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
     503              : 
     504        31378 :   r_index = 0;
     505       219646 :   FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
     506              :     {
     507       188268 :       scalar_float_mode mode = float_mode_iter.require ();
     508       188268 :       const struct real_format *fmt = REAL_MODE_FORMAT (mode);
     509       188268 :       int kind;
     510              : 
     511       188268 :       if (fmt == NULL)
     512       188268 :         continue;
     513       188268 :       if (!targetm.scalar_mode_supported_p (mode))
     514            0 :         continue;
     515              : 
     516      1317876 :       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       188268 :       if (!targetm.libgcc_floating_mode_supported_p (mode))
     524            0 :         continue;
     525       188268 :       if (mode != TYPE_MODE (float_type_node)
     526       156890 :             && (mode != TYPE_MODE (double_type_node))
     527       125512 :             && (mode != TYPE_MODE (long_double_type_node))
     528              : #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
     529       282402 :             && (mode != TFmode)
     530              : #endif
     531              :            )
     532        62756 :         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       125512 :       kind = (GET_MODE_PRECISION (mode) + 7) / 8;
     555              : 
     556       125512 :       if (kind == 4)
     557              :         saw_r4 = true;
     558        94134 :       if (kind == 8)
     559              :         saw_r8 = true;
     560        94134 :       if (kind == 10)
     561              :         saw_r10 = true;
     562        94134 :       if (kind == 16)
     563              :         {
     564        31378 :           saw_r16 = true;
     565        31378 :           r16_mode = mode;
     566              :         }
     567              : 
     568              :       /* Careful we don't stumble a weird internal mode.  */
     569       125512 :       gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
     570              :       /* Or have too many modes for the allocated space.  */
     571        94134 :       gcc_assert (r_index != MAX_REAL_KINDS);
     572              : 
     573       125512 :       gfc_real_kinds[r_index].kind = kind;
     574       125512 :       gfc_real_kinds[r_index].abi_kind = kind;
     575       125512 :       gfc_real_kinds[r_index].radix = fmt->b;
     576       125512 :       gfc_real_kinds[r_index].digits = fmt->p;
     577       125512 :       gfc_real_kinds[r_index].min_exponent = fmt->emin;
     578       125512 :       gfc_real_kinds[r_index].max_exponent = fmt->emax;
     579       125512 :       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       125512 :       gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
     589       125512 :       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        31378 :   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        31378 :   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        31378 :   gfc_numeric_storage_size = 4 * 8;
     628              : 
     629        31378 :   if (flag_default_integer)
     630              :     {
     631           91 :       if (!saw_i8)
     632            0 :         gfc_fatal_error ("INTEGER(KIND=8) is not available for "
     633              :                          "%<-fdefault-integer-8%> option");
     634              : 
     635           91 :       gfc_default_integer_kind = 8;
     636              : 
     637              :     }
     638        31287 :   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        31287 :   else if (saw_i4)
     647              :     {
     648        31287 :       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        31378 :   gfc_default_unsigned_kind = gfc_default_integer_kind;
     657              : 
     658              :   /* Choose the default real kind.  Again, we choose 4 when possible.  */
     659        31378 :   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        31376 :   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        31370 :   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        31364 :   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        31340 :   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        31316 :   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        31292 :   else if (saw_r4)
     708        31292 :     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        31378 :   if (flag_default_double && saw_r8)
     717            0 :     gfc_default_double_kind = 8;
     718        31378 :   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        31364 :   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        31340 :   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        31316 :   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        31292 :   else if (saw_r4 && saw_r8)
     755        31292 :     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        31378 :   gfc_default_logical_kind = gfc_default_integer_kind;
     776        31378 :   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        31378 :   i_index = 0;
     782        62756 :   if ((kind = get_int_kind_from_width (8)) > 0)
     783              :     {
     784        31378 :       gfc_character_kinds[i_index].kind = kind;
     785        31378 :       gfc_character_kinds[i_index].bit_size = 8;
     786        31378 :       gfc_character_kinds[i_index].name = "ascii";
     787        31378 :       i_index++;
     788              :     }
     789        62756 :   if ((kind = get_int_kind_from_width (32)) > 0)
     790              :     {
     791        31378 :       gfc_character_kinds[i_index].kind = kind;
     792        31378 :       gfc_character_kinds[i_index].bit_size = 32;
     793        31378 :       gfc_character_kinds[i_index].name = "iso_10646";
     794        31378 :       i_index++;
     795              :     }
     796              : 
     797              :   /* Choose the smallest integer kind for our default character.  */
     798        31378 :   gfc_default_character_kind = gfc_character_kinds[0].kind;
     799        31378 :   gfc_character_storage_size = gfc_default_character_kind * 8;
     800              : 
     801        31785 :   gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
     802              : 
     803        31378 :   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        31378 :   gfc_c_int_kind = INT_TYPE_SIZE / 8;
     808              : 
     809              :   /* UNSIGNED has the same as INT.  */
     810        31378 :   gfc_c_uint_kind = gfc_c_int_kind;
     811              : 
     812              :   /* Choose atomic kinds to match C's int.  */
     813        31378 :   gfc_atomic_int_kind = gfc_c_int_kind;
     814        31378 :   gfc_atomic_logical_kind = gfc_c_int_kind;
     815              : 
     816        31378 :   gfc_c_intptr_kind = POINTER_SIZE / 8;
     817        31378 : }
     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     81325096 :   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
     829     81323039 :     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      4436486 :   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      5280187 :   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
     853      5280176 :     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      1948816 :   for (i = 0; gfc_logical_kinds[i].kind; i++)
     865      1948808 :     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      1385405 :   for (i = 0; gfc_character_kinds[i].kind; i++)
     877      1385391 :     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     34166997 : gfc_validate_kind (bt type, int kind, bool may_fail)
     889              : {
     890     34166997 :   int rc;
     891              : 
     892     34166997 :   switch (type)
     893              :     {
     894              :     case BT_REAL:               /* Fall through */
     895              :     case BT_COMPLEX:
     896     34166997 :       rc = validate_real (kind);
     897              :       break;
     898              :     case BT_INTEGER:
     899     34166997 :       rc = validate_integer (kind);
     900              :       break;
     901              :     case BT_UNSIGNED:
     902     34166997 :       rc = validate_unsigned (kind);
     903              :       break;
     904              :     case BT_LOGICAL:
     905     34166997 :       rc = validate_logical (kind);
     906              :       break;
     907              :     case BT_CHARACTER:
     908     34166997 :       rc = validate_character (kind);
     909              :       break;
     910              : 
     911            0 :     default:
     912            0 :       gfc_internal_error ("gfc_validate_kind(): Got bad type");
     913              :     }
     914              : 
     915     34166997 :   if (rc < 0 && !may_fail)
     916            0 :     gfc_internal_error ("gfc_validate_kind(): Got bad kind");
     917              : 
     918     34166997 :   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       156483 : gfc_build_int_type (gfc_integer_info *info)
     929              : {
     930       156483 :   int mode_precision = info->bit_size;
     931              : 
     932       156483 :   if (mode_precision == CHAR_TYPE_SIZE)
     933        31378 :     info->c_char = 1;
     934       156483 :   if (mode_precision == SHORT_TYPE_SIZE)
     935        31378 :     info->c_short = 1;
     936       156483 :   if (mode_precision == INT_TYPE_SIZE)
     937        31378 :     info->c_int = 1;
     938       158111 :   if (mode_precision == LONG_TYPE_SIZE)
     939        31378 :     info->c_long = 1;
     940       156483 :   if (mode_precision == LONG_LONG_TYPE_SIZE)
     941        31378 :     info->c_long_long = 1;
     942              : 
     943       156483 :   if (TYPE_PRECISION (intQI_type_node) == mode_precision)
     944              :     return intQI_type_node;
     945       125105 :   if (TYPE_PRECISION (intHI_type_node) == mode_precision)
     946              :     return intHI_type_node;
     947        93727 :   if (TYPE_PRECISION (intSI_type_node) == mode_precision)
     948              :     return intSI_type_node;
     949        62349 :   if (TYPE_PRECISION (intDI_type_node) == mode_precision)
     950              :     return intDI_type_node;
     951        30971 :   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        64237 : gfc_build_uint_type (int size)
     959              : {
     960        64237 :   if (size == CHAR_TYPE_SIZE)
     961        31504 :     return unsigned_char_type_node;
     962        32733 :   if (size == SHORT_TYPE_SIZE)
     963          371 :     return short_unsigned_type_node;
     964        32362 :   if (size == INT_TYPE_SIZE)
     965        31612 :     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       125512 : gfc_build_real_type (gfc_real_info *info)
     995              : {
     996       125512 :   int mode_precision = info->mode_precision;
     997       125512 :   tree new_type;
     998              : 
     999       125512 :   if (mode_precision == TYPE_PRECISION (float_type_node))
    1000        31378 :     info->c_float = 1;
    1001       125512 :   if (mode_precision == TYPE_PRECISION (double_type_node))
    1002        31378 :     info->c_double = 1;
    1003       125512 :   if (mode_precision == TYPE_PRECISION (long_double_type_node)
    1004       125512 :       && !info->c_float128)
    1005        31378 :     info->c_long_double = 1;
    1006       125512 :   if (mode_precision != TYPE_PRECISION (long_double_type_node)
    1007       125512 :       && mode_precision == 128)
    1008              :     {
    1009              :       /* TODO: see PR101835.  */
    1010        31378 :       info->c_float128 = 1;
    1011        31378 :       gfc_real16_is_float128 = true;
    1012        31378 :       if (TARGET_GLIBC_MAJOR > 2
    1013              :           || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26))
    1014              :         {
    1015        31378 :           info->use_iec_60559 = 1;
    1016        31378 :           gfc_real16_use_iec_60559 = true;
    1017              :         }
    1018              :     }
    1019              : 
    1020       125512 :   if (TYPE_PRECISION (float_type_node) == mode_precision)
    1021              :     return float_type_node;
    1022        94134 :   if (TYPE_PRECISION (double_type_node) == mode_precision)
    1023              :     return double_type_node;
    1024        62756 :   if (TYPE_PRECISION (long_double_type_node) == mode_precision)
    1025              :     return long_double_type_node;
    1026              : 
    1027        31378 :   new_type = make_node (REAL_TYPE);
    1028        31378 :   TYPE_PRECISION (new_type) = mode_precision;
    1029        31378 :   layout_type (new_type);
    1030        31378 :   return new_type;
    1031              : }
    1032              : 
    1033              : static tree
    1034       125512 : gfc_build_complex_type (tree scalar_type)
    1035              : {
    1036       125512 :   tree new_type;
    1037              : 
    1038       125512 :   if (scalar_type == NULL)
    1039              :     return NULL;
    1040       125512 :   if (scalar_type == float_type_node)
    1041        31378 :     return complex_float_type_node;
    1042        94134 :   if (scalar_type == double_type_node)
    1043        31378 :     return complex_double_type_node;
    1044        62756 :   if (scalar_type == long_double_type_node)
    1045        31378 :     return complex_long_double_type_node;
    1046              : 
    1047        31378 :   new_type = make_node (COMPLEX_TYPE);
    1048        31378 :   TREE_TYPE (new_type) = scalar_type;
    1049        31378 :   layout_type (new_type);
    1050        31378 :   return new_type;
    1051              : }
    1052              : 
    1053              : static tree
    1054       156483 : gfc_build_logical_type (gfc_logical_info *info)
    1055              : {
    1056       156483 :   int bit_size = info->bit_size;
    1057       156483 :   tree new_type;
    1058              : 
    1059       156483 :   if (bit_size == BOOL_TYPE_SIZE)
    1060              :     {
    1061        31378 :       info->c_bool = 1;
    1062        31378 :       return boolean_type_node;
    1063              :     }
    1064              : 
    1065       125105 :   new_type = make_unsigned_type (bit_size);
    1066       125105 :   TREE_SET_CODE (new_type, BOOLEAN_TYPE);
    1067       125105 :   TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
    1068       125105 :   TYPE_PRECISION (new_type) = 1;
    1069              : 
    1070       125105 :   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        31378 : gfc_init_types (void)
    1081              : {
    1082        31378 :   char name_buf[26];
    1083        31378 :   int index;
    1084        31378 :   tree type;
    1085        31378 :   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       187861 :   for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
    1093              :     {
    1094       156483 :       type = gfc_build_int_type (&gfc_integer_kinds[index]);
    1095              :       /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set.  */
    1096       156483 :       if (TYPE_STRING_FLAG (type))
    1097        31378 :         type = make_signed_type (gfc_integer_kinds[index].bit_size);
    1098       156483 :       gfc_integer_types[index] = type;
    1099       156483 :       snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
    1100              :                 gfc_integer_kinds[index].kind);
    1101       156483 :       PUSH_TYPE (name_buf, type);
    1102              :     }
    1103              : 
    1104       187861 :   for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
    1105              :     {
    1106       156483 :       type = gfc_build_logical_type (&gfc_logical_kinds[index]);
    1107       156483 :       gfc_logical_types[index] = type;
    1108       156483 :       snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
    1109              :                 gfc_logical_kinds[index].kind);
    1110       156483 :       PUSH_TYPE (name_buf, type);
    1111              :     }
    1112              : 
    1113       156890 :   for (index = 0; gfc_real_kinds[index].kind != 0; index++)
    1114              :     {
    1115       125512 :       type = gfc_build_real_type (&gfc_real_kinds[index]);
    1116       125512 :       gfc_real_types[index] = type;
    1117       125512 :       snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
    1118              :                 gfc_real_kinds[index].kind);
    1119       125512 :       PUSH_TYPE (name_buf, type);
    1120              : 
    1121       125512 :       if (gfc_real_kinds[index].c_float128)
    1122        31378 :         gfc_float128_type_node = type;
    1123              : 
    1124       125512 :       type = gfc_build_complex_type (type);
    1125       125512 :       gfc_complex_types[index] = type;
    1126       125512 :       snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
    1127              :                 gfc_real_kinds[index].kind);
    1128       125512 :       PUSH_TYPE (name_buf, type);
    1129              : 
    1130       125512 :       if (gfc_real_kinds[index].c_float128)
    1131        31378 :         gfc_complex_float128_type_node = type;
    1132              :     }
    1133              : 
    1134        94134 :   for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
    1135              :     {
    1136        62756 :       type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
    1137        62756 :       type = build_qualified_type (type, TYPE_UNQUALIFIED);
    1138        62756 :       TYPE_STRING_FLAG (type) = 1;
    1139        62756 :       snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
    1140              :                 gfc_character_kinds[index].kind);
    1141        62756 :       PUSH_TYPE (name_buf, type);
    1142        62756 :       gfc_character_types[index] = type;
    1143        62756 :       gfc_pcharacter_types[index] = build_pointer_type (type);
    1144              :     }
    1145        31378 :   gfc_character1_type_node = gfc_character_types[0];
    1146              : 
    1147        31378 :   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        31378 :   PUSH_TYPE ("byte", unsigned_char_type_node);
    1185        31378 :   PUSH_TYPE ("void", void_type_node);
    1186              : 
    1187              :   /* DBX debugging output gets upset if these aren't set.  */
    1188        31378 :   if (!TYPE_NAME (integer_type_node))
    1189            0 :     PUSH_TYPE ("c_integer", integer_type_node);
    1190        31378 :   if (!TYPE_NAME (char_type_node))
    1191        31378 :     PUSH_TYPE ("c_char", char_type_node);
    1192              : 
    1193              : #undef PUSH_TYPE
    1194              : 
    1195        31378 :   pvoid_type_node = build_pointer_type (void_type_node);
    1196        31378 :   prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
    1197        31378 :   ppvoid_type_node = build_pointer_type (pvoid_type_node);
    1198        31378 :   pchar_type_node = build_pointer_type (gfc_character1_type_node);
    1199        31378 :   pfunc_type_node
    1200        31378 :     = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
    1201              : 
    1202        31378 :   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        31378 :   gfc_array_range_type
    1206        31378 :           = 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        31378 :   n = TYPE_PRECISION (size_type_node);
    1215        31378 :   gfc_max_array_element_size
    1216        31378 :     = wide_int_to_tree (size_type_node,
    1217        31378 :                         wi::mask (n, UNSIGNED,
    1218        31378 :                                   TYPE_PRECISION (size_type_node)));
    1219              : 
    1220        31378 :   logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
    1221        31378 :   logical_true_node = build_int_cst (logical_type_node, 1);
    1222        31378 :   logical_false_node = build_int_cst (logical_type_node, 0);
    1223              : 
    1224              :   /* Character lengths are of type size_t, except signed.  */
    1225        31378 :   gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
    1226        31378 :   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        31378 :   gfc_size_kind = get_int_kind_from_node (size_type_node);
    1231        31378 : }
    1232              : 
    1233              : /* Get the type node for the given type and kind.  */
    1234              : 
    1235              : tree
    1236      5675562 : gfc_get_int_type (int kind)
    1237              : {
    1238      5675562 :   int index = gfc_validate_kind (BT_INTEGER, kind, true);
    1239      5675562 :   return index < 0 ? 0 : gfc_integer_types[index];
    1240              : }
    1241              : 
    1242              : tree
    1243      2982934 : gfc_get_unsigned_type (int kind)
    1244              : {
    1245      2982934 :   int index = gfc_validate_kind (BT_UNSIGNED, kind, true);
    1246      2982934 :   return index < 0 ? 0 : gfc_unsigned_types[index];
    1247              : }
    1248              : 
    1249              : tree
    1250       767008 : gfc_get_real_type (int kind)
    1251              : {
    1252       767008 :   int index = gfc_validate_kind (BT_REAL, kind, true);
    1253       767008 :   return index < 0 ? 0 : gfc_real_types[index];
    1254              : }
    1255              : 
    1256              : tree
    1257       465313 : gfc_get_complex_type (int kind)
    1258              : {
    1259       465313 :   int index = gfc_validate_kind (BT_COMPLEX, kind, true);
    1260       465313 :   return index < 0 ? 0 : gfc_complex_types[index];
    1261              : }
    1262              : 
    1263              : tree
    1264       584026 : gfc_get_logical_type (int kind)
    1265              : {
    1266       584026 :   int index = gfc_validate_kind (BT_LOGICAL, kind, true);
    1267       584026 :   return index < 0 ? 0 : gfc_logical_types[index];
    1268              : }
    1269              : 
    1270              : tree
    1271       425938 : gfc_get_char_type (int kind)
    1272              : {
    1273       425938 :   int index = gfc_validate_kind (BT_CHARACTER, kind, true);
    1274       425938 :   return index < 0 ? 0 : gfc_character_types[index];
    1275              : }
    1276              : 
    1277              : tree
    1278       162693 : gfc_get_pchar_type (int kind)
    1279              : {
    1280       162693 :   int index = gfc_validate_kind (BT_CHARACTER, kind, true);
    1281       162693 :   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        91518 : gfc_get_character_type_len_for_eltype (tree eltype, tree len)
    1289              : {
    1290        91518 :   tree bounds, type;
    1291              : 
    1292        91518 :   bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
    1293        91518 :   type = build_array_type (eltype, bounds);
    1294        91518 :   TYPE_STRING_FLAG (type) = 1;
    1295              : 
    1296        91518 :   return type;
    1297              : }
    1298              : 
    1299              : tree
    1300        83715 : gfc_get_character_type_len (int kind, tree len)
    1301              : {
    1302        83715 :   gfc_validate_kind (BT_CHARACTER, kind, false);
    1303        83715 :   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      1261612 : gfc_typenode_for_spec (gfc_typespec * spec, int codim)
    1325              : {
    1326      1261612 :   tree basetype;
    1327              : 
    1328      1261612 :   switch (spec->type)
    1329              :     {
    1330            0 :     case BT_UNKNOWN:
    1331            0 :       gcc_unreachable ();
    1332              : 
    1333       458934 :     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       458934 :       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       458577 :         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       137557 :     case BT_REAL:
    1354       137557 :       basetype = gfc_get_real_type (spec->kind);
    1355       137557 :       break;
    1356              : 
    1357        25711 :     case BT_COMPLEX:
    1358        25711 :       basetype = gfc_get_complex_type (spec->kind);
    1359        25711 :       break;
    1360              : 
    1361       417222 :     case BT_LOGICAL:
    1362       417222 :       basetype = gfc_get_logical_type (spec->kind);
    1363       417222 :       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       154412 :     case BT_DERIVED:
    1380       154412 :     case BT_CLASS:
    1381       154412 :       basetype = gfc_get_derived_type (spec->u.derived, codim);
    1382              : 
    1383       154412 :       if (spec->type == BT_CLASS)
    1384        29779 :         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       154412 :       if (spec->u.derived->ts.f90_type == BT_VOID)
    1391              :         {
    1392        12113 :           spec->type = BT_INTEGER;
    1393        12113 :           spec->kind = gfc_index_integer_kind;
    1394        12113 :           spec->f90_type = BT_VOID;
    1395        12113 :           spec->is_c_interop = 1;  /* Mark as escaping later.  */
    1396              :         }
    1397              :       break;
    1398         3626 :     case BT_VOID:
    1399         3626 :     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         3626 :       basetype = ptr_type_node;
    1403         3626 :       if (spec->f90_type == BT_VOID)
    1404              :         {
    1405          404 :           if (spec->u.derived
    1406            0 :               && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
    1407              :             basetype = ptr_type_node;
    1408              :           else
    1409          404 :             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      1261612 :   return basetype;
    1419              : }
    1420              : 
    1421              : /* Build an INT_CST for constant expressions, otherwise return NULL_TREE.  */
    1422              : 
    1423              : static tree
    1424       117961 : gfc_conv_array_bound (gfc_expr * expr)
    1425              : {
    1426              :   /* If expr is an integer constant, return that.  */
    1427       117961 :   if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
    1428        14588 :     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       329769 : gfc_get_element_type (tree type)
    1440              : {
    1441       329769 :   tree element;
    1442              : 
    1443       329769 :   if (GFC_ARRAY_TYPE_P (type))
    1444              :     {
    1445       124767 :       if (TREE_CODE (type) == POINTER_TYPE)
    1446        20176 :         type = TREE_TYPE (type);
    1447       124767 :       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       124270 :           gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
    1455       124270 :           element = TREE_TYPE (type);
    1456              :         }
    1457              :     }
    1458              :   else
    1459              :     {
    1460       205002 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
    1461       205002 :       element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
    1462              : 
    1463       205002 :       gcc_assert (TREE_CODE (element) == POINTER_TYPE);
    1464       205002 :       element = TREE_TYPE (element);
    1465              : 
    1466              :       /* For arrays, which are not scalar coarrays.  */
    1467       205002 :       if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
    1468       203448 :         element = TREE_TYPE (element);
    1469              :     }
    1470              : 
    1471       329769 :   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       109218 : gfc_is_nodesc_array (gfc_symbol * sym)
    1553              : {
    1554       109218 :   symbol_attribute *array_attr;
    1555       109218 :   gfc_array_spec *as;
    1556       109218 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    1557              : 
    1558       109218 :   array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
    1559       109218 :   as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
    1560              : 
    1561       109218 :   gcc_assert (array_attr->dimension || array_attr->codimension);
    1562              : 
    1563              :   /* We only want local arrays.  */
    1564       109218 :   if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    1565       102025 :       || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
    1566       102025 :       || 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        89899 :   if (sym->assoc && as->type != AS_EXPLICIT)
    1572              :     return 0;
    1573              : 
    1574              :   /* The dummy is stored in sym and not in the component.  */
    1575        88165 :   if (sym->attr.dummy)
    1576        39254 :     return as->type != AS_ASSUMED_SHAPE
    1577        58237 :         && as->type != AS_ASSUMED_RANK;
    1578              : 
    1579        48911 :   if (sym->attr.result || sym->attr.function)
    1580              :     return 0;
    1581              : 
    1582        39104 :   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        51766 : 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        51766 :   tree lbound[GFC_MAX_DIMENSIONS];
    1596        51766 :   tree ubound[GFC_MAX_DIMENSIONS];
    1597        51766 :   int n, corank;
    1598              : 
    1599              :   /* Assumed-shape arrays do not have codimension information stored in the
    1600              :      descriptor.  */
    1601        51766 :   corank = MAX (as->corank, codim);
    1602        51766 :   if (as->type == AS_ASSUMED_SHAPE ||
    1603         7691 :       (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
    1604        51766 :     corank = codim;
    1605              : 
    1606        51766 :   if (as->type == AS_ASSUMED_RANK)
    1607       123056 :     for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    1608              :       {
    1609       115365 :         lbound[n] = NULL_TREE;
    1610       115365 :         ubound[n] = NULL_TREE;
    1611              :       }
    1612              : 
    1613       118403 :   for (n = 0; n < as->rank; n++)
    1614              :     {
    1615              :       /* Create expressions for the known bounds of the array.  */
    1616        66637 :       if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
    1617        16559 :         lbound[n] = gfc_index_one_node;
    1618              :       else
    1619        50078 :         lbound[n] = gfc_conv_array_bound (as->lower[n]);
    1620        66637 :       ubound[n] = gfc_conv_array_bound (as->upper[n]);
    1621              :     }
    1622              : 
    1623        52804 :   for (n = as->rank; n < as->rank + corank; n++)
    1624              :     {
    1625         1038 :       if (as->type != AS_DEFERRED && as->lower[n] == NULL)
    1626           18 :         lbound[n] = gfc_index_one_node;
    1627              :       else
    1628         1020 :         lbound[n] = gfc_conv_array_bound (as->lower[n]);
    1629              : 
    1630         1038 :       if (n < as->rank + corank - 1)
    1631          226 :         ubound[n] = gfc_conv_array_bound (as->upper[n]);
    1632              :     }
    1633              : 
    1634        51766 :   if (as->type == AS_ASSUMED_SHAPE)
    1635        16469 :     akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
    1636              :                        : GFC_ARRAY_ASSUMED_SHAPE;
    1637        35297 :   else if (as->type == AS_ASSUMED_RANK)
    1638              :     {
    1639         7691 :       if (akind == GFC_ARRAY_ALLOCATABLE)
    1640              :         akind = GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE;
    1641         7310 :       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         6891 :         akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
    1646              :                            : GFC_ARRAY_ASSUMED_RANK;
    1647              :     }
    1648        95841 :   return gfc_get_array_type_bounds (type, as->rank == -1
    1649              :                                           ? GFC_MAX_DIMENSIONS : as->rank,
    1650              :                                     corank, lbound, ubound, 0, akind,
    1651        51766 :                                     restricted);
    1652              : }
    1653              : 
    1654              : /* Returns the struct descriptor_dimension type.  */
    1655              : 
    1656              : static tree
    1657        31393 : gfc_get_desc_dim_type (void)
    1658              : {
    1659        31393 :   tree type;
    1660        31393 :   tree decl, *chain = NULL;
    1661              : 
    1662        31393 :   if (gfc_desc_dim_type)
    1663              :     return gfc_desc_dim_type;
    1664              : 
    1665              :   /* Build the type node.  */
    1666        11876 :   type = make_node (RECORD_TYPE);
    1667              : 
    1668        11876 :   TYPE_NAME (type) = get_identifier ("descriptor_dimension");
    1669        11876 :   TYPE_PACKED (type) = 1;
    1670              : 
    1671              :   /* Consists of the stride, lbound and ubound members.  */
    1672        11876 :   decl = gfc_add_field_to_struct_1 (type,
    1673              :                                     get_identifier ("stride"),
    1674              :                                     gfc_array_index_type, &chain);
    1675        11876 :   suppress_warning (decl);
    1676              : 
    1677        11876 :   decl = gfc_add_field_to_struct_1 (type,
    1678              :                                     get_identifier ("lbound"),
    1679              :                                     gfc_array_index_type, &chain);
    1680        11876 :   suppress_warning (decl);
    1681              : 
    1682        11876 :   decl = gfc_add_field_to_struct_1 (type,
    1683              :                                     get_identifier ("ubound"),
    1684              :                                     gfc_array_index_type, &chain);
    1685        11876 :   suppress_warning (decl);
    1686              : 
    1687              :   /* Finish off the type.  */
    1688        11876 :   gfc_finish_type (type);
    1689        11876 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
    1690              : 
    1691        11876 :   gfc_desc_dim_type = type;
    1692        11876 :   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       140321 : gfc_get_dtype_rank_type (int rank, tree etype)
    1703              : {
    1704       140321 :   tree ptype;
    1705       140321 :   tree size;
    1706       140321 :   int n;
    1707       140321 :   tree tmp;
    1708       140321 :   tree dtype;
    1709       140321 :   tree field;
    1710       140321 :   vec<constructor_elt, va_gc> *v = NULL;
    1711              : 
    1712       140321 :   ptype = etype;
    1713       140321 :   while (TREE_CODE (etype) == POINTER_TYPE
    1714       170735 :          || TREE_CODE (etype) == ARRAY_TYPE)
    1715              :     {
    1716        30414 :       ptype = etype;
    1717        30414 :       etype = TREE_TYPE (etype);
    1718              :     }
    1719              : 
    1720       140321 :   gcc_assert (etype);
    1721              : 
    1722       140321 :   switch (TREE_CODE (etype))
    1723              :     {
    1724        83183 :     case INTEGER_TYPE:
    1725        83183 :       if (TREE_CODE (ptype) == ARRAY_TYPE
    1726        83183 :           && TYPE_STRING_FLAG (ptype))
    1727              :         n = BT_CHARACTER;
    1728              :       else
    1729              :         {
    1730        59871 :           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        19940 :     case RECORD_TYPE:
    1750        19940 :       if (GFC_CLASS_TYPE_P (etype))
    1751              :         n = BT_CLASS;
    1752              :       else
    1753              :         n = BT_DERIVED;
    1754              :       break;
    1755              : 
    1756         2277 :     case FUNCTION_TYPE:
    1757         2277 :     case VOID_TYPE:
    1758         2277 :       n = BT_VOID;
    1759         2277 :       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        25589 :   switch (n)
    1768              :     {
    1769        23312 :     case BT_CHARACTER:
    1770        23312 :       gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
    1771        23312 :       size = gfc_get_character_len_in_bytes (ptype);
    1772        23312 :       break;
    1773         2277 :     case BT_VOID:
    1774         2277 :       gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
    1775         2277 :       size = size_in_bytes (ptype);
    1776         2277 :       break;
    1777       114732 :     default:
    1778       114732 :       size = size_in_bytes (etype);
    1779       114732 :       break;
    1780              :     }
    1781              : 
    1782       140321 :   gcc_assert (size);
    1783              : 
    1784       140321 :   STRIP_NOPS (size);
    1785       140321 :   size = fold_convert (size_type_node, size);
    1786       140321 :   tmp = get_dtype_type_node ();
    1787       140321 :   field = gfc_advance_chain (TYPE_FIELDS (tmp),
    1788              :                              GFC_DTYPE_ELEM_LEN);
    1789       140321 :   CONSTRUCTOR_APPEND_ELT (v, field,
    1790              :                           fold_convert (TREE_TYPE (field), size));
    1791       140321 :   field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
    1792              :                              GFC_DTYPE_VERSION);
    1793       140321 :   CONSTRUCTOR_APPEND_ELT (v, field,
    1794              :                           build_zero_cst (TREE_TYPE (field)));
    1795              : 
    1796       140321 :   field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
    1797              :                              GFC_DTYPE_RANK);
    1798       140321 :   if (rank >= 0)
    1799       139734 :     CONSTRUCTOR_APPEND_ELT (v, field,
    1800              :                             build_int_cst (TREE_TYPE (field), rank));
    1801              : 
    1802       140321 :   field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
    1803              :                              GFC_DTYPE_TYPE);
    1804       140321 :   CONSTRUCTOR_APPEND_ELT (v, field,
    1805              :                           build_int_cst (TREE_TYPE (field), n));
    1806              : 
    1807       140321 :   dtype = build_constructor (tmp, v);
    1808              : 
    1809       140321 :   return dtype;
    1810              : }
    1811              : 
    1812              : 
    1813              : tree
    1814       113590 : gfc_get_dtype (tree type, int * rank)
    1815              : {
    1816       113590 :   tree dtype;
    1817       113590 :   tree etype;
    1818       113590 :   int irnk;
    1819              : 
    1820       113590 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
    1821              : 
    1822       113590 :   irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
    1823       113590 :   etype = gfc_get_element_type (type);
    1824       113590 :   dtype = gfc_get_dtype_rank_type (irnk, etype);
    1825              : 
    1826       113590 :   GFC_TYPE_ARRAY_DTYPE (type) = dtype;
    1827       113590 :   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       114625 : gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
    1836              :                            bool restricted)
    1837              : {
    1838       114625 :   tree range;
    1839       114625 :   tree type;
    1840       114625 :   tree tmp;
    1841       114625 :   int n;
    1842       114625 :   int known_stride;
    1843       114625 :   int known_offset;
    1844       114625 :   mpz_t offset;
    1845       114625 :   mpz_t stride;
    1846       114625 :   mpz_t delta;
    1847       114625 :   gfc_expr *expr;
    1848              : 
    1849       114625 :   mpz_init_set_ui (offset, 0);
    1850       114625 :   mpz_init_set_ui (stride, 1);
    1851       114625 :   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       114625 :   if (as->rank)
    1857       112797 :     type = make_node (ARRAY_TYPE);
    1858              :   else
    1859         1828 :     type = build_variant_type_copy (etype);
    1860              : 
    1861       114625 :   GFC_ARRAY_TYPE_P (type) = 1;
    1862       114625 :   TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
    1863              : 
    1864       114625 :   known_stride = (packed != PACKED_NO);
    1865       114625 :   known_offset = 1;
    1866       248998 :   for (n = 0; n < as->rank; n++)
    1867              :     {
    1868              :       /* Fill in the stride and bound components of the type.  */
    1869       134373 :       if (known_stride)
    1870       121180 :         tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
    1871              :       else
    1872              :         tmp = NULL_TREE;
    1873       134373 :       GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
    1874              : 
    1875       134373 :       expr = as->lower[n];
    1876       134373 :       if (expr && expr->expr_type == EXPR_CONSTANT)
    1877              :         {
    1878       133583 :           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       134373 :       GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
    1887              : 
    1888       134373 :       if (known_stride)
    1889              :         {
    1890              :           /* Calculate the offset.  */
    1891       120726 :           mpz_mul (delta, stride, as->lower[n]->value.integer);
    1892       120726 :           mpz_sub (offset, offset, delta);
    1893              :         }
    1894              :       else
    1895              :         known_offset = 0;
    1896              : 
    1897       134373 :       expr = as->upper[n];
    1898       134373 :       if (expr && expr->expr_type == EXPR_CONSTANT)
    1899              :         {
    1900       107642 :           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       134373 :       GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    1909              : 
    1910       134373 :       if (known_stride)
    1911              :         {
    1912              :           /* Calculate the stride.  */
    1913       106687 :           mpz_sub (delta, as->upper[n]->value.integer,
    1914       106687 :                    as->lower[n]->value.integer);
    1915       106687 :           mpz_add_ui (delta, delta, 1);
    1916       106687 :           mpz_mul (stride, stride, delta);
    1917              :         }
    1918              : 
    1919              :       /* Only the first stride is known for partial packed arrays.  */
    1920       134373 :       if (packed == PACKED_NO || packed == PACKED_PARTIAL)
    1921        10256 :         known_stride = 0;
    1922              :     }
    1923       117096 :   for (n = as->rank; n < as->rank + as->corank; n++)
    1924              :     {
    1925         2471 :       expr = as->lower[n];
    1926         2471 :       if (expr && expr->expr_type == EXPR_CONSTANT)
    1927         2357 :         tmp = gfc_conv_mpz_to_tree (expr->value.integer,
    1928              :                                     gfc_index_integer_kind);
    1929              :       else
    1930              :         tmp = NULL_TREE;
    1931         2471 :       GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
    1932              : 
    1933         2471 :       expr = as->upper[n];
    1934         2471 :       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         2471 :       if (n < as->rank + as->corank - 1)
    1940          274 :         GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    1941              :     }
    1942              : 
    1943       114625 :   if (known_offset)
    1944              :     {
    1945       104336 :       GFC_TYPE_ARRAY_OFFSET (type) =
    1946       104336 :         gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
    1947              :     }
    1948              :   else
    1949        10289 :     GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
    1950              : 
    1951       114625 :   if (known_stride)
    1952              :     {
    1953        85126 :       GFC_TYPE_ARRAY_SIZE (type) =
    1954        85126 :         gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
    1955              :     }
    1956              :   else
    1957        29499 :     GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
    1958              : 
    1959       114625 :   GFC_TYPE_ARRAY_RANK (type) = as->rank;
    1960       114625 :   GFC_TYPE_ARRAY_CORANK (type) = as->corank;
    1961       114625 :   GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
    1962       114625 :   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       114625 :   GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
    1966       114625 :     build_pointer_type (build_array_type (etype, range));
    1967       114625 :   if (restricted)
    1968       111538 :     GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
    1969       111538 :       build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
    1970              :                             TYPE_QUAL_RESTRICT);
    1971              : 
    1972       114625 :   if (as->rank == 0)
    1973              :     {
    1974         1828 :       if (packed != PACKED_STATIC  || flag_coarray == GFC_FCOARRAY_LIB)
    1975              :         {
    1976         1758 :           type = build_pointer_type (type);
    1977              : 
    1978         1758 :           if (restricted)
    1979         1758 :             type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
    1980              : 
    1981         1758 :           GFC_ARRAY_TYPE_P (type) = 1;
    1982         1758 :           TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
    1983              :         }
    1984              : 
    1985         1828 :       goto array_type_done;
    1986              :     }
    1987              : 
    1988       112797 :   if (known_stride)
    1989              :     {
    1990        83339 :       mpz_sub_ui (stride, stride, 1);
    1991        83339 :       range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
    1992              :     }
    1993              :   else
    1994              :     range = NULL_TREE;
    1995              : 
    1996       112797 :   range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
    1997       112797 :   TYPE_DOMAIN (type) = range;
    1998              : 
    1999       112797 :   build_pointer_type (etype);
    2000       112797 :   TREE_TYPE (type) = etype;
    2001              : 
    2002       112797 :   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       112797 :   if (known_offset)
    2008              :     {
    2009       102508 :       tree gtype = etype, rtype, type_decl;
    2010              : 
    2011       220714 :       for (n = as->rank - 1; n >= 0; n--)
    2012              :         {
    2013       472824 :           rtype = build_range_type (gfc_array_index_type,
    2014       118206 :                                     GFC_TYPE_ARRAY_LBOUND (type, n),
    2015       118206 :                                     GFC_TYPE_ARRAY_UBOUND (type, n));
    2016       118206 :           gtype = build_array_type (gtype, rtype);
    2017              :         }
    2018       102508 :       TYPE_NAME (type) = type_decl = build_decl (input_location,
    2019              :                                                  TYPE_DECL, NULL, gtype);
    2020       102508 :       DECL_ORIGINAL_TYPE (type_decl) = gtype;
    2021              :     }
    2022              : 
    2023       112797 :   if (packed != PACKED_STATIC || !known_stride
    2024        79161 :       || (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        33750 :       type = build_pointer_type (type);
    2029        33750 :       if (restricted)
    2030        32553 :         type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
    2031        33750 :       GFC_ARRAY_TYPE_P (type) = 1;
    2032        33750 :       TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
    2033              :     }
    2034              : 
    2035        79047 : array_type_done:
    2036       114625 :   mpz_clear (offset);
    2037       114625 :   mpz_clear (stride);
    2038       114625 :   mpz_clear (delta);
    2039              : 
    2040       114625 :   return type;
    2041              : }
    2042              : 
    2043              : 
    2044              : /* Return or create the base type for an array descriptor.  */
    2045              : 
    2046              : static tree
    2047       296098 : gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
    2048              : {
    2049       296098 :   tree fat_type, decl, arraytype, *chain = NULL;
    2050       296098 :   char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
    2051       296098 :   int idx;
    2052              : 
    2053              :   /* Assumed-rank array.  */
    2054       296098 :   if (dimen == -1)
    2055            0 :     dimen = GFC_MAX_DIMENSIONS;
    2056              : 
    2057       296098 :   idx = 2 * (codimen + dimen) + restricted;
    2058              : 
    2059       296098 :   gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
    2060              : 
    2061       296098 :   if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
    2062              :     {
    2063         2162 :       if (gfc_array_descriptor_base_caf[idx])
    2064              :         return gfc_array_descriptor_base_caf[idx];
    2065              :     }
    2066       293936 :   else if (gfc_array_descriptor_base[idx])
    2067              :     return gfc_array_descriptor_base[idx];
    2068              : 
    2069              :   /* Build the type node.  */
    2070        34099 :   fat_type = make_node (RECORD_TYPE);
    2071              : 
    2072        34099 :   sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
    2073        34099 :   TYPE_NAME (fat_type) = get_identifier (name);
    2074        34099 :   TYPE_NAMELESS (fat_type) = 1;
    2075              : 
    2076              :   /* Add the data member as the first element of the descriptor.  */
    2077        34099 :   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        34099 :   decl = gfc_add_field_to_struct_1 (fat_type,
    2085              :                                     get_identifier ("offset"),
    2086              :                                     gfc_array_index_type, &chain);
    2087        34099 :   suppress_warning (decl);
    2088              : 
    2089              :   /* Add the dtype component.  */
    2090        34099 :   decl = gfc_add_field_to_struct_1 (fat_type,
    2091              :                                     get_identifier ("dtype"),
    2092              :                                     get_dtype_type_node (), &chain);
    2093        34099 :   suppress_warning (decl);
    2094              : 
    2095              :   /* Add the span component.  */
    2096        34099 :   decl = gfc_add_field_to_struct_1 (fat_type,
    2097              :                                     get_identifier ("span"),
    2098              :                                     gfc_array_index_type, &chain);
    2099        34099 :   suppress_warning (decl);
    2100              : 
    2101              :   /* Build the array type for the stride and bound components.  */
    2102        34099 :   if (dimen + codimen > 0)
    2103              :     {
    2104        31393 :       arraytype =
    2105        31393 :         build_array_type (gfc_get_desc_dim_type (),
    2106              :                           build_range_type (gfc_array_index_type,
    2107              :                                             gfc_index_zero_node,
    2108        31393 :                                             gfc_rank_cst[codimen + dimen - 1]));
    2109              : 
    2110        31393 :       decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
    2111              :                                         arraytype, &chain);
    2112        31393 :       suppress_warning (decl);
    2113              :     }
    2114              : 
    2115        34099 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    2116              :     {
    2117         1634 :       decl = gfc_add_field_to_struct_1 (fat_type,
    2118              :                                         get_identifier ("token"),
    2119              :                                         prvoid_type_node, &chain);
    2120         1634 :       suppress_warning (decl);
    2121              :     }
    2122              : 
    2123              :   /* Finish off the type.  */
    2124        34099 :   gfc_finish_type (fat_type);
    2125        34099 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
    2126              : 
    2127        34099 :   if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
    2128          884 :     gfc_array_descriptor_base_caf[idx] = fat_type;
    2129              :   else
    2130        33215 :     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       148049 : 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       148049 :   char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
    2144       148049 :   tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
    2145       148049 :   const char *type_name;
    2146       148049 :   int n;
    2147              : 
    2148       148049 :   base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
    2149       148049 :   fat_type = build_distinct_type_copy (base_type);
    2150              :   /* Unshare TYPE_FIELDs.  */
    2151       887328 :   for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
    2152              :     {
    2153       739279 :       tree next = DECL_CHAIN (*tp);
    2154       739279 :       *tp = copy_node (*tp);
    2155       739279 :       DECL_CONTEXT (*tp) = fat_type;
    2156       739279 :       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       148049 :   base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
    2161       148049 :   TYPE_CANONICAL (fat_type) = base_type;
    2162       148049 :   TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
    2163              :   /* Arrays of unknown type must alias with all array descriptors.  */
    2164       148049 :   TYPE_TYPELESS_STORAGE (base_type) = 1;
    2165       148049 :   TYPE_TYPELESS_STORAGE (fat_type) = 1;
    2166       148049 :   gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
    2167              : 
    2168       148049 :   tmp = etype;
    2169       148049 :   if (TREE_CODE (tmp) == ARRAY_TYPE
    2170       148049 :       && TYPE_STRING_FLAG (tmp))
    2171        23895 :     tmp = TREE_TYPE (etype);
    2172       148049 :   tmp = TYPE_NAME (tmp);
    2173       148049 :   if (tmp && TREE_CODE (tmp) == TYPE_DECL)
    2174       123019 :     tmp = DECL_NAME (tmp);
    2175       123019 :   if (tmp)
    2176       144459 :     type_name = IDENTIFIER_POINTER (tmp);
    2177              :   else
    2178              :     type_name = "unknown";
    2179       148049 :   sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
    2180              :            GFC_MAX_SYMBOL_LEN, type_name);
    2181       148049 :   TYPE_NAME (fat_type) = get_identifier (name);
    2182       148049 :   TYPE_NAMELESS (fat_type) = 1;
    2183              : 
    2184       148049 :   GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
    2185       148049 :   TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
    2186              : 
    2187       148049 :   GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
    2188       148049 :   GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
    2189       148049 :   GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
    2190       148049 :   GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
    2191              : 
    2192              :   /* Build an array descriptor record type.  */
    2193       148049 :   if (packed != 0)
    2194        34542 :     stride = gfc_index_one_node;
    2195              :   else
    2196              :     stride = NULL_TREE;
    2197       465330 :   for (n = 0; n < dimen + codimen; n++)
    2198              :     {
    2199       319138 :       if (n < dimen)
    2200       316134 :         GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
    2201              : 
    2202       319138 :       if (lbound)
    2203       319138 :         lower = lbound[n];
    2204              :       else
    2205              :         lower = NULL_TREE;
    2206              : 
    2207       319138 :       if (lower != NULL_TREE)
    2208              :         {
    2209       165752 :           if (INTEGER_CST_P (lower))
    2210       164663 :             GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
    2211              :           else
    2212              :             lower = NULL_TREE;
    2213              :         }
    2214              : 
    2215       319138 :       if (codimen && n == dimen + codimen - 1)
    2216              :         break;
    2217              : 
    2218       317281 :       upper = ubound[n];
    2219       317281 :       if (upper != NULL_TREE)
    2220              :         {
    2221       133253 :           if (INTEGER_CST_P (upper))
    2222       100556 :             GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
    2223              :           else
    2224              :             upper = NULL_TREE;
    2225              :         }
    2226              : 
    2227       317281 :       if (n >= dimen)
    2228         1147 :         continue;
    2229              : 
    2230       316134 :       if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
    2231              :         {
    2232        28139 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2233              :                                  gfc_array_index_type, upper, lower);
    2234        28139 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2235              :                                  gfc_array_index_type, tmp,
    2236              :                                  gfc_index_one_node);
    2237        28139 :           stride = fold_build2_loc (input_location, MULT_EXPR,
    2238              :                                     gfc_array_index_type, tmp, stride);
    2239              :           /* Check the folding worked.  */
    2240        28139 :           gcc_assert (INTEGER_CST_P (stride));
    2241              :         }
    2242              :       else
    2243              :         stride = NULL_TREE;
    2244              :     }
    2245       148049 :   GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
    2246              : 
    2247              :   /* TODO: known offsets for descriptors.  */
    2248       148049 :   GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
    2249              : 
    2250       148049 :   if (dimen == 0)
    2251              :     {
    2252         6779 :       arraytype =  build_pointer_type (etype);
    2253         6779 :       if (restricted)
    2254         6096 :         arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
    2255              : 
    2256         6779 :       GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
    2257         6779 :       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       141270 :   if (stride)
    2263        21805 :     rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
    2264              :                               int_const_binop (MINUS_EXPR, stride,
    2265        43610 :                                                build_int_cst (TREE_TYPE (stride), 1)));
    2266              :   else
    2267       119465 :     rtype = gfc_array_range_type;
    2268       141270 :   arraytype = build_array_type (etype, rtype);
    2269       141270 :   arraytype = build_pointer_type (arraytype);
    2270       141270 :   if (restricted)
    2271        67375 :     arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
    2272       141270 :   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       141270 :   {
    2279       141270 :     struct array_descr_info info;
    2280       141270 :     gfc_get_array_descr_info (fat_type, &info);
    2281       141270 :     gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
    2282              :   }
    2283              : 
    2284       141270 :   return fat_type;
    2285              : }
    2286              : 
    2287              : /* Build a pointer type. This function is called from gfc_sym_type().  */
    2288              : 
    2289              : static tree
    2290        16505 : 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        16505 :     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         4108 : mirror_fields (tree to, tree from)
    2306              : {
    2307         4108 :   tree fto, ffrom;
    2308         4108 :   tree *chain;
    2309              : 
    2310              :   /* Forward to the end of TOs fields.  */
    2311         4108 :   fto = TYPE_FIELDS (to);
    2312         4108 :   ffrom = TYPE_FIELDS (from);
    2313         4108 :   chain = &TYPE_FIELDS (to);
    2314         4108 :   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        19823 :   for (; ffrom; ffrom = DECL_CHAIN (ffrom))
    2324              :     {
    2325        15715 :       tree newfield = copy_node (ffrom);
    2326        15715 :       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        15715 :       DECL_CHAIN (newfield) = NULL_TREE;
    2333        15715 :       *chain = newfield;
    2334        15715 :       chain = &DECL_CHAIN (newfield);
    2335              : 
    2336        15715 :       if (TREE_CODE (ffrom) == FIELD_DECL)
    2337              :         {
    2338        15715 :           tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
    2339        15715 :           TREE_TYPE (newfield) = elemtype;
    2340              :         }
    2341              :     }
    2342         4108 :   *chain = NULL_TREE;
    2343         4108 : }
    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       272692 : gfc_nonrestricted_type (tree t)
    2350              : {
    2351       272692 :   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       272692 :   if (!TYPE_SIZE (t))
    2356              :     return t;
    2357              : 
    2358       259933 :   if (!TYPE_LANG_SPECIFIC (t))
    2359       102904 :     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       259933 :   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       251526 :   if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
    2369       143657 :     return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
    2370              : 
    2371              :   /* Mark this type.  */
    2372       107869 :   TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
    2373              : 
    2374       107869 :   switch (TREE_CODE (t))
    2375              :     {
    2376              :       default:
    2377              :         break;
    2378              : 
    2379        41615 :       case POINTER_TYPE:
    2380        41615 :       case REFERENCE_TYPE:
    2381        41615 :         {
    2382        41615 :           tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
    2383        41615 :           if (totype == TREE_TYPE (t))
    2384              :             ret = t;
    2385         1437 :           else if (TREE_CODE (t) == POINTER_TYPE)
    2386         1437 :             ret = build_pointer_type (totype);
    2387              :           else
    2388            0 :             ret = build_reference_type (totype);
    2389        83230 :           ret = build_qualified_type (ret,
    2390        41615 :                                       TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
    2391              :         }
    2392        41615 :         break;
    2393              : 
    2394         6159 :       case ARRAY_TYPE:
    2395         6159 :         {
    2396         6159 :           tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
    2397         6159 :           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        29631 :       case RECORD_TYPE:
    2421        29631 :       case UNION_TYPE:
    2422        29631 :       case QUAL_UNION_TYPE:
    2423        29631 :         {
    2424        29631 :           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       150613 :           for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
    2436       125090 :             if (TREE_CODE (field) == FIELD_DECL)
    2437              :               {
    2438       125090 :                 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
    2439       125090 :                 if (elemtype != TREE_TYPE (field))
    2440              :                   break;
    2441              :               }
    2442        29631 :           if (!field)
    2443              :             break;
    2444         4108 :           ret = build_variant_type_copy (t);
    2445         4108 :           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         4108 :           TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
    2452         4108 :           mirror_fields (ret, t);
    2453              :         }
    2454         4108 :         break;
    2455              :     }
    2456              : 
    2457       107869 :   TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
    2458       107869 :   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       413294 : gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
    2471              : {
    2472       413294 :   tree type;
    2473       413294 :   int byref;
    2474       413294 :   bool restricted;
    2475              : 
    2476              :   /* Procedure Pointers inside COMMON blocks.  */
    2477       413294 :   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       413264 :   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       413264 :   if (sym->backend_decl && !sym->attr.function)
    2493          493 :     return TREE_TYPE (sym->backend_decl);
    2494              : 
    2495       412771 :   if (sym->attr.result
    2496         8219 :       && 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       412771 :   if (sym->ts.type == BT_CHARACTER
    2504       412771 :       && ((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       410914 :     type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
    2518              : 
    2519       412771 :   if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
    2520       150740 :       && !sym->pass_as_value)
    2521              :     byref = 1;
    2522              :   else
    2523       263331 :     byref = 0;
    2524              : 
    2525       383152 :   restricted = (!sym->attr.target && !IS_POINTER (sym)
    2526       778040 :                 && !IS_PROC_POINTER (sym) && !sym->attr.cray_pointee);
    2527        48203 :   if (!restricted)
    2528        48203 :     type = gfc_nonrestricted_type (type);
    2529              : 
    2530              :   /* Dummy argument to a bind(C) procedure.  */
    2531       412771 :   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       409131 :   else if (sym->attr.dimension || sym->attr.codimension)
    2535              :     {
    2536        98314 :       if (gfc_is_nodesc_array (sym))
    2537              :         {
    2538              :           /* If this is a character argument of unknown length, just use the
    2539              :              base type.  */
    2540        53543 :           if (sym->ts.type != BT_CHARACTER
    2541         5833 :               || !(sym->attr.dummy || sym->attr.function)
    2542         1906 :               || sym->ts.u.cl->backend_decl)
    2543              :             {
    2544        53121 :               type = gfc_get_nodesc_array_type (type, sym->as,
    2545              :                                                 byref ? PACKED_FULL
    2546              :                                                       : PACKED_STATIC,
    2547              :                                                 restricted);
    2548        53121 :               byref = 0;
    2549              :             }
    2550              :         }
    2551              :       else
    2552              :         {
    2553        44771 :           enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
    2554        44771 :           if (sym->attr.pointer)
    2555         7193 :             akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
    2556              :                                          : GFC_ARRAY_POINTER;
    2557        37578 :           else if (sym->attr.allocatable)
    2558        12042 :             akind = GFC_ARRAY_ALLOCATABLE;
    2559        44771 :           type = gfc_build_array_type (type, sym->as, akind, restricted,
    2560        44771 :                                        sym->attr.contiguous, sym->as->corank);
    2561              :         }
    2562              :     }
    2563              :   else
    2564              :     {
    2565       306511 :       if (sym->attr.allocatable || sym->attr.pointer
    2566       608171 :           || gfc_is_associate_pointer (sym))
    2567        16505 :         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       412771 :   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       135458 :       if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
    2578       135458 :           || sym->attr.optional
    2579       116956 :           || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
    2580        20159 :         type = build_pointer_type (type);
    2581              :       else
    2582       115299 :         type = build_reference_type (type);
    2583              : 
    2584       135458 :       if (restricted)
    2585       128096 :         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       336876 : gfc_finish_type (tree type)
    2595              : {
    2596       336876 :   tree decl;
    2597              : 
    2598       336876 :   decl = build_decl (input_location,
    2599              :                      TYPE_DECL, NULL_TREE, type);
    2600       336876 :   TYPE_STUB_DECL (type) = decl;
    2601       336876 :   layout_type (type);
    2602       336876 :   rest_of_type_compilation (type, 1);
    2603       336876 :   rest_of_decl_compilation (decl, 1, 0);
    2604       336876 : }
    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      4976460 : gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
    2614              : {
    2615      4976460 :   tree decl = build_decl (input_location, FIELD_DECL, name, type);
    2616              : 
    2617      4976460 :   DECL_CONTEXT (decl) = context;
    2618      4976460 :   DECL_CHAIN (decl) = NULL_TREE;
    2619      4976460 :   if (TYPE_FIELDS (context) == NULL_TREE)
    2620       329856 :     TYPE_FIELDS (context) = decl;
    2621      4976460 :   if (chain != NULL)
    2622              :     {
    2623      4976460 :       if (*chain != NULL)
    2624      4646604 :         **chain = decl;
    2625      4976460 :       *chain = &DECL_CHAIN (decl);
    2626              :     }
    2627              : 
    2628      4976460 :   return decl;
    2629              : }
    2630              : 
    2631              : /* Like `gfc_add_field_to_struct_1', but adds alignment
    2632              :    information.  */
    2633              : 
    2634              : tree
    2635      4602043 : gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
    2636              : {
    2637      4602043 :   tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
    2638              : 
    2639      4602043 :   DECL_INITIAL (decl) = 0;
    2640      4602043 :   SET_DECL_ALIGN (decl, 0);
    2641      4602043 :   DECL_USER_ALIGN (decl) = 0;
    2642              : 
    2643      4602043 :   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       347536 : gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
    2653              :                            bool from_gsym)
    2654              : {
    2655       347536 :   gfc_component *to_cm;
    2656       347536 :   gfc_component *from_cm;
    2657              : 
    2658       347536 :   if (from == to)
    2659              :     return 1;
    2660              : 
    2661       304123 :   if (from->backend_decl == NULL
    2662       304123 :         || !gfc_compare_derived_types (from, to))
    2663       289330 :     return 0;
    2664              : 
    2665        14793 :   to->backend_decl = from->backend_decl;
    2666              : 
    2667        14793 :   to_cm = to->components;
    2668        14793 :   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        55953 :   for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
    2677              :     {
    2678        41160 :       to_cm->backend_decl = from_cm->backend_decl;
    2679        41160 :       to_cm->caf_token = from_cm->caf_token;
    2680        41160 :       if (from_cm->ts.type == BT_UNION)
    2681           28 :         gfc_get_union_type (to_cm->ts.u.derived);
    2682        41132 :       else if (from_cm->ts.type == BT_DERIVED
    2683        14164 :           && (!from_cm->attr.pointer || from_gsym))
    2684        12882 :         gfc_get_derived_type (to_cm->ts.u.derived);
    2685        28250 :       else if (from_cm->ts.type == BT_CLASS
    2686          742 :                && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
    2687          735 :         gfc_get_derived_type (to_cm->ts.u.derived);
    2688        27515 :       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        34701 : gfc_get_ppc_type (gfc_component* c)
    2700              : {
    2701        34701 :   tree t;
    2702              : 
    2703              :   /* Explicit interface.  */
    2704        34701 :   if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
    2705         3472 :     return build_pointer_type (gfc_get_function_type (c->ts.interface));
    2706              : 
    2707              :   /* Implicit interface (only return value may be known).  */
    2708        31229 :   if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
    2709            9 :     t = gfc_typenode_for_spec (&c->ts);
    2710              :   else
    2711        31220 :     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        31229 :   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       191385 : gfc_get_derived_type (gfc_symbol * derived, int codimen)
    2829              : {
    2830       191385 :   tree typenode = NULL, field = NULL, field_type = NULL;
    2831       191385 :   tree canonical = NULL_TREE;
    2832       191385 :   tree *chain = NULL;
    2833       191385 :   bool got_canonical = false;
    2834       191385 :   bool unlimited_entity = false;
    2835       191385 :   gfc_component *c;
    2836       191385 :   gfc_namespace *ns;
    2837       191385 :   tree tmp;
    2838       191385 :   bool coarray_flag, class_coarray_flag;
    2839              : 
    2840       382770 :   coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
    2841       191385 :                  && derived->module && !derived->attr.vtype;
    2842       382770 :   class_coarray_flag = derived->components
    2843       179496 :                        && derived->components->ts.type == BT_DERIVED
    2844        58292 :                        && strcmp (derived->components->name, "_data") == 0
    2845        34019 :                        && derived->components->attr.codimension
    2846       192052 :                        && derived->components->as->cotype == AS_EXPLICIT;
    2847              : 
    2848       191385 :   gcc_assert (!derived->attr.pdt_template);
    2849              : 
    2850       191385 :   if (derived->attr.unlimited_polymorphic
    2851       187806 :       || (flag_coarray == GFC_FCOARRAY_LIB
    2852         3858 :           && 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         3702 :     return ptr_type_node;
    2857              : 
    2858       187683 :   if (flag_coarray != GFC_FCOARRAY_LIB
    2859       183948 :       && 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       187369 :   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       187369 :   if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
    2870              :     {
    2871        15001 :       if (derived->backend_decl)
    2872              :         return derived->backend_decl;
    2873              : 
    2874         5291 :       if (derived->intmod_sym_id == ISOCBINDING_PTR)
    2875         2857 :         derived->backend_decl = ptr_type_node;
    2876              :       else
    2877         2434 :         derived->backend_decl = pfunc_type_node;
    2878              : 
    2879         5291 :       derived->ts.kind = gfc_index_integer_kind;
    2880         5291 :       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         5291 :       derived->ts.f90_type = BT_VOID;
    2885              : 
    2886         5291 :       return derived->backend_decl;
    2887              :     }
    2888              : 
    2889              :   /* If use associated, use the module type for this one.  */
    2890       172368 :   if (derived->backend_decl == NULL
    2891        43445 :       && (derived->attr.use_assoc || derived->attr.used_in_submodule)
    2892        11095 :       && derived->module
    2893       183463 :       && gfc_get_module_backend_decl (derived))
    2894        10669 :     goto copy_derived_types;
    2895              : 
    2896              :   /* The derived types from an earlier namespace can be used as the
    2897              :      canonical type.  */
    2898       161699 :   if (derived->backend_decl == NULL
    2899        32776 :       && !derived->attr.use_assoc
    2900        32353 :       && !derived->attr.used_in_submodule
    2901        32350 :       && gfc_global_ns_list)
    2902              :     {
    2903         8128 :       for (ns = gfc_global_ns_list;
    2904        40472 :            ns->translated && !got_canonical;
    2905         8128 :            ns = ns->sibling)
    2906              :         {
    2907         8128 :           if (ns->derived_types)
    2908              :             {
    2909        29855 :               for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
    2910              :                    dt = dt->dt_next)
    2911              :                 {
    2912        29610 :                   gfc_copy_dt_decls_ifequal (dt, derived, true);
    2913        29610 :                   if (derived->backend_decl)
    2914          392 :                     got_canonical = true;
    2915        29610 :                   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        32344 :   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       161699 :   if (derived->backend_decl
    2936       161699 :       && (!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       128855 :       if (TYPE_FIELDS (derived->backend_decl))
    2942              :         return derived->backend_decl;
    2943         4814 :       else if (derived->attr.abstract
    2944          689 :                && 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        32844 :       typenode = make_node (RECORD_TYPE);
    2970        32844 :       TYPE_NAME (typenode) = get_identifier (derived->name);
    2971        32844 :       TYPE_PACKED (typenode) = flag_pack_derived;
    2972        32844 :       derived->backend_decl = typenode;
    2973              :     }
    2974              : 
    2975        37658 :   if (derived->components
    2976        30643 :       && derived->components->ts.type == BT_DERIVED
    2977        10652 :       && startswith (derived->name, "__class")
    2978         7498 :       && strcmp (derived->components->name, "_data") == 0
    2979        45156 :       && 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       154714 :   for (c = derived->components; c; c = c->next)
    2988              :     {
    2989       117056 :       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       117056 :       if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
    2993        76886 :         continue;
    2994              : 
    2995        40170 :       const bool incomplete_type
    2996        40170 :         = c->ts.u.derived->backend_decl
    2997        33643 :           && TREE_CODE (c->ts.u.derived->backend_decl) == RECORD_TYPE
    2998        71234 :           && !(TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)
    2999        17540 :                && TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)->size);
    3000        80340 :       const bool pointer_component
    3001        40170 :         = 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        40170 :       if (c->ts.u.derived->backend_decl == NULL
    3007        33643 :           || (c->attr.codimension && c->as->corank != codimen)
    3008        33352 :           || !(incomplete_type && pointer_component))
    3009              :         {
    3010        10319 :           int local_codim = c->attr.codimension ? c->as->corank: codimen;
    3011        10319 :           c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
    3012              :                                                                 local_codim);
    3013              :         }
    3014              : 
    3015        40170 :       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        37658 :   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       154500 :   for (c = derived->components; c; c = c->next)
    3042              :     {
    3043       233834 :       bool same_alloc_type = c->attr.allocatable
    3044       116917 :                              && 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       116917 :       if (c->attr.proc_pointer
    3049        34745 :           && (c->ts.type != BT_DERIVED || (c->ts.u.derived
    3050          185 :                     && !gfc_compare_derived_types (derived, c->ts.u.derived)))
    3051       151638 :           && (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        34701 :         field_type = gfc_get_ppc_type (c);
    3054        82216 :       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        82172 :       else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    3060        39494 :         field_type = c->ts.u.derived->backend_decl;
    3061        42678 :       else if (c->attr.caf_token)
    3062          661 :         field_type = pvoid_type_node;
    3063              :       else
    3064              :         {
    3065        42017 :           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        40614 :           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        42017 :           field_type = gfc_typenode_for_spec (&c->ts, codimen);
    3077              :         }
    3078              : 
    3079              :       /* This returns an array descriptor type.  Initialization may be
    3080              :          required.  */
    3081       116917 :       if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
    3082              :         {
    3083         8698 :           if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
    3084              :             {
    3085         6995 :               enum gfc_array_kind akind;
    3086         6995 :               bool is_ptr = ((c == derived->components
    3087         4622 :                               && derived->components->ts.type == BT_DERIVED
    3088         3420 :                               && startswith (derived->name, "__class")
    3089         2850 :                               && (strcmp (derived->components->name, "_data")
    3090              :                                   == 0))
    3091        11617 :                              ? c->attr.class_pointer : c->attr.pointer);
    3092         6995 :               if (is_ptr)
    3093         1816 :                 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
    3094              :                                            : GFC_ARRAY_POINTER;
    3095         5179 :               else if (c->attr.allocatable)
    3096              :                 akind = GFC_ARRAY_ALLOCATABLE;
    3097         1196 :               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         1058 :                 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         6995 :               field_type = gfc_build_array_type
    3110         6995 :                 (
    3111         6995 :                   field_type, c->as, akind, !c->attr.target && !c->attr.pointer,
    3112              :                   c->attr.contiguous,
    3113         6995 :                   c->attr.codimension || c->attr.pointer ? codimen : 0
    3114              :                 );
    3115         6995 :             }
    3116              :           else
    3117         1703 :             field_type = gfc_get_nodesc_array_type (field_type, c->as,
    3118              :                                                     PACKED_STATIC,
    3119              :                                                     !c->attr.target);
    3120              :         }
    3121       108219 :       else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
    3122        35713 :                && !c->attr.proc_pointer
    3123        35547 :                && !(unlimited_entity && c == derived->components))
    3124        34994 :         field_type = build_pointer_type (field_type);
    3125              : 
    3126       116917 :       if (c->attr.pointer || same_alloc_type)
    3127        35889 :         field_type = gfc_nonrestricted_type (field_type);
    3128              : 
    3129              :       /* vtype fields can point to different types to the base type.  */
    3130       116917 :       if (c->ts.type == BT_DERIVED
    3131        38908 :             && c->ts.u.derived && c->ts.u.derived->attr.vtype)
    3132        17229 :           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       116917 :       if (c->ts.type == BT_CLASS)
    3137              :         {
    3138         1109 :           if (POINTER_TYPE_P (field_type))
    3139          338 :             GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
    3140              :           else
    3141          771 :             GFC_CLASS_TYPE_P (field_type) = 1;
    3142              :         }
    3143              : 
    3144       116917 :       field = gfc_add_field_to_struct (typenode,
    3145              :                                        get_identifier (c->name),
    3146              :                                        field_type, &chain);
    3147       116917 :       if (GFC_LOCUS_IS_SET (c->loc))
    3148       116917 :         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       116917 :       gfc_finish_decl_attrs (field, &c->attr);
    3153              : 
    3154       116917 :       DECL_PACKED (field) |= TYPE_PACKED (typenode);
    3155              : 
    3156       116917 :       gcc_assert (field);
    3157              :       /* Overwrite for class array to supply different bounds for different
    3158              :          types.  */
    3159       116917 :       if (class_coarray_flag || !c->backend_decl || c->attr.caf_token)
    3160       115471 :         c->backend_decl = field;
    3161              : 
    3162       116917 :       if (c->attr.pointer && (c->attr.dimension || c->attr.codimension)
    3163         2829 :           && !(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        37583 :   if (canonical)
    3169          392 :     TYPE_CANONICAL (typenode) = canonical;
    3170              : 
    3171        37583 :   gfc_finish_type (typenode);
    3172        37583 :   gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
    3173        37583 :   if (derived->module && derived->ns->proc_name
    3174        21012 :       && derived->ns->proc_name->attr.flavor == FL_MODULE)
    3175              :     {
    3176        19823 :       if (derived->ns->proc_name->backend_decl
    3177        19808 :           && TREE_CODE (derived->ns->proc_name->backend_decl)
    3178              :              == NAMESPACE_DECL)
    3179              :         {
    3180        19808 :           TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
    3181        19808 :           DECL_CONTEXT (TYPE_STUB_DECL (typenode))
    3182        39616 :             = derived->ns->proc_name->backend_decl;
    3183              :         }
    3184              :     }
    3185              : 
    3186        37583 :   derived->backend_decl = typenode;
    3187              : 
    3188        48252 : copy_derived_types:
    3189              : 
    3190        48252 :   if (!derived->attr.vtype)
    3191        88673 :     for (c = derived->components; c; c = c->next)
    3192              :       {
    3193              :         /* Do not add a caf_token field for class container components.  */
    3194        53280 :         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       306555 :   for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
    3210              :     {
    3211       305569 :       gfc_copy_dt_decls_ifequal (derived, dt, false);
    3212       305569 :       if (dt->dt_next == gfc_derived_types)
    3213              :         break;
    3214              :     }
    3215              : 
    3216        48252 :   return derived->backend_decl;
    3217              : }
    3218              : 
    3219              : 
    3220              : bool
    3221       933115 : gfc_return_by_reference (gfc_symbol * sym)
    3222              : {
    3223       933115 :   if (!sym->attr.function)
    3224              :     return 0;
    3225              : 
    3226       465636 :   if (sym->attr.dimension)
    3227              :     return 1;
    3228              : 
    3229       392917 :   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       371143 :   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          214 : gfc_get_entry_result_type (gfc_symbol *sym)
    3253              : {
    3254          214 :   tree type;
    3255              : 
    3256          214 :   type = gfc_sym_type (sym->result);
    3257              : 
    3258              :   /* Mixed ENTRY master unions must use the ABI return type of each entry.
    3259              :      Under -ff2c, default REAL entries return C double even though their
    3260              :      Fortran result symbol remains default REAL.  */
    3261          214 :   if (flag_f2c
    3262            2 :       && sym->ts.type == BT_REAL
    3263            1 :       && sym->ts.kind == gfc_default_real_kind
    3264            1 :       && !sym->attr.pointer
    3265            1 :       && !sym->attr.allocatable
    3266            1 :       && !sym->attr.always_explicit)
    3267            1 :     type = gfc_get_real_type (gfc_default_double_kind);
    3268              : 
    3269          214 :   return type;
    3270              : }
    3271              : 
    3272              : static tree
    3273           98 : gfc_get_mixed_entry_union (gfc_namespace *ns)
    3274              : {
    3275           98 :   tree type;
    3276           98 :   tree *chain = NULL;
    3277           98 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    3278           98 :   gfc_entry_list *el, *el2;
    3279              : 
    3280           98 :   gcc_assert (ns->proc_name->attr.mixed_entry_master);
    3281           98 :   gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
    3282              : 
    3283           98 :   snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
    3284              : 
    3285              :   /* Build the type node.  */
    3286           98 :   type = make_node (UNION_TYPE);
    3287              : 
    3288           98 :   TYPE_NAME (type) = get_identifier (name);
    3289              : 
    3290          312 :   for (el = ns->entries; el; el = el->next)
    3291              :     {
    3292              :       /* Search for duplicates.  */
    3293          348 :       for (el2 = ns->entries; el2 != el; el2 = el2->next)
    3294          134 :         if (el2->sym->result == el->sym->result)
    3295              :           break;
    3296              : 
    3297          214 :       if (el == el2)
    3298          428 :         gfc_add_field_to_struct_1 (type,
    3299          214 :                                    get_identifier (el->sym->result->name),
    3300              :                                    gfc_get_entry_result_type (el->sym),
    3301              :                                    &chain);
    3302              :     }
    3303              : 
    3304              :   /* Finish off the type.  */
    3305           98 :   gfc_finish_type (type);
    3306           98 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
    3307           98 :   return type;
    3308              : }
    3309              : 
    3310              : /* Create a "fn spec" based on the formal arguments;
    3311              :    cf. create_function_arglist.  */
    3312              : 
    3313              : static tree
    3314       107988 : create_fn_spec (gfc_symbol *sym, tree fntype)
    3315              : {
    3316       107988 :   char spec[150];
    3317       107988 :   size_t spec_len;
    3318       107988 :   gfc_formal_arglist *f;
    3319       107988 :   tree tmp;
    3320              : 
    3321       107988 :   memset (&spec, 0, sizeof (spec));
    3322       107988 :   spec[0] = '.';
    3323       107988 :   spec[1] = ' ';
    3324       107988 :   spec_len = 2;
    3325              : 
    3326       107988 :   if (sym->attr.entry_master)
    3327              :     {
    3328          634 :       spec[spec_len++] = 'R';
    3329          634 :       spec[spec_len++] = ' ';
    3330              :     }
    3331       107988 :   if (gfc_return_by_reference (sym))
    3332              :     {
    3333        10444 :       gfc_symbol *result = sym->result ? sym->result : sym;
    3334              : 
    3335        10444 :       if (result->attr.pointer || sym->attr.proc_pointer)
    3336              :         {
    3337          334 :           spec[spec_len++] = '.';
    3338          334 :           spec[spec_len++] = ' ';
    3339              :         }
    3340              :       else
    3341              :         {
    3342        10110 :           spec[spec_len++] = 'w';
    3343        10110 :           spec[spec_len++] = ' ';
    3344              :         }
    3345        10444 :       if (sym->ts.type == BT_CHARACTER)
    3346              :         {
    3347         2847 :           if (!sym->ts.u.cl->length
    3348         1552 :               && (sym->attr.allocatable || sym->attr.pointer))
    3349          299 :             spec[spec_len++] = 'w';
    3350              :           else
    3351         2548 :             spec[spec_len++] = 'R';
    3352         2847 :           spec[spec_len++] = ' ';
    3353              :         }
    3354              :     }
    3355              : 
    3356       254633 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    3357       146645 :     if (spec_len < sizeof (spec))
    3358              :       {
    3359       146645 :         bool is_class = false;
    3360       146645 :         bool is_pointer = false;
    3361              : 
    3362       146645 :         if (f->sym)
    3363              :           {
    3364         9682 :             is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
    3365       156223 :               && f->sym->attr.class_ok;
    3366       146541 :             is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
    3367       136859 :                                   : f->sym->attr.pointer;
    3368              :           }
    3369              : 
    3370       146645 :         if (f->sym == NULL || is_pointer || f->sym->attr.target
    3371       139516 :             || f->sym->attr.external || f->sym->attr.cray_pointer
    3372       139039 :             || (f->sym->ts.type == BT_DERIVED
    3373        26251 :                 && (f->sym->ts.u.derived->attr.proc_pointer_comp
    3374        25590 :                     || f->sym->ts.u.derived->attr.pointer_comp))
    3375       136047 :             || (is_class
    3376         8732 :                 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
    3377         8071 :                     || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
    3378       134654 :             || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
    3379              :           {
    3380        20042 :             spec[spec_len++] = '.';
    3381        20042 :             spec[spec_len++] = ' ';
    3382              :           }
    3383       126603 :         else if (f->sym->attr.intent == INTENT_IN)
    3384              :           {
    3385        60331 :             spec[spec_len++] = 'r';
    3386        60331 :             spec[spec_len++] = ' ';
    3387              :           }
    3388        66272 :         else if (f->sym)
    3389              :           {
    3390        66272 :             spec[spec_len++] = 'w';
    3391        66272 :             spec[spec_len++] = ' ';
    3392              :           }
    3393              :       }
    3394              : 
    3395       107988 :   tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
    3396       107988 :   tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
    3397       107988 :   return build_type_attribute_variant (fntype, tmp);
    3398              : }
    3399              : 
    3400              : 
    3401              : /* NOTE: The returned function type must match the argument list created by
    3402              :    create_function_arglist.  */
    3403              : 
    3404              : tree
    3405       110113 : gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
    3406              :                        const char *fnspec)
    3407              : {
    3408       110113 :   tree type;
    3409       110113 :   vec<tree, va_gc> *typelist = NULL;
    3410       110113 :   vec<tree, va_gc> *hidden_typelist = NULL;
    3411       110113 :   gfc_formal_arglist *f;
    3412       110113 :   gfc_symbol *arg;
    3413       110113 :   int alternate_return = 0;
    3414       110113 :   bool is_varargs = true;
    3415              : 
    3416              :   /* Make sure this symbol is a function, a subroutine or the main
    3417              :      program.  */
    3418       110113 :   gcc_assert (sym->attr.flavor == FL_PROCEDURE
    3419              :               || sym->attr.flavor == FL_PROGRAM);
    3420              : 
    3421              :   /* To avoid recursing infinitely on recursive types, we use error_mark_node
    3422              :      so that they can be detected here and handled further down.  */
    3423       110113 :   if (sym->backend_decl == NULL)
    3424       109880 :     sym->backend_decl = error_mark_node;
    3425          233 :   else if (sym->backend_decl == error_mark_node)
    3426           47 :     goto arg_type_list_done;
    3427          186 :   else if (sym->attr.proc_pointer)
    3428            0 :     return TREE_TYPE (TREE_TYPE (sym->backend_decl));
    3429              :   else
    3430          186 :     return TREE_TYPE (sym->backend_decl);
    3431              : 
    3432       109880 :   if (sym->attr.entry_master)
    3433              :     /* Additional parameter for selecting an entry point.  */
    3434          634 :     vec_safe_push (typelist, gfc_array_index_type);
    3435              : 
    3436       109880 :   if (sym->result)
    3437        33024 :     arg = sym->result;
    3438              :   else
    3439              :     arg = sym;
    3440              : 
    3441       109880 :   if (arg->ts.type == BT_CHARACTER)
    3442         3150 :     gfc_conv_const_charlen (arg->ts.u.cl);
    3443              : 
    3444              :   /* Some functions we use an extra parameter for the return value.  */
    3445       109880 :   if (gfc_return_by_reference (sym))
    3446              :     {
    3447        12228 :       type = gfc_sym_type (arg);
    3448        12228 :       if (arg->ts.type == BT_COMPLEX
    3449        11810 :           || arg->attr.dimension
    3450         1610 :           || arg->ts.type == BT_CHARACTER)
    3451        12228 :         type = build_reference_type (type);
    3452              : 
    3453        12228 :       vec_safe_push (typelist, type);
    3454        12228 :       if (arg->ts.type == BT_CHARACTER)
    3455              :         {
    3456         3087 :           if (!arg->ts.deferred)
    3457              :             /* Transfer by value.  */
    3458         2740 :             vec_safe_push (typelist, gfc_charlen_type_node);
    3459              :           else
    3460              :             /* Deferred character lengths are transferred by reference
    3461              :                so that the value can be returned.  */
    3462          347 :             vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
    3463              :         }
    3464              :     }
    3465       109880 :   if (sym->backend_decl == error_mark_node && actual_args != NULL
    3466        15792 :       && sym->ts.interface == NULL
    3467        15786 :       && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
    3468         1105 :                                  || sym->attr.proc == PROC_UNKNOWN))
    3469          787 :     gfc_get_formal_from_actual_arglist (sym, actual_args);
    3470              : 
    3471              :   /* Build the argument types for the function.  */
    3472       262179 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    3473              :     {
    3474       152299 :       arg = f->sym;
    3475       152299 :       if (arg)
    3476              :         {
    3477              :           /* Evaluate constant character lengths here so that they can be
    3478              :              included in the type.  */
    3479       152195 :           if (arg->ts.type == BT_CHARACTER)
    3480        12664 :             gfc_conv_const_charlen (arg->ts.u.cl);
    3481              : 
    3482       152195 :           if (arg->attr.flavor == FL_PROCEDURE)
    3483              :             {
    3484          991 :               type = gfc_get_function_type (arg);
    3485          991 :               type = build_pointer_type (type);
    3486              :             }
    3487              :           else
    3488       151204 :             type = gfc_sym_type (arg, sym->attr.is_bind_c);
    3489              : 
    3490              :           /* Parameter Passing Convention
    3491              : 
    3492              :              We currently pass all parameters by reference.
    3493              :              Parameters with INTENT(IN) could be passed by value.
    3494              :              The problem arises if a function is called via an implicit
    3495              :              prototype. In this situation the INTENT is not known.
    3496              :              For this reason all parameters to global functions must be
    3497              :              passed by reference.  Passing by value would potentially
    3498              :              generate bad code.  Worse there would be no way of telling that
    3499              :              this code was bad, except that it would give incorrect results.
    3500              : 
    3501              :              Contained procedures could pass by value as these are never
    3502              :              used without an explicit interface, and cannot be passed as
    3503              :              actual parameters for a dummy procedure.  */
    3504              : 
    3505       152195 :           vec_safe_push (typelist, type);
    3506              :         }
    3507              :       else
    3508              :         {
    3509          104 :           if (sym->attr.subroutine)
    3510       152299 :             alternate_return = 1;
    3511              :         }
    3512              :     }
    3513              : 
    3514              :   /* Add hidden arguments.  */
    3515       262179 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    3516              :     {
    3517       152299 :       arg = f->sym;
    3518              :       /* Add hidden string length parameters.  */
    3519       152299 :       if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
    3520              :         {
    3521        10578 :           if (!arg->ts.deferred)
    3522              :             /* Transfer by value.  */
    3523         9705 :             type = gfc_charlen_type_node;
    3524              :           else
    3525              :             /* Deferred character lengths are transferred by reference
    3526              :                so that the value can be returned.  */
    3527          873 :             type = build_pointer_type (gfc_charlen_type_node);
    3528              : 
    3529        10578 :           vec_safe_push (hidden_typelist, type);
    3530              :         }
    3531              :       /* For scalar intrinsic types or derived types, VALUE passes the value,
    3532              :          hence, the optional status cannot be transferred via a NULL pointer.
    3533              :          Thus, we will use a hidden argument in that case.  */
    3534              :       if (arg
    3535       152195 :           && arg->attr.optional
    3536        20146 :           && arg->attr.value
    3537          524 :           && !arg->attr.dimension
    3538          524 :           && arg->ts.type != BT_CLASS)
    3539          524 :         vec_safe_push (typelist, boolean_type_node);
    3540              :       /* Coarrays which are descriptorless or assumed-shape pass with
    3541              :          -fcoarray=lib the token and the offset as hidden arguments.  */
    3542          628 :       if (arg
    3543       152195 :           && flag_coarray == GFC_FCOARRAY_LIB
    3544         7123 :           && ((arg->ts.type != BT_CLASS
    3545         7092 :                && arg->attr.codimension
    3546         1533 :                && !arg->attr.allocatable)
    3547         5618 :               || (arg->ts.type == BT_CLASS
    3548           31 :                   && CLASS_DATA (arg)->attr.codimension
    3549           24 :                   && !CLASS_DATA (arg)->attr.allocatable)))
    3550              :         {
    3551         1525 :           vec_safe_push (hidden_typelist, pvoid_type_node);  /* caf_token.  */
    3552         1525 :           vec_safe_push (hidden_typelist, gfc_array_index_type);  /* caf_offset.  */
    3553              :         }
    3554              :     }
    3555              : 
    3556              :   /* Put hidden character length, caf_token, caf_offset at the end.  */
    3557       118511 :   vec_safe_reserve (typelist, vec_safe_length (hidden_typelist));
    3558       109880 :   vec_safe_splice (typelist, hidden_typelist);
    3559              : 
    3560       109880 :   if (!vec_safe_is_empty (typelist)
    3561        42623 :       || sym->attr.is_main_program
    3562        16538 :       || sym->attr.if_source != IFSRC_UNKNOWN)
    3563              :     is_varargs = false;
    3564              : 
    3565       109880 :   if (sym->backend_decl == error_mark_node)
    3566       109880 :     sym->backend_decl = NULL_TREE;
    3567              : 
    3568       109927 : arg_type_list_done:
    3569              : 
    3570       109927 :   if (alternate_return)
    3571           74 :     type = integer_type_node;
    3572       109853 :   else if (!sym->attr.function || gfc_return_by_reference (sym))
    3573        88631 :     type = void_type_node;
    3574        21222 :   else if (sym->attr.mixed_entry_master)
    3575           98 :     type = gfc_get_mixed_entry_union (sym->ns);
    3576        21124 :   else if (flag_f2c && sym->ts.type == BT_REAL
    3577          389 :            && sym->ts.kind == gfc_default_real_kind
    3578          215 :            && !sym->attr.pointer
    3579          190 :            && !sym->attr.allocatable
    3580          172 :            && !sym->attr.always_explicit)
    3581              :     {
    3582              :       /* Special case: f2c calling conventions require that (scalar)
    3583              :          default REAL functions return the C type double instead.  f2c
    3584              :          compatibility is only an issue with functions that don't
    3585              :          require an explicit interface, as only these could be
    3586              :          implemented in Fortran 77.  */
    3587          172 :       sym->ts.kind = gfc_default_double_kind;
    3588          172 :       type = gfc_typenode_for_spec (&sym->ts);
    3589          172 :       sym->ts.kind = gfc_default_real_kind;
    3590              :     }
    3591        20952 :   else if (sym->result && sym->result->attr.proc_pointer)
    3592              :     /* Procedure pointer return values.  */
    3593              :     {
    3594          467 :       if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
    3595              :         {
    3596              :           /* Unset proc_pointer as gfc_get_function_type
    3597              :              is called recursively.  */
    3598          166 :           sym->result->attr.proc_pointer = 0;
    3599          166 :           type = build_pointer_type (gfc_get_function_type (sym->result));
    3600          166 :           sym->result->attr.proc_pointer = 1;
    3601              :         }
    3602              :       else
    3603          301 :        type = gfc_sym_type (sym->result);
    3604              :     }
    3605              :   else
    3606        20485 :     type = gfc_sym_type (sym);
    3607              : 
    3608       109927 :   if (is_varargs)
    3609              :     /* This should be represented as an unprototyped type, not a type
    3610              :        with (...) prototype.  */
    3611         1937 :     type = build_function_type (type, NULL_TREE);
    3612              :   else
    3613       242504 :     type = build_function_type_vec (type, typelist);
    3614              : 
    3615              :   /* If we were passed an fn spec, add it here, otherwise determine it from
    3616              :      the formal arguments.  */
    3617       109927 :   if (fnspec)
    3618              :     {
    3619         1939 :       tree tmp;
    3620         1939 :       int spec_len = strlen (fnspec);
    3621         1939 :       tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
    3622         1939 :       tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
    3623         1939 :       type = build_type_attribute_variant (type, tmp);
    3624              :     }
    3625              :   else
    3626       107988 :     type = create_fn_spec (sym, type);
    3627              : 
    3628       109927 :   return type;
    3629              : }
    3630              : 
    3631              : /* Language hooks for middle-end access to type nodes.  */
    3632              : 
    3633              : /* Return an integer type with BITS bits of precision,
    3634              :    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
    3635              : 
    3636              : tree
    3637       699144 : gfc_type_for_size (unsigned bits, int unsignedp)
    3638              : {
    3639       699144 :   if (!unsignedp)
    3640              :     {
    3641              :       int i;
    3642       448373 :       for (i = 0; i <= MAX_INT_KINDS; ++i)
    3643              :         {
    3644       448343 :           tree type = gfc_integer_types[i];
    3645       448343 :           if (type && bits == TYPE_PRECISION (type))
    3646              :             return type;
    3647              :         }
    3648              : 
    3649              :       /* Handle TImode as a special case because it is used by some backends
    3650              :          (e.g. ARM) even though it is not available for normal use.  */
    3651              : #if HOST_BITS_PER_WIDE_INT >= 64
    3652           30 :       if (bits == TYPE_PRECISION (intTI_type_node))
    3653              :         return intTI_type_node;
    3654              : #endif
    3655              : 
    3656           30 :       if (bits <= TYPE_PRECISION (intQI_type_node))
    3657              :         return intQI_type_node;
    3658            0 :       if (bits <= TYPE_PRECISION (intHI_type_node))
    3659              :         return intHI_type_node;
    3660            0 :       if (bits <= TYPE_PRECISION (intSI_type_node))
    3661              :         return intSI_type_node;
    3662            0 :       if (bits <= TYPE_PRECISION (intDI_type_node))
    3663              :         return intDI_type_node;
    3664            0 :       if (bits <= TYPE_PRECISION (intTI_type_node))
    3665              :         return intTI_type_node;
    3666              :     }
    3667              :   else
    3668              :     {
    3669       569478 :       if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
    3670              :         return unsigned_intQI_type_node;
    3671       537408 :       if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
    3672              :         return unsigned_intHI_type_node;
    3673       505754 :       if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
    3674              :         return unsigned_intSI_type_node;
    3675       467478 :       if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
    3676              :         return unsigned_intDI_type_node;
    3677        31445 :       if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
    3678              :         return unsigned_intTI_type_node;
    3679              :     }
    3680              : 
    3681              :   return NULL_TREE;
    3682              : }
    3683              : 
    3684              : /* Return a data type that has machine mode MODE.  If the mode is an
    3685              :    integer, then UNSIGNEDP selects between signed and unsigned types.  */
    3686              : 
    3687              : tree
    3688       715884 : gfc_type_for_mode (machine_mode mode, int unsignedp)
    3689              : {
    3690       715884 :   int i;
    3691       715884 :   tree *base;
    3692       715884 :   scalar_int_mode int_mode;
    3693              : 
    3694       715884 :   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
    3695              :     base = gfc_real_types;
    3696       708070 :   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
    3697              :     base = gfc_complex_types;
    3698       518830 :   else if (is_a <scalar_int_mode> (mode, &int_mode))
    3699              :     {
    3700       518446 :       tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
    3701       518446 :       return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
    3702              :     }
    3703          384 :   else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
    3704          384 :            && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
    3705              :     {
    3706            0 :       unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
    3707              :                                                     GET_MODE_NUNITS (mode));
    3708            0 :       tree bool_type = build_nonstandard_boolean_type (elem_bits);
    3709            0 :       return build_vector_type_for_mode (bool_type, mode);
    3710              :     }
    3711           37 :   else if (VECTOR_MODE_P (mode)
    3712        63491 :            && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
    3713              :     {
    3714          379 :       machine_mode inner_mode = GET_MODE_INNER (mode);
    3715          379 :       tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
    3716          379 :       if (inner_type != NULL_TREE)
    3717          379 :         return build_vector_type_for_mode (inner_type, mode);
    3718              :       return NULL_TREE;
    3719              :     }
    3720              :   else
    3721              :     return NULL_TREE;
    3722              : 
    3723       768675 :   for (i = 0; i <= MAX_REAL_KINDS; ++i)
    3724              :     {
    3725       705953 :       tree type = base[i];
    3726       705953 :       if (type && mode == TYPE_MODE (type))
    3727              :         return type;
    3728              :     }
    3729              : 
    3730              :   return NULL_TREE;
    3731              : }
    3732              : 
    3733              : /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
    3734              :    in that case.  */
    3735              : 
    3736              : bool
    3737       407150 : gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
    3738              : {
    3739       407150 :   int rank, dim;
    3740       407150 :   bool indirect = false;
    3741       407150 :   tree etype, ptype, t, base_decl;
    3742       407150 :   tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
    3743       407150 :   tree lower_suboff, upper_suboff, stride_suboff;
    3744       407150 :   tree dtype, field, rank_off;
    3745              : 
    3746       407150 :   if (! GFC_DESCRIPTOR_TYPE_P (type))
    3747              :     {
    3748       257910 :       if (! POINTER_TYPE_P (type))
    3749              :         return false;
    3750       165196 :       type = TREE_TYPE (type);
    3751       165196 :       if (! GFC_DESCRIPTOR_TYPE_P (type))
    3752              :         return false;
    3753              :       indirect = true;
    3754              :     }
    3755              : 
    3756       292255 :   rank = GFC_TYPE_ARRAY_RANK (type);
    3757       292255 :   if (rank >= (int) (ARRAY_SIZE (info->dimen)))
    3758              :     return false;
    3759              : 
    3760       292255 :   etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
    3761       292255 :   gcc_assert (POINTER_TYPE_P (etype));
    3762       292255 :   etype = TREE_TYPE (etype);
    3763              : 
    3764              :   /* If the type is not a scalar coarray.  */
    3765       292255 :   if (TREE_CODE (etype) == ARRAY_TYPE)
    3766       292230 :     etype = TREE_TYPE (etype);
    3767              : 
    3768              :   /* Can't handle variable sized elements yet.  */
    3769       292255 :   if (int_size_in_bytes (etype) <= 0)
    3770              :     return false;
    3771              :   /* Nor non-constant lower bounds in assumed shape arrays.  */
    3772       271359 :   if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
    3773       271359 :       || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
    3774              :     {
    3775        82535 :       for (dim = 0; dim < rank; dim++)
    3776        51256 :         if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
    3777        51256 :             || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
    3778              :           return false;
    3779              :     }
    3780              : 
    3781       271119 :   memset (info, '\0', sizeof (*info));
    3782       271119 :   info->ndimensions = rank;
    3783       271119 :   info->ordering = array_descr_ordering_column_major;
    3784       271119 :   info->element_type = etype;
    3785       271119 :   ptype = build_pointer_type (gfc_array_index_type);
    3786       271119 :   base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
    3787       271119 :   if (!base_decl)
    3788              :     {
    3789       394448 :       base_decl = build_debug_expr_decl (indirect
    3790       131475 :                                          ? build_pointer_type (ptype) : ptype);
    3791       262973 :       GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
    3792              :     }
    3793       271119 :   info->base_decl = base_decl;
    3794       271119 :   if (indirect)
    3795       133011 :     base_decl = build1 (INDIRECT_REF, ptype, base_decl);
    3796              : 
    3797       271119 :   gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
    3798              :                                        &dim_off, &dim_size, &stride_suboff,
    3799              :                                        &lower_suboff, &upper_suboff);
    3800              : 
    3801       271119 :   t = fold_build_pointer_plus (base_decl, span_off);
    3802       271119 :   elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3803              : 
    3804       271119 :   t = base_decl;
    3805       271119 :   if (!integer_zerop (data_off))
    3806            0 :     t = fold_build_pointer_plus (t, data_off);
    3807       271119 :   t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
    3808       271119 :   info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
    3809       271119 :   enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
    3810       271119 :   if (akind == GFC_ARRAY_ALLOCATABLE
    3811       271119 :       || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE)
    3812        33554 :     info->allocated = build2 (NE_EXPR, logical_type_node,
    3813              :                               info->data_location, null_pointer_node);
    3814       237565 :   else if (akind == GFC_ARRAY_POINTER
    3815       237565 :            || akind == GFC_ARRAY_POINTER_CONT
    3816       237565 :            || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
    3817       220367 :            || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
    3818        17198 :     info->associated = build2 (NE_EXPR, logical_type_node,
    3819              :                                info->data_location, null_pointer_node);
    3820       271119 :   if ((akind == GFC_ARRAY_ASSUMED_RANK
    3821              :        || akind == GFC_ARRAY_ASSUMED_RANK_CONT
    3822              :        || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
    3823              :        || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
    3824       271119 :        || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
    3825        14330 :       && dwarf_version >= 5)
    3826              :     {
    3827        14330 :       rank = 1;
    3828        14330 :       info->ndimensions = 1;
    3829        14330 :       t = fold_build_pointer_plus (base_decl, dtype_off);
    3830        14330 :       dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
    3831        14330 :       field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
    3832        14330 :       rank_off = byte_position (field);
    3833        14330 :       t = fold_build_pointer_plus (t, rank_off);
    3834              : 
    3835        14330 :       t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
    3836        14330 :       t = build1 (INDIRECT_REF, TREE_TYPE (field), t);
    3837        14330 :       info->rank = t;
    3838        14330 :       t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
    3839        14330 :       t = size_binop (MULT_EXPR, t, dim_size);
    3840        14330 :       dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
    3841              :     }
    3842              : 
    3843       674873 :   for (dim = 0; dim < rank; dim++)
    3844              :     {
    3845       403754 :       t = fold_build_pointer_plus (base_decl,
    3846              :                                    size_binop (PLUS_EXPR,
    3847              :                                                dim_off, lower_suboff));
    3848       403754 :       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3849       403754 :       info->dimen[dim].lower_bound = t;
    3850       403754 :       t = fold_build_pointer_plus (base_decl,
    3851              :                                    size_binop (PLUS_EXPR,
    3852              :                                                dim_off, upper_suboff));
    3853       403754 :       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3854       403754 :       info->dimen[dim].upper_bound = t;
    3855       403754 :       if (akind == GFC_ARRAY_ASSUMED_SHAPE
    3856       403754 :           || akind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
    3857              :         {
    3858              :           /* Assumed shape arrays have known lower bounds.  */
    3859        51016 :           info->dimen[dim].upper_bound
    3860        51016 :             = build2 (MINUS_EXPR, gfc_array_index_type,
    3861              :                       info->dimen[dim].upper_bound,
    3862              :                       info->dimen[dim].lower_bound);
    3863        51016 :           info->dimen[dim].lower_bound
    3864        51016 :             = fold_convert (gfc_array_index_type,
    3865              :                             GFC_TYPE_ARRAY_LBOUND (type, dim));
    3866        51016 :           info->dimen[dim].upper_bound
    3867        51016 :             = build2 (PLUS_EXPR, gfc_array_index_type,
    3868              :                       info->dimen[dim].lower_bound,
    3869              :                       info->dimen[dim].upper_bound);
    3870              :         }
    3871       403754 :       t = fold_build_pointer_plus (base_decl,
    3872              :                                    size_binop (PLUS_EXPR,
    3873              :                                                dim_off, stride_suboff));
    3874       403754 :       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
    3875       403754 :       t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
    3876       403754 :       info->dimen[dim].stride = t;
    3877       403754 :       if (dim + 1 < rank)
    3878       132660 :         dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
    3879              :     }
    3880              : 
    3881              :   return true;
    3882              : }
    3883              : 
    3884              : 
    3885              : /* Create a type to handle vector subscripts for coarray library calls. It
    3886              :    has the form:
    3887              :      struct caf_vector_t {
    3888              :        size_t nvec;  // size of the vector
    3889              :        union {
    3890              :          struct {
    3891              :            void *vector;
    3892              :            int kind;
    3893              :          } v;
    3894              :          struct {
    3895              :            ptrdiff_t lower_bound;
    3896              :            ptrdiff_t upper_bound;
    3897              :            ptrdiff_t stride;
    3898              :          } triplet;
    3899              :        } u;
    3900              :      }
    3901              :    where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
    3902              :    size in case of DIMEN_VECTOR, where kind is the integer type of the vector.  */
    3903              : 
    3904              : tree
    3905            0 : gfc_get_caf_vector_type (int dim)
    3906              : {
    3907            0 :   static tree vector_types[GFC_MAX_DIMENSIONS];
    3908            0 :   static tree vec_type = NULL_TREE;
    3909            0 :   tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
    3910              : 
    3911            0 :   if (vector_types[dim-1] != NULL_TREE)
    3912              :     return vector_types[dim-1];
    3913              : 
    3914            0 :   if (vec_type == NULL_TREE)
    3915              :     {
    3916            0 :       chain = 0;
    3917            0 :       vect_struct_type = make_node (RECORD_TYPE);
    3918            0 :       tmp = gfc_add_field_to_struct_1 (vect_struct_type,
    3919              :                                        get_identifier ("vector"),
    3920              :                                        pvoid_type_node, &chain);
    3921            0 :       suppress_warning (tmp);
    3922            0 :       tmp = gfc_add_field_to_struct_1 (vect_struct_type,
    3923              :                                        get_identifier ("kind"),
    3924              :                                        integer_type_node, &chain);
    3925            0 :       suppress_warning (tmp);
    3926            0 :       gfc_finish_type (vect_struct_type);
    3927              : 
    3928            0 :       chain = 0;
    3929            0 :       triplet_struct_type = make_node (RECORD_TYPE);
    3930            0 :       tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
    3931              :                                        get_identifier ("lower_bound"),
    3932              :                                        gfc_array_index_type, &chain);
    3933            0 :       suppress_warning (tmp);
    3934            0 :       tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
    3935              :                                        get_identifier ("upper_bound"),
    3936              :                                        gfc_array_index_type, &chain);
    3937            0 :       suppress_warning (tmp);
    3938            0 :       tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
    3939              :                                        gfc_array_index_type, &chain);
    3940            0 :       suppress_warning (tmp);
    3941            0 :       gfc_finish_type (triplet_struct_type);
    3942              : 
    3943            0 :       chain = 0;
    3944            0 :       union_type = make_node (UNION_TYPE);
    3945            0 :       tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
    3946              :                                        vect_struct_type, &chain);
    3947            0 :       suppress_warning (tmp);
    3948            0 :       tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
    3949              :                                        triplet_struct_type, &chain);
    3950            0 :       suppress_warning (tmp);
    3951            0 :       gfc_finish_type (union_type);
    3952              : 
    3953            0 :       chain = 0;
    3954            0 :       vec_type = make_node (RECORD_TYPE);
    3955            0 :       tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
    3956              :                                        size_type_node, &chain);
    3957            0 :       suppress_warning (tmp);
    3958            0 :       tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
    3959              :                                        union_type, &chain);
    3960            0 :       suppress_warning (tmp);
    3961            0 :       gfc_finish_type (vec_type);
    3962            0 :       TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
    3963              :     }
    3964              : 
    3965            0 :   tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
    3966              :                           gfc_rank_cst[dim-1]);
    3967            0 :   vector_types[dim-1] = build_array_type (vec_type, tmp);
    3968            0 :   return vector_types[dim-1];
    3969              : }
    3970              : 
    3971              : 
    3972              : tree
    3973            0 : gfc_get_caf_reference_type ()
    3974              : {
    3975            0 :   static tree reference_type = NULL_TREE;
    3976            0 :   tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
    3977              :       a_struct_type, u_union_type, tmp, *chain;
    3978              : 
    3979            0 :   if (reference_type != NULL_TREE)
    3980              :     return reference_type;
    3981              : 
    3982            0 :   chain = 0;
    3983            0 :   c_struct_type = make_node (RECORD_TYPE);
    3984            0 :   tmp = gfc_add_field_to_struct_1 (c_struct_type,
    3985              :                                    get_identifier ("offset"),
    3986              :                                    gfc_array_index_type, &chain);
    3987            0 :   suppress_warning (tmp);
    3988            0 :   tmp = gfc_add_field_to_struct_1 (c_struct_type,
    3989              :                                    get_identifier ("caf_token_offset"),
    3990              :                                    gfc_array_index_type, &chain);
    3991            0 :   suppress_warning (tmp);
    3992            0 :   gfc_finish_type (c_struct_type);
    3993              : 
    3994            0 :   chain = 0;
    3995            0 :   s_struct_type = make_node (RECORD_TYPE);
    3996            0 :   tmp = gfc_add_field_to_struct_1 (s_struct_type,
    3997              :                                    get_identifier ("start"),
    3998              :                                    gfc_array_index_type, &chain);
    3999            0 :   suppress_warning (tmp);
    4000            0 :   tmp = gfc_add_field_to_struct_1 (s_struct_type,
    4001              :                                    get_identifier ("end"),
    4002              :                                    gfc_array_index_type, &chain);
    4003            0 :   suppress_warning (tmp);
    4004            0 :   tmp = gfc_add_field_to_struct_1 (s_struct_type,
    4005              :                                    get_identifier ("stride"),
    4006              :                                    gfc_array_index_type, &chain);
    4007            0 :   suppress_warning (tmp);
    4008            0 :   gfc_finish_type (s_struct_type);
    4009              : 
    4010            0 :   chain = 0;
    4011            0 :   v_struct_type = make_node (RECORD_TYPE);
    4012            0 :   tmp = gfc_add_field_to_struct_1 (v_struct_type,
    4013              :                                    get_identifier ("vector"),
    4014              :                                    pvoid_type_node, &chain);
    4015            0 :   suppress_warning (tmp);
    4016            0 :   tmp = gfc_add_field_to_struct_1 (v_struct_type,
    4017              :                                    get_identifier ("nvec"),
    4018              :                                    size_type_node, &chain);
    4019            0 :   suppress_warning (tmp);
    4020            0 :   tmp = gfc_add_field_to_struct_1 (v_struct_type,
    4021              :                                    get_identifier ("kind"),
    4022              :                                    integer_type_node, &chain);
    4023            0 :   suppress_warning (tmp);
    4024            0 :   gfc_finish_type (v_struct_type);
    4025              : 
    4026            0 :   chain = 0;
    4027            0 :   union_type = make_node (UNION_TYPE);
    4028            0 :   tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
    4029              :                                    s_struct_type, &chain);
    4030            0 :   suppress_warning (tmp);
    4031            0 :   tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
    4032              :                                    v_struct_type, &chain);
    4033            0 :   suppress_warning (tmp);
    4034            0 :   gfc_finish_type (union_type);
    4035              : 
    4036            0 :   tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
    4037              :                           gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
    4038            0 :   dim_union_type = build_array_type (union_type, tmp);
    4039              : 
    4040            0 :   chain = 0;
    4041            0 :   a_struct_type = make_node (RECORD_TYPE);
    4042            0 :   tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
    4043              :                 build_array_type (unsigned_char_type_node,
    4044              :                                   build_range_type (gfc_array_index_type,
    4045              :                                                     gfc_index_zero_node,
    4046              :                                          gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
    4047              :                 &chain);
    4048            0 :   suppress_warning (tmp);
    4049            0 :   tmp = gfc_add_field_to_struct_1 (a_struct_type,
    4050              :                                    get_identifier ("static_array_type"),
    4051              :                                    integer_type_node, &chain);
    4052            0 :   suppress_warning (tmp);
    4053            0 :   tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
    4054              :                                    dim_union_type, &chain);
    4055            0 :   suppress_warning (tmp);
    4056            0 :   gfc_finish_type (a_struct_type);
    4057              : 
    4058            0 :   chain = 0;
    4059            0 :   u_union_type = make_node (UNION_TYPE);
    4060            0 :   tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
    4061              :                                    c_struct_type, &chain);
    4062            0 :   suppress_warning (tmp);
    4063            0 :   tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
    4064              :                                    a_struct_type, &chain);
    4065            0 :   suppress_warning (tmp);
    4066            0 :   gfc_finish_type (u_union_type);
    4067              : 
    4068            0 :   chain = 0;
    4069            0 :   reference_type = make_node (RECORD_TYPE);
    4070            0 :   tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
    4071              :                                    build_pointer_type (reference_type), &chain);
    4072            0 :   suppress_warning (tmp);
    4073            0 :   tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
    4074              :                                    integer_type_node, &chain);
    4075            0 :   suppress_warning (tmp);
    4076            0 :   tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
    4077              :                                    size_type_node, &chain);
    4078            0 :   suppress_warning (tmp);
    4079            0 :   tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
    4080              :                                    u_union_type, &chain);
    4081            0 :   suppress_warning (tmp);
    4082            0 :   gfc_finish_type (reference_type);
    4083            0 :   TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
    4084              : 
    4085            0 :   return reference_type;
    4086              : }
    4087              : 
    4088              : static tree
    4089         1337 : gfc_get_cfi_dim_type ()
    4090              : {
    4091         1337 :   static tree CFI_dim_t = NULL;
    4092              : 
    4093         1337 :   if (CFI_dim_t)
    4094              :     return CFI_dim_t;
    4095              : 
    4096          638 :   CFI_dim_t = make_node (RECORD_TYPE);
    4097          638 :   TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
    4098          638 :   TYPE_NAMELESS (CFI_dim_t) = 1;
    4099          638 :   tree field;
    4100          638 :   tree *chain = NULL;
    4101          638 :   field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
    4102              :                                      gfc_array_index_type, &chain);
    4103          638 :   suppress_warning (field);
    4104          638 :   field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
    4105              :                                      gfc_array_index_type, &chain);
    4106          638 :   suppress_warning (field);
    4107          638 :   field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
    4108              :                                      gfc_array_index_type, &chain);
    4109          638 :   suppress_warning (field);
    4110          638 :   gfc_finish_type (CFI_dim_t);
    4111          638 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
    4112          638 :   return CFI_dim_t;
    4113              : }
    4114              : 
    4115              : 
    4116              : /* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
    4117              :    otherwise dim[dimen] is used.  */
    4118              : 
    4119              : tree
    4120        12515 : gfc_get_cfi_type (int dimen, bool restricted)
    4121              : {
    4122        12515 :   gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
    4123              : 
    4124        12515 :   int idx = 2*(dimen + 1) + restricted;
    4125              : 
    4126        12515 :   if (gfc_cfi_descriptor_base[idx])
    4127              :     return gfc_cfi_descriptor_base[idx];
    4128              : 
    4129              :   /* Build the type node.  */
    4130         1516 :   tree CFI_cdesc_t = make_node (RECORD_TYPE);
    4131         1516 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    4132         1516 :   if (dimen != -1)
    4133          959 :     sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
    4134         1516 :   TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
    4135         1516 :   TYPE_NAMELESS (CFI_cdesc_t) = 1;
    4136              : 
    4137         1516 :   tree field;
    4138         1516 :   tree *chain = NULL;
    4139         1516 :   field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
    4140              :                                      (restricted ? prvoid_type_node
    4141              :                                                  : ptr_type_node), &chain);
    4142         1516 :   suppress_warning (field);
    4143         1516 :   field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
    4144              :                                      size_type_node, &chain);
    4145         1516 :   suppress_warning (field);
    4146         1516 :   field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
    4147              :                                      integer_type_node, &chain);
    4148         1516 :   suppress_warning (field);
    4149         1516 :   field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
    4150              :                                      signed_char_type_node, &chain);
    4151         1516 :   suppress_warning (field);
    4152         1516 :   field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
    4153              :                                      signed_char_type_node, &chain);
    4154         1516 :   suppress_warning (field);
    4155         1516 :   field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
    4156              :                                      get_typenode_from_name (INT16_TYPE),
    4157              :                                      &chain);
    4158         1516 :   suppress_warning (field);
    4159              : 
    4160         1516 :   if (dimen != 0)
    4161              :     {
    4162         1337 :       tree range = NULL_TREE;
    4163         1337 :       if (dimen > 0)
    4164          780 :         range = gfc_rank_cst[dimen - 1];
    4165         1337 :       range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
    4166              :                                 range);
    4167         1337 :       tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
    4168         1337 :       field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
    4169              :                                          CFI_dim_t, &chain);
    4170         1337 :       suppress_warning (field);
    4171              :     }
    4172              : 
    4173         1516 :   TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
    4174         1516 :   gfc_finish_type (CFI_cdesc_t);
    4175         1516 :   gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
    4176         1516 :   return CFI_cdesc_t;
    4177              : }
    4178              : 
    4179              : #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.