LCOV - code coverage report
Current view: top level - gcc/fortran - trans-array.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.0 % 6254 6005
Test Date: 2026-12-05 16:10:02 Functions: 99.4 % 176 175
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Array translation routines
       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-array.cc-- Various array related code, including scalarization,
      23              :                    allocation, initialization and other support routines.  */
      24              : 
      25              : /* How the scalarizer works.
      26              :    In gfortran, array expressions use the same core routines as scalar
      27              :    expressions.
      28              :    First, a Scalarization State (SS) chain is built.  This is done by walking
      29              :    the expression tree, and building a linear list of the terms in the
      30              :    expression.  As the tree is walked, scalar subexpressions are translated.
      31              : 
      32              :    The scalarization parameters are stored in a gfc_loopinfo structure.
      33              :    First the start and stride of each term is calculated by
      34              :    gfc_conv_ss_startstride.  During this process the expressions for the array
      35              :    descriptors and data pointers are also translated.
      36              : 
      37              :    If the expression is an assignment, we must then resolve any dependencies.
      38              :    In Fortran all the rhs values of an assignment must be evaluated before
      39              :    any assignments take place.  This can require a temporary array to store the
      40              :    values.  We also require a temporary when we are passing array expressions
      41              :    or vector subscripts as procedure parameters.
      42              : 
      43              :    Array sections are passed without copying to a temporary.  These use the
      44              :    scalarizer to determine the shape of the section.  The flag
      45              :    loop->array_parameter tells the scalarizer that the actual values and loop
      46              :    variables will not be required.
      47              : 
      48              :    The function gfc_conv_loop_setup generates the scalarization setup code.
      49              :    It determines the range of the scalarizing loop variables.  If a temporary
      50              :    is required, this is created and initialized.  Code for scalar expressions
      51              :    taken outside the loop is also generated at this time.  Next the offset and
      52              :    scaling required to translate from loop variables to array indices for each
      53              :    term is calculated.
      54              : 
      55              :    A call to gfc_start_scalarized_body marks the start of the scalarized
      56              :    expression.  This creates a scope and declares the loop variables.  Before
      57              :    calling this gfc_make_ss_chain_used must be used to indicate which terms
      58              :    will be used inside this loop.
      59              : 
      60              :    The scalar gfc_conv_* functions are then used to build the main body of the
      61              :    scalarization loop.  Scalarization loop variables and precalculated scalar
      62              :    values are automatically substituted.  Note that gfc_advance_se_ss_chain
      63              :    must be used, rather than changing the se->ss directly.
      64              : 
      65              :    For assignment expressions requiring a temporary two sub loops are
      66              :    generated.  The first stores the result of the expression in the temporary,
      67              :    the second copies it to the result.  A call to
      68              :    gfc_trans_scalarized_loop_boundary marks the end of the main loop code and
      69              :    the start of the copying loop.  The temporary may be less than full rank.
      70              : 
      71              :    Finally gfc_trans_scalarizing_loops is called to generate the implicit do
      72              :    loops.  The loops are added to the pre chain of the loopinfo.  The post
      73              :    chain may still contain cleanup code.
      74              : 
      75              :    After the loop code has been added into its parent scope gfc_cleanup_loop
      76              :    is called to free all the SS allocated by the scalarizer.  */
      77              : 
      78              : #include "config.h"
      79              : #include "system.h"
      80              : #include "coretypes.h"
      81              : #include "options.h"
      82              : #include "tree.h"
      83              : #include "gfortran.h"
      84              : #include "gimple-expr.h"
      85              : #include "tree-iterator.h"
      86              : #include "stringpool.h"  /* Required by "attribs.h".  */
      87              : #include "attribs.h" /* For lookup_attribute.  */
      88              : #include "trans.h"
      89              : #include "fold-const.h"
      90              : #include "stor-layout.h" /* For min_align_of_type.  */
      91              : #include "constructor.h"
      92              : #include "trans-types.h"
      93              : #include "trans-array.h"
      94              : #include "trans-const.h"
      95              : #include "dependency.h"
      96              : #include "trans-descriptor.h"
      97              : #include "cgraph.h"   /* For cgraph_node::add_new_function.  */
      98              : #include "function.h" /* For push_struct_function.  */
      99              : 
     100              : static bool gfc_get_array_constructor_size (mpz_t *, gfc_constructor_base);
     101              : 
     102              : /* The contents of this structure aren't actually used, just the address.  */
     103              : static gfc_ss gfc_ss_terminator_var;
     104              : gfc_ss * const gfc_ss_terminator = &gfc_ss_terminator_var;
     105              : 
     106              : 
     107              : static tree
     108        65287 : gfc_array_dataptr_type (tree desc)
     109              : {
     110        65287 :   return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
     111              : }
     112              : 
     113              : /* Build expressions to access members of the CFI descriptor.  */
     114              : #define CFI_FIELD_BASE_ADDR 0
     115              : #define CFI_FIELD_ELEM_LEN 1
     116              : #define CFI_FIELD_VERSION 2
     117              : #define CFI_FIELD_RANK 3
     118              : #define CFI_FIELD_ATTRIBUTE 4
     119              : #define CFI_FIELD_TYPE 5
     120              : #define CFI_FIELD_DIM 6
     121              : 
     122              : #define CFI_DIM_FIELD_LOWER_BOUND 0
     123              : #define CFI_DIM_FIELD_EXTENT 1
     124              : #define CFI_DIM_FIELD_SM 2
     125              : 
     126              : static tree
     127        84967 : gfc_get_cfi_descriptor_field (tree desc, unsigned field_idx)
     128              : {
     129        84967 :   tree type = TREE_TYPE (desc);
     130        84967 :   gcc_assert (TREE_CODE (type) == RECORD_TYPE
     131              :               && TYPE_FIELDS (type)
     132              :               && (strcmp ("base_addr",
     133              :                          IDENTIFIER_POINTER (DECL_NAME (TYPE_FIELDS (type))))
     134              :                   == 0));
     135        84967 :   tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
     136        84967 :   gcc_assert (field != NULL_TREE);
     137              : 
     138        84967 :   return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
     139        84967 :                           desc, field, NULL_TREE);
     140              : }
     141              : 
     142              : tree
     143        14205 : gfc_get_cfi_desc_base_addr (tree desc)
     144              : {
     145        14205 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_BASE_ADDR);
     146              : }
     147              : 
     148              : tree
     149        10685 : gfc_get_cfi_desc_elem_len (tree desc)
     150              : {
     151        10685 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ELEM_LEN);
     152              : }
     153              : 
     154              : tree
     155         7195 : gfc_get_cfi_desc_version (tree desc)
     156              : {
     157         7195 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_VERSION);
     158              : }
     159              : 
     160              : tree
     161         7820 : gfc_get_cfi_desc_rank (tree desc)
     162              : {
     163         7820 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_RANK);
     164              : }
     165              : 
     166              : tree
     167         7287 : gfc_get_cfi_desc_type (tree desc)
     168              : {
     169         7287 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_TYPE);
     170              : }
     171              : 
     172              : tree
     173         7195 : gfc_get_cfi_desc_attribute (tree desc)
     174              : {
     175         7195 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ATTRIBUTE);
     176              : }
     177              : 
     178              : static tree
     179        30580 : gfc_get_cfi_dim_item (tree desc, tree idx, unsigned field_idx)
     180              : {
     181        30580 :   tree tmp = gfc_get_cfi_descriptor_field (desc, CFI_FIELD_DIM);
     182        30580 :   tmp = gfc_build_array_ref (tmp, idx, NULL_TREE, true);
     183        30580 :   tree field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (tmp)), field_idx);
     184        30580 :   gcc_assert (field != NULL_TREE);
     185        30580 :   return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
     186        30580 :                           tmp, field, NULL_TREE);
     187              : }
     188              : 
     189              : tree
     190         6786 : gfc_get_cfi_dim_lbound (tree desc, tree idx)
     191              : {
     192         6786 :   return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_LOWER_BOUND);
     193              : }
     194              : 
     195              : tree
     196        11926 : gfc_get_cfi_dim_extent (tree desc, tree idx)
     197              : {
     198        11926 :   return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_EXTENT);
     199              : }
     200              : 
     201              : tree
     202        11868 : gfc_get_cfi_dim_sm (tree desc, tree idx)
     203              : {
     204        11868 :   return gfc_get_cfi_dim_item (desc, idx, CFI_DIM_FIELD_SM);
     205              : }
     206              : 
     207              : #undef CFI_FIELD_BASE_ADDR
     208              : #undef CFI_FIELD_ELEM_LEN
     209              : #undef CFI_FIELD_VERSION
     210              : #undef CFI_FIELD_RANK
     211              : #undef CFI_FIELD_ATTRIBUTE
     212              : #undef CFI_FIELD_TYPE
     213              : #undef CFI_FIELD_DIM
     214              : 
     215              : #undef CFI_DIM_FIELD_LOWER_BOUND
     216              : #undef CFI_DIM_FIELD_EXTENT
     217              : #undef CFI_DIM_FIELD_SM
     218              : 
     219              : 
     220              : /* Mark a SS chain as used.  Flags specifies in which loops the SS is used.
     221              :    flags & 1 = Main loop body.
     222              :    flags & 2 = temp copy loop.  */
     223              : 
     224              : void
     225       178983 : gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
     226              : {
     227       420297 :   for (; ss != gfc_ss_terminator; ss = ss->next)
     228       241314 :     ss->info->useflags = flags;
     229       178983 : }
     230              : 
     231              : 
     232              : /* Free a gfc_ss chain.  */
     233              : 
     234              : void
     235       193064 : gfc_free_ss_chain (gfc_ss * ss)
     236              : {
     237       193064 :   gfc_ss *next;
     238              : 
     239       394404 :   while (ss != gfc_ss_terminator)
     240              :     {
     241       201340 :       gcc_assert (ss != NULL);
     242       201340 :       next = ss->next;
     243       201340 :       gfc_free_ss (ss);
     244       201340 :       ss = next;
     245              :     }
     246       193064 : }
     247              : 
     248              : 
     249              : static void
     250       518726 : free_ss_info (gfc_ss_info *ss_info)
     251              : {
     252       518726 :   int n;
     253              : 
     254       518726 :   ss_info->refcount--;
     255       518726 :   if (ss_info->refcount > 0)
     256              :     return;
     257              : 
     258       513979 :   gcc_assert (ss_info->refcount == 0);
     259              : 
     260       513979 :   switch (ss_info->type)
     261              :     {
     262              :     case GFC_SS_SECTION:
     263      5747088 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
     264      5387895 :         if (ss_info->data.array.subscript[n])
     265         7944 :           gfc_free_ss_chain (ss_info->data.array.subscript[n]);
     266              :       break;
     267              : 
     268              :     default:
     269              :       break;
     270              :     }
     271              : 
     272       513979 :   free (ss_info);
     273              : }
     274              : 
     275              : 
     276              : /* Free a SS.  */
     277              : 
     278              : void
     279       518726 : gfc_free_ss (gfc_ss * ss)
     280              : {
     281       518726 :   free_ss_info (ss->info);
     282       518726 :   free (ss);
     283       518726 : }
     284              : 
     285              : 
     286              : /* Creates and initializes an array type gfc_ss struct.  */
     287              : 
     288              : gfc_ss *
     289       435970 : gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
     290              : {
     291       435970 :   gfc_ss *ss;
     292       435970 :   gfc_ss_info *ss_info;
     293       435970 :   int i;
     294              : 
     295       435970 :   ss_info = gfc_get_ss_info ();
     296       435970 :   ss_info->refcount++;
     297       435970 :   ss_info->type = type;
     298       435970 :   ss_info->expr = expr;
     299              : 
     300       435970 :   ss = gfc_get_ss ();
     301       435970 :   ss->info = ss_info;
     302       435970 :   ss->next = next;
     303       435970 :   ss->dimen = dimen;
     304       913783 :   for (i = 0; i < ss->dimen; i++)
     305       477813 :     ss->dim[i] = i;
     306              : 
     307       435970 :   return ss;
     308              : }
     309              : 
     310              : 
     311              : /* Creates and initializes a temporary type gfc_ss struct.  */
     312              : 
     313              : gfc_ss *
     314        11960 : gfc_get_temp_ss (tree type, tree string_length, int dimen)
     315              : {
     316        11960 :   gfc_ss *ss;
     317        11960 :   gfc_ss_info *ss_info;
     318        11960 :   int i;
     319              : 
     320        11960 :   ss_info = gfc_get_ss_info ();
     321        11960 :   ss_info->refcount++;
     322        11960 :   ss_info->type = GFC_SS_TEMP;
     323        11960 :   ss_info->string_length = string_length;
     324        11960 :   ss_info->data.temp.type = type;
     325              : 
     326        11960 :   ss = gfc_get_ss ();
     327        11960 :   ss->info = ss_info;
     328        11960 :   ss->next = gfc_ss_terminator;
     329        11960 :   ss->dimen = dimen;
     330        26681 :   for (i = 0; i < ss->dimen; i++)
     331        14721 :     ss->dim[i] = i;
     332              : 
     333        11960 :   return ss;
     334              : }
     335              : 
     336              : 
     337              : /* Creates and initializes a scalar type gfc_ss struct.  */
     338              : 
     339              : gfc_ss *
     340        68199 : gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
     341              : {
     342        68199 :   gfc_ss *ss;
     343        68199 :   gfc_ss_info *ss_info;
     344              : 
     345        68199 :   ss_info = gfc_get_ss_info ();
     346        68199 :   ss_info->refcount++;
     347        68199 :   ss_info->type = GFC_SS_SCALAR;
     348        68199 :   ss_info->expr = expr;
     349              : 
     350        68199 :   ss = gfc_get_ss ();
     351        68199 :   ss->info = ss_info;
     352        68199 :   ss->next = next;
     353              : 
     354        68199 :   return ss;
     355              : }
     356              : 
     357              : 
     358              : /* Free all the SS associated with a loop.  */
     359              : 
     360              : void
     361       193679 : gfc_cleanup_loop (gfc_loopinfo * loop)
     362              : {
     363       193679 :   gfc_loopinfo *loop_next, **ploop;
     364       193679 :   gfc_ss *ss;
     365       193679 :   gfc_ss *next;
     366              : 
     367       193679 :   ss = loop->ss;
     368       510686 :   while (ss != gfc_ss_terminator)
     369              :     {
     370       317007 :       gcc_assert (ss != NULL);
     371       317007 :       next = ss->loop_chain;
     372       317007 :       gfc_free_ss (ss);
     373       317007 :       ss = next;
     374              :     }
     375              : 
     376              :   /* Remove reference to self in the parent loop.  */
     377       193679 :   if (loop->parent)
     378         3364 :     for (ploop = &loop->parent->nested; *ploop; ploop = &(*ploop)->next)
     379         3364 :       if (*ploop == loop)
     380              :         {
     381         3364 :           *ploop = loop->next;
     382         3364 :           break;
     383              :         }
     384              : 
     385              :   /* Free non-freed nested loops.  */
     386       197043 :   for (loop = loop->nested; loop; loop = loop_next)
     387              :     {
     388         3364 :       loop_next = loop->next;
     389         3364 :       gfc_cleanup_loop (loop);
     390         3364 :       free (loop);
     391              :     }
     392       193679 : }
     393              : 
     394              : 
     395              : static void
     396       261445 : set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
     397              : {
     398       261445 :   int n;
     399              : 
     400       587741 :   for (; ss != gfc_ss_terminator; ss = ss->next)
     401              :     {
     402       326296 :       ss->loop = loop;
     403              : 
     404       326296 :       if (ss->info->type == GFC_SS_SCALAR
     405              :           || ss->info->type == GFC_SS_REFERENCE
     406       276368 :           || ss->info->type == GFC_SS_TEMP)
     407        61888 :         continue;
     408              : 
     409      4230528 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
     410      3966120 :         if (ss->info->data.array.subscript[n] != NULL)
     411         7671 :           set_ss_loop (ss->info->data.array.subscript[n], loop);
     412              :     }
     413       261445 : }
     414              : 
     415              : 
     416              : /* Associate a SS chain with a loop.  */
     417              : 
     418              : void
     419       253774 : gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
     420              : {
     421       253774 :   gfc_ss *ss;
     422       253774 :   gfc_loopinfo *nested_loop;
     423              : 
     424       253774 :   if (head == gfc_ss_terminator)
     425              :     return;
     426              : 
     427       253774 :   set_ss_loop (head, loop);
     428              : 
     429       253774 :   ss = head;
     430       826173 :   for (; ss && ss != gfc_ss_terminator; ss = ss->next)
     431              :     {
     432       318625 :       if (ss->nested_ss)
     433              :         {
     434         4740 :           nested_loop = ss->nested_ss->loop;
     435              : 
     436              :           /* More than one ss can belong to the same loop.  Hence, we add the
     437              :              loop to the chain only if it is different from the previously
     438              :              added one, to avoid duplicate nested loops.  */
     439         4740 :           if (nested_loop != loop->nested)
     440              :             {
     441         3364 :               gcc_assert (nested_loop->parent == NULL);
     442         3364 :               nested_loop->parent = loop;
     443              : 
     444         3364 :               gcc_assert (nested_loop->next == NULL);
     445         3364 :               nested_loop->next = loop->nested;
     446         3364 :               loop->nested = nested_loop;
     447              :             }
     448              :           else
     449         1376 :             gcc_assert (nested_loop->parent == loop);
     450              :         }
     451              : 
     452       318625 :       if (ss->next == gfc_ss_terminator)
     453       253774 :         ss->loop_chain = loop->ss;
     454              :       else
     455              :         ss->loop_chain = ss->next;
     456              :     }
     457       253774 :   gcc_assert (ss == gfc_ss_terminator);
     458       253774 :   loop->ss = head;
     459              : }
     460              : 
     461              : 
     462              : /* Returns true if the expression is an array pointer.  The tree must be a
     463              :    descriptor.  */
     464              : 
     465              : static bool
     466       572676 : is_pointer_array (tree expr)
     467              : {
     468       572676 :   if (expr == NULL_TREE
     469       572676 :       || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
     470       700356 :       || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
     471              :     return false;
     472              : 
     473       127680 :   if (VAR_P (expr)
     474       127680 :       && GFC_DECL_PTR_ARRAY_P (expr))
     475              :     return true;
     476              : 
     477       120168 :   if (TREE_CODE (expr) == PARM_DECL
     478       120168 :       && GFC_DECL_PTR_ARRAY_P (expr))
     479              :     return true;
     480              : 
     481       120168 :   if (INDIRECT_REF_P (expr)
     482       120168 :       && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
     483              :     return true;
     484              : 
     485              :   /* The field declaration is marked as a pointer array.  */
     486       117611 :   if (TREE_CODE (expr) == COMPONENT_REF
     487       117611 :       && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1)))
     488         3933 :     return true;
     489              : 
     490              :   return false;
     491              : }
     492              : 
     493              : 
     494              : /* If the elements of the array are spaced by the span of its descriptor,
     495              :    return the decl that provides that span, otherwise NULL_TREE.  This is
     496              :    either a descriptor or the local decl of a descriptorless dummy array,
     497              :    which keeps the descriptor it was built from as the saved one.  */
     498              : 
     499              : static bool
     500       572676 : is_span_addressed_array (tree expr)
     501              : {
     502       572676 :   if (is_pointer_array (expr))
     503              :     {
     504              :       /* For classes, index arrays using the size from the virtual pointer if
     505              :          the array is contiguous.  Otherwise use the span.  */
     506        14002 :       if (TREE_CODE (expr) == COMPONENT_REF
     507         3933 :           && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
     508        14246 :           && TYPE_LANG_SPECIFIC (TREE_TYPE (expr)))
     509              :         {
     510          244 :           switch (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (expr)))
     511              :             {
     512              :             case GFC_ARRAY_ASSUMED_SHAPE_CONT:
     513              :             case GFC_ARRAY_ASSUMED_RANK_CONT:
     514              :             case GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE:
     515              :             case GFC_ARRAY_ASSUMED_RANK_POINTER_CONT:
     516              :             case GFC_ARRAY_ALLOCATABLE:
     517              :             case GFC_ARRAY_POINTER_CONT:
     518              :               return false;
     519              : 
     520              :             default:
     521              :               break;
     522              :             }
     523              :         }
     524              : 
     525        14002 :       return true;
     526              :     }
     527              : 
     528       558674 :   if (VAR_P (expr)
     529       488273 :       && GFC_DECL_PTR_ARRAY_P (expr)
     530          319 :       && !GFC_DECL_CLASS (expr)
     531          319 :       && GFC_ARRAY_TYPE_P (TREE_TYPE (expr))
     532          319 :       && DECL_LANG_SPECIFIC (expr)
     533       558993 :       && GFC_DECL_SAVED_DESCRIPTOR (expr))
     534          319 :     return true;
     535              : 
     536              :   return false;
     537              : }
     538              : 
     539              : 
     540              : /* Return true if the spacing of the elements of a directly passed actual
     541              :    argument can be folded into the strides of the dummy's descriptor, so
     542              :    that the elements are addressed by a constant element length instead of
     543              :    by the span.  */
     544              : 
     545              : bool
     546        14370 : gfc_span_folds_into_stride (gfc_symbol *sym)
     547              : {
     548        14370 :   if (!gfc_dummy_requires_direct_arg (sym))
     549              :     return false;
     550              : 
     551              :   /* A character element length is not necessarily constant and a complex or
     552              :      derived type can be larger than its alignment.  */
     553         7323 :   if (sym->ts.type != BT_INTEGER
     554         5735 :       && sym->ts.type != BT_REAL
     555         2271 :       && sym->ts.type != BT_LOGICAL)
     556              :     return false;
     557              : 
     558              :   /* An assumed rank dummy has no strides to fold the spacing into.  */
     559         5052 :   if (!sym->as || sym->as->type != AS_ASSUMED_SHAPE || sym->as->rank < 1)
     560              :     return false;
     561              : 
     562         4918 :   tree etype = gfc_typenode_for_spec (&sym->ts);
     563         4918 :   tree size = etype ? TYPE_SIZE_UNIT (etype) : NULL_TREE;
     564              : 
     565         4918 :   return (size
     566         4918 :           && tree_fits_uhwi_p (size)
     567         9836 :           && tree_to_uhwi (size) == min_align_of_type (etype));
     568              : }
     569              : 
     570              : 
     571              : /* Check if a dummy argument must be addressed using the span of its
     572              :    descriptor.  Where the spacing of the elements is folded into the strides
     573              :    instead, the dummy is addressed like any other array and its descriptor is
     574              :    built with the element length as span, so it is not span addressed.  */
     575              : 
     576              : bool
     577       624808 : gfc_is_span_addressed_dummy (gfc_symbol *sym)
     578              : {
     579       624808 :   return gfc_dummy_requires_direct_arg (sym)
     580       624808 :          && !gfc_span_folds_into_stride (sym);
     581              : }
     582              : 
     583              : 
     584              : /* Set se->expr to a test that the span of the descriptor of ARG is the
     585              :    element length, ie. that its elements are not subobjects of larger
     586              :    ones.  */
     587              : 
     588              : void
     589           90 : gfc_conv_span_is_elem_len (gfc_se *se, gfc_expr *arg)
     590              : {
     591           90 :   gfc_se argse;
     592           90 :   gfc_ss *ss;
     593              : 
     594           90 :   if (arg->ts.type == BT_CLASS)
     595            0 :     gfc_add_class_array_ref (arg);
     596              : 
     597           90 :   ss = gfc_walk_expr (arg);
     598           90 :   gcc_assert (ss != gfc_ss_terminator);
     599              : 
     600           90 :   gfc_init_se (&argse, NULL);
     601           90 :   argse.data_not_needed = 1;
     602           90 :   gfc_conv_expr_descriptor (&argse, arg);
     603           90 :   gfc_add_block_to_block (&se->pre, &argse.pre);
     604           90 :   gfc_add_block_to_block (&se->post, &argse.post);
     605           90 :   gfc_free_ss_chain (ss);
     606              : 
     607           90 :   tree desc = gfc_evaluate_now (argse.expr, &se->pre);
     608           90 :   tree span = gfc_conv_descriptor_span_get (desc);
     609           90 :   tree elem_len = fold_convert (TREE_TYPE (span),
     610              :                                 gfc_conv_descriptor_elem_len_get (desc));
     611           90 :   se->expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
     612              :                               span, elem_len);
     613           90 : }
     614              : 
     615              : 
     616              : /* If the symbol or expression reference a CFI descriptor, return the
     617              :    pointer to the converted gfc descriptor. If an array reference is
     618              :    present as the last argument, check that it is the one applied to
     619              :    the CFI descriptor in the expression. Note that the CFI object is
     620              :    always the symbol in the expression!  */
     621              : 
     622              : static bool
     623       392490 : get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
     624              :               tree *desc, gfc_array_ref *ar)
     625              : {
     626       392490 :   tree tmp;
     627              : 
     628       392490 :   if (!is_CFI_desc (sym, expr))
     629              :     return false;
     630              : 
     631         4727 :   if (expr && ar)
     632              :     {
     633         4061 :       if (!(expr->ref && expr->ref->type == REF_ARRAY)
     634         4043 :           || (&expr->ref->u.ar != ar))
     635              :         return false;
     636              :     }
     637              : 
     638         4697 :   if (sym == NULL)
     639         1108 :     tmp = expr->symtree->n.sym->backend_decl;
     640              :   else
     641         3589 :     tmp = sym->backend_decl;
     642              : 
     643         4697 :   if (tmp && DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
     644            0 :     tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
     645              : 
     646         4697 :   *desc = tmp;
     647         4697 :   return true;
     648              : }
     649              : 
     650              : 
     651              : /* A helper function for gfc_get_array_span that returns the array element size
     652              :    of a class entity.  */
     653              : static tree
     654         1068 : class_array_element_size (tree decl, bool unlimited)
     655              : {
     656              :   /* Class dummys usually require extraction from the saved descriptor,
     657              :      which gfc_class_vptr_get does for us if necessary. This, of course,
     658              :      will be a component of the class object.  */
     659         1068 :   tree vptr = gfc_class_vptr_get (decl);
     660              :   /* If this is an unlimited polymorphic entity with a character payload,
     661              :      the element size will be corrected for the string length.  */
     662         1068 :   if (unlimited)
     663          938 :     return gfc_resize_class_size_with_len (NULL,
     664          469 :                                            TREE_OPERAND (vptr, 0),
     665          469 :                                            gfc_vptr_size_get (vptr));
     666              :   else
     667          599 :     return gfc_vptr_size_get (vptr);
     668              : }
     669              : 
     670              : 
     671              : /* Given a data reference EXPR, return the main underlying declaration
     672              :    (variable, parameter, etc) if there is one, otherwise return NULL_TREE.
     673              :    Having the declaration gives access to the additional metadata stored in
     674              :    its lang-specific node.  */
     675              : 
     676              : static tree
     677        64480 : get_main_decl (tree expr)
     678              : {
     679        64480 :   tree decl = expr;
     680        66531 :   while (decl && !DECL_P (decl))
     681              :     {
     682         2741 :       if (TREE_CODE (decl) == ADDR_EXPR
     683              :           || INDIRECT_REF_P (decl)
     684              :           || CONVERT_EXPR_P (decl))
     685         2051 :         decl = TREE_OPERAND (decl, 0);
     686              :       else
     687              :         decl = NULL_TREE;
     688              :     }
     689              : 
     690        64480 :   return decl;
     691              : }
     692              : 
     693              : 
     694              : /* Return the span of an array.  */
     695              : 
     696              : tree
     697        64480 : gfc_get_array_span (tree desc, gfc_expr *expr)
     698              : {
     699        64480 :   tree tmp;
     700        64480 :   gfc_symbol *sym = (expr && expr->expr_type == EXPR_VARIABLE) ?
     701        56793 :                     expr->symtree->n.sym : NULL;
     702              : 
     703        64480 :   tree decl = get_main_decl (desc);
     704              : 
     705        65170 :   if (tree span = GFC_DECL_GET_SPAN (decl ? decl : desc))
     706              :     /* A span addressed dummy loaded its span on entry.  */
     707              :     tmp = span;
     708        64228 :   else if (is_span_addressed_array (desc)
     709        64228 :            || (get_CFI_desc (NULL, expr, &desc, NULL)
     710         1332 :                && (POINTER_TYPE_P (TREE_TYPE (desc))
     711          666 :                    ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
     712            0 :                    : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
     713              :     /* This will have the span field set.  */
     714          638 :     tmp = gfc_conv_descriptor_span_get (gfc_get_span_descriptor (desc));
     715        63590 :   else if (expr->ts.type == BT_ASSUMED)
     716              :     {
     717          127 :       if (DECL_LANG_SPECIFIC (desc) && GFC_DECL_SAVED_DESCRIPTOR (desc))
     718          127 :         desc = GFC_DECL_SAVED_DESCRIPTOR (desc);
     719          127 :       if (POINTER_TYPE_P (TREE_TYPE (desc)))
     720          127 :         desc = build_fold_indirect_ref_loc (input_location, desc);
     721          127 :       tmp = gfc_conv_descriptor_span_get (desc);
     722              :     }
     723        63463 :   else if (TREE_CODE (desc) == COMPONENT_REF
     724          539 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
     725        63568 :            && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
     726              :     /* The descriptor is the _data field of a class object.  */
     727           32 :     tmp = class_array_element_size (TREE_OPERAND (desc, 0),
     728           32 :                                     UNLIMITED_POLY (expr));
     729        63431 :   else if (sym && sym->ts.type == BT_CLASS
     730         1092 :            && expr->ref->type == REF_COMPONENT
     731         1092 :            && expr->ref->next->type == REF_ARRAY
     732         1092 :            && expr->ref->next->next == NULL
     733         1074 :            && CLASS_DATA (sym)->attr.dimension)
     734              :     /* Having escaped the above, this can only be a class array dummy.  */
     735         1036 :     tmp = class_array_element_size (sym->backend_decl,
     736         1036 :                                     UNLIMITED_POLY (sym));
     737              :   else
     738              :     {
     739              :       /* If none of the fancy stuff works, the span is the element
     740              :          size of the array. Attempt to deal with unbounded character
     741              :          types if possible. Otherwise, return NULL_TREE.  */
     742        62395 :       tmp = gfc_get_element_type (TREE_TYPE (desc));
     743        62395 :       if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
     744              :         {
     745        11089 :           gcc_assert (expr->ts.type == BT_CHARACTER);
     746              : 
     747        11089 :           tmp = gfc_get_character_len_in_bytes (tmp);
     748              : 
     749        11089 :           if (tmp == NULL_TREE || integer_zerop (tmp))
     750              :             {
     751           68 :               tree bs;
     752              : 
     753           68 :               tmp = gfc_get_expr_charlen (expr);
     754           68 :               tmp = fold_convert (gfc_array_index_type, tmp);
     755           68 :               bs = build_int_cst (gfc_array_index_type, expr->ts.kind);
     756           68 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
     757              :                                      gfc_array_index_type, tmp, bs);
     758              :             }
     759              : 
     760        22110 :           tmp = (tmp && !integer_zerop (tmp))
     761        22110 :             ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
     762              :         }
     763              :       else
     764        51306 :         tmp = fold_convert (gfc_array_index_type,
     765              :                             size_in_bytes (tmp));
     766              :     }
     767        64480 :   return tmp;
     768              : }
     769              : 
     770              : 
     771              : /* Generate an initializer for a static pointer or allocatable array.  */
     772              : 
     773              : void
     774          276 : gfc_trans_static_array_pointer (gfc_symbol * sym)
     775              : {
     776          276 :   tree type;
     777              : 
     778          276 :   gcc_assert (TREE_STATIC (sym->backend_decl));
     779              :   /* Just zero the data member.  */
     780          276 :   type = TREE_TYPE (sym->backend_decl);
     781          276 :   DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
     782          276 : }
     783              : 
     784              : 
     785              : /* If the bounds of SE's loop have not yet been set, see if they can be
     786              :    determined from array spec AS, which is the array spec of a called
     787              :    function.  MAPPING maps the callee's dummy arguments to the values
     788              :    that the caller is passing.  Add any initialization and finalization
     789              :    code to SE.  */
     790              : 
     791              : void
     792         9089 : gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
     793              :                                      gfc_se * se, gfc_array_spec * as)
     794              : {
     795         9089 :   int n, dim, total_dim;
     796         9089 :   gfc_se tmpse;
     797         9089 :   gfc_ss *ss;
     798         9089 :   tree lower;
     799         9089 :   tree upper;
     800         9089 :   tree tmp;
     801              : 
     802         9089 :   total_dim = 0;
     803              : 
     804         9089 :   if (!as || as->type != AS_EXPLICIT)
     805         7892 :     return;
     806              : 
     807         2419 :   for (ss = se->ss; ss; ss = ss->parent)
     808              :     {
     809         1222 :       total_dim += ss->loop->dimen;
     810         2823 :       for (n = 0; n < ss->loop->dimen; n++)
     811              :         {
     812              :           /* The bound is known, nothing to do.  */
     813         1601 :           if (ss->loop->to[n] != NULL_TREE)
     814          485 :             continue;
     815              : 
     816         1116 :           dim = ss->dim[n];
     817         1116 :           gcc_assert (dim < as->rank);
     818         1116 :           gcc_assert (ss->loop->dimen <= as->rank);
     819              : 
     820              :           /* Evaluate the lower bound.  */
     821         1116 :           gfc_init_se (&tmpse, NULL);
     822         1116 :           gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
     823         1116 :           gfc_add_block_to_block (&se->pre, &tmpse.pre);
     824         1116 :           gfc_add_block_to_block (&se->post, &tmpse.post);
     825         1116 :           lower = fold_convert (gfc_array_index_type, tmpse.expr);
     826              : 
     827              :           /* ...and the upper bound.  */
     828         1116 :           gfc_init_se (&tmpse, NULL);
     829         1116 :           gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
     830         1116 :           gfc_add_block_to_block (&se->pre, &tmpse.pre);
     831         1116 :           gfc_add_block_to_block (&se->post, &tmpse.post);
     832         1116 :           upper = fold_convert (gfc_array_index_type, tmpse.expr);
     833              : 
     834              :           /* Set the upper bound of the loop to UPPER - LOWER.  */
     835         1116 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
     836              :                                  gfc_array_index_type, upper, lower);
     837         1116 :           tmp = gfc_evaluate_now (tmp, &se->pre);
     838         1116 :           ss->loop->to[n] = tmp;
     839              :         }
     840              :     }
     841              : 
     842         1197 :   gcc_assert (total_dim == as->rank);
     843              : }
     844              : 
     845              : 
     846              : /* Generate code to allocate an array temporary, or create a variable to
     847              :    hold the data.  If size is NULL, zero the descriptor so that the
     848              :    callee will allocate the array.  If DEALLOC is true, also generate code to
     849              :    free the array afterwards.
     850              : 
     851              :    If INITIAL is not NULL, it is packed using internal_pack and the result used
     852              :    as data instead of allocating a fresh, uninitialized area of memory.
     853              : 
     854              :    Initialization code is added to PRE and finalization code to POST.
     855              :    DYNAMIC is true if the caller may want to extend the array later
     856              :    using realloc.  This prevents us from putting the array on the stack.  */
     857              : 
     858              : static void
     859        29324 : gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
     860              :                                   gfc_array_info * info, tree size, tree nelem,
     861              :                                   tree initial, bool dynamic, bool dealloc)
     862              : {
     863        29324 :   tree tmp;
     864        29324 :   tree desc;
     865        29324 :   bool onstack;
     866              : 
     867        29324 :   desc = info->descriptor;
     868        29324 :   info->offset = gfc_index_zero_node;
     869        29324 :   if (size == NULL_TREE || (dynamic && integer_zerop (size)))
     870              :     {
     871              :       /* A callee allocated array.  */
     872         3088 :       gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
     873         3088 :       onstack = false;
     874              :     }
     875              :   else
     876              :     {
     877              :       /* Allocate the temporary.  */
     878        52472 :       onstack = !dynamic && initial == NULL_TREE
     879        26236 :                          && (flag_stack_arrays
     880        25851 :                              || gfc_can_put_var_on_stack (size));
     881              : 
     882         5577 :       if (onstack)
     883              :         {
     884              :           /* Make a temporary variable to hold the data.  */
     885        20659 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
     886              :                                  nelem, gfc_index_one_node);
     887        20659 :           tmp = gfc_evaluate_now (tmp, pre);
     888        20659 :           tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
     889              :                                   tmp);
     890        20659 :           tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
     891              :                                   tmp);
     892        20659 :           tmp = gfc_create_var (tmp, "A");
     893              :           /* If we're here only because of -fstack-arrays we have to
     894              :              emit a DECL_EXPR to make the gimplifier emit alloca calls.  */
     895        20659 :           if (!gfc_can_put_var_on_stack (size))
     896           17 :             gfc_add_expr_to_block (pre,
     897              :                                    fold_build1_loc (input_location,
     898           17 :                                                     DECL_EXPR, TREE_TYPE (tmp),
     899              :                                                     tmp));
     900        20659 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
     901        20659 :           gfc_conv_descriptor_data_set (pre, desc, tmp);
     902              :         }
     903              :       else
     904              :         {
     905              :           /* Allocate memory to hold the data or call internal_pack.  */
     906         5577 :           if (initial == NULL_TREE)
     907              :             {
     908         5434 :               tmp = gfc_call_malloc (pre, NULL, size);
     909         5434 :               tmp = gfc_evaluate_now (tmp, pre);
     910              :             }
     911              :           else
     912              :             {
     913          143 :               tree packed;
     914          143 :               tree source_data;
     915          143 :               tree was_packed;
     916          143 :               stmtblock_t do_copying;
     917              : 
     918          143 :               tmp = TREE_TYPE (initial); /* Pointer to descriptor.  */
     919          143 :               gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
     920          143 :               tmp = TREE_TYPE (tmp); /* The descriptor itself.  */
     921          143 :               tmp = gfc_get_element_type (tmp);
     922          143 :               packed = gfc_create_var (build_pointer_type (tmp), "data");
     923              : 
     924          143 :               tmp = build_call_expr_loc (input_location,
     925              :                                      gfor_fndecl_in_pack, 1, initial);
     926          143 :               tmp = fold_convert (TREE_TYPE (packed), tmp);
     927          143 :               gfc_add_modify (pre, packed, tmp);
     928              : 
     929          143 :               tmp = build_fold_indirect_ref_loc (input_location,
     930              :                                              initial);
     931          143 :               source_data = gfc_conv_descriptor_data_get (tmp);
     932              : 
     933              :               /* internal_pack may return source->data without any allocation
     934              :                  or copying if it is already packed.  If that's the case, we
     935              :                  need to allocate and copy manually.  */
     936              : 
     937          143 :               gfc_start_block (&do_copying);
     938          143 :               tmp = gfc_call_malloc (&do_copying, NULL, size);
     939          143 :               tmp = fold_convert (TREE_TYPE (packed), tmp);
     940          143 :               gfc_add_modify (&do_copying, packed, tmp);
     941          143 :               tmp = gfc_build_memcpy_call (packed, source_data, size);
     942          143 :               gfc_add_expr_to_block (&do_copying, tmp);
     943              : 
     944          143 :               was_packed = fold_build2_loc (input_location, EQ_EXPR,
     945              :                                             logical_type_node, packed,
     946              :                                             source_data);
     947          143 :               tmp = gfc_finish_block (&do_copying);
     948          143 :               tmp = build3_v (COND_EXPR, was_packed, tmp,
     949              :                               build_empty_stmt (input_location));
     950          143 :               gfc_add_expr_to_block (pre, tmp);
     951              : 
     952          143 :               tmp = fold_convert (pvoid_type_node, packed);
     953              :             }
     954              : 
     955         5577 :           gfc_conv_descriptor_data_set (pre, desc, tmp);
     956              :         }
     957              :     }
     958        29324 :   info->data = gfc_conv_descriptor_data_get (desc);
     959              : 
     960              :   /* The offset is zero because we create temporaries with a zero
     961              :      lower bound.  */
     962        29324 :   gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
     963              : 
     964        29324 :   if (dealloc && !onstack)
     965              :     {
     966              :       /* Free the temporary.  */
     967         8247 :       tmp = gfc_conv_descriptor_data_get (desc);
     968         8247 :       tmp = gfc_call_free (tmp);
     969         8247 :       gfc_add_expr_to_block (post, tmp);
     970              :     }
     971        29324 : }
     972              : 
     973              : 
     974              : /* Get the scalarizer array dimension corresponding to actual array dimension
     975              :    given by ARRAY_DIM.
     976              : 
     977              :    For example, if SS represents the array ref a(1,:,:,1), it is a
     978              :    bidimensional scalarizer array, and the result would be 0 for ARRAY_DIM=1,
     979              :    and 1 for ARRAY_DIM=2.
     980              :    If SS represents transpose(a(:,1,1,:)), it is again a bidimensional
     981              :    scalarizer array, and the result would be 1 for ARRAY_DIM=0 and 0 for
     982              :    ARRAY_DIM=3.
     983              :    If SS represents sum(a(:,:,:,1), dim=1), it is a 2+1-dimensional scalarizer
     984              :    array.  If called on the inner ss, the result would be respectively 0,1,2 for
     985              :    ARRAY_DIM=0,1,2.  If called on the outer ss, the result would be 0,1
     986              :    for ARRAY_DIM=1,2.  */
     987              : 
     988              : static int
     989       275317 : get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
     990              : {
     991       275317 :   int array_ref_dim;
     992       275317 :   int n;
     993              : 
     994       275317 :   array_ref_dim = 0;
     995              : 
     996       556767 :   for (; ss; ss = ss->parent)
     997       724200 :     for (n = 0; n < ss->dimen; n++)
     998       442750 :       if (ss->dim[n] < array_dim)
     999        80803 :         array_ref_dim++;
    1000              : 
    1001       275317 :   return array_ref_dim;
    1002              : }
    1003              : 
    1004              : 
    1005              : static gfc_ss *
    1006       232959 : innermost_ss (gfc_ss *ss)
    1007              : {
    1008       424998 :   while (ss->nested_ss != NULL)
    1009              :     ss = ss->nested_ss;
    1010              : 
    1011       416790 :   return ss;
    1012              : }
    1013              : 
    1014              : 
    1015              : 
    1016              : /* Get the array reference dimension corresponding to the given loop dimension.
    1017              :    It is different from the true array dimension given by the dim array in
    1018              :    the case of a partial array reference (i.e. a(:,:,1,:) for example)
    1019              :    It is different from the loop dimension in the case of a transposed array.
    1020              :    */
    1021              : 
    1022              : static int
    1023       232959 : get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
    1024              : {
    1025       232959 :   return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
    1026       232959 :                                            ss->dim[loop_dim]);
    1027              : }
    1028              : 
    1029              : 
    1030              : /* Use the information in the ss to obtain the required information about
    1031              :    the type and size of an array temporary, when the lhs in an assignment
    1032              :    is a class expression.  */
    1033              : 
    1034              : static tree
    1035          351 : get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype,
    1036              :                         gfc_ss **fcnss)
    1037              : {
    1038          351 :   gfc_ss *loop_ss = ss->loop->ss;
    1039          351 :   gfc_ss *lhs_ss;
    1040          351 :   gfc_ss *rhs_ss;
    1041          351 :   gfc_ss *fcn_ss = NULL;
    1042          351 :   tree tmp;
    1043          351 :   tree tmp2;
    1044          351 :   tree vptr;
    1045          351 :   tree class_expr = NULL_TREE;
    1046          351 :   tree lhs_class_expr = NULL_TREE;
    1047          351 :   bool unlimited_rhs = false;
    1048          351 :   bool unlimited_lhs = false;
    1049          351 :   bool rhs_function = false;
    1050          351 :   bool unlimited_arg1 = false;
    1051          351 :   gfc_symbol *vtab;
    1052          351 :   tree cntnr = NULL_TREE;
    1053              : 
    1054              :   /* The second element in the loop chain contains the source for the
    1055              :      class temporary created in gfc_trans_create_temp_array.  */
    1056          351 :   rhs_ss = loop_ss->loop_chain;
    1057              : 
    1058          351 :   if (rhs_ss != gfc_ss_terminator
    1059          327 :       && rhs_ss->info
    1060          327 :       && rhs_ss->info->expr
    1061          327 :       && rhs_ss->info->expr->ts.type == BT_CLASS
    1062          199 :       && rhs_ss->info->data.array.descriptor)
    1063              :     {
    1064          187 :       if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
    1065           37 :         class_expr
    1066           37 :           = gfc_get_class_from_expr (rhs_ss->info->data.array.descriptor);
    1067              :       else
    1068          150 :         class_expr = gfc_get_class_from_gfc_expr (rhs_ss->info->expr);
    1069          187 :       unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
    1070          187 :       if (rhs_ss->info->expr->expr_type == EXPR_FUNCTION)
    1071              :         rhs_function = true;
    1072              :     }
    1073              : 
    1074              :   /* Usually, ss points to the function. When the function call is an actual
    1075              :      argument, it is instead rhs_ss because the ss chain is shifted by one.  */
    1076          351 :   *fcnss = fcn_ss = rhs_function ? rhs_ss : ss;
    1077              : 
    1078              :   /* If this is a transformational function with a class result, the info
    1079              :      class_container field points to the class container of arg1.  */
    1080          351 :   if (class_expr != NULL_TREE
    1081          187 :       && fcn_ss->info && fcn_ss->info->expr
    1082           91 :       && fcn_ss->info->expr->expr_type == EXPR_FUNCTION
    1083           91 :       && fcn_ss->info->expr->value.function.isym
    1084           60 :       && fcn_ss->info->expr->value.function.isym->transformational)
    1085              :     {
    1086           60 :       cntnr = ss->info->class_container;
    1087           60 :       unlimited_arg1
    1088           60 :            = UNLIMITED_POLY (fcn_ss->info->expr->value.function.actual->expr);
    1089              :     }
    1090              : 
    1091              :   /* For an assignment the lhs is the next element in the loop chain.
    1092              :      If we have a class rhs, this had better be a class variable
    1093              :      expression!  Otherwise, the class container from arg1 can be used
    1094              :      to set the vptr and len fields of the result class container.  */
    1095          351 :   lhs_ss = rhs_ss->loop_chain;
    1096          351 :   if (lhs_ss && lhs_ss != gfc_ss_terminator
    1097          225 :       && lhs_ss->info && lhs_ss->info->expr
    1098          225 :       && lhs_ss->info->expr->expr_type ==EXPR_VARIABLE
    1099          225 :       && lhs_ss->info->expr->ts.type == BT_CLASS)
    1100              :     {
    1101          225 :       tmp = lhs_ss->info->data.array.descriptor;
    1102          225 :       unlimited_lhs = UNLIMITED_POLY (rhs_ss->info->expr);
    1103              :     }
    1104          126 :   else if (cntnr != NULL_TREE)
    1105              :     {
    1106           54 :       tmp = gfc_class_vptr_get (class_expr);
    1107           54 :       gfc_add_modify (pre, tmp, fold_convert (TREE_TYPE (tmp),
    1108              :                                               gfc_class_vptr_get (cntnr)));
    1109           54 :       if (unlimited_rhs)
    1110              :         {
    1111            6 :           tmp = gfc_class_len_get (class_expr);
    1112            6 :           if (unlimited_arg1)
    1113            6 :             gfc_add_modify (pre, tmp, gfc_class_len_get (cntnr));
    1114              :         }
    1115              :       tmp = NULL_TREE;
    1116              :     }
    1117              :   else
    1118              :     tmp = NULL_TREE;
    1119              : 
    1120              :   /* Get the lhs class expression.  */
    1121          231 :   if (tmp != NULL_TREE && lhs_ss->loop_chain == gfc_ss_terminator)
    1122          213 :     lhs_class_expr = gfc_get_class_from_expr (tmp);
    1123              :   else
    1124              :     return class_expr;
    1125              : 
    1126          213 :   gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (lhs_class_expr)));
    1127              : 
    1128              :   /* Set the lhs vptr and, if necessary, the _len field.  */
    1129          213 :   if (class_expr)
    1130              :     {
    1131              :       /* Both lhs and rhs are class expressions.  */
    1132           79 :       tmp = gfc_class_vptr_get (lhs_class_expr);
    1133          158 :       gfc_add_modify (pre, tmp,
    1134           79 :                       fold_convert (TREE_TYPE (tmp),
    1135              :                                     gfc_class_vptr_get (class_expr)));
    1136           79 :       if (unlimited_lhs)
    1137              :         {
    1138           31 :           gcc_assert (unlimited_rhs);
    1139           31 :           tmp = gfc_class_len_get (lhs_class_expr);
    1140           31 :           tmp2 = gfc_class_len_get (class_expr);
    1141           31 :           gfc_add_modify (pre, tmp, tmp2);
    1142              :         }
    1143              :     }
    1144          134 :   else if (rhs_ss->info->data.array.descriptor)
    1145              :    {
    1146              :       /* lhs is class and rhs is intrinsic or derived type.  */
    1147          128 :       *eltype = TREE_TYPE (rhs_ss->info->data.array.descriptor);
    1148          128 :       *eltype = gfc_get_element_type (*eltype);
    1149          128 :       vtab = gfc_find_vtab (&rhs_ss->info->expr->ts);
    1150          128 :       vptr = vtab->backend_decl;
    1151          128 :       if (vptr == NULL_TREE)
    1152           24 :         vptr = gfc_get_symbol_decl (vtab);
    1153          128 :       vptr = gfc_build_addr_expr (NULL_TREE, vptr);
    1154          128 :       tmp = gfc_class_vptr_get (lhs_class_expr);
    1155          128 :       gfc_add_modify (pre, tmp,
    1156          128 :                       fold_convert (TREE_TYPE (tmp), vptr));
    1157              : 
    1158          128 :       if (unlimited_lhs)
    1159              :         {
    1160            0 :           tmp = gfc_class_len_get (lhs_class_expr);
    1161            0 :           if (rhs_ss->info
    1162            0 :               && rhs_ss->info->expr
    1163            0 :               && rhs_ss->info->expr->ts.type == BT_CHARACTER)
    1164            0 :             tmp2 = build_int_cst (TREE_TYPE (tmp),
    1165            0 :                                   rhs_ss->info->expr->ts.kind);
    1166              :           else
    1167            0 :             tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
    1168            0 :           gfc_add_modify (pre, tmp, tmp2);
    1169              :         }
    1170              :     }
    1171              : 
    1172              :   return class_expr;
    1173              : }
    1174              : 
    1175              : 
    1176              : 
    1177              : /* Generate code to create and initialize the descriptor for a temporary
    1178              :    array.  This is used for both temporaries needed by the scalarizer, and
    1179              :    functions returning arrays.  Adjusts the loop variables to be
    1180              :    zero-based, and calculates the loop bounds for callee allocated arrays.
    1181              :    Allocate the array unless it's callee allocated (we have a callee
    1182              :    allocated array if 'callee_alloc' is true, or if loop->to[n] is
    1183              :    NULL_TREE for any n).  Also fills in the descriptor, data and offset
    1184              :    fields of info if known.  Returns the size of the array, or NULL for a
    1185              :    callee allocated array.
    1186              : 
    1187              :    'eltype' == NULL signals that the temporary should be a class object.
    1188              :    The 'initial' expression is used to obtain the size of the dynamic
    1189              :    type; otherwise the allocation and initialization proceeds as for any
    1190              :    other expression
    1191              : 
    1192              :    PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
    1193              :    gfc_trans_allocate_array_storage.  */
    1194              : 
    1195              : tree
    1196        29324 : gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, gfc_ss * ss,
    1197              :                              tree eltype, tree initial, bool dynamic,
    1198              :                              bool dealloc, bool callee_alloc, locus * where)
    1199              : {
    1200        29324 :   gfc_loopinfo *loop;
    1201        29324 :   gfc_ss *s;
    1202        29324 :   gfc_array_info *info;
    1203        29324 :   tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
    1204        29324 :   tree type;
    1205        29324 :   tree desc;
    1206        29324 :   tree tmp;
    1207        29324 :   tree size;
    1208        29324 :   tree nelem;
    1209        29324 :   tree cond;
    1210        29324 :   tree or_expr;
    1211        29324 :   tree elemsize;
    1212        29324 :   tree class_expr = NULL_TREE;
    1213        29324 :   gfc_ss *fcn_ss = NULL;
    1214        29324 :   int n, dim, tmp_dim;
    1215        29324 :   int total_dim = 0;
    1216              : 
    1217              :   /* This signals a class array for which we need the size of the
    1218              :      dynamic type.  Generate an eltype and then the class expression.  */
    1219        29324 :   if (eltype == NULL_TREE && initial)
    1220              :     {
    1221            0 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (initial)));
    1222            0 :       class_expr = build_fold_indirect_ref_loc (input_location, initial);
    1223              :       /* Obtain the structure (class) expression.  */
    1224            0 :       class_expr = gfc_get_class_from_expr (class_expr);
    1225            0 :       gcc_assert (class_expr);
    1226              :     }
    1227              : 
    1228              :   /* Otherwise, some expressions, such as class functions, arising from
    1229              :      dependency checking in assignments come here with class element type.
    1230              :      The descriptor can be obtained from the ss->info and then converted
    1231              :      to the class object.  */
    1232        29324 :   if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
    1233          351 :     class_expr = get_class_info_from_ss (pre, ss, &eltype, &fcn_ss);
    1234              : 
    1235              :   /* If the dynamic type is not available, use the declared type.  */
    1236        29324 :   if (eltype && GFC_CLASS_TYPE_P (eltype))
    1237          223 :     eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
    1238              : 
    1239        29324 :   if (class_expr == NULL_TREE)
    1240        29137 :     elemsize = fold_convert (gfc_array_index_type,
    1241              :                              TYPE_SIZE_UNIT (eltype));
    1242              :   else
    1243              :     {
    1244              :       /* Unlimited polymorphic entities are initialised with NULL vptr. They
    1245              :          can be tested for by checking if the len field is present. If so
    1246              :          test the vptr before using the vtable size.  */
    1247          187 :       tmp = gfc_class_vptr_get (class_expr);
    1248          187 :       tmp = fold_build2_loc (input_location, NE_EXPR,
    1249              :                              logical_type_node,
    1250          187 :                              tmp, build_int_cst (TREE_TYPE (tmp), 0));
    1251          187 :       elemsize = fold_build3_loc (input_location, COND_EXPR,
    1252              :                                   gfc_array_index_type,
    1253              :                                   tmp,
    1254              :                                   gfc_class_vtab_size_get (class_expr),
    1255              :                                   gfc_index_zero_node);
    1256          187 :       elemsize = gfc_evaluate_now (elemsize, pre);
    1257          187 :       elemsize = gfc_resize_class_size_with_len (pre, class_expr, elemsize);
    1258              :       /* Casting the data as a character of the dynamic length ensures that
    1259              :          assignment of elements works when needed.  */
    1260          187 :       eltype = gfc_get_character_type_len (1, elemsize);
    1261              :     }
    1262              : 
    1263        29324 :   memset (from, 0, sizeof (from));
    1264        29324 :   memset (to, 0, sizeof (to));
    1265              : 
    1266        29324 :   info = &ss->info->data.array;
    1267              : 
    1268        29324 :   gcc_assert (ss->dimen > 0);
    1269        29324 :   gcc_assert (ss->loop->dimen == ss->dimen);
    1270              : 
    1271        29324 :   if (warn_array_temporaries && where)
    1272          205 :     gfc_warning (OPT_Warray_temporaries,
    1273              :                  "Creating array temporary at %L", where);
    1274              : 
    1275              :   /* Set the lower bound to zero.  */
    1276        58683 :   for (s = ss; s; s = s->parent)
    1277              :     {
    1278        29359 :       loop = s->loop;
    1279              : 
    1280        29359 :       total_dim += loop->dimen;
    1281        68113 :       for (n = 0; n < loop->dimen; n++)
    1282              :         {
    1283        38754 :           dim = s->dim[n];
    1284              : 
    1285              :           /* Callee allocated arrays may not have a known bound yet.  */
    1286        38754 :           if (loop->to[n])
    1287        35155 :             loop->to[n] = gfc_evaluate_now (
    1288              :                         fold_build2_loc (input_location, MINUS_EXPR,
    1289              :                                          gfc_array_index_type,
    1290              :                                          loop->to[n], loop->from[n]),
    1291              :                         pre);
    1292        38754 :           loop->from[n] = gfc_index_zero_node;
    1293              : 
    1294              :           /* We have just changed the loop bounds, we must clear the
    1295              :              corresponding specloop, so that delta calculation is not skipped
    1296              :              later in gfc_set_delta.  */
    1297        38754 :           loop->specloop[n] = NULL;
    1298              : 
    1299              :           /* We are constructing the temporary's descriptor based on the loop
    1300              :              dimensions.  As the dimensions may be accessed in arbitrary order
    1301              :              (think of transpose) the size taken from the n'th loop may not map
    1302              :              to the n'th dimension of the array.  We need to reconstruct loop
    1303              :              infos in the right order before using it to set the descriptor
    1304              :              bounds.  */
    1305        38754 :           tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
    1306        38754 :           from[tmp_dim] = loop->from[n];
    1307        38754 :           to[tmp_dim] = loop->to[n];
    1308              : 
    1309        38754 :           info->delta[dim] = gfc_index_zero_node;
    1310        38754 :           info->start[dim] = gfc_index_zero_node;
    1311        38754 :           info->end[dim] = gfc_index_zero_node;
    1312        38754 :           info->stride[dim] = gfc_index_one_node;
    1313              :         }
    1314              :     }
    1315              : 
    1316              :   /* Initialize the descriptor.  */
    1317        29324 :   type =
    1318        29324 :     gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
    1319              :                                GFC_ARRAY_UNKNOWN, true);
    1320        29324 :   desc = gfc_create_var (type, "atmp");
    1321        29324 :   GFC_DECL_PACKED_ARRAY (desc) = 1;
    1322              : 
    1323              :   /* Emit a DECL_EXPR for the variable sized array type in
    1324              :      GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
    1325              :      sizes works correctly.  */
    1326        29324 :   tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
    1327        29324 :   if (! TYPE_NAME (arraytype))
    1328        29324 :     TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
    1329              :                                         NULL_TREE, arraytype);
    1330        29324 :   gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
    1331        29324 :                                       arraytype, TYPE_NAME (arraytype)));
    1332              : 
    1333        29324 :   if (fcn_ss && fcn_ss->info && fcn_ss->info->class_container)
    1334              :     {
    1335           90 :       suppress_warning (desc);
    1336           90 :       TREE_USED (desc) = 0;
    1337              :     }
    1338              : 
    1339        29324 :   if (class_expr != NULL_TREE
    1340        29137 :       || (fcn_ss && fcn_ss->info && fcn_ss->info->class_container))
    1341              :     {
    1342          217 :       tree class_data;
    1343          217 :       tree dtype;
    1344          217 :       gfc_expr *expr1 = fcn_ss ? fcn_ss->info->expr : NULL;
    1345          217 :       bool rank_changer;
    1346              : 
    1347              :       /* Pick out these transformational functions because they change the rank
    1348              :          or shape of the first argument. This requires that the class type be
    1349              :          changed, the dtype updated and the correct rank used.  */
    1350          121 :       rank_changer = expr1 && expr1->expr_type == EXPR_FUNCTION
    1351          121 :                      && expr1->value.function.isym
    1352          307 :                      && (expr1->value.function.isym->id == GFC_ISYM_RESHAPE
    1353              :                          || expr1->value.function.isym->id == GFC_ISYM_SPREAD
    1354              :                          || expr1->value.function.isym->id == GFC_ISYM_PACK
    1355              :                          || expr1->value.function.isym->id == GFC_ISYM_UNPACK);
    1356              : 
    1357              :       /* Create a class temporary for the result using the lhs class object.  */
    1358          217 :       if (class_expr != NULL_TREE && !rank_changer)
    1359              :         {
    1360          139 :           tmp = gfc_create_var (TREE_TYPE (class_expr), "ctmp");
    1361          139 :           gfc_add_modify (pre, tmp, class_expr);
    1362              :         }
    1363              :       else
    1364              :         {
    1365           78 :           tree vptr;
    1366           78 :           class_expr = fcn_ss->info->class_container;
    1367           78 :           gcc_assert (expr1);
    1368              : 
    1369              :           /* Build a new class container using the arg1 class object. The class
    1370              :              typespec must be rebuilt because the rank might have changed.  */
    1371           78 :           gfc_typespec ts = CLASS_DATA (expr1)->ts;
    1372           78 :           symbol_attribute attr = CLASS_DATA (expr1)->attr;
    1373           78 :           gfc_change_class (&ts, &attr, NULL, expr1->rank, 0);
    1374           78 :           tmp = gfc_create_var (gfc_typenode_for_spec (&ts), "ctmp");
    1375           78 :           fcn_ss->info->class_container = tmp;
    1376              : 
    1377              :           /* Set the vptr and obtain the element size.  */
    1378           78 :           vptr = gfc_class_vptr_get (tmp);
    1379          156 :           gfc_add_modify (pre, vptr,
    1380           78 :                           fold_convert (TREE_TYPE (vptr),
    1381              :                                         gfc_class_vptr_get (class_expr)));
    1382           78 :           elemsize = gfc_class_vtab_size_get (class_expr);
    1383              : 
    1384              :           /* Set the _len field, if necessary.  */
    1385           78 :           if (UNLIMITED_POLY (expr1))
    1386              :             {
    1387           18 :               gfc_add_modify (pre, gfc_class_len_get (tmp),
    1388              :                               gfc_class_len_get (class_expr));
    1389           18 :               elemsize = gfc_resize_class_size_with_len (pre, class_expr,
    1390              :                                                          elemsize);
    1391              :             }
    1392              : 
    1393           78 :           elemsize = gfc_evaluate_now (elemsize, pre);
    1394              :         }
    1395              : 
    1396          217 :       class_data = gfc_class_data_get (tmp);
    1397              : 
    1398          217 :       if (rank_changer)
    1399              :         {
    1400              :           /* Take the dtype from the class expression.  */
    1401           72 :           tree class_descr = gfc_class_data_get (class_expr);
    1402           72 :           dtype = gfc_conv_descriptor_dtype_get (class_descr);
    1403           72 :           gfc_conv_descriptor_dtype_set (pre, desc, dtype);
    1404              : 
    1405              :           /* These transformational functions change the rank.  */
    1406           72 :           gfc_conv_descriptor_rank_set (pre, desc, ss->loop->dimen);
    1407           72 :           fcn_ss->info->class_container = NULL_TREE;
    1408              :         }
    1409              : 
    1410              :       /* Assign the new descriptor to the _data field. This allows the
    1411              :          vptr _copy to be used for scalarized assignment since the class
    1412              :          temporary can be found from the descriptor.  */
    1413          217 :       tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1414          217 :                              TREE_TYPE (desc), desc);
    1415          217 :       gfc_add_modify (pre, class_data, tmp);
    1416              : 
    1417              :       /* Point desc to the class _data field.  */
    1418          217 :       desc = class_data;
    1419          217 :     }
    1420              :   else
    1421              :     {
    1422              :       /* Fill in the array dtype.  */
    1423        29107 :       gfc_conv_descriptor_dtype_set (pre, desc,
    1424        29107 :                                      gfc_get_dtype (TREE_TYPE (desc)));
    1425              :     }
    1426              : 
    1427        29324 :   info->descriptor = desc;
    1428        29324 :   size = gfc_index_one_node;
    1429              : 
    1430              :   /*
    1431              :      Fill in the bounds and stride.  This is a packed array, so:
    1432              : 
    1433              :      size = 1;
    1434              :      for (n = 0; n < rank; n++)
    1435              :        {
    1436              :          stride[n] = size
    1437              :          delta = ubound[n] + 1 - lbound[n];
    1438              :          size = size * delta;
    1439              :        }
    1440              :      size = size * sizeof(element);
    1441              :   */
    1442              : 
    1443        29324 :   or_expr = NULL_TREE;
    1444              : 
    1445              :   /* If there is at least one null loop->to[n], it is a callee allocated
    1446              :      array.  */
    1447        64479 :   for (n = 0; n < total_dim; n++)
    1448        37370 :     if (to[n] == NULL_TREE)
    1449              :       {
    1450              :         size = NULL_TREE;
    1451              :         break;
    1452              :       }
    1453              : 
    1454        29324 :   if (size == NULL_TREE)
    1455         4440 :     for (s = ss; s; s = s->parent)
    1456         5829 :       for (n = 0; n < s->loop->dimen; n++)
    1457              :         {
    1458         3604 :           dim = get_scalarizer_dim_for_array_dim (ss, s->dim[n]);
    1459              : 
    1460              :           /* For a callee allocated array express the loop bounds in terms
    1461              :              of the descriptor fields.  */
    1462         3604 :           tmp = fold_build2_loc (input_location,
    1463              :                 MINUS_EXPR, gfc_array_index_type,
    1464              :                 gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]),
    1465              :                 gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]));
    1466         3604 :           s->loop->to[n] = tmp;
    1467              :         }
    1468              :   else
    1469              :     {
    1470        62259 :       for (n = 0; n < total_dim; n++)
    1471              :         {
    1472              :           /* Store the stride and bound components in the descriptor.  */
    1473        35150 :           gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
    1474              : 
    1475        35150 :           gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
    1476              :                                           gfc_index_zero_node);
    1477              : 
    1478        35150 :           gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
    1479              : 
    1480        35150 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    1481              :                                  gfc_array_index_type,
    1482              :                                  to[n], gfc_index_one_node);
    1483              : 
    1484              :           /* Check whether the size for this dimension is negative.  */
    1485        35150 :           cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    1486              :                                   tmp, gfc_index_zero_node);
    1487        35150 :           cond = gfc_evaluate_now (cond, pre);
    1488              : 
    1489        35150 :           if (n == 0)
    1490              :             or_expr = cond;
    1491              :           else
    1492         8041 :             or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    1493              :                                        logical_type_node, or_expr, cond);
    1494              : 
    1495        35150 :           size = fold_build2_loc (input_location, MULT_EXPR,
    1496              :                                   gfc_array_index_type, size, tmp);
    1497        35150 :           size = gfc_evaluate_now (size, pre);
    1498              :         }
    1499              :     }
    1500              : 
    1501              :   /* Get the size of the array.  */
    1502        29324 :   if (size && !callee_alloc)
    1503              :     {
    1504              :       /* If or_expr is true, then the extent in at least one
    1505              :          dimension is zero and the size is set to zero.  */
    1506        26883 :       size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    1507              :                               or_expr, gfc_index_zero_node, size);
    1508              : 
    1509        26883 :       nelem = size;
    1510        26883 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    1511              :                               size, elemsize);
    1512              :     }
    1513              :   else
    1514              :     {
    1515              :       nelem = size;
    1516              :       size = NULL_TREE;
    1517              :     }
    1518              : 
    1519              :   /* Set the span.  */
    1520        29324 :   tmp = fold_convert (gfc_array_index_type, elemsize);
    1521        29324 :   gfc_conv_descriptor_span_set (pre, desc, tmp);
    1522              : 
    1523        29324 :   gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
    1524              :                                     dynamic, dealloc);
    1525              : 
    1526        58683 :   while (ss->parent)
    1527              :     ss = ss->parent;
    1528              : 
    1529        29324 :   if (ss->dimen > ss->loop->temp_dim)
    1530        25344 :     ss->loop->temp_dim = ss->dimen;
    1531              : 
    1532        29324 :   return size;
    1533              : }
    1534              : 
    1535              : 
    1536              : /* Return the number of iterations in a loop that starts at START,
    1537              :    ends at END, and has step STEP.  */
    1538              : 
    1539              : static tree
    1540         1080 : gfc_get_iteration_count (tree start, tree end, tree step)
    1541              : {
    1542         1080 :   tree tmp;
    1543         1080 :   tree type;
    1544              : 
    1545         1080 :   type = TREE_TYPE (step);
    1546         1080 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
    1547         1080 :   tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
    1548         1080 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
    1549              :                          build_int_cst (type, 1));
    1550         1080 :   tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
    1551              :                          build_int_cst (type, 0));
    1552         1080 :   return fold_convert (gfc_array_index_type, tmp);
    1553              : }
    1554              : 
    1555              : 
    1556              : /* Return true if the bounds of iterator I can only be determined
    1557              :    at run time.  */
    1558              : 
    1559              : static inline bool
    1560         2429 : gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
    1561              : {
    1562         2429 :   return (i->start->expr_type != EXPR_CONSTANT
    1563         2011 :           || i->end->expr_type != EXPR_CONSTANT
    1564         2602 :           || i->step->expr_type != EXPR_CONSTANT);
    1565              : }
    1566              : 
    1567              : 
    1568              : /* Split the size of constructor element EXPR into the sum of two terms,
    1569              :    one of which can be determined at compile time and one of which must
    1570              :    be calculated at run time.  Set *SIZE to the former and return true
    1571              :    if the latter might be nonzero.  */
    1572              : 
    1573              : static bool
    1574         3270 : gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
    1575              : {
    1576         3270 :   if (expr->expr_type == EXPR_ARRAY)
    1577          685 :     return gfc_get_array_constructor_size (size, expr->value.constructor);
    1578         2585 :   else if (expr->rank > 0)
    1579              :     {
    1580              :       /* Calculate everything at run time.  */
    1581         1035 :       mpz_set_ui (*size, 0);
    1582         1035 :       return true;
    1583              :     }
    1584              :   else
    1585              :     {
    1586              :       /* A single element.  */
    1587         1550 :       mpz_set_ui (*size, 1);
    1588         1550 :       return false;
    1589              :     }
    1590              : }
    1591              : 
    1592              : 
    1593              : /* Like gfc_get_array_constructor_element_size, but applied to the whole
    1594              :    of array constructor C.  */
    1595              : 
    1596              : static bool
    1597         3092 : gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
    1598              : {
    1599         3092 :   gfc_constructor *c;
    1600         3092 :   gfc_iterator *i;
    1601         3092 :   mpz_t val;
    1602         3092 :   mpz_t len;
    1603         3092 :   bool dynamic;
    1604              : 
    1605         3092 :   mpz_set_ui (*size, 0);
    1606         3092 :   mpz_init (len);
    1607         3092 :   mpz_init (val);
    1608              : 
    1609         3092 :   dynamic = false;
    1610         7516 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1611              :     {
    1612         4424 :       i = c->iterator;
    1613         4424 :       if (i && gfc_iterator_has_dynamic_bounds (i))
    1614              :         dynamic = true;
    1615              :       else
    1616              :         {
    1617         2719 :           dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
    1618         2719 :           if (i)
    1619              :             {
    1620              :               /* Multiply the static part of the element size by the
    1621              :                  number of iterations.  */
    1622          128 :               mpz_sub (val, i->end->value.integer, i->start->value.integer);
    1623          128 :               mpz_fdiv_q (val, val, i->step->value.integer);
    1624          128 :               mpz_add_ui (val, val, 1);
    1625          128 :               if (mpz_sgn (val) > 0)
    1626           92 :                 mpz_mul (len, len, val);
    1627              :               else
    1628           36 :                 mpz_set_ui (len, 0);
    1629              :             }
    1630         2719 :           mpz_add (*size, *size, len);
    1631              :         }
    1632              :     }
    1633         3092 :   mpz_clear (len);
    1634         3092 :   mpz_clear (val);
    1635         3092 :   return dynamic;
    1636              : }
    1637              : 
    1638              : 
    1639              : /* Make sure offset is a variable.  */
    1640              : 
    1641              : static void
    1642         3422 : gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
    1643              :                          tree * offsetvar)
    1644              : {
    1645              :   /* We should have already created the offset variable.  We cannot
    1646              :      create it here because we may be in an inner scope.  */
    1647         3422 :   gcc_assert (*offsetvar != NULL_TREE);
    1648         3422 :   gfc_add_modify (pblock, *offsetvar, *poffset);
    1649         3422 :   *poffset = *offsetvar;
    1650         3422 :   TREE_USED (*offsetvar) = 1;
    1651         3422 : }
    1652              : 
    1653              : 
    1654              : /* Variables needed for bounds-checking.  */
    1655              : static bool first_len;
    1656              : static tree first_len_val;
    1657              : static bool typespec_chararray_ctor;
    1658              : 
    1659              : /* Return true if DER has any CLASS allocatable component.  Such components
    1660              :    are initialised by VIEW_CONVERT in structure constructors (a bitwise copy
    1661              :    of the class descriptor), so their _data pointer may refer to a non-heap
    1662              :    object and must not be passed to gfc_deallocate_alloc_comp_no_caf.  */
    1663              : 
    1664              : static bool
    1665         4923 : has_class_alloc_comp (gfc_symbol *der)
    1666              : {
    1667        13267 :   for (gfc_component *c = der->components; c; c = c->next)
    1668         8410 :     if (c->ts.type == BT_CLASS && !c->attr.class_pointer)
    1669              :       return true;
    1670              :   return false;
    1671              : }
    1672              : 
    1673              : static void
    1674        13826 : gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
    1675              :                               tree offset, gfc_se * se, gfc_expr * expr)
    1676              : {
    1677        13826 :   tree tmp, offset_eval;
    1678              : 
    1679        13826 :   gfc_conv_expr (se, expr);
    1680              : 
    1681              :   /* Store the value.  */
    1682        13826 :   tmp = build_fold_indirect_ref_loc (input_location,
    1683              :                                  gfc_conv_descriptor_data_get (desc));
    1684              : 
    1685              :   /* The offset may change, so get its value now and use that to free memory.  */
    1686        13826 :   offset_eval = gfc_evaluate_now (offset, &se->pre);
    1687        13826 :   tmp = gfc_build_array_ref (tmp, offset_eval, NULL);
    1688              : 
    1689        13826 :   if (expr->ts.type == BT_DERIVED
    1690         4846 :       && (expr->expr_type == EXPR_FUNCTION
    1691         4762 :           || (expr->expr_type == EXPR_STRUCTURE
    1692         4144 :               && !has_class_alloc_comp (expr->ts.u.derived)))
    1693        18012 :       && expr->ts.u.derived->attr.alloc_comp)
    1694          800 :     gfc_add_expr_to_block (&se->finalblock,
    1695              :                            gfc_deallocate_alloc_comp_no_caf (expr->ts.u.derived,
    1696              :                                                              tmp, expr->rank,
    1697              :                                                              true));
    1698              : 
    1699        13826 :   if (expr->ts.type == BT_CHARACTER)
    1700              :     {
    1701         2730 :       int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
    1702         2730 :       tree esize;
    1703              : 
    1704         2730 :       esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
    1705         2730 :       esize = fold_convert (gfc_charlen_type_node, esize);
    1706         5460 :       esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    1707         2730 :                                TREE_TYPE (esize), esize,
    1708         2730 :                                build_int_cst (TREE_TYPE (esize),
    1709         2730 :                                           gfc_character_kinds[i].bit_size / 8));
    1710              : 
    1711         2730 :       gfc_conv_string_parameter (se);
    1712         2730 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    1713              :         {
    1714              :           /* The temporary is an array of pointers.  */
    1715            6 :           se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1716            6 :           gfc_add_modify (&se->pre, tmp, se->expr);
    1717              :         }
    1718              :       else
    1719              :         {
    1720              :           /* The temporary is an array of string values.  */
    1721         2724 :           tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
    1722              :           /* We know the temporary and the value will be the same length,
    1723              :              so can use memcpy.  */
    1724         2724 :           gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
    1725              :                                  se->string_length, se->expr, expr->ts.kind);
    1726              :         }
    1727         2730 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
    1728              :         {
    1729          310 :           if (first_len)
    1730              :             {
    1731          130 :               gfc_add_modify (&se->pre, first_len_val,
    1732          130 :                               fold_convert (TREE_TYPE (first_len_val),
    1733              :                                             se->string_length));
    1734          130 :               first_len = false;
    1735              :             }
    1736              :           else
    1737              :             {
    1738              :               /* Verify that all constructor elements are of the same
    1739              :                  length.  */
    1740          180 :               tree rhs = fold_convert (TREE_TYPE (first_len_val),
    1741              :                                        se->string_length);
    1742          180 :               tree cond = fold_build2_loc (input_location, NE_EXPR,
    1743              :                                            logical_type_node, first_len_val,
    1744              :                                            rhs);
    1745          180 :               gfc_trans_runtime_check
    1746          180 :                 (true, false, cond, &se->pre, &expr->where,
    1747              :                  "Different CHARACTER lengths (%ld/%ld) in array constructor",
    1748              :                  fold_convert (long_integer_type_node, first_len_val),
    1749              :                  fold_convert (long_integer_type_node, se->string_length));
    1750              :             }
    1751              :         }
    1752              :     }
    1753        11096 :   else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
    1754        11096 :            && !GFC_CLASS_TYPE_P (gfc_get_element_type (TREE_TYPE (desc))))
    1755              :     {
    1756              :       /* Assignment of a CLASS array constructor to a derived type array.  */
    1757           28 :       if (expr->expr_type == EXPR_FUNCTION)
    1758           18 :         se->expr = gfc_evaluate_now (se->expr, pblock);
    1759           28 :       se->expr = gfc_class_data_get (se->expr);
    1760           28 :       se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    1761           28 :       se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1762           28 :       gfc_add_modify (&se->pre, tmp, se->expr);
    1763              :     }
    1764              :   else
    1765              :     {
    1766              :       /* TODO: Should the frontend already have done this conversion?  */
    1767        11068 :       se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1768        11068 :       gfc_add_modify (&se->pre, tmp, se->expr);
    1769              :     }
    1770              : 
    1771        13826 :   gfc_add_block_to_block (pblock, &se->pre);
    1772        13826 :   gfc_add_block_to_block (pblock, &se->post);
    1773        13826 : }
    1774              : 
    1775              : 
    1776              : /* Add the contents of an array to the constructor.  DYNAMIC is as for
    1777              :    gfc_trans_array_constructor_value.  */
    1778              : 
    1779              : static void
    1780         1155 : gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
    1781              :                                       tree type ATTRIBUTE_UNUSED,
    1782              :                                       tree desc, gfc_expr * expr,
    1783              :                                       tree * poffset, tree * offsetvar,
    1784              :                                       bool dynamic)
    1785              : {
    1786         1155 :   gfc_se se;
    1787         1155 :   gfc_ss *ss;
    1788         1155 :   gfc_loopinfo loop;
    1789         1155 :   stmtblock_t body;
    1790         1155 :   tree tmp;
    1791         1155 :   tree size;
    1792         1155 :   int n;
    1793              : 
    1794              :   /* We need this to be a variable so we can increment it.  */
    1795         1155 :   gfc_put_offset_into_var (pblock, poffset, offsetvar);
    1796              : 
    1797         1155 :   gfc_init_se (&se, NULL);
    1798              : 
    1799              :   /* Walk the array expression.  */
    1800         1155 :   ss = gfc_walk_expr (expr);
    1801         1155 :   gcc_assert (ss != gfc_ss_terminator);
    1802              : 
    1803              :   /* Initialize the scalarizer.  */
    1804         1155 :   gfc_init_loopinfo (&loop);
    1805         1155 :   gfc_add_ss_to_loop (&loop, ss);
    1806              : 
    1807              :   /* Initialize the loop.  */
    1808         1155 :   gfc_conv_ss_startstride (&loop);
    1809         1155 :   gfc_conv_loop_setup (&loop, &expr->where);
    1810              : 
    1811              :   /* Make sure the constructed array has room for the new data.  */
    1812         1155 :   if (dynamic)
    1813              :     {
    1814              :       /* Set SIZE to the total number of elements in the subarray.  */
    1815          517 :       size = gfc_index_one_node;
    1816         1046 :       for (n = 0; n < loop.dimen; n++)
    1817              :         {
    1818          529 :           tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
    1819              :                                          gfc_index_one_node);
    1820          529 :           size = fold_build2_loc (input_location, MULT_EXPR,
    1821              :                                   gfc_array_index_type, size, tmp);
    1822              :         }
    1823              : 
    1824              :       /* Grow the constructed array by SIZE elements.  */
    1825          517 :       gfc_grow_array (&loop.pre, desc, size);
    1826              :     }
    1827              : 
    1828              :   /* Make the loop body.  */
    1829         1155 :   gfc_mark_ss_chain_used (ss, 1);
    1830         1155 :   gfc_start_scalarized_body (&loop, &body);
    1831         1155 :   gfc_copy_loopinfo_to_se (&se, &loop);
    1832         1155 :   se.ss = ss;
    1833              : 
    1834         1155 :   gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
    1835         1155 :   gcc_assert (se.ss == gfc_ss_terminator);
    1836              : 
    1837              :   /* Increment the offset.  */
    1838         1155 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    1839              :                          *poffset, gfc_index_one_node);
    1840         1155 :   gfc_add_modify (&body, *poffset, tmp);
    1841              : 
    1842              :   /* Finish the loop.  */
    1843         1155 :   gfc_trans_scalarizing_loops (&loop, &body);
    1844         1155 :   gfc_add_block_to_block (&loop.pre, &loop.post);
    1845         1155 :   tmp = gfc_finish_block (&loop.pre);
    1846         1155 :   gfc_add_expr_to_block (pblock, tmp);
    1847              : 
    1848         1155 :   gfc_cleanup_loop (&loop);
    1849         1155 : }
    1850              : 
    1851              : 
    1852              : /* Return true if every leaf element of an array constructor is a function
    1853              :    reference returning derived type DER, which has allocatable components.
    1854              :    Such results are moved (shallow-copied) into the constructor temporary, so
    1855              :    the temporary owns their allocatable components and they can all be freed
    1856              :    in a single sweep over the whole temporary.  Returns false as soon as an
    1857              :    element is anything else - notably a variable, whose allocatable components
    1858              :    are aliased rather than owned by the temporary and must not be freed.  */
    1859              : 
    1860              : static bool
    1861          533 : gfc_constructor_is_owned_alloc_comp (gfc_constructor_base base,
    1862              :                                      gfc_symbol *der)
    1863              : {
    1864          533 :   gfc_constructor *c;
    1865              : 
    1866          533 :   if (base == NULL)
    1867              :     return false;
    1868              : 
    1869         1381 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1870              :     {
    1871         1077 :       gfc_expr *e = c->expr;
    1872         1077 :       if (e->expr_type == EXPR_ARRAY)
    1873              :         {
    1874           54 :           if (!gfc_constructor_is_owned_alloc_comp (e->value.constructor, der))
    1875              :             return false;
    1876              :         }
    1877         1023 :       else if (!(e->ts.type == BT_DERIVED
    1878         1023 :                  && (e->expr_type == EXPR_FUNCTION
    1879          984 :                      || (e->expr_type == EXPR_STRUCTURE
    1880          779 :                          && !has_class_alloc_comp (e->ts.u.derived)))
    1881          794 :                  && e->ts.u.derived == der))
    1882              :         return false;
    1883              :     }
    1884              :   return true;
    1885              : }
    1886              : 
    1887              : 
    1888              : /* Assign the values to the elements of an array constructor.  DYNAMIC
    1889              :    is true if descriptor DESC only contains enough data for the static
    1890              :    size calculated by gfc_get_array_constructor_size.  When true, memory
    1891              :    for the dynamic parts must be allocated using realloc.  OWNED_SWEEP is
    1892              :    true when the caller will free the allocatable components of every
    1893              :    constructor element in one sweep over the whole temporary; in that case
    1894              :    the per-element finalization built here is suppressed to avoid a double
    1895              :    free.  */
    1896              : 
    1897              : static void
    1898         8763 : gfc_trans_array_constructor_value (stmtblock_t * pblock,
    1899              :                                    stmtblock_t * finalblock,
    1900              :                                    tree type, tree desc,
    1901              :                                    gfc_constructor_base base, tree * poffset,
    1902              :                                    tree * offsetvar, bool dynamic,
    1903              :                                    bool owned_sweep)
    1904              : {
    1905         8763 :   tree tmp;
    1906         8763 :   tree start = NULL_TREE;
    1907         8763 :   tree end = NULL_TREE;
    1908         8763 :   tree step = NULL_TREE;
    1909         8763 :   stmtblock_t body;
    1910         8763 :   gfc_se se;
    1911         8763 :   mpz_t size;
    1912         8763 :   gfc_constructor *c;
    1913         8763 :   gfc_typespec ts;
    1914         8763 :   int ctr = 0;
    1915              : 
    1916         8763 :   tree shadow_loopvar = NULL_TREE;
    1917         8763 :   gfc_saved_var saved_loopvar;
    1918              : 
    1919         8763 :   ts.type = BT_UNKNOWN;
    1920         8763 :   mpz_init (size);
    1921        24271 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1922              :     {
    1923        15508 :       ctr++;
    1924              :       /* If this is an iterator or an array, the offset must be a variable.  */
    1925        15508 :       if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
    1926         2267 :         gfc_put_offset_into_var (pblock, poffset, offsetvar);
    1927              : 
    1928              :       /* Shadowing the iterator avoids changing its value and saves us from
    1929              :          keeping track of it. Further, it makes sure that there's always a
    1930              :          backend-decl for the symbol, even if there wasn't one before,
    1931              :          e.g. in the case of an iterator that appears in a specification
    1932              :          expression in an interface mapping.  */
    1933        15508 :       if (c->iterator)
    1934              :         {
    1935         1555 :           gfc_symbol *sym;
    1936         1555 :           tree type;
    1937              : 
    1938              :           /* Evaluate loop bounds before substituting the loop variable
    1939              :              in case they depend on it.  Such a case is invalid, but it is
    1940              :              not more expensive to do the right thing here.
    1941              :              See PR 44354.  */
    1942         1555 :           gfc_init_se (&se, NULL);
    1943         1555 :           gfc_conv_expr_val (&se, c->iterator->start);
    1944         1555 :           gfc_add_block_to_block (pblock, &se.pre);
    1945         1555 :           start = gfc_evaluate_now (se.expr, pblock);
    1946              : 
    1947         1555 :           gfc_init_se (&se, NULL);
    1948         1555 :           gfc_conv_expr_val (&se, c->iterator->end);
    1949         1555 :           gfc_add_block_to_block (pblock, &se.pre);
    1950         1555 :           end = gfc_evaluate_now (se.expr, pblock);
    1951              : 
    1952         1555 :           gfc_init_se (&se, NULL);
    1953         1555 :           gfc_conv_expr_val (&se, c->iterator->step);
    1954         1555 :           gfc_add_block_to_block (pblock, &se.pre);
    1955         1555 :           step = gfc_evaluate_now (se.expr, pblock);
    1956              : 
    1957         1555 :           sym = c->iterator->var->symtree->n.sym;
    1958         1555 :           type = gfc_typenode_for_spec (&sym->ts);
    1959              : 
    1960         1555 :           shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
    1961         1555 :           gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
    1962              :         }
    1963              : 
    1964        15508 :       gfc_start_block (&body);
    1965              : 
    1966        15508 :       if (c->expr->expr_type == EXPR_ARRAY)
    1967              :         {
    1968              :           /* Array constructors can be nested.  */
    1969         1577 :           gfc_trans_array_constructor_value (&body, finalblock, type,
    1970              :                                              desc, c->expr->value.constructor,
    1971              :                                              poffset, offsetvar, dynamic,
    1972              :                                              owned_sweep);
    1973              :         }
    1974        13931 :       else if (c->expr->rank > 0)
    1975              :         {
    1976         1155 :           gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
    1977              :                                                 poffset, offsetvar, dynamic);
    1978              :         }
    1979              :       else
    1980              :         {
    1981              :           /* This code really upsets the gimplifier so don't bother for now.  */
    1982              :           gfc_constructor *p;
    1983              :           HOST_WIDE_INT n;
    1984              :           HOST_WIDE_INT size;
    1985              : 
    1986              :           p = c;
    1987              :           n = 0;
    1988        14590 :           while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
    1989              :             {
    1990         1814 :               p = gfc_constructor_next (p);
    1991         1814 :               n++;
    1992              :             }
    1993              :           /* Constructor with few constant elements, or element size not
    1994              :              known at compile time (e.g. deferred-length character).  */
    1995        12776 :           if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type)))
    1996              :             {
    1997              :               /* Scalar values.  */
    1998        12671 :               gfc_init_se (&se, NULL);
    1999        12671 :               if (IS_PDT (c->expr) && c->expr->expr_type == EXPR_STRUCTURE)
    2000          288 :                 c->expr->must_finalize = 1;
    2001              : 
    2002        12671 :               gfc_trans_array_ctor_element (&body, desc, *poffset,
    2003              :                                             &se, c->expr);
    2004              : 
    2005        12671 :               *poffset = fold_build2_loc (input_location, PLUS_EXPR,
    2006              :                                           gfc_array_index_type,
    2007              :                                           *poffset, gfc_index_one_node);
    2008              :               /* Unless the whole temporary is being swept by the caller, add
    2009              :                  the per-element finalization.  The sweep is used when every
    2010              :                  element is an owned function result, which is the only way to
    2011              :                  correctly free elements produced inside an implied-do loop.  */
    2012        12671 :               if (finalblock && !owned_sweep)
    2013          508 :                 gfc_add_block_to_block (finalblock, &se.finalblock);
    2014              :             }
    2015              :           else
    2016              :             {
    2017              :               /* Collect multiple scalar constants into a constructor.  */
    2018          105 :               vec<constructor_elt, va_gc> *v = NULL;
    2019          105 :               tree init;
    2020          105 :               tree bound;
    2021          105 :               tree tmptype;
    2022          105 :               HOST_WIDE_INT idx = 0;
    2023              : 
    2024          105 :               p = c;
    2025              :               /* Count the number of consecutive scalar constants.  */
    2026          837 :               while (p && !(p->iterator
    2027          745 :                             || p->expr->expr_type != EXPR_CONSTANT))
    2028              :                 {
    2029          732 :                   gfc_init_se (&se, NULL);
    2030          732 :                   gfc_conv_constant (&se, p->expr);
    2031              : 
    2032          732 :                   if (c->expr->ts.type != BT_CHARACTER)
    2033          660 :                     se.expr = fold_convert (type, se.expr);
    2034              :                   /* For constant character array constructors we build
    2035              :                      an array of pointers.  */
    2036           72 :                   else if (POINTER_TYPE_P (type))
    2037            0 :                     se.expr = gfc_build_addr_expr
    2038            0 :                                 (gfc_get_pchar_type (p->expr->ts.kind),
    2039              :                                  se.expr);
    2040              : 
    2041          732 :                   CONSTRUCTOR_APPEND_ELT (v,
    2042              :                                           build_int_cst (gfc_array_index_type,
    2043              :                                                          idx++),
    2044              :                                           se.expr);
    2045          732 :                   c = p;
    2046          732 :                   p = gfc_constructor_next (p);
    2047              :                 }
    2048              : 
    2049          105 :               bound = size_int (n - 1);
    2050              :               /* Create an array type to hold them.  */
    2051          105 :               tmptype = build_range_type (gfc_array_index_type,
    2052              :                                           gfc_index_zero_node, bound);
    2053          105 :               tmptype = build_array_type (type, tmptype);
    2054              : 
    2055          105 :               init = build_constructor (tmptype, v);
    2056          105 :               TREE_CONSTANT (init) = 1;
    2057          105 :               TREE_STATIC (init) = 1;
    2058              :               /* Create a static variable to hold the data.  */
    2059          105 :               tmp = gfc_create_var (tmptype, "data");
    2060          105 :               TREE_STATIC (tmp) = 1;
    2061          105 :               TREE_CONSTANT (tmp) = 1;
    2062          105 :               TREE_READONLY (tmp) = 1;
    2063          105 :               DECL_INITIAL (tmp) = init;
    2064          105 :               init = tmp;
    2065              : 
    2066              :               /* Use BUILTIN_MEMCPY to assign the values.  */
    2067          105 :               tmp = gfc_conv_descriptor_data_get (desc);
    2068          105 :               tmp = build_fold_indirect_ref_loc (input_location,
    2069              :                                              tmp);
    2070          105 :               tmp = gfc_build_array_ref (tmp, *poffset, NULL);
    2071          105 :               tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    2072          105 :               init = gfc_build_addr_expr (NULL_TREE, init);
    2073              : 
    2074          105 :               size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
    2075          105 :               bound = build_int_cst (size_type_node, n * size);
    2076          105 :               tmp = build_call_expr_loc (input_location,
    2077              :                                          builtin_decl_explicit (BUILT_IN_MEMCPY),
    2078              :                                          3, tmp, init, bound);
    2079          105 :               gfc_add_expr_to_block (&body, tmp);
    2080              : 
    2081          105 :               *poffset = fold_build2_loc (input_location, PLUS_EXPR,
    2082              :                                       gfc_array_index_type, *poffset,
    2083          105 :                                       build_int_cst (gfc_array_index_type, n));
    2084              :             }
    2085        12776 :           if (!INTEGER_CST_P (*poffset))
    2086              :             {
    2087         1857 :               gfc_add_modify (&body, *offsetvar, *poffset);
    2088         1857 :               *poffset = *offsetvar;
    2089              :             }
    2090              : 
    2091        12776 :           if (!c->iterator)
    2092        12776 :             ts = c->expr->ts;
    2093              :         }
    2094              : 
    2095              :       /* The frontend should already have done any expansions
    2096              :          at compile-time.  */
    2097        15508 :       if (!c->iterator)
    2098              :         {
    2099              :           /* Pass the code as is.  */
    2100        13953 :           tmp = gfc_finish_block (&body);
    2101        13953 :           gfc_add_expr_to_block (pblock, tmp);
    2102              :         }
    2103              :       else
    2104              :         {
    2105              :           /* Build the implied do-loop.  */
    2106         1555 :           stmtblock_t implied_do_block;
    2107         1555 :           tree cond;
    2108         1555 :           tree exit_label;
    2109         1555 :           tree loopbody;
    2110         1555 :           tree tmp2;
    2111              : 
    2112         1555 :           loopbody = gfc_finish_block (&body);
    2113              : 
    2114              :           /* Create a new block that holds the implied-do loop. A temporary
    2115              :              loop-variable is used.  */
    2116         1555 :           gfc_start_block(&implied_do_block);
    2117              : 
    2118              :           /* Initialize the loop.  */
    2119         1555 :           gfc_add_modify (&implied_do_block, shadow_loopvar, start);
    2120              : 
    2121              :           /* If this array expands dynamically, and the number of iterations
    2122              :              is not constant, we won't have allocated space for the static
    2123              :              part of C->EXPR's size.  Do that now.  */
    2124         1555 :           if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
    2125              :             {
    2126              :               /* Get the number of iterations.  */
    2127          551 :               tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
    2128              : 
    2129              :               /* Get the static part of C->EXPR's size.  */
    2130          551 :               gfc_get_array_constructor_element_size (&size, c->expr);
    2131          551 :               tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
    2132              : 
    2133              :               /* Grow the array by TMP * TMP2 elements.  */
    2134          551 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    2135              :                                      gfc_array_index_type, tmp, tmp2);
    2136          551 :               gfc_grow_array (&implied_do_block, desc, tmp);
    2137              :             }
    2138              : 
    2139              :           /* Generate the loop body.  */
    2140         1555 :           exit_label = gfc_build_label_decl (NULL_TREE);
    2141         1555 :           gfc_start_block (&body);
    2142              : 
    2143              :           /* Generate the exit condition.  Depending on the sign of
    2144              :              the step variable we have to generate the correct
    2145              :              comparison.  */
    2146         1555 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2147         1555 :                                  step, build_int_cst (TREE_TYPE (step), 0));
    2148         1555 :           cond = fold_build3_loc (input_location, COND_EXPR,
    2149              :                       logical_type_node, tmp,
    2150              :                       fold_build2_loc (input_location, GT_EXPR,
    2151              :                                        logical_type_node, shadow_loopvar, end),
    2152              :                       fold_build2_loc (input_location, LT_EXPR,
    2153              :                                        logical_type_node, shadow_loopvar, end));
    2154         1555 :           tmp = build1_v (GOTO_EXPR, exit_label);
    2155         1555 :           TREE_USED (exit_label) = 1;
    2156         1555 :           tmp = build3_v (COND_EXPR, cond, tmp,
    2157              :                           build_empty_stmt (input_location));
    2158         1555 :           gfc_add_expr_to_block (&body, tmp);
    2159              : 
    2160              :           /* The main loop body.  */
    2161         1555 :           gfc_add_expr_to_block (&body, loopbody);
    2162              : 
    2163              :           /* Increase loop variable by step.  */
    2164         1555 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2165         1555 :                                  TREE_TYPE (shadow_loopvar), shadow_loopvar,
    2166              :                                  step);
    2167         1555 :           gfc_add_modify (&body, shadow_loopvar, tmp);
    2168              : 
    2169              :           /* Finish the loop.  */
    2170         1555 :           tmp = gfc_finish_block (&body);
    2171         1555 :           tmp = build1_v (LOOP_EXPR, tmp);
    2172         1555 :           gfc_add_expr_to_block (&implied_do_block, tmp);
    2173              : 
    2174              :           /* Add the exit label.  */
    2175         1555 :           tmp = build1_v (LABEL_EXPR, exit_label);
    2176         1555 :           gfc_add_expr_to_block (&implied_do_block, tmp);
    2177              : 
    2178              :           /* Finish the implied-do loop.  */
    2179         1555 :           tmp = gfc_finish_block(&implied_do_block);
    2180         1555 :           gfc_add_expr_to_block(pblock, tmp);
    2181              : 
    2182         1555 :           gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
    2183              :         }
    2184              :     }
    2185              : 
    2186              :   /* F2008 4.5.6.3 para 5: If an executable construct references a structure
    2187              :      constructor or array constructor, the entity created by the constructor is
    2188              :      finalized after execution of the innermost executable construct containing
    2189              :      the reference. This, in fact, was later deleted by the Combined Technical
    2190              :      Corrigenda 1 TO 4 for fortran 2008 (f08/0011).
    2191              : 
    2192              :      Transmit finalization of this constructor through 'finalblock'. */
    2193         8763 :   if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
    2194         8763 :       && !(gfc_option.allow_std & GFC_STD_GNU)
    2195           70 :       && finalblock != NULL
    2196           24 :       && gfc_may_be_finalized (ts)
    2197           18 :       && ctr > 0 && desc != NULL_TREE
    2198         8781 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    2199              :     {
    2200           18 :       symbol_attribute attr;
    2201           18 :       gfc_se fse;
    2202           18 :       locus loc;
    2203           18 :       gfc_locus_from_location (&loc, input_location);
    2204           18 :       gfc_warning (0, "The structure constructor at %L has been"
    2205              :                          " finalized. This feature was removed by f08/0011."
    2206              :                          " Use -std=f2018 or -std=gnu to eliminate the"
    2207              :                          " finalization.", &loc);
    2208           18 :       attr.pointer = attr.allocatable = 0;
    2209           18 :       gfc_init_se (&fse, NULL);
    2210           18 :       fse.expr = desc;
    2211           18 :       gfc_finalize_tree_expr (&fse, ts.u.derived, attr, 1);
    2212           18 :       gfc_add_block_to_block (finalblock, &fse.pre);
    2213           18 :       gfc_add_block_to_block (finalblock, &fse.finalblock);
    2214           18 :       gfc_add_block_to_block (finalblock, &fse.post);
    2215              :     }
    2216              : 
    2217         8763 :   mpz_clear (size);
    2218         8763 : }
    2219              : 
    2220              : 
    2221              : /* The array constructor code can create a string length with an operand
    2222              :    in the form of a temporary variable.  This variable will retain its
    2223              :    context (current_function_decl).  If we store this length tree in a
    2224              :    gfc_charlen structure which is shared by a variable in another
    2225              :    context, the resulting gfc_charlen structure with a variable in a
    2226              :    different context, we could trip the assertion in expand_expr_real_1
    2227              :    when it sees that a variable has been created in one context and
    2228              :    referenced in another.
    2229              : 
    2230              :    If this might be the case, we create a new gfc_charlen structure and
    2231              :    link it into the current namespace.  */
    2232              : 
    2233              : static void
    2234         8622 : store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
    2235              : {
    2236         8622 :   if (force_new_cl)
    2237              :     {
    2238         8594 :       gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
    2239         8594 :       *clp = new_cl;
    2240              :     }
    2241         8622 :   (*clp)->backend_decl = len;
    2242         8622 : }
    2243              : 
    2244              : /* A catch-all to obtain the string length for anything that is not
    2245              :    a substring of non-constant length, a constant, array or variable.  */
    2246              : 
    2247              : static void
    2248          293 : get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
    2249              : {
    2250          293 :   gfc_se se;
    2251              : 
    2252              :   /* Don't bother if we already know the length is a constant.  */
    2253          293 :   if (*len && INTEGER_CST_P (*len))
    2254           52 :     return;
    2255              : 
    2256          241 :   if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
    2257           35 :         && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    2258              :     {
    2259              :       /* This is easy.  */
    2260            1 :       gfc_conv_const_charlen (e->ts.u.cl);
    2261            1 :       *len = e->ts.u.cl->backend_decl;
    2262              :     }
    2263              :   else
    2264              :     {
    2265              :       /* Otherwise, be brutal even if inefficient.  */
    2266          240 :       gfc_init_se (&se, NULL);
    2267              : 
    2268              :       /* No function call, in case of side effects.  */
    2269          240 :       se.no_function_call = 1;
    2270          240 :       if (e->rank == 0)
    2271          121 :         gfc_conv_expr (&se, e);
    2272              :       else
    2273          119 :         gfc_conv_expr_descriptor (&se, e);
    2274              : 
    2275              :       /* Fix the value.  */
    2276          240 :       *len = gfc_evaluate_now (se.string_length, &se.pre);
    2277              : 
    2278          240 :       gfc_add_block_to_block (block, &se.pre);
    2279          240 :       gfc_add_block_to_block (block, &se.post);
    2280              : 
    2281          240 :       store_backend_decl (&e->ts.u.cl, *len, true);
    2282              :     }
    2283              : }
    2284              : 
    2285              : 
    2286              : /* Figure out the string length of a variable reference expression.
    2287              :    Used by get_array_ctor_strlen.  */
    2288              : 
    2289              : static void
    2290         1074 : get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
    2291              : {
    2292         1074 :   gfc_ref *ref;
    2293         1074 :   gfc_typespec *ts;
    2294         1074 :   mpz_t char_len;
    2295         1074 :   gfc_se se;
    2296              : 
    2297              :   /* Don't bother if we already know the length is a constant.  */
    2298         1074 :   if (*len && INTEGER_CST_P (*len))
    2299          743 :     return;
    2300              : 
    2301          491 :   ts = &expr->symtree->n.sym->ts;
    2302          722 :   for (ref = expr->ref; ref; ref = ref->next)
    2303              :     {
    2304          391 :       switch (ref->type)
    2305              :         {
    2306          186 :         case REF_ARRAY:
    2307              :           /* Array references don't change the string length.  */
    2308          186 :           if (ts->deferred)
    2309          112 :             get_array_ctor_all_strlen (block, expr, len);
    2310              :           break;
    2311              : 
    2312           45 :         case REF_COMPONENT:
    2313              :           /* Use the length of the component.  */
    2314           45 :           ts = &ref->u.c.component->ts;
    2315           45 :           break;
    2316              : 
    2317          160 :         case REF_SUBSTRING:
    2318          160 :           if (ref->u.ss.end == NULL
    2319          148 :               || ref->u.ss.start->expr_type != EXPR_CONSTANT
    2320          148 :               || ref->u.ss.end->expr_type != EXPR_CONSTANT)
    2321              :             {
    2322              :               /* Note that this might evaluate expr.  */
    2323           45 :               get_array_ctor_all_strlen (block, expr, len);
    2324           45 :               return;
    2325              :             }
    2326          115 :           mpz_init_set_ui (char_len, 1);
    2327          115 :           mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
    2328          115 :           mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
    2329          115 :           *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
    2330          115 :           mpz_clear (char_len);
    2331          115 :           return;
    2332              : 
    2333              :         case REF_INQUIRY:
    2334              :           break;
    2335              : 
    2336            0 :         default:
    2337            0 :          gcc_unreachable ();
    2338              :         }
    2339              :     }
    2340              : 
    2341              :   /* A last ditch attempt that is sometimes needed for deferred characters.  */
    2342          331 :   if (!ts->u.cl->backend_decl)
    2343              :     {
    2344            7 :       gfc_init_se (&se, NULL);
    2345            7 :       if (expr->rank)
    2346            0 :         gfc_conv_expr_descriptor (&se, expr);
    2347              :       else
    2348            7 :         gfc_conv_expr (&se, expr);
    2349            7 :       gcc_assert (se.string_length != NULL_TREE);
    2350            7 :       gfc_add_block_to_block (block, &se.pre);
    2351            7 :       ts->u.cl->backend_decl = se.string_length;
    2352              :     }
    2353              : 
    2354          331 :   *len = ts->u.cl->backend_decl;
    2355              : }
    2356              : 
    2357              : 
    2358              : /* Figure out the string length of a character array constructor.
    2359              :    If len is NULL, don't calculate the length; this happens for recursive calls
    2360              :    when a sub-array-constructor is an element but not at the first position,
    2361              :    so when we're not interested in the length.
    2362              :    Returns TRUE if all elements are character constants.  */
    2363              : 
    2364              : bool
    2365         9077 : get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
    2366              : {
    2367         9077 :   gfc_constructor *c;
    2368         9077 :   bool is_const;
    2369              : 
    2370         9077 :   is_const = true;
    2371              : 
    2372         9077 :   if (gfc_constructor_first (base) == NULL)
    2373              :     {
    2374          273 :       if (len)
    2375          273 :         *len = build_int_cstu (gfc_charlen_type_node, 0);
    2376              :       return is_const;
    2377              :     }
    2378              : 
    2379              :   /* Loop over all constructor elements to find out is_const, but in len we
    2380              :      want to store the length of the first, not the last, element.  We can
    2381              :      of course exit the loop as soon as is_const is found to be false.  */
    2382         8804 :   for (c = gfc_constructor_first (base);
    2383        47589 :        c && is_const; c = gfc_constructor_next (c))
    2384              :     {
    2385        38785 :       switch (c->expr->expr_type)
    2386              :         {
    2387        37472 :         case EXPR_CONSTANT:
    2388        37472 :           if (len && !(*len && INTEGER_CST_P (*len)))
    2389          386 :             *len = build_int_cstu (gfc_charlen_type_node,
    2390          386 :                                    c->expr->value.character.length);
    2391              :           break;
    2392              : 
    2393           43 :         case EXPR_ARRAY:
    2394           43 :           if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
    2395        38785 :             is_const = false;
    2396              :           break;
    2397              : 
    2398         1134 :         case EXPR_VARIABLE:
    2399         1134 :           is_const = false;
    2400         1134 :           if (len)
    2401         1074 :             get_array_ctor_var_strlen (block, c->expr, len);
    2402              :           break;
    2403              : 
    2404          136 :         default:
    2405          136 :           is_const = false;
    2406          136 :           if (len)
    2407          136 :             get_array_ctor_all_strlen (block, c->expr, len);
    2408              :           break;
    2409              :         }
    2410              : 
    2411              :       /* After the first iteration, we don't want the length modified.  */
    2412        38785 :       len = NULL;
    2413              :     }
    2414              : 
    2415              :   return is_const;
    2416              : }
    2417              : 
    2418              : /* Check whether the array constructor C consists entirely of constant
    2419              :    elements, and if so returns the number of those elements, otherwise
    2420              :    return zero.  Note, an empty or NULL array constructor returns zero.  */
    2421              : 
    2422              : unsigned HOST_WIDE_INT
    2423        62344 : gfc_constant_array_constructor_p (gfc_constructor_base base)
    2424              : {
    2425        62344 :   unsigned HOST_WIDE_INT nelem = 0;
    2426              : 
    2427        62344 :   gfc_constructor *c = gfc_constructor_first (base);
    2428       555187 :   while (c)
    2429              :     {
    2430       438087 :       if (c->iterator
    2431       436416 :           || c->expr->rank > 0
    2432       435594 :           || c->expr->expr_type != EXPR_CONSTANT)
    2433              :         return 0;
    2434       430499 :       c = gfc_constructor_next (c);
    2435       430499 :       nelem++;
    2436              :     }
    2437              :   return nelem;
    2438              : }
    2439              : 
    2440              : 
    2441              : /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
    2442              :    and the tree type of it's elements, TYPE, return a static constant
    2443              :    variable that is compile-time initialized.  */
    2444              : 
    2445              : tree
    2446        43792 : gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
    2447              : {
    2448        43792 :   tree tmptype, init, tmp;
    2449        43792 :   HOST_WIDE_INT nelem;
    2450        43792 :   gfc_constructor *c;
    2451        43792 :   gfc_array_spec as;
    2452        43792 :   gfc_se se;
    2453        43792 :   int i;
    2454        43792 :   vec<constructor_elt, va_gc> *v = NULL;
    2455              : 
    2456              :   /* First traverse the constructor list, converting the constants
    2457              :      to tree to build an initializer.  */
    2458        43792 :   nelem = 0;
    2459        43792 :   c = gfc_constructor_first (expr->value.constructor);
    2460       433428 :   while (c)
    2461              :     {
    2462       345844 :       gfc_init_se (&se, NULL);
    2463       345844 :       gfc_conv_constant (&se, c->expr);
    2464       345844 :       if (c->expr->ts.type != BT_CHARACTER)
    2465       309292 :         se.expr = fold_convert (type, se.expr);
    2466        36552 :       else if (POINTER_TYPE_P (type))
    2467        36552 :         se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
    2468              :                                        se.expr);
    2469       345844 :       CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
    2470              :                               se.expr);
    2471       345844 :       c = gfc_constructor_next (c);
    2472       345844 :       nelem++;
    2473              :     }
    2474              : 
    2475              :   /* Next determine the tree type for the array.  We use the gfortran
    2476              :      front-end's gfc_get_nodesc_array_type in order to create a suitable
    2477              :      GFC_ARRAY_TYPE_P that may be used by the scalarizer.  */
    2478              : 
    2479        43792 :   memset (&as, 0, sizeof (gfc_array_spec));
    2480              : 
    2481        43792 :   as.rank = expr->rank;
    2482        43792 :   as.type = AS_EXPLICIT;
    2483        43792 :   if (!expr->shape)
    2484              :     {
    2485            4 :       as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
    2486            4 :       as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
    2487              :                                       NULL, nelem - 1);
    2488              :     }
    2489              :   else
    2490        94345 :     for (i = 0; i < expr->rank; i++)
    2491              :       {
    2492        50557 :         int tmp = (int) mpz_get_si (expr->shape[i]);
    2493        50557 :         as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
    2494        50557 :         as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
    2495        50557 :                                         NULL, tmp - 1);
    2496              :       }
    2497              : 
    2498        43792 :   tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
    2499              : 
    2500              :   /* as is not needed anymore.  */
    2501       138145 :   for (i = 0; i < as.rank + as.corank; i++)
    2502              :     {
    2503        50561 :       gfc_free_expr (as.lower[i]);
    2504        50561 :       gfc_free_expr (as.upper[i]);
    2505              :     }
    2506              : 
    2507        43792 :   init = build_constructor (tmptype, v);
    2508              : 
    2509        43792 :   TREE_CONSTANT (init) = 1;
    2510        43792 :   TREE_STATIC (init) = 1;
    2511              : 
    2512        43792 :   tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
    2513              :                     tmptype);
    2514        43792 :   DECL_ARTIFICIAL (tmp) = 1;
    2515        43792 :   DECL_IGNORED_P (tmp) = 1;
    2516        43792 :   TREE_STATIC (tmp) = 1;
    2517        43792 :   TREE_CONSTANT (tmp) = 1;
    2518        43792 :   TREE_READONLY (tmp) = 1;
    2519        43792 :   DECL_INITIAL (tmp) = init;
    2520        43792 :   pushdecl (tmp);
    2521              : 
    2522        43792 :   return tmp;
    2523              : }
    2524              : 
    2525              : 
    2526              : /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
    2527              :    This mostly initializes the scalarizer state info structure with the
    2528              :    appropriate values to directly use the array created by the function
    2529              :    gfc_build_constant_array_constructor.  */
    2530              : 
    2531              : static void
    2532        37803 : trans_constant_array_constructor (gfc_ss * ss, tree type)
    2533              : {
    2534        37803 :   gfc_array_info *info;
    2535        37803 :   tree tmp;
    2536        37803 :   int i;
    2537              : 
    2538        37803 :   tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
    2539              : 
    2540        37803 :   info = &ss->info->data.array;
    2541              : 
    2542        37803 :   info->descriptor = tmp;
    2543        37803 :   info->data = gfc_build_addr_expr (NULL_TREE, tmp);
    2544        37803 :   info->offset = gfc_index_zero_node;
    2545              : 
    2546        79491 :   for (i = 0; i < ss->dimen; i++)
    2547              :     {
    2548        41688 :       info->delta[i] = gfc_index_zero_node;
    2549        41688 :       info->start[i] = gfc_index_zero_node;
    2550        41688 :       info->end[i] = gfc_index_zero_node;
    2551        41688 :       info->stride[i] = gfc_index_one_node;
    2552              :     }
    2553        37803 : }
    2554              : 
    2555              : 
    2556              : static int
    2557        37810 : get_rank (gfc_loopinfo *loop)
    2558              : {
    2559        37810 :   int rank;
    2560              : 
    2561        37810 :   rank = 0;
    2562       162584 :   for (; loop; loop = loop->parent)
    2563        81298 :     rank += loop->dimen;
    2564              : 
    2565        43476 :   return rank;
    2566              : }
    2567              : 
    2568              : 
    2569              : /* Helper routine of gfc_trans_array_constructor to determine if the
    2570              :    bounds of the loop specified by LOOP are constant and simple enough
    2571              :    to use with trans_constant_array_constructor.  Returns the
    2572              :    iteration count of the loop if suitable, and NULL_TREE otherwise.  */
    2573              : 
    2574              : static tree
    2575        37810 : constant_array_constructor_loop_size (gfc_loopinfo * l)
    2576              : {
    2577        37810 :   gfc_loopinfo *loop;
    2578        37810 :   tree size = gfc_index_one_node;
    2579        37810 :   tree tmp;
    2580        37810 :   int i, total_dim;
    2581              : 
    2582        37810 :   total_dim = get_rank (l);
    2583              : 
    2584        75620 :   for (loop = l; loop; loop = loop->parent)
    2585              :     {
    2586        79517 :       for (i = 0; i < loop->dimen; i++)
    2587              :         {
    2588              :           /* If the bounds aren't constant, return NULL_TREE.  */
    2589        41707 :           if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
    2590              :             return NULL_TREE;
    2591        41701 :           if (!integer_zerop (loop->from[i]))
    2592              :             {
    2593              :               /* Only allow nonzero "from" in one-dimensional arrays.  */
    2594            0 :               if (total_dim != 1)
    2595              :                 return NULL_TREE;
    2596            0 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2597              :                                      gfc_array_index_type,
    2598              :                                      loop->to[i], loop->from[i]);
    2599              :             }
    2600              :           else
    2601        41701 :             tmp = loop->to[i];
    2602        41701 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2603              :                                  gfc_array_index_type, tmp, gfc_index_one_node);
    2604        41701 :           size = fold_build2_loc (input_location, MULT_EXPR,
    2605              :                                   gfc_array_index_type, size, tmp);
    2606              :         }
    2607              :     }
    2608              : 
    2609              :   return size;
    2610              : }
    2611              : 
    2612              : 
    2613              : static tree *
    2614        44989 : get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
    2615              : {
    2616        44989 :   gfc_ss *ss;
    2617        44989 :   int n;
    2618              : 
    2619        44989 :   gcc_assert (array->nested_ss == NULL);
    2620              : 
    2621        44989 :   for (ss = array; ss; ss = ss->parent)
    2622        44989 :     for (n = 0; n < ss->loop->dimen; n++)
    2623        44989 :       if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
    2624        44989 :         return &(ss->loop->to[n]);
    2625              : 
    2626            0 :   gcc_unreachable ();
    2627              : }
    2628              : 
    2629              : 
    2630              : static gfc_loopinfo *
    2631       743408 : outermost_loop (gfc_loopinfo * loop)
    2632              : {
    2633       969946 :   while (loop->parent != NULL)
    2634              :     loop = loop->parent;
    2635              : 
    2636       750251 :   return loop;
    2637              : }
    2638              : 
    2639              : 
    2640              : /* Array constructors are handled by constructing a temporary, then using that
    2641              :    within the scalarization loop.  This is not optimal, but seems by far the
    2642              :    simplest method.  */
    2643              : 
    2644              : static void
    2645        44989 : trans_array_constructor (gfc_ss * ss, locus * where)
    2646              : {
    2647        44989 :   gfc_constructor_base c;
    2648        44989 :   tree offset;
    2649        44989 :   tree offsetvar;
    2650        44989 :   tree desc;
    2651        44989 :   tree type;
    2652        44989 :   tree tmp;
    2653        44989 :   tree *loop_ubound0;
    2654        44989 :   bool dynamic;
    2655        44989 :   bool old_first_len, old_typespec_chararray_ctor;
    2656        44989 :   tree old_first_len_val;
    2657        44989 :   gfc_loopinfo *loop, *outer_loop;
    2658        44989 :   gfc_ss_info *ss_info;
    2659        44989 :   gfc_expr *expr;
    2660        44989 :   gfc_ss *s;
    2661        44989 :   tree neg_len;
    2662        44989 :   char *msg;
    2663        44989 :   stmtblock_t finalblock;
    2664        44989 :   bool finalize_required;
    2665        44989 :   bool owned_sweep = false;
    2666              : 
    2667              :   /* Save the old values for nested checking.  */
    2668        44989 :   old_first_len = first_len;
    2669        44989 :   old_first_len_val = first_len_val;
    2670        44989 :   old_typespec_chararray_ctor = typespec_chararray_ctor;
    2671              : 
    2672        44989 :   loop = ss->loop;
    2673        44989 :   outer_loop = outermost_loop (loop);
    2674        44989 :   ss_info = ss->info;
    2675        44989 :   expr = ss_info->expr;
    2676              : 
    2677              :   /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
    2678              :      typespec was given for the array constructor.  */
    2679        89978 :   typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
    2680         8382 :                              && expr->ts.u.cl
    2681        53371 :                              && expr->ts.u.cl->length_from_typespec);
    2682              : 
    2683        44989 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2684         2542 :       && expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
    2685              :     {
    2686         1468 :       first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
    2687         1468 :       first_len = true;
    2688              :     }
    2689              : 
    2690        44989 :   gcc_assert (ss->dimen == ss->loop->dimen);
    2691              : 
    2692        44989 :   c = expr->value.constructor;
    2693        44989 :   if (expr->ts.type == BT_CHARACTER)
    2694              :     {
    2695         8382 :       bool const_string;
    2696         8382 :       bool force_new_cl = false;
    2697              : 
    2698              :       /* get_array_ctor_strlen walks the elements of the constructor, if a
    2699              :          typespec was given, we already know the string length and want the one
    2700              :          specified there.  */
    2701         8382 :       if (typespec_chararray_ctor && expr->ts.u.cl->length
    2702          520 :           && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    2703              :         {
    2704           28 :           gfc_se length_se;
    2705              : 
    2706           28 :           const_string = false;
    2707           28 :           gfc_init_se (&length_se, NULL);
    2708           28 :           gfc_conv_expr_type (&length_se, expr->ts.u.cl->length,
    2709              :                               gfc_charlen_type_node);
    2710           28 :           ss_info->string_length = length_se.expr;
    2711              : 
    2712              :           /* Check if the character length is negative.  If it is, then
    2713              :              set LEN = 0.  */
    2714           28 :           neg_len = fold_build2_loc (input_location, LT_EXPR,
    2715              :                                      logical_type_node, ss_info->string_length,
    2716           28 :                                      build_zero_cst (TREE_TYPE
    2717              :                                                      (ss_info->string_length)));
    2718              :           /* Print a warning if bounds checking is enabled.  */
    2719           28 :           if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2720              :             {
    2721           18 :               msg = xasprintf ("Negative character length treated as LEN = 0");
    2722           18 :               gfc_trans_runtime_check (false, true, neg_len, &length_se.pre,
    2723              :                                        where, msg);
    2724           18 :               free (msg);
    2725              :             }
    2726              : 
    2727           28 :           ss_info->string_length
    2728           28 :             = fold_build3_loc (input_location, COND_EXPR,
    2729              :                                gfc_charlen_type_node, neg_len,
    2730              :                                build_zero_cst
    2731           28 :                                (TREE_TYPE (ss_info->string_length)),
    2732              :                                ss_info->string_length);
    2733           28 :           ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
    2734              :                                                      &length_se.pre);
    2735           28 :           gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
    2736           28 :           gfc_add_block_to_block (&outer_loop->post, &length_se.post);
    2737           28 :         }
    2738              :       else
    2739              :         {
    2740         8354 :           const_string = get_array_ctor_strlen (&outer_loop->pre, c,
    2741              :                                                 &ss_info->string_length);
    2742         8354 :           force_new_cl = true;
    2743              : 
    2744              :           /* Initialize "len" with string length for bounds checking.  */
    2745         8354 :           if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2746         1486 :               && !typespec_chararray_ctor
    2747         1468 :               && ss_info->string_length)
    2748              :             {
    2749         1468 :               gfc_se length_se;
    2750              : 
    2751         1468 :               gfc_init_se (&length_se, NULL);
    2752         1468 :               gfc_add_modify (&length_se.pre, first_len_val,
    2753         1468 :                               fold_convert (TREE_TYPE (first_len_val),
    2754              :                                             ss_info->string_length));
    2755         1468 :               ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
    2756              :                                                          &length_se.pre);
    2757         1468 :               gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
    2758         1468 :               gfc_add_block_to_block (&outer_loop->post, &length_se.post);
    2759              :             }
    2760              :         }
    2761              : 
    2762              :       /* Complex character array constructors should have been taken care of
    2763              :          and not end up here.  */
    2764         8382 :       gcc_assert (ss_info->string_length);
    2765              : 
    2766         8382 :       store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
    2767              : 
    2768         8382 :       type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
    2769         8382 :       if (const_string)
    2770         7328 :         type = build_pointer_type (type);
    2771              :     }
    2772              :   else
    2773        36607 :     type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
    2774            0 :                                   ? &CLASS_DATA (expr)->ts : &expr->ts);
    2775              : 
    2776              :   /* See if the constructor determines the loop bounds.  */
    2777        44989 :   dynamic = false;
    2778              : 
    2779        44989 :   loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
    2780              : 
    2781        88465 :   if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
    2782              :     {
    2783              :       /* We have a multidimensional parameter.  */
    2784            0 :       for (s = ss; s; s = s->parent)
    2785              :         {
    2786              :           int n;
    2787            0 :           for (n = 0; n < s->loop->dimen; n++)
    2788              :             {
    2789            0 :               s->loop->from[n] = gfc_index_zero_node;
    2790            0 :               s->loop->to[n] = gfc_conv_mpz_to_tree (expr->shape[s->dim[n]],
    2791              :                                                      gfc_index_integer_kind);
    2792            0 :               s->loop->to[n] = fold_build2_loc (input_location, MINUS_EXPR,
    2793              :                                                 gfc_array_index_type,
    2794            0 :                                                 s->loop->to[n],
    2795              :                                                 gfc_index_one_node);
    2796              :             }
    2797              :         }
    2798              :     }
    2799              : 
    2800        44989 :   if (*loop_ubound0 == NULL_TREE)
    2801              :     {
    2802          894 :       mpz_t size;
    2803              : 
    2804              :       /* We should have a 1-dimensional, zero-based loop.  */
    2805          894 :       gcc_assert (loop->parent == NULL && loop->nested == NULL);
    2806          894 :       gcc_assert (loop->dimen == 1);
    2807          894 :       gcc_assert (integer_zerop (loop->from[0]));
    2808              : 
    2809              :       /* Split the constructor size into a static part and a dynamic part.
    2810              :          Allocate the static size up-front and record whether the dynamic
    2811              :          size might be nonzero.  */
    2812          894 :       mpz_init (size);
    2813          894 :       dynamic = gfc_get_array_constructor_size (&size, c);
    2814          894 :       mpz_sub_ui (size, size, 1);
    2815          894 :       loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
    2816          894 :       mpz_clear (size);
    2817              :     }
    2818              : 
    2819              :   /* Special case constant array constructors.  */
    2820          894 :   if (!dynamic)
    2821              :     {
    2822        44120 :       unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
    2823        44120 :       if (nelem > 0)
    2824              :         {
    2825        37810 :           tree size = constant_array_constructor_loop_size (loop);
    2826        37804 :           if (size && compare_tree_int (size, nelem) == 0
    2827        75614 :               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
    2828              :             {
    2829        37803 :               trans_constant_array_constructor (ss, type);
    2830        37803 :               goto finish;
    2831              :             }
    2832              :         }
    2833              :     }
    2834              : 
    2835         7186 :   gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
    2836              :                                NULL_TREE, dynamic, true, false, where);
    2837              : 
    2838         7186 :   desc = ss_info->data.array.descriptor;
    2839         7186 :   offset = gfc_index_zero_node;
    2840         7186 :   offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
    2841         7186 :   suppress_warning (offsetvar);
    2842         7186 :   TREE_USED (offsetvar) = 0;
    2843              : 
    2844         7186 :   gfc_init_block (&finalblock);
    2845         7186 :   finalize_required = expr->must_finalize;
    2846         7186 :   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
    2847              :     finalize_required = true;
    2848              : 
    2849         7186 :   if (IS_PDT (expr))
    2850              :    finalize_required = true;
    2851              : 
    2852              :   /* If every element of the constructor is a function result with allocatable
    2853              :      components, those components are owned by the temporary and are freed in a
    2854              :      single sweep over the whole array below.  This is the only way to free the
    2855              :      elements produced inside an implied-do loop, where a single compile-time
    2856              :      element stands for many runtime elements.  */
    2857        14293 :   owned_sweep = finalize_required
    2858          570 :     && expr->ts.type == BT_DERIVED
    2859          570 :     && expr->ts.u.derived->attr.alloc_comp
    2860         7586 :     && gfc_constructor_is_owned_alloc_comp (c, expr->ts.u.derived);
    2861              : 
    2862         7186 :   gfc_trans_array_constructor_value (&outer_loop->pre,
    2863              :                                      finalize_required ? &finalblock : NULL,
    2864              :                                      type, desc, c, &offset, &offsetvar,
    2865              :                                      dynamic, owned_sweep);
    2866              : 
    2867         7186 :   if (owned_sweep)
    2868          250 :     gfc_add_expr_to_block (&finalblock,
    2869          250 :                            gfc_deallocate_alloc_comp_no_caf (expr->ts.u.derived,
    2870              :                                                              desc, 1, true));
    2871              : 
    2872              :   /* If the array grows dynamically, the upper bound of the loop variable
    2873              :      is determined by the array's final upper bound.  */
    2874         7186 :   if (dynamic)
    2875              :     {
    2876          869 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2877              :                              gfc_array_index_type,
    2878              :                              offsetvar, gfc_index_one_node);
    2879          869 :       tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
    2880          869 :       if (*loop_ubound0 && VAR_P (*loop_ubound0))
    2881            0 :         gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
    2882              :       else
    2883          869 :         *loop_ubound0 = tmp;
    2884              :     }
    2885              : 
    2886         7186 :   if (TREE_USED (offsetvar))
    2887         2267 :     pushdecl (offsetvar);
    2888              :   else
    2889         4919 :     gcc_assert (INTEGER_CST_P (offset));
    2890              : 
    2891              : #if 0
    2892              :   /* Disable bound checking for now because it's probably broken.  */
    2893              :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2894              :     {
    2895              :       gcc_unreachable ();
    2896              :     }
    2897              : #endif
    2898              : 
    2899         4919 : finish:
    2900              :   /* Restore old values of globals.  */
    2901        44989 :   first_len = old_first_len;
    2902        44989 :   first_len_val = old_first_len_val;
    2903        44989 :   typespec_chararray_ctor = old_typespec_chararray_ctor;
    2904              : 
    2905              :   /* F2008 4.5.6.3 para 5: If an executable construct references a structure
    2906              :      constructor or array constructor, the entity created by the constructor is
    2907              :      finalized after execution of the innermost executable construct containing
    2908              :      the reference.  */
    2909        44989 :   if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    2910         1826 :        && finalblock.head != NULL_TREE)
    2911          328 :     gfc_prepend_expr_to_block (&loop->post, finalblock.head);
    2912        44989 : }
    2913              : 
    2914              : 
    2915              : /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
    2916              :    called after evaluating all of INFO's vector dimensions.  Go through
    2917              :    each such vector dimension and see if we can now fill in any missing
    2918              :    loop bounds.  */
    2919              : 
    2920              : static void
    2921       190399 : set_vector_loop_bounds (gfc_ss * ss)
    2922              : {
    2923       190399 :   gfc_loopinfo *loop, *outer_loop;
    2924       190399 :   gfc_array_info *info;
    2925       190399 :   gfc_se se;
    2926       190399 :   tree tmp;
    2927       190399 :   tree desc;
    2928       190399 :   tree zero;
    2929       190399 :   int n;
    2930       190399 :   int dim;
    2931              : 
    2932       190399 :   outer_loop = outermost_loop (ss->loop);
    2933              : 
    2934       190399 :   info = &ss->info->data.array;
    2935              : 
    2936       385434 :   for (; ss; ss = ss->parent)
    2937              :     {
    2938       195035 :       loop = ss->loop;
    2939              : 
    2940       463702 :       for (n = 0; n < loop->dimen; n++)
    2941              :         {
    2942       268667 :           dim = ss->dim[n];
    2943       268667 :           if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
    2944          987 :               || loop->to[n] != NULL)
    2945       268486 :             continue;
    2946              : 
    2947              :           /* Loop variable N indexes vector dimension DIM, and we don't
    2948              :              yet know the upper bound of loop variable N.  Set it to the
    2949              :              difference between the vector's upper and lower bounds.  */
    2950          181 :           gcc_assert (loop->from[n] == gfc_index_zero_node);
    2951          181 :           gcc_assert (info->subscript[dim]
    2952              :                       && info->subscript[dim]->info->type == GFC_SS_VECTOR);
    2953              : 
    2954          181 :           gfc_init_se (&se, NULL);
    2955          181 :           desc = info->subscript[dim]->info->data.array.descriptor;
    2956          181 :           zero = gfc_rank_cst[0];
    2957          181 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2958              :                              gfc_array_index_type,
    2959              :                              gfc_conv_descriptor_ubound_get (desc, zero),
    2960              :                              gfc_conv_descriptor_lbound_get (desc, zero));
    2961          181 :           tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
    2962          181 :           loop->to[n] = tmp;
    2963              :         }
    2964              :     }
    2965       190399 : }
    2966              : 
    2967              : 
    2968              : /* Tells whether a scalar argument to an elemental procedure is saved out
    2969              :    of a scalarization loop as a value or as a reference.  */
    2970              : 
    2971              : bool
    2972        46755 : gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
    2973              : {
    2974        46755 :   if (ss_info->type != GFC_SS_REFERENCE)
    2975              :     return false;
    2976              : 
    2977        10462 :   if (ss_info->data.scalar.needs_temporary)
    2978              :     return false;
    2979              : 
    2980              :   /* If the actual argument can be absent (in other words, it can
    2981              :      be a NULL reference), don't try to evaluate it; pass instead
    2982              :      the reference directly.  */
    2983        10050 :   if (ss_info->can_be_null_ref)
    2984              :     return true;
    2985              : 
    2986              :   /* If the expression is of polymorphic type, it's actual size is not known,
    2987              :      so we avoid copying it anywhere.  */
    2988         9374 :   if (ss_info->data.scalar.dummy_arg
    2989         1402 :       && gfc_dummy_arg_get_typespec (*ss_info->data.scalar.dummy_arg).type
    2990              :          == BT_CLASS
    2991         9498 :       && ss_info->expr->ts.type == BT_CLASS)
    2992              :     return true;
    2993              : 
    2994              :   /* If the expression is a data reference of aggregate type,
    2995              :      and the data reference is not used on the left hand side,
    2996              :      avoid a copy by saving a reference to the content.  */
    2997         9350 :   if (!ss_info->data.scalar.needs_temporary
    2998         9350 :       && (ss_info->expr->ts.type == BT_DERIVED
    2999         8230 :           || ss_info->expr->ts.type == BT_CLASS)
    3000        10518 :       && gfc_expr_is_variable (ss_info->expr))
    3001              :     return true;
    3002              : 
    3003              :   /* Otherwise the expression is evaluated to a temporary variable before the
    3004              :      scalarization loop.  */
    3005              :   return false;
    3006              : }
    3007              : 
    3008              : 
    3009              : /* Add the pre and post chains for all the scalar expressions in a SS chain
    3010              :    to loop.  This is called after the loop parameters have been calculated,
    3011              :    but before the actual scalarizing loops.  */
    3012              : 
    3013              : static void
    3014       201350 : gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
    3015              :                       locus * where)
    3016              : {
    3017       201350 :   gfc_loopinfo *nested_loop, *outer_loop;
    3018       201350 :   gfc_se se;
    3019       201350 :   gfc_ss_info *ss_info;
    3020       201350 :   gfc_array_info *info;
    3021       201350 :   gfc_expr *expr;
    3022       201350 :   int n;
    3023              : 
    3024              :   /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
    3025              :      arguments could get evaluated multiple times.  */
    3026       201350 :   if (ss->is_alloc_lhs)
    3027          203 :     return;
    3028              : 
    3029       526979 :   outer_loop = outermost_loop (loop);
    3030              : 
    3031              :   /* TODO: This can generate bad code if there are ordering dependencies,
    3032              :      e.g., a callee allocated function and an unknown size constructor.  */
    3033              :   gcc_assert (ss != NULL);
    3034              : 
    3035       526979 :   for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
    3036              :     {
    3037       325832 :       gcc_assert (ss);
    3038              : 
    3039              :       /* Cross loop arrays are handled from within the most nested loop.  */
    3040       325832 :       if (ss->nested_ss != NULL)
    3041         4740 :         continue;
    3042              : 
    3043       321092 :       ss_info = ss->info;
    3044       321092 :       expr = ss_info->expr;
    3045       321092 :       info = &ss_info->data.array;
    3046              : 
    3047       321092 :       switch (ss_info->type)
    3048              :         {
    3049        44634 :         case GFC_SS_SCALAR:
    3050              :           /* Scalar expression.  Evaluate this now.  This includes elemental
    3051              :              dimension indices, but not array section bounds.  */
    3052        44634 :           gfc_init_se (&se, NULL);
    3053        44634 :           gfc_conv_expr (&se, expr);
    3054        44634 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3055              : 
    3056        44634 :           if (expr->ts.type != BT_CHARACTER
    3057        44634 :               && !gfc_is_alloc_class_scalar_function (expr))
    3058              :             {
    3059              :               /* Move the evaluation of scalar expressions outside the
    3060              :                  scalarization loop, except for WHERE assignments.  */
    3061        40568 :               if (subscript)
    3062         6646 :                 se.expr = convert(gfc_array_index_type, se.expr);
    3063        40568 :               if (!ss_info->where)
    3064        40154 :                 se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
    3065        40568 :               gfc_add_block_to_block (&outer_loop->pre, &se.post);
    3066              :             }
    3067              :           else
    3068         4066 :             gfc_add_block_to_block (&outer_loop->post, &se.post);
    3069              : 
    3070        44634 :           ss_info->data.scalar.value = se.expr;
    3071        44634 :           ss_info->string_length = se.string_length;
    3072        44634 :           break;
    3073              : 
    3074         5231 :         case GFC_SS_REFERENCE:
    3075              :           /* Scalar argument to elemental procedure.  */
    3076         5231 :           gfc_init_se (&se, NULL);
    3077         5231 :           if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
    3078          910 :             gfc_conv_expr_reference (&se, expr);
    3079              :           else
    3080              :             {
    3081              :               /* Evaluate the argument outside the loop and pass
    3082              :                  a reference to the value.  */
    3083         4321 :               gfc_conv_expr (&se, expr);
    3084              :             }
    3085              : 
    3086              :           /* Ensure that a pointer to the string is stored.  */
    3087         5231 :           if (expr->ts.type == BT_CHARACTER)
    3088          174 :             gfc_conv_string_parameter (&se);
    3089              : 
    3090         5231 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3091         5231 :           gfc_add_block_to_block (&outer_loop->post, &se.post);
    3092         5231 :           if (gfc_is_class_scalar_expr (expr))
    3093              :             /* This is necessary because the dynamic type will always be
    3094              :                large than the declared type.  In consequence, assigning
    3095              :                the value to a temporary could segfault.
    3096              :                OOP-TODO: see if this is generally correct or is the value
    3097              :                has to be written to an allocated temporary, whose address
    3098              :                is passed via ss_info.  */
    3099           48 :             ss_info->data.scalar.value = se.expr;
    3100              :           else
    3101         5183 :             ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
    3102              :                                                            &outer_loop->pre);
    3103              : 
    3104         5231 :           ss_info->string_length = se.string_length;
    3105         5231 :           break;
    3106              : 
    3107              :         case GFC_SS_SECTION:
    3108              :           /* Add the expressions for scalar and vector subscripts.  */
    3109      3046384 :           for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    3110      2855985 :             if (info->subscript[n])
    3111         7633 :               gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
    3112              : 
    3113       190399 :           set_vector_loop_bounds (ss);
    3114       190399 :           break;
    3115              : 
    3116          987 :         case GFC_SS_VECTOR:
    3117              :           /* Get the vector's descriptor and store it in SS.  */
    3118          987 :           gfc_init_se (&se, NULL);
    3119          987 :           gfc_conv_expr_descriptor (&se, expr);
    3120          987 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3121          987 :           gfc_add_block_to_block (&outer_loop->post, &se.post);
    3122          987 :           info->descriptor = se.expr;
    3123          987 :           break;
    3124              : 
    3125        11710 :         case GFC_SS_INTRINSIC:
    3126        11710 :           gfc_add_intrinsic_ss_code (loop, ss);
    3127        11710 :           break;
    3128              : 
    3129         9988 :         case GFC_SS_FUNCTION:
    3130         9988 :           {
    3131              :             /* Array function return value.  We call the function and save its
    3132              :                result in a temporary for use inside the loop.  */
    3133         9988 :             gfc_init_se (&se, NULL);
    3134         9988 :             se.loop = loop;
    3135         9988 :             se.ss = ss;
    3136         9988 :             bool class_func = gfc_is_class_array_function (expr);
    3137         9988 :             if (class_func)
    3138          189 :               expr->must_finalize = 1;
    3139         9988 :             gfc_conv_expr (&se, expr);
    3140         9988 :             gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3141         9988 :             if (class_func
    3142          189 :                 && se.expr
    3143        10177 :                 && GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
    3144              :               {
    3145          189 :                 tree tmp = gfc_class_data_get (se.expr);
    3146          189 :                 info->descriptor = tmp;
    3147          189 :                 info->data = gfc_conv_descriptor_data_get (tmp);
    3148          189 :                 info->offset = gfc_conv_descriptor_offset_get (tmp);
    3149          378 :                 for (gfc_ss *s = ss; s; s = s->parent)
    3150          390 :                   for (int n = 0; n < s->dimen; n++)
    3151              :                     {
    3152          201 :                       int dim = s->dim[n];
    3153          201 :                       tree tree_dim = gfc_rank_cst[dim];
    3154              : 
    3155          201 :                       tree start;
    3156          201 :                       start = gfc_conv_descriptor_lbound_get (tmp, tree_dim);
    3157          201 :                       start = gfc_evaluate_now (start, &outer_loop->pre);
    3158          201 :                       info->start[dim] = start;
    3159              : 
    3160          201 :                       tree end;
    3161          201 :                       end = gfc_conv_descriptor_ubound_get (tmp, tree_dim);
    3162          201 :                       end = gfc_evaluate_now (end, &outer_loop->pre);
    3163          201 :                       info->end[dim] = end;
    3164              : 
    3165          201 :                       tree stride;
    3166          201 :                       stride = gfc_conv_descriptor_stride_get (tmp, tree_dim);
    3167          201 :                       stride = gfc_evaluate_now (stride, &outer_loop->pre);
    3168          201 :                       info->stride[dim] = stride;
    3169              :                     }
    3170              :               }
    3171         9988 :             gfc_add_block_to_block (&outer_loop->post, &se.post);
    3172         9988 :             gfc_add_block_to_block (&outer_loop->post, &se.finalblock);
    3173         9988 :             ss_info->string_length = se.string_length;
    3174              :           }
    3175         9988 :           break;
    3176              : 
    3177        44989 :         case GFC_SS_CONSTRUCTOR:
    3178        44989 :           if (expr->ts.type == BT_CHARACTER
    3179         8382 :               && ss_info->string_length == NULL
    3180         8382 :               && expr->ts.u.cl
    3181         8382 :               && expr->ts.u.cl->length
    3182         8081 :               && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    3183              :             {
    3184         8005 :               gfc_init_se (&se, NULL);
    3185         8005 :               gfc_conv_expr_type (&se, expr->ts.u.cl->length,
    3186              :                                   gfc_charlen_type_node);
    3187         8005 :               ss_info->string_length = se.expr;
    3188         8005 :               gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3189         8005 :               gfc_add_block_to_block (&outer_loop->post, &se.post);
    3190              :             }
    3191        44989 :           trans_array_constructor (ss, where);
    3192        44989 :           break;
    3193              : 
    3194              :         case GFC_SS_TEMP:
    3195              :         case GFC_SS_COMPONENT:
    3196              :           /* Do nothing.  These are handled elsewhere.  */
    3197              :           break;
    3198              : 
    3199            0 :         default:
    3200            0 :           gcc_unreachable ();
    3201              :         }
    3202              :     }
    3203              : 
    3204       201147 :   if (!subscript)
    3205       196878 :     for (nested_loop = loop->nested; nested_loop;
    3206         3364 :          nested_loop = nested_loop->next)
    3207         3364 :       gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
    3208              : }
    3209              : 
    3210              : 
    3211              : /* Given an array descriptor expression DESCR and its data pointer DATA, decide
    3212              :    whether to either save the data pointer to a variable and use the variable or
    3213              :    use the data pointer expression directly without any intermediary variable.
    3214              :    */
    3215              : 
    3216              : static bool
    3217       132882 : save_descriptor_data (tree descr, tree data)
    3218              : {
    3219       132882 :   return !(DECL_P (data)
    3220       121464 :            || (TREE_CODE (data) == ADDR_EXPR
    3221        70859 :                && DECL_P (TREE_OPERAND (data, 0)))
    3222        53859 :            || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (descr))
    3223        50129 :                && TREE_CODE (descr) == COMPONENT_REF
    3224        12770 :                && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (descr, 0)))));
    3225              : }
    3226              : 
    3227              : 
    3228              : /* Type of the DATA argument passed to walk_tree by substitute_subexpr_in_expr
    3229              :    and used by maybe_substitute_expr.  */
    3230              : 
    3231              : typedef struct
    3232              : {
    3233              :   tree target, repl;
    3234              : }
    3235              : substitute_t;
    3236              : 
    3237              : 
    3238              : /* Check if the expression in *TP is equal to the substitution target provided
    3239              :    in DATA->TARGET and replace it with DATA->REPL in that case.   This is a
    3240              :    callback function for use with walk_tree.  */
    3241              : 
    3242              : static tree
    3243        24291 : maybe_substitute_expr (tree *tp, int *walk_subtree, void *data)
    3244              : {
    3245        24291 :   substitute_t *subst = (substitute_t *) data;
    3246        24291 :   if (*tp == subst->target)
    3247              :     {
    3248         4618 :       *tp = subst->repl;
    3249         4618 :       *walk_subtree = 0;
    3250              :     }
    3251              : 
    3252        24291 :   return NULL_TREE;
    3253              : }
    3254              : 
    3255              : 
    3256              : /* Substitute in EXPR any occurrence of TARGET with REPLACEMENT.  */
    3257              : 
    3258              : static void
    3259         4299 : substitute_subexpr_in_expr (tree target, tree replacement, tree expr)
    3260              : {
    3261         4299 :   substitute_t subst;
    3262         4299 :   subst.target = target;
    3263         4299 :   subst.repl = replacement;
    3264              : 
    3265         4299 :   walk_tree (&expr, maybe_substitute_expr, &subst, nullptr);
    3266         4299 : }
    3267              : 
    3268              : 
    3269              : /* Save REF to a fresh variable in all of REPLACEMENT_ROOTS, appending extra
    3270              :    code to CODE.  Before returning, add REF to REPLACEMENT_ROOTS and clear
    3271              :    REF.  */
    3272              : 
    3273              : static void
    3274         4097 : save_ref (tree &code, tree &ref, vec<tree> &replacement_roots)
    3275              : {
    3276         4097 :   stmtblock_t tmp_block;
    3277         4097 :   gfc_init_block (&tmp_block);
    3278         4097 :   tree var = gfc_evaluate_now (ref, &tmp_block);
    3279         4097 :   gfc_add_expr_to_block (&tmp_block, code);
    3280         4097 :   code = gfc_finish_block (&tmp_block);
    3281              : 
    3282         4097 :   unsigned i;
    3283         4097 :   tree repl_root;
    3284         8396 :   FOR_EACH_VEC_ELT (replacement_roots, i, repl_root)
    3285         4299 :     substitute_subexpr_in_expr (ref, var, repl_root);
    3286              : 
    3287         4097 :   replacement_roots.safe_push (ref);
    3288         4097 :   ref = NULL_TREE;
    3289         4097 : }
    3290              : 
    3291              : 
    3292              : /* If REF isn't shared with code in PREVIOUS_CODE, replace it with a fresh
    3293              :    variable in all of REPLACEMENT_ROOTS, appending extra code to CODE.  */
    3294              : 
    3295              : static void
    3296         4169 : maybe_save_ref (tree &code, tree &ref, vec<tree> &replacement_roots,
    3297              :                 stmtblock_t *previous_code)
    3298              : {
    3299         4169 :   if (find_tree (previous_code->head, ref))
    3300              :     return;
    3301              : 
    3302         4097 :   save_ref (code, ref, replacement_roots);
    3303              : }
    3304              : 
    3305              : 
    3306              : /* Save the descriptor reference VALUE to storage pointed by DESC_PTR.  Before
    3307              :    that, try to create fresh variables to factor subexpressions of VALUE, if
    3308              :    those subexpressions aren't shared with code in PRELIMINARY_CODE.  Add any
    3309              :    necessary additional code (initialization of variables typically) to BLOCK.
    3310              : 
    3311              :    The candidate references to factoring are dereferenced pointers because they
    3312              :    are cheap to copy and array descriptors because they are often the base of
    3313              :    multiple subreferences.  */
    3314              : 
    3315              : static void
    3316       348562 : set_factored_descriptor_value (tree *desc_ptr, tree value, stmtblock_t *block,
    3317              :                                stmtblock_t *preliminary_code)
    3318              : {
    3319              :   /* As the reference is processed from outer to inner, variable definitions
    3320              :      will be generated in reversed order, so can't be put directly in BLOCK.
    3321              :      We use temporary blocks instead, which we save in ACCUMULATED_CODE, and
    3322              :      only append to BLOCK at the end.  */
    3323       348562 :   tree accumulated_code = NULL_TREE;
    3324              : 
    3325              :   /* The current candidate to factoring.  */
    3326       348562 :   tree saveable_ref = NULL_TREE;
    3327              : 
    3328              :   /* The root expressions in which we look for subexpressions to replace with
    3329              :      variables.  */
    3330       348562 :   auto_vec<tree> replacement_roots;
    3331       348562 :   replacement_roots.safe_push (value);
    3332              : 
    3333       348562 :   tree data_ref = value;
    3334       348562 :   tree next_ref = NULL_TREE;
    3335              : 
    3336              :   /* If the candidate reference is not followed by a subreference, it can't be
    3337              :      saved to a variable as it may be reallocatable, and we have to keep the
    3338              :      parent reference to be able to store the new pointer value in case of
    3339              :      reallocation.  */
    3340       348562 :   bool maybe_reallocatable = true;
    3341              : 
    3342       586392 :   while (true)
    3343              :     {
    3344       467477 :       if (!maybe_reallocatable
    3345       467477 :           && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (data_ref)))
    3346         2548 :         saveable_ref = data_ref;
    3347              : 
    3348       467477 :       if (TREE_CODE (data_ref) == INDIRECT_REF)
    3349              :         {
    3350        64512 :           next_ref = TREE_OPERAND (data_ref, 0);
    3351              : 
    3352        64512 :           if (!maybe_reallocatable)
    3353              :             {
    3354        16160 :               if (saveable_ref != NULL_TREE && saveable_ref != data_ref)
    3355              :                 {
    3356              :                   /* A reference worth saving has been seen, and now the pointer
    3357              :                      to the current reference is also worth saving.  If the
    3358              :                      previous reference to save wasn't the current one, do save
    3359              :                      it now.  Otherwise drop it as we prefer saving the
    3360              :                      pointer.  */
    3361         1989 :                   maybe_save_ref (accumulated_code, saveable_ref,
    3362              :                                   replacement_roots, preliminary_code);
    3363              :                 }
    3364              : 
    3365              :               /* Don't evaluate the pointer to a variable yet; do it only if the
    3366              :                  variable would be significantly more simple than the reference
    3367              :                  it replaces.  That is if the reference contains anything
    3368              :                  different from NOPs, COMPONENTs and DECLs.  */
    3369        16160 :               saveable_ref = next_ref;
    3370              :             }
    3371              :         }
    3372       402965 :       else if (TREE_CODE (data_ref) == COMPONENT_REF)
    3373              :         {
    3374        44852 :           maybe_reallocatable = false;
    3375        44852 :           next_ref = TREE_OPERAND (data_ref, 0);
    3376              :         }
    3377       358113 :       else if (TREE_CODE (data_ref) == NOP_EXPR)
    3378         3833 :         next_ref = TREE_OPERAND (data_ref, 0);
    3379              :       else
    3380              :         {
    3381       354280 :           if (DECL_P (data_ref))
    3382              :             break;
    3383              : 
    3384         7576 :           if (TREE_CODE (data_ref) == ARRAY_REF)
    3385              :             {
    3386         5718 :               maybe_reallocatable = false;
    3387         5718 :               next_ref = TREE_OPERAND (data_ref, 0);
    3388              :             }
    3389              : 
    3390         7576 :           if (saveable_ref != NULL_TREE)
    3391              :             /* We have seen a reference worth saving.  Do it now.  */
    3392         2180 :             maybe_save_ref (accumulated_code, saveable_ref, replacement_roots,
    3393              :                             preliminary_code);
    3394              : 
    3395         7576 :           if (TREE_CODE (data_ref) != ARRAY_REF)
    3396              :             break;
    3397              :         }
    3398              : 
    3399       118915 :       data_ref = next_ref;
    3400              :     }
    3401              : 
    3402       348562 :   *desc_ptr = value;
    3403       348562 :   gfc_add_expr_to_block (block, accumulated_code);
    3404       348562 : }
    3405              : 
    3406              : 
    3407              : /* Translate expressions for the descriptor and data pointer of a SS.  */
    3408              : /*GCC ARRAYS*/
    3409              : 
    3410              : static void
    3411       348562 : gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
    3412              : {
    3413       348562 :   gfc_se se;
    3414       348562 :   gfc_ss_info *ss_info;
    3415       348562 :   gfc_array_info *info;
    3416       348562 :   tree tmp;
    3417              : 
    3418       348562 :   ss_info = ss->info;
    3419       348562 :   info = &ss_info->data.array;
    3420              : 
    3421              :   /* Get the descriptor for the array to be scalarized.  */
    3422       348562 :   gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
    3423       348562 :   gfc_init_se (&se, NULL);
    3424       348562 :   se.descriptor_only = 1;
    3425       348562 :   gfc_conv_expr_lhs (&se, ss_info->expr);
    3426       348562 :   stmtblock_t tmp_block;
    3427       348562 :   gfc_init_block (&tmp_block);
    3428       348562 :   set_factored_descriptor_value (&info->descriptor, se.expr, &tmp_block,
    3429              :                                  &se.pre);
    3430       348562 :   gfc_add_block_to_block (block, &se.pre);
    3431       348562 :   gfc_add_block_to_block (block, &tmp_block);
    3432       348562 :   ss_info->string_length = se.string_length;
    3433       348562 :   ss_info->class_container = se.class_container;
    3434              : 
    3435       348562 :   if (base)
    3436              :     {
    3437       125982 :       if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
    3438        23202 :           && ss_info->expr->ts.u.cl->length == NULL)
    3439              :         {
    3440              :           /* Emit a DECL_EXPR for the variable sized array type in
    3441              :              GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
    3442              :              sizes works correctly.  */
    3443         1169 :           tree arraytype = TREE_TYPE (
    3444              :                 GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (info->descriptor)));
    3445         1169 :           if (! TYPE_NAME (arraytype))
    3446          917 :             TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
    3447              :                                                 NULL_TREE, arraytype);
    3448         1169 :           gfc_add_expr_to_block (block, build1 (DECL_EXPR, arraytype,
    3449         1169 :                                                 TYPE_NAME (arraytype)));
    3450              :         }
    3451              :       /* Also the data pointer.  */
    3452       125982 :       tmp = gfc_conv_array_data (se.expr);
    3453              :       /* If this is a variable or address or a class array, use it directly.
    3454              :          Otherwise we must evaluate it now to avoid breaking dependency
    3455              :          analysis by pulling the expressions for elemental array indices
    3456              :          inside the loop.  */
    3457       125982 :       if (save_descriptor_data (se.expr, tmp) && !ss->is_alloc_lhs)
    3458        37769 :         tmp = gfc_evaluate_now (tmp, block);
    3459       125982 :       info->data = tmp;
    3460              : 
    3461       125982 :       tmp = gfc_conv_array_offset (se.expr);
    3462       125982 :       if (!ss->is_alloc_lhs)
    3463       119285 :         tmp = gfc_evaluate_now (tmp, block);
    3464       125982 :       info->offset = tmp;
    3465              : 
    3466              :       /* Make absolutely sure that the saved_offset is indeed saved
    3467              :          so that the variable is still accessible after the loops
    3468              :          are translated.  */
    3469       125982 :       info->saved_offset = info->offset;
    3470              :     }
    3471       348562 : }
    3472              : 
    3473              : 
    3474              : /* Initialize a gfc_loopinfo structure.  */
    3475              : 
    3476              : void
    3477       201304 : gfc_init_loopinfo (gfc_loopinfo * loop)
    3478              : {
    3479       201304 :   int n;
    3480              : 
    3481       201304 :   memset (loop, 0, sizeof (gfc_loopinfo));
    3482       201304 :   gfc_init_block (&loop->pre);
    3483       201304 :   gfc_init_block (&loop->post);
    3484              : 
    3485              :   /* Initially scalarize in order and default to no loop reversal.  */
    3486      3422168 :   for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    3487              :     {
    3488      3019560 :       loop->order[n] = n;
    3489      3019560 :       loop->reverse[n] = GFC_INHIBIT_REVERSE;
    3490              :     }
    3491              : 
    3492       201304 :   loop->ss = gfc_ss_terminator;
    3493       201304 : }
    3494              : 
    3495              : 
    3496              : /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
    3497              :    chain.  */
    3498              : 
    3499              : void
    3500       195904 : gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
    3501              : {
    3502       195904 :   se->loop = loop;
    3503       195904 : }
    3504              : 
    3505              : 
    3506              : /* Return an expression for the data pointer of an array.  */
    3507              : 
    3508              : tree
    3509       354522 : gfc_conv_array_data (tree descriptor)
    3510              : {
    3511       354522 :   tree type;
    3512              : 
    3513       354522 :   type = TREE_TYPE (descriptor);
    3514       354522 :   if (GFC_ARRAY_TYPE_P (type))
    3515              :     {
    3516       249495 :       if (TREE_CODE (type) == POINTER_TYPE)
    3517              :         return descriptor;
    3518              :       else
    3519              :         {
    3520              :           /* Descriptorless arrays.  */
    3521       183549 :           return gfc_build_addr_expr (NULL_TREE, descriptor);
    3522              :         }
    3523              :     }
    3524              :   else
    3525       105027 :     return gfc_conv_descriptor_data_get (descriptor);
    3526              : }
    3527              : 
    3528              : 
    3529              : /* Return an expression for the base offset of an array.  */
    3530              : 
    3531              : tree
    3532       260660 : gfc_conv_array_offset (tree descriptor)
    3533              : {
    3534       260660 :   tree type;
    3535              : 
    3536       260660 :   type = TREE_TYPE (descriptor);
    3537       260660 :   if (GFC_ARRAY_TYPE_P (type))
    3538       186227 :     return GFC_TYPE_ARRAY_OFFSET (type);
    3539              :   else
    3540        74433 :     return gfc_conv_descriptor_offset_get (descriptor);
    3541              : }
    3542              : 
    3543              : 
    3544              : /* Get an expression for the array stride.  */
    3545              : 
    3546              : tree
    3547       520198 : gfc_conv_array_stride (tree descriptor, int dim)
    3548              : {
    3549       520198 :   tree tmp;
    3550       520198 :   tree type;
    3551              : 
    3552       520198 :   type = TREE_TYPE (descriptor);
    3553              : 
    3554              :   /* For descriptorless arrays use the array size.  */
    3555       520198 :   tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
    3556       520198 :   if (tmp != NULL_TREE)
    3557              :     return tmp;
    3558              : 
    3559       117528 :   tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
    3560       117528 :   return tmp;
    3561              : }
    3562              : 
    3563              : 
    3564              : /* Like gfc_conv_array_stride, but for the lower bound.  */
    3565              : 
    3566              : tree
    3567       344189 : gfc_conv_array_lbound (tree descriptor, int dim)
    3568              : {
    3569       344189 :   tree tmp;
    3570       344189 :   tree type;
    3571              : 
    3572       344189 :   type = TREE_TYPE (descriptor);
    3573              : 
    3574       344189 :   tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
    3575       344189 :   if (tmp != NULL_TREE)
    3576              :     return tmp;
    3577              : 
    3578        19484 :   tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
    3579        19484 :   return tmp;
    3580              : }
    3581              : 
    3582              : 
    3583              : /* Like gfc_conv_array_stride, but for the upper bound.  */
    3584              : 
    3585              : tree
    3586       222901 : gfc_conv_array_ubound (tree descriptor, int dim)
    3587              : {
    3588       222901 :   tree tmp;
    3589       222901 :   tree type;
    3590              : 
    3591       222901 :   type = TREE_TYPE (descriptor);
    3592              : 
    3593       222901 :   tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
    3594       222901 :   if (tmp != NULL_TREE)
    3595              :     return tmp;
    3596              : 
    3597              :   /* This should only ever happen when passing an assumed shape array
    3598              :      as an actual parameter.  The value will never be used.  */
    3599         9550 :   if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
    3600          554 :     return gfc_index_zero_node;
    3601              : 
    3602         8996 :   tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
    3603         8996 :   return tmp;
    3604              : }
    3605              : 
    3606              : 
    3607              : /* Generate abridged name of a part-ref for use in bounds-check message.
    3608              :    Cases:
    3609              :    (1) for an ordinary array variable x return "x"
    3610              :    (2) for z a DT scalar and array component x (at level 1) return "z%%x"
    3611              :    (3) for z a DT scalar and array component x (at level > 1) or
    3612              :        for z a DT array and array x (at any number of levels): "z...%%x"
    3613              :  */
    3614              : 
    3615              : static char *
    3616        36757 : abridged_ref_name (gfc_expr * expr, gfc_array_ref * ar)
    3617              : {
    3618        36757 :   gfc_ref *ref;
    3619        36757 :   gfc_symbol *sym;
    3620        36757 :   char *ref_name = NULL;
    3621        36757 :   const char *comp_name = NULL;
    3622        36757 :   int len_sym, last_len = 0, level = 0;
    3623        36757 :   bool sym_is_array;
    3624              : 
    3625        36757 :   gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->ref != NULL);
    3626              : 
    3627        36757 :   sym = expr->symtree->n.sym;
    3628        73127 :   sym_is_array = (sym->ts.type != BT_CLASS
    3629        36757 :                   ? sym->as != NULL
    3630          387 :                   : IS_CLASS_ARRAY (sym));
    3631        36757 :   len_sym = strlen (sym->name);
    3632              : 
    3633              :   /* Scan ref chain to get name of the array component (when ar != NULL) or
    3634              :      array section, determine depth and remember its component name.  */
    3635        52343 :   for (ref = expr->ref; ref; ref = ref->next)
    3636              :     {
    3637        38206 :       if (ref->type == REF_COMPONENT
    3638         1048 :           && strcmp (ref->u.c.component->name, "_data") != 0)
    3639              :         {
    3640          918 :           level++;
    3641          918 :           comp_name = ref->u.c.component->name;
    3642          918 :           continue;
    3643              :         }
    3644              : 
    3645        37288 :       if (ref->type != REF_ARRAY)
    3646          150 :         continue;
    3647              : 
    3648        37138 :       if (ar)
    3649              :         {
    3650        16092 :           if (&ref->u.ar == ar)
    3651              :             break;
    3652              :         }
    3653        21046 :       else if (ref->u.ar.type == AR_SECTION)
    3654              :         break;
    3655              :     }
    3656              : 
    3657        36757 :   if (level > 0)
    3658          800 :     last_len = strlen (comp_name);
    3659              : 
    3660              :   /* Provide a buffer sufficiently large to hold "x...%%z".  */
    3661        36757 :   ref_name = XNEWVEC (char, len_sym + last_len + 6);
    3662        36757 :   strcpy (ref_name, sym->name);
    3663              : 
    3664        36757 :   if (level == 1 && !sym_is_array)
    3665              :     {
    3666          442 :       strcat (ref_name, "%%");
    3667          442 :       strcat (ref_name, comp_name);
    3668              :     }
    3669        36315 :   else if (level > 0)
    3670              :     {
    3671          358 :       strcat (ref_name, "...%%");
    3672          358 :       strcat (ref_name, comp_name);
    3673              :     }
    3674              : 
    3675        36757 :   return ref_name;
    3676              : }
    3677              : 
    3678              : 
    3679              : /* Generate code to perform an array index bound check.  */
    3680              : 
    3681              : static tree
    3682         5736 : trans_array_bound_check (stmtblock_t *block, gfc_ss *ss, tree index, int n,
    3683              :                          locus * where, bool check_upper,
    3684              :                          const char *compname = NULL)
    3685              : {
    3686         5736 :   tree fault;
    3687         5736 :   tree tmp_lo, tmp_up;
    3688         5736 :   tree descriptor;
    3689         5736 :   char *msg;
    3690         5736 :   char *ref_name = NULL;
    3691         5736 :   const char * name = NULL;
    3692         5736 :   gfc_expr *expr;
    3693              : 
    3694         5736 :   if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
    3695              :     return index;
    3696              : 
    3697          252 :   descriptor = ss->info->data.array.descriptor;
    3698              : 
    3699          252 :   index = gfc_evaluate_now (index, block);
    3700              : 
    3701              :   /* We find a name for the error message.  */
    3702          252 :   name = ss->info->expr->symtree->n.sym->name;
    3703          252 :   gcc_assert (name != NULL);
    3704              : 
    3705              :   /* When we have a component ref, get name of the array section.
    3706              :      Note that there can only be one part ref.  */
    3707          252 :   expr = ss->info->expr;
    3708          252 :   if (expr->ref && !compname)
    3709          142 :     name = ref_name = abridged_ref_name (expr, NULL);
    3710              : 
    3711          252 :   if (VAR_P (descriptor))
    3712          162 :     name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
    3713              : 
    3714              :   /* Use given (array component) name.  */
    3715          252 :   if (compname)
    3716          110 :     name = compname;
    3717              : 
    3718              :   /* If upper bound is present, include both bounds in the error message.  */
    3719          252 :   if (check_upper)
    3720              :     {
    3721          225 :       tmp_lo = gfc_conv_array_lbound (descriptor, n);
    3722          225 :       tmp_up = gfc_conv_array_ubound (descriptor, n);
    3723              : 
    3724          225 :       if (name)
    3725          225 :         msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    3726              :                          "outside of expected range (%%ld:%%ld)", n+1, name);
    3727              :       else
    3728            0 :         msg = xasprintf ("Index '%%ld' of dimension %d "
    3729              :                          "outside of expected range (%%ld:%%ld)", n+1);
    3730              : 
    3731          225 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3732              :                                index, tmp_lo);
    3733          225 :       gfc_trans_runtime_check (true, false, fault, block, where, msg,
    3734              :                                fold_convert (long_integer_type_node, index),
    3735              :                                fold_convert (long_integer_type_node, tmp_lo),
    3736              :                                fold_convert (long_integer_type_node, tmp_up));
    3737          225 :       fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3738              :                                index, tmp_up);
    3739          225 :       gfc_trans_runtime_check (true, false, fault, block, where, msg,
    3740              :                                fold_convert (long_integer_type_node, index),
    3741              :                                fold_convert (long_integer_type_node, tmp_lo),
    3742              :                                fold_convert (long_integer_type_node, tmp_up));
    3743          225 :       free (msg);
    3744              :     }
    3745              :   else
    3746              :     {
    3747           27 :       tmp_lo = gfc_conv_array_lbound (descriptor, n);
    3748              : 
    3749           27 :       if (name)
    3750           27 :         msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    3751              :                          "below lower bound of %%ld", n+1, name);
    3752              :       else
    3753            0 :         msg = xasprintf ("Index '%%ld' of dimension %d "
    3754              :                          "below lower bound of %%ld", n+1);
    3755              : 
    3756           27 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3757              :                                index, tmp_lo);
    3758           27 :       gfc_trans_runtime_check (true, false, fault, block, where, msg,
    3759              :                                fold_convert (long_integer_type_node, index),
    3760              :                                fold_convert (long_integer_type_node, tmp_lo));
    3761           27 :       free (msg);
    3762              :     }
    3763              : 
    3764          252 :   free (ref_name);
    3765          252 :   return index;
    3766              : }
    3767              : 
    3768              : 
    3769              : /* Helper functions to detect impure functions in an expression.  */
    3770              : 
    3771              : static const char *impure_name = NULL;
    3772              : static bool
    3773          126 : expr_contains_impure_fcn (gfc_expr *e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    3774              :          int* g ATTRIBUTE_UNUSED)
    3775              : {
    3776          126 :   if (e && e->expr_type == EXPR_FUNCTION
    3777            6 :       && !gfc_pure_function (e, &impure_name)
    3778          129 :       && !gfc_implicit_pure_function (e))
    3779            3 :     return true;
    3780              : 
    3781              :   return false;
    3782              : }
    3783              : 
    3784              : static bool
    3785          110 : gfc_expr_contains_impure_fcn (gfc_expr *e)
    3786              : {
    3787          110 :   impure_name = NULL;
    3788          110 :   return gfc_traverse_expr (e, NULL, &expr_contains_impure_fcn, 0);
    3789              : }
    3790              : 
    3791              : 
    3792              : /* Generate code for bounds checking for elemental dimensions.  */
    3793              : 
    3794              : static void
    3795         6843 : array_bound_check_elemental (stmtblock_t *block, gfc_ss * ss, gfc_expr * expr)
    3796              : {
    3797         6843 :   gfc_array_ref *ar;
    3798         6843 :   gfc_ref *ref;
    3799         6843 :   char *var_name = NULL;
    3800         6843 :   int dim;
    3801              : 
    3802         6843 :   if (expr->expr_type == EXPR_VARIABLE)
    3803              :     {
    3804        12843 :       for (ref = expr->ref; ref; ref = ref->next)
    3805              :         {
    3806         6458 :           if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    3807              :             {
    3808         4014 :               ar = &ref->u.ar;
    3809         4014 :               var_name = abridged_ref_name (expr, ar);
    3810        12317 :               for (dim = 0; dim < ar->dimen; dim++)
    3811              :                 {
    3812         4289 :                   if (ar->dimen_type[dim] == DIMEN_ELEMENT)
    3813              :                     {
    3814          110 :                       if (gfc_expr_contains_impure_fcn (ar->start[dim]))
    3815            3 :                         gfc_warning_now (0, "Bounds checking of the elemental "
    3816              :                                          "index at %L will cause two calls to "
    3817              :                                          "%qs, which is not declared to be "
    3818              :                                          "PURE or is not implicitly pure.",
    3819            3 :                                          &ar->start[dim]->where, impure_name);
    3820          110 :                       gfc_se indexse;
    3821          110 :                       gfc_init_se (&indexse, NULL);
    3822          110 :                       gfc_conv_expr_type (&indexse, ar->start[dim],
    3823              :                                           gfc_array_index_type);
    3824          110 :                       gfc_add_block_to_block (block, &indexse.pre);
    3825          110 :                       trans_array_bound_check (block, ss, indexse.expr, dim,
    3826              :                                                &ar->where,
    3827          110 :                                                ar->as->type != AS_ASSUMED_SIZE
    3828           18 :                                                || dim < ar->dimen - 1,
    3829              :                                                var_name);
    3830              :                     }
    3831              :                 }
    3832         4014 :               free (var_name);
    3833              :             }
    3834              :         }
    3835              :     }
    3836         6843 : }
    3837              : 
    3838              : 
    3839              : /* Return the offset for an index.  Performs bound checking for elemental
    3840              :    dimensions.  Single element references are processed separately.
    3841              :    DIM is the array dimension, I is the loop dimension.  */
    3842              : 
    3843              : static tree
    3844       259412 : conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
    3845              :                          gfc_array_ref * ar, tree stride)
    3846              : {
    3847       259412 :   gfc_array_info *info;
    3848       259412 :   tree index;
    3849       259412 :   tree desc;
    3850       259412 :   tree data;
    3851              : 
    3852       259412 :   info = &ss->info->data.array;
    3853              : 
    3854              :   /* Get the index into the array for this dimension.  */
    3855       259412 :   if (ar)
    3856              :     {
    3857       183479 :       gcc_assert (ar->type != AR_ELEMENT);
    3858       183479 :       switch (ar->dimen_type[dim])
    3859              :         {
    3860            0 :         case DIMEN_THIS_IMAGE:
    3861            0 :           gcc_unreachable ();
    3862         4642 :           break;
    3863         4642 :         case DIMEN_ELEMENT:
    3864              :           /* Elemental dimension.  */
    3865         4642 :           gcc_assert (info->subscript[dim]
    3866              :                       && info->subscript[dim]->info->type == GFC_SS_SCALAR);
    3867              :           /* We've already translated this value outside the loop.  */
    3868         4642 :           index = info->subscript[dim]->info->data.scalar.value;
    3869              : 
    3870         9341 :           index = trans_array_bound_check (&se->pre, ss, index, dim, &ar->where,
    3871         4642 :                                            ar->as->type != AS_ASSUMED_SIZE
    3872           57 :                                            || dim < ar->dimen - 1);
    3873         4642 :           break;
    3874              : 
    3875          984 :         case DIMEN_VECTOR:
    3876          984 :           gcc_assert (info && se->loop);
    3877          984 :           gcc_assert (info->subscript[dim]
    3878              :                       && info->subscript[dim]->info->type == GFC_SS_VECTOR);
    3879          984 :           desc = info->subscript[dim]->info->data.array.descriptor;
    3880              : 
    3881              :           /* Get a zero-based index into the vector.  */
    3882          984 :           index = fold_build2_loc (input_location, MINUS_EXPR,
    3883              :                                    gfc_array_index_type,
    3884              :                                    se->loop->loopvar[i], se->loop->from[i]);
    3885              : 
    3886              :           /* Multiply the index by the stride.  */
    3887          984 :           index = fold_build2_loc (input_location, MULT_EXPR,
    3888              :                                    gfc_array_index_type,
    3889              :                                    index, gfc_conv_array_stride (desc, 0));
    3890              : 
    3891              :           /* Read the vector to get an index into info->descriptor.  */
    3892          984 :           data = build_fold_indirect_ref_loc (input_location,
    3893              :                                           gfc_conv_array_data (desc));
    3894          984 :           index = gfc_build_array_ref (data, index, NULL);
    3895          984 :           index = gfc_evaluate_now (index, &se->pre);
    3896          984 :           index = fold_convert (gfc_array_index_type, index);
    3897              : 
    3898              :           /* Do any bounds checking on the final info->descriptor index.  */
    3899         1974 :           index = trans_array_bound_check (&se->pre, ss, index, dim, &ar->where,
    3900          984 :                                            ar->as->type != AS_ASSUMED_SIZE
    3901            6 :                                            || dim < ar->dimen - 1);
    3902          984 :           break;
    3903              : 
    3904       177853 :         case DIMEN_RANGE:
    3905              :           /* Scalarized dimension.  */
    3906       177853 :           gcc_assert (info && se->loop);
    3907              : 
    3908              :           /* Multiply the loop variable by the stride and delta.  */
    3909       177853 :           index = se->loop->loopvar[i];
    3910       177853 :           if (!integer_onep (info->stride[dim]))
    3911         6996 :             index = fold_build2_loc (input_location, MULT_EXPR,
    3912              :                                      gfc_array_index_type, index,
    3913              :                                      info->stride[dim]);
    3914       177853 :           if (!integer_zerop (info->delta[dim]))
    3915        68600 :             index = fold_build2_loc (input_location, PLUS_EXPR,
    3916              :                                      gfc_array_index_type, index,
    3917              :                                      info->delta[dim]);
    3918              :           break;
    3919              : 
    3920            0 :         default:
    3921            0 :           gcc_unreachable ();
    3922              :         }
    3923              :     }
    3924              :   else
    3925              :     {
    3926              :       /* Temporary array or derived type component.  */
    3927        75933 :       gcc_assert (se->loop);
    3928        75933 :       index = se->loop->loopvar[se->loop->order[i]];
    3929              : 
    3930              :       /* Pointer functions can have stride[0] different from unity.
    3931              :          Use the stride returned by the function call and stored in
    3932              :          the descriptor for the temporary.  */
    3933        75933 :       if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
    3934         8212 :           && se->ss->info->expr
    3935         8212 :           && se->ss->info->expr->symtree
    3936         8212 :           && se->ss->info->expr->symtree->n.sym->result
    3937         7772 :           && se->ss->info->expr->symtree->n.sym->result->attr.pointer)
    3938          174 :         stride = gfc_conv_descriptor_stride_get (info->descriptor,
    3939              :                                                  gfc_rank_cst[dim]);
    3940              : 
    3941        75933 :       if (info->delta[dim] && !integer_zerop (info->delta[dim]))
    3942          858 :         index = fold_build2_loc (input_location, PLUS_EXPR,
    3943              :                                  gfc_array_index_type, index, info->delta[dim]);
    3944              :     }
    3945              : 
    3946              :   /* Multiply by the stride.  */
    3947       259412 :   if (stride != NULL && !integer_onep (stride))
    3948        79219 :     index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    3949              :                              index, stride);
    3950              : 
    3951       259412 :   return index;
    3952              : }
    3953              : 
    3954              : 
    3955              : /* Build a scalarized array reference using the vptr 'size'.  */
    3956              : 
    3957              : static bool
    3958       199645 : build_class_array_ref (gfc_se *se, tree base, tree index)
    3959              : {
    3960       199645 :   tree size;
    3961       199645 :   tree decl = NULL_TREE;
    3962       199645 :   tree tmp;
    3963       199645 :   gfc_expr *expr = se->ss->info->expr;
    3964       199645 :   gfc_expr *class_expr;
    3965       199645 :   gfc_typespec *ts;
    3966       199645 :   gfc_symbol *sym;
    3967              : 
    3968       199645 :   tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
    3969              : 
    3970        94446 :   if (tmp != NULL_TREE)
    3971              :     decl = tmp;
    3972              :   else
    3973              :     {
    3974              :       /* The base expression does not contain a class component, either
    3975              :          because it is a temporary array or array descriptor.  Class
    3976              :          array functions are correctly resolved above.  */
    3977       195934 :       if (!expr
    3978       195934 :           || (expr->ts.type != BT_CLASS
    3979       181411 :               && !gfc_is_class_array_ref (expr, NULL)))
    3980              :         return false;
    3981              : 
    3982              :       /* Obtain the expression for the class entity or component that is
    3983              :          followed by an array reference, which is not an element, so that
    3984              :          the span of the array can be obtained.  */
    3985          494 :       class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
    3986              : 
    3987          494 :       if (!ts)
    3988              :         return false;
    3989              : 
    3990          494 :       sym = (!class_expr && expr) ? expr->symtree->n.sym : NULL;
    3991            0 :       if (sym && sym->attr.function
    3992            0 :           && sym == sym->result
    3993            0 :           && sym->backend_decl == current_function_decl)
    3994              :         /* The temporary is the data field of the class data component
    3995              :            of the current function.  */
    3996            0 :         decl = gfc_get_fake_result_decl (sym, 0);
    3997          494 :       else if (sym)
    3998              :         {
    3999            0 :           if (decl == NULL_TREE)
    4000            0 :             decl = expr->symtree->n.sym->backend_decl;
    4001              :           /* For class arrays the tree containing the class is stored in
    4002              :              GFC_DECL_SAVED_DESCRIPTOR of the sym's backend_decl.
    4003              :              For all others it's sym's backend_decl directly.  */
    4004            0 :           if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
    4005            0 :             decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    4006              :         }
    4007              :       else
    4008          494 :         decl = gfc_get_class_from_gfc_expr (class_expr);
    4009              : 
    4010          494 :       if (POINTER_TYPE_P (TREE_TYPE (decl)))
    4011            0 :         decl = build_fold_indirect_ref_loc (input_location, decl);
    4012              : 
    4013          494 :       if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
    4014              :         return false;
    4015              :     }
    4016              : 
    4017         4205 :   se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
    4018              : 
    4019         4205 :   size = gfc_class_vtab_size_get (decl);
    4020              :   /* For unlimited polymorphic entities then _len component needs to be
    4021              :      multiplied with the size.  */
    4022         4205 :   size = gfc_resize_class_size_with_len (&se->pre, decl, size);
    4023         4205 :   size = fold_convert (TREE_TYPE (index), size);
    4024              : 
    4025              :   /* Return the element in the se expression.  */
    4026         4205 :   se->expr = gfc_build_spanned_array_ref (base, index, size);
    4027         4205 :   return true;
    4028              : }
    4029              : 
    4030              : 
    4031              : /* Indicates that the tree EXPR is a reference to an array that can’t
    4032              :    have any negative stride.  */
    4033              : 
    4034              : static bool
    4035       330974 : non_negative_strides_array_p (tree expr)
    4036              : {
    4037       345371 :   if (expr == NULL_TREE)
    4038              :     return false;
    4039              : 
    4040       345371 :   tree type = TREE_TYPE (expr);
    4041       345371 :   if (POINTER_TYPE_P (type))
    4042        80563 :     type = TREE_TYPE (type);
    4043              : 
    4044       345371 :   if (TYPE_LANG_SPECIFIC (type))
    4045              :     {
    4046       345371 :       gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
    4047              : 
    4048       345371 :       if (array_kind == GFC_ARRAY_ALLOCATABLE
    4049       345371 :           || array_kind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
    4050              :         return true;
    4051              :     }
    4052              : 
    4053              :   /* An array with descriptor can have negative strides.
    4054              :      We try to be conservative and return false by default here
    4055              :      if we don’t recognize a contiguous array instead of
    4056              :      returning false if we can identify a non-contiguous one.  */
    4057       286882 :   if (!GFC_ARRAY_TYPE_P (type))
    4058              :     return false;
    4059              : 
    4060              :   /* If the array was originally a dummy with a descriptor, strides can be
    4061              :      negative.  */
    4062       250204 :   if (DECL_P (expr)
    4063       240868 :       && DECL_LANG_SPECIFIC (expr)
    4064        49371 :       && GFC_DECL_SAVED_DESCRIPTOR (expr)
    4065       264620 :       && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
    4066        14397 :     return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
    4067              : 
    4068              :   return true;
    4069              : }
    4070              : 
    4071              : 
    4072              : /* Build a scalarized reference to an array.  */
    4073              : 
    4074              : static void
    4075       199645 : gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
    4076              :                                bool tmp_array = false)
    4077              : {
    4078       199645 :   gfc_array_info *info;
    4079       199645 :   tree decl = NULL_TREE;
    4080       199645 :   tree index;
    4081       199645 :   tree base;
    4082       199645 :   gfc_ss *ss;
    4083       199645 :   gfc_expr *expr;
    4084       199645 :   int n;
    4085              : 
    4086       199645 :   ss = se->ss;
    4087       199645 :   expr = ss->info->expr;
    4088       199645 :   info = &ss->info->data.array;
    4089       199645 :   if (ar)
    4090       135996 :     n = se->loop->order[0];
    4091              :   else
    4092              :     n = 0;
    4093              : 
    4094       199645 :   index = conv_array_index_offset (se, ss, ss->dim[n], n, ar, info->stride0);
    4095              :   /* Add the offset for this dimension to the stored offset for all other
    4096              :      dimensions.  */
    4097       199645 :   if (info->offset && !integer_zerop (info->offset))
    4098       145971 :     index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4099              :                              index, info->offset);
    4100              : 
    4101       199645 :   base = build_fold_indirect_ref_loc (input_location, info->data);
    4102              : 
    4103              :   /* Use the vptr 'size' field to access the element of a class array.  */
    4104       199645 :   if (build_class_array_ref (se, base, index))
    4105         4205 :     return;
    4106              : 
    4107       195440 :   if (get_CFI_desc (NULL, expr, &decl, ar))
    4108          442 :     decl = build_fold_indirect_ref_loc (input_location, decl);
    4109              : 
    4110              :   /* A pointer array component can be detected from its field decl. Fix
    4111              :      the descriptor, mark the resulting variable decl and pass it to
    4112              :      gfc_build_array_ref.  */
    4113       195440 :   if (is_span_addressed_array (info->descriptor)
    4114       195440 :       || (expr && ((expr->ts.deferred && info->descriptor
    4115         2842 :                     && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor)))
    4116       195440 :                    || (expr && gfc_expr_attr (expr).pdt_string))))
    4117              :     {
    4118         9967 :       if (TREE_CODE (info->descriptor) == COMPONENT_REF)
    4119         1648 :         decl = info->descriptor;
    4120         8319 :       else if (INDIRECT_REF_P (info->descriptor))
    4121         1495 :         decl = TREE_OPERAND (info->descriptor, 0);
    4122              : 
    4123         9967 :       if (decl == NULL_TREE)
    4124         6824 :         decl = info->descriptor;
    4125              :     }
    4126              : 
    4127       195440 :   bool non_negative_stride = tmp_array
    4128       195440 :                              || non_negative_strides_array_p (info->descriptor);
    4129       195440 :   se->expr = gfc_build_array_ref (base, index, decl,
    4130              :                                   non_negative_stride);
    4131              : }
    4132              : 
    4133              : 
    4134              : /* Translate access of temporary array.  */
    4135              : 
    4136              : void
    4137        63649 : gfc_conv_tmp_array_ref (gfc_se * se)
    4138              : {
    4139        63649 :   se->string_length = se->ss->info->string_length;
    4140        63649 :   gfc_conv_scalarized_array_ref (se, NULL, true);
    4141        63649 :   gfc_advance_se_ss_chain (se);
    4142        63649 : }
    4143              : 
    4144              : /* Add T to the offset pair *OFFSET, *CST_OFFSET.  */
    4145              : 
    4146              : static void
    4147       294831 : add_to_offset (tree *cst_offset, tree *offset, tree t)
    4148              : {
    4149       294831 :   if (TREE_CODE (t) == INTEGER_CST)
    4150       148540 :     *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
    4151              :   else
    4152              :     {
    4153       146291 :       if (!integer_zerop (*offset))
    4154        49823 :         *offset = fold_build2_loc (input_location, PLUS_EXPR,
    4155              :                                    gfc_array_index_type, *offset, t);
    4156              :       else
    4157        96468 :         *offset = t;
    4158              :     }
    4159       294831 : }
    4160              : 
    4161              : 
    4162              : static tree
    4163       198747 : build_array_ref (tree desc, tree offset, tree decl, tree vptr)
    4164              : {
    4165       198747 :   tree tmp;
    4166       198747 :   tree type;
    4167       198747 :   tree cdesc;
    4168              : 
    4169              :   /* For class arrays the class declaration is stored in the saved
    4170              :      descriptor.  */
    4171       198747 :   if (INDIRECT_REF_P (desc)
    4172         7439 :       && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
    4173       201140 :       && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
    4174          938 :     cdesc = gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (
    4175              :                                   TREE_OPERAND (desc, 0)));
    4176              :   else
    4177              :     cdesc = desc;
    4178              : 
    4179              :   /* Class container types do not always have the GFC_CLASS_TYPE_P
    4180              :      but the canonical type does.  */
    4181       198747 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
    4182       198747 :       && TREE_CODE (cdesc) == COMPONENT_REF)
    4183              :     {
    4184        12312 :       type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
    4185        12312 :       if (TYPE_CANONICAL (type)
    4186        12312 :           && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
    4187              :         {
    4188         4034 :           vptr = gfc_class_vptr_get (TREE_OPERAND (cdesc, 0));
    4189              :           /* Pass the class container as decl so that gfc_build_array_ref can
    4190              :              correct the element size for an unlimited polymorphic character
    4191              :              payload (the _len field), which the vptr size alone omits.  Only do
    4192              :              this for a genuine array element reference; a scalar coarray has
    4193              :              nothing to span-correct and gfc_build_array_ref asserts decl is null
    4194              :              for it.  */
    4195         4034 :           if (decl == NULL_TREE
    4196         4034 :               && GFC_TYPE_ARRAY_RANK (TREE_TYPE (cdesc)) > 0)
    4197         3727 :             decl = TREE_OPERAND (cdesc, 0);
    4198              :         }
    4199              :     }
    4200              : 
    4201       198747 :   if (decl == NULL_TREE
    4202       198747 :       && is_span_addressed_array (desc))
    4203              :     decl = desc;
    4204              : 
    4205       198747 :   tmp = gfc_conv_array_data (desc);
    4206       198747 :   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    4207       198747 :   tmp = gfc_build_array_ref (tmp, offset, decl,
    4208              :                              non_negative_strides_array_p (desc),
    4209              :                              vptr);
    4210       198747 :   return tmp;
    4211              : }
    4212              : 
    4213              : 
    4214              : /* Build an array reference.  se->expr already holds the array descriptor.
    4215              :    This should be either a variable, indirect variable reference or component
    4216              :    reference.  For arrays which do not have a descriptor, se->expr will be
    4217              :    the data pointer.
    4218              :    a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
    4219              : 
    4220              : void
    4221       274295 : gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
    4222              :                     locus * where)
    4223              : {
    4224       274295 :   int n;
    4225       274295 :   tree offset, cst_offset;
    4226       274295 :   tree tmp;
    4227       274295 :   tree stride;
    4228       274295 :   tree decl = NULL_TREE;
    4229       274295 :   gfc_se indexse;
    4230       274295 :   gfc_se tmpse;
    4231       274295 :   gfc_symbol * sym = expr->symtree->n.sym;
    4232       274295 :   char *var_name = NULL;
    4233              : 
    4234       274295 :   if (ar->stat)
    4235              :     {
    4236            3 :       gfc_se statse;
    4237              : 
    4238            3 :       gfc_init_se (&statse, NULL);
    4239            3 :       gfc_conv_expr_lhs (&statse, ar->stat);
    4240            3 :       gfc_add_block_to_block (&se->pre, &statse.pre);
    4241            3 :       gfc_add_modify (&se->pre, statse.expr, integer_zero_node);
    4242              :     }
    4243       274295 :   if (ar->dimen == 0)
    4244              :     {
    4245         4839 :       gcc_assert (ar->codimen || sym->attr.select_rank_temporary
    4246              :                   || (ar->as && ar->as->corank));
    4247              : 
    4248         4839 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
    4249         1248 :         se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
    4250              :       else
    4251              :         {
    4252         3591 :           if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
    4253         3591 :               && TREE_CODE (TREE_TYPE (se->expr)) == POINTER_TYPE)
    4254         2626 :             se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    4255              : 
    4256              :           /* Use the actual tree type and not the wrapped coarray.  */
    4257         3591 :           if (!se->want_pointer)
    4258         2609 :             se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
    4259              :                                      se->expr);
    4260              :         }
    4261              : 
    4262       140835 :       return;
    4263              :     }
    4264              : 
    4265              :   /* Handle scalarized references separately.  */
    4266       269456 :   if (ar->type != AR_ELEMENT)
    4267              :     {
    4268       135996 :       gfc_conv_scalarized_array_ref (se, ar);
    4269       135996 :       gfc_advance_se_ss_chain (se);
    4270       135996 :       return;
    4271              :     }
    4272              : 
    4273       133460 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    4274        11909 :     var_name = abridged_ref_name (expr, ar);
    4275              : 
    4276       133460 :   decl = se->expr;
    4277       133460 :   if (UNLIMITED_POLY(sym)
    4278          104 :       && IS_CLASS_ARRAY (sym)
    4279          103 :       && sym->attr.dummy
    4280           60 :       && ar->as->type != AS_DEFERRED)
    4281           48 :     decl = sym->backend_decl;
    4282              : 
    4283       133460 :   cst_offset = offset = gfc_index_zero_node;
    4284       133460 :   add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
    4285              : 
    4286              :   /* Calculate the offsets from all the dimensions.  Make sure to associate
    4287              :      the final offset so that we form a chain of loop invariant summands.  */
    4288       294831 :   for (n = ar->dimen - 1; n >= 0; n--)
    4289              :     {
    4290              :       /* Calculate the index for this dimension.  */
    4291       161371 :       gfc_init_se (&indexse, se);
    4292       161371 :       gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
    4293       161371 :       gfc_add_block_to_block (&se->pre, &indexse.pre);
    4294              : 
    4295       161371 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
    4296              :         {
    4297              :           /* Check array bounds.  */
    4298        15449 :           tree cond;
    4299        15449 :           char *msg;
    4300              : 
    4301              :           /* Evaluate the indexse.expr only once.  */
    4302        15449 :           indexse.expr = save_expr (indexse.expr);
    4303              : 
    4304              :           /* Lower bound.  */
    4305        15449 :           tmp = gfc_conv_array_lbound (decl, n);
    4306        15449 :           if (sym->attr.temporary)
    4307              :             {
    4308           18 :               gfc_init_se (&tmpse, se);
    4309           18 :               gfc_conv_expr_type (&tmpse, ar->as->lower[n],
    4310              :                                   gfc_array_index_type);
    4311           18 :               gfc_add_block_to_block (&se->pre, &tmpse.pre);
    4312           18 :               tmp = tmpse.expr;
    4313              :             }
    4314              : 
    4315        15449 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4316              :                                   indexse.expr, tmp);
    4317        15449 :           msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    4318              :                            "below lower bound of %%ld", n+1, var_name);
    4319        15449 :           gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
    4320              :                                    fold_convert (long_integer_type_node,
    4321              :                                                  indexse.expr),
    4322              :                                    fold_convert (long_integer_type_node, tmp));
    4323        15449 :           free (msg);
    4324              : 
    4325              :           /* Upper bound, but not for the last dimension of assumed-size
    4326              :              arrays.  */
    4327        15449 :           if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
    4328              :             {
    4329        13716 :               tmp = gfc_conv_array_ubound (decl, n);
    4330        13716 :               if (sym->attr.temporary)
    4331              :                 {
    4332           18 :                   gfc_init_se (&tmpse, se);
    4333           18 :                   gfc_conv_expr_type (&tmpse, ar->as->upper[n],
    4334              :                                       gfc_array_index_type);
    4335           18 :                   gfc_add_block_to_block (&se->pre, &tmpse.pre);
    4336           18 :                   tmp = tmpse.expr;
    4337              :                 }
    4338              : 
    4339        13716 :               cond = fold_build2_loc (input_location, GT_EXPR,
    4340              :                                       logical_type_node, indexse.expr, tmp);
    4341        13716 :               msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    4342              :                                "above upper bound of %%ld", n+1, var_name);
    4343        13716 :               gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
    4344              :                                    fold_convert (long_integer_type_node,
    4345              :                                                  indexse.expr),
    4346              :                                    fold_convert (long_integer_type_node, tmp));
    4347        13716 :               free (msg);
    4348              :             }
    4349              :         }
    4350              : 
    4351              :       /* Multiply the index by the stride.  */
    4352       161371 :       stride = gfc_conv_array_stride (decl, n);
    4353       161371 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    4354              :                              indexse.expr, stride);
    4355              : 
    4356              :       /* And add it to the total.  */
    4357       161371 :       add_to_offset (&cst_offset, &offset, tmp);
    4358              :     }
    4359              : 
    4360       133460 :   if (!integer_zerop (cst_offset))
    4361        70074 :     offset = fold_build2_loc (input_location, PLUS_EXPR,
    4362              :                               gfc_array_index_type, offset, cst_offset);
    4363              : 
    4364              :   /* A pointer array component can be detected from its field decl. Fix
    4365              :      the descriptor, mark the resulting variable decl and pass it to
    4366              :      build_array_ref.  */
    4367       133460 :   decl = NULL_TREE;
    4368       133460 :   if (get_CFI_desc (sym, expr, &decl, ar))
    4369         3589 :     decl = build_fold_indirect_ref_loc (input_location, decl);
    4370       132317 :   if (!expr->ts.deferred && !sym->attr.codimension
    4371       263537 :       && is_span_addressed_array (se->expr))
    4372              :     {
    4373         5424 :       if (INDIRECT_REF_P (se->expr))
    4374          992 :         decl = TREE_OPERAND (se->expr, 0);
    4375              :       else
    4376         4432 :         decl = se->expr;
    4377              :     }
    4378       128036 :   else if (expr->ts.deferred
    4379       126893 :            || (sym->ts.type == BT_CHARACTER
    4380        15479 :                && sym->attr.select_type_temporary)
    4381       253213 :            || (expr->ts.type == BT_CHARACTER
    4382        15922 :                && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr))
    4383         5392 :                && gfc_expr_attr (expr).pdt_string))
    4384              :     {
    4385         2973 :       decl = se->expr;
    4386         2973 :       if (INDIRECT_REF_P (decl))
    4387           20 :         decl = TREE_OPERAND (decl, 0);
    4388              :     }
    4389       125063 :   else if (sym->ts.type == BT_CLASS)
    4390              :     {
    4391         2537 :       if (UNLIMITED_POLY (sym))
    4392              :         {
    4393          103 :           gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
    4394          103 :           gfc_init_se (&tmpse, NULL);
    4395          103 :           gfc_conv_expr (&tmpse, class_expr);
    4396          103 :           if (!se->class_vptr)
    4397          103 :             se->class_vptr = gfc_class_vptr_get (tmpse.expr);
    4398          103 :           gfc_free_expr (class_expr);
    4399          103 :           decl = tmpse.expr;
    4400          103 :         }
    4401              :       else
    4402         2434 :         decl = NULL_TREE;
    4403              :     }
    4404              : 
    4405       133460 :   free (var_name);
    4406       133460 :   se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
    4407              : }
    4408              : 
    4409              : 
    4410              : /* Add the offset corresponding to array's ARRAY_DIM dimension and loop's
    4411              :    LOOP_DIM dimension (if any) to array's offset.  */
    4412              : 
    4413              : static void
    4414        59767 : add_array_offset (stmtblock_t *pblock, gfc_loopinfo *loop, gfc_ss *ss,
    4415              :                   gfc_array_ref *ar, int array_dim, int loop_dim)
    4416              : {
    4417        59767 :   gfc_se se;
    4418        59767 :   gfc_array_info *info;
    4419        59767 :   tree stride, index;
    4420              : 
    4421        59767 :   info = &ss->info->data.array;
    4422              : 
    4423        59767 :   gfc_init_se (&se, NULL);
    4424        59767 :   se.loop = loop;
    4425        59767 :   se.expr = info->descriptor;
    4426        59767 :   stride = gfc_conv_array_stride (info->descriptor, array_dim);
    4427        59767 :   index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
    4428        59767 :   gfc_add_block_to_block (pblock, &se.pre);
    4429              : 
    4430        59767 :   info->offset = fold_build2_loc (input_location, PLUS_EXPR,
    4431              :                                   gfc_array_index_type,
    4432              :                                   info->offset, index);
    4433        59767 :   info->offset = gfc_evaluate_now (info->offset, pblock);
    4434        59767 : }
    4435              : 
    4436              : 
    4437              : /* Generate the code to be executed immediately before entering a
    4438              :    scalarization loop.  */
    4439              : 
    4440              : static void
    4441       150526 : gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
    4442              :                          stmtblock_t * pblock)
    4443              : {
    4444       150526 :   tree stride;
    4445       150526 :   gfc_ss_info *ss_info;
    4446       150526 :   gfc_array_info *info;
    4447       150526 :   gfc_ss_type ss_type;
    4448       150526 :   gfc_ss *ss, *pss;
    4449       150526 :   gfc_loopinfo *ploop;
    4450       150526 :   gfc_array_ref *ar;
    4451              : 
    4452              :   /* This code will be executed before entering the scalarization loop
    4453              :      for this dimension.  */
    4454       458049 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4455              :     {
    4456       307523 :       ss_info = ss->info;
    4457              : 
    4458       307523 :       if ((ss_info->useflags & flag) == 0)
    4459         1477 :         continue;
    4460              : 
    4461       306046 :       ss_type = ss_info->type;
    4462       373136 :       if (ss_type != GFC_SS_SECTION
    4463              :           && ss_type != GFC_SS_FUNCTION
    4464       306046 :           && ss_type != GFC_SS_CONSTRUCTOR
    4465       306046 :           && ss_type != GFC_SS_COMPONENT)
    4466        67090 :         continue;
    4467              : 
    4468       238956 :       info = &ss_info->data.array;
    4469              : 
    4470       238956 :       gcc_assert (dim < ss->dimen);
    4471       238956 :       gcc_assert (ss->dimen == loop->dimen);
    4472              : 
    4473       238956 :       if (info->ref)
    4474       167654 :         ar = &info->ref->u.ar;
    4475              :       else
    4476              :         ar = NULL;
    4477              : 
    4478       238956 :       if (dim == loop->dimen - 1 && loop->parent != NULL)
    4479              :         {
    4480              :           /* If we are in the outermost dimension of this loop, the previous
    4481              :              dimension shall be in the parent loop.  */
    4482         4687 :           gcc_assert (ss->parent != NULL);
    4483              : 
    4484         4687 :           pss = ss->parent;
    4485         4687 :           ploop = loop->parent;
    4486              : 
    4487              :           /* ss and ss->parent are about the same array.  */
    4488         4687 :           gcc_assert (ss_info == pss->info);
    4489              :         }
    4490              :       else
    4491              :         {
    4492              :           ploop = loop;
    4493              :           pss = ss;
    4494              :         }
    4495              : 
    4496       238956 :       if (dim == loop->dimen - 1 && loop->parent == NULL)
    4497              :         {
    4498       183831 :           gcc_assert (0 == ploop->order[0]);
    4499              : 
    4500       367662 :           stride = gfc_conv_array_stride (info->descriptor,
    4501       183831 :                                           innermost_ss (ss)->dim[0]);
    4502              : 
    4503              :           /* Calculate the stride of the innermost loop.  Hopefully this will
    4504              :              allow the backend optimizers to do their stuff more effectively.
    4505              :            */
    4506       183831 :           info->stride0 = gfc_evaluate_now (stride, pblock);
    4507              : 
    4508              :           /* For the outermost loop calculate the offset due to any
    4509              :              elemental dimensions.  It will have been initialized with the
    4510              :              base offset of the array.  */
    4511       183831 :           if (info->ref)
    4512              :             {
    4513       294702 :               for (int i = 0; i < ar->dimen; i++)
    4514              :                 {
    4515       169889 :                   if (ar->dimen_type[i] != DIMEN_ELEMENT)
    4516       165247 :                     continue;
    4517              : 
    4518         4642 :                   add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
    4519              :                 }
    4520              :             }
    4521              :         }
    4522              :       else
    4523              :         {
    4524        55125 :           int i;
    4525              : 
    4526        55125 :           if (dim == loop->dimen - 1)
    4527              :             i = 0;
    4528              :           else
    4529        50438 :             i = dim + 1;
    4530              : 
    4531              :           /* For the time being, there is no loop reordering.  */
    4532        55125 :           gcc_assert (i == ploop->order[i]);
    4533        55125 :           i = ploop->order[i];
    4534              : 
    4535              :           /* Add the offset for the previous loop dimension.  */
    4536        55125 :           add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
    4537              :         }
    4538              : 
    4539              :       /* Remember this offset for the second loop.  */
    4540       238956 :       if (dim == loop->temp_dim - 1 && loop->parent == NULL)
    4541        56293 :         info->saved_offset = info->offset;
    4542              :     }
    4543       150526 : }
    4544              : 
    4545              : 
    4546              : /* Start a scalarized expression.  Creates a scope and declares loop
    4547              :    variables.  */
    4548              : 
    4549              : void
    4550       119917 : gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
    4551              : {
    4552       119917 :   int dim;
    4553       119917 :   int n;
    4554       119917 :   int flags;
    4555              : 
    4556       119917 :   gcc_assert (!loop->array_parameter);
    4557              : 
    4558       268846 :   for (dim = loop->dimen - 1; dim >= 0; dim--)
    4559              :     {
    4560       148929 :       n = loop->order[dim];
    4561              : 
    4562       148929 :       gfc_start_block (&loop->code[n]);
    4563              : 
    4564              :       /* Create the loop variable.  */
    4565       148929 :       loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
    4566              : 
    4567       148929 :       if (dim < loop->temp_dim)
    4568              :         flags = 3;
    4569              :       else
    4570       101956 :         flags = 1;
    4571              :       /* Calculate values that will be constant within this loop.  */
    4572       148929 :       gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
    4573              :     }
    4574       119917 :   gfc_start_block (pbody);
    4575       119917 : }
    4576              : 
    4577              : 
    4578              : /* Generates the actual loop code for a scalarization loop.  */
    4579              : 
    4580              : static void
    4581       165739 : gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
    4582              :                                stmtblock_t * pbody)
    4583              : {
    4584       165739 :   stmtblock_t block;
    4585       165739 :   tree cond;
    4586       165739 :   tree tmp;
    4587       165739 :   tree loopbody;
    4588       165739 :   tree exit_label;
    4589       165739 :   tree stmt;
    4590       165739 :   tree init;
    4591       165739 :   tree incr;
    4592              : 
    4593       165739 :   if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
    4594              :                       | OMPWS_SCALARIZER_BODY))
    4595              :       == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
    4596           80 :       && n == loop->dimen - 1)
    4597              :     {
    4598              :       /* We create an OMP_FOR construct for the outermost scalarized loop.  */
    4599           65 :       init = make_tree_vec (1);
    4600           65 :       cond = make_tree_vec (1);
    4601           65 :       incr = make_tree_vec (1);
    4602              : 
    4603              :       /* Cycle statement is implemented with a goto.  Exit statement must not
    4604              :          be present for this loop.  */
    4605           65 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4606           65 :       TREE_USED (exit_label) = 1;
    4607              : 
    4608              :       /* Label for cycle statements (if needed).  */
    4609           65 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4610           65 :       gfc_add_expr_to_block (pbody, tmp);
    4611              : 
    4612           65 :       stmt = make_node (OMP_FOR);
    4613              : 
    4614           65 :       TREE_TYPE (stmt) = void_type_node;
    4615           65 :       OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
    4616              : 
    4617           65 :       OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
    4618              :                                                  OMP_CLAUSE_SCHEDULE);
    4619           65 :       OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
    4620           65 :         = OMP_CLAUSE_SCHEDULE_STATIC;
    4621           65 :       if (ompws_flags & OMPWS_NOWAIT)
    4622           32 :         OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
    4623           64 :           = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
    4624              : 
    4625              :       /* Initialize the loopvar.  */
    4626           65 :       TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
    4627              :                                          loop->from[n]);
    4628           65 :       OMP_FOR_INIT (stmt) = init;
    4629              :       /* The exit condition.  */
    4630           65 :       TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
    4631              :                                            logical_type_node,
    4632              :                                            loop->loopvar[n], loop->to[n]);
    4633           65 :       SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
    4634           65 :       OMP_FOR_COND (stmt) = cond;
    4635              :       /* Increment the loopvar.  */
    4636           65 :       tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4637              :                         loop->loopvar[n], gfc_index_one_node);
    4638           65 :       TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
    4639              :           void_type_node, loop->loopvar[n], tmp);
    4640           65 :       OMP_FOR_INCR (stmt) = incr;
    4641              : 
    4642           65 :       ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
    4643           65 :       gfc_add_expr_to_block (&loop->code[n], stmt);
    4644              :     }
    4645              :   else
    4646              :     {
    4647       331348 :       bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
    4648       165674 :                              && (loop->temp_ss == NULL);
    4649              : 
    4650       165674 :       loopbody = gfc_finish_block (pbody);
    4651              : 
    4652       165674 :       if (reverse_loop)
    4653          174 :         std::swap (loop->from[n], loop->to[n]);
    4654              : 
    4655              :       /* Initialize the loopvar.  */
    4656       165674 :       if (loop->loopvar[n] != loop->from[n])
    4657       164853 :         gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
    4658              : 
    4659       165674 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4660              : 
    4661              :       /* Generate the loop body.  */
    4662       165674 :       gfc_init_block (&block);
    4663              : 
    4664              :       /* The exit condition.  */
    4665       331174 :       cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
    4666              :                           logical_type_node, loop->loopvar[n], loop->to[n]);
    4667       165674 :       tmp = build1_v (GOTO_EXPR, exit_label);
    4668       165674 :       TREE_USED (exit_label) = 1;
    4669       165674 :       tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    4670       165674 :       gfc_add_expr_to_block (&block, tmp);
    4671              : 
    4672              :       /* The main body.  */
    4673       165674 :       gfc_add_expr_to_block (&block, loopbody);
    4674              : 
    4675              :       /* Increment the loopvar.  */
    4676       331174 :       tmp = fold_build2_loc (input_location,
    4677              :                              reverse_loop ? MINUS_EXPR : PLUS_EXPR,
    4678              :                              gfc_array_index_type, loop->loopvar[n],
    4679              :                              gfc_index_one_node);
    4680              : 
    4681       165674 :       gfc_add_modify (&block, loop->loopvar[n], tmp);
    4682              : 
    4683              :       /* Build the loop.  */
    4684       165674 :       tmp = gfc_finish_block (&block);
    4685       165674 :       tmp = build1_v (LOOP_EXPR, tmp);
    4686       165674 :       gfc_add_expr_to_block (&loop->code[n], tmp);
    4687              : 
    4688              :       /* Add the exit label.  */
    4689       165674 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4690       165674 :       gfc_add_expr_to_block (&loop->code[n], tmp);
    4691              :     }
    4692              : 
    4693       165739 : }
    4694              : 
    4695              : 
    4696              : /* Finishes and generates the loops for a scalarized expression.  */
    4697              : 
    4698              : void
    4699       126912 : gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
    4700              : {
    4701       126912 :   int dim;
    4702       126912 :   int n;
    4703       126912 :   gfc_ss *ss;
    4704       126912 :   stmtblock_t *pblock;
    4705       126912 :   tree tmp;
    4706              : 
    4707       126912 :   pblock = body;
    4708              :   /* Generate the loops.  */
    4709       282827 :   for (dim = 0; dim < loop->dimen; dim++)
    4710              :     {
    4711       155915 :       n = loop->order[dim];
    4712       155915 :       gfc_trans_scalarized_loop_end (loop, n, pblock);
    4713       155915 :       loop->loopvar[n] = NULL_TREE;
    4714       155915 :       pblock = &loop->code[n];
    4715              :     }
    4716              : 
    4717       126912 :   tmp = gfc_finish_block (pblock);
    4718       126912 :   gfc_add_expr_to_block (&loop->pre, tmp);
    4719              : 
    4720              :   /* Clear all the used flags.  */
    4721       369588 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4722       242676 :     if (ss->parent == NULL)
    4723       237926 :       ss->info->useflags = 0;
    4724       126912 : }
    4725              : 
    4726              : 
    4727              : /* Finish the main body of a scalarized expression, and start the secondary
    4728              :    copying body.  */
    4729              : 
    4730              : void
    4731         8227 : gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
    4732              : {
    4733         8227 :   int dim;
    4734         8227 :   int n;
    4735         8227 :   stmtblock_t *pblock;
    4736         8227 :   gfc_ss *ss;
    4737              : 
    4738         8227 :   pblock = body;
    4739              :   /* We finish as many loops as are used by the temporary.  */
    4740         9824 :   for (dim = 0; dim < loop->temp_dim - 1; dim++)
    4741              :     {
    4742         1597 :       n = loop->order[dim];
    4743         1597 :       gfc_trans_scalarized_loop_end (loop, n, pblock);
    4744         1597 :       loop->loopvar[n] = NULL_TREE;
    4745         1597 :       pblock = &loop->code[n];
    4746              :     }
    4747              : 
    4748              :   /* We don't want to finish the outermost loop entirely.  */
    4749         8227 :   n = loop->order[loop->temp_dim - 1];
    4750         8227 :   gfc_trans_scalarized_loop_end (loop, n, pblock);
    4751              : 
    4752              :   /* Restore the initial offsets.  */
    4753        23575 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4754              :     {
    4755        15348 :       gfc_ss_type ss_type;
    4756        15348 :       gfc_ss_info *ss_info;
    4757              : 
    4758        15348 :       ss_info = ss->info;
    4759              : 
    4760        15348 :       if ((ss_info->useflags & 2) == 0)
    4761         4538 :         continue;
    4762              : 
    4763        10810 :       ss_type = ss_info->type;
    4764        10964 :       if (ss_type != GFC_SS_SECTION
    4765              :           && ss_type != GFC_SS_FUNCTION
    4766        10810 :           && ss_type != GFC_SS_CONSTRUCTOR
    4767        10810 :           && ss_type != GFC_SS_COMPONENT)
    4768          154 :         continue;
    4769              : 
    4770        10656 :       ss_info->data.array.offset = ss_info->data.array.saved_offset;
    4771              :     }
    4772              : 
    4773              :   /* Restart all the inner loops we just finished.  */
    4774         9824 :   for (dim = loop->temp_dim - 2; dim >= 0; dim--)
    4775              :     {
    4776         1597 :       n = loop->order[dim];
    4777              : 
    4778         1597 :       gfc_start_block (&loop->code[n]);
    4779              : 
    4780         1597 :       loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
    4781              : 
    4782         1597 :       gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
    4783              :     }
    4784              : 
    4785              :   /* Start a block for the secondary copying code.  */
    4786         8227 :   gfc_start_block (body);
    4787         8227 : }
    4788              : 
    4789              : 
    4790              : /* Precalculate (either lower or upper) bound of an array section.
    4791              :      BLOCK: Block in which the (pre)calculation code will go.
    4792              :      BOUNDS[DIM]: Where the bound value will be stored once evaluated.
    4793              :      VALUES[DIM]: Specified bound (NULL <=> unspecified).
    4794              :      DESC: Array descriptor from which the bound will be picked if unspecified
    4795              :        (either lower or upper bound according to LBOUND).  */
    4796              : 
    4797              : static void
    4798       538371 : evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
    4799              :                 tree desc, int dim, bool lbound, bool deferred, bool save_value)
    4800              : {
    4801       538371 :   gfc_se se;
    4802       538371 :   gfc_expr * input_val = values[dim];
    4803       538371 :   tree *output = &bounds[dim];
    4804              : 
    4805       538371 :   if (input_val)
    4806              :     {
    4807              :       /* Specified section bound.  */
    4808        48438 :       gfc_init_se (&se, NULL);
    4809        48438 :       gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
    4810        48438 :       gfc_add_block_to_block (block, &se.pre);
    4811        48438 :       *output = se.expr;
    4812              :     }
    4813       489933 :   else if (deferred && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    4814              :     {
    4815              :       /* The gfc_conv_array_lbound () routine returns a constant zero for
    4816              :          deferred length arrays, which in the scalarizer wreaks havoc, when
    4817              :          copying to a (newly allocated) one-based array.
    4818              :          Keep returning the actual result in sync for both bounds.  */
    4819       194921 :       *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
    4820              :                                                          gfc_rank_cst[dim]):
    4821        65087 :                          gfc_conv_descriptor_ubound_get (desc,
    4822              :                                                          gfc_rank_cst[dim]);
    4823              :     }
    4824              :   else
    4825              :     {
    4826              :       /* No specific bound specified so use the bound of the array.  */
    4827       536706 :       *output = lbound ? gfc_conv_array_lbound (desc, dim) :
    4828       176607 :                          gfc_conv_array_ubound (desc, dim);
    4829              :     }
    4830       538371 :   if (save_value)
    4831       518703 :     *output = gfc_evaluate_now (*output, block);
    4832       538371 : }
    4833              : 
    4834              : 
    4835              : /* Calculate the lower bound of an array section.  */
    4836              : 
    4837              : static void
    4838       269797 : gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
    4839              : {
    4840       269797 :   gfc_expr *stride = NULL;
    4841       269797 :   tree desc;
    4842       269797 :   gfc_se se;
    4843       269797 :   gfc_array_info *info;
    4844       269797 :   gfc_array_ref *ar;
    4845              : 
    4846       269797 :   gcc_assert (ss->info->type == GFC_SS_SECTION);
    4847              : 
    4848       269797 :   info = &ss->info->data.array;
    4849       269797 :   ar = &info->ref->u.ar;
    4850              : 
    4851       269797 :   if (ar->dimen_type[dim] == DIMEN_VECTOR)
    4852              :     {
    4853              :       /* We use a zero-based index to access the vector.  */
    4854          987 :       info->start[dim] = gfc_index_zero_node;
    4855          987 :       info->end[dim] = NULL;
    4856          987 :       info->stride[dim] = gfc_index_one_node;
    4857          987 :       return;
    4858              :     }
    4859              : 
    4860       268810 :   gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
    4861              :               || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
    4862       268810 :   desc = info->descriptor;
    4863       268810 :   stride = ar->stride[dim];
    4864       268810 :   bool save_value = !ss->is_alloc_lhs;
    4865              : 
    4866              :   /* Calculate the start of the range.  For vector subscripts this will
    4867              :      be the range of the vector.  */
    4868       268810 :   evaluate_bound (block, info->start, ar->start, desc, dim, true,
    4869       268810 :                   ar->as->type == AS_DEFERRED, save_value);
    4870              : 
    4871              :   /* Similarly calculate the end.  Although this is not used in the
    4872              :      scalarizer, it is needed when checking bounds and where the end
    4873              :      is an expression with side-effects.  */
    4874       268810 :   evaluate_bound (block, info->end, ar->end, desc, dim, false,
    4875       268810 :                   ar->as->type == AS_DEFERRED, save_value);
    4876              : 
    4877              : 
    4878              :   /* Calculate the stride.  */
    4879       268810 :   if (stride == NULL)
    4880       255856 :     info->stride[dim] = gfc_index_one_node;
    4881              :   else
    4882              :     {
    4883        12954 :       gfc_init_se (&se, NULL);
    4884        12954 :       gfc_conv_expr_type (&se, stride, gfc_array_index_type);
    4885        12954 :       gfc_add_block_to_block (block, &se.pre);
    4886        12954 :       tree value = se.expr;
    4887        12954 :       if (save_value)
    4888        12954 :         info->stride[dim] = gfc_evaluate_now (value, block);
    4889              :       else
    4890            0 :         info->stride[dim] = value;
    4891              :     }
    4892              : }
    4893              : 
    4894              : 
    4895              : /* Generate in INNER the bounds checking code along the dimension DIM for
    4896              :    the array associated with SS_INFO.  */
    4897              : 
    4898              : static void
    4899        24208 : add_check_section_in_array_bounds (stmtblock_t *inner, gfc_ss_info *ss_info,
    4900              :                                    int dim)
    4901              : {
    4902        24208 :   gfc_expr *expr = ss_info->expr;
    4903        24208 :   locus *expr_loc = &expr->where;
    4904        24208 :   const char *expr_name = expr->symtree->name;
    4905              : 
    4906        24208 :   gfc_array_info *info = &ss_info->data.array;
    4907              : 
    4908        24208 :   bool check_upper;
    4909        24208 :   if (dim == info->ref->u.ar.dimen - 1
    4910        20501 :       && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    4911              :     check_upper = false;
    4912              :   else
    4913        23912 :     check_upper = true;
    4914              : 
    4915              :   /* Zero stride is not allowed.  */
    4916        24208 :   tree tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    4917              :                               info->stride[dim], gfc_index_zero_node);
    4918        24208 :   char * msg = xasprintf ("Zero stride is not allowed, for dimension %d "
    4919              :                           "of array '%s'", dim + 1, expr_name);
    4920        24208 :   gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg);
    4921        24208 :   free (msg);
    4922              : 
    4923        24208 :   tree desc = info->descriptor;
    4924              : 
    4925              :   /* This is the run-time equivalent of resolve.cc's
    4926              :      check_dimension.  The logical is more readable there
    4927              :      than it is here, with all the trees.  */
    4928        24208 :   tree lbound = gfc_conv_array_lbound (desc, dim);
    4929        24208 :   tree end = info->end[dim];
    4930        24208 :   tree ubound = check_upper ? gfc_conv_array_ubound (desc, dim) : NULL_TREE;
    4931              : 
    4932              :   /* non_zerosized is true when the selected range is not
    4933              :      empty.  */
    4934        24208 :   tree stride_pos = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4935              :                                      info->stride[dim], gfc_index_zero_node);
    4936        24208 :   tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    4937              :                          info->start[dim], end);
    4938        24208 :   stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    4939              :                                 logical_type_node, stride_pos, tmp);
    4940              : 
    4941        24208 :   tree stride_neg = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4942              :                                      info->stride[dim], gfc_index_zero_node);
    4943        24208 :   tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    4944              :                          info->start[dim], end);
    4945        24208 :   stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    4946              :                                 logical_type_node, stride_neg, tmp);
    4947        24208 :   tree non_zerosized = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    4948              :                                         logical_type_node, stride_pos,
    4949              :                                         stride_neg);
    4950              : 
    4951              :   /* Check the start of the range against the lower and upper
    4952              :      bounds of the array, if the range is not empty.
    4953              :      If upper bound is present, include both bounds in the
    4954              :      error message.  */
    4955        24208 :   if (check_upper)
    4956              :     {
    4957        23912 :       tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4958              :                              info->start[dim], lbound);
    4959        23912 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4960              :                              non_zerosized, tmp);
    4961        23912 :       tree tmp2 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4962              :                                    info->start[dim], ubound);
    4963        23912 :       tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4964              :                               non_zerosized, tmp2);
    4965        23912 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
    4966              :                        "expected range (%%ld:%%ld)", dim + 1, expr_name);
    4967        23912 :       gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
    4968              :           fold_convert (long_integer_type_node, info->start[dim]),
    4969              :           fold_convert (long_integer_type_node, lbound),
    4970              :           fold_convert (long_integer_type_node, ubound));
    4971        23912 :       gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
    4972              :           fold_convert (long_integer_type_node, info->start[dim]),
    4973              :           fold_convert (long_integer_type_node, lbound),
    4974              :           fold_convert (long_integer_type_node, ubound));
    4975        23912 :       free (msg);
    4976              :     }
    4977              :   else
    4978              :     {
    4979          296 :       tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4980              :                              info->start[dim], lbound);
    4981          296 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4982              :                              non_zerosized, tmp);
    4983          296 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
    4984              :                        "lower bound of %%ld", dim + 1, expr_name);
    4985          296 :       gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
    4986              :           fold_convert (long_integer_type_node, info->start[dim]),
    4987              :           fold_convert (long_integer_type_node, lbound));
    4988          296 :       free (msg);
    4989              :     }
    4990              : 
    4991              :   /* Compute the last element of the range, which is not
    4992              :      necessarily "end" (think 0:5:3, which doesn't contain 5)
    4993              :      and check it against both lower and upper bounds.  */
    4994              : 
    4995        24208 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    4996              :                          end, info->start[dim]);
    4997        24208 :   tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR, gfc_array_index_type,
    4998              :                          tmp, info->stride[dim]);
    4999        24208 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5000              :                          end, tmp);
    5001        24208 :   tree tmp2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    5002              :                                tmp, lbound);
    5003        24208 :   tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    5004              :                           non_zerosized, tmp2);
    5005        24208 :   if (check_upper)
    5006              :     {
    5007        23912 :       tree tmp3 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    5008              :                                    tmp, ubound);
    5009        23912 :       tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    5010              :                               non_zerosized, tmp3);
    5011        23912 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
    5012              :                        "expected range (%%ld:%%ld)", dim + 1, expr_name);
    5013        23912 :       gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
    5014              :           fold_convert (long_integer_type_node, tmp),
    5015              :           fold_convert (long_integer_type_node, ubound),
    5016              :           fold_convert (long_integer_type_node, lbound));
    5017        23912 :       gfc_trans_runtime_check (true, false, tmp3, inner, expr_loc, msg,
    5018              :           fold_convert (long_integer_type_node, tmp),
    5019              :           fold_convert (long_integer_type_node, ubound),
    5020              :           fold_convert (long_integer_type_node, lbound));
    5021        23912 :       free (msg);
    5022              :     }
    5023              :   else
    5024              :     {
    5025          296 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
    5026              :                        "lower bound of %%ld", dim + 1, expr_name);
    5027          296 :       gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
    5028              :           fold_convert (long_integer_type_node, tmp),
    5029              :           fold_convert (long_integer_type_node, lbound));
    5030          296 :       free (msg);
    5031              :     }
    5032        24208 : }
    5033              : 
    5034              : 
    5035              : /* Tells whether we need to generate bounds checking code for the array
    5036              :    associated with SS.  */
    5037              : 
    5038              : bool
    5039        25067 : bounds_check_needed (gfc_ss *ss)
    5040              : {
    5041              :   /* Catch allocatable lhs in f2003.  */
    5042        25067 :   if (flag_realloc_lhs && ss->no_bounds_check)
    5043              :     return false;
    5044              : 
    5045        24791 :   gfc_ss_info *ss_info = ss->info;
    5046        24791 :   if (ss_info->type == GFC_SS_SECTION)
    5047              :     return true;
    5048              : 
    5049         4099 :   if (!(ss_info->type == GFC_SS_INTRINSIC
    5050          227 :         && ss_info->expr
    5051          227 :         && ss_info->expr->expr_type == EXPR_FUNCTION))
    5052              :     return false;
    5053              : 
    5054          227 :   gfc_intrinsic_sym *isym = ss_info->expr->value.function.isym;
    5055          227 :   if (!(isym
    5056          227 :         && (isym->id == GFC_ISYM_MAXLOC
    5057          203 :             || isym->id == GFC_ISYM_MINLOC)))
    5058              :     return false;
    5059              : 
    5060           34 :   return gfc_inline_intrinsic_function_p (ss_info->expr);
    5061              : }
    5062              : 
    5063              : 
    5064              : /* Calculates the range start and stride for a SS chain.  Also gets the
    5065              :    descriptor and data pointer.  The range of vector subscripts is the size
    5066              :    of the vector.  Array bounds are also checked.  */
    5067              : 
    5068              : void
    5069       193717 : gfc_conv_ss_startstride (gfc_loopinfo * loop)
    5070              : {
    5071       193717 :   int n;
    5072       193717 :   tree tmp;
    5073       193717 :   gfc_ss *ss;
    5074              : 
    5075       193717 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    5076              : 
    5077       193717 :   loop->dimen = 0;
    5078              :   /* Determine the rank of the loop.  */
    5079       214559 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5080              :     {
    5081       214559 :       switch (ss->info->type)
    5082              :         {
    5083       182300 :         case GFC_SS_SECTION:
    5084       182300 :         case GFC_SS_CONSTRUCTOR:
    5085       182300 :         case GFC_SS_FUNCTION:
    5086       182300 :         case GFC_SS_COMPONENT:
    5087       182300 :           loop->dimen = ss->dimen;
    5088       182300 :           goto done;
    5089              : 
    5090              :         /* As usual, lbound and ubound are exceptions!.  */
    5091        11417 :         case GFC_SS_INTRINSIC:
    5092        11417 :           switch (ss->info->expr->value.function.isym->id)
    5093              :             {
    5094        11417 :             case GFC_ISYM_LBOUND:
    5095        11417 :             case GFC_ISYM_UBOUND:
    5096        11417 :             case GFC_ISYM_COSHAPE:
    5097        11417 :             case GFC_ISYM_LCOBOUND:
    5098        11417 :             case GFC_ISYM_UCOBOUND:
    5099        11417 :             case GFC_ISYM_MAXLOC:
    5100        11417 :             case GFC_ISYM_MINLOC:
    5101        11417 :             case GFC_ISYM_SHAPE:
    5102        11417 :             case GFC_ISYM_THIS_IMAGE:
    5103        11417 :               loop->dimen = ss->dimen;
    5104        11417 :               goto done;
    5105              : 
    5106              :             default:
    5107              :               break;
    5108              :             }
    5109              : 
    5110        20842 :         default:
    5111        20842 :           break;
    5112              :         }
    5113              :     }
    5114              : 
    5115              :   /* We should have determined the rank of the expression by now.  If
    5116              :      not, that's bad news.  */
    5117            0 :   gcc_unreachable ();
    5118              : 
    5119       193717 : done:
    5120              :   /* Loop over all the SS in the chain.  */
    5121       500362 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5122              :     {
    5123       306645 :       gfc_ss_info *ss_info;
    5124       306645 :       gfc_array_info *info;
    5125       306645 :       gfc_expr *expr;
    5126              : 
    5127       306645 :       ss_info = ss->info;
    5128       306645 :       expr = ss_info->expr;
    5129       306645 :       info = &ss_info->data.array;
    5130              : 
    5131       306645 :       if (expr && expr->shape && !info->shape)
    5132       178603 :         info->shape = expr->shape;
    5133              : 
    5134       306645 :       switch (ss_info->type)
    5135              :         {
    5136       195238 :         case GFC_SS_SECTION:
    5137              :           /* Get the descriptor for the array.  If it is a cross loops array,
    5138              :              we got the descriptor already in the outermost loop.  */
    5139       195238 :           if (ss->parent == NULL)
    5140       190602 :             gfc_conv_ss_descriptor (&outer_loop->pre, ss,
    5141       190602 :                                     !loop->array_parameter);
    5142              : 
    5143       464207 :           for (n = 0; n < ss->dimen; n++)
    5144       268969 :             gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
    5145              :           break;
    5146              : 
    5147        11710 :         case GFC_SS_INTRINSIC:
    5148        11710 :           switch (expr->value.function.isym->id)
    5149              :             {
    5150         3297 :             case GFC_ISYM_MINLOC:
    5151         3297 :             case GFC_ISYM_MAXLOC:
    5152         3297 :               {
    5153         3297 :                 gfc_se se;
    5154         3297 :                 gfc_init_se (&se, nullptr);
    5155         3297 :                 se.loop = loop;
    5156         3297 :                 se.ss = ss;
    5157         3297 :                 gfc_conv_intrinsic_function (&se, expr);
    5158         3297 :                 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    5159         3297 :                 gfc_add_block_to_block (&outer_loop->post, &se.post);
    5160              : 
    5161         3297 :                 info->descriptor = se.expr;
    5162              : 
    5163         3297 :                 info->data = gfc_conv_array_data (info->descriptor);
    5164         3297 :                 info->data = gfc_evaluate_now (info->data, &outer_loop->pre);
    5165              : 
    5166         3297 :                 gfc_expr *array = expr->value.function.actual->expr;
    5167         3297 :                 tree rank = build_int_cst (gfc_array_index_type, array->rank);
    5168              : 
    5169         3297 :                 tree tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5170              :                                             gfc_array_index_type, rank,
    5171              :                                             gfc_index_one_node);
    5172              : 
    5173         3297 :                 info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5174         3297 :                 info->start[0] = gfc_index_zero_node;
    5175         3297 :                 info->stride[0] = gfc_index_one_node;
    5176         3297 :                 info->offset = gfc_index_zero_node;
    5177         3297 :                 continue;
    5178         3297 :               }
    5179              : 
    5180              :             /* Fall through to supply start and stride.  */
    5181         3004 :             case GFC_ISYM_LBOUND:
    5182         3004 :             case GFC_ISYM_UBOUND:
    5183              :               /* This is the variant without DIM=...  */
    5184         3004 :               gcc_assert (expr->value.function.actual->next->expr == NULL);
    5185              :               /* Fall through.  */
    5186              : 
    5187         8034 :             case GFC_ISYM_SHAPE:
    5188         8034 :               {
    5189         8034 :                 gfc_expr *arg;
    5190              : 
    5191         8034 :                 arg = expr->value.function.actual->expr;
    5192         8034 :                 if (arg->rank == -1)
    5193              :                   {
    5194         1175 :                     gfc_se se;
    5195         1175 :                     tree rank, tmp;
    5196              : 
    5197              :                     /* The rank (hence the return value's shape) is unknown,
    5198              :                        we have to retrieve it.  */
    5199         1175 :                     gfc_init_se (&se, NULL);
    5200         1175 :                     se.descriptor_only = 1;
    5201         1175 :                     gfc_conv_expr (&se, arg);
    5202              :                     /* This is a bare variable, so there is no preliminary
    5203              :                        or cleanup code unless -std=f202y and bounds checking
    5204              :                        is on.  */
    5205         1175 :                     if (!((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    5206            0 :                           && (gfc_option.allow_std & GFC_STD_F202Y)))
    5207         1175 :                       gcc_assert (se.pre.head == NULL_TREE
    5208              :                                   && se.post.head == NULL_TREE);
    5209         1175 :                     rank = gfc_conv_descriptor_rank_get (se.expr);
    5210         1175 :                     tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5211              :                                            gfc_array_index_type,
    5212              :                                            fold_convert (gfc_array_index_type,
    5213              :                                                          rank),
    5214              :                                            gfc_index_one_node);
    5215         1175 :                     info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5216         1175 :                     info->start[0] = gfc_index_zero_node;
    5217         1175 :                     info->stride[0] = gfc_index_one_node;
    5218         1175 :                     continue;
    5219         1175 :                   }
    5220              :                   /* Otherwise fall through GFC_SS_FUNCTION.  */
    5221              :                   gcc_fallthrough ();
    5222              :               }
    5223              :             case GFC_ISYM_COSHAPE:
    5224              :             case GFC_ISYM_LCOBOUND:
    5225              :             case GFC_ISYM_UCOBOUND:
    5226              :             case GFC_ISYM_THIS_IMAGE:
    5227              :               break;
    5228              : 
    5229            0 :             default:
    5230            0 :               continue;
    5231            0 :             }
    5232              : 
    5233              :           /* FALLTHRU */
    5234              :         case GFC_SS_CONSTRUCTOR:
    5235              :         case GFC_SS_FUNCTION:
    5236       135477 :           for (n = 0; n < ss->dimen; n++)
    5237              :             {
    5238        73018 :               int dim = ss->dim[n];
    5239              : 
    5240        73018 :               info->start[dim]  = gfc_index_zero_node;
    5241        73018 :               if (ss_info->type != GFC_SS_FUNCTION)
    5242        57999 :                 info->end[dim]    = gfc_index_zero_node;
    5243        73018 :               info->stride[dim] = gfc_index_one_node;
    5244              :             }
    5245              :           break;
    5246              : 
    5247              :         default:
    5248              :           break;
    5249              :         }
    5250              :     }
    5251              : 
    5252              :   /* The rest is just runtime bounds checking.  */
    5253       193717 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    5254              :     {
    5255        17034 :       stmtblock_t block;
    5256        17034 :       tree size[GFC_MAX_DIMENSIONS];
    5257        17034 :       tree tmp3;
    5258        17034 :       gfc_array_info *info;
    5259        17034 :       char *msg;
    5260        17034 :       int dim;
    5261              : 
    5262        17034 :       gfc_start_block (&block);
    5263              : 
    5264        54606 :       for (n = 0; n < loop->dimen; n++)
    5265        20538 :         size[n] = NULL_TREE;
    5266              : 
    5267              :       /* If there is a constructor involved, derive size[] from its shape.  */
    5268        39275 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5269              :         {
    5270        24721 :           gfc_ss_info *ss_info;
    5271              : 
    5272        24721 :           ss_info = ss->info;
    5273        24721 :           info = &ss_info->data.array;
    5274              : 
    5275        24721 :           if (ss_info->type == GFC_SS_CONSTRUCTOR && info->shape)
    5276              :             {
    5277         5224 :               for (n = 0; n < loop->dimen; n++)
    5278              :                 {
    5279         2744 :                   if (size[n] == NULL)
    5280              :                     {
    5281         2744 :                       gcc_assert (info->shape[n]);
    5282         2744 :                       size[n] = gfc_conv_mpz_to_tree (info->shape[n],
    5283              :                                                       gfc_index_integer_kind);
    5284              :                     }
    5285              :                 }
    5286              :               break;
    5287              :             }
    5288              :         }
    5289              : 
    5290        42101 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5291              :         {
    5292        25067 :           stmtblock_t inner;
    5293        25067 :           gfc_ss_info *ss_info;
    5294        25067 :           gfc_expr *expr;
    5295        25067 :           locus *expr_loc;
    5296        25067 :           const char *expr_name;
    5297        25067 :           char *ref_name = NULL;
    5298              : 
    5299        25067 :           if (!bounds_check_needed (ss))
    5300         4341 :             continue;
    5301              : 
    5302        20726 :           ss_info = ss->info;
    5303        20726 :           expr = ss_info->expr;
    5304        20726 :           expr_loc = &expr->where;
    5305        20726 :           if (expr->ref)
    5306        20692 :             expr_name = ref_name = abridged_ref_name (expr, NULL);
    5307              :           else
    5308           34 :             expr_name = expr->symtree->name;
    5309              : 
    5310        20726 :           gfc_start_block (&inner);
    5311              : 
    5312              :           /* TODO: range checking for mapped dimensions.  */
    5313        20726 :           info = &ss_info->data.array;
    5314              : 
    5315              :           /* This code only checks ranges.  Elemental and vector
    5316              :              dimensions are checked later.  */
    5317        65708 :           for (n = 0; n < loop->dimen; n++)
    5318              :             {
    5319        24256 :               dim = ss->dim[n];
    5320        24256 :               if (ss_info->type == GFC_SS_SECTION)
    5321              :                 {
    5322        24222 :                   if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
    5323           14 :                     continue;
    5324              : 
    5325        24208 :                   add_check_section_in_array_bounds (&inner, ss_info, dim);
    5326              :                 }
    5327              : 
    5328              :               /* Check the section sizes match.  */
    5329        24242 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5330              :                                      gfc_array_index_type, info->end[dim],
    5331              :                                      info->start[dim]);
    5332        24242 :               tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
    5333              :                                      gfc_array_index_type, tmp,
    5334              :                                      info->stride[dim]);
    5335        24242 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5336              :                                      gfc_array_index_type,
    5337              :                                      gfc_index_one_node, tmp);
    5338        24242 :               tmp = fold_build2_loc (input_location, MAX_EXPR,
    5339              :                                      gfc_array_index_type, tmp,
    5340              :                                      build_int_cst (gfc_array_index_type, 0));
    5341              :               /* We remember the size of the first section, and check all the
    5342              :                  others against this.  */
    5343        24242 :               if (size[n])
    5344              :                 {
    5345         7152 :                   tmp3 = fold_build2_loc (input_location, NE_EXPR,
    5346              :                                           logical_type_node, tmp, size[n]);
    5347         7152 :                   if (ss_info->type == GFC_SS_INTRINSIC)
    5348            0 :                     msg = xasprintf ("Extent mismatch for dimension %d of the "
    5349              :                                      "result of intrinsic '%s' (%%ld/%%ld)",
    5350              :                                      dim + 1, expr_name);
    5351              :                   else
    5352         7152 :                     msg = xasprintf ("Array bound mismatch for dimension %d "
    5353              :                                      "of array '%s' (%%ld/%%ld)",
    5354              :                                      dim + 1, expr_name);
    5355              : 
    5356         7152 :                   gfc_trans_runtime_check (true, false, tmp3, &inner,
    5357              :                                            expr_loc, msg,
    5358              :                         fold_convert (long_integer_type_node, tmp),
    5359              :                         fold_convert (long_integer_type_node, size[n]));
    5360              : 
    5361         7152 :                   free (msg);
    5362              :                 }
    5363              :               else
    5364        17090 :                 size[n] = gfc_evaluate_now (tmp, &inner);
    5365              :             }
    5366              : 
    5367        20726 :           tmp = gfc_finish_block (&inner);
    5368              : 
    5369              :           /* For optional arguments, only check bounds if the argument is
    5370              :              present.  */
    5371        20726 :           if ((expr->symtree->n.sym->attr.optional
    5372        20418 :                || expr->symtree->n.sym->attr.not_always_present)
    5373          308 :               && expr->symtree->n.sym->attr.dummy)
    5374          307 :             tmp = build3_v (COND_EXPR,
    5375              :                             gfc_conv_expr_present (expr->symtree->n.sym),
    5376              :                             tmp, build_empty_stmt (input_location));
    5377              : 
    5378        20726 :           gfc_add_expr_to_block (&block, tmp);
    5379              : 
    5380        20726 :           free (ref_name);
    5381              :         }
    5382              : 
    5383        17034 :       tmp = gfc_finish_block (&block);
    5384        17034 :       gfc_add_expr_to_block (&outer_loop->pre, tmp);
    5385              :     }
    5386              : 
    5387       197081 :   for (loop = loop->nested; loop; loop = loop->next)
    5388         3364 :     gfc_conv_ss_startstride (loop);
    5389       193717 : }
    5390              : 
    5391              : /* Return true if both symbols could refer to the same data object.  Does
    5392              :    not take account of aliasing due to equivalence statements.  */
    5393              : 
    5394              : static bool
    5395        13885 : symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym, bool lsym_pointer,
    5396              :                      bool lsym_target, bool rsym_pointer, bool rsym_target)
    5397              : {
    5398              :   /* Aliasing isn't possible if the symbols have different base types,
    5399              :      except for complex types where an inquiry reference (%RE, %IM) could
    5400              :      alias with a real type with the same kind parameter.  */
    5401        13885 :   if (!gfc_compare_types (&lsym->ts, &rsym->ts)
    5402        13885 :       && !(((lsym->ts.type == BT_COMPLEX && rsym->ts.type == BT_REAL)
    5403         5221 :             || (lsym->ts.type == BT_REAL && rsym->ts.type == BT_COMPLEX))
    5404           76 :            && lsym->ts.kind == rsym->ts.kind))
    5405              :     return false;
    5406              : 
    5407              :   /* Pointers can point to other pointers and target objects.  */
    5408              : 
    5409         8677 :   if ((lsym_pointer && (rsym_pointer || rsym_target))
    5410         8469 :       || (rsym_pointer && (lsym_pointer || lsym_target)))
    5411              :     return true;
    5412              : 
    5413              :   /* Special case: Argument association, cf. F90 12.4.1.6, F2003 12.4.1.7
    5414              :      and F2008 12.5.2.13 items 3b and 4b. The pointer case (a) is already
    5415              :      checked above.  */
    5416         8555 :   if (lsym_target && rsym_target
    5417           13 :       && ((lsym->attr.dummy && !lsym->attr.contiguous
    5418            0 :            && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
    5419           13 :           || (rsym->attr.dummy && !rsym->attr.contiguous
    5420            6 :               && (!rsym->attr.dimension
    5421            6 :                   || rsym->as->type == AS_ASSUMED_SHAPE))))
    5422            6 :     return true;
    5423              : 
    5424              :   return false;
    5425              : }
    5426              : 
    5427              : 
    5428              : /* Return true if the two SS could be aliased, i.e. both point to the same data
    5429              :    object.  */
    5430              : /* TODO: resolve aliases based on frontend expressions.  */
    5431              : 
    5432              : static int
    5433        11423 : gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
    5434              : {
    5435        11423 :   gfc_ref *lref;
    5436        11423 :   gfc_ref *rref;
    5437        11423 :   gfc_expr *lexpr, *rexpr;
    5438        11423 :   gfc_symbol *lsym;
    5439        11423 :   gfc_symbol *rsym;
    5440        11423 :   bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
    5441              : 
    5442        11423 :   lexpr = lss->info->expr;
    5443        11423 :   rexpr = rss->info->expr;
    5444              : 
    5445        11423 :   lsym = lexpr->symtree->n.sym;
    5446        11423 :   rsym = rexpr->symtree->n.sym;
    5447              : 
    5448        11423 :   lsym_pointer = lsym->attr.pointer;
    5449        11423 :   lsym_target = lsym->attr.target;
    5450        11423 :   rsym_pointer = rsym->attr.pointer;
    5451        11423 :   rsym_target = rsym->attr.target;
    5452              : 
    5453        11423 :   if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
    5454              :                            rsym_pointer, rsym_target))
    5455              :     return 1;
    5456              : 
    5457        11333 :   if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
    5458         9807 :       && lsym->ts.type != BT_DERIVED && lsym->ts.type != BT_CLASS)
    5459              :     return 0;
    5460              : 
    5461              :   /* For derived types we must check all the component types.  We can ignore
    5462              :      array references as these will have the same base type as the previous
    5463              :      component ref.  */
    5464         3104 :   for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
    5465              :     {
    5466         1117 :       if (lref->type != REF_COMPONENT)
    5467          107 :         continue;
    5468              : 
    5469         1010 :       lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
    5470         1010 :       lsym_target  = lsym_target  || lref->u.c.sym->attr.target;
    5471              : 
    5472         1010 :       if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
    5473              :                                rsym_pointer, rsym_target))
    5474              :         return 1;
    5475              : 
    5476         1010 :       if ((lsym_pointer && (rsym_pointer || rsym_target))
    5477          995 :           || (rsym_pointer && (lsym_pointer || lsym_target)))
    5478              :         {
    5479            6 :           if (gfc_compare_types (&lref->u.c.component->ts,
    5480              :                                  &rsym->ts))
    5481              :             return 1;
    5482              :         }
    5483              : 
    5484         1518 :       for (rref = rexpr->ref; rref != rss->info->data.array.ref;
    5485          514 :            rref = rref->next)
    5486              :         {
    5487          515 :           if (rref->type != REF_COMPONENT)
    5488           36 :             continue;
    5489              : 
    5490          479 :           rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
    5491          479 :           rsym_target  = lsym_target  || rref->u.c.sym->attr.target;
    5492              : 
    5493          479 :           if (symbols_could_alias (lref->u.c.sym, rref->u.c.sym,
    5494              :                                    lsym_pointer, lsym_target,
    5495              :                                    rsym_pointer, rsym_target))
    5496              :             return 1;
    5497              : 
    5498          478 :           if ((lsym_pointer && (rsym_pointer || rsym_target))
    5499          474 :               || (rsym_pointer && (lsym_pointer || lsym_target)))
    5500              :             {
    5501            0 :               if (gfc_compare_types (&lref->u.c.component->ts,
    5502            0 :                                      &rref->u.c.sym->ts))
    5503              :                 return 1;
    5504            0 :               if (gfc_compare_types (&lref->u.c.sym->ts,
    5505            0 :                                      &rref->u.c.component->ts))
    5506              :                 return 1;
    5507            0 :               if (gfc_compare_types (&lref->u.c.component->ts,
    5508            0 :                                      &rref->u.c.component->ts))
    5509              :                 return 1;
    5510              :             }
    5511              :         }
    5512              :     }
    5513              : 
    5514         1987 :   lsym_pointer = lsym->attr.pointer;
    5515         1987 :   lsym_target = lsym->attr.target;
    5516              : 
    5517         2954 :   for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
    5518              :     {
    5519         1138 :       if (rref->type != REF_COMPONENT)
    5520              :         break;
    5521              : 
    5522          973 :       rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
    5523          973 :       rsym_target  = lsym_target  || rref->u.c.sym->attr.target;
    5524              : 
    5525          973 :       if (symbols_could_alias (rref->u.c.sym, lsym,
    5526              :                                lsym_pointer, lsym_target,
    5527              :                                rsym_pointer, rsym_target))
    5528              :         return 1;
    5529              : 
    5530          973 :       if ((lsym_pointer && (rsym_pointer || rsym_target))
    5531          955 :           || (rsym_pointer && (lsym_pointer || lsym_target)))
    5532              :         {
    5533            6 :           if (gfc_compare_types (&lsym->ts, &rref->u.c.component->ts))
    5534              :             return 1;
    5535              :         }
    5536              :     }
    5537              : 
    5538              :   return 0;
    5539              : }
    5540              : 
    5541              : 
    5542              : /* Resolve array data dependencies.  Creates a temporary if required.  */
    5543              : /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
    5544              :    dependency.cc.  */
    5545              : 
    5546              : void
    5547        39016 : gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
    5548              :                                gfc_ss * rss)
    5549              : {
    5550        39016 :   gfc_ss *ss;
    5551        39016 :   gfc_ref *lref;
    5552        39016 :   gfc_ref *rref;
    5553        39016 :   gfc_ss_info *ss_info;
    5554        39016 :   gfc_expr *dest_expr;
    5555        39016 :   gfc_expr *ss_expr;
    5556        39016 :   int nDepend = 0;
    5557        39016 :   int i, j;
    5558              : 
    5559        39016 :   loop->temp_ss = NULL;
    5560        39016 :   dest_expr = dest->info->expr;
    5561              : 
    5562        83996 :   for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
    5563              :     {
    5564        46165 :       ss_info = ss->info;
    5565        46165 :       ss_expr = ss_info->expr;
    5566              : 
    5567        46165 :       if (ss_info->array_outer_dependency)
    5568              :         {
    5569              :           nDepend = 1;
    5570              :           break;
    5571              :         }
    5572              : 
    5573        46042 :       if (ss_info->type != GFC_SS_SECTION)
    5574              :         {
    5575        31869 :           if (flag_realloc_lhs
    5576        30772 :               && dest_expr != ss_expr
    5577        30772 :               && gfc_is_reallocatable_lhs (dest_expr)
    5578        39248 :               && ss_expr->rank)
    5579         3616 :             nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
    5580              : 
    5581              :           /* Check for cases like   c(:)(1:2) = c(2)(2:3)  */
    5582        31869 :           if (!nDepend && dest_expr->rank > 0
    5583        31338 :               && dest_expr->ts.type == BT_CHARACTER
    5584         4935 :               && ss_expr->expr_type == EXPR_VARIABLE)
    5585              : 
    5586          165 :             nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
    5587              : 
    5588        31869 :           if (ss_info->type == GFC_SS_REFERENCE
    5589        31869 :               && gfc_check_dependency (dest_expr, ss_expr, false))
    5590          206 :             ss_info->data.scalar.needs_temporary = 1;
    5591              : 
    5592        31869 :           if (nDepend)
    5593              :             break;
    5594              :           else
    5595        31326 :             continue;
    5596              :         }
    5597              : 
    5598        14173 :       if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
    5599              :         {
    5600        11423 :           if (gfc_could_be_alias (dest, ss)
    5601        11423 :               || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
    5602              :             {
    5603              :               nDepend = 1;
    5604              :               break;
    5605              :             }
    5606              :         }
    5607              :       else
    5608              :         {
    5609         2750 :           lref = dest_expr->ref;
    5610         2750 :           rref = ss_expr->ref;
    5611              : 
    5612         2750 :           nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
    5613              : 
    5614         2750 :           if (nDepend == 1)
    5615              :             break;
    5616              : 
    5617         5469 :           for (i = 0; i < dest->dimen; i++)
    5618         7392 :             for (j = 0; j < ss->dimen; j++)
    5619         4386 :               if (i != j
    5620         1317 :                   && dest->dim[i] == ss->dim[j])
    5621              :                 {
    5622              :                   /* If we don't access array elements in the same order,
    5623              :                      there is a dependency.  */
    5624           63 :                   nDepend = 1;
    5625           63 :                   goto temporary;
    5626              :                 }
    5627              : #if 0
    5628              :           /* TODO : loop shifting.  */
    5629              :           if (nDepend == 1)
    5630              :             {
    5631              :               /* Mark the dimensions for LOOP SHIFTING */
    5632              :               for (n = 0; n < loop->dimen; n++)
    5633              :                 {
    5634              :                   int dim = dest->data.info.dim[n];
    5635              : 
    5636              :                   if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
    5637              :                     depends[n] = 2;
    5638              :                   else if (! gfc_is_same_range (&lref->u.ar,
    5639              :                                                 &rref->u.ar, dim, 0))
    5640              :                     depends[n] = 1;
    5641              :                  }
    5642              : 
    5643              :               /* Put all the dimensions with dependencies in the
    5644              :                  innermost loops.  */
    5645              :               dim = 0;
    5646              :               for (n = 0; n < loop->dimen; n++)
    5647              :                 {
    5648              :                   gcc_assert (loop->order[n] == n);
    5649              :                   if (depends[n])
    5650              :                   loop->order[dim++] = n;
    5651              :                 }
    5652              :               for (n = 0; n < loop->dimen; n++)
    5653              :                 {
    5654              :                   if (! depends[n])
    5655              :                   loop->order[dim++] = n;
    5656              :                 }
    5657              : 
    5658              :               gcc_assert (dim == loop->dimen);
    5659              :               break;
    5660              :             }
    5661              : #endif
    5662              :         }
    5663              :     }
    5664              : 
    5665          830 : temporary:
    5666              : 
    5667        39016 :   if (nDepend == 1)
    5668              :     {
    5669         1185 :       tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
    5670         1185 :       if (GFC_ARRAY_TYPE_P (base_type)
    5671         1185 :           || GFC_DESCRIPTOR_TYPE_P (base_type))
    5672            0 :         base_type = gfc_get_element_type (base_type);
    5673         1185 :       loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
    5674              :                                        loop->dimen);
    5675         1185 :       gfc_add_ss_to_loop (loop, loop->temp_ss);
    5676              :     }
    5677              :   else
    5678        37831 :     loop->temp_ss = NULL;
    5679        39016 : }
    5680              : 
    5681              : 
    5682              : /* Browse through each array's information from the scalarizer and set the loop
    5683              :    bounds according to the "best" one (per dimension), i.e. the one which
    5684              :    provides the most information (constant bounds, shape, etc.).  */
    5685              : 
    5686              : static void
    5687       193717 : set_loop_bounds (gfc_loopinfo *loop)
    5688              : {
    5689       193717 :   int n, dim, spec_dim;
    5690       193717 :   gfc_array_info *info;
    5691       193717 :   gfc_array_info *specinfo;
    5692       193717 :   gfc_ss *ss;
    5693       193717 :   tree tmp;
    5694       193717 :   gfc_ss **loopspec;
    5695       193717 :   bool dynamic[GFC_MAX_DIMENSIONS];
    5696       193717 :   mpz_t *cshape;
    5697       193717 :   mpz_t i;
    5698       193717 :   bool nonoptional_arr;
    5699              : 
    5700       193717 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    5701              : 
    5702       193717 :   loopspec = loop->specloop;
    5703              : 
    5704       193717 :   mpz_init (i);
    5705       649339 :   for (n = 0; n < loop->dimen; n++)
    5706              :     {
    5707       261905 :       loopspec[n] = NULL;
    5708       261905 :       dynamic[n] = false;
    5709              : 
    5710              :       /* If there are both optional and nonoptional array arguments, scalarize
    5711              :          over the nonoptional; otherwise, it does not matter as then all
    5712              :          (optional) arrays have to be present per F2008, 125.2.12p3(6).  */
    5713              : 
    5714       261905 :       nonoptional_arr = false;
    5715              : 
    5716       304629 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5717       304609 :         if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
    5718       268566 :             && ss->info->type != GFC_SS_REFERENCE && !ss->info->can_be_null_ref)
    5719              :           {
    5720              :             nonoptional_arr = true;
    5721              :             break;
    5722              :           }
    5723              : 
    5724              :       /* We use one SS term, and use that to determine the bounds of the
    5725              :          loop for this dimension.  We try to pick the simplest term.  */
    5726       681319 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5727              :         {
    5728       419414 :           gfc_ss_type ss_type;
    5729              : 
    5730       419414 :           ss_type = ss->info->type;
    5731       491383 :           if (ss_type == GFC_SS_SCALAR
    5732       419414 :               || ss_type == GFC_SS_TEMP
    5733       356811 :               || ss_type == GFC_SS_REFERENCE
    5734       347728 :               || (ss->info->can_be_null_ref && nonoptional_arr))
    5735        71969 :             continue;
    5736              : 
    5737       347445 :           info = &ss->info->data.array;
    5738       347445 :           dim = ss->dim[n];
    5739              : 
    5740       347445 :           if (loopspec[n] != NULL)
    5741              :             {
    5742        85540 :               specinfo = &loopspec[n]->info->data.array;
    5743        85540 :               spec_dim = loopspec[n]->dim[n];
    5744              :             }
    5745              :           else
    5746              :             {
    5747              :               /* Silence uninitialized warnings.  */
    5748              :               specinfo = NULL;
    5749              :               spec_dim = 0;
    5750              :             }
    5751              : 
    5752       347445 :           if (info->shape)
    5753              :             {
    5754              :               /* The frontend has worked out the size for us.  */
    5755       235773 :               if (!loopspec[n]
    5756        60854 :                   || !specinfo->shape
    5757       283369 :                   || !integer_zerop (specinfo->start[spec_dim]))
    5758              :                 /* Prefer zero-based descriptors if possible.  */
    5759       218354 :                 loopspec[n] = ss;
    5760       235773 :               continue;
    5761              :             }
    5762              : 
    5763       111672 :           if (ss_type == GFC_SS_CONSTRUCTOR)
    5764              :             {
    5765         1513 :               gfc_constructor_base base;
    5766              :               /* An unknown size constructor will always be rank one.
    5767              :                  Higher rank constructors will either have known shape,
    5768              :                  or still be wrapped in a call to reshape.  */
    5769         1513 :               gcc_assert (loop->dimen == 1);
    5770              : 
    5771              :               /* Always prefer to use the constructor bounds if the size
    5772              :                  can be determined at compile time.  Prefer not to otherwise,
    5773              :                  since the general case involves realloc, and it's better to
    5774              :                  avoid that overhead if possible.  */
    5775         1513 :               base = ss->info->expr->value.constructor;
    5776         1513 :               dynamic[n] = gfc_get_array_constructor_size (&i, base);
    5777         1513 :               if (!dynamic[n] || !loopspec[n])
    5778         1284 :                 loopspec[n] = ss;
    5779         1513 :               continue;
    5780         1513 :             }
    5781              : 
    5782              :           /* Avoid using an allocatable lhs in an assignment, since
    5783              :              there might be a reallocation coming.  */
    5784       110159 :           if (loopspec[n] && ss->is_alloc_lhs)
    5785         9834 :             continue;
    5786              : 
    5787       100325 :           if (!loopspec[n])
    5788        85702 :             loopspec[n] = ss;
    5789              :           /* Criteria for choosing a loop specifier (most important first):
    5790              :              doesn't need realloc
    5791              :              stride of one
    5792              :              known stride
    5793              :              known lower bound
    5794              :              known upper bound
    5795              :            */
    5796        14623 :           else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
    5797          295 :             loopspec[n] = ss;
    5798        14328 :           else if (integer_onep (info->stride[dim])
    5799        14328 :                    && !integer_onep (specinfo->stride[spec_dim]))
    5800          126 :             loopspec[n] = ss;
    5801        14202 :           else if (INTEGER_CST_P (info->stride[dim])
    5802        13978 :                    && !INTEGER_CST_P (specinfo->stride[spec_dim]))
    5803            0 :             loopspec[n] = ss;
    5804        14202 :           else if (INTEGER_CST_P (info->start[dim])
    5805         4541 :                    && !INTEGER_CST_P (specinfo->start[spec_dim])
    5806          868 :                    && integer_onep (info->stride[dim])
    5807          434 :                       == integer_onep (specinfo->stride[spec_dim])
    5808        14202 :                    && INTEGER_CST_P (info->stride[dim])
    5809          407 :                       == INTEGER_CST_P (specinfo->stride[spec_dim]))
    5810          407 :             loopspec[n] = ss;
    5811              :           /* We don't work out the upper bound.
    5812              :              else if (INTEGER_CST_P (info->finish[n])
    5813              :              && ! INTEGER_CST_P (specinfo->finish[n]))
    5814              :              loopspec[n] = ss; */
    5815              :         }
    5816              : 
    5817              :       /* We should have found the scalarization loop specifier.  If not,
    5818              :          that's bad news.  */
    5819       261905 :       gcc_assert (loopspec[n]);
    5820              : 
    5821       261905 :       info = &loopspec[n]->info->data.array;
    5822       261905 :       dim = loopspec[n]->dim[n];
    5823              : 
    5824              :       /* Set the extents of this range.  */
    5825       261905 :       cshape = info->shape;
    5826       261905 :       if (cshape && INTEGER_CST_P (info->start[dim])
    5827       187970 :           && INTEGER_CST_P (info->stride[dim]))
    5828              :         {
    5829       187970 :           loop->from[n] = info->start[dim];
    5830       187970 :           mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
    5831       187970 :           mpz_sub_ui (i, i, 1);
    5832              :           /* To = from + (size - 1) * stride.  */
    5833       187970 :           tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
    5834       187970 :           if (!integer_onep (info->stride[dim]))
    5835         8803 :             tmp = fold_build2_loc (input_location, MULT_EXPR,
    5836              :                                    gfc_array_index_type, tmp,
    5837              :                                    info->stride[dim]);
    5838       187970 :           loop->to[n] = fold_build2_loc (input_location, PLUS_EXPR,
    5839              :                                          gfc_array_index_type,
    5840              :                                          loop->from[n], tmp);
    5841              :         }
    5842              :       else
    5843              :         {
    5844        73935 :           loop->from[n] = info->start[dim];
    5845        73935 :           switch (loopspec[n]->info->type)
    5846              :             {
    5847          894 :             case GFC_SS_CONSTRUCTOR:
    5848              :               /* The upper bound is calculated when we expand the
    5849              :                  constructor.  */
    5850          894 :               gcc_assert (loop->to[n] == NULL_TREE);
    5851              :               break;
    5852              : 
    5853        67103 :             case GFC_SS_SECTION:
    5854              :               /* Use the end expression if it exists and is not constant,
    5855              :                  so that it is only evaluated once.  */
    5856        67103 :               loop->to[n] = info->end[dim];
    5857        67103 :               break;
    5858              : 
    5859         5159 :             case GFC_SS_FUNCTION:
    5860              :               /* The loop bound will be set when we generate the call.  */
    5861         5159 :               gcc_assert (loop->to[n] == NULL_TREE);
    5862              :               break;
    5863              : 
    5864          767 :             case GFC_SS_INTRINSIC:
    5865          767 :               {
    5866          767 :                 gfc_expr *expr = loopspec[n]->info->expr;
    5867              : 
    5868              :                 /* The {l,u}bound of an assumed rank.  */
    5869          767 :                 if (expr->value.function.isym->id == GFC_ISYM_SHAPE)
    5870          255 :                   gcc_assert (expr->value.function.actual->expr->rank == -1);
    5871              :                 else
    5872          512 :                   gcc_assert ((expr->value.function.isym->id == GFC_ISYM_LBOUND
    5873              :                                || expr->value.function.isym->id == GFC_ISYM_UBOUND)
    5874              :                               && expr->value.function.actual->next->expr == NULL
    5875              :                               && expr->value.function.actual->expr->rank == -1);
    5876              : 
    5877          767 :                 loop->to[n] = info->end[dim];
    5878          767 :                 break;
    5879              :               }
    5880              : 
    5881           12 :             case GFC_SS_COMPONENT:
    5882           12 :               {
    5883           12 :                 if (info->end[dim] != NULL_TREE)
    5884              :                   {
    5885           12 :                     loop->to[n] = info->end[dim];
    5886           12 :                     break;
    5887              :                   }
    5888              :                 else
    5889            0 :                   gcc_unreachable ();
    5890              :               }
    5891              : 
    5892            0 :             default:
    5893            0 :               gcc_unreachable ();
    5894              :             }
    5895              :         }
    5896              : 
    5897              :       /* Transform everything so we have a simple incrementing variable.  */
    5898       261905 :       if (integer_onep (info->stride[dim]))
    5899       250921 :         info->delta[dim] = gfc_index_zero_node;
    5900              :       else
    5901              :         {
    5902              :           /* Set the delta for this section.  */
    5903        10984 :           info->delta[dim] = gfc_evaluate_now (loop->from[n], &outer_loop->pre);
    5904              :           /* Number of iterations is (end - start + step) / step.
    5905              :              with start = 0, this simplifies to
    5906              :              last = end / step;
    5907              :              for (i = 0; i<=last; i++){...};  */
    5908        10984 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5909              :                                  gfc_array_index_type, loop->to[n],
    5910              :                                  loop->from[n]);
    5911        10984 :           tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
    5912              :                                  gfc_array_index_type, tmp, info->stride[dim]);
    5913        10984 :           tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    5914              :                                  tmp, build_int_cst (gfc_array_index_type, -1));
    5915        10984 :           loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5916              :           /* Make the loop variable start at 0.  */
    5917        10984 :           loop->from[n] = gfc_index_zero_node;
    5918              :         }
    5919              :     }
    5920       193717 :   mpz_clear (i);
    5921              : 
    5922       197081 :   for (loop = loop->nested; loop; loop = loop->next)
    5923         3364 :     set_loop_bounds (loop);
    5924       193717 : }
    5925              : 
    5926              : 
    5927              : /* Last attempt to set the loop bounds, in case they depend on an allocatable
    5928              :    function result.  */
    5929              : 
    5930              : static void
    5931       193717 : late_set_loop_bounds (gfc_loopinfo *loop)
    5932              : {
    5933       193717 :   int n, dim;
    5934       193717 :   gfc_array_info *info;
    5935       193717 :   gfc_ss **loopspec;
    5936              : 
    5937       193717 :   loopspec = loop->specloop;
    5938              : 
    5939       455622 :   for (n = 0; n < loop->dimen; n++)
    5940              :     {
    5941              :       /* Set the extents of this range.  */
    5942       261905 :       if (loop->from[n] == NULL_TREE
    5943       261905 :           || loop->to[n] == NULL_TREE)
    5944              :         {
    5945              :           /* We should have found the scalarization loop specifier.  If not,
    5946              :              that's bad news.  */
    5947          461 :           gcc_assert (loopspec[n]);
    5948              : 
    5949          461 :           info = &loopspec[n]->info->data.array;
    5950          461 :           dim = loopspec[n]->dim[n];
    5951              : 
    5952          461 :           if (loopspec[n]->info->type == GFC_SS_FUNCTION
    5953          461 :               && info->start[dim]
    5954          461 :               && info->end[dim])
    5955              :             {
    5956          159 :               loop->from[n] = info->start[dim];
    5957          159 :               loop->to[n] = info->end[dim];
    5958              :             }
    5959              :         }
    5960              :     }
    5961              : 
    5962       197081 :   for (loop = loop->nested; loop; loop = loop->next)
    5963         3364 :     late_set_loop_bounds (loop);
    5964       193717 : }
    5965              : 
    5966              : 
    5967              : /* Initialize the scalarization loop.  Creates the loop variables.  Determines
    5968              :    the range of the loop variables.  Creates a temporary if required.
    5969              :    Also generates code for scalar expressions which have been
    5970              :    moved outside the loop.  */
    5971              : 
    5972              : void
    5973       190353 : gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
    5974              : {
    5975       190353 :   gfc_ss *tmp_ss;
    5976       190353 :   tree tmp;
    5977              : 
    5978       190353 :   set_loop_bounds (loop);
    5979              : 
    5980              :   /* Add all the scalar code that can be taken out of the loops.
    5981              :      This may include calculating the loop bounds, so do it before
    5982              :      allocating the temporary.  */
    5983       190353 :   gfc_add_loop_ss_code (loop, loop->ss, false, where);
    5984              : 
    5985       190353 :   late_set_loop_bounds (loop);
    5986              : 
    5987       190353 :   tmp_ss = loop->temp_ss;
    5988              :   /* If we want a temporary then create it.  */
    5989       190353 :   if (tmp_ss != NULL)
    5990              :     {
    5991        11960 :       gfc_ss_info *tmp_ss_info;
    5992              : 
    5993        11960 :       tmp_ss_info = tmp_ss->info;
    5994        11960 :       gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
    5995        11960 :       gcc_assert (loop->parent == NULL);
    5996              : 
    5997              :       /* Make absolutely sure that this is a complete type.  */
    5998        11960 :       if (tmp_ss_info->string_length)
    5999         2924 :         tmp_ss_info->data.temp.type
    6000         2924 :                 = gfc_get_character_type_len_for_eltype
    6001         2924 :                         (TREE_TYPE (tmp_ss_info->data.temp.type),
    6002              :                          tmp_ss_info->string_length);
    6003              : 
    6004        11960 :       tmp = tmp_ss_info->data.temp.type;
    6005        11960 :       memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
    6006        11960 :       tmp_ss_info->type = GFC_SS_SECTION;
    6007              : 
    6008        11960 :       gcc_assert (tmp_ss->dimen != 0);
    6009              : 
    6010        11960 :       gfc_trans_create_temp_array (&loop->pre, &loop->post, tmp_ss, tmp,
    6011              :                                    NULL_TREE, false, true, false, where);
    6012              :     }
    6013              : 
    6014              :   /* For array parameters we don't have loop variables, so don't calculate the
    6015              :      translations.  */
    6016       190353 :   if (!loop->array_parameter)
    6017       116755 :     gfc_set_delta (loop);
    6018       190353 : }
    6019              : 
    6020              : 
    6021              : /* Calculates how to transform from loop variables to array indices for each
    6022              :    array: once loop bounds are chosen, sets the difference (DELTA field) between
    6023              :    loop bounds and array reference bounds, for each array info.  */
    6024              : 
    6025              : void
    6026       120586 : gfc_set_delta (gfc_loopinfo *loop)
    6027              : {
    6028       120586 :   gfc_ss *ss, **loopspec;
    6029       120586 :   gfc_array_info *info;
    6030       120586 :   tree tmp;
    6031       120586 :   int n, dim;
    6032              : 
    6033       120586 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    6034              : 
    6035       120586 :   loopspec = loop->specloop;
    6036              : 
    6037              :   /* Calculate the translation from loop variables to array indices.  */
    6038       364866 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    6039              :     {
    6040       244280 :       gfc_ss_type ss_type;
    6041              : 
    6042       244280 :       ss_type = ss->info->type;
    6043        62418 :       if (!(ss_type == GFC_SS_SECTION
    6044       244280 :             || ss_type == GFC_SS_COMPONENT
    6045        99726 :             || ss_type == GFC_SS_CONSTRUCTOR
    6046              :             || (ss_type == GFC_SS_FUNCTION
    6047         8446 :                 && gfc_is_class_array_function (ss->info->expr))))
    6048        62260 :         continue;
    6049              : 
    6050       182020 :       info = &ss->info->data.array;
    6051              : 
    6052       408386 :       for (n = 0; n < ss->dimen; n++)
    6053              :         {
    6054              :           /* If we are specifying the range the delta is already set.  */
    6055       226366 :           if (loopspec[n] != ss)
    6056              :             {
    6057       117867 :               dim = ss->dim[n];
    6058              : 
    6059              :               /* Calculate the offset relative to the loop variable.
    6060              :                  First multiply by the stride.  */
    6061       117867 :               tmp = loop->from[n];
    6062       117867 :               if (!integer_onep (info->stride[dim]))
    6063         3132 :                 tmp = fold_build2_loc (input_location, MULT_EXPR,
    6064              :                                        gfc_array_index_type,
    6065              :                                        tmp, info->stride[dim]);
    6066              : 
    6067              :               /* Then subtract this from our starting value.  */
    6068       117867 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    6069              :                                      gfc_array_index_type,
    6070              :                                      info->start[dim], tmp);
    6071              : 
    6072       117867 :               if (ss->is_alloc_lhs)
    6073         9834 :                 info->delta[dim] = tmp;
    6074              :               else
    6075       108033 :                 info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
    6076              :             }
    6077              :         }
    6078              :     }
    6079              : 
    6080       124038 :   for (loop = loop->nested; loop; loop = loop->next)
    6081         3452 :     gfc_set_delta (loop);
    6082       120586 : }
    6083              : 
    6084              : 
    6085              : /* Calculate the size of a given array dimension from the bounds.  This
    6086              :    is simply (ubound - lbound + 1) if this expression is positive
    6087              :    or 0 if it is negative (pick either one if it is zero).  Optionally
    6088              :    (if or_expr is present) OR the (expression != 0) condition to it.  */
    6089              : 
    6090              : tree
    6091        30670 : gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
    6092              : {
    6093        30670 :   tree res;
    6094        30670 :   tree cond;
    6095              : 
    6096              :   /* Calculate (ubound - lbound + 1).  */
    6097        30670 :   res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6098              :                          ubound, lbound);
    6099        30670 :   res = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, res,
    6100              :                          gfc_index_one_node);
    6101              : 
    6102              :   /* Check whether the size for this dimension is negative.  */
    6103        30670 :   cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
    6104              :                           gfc_index_zero_node);
    6105        30670 :   res = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
    6106              :                          gfc_index_zero_node, res);
    6107              : 
    6108              :   /* Build OR expression.  */
    6109        30670 :   if (or_expr)
    6110        18378 :     *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    6111              :                                 logical_type_node, *or_expr, cond);
    6112              : 
    6113        30670 :   return res;
    6114              : }
    6115              : 
    6116              : 
    6117              : /* Fills in an array descriptor, and returns the size of the array.
    6118              :    The size will be a simple_val, ie a variable or a constant.  Also
    6119              :    calculates the offset of the base.  The pointer argument overflow,
    6120              :    which should be of integer type, will increase in value if overflow
    6121              :    occurs during the size calculation.  Returns the size of the array.
    6122              :    {
    6123              :     stride = 1;
    6124              :     offset = 0;
    6125              :     for (n = 0; n < rank; n++)
    6126              :       {
    6127              :         a.lbound[n] = specified_lower_bound;
    6128              :         offset = offset + a.lbond[n] * stride;
    6129              :         size = 1 - lbound;
    6130              :         a.ubound[n] = specified_upper_bound;
    6131              :         a.stride[n] = stride;
    6132              :         size = size >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
    6133              :         overflow += size == 0 ? 0: (MAX/size < stride ? 1: 0);
    6134              :         stride = stride * size;
    6135              :       }
    6136              :     for (n = rank; n < rank+corank; n++)
    6137              :       (Set lcobound/ucobound as above.)
    6138              :     element_size = sizeof (array element);
    6139              :     if (!rank)
    6140              :       return element_size
    6141              :     stride = (size_t) stride;
    6142              :     overflow += element_size == 0 ? 0: (MAX/element_size < stride ? 1: 0);
    6143              :     stride = stride * element_size;
    6144              :     return (stride);
    6145              :    }  */
    6146              : /*GCC ARRAYS*/
    6147              : 
    6148              : static tree
    6149        12702 : gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
    6150              :                      gfc_expr ** lower, gfc_expr ** upper, stmtblock_t * pblock,
    6151              :                      stmtblock_t * descriptor_block, tree * overflow,
    6152              :                      tree expr3_elem_size, gfc_expr *expr3, tree expr3_desc,
    6153              :                      bool e3_has_nodescriptor, gfc_expr *expr,
    6154              :                      tree *element_size, bool explicit_ts)
    6155              : {
    6156        12702 :   tree type;
    6157        12702 :   tree tmp;
    6158        12702 :   tree size;
    6159        12702 :   tree offset;
    6160        12702 :   tree stride;
    6161        12702 :   tree or_expr;
    6162        12702 :   tree thencase;
    6163        12702 :   tree elsecase;
    6164        12702 :   tree cond;
    6165        12702 :   tree var;
    6166        12702 :   stmtblock_t thenblock;
    6167        12702 :   stmtblock_t elseblock;
    6168        12702 :   gfc_expr *ubound;
    6169        12702 :   gfc_se se;
    6170        12702 :   int n;
    6171              : 
    6172        12702 :   if (expr->ts.type == BT_CLASS
    6173         1857 :       && expr3_desc != NULL_TREE
    6174        13095 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
    6175          393 :     type = TREE_TYPE (expr3_desc);
    6176              :   else
    6177        12309 :     type = TREE_TYPE (descriptor);
    6178              : 
    6179              : 
    6180        12702 :   stride = gfc_index_one_node;
    6181        12702 :   offset = gfc_index_zero_node;
    6182              : 
    6183              :   /* Set the dtype before the alloc, because registration of coarrays needs
    6184              :      it initialized.  */
    6185        12702 :   if (expr->ts.type == BT_CHARACTER
    6186         1103 :       && expr->ts.deferred
    6187          563 :       && VAR_P (expr->ts.u.cl->backend_decl))
    6188              :     {
    6189          378 :       type = gfc_typenode_for_spec (&expr->ts);
    6190          378 :       gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6191              :                                      gfc_get_dtype_rank_type (rank, type));
    6192              :     }
    6193        12324 :   else if (expr->ts.type == BT_CHARACTER
    6194          725 :            && expr->ts.deferred
    6195          185 :            && TREE_CODE (descriptor) == COMPONENT_REF)
    6196              :     {
    6197              :       /* Deferred character components have their string length tucked away
    6198              :          in a hidden field of the derived type. Obtain that and use it to
    6199              :          set the dtype. The charlen backend decl is zero because the field
    6200              :          type is zero length.  */
    6201          167 :       gfc_ref *ref;
    6202          167 :       tmp = NULL_TREE;
    6203          167 :       for (ref = expr->ref; ref; ref = ref->next)
    6204          167 :         if (ref->type == REF_COMPONENT
    6205          167 :             && gfc_deferred_strlen (ref->u.c.component, &tmp))
    6206              :           break;
    6207          167 :       gcc_assert (tmp != NULL_TREE);
    6208          167 :       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
    6209          167 :                              TREE_OPERAND (descriptor, 0), tmp, NULL_TREE);
    6210          167 :       tmp = fold_convert (gfc_charlen_type_node, tmp);
    6211          167 :       type = gfc_get_character_type_len (expr->ts.kind, tmp);
    6212          167 :       gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6213              :                                      gfc_get_dtype_rank_type (rank, type));
    6214          167 :     }
    6215        12157 :   else if (expr3_desc && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
    6216          983 :     gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6217              :                                    gfc_conv_descriptor_dtype_get (expr3_desc));
    6218        11174 :   else if (expr->ts.type == BT_CLASS && !explicit_ts
    6219         1464 :            && expr3 && expr3->ts.type != BT_CLASS
    6220          380 :            && expr3_elem_size != NULL_TREE && expr3_desc == NULL_TREE)
    6221              :     {
    6222          380 :       gfc_conv_descriptor_dtype_set (pblock, descriptor, gfc_get_dtype (type));
    6223          380 :       gfc_conv_descriptor_elem_len_set (pblock, descriptor, expr3_elem_size);
    6224              :     }
    6225              :   else
    6226        10794 :     gfc_conv_descriptor_dtype_set (pblock, descriptor, gfc_get_dtype (type));
    6227              : 
    6228        12702 :   or_expr = logical_false_node;
    6229              : 
    6230        31080 :   for (n = 0; n < rank; n++)
    6231              :     {
    6232        18378 :       tree conv_lbound;
    6233        18378 :       tree conv_ubound;
    6234              : 
    6235              :       /* We have 3 possibilities for determining the size of the array:
    6236              :          lower == NULL    => lbound = 1, ubound = upper[n]
    6237              :          upper[n] = NULL  => lbound = 1, ubound = lower[n]
    6238              :          upper[n] != NULL => lbound = lower[n], ubound = upper[n]  */
    6239        18378 :       ubound = upper[n];
    6240              : 
    6241              :       /* Set lower bound.  */
    6242        18378 :       gfc_init_se (&se, NULL);
    6243        18378 :       if (expr3_desc != NULL_TREE)
    6244              :         {
    6245         1526 :           if (e3_has_nodescriptor)
    6246              :             /* The lbound of nondescriptor arrays like array constructors,
    6247              :                nonallocatable/nonpointer function results/variables,
    6248              :                start at zero, but when allocating it, the standard expects
    6249              :                the array to start at one.  */
    6250          926 :             se.expr = gfc_index_one_node;
    6251              :           else
    6252          600 :             se.expr = gfc_conv_descriptor_lbound_get (expr3_desc,
    6253              :                                                       gfc_rank_cst[n]);
    6254              :         }
    6255        16852 :       else if (lower == NULL)
    6256        13632 :         se.expr = gfc_index_one_node;
    6257              :       else
    6258              :         {
    6259         3220 :           gcc_assert (lower[n]);
    6260         3220 :           if (ubound)
    6261              :             {
    6262         2457 :               gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
    6263         2457 :               gfc_add_block_to_block (pblock, &se.pre);
    6264              :             }
    6265              :           else
    6266              :             {
    6267          763 :               se.expr = gfc_index_one_node;
    6268          763 :               ubound = lower[n];
    6269              :             }
    6270              :         }
    6271        18378 :       gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
    6272              :                                       gfc_rank_cst[n], se.expr);
    6273        18378 :       conv_lbound = se.expr;
    6274              : 
    6275              :       /* Work out the offset for this component.  */
    6276        18378 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6277              :                              se.expr, stride);
    6278        18378 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    6279              :                                 gfc_array_index_type, offset, tmp);
    6280              : 
    6281              :       /* Set upper bound.  */
    6282        18378 :       gfc_init_se (&se, NULL);
    6283        18378 :       if (expr3_desc != NULL_TREE)
    6284              :         {
    6285         1526 :           if (e3_has_nodescriptor)
    6286              :             {
    6287              :               /* The lbound of nondescriptor arrays like array constructors,
    6288              :                  nonallocatable/nonpointer function results/variables,
    6289              :                  start at zero, but when allocating it, the standard expects
    6290              :                  the array to start at one.  Therefore fix the upper bound to be
    6291              :                  (desc.ubound - desc.lbound) + 1.  */
    6292          926 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    6293              :                                      gfc_array_index_type,
    6294              :                                      gfc_conv_descriptor_ubound_get (
    6295              :                                        expr3_desc, gfc_rank_cst[n]),
    6296              :                                      gfc_conv_descriptor_lbound_get (
    6297              :                                        expr3_desc, gfc_rank_cst[n]));
    6298          926 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    6299              :                                      gfc_array_index_type, tmp,
    6300              :                                      gfc_index_one_node);
    6301          926 :               se.expr = gfc_evaluate_now (tmp, pblock);
    6302              :             }
    6303              :           else
    6304          600 :             se.expr = gfc_conv_descriptor_ubound_get (expr3_desc,
    6305              :                                                       gfc_rank_cst[n]);
    6306              :         }
    6307              :       else
    6308              :         {
    6309        16852 :           gcc_assert (ubound);
    6310        16852 :           gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
    6311        16852 :           gfc_add_block_to_block (pblock, &se.pre);
    6312        16852 :           if (ubound->expr_type == EXPR_FUNCTION)
    6313          781 :             se.expr = gfc_evaluate_now (se.expr, pblock);
    6314              :         }
    6315        18378 :       gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
    6316              :                                       gfc_rank_cst[n], se.expr);
    6317        18378 :       conv_ubound = se.expr;
    6318              : 
    6319              :       /* Store the stride.  */
    6320        18378 :       gfc_conv_descriptor_stride_set (descriptor_block, descriptor,
    6321              :                                       gfc_rank_cst[n], stride);
    6322              : 
    6323              :       /* Calculate size and check whether extent is negative.  */
    6324        18378 :       size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
    6325        18378 :       size = gfc_evaluate_now (size, pblock);
    6326              : 
    6327              :       /* Check whether multiplying the stride by the number of
    6328              :          elements in this dimension would overflow. We must also check
    6329              :          whether the current dimension has zero size in order to avoid
    6330              :          division by zero.
    6331              :       */
    6332        18378 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6333              :                              gfc_array_index_type,
    6334        18378 :                              fold_convert (gfc_array_index_type,
    6335              :                                            TYPE_MAX_VALUE (gfc_array_index_type)),
    6336              :                                            size);
    6337        18378 :       cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
    6338              :                                             logical_type_node, tmp, stride),
    6339              :                            PRED_FORTRAN_OVERFLOW);
    6340        18378 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6341              :                              integer_one_node, integer_zero_node);
    6342        18378 :       cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
    6343              :                                             logical_type_node, size,
    6344              :                                             gfc_index_zero_node),
    6345              :                            PRED_FORTRAN_SIZE_ZERO);
    6346        18378 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6347              :                              integer_zero_node, tmp);
    6348        18378 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    6349              :                              *overflow, tmp);
    6350        18378 :       *overflow = gfc_evaluate_now (tmp, pblock);
    6351              : 
    6352              :       /* Multiply the stride by the number of elements in this dimension.  */
    6353        18378 :       stride = fold_build2_loc (input_location, MULT_EXPR,
    6354              :                                 gfc_array_index_type, stride, size);
    6355        18378 :       stride = gfc_evaluate_now (stride, pblock);
    6356              :     }
    6357              : 
    6358        13427 :   for (n = rank; n < rank + corank; n++)
    6359              :     {
    6360          725 :       ubound = upper[n];
    6361              : 
    6362              :       /* Set lower bound.  */
    6363          725 :       gfc_init_se (&se, NULL);
    6364          725 :       if (lower == NULL || lower[n] == NULL)
    6365              :         {
    6366          444 :           gcc_assert (n == rank + corank - 1);
    6367          444 :           se.expr = gfc_index_one_node;
    6368              :         }
    6369              :       else
    6370              :         {
    6371          281 :           if (ubound || n == rank + corank - 1)
    6372              :             {
    6373          184 :               gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
    6374          184 :               gfc_add_block_to_block (pblock, &se.pre);
    6375              :             }
    6376              :           else
    6377              :             {
    6378           97 :               se.expr = gfc_index_one_node;
    6379           97 :               ubound = lower[n];
    6380              :             }
    6381              :         }
    6382          725 :       gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
    6383              :                                       gfc_rank_cst[n], se.expr);
    6384              : 
    6385          725 :       if (n < rank + corank - 1)
    6386              :         {
    6387          181 :           gfc_init_se (&se, NULL);
    6388          181 :           gcc_assert (ubound);
    6389          181 :           gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
    6390          181 :           gfc_add_block_to_block (pblock, &se.pre);
    6391          181 :           gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
    6392              :                                           gfc_rank_cst[n], se.expr);
    6393              :         }
    6394              :     }
    6395              : 
    6396              :   /* The stride is the number of elements in the array, so multiply by the
    6397              :      size of an element to get the total size.  Obviously, if there is a
    6398              :      SOURCE expression (expr3) we must use its element size.  */
    6399        12702 :   if (expr3_elem_size != NULL_TREE)
    6400         3279 :     tmp = expr3_elem_size;
    6401         9423 :   else if (expr3 != NULL)
    6402              :     {
    6403            0 :       if (expr3->ts.type == BT_CLASS)
    6404              :         {
    6405            0 :           gfc_se se_sz;
    6406            0 :           gfc_expr *sz = gfc_copy_expr (expr3);
    6407            0 :           gfc_add_vptr_component (sz);
    6408            0 :           gfc_add_size_component (sz);
    6409            0 :           gfc_init_se (&se_sz, NULL);
    6410            0 :           gfc_conv_expr (&se_sz, sz);
    6411            0 :           gfc_free_expr (sz);
    6412            0 :           tmp = se_sz.expr;
    6413              :         }
    6414              :       else
    6415              :         {
    6416            0 :           tmp = gfc_typenode_for_spec (&expr3->ts);
    6417            0 :           tmp = TYPE_SIZE_UNIT (tmp);
    6418              :         }
    6419              :     }
    6420              :   else
    6421         9423 :     tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    6422              : 
    6423              :   /* Convert to size_t.  */
    6424        12702 :   *element_size = fold_convert (size_type_node, tmp);
    6425              : 
    6426        12702 :   if (rank == 0)
    6427              :     return *element_size;
    6428              : 
    6429        12449 :   stride = fold_convert (size_type_node, stride);
    6430              : 
    6431              :   /* First check for overflow. Since an array of type character can
    6432              :      have zero element_size, we must check for that before
    6433              :      dividing.  */
    6434        12449 :   tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6435              :                          size_type_node,
    6436        12449 :                          TYPE_MAX_VALUE (size_type_node), *element_size);
    6437        12449 :   cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
    6438              :                                         logical_type_node, tmp, stride),
    6439              :                        PRED_FORTRAN_OVERFLOW);
    6440        12449 :   tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6441              :                          integer_one_node, integer_zero_node);
    6442        12449 :   cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
    6443              :                                         logical_type_node, *element_size,
    6444              :                                         build_int_cst (size_type_node, 0)),
    6445              :                        PRED_FORTRAN_SIZE_ZERO);
    6446        12449 :   tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6447              :                          integer_zero_node, tmp);
    6448        12449 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    6449              :                          *overflow, tmp);
    6450        12449 :   *overflow = gfc_evaluate_now (tmp, pblock);
    6451              : 
    6452        12449 :   size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6453              :                           stride, *element_size);
    6454              : 
    6455        12449 :   if (poffset != NULL)
    6456              :     {
    6457        12449 :       offset = gfc_evaluate_now (offset, pblock);
    6458        12449 :       *poffset = offset;
    6459              :     }
    6460              : 
    6461        12449 :   if (integer_zerop (or_expr))
    6462              :     return size;
    6463         3742 :   if (integer_onep (or_expr))
    6464          605 :     return build_int_cst (size_type_node, 0);
    6465              : 
    6466         3137 :   var = gfc_create_var (TREE_TYPE (size), "size");
    6467         3137 :   gfc_start_block (&thenblock);
    6468         3137 :   gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
    6469         3137 :   thencase = gfc_finish_block (&thenblock);
    6470              : 
    6471         3137 :   gfc_start_block (&elseblock);
    6472         3137 :   gfc_add_modify (&elseblock, var, size);
    6473         3137 :   elsecase = gfc_finish_block (&elseblock);
    6474              : 
    6475         3137 :   tmp = gfc_evaluate_now (or_expr, pblock);
    6476         3137 :   tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
    6477         3137 :   gfc_add_expr_to_block (pblock, tmp);
    6478              : 
    6479         3137 :   return var;
    6480              : }
    6481              : 
    6482              : 
    6483              : /* Retrieve the last ref from the chain.  This routine is specific to
    6484              :    gfc_array_allocate ()'s needs.  */
    6485              : 
    6486              : bool
    6487        18270 : retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
    6488              : {
    6489        18270 :   gfc_ref *ref, *prev_ref;
    6490              : 
    6491        18270 :   ref = *ref_in;
    6492              :   /* Prevent warnings for uninitialized variables.  */
    6493        18270 :   prev_ref = *prev_ref_in;
    6494        26047 :   while (ref && ref->next != NULL)
    6495              :     {
    6496         7777 :       gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
    6497              :                   || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
    6498         7777 :       prev_ref = ref;
    6499         7777 :       ref = ref->next;
    6500              :     }
    6501              : 
    6502        18270 :   if (ref == NULL || ref->type != REF_ARRAY)
    6503              :     return false;
    6504              : 
    6505        12702 :   *ref_in = ref;
    6506        12702 :   *prev_ref_in = prev_ref;
    6507        12702 :   return true;
    6508              : }
    6509              : 
    6510              : /* Initializes the descriptor and generates a call to _gfor_allocate.  Does
    6511              :    the work for an ALLOCATE statement.  */
    6512              : /*GCC ARRAYS*/
    6513              : 
    6514              : bool
    6515        18270 : gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
    6516              :                     tree errlen, tree label_finish, tree expr3_elem_size,
    6517              :                     gfc_expr *expr3, tree e3_arr_desc, bool e3_has_nodescriptor,
    6518              :                     gfc_omp_namelist *omp_alloc, bool explicit_ts)
    6519              : {
    6520        18270 :   tree tmp;
    6521        18270 :   tree pointer;
    6522        18270 :   tree offset = NULL_TREE;
    6523        18270 :   tree token = NULL_TREE;
    6524        18270 :   tree size;
    6525        18270 :   tree msg;
    6526        18270 :   tree error = NULL_TREE;
    6527        18270 :   tree overflow; /* Boolean storing whether size calculation overflows.  */
    6528        18270 :   tree var_overflow = NULL_TREE;
    6529        18270 :   tree cond;
    6530        18270 :   tree set_descriptor;
    6531        18270 :   tree not_prev_allocated = NULL_TREE;
    6532        18270 :   tree element_size = NULL_TREE;
    6533        18270 :   stmtblock_t set_descriptor_block;
    6534        18270 :   stmtblock_t elseblock;
    6535        18270 :   gfc_expr **lower;
    6536        18270 :   gfc_expr **upper;
    6537        18270 :   gfc_ref *ref, *prev_ref = NULL, *coref;
    6538        18270 :   bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
    6539              :       non_ulimate_coarray_ptr_comp;
    6540        18270 :   tree omp_cond = NULL_TREE, omp_alt_alloc = NULL_TREE;
    6541              : 
    6542        18270 :   ref = expr->ref;
    6543              : 
    6544              :   /* Find the last reference in the chain.  */
    6545        18270 :   if (!retrieve_last_ref (&ref, &prev_ref))
    6546              :     return false;
    6547              : 
    6548              :   /* Take the allocatable and coarray properties solely from the expr-ref's
    6549              :      attributes and not from source=-expression.  */
    6550        12702 :   if (!prev_ref)
    6551              :     {
    6552         8531 :       allocatable = expr->symtree->n.sym->attr.allocatable;
    6553         8531 :       dimension = expr->symtree->n.sym->attr.dimension;
    6554         8531 :       non_ulimate_coarray_ptr_comp = false;
    6555              :     }
    6556              :   else
    6557              :     {
    6558         4171 :       allocatable = prev_ref->u.c.component->attr.allocatable;
    6559              :       /* Pointer components in coarrayed derived types must be treated
    6560              :          specially in that they are registered without a check if the are
    6561              :          already associated.  This does not hold for ultimate coarray
    6562              :          pointers.  */
    6563         8342 :       non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
    6564         4171 :               && !prev_ref->u.c.component->attr.codimension);
    6565         4171 :       dimension = prev_ref->u.c.component->attr.dimension;
    6566              :     }
    6567              : 
    6568              :   /* For allocatable/pointer arrays in derived types, one of the refs has to be
    6569              :      a coarray.  In this case it does not matter whether we are on this_image
    6570              :      or not.  */
    6571        12702 :   coarray = false;
    6572        30690 :   for (coref = expr->ref; coref; coref = coref->next)
    6573        18713 :     if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
    6574              :       {
    6575              :         coarray = true;
    6576              :         break;
    6577              :       }
    6578              : 
    6579        12702 :   if (!dimension)
    6580          253 :     gcc_assert (coarray);
    6581              : 
    6582        12702 :   gfc_array_ref *ar;
    6583        12702 :   if (ref->u.ar.type == AR_FULL && expr3 != NULL)
    6584              :     {
    6585              :       /* F08:C633: Array shape from expr3.  */
    6586         1268 :       ar = gfc_find_array_ref (expr3, true);
    6587         1268 :       if (!ar)
    6588              :         {
    6589            0 :           if (expr3->expr_type == EXPR_FUNCTION
    6590            0 :               && gfc_expr_attr (expr3).dimension)
    6591            0 :             ar = &ref->u.ar;
    6592              :           else
    6593            0 :             return false;
    6594              :         }
    6595              :       alloc_w_e3_arr_spec = true;
    6596              :     }
    6597              :   else
    6598        11434 :     ar = &ref->u.ar;
    6599              : 
    6600              :   /* Figure out the size of the array.  */
    6601        12702 :   switch (ar->type)
    6602              :     {
    6603         9758 :     case AR_ELEMENT:
    6604         9758 :       if (!coarray)
    6605              :         {
    6606         9085 :           lower = NULL;
    6607         9085 :           upper = ar->start;
    6608         9085 :           break;
    6609              :         }
    6610              :       /* Fall through.  */
    6611              : 
    6612         2398 :     case AR_SECTION:
    6613         2398 :       lower = ar->start;
    6614         2398 :       upper = ar->end;
    6615         2398 :       break;
    6616              : 
    6617         1219 :     case AR_FULL:
    6618         1219 :       gcc_assert (ar->as->type == AS_EXPLICIT
    6619              :                   || alloc_w_e3_arr_spec);
    6620              : 
    6621         1219 :       lower = ar->as->lower;
    6622         1219 :       upper = ar->as->upper;
    6623         1219 :       break;
    6624              : 
    6625            0 :     default:
    6626            0 :       gcc_unreachable ();
    6627        12702 :       break;
    6628              :     }
    6629              : 
    6630        12702 :   overflow = integer_zero_node;
    6631              : 
    6632        12702 :   if (expr->ts.type == BT_CHARACTER
    6633         1103 :       && TREE_CODE (se->string_length) == COMPONENT_REF
    6634          167 :       && expr->ts.u.cl->backend_decl != se->string_length
    6635          167 :       && VAR_P (expr->ts.u.cl->backend_decl))
    6636            0 :     gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    6637            0 :                     fold_convert (TREE_TYPE (expr->ts.u.cl->backend_decl),
    6638              :                                   se->string_length));
    6639              : 
    6640        12702 :   gfc_init_block (&set_descriptor_block);
    6641              :   /* Take the corank only from the actual ref and not from the coref.  The
    6642              :      later will mislead the generation of the array dimensions for allocatable/
    6643              :      pointer components in derived types.  */
    6644        24861 :   size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
    6645        11434 :                                                            : ar->as->rank,
    6646          725 :                               coarray ? ar->as->corank : 0,
    6647              :                               &offset, lower, upper,
    6648              :                               &se->pre, &set_descriptor_block, &overflow,
    6649              :                               expr3_elem_size, expr3, e3_arr_desc,
    6650              :                               e3_has_nodescriptor, expr, &element_size,
    6651              :                               explicit_ts);
    6652              : 
    6653        12702 :   if (dimension)
    6654              :     {
    6655        12449 :       var_overflow = gfc_create_var (integer_type_node, "overflow");
    6656        12449 :       gfc_add_modify (&se->pre, var_overflow, overflow);
    6657              : 
    6658        12449 :       if (status == NULL_TREE)
    6659              :         {
    6660              :           /* Generate the block of code handling overflow.  */
    6661        12208 :           msg = gfc_build_addr_expr (pchar_type_node,
    6662              :                     gfc_build_localized_cstring_const
    6663              :                         ("Integer overflow when calculating the amount of "
    6664              :                          "memory to allocate"));
    6665        12208 :           error = build_call_expr_loc (input_location,
    6666              :                                        gfor_fndecl_runtime_error, 1, msg);
    6667              :         }
    6668              :       else
    6669              :         {
    6670          241 :           tree status_type = TREE_TYPE (status);
    6671          241 :           stmtblock_t set_status_block;
    6672              : 
    6673          241 :           gfc_start_block (&set_status_block);
    6674          241 :           gfc_add_modify (&set_status_block, status,
    6675              :                           build_int_cst (status_type, LIBERROR_NO_MEMORY));
    6676          241 :           error = gfc_finish_block (&set_status_block);
    6677              :         }
    6678              :     }
    6679              : 
    6680              :   /* Allocate memory to store the data.  */
    6681        12702 :   if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
    6682            0 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    6683              : 
    6684        12702 :   if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
    6685              :     {
    6686          465 :       pointer = non_ulimate_coarray_ptr_comp ? se->expr
    6687          393 :                                       : gfc_conv_descriptor_data_get (se->expr);
    6688          465 :       token = gfc_conv_descriptor_token (se->expr);
    6689          465 :       token = gfc_build_addr_expr (NULL_TREE, token);
    6690              :     }
    6691              :   else
    6692              :     {
    6693        12237 :       pointer = gfc_conv_descriptor_data_get (se->expr);
    6694        12237 :       if (omp_alloc)
    6695           33 :         omp_cond = boolean_true_node;
    6696              :     }
    6697        12702 :   STRIP_NOPS (pointer);
    6698              : 
    6699        12702 :   if (allocatable)
    6700              :     {
    6701        10491 :       not_prev_allocated = gfc_create_var (logical_type_node,
    6702              :                                            "not_prev_allocated");
    6703        10491 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
    6704              :                              logical_type_node, pointer,
    6705        10491 :                              build_int_cst (TREE_TYPE (pointer), 0));
    6706              : 
    6707        10491 :       gfc_add_modify (&se->pre, not_prev_allocated, tmp);
    6708              :     }
    6709              : 
    6710        12702 :   gfc_start_block (&elseblock);
    6711              : 
    6712        12702 :   tree succ_add_expr = NULL_TREE;
    6713        12702 :   if (omp_cond)
    6714              :     {
    6715           33 :       tree align, alloc, sz;
    6716           33 :       gfc_se se2;
    6717           33 :       if (omp_alloc->u2.allocator)
    6718              :         {
    6719           10 :           gfc_init_se (&se2, NULL);
    6720           10 :           gfc_conv_expr (&se2, omp_alloc->u2.allocator);
    6721           10 :           gfc_add_block_to_block (&elseblock, &se2.pre);
    6722           10 :           alloc = gfc_evaluate_now (se2.expr, &elseblock);
    6723           10 :           gfc_add_block_to_block (&elseblock, &se2.post);
    6724              :         }
    6725              :       else
    6726           23 :         alloc = build_zero_cst (ptr_type_node);
    6727           33 :       tmp = TREE_TYPE (TREE_TYPE (pointer));
    6728           33 :       if (tmp == void_type_node)
    6729           33 :         tmp = gfc_typenode_for_spec (&expr->ts, 0);
    6730           33 :       if (omp_alloc->u.align)
    6731              :         {
    6732           17 :           gfc_init_se (&se2, NULL);
    6733           17 :           gfc_conv_expr (&se2, omp_alloc->u.align);
    6734           17 :           gcc_assert (CONSTANT_CLASS_P (se2.expr)
    6735              :                       && se2.pre.head == NULL
    6736              :                       && se2.post.head == NULL);
    6737           17 :           align = build_int_cst (size_type_node,
    6738           17 :                                  MAX (tree_to_uhwi (se2.expr),
    6739              :                                       TYPE_ALIGN_UNIT (tmp)));
    6740              :         }
    6741              :       else
    6742           16 :         align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
    6743           33 :       sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
    6744              :                             fold_convert (size_type_node, size),
    6745              :                             build_int_cst (size_type_node, 1));
    6746           33 :       omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
    6747           33 :       DECL_ATTRIBUTES (omp_alt_alloc)
    6748           33 :         = tree_cons (get_identifier ("omp allocator"),
    6749              :                      build_tree_list (NULL_TREE, alloc),
    6750           33 :                      DECL_ATTRIBUTES (omp_alt_alloc));
    6751           33 :       omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
    6752           33 :       stmtblock_t tmp_block;
    6753           33 :       gfc_init_block (&tmp_block);
    6754           33 :       gfc_conv_descriptor_version_set (&tmp_block, se->expr, integer_one_node);
    6755           33 :       succ_add_expr = gfc_finish_block (&tmp_block);
    6756              :     }
    6757              : 
    6758              :   /* The allocatable variant takes the old pointer as first argument.  */
    6759        12702 :   if (allocatable)
    6760        11141 :     gfc_allocate_allocatable (&elseblock, pointer, size, token,
    6761              :                               status, errmsg, errlen, label_finish, expr,
    6762          650 :                               coref != NULL ? coref->u.ar.as->corank : 0,
    6763              :                               omp_cond, omp_alt_alloc, succ_add_expr,
    6764              :                               var_overflow);
    6765         2211 :   else if (non_ulimate_coarray_ptr_comp && token)
    6766              :     /* The token is set only for GFC_FCOARRAY_LIB mode.  */
    6767           72 :     gfc_allocate_using_caf_lib (&elseblock, pointer, size, token, status,
    6768              :                                 errmsg, errlen,
    6769              :                                 GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY);
    6770              :   else
    6771         2139 :     gfc_allocate_using_malloc (&elseblock, pointer, size, status,
    6772              :                                omp_cond, omp_alt_alloc, succ_add_expr);
    6773              : 
    6774              :   /* When STATUS is present, we give precedence to "already allocated variable"
    6775              :      error over overflow error in gfc_allocate_allocatable.  */
    6776        12702 :   if (dimension && !(allocatable && status))
    6777              :     {
    6778        12226 :       cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
    6779              :                            logical_type_node, var_overflow, integer_zero_node),
    6780              :                            PRED_FORTRAN_OVERFLOW);
    6781        12226 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6782              :                              error, gfc_finish_block (&elseblock));
    6783              :     }
    6784              :   else
    6785          476 :     tmp = gfc_finish_block (&elseblock);
    6786              : 
    6787        12702 :   gfc_add_expr_to_block (&se->pre, tmp);
    6788              : 
    6789              :   /* Update the array descriptor with the offset and the span.  */
    6790        12702 :   if (dimension)
    6791              :     {
    6792        12449 :       gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
    6793        12449 :       tmp = fold_convert (gfc_array_index_type, element_size);
    6794        12449 :       gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
    6795              :     }
    6796              : 
    6797        12702 :   set_descriptor = gfc_finish_block (&set_descriptor_block);
    6798        12702 :   if (status != NULL_TREE)
    6799              :     {
    6800          263 :       cond = fold_build2_loc (input_location, EQ_EXPR,
    6801              :                           logical_type_node, status,
    6802          263 :                           build_int_cst (TREE_TYPE (status), 0));
    6803              : 
    6804          263 :       if (not_prev_allocated != NULL_TREE)
    6805          245 :         cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    6806              :                                 logical_type_node, cond, not_prev_allocated);
    6807              : 
    6808          263 :       gfc_add_expr_to_block (&se->pre,
    6809              :                  fold_build3_loc (input_location, COND_EXPR, void_type_node,
    6810              :                                   cond,
    6811              :                                   set_descriptor,
    6812              :                                   build_empty_stmt (input_location)));
    6813              :     }
    6814              :   else
    6815        12439 :       gfc_add_expr_to_block (&se->pre, set_descriptor);
    6816              : 
    6817              :   return true;
    6818              : }
    6819              : 
    6820              : 
    6821              : /* Create an array constructor from an initialization expression.
    6822              :    We assume the frontend already did any expansions and conversions.  */
    6823              : 
    6824              : tree
    6825         8319 : gfc_conv_array_initializer (tree type, gfc_expr * expr)
    6826              : {
    6827         8319 :   gfc_constructor *c;
    6828         8319 :   tree tmp;
    6829         8319 :   gfc_se se;
    6830         8319 :   tree index, range;
    6831         8319 :   vec<constructor_elt, va_gc> *v = NULL;
    6832              : 
    6833         8319 :   if (expr->expr_type == EXPR_VARIABLE
    6834            1 :       && expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    6835            1 :       && expr->symtree->n.sym->value
    6836            1 :       && !expr->ref)
    6837         8319 :     expr = expr->symtree->n.sym->value;
    6838              : 
    6839              :   /* After parameter substitution the expression should be a constant, array
    6840              :      constructor, structure constructor, or NULL.  Anything else is invalid
    6841              :      and must not ICE later in lowering.  */
    6842         8319 :   if (expr->expr_type != EXPR_CONSTANT
    6843         7810 :       && expr->expr_type != EXPR_STRUCTURE
    6844         6898 :       && expr->expr_type != EXPR_ARRAY
    6845            4 :       && expr->expr_type != EXPR_NULL)
    6846              :     {
    6847            4 :       gfc_error ("Array initializer at %L does not reduce to a constant "
    6848              :                  "expression", &expr->where);
    6849            4 :       return build_constructor (type, NULL);
    6850              :     }
    6851              : 
    6852         8315 :   switch (expr->expr_type)
    6853              :     {
    6854         1421 :     case EXPR_CONSTANT:
    6855         1421 :     case EXPR_STRUCTURE:
    6856              :       /* A single scalar or derived type value.  Create an array with all
    6857              :          elements equal to that value.  */
    6858         1421 :       gfc_init_se (&se, NULL);
    6859              : 
    6860         1421 :       if (expr->expr_type == EXPR_CONSTANT)
    6861          509 :         gfc_conv_constant (&se, expr);
    6862              :       else
    6863          912 :         gfc_conv_structure (&se, expr, 1);
    6864              : 
    6865         2842 :       if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
    6866         1421 :                            TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
    6867              :         break;
    6868         2818 :       else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
    6869         1409 :                                    TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
    6870          179 :         range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
    6871              :       else
    6872         2460 :         range = build2 (RANGE_EXPR, gfc_array_index_type,
    6873         1230 :                         TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
    6874         1230 :                         TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
    6875         1409 :       CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
    6876         1409 :       break;
    6877              : 
    6878         6894 :     case EXPR_ARRAY:
    6879              :       /* Create a vector of all the elements.  */
    6880         6894 :       for (c = gfc_constructor_first (expr->value.constructor);
    6881       166515 :            c && c->expr; c = gfc_constructor_next (c))
    6882              :         {
    6883       159621 :           if (c->iterator)
    6884              :             {
    6885              :               /* Problems occur when we get something like
    6886              :                  integer :: a(lots) = (/(i, i=1, lots)/)  */
    6887            0 :               gfc_fatal_error ("The number of elements in the array "
    6888              :                                "constructor at %L requires an increase of "
    6889              :                                "the allowed %d upper limit. See "
    6890              :                                "%<-fmax-array-constructor%> option",
    6891              :                                &expr->where, flag_max_array_constructor);
    6892              :               return NULL_TREE;
    6893              :             }
    6894       159621 :           index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
    6895              : 
    6896       159621 :           if (mpz_cmp_si (c->repeat, 1) > 0)
    6897              :             {
    6898          127 :               tree tmp1, tmp2;
    6899          127 :               mpz_t maxval;
    6900              : 
    6901          127 :               mpz_init (maxval);
    6902          127 :               mpz_add (maxval, c->offset, c->repeat);
    6903          127 :               mpz_sub_ui (maxval, maxval, 1);
    6904          127 :               tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
    6905          127 :               if (mpz_cmp_si (c->offset, 0) != 0)
    6906              :                 {
    6907           27 :                   mpz_add_ui (maxval, c->offset, 1);
    6908           27 :                   tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
    6909              :                 }
    6910              :               else
    6911          100 :                 tmp1 = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
    6912              : 
    6913          127 :               range = fold_build2 (RANGE_EXPR, gfc_array_index_type, tmp1, tmp2);
    6914          127 :               mpz_clear (maxval);
    6915              :             }
    6916              :           else
    6917              :             range = NULL;
    6918              : 
    6919       159621 :           gfc_init_se (&se, NULL);
    6920       159621 :           switch (c->expr->expr_type)
    6921              :             {
    6922       158096 :             case EXPR_CONSTANT:
    6923       158096 :               gfc_conv_constant (&se, c->expr);
    6924              : 
    6925              :               /* See gfortran.dg/charlen_15.f90 for instance.  */
    6926       158096 :               if (TREE_CODE (se.expr) == STRING_CST
    6927         5724 :                   && TREE_CODE (type) == ARRAY_TYPE)
    6928              :                 {
    6929              :                   tree atype = type;
    6930        11448 :                   while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
    6931         5724 :                     atype = TREE_TYPE (atype);
    6932         5724 :                   gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
    6933              :                                        == INTEGER_TYPE);
    6934         5724 :                   gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
    6935              :                                        == TREE_TYPE (atype));
    6936         5724 :                   if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
    6937         5724 :                       > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
    6938              :                     {
    6939            0 :                       unsigned HOST_WIDE_INT size
    6940            0 :                         = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
    6941            0 :                       const char *p = TREE_STRING_POINTER (se.expr);
    6942              : 
    6943            0 :                       se.expr = build_string (size, p);
    6944              :                     }
    6945         5724 :                   TREE_TYPE (se.expr) = atype;
    6946              :                 }
    6947              :               break;
    6948              : 
    6949         1525 :             case EXPR_STRUCTURE:
    6950         1525 :               gfc_conv_structure (&se, c->expr, 1);
    6951         1525 :               break;
    6952              : 
    6953            0 :             default:
    6954              :               /* Catch those occasional beasts that do not simplify
    6955              :                  for one reason or another, assuming that if they are
    6956              :                  standard defying the frontend will catch them.  */
    6957            0 :               gfc_conv_expr (&se, c->expr);
    6958            0 :               break;
    6959              :             }
    6960              : 
    6961       159621 :           if (range == NULL_TREE)
    6962       159494 :             CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
    6963              :           else
    6964              :             {
    6965          127 :               if (!integer_zerop (index))
    6966           27 :                 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
    6967       159748 :               CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
    6968              :             }
    6969              :         }
    6970              :       break;
    6971              : 
    6972            0 :     case EXPR_NULL:
    6973            0 :       return gfc_build_null_descriptor (type);
    6974              : 
    6975              :     default:
    6976              :       gcc_unreachable ();
    6977              :     }
    6978              : 
    6979              :   /* Create a constructor from the list of elements.  */
    6980         8315 :   tmp = build_constructor (type, v);
    6981         8315 :   TREE_CONSTANT (tmp) = 1;
    6982         8315 :   return tmp;
    6983              : }
    6984              : 
    6985              : 
    6986              : /* Generate code to evaluate non-constant coarray cobounds.  */
    6987              : 
    6988              : void
    6989        24361 : gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
    6990              :                           const gfc_symbol *sym)
    6991              : {
    6992        24361 :   int dim;
    6993        24361 :   tree ubound;
    6994        24361 :   tree lbound;
    6995        24361 :   gfc_se se;
    6996        24361 :   gfc_array_spec *as;
    6997              : 
    6998        24361 :   as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    6999              : 
    7000        26802 :   for (dim = as->rank; dim < as->rank + as->corank; dim++)
    7001              :     {
    7002              :       /* Evaluate non-constant array bound expressions.
    7003              :          F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
    7004              :          references a function, the result is finalized before execution of the
    7005              :          executable constructs in the scoping unit.
    7006              :          Adding the finalblocks enables this.  */
    7007         2441 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    7008         2441 :       if (as->lower[dim] && !INTEGER_CST_P (lbound))
    7009              :         {
    7010          114 :           gfc_init_se (&se, NULL);
    7011          114 :           gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
    7012          114 :           gfc_add_block_to_block (pblock, &se.pre);
    7013          114 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7014          114 :           gfc_add_modify (pblock, lbound, se.expr);
    7015              :         }
    7016         2441 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    7017         2441 :       if (as->upper[dim] && !INTEGER_CST_P (ubound))
    7018              :         {
    7019           60 :           gfc_init_se (&se, NULL);
    7020           60 :           gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
    7021           60 :           gfc_add_block_to_block (pblock, &se.pre);
    7022           60 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7023           60 :           gfc_add_modify (pblock, ubound, se.expr);
    7024              :         }
    7025              :     }
    7026        24361 : }
    7027              : 
    7028              : 
    7029              : /* Generate code to evaluate non-constant array bounds.  Sets *poffset and
    7030              :    returns the size (in elements) of the array.  */
    7031              : 
    7032              : tree
    7033        16365 : gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
    7034              :                         stmtblock_t * pblock)
    7035              : {
    7036        16365 :   gfc_array_spec *as;
    7037        16365 :   tree size;
    7038        16365 :   tree stride;
    7039        16365 :   tree offset;
    7040        16365 :   tree ubound;
    7041        16365 :   tree lbound;
    7042        16365 :   tree tmp;
    7043        16365 :   gfc_se se;
    7044              : 
    7045        16365 :   int dim;
    7046              : 
    7047        16365 :   as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    7048              : 
    7049        16365 :   size = gfc_index_one_node;
    7050        16365 :   offset = gfc_index_zero_node;
    7051        16365 :   stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
    7052        16365 :   if (stride && VAR_P (stride))
    7053          130 :     gfc_add_modify (pblock, stride, gfc_index_one_node);
    7054        34589 :   for (dim = 0; dim < as->rank; dim++)
    7055              :     {
    7056              :       /* Evaluate non-constant array bound expressions.
    7057              :          F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
    7058              :          references a function, the result is finalized before execution of the
    7059              :          executable constructs in the scoping unit.
    7060              :          Adding the finalblocks enables this.  */
    7061        18224 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    7062        18224 :       if (as->lower[dim] && !INTEGER_CST_P (lbound))
    7063              :         {
    7064          475 :           gfc_init_se (&se, NULL);
    7065          475 :           gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
    7066          475 :           gfc_add_block_to_block (pblock, &se.pre);
    7067          475 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7068          475 :           gfc_add_modify (pblock, lbound, se.expr);
    7069              :         }
    7070        18224 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    7071        18224 :       if (as->upper[dim] && !INTEGER_CST_P (ubound))
    7072              :         {
    7073        11522 :           gfc_init_se (&se, NULL);
    7074        11522 :           gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
    7075        11522 :           gfc_add_block_to_block (pblock, &se.pre);
    7076        11522 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7077        11522 :           gfc_add_modify (pblock, ubound, se.expr);
    7078              :         }
    7079              :       /* The offset of this dimension.  offset = offset - lbound * stride.  */
    7080        18224 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7081              :                              lbound, size);
    7082        18224 :       offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    7083              :                                 offset, tmp);
    7084              : 
    7085              :       /* The size of this dimension, and the stride of the next.  */
    7086        18224 :       if (dim + 1 < as->rank)
    7087         3495 :         stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
    7088              :       else
    7089        14729 :         stride = GFC_TYPE_ARRAY_SIZE (type);
    7090              : 
    7091        18224 :       if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
    7092              :         {
    7093              :           /* Calculate stride = size * (ubound + 1 - lbound).  */
    7094        11719 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7095              :                                  gfc_array_index_type,
    7096              :                                  gfc_index_one_node, lbound);
    7097        11719 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7098              :                                  gfc_array_index_type, ubound, tmp);
    7099        11719 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    7100              :                                  gfc_array_index_type, size, tmp);
    7101        11719 :           if (stride)
    7102        11719 :             gfc_add_modify (pblock, stride, tmp);
    7103              :           else
    7104            0 :             stride = gfc_evaluate_now (tmp, pblock);
    7105              : 
    7106              :           /* Make sure that negative size arrays are translated
    7107              :              to being zero size.  */
    7108        11719 :           tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    7109              :                                  stride, gfc_index_zero_node);
    7110        11719 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    7111              :                                  gfc_array_index_type, tmp,
    7112              :                                  stride, gfc_index_zero_node);
    7113        11719 :           gfc_add_modify (pblock, stride, tmp);
    7114              :         }
    7115              : 
    7116        18224 :       size = stride;
    7117              :     }
    7118              : 
    7119        16365 :   gfc_trans_array_cobounds (type, pblock, sym);
    7120        16365 :   gfc_trans_vla_type_sizes (sym, pblock);
    7121              : 
    7122        16365 :   *poffset = offset;
    7123        16365 :   return size;
    7124              : }
    7125              : 
    7126              : 
    7127              : /* Generate code to initialize/allocate an array variable.  */
    7128              : 
    7129              : void
    7130        33528 : gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
    7131              :                                  gfc_wrapped_block * block)
    7132              : {
    7133        33528 :   stmtblock_t init;
    7134        33528 :   tree type;
    7135        33528 :   tree tmp = NULL_TREE;
    7136        33528 :   tree size;
    7137        33528 :   tree offset;
    7138        33528 :   tree space;
    7139        33528 :   tree inittree;
    7140        33528 :   bool onstack;
    7141        33528 :   bool back;
    7142              : 
    7143        33528 :   gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
    7144              : 
    7145              :   /* Do nothing for USEd variables.  */
    7146        33528 :   if (sym->attr.use_assoc)
    7147        26553 :     return;
    7148              : 
    7149        33479 :   type = TREE_TYPE (decl);
    7150        33479 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7151        33479 :   onstack = TREE_CODE (type) != POINTER_TYPE;
    7152              : 
    7153              :   /* In the case of non-dummy symbols with dependencies on an old-fashioned
    7154              :      function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
    7155              :      must be called with the last, optional argument false so that the alloc-
    7156              :      ation occurs after the processing of the result.  */
    7157        33479 :   back = sym->fn_result_dep;
    7158              : 
    7159        33479 :   gfc_init_block (&init);
    7160              : 
    7161              :   /* Evaluate character string length.  */
    7162        33479 :   if (sym->ts.type == BT_CHARACTER
    7163         3194 :       && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
    7164              :     {
    7165           49 :       gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7166              : 
    7167           49 :       gfc_trans_vla_type_sizes (sym, &init);
    7168              : 
    7169              :       /* Emit a DECL_EXPR for this variable, which will cause the
    7170              :          gimplifier to allocate storage, and all that good stuff.  */
    7171           49 :       tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
    7172           49 :       gfc_add_expr_to_block (&init, tmp);
    7173           49 :       if (sym->attr.omp_allocate)
    7174              :         {
    7175              :           /* Save location of size calculation to ensure GOMP_alloc is placed
    7176              :              after it.  */
    7177            0 :           tree omp_alloc = lookup_attribute ("omp allocate",
    7178            0 :                                              DECL_ATTRIBUTES (decl));
    7179            0 :           TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
    7180            0 :             = build_tree_list (NULL_TREE, tsi_stmt (tsi_last (init.head)));
    7181              :         }
    7182              :     }
    7183              : 
    7184        33277 :   if (onstack)
    7185              :     {
    7186        26364 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
    7187              :                             back);
    7188        26364 :       return;
    7189              :     }
    7190              : 
    7191         7115 :   type = TREE_TYPE (type);
    7192              : 
    7193         7115 :   gcc_assert (!sym->attr.use_assoc);
    7194         7115 :   gcc_assert (!sym->module);
    7195              : 
    7196         7115 :   if (sym->ts.type == BT_CHARACTER
    7197          202 :       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
    7198           94 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7199              : 
    7200         7115 :   size = gfc_trans_array_bounds (type, sym, &offset, &init);
    7201              : 
    7202              :   /* Don't actually allocate space for Cray Pointees.  */
    7203         7115 :   if (sym->attr.cray_pointee)
    7204              :     {
    7205          140 :       if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7206           49 :         gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7207              : 
    7208          140 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
    7209          140 :       return;
    7210              :     }
    7211         6975 :   if (sym->attr.omp_allocate)
    7212              :     {
    7213              :       /* The size is the number of elements in the array, so multiply by the
    7214              :          size of an element to get the total size.  */
    7215            7 :       tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    7216            7 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7217              :                               size, fold_convert (gfc_array_index_type, tmp));
    7218            7 :       size = gfc_evaluate_now (size, &init);
    7219              : 
    7220            7 :       tree omp_alloc = lookup_attribute ("omp allocate",
    7221            7 :                                          DECL_ATTRIBUTES (decl));
    7222            7 :       TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
    7223            7 :         = build_tree_list (size, NULL_TREE);
    7224            7 :       space = NULL_TREE;
    7225              :     }
    7226         6968 :   else if (flag_stack_arrays)
    7227              :     {
    7228           18 :       gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
    7229           18 :       space = build_decl (gfc_get_location (&sym->declared_at),
    7230              :                           VAR_DECL, create_tmp_var_name ("A"),
    7231           18 :                           TREE_TYPE (TREE_TYPE (decl)));
    7232           18 :       gfc_trans_vla_type_sizes (sym, &init);
    7233              :     }
    7234              :   else
    7235              :     {
    7236              :       /* The size is the number of elements in the array, so multiply by the
    7237              :          size of an element to get the total size.  */
    7238         6950 :       tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    7239         6950 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7240              :                               size, fold_convert (gfc_array_index_type, tmp));
    7241              : 
    7242              :       /* Allocate memory to hold the data.  */
    7243         6950 :       tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
    7244         6950 :       gfc_add_modify (&init, decl, tmp);
    7245              : 
    7246              :       /* Free the temporary.  */
    7247         6950 :       tmp = gfc_call_free (decl);
    7248         6950 :       space = NULL_TREE;
    7249              :     }
    7250              : 
    7251              :   /* Set offset of the array.  */
    7252         6975 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7253          388 :     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7254              : 
    7255              :   /* Automatic arrays should not have initializers.  */
    7256         6975 :   gcc_assert (!sym->value);
    7257              : 
    7258         6975 :   inittree = gfc_finish_block (&init);
    7259              : 
    7260         6975 :   if (space)
    7261              :     {
    7262           18 :       tree addr;
    7263           18 :       pushdecl (space);
    7264              : 
    7265              :       /* Don't create new scope, emit the DECL_EXPR in exactly the scope
    7266              :          where also space is located.  */
    7267           18 :       gfc_init_block (&init);
    7268           18 :       tmp = fold_build1_loc (input_location, DECL_EXPR,
    7269           18 :                              TREE_TYPE (space), space);
    7270           18 :       gfc_add_expr_to_block (&init, tmp);
    7271           18 :       addr = fold_build1_loc (gfc_get_location (&sym->declared_at),
    7272           18 :                               ADDR_EXPR, TREE_TYPE (decl), space);
    7273           18 :       gfc_add_modify (&init, decl, addr);
    7274           18 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
    7275              :                             back);
    7276           18 :       tmp = NULL_TREE;
    7277              :     }
    7278         6975 :   gfc_add_init_cleanup (block, inittree, tmp, back);
    7279              : }
    7280              : 
    7281              : 
    7282              : /* Generate entry and exit code for g77 calling convention arrays.  */
    7283              : 
    7284              : void
    7285         8996 : gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
    7286              : {
    7287         8996 :   tree parm;
    7288         8996 :   tree type;
    7289         8996 :   tree offset;
    7290         8996 :   tree tmp;
    7291         8996 :   tree stmt;
    7292         8996 :   stmtblock_t init;
    7293              : 
    7294         8996 :   location_t loc = input_location;
    7295         8996 :   input_location = gfc_get_location (&sym->declared_at);
    7296              : 
    7297              :   /* Descriptor type.  */
    7298         8996 :   parm = sym->backend_decl;
    7299         8996 :   type = TREE_TYPE (parm);
    7300         8996 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7301              : 
    7302         8996 :   gfc_start_block (&init);
    7303              : 
    7304         8996 :   if (sym->ts.type == BT_CHARACTER
    7305          752 :       && VAR_P (sym->ts.u.cl->backend_decl))
    7306           85 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7307              : 
    7308              :   /* Evaluate the bounds of the array.  */
    7309         8996 :   gfc_trans_array_bounds (type, sym, &offset, &init);
    7310              : 
    7311              :   /* Set the offset.  */
    7312         8996 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7313         1226 :     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7314              : 
    7315              :   /* Set the pointer itself if we aren't using the parameter directly.  */
    7316         8996 :   if (TREE_CODE (parm) != PARM_DECL)
    7317              :     {
    7318          631 :       tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
    7319          631 :       if (sym->ts.type == BT_CLASS)
    7320              :         {
    7321          256 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    7322          256 :           tmp = gfc_class_data_get (tmp);
    7323          256 :           tmp = gfc_conv_descriptor_data_get (tmp);
    7324              :         }
    7325          631 :       tmp = convert (TREE_TYPE (parm), tmp);
    7326          631 :       gfc_add_modify (&init, parm, tmp);
    7327              :     }
    7328         8996 :   stmt = gfc_finish_block (&init);
    7329              : 
    7330         8996 :   input_location = loc;
    7331              : 
    7332              :   /* Add the initialization code to the start of the function.  */
    7333              : 
    7334         8996 :   if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
    7335         8996 :       || sym->attr.optional
    7336         8501 :       || sym->attr.not_always_present)
    7337              :     {
    7338          555 :       tree nullify;
    7339          555 :       if (TREE_CODE (parm) != PARM_DECL)
    7340          105 :         nullify = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7341              :                                    parm, null_pointer_node);
    7342              :       else
    7343          450 :         nullify = build_empty_stmt (input_location);
    7344          555 :       tmp = gfc_conv_expr_present (sym, true);
    7345          555 :       stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
    7346              :     }
    7347              : 
    7348         8996 :   gfc_add_init_cleanup (block, stmt, NULL_TREE);
    7349         8996 : }
    7350              : 
    7351              : 
    7352              : /* Modify the descriptor of an array parameter so that it has the
    7353              :    correct lower bound.  Also move the upper bound accordingly.
    7354              :    If the array is not packed, it will be copied into a temporary.
    7355              :    For each dimension we set the new lower and upper bounds.  Then we copy the
    7356              :    stride and calculate the offset for this dimension.  We also work out
    7357              :    what the stride of a packed array would be, and see it the two match.
    7358              :    If the array need repacking, we set the stride to the values we just
    7359              :    calculated, recalculate the offset and copy the array data.
    7360              :    Code is also added to copy the data back at the end of the function.
    7361              :    */
    7362              : 
    7363              : void
    7364        15215 : gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
    7365              :                             gfc_wrapped_block * block)
    7366              : {
    7367        15215 :   tree size;
    7368        15215 :   tree type;
    7369        15215 :   tree offset;
    7370        15215 :   stmtblock_t init;
    7371        15215 :   tree stmtInit, stmtCleanup;
    7372        15215 :   tree lbound;
    7373        15215 :   tree ubound;
    7374        15215 :   tree dubound;
    7375        15215 :   tree dlbound;
    7376        15215 :   tree dumdesc;
    7377        15215 :   tree tmp;
    7378        15215 :   tree stride, stride2;
    7379        15215 :   tree stmt_packed;
    7380        15215 :   tree stmt_unpacked;
    7381        15215 :   tree partial;
    7382        15215 :   gfc_se se;
    7383        15215 :   int n;
    7384        15215 :   int checkparm;
    7385        15215 :   int no_repack;
    7386        15215 :   bool optional_arg;
    7387        15215 :   gfc_array_spec *as;
    7388        15215 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    7389              : 
    7390              :   /* Do nothing for pointer and allocatable arrays.  */
    7391        15215 :   if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    7392        15118 :       || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
    7393        15118 :       || sym->attr.allocatable
    7394        15012 :       || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
    7395         7646 :     return;
    7396              : 
    7397          930 :   if ((!is_classarray
    7398          930 :        || (is_classarray && CLASS_DATA (sym)->as->type == AS_EXPLICIT))
    7399        14273 :       && sym->attr.dummy && !sym->attr.elemental && gfc_is_nodesc_array (sym))
    7400              :     {
    7401         7443 :       gfc_trans_g77_array (sym, block);
    7402         7443 :       return;
    7403              :     }
    7404              : 
    7405         7569 :   location_t loc = input_location;
    7406         7569 :   input_location = gfc_get_location (&sym->declared_at);
    7407              : 
    7408              :   /* Descriptor type.  */
    7409         7569 :   type = TREE_TYPE (tmpdesc);
    7410         7569 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7411         7569 :   dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7412         7569 :   if (is_classarray)
    7413              :     /* For a class array the dummy array descriptor is in the _class
    7414              :        component.  */
    7415          758 :     dumdesc = gfc_class_data_get (dumdesc);
    7416              :   else
    7417         6811 :     dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
    7418         7569 :   as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    7419         7569 :   gfc_start_block (&init);
    7420              : 
    7421         7569 :   if (sym->ts.type == BT_CHARACTER
    7422          883 :       && VAR_P (sym->ts.u.cl->backend_decl))
    7423           99 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7424              : 
    7425              :   /* TODO: Fix the exclusion of class arrays from extent checking.  */
    7426         1114 :   checkparm = (as->type == AS_EXPLICIT && !is_classarray
    7427         8664 :                && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
    7428              : 
    7429         7569 :   no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
    7430         7568 :                 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
    7431              : 
    7432         7569 :   if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
    7433              :     {
    7434              :       /* For non-constant shape arrays we only check if the first dimension
    7435              :          is contiguous.  Repacking higher dimensions wouldn't gain us
    7436              :          anything as we still don't know the array stride.  */
    7437            1 :       partial = gfc_create_var (logical_type_node, "partial");
    7438            1 :       TREE_USED (partial) = 1;
    7439            1 :       tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
    7440            1 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
    7441              :                              gfc_index_one_node);
    7442            1 :       gfc_add_modify (&init, partial, tmp);
    7443              :     }
    7444              :   else
    7445              :     partial = NULL_TREE;
    7446              : 
    7447              :   /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
    7448              :      here, however I think it does the right thing.  */
    7449         7569 :   if (no_repack)
    7450              :     {
    7451              :       /* Set the first stride.  */
    7452         7567 :       stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
    7453         7567 :       stride = gfc_evaluate_now (stride, &init);
    7454              : 
    7455         7567 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7456              :                              stride, gfc_index_zero_node);
    7457         7567 :       tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    7458              :                              tmp, gfc_index_one_node, stride);
    7459         7567 :       stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
    7460         7567 :       gfc_add_modify (&init, stride, tmp);
    7461              : 
    7462              :       /* Allow the user to disable array repacking.  */
    7463         7567 :       stmt_unpacked = NULL_TREE;
    7464              :     }
    7465              :   else
    7466              :     {
    7467            2 :       gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
    7468              :       /* A library call to repack the array if necessary.  */
    7469            2 :       tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7470            2 :       stmt_unpacked = build_call_expr_loc (input_location,
    7471              :                                        gfor_fndecl_in_pack, 1, tmp);
    7472              : 
    7473            2 :       stride = gfc_index_one_node;
    7474              : 
    7475            2 :       if (warn_array_temporaries)
    7476              :         {
    7477            1 :           locus where;
    7478            1 :           gfc_locus_from_location (&where, loc);
    7479            1 :           gfc_warning (OPT_Warray_temporaries,
    7480              :                      "Creating array temporary at %L", &where);
    7481              :         }
    7482              :     }
    7483              : 
    7484              :   /* This is for the case where the array data is used directly without
    7485              :      calling the repack function.  */
    7486         7569 :   if (no_repack || partial != NULL_TREE)
    7487         7568 :     stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
    7488              :   else
    7489              :     stmt_packed = NULL_TREE;
    7490              : 
    7491              :   /* Assign the data pointer.  */
    7492         7569 :   if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
    7493              :     {
    7494              :       /* Don't repack unknown shape arrays when the first stride is 1.  */
    7495            1 :       tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (stmt_packed),
    7496              :                              partial, stmt_packed, stmt_unpacked);
    7497              :     }
    7498              :   else
    7499         7568 :     tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
    7500         7569 :   gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
    7501              : 
    7502         7569 :   offset = gfc_index_zero_node;
    7503         7569 :   size = gfc_index_one_node;
    7504              : 
    7505              :   /* Evaluate the bounds of the array.  */
    7506        17626 :   for (n = 0; n < as->rank; n++)
    7507              :     {
    7508        10057 :       if (checkparm || !as->upper[n])
    7509              :         {
    7510              :           /* Get the bounds of the actual parameter.  */
    7511         8690 :           dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
    7512         8690 :           dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
    7513              :         }
    7514              :       else
    7515              :         {
    7516              :           dubound = NULL_TREE;
    7517              :           dlbound = NULL_TREE;
    7518              :         }
    7519              : 
    7520        10057 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
    7521        10057 :       if (!INTEGER_CST_P (lbound))
    7522              :         {
    7523           46 :           gfc_init_se (&se, NULL);
    7524           46 :           gfc_conv_expr_type (&se, as->lower[n],
    7525              :                               gfc_array_index_type);
    7526           46 :           gfc_add_block_to_block (&init, &se.pre);
    7527           46 :           gfc_add_modify (&init, lbound, se.expr);
    7528              :         }
    7529              : 
    7530        10057 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
    7531              :       /* Set the desired upper bound.  */
    7532        10057 :       if (as->upper[n])
    7533              :         {
    7534              :           /* We know what we want the upper bound to be.  */
    7535         1425 :           if (!INTEGER_CST_P (ubound))
    7536              :             {
    7537          651 :               gfc_init_se (&se, NULL);
    7538          651 :               gfc_conv_expr_type (&se, as->upper[n],
    7539              :                                   gfc_array_index_type);
    7540          651 :               gfc_add_block_to_block (&init, &se.pre);
    7541          651 :               gfc_add_modify (&init, ubound, se.expr);
    7542              :             }
    7543              : 
    7544              :           /* Check the sizes match.  */
    7545         1425 :           if (checkparm)
    7546              :             {
    7547              :               /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)).  */
    7548           58 :               char * msg;
    7549           58 :               tree temp;
    7550           58 :               locus where;
    7551              : 
    7552           58 :               gfc_locus_from_location (&where, loc);
    7553           58 :               temp = fold_build2_loc (input_location, MINUS_EXPR,
    7554              :                                       gfc_array_index_type, ubound, lbound);
    7555           58 :               temp = fold_build2_loc (input_location, PLUS_EXPR,
    7556              :                                       gfc_array_index_type,
    7557              :                                       gfc_index_one_node, temp);
    7558           58 :               stride2 = fold_build2_loc (input_location, MINUS_EXPR,
    7559              :                                          gfc_array_index_type, dubound,
    7560              :                                          dlbound);
    7561           58 :               stride2 = fold_build2_loc (input_location, PLUS_EXPR,
    7562              :                                          gfc_array_index_type,
    7563              :                                          gfc_index_one_node, stride2);
    7564           58 :               tmp = fold_build2_loc (input_location, NE_EXPR,
    7565              :                                      gfc_array_index_type, temp, stride2);
    7566           58 :               msg = xasprintf ("Dimension %d of array '%s' has extent "
    7567              :                                "%%ld instead of %%ld", n+1, sym->name);
    7568              : 
    7569           58 :               gfc_trans_runtime_check (true, false, tmp, &init, &where, msg,
    7570              :                         fold_convert (long_integer_type_node, temp),
    7571              :                         fold_convert (long_integer_type_node, stride2));
    7572              : 
    7573           58 :               free (msg);
    7574              :             }
    7575              :         }
    7576              :       else
    7577              :         {
    7578              :           /* For assumed shape arrays move the upper bound by the same amount
    7579              :              as the lower bound.  */
    7580         8632 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7581              :                                  gfc_array_index_type, dubound, dlbound);
    7582         8632 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7583              :                                  gfc_array_index_type, tmp, lbound);
    7584         8632 :           gfc_add_modify (&init, ubound, tmp);
    7585              :         }
    7586              :       /* The offset of this dimension.  offset = offset - lbound * stride.  */
    7587        10057 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7588              :                              lbound, stride);
    7589        10057 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    7590              :                                 gfc_array_index_type, offset, tmp);
    7591              : 
    7592              :       /* The size of this dimension, and the stride of the next.  */
    7593        10057 :       if (n + 1 < as->rank)
    7594              :         {
    7595         2488 :           stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
    7596              : 
    7597         2488 :           if (no_repack || partial != NULL_TREE)
    7598         2487 :             stmt_unpacked =
    7599         2487 :               gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
    7600              : 
    7601              :           /* Figure out the stride if not a known constant.  */
    7602         2488 :           if (!INTEGER_CST_P (stride))
    7603              :             {
    7604         2487 :               if (no_repack)
    7605              :                 stmt_packed = NULL_TREE;
    7606              :               else
    7607              :                 {
    7608              :                   /* Calculate stride = size * (ubound + 1 - lbound).  */
    7609            0 :                   tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7610              :                                          gfc_array_index_type,
    7611              :                                          gfc_index_one_node, lbound);
    7612            0 :                   tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7613              :                                          gfc_array_index_type, ubound, tmp);
    7614            0 :                   size = fold_build2_loc (input_location, MULT_EXPR,
    7615              :                                           gfc_array_index_type, size, tmp);
    7616            0 :                   stmt_packed = size;
    7617              :                 }
    7618              : 
    7619              :               /* Assign the stride.  */
    7620         2487 :               if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
    7621            0 :                 tmp = fold_build3_loc (input_location, COND_EXPR,
    7622              :                                        gfc_array_index_type, partial,
    7623              :                                        stmt_unpacked, stmt_packed);
    7624              :               else
    7625         2487 :                 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
    7626         2487 :               gfc_add_modify (&init, stride, tmp);
    7627              :             }
    7628              :         }
    7629              :       else
    7630              :         {
    7631         7569 :           stride = GFC_TYPE_ARRAY_SIZE (type);
    7632              : 
    7633         7569 :           if (stride && !INTEGER_CST_P (stride))
    7634              :             {
    7635              :               /* Calculate size = stride * (ubound + 1 - lbound).  */
    7636         7568 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7637              :                                      gfc_array_index_type,
    7638              :                                      gfc_index_one_node, lbound);
    7639         7568 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7640              :                                      gfc_array_index_type,
    7641              :                                      ubound, tmp);
    7642        22704 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    7643              :                                      gfc_array_index_type,
    7644         7568 :                                      GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
    7645         7568 :               gfc_add_modify (&init, stride, tmp);
    7646              :             }
    7647              :         }
    7648              :     }
    7649              : 
    7650         7569 :   gfc_trans_array_cobounds (type, &init, sym);
    7651              : 
    7652              :   /* Set the offset.  */
    7653         7569 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7654         7567 :     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7655              : 
    7656              :   /* Fold the element spacing of the actual argument into the strides and the
    7657              :      offset, so that the elements are addressed by the constant element length
    7658              :      rather than by a span loaded from the descriptor.  */
    7659         7569 :   if (DECL_LANG_SPECIFIC (tmpdesc) && GFC_DECL_SPAN_NORMALIZED (tmpdesc))
    7660              :     {
    7661          463 :       tree element = fold_convert (gfc_array_index_type,
    7662              :                                    TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    7663          463 :       tree span = gfc_evaluate_now (gfc_conv_descriptor_span_get (dumdesc),
    7664              :                                     &init);
    7665          463 :       tree unit = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7666          463 :                                    span, element);
    7667          463 :       tree factor = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    7668          463 :                                      gfc_array_index_type, span, element);
    7669          463 :       factor = gfc_evaluate_now (factor, &init);
    7670              : 
    7671         1439 :       auto scale = [&] (tree var)
    7672              :         {
    7673          976 :           tree scaled = fold_build2_loc (input_location, MULT_EXPR,
    7674              :                                          gfc_array_index_type, var, factor);
    7675          976 :           scaled = fold_build3_loc (input_location, COND_EXPR,
    7676              :                                     gfc_array_index_type, unit, var, scaled);
    7677          976 :           gfc_add_modify (&init, var, scaled);
    7678         1439 :         };
    7679              : 
    7680              :       /* A span addressed dummy is never repacked, so its strides and its
    7681              :          offset are all variables loaded from the descriptor.  */
    7682          976 :       for (n = 0; n < as->rank; n++)
    7683              :         {
    7684          513 :           gcc_assert (VAR_P (GFC_TYPE_ARRAY_STRIDE (type, n)));
    7685          513 :           scale (GFC_TYPE_ARRAY_STRIDE (type, n));
    7686              :         }
    7687              : 
    7688          463 :       gcc_assert (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)));
    7689          463 :       scale (GFC_TYPE_ARRAY_OFFSET (type));
    7690              :     }
    7691              : 
    7692              :   /* Load the span once here, like the bounds above, so that element
    7693              :      addressing does not reload it from the descriptor.  The descriptor
    7694              :      itself is not available in an outlined region, such as an OpenMP
    7695              :      target region, whereas this local variable is.  */
    7696         7569 :   if (tree span = GFC_DECL_GET_SPAN (tmpdesc))
    7697          229 :     gfc_add_modify (&init, span, gfc_conv_descriptor_span_get (dumdesc));
    7698              : 
    7699         7569 :   gfc_trans_vla_type_sizes (sym, &init);
    7700              : 
    7701         7569 :   stmtInit = gfc_finish_block (&init);
    7702              : 
    7703              :   /* Only do the entry/initialization code if the arg is present.  */
    7704         7569 :   dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7705         7569 :   optional_arg = (sym->attr.optional
    7706         7569 :                   || (sym->ns->proc_name->attr.entry_master
    7707           79 :                       && sym->attr.dummy));
    7708              :   if (optional_arg)
    7709              :     {
    7710          783 :       tree zero_init = fold_convert (TREE_TYPE (tmpdesc), null_pointer_node);
    7711          783 :       zero_init = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7712              :                                    tmpdesc, zero_init);
    7713          783 :       tmp = gfc_conv_expr_present (sym, true);
    7714          783 :       stmtInit = build3_v (COND_EXPR, tmp, stmtInit, zero_init);
    7715              :     }
    7716              : 
    7717              :   /* Cleanup code.  */
    7718         7569 :   if (no_repack)
    7719              :     stmtCleanup = NULL_TREE;
    7720              :   else
    7721              :     {
    7722            2 :       stmtblock_t cleanup;
    7723            2 :       gfc_start_block (&cleanup);
    7724              : 
    7725            2 :       if (sym->attr.intent != INTENT_IN)
    7726              :         {
    7727              :           /* Copy the data back.  */
    7728            2 :           tmp = build_call_expr_loc (input_location,
    7729              :                                  gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
    7730            2 :           gfc_add_expr_to_block (&cleanup, tmp);
    7731              :         }
    7732              : 
    7733              :       /* Free the temporary.  */
    7734            2 :       tmp = gfc_call_free (tmpdesc);
    7735            2 :       gfc_add_expr_to_block (&cleanup, tmp);
    7736              : 
    7737            2 :       stmtCleanup = gfc_finish_block (&cleanup);
    7738              : 
    7739              :       /* Only do the cleanup if the array was repacked.  */
    7740            2 :       if (is_classarray)
    7741              :         /* For a class array the dummy array descriptor is in the _class
    7742              :            component.  */
    7743            1 :         tmp = gfc_class_data_get (dumdesc);
    7744              :       else
    7745            1 :         tmp = build_fold_indirect_ref_loc (input_location, dumdesc);
    7746            2 :       tmp = gfc_conv_descriptor_data_get (tmp);
    7747            2 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    7748              :                              tmp, tmpdesc);
    7749            2 :       stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
    7750              :                               build_empty_stmt (input_location));
    7751              : 
    7752            2 :       if (optional_arg)
    7753              :         {
    7754            0 :           tmp = gfc_conv_expr_present (sym);
    7755            0 :           stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
    7756              :                                   build_empty_stmt (input_location));
    7757              :         }
    7758              :     }
    7759              : 
    7760              :   /* We don't need to free any memory allocated by internal_pack as it will
    7761              :      be freed at the end of the function by pop_context.  */
    7762         7569 :   gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
    7763              : 
    7764         7569 :   input_location = loc;
    7765              : }
    7766              : 
    7767              : 
    7768              : /* Calculate the overall offset, including subreferences.  */
    7769              : void
    7770        66218 : gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
    7771              :                         bool subref, gfc_expr *expr)
    7772              : {
    7773        66218 :   tree tmp;
    7774        66218 :   tree field;
    7775        66218 :   tree stride;
    7776        66218 :   tree index;
    7777        66218 :   gfc_ref *ref;
    7778        66218 :   gfc_se start;
    7779        66218 :   int n;
    7780              : 
    7781              :   /* If offset is NULL and this is not a subreferenced array, there is
    7782              :      nothing to do.  */
    7783        66218 :   if (offset == NULL_TREE)
    7784              :     {
    7785         1108 :       if (subref)
    7786          177 :         offset = gfc_index_zero_node;
    7787              :       else
    7788          931 :         return;
    7789              :     }
    7790              : 
    7791              :   /* An array whose elements are spaced by the span needs pointer arithmetic
    7792              :      to reference an element.  */
    7793        65287 :   tmp = build_array_ref (desc, offset, NULL, NULL);
    7794              : 
    7795              :   /* Offset the data pointer for pointer assignments from arrays with
    7796              :      subreferences; e.g. my_integer => my_type(:)%integer_component.  */
    7797        65287 :   if (subref)
    7798              :     {
    7799              :       /* Go past the array reference.  */
    7800         1416 :       for (ref = expr->ref; ref; ref = ref->next)
    7801         1416 :         if (ref->type == REF_ARRAY &&
    7802         1239 :               ref->u.ar.type != AR_ELEMENT)
    7803              :           {
    7804         1215 :             ref = ref->next;
    7805         1215 :             break;
    7806              :           }
    7807              : 
    7808              :       /* Calculate the offset for each subsequent subreference.  */
    7809         2226 :       for (; ref; ref = ref->next)
    7810              :         {
    7811         1011 :           switch (ref->type)
    7812              :             {
    7813          571 :             case REF_COMPONENT:
    7814          571 :               field = ref->u.c.component->backend_decl;
    7815          571 :               gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
    7816         1142 :               tmp = fold_build3_loc (input_location, COMPONENT_REF,
    7817          571 :                                      TREE_TYPE (field),
    7818              :                                      tmp, field, NULL_TREE);
    7819          571 :               break;
    7820              : 
    7821          332 :             case REF_SUBSTRING:
    7822          332 :               gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
    7823          332 :               gfc_init_se (&start, NULL);
    7824          332 :               gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
    7825          332 :               gfc_add_block_to_block (block, &start.pre);
    7826          332 :               tmp = gfc_build_array_ref (tmp, start.expr, NULL);
    7827          332 :               break;
    7828              : 
    7829           24 :             case REF_ARRAY:
    7830           24 :               gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
    7831              :                             && ref->u.ar.type == AR_ELEMENT);
    7832              : 
    7833              :               /* TODO - Add bounds checking.  */
    7834           24 :               stride = gfc_index_one_node;
    7835           24 :               index = gfc_index_zero_node;
    7836           55 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7837              :                 {
    7838           31 :                   tree itmp;
    7839           31 :                   tree jtmp;
    7840              : 
    7841              :                   /* Update the index.  */
    7842           31 :                   gfc_init_se (&start, NULL);
    7843           31 :                   gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
    7844           31 :                   itmp = gfc_evaluate_now (start.expr, block);
    7845           31 :                   gfc_init_se (&start, NULL);
    7846           31 :                   gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
    7847           31 :                   jtmp = gfc_evaluate_now (start.expr, block);
    7848           31 :                   itmp = fold_build2_loc (input_location, MINUS_EXPR,
    7849              :                                           gfc_array_index_type, itmp, jtmp);
    7850           31 :                   itmp = fold_build2_loc (input_location, MULT_EXPR,
    7851              :                                           gfc_array_index_type, itmp, stride);
    7852           31 :                   index = fold_build2_loc (input_location, PLUS_EXPR,
    7853              :                                           gfc_array_index_type, itmp, index);
    7854           31 :                   index = gfc_evaluate_now (index, block);
    7855              : 
    7856              :                   /* Update the stride.  */
    7857           31 :                   gfc_init_se (&start, NULL);
    7858           31 :                   gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
    7859           31 :                   itmp =  fold_build2_loc (input_location, MINUS_EXPR,
    7860              :                                            gfc_array_index_type, start.expr,
    7861              :                                            jtmp);
    7862           31 :                   itmp =  fold_build2_loc (input_location, PLUS_EXPR,
    7863              :                                            gfc_array_index_type,
    7864              :                                            gfc_index_one_node, itmp);
    7865           31 :                   stride =  fold_build2_loc (input_location, MULT_EXPR,
    7866              :                                              gfc_array_index_type, stride, itmp);
    7867           31 :                   stride = gfc_evaluate_now (stride, block);
    7868              :                 }
    7869              : 
    7870              :               /* Apply the index to obtain the array element.  */
    7871           24 :               tmp = gfc_build_array_ref (tmp, index, NULL);
    7872           24 :               break;
    7873              : 
    7874           84 :             case REF_INQUIRY:
    7875           84 :               switch (ref->u.i)
    7876              :                 {
    7877           60 :                 case INQUIRY_RE:
    7878          120 :                   tmp = fold_build1_loc (input_location, REALPART_EXPR,
    7879           60 :                                          TREE_TYPE (TREE_TYPE (tmp)), tmp);
    7880           60 :                   break;
    7881              : 
    7882           24 :                 case INQUIRY_IM:
    7883           48 :                   tmp = fold_build1_loc (input_location, IMAGPART_EXPR,
    7884           24 :                                          TREE_TYPE (TREE_TYPE (tmp)), tmp);
    7885           24 :                   break;
    7886              : 
    7887              :                 default:
    7888              :                   break;
    7889              :                 }
    7890              :               break;
    7891              : 
    7892            0 :             default:
    7893            0 :               gcc_unreachable ();
    7894         1011 :               break;
    7895              :             }
    7896              :         }
    7897              :     }
    7898              : 
    7899              :   /* Set the target data pointer.  */
    7900        65287 :   offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
    7901              : 
    7902              :   /* Check for optional dummy argument being present.  Arguments of BIND(C)
    7903              :      procedures are excepted here since they are handled differently.  */
    7904        65287 :   if (expr->expr_type == EXPR_VARIABLE
    7905        57521 :       && expr->symtree->n.sym->attr.dummy
    7906         6858 :       && expr->symtree->n.sym->attr.optional
    7907        66299 :       && !is_CFI_desc (NULL, expr))
    7908         1664 :     offset = build3_loc (input_location, COND_EXPR, TREE_TYPE (offset),
    7909          832 :                          gfc_conv_expr_present (expr->symtree->n.sym), offset,
    7910          832 :                          fold_convert (TREE_TYPE (offset), gfc_index_zero_node));
    7911              : 
    7912        65287 :   gfc_conv_descriptor_data_set (block, parm, offset);
    7913              : }
    7914              : 
    7915              : 
    7916              : /* gfc_conv_expr_descriptor needs the string length an expression
    7917              :    so that the size of the temporary can be obtained.  This is done
    7918              :    by adding up the string lengths of all the elements in the
    7919              :    expression.  Function with non-constant expressions have their
    7920              :    string lengths mapped onto the actual arguments using the
    7921              :    interface mapping machinery in trans-expr.cc.  */
    7922              : static void
    7923         1674 : get_array_charlen (gfc_expr *expr, gfc_se *se)
    7924              : {
    7925         1674 :   gfc_interface_mapping mapping;
    7926         1674 :   gfc_formal_arglist *formal;
    7927         1674 :   gfc_actual_arglist *arg;
    7928         1674 :   gfc_se tse;
    7929         1674 :   gfc_expr *e;
    7930              : 
    7931         1674 :   if (expr->ts.u.cl->length
    7932         1674 :         && gfc_is_constant_expr (expr->ts.u.cl->length))
    7933              :     {
    7934         1327 :       if (!expr->ts.u.cl->backend_decl)
    7935          465 :         gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    7936         1459 :       return;
    7937              :     }
    7938              : 
    7939          347 :   switch (expr->expr_type)
    7940              :     {
    7941          130 :     case EXPR_ARRAY:
    7942              : 
    7943              :       /* This is somewhat brutal. The expression for the first
    7944              :          element of the array is evaluated and assigned to a
    7945              :          new string length for the original expression.  */
    7946          130 :       e = gfc_constructor_first (expr->value.constructor)->expr;
    7947              : 
    7948          130 :       gfc_init_se (&tse, NULL);
    7949              : 
    7950              :       /* Avoid evaluating trailing array references since all we need is
    7951              :          the string length.  */
    7952          130 :       if (e->rank)
    7953           38 :         tse.descriptor_only = 1;
    7954          130 :       if (e->rank && e->expr_type != EXPR_VARIABLE)
    7955            1 :         gfc_conv_expr_descriptor (&tse, e);
    7956              :       else
    7957          129 :         gfc_conv_expr (&tse, e);
    7958              : 
    7959          130 :       gfc_add_block_to_block (&se->pre, &tse.pre);
    7960          130 :       gfc_add_block_to_block (&se->post, &tse.post);
    7961              : 
    7962          130 :       if (!expr->ts.u.cl->backend_decl || !VAR_P (expr->ts.u.cl->backend_decl))
    7963              :         {
    7964           87 :           expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7965           87 :           expr->ts.u.cl->backend_decl =
    7966           87 :                         gfc_create_var (gfc_charlen_type_node, "sln");
    7967              :         }
    7968              : 
    7969          130 :       gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    7970              :                       tse.string_length);
    7971              : 
    7972              :       /* Make sure that deferred length components point to the hidden
    7973              :          string_length component.  */
    7974          130 :       if (TREE_CODE (tse.expr) == COMPONENT_REF
    7975           25 :           && TREE_CODE (tse.string_length) == COMPONENT_REF
    7976          149 :           && TREE_OPERAND (tse.expr, 0) == TREE_OPERAND (tse.string_length, 0))
    7977           19 :         e->ts.u.cl->backend_decl = expr->ts.u.cl->backend_decl;
    7978              : 
    7979              :       return;
    7980              : 
    7981           91 :     case EXPR_OP:
    7982           91 :       get_array_charlen (expr->value.op.op1, se);
    7983              : 
    7984              :       /* For parentheses the expression ts.u.cl should be identical.  */
    7985           91 :       if (expr->value.op.op == INTRINSIC_PARENTHESES)
    7986              :         {
    7987            2 :           if (expr->value.op.op1->ts.u.cl != expr->ts.u.cl)
    7988            2 :             expr->ts.u.cl->backend_decl
    7989            2 :                         = expr->value.op.op1->ts.u.cl->backend_decl;
    7990              :           return;
    7991              :         }
    7992              : 
    7993          178 :       expr->ts.u.cl->backend_decl =
    7994           89 :                 gfc_create_var (gfc_charlen_type_node, "sln");
    7995              : 
    7996           89 :       if (expr->value.op.op2)
    7997              :         {
    7998           89 :           get_array_charlen (expr->value.op.op2, se);
    7999              : 
    8000           89 :           gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
    8001              : 
    8002              :           /* Add the string lengths and assign them to the expression
    8003              :              string length backend declaration.  */
    8004           89 :           gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    8005              :                           fold_build2_loc (input_location, PLUS_EXPR,
    8006              :                                 gfc_charlen_type_node,
    8007           89 :                                 expr->value.op.op1->ts.u.cl->backend_decl,
    8008           89 :                                 expr->value.op.op2->ts.u.cl->backend_decl));
    8009              :         }
    8010              :       else
    8011            0 :         gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    8012            0 :                         expr->value.op.op1->ts.u.cl->backend_decl);
    8013              :       break;
    8014              : 
    8015           44 :     case EXPR_FUNCTION:
    8016           44 :       if (expr->value.function.esym == NULL
    8017           37 :             || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    8018              :         {
    8019            7 :           gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    8020            7 :           break;
    8021              :         }
    8022              : 
    8023              :       /* Map expressions involving the dummy arguments onto the actual
    8024              :          argument expressions.  */
    8025           37 :       gfc_init_interface_mapping (&mapping);
    8026           37 :       formal = gfc_sym_get_dummy_args (expr->symtree->n.sym);
    8027           37 :       arg = expr->value.function.actual;
    8028              : 
    8029              :       /* Set se = NULL in the calls to the interface mapping, to suppress any
    8030              :          backend stuff.  */
    8031          113 :       for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
    8032              :         {
    8033           38 :           if (!arg->expr)
    8034            0 :             continue;
    8035           38 :           if (formal->sym)
    8036           38 :           gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
    8037              :         }
    8038              : 
    8039           37 :       gfc_init_se (&tse, NULL);
    8040              : 
    8041              :       /* Build the expression for the character length and convert it.  */
    8042           37 :       gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
    8043              : 
    8044           37 :       gfc_add_block_to_block (&se->pre, &tse.pre);
    8045           37 :       gfc_add_block_to_block (&se->post, &tse.post);
    8046           37 :       tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
    8047           74 :       tse.expr = fold_build2_loc (input_location, MAX_EXPR,
    8048           37 :                                   TREE_TYPE (tse.expr), tse.expr,
    8049           37 :                                   build_zero_cst (TREE_TYPE (tse.expr)));
    8050           37 :       expr->ts.u.cl->backend_decl = tse.expr;
    8051           37 :       gfc_free_interface_mapping (&mapping);
    8052           37 :       break;
    8053              : 
    8054           82 :     default:
    8055           82 :       gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    8056           82 :       break;
    8057              :     }
    8058              : }
    8059              : 
    8060              : 
    8061              : /* Helper function to check dimensions.  */
    8062              : static bool
    8063            0 : transposed_dims (gfc_ss *ss)
    8064              : {
    8065            0 :   int n;
    8066              : 
    8067       189225 :   for (n = 0; n < ss->dimen; n++)
    8068        93565 :     if (ss->dim[n] != n)
    8069              :       return true;
    8070              :   return false;
    8071              : }
    8072              : 
    8073              : 
    8074              : /* Convert the last ref of a scalar coarray from an AR_ELEMENT to an
    8075              :    AR_FULL, suitable for the scalarizer.  */
    8076              : 
    8077              : static gfc_ss *
    8078         1657 : walk_coarray (gfc_expr *e)
    8079              : {
    8080         1657 :   gfc_ss *ss;
    8081              : 
    8082         1657 :   ss = gfc_walk_expr (e);
    8083              : 
    8084              :   /* Fix scalar coarray.  */
    8085         1657 :   if (ss == gfc_ss_terminator)
    8086              :     {
    8087          481 :       gfc_ref *ref;
    8088              : 
    8089          481 :       ref = e->ref;
    8090          659 :       while (ref)
    8091              :         {
    8092          659 :           if (ref->type == REF_ARRAY
    8093          481 :               && ref->u.ar.codimen > 0)
    8094              :             break;
    8095              : 
    8096          178 :           ref = ref->next;
    8097              :         }
    8098              : 
    8099          481 :       gcc_assert (ref != NULL);
    8100          481 :       if (ref->u.ar.type == AR_ELEMENT)
    8101          463 :         ref->u.ar.type = AR_SECTION;
    8102          481 :       ss = gfc_reverse_ss (gfc_walk_array_ref (ss, e, ref, false));
    8103              :     }
    8104              : 
    8105         1657 :   return ss;
    8106              : }
    8107              : 
    8108              : gfc_array_spec *
    8109         2331 : get_coarray_as (const gfc_expr *e)
    8110              : {
    8111         2331 :   gfc_array_spec *as;
    8112         2331 :   gfc_symbol *sym = e->symtree->n.sym;
    8113         2331 :   gfc_component *comp;
    8114              : 
    8115         2331 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.codimension)
    8116          631 :     as = CLASS_DATA (sym)->as;
    8117         1700 :   else if (sym->attr.codimension)
    8118         1640 :     as = sym->as;
    8119              :   else
    8120              :     as = nullptr;
    8121              : 
    8122         5395 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
    8123              :     {
    8124         3064 :       switch (ref->type)
    8125              :         {
    8126          733 :         case REF_COMPONENT:
    8127          733 :           comp = ref->u.c.component;
    8128          733 :           if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.codimension)
    8129           18 :             as = CLASS_DATA (comp)->as;
    8130          715 :           else if (comp->ts.type != BT_CLASS && comp->attr.codimension)
    8131          691 :             as = comp->as;
    8132              :           break;
    8133              : 
    8134              :         case REF_ARRAY:
    8135              :         case REF_SUBSTRING:
    8136              :         case REF_INQUIRY:
    8137              :           break;
    8138              :         }
    8139              :     }
    8140              : 
    8141         2331 :   return as;
    8142              : }
    8143              : 
    8144              : bool
    8145       157656 : is_explicit_coarray (gfc_expr *expr)
    8146              : {
    8147       157656 :   if (!gfc_is_coarray (expr))
    8148              :     return false;
    8149              : 
    8150         2331 :   gfc_array_spec *cas = get_coarray_as (expr);
    8151         2331 :   return cas && cas->cotype == AS_EXPLICIT;
    8152              : }
    8153              : 
    8154              : /* Convert an array for passing as an actual argument.  Expressions and
    8155              :    vector subscripts are evaluated and stored in a temporary, which is then
    8156              :    passed.  For whole arrays the descriptor is passed.  For array sections
    8157              :    a modified copy of the descriptor is passed, but using the original data.
    8158              : 
    8159              :    This function is also used for array pointer assignments, and there
    8160              :    are three cases:
    8161              : 
    8162              :      - se->want_pointer && !se->direct_byref
    8163              :          EXPR is an actual argument.  On exit, se->expr contains a
    8164              :          pointer to the array descriptor.
    8165              : 
    8166              :      - !se->want_pointer && !se->direct_byref
    8167              :          EXPR is an actual argument to an intrinsic function or the
    8168              :          left-hand side of a pointer assignment.  On exit, se->expr
    8169              :          contains the descriptor for EXPR.
    8170              : 
    8171              :      - !se->want_pointer && se->direct_byref
    8172              :          EXPR is the right-hand side of a pointer assignment and
    8173              :          se->expr is the descriptor for the previously-evaluated
    8174              :          left-hand side.  The function creates an assignment from
    8175              :          EXPR to se->expr.
    8176              : 
    8177              : 
    8178              :    The se->force_tmp flag disables the non-copying descriptor optimization
    8179              :    that is used for transpose. It may be used in cases where there is an
    8180              :    alias between the transpose argument and another argument in the same
    8181              :    function call.  */
    8182              : 
    8183              : void
    8184       175040 : gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
    8185              : {
    8186       175040 :   gfc_ss *ss;
    8187       175040 :   gfc_ss_type ss_type;
    8188       175040 :   gfc_ss_info *ss_info;
    8189       175040 :   gfc_loopinfo loop;
    8190       175040 :   gfc_array_info *info;
    8191       175040 :   int need_tmp;
    8192       175040 :   int n;
    8193       175040 :   tree tmp;
    8194       175040 :   tree desc;
    8195       175040 :   stmtblock_t block;
    8196       175040 :   tree start;
    8197       175040 :   int full;
    8198       175040 :   bool subref_array_target = false;
    8199       175040 :   bool deferred_array_component = false;
    8200       175040 :   bool substr = false;
    8201       175040 :   gfc_expr *arg, *ss_expr;
    8202              : 
    8203       175040 :   if (se->want_coarray || expr->rank == 0)
    8204         1657 :     ss = walk_coarray (expr);
    8205              :   else
    8206       173383 :     ss = gfc_walk_expr (expr);
    8207              : 
    8208       175040 :   gcc_assert (ss != NULL);
    8209       175040 :   gcc_assert (ss != gfc_ss_terminator);
    8210              : 
    8211       175040 :   ss_info = ss->info;
    8212       175040 :   ss_type = ss_info->type;
    8213       175040 :   ss_expr = ss_info->expr;
    8214              : 
    8215              :   /* Special case: TRANSPOSE which needs no temporary.  */
    8216       180800 :   while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
    8217       180409 :          && (arg = gfc_get_noncopying_intrinsic_argument (expr)) != NULL)
    8218              :     {
    8219              :       /* This is a call to transpose which has already been handled by the
    8220              :          scalarizer, so that we just need to get its argument's descriptor.  */
    8221          510 :       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
    8222          510 :       expr = expr->value.function.actual->expr;
    8223              :     }
    8224              : 
    8225       175040 :   if (!se->direct_byref)
    8226       336675 :     se->unlimited_polymorphic = UNLIMITED_POLY (expr);
    8227              : 
    8228              :   /* Special case things we know we can pass easily.  */
    8229       175040 :   switch (expr->expr_type)
    8230              :     {
    8231       157960 :     case EXPR_VARIABLE:
    8232              :       /* If we have a linear array section, we can pass it directly.
    8233              :          Otherwise we need to copy it into a temporary.  */
    8234              : 
    8235       157960 :       gcc_assert (ss_type == GFC_SS_SECTION);
    8236       157960 :       gcc_assert (ss_expr == expr);
    8237       157960 :       info = &ss_info->data.array;
    8238              : 
    8239              :       /* Get the descriptor for the array.  */
    8240       157960 :       gfc_conv_ss_descriptor (&se->pre, ss, 0);
    8241       157960 :       desc = info->descriptor;
    8242              : 
    8243              :       /* The charlen backend decl for deferred character components cannot
    8244              :          be used because it is fixed at zero.  Instead, the hidden string
    8245              :          length component is used.  */
    8246       157960 :       if (expr->ts.type == BT_CHARACTER
    8247        20493 :           && expr->ts.deferred
    8248         2878 :           && TREE_CODE (desc) == COMPONENT_REF)
    8249       157960 :         deferred_array_component = true;
    8250              : 
    8251       157960 :       substr = info->ref && info->ref->next
    8252       159121 :                && info->ref->next->type == REF_SUBSTRING;
    8253              : 
    8254              :       /* A vector subscript always needs a temporary.  */
    8255       157960 :       subref_array_target = (is_subref_array (expr)
    8256         4562 :                              && !gfc_has_vector_subscript (expr)
    8257       162515 :                              && (se->direct_byref
    8258         3762 :                                  || se->force_no_tmp
    8259         3067 :                                  || expr->ts.type == BT_CHARACTER));
    8260       157960 :       need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
    8261       157960 :                   && !subref_array_target);
    8262              : 
    8263       157960 :       if (se->force_tmp)
    8264              :         need_tmp = 1;
    8265       157759 :       else if (se->force_no_tmp)
    8266              :         need_tmp = 0;
    8267              : 
    8268       151054 :       if (need_tmp)
    8269              :         full = 0;
    8270       157656 :       else if (is_explicit_coarray (expr))
    8271              :         full = 0;
    8272       156779 :       else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
    8273              :         {
    8274              :           /* Create a new descriptor if the array doesn't have one.  */
    8275              :           full = 0;
    8276              :         }
    8277       101805 :       else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
    8278              :         full = 1;
    8279         9031 :       else if (se->direct_byref)
    8280              :         full = 0;
    8281         8668 :       else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
    8282              :         full = 1;
    8283         8458 :       else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
    8284              :         full = 0;
    8285              :       else
    8286         4516 :         full = gfc_full_array_ref_p (info->ref, NULL);
    8287              : 
    8288              :       /* A subobject of the array elements is described by a new descriptor,
    8289              :          whose element type is that of the subobject and whose span is the
    8290              :          element size of the array.  */
    8291       157960 :       if (subref_array_target && !se->direct_byref
    8292         1446 :           && info->ref && info->ref->next)
    8293              :         full = 0;
    8294              : 
    8295       251624 :       if (full && !transposed_dims (ss))
    8296              :         {
    8297        93983 :           if (se->direct_byref && !se->byref_noassign)
    8298              :             {
    8299         1108 :               struct lang_type *lhs_ls
    8300         1108 :                 = TYPE_LANG_SPECIFIC (TREE_TYPE (se->expr)),
    8301         1108 :                 *rhs_ls = TYPE_LANG_SPECIFIC (TREE_TYPE (desc));
    8302              :               /* When only the array_kind differs, do a view_convert.  */
    8303         1516 :               tmp = lhs_ls && rhs_ls && lhs_ls->rank == rhs_ls->rank
    8304         1108 :                         && lhs_ls->akind != rhs_ls->akind
    8305         1516 :                       ? build1 (VIEW_CONVERT_EXPR, TREE_TYPE (se->expr), desc)
    8306              :                       : desc;
    8307              :               /* Copy the descriptor for pointer assignments.  */
    8308         1108 :               gfc_add_modify (&se->pre, se->expr, tmp);
    8309              : 
    8310              :               /* Add any offsets from subreferences.  */
    8311         1108 :               gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
    8312              :                                       subref_array_target, expr);
    8313              : 
    8314              :               /* ....and set the span field.  */
    8315         1108 :               if (ss_info->expr->ts.type == BT_CHARACTER)
    8316          147 :                 tmp = gfc_conv_descriptor_span_get (desc);
    8317              :               else
    8318          961 :                 tmp = gfc_get_array_span (desc, expr);
    8319         1108 :               gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
    8320         1108 :             }
    8321        92875 :           else if (se->want_pointer)
    8322              :             {
    8323              :               /* We pass full arrays directly.  This means that pointers and
    8324              :                  allocatable arrays should also work.  */
    8325        14417 :               se->expr = gfc_build_addr_expr (NULL_TREE, desc);
    8326              :             }
    8327              :           else
    8328              :             {
    8329        78458 :               se->expr = desc;
    8330              :             }
    8331              : 
    8332        93983 :           if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
    8333         8529 :             se->string_length = gfc_get_expr_charlen (expr);
    8334              :           /* The ss_info string length is returned set to the value of the
    8335              :              hidden string length component.  */
    8336        85185 :           else if (deferred_array_component)
    8337          269 :             se->string_length = ss_info->string_length;
    8338              : 
    8339        93983 :           se->class_container = ss_info->class_container;
    8340              : 
    8341        93983 :           gfc_free_ss_chain (ss);
    8342       188122 :           return;
    8343              :         }
    8344              :       break;
    8345              : 
    8346         5250 :     case EXPR_FUNCTION:
    8347              :       /* A transformational function return value will be a temporary
    8348              :          array descriptor.  We still need to go through the scalarizer
    8349              :          to create the descriptor.  Elemental functions are handled as
    8350              :          arbitrary expressions, i.e. copy to a temporary.  */
    8351              : 
    8352         5250 :       if (se->direct_byref)
    8353              :         {
    8354          156 :           gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
    8355              : 
    8356              :           /* For pointer assignments pass the descriptor directly.  */
    8357          156 :           if (se->ss == NULL)
    8358          156 :             se->ss = ss;
    8359              :           else
    8360            0 :             gcc_assert (se->ss == ss);
    8361              : 
    8362          156 :           se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    8363          156 :           gfc_conv_expr (se, expr);
    8364              : 
    8365          156 :           gfc_free_ss_chain (ss);
    8366          156 :           return;
    8367              :         }
    8368              : 
    8369         5094 :       if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
    8370              :         {
    8371         3338 :           if (ss_expr != expr)
    8372              :             /* Elemental function.  */
    8373         2589 :             gcc_assert ((expr->value.function.esym != NULL
    8374              :                          && expr->value.function.esym->attr.elemental)
    8375              :                         || (expr->value.function.isym != NULL
    8376              :                             && expr->value.function.isym->elemental)
    8377              :                         || (gfc_expr_attr (expr).proc_pointer
    8378              :                             && gfc_expr_attr (expr).elemental)
    8379              :                         || gfc_inline_intrinsic_function_p (expr));
    8380              : 
    8381         3338 :           need_tmp = 1;
    8382         3338 :           if (expr->ts.type == BT_CHARACTER
    8383           35 :                 && expr->ts.u.cl->length
    8384           29 :                 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    8385           13 :             get_array_charlen (expr, se);
    8386              : 
    8387              :           info = NULL;
    8388              :         }
    8389              :       else
    8390              :         {
    8391              :           /* Transformational function.  */
    8392         1756 :           info = &ss_info->data.array;
    8393         1756 :           need_tmp = 0;
    8394              :         }
    8395              :       break;
    8396              : 
    8397        11096 :     case EXPR_ARRAY:
    8398              :       /* Constant array constructors don't need a temporary.  */
    8399        11096 :       if (ss_type == GFC_SS_CONSTRUCTOR
    8400        11096 :           && expr->ts.type != BT_CHARACTER
    8401        20837 :           && gfc_constant_array_constructor_p (expr->value.constructor))
    8402              :         {
    8403         7687 :           need_tmp = 0;
    8404         7687 :           info = &ss_info->data.array;
    8405              :         }
    8406              :       else
    8407              :         {
    8408              :           need_tmp = 1;
    8409              :           info = NULL;
    8410              :         }
    8411              :       break;
    8412              : 
    8413              :     default:
    8414              :       /* Something complicated.  Copy it into a temporary.  */
    8415              :       need_tmp = 1;
    8416              :       info = NULL;
    8417              :       break;
    8418              :     }
    8419              : 
    8420              :   /* If we are creating a temporary, we don't need to bother about aliases
    8421              :      anymore.  */
    8422        73433 :   if (need_tmp)
    8423         7785 :     se->force_tmp = 0;
    8424              : 
    8425        80901 :   gfc_init_loopinfo (&loop);
    8426              : 
    8427              :   /* Associate the SS with the loop.  */
    8428        80901 :   gfc_add_ss_to_loop (&loop, ss);
    8429              : 
    8430              :   /* Tell the scalarizer not to bother creating loop variables, etc.  */
    8431        80901 :   if (!need_tmp)
    8432        73116 :     loop.array_parameter = 1;
    8433              :   else
    8434              :     /* The right-hand side of a pointer assignment mustn't use a temporary.  */
    8435         7785 :     gcc_assert (!se->direct_byref);
    8436              : 
    8437              :   /* Do we need bounds checking or not?  */
    8438        80901 :   ss->no_bounds_check = expr->no_bounds_check;
    8439              : 
    8440              :   /* Setup the scalarizing loops and bounds.  */
    8441        80901 :   gfc_conv_ss_startstride (&loop);
    8442              : 
    8443              :   /* Add bounds-checking for elemental dimensions.  */
    8444        80901 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check)
    8445         6843 :     array_bound_check_elemental (&outermost_loop (&loop)->pre, ss, expr);
    8446              : 
    8447        80901 :   if (need_tmp)
    8448              :     {
    8449         7785 :       if (expr->ts.type == BT_CHARACTER
    8450         1590 :           && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
    8451         1481 :         get_array_charlen (expr, se);
    8452              : 
    8453              :       /* Tell the scalarizer to make a temporary.  */
    8454         7785 :       loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
    8455         7785 :                                       ((expr->ts.type == BT_CHARACTER)
    8456         1590 :                                        ? expr->ts.u.cl->backend_decl
    8457              :                                        : NULL),
    8458              :                                       loop.dimen);
    8459              : 
    8460         7785 :       se->string_length = loop.temp_ss->info->string_length;
    8461         7785 :       gcc_assert (loop.temp_ss->dimen == loop.dimen);
    8462         7785 :       gfc_add_ss_to_loop (&loop, loop.temp_ss);
    8463              :     }
    8464              : 
    8465        80901 :   gfc_conv_loop_setup (&loop, & expr->where);
    8466              : 
    8467        80901 :   if (need_tmp)
    8468              :     {
    8469              :       /* Copy into a temporary and pass that.  We don't need to copy the data
    8470              :          back because expressions and vector subscripts must be INTENT_IN.  */
    8471              :       /* TODO: Optimize passing function return values.  */
    8472         7785 :       gfc_se lse;
    8473         7785 :       gfc_se rse;
    8474         7785 :       bool deep_copy;
    8475              : 
    8476              :       /* Start the copying loops.  */
    8477         7785 :       gfc_mark_ss_chain_used (loop.temp_ss, 1);
    8478         7785 :       gfc_mark_ss_chain_used (ss, 1);
    8479         7785 :       gfc_start_scalarized_body (&loop, &block);
    8480              : 
    8481              :       /* Copy each data element.  */
    8482         7785 :       gfc_init_se (&lse, NULL);
    8483         7785 :       gfc_copy_loopinfo_to_se (&lse, &loop);
    8484         7785 :       gfc_init_se (&rse, NULL);
    8485         7785 :       gfc_copy_loopinfo_to_se (&rse, &loop);
    8486              : 
    8487         7785 :       lse.ss = loop.temp_ss;
    8488         7785 :       rse.ss = ss;
    8489              : 
    8490         7785 :       gfc_conv_tmp_array_ref (&lse);
    8491         7785 :       if (expr->ts.type == BT_CHARACTER)
    8492              :         {
    8493         1590 :           gfc_conv_expr (&rse, expr);
    8494         1590 :           if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
    8495         1057 :             rse.expr = build_fold_indirect_ref_loc (input_location,
    8496              :                                                 rse.expr);
    8497              :         }
    8498              :       else
    8499         6195 :         gfc_conv_expr_val (&rse, expr);
    8500              : 
    8501         7785 :       gfc_add_block_to_block (&block, &rse.pre);
    8502         7785 :       gfc_add_block_to_block (&block, &lse.pre);
    8503              : 
    8504         7785 :       lse.string_length = rse.string_length;
    8505              : 
    8506              :       /* A parenthesized constructor is copied like the constructor.  */
    8507         7785 :       gfc_expr *src = expr;
    8508         7785 :       if (src->expr_type == EXPR_OP
    8509          734 :           && src->value.op.op == INTRINSIC_PARENTHESES
    8510          264 :           && src->value.op.op1->expr_type == EXPR_ARRAY)
    8511         7785 :         src = src->value.op.op1;
    8512        15570 :       deep_copy = !se->data_not_needed
    8513         7785 :                   && (src->expr_type == EXPR_VARIABLE
    8514         7228 :                       || src->expr_type == EXPR_ARRAY);
    8515         7785 :       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
    8516              :                                      deep_copy, false);
    8517         7785 :       gfc_add_expr_to_block (&block, tmp);
    8518              : 
    8519              :       /* Finish the copying loops.  */
    8520         7785 :       gfc_trans_scalarizing_loops (&loop, &block);
    8521              : 
    8522         7785 :       desc = loop.temp_ss->info->data.array.descriptor;
    8523              : 
    8524              :       /* The _copy of a CLASS element is deep, so finalize the temporary
    8525              :          through the vtable to free the components of the copies.  */
    8526           48 :       tree ctmp = expr->ts.type == BT_CLASS && expr->expr_type == EXPR_OP
    8527         7815 :                   ? gfc_get_class_from_expr (desc) : NULL_TREE;
    8528           30 :       if (ctmp)
    8529              :         {
    8530           30 :           tree final_fn, has_final, elsize;
    8531              : 
    8532           30 :           final_fn = gfc_evaluate_now (gfc_class_vtab_final_get (ctmp),
    8533              :                                        &se->post);
    8534           30 :           has_final = fold_build2_loc (input_location, NE_EXPR,
    8535              :                                        logical_type_node, final_fn,
    8536           30 :                                        fold_convert (TREE_TYPE (final_fn),
    8537              :                                                      null_pointer_node));
    8538           30 :           elsize = gfc_resize_class_size_with_len (&se->post, ctmp,
    8539              :                                                    gfc_class_vtab_size_get (ctmp));
    8540              :           /* The finalization wrapper takes the rank from the descriptor.  */
    8541           30 :           gfc_conv_descriptor_rank_set (&se->post, desc, loop.dimen);
    8542           30 :           tmp = build_call_expr_loc (input_location,
    8543              :                                      build_fold_indirect_ref_loc (input_location,
    8544              :                                                                   final_fn),
    8545              :                                      3, gfc_build_addr_expr (NULL_TREE, desc),
    8546              :                                      fold_convert (gfc_charlen_type_node,
    8547              :                                                    elsize),
    8548              :                                      boolean_false_node);
    8549           30 :           tmp = build3_v (COND_EXPR, has_final, tmp,
    8550              :                           build_empty_stmt (input_location));
    8551           30 :           gfc_add_expr_to_block (&se->post, tmp);
    8552              :         }
    8553              :     }
    8554        74872 :   else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
    8555              :     {
    8556         1677 :       desc = info->descriptor;
    8557         1677 :       se->string_length = ss_info->string_length;
    8558              :     }
    8559              :   else
    8560              :     {
    8561              :       /* We pass sections without copying to a temporary.  Make a new
    8562              :          descriptor and point it at the section we want.  The loop variable
    8563              :          limits will be the limits of the section.
    8564              :          A function may decide to repack the array to speed up access, but
    8565              :          we're not bothered about that here.  */
    8566        71439 :       int dim, ndim, codim;
    8567        71439 :       tree parm;
    8568        71439 :       tree parmtype;
    8569        71439 :       tree dtype;
    8570        71439 :       tree stride;
    8571        71439 :       tree from;
    8572        71439 :       tree to;
    8573        71439 :       tree base;
    8574        71439 :       tree offset;
    8575              : 
    8576        71439 :       ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
    8577              : 
    8578        71439 :       if (se->want_coarray)
    8579              :         {
    8580          751 :           gfc_array_ref *ar = &info->ref->u.ar;
    8581              : 
    8582          751 :           codim = expr->corank;
    8583         1579 :           for (n = 0; n < codim - 1; n++)
    8584              :             {
    8585              :               /* Make sure we are not lost somehow.  */
    8586          828 :               gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
    8587              : 
    8588              :               /* Make sure the call to gfc_conv_section_startstride won't
    8589              :                  generate unnecessary code to calculate stride.  */
    8590          828 :               gcc_assert (ar->stride[n + ndim] == NULL);
    8591              : 
    8592          828 :               gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
    8593          828 :               loop.from[n + loop.dimen] = info->start[n + ndim];
    8594          828 :               loop.to[n + loop.dimen]   = info->end[n + ndim];
    8595              :             }
    8596              : 
    8597          751 :           gcc_assert (n == codim - 1);
    8598          751 :           evaluate_bound (&loop.pre, info->start, ar->start,
    8599              :                           info->descriptor, n + ndim, true,
    8600          751 :                           ar->as->type == AS_DEFERRED, true);
    8601          751 :           loop.from[n + loop.dimen] = info->start[n + ndim];
    8602              :         }
    8603              :       else
    8604              :         codim = 0;
    8605              : 
    8606              :       /* Set the string_length for a character array.  */
    8607        71439 :       if (expr->ts.type == BT_CHARACTER)
    8608              :         {
    8609        11716 :           if ((deferred_array_component && !substr)
    8610        11679 :               || expr->expr_type == EXPR_FUNCTION)
    8611          103 :             se->string_length = ss_info->string_length;
    8612              :           else
    8613        11613 :             se->string_length =  gfc_get_expr_charlen (expr);
    8614              : 
    8615        11716 :           if (VAR_P (se->string_length)
    8616          996 :               && expr->ts.u.cl->backend_decl == se->string_length)
    8617          984 :             tmp = ss_info->string_length;
    8618              :           else
    8619              :             tmp = se->string_length;
    8620              : 
    8621        11716 :           if (expr->ts.deferred && expr->ts.u.cl->backend_decl
    8622          211 :               && expr->expr_type != EXPR_FUNCTION
    8623          205 :               && VAR_P (expr->ts.u.cl->backend_decl))
    8624          150 :             gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
    8625              :           else
    8626        11566 :             expr->ts.u.cl->backend_decl = tmp;
    8627              :         }
    8628              : 
    8629              :       /* If we have an array section, are assigning  or passing an array
    8630              :          section argument make sure that the lower bound is 1.  References
    8631              :          to the full array should otherwise keep the original bounds.  */
    8632        71439 :       if (!info->ref || info->ref->u.ar.type != AR_FULL)
    8633        87079 :         for (dim = 0; dim < loop.dimen; dim++)
    8634        52723 :           if (!integer_onep (loop.from[dim]))
    8635              :             {
    8636        28637 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8637              :                                      gfc_array_index_type, gfc_index_one_node,
    8638              :                                      loop.from[dim]);
    8639        28637 :               loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
    8640              :                                               gfc_array_index_type,
    8641              :                                               loop.to[dim], tmp);
    8642        28637 :               loop.from[dim] = gfc_index_one_node;
    8643              :             }
    8644              : 
    8645        71439 :       desc = info->descriptor;
    8646        71439 :       if (se->direct_byref && !se->byref_noassign)
    8647              :         {
    8648              :           /* For pointer assignments we fill in the destination.  */
    8649         2887 :           parm = se->expr;
    8650         2887 :           parmtype = TREE_TYPE (parm);
    8651              :         }
    8652              :       else
    8653              :         {
    8654              :           /* Otherwise make a new one.  The element type is that of the
    8655              :              subobject for a subreference of the array.  */
    8656        68552 :           if (expr->ts.type == BT_CHARACTER
    8657        57542 :               || (subref_array_target && !se->direct_byref))
    8658              :             {
    8659        11226 :               gfc_typespec *ts = &expr->ts;
    8660        11226 :               if (ts->type == BT_CLASS
    8661           18 :                   && ts->u.derived->attr.is_class)
    8662           18 :                 ts = &ts->u.derived->components->ts;
    8663        11226 :               parmtype = gfc_typenode_for_spec (ts);
    8664        11226 :             }
    8665              :           else
    8666        57326 :             parmtype = gfc_get_element_type (TREE_TYPE (desc));
    8667              : 
    8668        68552 :           parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
    8669              :                                                 loop.from, loop.to, 0,
    8670              :                                                 GFC_ARRAY_UNKNOWN, false);
    8671        68552 :           parm = gfc_create_var (parmtype, "parm");
    8672              : 
    8673              :           /* When expression is a class object, then add the class' handle to
    8674              :              the parm_decl.  */
    8675        68552 :           if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
    8676              :             {
    8677         1312 :               gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
    8678         1312 :               gfc_se classse;
    8679              : 
    8680              :               /* class_expr can be NULL, when no _class ref is in expr.
    8681              :                  We must not fix this here with a gfc_fix_class_ref ().  */
    8682         1312 :               if (class_expr)
    8683              :                 {
    8684         1296 :                   gfc_init_se (&classse, NULL);
    8685         1296 :                   gfc_conv_expr (&classse, class_expr);
    8686         1296 :                   gfc_free_expr (class_expr);
    8687              : 
    8688         1296 :                   gcc_assert (classse.pre.head == NULL_TREE
    8689              :                               && classse.post.head == NULL_TREE);
    8690         1296 :                   gfc_allocate_lang_decl (parm);
    8691         1296 :                   GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
    8692              :                 }
    8693              :             }
    8694              :         }
    8695              : 
    8696        71439 :       if (expr->ts.type == BT_CHARACTER
    8697        71439 :           && VAR_P (TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)))))
    8698              :         {
    8699            0 :           tree elem_len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)));
    8700            0 :           gfc_add_modify (&loop.pre, elem_len,
    8701            0 :                           fold_convert (TREE_TYPE (elem_len),
    8702              :                           gfc_get_array_span (desc, expr)));
    8703              :         }
    8704              : 
    8705              :       /* Set the span field.  */
    8706        71439 :       tmp = NULL_TREE;
    8707        71439 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    8708         7920 :         tmp = gfc_conv_descriptor_span_get (desc);
    8709              :       else
    8710        63519 :         tmp = gfc_get_array_span (desc, expr);
    8711        71439 :       if (tmp)
    8712        71371 :         gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
    8713              : 
    8714              :       /* The following can be somewhat confusing.  We have two
    8715              :          descriptors, a new one and the original array.
    8716              :          {parm, parmtype, dim} refer to the new one.
    8717              :          {desc, type, n, loop} refer to the original, which maybe
    8718              :          a descriptorless array.
    8719              :          The bounds of the scalarization are the bounds of the section.
    8720              :          We don't have to worry about numeric overflows when calculating
    8721              :          the offsets because all elements are within the array data.  */
    8722              : 
    8723              :       /* Set the dtype.  */
    8724        71439 :       if (se->unlimited_polymorphic)
    8725          739 :         dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
    8726        70700 :       else if (expr->ts.type == BT_ASSUMED)
    8727              :         {
    8728          127 :           tree tmp2 = desc;
    8729          127 :           if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
    8730          127 :             tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
    8731          127 :           if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
    8732          127 :             tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
    8733          127 :           dtype = gfc_conv_descriptor_dtype_get (tmp2);
    8734              :         }
    8735              :       else
    8736        70573 :         dtype = gfc_get_dtype (parmtype);
    8737        71439 :       gfc_conv_descriptor_dtype_set (&loop.pre, parm, dtype);
    8738              : 
    8739              :       /* The 1st element in the section.  */
    8740        71439 :       base = gfc_index_zero_node;
    8741        71439 :       if (expr->ts.type == BT_CHARACTER && expr->rank == 0 && codim)
    8742            6 :         base = gfc_index_one_node;
    8743              : 
    8744              :       /* The offset from the 1st element in the section.  */
    8745        71439 :       offset = gfc_index_zero_node;
    8746              : 
    8747       182529 :       for (n = 0; n < ndim; n++)
    8748              :         {
    8749       111090 :           stride = gfc_conv_array_stride (desc, n);
    8750              : 
    8751              :           /* Work out the 1st element in the section.  */
    8752       111090 :           if (info->ref
    8753       102813 :               && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
    8754              :             {
    8755         1433 :               gcc_assert (info->subscript[n]
    8756              :                           && info->subscript[n]->info->type == GFC_SS_SCALAR);
    8757         1433 :               start = info->subscript[n]->info->data.scalar.value;
    8758              :             }
    8759              :           else
    8760              :             {
    8761              :               /* Evaluate and remember the start of the section.  */
    8762       109657 :               start = info->start[n];
    8763       109657 :               stride = gfc_evaluate_now (stride, &loop.pre);
    8764              :             }
    8765              : 
    8766       111090 :           tmp = gfc_conv_array_lbound (desc, n);
    8767       111090 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
    8768              :                                  start, tmp);
    8769       111090 :           tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    8770              :                                  tmp, stride);
    8771       111090 :           base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    8772              :                                     base, tmp);
    8773              : 
    8774       111090 :           if (info->ref
    8775       102813 :               && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
    8776              :             {
    8777              :               /* For elemental dimensions, we only need the 1st
    8778              :                  element in the section.  */
    8779         1433 :               continue;
    8780              :             }
    8781              : 
    8782              :           /* Vector subscripts need copying and are handled elsewhere.  */
    8783       109657 :           if (info->ref)
    8784       101380 :             gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
    8785              : 
    8786              :           /* look for the corresponding scalarizer dimension: dim.  */
    8787       164329 :           for (dim = 0; dim < ndim; dim++)
    8788       164329 :             if (ss->dim[dim] == n)
    8789              :               break;
    8790              : 
    8791              :           /* loop exited early: the DIM being looked for has been found.  */
    8792       109657 :           gcc_assert (dim < ndim);
    8793              : 
    8794              :           /* Set the new lower bound.  */
    8795       109657 :           from = loop.from[dim];
    8796       109657 :           to = loop.to[dim];
    8797              : 
    8798       109657 :           gfc_conv_descriptor_lbound_set (&loop.pre, parm,
    8799              :                                           gfc_rank_cst[dim], from);
    8800              : 
    8801              :           /* Set the new upper bound.  */
    8802       109657 :           gfc_conv_descriptor_ubound_set (&loop.pre, parm,
    8803              :                                           gfc_rank_cst[dim], to);
    8804              : 
    8805              :           /* Multiply the stride by the section stride to get the
    8806              :              total stride.  */
    8807       109657 :           stride = fold_build2_loc (input_location, MULT_EXPR,
    8808              :                                     gfc_array_index_type,
    8809              :                                     stride, info->stride[n]);
    8810              : 
    8811       109657 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8812       109657 :                                  TREE_TYPE (offset), stride, from);
    8813       109657 :           offset = fold_build2_loc (input_location, MINUS_EXPR,
    8814       109657 :                                    TREE_TYPE (offset), offset, tmp);
    8815              : 
    8816              :           /* Store the new stride.  */
    8817       109657 :           gfc_conv_descriptor_stride_set (&loop.pre, parm,
    8818              :                                           gfc_rank_cst[dim], stride);
    8819              :         }
    8820              : 
    8821              :       /* For deferred-length character we need to take the dynamic length
    8822              :          into account for the dataptr offset.  */
    8823        71439 :       if (expr->ts.type == BT_CHARACTER
    8824        11716 :           && expr->ts.deferred
    8825          217 :           && expr->ts.u.cl->backend_decl
    8826          217 :           && VAR_P (expr->ts.u.cl->backend_decl))
    8827              :         {
    8828          156 :           tree base_type = TREE_TYPE (base);
    8829          156 :           base = fold_build2_loc (input_location, MULT_EXPR, base_type, base,
    8830              :                                   fold_convert (base_type,
    8831              :                                                 expr->ts.u.cl->backend_decl));
    8832              :         }
    8833              : 
    8834        73018 :       for (n = loop.dimen; n < loop.dimen + codim; n++)
    8835              :         {
    8836         1579 :           from = loop.from[n];
    8837         1579 :           to = loop.to[n];
    8838         1579 :           gfc_conv_descriptor_lbound_set (&loop.pre, parm,
    8839              :                                           gfc_rank_cst[n], from);
    8840         1579 :           if (n < loop.dimen + codim - 1)
    8841          828 :             gfc_conv_descriptor_ubound_set (&loop.pre, parm,
    8842              :                                             gfc_rank_cst[n], to);
    8843              :         }
    8844              : 
    8845        71439 :       if (se->data_not_needed)
    8846         6329 :         gfc_conv_descriptor_data_set (&loop.pre, parm,
    8847              :                                       gfc_index_zero_node);
    8848              :       else
    8849              :         /* Point the data pointer at the 1st element in the section.  */
    8850        65110 :         gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
    8851              :                                 subref_array_target, expr);
    8852              : 
    8853        71439 :       gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
    8854              : 
    8855        71439 :       if (flag_coarray == GFC_FCOARRAY_LIB && expr->corank)
    8856              :         {
    8857          448 :           tmp = INDIRECT_REF_P (desc) ? TREE_OPERAND (desc, 0) : desc;
    8858          448 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8859              :             {
    8860           24 :               tmp = gfc_conv_descriptor_token (tmp);
    8861              :             }
    8862          424 :           else if (DECL_P (tmp) && DECL_LANG_SPECIFIC (tmp)
    8863          504 :                    && GFC_DECL_TOKEN (tmp) != NULL_TREE)
    8864           64 :             tmp = GFC_DECL_TOKEN (tmp);
    8865              :           else
    8866              :             {
    8867          360 :               tmp = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (tmp));
    8868              :             }
    8869              : 
    8870          448 :           gfc_conv_descriptor_token_set (&loop.pre, parm, tmp);
    8871              :         }
    8872              :       desc = parm;
    8873              :     }
    8874              : 
    8875              :   /* For class arrays add the class tree into the saved descriptor to
    8876              :      enable getting of _vptr and the like.  */
    8877        80901 :   if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
    8878        62874 :       && IS_CLASS_ARRAY (expr->symtree->n.sym))
    8879              :     {
    8880         1242 :       gfc_allocate_lang_decl (desc);
    8881         1242 :       GFC_DECL_SAVED_DESCRIPTOR (desc) =
    8882         1242 :           DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
    8883         1144 :             GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
    8884              :           : expr->symtree->n.sym->backend_decl;
    8885              :     }
    8886        79659 :   else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
    8887        11096 :            && IS_CLASS_ARRAY (expr))
    8888              :     {
    8889            0 :       tree vtype;
    8890            0 :       gfc_allocate_lang_decl (desc);
    8891            0 :       tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
    8892            0 :       GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
    8893            0 :       vtype = gfc_class_vptr_get (tmp);
    8894            0 :       gfc_add_modify (&se->pre, vtype,
    8895            0 :                       gfc_build_addr_expr (TREE_TYPE (vtype),
    8896            0 :                                       gfc_find_vtab (&expr->ts)->backend_decl));
    8897              :     }
    8898        80901 :   if (!se->direct_byref || se->byref_noassign)
    8899              :     {
    8900              :       /* Get a pointer to the new descriptor.  */
    8901        78014 :       if (se->want_pointer)
    8902        41441 :         se->expr = gfc_build_addr_expr (NULL_TREE, desc);
    8903              :       else
    8904        36573 :         se->expr = desc;
    8905              :     }
    8906              : 
    8907        80901 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    8908        80901 :   gfc_add_block_to_block (&se->post, &loop.post);
    8909              : 
    8910              :   /* Cleanup the scalarizer.  */
    8911        80901 :   gfc_cleanup_loop (&loop);
    8912              : }
    8913              : 
    8914              : 
    8915              : /* Calculate the array size (number of elements); if dim != NULL_TREE,
    8916              :    return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P).
    8917              :    If !expr && descriptor array, the rank is taken from the descriptor.  */
    8918              : tree
    8919        16540 : gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
    8920              : {
    8921        16540 :   if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
    8922              :     {
    8923           40 :       gcc_assert (dim == NULL_TREE);
    8924           40 :       return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
    8925              :     }
    8926        16500 :   tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
    8927        16500 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
    8928        16500 :   enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc));
    8929        16500 :   if (expr == NULL || expr->rank < 0)
    8930         4087 :     rank = gfc_conv_descriptor_rank_get (desc);
    8931              :   else
    8932        12413 :     rank = gfc_rank_cst[expr->rank];
    8933              : 
    8934        16500 :   if (dim || (expr && expr->rank == 1))
    8935              :     {
    8936         4934 :       if (dim)
    8937         9897 :         dim = fold_convert_loc (input_location, gfc_array_dim_rank_type, dim);
    8938              :       else
    8939         4934 :         dim = gfc_rank_cst[0];
    8940        14831 :       tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
    8941        14831 :       tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
    8942              : 
    8943        14831 :       size = fold_build2_loc (input_location, MINUS_EXPR,
    8944              :                               gfc_array_index_type, ubound, lbound);
    8945        14831 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    8946              :                               size, gfc_index_one_node);
    8947              :       /* if (!allocatable && !pointer && assumed rank)
    8948              :            size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
    8949              :          else
    8950              :            size = max (0, size);  */
    8951        14831 :       size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    8952              :                               size, gfc_index_zero_node);
    8953        14831 :       if (akind == GFC_ARRAY_ASSUMED_RANK_CONT
    8954        14831 :           || akind == GFC_ARRAY_ASSUMED_RANK)
    8955              :         {
    8956         3388 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8957              :                                  gfc_array_dim_rank_type, rank,
    8958              :                                  gfc_rank_cst[1]);
    8959         3388 :           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8960              :                                   dim, tmp);
    8961         3388 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8962              :                                  gfc_conv_descriptor_ubound_get (desc, dim),
    8963              :                                  build_int_cst (gfc_array_index_type, -1));
    8964         3388 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
    8965              :                                   cond, tmp);
    8966         3388 :           tmp = build_int_cst (gfc_array_index_type, -1);
    8967         3388 :           size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    8968              :                              cond, tmp, size);
    8969              :         }
    8970              :       return size;
    8971              :     }
    8972              : 
    8973              :   /* size = 1. */
    8974         1669 :   size = gfc_create_var (gfc_array_index_type, "size");
    8975         1669 :   gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
    8976         1669 :   tree extent = gfc_create_var (gfc_array_index_type, "extent");
    8977              : 
    8978         1669 :   stmtblock_t cond_block, loop_body;
    8979         1669 :   gfc_init_block (&cond_block);
    8980         1669 :   gfc_init_block (&loop_body);
    8981              : 
    8982              :   /* Loop: for (i = 0; i < rank; ++i).  */
    8983         1669 :   tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    8984              :   /* Loop body.  */
    8985              :   /* #if (assumed-rank + !allocatable && !pointer)
    8986              :        if (idx + 1 == rank && dim[idx].ubound == -1)
    8987              :          extent = -1;
    8988              :        else
    8989              :      #endif
    8990              :          extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
    8991              :          if (extent < 0)
    8992              :            extent = 0
    8993              :       size *= extent.  */
    8994         1669 :   cond = NULL_TREE;
    8995         1669 :   if (akind == GFC_ARRAY_ASSUMED_RANK_CONT || akind == GFC_ARRAY_ASSUMED_RANK)
    8996              :     {
    8997          471 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8998              :                              gfc_array_dim_rank_type, idx, gfc_rank_cst[1]);
    8999          471 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    9000              :                               tmp, rank);
    9001          471 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    9002              :                              gfc_conv_descriptor_ubound_get (desc, idx),
    9003              :                              build_int_cst (gfc_array_index_type, -1));
    9004          471 :       cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
    9005              :                               cond, tmp);
    9006              :     }
    9007         1669 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9008              :                          gfc_conv_descriptor_ubound_get (desc, idx),
    9009              :                          gfc_conv_descriptor_lbound_get (desc, idx));
    9010         1669 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9011              :                          tmp, gfc_index_one_node);
    9012         1669 :   gfc_add_modify (&cond_block, extent, tmp);
    9013         1669 :   tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
    9014              :                          extent, gfc_index_zero_node);
    9015         1669 :   tmp = build3_v (COND_EXPR, tmp,
    9016              :                   fold_build2_loc (input_location, MODIFY_EXPR,
    9017              :                                    gfc_array_index_type,
    9018              :                                    extent, gfc_index_zero_node),
    9019              :                   build_empty_stmt (input_location));
    9020         1669 :   gfc_add_expr_to_block (&cond_block, tmp);
    9021         1669 :   tmp = gfc_finish_block (&cond_block);
    9022         1669 :   if (cond)
    9023          471 :     tmp = build3_v (COND_EXPR, cond,
    9024              :                     fold_build2_loc (input_location, MODIFY_EXPR,
    9025              :                                      gfc_array_index_type, extent,
    9026              :                                      build_int_cst (gfc_array_index_type, -1)),
    9027              :                     tmp);
    9028         1669 :    gfc_add_expr_to_block (&loop_body, tmp);
    9029              :    /* size *= extent.  */
    9030         1669 :    gfc_add_modify (&loop_body, size,
    9031              :                    fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9032              :                                     size, extent));
    9033              :   /* Generate loop. */
    9034         3338 :   gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
    9035         1669 :                        build_int_cst (TREE_TYPE (idx), 1),
    9036              :                        gfc_finish_block (&loop_body));
    9037         1669 :   return size;
    9038              : }
    9039              : 
    9040              : /* Helper function for gfc_conv_array_parameter if array size needs to be
    9041              :    computed.  */
    9042              : 
    9043              : static void
    9044          152 : array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
    9045              : {
    9046          152 :   tree elem;
    9047          152 :   *size = gfc_tree_array_size (block, desc, expr, NULL);
    9048          152 :   elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
    9049          152 :   *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9050              :                            *size, fold_convert (gfc_array_index_type, elem));
    9051          152 : }
    9052              : 
    9053              : /* Helper function - return true if the argument is a pointer.  */
    9054              : 
    9055              : static bool
    9056          719 : is_pointer (gfc_expr *e)
    9057              : {
    9058          719 :   gfc_symbol *sym;
    9059              : 
    9060          719 :   if (e->expr_type != EXPR_VARIABLE ||  e->symtree == NULL)
    9061              :     return false;
    9062              : 
    9063          719 :   sym = e->symtree->n.sym;
    9064          719 :   if (sym == NULL)
    9065              :     return false;
    9066              : 
    9067          719 :   return sym->attr.pointer || sym->attr.proc_pointer;
    9068              : }
    9069              : 
    9070              : /* Assumed-rank actual argument: the caller only allocates storage for dtype
    9071              :    rank dimensions. Copying GFC_MAX_DIMENSIONS dim entries would read past the
    9072              :    physical end of the descriptor. Copy the header fields explicitly and use a
    9073              :    runtime-sized memcpy for the dim[] entries.  */
    9074              : void
    9075           78 : gfc_resize_assumed_rank_dim_field (gfc_se *se, stmtblock_t *block, tree desc)
    9076              : {
    9077           78 :   tree rank, dim_field, dim_size, copy_size, dst_ptr, src_ptr;
    9078              : 
    9079           78 :   gfc_conv_descriptor_data_set (block, desc,
    9080              :                                 gfc_conv_descriptor_data_get (se->expr));
    9081           78 :   gfc_conv_descriptor_offset_set (block, desc,
    9082              :                                   gfc_conv_descriptor_offset_get (se->expr));
    9083           78 :   gfc_conv_descriptor_dtype_set (block, desc,
    9084              :                                  gfc_conv_descriptor_dtype_get (se->expr));
    9085           78 :   rank = fold_convert (size_type_node, gfc_conv_descriptor_rank_get (se->expr));
    9086           78 :   dim_field = gfc_get_descriptor_dimension (se->expr);
    9087           78 :   dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dim_field)));
    9088           78 :   copy_size = fold_build2_loc (input_location, MULT_EXPR,
    9089              :                                size_type_node, rank, dim_size);
    9090           78 :   dst_ptr = gfc_build_addr_expr (pvoid_type_node,
    9091              :                                  gfc_get_descriptor_dimension (desc));
    9092           78 :   src_ptr = gfc_build_addr_expr (pvoid_type_node, dim_field);
    9093           78 :   gfc_add_expr_to_block (block, build_call_expr_loc (input_location,
    9094              :                          builtin_decl_explicit (BUILT_IN_MEMCPY),
    9095              :                          3, dst_ptr, src_ptr, copy_size));
    9096           78 : }
    9097              : 
    9098              : /* Convert an array for passing as an actual parameter.  */
    9099              : 
    9100              : void
    9101        68086 : gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
    9102              :                           const gfc_symbol *fsym, const char *proc_name,
    9103              :                           tree *size, tree *lbshift, tree *packed)
    9104              : {
    9105        68086 :   tree ptr;
    9106        68086 :   tree desc;
    9107        68086 :   tree tmp = NULL_TREE;
    9108        68086 :   tree stmt;
    9109        68086 :   tree parent = DECL_CONTEXT (current_function_decl);
    9110        68086 :   tree ctree;
    9111        68086 :   tree pack_attr = NULL_TREE; /* Set when packing class arrays.  */
    9112        68086 :   bool full_array_var;
    9113        68086 :   bool this_array_result;
    9114        68086 :   bool contiguous;
    9115        68086 :   bool no_pack;
    9116        68086 :   bool array_constructor;
    9117        68086 :   bool good_allocatable;
    9118        68086 :   bool ultimate_ptr_comp;
    9119        68086 :   bool ultimate_alloc_comp;
    9120        68086 :   bool readonly;
    9121        68086 :   gfc_symbol *sym;
    9122        68086 :   stmtblock_t block;
    9123        68086 :   gfc_ref *ref;
    9124              : 
    9125        68086 :   ultimate_ptr_comp = false;
    9126        68086 :   ultimate_alloc_comp = false;
    9127              : 
    9128        69080 :   for (ref = expr->ref; ref; ref = ref->next)
    9129              :     {
    9130        57082 :       if (ref->next == NULL)
    9131              :         break;
    9132              : 
    9133          994 :       if (ref->type == REF_COMPONENT)
    9134              :         {
    9135          764 :           ultimate_ptr_comp = ref->u.c.component->attr.pointer;
    9136          764 :           ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
    9137              :         }
    9138              :     }
    9139              : 
    9140        68086 :   full_array_var = false;
    9141        68086 :   contiguous = false;
    9142              : 
    9143        68086 :   if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
    9144        55935 :     full_array_var = gfc_full_array_ref_p (ref, &contiguous);
    9145              : 
    9146        55935 :   sym = full_array_var ? expr->symtree->n.sym : NULL;
    9147              : 
    9148              :   /* The symbol should have an array specification.  */
    9149        64876 :   gcc_assert (!sym || sym->as || ref->u.ar.as);
    9150              : 
    9151        68086 :   if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
    9152              :     {
    9153          804 :       if (expr->ts.u.cl->length_from_typespec && expr->ts.u.cl->length)
    9154              :         {
    9155              :           /* The constructor has an explicit character type-spec length
    9156              :              so convert it directly.  */
    9157          126 :           gfc_se cse;
    9158          126 :           gfc_init_se (&cse, NULL);
    9159          126 :           gfc_conv_expr_type (&cse, expr->ts.u.cl->length,
    9160              :                               gfc_charlen_type_node);
    9161          126 :           gfc_add_block_to_block (&se->pre, &cse.pre);
    9162          126 :           tmp = cse.expr;
    9163          126 :         }
    9164              :       else
    9165          678 :         get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
    9166              : 
    9167          804 :       expr->ts.u.cl->backend_decl = tmp;
    9168          804 :       se->string_length = tmp;
    9169              :     }
    9170              : 
    9171              :   /* Is this the result of the enclosing procedure?  */
    9172        68086 :   this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
    9173           58 :   if (this_array_result
    9174           58 :         && (sym->backend_decl != current_function_decl)
    9175            0 :         && (sym->backend_decl != parent))
    9176        68086 :     this_array_result = false;
    9177              : 
    9178              :   /* Passing an optional dummy argument as actual to an optional dummy?  */
    9179        68086 :   bool pass_optional;
    9180        68086 :   pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
    9181              : 
    9182              :   /* Passing address of the array if it is not pointer or assumed-shape.  */
    9183        68086 :   if (full_array_var && g77 && !this_array_result
    9184        16762 :       && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    9185              :     {
    9186        12647 :       tmp = gfc_get_symbol_decl (sym);
    9187              : 
    9188        12647 :       if (sym->ts.type == BT_CHARACTER)
    9189         2821 :         se->string_length = sym->ts.u.cl->backend_decl;
    9190              : 
    9191        12647 :       if (!sym->attr.pointer
    9192        12156 :           && sym->as
    9193        12156 :           && sym->as->type != AS_ASSUMED_SHAPE
    9194        11905 :           && sym->as->type != AS_DEFERRED
    9195        10409 :           && sym->as->type != AS_ASSUMED_RANK
    9196        10333 :           && !sym->attr.allocatable)
    9197              :         {
    9198              :           /* Some variables are declared directly, others are declared as
    9199              :              pointers and allocated on the heap.  */
    9200         9827 :           if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
    9201         2518 :             se->expr = tmp;
    9202              :           else
    9203         7309 :             se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    9204         9827 :           if (size)
    9205           40 :             array_parameter_size (&se->pre, tmp, expr, size);
    9206        17666 :           return;
    9207              :         }
    9208              : 
    9209         2820 :       if (sym->attr.allocatable)
    9210              :         {
    9211         1882 :           if (sym->attr.dummy || sym->attr.result)
    9212              :             {
    9213         1176 :               gfc_conv_expr_descriptor (se, expr);
    9214         1176 :               tmp = se->expr;
    9215              :             }
    9216         1882 :           if (size)
    9217           14 :             array_parameter_size (&se->pre, tmp, expr, size);
    9218         1882 :           se->expr = gfc_conv_array_data (tmp);
    9219         1882 :           if (pass_optional)
    9220              :             {
    9221           18 :               tree cond = gfc_conv_expr_present (sym);
    9222           36 :               se->expr = build3_loc (input_location, COND_EXPR,
    9223           18 :                                      TREE_TYPE (se->expr), cond, se->expr,
    9224           18 :                                      fold_convert (TREE_TYPE (se->expr),
    9225              :                                                    null_pointer_node));
    9226              :             }
    9227              :           return;
    9228              :         }
    9229              :     }
    9230              : 
    9231              :   /* A convenient reduction in scope.  */
    9232        56377 :   contiguous = g77 && !this_array_result && contiguous;
    9233              : 
    9234              :   /* There is no need to pack and unpack the array, if it is contiguous
    9235              :      and not a deferred- or assumed-shape array, or if it is simply
    9236              :      contiguous.  */
    9237        56377 :   no_pack = false;
    9238              :   // clang-format off
    9239        56377 :   if (sym)
    9240              :     {
    9241        41167 :       symbol_attribute *attr = &(IS_CLASS_ARRAY (sym)
    9242              :                                  ? CLASS_DATA (sym)->attr : sym->attr);
    9243        41167 :       gfc_array_spec *as = IS_CLASS_ARRAY (sym)
    9244        41167 :                            ? CLASS_DATA (sym)->as : sym->as;
    9245        41167 :       no_pack = (as
    9246        40899 :                  && !attr->pointer
    9247        37615 :                  && as->type != AS_DEFERRED
    9248        27865 :                  && as->type != AS_ASSUMED_RANK
    9249        65305 :                  && as->type != AS_ASSUMED_SHAPE);
    9250              :     }
    9251        56377 :   if (ref && ref->u.ar.as)
    9252        44233 :     no_pack = no_pack
    9253        44233 :               || (ref->u.ar.as->type != AS_DEFERRED
    9254              :                   && ref->u.ar.as->type != AS_ASSUMED_RANK
    9255              :                   && ref->u.ar.as->type != AS_ASSUMED_SHAPE);
    9256       112754 :   no_pack = contiguous
    9257        56377 :             && (no_pack || gfc_is_simply_contiguous (expr, false, true));
    9258              :   // clang-format on
    9259              : 
    9260              :   /* If we have an EXPR_OP or a function returning an explicit-shaped
    9261              :      or allocatable array, an array temporary will be generated which
    9262              :      does not need to be packed / unpacked if passed to an
    9263              :      explicit-shape dummy array.  */
    9264              : 
    9265        56377 :   if (g77)
    9266              :     {
    9267         7000 :       if (expr->expr_type == EXPR_OP)
    9268              :         no_pack = 1;
    9269         6923 :       else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
    9270              :         {
    9271           34 :           gfc_symbol *result = expr->value.function.esym->result;
    9272           34 :           if (result->attr.dimension
    9273           34 :               && (result->as->type == AS_EXPLICIT
    9274            7 :                   || result->attr.allocatable
    9275            0 :                   || result->attr.contiguous))
    9276        56377 :             no_pack = 1;
    9277              :         }
    9278              :     }
    9279              : 
    9280              :   /* Array constructors are always contiguous and do not need packing.  */
    9281        56377 :   array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
    9282              : 
    9283              :   /* Same is true of contiguous sections from allocatable variables.  */
    9284       112754 :   good_allocatable = contiguous
    9285         5190 :                        && expr->symtree
    9286        61567 :                        && expr->symtree->n.sym->attr.allocatable;
    9287              : 
    9288              :   /* Or ultimate allocatable components.  */
    9289        56377 :   ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
    9290              : 
    9291        56377 :   if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
    9292              :     {
    9293         5570 :       gfc_conv_expr_descriptor (se, expr);
    9294              :       /* Deallocate the allocatable components of structures that are
    9295              :          not variable.  */
    9296         5570 :       if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    9297         4018 :            && expr->ts.u.derived->attr.alloc_comp
    9298         2332 :            && expr->expr_type != EXPR_VARIABLE)
    9299              :         {
    9300            2 :           tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
    9301              : 
    9302              :           /* The components shall be deallocated before their containing entity.  */
    9303            2 :           gfc_prepend_expr_to_block (&se->post, tmp);
    9304              :         }
    9305         5570 :       if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
    9306          309 :         se->string_length = expr->ts.u.cl->backend_decl;
    9307         5570 :       if (size)
    9308           68 :         array_parameter_size (&se->pre, se->expr, expr, size);
    9309         5570 :       se->expr = gfc_conv_array_data (se->expr);
    9310         5570 :       return;
    9311              :     }
    9312              : 
    9313        50807 :   if (fsym && fsym->ts.type == BT_CLASS)
    9314              :     {
    9315         1291 :       gcc_assert (se->expr);
    9316              :       ctree = se->expr;
    9317              :     }
    9318              :   else
    9319              :     ctree = NULL_TREE;
    9320              : 
    9321        50807 :   if (this_array_result)
    9322              :     {
    9323              :       /* Result of the enclosing function.  */
    9324           58 :       gfc_conv_expr_descriptor (se, expr);
    9325           58 :       if (size)
    9326            0 :         array_parameter_size (&se->pre, se->expr, expr, size);
    9327           58 :       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    9328              : 
    9329           18 :       if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
    9330           76 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
    9331           18 :         se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
    9332              :                                                                  se->expr));
    9333              : 
    9334              :       return;
    9335              :     }
    9336              :   else
    9337              :     {
    9338              :       /* Every other type of array.  */
    9339        50749 :       se->want_pointer = (ctree) ? 0 : 1;
    9340        50749 :       se->want_coarray = expr->corank;
    9341        50749 :       gfc_conv_expr_descriptor (se, expr);
    9342              : 
    9343        50749 :       if (size)
    9344           30 :         array_parameter_size (&se->pre,
    9345              :                               build_fold_indirect_ref_loc (input_location,
    9346              :                                                             se->expr),
    9347              :                               expr, size);
    9348        50749 :       if (ctree)
    9349              :         {
    9350         1291 :           stmtblock_t block;
    9351              : 
    9352         1291 :           gfc_init_block (&block);
    9353         1291 :           if (lbshift && *lbshift)
    9354              :             {
    9355              :               /* Apply a shift of the lbound when supplied.  */
    9356           98 :               for (int dim = 0; dim < expr->rank; ++dim)
    9357           49 :                 gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
    9358              :                                                   *lbshift);
    9359              :             }
    9360         1291 :           tmp = gfc_class_data_get (ctree);
    9361         1291 :           if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
    9362           84 :               && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
    9363              :             {
    9364           36 :               tree arr = gfc_create_var (TREE_TYPE (tmp), "parm");
    9365           36 :               gfc_conv_descriptor_data_set (&block, arr,
    9366              :                                             gfc_conv_descriptor_data_get (
    9367              :                                               se->expr));
    9368           36 :               gfc_conv_descriptor_lbound_set (&block, arr, gfc_index_zero_node,
    9369              :                                               gfc_index_zero_node);
    9370           36 :               gfc_conv_descriptor_ubound_set (
    9371              :                 &block, arr, gfc_index_zero_node,
    9372              :                 gfc_conv_descriptor_size (se->expr, expr->rank));
    9373           36 :               gfc_conv_descriptor_stride_set (
    9374              :                 &block, arr, gfc_index_zero_node,
    9375              :                 gfc_conv_descriptor_stride_get (se->expr, gfc_index_zero_node));
    9376           36 :               tree dtype_val = gfc_conv_descriptor_dtype_get (se->expr);
    9377           36 :               gfc_conv_descriptor_dtype_set (&block, arr, dtype_val);
    9378           36 :               gfc_conv_descriptor_rank_set (&block, arr, 1);
    9379           36 :               gfc_conv_descriptor_span_set (&block, arr,
    9380              :                                             gfc_conv_descriptor_span_get (arr));
    9381           36 :               gfc_conv_descriptor_offset_set (&block, arr, gfc_index_zero_node);
    9382           36 :               se->expr = arr;
    9383              :             }
    9384         1291 :           if (expr->rank == -1)
    9385           78 :             gfc_resize_assumed_rank_dim_field (se, &block, tmp);
    9386         1213 :           else if (CLASS_DATA (fsym)->as->rank == -1)
    9387          397 :             gfc_class_array_data_assign (&block, tmp, se->expr, false);
    9388              :           else
    9389          816 :             gfc_class_array_data_assign (&block, tmp, se->expr, true);
    9390              : 
    9391              :           /* Handle optional.  */
    9392         1291 :           if (fsym && fsym->attr.optional && sym && sym->attr.optional)
    9393          348 :             tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
    9394              :                             gfc_finish_block (&block),
    9395              :                             build_empty_stmt (input_location));
    9396              :           else
    9397          943 :             tmp = gfc_finish_block (&block);
    9398              : 
    9399         1291 :           gfc_add_expr_to_block (&se->pre, tmp);
    9400              :         }
    9401        49458 :       else if (pass_optional && full_array_var && sym->as && sym->as->rank != 0)
    9402              :         {
    9403              :           /* Perform calculation of bounds and strides of optional array dummy
    9404              :              only if the argument is present.  */
    9405          219 :           tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
    9406              :                           gfc_finish_block (&se->pre),
    9407              :                           build_empty_stmt (input_location));
    9408          219 :           gfc_add_expr_to_block (&se->pre, tmp);
    9409              :         }
    9410              :     }
    9411              : 
    9412              :   /* Deallocate the allocatable components of structures that are
    9413              :      not variable, for descriptorless arguments.
    9414              :      Arguments with a descriptor are handled in gfc_conv_procedure_call.  */
    9415        50749 :   if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    9416           76 :           && expr->ts.u.derived->attr.alloc_comp
    9417           18 :           && expr->expr_type != EXPR_VARIABLE)
    9418              :     {
    9419            0 :       tmp = build_fold_indirect_ref_loc (input_location, se->expr);
    9420            0 :       tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
    9421              : 
    9422              :       /* The components shall be deallocated before their containing entity.  */
    9423            0 :       gfc_prepend_expr_to_block (&se->post, tmp);
    9424              :     }
    9425              : 
    9426        49337 :   if (g77 || (fsym && fsym->attr.contiguous
    9427         1605 :               && !gfc_is_simply_contiguous (expr, false, true)))
    9428              :     {
    9429         1592 :       tree origptr = NULL_TREE, packedptr = NULL_TREE;
    9430              : 
    9431         1592 :       desc = se->expr;
    9432              : 
    9433              :       /* For contiguous arrays, save the original value of the descriptor.  */
    9434         1592 :       if (!g77 && !ctree)
    9435              :         {
    9436           84 :           origptr = gfc_create_var (pvoid_type_node, "origptr");
    9437           84 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9438           84 :           tmp = gfc_conv_array_data (tmp);
    9439          168 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    9440           84 :                                  TREE_TYPE (origptr), origptr,
    9441           84 :                                  fold_convert (TREE_TYPE (origptr), tmp));
    9442           84 :           gfc_add_expr_to_block (&se->pre, tmp);
    9443              :         }
    9444              : 
    9445              :       /* Repack the array.  */
    9446         1592 :       if (warn_array_temporaries)
    9447              :         {
    9448           28 :           if (fsym)
    9449           18 :             gfc_warning (OPT_Warray_temporaries,
    9450              :                          "Creating array temporary at %L for argument %qs",
    9451           18 :                          &expr->where, fsym->name);
    9452              :           else
    9453           10 :             gfc_warning (OPT_Warray_temporaries,
    9454              :                          "Creating array temporary at %L", &expr->where);
    9455              :         }
    9456              : 
    9457              :       /* When optimizing, we can use gfc_conv_subref_array_arg for
    9458              :          making the packing and unpacking operation visible to the
    9459              :          optimizers.  */
    9460              : 
    9461         1412 :       if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
    9462          719 :           && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
    9463          349 :           && !(expr->symtree->n.sym->as
    9464          332 :                && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
    9465         1941 :           && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
    9466              :         {
    9467          329 :           gfc_conv_subref_array_arg (se, expr, g77,
    9468          153 :                                      fsym ? fsym->attr.intent : INTENT_INOUT,
    9469              :                                      fsym, proc_name, sym, true);
    9470          329 :           return;
    9471              :         }
    9472              : 
    9473         1263 :       if (ctree)
    9474              :         {
    9475           96 :           packedptr
    9476           96 :             = gfc_build_addr_expr (NULL_TREE, gfc_create_var (TREE_TYPE (ctree),
    9477              :                                                               "packed"));
    9478           96 :           if (fsym)
    9479              :             {
    9480           96 :               int pack_mask = 0;
    9481              : 
    9482              :               /* Set bit 0 to the mask, when this is an unlimited_poly
    9483              :                  class.  */
    9484           96 :               if (CLASS_DATA (fsym)->ts.u.derived->attr.unlimited_polymorphic)
    9485           36 :                 pack_mask = 1 << 0;
    9486           96 :               pack_attr = build_int_cst (integer_type_node, pack_mask);
    9487              :             }
    9488              :           else
    9489            0 :             pack_attr = integer_zero_node;
    9490              : 
    9491           96 :           gfc_add_expr_to_block (
    9492              :             &se->pre,
    9493              :             build_call_expr_loc (input_location, gfor_fndecl_in_pack_class, 4,
    9494              :                                  packedptr,
    9495              :                                  gfc_build_addr_expr (NULL_TREE, ctree),
    9496           96 :                                  size_in_bytes (TREE_TYPE (ctree)), pack_attr));
    9497           96 :           ptr = gfc_conv_array_data (gfc_class_data_get (packedptr));
    9498           96 :           se->expr = packedptr;
    9499           96 :           if (packed)
    9500           96 :             *packed = packedptr;
    9501              :         }
    9502              :       else
    9503              :         {
    9504         1167 :           ptr = build_call_expr_loc (input_location, gfor_fndecl_in_pack, 1,
    9505              :                                      desc);
    9506              : 
    9507         1167 :           if (fsym && fsym->attr.optional && sym && sym->attr.optional)
    9508              :             {
    9509           11 :               tmp = gfc_conv_expr_present (sym);
    9510           22 :               ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
    9511           11 :                                 tmp, fold_convert (TREE_TYPE (se->expr), ptr),
    9512           11 :                                 fold_convert (TREE_TYPE (se->expr),
    9513              :                                               null_pointer_node));
    9514              :             }
    9515              : 
    9516         1167 :           ptr = gfc_evaluate_now (ptr, &se->pre);
    9517              :         }
    9518              : 
    9519              :       /* Use the packed data for the actual argument, except for contiguous arrays,
    9520              :          where the descriptor's data component is set.  */
    9521         1263 :       if (g77)
    9522         1083 :         se->expr = ptr;
    9523              :       else
    9524              :         {
    9525          180 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9526              : 
    9527          180 :           if (!ctree)
    9528              :             {
    9529              :               /* The original descriptor may have transposed dims so we
    9530              :                  can't reuse it directly; we have to create a new one.  */
    9531           84 :               tree old_field;
    9532           84 :               tree old_desc = tmp;
    9533           84 :               tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
    9534              : 
    9535           84 :               old_field = gfc_conv_descriptor_dtype_get (old_desc);
    9536           84 :               gfc_conv_descriptor_dtype_set (&se->pre, new_desc, old_field);
    9537              : 
    9538           84 :               if (expr->rank == -1)
    9539              :                 {
    9540           12 :                   tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    9541           12 :                   tree stride = gfc_create_var (gfc_array_index_type, "stride");
    9542           12 :                   stmtblock_t loop_body;
    9543              : 
    9544           12 :                   gfc_conv_descriptor_offset_set (&se->pre, new_desc,
    9545              :                                                   gfc_index_zero_node);
    9546           12 :                   gfc_conv_descriptor_span_set (&se->pre, new_desc,
    9547              :                                                 gfc_conv_descriptor_span_get
    9548              :                                                 (old_desc));
    9549           12 :                   gfc_add_modify (&se->pre, stride, gfc_index_one_node);
    9550              : 
    9551           12 :                   gfc_init_block (&loop_body);
    9552              : 
    9553           12 :                   old_field = gfc_conv_descriptor_lbound_get (old_desc, idx);
    9554           12 :                   gfc_conv_descriptor_lbound_set (&loop_body, new_desc, idx,
    9555              :                                                   old_field);
    9556              : 
    9557           12 :                   old_field = gfc_conv_descriptor_ubound_get (old_desc, idx);
    9558           12 :                   gfc_conv_descriptor_ubound_set (&loop_body, new_desc, idx,
    9559              :                                                   old_field);
    9560              : 
    9561           12 :                   gfc_conv_descriptor_stride_set (&loop_body, new_desc, idx,
    9562              :                                                   stride);
    9563              : 
    9564           12 :                   tree offset = fold_build2_loc (input_location, MULT_EXPR,
    9565              :                                                  gfc_array_index_type, stride,
    9566              :                                                  gfc_conv_descriptor_lbound_get
    9567              :                                                  (new_desc, idx));
    9568           12 :                   offset = fold_build2_loc (input_location, MINUS_EXPR,
    9569              :                                             gfc_array_index_type,
    9570              :                                             gfc_conv_descriptor_offset_get
    9571              :                                             (new_desc), offset);
    9572           12 :                   gfc_conv_descriptor_offset_set (&loop_body, new_desc, offset);
    9573              : 
    9574           12 :                   tree extent = gfc_conv_array_extent_dim
    9575           12 :                                 (gfc_conv_descriptor_lbound_get (new_desc, idx),
    9576              :                                  gfc_conv_descriptor_ubound_get (new_desc, idx),
    9577              :                                  NULL);
    9578           12 :                   extent = fold_build2_loc (input_location, MULT_EXPR,
    9579              :                                             gfc_array_index_type, stride,
    9580              :                                             extent);
    9581           12 :                   gfc_add_modify (&loop_body, stride, extent);
    9582              : 
    9583           36 :                   gfc_simple_for_loop (&se->pre, idx,
    9584           12 :                                        build_int_cst (TREE_TYPE (idx), 0),
    9585              :                                        gfc_conv_descriptor_rank_get (old_desc),
    9586              :                                        LT_EXPR,
    9587           12 :                                        build_int_cst (TREE_TYPE (idx), 1),
    9588              :                                        gfc_finish_block (&loop_body));
    9589              :                 }
    9590              :               else
    9591              :                 {
    9592           72 :                   tree offset = gfc_index_zero_node;
    9593              : 
    9594           72 :                   tree stride = gfc_index_one_node;
    9595              : 
    9596          102 :                   for (int i = 0; i < expr->rank; i++)
    9597              :                     {
    9598          102 :                       tree dim = gfc_rank_cst[i];
    9599              : 
    9600          102 :                       tree lbound = gfc_conv_descriptor_lbound_get (old_desc,
    9601              :                                                                     dim);
    9602          102 :                       lbound = gfc_evaluate_now (lbound, &se->pre);
    9603          102 :                       gfc_conv_descriptor_lbound_set (&se->pre, new_desc, dim,
    9604              :                                                       lbound);
    9605              : 
    9606          102 :                       tree ubound = gfc_conv_descriptor_ubound_get (old_desc,
    9607              :                                                                     dim);
    9608          102 :                       ubound = gfc_evaluate_now (ubound, &se->pre);
    9609          102 :                       gfc_conv_descriptor_ubound_set (&se->pre, new_desc, dim,
    9610              :                                                       ubound);
    9611              : 
    9612          102 :                       gfc_conv_descriptor_stride_set (&se->pre, new_desc, dim,
    9613              :                                                       stride);
    9614              : 
    9615          102 :                       tree tmp = fold_build2_loc (input_location, MULT_EXPR,
    9616              :                                                   gfc_array_index_type,
    9617              :                                                   stride, lbound);
    9618          102 :                       offset = fold_build2_loc (input_location, MINUS_EXPR,
    9619              :                                                 gfc_array_index_type,
    9620              :                                                 offset, tmp);
    9621          102 :                       offset = gfc_evaluate_now (offset, &se->pre);
    9622              : 
    9623              :                       /* Now calculate the stride for next dimension, unless the
    9624              :                          current dimension is the last one.  */
    9625          102 :                       if (i == expr->rank - 1)
    9626              :                         break;
    9627              : 
    9628           30 :                       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9629              :                                              gfc_array_index_type,
    9630              :                                              lbound, gfc_index_one_node);
    9631           30 :                       tree extent = fold_build2_loc (input_location, MINUS_EXPR,
    9632              :                                                      gfc_array_index_type,
    9633              :                                                      ubound, tmp);
    9634           30 :                       stride = fold_build2_loc (input_location, MULT_EXPR,
    9635              :                                                 gfc_array_index_type,
    9636              :                                                 stride, extent);
    9637           30 :                       stride = gfc_evaluate_now (stride, &se->pre);
    9638              :                     }
    9639              : 
    9640           72 :                   gfc_conv_descriptor_offset_set (&se->pre, new_desc, offset);
    9641              :                 }
    9642              : 
    9643           84 :               if (flag_coarray == GFC_FCOARRAY_LIB
    9644            0 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
    9645           84 :                   && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
    9646              :                      == GFC_ARRAY_ALLOCATABLE)
    9647              :                 {
    9648            0 :                   old_field = gfc_conv_descriptor_token (old_desc);
    9649            0 :                   gfc_conv_descriptor_token_set (&se->pre, new_desc,
    9650              :                                                  old_field);
    9651              :                 }
    9652              : 
    9653           84 :               gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
    9654           84 :               se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
    9655              :             }
    9656              :         }
    9657              : 
    9658         1263 :       if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
    9659              :         {
    9660            8 :           char * msg;
    9661              : 
    9662            8 :           if (fsym && proc_name)
    9663            8 :             msg = xasprintf ("An array temporary was created for argument "
    9664            8 :                              "'%s' of procedure '%s'", fsym->name, proc_name);
    9665              :           else
    9666            0 :             msg = xasprintf ("An array temporary was created");
    9667              : 
    9668            8 :           tmp = build_fold_indirect_ref_loc (input_location,
    9669              :                                          desc);
    9670            8 :           tmp = gfc_conv_array_data (tmp);
    9671            8 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9672            8 :                                  fold_convert (TREE_TYPE (tmp), ptr), tmp);
    9673              : 
    9674            8 :           if (pass_optional)
    9675            6 :             tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9676              :                                    logical_type_node,
    9677              :                                    gfc_conv_expr_present (sym), tmp);
    9678              : 
    9679            8 :           gfc_trans_runtime_check (false, true, tmp, &se->pre,
    9680              :                                    &expr->where, msg);
    9681            8 :           free (msg);
    9682              :         }
    9683              : 
    9684         1263 :       gfc_start_block (&block);
    9685              : 
    9686              :       /* Copy the data back.  If input expr is read-only, e.g. a PARAMETER
    9687              :          array, copying back modified values is undefined behavior.  */
    9688         2526 :       readonly = (expr->expr_type == EXPR_VARIABLE
    9689          858 :                   && expr->symtree
    9690         2121 :                   && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
    9691              : 
    9692         1263 :       if ((fsym == NULL || fsym->attr.intent != INTENT_IN) && !readonly)
    9693              :         {
    9694         1106 :           if (ctree)
    9695              :             {
    9696           66 :               tmp = gfc_build_addr_expr (NULL_TREE, ctree);
    9697           66 :               tmp = build_call_expr_loc (input_location,
    9698              :                                          gfor_fndecl_in_unpack_class, 4, tmp,
    9699              :                                          packedptr,
    9700           66 :                                          size_in_bytes (TREE_TYPE (ctree)),
    9701              :                                          pack_attr);
    9702              :             }
    9703              :           else
    9704         1040 :             tmp = build_call_expr_loc (input_location, gfor_fndecl_in_unpack, 2,
    9705              :                                        desc, ptr);
    9706         1106 :           gfc_add_expr_to_block (&block, tmp);
    9707              :         }
    9708          157 :       else if (ctree && fsym->attr.intent == INTENT_IN)
    9709              :         {
    9710              :           /* Need to free the memory for class arrays, that got packed.  */
    9711           30 :           gfc_add_expr_to_block (&block, gfc_call_free (ptr));
    9712              :         }
    9713              : 
    9714              :       /* Free the temporary.  */
    9715         1136 :       if (!ctree)
    9716         1167 :         gfc_add_expr_to_block (&block, gfc_call_free (ptr));
    9717              : 
    9718         1263 :       stmt = gfc_finish_block (&block);
    9719              : 
    9720         1263 :       gfc_init_block (&block);
    9721              :       /* Only if it was repacked.  This code needs to be executed before the
    9722              :          loop cleanup code.  */
    9723         1263 :       tmp = (ctree) ? desc : build_fold_indirect_ref_loc (input_location, desc);
    9724         1263 :       tmp = gfc_conv_array_data (tmp);
    9725         1263 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9726         1263 :                              fold_convert (TREE_TYPE (tmp), ptr), tmp);
    9727              : 
    9728         1263 :       if (pass_optional)
    9729           11 :         tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9730              :                                logical_type_node,
    9731              :                                gfc_conv_expr_present (sym), tmp);
    9732              : 
    9733         1263 :       tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
    9734              : 
    9735         1263 :       gfc_add_expr_to_block (&block, tmp);
    9736         1263 :       gfc_add_block_to_block (&block, &se->post);
    9737              : 
    9738         1263 :       gfc_init_block (&se->post);
    9739              : 
    9740              :       /* Reset the descriptor pointer.  */
    9741         1263 :       if (!g77 && !ctree)
    9742              :         {
    9743           84 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9744           84 :           gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
    9745              :         }
    9746              : 
    9747         1263 :       gfc_add_block_to_block (&se->post, &block);
    9748              :     }
    9749              : }
    9750              : 
    9751              : 
    9752              : /* This helper function calculates the size in words of a full array.  */
    9753              : 
    9754              : tree
    9755        22368 : gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
    9756              : {
    9757        22368 :   tree idx;
    9758        22368 :   tree nelems;
    9759        22368 :   tree tmp;
    9760        22368 :   if (rank < 0)
    9761            0 :     idx = gfc_conv_descriptor_rank_get (decl);
    9762              :   else
    9763        22368 :     idx = gfc_rank_cst[rank - 1];
    9764        22368 :   nelems = gfc_conv_descriptor_ubound_get (decl, idx);
    9765        22368 :   tmp = gfc_conv_descriptor_lbound_get (decl, idx);
    9766        22368 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9767              :                          nelems, tmp);
    9768        22368 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9769              :                          tmp, gfc_index_one_node);
    9770        22368 :   tmp = gfc_evaluate_now (tmp, block);
    9771              : 
    9772        22368 :   nelems = gfc_conv_descriptor_stride_get (decl, idx);
    9773        22368 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9774              :                          nelems, tmp);
    9775        22368 :   return gfc_evaluate_now (tmp, block);
    9776              : }
    9777              : 
    9778              : 
    9779              : /* Allocate dest to the same size as src, and copy src -> dest.
    9780              :    If no_malloc is set, only the copy is done.  */
    9781              : 
    9782              : static tree
    9783        11200 : duplicate_allocatable (tree dest, tree src, tree type, int rank,
    9784              :                        bool no_malloc, bool no_memcpy, tree str_sz,
    9785              :                        tree add_when_allocated)
    9786              : {
    9787        11200 :   tree tmp;
    9788        11200 :   tree eltype;
    9789        11200 :   tree size;
    9790        11200 :   tree nelems;
    9791        11200 :   tree null_cond;
    9792        11200 :   tree null_data;
    9793        11200 :   stmtblock_t block;
    9794              : 
    9795              :   /* If the source is null, set the destination to null.  Then,
    9796              :      allocate memory to the destination.  */
    9797        11200 :   gfc_init_block (&block);
    9798              : 
    9799        11200 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
    9800              :     {
    9801         2653 :       gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
    9802         2653 :       null_data = gfc_finish_block (&block);
    9803              : 
    9804         2653 :       gfc_init_block (&block);
    9805         2653 :       eltype = TREE_TYPE (type);
    9806         2653 :       if (str_sz != NULL_TREE)
    9807              :         size = str_sz;
    9808              :       else
    9809         2254 :         size = TYPE_SIZE_UNIT (eltype);
    9810              : 
    9811         2653 :       if (!no_malloc)
    9812              :         {
    9813         2653 :           tmp = gfc_call_malloc (&block, type, size);
    9814         2653 :           gfc_add_modify (&block, dest, fold_convert (type, tmp));
    9815              :         }
    9816              : 
    9817         2653 :       if (!no_memcpy)
    9818              :         {
    9819         1834 :           tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9820         1834 :           tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
    9821              :                                      fold_convert (size_type_node, size));
    9822         1834 :           gfc_add_expr_to_block (&block, tmp);
    9823              :         }
    9824              :     }
    9825              :   else
    9826              :     {
    9827         8547 :       gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9828         8547 :       null_data = gfc_finish_block (&block);
    9829              : 
    9830         8547 :       gfc_init_block (&block);
    9831         8547 :       if (rank)
    9832         8532 :         nelems = gfc_full_array_size (&block, src, rank);
    9833              :       else
    9834           15 :         nelems = gfc_index_one_node;
    9835              : 
    9836              :       /* If type is not the array type, then it is the element type.  */
    9837         8547 :       if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
    9838         8517 :         eltype = gfc_get_element_type (type);
    9839              :       else
    9840              :         eltype = type;
    9841              : 
    9842         8547 :       if (str_sz != NULL_TREE)
    9843           55 :         tmp = fold_convert (gfc_array_index_type, str_sz);
    9844              :       else
    9845         8492 :         tmp = fold_convert (gfc_array_index_type,
    9846              :                             TYPE_SIZE_UNIT (eltype));
    9847              : 
    9848         8547 :       tmp = gfc_evaluate_now (tmp, &block);
    9849         8547 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9850              :                               nelems, tmp);
    9851         8547 :       if (!no_malloc)
    9852              :         {
    9853         8449 :           tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
    9854         8449 :           tmp = gfc_call_malloc (&block, tmp, size);
    9855         8449 :           gfc_conv_descriptor_data_set (&block, dest, tmp);
    9856              :         }
    9857              : 
    9858              :       /* We know the temporary and the value will be the same length,
    9859              :          so can use memcpy.  */
    9860         8547 :       if (!no_memcpy)
    9861              :         {
    9862         7186 :           tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9863         7186 :           tmp = build_call_expr_loc (input_location, tmp, 3,
    9864              :                                      gfc_conv_descriptor_data_get (dest),
    9865              :                                      gfc_conv_descriptor_data_get (src),
    9866              :                                      fold_convert (size_type_node, size));
    9867         7186 :           gfc_add_expr_to_block (&block, tmp);
    9868              :         }
    9869              :     }
    9870              : 
    9871        11200 :   gfc_add_expr_to_block (&block, add_when_allocated);
    9872        11200 :   tmp = gfc_finish_block (&block);
    9873              : 
    9874              :   /* Null the destination if the source is null; otherwise do
    9875              :      the allocate and copy.  */
    9876        11200 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
    9877              :     null_cond = src;
    9878              :   else
    9879         8547 :     null_cond = gfc_conv_descriptor_data_get (src);
    9880              : 
    9881        11200 :   null_cond = convert (pvoid_type_node, null_cond);
    9882        11200 :   null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9883              :                                null_cond, null_pointer_node);
    9884        11200 :   return build3_v (COND_EXPR, null_cond, tmp, null_data);
    9885              : }
    9886              : 
    9887              : 
    9888              : /* Allocate dest to the same size as src, and copy data src -> dest.  */
    9889              : 
    9890              : tree
    9891         8015 : gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
    9892              :                            tree add_when_allocated)
    9893              : {
    9894         8015 :   return duplicate_allocatable (dest, src, type, rank, false, false,
    9895         8015 :                                 NULL_TREE, add_when_allocated);
    9896              : }
    9897              : 
    9898              : 
    9899              : /* Copy data src -> dest.  */
    9900              : 
    9901              : tree
    9902           98 : gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
    9903              : {
    9904           98 :   return duplicate_allocatable (dest, src, type, rank, true, false,
    9905           98 :                                 NULL_TREE, NULL_TREE);
    9906              : }
    9907              : 
    9908              : /* Allocate dest to the same size as src, but don't copy anything.  */
    9909              : 
    9910              : tree
    9911         2180 : gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
    9912              : {
    9913         2180 :   return duplicate_allocatable (dest, src, type, rank, false, true,
    9914         2180 :                                 NULL_TREE, NULL_TREE);
    9915              : }
    9916              : 
    9917              : static tree
    9918           64 : duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
    9919              :                                int rank, tree add_when_allocated)
    9920              : {
    9921           64 :   tree tmp;
    9922           64 :   tree size;
    9923           64 :   tree nelems;
    9924           64 :   tree null_cond;
    9925           64 :   tree null_data;
    9926           64 :   stmtblock_t block, globalblock;
    9927              : 
    9928              :   /* If the source is null, set the destination to null.  Then,
    9929              :      allocate memory to the destination.  */
    9930           64 :   gfc_init_block (&block);
    9931           64 :   gfc_init_block (&globalblock);
    9932              : 
    9933           64 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
    9934              :     {
    9935           18 :       gfc_se se;
    9936           18 :       symbol_attribute attr;
    9937           18 :       tree dummy_desc;
    9938              : 
    9939           18 :       gfc_init_se (&se, NULL);
    9940           18 :       gfc_clear_attr (&attr);
    9941           18 :       attr.allocatable = 1;
    9942           18 :       dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
    9943           18 :       gfc_add_block_to_block (&globalblock, &se.pre);
    9944           18 :       size = TYPE_SIZE_UNIT (TREE_TYPE (type));
    9945              : 
    9946           18 :       gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
    9947           18 :       gfc_allocate_using_caf_lib (&block, dummy_desc, size,
    9948              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9949              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9950              :                                   GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
    9951           18 :       gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
    9952           18 :       null_data = gfc_finish_block (&block);
    9953              : 
    9954           18 :       gfc_init_block (&block);
    9955              : 
    9956           18 :       gfc_allocate_using_caf_lib (&block, dummy_desc,
    9957              :                                   fold_convert (size_type_node, size),
    9958              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9959              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9960              :                                   GFC_CAF_COARRAY_ALLOC);
    9961           18 :       gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
    9962              : 
    9963           18 :       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9964           18 :       tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
    9965              :                                  fold_convert (size_type_node, size));
    9966           18 :       gfc_add_expr_to_block (&block, tmp);
    9967              :     }
    9968              :   else
    9969              :     {
    9970              :       /* Set the rank or uninitialized memory access may be reported.  */
    9971           46 :       gfc_conv_descriptor_rank_set (&globalblock, dest, rank);
    9972              : 
    9973           46 :       if (rank)
    9974           46 :         nelems = gfc_full_array_size (&globalblock, src, rank);
    9975              :       else
    9976            0 :         nelems = integer_one_node;
    9977              : 
    9978           46 :       tmp = fold_convert (size_type_node,
    9979              :                           TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    9980           46 :       size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    9981              :                               fold_convert (size_type_node, nelems), tmp);
    9982              : 
    9983           46 :       gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9984           46 :       gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
    9985              :                                                               size),
    9986              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9987              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9988              :                                   GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
    9989           46 :       null_data = gfc_finish_block (&block);
    9990              : 
    9991           46 :       gfc_init_block (&block);
    9992           46 :       gfc_allocate_using_caf_lib (&block, dest,
    9993              :                                   fold_convert (size_type_node, size),
    9994              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9995              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9996              :                                   GFC_CAF_COARRAY_ALLOC);
    9997              : 
    9998           46 :       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9999           46 :       tmp = build_call_expr_loc (input_location, tmp, 3,
   10000              :                                  gfc_conv_descriptor_data_get (dest),
   10001              :                                  gfc_conv_descriptor_data_get (src),
   10002              :                                  fold_convert (size_type_node, size));
   10003           46 :       gfc_add_expr_to_block (&block, tmp);
   10004              :     }
   10005           64 :   gfc_add_expr_to_block (&block, add_when_allocated);
   10006           64 :   tmp = gfc_finish_block (&block);
   10007              : 
   10008              :   /* Null the destination if the source is null; otherwise do
   10009              :      the register and copy.  */
   10010           64 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
   10011              :     null_cond = src;
   10012              :   else
   10013           46 :     null_cond = gfc_conv_descriptor_data_get (src);
   10014              : 
   10015           64 :   null_cond = convert (pvoid_type_node, null_cond);
   10016           64 :   null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10017              :                                null_cond, null_pointer_node);
   10018           64 :   gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
   10019              :                                                  null_data));
   10020           64 :   return gfc_finish_block (&globalblock);
   10021              : }
   10022              : 
   10023              : 
   10024              : /* Helper function to abstract whether coarray processing is enabled.  */
   10025              : 
   10026              : static bool
   10027         4967 : caf_enabled (int caf_mode)
   10028              : {
   10029         4967 :   return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
   10030         4967 :       == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
   10031              : }
   10032              : 
   10033              : 
   10034              : /* Helper function to abstract whether coarray processing is enabled
   10035              :    and we are in a derived type coarray.  */
   10036              : 
   10037              : static bool
   10038        15169 : caf_in_coarray (int caf_mode)
   10039              : {
   10040        15169 :   static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   10041              :                          | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
   10042        15169 :   return (caf_mode & pat) == pat;
   10043              : }
   10044              : 
   10045              : 
   10046              : /* Helper function to abstract whether coarray is to deallocate only.  */
   10047              : 
   10048              : bool
   10049          427 : gfc_caf_is_dealloc_only (int caf_mode)
   10050              : {
   10051          427 :   return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
   10052          427 :       == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
   10053              : }
   10054              : 
   10055              : 
   10056              : /* Recursively traverse an object of derived type, generating code to
   10057              :    deallocate, nullify or copy allocatable components.  This is the work horse
   10058              :    function for the functions named in this enum.  */
   10059              : 
   10060              : enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
   10061              :       COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
   10062              :       ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
   10063              :       CHECK_PDT_INTENT_OUT_DUMMY, BCAST_ALLOC_COMP};
   10064              : 
   10065              : static gfc_actual_arglist *pdt_param_list;
   10066              : static bool generating_copy_helper;
   10067              : static hash_set<gfc_symbol *> seen_derived_types;
   10068              : 
   10069              : /* Forward declaration of structure_alloc_comps for wrapper generator.  */
   10070              : static tree structure_alloc_comps (gfc_symbol *, tree, tree, int, int, int,
   10071              :                                     gfc_co_subroutines_args *, bool);
   10072              : 
   10073              : /* Generate a wrapper function that performs element-wise deep copy for
   10074              :    recursive allocatable array components. This wrapper is passed as a
   10075              :    function pointer to the runtime helper _gfortran_cfi_deep_copy_array,
   10076              :    allowing recursion to happen at runtime instead of compile time.  */
   10077              : 
   10078              : static tree
   10079          505 : get_copy_helper_function_type (void)
   10080              : {
   10081          505 :   static tree fn_type = NULL_TREE;
   10082          505 :   if (fn_type == NULL_TREE)
   10083          105 :     fn_type = build_function_type_list (void_type_node,
   10084              :                                         pvoid_type_node,
   10085              :                                         pvoid_type_node,
   10086              :                                         NULL_TREE);
   10087          505 :   return fn_type;
   10088              : }
   10089              : 
   10090              : static tree
   10091         1724 : get_copy_helper_pointer_type (void)
   10092              : {
   10093         1724 :   static tree ptr_type = NULL_TREE;
   10094         1724 :   if (ptr_type == NULL_TREE)
   10095          105 :     ptr_type = build_pointer_type (get_copy_helper_function_type ());
   10096         1724 :   return ptr_type;
   10097              : }
   10098              : 
   10099              : static tree
   10100          400 : generate_element_copy_wrapper (gfc_symbol *der_type, tree comp_type,
   10101              :                                 int purpose, int caf_mode)
   10102              : {
   10103          400 :   tree fndecl, fntype, result_decl;
   10104          400 :   tree dest_parm, src_parm, dest_typed, src_typed;
   10105          400 :   tree der_type_ptr;
   10106          400 :   stmtblock_t block;
   10107          400 :   tree decls;
   10108          400 :   tree body;
   10109              : 
   10110          400 :   fntype = get_copy_helper_function_type ();
   10111              : 
   10112          400 :   fndecl = build_decl (input_location, FUNCTION_DECL,
   10113              :                        create_tmp_var_name ("copy_element"),
   10114              :                        fntype);
   10115              : 
   10116          400 :   TREE_STATIC (fndecl) = 1;
   10117          400 :   TREE_USED (fndecl) = 1;
   10118          400 :   DECL_ARTIFICIAL (fndecl) = 1;
   10119          400 :   DECL_IGNORED_P (fndecl) = 0;
   10120          400 :   TREE_PUBLIC (fndecl) = 0;
   10121          400 :   DECL_UNINLINABLE (fndecl) = 1;
   10122          400 :   DECL_EXTERNAL (fndecl) = 0;
   10123          400 :   DECL_CONTEXT (fndecl) = NULL_TREE;
   10124          400 :   DECL_INITIAL (fndecl) = make_node (BLOCK);
   10125          400 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
   10126              : 
   10127          400 :   result_decl = build_decl (input_location, RESULT_DECL, NULL_TREE,
   10128              :                             void_type_node);
   10129          400 :   DECL_ARTIFICIAL (result_decl) = 1;
   10130          400 :   DECL_IGNORED_P (result_decl) = 1;
   10131          400 :   DECL_CONTEXT (result_decl) = fndecl;
   10132          400 :   DECL_RESULT (fndecl) = result_decl;
   10133              : 
   10134          400 :   dest_parm = build_decl (input_location, PARM_DECL,
   10135              :                           get_identifier ("dest"), pvoid_type_node);
   10136          400 :   src_parm = build_decl (input_location, PARM_DECL,
   10137              :                          get_identifier ("src"), pvoid_type_node);
   10138              : 
   10139          400 :   DECL_ARTIFICIAL (dest_parm) = 1;
   10140          400 :   DECL_ARTIFICIAL (src_parm) = 1;
   10141          400 :   DECL_ARG_TYPE (dest_parm) = pvoid_type_node;
   10142          400 :   DECL_ARG_TYPE (src_parm) = pvoid_type_node;
   10143          400 :   DECL_CONTEXT (dest_parm) = fndecl;
   10144          400 :   DECL_CONTEXT (src_parm) = fndecl;
   10145              : 
   10146          400 :   DECL_ARGUMENTS (fndecl) = dest_parm;
   10147          400 :   TREE_CHAIN (dest_parm) = src_parm;
   10148              : 
   10149          400 :   push_struct_function (fndecl);
   10150          400 :   cfun->function_end_locus = input_location;
   10151              : 
   10152          400 :   pushlevel ();
   10153          400 :   gfc_init_block (&block);
   10154              : 
   10155          400 :   bool saved_generating = generating_copy_helper;
   10156          400 :   generating_copy_helper = true;
   10157              : 
   10158              :   /* When generating a wrapper, we need a fresh type tracking state to
   10159              :      avoid inheriting the parent context's seen_derived_types, which would
   10160              :      cause infinite recursion when the wrapper tries to handle the same
   10161              :      recursive type.  Save elements, clear the set, generate wrapper, then
   10162              :      restore elements.  */
   10163          400 :   vec<gfc_symbol *> saved_symbols = vNULL;
   10164          400 :   for (hash_set<gfc_symbol *>::iterator it = seen_derived_types.begin ();
   10165          960 :        it != seen_derived_types.end (); ++it)
   10166          560 :     saved_symbols.safe_push (*it);
   10167          400 :   seen_derived_types.empty ();
   10168              : 
   10169          400 :   der_type_ptr = build_pointer_type (comp_type);
   10170          400 :   dest_typed = fold_convert (der_type_ptr, dest_parm);
   10171          400 :   src_typed = fold_convert (der_type_ptr, src_parm);
   10172              : 
   10173          400 :   dest_typed = build_fold_indirect_ref (dest_typed);
   10174          400 :   src_typed = build_fold_indirect_ref (src_typed);
   10175              : 
   10176          400 :   body = structure_alloc_comps (der_type, src_typed, dest_typed,
   10177              :                                 0, purpose, caf_mode, NULL, false);
   10178          400 :   gfc_add_expr_to_block (&block, body);
   10179              : 
   10180              :   /* Restore saved symbols.  */
   10181          400 :   seen_derived_types.empty ();
   10182          960 :   for (unsigned i = 0; i < saved_symbols.length (); i++)
   10183          560 :     seen_derived_types.add (saved_symbols[i]);
   10184          400 :   saved_symbols.release ();
   10185          400 :   generating_copy_helper = saved_generating;
   10186              : 
   10187          400 :   body = gfc_finish_block (&block);
   10188          400 :   decls = getdecls ();
   10189              : 
   10190          400 :   poplevel (1, 1);
   10191              : 
   10192          800 :   DECL_SAVED_TREE (fndecl)
   10193          400 :     = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR,
   10194          400 :                        void_type_node, decls, body, DECL_INITIAL (fndecl));
   10195              : 
   10196          400 :   pop_cfun ();
   10197              : 
   10198              :   /* Use finalize_function with no_collect=true to skip the ggc_collect
   10199              :      call that add_new_function would trigger.  This function is called
   10200              :      during tree lowering of structure_alloc_comps where caller stack
   10201              :      frames hold locally-computed tree nodes (COMPONENT_REFs etc.) that
   10202              :      are not yet attached to any GC root.  A collection at this point
   10203              :      would free those nodes and cause segfaults.  PR124235.  */
   10204          400 :   cgraph_node::finalize_function (fndecl, true);
   10205              : 
   10206          400 :   return build1 (ADDR_EXPR, get_copy_helper_pointer_type (), fndecl);
   10207              : }
   10208              : 
   10209              : static tree
   10210        31061 : structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
   10211              :                        int rank, int purpose, int caf_mode,
   10212              :                        gfc_co_subroutines_args *args,
   10213              :                        bool no_finalization = false)
   10214              : {
   10215        31061 :   gfc_component *c;
   10216        31061 :   gfc_loopinfo loop;
   10217        31061 :   stmtblock_t fnblock;
   10218        31061 :   stmtblock_t loopbody;
   10219        31061 :   stmtblock_t tmpblock;
   10220        31061 :   tree decl_type;
   10221        31061 :   tree tmp;
   10222        31061 :   tree comp;
   10223        31061 :   tree dcmp;
   10224        31061 :   tree nelems;
   10225        31061 :   tree index;
   10226        31061 :   tree var;
   10227        31061 :   tree cdecl;
   10228        31061 :   tree ctype;
   10229        31061 :   tree vref, dref;
   10230        31061 :   tree null_cond = NULL_TREE;
   10231        31061 :   tree add_when_allocated;
   10232        31061 :   tree dealloc_fndecl;
   10233        31061 :   tree caf_token;
   10234        31061 :   gfc_symbol *vtab;
   10235        31061 :   int caf_dereg_mode;
   10236        31061 :   symbol_attribute *attr;
   10237        31061 :   bool deallocate_called;
   10238        31061 :   bool intent_out = purpose == CHECK_PDT_INTENT_OUT_DUMMY;
   10239              : 
   10240        31061 :   gfc_init_block (&fnblock);
   10241              : 
   10242        31061 :   decl_type = TREE_TYPE (decl);
   10243              : 
   10244        31061 :   if ((POINTER_TYPE_P (decl_type))
   10245              :         || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
   10246              :     {
   10247         2104 :       decl = build_fold_indirect_ref_loc (input_location, decl);
   10248              :       /* Deref dest in sync with decl, but only when it is not NULL.  */
   10249         2104 :       if (dest)
   10250          124 :         dest = build_fold_indirect_ref_loc (input_location, dest);
   10251              : 
   10252              :       /* Update the decl_type because it got dereferenced.  */
   10253         2104 :       decl_type = TREE_TYPE (decl);
   10254              :     }
   10255              : 
   10256              :   /* If this is an array of derived types with allocatable components
   10257              :      build a loop and recursively call this function.  */
   10258        31061 :   if (TREE_CODE (decl_type) == ARRAY_TYPE
   10259        31061 :       || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
   10260              :     {
   10261         5301 :       tmp = gfc_conv_array_data (decl);
   10262         5301 :       var = build_fold_indirect_ref_loc (input_location, tmp);
   10263              : 
   10264              :       /* Get the number of elements - 1 and set the counter.  */
   10265         5301 :       if (GFC_DESCRIPTOR_TYPE_P (decl_type))
   10266              :         {
   10267              :           /* Use the descriptor for an allocatable array.  Since this
   10268              :              is a full array reference, we only need the descriptor
   10269              :              information from dimension = rank.  */
   10270         3804 :           tmp = gfc_full_array_size (&fnblock, decl, rank);
   10271         3804 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
   10272              :                                  gfc_array_index_type, tmp,
   10273              :                                  gfc_index_one_node);
   10274              : 
   10275         3804 :           null_cond = gfc_conv_descriptor_data_get (decl);
   10276         3804 :           null_cond = fold_build2_loc (input_location, NE_EXPR,
   10277              :                                        logical_type_node, null_cond,
   10278         3804 :                                        build_int_cst (TREE_TYPE (null_cond), 0));
   10279              :         }
   10280              :       else
   10281              :         {
   10282              :           /*  Otherwise use the TYPE_DOMAIN information.  */
   10283         1497 :           tmp = array_type_nelts_minus_one (decl_type);
   10284         1497 :           tmp = fold_convert (gfc_array_index_type, tmp);
   10285              :         }
   10286              : 
   10287              :       /* Remember that this is, in fact, the no. of elements - 1.  */
   10288         5301 :       nelems = gfc_evaluate_now (tmp, &fnblock);
   10289         5301 :       index = gfc_create_var (gfc_array_index_type, "S");
   10290              : 
   10291              :       /* Build the body of the loop.  */
   10292         5301 :       gfc_init_block (&loopbody);
   10293              : 
   10294         5301 :       vref = gfc_build_array_ref (var, index, NULL);
   10295              : 
   10296         5301 :       if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
   10297              :         {
   10298         1132 :           tmp = build_fold_indirect_ref_loc (input_location,
   10299              :                                              gfc_conv_array_data (dest));
   10300         1132 :           dref = gfc_build_array_ref (tmp, index, NULL);
   10301         1132 :           tmp = structure_alloc_comps (der_type, vref, dref, rank,
   10302              :                                        COPY_ALLOC_COMP, caf_mode, args,
   10303              :                                        no_finalization);
   10304              :         }
   10305              :       else
   10306         4169 :         tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
   10307              :                                      caf_mode, args, no_finalization);
   10308              : 
   10309         5301 :       gfc_add_expr_to_block (&loopbody, tmp);
   10310              : 
   10311              :       /* Build the loop and return.  */
   10312         5301 :       gfc_init_loopinfo (&loop);
   10313         5301 :       loop.dimen = 1;
   10314         5301 :       loop.from[0] = gfc_index_zero_node;
   10315         5301 :       loop.loopvar[0] = index;
   10316         5301 :       loop.to[0] = nelems;
   10317         5301 :       gfc_trans_scalarizing_loops (&loop, &loopbody);
   10318         5301 :       gfc_add_block_to_block (&fnblock, &loop.pre);
   10319              : 
   10320         5301 :       tmp = gfc_finish_block (&fnblock);
   10321              :       /* When copying allocateable components, the above implements the
   10322              :          deep copy.  Nevertheless is a deep copy only allowed, when the current
   10323              :          component is allocated, for which code will be generated in
   10324              :          gfc_duplicate_allocatable (), where the deep copy code is just added
   10325              :          into the if's body, by adding tmp (the deep copy code) as last
   10326              :          argument to gfc_duplicate_allocatable ().  */
   10327         5301 :       if (purpose == COPY_ALLOC_COMP && caf_mode == 0
   10328         5301 :           && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
   10329          847 :         tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
   10330              :                                          tmp);
   10331         4454 :       else if (null_cond != NULL_TREE)
   10332         2957 :         tmp = build3_v (COND_EXPR, null_cond, tmp,
   10333              :                         build_empty_stmt (input_location));
   10334              : 
   10335         5301 :       return tmp;
   10336              :     }
   10337              : 
   10338        25760 :   if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
   10339              :     {
   10340          961 :       tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   10341              :                                    DEALLOCATE_PDT_COMP, 0, args,
   10342              :                                    no_finalization);
   10343          961 :       gfc_add_expr_to_block (&fnblock, tmp);
   10344              :     }
   10345        24799 :   else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
   10346              :     {
   10347          185 :       tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   10348              :                                    NULLIFY_ALLOC_COMP, 0, args,
   10349              :                                    no_finalization);
   10350          185 :       gfc_add_expr_to_block (&fnblock, tmp);
   10351              :     }
   10352              : 
   10353              :   /* Still having a descriptor array of rank == 0 here, indicates an
   10354              :      allocatable coarrays.  Dereference it correctly.  */
   10355        25760 :   if (GFC_DESCRIPTOR_TYPE_P (decl_type))
   10356              :     {
   10357           23 :       decl = build_fold_indirect_ref (gfc_conv_array_data (decl));
   10358              :     }
   10359              :   /* Otherwise, act on the components or recursively call self to
   10360              :      act on a chain of components.  */
   10361        25760 :   seen_derived_types.add (der_type);
   10362        76782 :   for (c = der_type->components; c; c = c->next)
   10363              :     {
   10364        51022 :       bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
   10365        51022 :                                   || c->ts.type == BT_CLASS)
   10366        51022 :                                     && c->ts.u.derived->attr.alloc_comp;
   10367        51022 :       bool same_type
   10368              :         = (c->ts.type == BT_DERIVED
   10369        11176 :            && seen_derived_types.contains (c->ts.u.derived))
   10370        59100 :           || (c->ts.type == BT_CLASS
   10371         2905 :               && seen_derived_types.contains (CLASS_DATA (c)->ts.u.derived));
   10372        51022 :       bool inside_wrapper = generating_copy_helper;
   10373              : 
   10374        51022 :       bool is_pdt_type = IS_PDT (c);
   10375        51022 :       tree strlen = NULL_TREE;
   10376              : 
   10377        51022 :       cdecl = c->backend_decl;
   10378        51022 :       ctype = TREE_TYPE (cdecl);
   10379              : 
   10380        51022 :       switch (purpose)
   10381              :         {
   10382              : 
   10383            3 :         case BCAST_ALLOC_COMP:
   10384              : 
   10385            3 :           tree ubound;
   10386            3 :           tree cdesc;
   10387            3 :           stmtblock_t derived_type_block;
   10388              : 
   10389            3 :           gfc_init_block (&tmpblock);
   10390              : 
   10391            3 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10392              :                                   decl, cdecl, NULL_TREE);
   10393              : 
   10394              :           /* Shortcut to get the attributes of the component.  */
   10395            3 :           if (c->ts.type == BT_CLASS)
   10396              :             {
   10397            0 :               attr = &CLASS_DATA (c)->attr;
   10398            0 :               if (attr->class_pointer)
   10399            0 :                 continue;
   10400              :             }
   10401              :           else
   10402              :             {
   10403            3 :               attr = &c->attr;
   10404            3 :               if (attr->pointer)
   10405            0 :                 continue;
   10406              :             }
   10407              : 
   10408              :           /* Do not broadcast a caf_token.  These are local to the image.  */
   10409            3 :           if (attr->caf_token)
   10410            1 :             continue;
   10411              : 
   10412            2 :           add_when_allocated = NULL_TREE;
   10413            2 :           if (cmp_has_alloc_comps
   10414            0 :               && !c->attr.pointer && !c->attr.proc_pointer)
   10415              :             {
   10416            0 :               if (c->ts.type == BT_CLASS)
   10417              :                 {
   10418            0 :                   rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
   10419            0 :                   add_when_allocated
   10420            0 :                       = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
   10421              :                                                comp, NULL_TREE, rank, purpose,
   10422              :                                                caf_mode, args, no_finalization);
   10423              :                 }
   10424              :               else
   10425              :                 {
   10426            0 :                   rank = c->as ? c->as->rank : 0;
   10427            0 :                   add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10428              :                                                               comp, NULL_TREE,
   10429              :                                                               rank, purpose,
   10430              :                                                               caf_mode, args,
   10431              :                                                               no_finalization);
   10432              :                 }
   10433              :             }
   10434              : 
   10435            2 :           gfc_init_block (&derived_type_block);
   10436            2 :           if (add_when_allocated)
   10437            0 :             gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
   10438            2 :           tmp = gfc_finish_block (&derived_type_block);
   10439            2 :           gfc_add_expr_to_block (&tmpblock, tmp);
   10440              : 
   10441              :           /* Convert the component into a rank 1 descriptor type.  */
   10442            2 :           if (attr->dimension)
   10443              :             {
   10444            0 :               tmp = gfc_get_element_type (TREE_TYPE (comp));
   10445            0 :               if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10446            0 :                 ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
   10447              :               else
   10448            0 :                 ubound = gfc_full_array_size (&tmpblock, comp,
   10449            0 :                                               c->ts.type == BT_CLASS
   10450            0 :                                               ? CLASS_DATA (c)->as->rank
   10451            0 :                                               : c->as->rank);
   10452              :             }
   10453              :           else
   10454              :             {
   10455            2 :               tmp = TREE_TYPE (comp);
   10456            2 :               ubound = build_int_cst (gfc_array_index_type, 1);
   10457              :             }
   10458              : 
   10459              :           /* Treat strings like arrays.  Or the other way around, do not
   10460              :            * generate an additional array layer for scalar components.  */
   10461            2 :           if (attr->dimension || c->ts.type == BT_CHARACTER)
   10462              :             {
   10463            0 :               cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
   10464              :                                                  &ubound, 1,
   10465              :                                                  GFC_ARRAY_ALLOCATABLE, false);
   10466              : 
   10467            0 :               cdesc = gfc_create_var (cdesc, "cdesc");
   10468            0 :               DECL_ARTIFICIAL (cdesc) = 1;
   10469              : 
   10470            0 :               gfc_conv_descriptor_dtype_set (&tmpblock, cdesc,
   10471              :                                              gfc_get_dtype_rank_type (1, tmp));
   10472            0 :               gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
   10473              :                                               gfc_index_zero_node,
   10474              :                                               gfc_index_one_node);
   10475            0 :               gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
   10476              :                                               gfc_index_zero_node,
   10477              :                                               gfc_index_one_node);
   10478            0 :               gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
   10479              :                                               gfc_index_zero_node, ubound);
   10480              :             }
   10481              :           else
   10482              :             /* Prevent warning.  */
   10483              :             cdesc = NULL_TREE;
   10484              : 
   10485            2 :           if (attr->dimension)
   10486              :             {
   10487            0 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10488            0 :                 comp = gfc_conv_descriptor_data_get (comp);
   10489              :               else
   10490            0 :                 comp = gfc_build_addr_expr (NULL_TREE, comp);
   10491              :             }
   10492              :           else
   10493              :             {
   10494            2 :               gfc_se se;
   10495              : 
   10496            2 :               gfc_init_se (&se, NULL);
   10497              : 
   10498            2 :               comp = gfc_conv_scalar_to_descriptor (&se, comp,
   10499            2 :                                                      c->ts.type == BT_CLASS
   10500            2 :                                                      ? CLASS_DATA (c)->attr
   10501              :                                                      : c->attr);
   10502            2 :               if (c->ts.type == BT_CHARACTER)
   10503            0 :                 comp = gfc_build_addr_expr (NULL_TREE, comp);
   10504            2 :               gfc_add_block_to_block (&tmpblock, &se.pre);
   10505              :             }
   10506              : 
   10507            2 :           if (attr->dimension || c->ts.type == BT_CHARACTER)
   10508            0 :             gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
   10509              :           else
   10510            2 :             cdesc = comp;
   10511              : 
   10512            2 :           tree fndecl;
   10513              : 
   10514            2 :           fndecl = build_call_expr_loc (input_location,
   10515              :                                         gfor_fndecl_co_broadcast, 5,
   10516              :                                         gfc_build_addr_expr (pvoid_type_node,cdesc),
   10517              :                                         args->image_index,
   10518              :                                         null_pointer_node, null_pointer_node,
   10519              :                                         null_pointer_node);
   10520              : 
   10521            2 :           gfc_add_expr_to_block (&tmpblock, fndecl);
   10522            2 :           gfc_add_block_to_block (&fnblock, &tmpblock);
   10523              : 
   10524        39790 :           break;
   10525              : 
   10526        18836 :         case DEALLOCATE_ALLOC_COMP:
   10527              : 
   10528        18836 :           gfc_init_block (&tmpblock);
   10529              : 
   10530        18836 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10531              :                                   decl, cdecl, NULL_TREE);
   10532              : 
   10533              :           /* Shortcut to get the attributes of the component.  */
   10534        18836 :           if (c->ts.type == BT_CLASS)
   10535              :             {
   10536         1443 :               attr = &CLASS_DATA (c)->attr;
   10537         1443 :               if (attr->class_pointer || c->attr.proc_pointer)
   10538           24 :                 continue;
   10539              :             }
   10540              :           else
   10541              :             {
   10542        17393 :               attr = &c->attr;
   10543        17393 :               if (attr->pointer || attr->proc_pointer)
   10544          174 :                 continue;
   10545              :             }
   10546              : 
   10547        18638 :           if (!no_finalization && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
   10548        11508 :              || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer)))
   10549              :             /* Call the finalizer, which will free the memory and nullify the
   10550              :                pointer of an array.  */
   10551         4892 :             deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
   10552              :                                                          caf_enabled (caf_mode))
   10553         4892 :                 && attr->dimension;
   10554              :           else
   10555              :             deallocate_called = false;
   10556              : 
   10557              :           /* Add the _class ref for classes.  */
   10558        18638 :           if (c->ts.type == BT_CLASS && attr->allocatable)
   10559         1419 :             comp = gfc_class_data_get (comp);
   10560              : 
   10561        18638 :           add_when_allocated = NULL_TREE;
   10562        18638 :           if (cmp_has_alloc_comps
   10563         4136 :               && !c->attr.pointer && !c->attr.proc_pointer
   10564              :               && !same_type
   10565         4136 :               && !deallocate_called)
   10566              :             {
   10567              :               /* Add checked deallocation of the components.  This code is
   10568              :                  obviously added because the finalizer is not trusted to free
   10569              :                  all memory.  */
   10570         2579 :               if (c->ts.type == BT_CLASS)
   10571              :                 {
   10572          332 :                   rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
   10573          332 :                   add_when_allocated
   10574          332 :                       = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
   10575              :                                                comp, NULL_TREE, rank, purpose,
   10576              :                                                caf_mode, args, no_finalization);
   10577              :                 }
   10578              :               else
   10579              :                 {
   10580         2247 :                   rank = c->as ? c->as->rank : 0;
   10581         2247 :                   add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10582              :                                                               comp, NULL_TREE,
   10583              :                                                               rank, purpose,
   10584              :                                                               caf_mode, args,
   10585              :                                                               no_finalization);
   10586              :                 }
   10587              :             }
   10588              : 
   10589        11653 :           if (attr->allocatable && !same_type
   10590        29082 :               && (!attr->codimension || caf_enabled (caf_mode)))
   10591              :             {
   10592              :               /* Handle all types of components besides components of the
   10593              :                  same_type as the current one, because those would create an
   10594              :                  endless loop.  */
   10595           51 :               caf_dereg_mode = (caf_in_coarray (caf_mode)
   10596           58 :                                 && (attr->dimension || c->caf_token))
   10597        10380 :                                     || attr->codimension
   10598        10515 :                                  ? (gfc_caf_is_dealloc_only (caf_mode)
   10599              :                                       ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
   10600              :                                       : GFC_CAF_COARRAY_DEREGISTER)
   10601              :                                  : GFC_CAF_COARRAY_NOCOARRAY;
   10602              : 
   10603        10437 :               caf_token = NULL_TREE;
   10604              :               /* Coarray components are handled directly by
   10605              :                  deallocate_with_status.  */
   10606        10437 :               if (!attr->codimension
   10607        10416 :                   && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
   10608              :                 {
   10609           57 :                   if (c->caf_token)
   10610           19 :                     caf_token
   10611           19 :                       = fold_build3_loc (input_location, COMPONENT_REF,
   10612           19 :                                          TREE_TYPE (gfc_comp_caf_token (c)),
   10613              :                                          decl, gfc_comp_caf_token (c),
   10614              :                                          NULL_TREE);
   10615           38 :                   else if (attr->dimension && !attr->proc_pointer)
   10616           38 :                     caf_token = gfc_conv_descriptor_token (comp);
   10617              :                 }
   10618              : 
   10619        10437 :               tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
   10620              :                                                 NULL_TREE, NULL_TREE, true,
   10621              :                                                 NULL, caf_dereg_mode, NULL_TREE,
   10622              :                                                 add_when_allocated, caf_token);
   10623              : 
   10624        10437 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10625              :             }
   10626         8201 :           else if (attr->allocatable && !attr->codimension
   10627         1209 :                    && !deallocate_called)
   10628              :             {
   10629              :               /* Case of recursive allocatable derived types.  */
   10630         1209 :               tree is_allocated;
   10631         1209 :               tree ubound;
   10632         1209 :               tree cdesc;
   10633         1209 :               stmtblock_t dealloc_block;
   10634              : 
   10635         1209 :               gfc_init_block (&dealloc_block);
   10636         1209 :               if (add_when_allocated)
   10637            0 :                 gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
   10638              : 
   10639              :               /* Convert the component into a rank 1 descriptor type.  */
   10640         1209 :               if (attr->dimension)
   10641              :                 {
   10642          327 :                   tmp = gfc_get_element_type (TREE_TYPE (comp));
   10643          327 :                   ubound = gfc_full_array_size (&dealloc_block, comp,
   10644          327 :                                                 c->ts.type == BT_CLASS
   10645            0 :                                                 ? CLASS_DATA (c)->as->rank
   10646          327 :                                                 : c->as->rank);
   10647              :                 }
   10648              :               else
   10649              :                 {
   10650          882 :                   tmp = TREE_TYPE (comp);
   10651          882 :                   ubound = build_int_cst (gfc_array_index_type, 1);
   10652              :                 }
   10653              : 
   10654         1209 :               cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
   10655              :                                                  &ubound, 1,
   10656              :                                                  GFC_ARRAY_ALLOCATABLE, false);
   10657              : 
   10658         1209 :               cdesc = gfc_create_var (cdesc, "cdesc");
   10659         1209 :               DECL_ARTIFICIAL (cdesc) = 1;
   10660              : 
   10661         1209 :               gfc_conv_descriptor_dtype_set (&dealloc_block, cdesc,
   10662              :                                              gfc_get_dtype_rank_type (1, tmp));
   10663         1209 :               gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
   10664              :                                               gfc_index_zero_node,
   10665              :                                               gfc_index_one_node);
   10666         1209 :               gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
   10667              :                                               gfc_index_zero_node,
   10668              :                                               gfc_index_one_node);
   10669         1209 :               gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
   10670              :                                               gfc_index_zero_node, ubound);
   10671              : 
   10672         1209 :               if (attr->dimension)
   10673          327 :                 comp = gfc_conv_descriptor_data_get (comp);
   10674              : 
   10675         1209 :               gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
   10676              : 
   10677              :               /* Now call the deallocator.  */
   10678         1209 :               vtab = gfc_find_vtab (&c->ts);
   10679         1209 :               if (vtab->backend_decl == NULL)
   10680           53 :                 gfc_get_symbol_decl (vtab);
   10681         1209 :               tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
   10682         1209 :               dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
   10683         1209 :               dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
   10684              :                                                             dealloc_fndecl);
   10685         1209 :               tmp = build_int_cst (TREE_TYPE (comp), 0);
   10686         1209 :               is_allocated = fold_build2_loc (input_location, NE_EXPR,
   10687              :                                               logical_type_node, tmp,
   10688              :                                               comp);
   10689         1209 :               cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
   10690              : 
   10691         1209 :               tmp = build_call_expr_loc (input_location,
   10692              :                                          dealloc_fndecl, 1,
   10693              :                                          cdesc);
   10694         1209 :               gfc_add_expr_to_block (&dealloc_block, tmp);
   10695              : 
   10696         1209 :               tmp = gfc_finish_block (&dealloc_block);
   10697              : 
   10698         1209 :               tmp = fold_build3_loc (input_location, COND_EXPR,
   10699              :                                      void_type_node, is_allocated, tmp,
   10700              :                                      build_empty_stmt (input_location));
   10701              : 
   10702         1209 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10703         1209 :             }
   10704         6992 :           else if (add_when_allocated)
   10705         1397 :             gfc_add_expr_to_block (&tmpblock, add_when_allocated);
   10706              : 
   10707         1419 :           if (c->ts.type == BT_CLASS && attr->allocatable
   10708        20057 :               && (!attr->codimension || !caf_enabled (caf_mode)))
   10709              :             {
   10710              :               /* Finally, reset the vptr to the declared type vtable and, if
   10711              :                  necessary reset the _len field.
   10712              : 
   10713              :                  First recover the reference to the component and obtain
   10714              :                  the vptr.  */
   10715         1404 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10716              :                                       decl, cdecl, NULL_TREE);
   10717         1404 :               tmp = gfc_class_vptr_get (comp);
   10718              : 
   10719         1404 :               if (UNLIMITED_POLY (c))
   10720              :                 {
   10721              :                   /* Both vptr and _len field should be nulled.  */
   10722          240 :                   gfc_add_modify (&tmpblock, tmp,
   10723          240 :                                   build_int_cst (TREE_TYPE (tmp), 0));
   10724          240 :                   tmp = gfc_class_len_get (comp);
   10725          240 :                   gfc_add_modify (&tmpblock, tmp,
   10726          240 :                                   build_int_cst (TREE_TYPE (tmp), 0));
   10727              :                 }
   10728              :               else
   10729              :                 {
   10730              :                   /* Build the vtable address and set the vptr with it.  */
   10731         1164 :                   gfc_reset_vptr (&tmpblock, nullptr, tmp, c->ts.u.derived);
   10732              :                 }
   10733              :             }
   10734              : 
   10735              :           /* Now add the deallocation of this component.  */
   10736        18638 :           gfc_add_block_to_block (&fnblock, &tmpblock);
   10737        18638 :           break;
   10738              : 
   10739         6673 :         case NULLIFY_ALLOC_COMP:
   10740              :           /* Nullify
   10741              :              - allocatable components (regular or in class)
   10742              :              - components that have allocatable components
   10743              :              - pointer components when in a coarray.
   10744              :              Skip everything else especially proc_pointers, which may come
   10745              :              coupled with the regular pointer attribute.  */
   10746         9048 :           if (c->attr.proc_pointer
   10747         6673 :               || !(c->attr.allocatable || (c->ts.type == BT_CLASS
   10748          560 :                                            && CLASS_DATA (c)->attr.allocatable)
   10749         3031 :                    || (cmp_has_alloc_comps
   10750          573 :                        && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
   10751           18 :                            || (c->ts.type == BT_CLASS
   10752           12 :                                && !CLASS_DATA (c)->attr.class_pointer)))
   10753         2476 :                    || (caf_in_coarray (caf_mode) && c->attr.pointer)))
   10754         2375 :             continue;
   10755              : 
   10756              :           /* Process class components first, because they always have the
   10757              :              pointer-attribute set which would be caught wrong else.  */
   10758         4298 :           if (c->ts.type == BT_CLASS
   10759          541 :               && (CLASS_DATA (c)->attr.allocatable
   10760            0 :                   || CLASS_DATA (c)->attr.class_pointer))
   10761              :             {
   10762          541 :               tree class_ref;
   10763              : 
   10764              :               /* Allocatable CLASS components.  */
   10765          541 :               class_ref = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10766              :                                            decl, cdecl, NULL_TREE);
   10767              : 
   10768          541 :               comp = gfc_class_data_get (class_ref);
   10769          541 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10770          317 :                 gfc_conv_descriptor_data_set (&fnblock, comp,
   10771              :                                               null_pointer_node);
   10772              :               else
   10773              :                 {
   10774          224 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10775              :                                          void_type_node, comp,
   10776          224 :                                          build_int_cst (TREE_TYPE (comp), 0));
   10777          224 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10778              :                 }
   10779              : 
   10780              :               /* The dynamic type of a disassociated pointer or unallocated
   10781              :                  allocatable variable is its declared type. An unlimited
   10782              :                  polymorphic entity has no declared type.  */
   10783          541 :               gfc_reset_vptr (&fnblock, nullptr, class_ref, c->ts.u.derived);
   10784              : 
   10785          541 :               cmp_has_alloc_comps = false;
   10786          541 :             }
   10787              :           /* Coarrays need the component to be nulled before the api-call
   10788              :              is made.  */
   10789         3757 :           else if (c->attr.pointer || c->attr.allocatable)
   10790              :             {
   10791         3202 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10792              :                                       decl, cdecl, NULL_TREE);
   10793         3202 :               if (c->attr.dimension || c->attr.codimension)
   10794         2287 :                 gfc_conv_descriptor_data_set (&fnblock, comp,
   10795              :                                               null_pointer_node);
   10796              :               else
   10797          915 :                 gfc_add_modify (&fnblock, comp,
   10798          915 :                                 build_int_cst (TREE_TYPE (comp), 0));
   10799         3202 :               if (!c->attr.pdt_string && gfc_deferred_strlen (c, &comp))
   10800              :                 {
   10801          323 :                   comp = fold_build3_loc (input_location, COMPONENT_REF,
   10802          323 :                                           TREE_TYPE (comp),
   10803              :                                           decl, comp, NULL_TREE);
   10804          646 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10805          323 :                                          TREE_TYPE (comp), comp,
   10806          323 :                                          build_int_cst (TREE_TYPE (comp), 0));
   10807          323 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10808              :                 }
   10809              :               cmp_has_alloc_comps = false;
   10810              :             }
   10811              : 
   10812         4298 :           if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
   10813              :             {
   10814              :               /* Register a component of a derived type coarray with the
   10815              :                  coarray library.  Do not register ultimate component
   10816              :                  coarrays here.  They are treated like regular coarrays and
   10817              :                  are either allocated on all images or on none.  */
   10818          132 :               tree token;
   10819              : 
   10820          132 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10821              :                                       decl, cdecl, NULL_TREE);
   10822          132 :               if (c->attr.dimension)
   10823              :                 {
   10824              :                   /* Set the dtype, because caf_register needs it.  */
   10825          104 :                   tree dtype_val = gfc_get_dtype (TREE_TYPE (comp));
   10826          104 :                   gfc_conv_descriptor_dtype_set (&fnblock, comp, dtype_val);
   10827          104 :                   tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10828              :                                          decl, cdecl, NULL_TREE);
   10829          104 :                   token = gfc_conv_descriptor_token (tmp);
   10830              :                 }
   10831              :               else
   10832              :                 {
   10833           28 :                   gfc_se se;
   10834              : 
   10835           28 :                   gfc_init_se (&se, NULL);
   10836           56 :                   token = fold_build3_loc (input_location, COMPONENT_REF,
   10837              :                                            pvoid_type_node, decl,
   10838           28 :                                            gfc_comp_caf_token (c), NULL_TREE);
   10839           28 :                   comp = gfc_conv_scalar_to_descriptor (&se, comp,
   10840           28 :                                                         c->ts.type == BT_CLASS
   10841           28 :                                                         ? CLASS_DATA (c)->attr
   10842              :                                                         : c->attr);
   10843           28 :                   gfc_add_block_to_block (&fnblock, &se.pre);
   10844              :                 }
   10845              : 
   10846          132 :               gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
   10847              :                                           gfc_build_addr_expr (NULL_TREE,
   10848              :                                                                token),
   10849              :                                           NULL_TREE, NULL_TREE, NULL_TREE,
   10850              :                                           GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
   10851              :             }
   10852              : 
   10853         4298 :           if (cmp_has_alloc_comps)
   10854              :             {
   10855          555 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10856              :                                       decl, cdecl, NULL_TREE);
   10857          555 :               rank = c->as ? c->as->rank : 0;
   10858          555 :               tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
   10859              :                                            rank, purpose, caf_mode, args,
   10860              :                                            no_finalization);
   10861          555 :               gfc_add_expr_to_block (&fnblock, tmp);
   10862              :             }
   10863              :           break;
   10864              : 
   10865           30 :         case REASSIGN_CAF_COMP:
   10866           30 :           if (caf_enabled (caf_mode)
   10867           30 :               && (c->attr.codimension
   10868           23 :                   || (c->ts.type == BT_CLASS
   10869            2 :                       && (CLASS_DATA (c)->attr.coarray_comp
   10870            2 :                           || caf_in_coarray (caf_mode)))
   10871           21 :                   || (c->ts.type == BT_DERIVED
   10872            7 :                       && (c->ts.u.derived->attr.coarray_comp
   10873            6 :                           || caf_in_coarray (caf_mode))))
   10874           46 :               && !same_type)
   10875              :             {
   10876           14 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10877              :                                       decl, cdecl, NULL_TREE);
   10878           14 :               dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10879              :                                       dest, cdecl, NULL_TREE);
   10880              : 
   10881           14 :               if (c->attr.codimension)
   10882              :                 {
   10883            7 :                   if (c->ts.type == BT_CLASS)
   10884              :                     {
   10885            0 :                       comp = gfc_class_data_get (comp);
   10886            0 :                       dcmp = gfc_class_data_get (dcmp);
   10887              :                     }
   10888            7 :                   gfc_conv_descriptor_data_set (&fnblock, dcmp,
   10889              :                                            gfc_conv_descriptor_data_get (comp));
   10890              :                 }
   10891              :               else
   10892              :                 {
   10893            7 :                   tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
   10894              :                                                rank, purpose, caf_mode
   10895              :                                                | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
   10896              :                                                args, no_finalization);
   10897            7 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10898              :                 }
   10899              :             }
   10900              :           break;
   10901              : 
   10902        14431 :         case COPY_ALLOC_COMP:
   10903        14431 :           if (c->attr.pointer || c->attr.proc_pointer)
   10904          165 :             continue;
   10905              : 
   10906              :           /* We need source and destination components.  */
   10907        14266 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
   10908              :                                   cdecl, NULL_TREE);
   10909        14266 :           dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
   10910              :                                   cdecl, NULL_TREE);
   10911        14266 :           dcmp = fold_convert (TREE_TYPE (comp), dcmp);
   10912              : 
   10913        14266 :           if (IS_PDT (c) && !c->attr.allocatable)
   10914              :             {
   10915          153 :               tmp = gfc_copy_alloc_comp (c->ts.u.derived, comp, dcmp,
   10916              :                                          0, 0);
   10917          153 :               gfc_add_expr_to_block (&fnblock, tmp);
   10918          153 :               continue;
   10919              :             }
   10920              : 
   10921        14113 :           if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
   10922              :             {
   10923          860 :               tree ftn_tree;
   10924          860 :               tree size;
   10925          860 :               tree dst_data;
   10926          860 :               tree src_data;
   10927          860 :               tree null_data;
   10928              : 
   10929          860 :               dst_data = gfc_class_data_get (dcmp);
   10930          860 :               src_data = gfc_class_data_get (comp);
   10931          860 :               size = fold_convert (size_type_node,
   10932              :                                    gfc_class_vtab_size_get (comp));
   10933              : 
   10934          860 :               if (CLASS_DATA (c)->attr.dimension)
   10935              :                 {
   10936          864 :                   nelems = gfc_conv_descriptor_size (src_data,
   10937          432 :                                                      CLASS_DATA (c)->as->rank);
   10938          432 :                   size = fold_build2_loc (input_location, MULT_EXPR,
   10939              :                                           size_type_node, size,
   10940              :                                           fold_convert (size_type_node,
   10941              :                                                         nelems));
   10942              :                 }
   10943              :               else
   10944          428 :                 nelems = build_int_cst (size_type_node, 1);
   10945              : 
   10946          860 :               if (CLASS_DATA (c)->attr.dimension
   10947          428 :                   || CLASS_DATA (c)->attr.codimension)
   10948              :                 {
   10949          440 :                   src_data = gfc_conv_descriptor_data_get (src_data);
   10950          440 :                   dst_data = gfc_conv_descriptor_data_get (dst_data);
   10951              :                 }
   10952              : 
   10953          860 :               gfc_init_block (&tmpblock);
   10954              : 
   10955          860 :               gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
   10956              :                               gfc_class_vptr_get (comp));
   10957              : 
   10958              :               /* Copy the unlimited '_len' field. If it is greater than zero
   10959              :                  (ie. a character(_len)), multiply it by size and use this
   10960              :                  for the malloc call.  */
   10961          860 :               if (UNLIMITED_POLY (c))
   10962              :                 {
   10963          158 :                   gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
   10964              :                                   gfc_class_len_get (comp));
   10965          158 :                   size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
   10966              :                 }
   10967              : 
   10968              :               /* Coarray component have to have the same allocation status and
   10969              :                  shape/type-parameter/effective-type on the LHS and RHS of an
   10970              :                  intrinsic assignment. Hence, we did not deallocated them - and
   10971              :                  do not allocate them here.  */
   10972          860 :               if (!CLASS_DATA (c)->attr.codimension)
   10973              :                 {
   10974          845 :                   ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
   10975          845 :                   tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
   10976          845 :                   gfc_add_modify (&tmpblock, dst_data,
   10977          845 :                                   fold_convert (TREE_TYPE (dst_data), tmp));
   10978              :                 }
   10979              : 
   10980         1705 :               tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
   10981          860 :                                              UNLIMITED_POLY (c));
   10982          860 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10983          860 :               tmp = gfc_finish_block (&tmpblock);
   10984              : 
   10985          860 :               gfc_init_block (&tmpblock);
   10986          860 :               gfc_add_modify (&tmpblock, dst_data,
   10987          860 :                               fold_convert (TREE_TYPE (dst_data),
   10988              :                                             null_pointer_node));
   10989          860 :               null_data = gfc_finish_block (&tmpblock);
   10990              : 
   10991          860 :               null_cond = fold_build2_loc (input_location, NE_EXPR,
   10992              :                                            logical_type_node, src_data,
   10993              :                                            null_pointer_node);
   10994              : 
   10995          860 :               gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
   10996              :                                                          tmp, null_data));
   10997          860 :               continue;
   10998          860 :             }
   10999              : 
   11000              :           /* To implement guarded deep copy, i.e., deep copy only allocatable
   11001              :              components that are really allocated, the deep copy code has to
   11002              :              be generated first and then added to the if-block in
   11003              :              gfc_duplicate_allocatable ().  */
   11004        13253 :           if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
   11005              :             {
   11006         1948 :               rank = c->as ? c->as->rank : 0;
   11007         1948 :               tmp = fold_convert (TREE_TYPE (dcmp), comp);
   11008         1948 :               gfc_add_modify (&fnblock, dcmp, tmp);
   11009         1948 :               add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   11010              :                                                           comp, dcmp,
   11011              :                                                           rank, purpose,
   11012              :                                                           caf_mode, args,
   11013              :                                                           no_finalization);
   11014              :             }
   11015              :           else
   11016              :             add_when_allocated = NULL_TREE;
   11017              : 
   11018        13253 :           if (gfc_deferred_strlen (c, &tmp))
   11019              :             {
   11020          454 :               tree len, size;
   11021          454 :               len = tmp;
   11022          454 :               tmp = fold_build3_loc (input_location, COMPONENT_REF,
   11023          454 :                                      TREE_TYPE (len),
   11024              :                                      decl, len, NULL_TREE);
   11025          454 :               len = fold_build3_loc (input_location, COMPONENT_REF,
   11026          454 :                                      TREE_TYPE (len),
   11027              :                                      dest, len, NULL_TREE);
   11028          454 :               tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   11029          454 :                                      TREE_TYPE (len), len, tmp);
   11030          454 :               gfc_add_expr_to_block (&fnblock, tmp);
   11031          454 :               size = size_of_string_in_bytes (c->ts.kind, len);
   11032              :               /* This component cannot have allocatable components,
   11033              :                  therefore add_when_allocated of duplicate_allocatable ()
   11034              :                  is always NULL.  */
   11035          454 :               rank = c->as ? c->as->rank : 0;
   11036          454 :               tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
   11037              :                                            false, false, size, NULL_TREE);
   11038          454 :               gfc_add_expr_to_block (&fnblock, tmp);
   11039              :             }
   11040        12799 :           else if (c->attr.pdt_array
   11041          453 :                    && !c->attr.allocatable && !c->attr.pointer)
   11042              :             {
   11043          453 :               tmp = duplicate_allocatable (dcmp, comp, ctype,
   11044          453 :                                            c->as ? c->as->rank : 0,
   11045              :                                            false, false, NULL_TREE, NULL_TREE);
   11046          453 :               gfc_add_expr_to_block (&fnblock, tmp);
   11047              :             }
   11048              :       /* Special case: recursive allocatable array components require
   11049              :          runtime helpers to avoid compile-time infinite recursion.  Generate
   11050              :          a call to _gfortran_cfi_deep_copy_array with an element copy
   11051              :          wrapper.  When inside a wrapper, reuse current_function_decl.  */
   11052         7112 :       else if (c->attr.allocatable && cmp_has_alloc_comps && same_type
   11053         1324 :                && purpose == COPY_ALLOC_COMP && !c->attr.proc_pointer
   11054         1324 :                && !c->attr.codimension && !caf_in_coarray (caf_mode)
   11055        13670 :                && c->ts.type == BT_DERIVED && c->ts.u.derived != NULL)
   11056              :             {
   11057         1324 :               tree copy_wrapper, call, dest_addr, src_addr, elem_type;
   11058         1324 :               tree helper_ptr_type;
   11059         1324 :               tree alloc_expr;
   11060         1324 :               int comp_rank;
   11061              : 
   11062              :               /* Get the element type from ctype (already the component
   11063              :                  type).  For arrays we need the element type, not the array
   11064              :                  type.  */
   11065         1324 :               elem_type = ctype;
   11066         1324 :               if (GFC_DESCRIPTOR_TYPE_P (ctype))
   11067          930 :                 elem_type = gfc_get_element_type (ctype);
   11068          394 :               else if (TREE_CODE (ctype) == ARRAY_TYPE)
   11069            0 :                 elem_type = TREE_TYPE (ctype);
   11070          394 :               else if (!c->as)
   11071          394 :                 elem_type = TREE_TYPE (TREE_TYPE (comp));
   11072              : 
   11073         1324 :               helper_ptr_type = get_copy_helper_pointer_type ();
   11074              : 
   11075         1324 :               comp_rank = c->as ? c->as->rank : 0;
   11076         1324 :               alloc_expr = gfc_duplicate_allocatable_nocopy (dcmp, comp, ctype,
   11077              :                                                              comp_rank);
   11078         1324 :               gfc_add_expr_to_block (&fnblock, alloc_expr);
   11079              : 
   11080              :               /* Generate or reuse the element copy helper.  Inside an
   11081              :                  existing helper we can reuse the current function to
   11082              :                  prevent recursive generation.  */
   11083         1324 :               if (inside_wrapper)
   11084          924 :                 copy_wrapper
   11085          924 :                   = gfc_build_addr_expr (NULL_TREE, current_function_decl);
   11086              :               else
   11087          400 :                 copy_wrapper
   11088          400 :                   = generate_element_copy_wrapper (c->ts.u.derived, elem_type,
   11089              :                                                    purpose, caf_mode);
   11090         1324 :               copy_wrapper = fold_convert (helper_ptr_type, copy_wrapper);
   11091              : 
   11092         1324 :               if (c->as)
   11093              :                 {
   11094              :                   /* Build addresses of descriptors.  */
   11095          930 :                   dest_addr = gfc_build_addr_expr (pvoid_type_node, dcmp);
   11096          930 :                   src_addr = gfc_build_addr_expr (pvoid_type_node, comp);
   11097              :                 }
   11098              :               else
   11099              :                 {
   11100              :                   /* For scalars, create separate descriptors for source and
   11101              :                      dest, then pass their addresses.  */
   11102          394 :                   gfc_se se;
   11103          394 :                   gfc_init_se (&se, NULL);
   11104          394 :                   tmp = gfc_conv_scalar_to_descriptor (&se, dcmp, c->attr);
   11105          394 :                   dest_addr = gfc_build_addr_expr (pvoid_type_node, tmp);
   11106          394 :                   tmp = gfc_conv_scalar_to_descriptor (&se, comp, c->attr);
   11107          394 :                   src_addr = gfc_build_addr_expr (pvoid_type_node, tmp);
   11108          394 :                   gfc_add_block_to_block (&fnblock, &se.pre);
   11109              :                 }
   11110              : 
   11111              :               /* Build call: _gfortran_cfi_deep_copy_array (&dcmp, &comp, wrapper).  */
   11112         1324 :               call = build_call_expr_loc (input_location,
   11113              :                                           gfor_fndecl_cfi_deep_copy_array, 3,
   11114              :                                           dest_addr, src_addr,
   11115              :                                           copy_wrapper);
   11116              : 
   11117         1324 :               gfc_add_expr_to_block (&fnblock, call);
   11118              :             }
   11119              :           /* For allocatable arrays with nested allocatable components,
   11120              :              add_when_allocated already includes gfc_duplicate_allocatable
   11121              :              (from the recursive structure_alloc_comps call at line 10290-10293),
   11122              :              so we must not call it again here.  PR121628 added an
   11123              :              add_when_allocated != NULL clause that was redundant for scalars
   11124              :              (already handled by !c->as) and wrong for arrays (double alloc).  */
   11125         5788 :           else if (c->attr.allocatable && !c->attr.proc_pointer
   11126        16810 :                    && (!cmp_has_alloc_comps
   11127          776 :                        || !c->as
   11128          662 :                        || c->attr.codimension
   11129          659 :                        || caf_in_coarray (caf_mode)))
   11130              :             {
   11131         5135 :               rank = c->as ? c->as->rank : 0;
   11132         5135 :               if (c->attr.codimension)
   11133           20 :                 tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
   11134         5115 :               else if (flag_coarray == GFC_FCOARRAY_LIB
   11135         5115 :                        && caf_in_coarray (caf_mode))
   11136              :                 {
   11137           64 :                   tree dst_tok;
   11138           64 :                   if (c->as)
   11139           46 :                     dst_tok = gfc_conv_descriptor_token (dcmp);
   11140              :                   else
   11141              :                     {
   11142           18 :                       dst_tok
   11143           18 :                         = fold_build3_loc (input_location, COMPONENT_REF,
   11144              :                                            pvoid_type_node, dest,
   11145           18 :                                            gfc_comp_caf_token (c), NULL_TREE);
   11146              :                     }
   11147           64 :                   tmp
   11148           64 :                     = duplicate_allocatable_coarray (dcmp, dst_tok, comp, ctype,
   11149              :                                                      rank, add_when_allocated);
   11150              :                 }
   11151              :               else
   11152         5051 :                 tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
   11153              :                                                  add_when_allocated);
   11154         5135 :               gfc_add_expr_to_block (&fnblock, tmp);
   11155              :             }
   11156              :           else
   11157         5887 :             if (cmp_has_alloc_comps || is_pdt_type)
   11158         1981 :               gfc_add_expr_to_block (&fnblock, add_when_allocated);
   11159              : 
   11160              :           break;
   11161              : 
   11162         4103 :         case ALLOCATE_PDT_COMP:
   11163              : 
   11164         4103 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11165              :                                   decl, cdecl, NULL_TREE);
   11166              : 
   11167              :           /* Set the PDT KIND and LEN fields.  */
   11168         4103 :           if (c->attr.pdt_kind || c->attr.pdt_len)
   11169              :             {
   11170         1735 :               gfc_se tse;
   11171         1735 :               gfc_expr *c_expr = NULL;
   11172         1735 :               gfc_actual_arglist *param = pdt_param_list;
   11173         1735 :               gfc_init_se (&tse, NULL);
   11174         6405 :               for (; param; param = param->next)
   11175         2935 :                 if (param->name && !strcmp (c->name, param->name))
   11176         1735 :                   c_expr = param->expr;
   11177              : 
   11178         1735 :               if (!c_expr)
   11179           18 :                 c_expr = c->initializer;
   11180              : 
   11181           18 :               if (c_expr)
   11182              :                 {
   11183         1717 :                   gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11184         1717 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11185         1717 :                   gfc_add_modify (&fnblock, comp, tse.expr);
   11186         1717 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11187              :                 }
   11188         1735 :             }
   11189         2368 :           else if (c->initializer && !c->attr.pdt_string && !c->attr.pdt_array
   11190          348 :                    && !c->as && !IS_PDT (c))   /* Take care of arrays.  */
   11191              :             {
   11192          139 :               gfc_se tse;
   11193          139 :               gfc_expr *c_expr;
   11194          139 :               gfc_init_se (&tse, NULL);
   11195          139 :               c_expr = c->initializer;
   11196          139 :               gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11197          139 :               gfc_add_block_to_block (&fnblock, &tse.pre);
   11198          139 :               gfc_add_modify (&fnblock, comp, tse.expr);
   11199          139 :               gfc_add_block_to_block (&fnblock, &tse.post);
   11200              :             }
   11201              : 
   11202         4103 :           if (c->attr.pdt_string)
   11203              :             {
   11204          246 :               gfc_se tse;
   11205          246 :               gfc_init_se (&tse, NULL);
   11206          246 :               gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
   11207              :               /* Convert the parameterized string length to its value. The
   11208              :                  string length is stored in a hidden field in the same way as
   11209              :                  deferred string lengths.  */
   11210          246 :               gfc_insert_parameter_exprs (e, pdt_param_list);
   11211          246 :               if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
   11212              :                 {
   11213          246 :                   gfc_conv_expr_type (&tse, e,
   11214          246 :                                       TREE_TYPE (strlen));
   11215          246 :                   strlen = fold_build3_loc (input_location, COMPONENT_REF,
   11216          246 :                                             TREE_TYPE (strlen),
   11217              :                                             decl, strlen, NULL_TREE);
   11218          246 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11219          246 :                   gfc_add_modify (&fnblock, strlen, tse.expr);
   11220          246 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11221          246 :                   c->ts.u.cl->backend_decl = strlen;
   11222              :                 }
   11223          246 :               gfc_free_expr (e);
   11224              : 
   11225              :               /* Scalar parameterized strings can be allocated now.  */
   11226          246 :               if (!c->as)
   11227              :                 {
   11228          150 :                   tmp = fold_convert (gfc_array_index_type, strlen);
   11229          150 :                   tmp = size_of_string_in_bytes (c->ts.kind, tmp);
   11230          150 :                   tmp = gfc_evaluate_now (tmp, &fnblock);
   11231          150 :                   tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
   11232          150 :                   gfc_add_modify (&fnblock, comp, tmp);
   11233              :                 }
   11234              :             }
   11235              : 
   11236              :           /* Allocate parameterized arrays of parameterized derived types.  */
   11237         4103 :           if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
   11238         3216 :               && !(IS_PDT (c) || IS_CLASS_PDT (c)))
   11239         2901 :             continue;
   11240              : 
   11241         1202 :           if (c->ts.type == BT_CLASS)
   11242            0 :             comp = gfc_class_data_get (comp);
   11243              : 
   11244         1202 :           if (c->attr.pdt_array
   11245         1202 :               || (c->attr.pdt_string
   11246            0 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp))))
   11247              :             {
   11248          887 :               gfc_se tse;
   11249          887 :               int i;
   11250          887 :               tree size = gfc_index_one_node;
   11251          887 :               tree offset = gfc_index_zero_node;
   11252          887 :               tree lower, upper;
   11253          887 :               gfc_expr *e;
   11254              : 
   11255              :               /* This chunk takes the expressions for 'lower' and 'upper'
   11256              :                  in the arrayspec and substitutes in the expressions for
   11257              :                  the parameters from 'pdt_param_list'. The descriptor
   11258              :                  fields can then be filled from the values so obtained.  */
   11259          887 :               gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
   11260         1906 :               for (i = 0; i < c->as->rank; i++)
   11261              :                 {
   11262         1019 :                   gfc_init_se (&tse, NULL);
   11263         1019 :                   e = gfc_copy_expr (c->as->lower[i]);
   11264         1019 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11265         1019 :                   gfc_conv_expr_type (&tse, e, gfc_array_index_type);
   11266         1019 :                   gfc_free_expr (e);
   11267         1019 :                   lower = tse.expr;
   11268         1019 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11269         1019 :                   gfc_conv_descriptor_lbound_set (&fnblock, comp,
   11270              :                                                   gfc_rank_cst[i],
   11271              :                                                   lower);
   11272         1019 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11273         1019 :                   e = gfc_copy_expr (c->as->upper[i]);
   11274         1019 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11275         1019 :                   gfc_conv_expr_type (&tse, e, gfc_array_index_type);
   11276         1019 :                   gfc_free_expr (e);
   11277         1019 :                   upper = tse.expr;
   11278         1019 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11279         1019 :                   gfc_conv_descriptor_ubound_set (&fnblock, comp,
   11280              :                                                   gfc_rank_cst[i],
   11281              :                                                   upper);
   11282         1019 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11283         1019 :                   gfc_conv_descriptor_stride_set (&fnblock, comp,
   11284              :                                                   gfc_rank_cst[i],
   11285              :                                                   size);
   11286         1019 :                   size = gfc_evaluate_now (size, &fnblock);
   11287         1019 :                   tmp = fold_build2_loc (input_location,
   11288              :                                          MULT_EXPR,
   11289              :                                          gfc_array_index_type,
   11290              :                                          lower, size);
   11291         1019 :                   offset = fold_build2_loc (input_location,
   11292              :                                             MINUS_EXPR,
   11293              :                                             gfc_array_index_type,
   11294              :                                             offset, tmp);
   11295         1019 :                   offset = gfc_evaluate_now (offset, &fnblock);
   11296         1019 :                   tmp = fold_build2_loc (input_location, MINUS_EXPR,
   11297              :                                          gfc_array_index_type,
   11298              :                                          upper, lower);
   11299         1019 :                   tmp = fold_build2_loc (input_location, PLUS_EXPR,
   11300              :                                          gfc_array_index_type,
   11301              :                                          tmp, gfc_index_one_node);
   11302         1019 :                   size = fold_build2_loc (input_location, MULT_EXPR,
   11303              :                                           gfc_array_index_type, size, tmp);
   11304              :                 }
   11305          887 :               gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
   11306          887 :               if (c->ts.type == BT_CLASS)
   11307              :                 {
   11308            0 :                   tmp = gfc_get_vptr_from_expr (comp);
   11309            0 :                   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
   11310            0 :                     tmp = build_fold_indirect_ref_loc (input_location, tmp);
   11311            0 :                   tmp = gfc_vptr_size_get (tmp);
   11312              :                 }
   11313          887 :               else if (strlen != NULL_TREE)
   11314           90 :                 tmp = strlen;
   11315              :               else
   11316          797 :                 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
   11317          887 :               tmp = fold_convert (gfc_array_index_type, tmp);
   11318          887 :               size = fold_build2_loc (input_location, MULT_EXPR,
   11319              :                                       gfc_array_index_type, size, tmp);
   11320          887 :               size = gfc_evaluate_now (size, &fnblock);
   11321          887 :               tmp = gfc_call_malloc (&fnblock, NULL, size);
   11322          887 :               gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
   11323          887 :               gfc_conv_descriptor_dtype_set (&fnblock, comp,
   11324              :                                              gfc_get_dtype (ctype));
   11325          887 :               if (strlen != NULL_TREE)
   11326              :                 {
   11327           90 :                   tmp = gfc_conv_descriptor_elem_len_get (comp);
   11328           90 :                   gfc_add_modify (&fnblock, tmp, fold_convert (TREE_TYPE (tmp), strlen));
   11329           90 :                   gfc_conv_descriptor_span_set (&fnblock, comp, strlen);
   11330              :                 }
   11331              : 
   11332          887 :               if (c->initializer && c->initializer->rank)
   11333              :                 {
   11334            0 :                   gfc_init_se (&tse, NULL);
   11335            0 :                   e = gfc_copy_expr (c->initializer);
   11336            0 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11337            0 :                   gfc_conv_expr_descriptor (&tse, e);
   11338            0 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11339            0 :                   gfc_free_expr (e);
   11340            0 :                   tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
   11341            0 :                   tmp = build_call_expr_loc (input_location, tmp, 3,
   11342              :                                      gfc_conv_descriptor_data_get (comp),
   11343              :                                      gfc_conv_descriptor_data_get (tse.expr),
   11344              :                                      fold_convert (size_type_node, size));
   11345            0 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11346            0 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11347              :                 }
   11348              :             }
   11349              : 
   11350              :           /* Recurse in to PDT components.  */
   11351         1202 :           if ((IS_PDT (c) || IS_CLASS_PDT (c))
   11352          329 :                && !(c->attr.pointer || c->attr.allocatable))
   11353              :             {
   11354          215 :               gfc_actual_arglist *tail = c->param_list, *parms;
   11355          215 :               bool ext;
   11356              : 
   11357          215 :               ext = gfc_get_derived_super_type (der_type) == c->ts.u.derived;
   11358              : 
   11359          538 :               for (; tail; tail = tail->next)
   11360          347 :                 if (tail->spec_type != SPEC_EXPLICIT && tail->expr)
   11361              :                   break;
   11362              : 
   11363          215 :               if (tail && ext)
   11364              :                 /* 'der_type' extends the type of this component but has no
   11365              :                    type parameters of its own and so some of the components
   11366              :                    might be assumed.  The pdt_param_list will have what is
   11367              :                    needed.  */
   11368           24 :                 parms = pdt_param_list;
   11369              :               else
   11370              :                 /* Otherwise, use the component's type parameters.  */
   11371              :                 {
   11372          191 :                   tail = parms = c->param_list;
   11373          502 :                   for (; tail; tail = tail->next)
   11374          311 :                     if (tail->expr)
   11375          287 :                       gfc_insert_parameter_exprs (tail->expr, pdt_param_list);
   11376              :                 }
   11377              : 
   11378          215 :               tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
   11379          215 :                                            c->as ? c->as->rank : 0,
   11380              :                                            parms);
   11381          215 :               gfc_add_expr_to_block (&fnblock, tmp);
   11382              :             }
   11383              : 
   11384              :           break;
   11385              : 
   11386         6266 :         case DEALLOCATE_PDT_COMP:
   11387              :           /* Deallocate array or parameterized string length components
   11388              :              of parameterized derived types.  */
   11389         6266 :           if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
   11390         4860 :               && !c->attr.pdt_string
   11391         4680 :               && !(IS_PDT (c) || IS_CLASS_PDT (c)))
   11392         4129 :             continue;
   11393              : 
   11394         2137 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11395              :                                   decl, cdecl, NULL_TREE);
   11396         2137 :           if (c->ts.type == BT_CLASS)
   11397            0 :             comp = gfc_class_data_get (comp);
   11398              : 
   11399              :           /* Recurse in to PDT components.  */
   11400         2137 :           if ((IS_PDT (c) || IS_CLASS_PDT (c))
   11401          589 :               && (!c->attr.pointer && !c->attr.allocatable))
   11402              :             {
   11403          380 :               tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
   11404          380 :                                              c->as ? c->as->rank : 0);
   11405          380 :               gfc_add_expr_to_block (&fnblock, tmp);
   11406              :             }
   11407              : 
   11408         2137 :           if (c->attr.pdt_array || c->attr.pdt_string)
   11409              :             {
   11410         1586 :               tmp = comp;
   11411         1586 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   11412         1412 :                 tmp = gfc_conv_descriptor_data_get (comp);
   11413         1586 :               null_cond = fold_build2_loc (input_location, NE_EXPR,
   11414              :                                            logical_type_node, tmp,
   11415         1586 :                                            build_int_cst (TREE_TYPE (tmp), 0));
   11416         1586 :               if (flag_openmp_allocators)
   11417              :                 {
   11418            0 :                   tree cd, t;
   11419            0 :                   if (c->attr.pdt_array)
   11420              :                     {
   11421            0 :                       tree version_val = gfc_conv_descriptor_version_get (comp);
   11422            0 :                       cd = fold_build2_loc (input_location, EQ_EXPR,
   11423              :                                             boolean_type_node, version_val,
   11424              :                                             integer_one_node);
   11425              :                     }
   11426              :                   else
   11427            0 :                     cd = gfc_omp_call_is_alloc (tmp);
   11428            0 :                   t = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
   11429            0 :                   t = build_call_expr_loc (input_location, t, 1, tmp);
   11430              : 
   11431            0 :                   stmtblock_t tblock;
   11432            0 :                   gfc_init_block (&tblock);
   11433            0 :                   gfc_add_expr_to_block (&tblock, t);
   11434            0 :                   if (c->attr.pdt_array)
   11435            0 :                     gfc_conv_descriptor_version_set (&tblock, comp,
   11436              :                                                      integer_zero_node);
   11437            0 :                   tmp = build3_loc (input_location, COND_EXPR, void_type_node,
   11438              :                                     cd, gfc_finish_block (&tblock),
   11439              :                                     gfc_call_free (tmp));
   11440              :                 }
   11441              :               else
   11442         1586 :                 tmp = gfc_call_free (tmp);
   11443         1586 :               tmp = build3_v (COND_EXPR, null_cond, tmp,
   11444              :                               build_empty_stmt (input_location));
   11445         1586 :               gfc_add_expr_to_block (&fnblock, tmp);
   11446              : 
   11447         1586 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   11448         1412 :                 gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
   11449              :               else
   11450              :                 {
   11451          174 :                   tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
   11452          174 :                   gfc_add_modify (&fnblock, comp, tmp);
   11453              :                 }
   11454              :             }
   11455              : 
   11456              :           break;
   11457              : 
   11458          680 :         case CHECK_PDT_DUMMY:
   11459          680 :         case CHECK_PDT_INTENT_OUT_DUMMY:
   11460              : 
   11461          680 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11462              :                                   decl, cdecl, NULL_TREE);
   11463          680 :           if (c->ts.type == BT_CLASS)
   11464            0 :             comp = gfc_class_data_get (comp);
   11465              : 
   11466              :           /* Recurse in to PDT components.  */
   11467          680 :           if (((c->ts.type == BT_DERIVED
   11468           48 :                 && !c->attr.allocatable && !c->attr.pointer)
   11469          632 :                || (c->ts.type == BT_CLASS
   11470            0 :                    && !CLASS_DATA (c)->attr.allocatable
   11471            0 :                    && !CLASS_DATA (c)->attr.pointer))
   11472           48 :               && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
   11473              :             {
   11474           18 :               tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
   11475           18 :                                          c->as ? c->as->rank : 0,
   11476              :                                          pdt_param_list, intent_out);
   11477           18 :               gfc_add_expr_to_block (&fnblock, tmp);
   11478              :             }
   11479              : 
   11480          680 :           if (!c->attr.pdt_len)
   11481              :             {
   11482          548 :               gfc_se tse;
   11483         1000 :               if (!intent_out
   11484          180 :                   || c->attr.codimension
   11485          180 :                   || c->attr.pointer
   11486          180 :                   || IS_PDT (c) || IS_CLASS_PDT (c)
   11487          174 :                   || c->attr.pdt_comp || c->attr.pdt_kind
   11488          174 :                   || !c->initializer
   11489          644 :                   || !gfc_is_constant_expr (c->initializer)) // Just in case!
   11490          452 :                 continue;
   11491              : 
   11492              :               /* F2023 19.6.5(24) "Invocation of a procedure that has a
   11493              :                  nonpointer, nonallocatable INTENT (OUT) dummy argument causes
   11494              :                  all nonpointer default-initialized subcomponents of the dummy
   11495              :                  argument to become defined."  */
   11496           96 :               if (c->initializer->expr_type == EXPR_CONSTANT)
   11497              :                 {
   11498           36 :                   gfc_init_se (&tse, NULL);
   11499           36 :                   gfc_conv_expr (&tse, c->initializer);
   11500           36 :                   gfc_add_modify (&fnblock, comp, tse.expr);
   11501              :                 }
   11502           60 :               else if (c->initializer->expr_type == EXPR_STRUCTURE)
   11503              :                 {
   11504           30 :                   tmp = gfc_trans_structure_assign (comp, c->initializer,
   11505              :                                                     true, false);
   11506           30 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11507              :                 }
   11508           30 :               else if (c->initializer->expr_type == EXPR_ARRAY)
   11509              :                 {
   11510           30 :                   tree type = TREE_TYPE (TREE_TYPE (comp));
   11511           30 :                   tree len = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
   11512           30 :                   len = fold_build2_loc (input_location, MULT_EXPR,
   11513           30 :                                          TREE_TYPE (len), len,
   11514           30 :                                          TYPE_SIZE_UNIT (type));
   11515              : 
   11516           30 :                   tmp = gfc_build_constant_array_constructor (c->initializer, type);
   11517           30 :                   tmp = gfc_build_memcpy_call (gfc_conv_array_data (comp), tmp, len);
   11518           30 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11519              :                 }
   11520              :               else
   11521            0 :                 continue;
   11522              :             }
   11523              :           else
   11524              :             {
   11525          132 :               gfc_se tse;
   11526          132 :               gfc_expr *c_expr = NULL;
   11527          132 :               gfc_actual_arglist *param = pdt_param_list;
   11528          132 :               gfc_init_se (&tse, NULL);
   11529          456 :               for (; param; param = param->next)
   11530          192 :                 if (!strcmp (c->name, param->name)
   11531          132 :                     && param->spec_type == SPEC_EXPLICIT)
   11532           90 :                   c_expr = param->expr;
   11533              : 
   11534          132 :               if (c_expr)
   11535              :                 {
   11536           90 :                   tree error, cond, cname;
   11537           90 :                   gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11538           90 :                   cond = fold_build2_loc (input_location, NE_EXPR,
   11539              :                                           logical_type_node,
   11540              :                                           comp, tse.expr);
   11541           90 :                   cname = gfc_build_cstring_const (c->name);
   11542           90 :                   cname = gfc_build_addr_expr (pchar_type_node, cname);
   11543           90 :                   error = gfc_trans_runtime_error (true, NULL,
   11544              :                                                    "The value of the PDT LEN "
   11545              :                                                    "parameter '%s' does not "
   11546              :                                                    "agree with that in the "
   11547              :                                                    "dummy declaration",
   11548              :                                                    cname);
   11549           90 :                   tmp = fold_build3_loc (input_location, COND_EXPR,
   11550              :                                          void_type_node, cond, error,
   11551              :                                          build_empty_stmt (input_location));
   11552           90 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11553              :                 }
   11554              :             }
   11555              :           break;
   11556              : 
   11557            0 :         default:
   11558            0 :           gcc_unreachable ();
   11559        11234 :           break;
   11560              :         }
   11561              :     }
   11562        25760 :   seen_derived_types.remove (der_type);
   11563              : 
   11564        25760 :   return gfc_finish_block (&fnblock);
   11565              : }
   11566              : 
   11567              : /* Recursively traverse an object of derived type, generating code to
   11568              :    nullify allocatable components.  */
   11569              : 
   11570              : tree
   11571         3317 : gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
   11572              :                         int caf_mode)
   11573              : {
   11574         3317 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11575              :                                 NULLIFY_ALLOC_COMP,
   11576              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
   11577         3317 :                                 NULL);
   11578              : }
   11579              : 
   11580              : 
   11581              : /* Recursively traverse an object of derived type, generating code to
   11582              :    deallocate allocatable components.  */
   11583              : 
   11584              : tree
   11585         3924 : gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
   11586              :                            int caf_mode, bool no_finalization)
   11587              : {
   11588         3924 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11589              :                                 DEALLOCATE_ALLOC_COMP,
   11590              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
   11591         3924 :                                 NULL, no_finalization);
   11592              : }
   11593              : 
   11594              : tree
   11595            1 : gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
   11596              :                       tree image_index, tree stat, tree errmsg,
   11597              :                       tree errmsg_len)
   11598              : {
   11599            1 :   tree tmp, array;
   11600            1 :   gfc_se argse;
   11601            1 :   stmtblock_t block, post_block;
   11602            1 :   gfc_co_subroutines_args args;
   11603              : 
   11604            1 :   args.image_index = image_index;
   11605            1 :   args.stat = stat;
   11606            1 :   args.errmsg = errmsg;
   11607            1 :   args.errmsg_len = errmsg_len;
   11608              : 
   11609            1 :   if (rank == 0)
   11610              :     {
   11611            1 :       gfc_start_block (&block);
   11612            1 :       gfc_init_block (&post_block);
   11613            1 :       gfc_init_se (&argse, NULL);
   11614            1 :       gfc_conv_expr (&argse, expr);
   11615            1 :       gfc_add_block_to_block (&block, &argse.pre);
   11616            1 :       gfc_add_block_to_block (&post_block, &argse.post);
   11617            1 :       array = argse.expr;
   11618              :     }
   11619              :   else
   11620              :     {
   11621            0 :       gfc_init_se (&argse, NULL);
   11622            0 :       argse.want_pointer = 1;
   11623            0 :       gfc_conv_expr_descriptor (&argse, expr);
   11624            0 :       array = argse.expr;
   11625              :     }
   11626              : 
   11627            1 :   tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
   11628              :                                BCAST_ALLOC_COMP,
   11629              :                                GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
   11630              :                                &args);
   11631            1 :   return tmp;
   11632              : }
   11633              : 
   11634              : /* Recursively traverse an object of derived type, generating code to
   11635              :    deallocate allocatable components.  But do not deallocate coarrays.
   11636              :    To be used for intrinsic assignment, which may not change the allocation
   11637              :    status of coarrays.  */
   11638              : 
   11639              : tree
   11640         3741 : gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank,
   11641              :                                   bool no_finalization)
   11642              : {
   11643         3741 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11644              :                                 DEALLOCATE_ALLOC_COMP, 0, NULL,
   11645         3741 :                                 no_finalization);
   11646              : }
   11647              : 
   11648              : 
   11649              : tree
   11650            5 : gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
   11651              : {
   11652            5 :   return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
   11653              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
   11654            5 :                                 NULL);
   11655              : }
   11656              : 
   11657              : 
   11658              : /* Recursively traverse an object of derived type, generating code to
   11659              :    copy it and its allocatable components.  */
   11660              : 
   11661              : tree
   11662         5238 : gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
   11663              :                      int caf_mode)
   11664              : {
   11665         5238 :   return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
   11666         5238 :                                 caf_mode, NULL);
   11667              : }
   11668              : 
   11669              : 
   11670              : /* Recursively traverse an object of derived type, generating code to
   11671              :    copy it and its allocatable components, while suppressing any
   11672              :    finalization that might occur.  This is used in the finalization of
   11673              :    function results.  */
   11674              : 
   11675              : tree
   11676           38 : gfc_copy_alloc_comp_no_fini (gfc_symbol * der_type, tree decl, tree dest,
   11677              :                              int rank, int caf_mode)
   11678              : {
   11679           38 :   return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
   11680           38 :                                 caf_mode, NULL, true);
   11681              : }
   11682              : 
   11683              : 
   11684              : /* Recursively traverse an object of derived type, generating code to
   11685              :    copy only its allocatable components.  */
   11686              : 
   11687              : tree
   11688            0 : gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
   11689              : {
   11690            0 :   return structure_alloc_comps (der_type, decl, dest, rank,
   11691            0 :                                 COPY_ONLY_ALLOC_COMP, 0, NULL);
   11692              : }
   11693              : 
   11694              : 
   11695              : /* Recursively traverse an object of parameterized derived type, generating
   11696              :    code to allocate parameterized components.  */
   11697              : 
   11698              : tree
   11699         1377 : gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
   11700              :                        gfc_actual_arglist *param_list)
   11701              : {
   11702         1377 :   tree res;
   11703         1377 :   gfc_actual_arglist *old_param_list = pdt_param_list;
   11704         1377 :   pdt_param_list = param_list;
   11705         1377 :   res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11706              :                                ALLOCATE_PDT_COMP, 0, NULL);
   11707         1377 :   pdt_param_list = old_param_list;
   11708         1377 :   return res;
   11709              : }
   11710              : 
   11711              : /* Recursively traverse an object of parameterized derived type, generating
   11712              :    code to deallocate parameterized components.  */
   11713              : 
   11714              : tree
   11715         2188 : gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
   11716              : {
   11717              :   /* A type without parameterized components causes gimplifier problems.  */
   11718         2188 :   if (!has_parameterized_comps (der_type))
   11719              :     return NULL_TREE;
   11720              : 
   11721         1321 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11722         1321 :                                 DEALLOCATE_PDT_COMP, 0, NULL);
   11723              : }
   11724              : 
   11725              : 
   11726              : /* Recursively traverse a dummy of parameterized derived type to check the
   11727              :    values of LEN parameters.  */
   11728              : 
   11729              : tree
   11730          163 : gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
   11731              :                      gfc_actual_arglist *param_list, bool intent_out)
   11732              : {
   11733          163 :   tree res;
   11734          163 :   gfc_actual_arglist *old_param_list = pdt_param_list;
   11735          163 :   pdt_param_list = param_list;
   11736          272 :   res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11737              :                                intent_out ? CHECK_PDT_INTENT_OUT_DUMMY :
   11738              :                                             CHECK_PDT_DUMMY,
   11739              :                                0, NULL);
   11740          163 :   pdt_param_list = old_param_list;
   11741          163 :   return res;
   11742              : }
   11743              : 
   11744              : 
   11745              : /* Returns the value of LBOUND for an expression.  This could be broken out
   11746              :    from gfc_conv_intrinsic_bound but this seemed to be simpler.  This is
   11747              :    called by gfc_alloc_allocatable_for_assignment.  */
   11748              : static tree
   11749         1110 : get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
   11750              : {
   11751         1110 :   tree lbound;
   11752         1110 :   tree ubound;
   11753         1110 :   tree stride;
   11754         1110 :   tree cond, cond1, cond3, cond4;
   11755         1110 :   tree tmp;
   11756         1110 :   gfc_ref *ref;
   11757              : 
   11758         1110 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   11759              :     {
   11760          521 :       tmp = gfc_rank_cst[dim];
   11761          521 :       lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
   11762          521 :       ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
   11763          521 :       stride = gfc_conv_descriptor_stride_get (desc, tmp);
   11764          521 :       cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11765              :                                ubound, lbound);
   11766          521 :       cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11767              :                                stride, gfc_index_zero_node);
   11768          521 :       cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   11769              :                                logical_type_node, cond3, cond1);
   11770          521 :       cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   11771              :                                stride, gfc_index_zero_node);
   11772          521 :       if (assumed_size)
   11773            0 :         cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11774              :                                 tmp, build_int_cst (gfc_array_index_type,
   11775            0 :                                                     expr->rank - 1));
   11776              :       else
   11777          521 :         cond = logical_false_node;
   11778              : 
   11779          521 :       cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11780              :                                logical_type_node, cond3, cond4);
   11781          521 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11782              :                               logical_type_node, cond, cond1);
   11783              : 
   11784          521 :       return fold_build3_loc (input_location, COND_EXPR,
   11785              :                               gfc_array_index_type, cond,
   11786          521 :                               lbound, gfc_index_one_node);
   11787              :     }
   11788              : 
   11789          589 :   if (expr->expr_type == EXPR_FUNCTION)
   11790              :     {
   11791              :       /* A conversion function, so use the argument.  */
   11792            7 :       gcc_assert (expr->value.function.isym
   11793              :                   && expr->value.function.isym->conversion);
   11794            7 :       expr = expr->value.function.actual->expr;
   11795              :     }
   11796              : 
   11797          589 :   if (expr->expr_type == EXPR_VARIABLE)
   11798              :     {
   11799          589 :       tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
   11800         1522 :       for (ref = expr->ref; ref; ref = ref->next)
   11801              :         {
   11802          933 :           if (ref->type == REF_COMPONENT
   11803          295 :                 && ref->u.c.component->as
   11804          246 :                 && ref->next
   11805          246 :                 && ref->next->u.ar.type == AR_FULL)
   11806          204 :             tmp = TREE_TYPE (ref->u.c.component->backend_decl);
   11807              :         }
   11808          589 :       return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
   11809              :     }
   11810              : 
   11811            0 :   return gfc_index_one_node;
   11812              : }
   11813              : 
   11814              : 
   11815              : /* Returns true if an expression represents an lhs that can be reallocated
   11816              :    on assignment.  */
   11817              : 
   11818              : bool
   11819       689474 : gfc_is_reallocatable_lhs (gfc_expr *expr)
   11820              : {
   11821       689474 :   gfc_ref * ref;
   11822       689474 :   gfc_symbol *sym;
   11823              : 
   11824       689474 :   if (!flag_realloc_lhs)
   11825              :     return false;
   11826              : 
   11827       688358 :   if (!expr->ref)
   11828              :     return false;
   11829              : 
   11830       256190 :   sym = expr->symtree->n.sym;
   11831              : 
   11832       256190 :   if (sym->attr.associate_var && !expr->ref)
   11833              :     return false;
   11834              : 
   11835              :   /* An allocatable class variable with no reference.  */
   11836       256190 :   if (sym->ts.type == BT_CLASS
   11837         7165 :       && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
   11838         6990 :       && CLASS_DATA (sym)->attr.allocatable
   11839              :       && expr->ref
   11840         4344 :       && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
   11841          724 :            && expr->ref->next == NULL)
   11842         3705 :           || (expr->ref->type == REF_COMPONENT
   11843         3292 :               && strcmp (expr->ref->u.c.component->name, "_data") == 0
   11844         2321 :               && (expr->ref->next == NULL
   11845         2321 :                   || (expr->ref->next->type == REF_ARRAY
   11846         2321 :                       && expr->ref->next->u.ar.type == AR_FULL
   11847         1785 :                       && expr->ref->next->next == NULL)))))
   11848              :     return true;
   11849              : 
   11850              :   /* An allocatable variable.  */
   11851       253930 :   if (sym->attr.allocatable
   11852        54224 :       && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
   11853              :       && expr->ref
   11854        54224 :       && expr->ref->type == REF_ARRAY
   11855        52603 :       && expr->ref->u.ar.type == AR_FULL)
   11856              :     return true;
   11857              : 
   11858              :   /* All that can be left are allocatable components.  */
   11859       220479 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
   11860              :     return false;
   11861              : 
   11862              :   /* Find a component ref followed by an array reference.  */
   11863        98689 :   for (ref = expr->ref; ref; ref = ref->next)
   11864        69174 :     if (ref->next
   11865        39659 :           && ref->type == REF_COMPONENT
   11866        23111 :           && ref->next->type == REF_ARRAY
   11867        19076 :           && !ref->next->next)
   11868              :       break;
   11869              : 
   11870        43968 :   if (!ref)
   11871              :     return false;
   11872              : 
   11873              :   /* Return true if valid reallocatable lhs.  */
   11874        14453 :   if (ref->u.c.component->attr.allocatable
   11875         6928 :         && ref->next->u.ar.type == AR_FULL)
   11876         5242 :     return true;
   11877              : 
   11878              :   return false;
   11879              : }
   11880              : 
   11881              : 
   11882              : static tree
   11883           56 : concat_str_length (gfc_expr* expr)
   11884              : {
   11885           56 :   tree type;
   11886           56 :   tree len1;
   11887           56 :   tree len2;
   11888           56 :   gfc_se se;
   11889              : 
   11890           56 :   type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
   11891           56 :   len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   11892           56 :   if (len1 == NULL_TREE)
   11893              :     {
   11894           56 :       if (expr->value.op.op1->expr_type == EXPR_OP)
   11895           31 :         len1 = concat_str_length (expr->value.op.op1);
   11896           25 :       else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
   11897           25 :         len1 = build_int_cst (gfc_charlen_type_node,
   11898           25 :                         expr->value.op.op1->value.character.length);
   11899            0 :       else if (expr->value.op.op1->ts.u.cl->length)
   11900              :         {
   11901            0 :           gfc_init_se (&se, NULL);
   11902            0 :           gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
   11903            0 :           len1 = se.expr;
   11904              :         }
   11905              :       else
   11906              :         {
   11907              :           /* Last resort!  */
   11908            0 :           gfc_init_se (&se, NULL);
   11909            0 :           se.want_pointer = 1;
   11910            0 :           se.descriptor_only = 1;
   11911            0 :           gfc_conv_expr (&se, expr->value.op.op1);
   11912            0 :           len1 = se.string_length;
   11913              :         }
   11914              :     }
   11915              : 
   11916           56 :   type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
   11917           56 :   len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   11918           56 :   if (len2 == NULL_TREE)
   11919              :     {
   11920           31 :       if (expr->value.op.op2->expr_type == EXPR_OP)
   11921            0 :         len2 = concat_str_length (expr->value.op.op2);
   11922           31 :       else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
   11923           25 :         len2 = build_int_cst (gfc_charlen_type_node,
   11924           25 :                         expr->value.op.op2->value.character.length);
   11925            6 :       else if (expr->value.op.op2->ts.u.cl->length)
   11926              :         {
   11927            6 :           gfc_init_se (&se, NULL);
   11928            6 :           gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
   11929            6 :           len2 = se.expr;
   11930              :         }
   11931              :       else
   11932              :         {
   11933              :           /* Last resort!  */
   11934            0 :           gfc_init_se (&se, NULL);
   11935            0 :           se.want_pointer = 1;
   11936            0 :           se.descriptor_only = 1;
   11937            0 :           gfc_conv_expr (&se, expr->value.op.op2);
   11938            0 :           len2 = se.string_length;
   11939              :         }
   11940              :     }
   11941              : 
   11942           56 :   gcc_assert(len1 && len2);
   11943           56 :   len1 = fold_convert (gfc_charlen_type_node, len1);
   11944           56 :   len2 = fold_convert (gfc_charlen_type_node, len2);
   11945              : 
   11946           56 :   return fold_build2_loc (input_location, PLUS_EXPR,
   11947           56 :                           gfc_charlen_type_node, len1, len2);
   11948              : }
   11949              : 
   11950              : 
   11951              : /* Among the scalarization chain of LOOP, find the element associated with an
   11952              :    allocatable array on the lhs of an assignment and evaluate its fields
   11953              :    (bounds, offset, etc) to new variables, putting the new code in BLOCK.  This
   11954              :    function is to be called after putting the reallocation code in BLOCK and
   11955              :    before the beginning of the scalarization loop body.
   11956              : 
   11957              :    The fields to be saved are expected to hold on entry to the function
   11958              :    expressions referencing the array descriptor.  Especially the expressions
   11959              :    shouldn't be already temporary variable references as the value saved before
   11960              :    reallocation would be incorrect after reallocation.
   11961              :    At the end of the function, the expressions have been replaced with variable
   11962              :    references.  */
   11963              : 
   11964              : static void
   11965         6900 : update_reallocated_descriptor (stmtblock_t *block, gfc_loopinfo *loop)
   11966              : {
   11967        23969 :   for (gfc_ss *s = loop->ss; s != gfc_ss_terminator; s = s->loop_chain)
   11968              :     {
   11969        17069 :       if (!s->is_alloc_lhs)
   11970        10169 :         continue;
   11971              : 
   11972         6900 :       gcc_assert (s->info->type == GFC_SS_SECTION);
   11973         6900 :       gfc_array_info *info = &s->info->data.array;
   11974              : 
   11975              : #define SAVE_VALUE(value) \
   11976              :               do \
   11977              :                 { \
   11978              :                   value = gfc_evaluate_now (value, block); \
   11979              :                 } \
   11980              :               while (0)
   11981              : 
   11982         6900 :       if (save_descriptor_data (info->descriptor, info->data))
   11983         6054 :         SAVE_VALUE (info->data);
   11984         6900 :       SAVE_VALUE (info->offset);
   11985         6900 :       info->saved_offset = info->offset;
   11986        17036 :       for (int i = 0; i < s->dimen; i++)
   11987              :         {
   11988        10136 :           int dim = s->dim[i];
   11989        10136 :           SAVE_VALUE (info->start[dim]);
   11990        10136 :           SAVE_VALUE (info->end[dim]);
   11991        10136 :           SAVE_VALUE (info->stride[dim]);
   11992        10136 :           SAVE_VALUE (info->delta[dim]);
   11993              :         }
   11994              : 
   11995              : #undef SAVE_VALUE
   11996              :     }
   11997         6900 : }
   11998              : 
   11999              : 
   12000              : /* Allocate the lhs of an assignment to an allocatable array, otherwise
   12001              :    reallocate it.  */
   12002              : 
   12003              : tree
   12004         6900 : gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
   12005              :                                       gfc_expr *expr1,
   12006              :                                       gfc_expr *expr2)
   12007              : {
   12008         6900 :   stmtblock_t realloc_block;
   12009         6900 :   stmtblock_t alloc_block;
   12010         6900 :   stmtblock_t fblock;
   12011         6900 :   stmtblock_t loop_pre_block;
   12012         6900 :   gfc_ref *ref;
   12013         6900 :   gfc_ss *rss;
   12014         6900 :   gfc_ss *lss;
   12015         6900 :   gfc_array_info *linfo;
   12016         6900 :   tree realloc_expr;
   12017         6900 :   tree alloc_expr;
   12018         6900 :   tree size1;
   12019         6900 :   tree size2;
   12020         6900 :   tree elemsize1;
   12021         6900 :   tree elemsize2;
   12022         6900 :   tree array1;
   12023         6900 :   tree cond_null;
   12024         6900 :   tree cond;
   12025         6900 :   tree tmp;
   12026         6900 :   tree tmp2;
   12027         6900 :   tree lbound;
   12028         6900 :   tree ubound;
   12029         6900 :   tree desc;
   12030         6900 :   tree old_desc;
   12031         6900 :   tree desc2;
   12032         6900 :   tree offset;
   12033         6900 :   tree jump_label1;
   12034         6900 :   tree jump_label2;
   12035         6900 :   tree lbd;
   12036         6900 :   tree class_expr2 = NULL_TREE;
   12037         6900 :   int n;
   12038         6900 :   gfc_array_spec * as;
   12039         6900 :   bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
   12040         6900 :                   && gfc_caf_attr (expr1, true).codimension);
   12041         6900 :   tree token;
   12042         6900 :   gfc_se caf_se;
   12043              : 
   12044              :   /* x = f(...) with x allocatable.  In this case, expr1 is the rhs.
   12045              :      Find the lhs expression in the loop chain and set expr1 and
   12046              :      expr2 accordingly.  */
   12047         6900 :   if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
   12048              :     {
   12049          203 :       expr2 = expr1;
   12050              :       /* Find the ss for the lhs.  */
   12051          203 :       lss = loop->ss;
   12052          406 :       for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
   12053          406 :         if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
   12054              :           break;
   12055          203 :       if (lss == gfc_ss_terminator)
   12056              :         return NULL_TREE;
   12057          203 :       expr1 = lss->info->expr;
   12058              :     }
   12059              : 
   12060              :   /* Bail out if this is not a valid allocate on assignment.  */
   12061         6900 :   if (!gfc_is_reallocatable_lhs (expr1)
   12062         6900 :         || (expr2 && !expr2->rank))
   12063              :     return NULL_TREE;
   12064              : 
   12065              :   /* Find the ss for the lhs.  */
   12066         6900 :   lss = loop->ss;
   12067        17069 :   for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
   12068        17069 :     if (lss->info->expr == expr1)
   12069              :       break;
   12070              : 
   12071         6900 :   if (lss == gfc_ss_terminator)
   12072              :     return NULL_TREE;
   12073              : 
   12074         6900 :   linfo = &lss->info->data.array;
   12075              : 
   12076              :   /* Find an ss for the rhs. For operator expressions, we see the
   12077              :      ss's for the operands. Any one of these will do.  */
   12078         6900 :   rss = loop->ss;
   12079         7505 :   for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
   12080         7505 :     if (rss->info->expr != expr1 && rss != loop->temp_ss)
   12081              :       break;
   12082              : 
   12083         6900 :   if (expr2 && rss == gfc_ss_terminator)
   12084              :     return NULL_TREE;
   12085              : 
   12086              :   /* Ensure that the string length from the current scope is used.  */
   12087         6900 :   if (expr2->ts.type == BT_CHARACTER
   12088         1037 :       && expr2->expr_type == EXPR_FUNCTION
   12089          154 :       && !expr2->value.function.isym)
   12090           21 :     expr2->ts.u.cl->backend_decl = rss->info->string_length;
   12091              : 
   12092              :   /* Since the lhs is allocatable, this must be a descriptor type.
   12093              :      Get the data and array size.  */
   12094         6900 :   desc = linfo->descriptor;
   12095         6900 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
   12096         6900 :   array1 = gfc_conv_descriptor_data_get (desc);
   12097              : 
   12098              :   /* If the data is null, set the descriptor bounds and offset.  This suppresses
   12099              :      the maybe used uninitialized warning.  Note that the always false variable
   12100              :      prevents this block from ever being executed, and makes sure that the
   12101              :      optimizers are able to remove it.  Component references are not subject to
   12102              :      the warnings, so we don't uselessly complicate the generated code for them.
   12103              :      */
   12104        12247 :   for (ref = expr1->ref; ref; ref = ref->next)
   12105         7137 :     if (ref->type == REF_COMPONENT)
   12106              :       break;
   12107              : 
   12108         6900 :   if (!ref)
   12109              :     {
   12110         5110 :       stmtblock_t unalloc_init_block;
   12111         5110 :       gfc_init_block (&unalloc_init_block);
   12112         5110 :       tree guard = gfc_create_var (logical_type_node, "unallocated_init_guard");
   12113         5110 :       gfc_add_modify (&unalloc_init_block, guard, logical_false_node);
   12114              : 
   12115         5110 :       gfc_start_block (&loop_pre_block);
   12116        18209 :       for (n = 0; n < expr1->rank; n++)
   12117              :         {
   12118         7989 :           gfc_conv_descriptor_lbound_set (&loop_pre_block, desc,
   12119              :                                           gfc_rank_cst[n],
   12120              :                                           gfc_index_one_node);
   12121         7989 :           gfc_conv_descriptor_ubound_set (&loop_pre_block, desc,
   12122              :                                           gfc_rank_cst[n],
   12123              :                                           gfc_index_zero_node);
   12124         7989 :           gfc_conv_descriptor_stride_set (&loop_pre_block, desc,
   12125              :                                           gfc_rank_cst[n],
   12126              :                                           gfc_index_zero_node);
   12127              :         }
   12128              : 
   12129         5110 :       gfc_conv_descriptor_offset_set (&loop_pre_block, desc,
   12130              :                                       gfc_index_zero_node);
   12131              : 
   12132         5110 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
   12133              :                              logical_type_node, array1,
   12134         5110 :                              build_int_cst (TREE_TYPE (array1), 0));
   12135         5110 :       tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
   12136              :                              logical_type_node, tmp, guard);
   12137         5110 :       tmp = build3_v (COND_EXPR, tmp,
   12138              :                       gfc_finish_block (&loop_pre_block),
   12139              :                       build_empty_stmt (input_location));
   12140         5110 :       gfc_prepend_expr_to_block (&loop->pre, tmp);
   12141         5110 :       gfc_prepend_expr_to_block (&loop->pre,
   12142              :                                  gfc_finish_block (&unalloc_init_block));
   12143              :     }
   12144              : 
   12145         6900 :   gfc_start_block (&fblock);
   12146              : 
   12147         6900 :   if (expr2)
   12148         6900 :     desc2 = rss->info->data.array.descriptor;
   12149              :   else
   12150              :     desc2 = NULL_TREE;
   12151              : 
   12152              :   /* Get the old lhs element size for deferred character and class expr1.  */
   12153         6900 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12154              :     {
   12155          693 :       if (expr1->ts.u.cl->backend_decl
   12156          693 :           && VAR_P (expr1->ts.u.cl->backend_decl))
   12157              :         elemsize1 = expr1->ts.u.cl->backend_decl;
   12158              :       else
   12159           70 :         elemsize1 = lss->info->string_length;
   12160          693 :       tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
   12161         1386 :       elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
   12162          693 :                                    TREE_TYPE (elemsize1), elemsize1,
   12163          693 :                                    fold_convert (TREE_TYPE (elemsize1), unit_size));
   12164              : 
   12165          693 :     }
   12166         6207 :   else if (expr1->ts.type == BT_CLASS)
   12167              :     {
   12168              :       /* Unfortunately, the lhs vptr is set too early in many cases.
   12169              :          Play it safe by using the descriptor element length.  */
   12170          675 :       tmp = gfc_conv_descriptor_elem_len_get (desc);
   12171          675 :       elemsize1 = fold_convert (gfc_array_index_type, tmp);
   12172              :     }
   12173              :   else
   12174              :     elemsize1 = NULL_TREE;
   12175         1368 :   if (elemsize1 != NULL_TREE)
   12176         1368 :     elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
   12177              : 
   12178              :   /* Get the new lhs size in bytes.  */
   12179         6900 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12180              :     {
   12181          693 :       if (expr2->ts.deferred)
   12182              :         {
   12183          183 :           if (expr2->ts.u.cl->backend_decl
   12184          183 :               && VAR_P (expr2->ts.u.cl->backend_decl))
   12185              :             tmp = expr2->ts.u.cl->backend_decl;
   12186              :           else
   12187            0 :             tmp = rss->info->string_length;
   12188              :         }
   12189              :       else
   12190              :         {
   12191          510 :           tmp = expr2->ts.u.cl->backend_decl;
   12192          510 :           if (!tmp && expr2->expr_type == EXPR_OP
   12193           25 :               && expr2->value.op.op == INTRINSIC_CONCAT)
   12194              :             {
   12195           25 :               tmp = concat_str_length (expr2);
   12196           25 :               expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
   12197              :             }
   12198           12 :           else if (!tmp && expr2->ts.u.cl->length)
   12199              :             {
   12200           12 :               gfc_se tmpse;
   12201           12 :               gfc_init_se (&tmpse, NULL);
   12202           12 :               gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
   12203              :                                   gfc_charlen_type_node);
   12204           12 :               tmp = tmpse.expr;
   12205           12 :               expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
   12206              :             }
   12207          510 :           tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
   12208              :         }
   12209              : 
   12210          693 :       if (expr1->ts.u.cl->backend_decl
   12211          693 :           && VAR_P (expr1->ts.u.cl->backend_decl))
   12212          623 :         gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
   12213              :       else
   12214           70 :         gfc_add_modify (&fblock, lss->info->string_length, tmp);
   12215              : 
   12216          693 :       if (expr1->ts.kind > 1)
   12217           12 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
   12218            6 :                                TREE_TYPE (tmp),
   12219            6 :                                tmp, build_int_cst (TREE_TYPE (tmp),
   12220            6 :                                                    expr1->ts.kind));
   12221              :     }
   12222         6207 :   else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
   12223              :     {
   12224          295 :       tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
   12225          295 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12226              :                              fold_convert (gfc_array_index_type, tmp),
   12227          295 :                              expr1->ts.u.cl->backend_decl);
   12228              :     }
   12229         5912 :   else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
   12230          164 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12231         5748 :   else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
   12232              :     {
   12233          291 :       tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
   12234          291 :       if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
   12235           54 :         tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
   12236              : 
   12237           54 :       if (tmp != NULL_TREE)
   12238          291 :         tmp = gfc_class_vtab_size_get (tmp);
   12239              :       else
   12240            0 :         tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
   12241              :     }
   12242         5457 :   else if (expr2->ts.type == BT_CLASS)
   12243              :     /* A variable that is not polymorphic has the declared type of expr2.  */
   12244           18 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
   12245              :   else
   12246         5439 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12247         6900 :   elemsize2 = fold_convert (gfc_array_index_type, tmp);
   12248         6900 :   elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
   12249              : 
   12250              :   /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
   12251              :      deallocated if expr is an array of different shape or any of the
   12252              :      corresponding length type parameter values of variable and expr
   12253              :      differ."  This assures F95 compatibility.  */
   12254         6900 :   jump_label1 = gfc_build_label_decl (NULL_TREE);
   12255         6900 :   jump_label2 = gfc_build_label_decl (NULL_TREE);
   12256              : 
   12257              :   /* Allocate if data is NULL.  */
   12258         6900 :   cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   12259         6900 :                          array1, build_int_cst (TREE_TYPE (array1), 0));
   12260         6900 :   cond_null= gfc_evaluate_now (cond_null, &fblock);
   12261              : 
   12262         6900 :   tmp = build3_v (COND_EXPR, cond_null,
   12263              :                   build1_v (GOTO_EXPR, jump_label1),
   12264              :                   build_empty_stmt (input_location));
   12265         6900 :   gfc_add_expr_to_block (&fblock, tmp);
   12266              : 
   12267              :   /* Get arrayspec if expr is a full array.  */
   12268         6900 :   if (expr2 && expr2->expr_type == EXPR_FUNCTION
   12269         2850 :         && expr2->value.function.isym
   12270         2325 :         && expr2->value.function.isym->conversion)
   12271              :     {
   12272              :       /* For conversion functions, take the arg.  */
   12273          245 :       gfc_expr *arg = expr2->value.function.actual->expr;
   12274          245 :       as = gfc_get_full_arrayspec_from_expr (arg);
   12275          245 :     }
   12276              :   else if (expr2)
   12277         6655 :     as = gfc_get_full_arrayspec_from_expr (expr2);
   12278              :   else
   12279              :     as = NULL;
   12280              : 
   12281              :   /* If the lhs shape is not the same as the rhs jump to setting the
   12282              :      bounds and doing the reallocation.......  */
   12283        17036 :   for (n = 0; n < expr1->rank; n++)
   12284              :     {
   12285              :       /* Check the shape.  */
   12286        10136 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12287        10136 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
   12288        10136 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12289              :                              gfc_array_index_type,
   12290              :                              loop->to[n], loop->from[n]);
   12291        10136 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12292              :                              gfc_array_index_type,
   12293              :                              tmp, lbound);
   12294        10136 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12295              :                              gfc_array_index_type,
   12296              :                              tmp, ubound);
   12297        10136 :       cond = fold_build2_loc (input_location, NE_EXPR,
   12298              :                               logical_type_node,
   12299              :                               tmp, gfc_index_zero_node);
   12300        10136 :       tmp = build3_v (COND_EXPR, cond,
   12301              :                       build1_v (GOTO_EXPR, jump_label1),
   12302              :                       build_empty_stmt (input_location));
   12303        10136 :       gfc_add_expr_to_block (&fblock, tmp);
   12304              :     }
   12305              : 
   12306              :   /* ...else if the element lengths are not the same also go to
   12307              :      setting the bounds and doing the reallocation.... */
   12308         6900 :   if (elemsize1 != NULL_TREE)
   12309              :     {
   12310         1368 :       cond = fold_build2_loc (input_location, NE_EXPR,
   12311              :                               logical_type_node,
   12312              :                               elemsize1, elemsize2);
   12313         1368 :       tmp = build3_v (COND_EXPR, cond,
   12314              :                       build1_v (GOTO_EXPR, jump_label1),
   12315              :                       build_empty_stmt (input_location));
   12316         1368 :       gfc_add_expr_to_block (&fblock, tmp);
   12317              :     }
   12318              : 
   12319              :   /* ....else jump past the (re)alloc code.  */
   12320         6900 :   tmp = build1_v (GOTO_EXPR, jump_label2);
   12321         6900 :   gfc_add_expr_to_block (&fblock, tmp);
   12322              : 
   12323              :   /* Add the label to start automatic (re)allocation.  */
   12324         6900 :   tmp = build1_v (LABEL_EXPR, jump_label1);
   12325         6900 :   gfc_add_expr_to_block (&fblock, tmp);
   12326              : 
   12327              :   /* Get the rhs size and fix it.  */
   12328         6900 :   size2 = gfc_index_one_node;
   12329        17036 :   for (n = 0; n < expr2->rank; n++)
   12330              :     {
   12331        10136 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12332              :                              gfc_array_index_type,
   12333              :                              loop->to[n], loop->from[n]);
   12334        10136 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12335              :                              gfc_array_index_type,
   12336              :                              tmp, gfc_index_one_node);
   12337        10136 :       size2 = fold_build2_loc (input_location, MULT_EXPR,
   12338              :                                gfc_array_index_type,
   12339              :                                tmp, size2);
   12340              :     }
   12341         6900 :   size2 = gfc_evaluate_now (size2, &fblock);
   12342              : 
   12343              :   /* Deallocation of allocatable components will have to occur on
   12344              :      reallocation.  Fix the old descriptor now.  */
   12345         6900 :   if ((expr1->ts.type == BT_DERIVED)
   12346          461 :         && expr1->ts.u.derived->attr.alloc_comp)
   12347          201 :     old_desc = gfc_evaluate_now (desc, &fblock);
   12348              :   else
   12349              :     old_desc = NULL_TREE;
   12350              : 
   12351              :   /* Now modify the lhs descriptor and the associated scalarizer
   12352              :      variables. F2003 7.4.1.3: "If variable is or becomes an
   12353              :      unallocated allocatable variable, then it is allocated with each
   12354              :      deferred type parameter equal to the corresponding type parameters
   12355              :      of expr , with the shape of expr , and with each lower bound equal
   12356              :      to the corresponding element of LBOUND(expr)."
   12357              :      Reuse size1 to keep a dimension-by-dimension track of the
   12358              :      stride of the new array.  */
   12359         6900 :   size1 = gfc_index_one_node;
   12360         6900 :   offset = gfc_index_zero_node;
   12361              : 
   12362        17036 :   for (n = 0; n < expr2->rank; n++)
   12363              :     {
   12364        10136 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12365              :                              gfc_array_index_type,
   12366              :                              loop->to[n], loop->from[n]);
   12367        10136 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12368              :                              gfc_array_index_type,
   12369              :                              tmp, gfc_index_one_node);
   12370              : 
   12371        10136 :       lbound = gfc_index_one_node;
   12372        10136 :       ubound = tmp;
   12373              : 
   12374        10136 :       if (as)
   12375              :         {
   12376         2220 :           lbd = get_std_lbound (expr2, desc2, n,
   12377         1110 :                                 as->type == AS_ASSUMED_SIZE);
   12378         1110 :           ubound = fold_build2_loc (input_location,
   12379              :                                     MINUS_EXPR,
   12380              :                                     gfc_array_index_type,
   12381              :                                     ubound, lbound);
   12382         1110 :           ubound = fold_build2_loc (input_location,
   12383              :                                     PLUS_EXPR,
   12384              :                                     gfc_array_index_type,
   12385              :                                     ubound, lbd);
   12386         1110 :           lbound = lbd;
   12387              :         }
   12388              : 
   12389        10136 :       gfc_conv_descriptor_lbound_set (&fblock, desc,
   12390              :                                       gfc_rank_cst[n],
   12391              :                                       lbound);
   12392        10136 :       gfc_conv_descriptor_ubound_set (&fblock, desc,
   12393              :                                       gfc_rank_cst[n],
   12394              :                                       ubound);
   12395        10136 :       gfc_conv_descriptor_stride_set (&fblock, desc,
   12396              :                                       gfc_rank_cst[n],
   12397              :                                       size1);
   12398        10136 :       lbound = gfc_conv_descriptor_lbound_get (desc,
   12399              :                                                gfc_rank_cst[n]);
   12400        10136 :       tmp2 = fold_build2_loc (input_location, MULT_EXPR,
   12401              :                               gfc_array_index_type,
   12402              :                               lbound, size1);
   12403        10136 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
   12404              :                                 gfc_array_index_type,
   12405              :                                 offset, tmp2);
   12406        10136 :       size1 = fold_build2_loc (input_location, MULT_EXPR,
   12407              :                                gfc_array_index_type,
   12408              :                                tmp, size1);
   12409              :     }
   12410              : 
   12411              :   /* Set the lhs descriptor and scalarizer offsets.  For rank > 1,
   12412              :      the array offset is saved and the info.offset is used for a
   12413              :      running offset.  Use the saved_offset instead.  */
   12414         6900 :   gfc_conv_descriptor_offset_set (&fblock, desc, offset);
   12415              : 
   12416              :   /* Take into account _len of unlimited polymorphic entities, so that span
   12417              :      for array descriptors and allocation sizes are computed correctly.  */
   12418         6900 :   if (UNLIMITED_POLY (expr2))
   12419              :     {
   12420          110 :       tree len = gfc_class_len_get (TREE_OPERAND (desc2, 0));
   12421          110 :       len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12422              :                              fold_convert (size_type_node, len),
   12423              :                              size_one_node);
   12424          110 :       elemsize2 = fold_build2_loc (input_location, MULT_EXPR,
   12425              :                                    gfc_array_index_type, elemsize2,
   12426              :                                    fold_convert (gfc_array_index_type, len));
   12427              :     }
   12428              : 
   12429         6900 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   12430         6900 :     gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
   12431              : 
   12432         6900 :   size2 = fold_build2_loc (input_location, MULT_EXPR,
   12433              :                            gfc_array_index_type,
   12434              :                            elemsize2, size2);
   12435         6900 :   size2 = fold_convert (size_type_node, size2);
   12436         6900 :   size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12437              :                            size2, size_one_node);
   12438         6900 :   size2 = gfc_evaluate_now (size2, &fblock);
   12439              : 
   12440              :   /* For deferred character length, the 'size' field of the dtype might
   12441              :      have changed so set the dtype.  */
   12442         6900 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
   12443         6900 :       && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12444              :     {
   12445          693 :       tree type;
   12446          693 :       if (expr2->ts.u.cl->backend_decl)
   12447          693 :         type = gfc_typenode_for_spec (&expr2->ts);
   12448              :       else
   12449            0 :         type = gfc_typenode_for_spec (&expr1->ts);
   12450              : 
   12451          693 :       gfc_conv_descriptor_dtype_set (&fblock, desc,
   12452              :                                      gfc_get_dtype_rank_type (expr1->rank,
   12453              :                                                               type));
   12454              :     }
   12455         6207 :   else if (expr1->ts.type == BT_CLASS)
   12456              :     {
   12457          675 :       tree type;
   12458              : 
   12459          675 :       if (expr2->ts.type != BT_CLASS)
   12460          384 :         type = gfc_typenode_for_spec (&expr2->ts);
   12461              :       else
   12462          291 :         type = gfc_get_character_type_len (1, elemsize2);
   12463              : 
   12464          675 :       gfc_conv_descriptor_dtype_set (&fblock, desc,
   12465              :                                      gfc_get_dtype_rank_type (expr2->rank,
   12466              :                                                               type));
   12467              : 
   12468              :       /* Set the _len field as well...  */
   12469          675 :       if (UNLIMITED_POLY (expr1))
   12470              :         {
   12471          274 :           tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
   12472          274 :           if (expr2->ts.type == BT_CHARACTER)
   12473           49 :             gfc_add_modify (&fblock, tmp,
   12474           49 :                             fold_convert (TREE_TYPE (tmp),
   12475              :                                           TYPE_SIZE_UNIT (type)));
   12476          225 :           else if (UNLIMITED_POLY (expr2))
   12477          110 :             gfc_add_modify (&fblock, tmp,
   12478          110 :                             gfc_class_len_get (TREE_OPERAND (desc2, 0)));
   12479              :           else
   12480          115 :             gfc_add_modify (&fblock, tmp,
   12481          115 :                             build_int_cst (TREE_TYPE (tmp), 0));
   12482              :         }
   12483              :       /* ...and the vptr.  */
   12484          675 :       tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
   12485          675 :       if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
   12486          291 :           && TREE_CODE (desc2) == COMPONENT_REF)
   12487              :         {
   12488          237 :           tmp2 = gfc_get_class_from_expr (desc2);
   12489          237 :           tmp2 = gfc_class_vptr_get (tmp2);
   12490              :         }
   12491          438 :       else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
   12492           54 :         tmp2 = gfc_class_vptr_get (class_expr2);
   12493              :       else
   12494              :         {
   12495          384 :           tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
   12496          384 :           tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
   12497              :         }
   12498              : 
   12499          675 :       gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
   12500              :     }
   12501         5532 :   else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   12502           39 :     gfc_conv_descriptor_dtype_set (&fblock, desc,
   12503           39 :                                    gfc_get_dtype (TREE_TYPE (desc)));
   12504              : 
   12505              :   /* Realloc expression.  Note that the scalarizer uses desc.data
   12506              :      in the array reference - (*desc.data)[<element>].  */
   12507         6900 :   gfc_init_block (&realloc_block);
   12508         6900 :   gfc_init_se (&caf_se, NULL);
   12509              : 
   12510         6900 :   if (coarray)
   12511              :     {
   12512           39 :       token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
   12513           39 :       if (token == NULL_TREE)
   12514              :         {
   12515            9 :           tmp = gfc_get_tree_for_caf_expr (expr1);
   12516            9 :           if (POINTER_TYPE_P (TREE_TYPE (tmp)))
   12517            6 :             tmp = build_fold_indirect_ref (tmp);
   12518            9 :           gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
   12519              :                                     expr1);
   12520            9 :           token = gfc_build_addr_expr (NULL_TREE, token);
   12521              :         }
   12522              : 
   12523           39 :       gfc_add_block_to_block (&realloc_block, &caf_se.pre);
   12524              :     }
   12525         6900 :   if ((expr1->ts.type == BT_DERIVED)
   12526          461 :         && expr1->ts.u.derived->attr.alloc_comp)
   12527              :     {
   12528          201 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
   12529              :                                               expr1->rank, true);
   12530          201 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12531              :     }
   12532              : 
   12533         6900 :   if (!coarray)
   12534              :     {
   12535         6861 :       tmp = build_call_expr_loc (input_location,
   12536              :                                  builtin_decl_explicit (BUILT_IN_REALLOC), 2,
   12537              :                                  fold_convert (pvoid_type_node, array1),
   12538              :                                  size2);
   12539         6861 :       if (flag_openmp_allocators)
   12540              :         {
   12541            2 :           tree cond, omp_tmp;
   12542            2 :           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
   12543              :                                   gfc_conv_descriptor_version_get (desc),
   12544              :                                   integer_one_node);
   12545            2 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
   12546            2 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
   12547              :                                  fold_convert (pvoid_type_node, array1), size2,
   12548              :                                  build_zero_cst (ptr_type_node),
   12549              :                                  build_zero_cst (ptr_type_node));
   12550            2 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
   12551              :                             omp_tmp, tmp);
   12552              :         }
   12553              : 
   12554         6861 :       gfc_conv_descriptor_data_set (&realloc_block, desc, tmp);
   12555              :     }
   12556              :   else
   12557              :     {
   12558           39 :       tmp = build_call_expr_loc (input_location,
   12559              :                                  gfor_fndecl_caf_deregister, 5, token,
   12560              :                                  build_int_cst (integer_type_node,
   12561              :                                                GFC_CAF_COARRAY_DEALLOCATE_ONLY),
   12562              :                                  null_pointer_node, null_pointer_node,
   12563              :                                  integer_zero_node);
   12564           39 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12565           39 :       tmp = build_call_expr_loc (input_location,
   12566              :                                  gfor_fndecl_caf_register,
   12567              :                                  7, size2,
   12568              :                                  build_int_cst (integer_type_node,
   12569              :                                            GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
   12570              :                                  token, gfc_build_addr_expr (NULL_TREE, desc),
   12571              :                                  null_pointer_node, null_pointer_node,
   12572              :                                  integer_zero_node);
   12573           39 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12574              :     }
   12575              : 
   12576         6900 :   if ((expr1->ts.type == BT_DERIVED)
   12577          461 :         && expr1->ts.u.derived->attr.alloc_comp)
   12578              :     {
   12579          201 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
   12580              :                                     expr1->rank);
   12581          201 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12582              :     }
   12583              : 
   12584         6900 :   gfc_add_block_to_block (&realloc_block, &caf_se.post);
   12585         6900 :   realloc_expr = gfc_finish_block (&realloc_block);
   12586              : 
   12587              :   /* Malloc expression.  */
   12588         6900 :   gfc_init_block (&alloc_block);
   12589         6900 :   if (!coarray)
   12590              :     {
   12591         6861 :       tmp = build_call_expr_loc (input_location,
   12592              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   12593              :                                  1, size2);
   12594         6861 :       gfc_conv_descriptor_data_set (&alloc_block,
   12595              :                                     desc, tmp);
   12596              :     }
   12597              :   else
   12598              :     {
   12599           39 :       tmp = build_call_expr_loc (input_location,
   12600              :                                  gfor_fndecl_caf_register,
   12601              :                                  7, size2,
   12602              :                                  build_int_cst (integer_type_node,
   12603              :                                                 GFC_CAF_COARRAY_ALLOC),
   12604              :                                  token, gfc_build_addr_expr (NULL_TREE, desc),
   12605              :                                  null_pointer_node, null_pointer_node,
   12606              :                                  integer_zero_node);
   12607           39 :       gfc_add_expr_to_block (&alloc_block, tmp);
   12608              :     }
   12609              : 
   12610              : 
   12611              :   /* We already set the dtype in the case of deferred character
   12612              :      length arrays and class lvalues.  */
   12613         6900 :   if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
   12614         6900 :         && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12615         6207 :             || coarray))
   12616        13068 :       && expr1->ts.type != BT_CLASS)
   12617         5493 :     gfc_conv_descriptor_dtype_set (&alloc_block, desc,
   12618         5493 :                                    gfc_get_dtype (TREE_TYPE (desc)));
   12619              : 
   12620         6900 :   if ((expr1->ts.type == BT_DERIVED)
   12621          461 :         && expr1->ts.u.derived->attr.alloc_comp)
   12622              :     {
   12623          201 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
   12624              :                                     expr1->rank);
   12625          201 :       gfc_add_expr_to_block (&alloc_block, tmp);
   12626              :     }
   12627         6900 :   alloc_expr = gfc_finish_block (&alloc_block);
   12628              : 
   12629              :   /* Malloc if not allocated; realloc otherwise.  */
   12630         6900 :   tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
   12631         6900 :   gfc_add_expr_to_block (&fblock, tmp);
   12632              : 
   12633              :   /* Add the label for same shape lhs and rhs.  */
   12634         6900 :   tmp = build1_v (LABEL_EXPR, jump_label2);
   12635         6900 :   gfc_add_expr_to_block (&fblock, tmp);
   12636              : 
   12637         6900 :   tree realloc_code = gfc_finish_block (&fblock);
   12638              : 
   12639         6900 :   stmtblock_t result_block;
   12640         6900 :   gfc_init_block (&result_block);
   12641         6900 :   gfc_add_expr_to_block (&result_block, realloc_code);
   12642         6900 :   update_reallocated_descriptor (&result_block, loop);
   12643              : 
   12644         6900 :   return gfc_finish_block (&result_block);
   12645              : }
   12646              : 
   12647              : 
   12648              : /* Initialize class descriptor's TKR information.  */
   12649              : 
   12650              : void
   12651         3308 : gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
   12652              : {
   12653         3308 :   tree type, etype;
   12654         3308 :   tree descriptor;
   12655         3308 :   stmtblock_t init;
   12656         3308 :   int rank;
   12657              : 
   12658              :   /* Make sure the frontend gets these right.  */
   12659         3308 :   gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   12660              :               && (CLASS_DATA (sym)->attr.class_pointer
   12661              :                   || CLASS_DATA (sym)->attr.allocatable));
   12662              : 
   12663         3308 :   gcc_assert (VAR_P (sym->backend_decl)
   12664              :               || TREE_CODE (sym->backend_decl) == PARM_DECL);
   12665              : 
   12666         3308 :   if (sym->attr.dummy)
   12667         1607 :     return;
   12668              : 
   12669         3308 :   descriptor = gfc_class_data_get (sym->backend_decl);
   12670         3308 :   type = TREE_TYPE (descriptor);
   12671              : 
   12672         3308 :   if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
   12673              :     return;
   12674              : 
   12675         1701 :   location_t loc = input_location;
   12676         1701 :   input_location = gfc_get_location (&sym->declared_at);
   12677         1701 :   gfc_init_block (&init);
   12678              : 
   12679         1701 :   rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
   12680         1701 :   gcc_assert (rank>=0);
   12681         1701 :   etype = gfc_get_element_type (type);
   12682         1701 :   gfc_conv_descriptor_dtype_set (&init, descriptor,
   12683              :                                  gfc_get_dtype_rank_type (rank, etype));
   12684              : 
   12685         1701 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12686         1701 :   input_location = loc;
   12687              : }
   12688              : 
   12689              : 
   12690              : /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
   12691              :    Do likewise, recursively if necessary, with the allocatable components of
   12692              :    derived types.  This function is also called for assumed-rank arrays, which
   12693              :    are always dummy arguments.  */
   12694              : 
   12695              : void
   12696        19071 : gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
   12697              : {
   12698        19071 :   tree type;
   12699        19071 :   tree tmp;
   12700        19071 :   tree descriptor;
   12701        19071 :   stmtblock_t init;
   12702        19071 :   stmtblock_t cleanup;
   12703        19071 :   int rank;
   12704        19071 :   bool sym_has_alloc_comp, has_finalizer;
   12705              : 
   12706        38142 :   sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
   12707        11256 :                         || sym->ts.type == BT_CLASS)
   12708        19071 :                           && sym->ts.u.derived->attr.alloc_comp;
   12709        19071 :   has_finalizer = gfc_may_be_finalized (sym->ts);
   12710              : 
   12711              :   /* Make sure the frontend gets these right.  */
   12712        19071 :   gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
   12713              :               || has_finalizer
   12714              :               || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
   12715              : 
   12716        19071 :   location_t loc = input_location;
   12717        19071 :   input_location = gfc_get_location (&sym->declared_at);
   12718        19071 :   gfc_init_block (&init);
   12719              : 
   12720        19071 :   gcc_assert (VAR_P (sym->backend_decl)
   12721              :               || TREE_CODE (sym->backend_decl) == PARM_DECL);
   12722              : 
   12723        19071 :   if (sym->ts.type == BT_CHARACTER
   12724         1444 :       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
   12725              :     {
   12726          848 :       if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
   12727              :         {
   12728          631 :           tree len_expr = sym->ts.u.cl->backend_decl;
   12729          631 :           tree init_val = build_zero_cst (TREE_TYPE (len_expr));
   12730          631 :           if (VAR_P (len_expr)
   12731          631 :               && sym->attr.save
   12732          686 :               && !DECL_INITIAL (len_expr))
   12733           55 :             DECL_INITIAL (len_expr) = init_val;
   12734              :           else
   12735          576 :             gfc_add_modify (&init, len_expr, init_val);
   12736              :         }
   12737          848 :       gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
   12738          848 :       gfc_trans_vla_type_sizes (sym, &init);
   12739              : 
   12740              :       /* Presence check of optional deferred-length character dummy.  */
   12741          848 :       if (sym->ts.deferred && sym->attr.dummy && sym->attr.optional)
   12742              :         {
   12743           43 :           tmp = gfc_finish_block (&init);
   12744           43 :           tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
   12745              :                           tmp, build_empty_stmt (input_location));
   12746           43 :           gfc_add_expr_to_block (&init, tmp);
   12747              :         }
   12748              :     }
   12749              : 
   12750              :   /* Dummy, use associated and result variables don't need anything special.  */
   12751        19071 :   if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
   12752              :     {
   12753         1037 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12754         1037 :       input_location = loc;
   12755         1313 :       return;
   12756              :     }
   12757              : 
   12758        18034 :   descriptor = sym->backend_decl;
   12759              : 
   12760              :   /* Although static, derived types with default initializers and
   12761              :      allocatable components must not be nulled wholesale; instead they
   12762              :      are treated component by component.  */
   12763        18034 :   if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
   12764              :     {
   12765              :       /* SAVEd variables are not freed on exit.  */
   12766          276 :       gfc_trans_static_array_pointer (sym);
   12767              : 
   12768          276 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12769          276 :       input_location = loc;
   12770          276 :       return;
   12771              :     }
   12772              : 
   12773              :   /* Get the descriptor type.  */
   12774        17758 :   type = TREE_TYPE (sym->backend_decl);
   12775              : 
   12776        17758 :   if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
   12777         6033 :       && !(sym->attr.pointer || sym->attr.allocatable))
   12778              :     {
   12779         3103 :       if (!sym->attr.save
   12780         2694 :           && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
   12781              :         {
   12782         2694 :           if (sym->value == NULL
   12783         2694 :               || !gfc_has_default_initializer (sym->ts.u.derived))
   12784              :             {
   12785         2196 :               rank = sym->as ? sym->as->rank : 0;
   12786         2196 :               tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
   12787              :                                             descriptor, rank);
   12788         2196 :               gfc_add_expr_to_block (&init, tmp);
   12789              :             }
   12790              :           else
   12791          498 :             gfc_init_default_dt (sym, &init, false);
   12792              :         }
   12793              :     }
   12794        14655 :   else if (!GFC_DESCRIPTOR_TYPE_P (type))
   12795              :     {
   12796              :       /* If the backend_decl is not a descriptor, we must have a pointer
   12797              :          to one.  */
   12798         2336 :       descriptor = build_fold_indirect_ref_loc (input_location,
   12799              :                                                 sym->backend_decl);
   12800         2336 :       type = TREE_TYPE (descriptor);
   12801              :     }
   12802              : 
   12803              :   /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
   12804              :      pointers when -fcheck=pointer is specified.  */
   12805        17758 :   if (GFC_DESCRIPTOR_TYPE_P (type)
   12806        17758 :       && (sym->attr.allocatable || sym->attr.pointer))
   12807              :     {
   12808        12319 :       if (flag_coarray == GFC_FCOARRAY_LIB
   12809          445 :           && sym->attr.codimension
   12810          271 :           && !sym->attr.save)
   12811              :         {
   12812              :           /* Declare the variable static so its array descriptor stays present
   12813              :              after leaving the scope.  It may still be accessed through another
   12814              :              image.  This may happen, for example, with the caf_mpi
   12815              :              implementation.  */
   12816          271 :           TREE_STATIC (descriptor) = 1;
   12817              :         }
   12818        12319 :       gfc_init_descriptor_variable (&init, sym, descriptor);
   12819              :     }
   12820              : 
   12821        17758 :   input_location = loc;
   12822        17758 :   gfc_init_block (&cleanup);
   12823              : 
   12824              :   /* Allocatable arrays need to be freed when they go out of scope.
   12825              :      The allocatable components of pointers must not be touched.  */
   12826        17758 :   if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
   12827          652 :       && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
   12828          357 :       && !sym->ns->proc_name->attr.is_main_program)
   12829              :     {
   12830          306 :       gfc_expr *e;
   12831          306 :       sym->attr.referenced = 1;
   12832          306 :       e = gfc_lval_expr_from_sym (sym);
   12833          306 :       gfc_add_finalizer_call (&cleanup, e);
   12834          306 :       gfc_free_expr (e);
   12835          306 :     }
   12836        17452 :   else if ((!sym->attr.allocatable || !has_finalizer)
   12837        17328 :       && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
   12838         5428 :       && !sym->attr.pointer && !sym->attr.save
   12839         2657 :       && !(sym->attr.artificial && sym->name[0] == '_')
   12840         2602 :       && !sym->ns->proc_name->attr.is_main_program)
   12841              :     {
   12842          719 :       int rank;
   12843          719 :       rank = sym->as ? sym->as->rank : 0;
   12844          719 :       tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank,
   12845          719 :                                        (sym->attr.codimension
   12846            3 :                                         && flag_coarray == GFC_FCOARRAY_LIB)
   12847              :                                        ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY
   12848              :                                        : 0);
   12849          719 :       gfc_add_expr_to_block (&cleanup, tmp);
   12850              :     }
   12851              : 
   12852        17758 :   if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
   12853         8995 :       && !sym->attr.save && !sym->attr.result
   12854         8988 :       && !sym->ns->proc_name->attr.is_main_program)
   12855              :     {
   12856         4616 :       gfc_expr *e;
   12857         4616 :       e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
   12858         9232 :       tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
   12859              :                                         NULL_TREE, NULL_TREE, true, e,
   12860         4616 :                                         sym->attr.codimension
   12861              :                                         ? GFC_CAF_COARRAY_DEREGISTER
   12862              :                                         : GFC_CAF_COARRAY_NOCOARRAY,
   12863              :                                         NULL_TREE, gfc_finish_block (&cleanup));
   12864         4616 :       if (e)
   12865           45 :         gfc_free_expr (e);
   12866         4616 :       gfc_init_block (&cleanup);
   12867         4616 :       gfc_add_expr_to_block (&cleanup, tmp);
   12868              :     }
   12869              : 
   12870        17758 :   gfc_add_init_cleanup (block, gfc_finish_block (&init),
   12871              :                         gfc_finish_block (&cleanup));
   12872              : }
   12873              : 
   12874              : /************ Expression Walking Functions ******************/
   12875              : 
   12876              : /* Walk a variable reference.
   12877              : 
   12878              :    Possible extension - multiple component subscripts.
   12879              :     x(:,:) = foo%a(:)%b(:)
   12880              :    Transforms to
   12881              :     forall (i=..., j=...)
   12882              :       x(i,j) = foo%a(j)%b(i)
   12883              :     end forall
   12884              :    This adds a fair amount of complexity because you need to deal with more
   12885              :    than one ref.  Maybe handle in a similar manner to vector subscripts.
   12886              :    Maybe not worth the effort.  */
   12887              : 
   12888              : 
   12889              : static gfc_ss *
   12890       719519 : gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
   12891              : {
   12892       719519 :   gfc_ref *ref;
   12893              : 
   12894       719519 :   gfc_fix_class_refs (expr);
   12895              : 
   12896       844311 :   for (ref = expr->ref; ref; ref = ref->next)
   12897       472994 :     if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
   12898              :       break;
   12899              : 
   12900       719519 :   return gfc_walk_array_ref (ss, expr, ref);
   12901              : }
   12902              : 
   12903              : gfc_ss *
   12904       720000 : gfc_walk_array_ref (gfc_ss *ss, gfc_expr *expr, gfc_ref *ref, bool array_only)
   12905              : {
   12906       720000 :   gfc_array_ref *ar;
   12907       720000 :   gfc_ss *newss;
   12908       720000 :   int n;
   12909              : 
   12910      1079042 :   for (; ref; ref = ref->next)
   12911              :     {
   12912       359042 :       if (ref->type == REF_SUBSTRING)
   12913              :         {
   12914         1320 :           ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
   12915         1320 :           if (ref->u.ss.end)
   12916         1294 :             ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
   12917              :         }
   12918              : 
   12919              :       /* We're only interested in array sections from now on.  */
   12920       359042 :       if (ref->type != REF_ARRAY
   12921       349103 :           || (array_only && ref->u.ar.as && ref->u.ar.as->rank == 0))
   12922        10054 :         continue;
   12923              : 
   12924       348988 :       ar = &ref->u.ar;
   12925              : 
   12926       348988 :       switch (ar->type)
   12927              :         {
   12928          338 :         case AR_ELEMENT:
   12929          723 :           for (n = ar->dimen - 1; n >= 0; n--)
   12930          385 :             ss = gfc_get_scalar_ss (ss, ar->start[n]);
   12931              :           break;
   12932              : 
   12933       290737 :         case AR_FULL:
   12934              :           /* Assumed shape arrays from interface mapping need this fix.  */
   12935       290737 :           if (!ar->as && expr->symtree->n.sym->as)
   12936              :             {
   12937            6 :               ar->as = gfc_get_array_spec();
   12938            6 :               *ar->as = *expr->symtree->n.sym->as;
   12939              :             }
   12940       290737 :           newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
   12941       290737 :           newss->info->data.array.ref = ref;
   12942              : 
   12943              :           /* Make sure array is the same as array(:,:), this way
   12944              :              we don't need to special case all the time.  */
   12945       290737 :           ar->dimen = ar->as->rank;
   12946       662868 :           for (n = 0; n < ar->dimen; n++)
   12947              :             {
   12948       372131 :               ar->dimen_type[n] = DIMEN_RANGE;
   12949              : 
   12950       372131 :               gcc_assert (ar->start[n] == NULL);
   12951       372131 :               gcc_assert (ar->end[n] == NULL);
   12952       372131 :               gcc_assert (ar->stride[n] == NULL);
   12953              :             }
   12954              :           ss = newss;
   12955              :           break;
   12956              : 
   12957        57913 :         case AR_SECTION:
   12958        57913 :           newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
   12959        57913 :           newss->info->data.array.ref = ref;
   12960              : 
   12961              :           /* We add SS chains for all the subscripts in the section.  */
   12962       148654 :           for (n = 0; n < ar->dimen; n++)
   12963              :             {
   12964        90741 :               gfc_ss *indexss;
   12965              : 
   12966        90741 :               switch (ar->dimen_type[n])
   12967              :                 {
   12968         6974 :                 case DIMEN_ELEMENT:
   12969              :                   /* Add SS for elemental (scalar) subscripts.  */
   12970         6974 :                   gcc_assert (ar->start[n]);
   12971         6974 :                   indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
   12972         6974 :                   indexss->loop_chain = gfc_ss_terminator;
   12973         6974 :                   newss->info->data.array.subscript[n] = indexss;
   12974         6974 :                   break;
   12975              : 
   12976        82720 :                 case DIMEN_RANGE:
   12977              :                   /* We don't add anything for sections, just remember this
   12978              :                      dimension for later.  */
   12979        82720 :                   newss->dim[newss->dimen] = n;
   12980        82720 :                   newss->dimen++;
   12981        82720 :                   break;
   12982              : 
   12983         1047 :                 case DIMEN_VECTOR:
   12984              :                   /* Create a GFC_SS_VECTOR index in which we can store
   12985              :                      the vector's descriptor.  */
   12986         1047 :                   indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
   12987              :                                               1, GFC_SS_VECTOR);
   12988         1047 :                   indexss->loop_chain = gfc_ss_terminator;
   12989         1047 :                   newss->info->data.array.subscript[n] = indexss;
   12990         1047 :                   newss->dim[newss->dimen] = n;
   12991         1047 :                   newss->dimen++;
   12992         1047 :                   break;
   12993              : 
   12994            0 :                 default:
   12995              :                   /* We should know what sort of section it is by now.  */
   12996            0 :                   gcc_unreachable ();
   12997              :                 }
   12998              :             }
   12999              :           /* We should have at least one non-elemental dimension,
   13000              :              unless we are creating a descriptor for a (scalar) coarray.  */
   13001        57913 :           gcc_assert (newss->dimen > 0
   13002              :                       || newss->info->data.array.ref->u.ar.as->corank > 0);
   13003              :           ss = newss;
   13004              :           break;
   13005              : 
   13006            0 :         default:
   13007              :           /* We should know what sort of section it is by now.  */
   13008            0 :           gcc_unreachable ();
   13009              :         }
   13010              : 
   13011              :     }
   13012       720000 :   return ss;
   13013              : }
   13014              : 
   13015              : 
   13016              : /* Walk an expression operator. If only one operand of a binary expression is
   13017              :    scalar, we must also add the scalar term to the SS chain.  */
   13018              : 
   13019              : static gfc_ss *
   13020        59483 : gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
   13021              : {
   13022        59483 :   gfc_ss *head;
   13023        59483 :   gfc_ss *head2;
   13024              : 
   13025        59483 :   head = gfc_walk_subexpr (ss, expr->value.op.op1);
   13026        59483 :   if (expr->value.op.op2 == NULL)
   13027              :     head2 = head;
   13028              :   else
   13029        56738 :     head2 = gfc_walk_subexpr (head, expr->value.op.op2);
   13030              : 
   13031              :   /* All operands are scalar.  Pass back and let the caller deal with it.  */
   13032        59483 :   if (head2 == ss)
   13033              :     return head2;
   13034              : 
   13035              :   /* All operands require scalarization.  */
   13036        53497 :   if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
   13037              :     return head2;
   13038              : 
   13039              :   /* One of the operands needs scalarization, the other is scalar.
   13040              :      Create a gfc_ss for the scalar expression.  */
   13041        19762 :   if (head == ss)
   13042              :     {
   13043              :       /* First operand is scalar.  We build the chain in reverse order, so
   13044              :          add the scalar SS after the second operand.  */
   13045              :       head = head2;
   13046         2304 :       while (head && head->next != ss)
   13047              :         head = head->next;
   13048              :       /* Check we haven't somehow broken the chain.  */
   13049         2061 :       gcc_assert (head);
   13050         2061 :       head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
   13051              :     }
   13052              :   else                          /* head2 == head */
   13053              :     {
   13054        17701 :       gcc_assert (head2 == head);
   13055              :       /* Second operand is scalar.  */
   13056        17701 :       head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
   13057              :     }
   13058              : 
   13059              :   return head2;
   13060              : }
   13061              : 
   13062              : static gfc_ss *
   13063           36 : gfc_walk_conditional_expr (gfc_ss *ss, gfc_expr *expr)
   13064              : {
   13065           36 :   gfc_ss *head;
   13066              : 
   13067           36 :   head = gfc_walk_subexpr (ss, expr->value.conditional.true_expr);
   13068           36 :   head = gfc_walk_subexpr (head, expr->value.conditional.false_expr);
   13069           36 :   return head;
   13070              : }
   13071              : 
   13072              : /* Reverse a SS chain.  */
   13073              : 
   13074              : gfc_ss *
   13075       899714 : gfc_reverse_ss (gfc_ss * ss)
   13076              : {
   13077       899714 :   gfc_ss *next;
   13078       899714 :   gfc_ss *head;
   13079              : 
   13080       899714 :   gcc_assert (ss != NULL);
   13081              : 
   13082              :   head = gfc_ss_terminator;
   13083      1361173 :   while (ss != gfc_ss_terminator)
   13084              :     {
   13085       461459 :       next = ss->next;
   13086              :       /* Check we didn't somehow break the chain.  */
   13087       461459 :       gcc_assert (next != NULL);
   13088       461459 :       ss->next = head;
   13089       461459 :       head = ss;
   13090       461459 :       ss = next;
   13091              :     }
   13092              : 
   13093       899714 :   return (head);
   13094              : }
   13095              : 
   13096              : 
   13097              : /* Given an expression referring to a procedure, return the symbol of its
   13098              :    interface.  We can't get the procedure symbol directly as we have to handle
   13099              :    the case of (deferred) type-bound procedures.  */
   13100              : 
   13101              : gfc_symbol *
   13102          246 : gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
   13103              : {
   13104          246 :   gfc_symbol *sym;
   13105          246 :   gfc_ref *ref;
   13106              : 
   13107          246 :   if (procedure_ref == NULL)
   13108              :     return NULL;
   13109              : 
   13110              :   /* Normal procedure case.  */
   13111          246 :   if (procedure_ref->expr_type == EXPR_FUNCTION
   13112          246 :       && procedure_ref->value.function.esym)
   13113              :     sym = procedure_ref->value.function.esym;
   13114              :   else
   13115           36 :     sym = procedure_ref->symtree->n.sym;
   13116              : 
   13117              :   /* Typebound procedure case.  */
   13118          318 :   for (ref = procedure_ref->ref; ref; ref = ref->next)
   13119              :     {
   13120           72 :       if (ref->type == REF_COMPONENT
   13121           72 :           && ref->u.c.component->attr.proc_pointer)
   13122           36 :         sym = ref->u.c.component->ts.interface;
   13123              :       else
   13124              :         sym = NULL;
   13125              :     }
   13126              : 
   13127              :   return sym;
   13128              : }
   13129              : 
   13130              : 
   13131              : /* Given an expression referring to an intrinsic function call,
   13132              :    return the intrinsic symbol.  */
   13133              : 
   13134              : gfc_intrinsic_sym *
   13135         7990 : gfc_get_intrinsic_for_expr (gfc_expr *call)
   13136              : {
   13137         7990 :   if (call == NULL)
   13138              :     return NULL;
   13139              : 
   13140              :   /* Normal procedure case.  */
   13141         2386 :   if (call->expr_type == EXPR_FUNCTION)
   13142         2274 :     return call->value.function.isym;
   13143              :   else
   13144              :     return NULL;
   13145              : }
   13146              : 
   13147              : 
   13148              : /* Indicates whether an argument to an intrinsic function should be used in
   13149              :    scalarization.  It is usually the case, except for some intrinsics
   13150              :    requiring the value to be constant, and using the value at compile time only.
   13151              :    As the value is not used at runtime in those cases, we don’t produce code
   13152              :    for it, and it should not be visible to the scalarizer.
   13153              :    FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
   13154              :    argument being examined in that call, and ARG_NUM the index number
   13155              :    of ACTUAL_ARG in the list of arguments.
   13156              :    The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
   13157              :    identified using the name in ACTUAL_ARG if it is present (that is: if it’s
   13158              :    a keyword argument), otherwise using ARG_NUM.  */
   13159              : 
   13160              : static bool
   13161        38357 : arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
   13162              :                                  gfc_dummy_arg *dummy_arg)
   13163              : {
   13164        38357 :   if (function != NULL && dummy_arg != NULL)
   13165              :     {
   13166        12667 :       switch (function->id)
   13167              :         {
   13168          241 :           case GFC_ISYM_INDEX:
   13169          241 :           case GFC_ISYM_LEN_TRIM:
   13170          241 :           case GFC_ISYM_MASKL:
   13171          241 :           case GFC_ISYM_MASKR:
   13172          241 :           case GFC_ISYM_SCAN:
   13173          241 :           case GFC_ISYM_VERIFY:
   13174          241 :             if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
   13175           33 :               return false;
   13176              :           /* Fallthrough.  */
   13177              : 
   13178              :           default:
   13179              :             break;
   13180              :         }
   13181              :     }
   13182              : 
   13183              :   return true;
   13184              : }
   13185              : 
   13186              : 
   13187              : /* Walk the arguments of an elemental function.
   13188              :    PROC_EXPR is used to check whether an argument is permitted to be absent.  If
   13189              :    it is NULL, we don't do the check and the argument is assumed to be present.
   13190              : */
   13191              : 
   13192              : gfc_ss *
   13193        27248 : gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
   13194              :                                   gfc_intrinsic_sym *intrinsic_sym,
   13195              :                                   gfc_ss_type type)
   13196              : {
   13197        27248 :   int scalar;
   13198        27248 :   gfc_ss *head;
   13199        27248 :   gfc_ss *tail;
   13200        27248 :   gfc_ss *newss;
   13201              : 
   13202        27248 :   head = gfc_ss_terminator;
   13203        27248 :   tail = NULL;
   13204              : 
   13205        27248 :   scalar = 1;
   13206        67075 :   for (; arg; arg = arg->next)
   13207              :     {
   13208        39827 :       gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
   13209        41330 :       if (!arg->expr
   13210        38507 :           || arg->expr->expr_type == EXPR_NULL
   13211        78184 :           || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
   13212         1503 :         continue;
   13213              : 
   13214        38324 :       newss = gfc_walk_subexpr (head, arg->expr);
   13215        38324 :       if (newss == head)
   13216              :         {
   13217              :           /* Scalar argument.  */
   13218        18753 :           gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
   13219        18753 :           newss = gfc_get_scalar_ss (head, arg->expr);
   13220        18753 :           newss->info->type = type;
   13221        18753 :           if (dummy_arg)
   13222        15597 :             newss->info->data.scalar.dummy_arg = dummy_arg;
   13223              :         }
   13224              :       else
   13225              :         scalar = 0;
   13226              : 
   13227        35168 :       if (dummy_arg != NULL
   13228        26654 :           && gfc_dummy_arg_is_optional (*dummy_arg)
   13229         2608 :           && arg->expr->expr_type == EXPR_VARIABLE
   13230        36878 :           && (gfc_expr_attr (arg->expr).optional
   13231         1199 :               || gfc_expr_attr (arg->expr).allocatable
   13232        38324 :               || gfc_expr_attr (arg->expr).pointer))
   13233         1023 :         newss->info->can_be_null_ref = true;
   13234              : 
   13235        38324 :       head = newss;
   13236        38324 :       if (!tail)
   13237              :         {
   13238              :           tail = head;
   13239        33984 :           while (tail->next != gfc_ss_terminator)
   13240              :             tail = tail->next;
   13241              :         }
   13242              :     }
   13243              : 
   13244        27248 :   if (scalar)
   13245              :     {
   13246              :       /* If all the arguments are scalar we don't need the argument SS.  */
   13247        10463 :       gfc_free_ss_chain (head);
   13248              :       /* Pass it back.  */
   13249        10463 :       return ss;
   13250              :     }
   13251              : 
   13252              :   /* Add it onto the existing chain.  */
   13253        16785 :   tail->next = ss;
   13254        16785 :   return head;
   13255              : }
   13256              : 
   13257              : 
   13258              : /* Walk a function call.  Scalar functions are passed back, and taken out of
   13259              :    scalarization loops.  For elemental functions we walk their arguments.
   13260              :    The result of functions returning arrays is stored in a temporary outside
   13261              :    the loop, so that the function is only called once.  Hence we do not need
   13262              :    to walk their arguments.  */
   13263              : 
   13264              : static gfc_ss *
   13265        64962 : gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
   13266              : {
   13267        64962 :   gfc_intrinsic_sym *isym;
   13268        64962 :   gfc_symbol *sym;
   13269        64962 :   gfc_component *comp = NULL;
   13270              : 
   13271        64962 :   isym = expr->value.function.isym;
   13272              : 
   13273              :   /* Handle intrinsic functions separately.  */
   13274        64962 :   if (isym)
   13275        56692 :     return gfc_walk_intrinsic_function (ss, expr, isym);
   13276              : 
   13277         8270 :   sym = expr->value.function.esym;
   13278         8270 :   if (!sym)
   13279          552 :     sym = expr->symtree->n.sym;
   13280              : 
   13281         8270 :   if (gfc_is_class_array_function (expr))
   13282          240 :     return gfc_get_array_ss (ss, expr,
   13283          240 :                              CLASS_DATA (expr->value.function.esym->result)->as->rank,
   13284          240 :                              GFC_SS_FUNCTION);
   13285              : 
   13286              :   /* A function that returns arrays.  */
   13287         8030 :   comp = gfc_get_proc_ptr_comp (expr);
   13288         7626 :   if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
   13289         8030 :       || (comp && comp->attr.dimension))
   13290         3100 :     return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
   13291              : 
   13292              :   /* Walk the parameters of an elemental function.  For now we always pass
   13293              :      by reference.  */
   13294         4930 :   if (sym->attr.elemental || (comp && comp->attr.elemental))
   13295              :     {
   13296         2238 :       gfc_ss *old_ss = ss;
   13297              : 
   13298         2238 :       ss = gfc_walk_elemental_function_args (old_ss,
   13299              :                                              expr->value.function.actual,
   13300              :                                              gfc_get_intrinsic_for_expr (expr),
   13301              :                                              GFC_SS_REFERENCE);
   13302         2238 :       if (ss != old_ss
   13303         1202 :           && (comp
   13304         1135 :               || sym->attr.proc_pointer
   13305         1135 :               || sym->attr.if_source != IFSRC_DECL
   13306         1013 :               || sym->attr.array_outer_dependency))
   13307          237 :         ss->info->array_outer_dependency = 1;
   13308              :     }
   13309              : 
   13310              :   /* Scalar functions are OK as these are evaluated outside the scalarization
   13311              :      loop.  Pass back and let the caller deal with it.  */
   13312              :   return ss;
   13313              : }
   13314              : 
   13315              : 
   13316              : /* An array temporary is constructed for array constructors.  */
   13317              : 
   13318              : static gfc_ss *
   13319        53019 : gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
   13320              : {
   13321            0 :   return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
   13322              : }
   13323              : 
   13324              : 
   13325              : /* Walk an expression.  Add walked expressions to the head of the SS chain.
   13326              :    A wholly scalar expression will not be added.  */
   13327              : 
   13328              : gfc_ss *
   13329      1058567 : gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
   13330              : {
   13331      1058567 :   gfc_ss *head;
   13332              : 
   13333      1058567 :   switch (expr->expr_type)
   13334              :     {
   13335       719519 :     case EXPR_VARIABLE:
   13336       719519 :       head = gfc_walk_variable_expr (ss, expr);
   13337       719519 :       return head;
   13338              : 
   13339        59483 :     case EXPR_OP:
   13340        59483 :       head = gfc_walk_op_expr (ss, expr);
   13341        59483 :       return head;
   13342              : 
   13343           36 :     case EXPR_CONDITIONAL:
   13344           36 :       head = gfc_walk_conditional_expr (ss, expr);
   13345           36 :       return head;
   13346              : 
   13347        64962 :     case EXPR_FUNCTION:
   13348        64962 :       head = gfc_walk_function_expr (ss, expr);
   13349        64962 :       return head;
   13350              : 
   13351              :     case EXPR_CONSTANT:
   13352              :     case EXPR_NULL:
   13353              :     case EXPR_STRUCTURE:
   13354              :       /* Pass back and let the caller deal with it.  */
   13355              :       break;
   13356              : 
   13357        53019 :     case EXPR_ARRAY:
   13358        53019 :       head = gfc_walk_array_constructor (ss, expr);
   13359        53019 :       return head;
   13360              : 
   13361              :     case EXPR_SUBSTRING:
   13362              :       /* Pass back and let the caller deal with it.  */
   13363              :       break;
   13364              : 
   13365            0 :     default:
   13366            0 :       gfc_internal_error ("bad expression type during walk (%d)",
   13367              :                       expr->expr_type);
   13368              :     }
   13369              :   return ss;
   13370              : }
   13371              : 
   13372              : 
   13373              : /* Entry point for expression walking.
   13374              :    A return value equal to the passed chain means this is
   13375              :    a scalar expression.  It is up to the caller to take whatever action is
   13376              :    necessary to translate these.  */
   13377              : 
   13378              : gfc_ss *
   13379       896765 : gfc_walk_expr (gfc_expr * expr)
   13380              : {
   13381       896765 :   gfc_ss *res;
   13382              : 
   13383       896765 :   res = gfc_walk_subexpr (gfc_ss_terminator, expr);
   13384       896765 :   return gfc_reverse_ss (res);
   13385              : }
        

Generated by: LCOV version 2.6-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.