LCOV - code coverage report
Current view: top level - gcc/fortran - trans-array.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.2 % 6249 6009
Test Date: 2026-11-14 16:39:50 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        60769 : gfc_array_dataptr_type (tree desc)
     109              : {
     110        60769 :   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        84943 : gfc_get_cfi_descriptor_field (tree desc, unsigned field_idx)
     128              : {
     129        84943 :   tree type = TREE_TYPE (desc);
     130        84943 :   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        84943 :   tree field = gfc_advance_chain (TYPE_FIELDS (type), field_idx);
     136        84943 :   gcc_assert (field != NULL_TREE);
     137              : 
     138        84943 :   return fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
     139        84943 :                           desc, field, NULL_TREE);
     140              : }
     141              : 
     142              : tree
     143        14201 : gfc_get_cfi_desc_base_addr (tree desc)
     144              : {
     145        14201 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_BASE_ADDR);
     146              : }
     147              : 
     148              : tree
     149        10681 : gfc_get_cfi_desc_elem_len (tree desc)
     150              : {
     151        10681 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_ELEM_LEN);
     152              : }
     153              : 
     154              : tree
     155         7191 : gfc_get_cfi_desc_version (tree desc)
     156              : {
     157         7191 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_VERSION);
     158              : }
     159              : 
     160              : tree
     161         7816 : gfc_get_cfi_desc_rank (tree desc)
     162              : {
     163         7816 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_RANK);
     164              : }
     165              : 
     166              : tree
     167         7283 : gfc_get_cfi_desc_type (tree desc)
     168              : {
     169         7283 :   return gfc_get_cfi_descriptor_field (desc, CFI_FIELD_TYPE);
     170              : }
     171              : 
     172              : tree
     173         7191 : gfc_get_cfi_desc_attribute (tree desc)
     174              : {
     175         7191 :   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       179478 : gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
     226              : {
     227       420998 :   for (; ss != gfc_ss_terminator; ss = ss->next)
     228       241520 :     ss->info->useflags = flags;
     229       179478 : }
     230              : 
     231              : 
     232              : /* Free a gfc_ss chain.  */
     233              : 
     234              : void
     235       187771 : gfc_free_ss_chain (gfc_ss * ss)
     236              : {
     237       187771 :   gfc_ss *next;
     238              : 
     239       383753 :   while (ss != gfc_ss_terminator)
     240              :     {
     241       195982 :       gcc_assert (ss != NULL);
     242       195982 :       next = ss->next;
     243       195982 :       gfc_free_ss (ss);
     244       195982 :       ss = next;
     245              :     }
     246       187771 : }
     247              : 
     248              : 
     249              : static void
     250       509044 : free_ss_info (gfc_ss_info *ss_info)
     251              : {
     252       509044 :   int n;
     253              : 
     254       509044 :   ss_info->refcount--;
     255       509044 :   if (ss_info->refcount > 0)
     256              :     return;
     257              : 
     258       504297 :   gcc_assert (ss_info->refcount == 0);
     259              : 
     260       504297 :   switch (ss_info->type)
     261              :     {
     262              :     case GFC_SS_SECTION:
     263      5611056 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
     264      5260365 :         if (ss_info->data.array.subscript[n])
     265         7900 :           gfc_free_ss_chain (ss_info->data.array.subscript[n]);
     266              :       break;
     267              : 
     268              :     default:
     269              :       break;
     270              :     }
     271              : 
     272       504297 :   free (ss_info);
     273              : }
     274              : 
     275              : 
     276              : /* Free a SS.  */
     277              : 
     278              : void
     279       509044 : gfc_free_ss (gfc_ss * ss)
     280              : {
     281       509044 :   free_ss_info (ss->info);
     282       509044 :   free (ss);
     283       509044 : }
     284              : 
     285              : 
     286              : /* Creates and initializes an array type gfc_ss struct.  */
     287              : 
     288              : gfc_ss *
     289       426562 : gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
     290              : {
     291       426562 :   gfc_ss *ss;
     292       426562 :   gfc_ss_info *ss_info;
     293       426562 :   int i;
     294              : 
     295       426562 :   ss_info = gfc_get_ss_info ();
     296       426562 :   ss_info->refcount++;
     297       426562 :   ss_info->type = type;
     298       426562 :   ss_info->expr = expr;
     299              : 
     300       426562 :   ss = gfc_get_ss ();
     301       426562 :   ss->info = ss_info;
     302       426562 :   ss->next = next;
     303       426562 :   ss->dimen = dimen;
     304       896054 :   for (i = 0; i < ss->dimen; i++)
     305       469492 :     ss->dim[i] = i;
     306              : 
     307       426562 :   return ss;
     308              : }
     309              : 
     310              : 
     311              : /* Creates and initializes a temporary type gfc_ss struct.  */
     312              : 
     313              : gfc_ss *
     314        11857 : gfc_get_temp_ss (tree type, tree string_length, int dimen)
     315              : {
     316        11857 :   gfc_ss *ss;
     317        11857 :   gfc_ss_info *ss_info;
     318        11857 :   int i;
     319              : 
     320        11857 :   ss_info = gfc_get_ss_info ();
     321        11857 :   ss_info->refcount++;
     322        11857 :   ss_info->type = GFC_SS_TEMP;
     323        11857 :   ss_info->string_length = string_length;
     324        11857 :   ss_info->data.temp.type = type;
     325              : 
     326        11857 :   ss = gfc_get_ss ();
     327        11857 :   ss->info = ss_info;
     328        11857 :   ss->next = gfc_ss_terminator;
     329        11857 :   ss->dimen = dimen;
     330        26466 :   for (i = 0; i < ss->dimen; i++)
     331        14609 :     ss->dim[i] = i;
     332              : 
     333        11857 :   return ss;
     334              : }
     335              : 
     336              : 
     337              : /* Creates and initializes a scalar type gfc_ss struct.  */
     338              : 
     339              : gfc_ss *
     340        68028 : gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
     341              : {
     342        68028 :   gfc_ss *ss;
     343        68028 :   gfc_ss_info *ss_info;
     344              : 
     345        68028 :   ss_info = gfc_get_ss_info ();
     346        68028 :   ss_info->refcount++;
     347        68028 :   ss_info->type = GFC_SS_SCALAR;
     348        68028 :   ss_info->expr = expr;
     349              : 
     350        68028 :   ss = gfc_get_ss ();
     351        68028 :   ss->info = ss_info;
     352        68028 :   ss->next = next;
     353              : 
     354        68028 :   return ss;
     355              : }
     356              : 
     357              : 
     358              : /* Free all the SS associated with a loop.  */
     359              : 
     360              : void
     361       189218 : gfc_cleanup_loop (gfc_loopinfo * loop)
     362              : {
     363       189218 :   gfc_loopinfo *loop_next, **ploop;
     364       189218 :   gfc_ss *ss;
     365       189218 :   gfc_ss *next;
     366              : 
     367       189218 :   ss = loop->ss;
     368       501901 :   while (ss != gfc_ss_terminator)
     369              :     {
     370       312683 :       gcc_assert (ss != NULL);
     371       312683 :       next = ss->loop_chain;
     372       312683 :       gfc_free_ss (ss);
     373       312683 :       ss = next;
     374              :     }
     375              : 
     376              :   /* Remove reference to self in the parent loop.  */
     377       189218 :   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       192582 :   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       189218 : }
     393              : 
     394              : 
     395              : static void
     396       257368 : set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
     397              : {
     398       257368 :   int n;
     399              : 
     400       579298 :   for (; ss != gfc_ss_terminator; ss = ss->next)
     401              :     {
     402       321930 :       ss->loop = loop;
     403              : 
     404       321930 :       if (ss->info->type == GFC_SS_SCALAR
     405              :           || ss->info->type == GFC_SS_REFERENCE
     406       272043 :           || ss->info->type == GFC_SS_TEMP)
     407        61744 :         continue;
     408              : 
     409      4162976 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
     410      3902790 :         if (ss->info->data.array.subscript[n] != NULL)
     411         7629 :           set_ss_loop (ss->info->data.array.subscript[n], loop);
     412              :     }
     413       257368 : }
     414              : 
     415              : 
     416              : /* Associate a SS chain with a loop.  */
     417              : 
     418              : void
     419       249739 : gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
     420              : {
     421       249739 :   gfc_ss *ss;
     422       249739 :   gfc_loopinfo *nested_loop;
     423              : 
     424       249739 :   if (head == gfc_ss_terminator)
     425              :     return;
     426              : 
     427       249739 :   set_ss_loop (head, loop);
     428              : 
     429       249739 :   ss = head;
     430       813779 :   for (; ss && ss != gfc_ss_terminator; ss = ss->next)
     431              :     {
     432       314301 :       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       314301 :       if (ss->next == gfc_ss_terminator)
     453       249739 :         ss->loop_chain = loop->ss;
     454              :       else
     455              :         ss->loop_chain = ss->next;
     456              :     }
     457       249739 :   gcc_assert (ss == gfc_ss_terminator);
     458       249739 :   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       556239 : is_pointer_array (tree expr)
     467              : {
     468       556239 :   if (expr == NULL_TREE
     469       556239 :       || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
     470       683083 :       || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
     471              :     return false;
     472              : 
     473       126844 :   if (VAR_P (expr)
     474       126844 :       && GFC_DECL_PTR_ARRAY_P (expr))
     475              :     return true;
     476              : 
     477       119333 :   if (TREE_CODE (expr) == PARM_DECL
     478       119333 :       && GFC_DECL_PTR_ARRAY_P (expr))
     479              :     return true;
     480              : 
     481       119333 :   if (INDIRECT_REF_P (expr)
     482       119333 :       && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
     483              :     return true;
     484              : 
     485              :   /* The field declaration is marked as a pointer array.  */
     486       116776 :   if (TREE_CODE (expr) == COMPONENT_REF
     487       116776 :       && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1)))
     488         3927 :     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       556239 : is_span_addressed_array (tree expr)
     501              : {
     502       556239 :   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        13995 :       if (TREE_CODE (expr) == COMPONENT_REF
     507         3927 :           && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
     508        14233 :           && TYPE_LANG_SPECIFIC (TREE_TYPE (expr)))
     509              :         {
     510          238 :           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        13995 :       return true;
     526              :     }
     527              : 
     528       542244 :   if (VAR_P (expr)
     529       472983 :       && GFC_DECL_PTR_ARRAY_P (expr)
     530          318 :       && !GFC_DECL_CLASS (expr)
     531          318 :       && GFC_ARRAY_TYPE_P (TREE_TYPE (expr))
     532          318 :       && DECL_LANG_SPECIFIC (expr)
     533       542562 :       && GFC_DECL_SAVED_DESCRIPTOR (expr))
     534          318 :     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        14254 : gfc_span_folds_into_stride (gfc_symbol *sym)
     547              : {
     548        14254 :   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         7299 :   if (sym->ts.type != BT_INTEGER
     554         5711 :       && sym->ts.type != BT_REAL
     555         2247 :       && 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       606528 : gfc_is_span_addressed_dummy (gfc_symbol *sym)
     578              : {
     579       606528 :   return gfc_dummy_requires_direct_arg (sym)
     580       606528 :          && !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       384403 : get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
     624              :               tree *desc, gfc_array_ref *ar)
     625              : {
     626       384403 :   tree tmp;
     627              : 
     628       384403 :   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         1056 : 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         1056 :   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         1056 :   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          587 :     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        60054 : get_main_decl (tree expr)
     678              : {
     679        60054 :   tree decl = expr;
     680        62093 :   while (decl && !DECL_P (decl))
     681              :     {
     682         2729 :       if (TREE_CODE (decl) == ADDR_EXPR
     683              :           || INDIRECT_REF_P (decl)
     684              :           || CONVERT_EXPR_P (decl))
     685         2039 :         decl = TREE_OPERAND (decl, 0);
     686              :       else
     687              :         decl = NULL_TREE;
     688              :     }
     689              : 
     690        60054 :   return decl;
     691              : }
     692              : 
     693              : 
     694              : /* Return the span of an array.  */
     695              : 
     696              : tree
     697        60054 : gfc_get_array_span (tree desc, gfc_expr *expr)
     698              : {
     699        60054 :   tree tmp;
     700        60054 :   gfc_symbol *sym = (expr && expr->expr_type == EXPR_VARIABLE) ?
     701        52548 :                     expr->symtree->n.sym : NULL;
     702              : 
     703        60054 :   tree decl = get_main_decl (desc);
     704              : 
     705        60744 :   if (tree span = GFC_DECL_GET_SPAN (decl ? decl : desc))
     706              :     /* A span addressed dummy loaded its span on entry.  */
     707              :     tmp = span;
     708        59802 :   else if (is_span_addressed_array (desc)
     709        59802 :            || (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        59164 :   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        59037 :   else if (TREE_CODE (desc) == COMPONENT_REF
     724          539 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
     725        59142 :            && 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        59005 :   else if (sym && sym->ts.type == BT_CLASS
     730         1080 :            && expr->ref->type == REF_COMPONENT
     731         1080 :            && expr->ref->next->type == REF_ARRAY
     732         1080 :            && expr->ref->next->next == NULL
     733         1062 :            && CLASS_DATA (sym)->attr.dimension)
     734              :     /* Having escaped the above, this can only be a class array dummy.  */
     735         1024 :     tmp = class_array_element_size (sym->backend_decl,
     736         1024 :                                     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        57981 :       tmp = gfc_get_element_type (TREE_TYPE (desc));
     743        57981 :       if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
     744              :         {
     745        11029 :           gcc_assert (expr->ts.type == BT_CHARACTER);
     746              : 
     747        11029 :           tmp = gfc_get_character_len_in_bytes (tmp);
     748              : 
     749        11029 :           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        21990 :           tmp = (tmp && !integer_zerop (tmp))
     761        21990 :             ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
     762              :         }
     763              :       else
     764        46952 :         tmp = fold_convert (gfc_array_index_type,
     765              :                             size_in_bytes (tmp));
     766              :     }
     767        60054 :   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         8967 : gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
     793              :                                      gfc_se * se, gfc_array_spec * as)
     794              : {
     795         8967 :   int n, dim, total_dim;
     796         8967 :   gfc_se tmpse;
     797         8967 :   gfc_ss *ss;
     798         8967 :   tree lower;
     799         8967 :   tree upper;
     800         8967 :   tree tmp;
     801              : 
     802         8967 :   total_dim = 0;
     803              : 
     804         8967 :   if (!as || as->type != AS_EXPLICIT)
     805         7794 :     return;
     806              : 
     807         2371 :   for (ss = se->ss; ss; ss = ss->parent)
     808              :     {
     809         1198 :       total_dim += ss->loop->dimen;
     810         2751 :       for (n = 0; n < ss->loop->dimen; n++)
     811              :         {
     812              :           /* The bound is known, nothing to do.  */
     813         1553 :           if (ss->loop->to[n] != NULL_TREE)
     814          485 :             continue;
     815              : 
     816         1068 :           dim = ss->dim[n];
     817         1068 :           gcc_assert (dim < as->rank);
     818         1068 :           gcc_assert (ss->loop->dimen <= as->rank);
     819              : 
     820              :           /* Evaluate the lower bound.  */
     821         1068 :           gfc_init_se (&tmpse, NULL);
     822         1068 :           gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
     823         1068 :           gfc_add_block_to_block (&se->pre, &tmpse.pre);
     824         1068 :           gfc_add_block_to_block (&se->post, &tmpse.post);
     825         1068 :           lower = fold_convert (gfc_array_index_type, tmpse.expr);
     826              : 
     827              :           /* ...and the upper bound.  */
     828         1068 :           gfc_init_se (&tmpse, NULL);
     829         1068 :           gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
     830         1068 :           gfc_add_block_to_block (&se->pre, &tmpse.pre);
     831         1068 :           gfc_add_block_to_block (&se->post, &tmpse.post);
     832         1068 :           upper = fold_convert (gfc_array_index_type, tmpse.expr);
     833              : 
     834              :           /* Set the upper bound of the loop to UPPER - LOWER.  */
     835         1068 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
     836              :                                  gfc_array_index_type, upper, lower);
     837         1068 :           tmp = gfc_evaluate_now (tmp, &se->pre);
     838         1068 :           ss->loop->to[n] = tmp;
     839              :         }
     840              :     }
     841              : 
     842         1173 :   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        28972 : 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        28972 :   tree tmp;
     864        28972 :   tree desc;
     865        28972 :   bool onstack;
     866              : 
     867        28972 :   desc = info->descriptor;
     868        28972 :   info->offset = gfc_index_zero_node;
     869        28972 :   if (size == NULL_TREE || (dynamic && integer_zerop (size)))
     870              :     {
     871              :       /* A callee allocated array.  */
     872         3057 :       gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
     873         3057 :       onstack = false;
     874              :     }
     875              :   else
     876              :     {
     877              :       /* Allocate the temporary.  */
     878        51830 :       onstack = !dynamic && initial == NULL_TREE
     879        25915 :                          && (flag_stack_arrays
     880        25530 :                              || gfc_can_put_var_on_stack (size));
     881              : 
     882         5543 :       if (onstack)
     883              :         {
     884              :           /* Make a temporary variable to hold the data.  */
     885        20372 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
     886              :                                  nelem, gfc_index_one_node);
     887        20372 :           tmp = gfc_evaluate_now (tmp, pre);
     888        20372 :           tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
     889              :                                   tmp);
     890        20372 :           tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
     891              :                                   tmp);
     892        20372 :           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        20372 :           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        20372 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
     901        20372 :           gfc_conv_descriptor_data_set (pre, desc, tmp);
     902              :         }
     903              :       else
     904              :         {
     905              :           /* Allocate memory to hold the data or call internal_pack.  */
     906         5543 :           if (initial == NULL_TREE)
     907              :             {
     908         5400 :               tmp = gfc_call_malloc (pre, NULL, size);
     909         5400 :               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         5543 :           gfc_conv_descriptor_data_set (pre, desc, tmp);
     956              :         }
     957              :     }
     958        28972 :   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        28972 :   gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
     963              : 
     964        28972 :   if (dealloc && !onstack)
     965              :     {
     966              :       /* Free the temporary.  */
     967         8182 :       tmp = gfc_conv_descriptor_data_get (desc);
     968         8182 :       tmp = gfc_call_free (tmp);
     969         8182 :       gfc_add_expr_to_block (post, tmp);
     970              :     }
     971        28972 : }
     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       268268 : get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
     990              : {
     991       268268 :   int array_ref_dim;
     992       268268 :   int n;
     993              : 
     994       268268 :   array_ref_dim = 0;
     995              : 
     996       542669 :   for (; ss; ss = ss->parent)
     997       703234 :     for (n = 0; n < ss->dimen; n++)
     998       428833 :       if (ss->dim[n] < array_dim)
     999        77375 :         array_ref_dim++;
    1000              : 
    1001       268268 :   return array_ref_dim;
    1002              : }
    1003              : 
    1004              : 
    1005              : static gfc_ss *
    1006       226451 : innermost_ss (gfc_ss *ss)
    1007              : {
    1008       418702 :   while (ss->nested_ss != NULL)
    1009              :     ss = ss->nested_ss;
    1010              : 
    1011       410494 :   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       226451 : get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
    1024              : {
    1025       226451 :   return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
    1026       226451 :                                            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          363 : get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype,
    1036              :                         gfc_ss **fcnss)
    1037              : {
    1038          363 :   gfc_ss *loop_ss = ss->loop->ss;
    1039          363 :   gfc_ss *lhs_ss;
    1040          363 :   gfc_ss *rhs_ss;
    1041          363 :   gfc_ss *fcn_ss = NULL;
    1042          363 :   tree tmp;
    1043          363 :   tree tmp2;
    1044          363 :   tree vptr;
    1045          363 :   tree class_expr = NULL_TREE;
    1046          363 :   tree lhs_class_expr = NULL_TREE;
    1047          363 :   bool unlimited_rhs = false;
    1048          363 :   bool unlimited_lhs = false;
    1049          363 :   bool rhs_function = false;
    1050          363 :   bool unlimited_arg1 = false;
    1051          363 :   gfc_symbol *vtab;
    1052          363 :   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          363 :   rhs_ss = loop_ss->loop_chain;
    1057              : 
    1058          363 :   if (rhs_ss != gfc_ss_terminator
    1059          339 :       && rhs_ss->info
    1060          339 :       && rhs_ss->info->expr
    1061          339 :       && rhs_ss->info->expr->ts.type == BT_CLASS
    1062          218 :       && rhs_ss->info->data.array.descriptor)
    1063              :     {
    1064          206 :       if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
    1065           56 :         class_expr
    1066           56 :           = 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          206 :       unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
    1070          206 :       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          363 :   *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          363 :   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          363 :   lhs_ss = rhs_ss->loop_chain;
    1096          363 :   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          138 :   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        28972 : 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        28972 :   gfc_loopinfo *loop;
    1201        28972 :   gfc_ss *s;
    1202        28972 :   gfc_array_info *info;
    1203        28972 :   tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
    1204        28972 :   tree type;
    1205        28972 :   tree desc;
    1206        28972 :   tree tmp;
    1207        28972 :   tree size;
    1208        28972 :   tree nelem;
    1209        28972 :   tree cond;
    1210        28972 :   tree or_expr;
    1211        28972 :   tree elemsize;
    1212        28972 :   tree class_expr = NULL_TREE;
    1213        28972 :   gfc_ss *fcn_ss = NULL;
    1214        28972 :   int n, dim, tmp_dim;
    1215        28972 :   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        28972 :   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        28972 :   if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
    1233          363 :     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        28972 :   if (eltype && GFC_CLASS_TYPE_P (eltype))
    1237          235 :     eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
    1238              : 
    1239        28972 :   if (class_expr == NULL_TREE)
    1240        28785 :     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        28972 :   memset (from, 0, sizeof (from));
    1264        28972 :   memset (to, 0, sizeof (to));
    1265              : 
    1266        28972 :   info = &ss->info->data.array;
    1267              : 
    1268        28972 :   gcc_assert (ss->dimen > 0);
    1269        28972 :   gcc_assert (ss->loop->dimen == ss->dimen);
    1270              : 
    1271        28972 :   if (warn_array_temporaries && where)
    1272          207 :     gfc_warning (OPT_Warray_temporaries,
    1273              :                  "Creating array temporary at %L", where);
    1274              : 
    1275              :   /* Set the lower bound to zero.  */
    1276        57979 :   for (s = ss; s; s = s->parent)
    1277              :     {
    1278        29007 :       loop = s->loop;
    1279              : 
    1280        29007 :       total_dim += loop->dimen;
    1281        67280 :       for (n = 0; n < loop->dimen; n++)
    1282              :         {
    1283        38273 :           dim = s->dim[n];
    1284              : 
    1285              :           /* Callee allocated arrays may not have a known bound yet.  */
    1286        38273 :           if (loop->to[n])
    1287        34734 :             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        38273 :           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        38273 :           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        38273 :           tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
    1306        38273 :           from[tmp_dim] = loop->from[n];
    1307        38273 :           to[tmp_dim] = loop->to[n];
    1308              : 
    1309        38273 :           info->delta[dim] = gfc_index_zero_node;
    1310        38273 :           info->start[dim] = gfc_index_zero_node;
    1311        38273 :           info->end[dim] = gfc_index_zero_node;
    1312        38273 :           info->stride[dim] = gfc_index_one_node;
    1313              :         }
    1314              :     }
    1315              : 
    1316              :   /* Initialize the descriptor.  */
    1317        28972 :   type =
    1318        28972 :     gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
    1319              :                                GFC_ARRAY_UNKNOWN, true);
    1320        28972 :   desc = gfc_create_var (type, "atmp");
    1321        28972 :   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        28972 :   tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
    1327        28972 :   if (! TYPE_NAME (arraytype))
    1328        28972 :     TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
    1329              :                                         NULL_TREE, arraytype);
    1330        28972 :   gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
    1331        28972 :                                       arraytype, TYPE_NAME (arraytype)));
    1332              : 
    1333        28972 :   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        28972 :   if (class_expr != NULL_TREE
    1340        28785 :       || (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        28755 :       gfc_conv_descriptor_dtype_set (pre, desc,
    1424        28755 :                                      gfc_get_dtype (TREE_TYPE (desc)));
    1425              :     }
    1426              : 
    1427        28972 :   info->descriptor = desc;
    1428        28972 :   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        28972 :   or_expr = NULL_TREE;
    1444              : 
    1445              :   /* If there is at least one null loop->to[n], it is a callee allocated
    1446              :      array.  */
    1447        63706 :   for (n = 0; n < total_dim; n++)
    1448        36919 :     if (to[n] == NULL_TREE)
    1449              :       {
    1450              :         size = NULL_TREE;
    1451              :         break;
    1452              :       }
    1453              : 
    1454        28972 :   if (size == NULL_TREE)
    1455         4380 :     for (s = ss; s; s = s->parent)
    1456         5739 :       for (n = 0; n < s->loop->dimen; n++)
    1457              :         {
    1458         3544 :           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         3544 :           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         3544 :           s->loop->to[n] = tmp;
    1467              :         }
    1468              :   else
    1469              :     {
    1470        61516 :       for (n = 0; n < total_dim; n++)
    1471              :         {
    1472              :           /* Store the stride and bound components in the descriptor.  */
    1473        34729 :           gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
    1474              : 
    1475        34729 :           gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
    1476              :                                           gfc_index_zero_node);
    1477              : 
    1478        34729 :           gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
    1479              : 
    1480        34729 :           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        34729 :           cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    1486              :                                   tmp, gfc_index_zero_node);
    1487        34729 :           cond = gfc_evaluate_now (cond, pre);
    1488              : 
    1489        34729 :           if (n == 0)
    1490              :             or_expr = cond;
    1491              :           else
    1492         7942 :             or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    1493              :                                        logical_type_node, or_expr, cond);
    1494              : 
    1495        34729 :           size = fold_build2_loc (input_location, MULT_EXPR,
    1496              :                                   gfc_array_index_type, size, tmp);
    1497        34729 :           size = gfc_evaluate_now (size, pre);
    1498              :         }
    1499              :     }
    1500              : 
    1501              :   /* Get the size of the array.  */
    1502        28972 :   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        26561 :       size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    1507              :                               or_expr, gfc_index_zero_node, size);
    1508              : 
    1509        26561 :       nelem = size;
    1510        26561 :       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        28972 :   tmp = fold_convert (gfc_array_index_type, elemsize);
    1521        28972 :   gfc_conv_descriptor_span_set (pre, desc, tmp);
    1522              : 
    1523        28972 :   gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
    1524              :                                     dynamic, dealloc);
    1525              : 
    1526        57979 :   while (ss->parent)
    1527              :     ss = ss->parent;
    1528              : 
    1529        28972 :   if (ss->dimen > ss->loop->temp_dim)
    1530        25104 :     ss->loop->temp_dim = ss->dimen;
    1531              : 
    1532        28972 :   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         1078 : gfc_get_iteration_count (tree start, tree end, tree step)
    1541              : {
    1542         1078 :   tree tmp;
    1543         1078 :   tree type;
    1544              : 
    1545         1078 :   type = TREE_TYPE (step);
    1546         1078 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
    1547         1078 :   tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
    1548         1078 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
    1549              :                          build_int_cst (type, 1));
    1550         1078 :   tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
    1551              :                          build_int_cst (type, 0));
    1552         1078 :   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         2423 : gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
    1561              : {
    1562         2423 :   return (i->start->expr_type != EXPR_CONSTANT
    1563         2005 :           || i->end->expr_type != EXPR_CONSTANT
    1564         2596 :           || 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         3290 : gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
    1575              : {
    1576         3290 :   if (expr->expr_type == EXPR_ARRAY)
    1577          685 :     return gfc_get_array_constructor_size (size, expr->value.constructor);
    1578         2605 :   else if (expr->rank > 0)
    1579              :     {
    1580              :       /* Calculate everything at run time.  */
    1581         1031 :       mpz_set_ui (*size, 0);
    1582         1031 :       return true;
    1583              :     }
    1584              :   else
    1585              :     {
    1586              :       /* A single element.  */
    1587         1574 :       mpz_set_ui (*size, 1);
    1588         1574 :       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         3090 : gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
    1598              : {
    1599         3090 :   gfc_constructor *c;
    1600         3090 :   gfc_iterator *i;
    1601         3090 :   mpz_t val;
    1602         3090 :   mpz_t len;
    1603         3090 :   bool dynamic;
    1604              : 
    1605         3090 :   mpz_set_ui (*size, 0);
    1606         3090 :   mpz_init (len);
    1607         3090 :   mpz_init (val);
    1608              : 
    1609         3090 :   dynamic = false;
    1610         7528 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1611              :     {
    1612         4438 :       i = c->iterator;
    1613         4438 :       if (i && gfc_iterator_has_dynamic_bounds (i))
    1614              :         dynamic = true;
    1615              :       else
    1616              :         {
    1617         2739 :           dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
    1618         2739 :           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         2739 :           mpz_add (*size, *size, len);
    1631              :         }
    1632              :     }
    1633         3090 :   mpz_clear (len);
    1634         3090 :   mpz_clear (val);
    1635         3090 :   return dynamic;
    1636              : }
    1637              : 
    1638              : 
    1639              : /* Make sure offset is a variable.  */
    1640              : 
    1641              : static void
    1642         3413 : 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         3413 :   gcc_assert (*offsetvar != NULL_TREE);
    1648         3413 :   gfc_add_modify (pblock, *offsetvar, *poffset);
    1649         3413 :   *poffset = *offsetvar;
    1650         3413 :   TREE_USED (*offsetvar) = 1;
    1651         3413 : }
    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         4893 : has_class_alloc_comp (gfc_symbol *der)
    1666              : {
    1667        13177 :   for (gfc_component *c = der->components; c; c = c->next)
    1668         8350 :     if (c->ts.type == BT_CLASS && !c->attr.class_pointer)
    1669              :       return true;
    1670              :   return false;
    1671              : }
    1672              : 
    1673              : static void
    1674        13181 : gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
    1675              :                               tree offset, gfc_se * se, gfc_expr * expr)
    1676              : {
    1677        13181 :   tree tmp, offset_eval;
    1678              : 
    1679        13181 :   gfc_conv_expr (se, expr);
    1680              : 
    1681              :   /* Store the value.  */
    1682        13181 :   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        13181 :   offset_eval = gfc_evaluate_now (offset, &se->pre);
    1687        13181 :   tmp = gfc_build_array_ref (tmp, offset_eval, NULL);
    1688              : 
    1689        13181 :   if (expr->ts.type == BT_DERIVED
    1690         4814 :       && (expr->expr_type == EXPR_FUNCTION
    1691         4730 :           || (expr->expr_type == EXPR_STRUCTURE
    1692         4114 :               && !has_class_alloc_comp (expr->ts.u.derived)))
    1693        17337 :       && 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        13181 :   if (expr->ts.type == BT_CHARACTER)
    1700              :     {
    1701         2154 :       int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
    1702         2154 :       tree esize;
    1703              : 
    1704         2154 :       esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
    1705         2154 :       esize = fold_convert (gfc_charlen_type_node, esize);
    1706         4308 :       esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    1707         2154 :                                TREE_TYPE (esize), esize,
    1708         2154 :                                build_int_cst (TREE_TYPE (esize),
    1709         2154 :                                           gfc_character_kinds[i].bit_size / 8));
    1710              : 
    1711         2154 :       gfc_conv_string_parameter (se);
    1712         2154 :       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         2148 :           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         2148 :           gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
    1725              :                                  se->string_length, se->expr, expr->ts.kind);
    1726              :         }
    1727         2154 :       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        11027 :   else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
    1754        11027 :            && !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           24 :       if (expr->expr_type == EXPR_FUNCTION)
    1758           18 :         se->expr = gfc_evaluate_now (se->expr, pblock);
    1759           24 :       se->expr = gfc_class_data_get (se->expr);
    1760           24 :       se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    1761           24 :       se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1762           24 :       gfc_add_modify (&se->pre, tmp, se->expr);
    1763              :     }
    1764              :   else
    1765              :     {
    1766              :       /* TODO: Should the frontend already have done this conversion?  */
    1767        11003 :       se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1768        11003 :       gfc_add_modify (&se->pre, tmp, se->expr);
    1769              :     }
    1770              : 
    1771        13181 :   gfc_add_block_to_block (pblock, &se->pre);
    1772        13181 :   gfc_add_block_to_block (pblock, &se->post);
    1773        13181 : }
    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         1153 : 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         1153 :   gfc_se se;
    1787         1153 :   gfc_ss *ss;
    1788         1153 :   gfc_loopinfo loop;
    1789         1153 :   stmtblock_t body;
    1790         1153 :   tree tmp;
    1791         1153 :   tree size;
    1792         1153 :   int n;
    1793              : 
    1794              :   /* We need this to be a variable so we can increment it.  */
    1795         1153 :   gfc_put_offset_into_var (pblock, poffset, offsetvar);
    1796              : 
    1797         1153 :   gfc_init_se (&se, NULL);
    1798              : 
    1799              :   /* Walk the array expression.  */
    1800         1153 :   ss = gfc_walk_expr (expr);
    1801         1153 :   gcc_assert (ss != gfc_ss_terminator);
    1802              : 
    1803              :   /* Initialize the scalarizer.  */
    1804         1153 :   gfc_init_loopinfo (&loop);
    1805         1153 :   gfc_add_ss_to_loop (&loop, ss);
    1806              : 
    1807              :   /* Initialize the loop.  */
    1808         1153 :   gfc_conv_ss_startstride (&loop);
    1809         1153 :   gfc_conv_loop_setup (&loop, &expr->where);
    1810              : 
    1811              :   /* Make sure the constructed array has room for the new data.  */
    1812         1153 :   if (dynamic)
    1813              :     {
    1814              :       /* Set SIZE to the total number of elements in the subarray.  */
    1815          515 :       size = gfc_index_one_node;
    1816         1042 :       for (n = 0; n < loop.dimen; n++)
    1817              :         {
    1818          527 :           tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
    1819              :                                          gfc_index_one_node);
    1820          527 :           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          515 :       gfc_grow_array (&loop.pre, desc, size);
    1826              :     }
    1827              : 
    1828              :   /* Make the loop body.  */
    1829         1153 :   gfc_mark_ss_chain_used (ss, 1);
    1830         1153 :   gfc_start_scalarized_body (&loop, &body);
    1831         1153 :   gfc_copy_loopinfo_to_se (&se, &loop);
    1832         1153 :   se.ss = ss;
    1833              : 
    1834         1153 :   gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
    1835         1153 :   gcc_assert (se.ss == gfc_ss_terminator);
    1836              : 
    1837              :   /* Increment the offset.  */
    1838         1153 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    1839              :                          *poffset, gfc_index_one_node);
    1840         1153 :   gfc_add_modify (&body, *poffset, tmp);
    1841              : 
    1842              :   /* Finish the loop.  */
    1843         1153 :   gfc_trans_scalarizing_loops (&loop, &body);
    1844         1153 :   gfc_add_block_to_block (&loop.pre, &loop.post);
    1845         1153 :   tmp = gfc_finish_block (&loop.pre);
    1846         1153 :   gfc_add_expr_to_block (pblock, tmp);
    1847              : 
    1848         1153 :   gfc_cleanup_loop (&loop);
    1849         1153 : }
    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         8630 : 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         8630 :   tree tmp;
    1906         8630 :   tree start = NULL_TREE;
    1907         8630 :   tree end = NULL_TREE;
    1908         8630 :   tree step = NULL_TREE;
    1909         8630 :   stmtblock_t body;
    1910         8630 :   gfc_se se;
    1911         8630 :   mpz_t size;
    1912         8630 :   gfc_constructor *c;
    1913         8630 :   gfc_typespec ts;
    1914         8630 :   int ctr = 0;
    1915              : 
    1916         8630 :   tree shadow_loopvar = NULL_TREE;
    1917         8630 :   gfc_saved_var saved_loopvar;
    1918              : 
    1919         8630 :   ts.type = BT_UNKNOWN;
    1920         8630 :   mpz_init (size);
    1921        23487 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1922              :     {
    1923        14857 :       ctr++;
    1924              :       /* If this is an iterator or an array, the offset must be a variable.  */
    1925        14857 :       if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
    1926         2260 :         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        14857 :       if (c->iterator)
    1934              :         {
    1935         1549 :           gfc_symbol *sym;
    1936         1549 :           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         1549 :           gfc_init_se (&se, NULL);
    1943         1549 :           gfc_conv_expr_val (&se, c->iterator->start);
    1944         1549 :           gfc_add_block_to_block (pblock, &se.pre);
    1945         1549 :           start = gfc_evaluate_now (se.expr, pblock);
    1946              : 
    1947         1549 :           gfc_init_se (&se, NULL);
    1948         1549 :           gfc_conv_expr_val (&se, c->iterator->end);
    1949         1549 :           gfc_add_block_to_block (pblock, &se.pre);
    1950         1549 :           end = gfc_evaluate_now (se.expr, pblock);
    1951              : 
    1952         1549 :           gfc_init_se (&se, NULL);
    1953         1549 :           gfc_conv_expr_val (&se, c->iterator->step);
    1954         1549 :           gfc_add_block_to_block (pblock, &se.pre);
    1955         1549 :           step = gfc_evaluate_now (se.expr, pblock);
    1956              : 
    1957         1549 :           sym = c->iterator->var->symtree->n.sym;
    1958         1549 :           type = gfc_typenode_for_spec (&sym->ts);
    1959              : 
    1960         1549 :           shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
    1961         1549 :           gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
    1962              :         }
    1963              : 
    1964        14857 :       gfc_start_block (&body);
    1965              : 
    1966        14857 :       if (c->expr->expr_type == EXPR_ARRAY)
    1967              :         {
    1968              :           /* Array constructors can be nested.  */
    1969         1571 :           gfc_trans_array_constructor_value (&body, finalblock, type,
    1970              :                                              desc, c->expr->value.constructor,
    1971              :                                              poffset, offsetvar, dynamic,
    1972              :                                              owned_sweep);
    1973              :         }
    1974        13286 :       else if (c->expr->rank > 0)
    1975              :         {
    1976         1153 :           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        13941 :           while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
    1989              :             {
    1990         1808 :               p = gfc_constructor_next (p);
    1991         1808 :               n++;
    1992              :             }
    1993              :           /* Constructor with few constant elements, or element size not
    1994              :              known at compile time (e.g. deferred-length character).  */
    1995        12133 :           if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type)))
    1996              :             {
    1997              :               /* Scalar values.  */
    1998        12028 :               gfc_init_se (&se, NULL);
    1999        12028 :               if (IS_PDT (c->expr) && c->expr->expr_type == EXPR_STRUCTURE)
    2000          288 :                 c->expr->must_finalize = 1;
    2001              : 
    2002        12028 :               gfc_trans_array_ctor_element (&body, desc, *poffset,
    2003              :                                             &se, c->expr);
    2004              : 
    2005        12028 :               *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        12028 :               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        12133 :           if (!INTEGER_CST_P (*poffset))
    2086              :             {
    2087         1851 :               gfc_add_modify (&body, *offsetvar, *poffset);
    2088         1851 :               *poffset = *offsetvar;
    2089              :             }
    2090              : 
    2091        12133 :           if (!c->iterator)
    2092        12133 :             ts = c->expr->ts;
    2093              :         }
    2094              : 
    2095              :       /* The frontend should already have done any expansions
    2096              :          at compile-time.  */
    2097        14857 :       if (!c->iterator)
    2098              :         {
    2099              :           /* Pass the code as is.  */
    2100        13308 :           tmp = gfc_finish_block (&body);
    2101        13308 :           gfc_add_expr_to_block (pblock, tmp);
    2102              :         }
    2103              :       else
    2104              :         {
    2105              :           /* Build the implied do-loop.  */
    2106         1549 :           stmtblock_t implied_do_block;
    2107         1549 :           tree cond;
    2108         1549 :           tree exit_label;
    2109         1549 :           tree loopbody;
    2110         1549 :           tree tmp2;
    2111              : 
    2112         1549 :           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         1549 :           gfc_start_block(&implied_do_block);
    2117              : 
    2118              :           /* Initialize the loop.  */
    2119         1549 :           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         1549 :           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         1549 :           exit_label = gfc_build_label_decl (NULL_TREE);
    2141         1549 :           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         1549 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2147         1549 :                                  step, build_int_cst (TREE_TYPE (step), 0));
    2148         1549 :           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         1549 :           tmp = build1_v (GOTO_EXPR, exit_label);
    2155         1549 :           TREE_USED (exit_label) = 1;
    2156         1549 :           tmp = build3_v (COND_EXPR, cond, tmp,
    2157              :                           build_empty_stmt (input_location));
    2158         1549 :           gfc_add_expr_to_block (&body, tmp);
    2159              : 
    2160              :           /* The main loop body.  */
    2161         1549 :           gfc_add_expr_to_block (&body, loopbody);
    2162              : 
    2163              :           /* Increase loop variable by step.  */
    2164         1549 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2165         1549 :                                  TREE_TYPE (shadow_loopvar), shadow_loopvar,
    2166              :                                  step);
    2167         1549 :           gfc_add_modify (&body, shadow_loopvar, tmp);
    2168              : 
    2169              :           /* Finish the loop.  */
    2170         1549 :           tmp = gfc_finish_block (&body);
    2171         1549 :           tmp = build1_v (LOOP_EXPR, tmp);
    2172         1549 :           gfc_add_expr_to_block (&implied_do_block, tmp);
    2173              : 
    2174              :           /* Add the exit label.  */
    2175         1549 :           tmp = build1_v (LABEL_EXPR, exit_label);
    2176         1549 :           gfc_add_expr_to_block (&implied_do_block, tmp);
    2177              : 
    2178              :           /* Finish the implied-do loop.  */
    2179         1549 :           tmp = gfc_finish_block(&implied_do_block);
    2180         1549 :           gfc_add_expr_to_block(pblock, tmp);
    2181              : 
    2182         1549 :           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         8630 :   if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
    2194         8630 :       && !(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         8648 :       && 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         8630 :   mpz_clear (size);
    2218         8630 : }
    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         8514 : store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
    2235              : {
    2236         8514 :   if (force_new_cl)
    2237              :     {
    2238         8486 :       gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
    2239         8486 :       *clp = new_cl;
    2240              :     }
    2241         8514 :   (*clp)->backend_decl = len;
    2242         8514 : }
    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          882 : get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
    2291              : {
    2292          882 :   gfc_ref *ref;
    2293          882 :   gfc_typespec *ts;
    2294          882 :   mpz_t char_len;
    2295          882 :   gfc_se se;
    2296              : 
    2297              :   /* Don't bother if we already know the length is a constant.  */
    2298          882 :   if (*len && INTEGER_CST_P (*len))
    2299          551 :     return;
    2300              : 
    2301          395 :   ts = &expr->symtree->n.sym->ts;
    2302          626 :   for (ref = expr->ref; ref; ref = ref->next)
    2303              :     {
    2304          295 :       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           64 :         case REF_SUBSTRING:
    2318           64 :           if (ref->u.ss.end == NULL
    2319           52 :               || ref->u.ss.start->expr_type != EXPR_CONSTANT
    2320           52 :               || 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           19 :           mpz_init_set_ui (char_len, 1);
    2327           19 :           mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
    2328           19 :           mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
    2329           19 :           *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
    2330           19 :           mpz_clear (char_len);
    2331           19 :           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         8873 : get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
    2366              : {
    2367         8873 :   gfc_constructor *c;
    2368         8873 :   bool is_const;
    2369              : 
    2370         8873 :   is_const = true;
    2371              : 
    2372         8873 :   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         8600 :   for (c = gfc_constructor_first (base);
    2383        47157 :        c && is_const; c = gfc_constructor_next (c))
    2384              :     {
    2385        38557 :       switch (c->expr->expr_type)
    2386              :         {
    2387        37436 :         case EXPR_CONSTANT:
    2388        37436 :           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        38557 :             is_const = false;
    2396              :           break;
    2397              : 
    2398          942 :         case EXPR_VARIABLE:
    2399          942 :           is_const = false;
    2400          942 :           if (len)
    2401          882 :             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        38557 :       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        61601 : gfc_constant_array_constructor_p (gfc_constructor_base base)
    2424              : {
    2425        61601 :   unsigned HOST_WIDE_INT nelem = 0;
    2426              : 
    2427        61601 :   gfc_constructor *c = gfc_constructor_first (base);
    2428       552021 :   while (c)
    2429              :     {
    2430       436273 :       if (c->iterator
    2431       434608 :           || c->expr->rank > 0
    2432       433786 :           || c->expr->expr_type != EXPR_CONSTANT)
    2433              :         return 0;
    2434       428819 :       c = gfc_constructor_next (c);
    2435       428819 :       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        43364 : gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
    2447              : {
    2448        43364 :   tree tmptype, init, tmp;
    2449        43364 :   HOST_WIDE_INT nelem;
    2450        43364 :   gfc_constructor *c;
    2451        43364 :   gfc_array_spec as;
    2452        43364 :   gfc_se se;
    2453        43364 :   int i;
    2454        43364 :   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        43364 :   nelem = 0;
    2459        43364 :   c = gfc_constructor_first (expr->value.constructor);
    2460       431301 :   while (c)
    2461              :     {
    2462       344573 :       gfc_init_se (&se, NULL);
    2463       344573 :       gfc_conv_constant (&se, c->expr);
    2464       344573 :       if (c->expr->ts.type != BT_CHARACTER)
    2465       308057 :         se.expr = fold_convert (type, se.expr);
    2466        36516 :       else if (POINTER_TYPE_P (type))
    2467        36516 :         se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
    2468              :                                        se.expr);
    2469       344573 :       CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
    2470              :                               se.expr);
    2471       344573 :       c = gfc_constructor_next (c);
    2472       344573 :       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        43364 :   memset (&as, 0, sizeof (gfc_array_spec));
    2480              : 
    2481        43364 :   as.rank = expr->rank;
    2482        43364 :   as.type = AS_EXPLICIT;
    2483        43364 :   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        93477 :     for (i = 0; i < expr->rank; i++)
    2491              :       {
    2492        50117 :         int tmp = (int) mpz_get_si (expr->shape[i]);
    2493        50117 :         as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
    2494        50117 :         as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
    2495        50117 :                                         NULL, tmp - 1);
    2496              :       }
    2497              : 
    2498        43364 :   tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
    2499              : 
    2500              :   /* as is not needed anymore.  */
    2501       136849 :   for (i = 0; i < as.rank + as.corank; i++)
    2502              :     {
    2503        50121 :       gfc_free_expr (as.lower[i]);
    2504        50121 :       gfc_free_expr (as.upper[i]);
    2505              :     }
    2506              : 
    2507        43364 :   init = build_constructor (tmptype, v);
    2508              : 
    2509        43364 :   TREE_CONSTANT (init) = 1;
    2510        43364 :   TREE_STATIC (init) = 1;
    2511              : 
    2512        43364 :   tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
    2513              :                     tmptype);
    2514        43364 :   DECL_ARTIFICIAL (tmp) = 1;
    2515        43364 :   DECL_IGNORED_P (tmp) = 1;
    2516        43364 :   TREE_STATIC (tmp) = 1;
    2517        43364 :   TREE_CONSTANT (tmp) = 1;
    2518        43364 :   TREE_READONLY (tmp) = 1;
    2519        43364 :   DECL_INITIAL (tmp) = init;
    2520        43364 :   pushdecl (tmp);
    2521              : 
    2522        43364 :   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        37375 : trans_constant_array_constructor (gfc_ss * ss, tree type)
    2533              : {
    2534        37375 :   gfc_array_info *info;
    2535        37375 :   tree tmp;
    2536        37375 :   int i;
    2537              : 
    2538        37375 :   tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
    2539              : 
    2540        37375 :   info = &ss->info->data.array;
    2541              : 
    2542        37375 :   info->descriptor = tmp;
    2543        37375 :   info->data = gfc_build_addr_expr (NULL_TREE, tmp);
    2544        37375 :   info->offset = gfc_index_zero_node;
    2545              : 
    2546        78623 :   for (i = 0; i < ss->dimen; i++)
    2547              :     {
    2548        41248 :       info->delta[i] = gfc_index_zero_node;
    2549        41248 :       info->start[i] = gfc_index_zero_node;
    2550        41248 :       info->end[i] = gfc_index_zero_node;
    2551        41248 :       info->stride[i] = gfc_index_one_node;
    2552              :     }
    2553        37375 : }
    2554              : 
    2555              : 
    2556              : static int
    2557        37382 : get_rank (gfc_loopinfo *loop)
    2558              : {
    2559        37382 :   int rank;
    2560              : 
    2561        37382 :   rank = 0;
    2562       160620 :   for (; loop; loop = loop->parent)
    2563        80316 :     rank += loop->dimen;
    2564              : 
    2565        42922 :   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        37382 : constant_array_constructor_loop_size (gfc_loopinfo * l)
    2576              : {
    2577        37382 :   gfc_loopinfo *loop;
    2578        37382 :   tree size = gfc_index_one_node;
    2579        37382 :   tree tmp;
    2580        37382 :   int i, total_dim;
    2581              : 
    2582        37382 :   total_dim = get_rank (l);
    2583              : 
    2584        74764 :   for (loop = l; loop; loop = loop->parent)
    2585              :     {
    2586        78649 :       for (i = 0; i < loop->dimen; i++)
    2587              :         {
    2588              :           /* If the bounds aren't constant, return NULL_TREE.  */
    2589        41267 :           if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
    2590              :             return NULL_TREE;
    2591        41261 :           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        41261 :             tmp = loop->to[i];
    2602        41261 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2603              :                                  gfc_array_index_type, tmp, gfc_index_one_node);
    2604        41261 :           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        44434 : get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
    2615              : {
    2616        44434 :   gfc_ss *ss;
    2617        44434 :   int n;
    2618              : 
    2619        44434 :   gcc_assert (array->nested_ss == NULL);
    2620              : 
    2621        44434 :   for (ss = array; ss; ss = ss->parent)
    2622        44434 :     for (n = 0; n < ss->loop->dimen; n++)
    2623        44434 :       if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
    2624        44434 :         return &(ss->loop->to[n]);
    2625              : 
    2626            0 :   gcc_unreachable ();
    2627              : }
    2628              : 
    2629              : 
    2630              : static gfc_loopinfo *
    2631       730487 : outermost_loop (gfc_loopinfo * loop)
    2632              : {
    2633       952367 :   while (loop->parent != NULL)
    2634              :     loop = loop->parent;
    2635              : 
    2636       737175 :   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        44434 : trans_array_constructor (gfc_ss * ss, locus * where)
    2646              : {
    2647        44434 :   gfc_constructor_base c;
    2648        44434 :   tree offset;
    2649        44434 :   tree offsetvar;
    2650        44434 :   tree desc;
    2651        44434 :   tree type;
    2652        44434 :   tree tmp;
    2653        44434 :   tree *loop_ubound0;
    2654        44434 :   bool dynamic;
    2655        44434 :   bool old_first_len, old_typespec_chararray_ctor;
    2656        44434 :   tree old_first_len_val;
    2657        44434 :   gfc_loopinfo *loop, *outer_loop;
    2658        44434 :   gfc_ss_info *ss_info;
    2659        44434 :   gfc_expr *expr;
    2660        44434 :   gfc_ss *s;
    2661        44434 :   tree neg_len;
    2662        44434 :   char *msg;
    2663        44434 :   stmtblock_t finalblock;
    2664        44434 :   bool finalize_required;
    2665        44434 :   bool owned_sweep = false;
    2666              : 
    2667              :   /* Save the old values for nested checking.  */
    2668        44434 :   old_first_len = first_len;
    2669        44434 :   old_first_len_val = first_len_val;
    2670        44434 :   old_typespec_chararray_ctor = typespec_chararray_ctor;
    2671              : 
    2672        44434 :   loop = ss->loop;
    2673        44434 :   outer_loop = outermost_loop (loop);
    2674        44434 :   ss_info = ss->info;
    2675        44434 :   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        88868 :   typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
    2680         8274 :                              && expr->ts.u.cl
    2681        52708 :                              && expr->ts.u.cl->length_from_typespec);
    2682              : 
    2683        44434 :   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        44434 :   gcc_assert (ss->dimen == ss->loop->dimen);
    2691              : 
    2692        44434 :   c = expr->value.constructor;
    2693        44434 :   if (expr->ts.type == BT_CHARACTER)
    2694              :     {
    2695         8274 :       bool const_string;
    2696         8274 :       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         8274 :       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         8246 :           const_string = get_array_ctor_strlen (&outer_loop->pre, c,
    2741              :                                                 &ss_info->string_length);
    2742         8246 :           force_new_cl = true;
    2743              : 
    2744              :           /* Initialize "len" with string length for bounds checking.  */
    2745         8246 :           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         8274 :       gcc_assert (ss_info->string_length);
    2765              : 
    2766         8274 :       store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
    2767              : 
    2768         8274 :       type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
    2769         8274 :       if (const_string)
    2770         7316 :         type = build_pointer_type (type);
    2771              :     }
    2772              :   else
    2773        36185 :     type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
    2774           25 :                                   ? &CLASS_DATA (expr)->ts : &expr->ts);
    2775              : 
    2776              :   /* See if the constructor determines the loop bounds.  */
    2777        44434 :   dynamic = false;
    2778              : 
    2779        44434 :   loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
    2780              : 
    2781        87356 :   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        44434 :   if (*loop_ubound0 == NULL_TREE)
    2801              :     {
    2802          893 :       mpz_t size;
    2803              : 
    2804              :       /* We should have a 1-dimensional, zero-based loop.  */
    2805          893 :       gcc_assert (loop->parent == NULL && loop->nested == NULL);
    2806          893 :       gcc_assert (loop->dimen == 1);
    2807          893 :       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          893 :       mpz_init (size);
    2813          893 :       dynamic = gfc_get_array_constructor_size (&size, c);
    2814          893 :       mpz_sub_ui (size, size, 1);
    2815          893 :       loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
    2816          893 :       mpz_clear (size);
    2817              :     }
    2818              : 
    2819              :   /* Special case constant array constructors.  */
    2820          893 :   if (!dynamic)
    2821              :     {
    2822        43566 :       unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
    2823        43566 :       if (nelem > 0)
    2824              :         {
    2825        37382 :           tree size = constant_array_constructor_loop_size (loop);
    2826        37376 :           if (size && compare_tree_int (size, nelem) == 0
    2827        74758 :               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
    2828              :             {
    2829        37375 :               trans_constant_array_constructor (ss, type);
    2830        37375 :               goto finish;
    2831              :             }
    2832              :         }
    2833              :     }
    2834              : 
    2835         7059 :   gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
    2836              :                                NULL_TREE, dynamic, true, false, where);
    2837              : 
    2838         7059 :   desc = ss_info->data.array.descriptor;
    2839         7059 :   offset = gfc_index_zero_node;
    2840         7059 :   offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
    2841         7059 :   suppress_warning (offsetvar);
    2842         7059 :   TREE_USED (offsetvar) = 0;
    2843              : 
    2844         7059 :   gfc_init_block (&finalblock);
    2845         7059 :   finalize_required = expr->must_finalize;
    2846         7059 :   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
    2847              :     finalize_required = true;
    2848              : 
    2849         7059 :   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        14039 :   owned_sweep = finalize_required
    2858          570 :     && expr->ts.type == BT_DERIVED
    2859          570 :     && expr->ts.u.derived->attr.alloc_comp
    2860         7459 :     && gfc_constructor_is_owned_alloc_comp (c, expr->ts.u.derived);
    2861              : 
    2862         7059 :   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         7059 :   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         7059 :   if (dynamic)
    2875              :     {
    2876          868 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2877              :                              gfc_array_index_type,
    2878              :                              offsetvar, gfc_index_one_node);
    2879          868 :       tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
    2880          868 :       if (*loop_ubound0 && VAR_P (*loop_ubound0))
    2881            0 :         gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
    2882              :       else
    2883          868 :         *loop_ubound0 = tmp;
    2884              :     }
    2885              : 
    2886         7059 :   if (TREE_USED (offsetvar))
    2887         2260 :     pushdecl (offsetvar);
    2888              :   else
    2889         4799 :     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         4799 : finish:
    2900              :   /* Restore old values of globals.  */
    2901        44434 :   first_len = old_first_len;
    2902        44434 :   first_len_val = old_first_len_val;
    2903        44434 :   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        44434 :   if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    2910         1813 :        && finalblock.head != NULL_TREE)
    2911          328 :     gfc_prepend_expr_to_block (&loop->post, finalblock.head);
    2912        44434 : }
    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       186886 : set_vector_loop_bounds (gfc_ss * ss)
    2922              : {
    2923       186886 :   gfc_loopinfo *loop, *outer_loop;
    2924       186886 :   gfc_array_info *info;
    2925       186886 :   gfc_se se;
    2926       186886 :   tree tmp;
    2927       186886 :   tree desc;
    2928       186886 :   tree zero;
    2929       186886 :   int n;
    2930       186886 :   int dim;
    2931              : 
    2932       186886 :   outer_loop = outermost_loop (ss->loop);
    2933              : 
    2934       186886 :   info = &ss->info->data.array;
    2935              : 
    2936       378408 :   for (; ss; ss = ss->parent)
    2937              :     {
    2938       191522 :       loop = ss->loop;
    2939              : 
    2940       454868 :       for (n = 0; n < loop->dimen; n++)
    2941              :         {
    2942       263346 :           dim = ss->dim[n];
    2943       263346 :           if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
    2944          986 :               || loop->to[n] != NULL)
    2945       263166 :             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          180 :           gcc_assert (loop->from[n] == gfc_index_zero_node);
    2951          180 :           gcc_assert (info->subscript[dim]
    2952              :                       && info->subscript[dim]->info->type == GFC_SS_VECTOR);
    2953              : 
    2954          180 :           gfc_init_se (&se, NULL);
    2955          180 :           desc = info->subscript[dim]->info->data.array.descriptor;
    2956          180 :           zero = gfc_rank_cst[0];
    2957          180 :           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          180 :           tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
    2962          180 :           loop->to[n] = tmp;
    2963              :         }
    2964              :     }
    2965       186886 : }
    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        46719 : gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
    2973              : {
    2974        46719 :   if (ss_info->type != GFC_SS_REFERENCE)
    2975              :     return false;
    2976              : 
    2977        10390 :   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         9978 :   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         9302 :   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         9426 :       && 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         9278 :   if (!ss_info->data.scalar.needs_temporary
    2998         9278 :       && (ss_info->expr->ts.type == BT_DERIVED
    2999         8230 :           || ss_info->expr->ts.type == BT_CLASS)
    3000        10374 :       && 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       196847 : gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
    3015              :                       locus * where)
    3016              : {
    3017       196847 :   gfc_loopinfo *nested_loop, *outer_loop;
    3018       196847 :   gfc_se se;
    3019       196847 :   gfc_ss_info *ss_info;
    3020       196847 :   gfc_array_info *info;
    3021       196847 :   gfc_expr *expr;
    3022       196847 :   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       196847 :   if (ss->is_alloc_lhs)
    3027          203 :     return;
    3028              : 
    3029       518110 :   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       518110 :   for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
    3036              :     {
    3037       321466 :       gcc_assert (ss);
    3038              : 
    3039              :       /* Cross loop arrays are handled from within the most nested loop.  */
    3040       321466 :       if (ss->nested_ss != NULL)
    3041         4740 :         continue;
    3042              : 
    3043       316726 :       ss_info = ss->info;
    3044       316726 :       expr = ss_info->expr;
    3045       316726 :       info = &ss_info->data.array;
    3046              : 
    3047       316726 :       switch (ss_info->type)
    3048              :         {
    3049        44629 :         case GFC_SS_SCALAR:
    3050              :           /* Scalar expression.  Evaluate this now.  This includes elemental
    3051              :              dimension indices, but not array section bounds.  */
    3052        44629 :           gfc_init_se (&se, NULL);
    3053        44629 :           gfc_conv_expr (&se, expr);
    3054        44629 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3055              : 
    3056        44629 :           if (expr->ts.type != BT_CHARACTER
    3057        44629 :               && !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        40573 :               if (subscript)
    3062         6605 :                 se.expr = convert(gfc_array_index_type, se.expr);
    3063        40573 :               if (!ss_info->where)
    3064        40159 :                 se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
    3065        40573 :               gfc_add_block_to_block (&outer_loop->pre, &se.post);
    3066              :             }
    3067              :           else
    3068         4056 :             gfc_add_block_to_block (&outer_loop->post, &se.post);
    3069              : 
    3070        44629 :           ss_info->data.scalar.value = se.expr;
    3071        44629 :           ss_info->string_length = se.string_length;
    3072        44629 :           break;
    3073              : 
    3074         5195 :         case GFC_SS_REFERENCE:
    3075              :           /* Scalar argument to elemental procedure.  */
    3076         5195 :           gfc_init_se (&se, NULL);
    3077         5195 :           if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
    3078          874 :             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         5195 :           if (expr->ts.type == BT_CHARACTER)
    3088          174 :             gfc_conv_string_parameter (&se);
    3089              : 
    3090         5195 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3091         5195 :           gfc_add_block_to_block (&outer_loop->post, &se.post);
    3092         5195 :           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         5147 :             ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
    3102              :                                                            &outer_loop->pre);
    3103              : 
    3104         5195 :           ss_info->string_length = se.string_length;
    3105         5195 :           break;
    3106              : 
    3107              :         case GFC_SS_SECTION:
    3108              :           /* Add the expressions for scalar and vector subscripts.  */
    3109      2990176 :           for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    3110      2803290 :             if (info->subscript[n])
    3111         7591 :               gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
    3112              : 
    3113       186886 :           set_vector_loop_bounds (ss);
    3114       186886 :           break;
    3115              : 
    3116          986 :         case GFC_SS_VECTOR:
    3117              :           /* Get the vector's descriptor and store it in SS.  */
    3118          986 :           gfc_init_se (&se, NULL);
    3119          986 :           gfc_conv_expr_descriptor (&se, expr);
    3120          986 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3121          986 :           gfc_add_block_to_block (&outer_loop->post, &se.post);
    3122          986 :           info->descriptor = se.expr;
    3123          986 :           break;
    3124              : 
    3125        11704 :         case GFC_SS_INTRINSIC:
    3126        11704 :           gfc_add_intrinsic_ss_code (loop, ss);
    3127        11704 :           break;
    3128              : 
    3129         9860 :         case GFC_SS_FUNCTION:
    3130         9860 :           {
    3131              :             /* Array function return value.  We call the function and save its
    3132              :                result in a temporary for use inside the loop.  */
    3133         9860 :             gfc_init_se (&se, NULL);
    3134         9860 :             se.loop = loop;
    3135         9860 :             se.ss = ss;
    3136         9860 :             bool class_func = gfc_is_class_array_function (expr);
    3137         9860 :             if (class_func)
    3138          183 :               expr->must_finalize = 1;
    3139         9860 :             gfc_conv_expr (&se, expr);
    3140         9860 :             gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3141         9860 :             if (class_func
    3142          183 :                 && se.expr
    3143        10043 :                 && GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
    3144              :               {
    3145          183 :                 tree tmp = gfc_class_data_get (se.expr);
    3146          183 :                 info->descriptor = tmp;
    3147          183 :                 info->data = gfc_conv_descriptor_data_get (tmp);
    3148          183 :                 info->offset = gfc_conv_descriptor_offset_get (tmp);
    3149          366 :                 for (gfc_ss *s = ss; s; s = s->parent)
    3150          378 :                   for (int n = 0; n < s->dimen; n++)
    3151              :                     {
    3152          195 :                       int dim = s->dim[n];
    3153          195 :                       tree tree_dim = gfc_rank_cst[dim];
    3154              : 
    3155          195 :                       tree start;
    3156          195 :                       start = gfc_conv_descriptor_lbound_get (tmp, tree_dim);
    3157          195 :                       start = gfc_evaluate_now (start, &outer_loop->pre);
    3158          195 :                       info->start[dim] = start;
    3159              : 
    3160          195 :                       tree end;
    3161          195 :                       end = gfc_conv_descriptor_ubound_get (tmp, tree_dim);
    3162          195 :                       end = gfc_evaluate_now (end, &outer_loop->pre);
    3163          195 :                       info->end[dim] = end;
    3164              : 
    3165          195 :                       tree stride;
    3166          195 :                       stride = gfc_conv_descriptor_stride_get (tmp, tree_dim);
    3167          195 :                       stride = gfc_evaluate_now (stride, &outer_loop->pre);
    3168          195 :                       info->stride[dim] = stride;
    3169              :                     }
    3170              :               }
    3171         9860 :             gfc_add_block_to_block (&outer_loop->post, &se.post);
    3172         9860 :             gfc_add_block_to_block (&outer_loop->post, &se.finalblock);
    3173         9860 :             ss_info->string_length = se.string_length;
    3174              :           }
    3175         9860 :           break;
    3176              : 
    3177        44434 :         case GFC_SS_CONSTRUCTOR:
    3178        44434 :           if (expr->ts.type == BT_CHARACTER
    3179         8274 :               && ss_info->string_length == NULL
    3180         8274 :               && expr->ts.u.cl
    3181         8274 :               && expr->ts.u.cl->length
    3182         7973 :               && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    3183              :             {
    3184         7897 :               gfc_init_se (&se, NULL);
    3185         7897 :               gfc_conv_expr_type (&se, expr->ts.u.cl->length,
    3186              :                                   gfc_charlen_type_node);
    3187         7897 :               ss_info->string_length = se.expr;
    3188         7897 :               gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3189         7897 :               gfc_add_block_to_block (&outer_loop->post, &se.post);
    3190              :             }
    3191        44434 :           trans_array_constructor (ss, where);
    3192        44434 :           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       196644 :   if (!subscript)
    3205       192417 :     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       133541 : save_descriptor_data (tree descr, tree data)
    3218              : {
    3219       133541 :   return !(DECL_P (data)
    3220       121811 :            || (TREE_CODE (data) == ADDR_EXPR
    3221        71350 :                && DECL_P (TREE_OPERAND (data, 0)))
    3222        53664 :            || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (descr))
    3223        50003 :                && TREE_CODE (descr) == COMPONENT_REF
    3224        12339 :                && 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        23883 : maybe_substitute_expr (tree *tp, int *walk_subtree, void *data)
    3244              : {
    3245        23883 :   substitute_t *subst = (substitute_t *) data;
    3246        23883 :   if (*tp == subst->target)
    3247              :     {
    3248         4516 :       *tp = subst->repl;
    3249         4516 :       *walk_subtree = 0;
    3250              :     }
    3251              : 
    3252        23883 :   return NULL_TREE;
    3253              : }
    3254              : 
    3255              : 
    3256              : /* Substitute in EXPR any occurrence of TARGET with REPLACEMENT.  */
    3257              : 
    3258              : static void
    3259         4197 : substitute_subexpr_in_expr (tree target, tree replacement, tree expr)
    3260              : {
    3261         4197 :   substitute_t subst;
    3262         4197 :   subst.target = target;
    3263         4197 :   subst.repl = replacement;
    3264              : 
    3265         4197 :   walk_tree (&expr, maybe_substitute_expr, &subst, nullptr);
    3266         4197 : }
    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         3995 : save_ref (tree &code, tree &ref, vec<tree> &replacement_roots)
    3275              : {
    3276         3995 :   stmtblock_t tmp_block;
    3277         3995 :   gfc_init_block (&tmp_block);
    3278         3995 :   tree var = gfc_evaluate_now (ref, &tmp_block);
    3279         3995 :   gfc_add_expr_to_block (&tmp_block, code);
    3280         3995 :   code = gfc_finish_block (&tmp_block);
    3281              : 
    3282         3995 :   unsigned i;
    3283         3995 :   tree repl_root;
    3284         8192 :   FOR_EACH_VEC_ELT (replacement_roots, i, repl_root)
    3285         4197 :     substitute_subexpr_in_expr (ref, var, repl_root);
    3286              : 
    3287         3995 :   replacement_roots.safe_push (ref);
    3288         3995 :   ref = NULL_TREE;
    3289         3995 : }
    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         4067 : maybe_save_ref (tree &code, tree &ref, vec<tree> &replacement_roots,
    3297              :                 stmtblock_t *previous_code)
    3298              : {
    3299         4067 :   if (find_tree (previous_code->head, ref))
    3300              :     return;
    3301              : 
    3302         3995 :   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       336124 : 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       336124 :   tree accumulated_code = NULL_TREE;
    3324              : 
    3325              :   /* The current candidate to factoring.  */
    3326       336124 :   tree saveable_ref = NULL_TREE;
    3327              : 
    3328              :   /* The root expressions in which we look for subexpressions to replace with
    3329              :      variables.  */
    3330       336124 :   auto_vec<tree> replacement_roots;
    3331       336124 :   replacement_roots.safe_push (value);
    3332              : 
    3333       336124 :   tree data_ref = value;
    3334       336124 :   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       336124 :   bool maybe_reallocatable = true;
    3341              : 
    3342       565862 :   while (true)
    3343              :     {
    3344       450993 :       if (!maybe_reallocatable
    3345       450993 :           && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (data_ref)))
    3346         2536 :         saveable_ref = data_ref;
    3347              : 
    3348       450993 :       if (TREE_CODE (data_ref) == INDIRECT_REF)
    3349              :         {
    3350        61513 :           next_ref = TREE_OPERAND (data_ref, 0);
    3351              : 
    3352        61513 :           if (!maybe_reallocatable)
    3353              :             {
    3354        15830 :               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         1959 :                   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        15830 :               saveable_ref = next_ref;
    3370              :             }
    3371              :         }
    3372       389480 :       else if (TREE_CODE (data_ref) == COMPONENT_REF)
    3373              :         {
    3374        43829 :           maybe_reallocatable = false;
    3375        43829 :           next_ref = TREE_OPERAND (data_ref, 0);
    3376              :         }
    3377       345651 :       else if (TREE_CODE (data_ref) == NOP_EXPR)
    3378         3821 :         next_ref = TREE_OPERAND (data_ref, 0);
    3379              :       else
    3380              :         {
    3381       341830 :           if (DECL_P (data_ref))
    3382              :             break;
    3383              : 
    3384         7492 :           if (TREE_CODE (data_ref) == ARRAY_REF)
    3385              :             {
    3386         5706 :               maybe_reallocatable = false;
    3387         5706 :               next_ref = TREE_OPERAND (data_ref, 0);
    3388              :             }
    3389              : 
    3390         7492 :           if (saveable_ref != NULL_TREE)
    3391              :             /* We have seen a reference worth saving.  Do it now.  */
    3392         2108 :             maybe_save_ref (accumulated_code, saveable_ref, replacement_roots,
    3393              :                             preliminary_code);
    3394              : 
    3395         7492 :           if (TREE_CODE (data_ref) != ARRAY_REF)
    3396              :             break;
    3397              :         }
    3398              : 
    3399       114869 :       data_ref = next_ref;
    3400              :     }
    3401              : 
    3402       336124 :   *desc_ptr = value;
    3403       336124 :   gfc_add_expr_to_block (block, accumulated_code);
    3404       336124 : }
    3405              : 
    3406              : 
    3407              : /* Translate expressions for the descriptor and data pointer of a SS.  */
    3408              : /*GCC ARRAYS*/
    3409              : 
    3410              : static void
    3411       336124 : gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
    3412              : {
    3413       336124 :   gfc_se se;
    3414       336124 :   gfc_ss_info *ss_info;
    3415       336124 :   gfc_array_info *info;
    3416       336124 :   tree tmp;
    3417              : 
    3418       336124 :   ss_info = ss->info;
    3419       336124 :   info = &ss_info->data.array;
    3420              : 
    3421              :   /* Get the descriptor for the array to be scalarized.  */
    3422       336124 :   gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
    3423       336124 :   gfc_init_se (&se, NULL);
    3424       336124 :   se.descriptor_only = 1;
    3425       336124 :   gfc_conv_expr_lhs (&se, ss_info->expr);
    3426       336124 :   stmtblock_t tmp_block;
    3427       336124 :   gfc_init_block (&tmp_block);
    3428       336124 :   set_factored_descriptor_value (&info->descriptor, se.expr, &tmp_block,
    3429              :                                  &se.pre);
    3430       336124 :   gfc_add_block_to_block (block, &se.pre);
    3431       336124 :   gfc_add_block_to_block (block, &tmp_block);
    3432       336124 :   ss_info->string_length = se.string_length;
    3433       336124 :   ss_info->class_container = se.class_container;
    3434              : 
    3435       336124 :   if (base)
    3436              :     {
    3437       126746 :       if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
    3438        23126 :           && 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       126746 :       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       126746 :       if (save_descriptor_data (se.expr, tmp) && !ss->is_alloc_lhs)
    3458        37854 :         tmp = gfc_evaluate_now (tmp, block);
    3459       126746 :       info->data = tmp;
    3460              : 
    3461       126746 :       tmp = gfc_conv_array_offset (se.expr);
    3462       126746 :       if (!ss->is_alloc_lhs)
    3463       120154 :         tmp = gfc_evaluate_now (tmp, block);
    3464       126746 :       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       126746 :       info->saved_offset = info->offset;
    3470              :     }
    3471       336124 : }
    3472              : 
    3473              : 
    3474              : /* Initialize a gfc_loopinfo structure.  */
    3475              : 
    3476              : void
    3477       196682 : gfc_init_loopinfo (gfc_loopinfo * loop)
    3478              : {
    3479       196682 :   int n;
    3480              : 
    3481       196682 :   memset (loop, 0, sizeof (gfc_loopinfo));
    3482       196682 :   gfc_init_block (&loop->pre);
    3483       196682 :   gfc_init_block (&loop->post);
    3484              : 
    3485              :   /* Initially scalarize in order and default to no loop reversal.  */
    3486      3343594 :   for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    3487              :     {
    3488      2950230 :       loop->order[n] = n;
    3489      2950230 :       loop->reverse[n] = GFC_INHIBIT_REVERSE;
    3490              :     }
    3491              : 
    3492       196682 :   loop->ss = gfc_ss_terminator;
    3493       196682 : }
    3494              : 
    3495              : 
    3496              : /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
    3497              :    chain.  */
    3498              : 
    3499              : void
    3500       196412 : gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
    3501              : {
    3502       196412 :   se->loop = loop;
    3503       196412 : }
    3504              : 
    3505              : 
    3506              : /* Return an expression for the data pointer of an array.  */
    3507              : 
    3508              : tree
    3509       346266 : gfc_conv_array_data (tree descriptor)
    3510              : {
    3511       346266 :   tree type;
    3512              : 
    3513       346266 :   type = TREE_TYPE (descriptor);
    3514       346266 :   if (GFC_ARRAY_TYPE_P (type))
    3515              :     {
    3516       242134 :       if (TREE_CODE (type) == POINTER_TYPE)
    3517              :         return descriptor;
    3518              :       else
    3519              :         {
    3520              :           /* Descriptorless arrays.  */
    3521       179638 :           return gfc_build_addr_expr (NULL_TREE, descriptor);
    3522              :         }
    3523              :     }
    3524              :   else
    3525       104132 :     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       257419 : gfc_conv_array_offset (tree descriptor)
    3533              : {
    3534       257419 :   tree type;
    3535              : 
    3536       257419 :   type = TREE_TYPE (descriptor);
    3537       257419 :   if (GFC_ARRAY_TYPE_P (type))
    3538       183335 :     return GFC_TYPE_ARRAY_OFFSET (type);
    3539              :   else
    3540        74084 :     return gfc_conv_descriptor_offset_get (descriptor);
    3541              : }
    3542              : 
    3543              : 
    3544              : /* Get an expression for the array stride.  */
    3545              : 
    3546              : tree
    3547       509536 : gfc_conv_array_stride (tree descriptor, int dim)
    3548              : {
    3549       509536 :   tree tmp;
    3550       509536 :   tree type;
    3551              : 
    3552       509536 :   type = TREE_TYPE (descriptor);
    3553              : 
    3554              :   /* For descriptorless arrays use the array size.  */
    3555       509536 :   tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
    3556       509536 :   if (tmp != NULL_TREE)
    3557              :     return tmp;
    3558              : 
    3559       117045 :   tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
    3560       117045 :   return tmp;
    3561              : }
    3562              : 
    3563              : 
    3564              : /* Like gfc_conv_array_stride, but for the lower bound.  */
    3565              : 
    3566              : tree
    3567       325372 : gfc_conv_array_lbound (tree descriptor, int dim)
    3568              : {
    3569       325372 :   tree tmp;
    3570       325372 :   tree type;
    3571              : 
    3572       325372 :   type = TREE_TYPE (descriptor);
    3573              : 
    3574       325372 :   tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
    3575       325372 :   if (tmp != NULL_TREE)
    3576              :     return tmp;
    3577              : 
    3578        18831 :   tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
    3579        18831 :   return tmp;
    3580              : }
    3581              : 
    3582              : 
    3583              : /* Like gfc_conv_array_stride, but for the upper bound.  */
    3584              : 
    3585              : tree
    3586       210898 : gfc_conv_array_ubound (tree descriptor, int dim)
    3587              : {
    3588       210898 :   tree tmp;
    3589       210898 :   tree type;
    3590              : 
    3591       210898 :   type = TREE_TYPE (descriptor);
    3592              : 
    3593       210898 :   tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
    3594       210898 :   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         8633 :   if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
    3600          554 :     return gfc_index_zero_node;
    3601              : 
    3602         8079 :   tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
    3603         8079 :   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        36604 : abridged_ref_name (gfc_expr * expr, gfc_array_ref * ar)
    3617              : {
    3618        36604 :   gfc_ref *ref;
    3619        36604 :   gfc_symbol *sym;
    3620        36604 :   char *ref_name = NULL;
    3621        36604 :   const char *comp_name = NULL;
    3622        36604 :   int len_sym, last_len = 0, level = 0;
    3623        36604 :   bool sym_is_array;
    3624              : 
    3625        36604 :   gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->ref != NULL);
    3626              : 
    3627        36604 :   sym = expr->symtree->n.sym;
    3628        72821 :   sym_is_array = (sym->ts.type != BT_CLASS
    3629        36604 :                   ? sym->as != NULL
    3630          387 :                   : IS_CLASS_ARRAY (sym));
    3631        36604 :   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        52135 :   for (ref = expr->ref; ref; ref = ref->next)
    3636              :     {
    3637        38053 :       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        37135 :       if (ref->type != REF_ARRAY)
    3646          150 :         continue;
    3647              : 
    3648        36985 :       if (ar)
    3649              :         {
    3650        15971 :           if (&ref->u.ar == ar)
    3651              :             break;
    3652              :         }
    3653        21014 :       else if (ref->u.ar.type == AR_SECTION)
    3654              :         break;
    3655              :     }
    3656              : 
    3657        36604 :   if (level > 0)
    3658          800 :     last_len = strlen (comp_name);
    3659              : 
    3660              :   /* Provide a buffer sufficiently large to hold "x...%%z".  */
    3661        36604 :   ref_name = XNEWVEC (char, len_sym + last_len + 6);
    3662        36604 :   strcpy (ref_name, sym->name);
    3663              : 
    3664        36604 :   if (level == 1 && !sym_is_array)
    3665              :     {
    3666          442 :       strcat (ref_name, "%%");
    3667          442 :       strcat (ref_name, comp_name);
    3668              :     }
    3669        36162 :   else if (level > 0)
    3670              :     {
    3671          358 :       strcat (ref_name, "...%%");
    3672          358 :       strcat (ref_name, comp_name);
    3673              :     }
    3674              : 
    3675        36604 :   return ref_name;
    3676              : }
    3677              : 
    3678              : 
    3679              : /* Generate code to perform an array index bound check.  */
    3680              : 
    3681              : static tree
    3682         5780 : 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         5780 :   tree fault;
    3687         5780 :   tree tmp_lo, tmp_up;
    3688         5780 :   tree descriptor;
    3689         5780 :   char *msg;
    3690         5780 :   char *ref_name = NULL;
    3691         5780 :   const char * name = NULL;
    3692         5780 :   gfc_expr *expr;
    3693              : 
    3694         5780 :   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          160 :     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           92 :     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          108 : expr_contains_impure_fcn (gfc_expr *e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    3774              :          int* g ATTRIBUTE_UNUSED)
    3775              : {
    3776          108 :   if (e && e->expr_type == EXPR_FUNCTION
    3777            6 :       && !gfc_pure_function (e, &impure_name)
    3778          111 :       && !gfc_implicit_pure_function (e))
    3779            3 :     return true;
    3780              : 
    3781              :   return false;
    3782              : }
    3783              : 
    3784              : static bool
    3785           92 : gfc_expr_contains_impure_fcn (gfc_expr *e)
    3786              : {
    3787           92 :   impure_name = NULL;
    3788           92 :   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         6688 : array_bound_check_elemental (stmtblock_t *block, gfc_ss * ss, gfc_expr * expr)
    3796              : {
    3797         6688 :   gfc_array_ref *ar;
    3798         6688 :   gfc_ref *ref;
    3799         6688 :   char *var_name = NULL;
    3800         6688 :   int dim;
    3801              : 
    3802         6688 :   if (expr->expr_type == EXPR_VARIABLE)
    3803              :     {
    3804        12533 :       for (ref = expr->ref; ref; ref = ref->next)
    3805              :         {
    3806         6303 :           if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    3807              :             {
    3808         3953 :               ar = &ref->u.ar;
    3809         3953 :               var_name = abridged_ref_name (expr, ar);
    3810        12111 :               for (dim = 0; dim < ar->dimen; dim++)
    3811              :                 {
    3812         4205 :                   if (ar->dimen_type[dim] == DIMEN_ELEMENT)
    3813              :                     {
    3814           92 :                       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           92 :                       gfc_se indexse;
    3821           92 :                       gfc_init_se (&indexse, NULL);
    3822           92 :                       gfc_conv_expr_type (&indexse, ar->start[dim],
    3823              :                                           gfc_array_index_type);
    3824           92 :                       gfc_add_block_to_block (block, &indexse.pre);
    3825           92 :                       trans_array_bound_check (block, ss, indexse.expr, dim,
    3826              :                                                &ar->where,
    3827           92 :                                                ar->as->type != AS_ASSUMED_SIZE
    3828            0 :                                                || dim < ar->dimen - 1,
    3829              :                                                var_name);
    3830              :                     }
    3831              :                 }
    3832         3953 :               free (var_name);
    3833              :             }
    3834              :         }
    3835              :     }
    3836         6688 : }
    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       259924 : conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
    3845              :                          gfc_array_ref * ar, tree stride)
    3846              : {
    3847       259924 :   gfc_array_info *info;
    3848       259924 :   tree index;
    3849       259924 :   tree desc;
    3850       259924 :   tree data;
    3851              : 
    3852       259924 :   info = &ss->info->data.array;
    3853              : 
    3854              :   /* Get the index into the array for this dimension.  */
    3855       259924 :   if (ar)
    3856              :     {
    3857       184599 :       gcc_assert (ar->type != AR_ELEMENT);
    3858       184599 :       switch (ar->dimen_type[dim])
    3859              :         {
    3860            0 :         case DIMEN_THIS_IMAGE:
    3861            0 :           gcc_unreachable ();
    3862         4705 :           break;
    3863         4705 :         case DIMEN_ELEMENT:
    3864              :           /* Elemental dimension.  */
    3865         4705 :           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         4705 :           index = info->subscript[dim]->info->data.scalar.value;
    3869              : 
    3870         9486 :           index = trans_array_bound_check (&se->pre, ss, index, dim, &ar->where,
    3871         4705 :                                            ar->as->type != AS_ASSUMED_SIZE
    3872           76 :                                            || dim < ar->dimen - 1);
    3873         4705 :           break;
    3874              : 
    3875          983 :         case DIMEN_VECTOR:
    3876          983 :           gcc_assert (info && se->loop);
    3877          983 :           gcc_assert (info->subscript[dim]
    3878              :                       && info->subscript[dim]->info->type == GFC_SS_VECTOR);
    3879          983 :           desc = info->subscript[dim]->info->data.array.descriptor;
    3880              : 
    3881              :           /* Get a zero-based index into the vector.  */
    3882          983 :           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          983 :           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          983 :           data = build_fold_indirect_ref_loc (input_location,
    3893              :                                           gfc_conv_array_data (desc));
    3894          983 :           index = gfc_build_array_ref (data, index, NULL);
    3895          983 :           index = gfc_evaluate_now (index, &se->pre);
    3896          983 :           index = fold_convert (gfc_array_index_type, index);
    3897              : 
    3898              :           /* Do any bounds checking on the final info->descriptor index.  */
    3899         1972 :           index = trans_array_bound_check (&se->pre, ss, index, dim, &ar->where,
    3900          983 :                                            ar->as->type != AS_ASSUMED_SIZE
    3901            6 :                                            || dim < ar->dimen - 1);
    3902          983 :           break;
    3903              : 
    3904       178911 :         case DIMEN_RANGE:
    3905              :           /* Scalarized dimension.  */
    3906       178911 :           gcc_assert (info && se->loop);
    3907              : 
    3908              :           /* Multiply the loop variable by the stride and delta.  */
    3909       178911 :           index = se->loop->loopvar[i];
    3910       178911 :           if (!integer_onep (info->stride[dim]))
    3911         6990 :             index = fold_build2_loc (input_location, MULT_EXPR,
    3912              :                                      gfc_array_index_type, index,
    3913              :                                      info->stride[dim]);
    3914       178911 :           if (!integer_zerop (info->delta[dim]))
    3915        68904 :             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        75325 :       gcc_assert (se->loop);
    3928        75325 :       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        75325 :       if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
    3934         8154 :           && se->ss->info->expr
    3935         8154 :           && se->ss->info->expr->symtree
    3936         8154 :           && se->ss->info->expr->symtree->n.sym->result
    3937         7714 :           && 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        75325 :       if (info->delta[dim] && !integer_zerop (info->delta[dim]))
    3942          852 :         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       259924 :   if (stride != NULL && !integer_onep (stride))
    3948        79458 :     index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    3949              :                              index, stride);
    3950              : 
    3951       259924 :   return index;
    3952              : }
    3953              : 
    3954              : 
    3955              : /* Build a scalarized array reference using the vptr 'size'.  */
    3956              : 
    3957              : static bool
    3958       199870 : build_class_array_ref (gfc_se *se, tree base, tree index)
    3959              : {
    3960       199870 :   tree size;
    3961       199870 :   tree decl = NULL_TREE;
    3962       199870 :   tree tmp;
    3963       199870 :   gfc_expr *expr = se->ss->info->expr;
    3964       199870 :   gfc_expr *class_expr;
    3965       199870 :   gfc_typespec *ts;
    3966       199870 :   gfc_symbol *sym;
    3967              : 
    3968       199870 :   tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
    3969              : 
    3970        94376 :   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       196241 :       if (!expr
    3978       196241 :           || (expr->ts.type != BT_CLASS
    3979       181812 :               && !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          501 :       class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
    3986              : 
    3987          501 :       if (!ts)
    3988              :         return false;
    3989              : 
    3990          476 :       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          476 :       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          476 :         decl = gfc_get_class_from_gfc_expr (class_expr);
    4009              : 
    4010          476 :       if (POINTER_TYPE_P (TREE_TYPE (decl)))
    4011            0 :         decl = build_fold_indirect_ref_loc (input_location, decl);
    4012              : 
    4013          476 :       if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
    4014              :         return false;
    4015              :     }
    4016              : 
    4017         4105 :   se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
    4018              : 
    4019         4105 :   size = gfc_class_vtab_size_get (decl);
    4020              :   /* For unlimited polymorphic entities then _len component needs to be
    4021              :      multiplied with the size.  */
    4022         4105 :   size = gfc_resize_class_size_with_len (&se->pre, decl, size);
    4023         4105 :   size = fold_convert (TREE_TYPE (index), size);
    4024              : 
    4025              :   /* Return the element in the se expression.  */
    4026         4105 :   se->expr = gfc_build_spanned_array_ref (base, index, size);
    4027         4105 :   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       323328 : non_negative_strides_array_p (tree expr)
    4036              : {
    4037       337621 :   if (expr == NULL_TREE)
    4038              :     return false;
    4039              : 
    4040       337621 :   tree type = TREE_TYPE (expr);
    4041       337621 :   if (POINTER_TYPE_P (type))
    4042        77009 :     type = TREE_TYPE (type);
    4043              : 
    4044       337621 :   if (TYPE_LANG_SPECIFIC (type))
    4045              :     {
    4046       337621 :       gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
    4047              : 
    4048       337621 :       if (array_kind == GFC_ARRAY_ALLOCATABLE
    4049       337621 :           || 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       279273 :   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       242929 :   if (DECL_P (expr)
    4063       233674 :       && DECL_LANG_SPECIFIC (expr)
    4064        49224 :       && GFC_DECL_SAVED_DESCRIPTOR (expr)
    4065       257241 :       && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
    4066        14293 :     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       199870 : gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
    4076              :                                bool tmp_array = false)
    4077              : {
    4078       199870 :   gfc_array_info *info;
    4079       199870 :   tree decl = NULL_TREE;
    4080       199870 :   tree index;
    4081       199870 :   tree base;
    4082       199870 :   gfc_ss *ss;
    4083       199870 :   gfc_expr *expr;
    4084       199870 :   int n;
    4085              : 
    4086       199870 :   ss = se->ss;
    4087       199870 :   expr = ss->info->expr;
    4088       199870 :   info = &ss->info->data.array;
    4089       199870 :   if (ar)
    4090       136760 :     n = se->loop->order[0];
    4091              :   else
    4092              :     n = 0;
    4093              : 
    4094       199870 :   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       199870 :   if (info->offset && !integer_zerop (info->offset))
    4098       146643 :     index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4099              :                              index, info->offset);
    4100              : 
    4101       199870 :   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       199870 :   if (build_class_array_ref (se, base, index))
    4105         4105 :     return;
    4106              : 
    4107       195765 :   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       195765 :   if (is_span_addressed_array (info->descriptor)
    4114       195765 :       || (expr && ((expr->ts.deferred && info->descriptor
    4115         2836 :                     && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor)))
    4116       195765 :                    || (expr && gfc_expr_attr (expr).pdt_string))))
    4117              :     {
    4118         9956 :       if (TREE_CODE (info->descriptor) == COMPONENT_REF)
    4119         1642 :         decl = info->descriptor;
    4120         8314 :       else if (INDIRECT_REF_P (info->descriptor))
    4121         1491 :         decl = TREE_OPERAND (info->descriptor, 0);
    4122              : 
    4123         9956 :       if (decl == NULL_TREE)
    4124         6823 :         decl = info->descriptor;
    4125              :     }
    4126              : 
    4127       195765 :   bool non_negative_stride = tmp_array
    4128       195765 :                              || non_negative_strides_array_p (info->descriptor);
    4129       195765 :   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        63110 : gfc_conv_tmp_array_ref (gfc_se * se)
    4138              : {
    4139        63110 :   se->string_length = se->ss->info->string_length;
    4140        63110 :   gfc_conv_scalarized_array_ref (se, NULL, true);
    4141        63110 :   gfc_advance_se_ss_chain (se);
    4142        63110 : }
    4143              : 
    4144              : /* Add T to the offset pair *OFFSET, *CST_OFFSET.  */
    4145              : 
    4146              : static void
    4147       286492 : add_to_offset (tree *cst_offset, tree *offset, tree t)
    4148              : {
    4149       286492 :   if (TREE_CODE (t) == INTEGER_CST)
    4150       144305 :     *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
    4151              :   else
    4152              :     {
    4153       142187 :       if (!integer_zerop (*offset))
    4154        49318 :         *offset = fold_build2_loc (input_location, PLUS_EXPR,
    4155              :                                    gfc_array_index_type, *offset, t);
    4156              :       else
    4157        92869 :         *offset = t;
    4158              :     }
    4159       286492 : }
    4160              : 
    4161              : 
    4162              : static tree
    4163       190243 : build_array_ref (tree desc, tree offset, tree decl, tree vptr)
    4164              : {
    4165       190243 :   tree tmp;
    4166       190243 :   tree type;
    4167       190243 :   tree cdesc;
    4168              : 
    4169              :   /* For class arrays the class declaration is stored in the saved
    4170              :      descriptor.  */
    4171       190243 :   if (INDIRECT_REF_P (desc)
    4172         7411 :       && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
    4173       192610 :       && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
    4174          914 :     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       190243 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
    4182       190243 :       && TREE_CODE (cdesc) == COMPONENT_REF)
    4183              :     {
    4184        12088 :       type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
    4185        12088 :       if (TYPE_CANONICAL (type)
    4186        12088 :           && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
    4187              :         {
    4188         3889 :           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         3889 :           if (decl == NULL_TREE
    4196         3889 :               && GFC_TYPE_ARRAY_RANK (TREE_TYPE (cdesc)) > 0)
    4197         3588 :             decl = TREE_OPERAND (cdesc, 0);
    4198              :         }
    4199              :     }
    4200              : 
    4201       190243 :   if (decl == NULL_TREE
    4202       190243 :       && is_span_addressed_array (desc))
    4203              :     decl = desc;
    4204              : 
    4205       190243 :   tmp = gfc_conv_array_data (desc);
    4206       190243 :   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    4207       190243 :   tmp = gfc_build_array_ref (tmp, offset, decl,
    4208              :                              non_negative_strides_array_p (desc),
    4209              :                              vptr);
    4210       190243 :   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       271070 : gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
    4222              :                     locus * where)
    4223              : {
    4224       271070 :   int n;
    4225       271070 :   tree offset, cst_offset;
    4226       271070 :   tree tmp;
    4227       271070 :   tree stride;
    4228       271070 :   tree decl = NULL_TREE;
    4229       271070 :   gfc_se indexse;
    4230       271070 :   gfc_se tmpse;
    4231       271070 :   gfc_symbol * sym = expr->symtree->n.sym;
    4232       271070 :   char *var_name = NULL;
    4233              : 
    4234       271070 :   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       271070 :   if (ar->dimen == 0)
    4244              :     {
    4245         4836 :       gcc_assert (ar->codimen || sym->attr.select_rank_temporary
    4246              :                   || (ar->as && ar->as->corank));
    4247              : 
    4248         4836 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
    4249         1247 :         se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
    4250              :       else
    4251              :         {
    4252         3589 :           if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
    4253         3589 :               && 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         3589 :           if (!se->want_pointer)
    4258         2608 :             se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
    4259              :                                      se->expr);
    4260              :         }
    4261              : 
    4262       141596 :       return;
    4263              :     }
    4264              : 
    4265              :   /* Handle scalarized references separately.  */
    4266       266234 :   if (ar->type != AR_ELEMENT)
    4267              :     {
    4268       136760 :       gfc_conv_scalarized_array_ref (se, ar);
    4269       136760 :       gfc_advance_se_ss_chain (se);
    4270       136760 :       return;
    4271              :     }
    4272              : 
    4273       129474 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    4274        11849 :     var_name = abridged_ref_name (expr, ar);
    4275              : 
    4276       129474 :   decl = se->expr;
    4277       129474 :   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       129474 :   cst_offset = offset = gfc_index_zero_node;
    4284       129474 :   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       286492 :   for (n = ar->dimen - 1; n >= 0; n--)
    4289              :     {
    4290              :       /* Calculate the index for this dimension.  */
    4291       157018 :       gfc_init_se (&indexse, se);
    4292       157018 :       gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
    4293       157018 :       gfc_add_block_to_block (&se->pre, &indexse.pre);
    4294              : 
    4295       157018 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
    4296              :         {
    4297              :           /* Check array bounds.  */
    4298        15389 :           tree cond;
    4299        15389 :           char *msg;
    4300              : 
    4301              :           /* Evaluate the indexse.expr only once.  */
    4302        15389 :           indexse.expr = save_expr (indexse.expr);
    4303              : 
    4304              :           /* Lower bound.  */
    4305        15389 :           tmp = gfc_conv_array_lbound (decl, n);
    4306        15389 :           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        15389 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4316              :                                   indexse.expr, tmp);
    4317        15389 :           msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    4318              :                            "below lower bound of %%ld", n+1, var_name);
    4319        15389 :           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        15389 :           free (msg);
    4324              : 
    4325              :           /* Upper bound, but not for the last dimension of assumed-size
    4326              :              arrays.  */
    4327        15389 :           if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
    4328              :             {
    4329        13656 :               tmp = gfc_conv_array_ubound (decl, n);
    4330        13656 :               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        13656 :               cond = fold_build2_loc (input_location, GT_EXPR,
    4340              :                                       logical_type_node, indexse.expr, tmp);
    4341        13656 :               msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    4342              :                                "above upper bound of %%ld", n+1, var_name);
    4343        13656 :               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        13656 :               free (msg);
    4348              :             }
    4349              :         }
    4350              : 
    4351              :       /* Multiply the index by the stride.  */
    4352       157018 :       stride = gfc_conv_array_stride (decl, n);
    4353       157018 :       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       157018 :       add_to_offset (&cst_offset, &offset, tmp);
    4358              :     }
    4359              : 
    4360       129474 :   if (!integer_zerop (cst_offset))
    4361        68597 :     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       129474 :   decl = NULL_TREE;
    4368       129474 :   if (get_CFI_desc (sym, expr, &decl, ar))
    4369         3589 :     decl = build_fold_indirect_ref_loc (input_location, decl);
    4370       128331 :   if (!expr->ts.deferred && !sym->attr.codimension
    4371       255565 :       && is_span_addressed_array (se->expr))
    4372              :     {
    4373         5415 :       if (INDIRECT_REF_P (se->expr))
    4374          990 :         decl = TREE_OPERAND (se->expr, 0);
    4375              :       else
    4376         4425 :         decl = se->expr;
    4377              :     }
    4378       124059 :   else if (expr->ts.deferred
    4379       122916 :            || (sym->ts.type == BT_CHARACTER
    4380        15467 :                && sym->attr.select_type_temporary)
    4381       245259 :            || (expr->ts.type == BT_CHARACTER
    4382        15904 :                && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr))
    4383         5386 :                && gfc_expr_attr (expr).pdt_string))
    4384              :     {
    4385         2967 :       decl = se->expr;
    4386         2967 :       if (INDIRECT_REF_P (decl))
    4387           20 :         decl = TREE_OPERAND (decl, 0);
    4388              :     }
    4389       121092 :   else if (sym->ts.type == BT_CLASS)
    4390              :     {
    4391         2374 :       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         2271 :         decl = NULL_TREE;
    4403              :     }
    4404              : 
    4405       129474 :   free (var_name);
    4406       129474 :   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        60054 : 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        60054 :   gfc_se se;
    4418        60054 :   gfc_array_info *info;
    4419        60054 :   tree stride, index;
    4420              : 
    4421        60054 :   info = &ss->info->data.array;
    4422              : 
    4423        60054 :   gfc_init_se (&se, NULL);
    4424        60054 :   se.loop = loop;
    4425        60054 :   se.expr = info->descriptor;
    4426        60054 :   stride = gfc_conv_array_stride (info->descriptor, array_dim);
    4427        60054 :   index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
    4428        60054 :   gfc_add_block_to_block (pblock, &se.pre);
    4429              : 
    4430        60054 :   info->offset = fold_build2_loc (input_location, PLUS_EXPR,
    4431              :                                   gfc_array_index_type,
    4432              :                                   info->offset, index);
    4433        60054 :   info->offset = gfc_evaluate_now (info->offset, pblock);
    4434        60054 : }
    4435              : 
    4436              : 
    4437              : /* Generate the code to be executed immediately before entering a
    4438              :    scalarization loop.  */
    4439              : 
    4440              : static void
    4441       150696 : gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
    4442              :                          stmtblock_t * pblock)
    4443              : {
    4444       150696 :   tree stride;
    4445       150696 :   gfc_ss_info *ss_info;
    4446       150696 :   gfc_array_info *info;
    4447       150696 :   gfc_ss_type ss_type;
    4448       150696 :   gfc_ss *ss, *pss;
    4449       150696 :   gfc_loopinfo *ploop;
    4450       150696 :   gfc_array_ref *ar;
    4451              : 
    4452              :   /* This code will be executed before entering the scalarization loop
    4453              :      for this dimension.  */
    4454       458639 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4455              :     {
    4456       307943 :       ss_info = ss->info;
    4457              : 
    4458       307943 :       if ((ss_info->useflags & flag) == 0)
    4459         1477 :         continue;
    4460              : 
    4461       306466 :       ss_type = ss_info->type;
    4462       373540 :       if (ss_type != GFC_SS_SECTION
    4463              :           && ss_type != GFC_SS_FUNCTION
    4464       306466 :           && ss_type != GFC_SS_CONSTRUCTOR
    4465       306466 :           && ss_type != GFC_SS_COMPONENT)
    4466        67074 :         continue;
    4467              : 
    4468       239392 :       info = &ss_info->data.array;
    4469              : 
    4470       239392 :       gcc_assert (dim < ss->dimen);
    4471       239392 :       gcc_assert (ss->dimen == loop->dimen);
    4472              : 
    4473       239392 :       if (info->ref)
    4474       168711 :         ar = &info->ref->u.ar;
    4475              :       else
    4476              :         ar = NULL;
    4477              : 
    4478       239392 :       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       239392 :       if (dim == loop->dimen - 1 && loop->parent == NULL)
    4497              :         {
    4498       184043 :           gcc_assert (0 == ploop->order[0]);
    4499              : 
    4500       368086 :           stride = gfc_conv_array_stride (info->descriptor,
    4501       184043 :                                           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       184043 :           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       184043 :           if (info->ref)
    4512              :             {
    4513       296586 :               for (int i = 0; i < ar->dimen; i++)
    4514              :                 {
    4515       171009 :                   if (ar->dimen_type[i] != DIMEN_ELEMENT)
    4516       166304 :                     continue;
    4517              : 
    4518         4705 :                   add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
    4519              :                 }
    4520              :             }
    4521              :         }
    4522              :       else
    4523              :         {
    4524        55349 :           int i;
    4525              : 
    4526        55349 :           if (dim == loop->dimen - 1)
    4527              :             i = 0;
    4528              :           else
    4529        50662 :             i = dim + 1;
    4530              : 
    4531              :           /* For the time being, there is no loop reordering.  */
    4532        55349 :           gcc_assert (i == ploop->order[i]);
    4533        55349 :           i = ploop->order[i];
    4534              : 
    4535              :           /* Add the offset for the previous loop dimension.  */
    4536        55349 :           add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
    4537              :         }
    4538              : 
    4539              :       /* Remember this offset for the second loop.  */
    4540       239392 :       if (dim == loop->temp_dim - 1 && loop->parent == NULL)
    4541        55980 :         info->saved_offset = info->offset;
    4542              :     }
    4543       150696 : }
    4544              : 
    4545              : 
    4546              : /* Start a scalarized expression.  Creates a scope and declares loop
    4547              :    variables.  */
    4548              : 
    4549              : void
    4550       119986 : gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
    4551              : {
    4552       119986 :   int dim;
    4553       119986 :   int n;
    4554       119986 :   int flags;
    4555              : 
    4556       119986 :   gcc_assert (!loop->array_parameter);
    4557              : 
    4558       269085 :   for (dim = loop->dimen - 1; dim >= 0; dim--)
    4559              :     {
    4560       149099 :       n = loop->order[dim];
    4561              : 
    4562       149099 :       gfc_start_block (&loop->code[n]);
    4563              : 
    4564              :       /* Create the loop variable.  */
    4565       149099 :       loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
    4566              : 
    4567       149099 :       if (dim < loop->temp_dim)
    4568              :         flags = 3;
    4569              :       else
    4570       102347 :         flags = 1;
    4571              :       /* Calculate values that will be constant within this loop.  */
    4572       149099 :       gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
    4573              :     }
    4574       119986 :   gfc_start_block (pbody);
    4575       119986 : }
    4576              : 
    4577              : 
    4578              : /* Generates the actual loop code for a scalarization loop.  */
    4579              : 
    4580              : static void
    4581       165761 : gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
    4582              :                                stmtblock_t * pbody)
    4583              : {
    4584       165761 :   stmtblock_t block;
    4585       165761 :   tree cond;
    4586       165761 :   tree tmp;
    4587       165761 :   tree loopbody;
    4588       165761 :   tree exit_label;
    4589       165761 :   tree stmt;
    4590       165761 :   tree init;
    4591       165761 :   tree incr;
    4592              : 
    4593       165761 :   if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
    4594              :                       | OMPWS_SCALARIZER_BODY))
    4595              :       == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
    4596          108 :       && n == loop->dimen - 1)
    4597              :     {
    4598              :       /* We create an OMP_FOR construct for the outermost scalarized loop.  */
    4599           80 :       init = make_tree_vec (1);
    4600           80 :       cond = make_tree_vec (1);
    4601           80 :       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           80 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4606           80 :       TREE_USED (exit_label) = 1;
    4607              : 
    4608              :       /* Label for cycle statements (if needed).  */
    4609           80 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4610           80 :       gfc_add_expr_to_block (pbody, tmp);
    4611              : 
    4612           80 :       stmt = make_node (OMP_FOR);
    4613              : 
    4614           80 :       TREE_TYPE (stmt) = void_type_node;
    4615           80 :       OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
    4616              : 
    4617           80 :       OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
    4618              :                                                  OMP_CLAUSE_SCHEDULE);
    4619           80 :       OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
    4620           80 :         = OMP_CLAUSE_SCHEDULE_STATIC;
    4621           80 :       if (ompws_flags & OMPWS_NOWAIT)
    4622           33 :         OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
    4623           66 :           = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
    4624              : 
    4625              :       /* Initialize the loopvar.  */
    4626           80 :       TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
    4627              :                                          loop->from[n]);
    4628           80 :       OMP_FOR_INIT (stmt) = init;
    4629              :       /* The exit condition.  */
    4630           80 :       TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
    4631              :                                            logical_type_node,
    4632              :                                            loop->loopvar[n], loop->to[n]);
    4633           80 :       SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
    4634           80 :       OMP_FOR_COND (stmt) = cond;
    4635              :       /* Increment the loopvar.  */
    4636           80 :       tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4637              :                         loop->loopvar[n], gfc_index_one_node);
    4638           80 :       TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
    4639              :           void_type_node, loop->loopvar[n], tmp);
    4640           80 :       OMP_FOR_INCR (stmt) = incr;
    4641              : 
    4642           80 :       ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
    4643           80 :       gfc_add_expr_to_block (&loop->code[n], stmt);
    4644              :     }
    4645              :   else
    4646              :     {
    4647       331362 :       bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
    4648       165681 :                              && (loop->temp_ss == NULL);
    4649              : 
    4650       165681 :       loopbody = gfc_finish_block (pbody);
    4651              : 
    4652       165681 :       if (reverse_loop)
    4653          204 :         std::swap (loop->from[n], loop->to[n]);
    4654              : 
    4655              :       /* Initialize the loopvar.  */
    4656       165681 :       if (loop->loopvar[n] != loop->from[n])
    4657       164860 :         gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
    4658              : 
    4659       165681 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4660              : 
    4661              :       /* Generate the loop body.  */
    4662       165681 :       gfc_init_block (&block);
    4663              : 
    4664              :       /* The exit condition.  */
    4665       331158 :       cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
    4666              :                           logical_type_node, loop->loopvar[n], loop->to[n]);
    4667       165681 :       tmp = build1_v (GOTO_EXPR, exit_label);
    4668       165681 :       TREE_USED (exit_label) = 1;
    4669       165681 :       tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    4670       165681 :       gfc_add_expr_to_block (&block, tmp);
    4671              : 
    4672              :       /* The main body.  */
    4673       165681 :       gfc_add_expr_to_block (&block, loopbody);
    4674              : 
    4675              :       /* Increment the loopvar.  */
    4676       331158 :       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       165681 :       gfc_add_modify (&block, loop->loopvar[n], tmp);
    4682              : 
    4683              :       /* Build the loop.  */
    4684       165681 :       tmp = gfc_finish_block (&block);
    4685       165681 :       tmp = build1_v (LOOP_EXPR, tmp);
    4686       165681 :       gfc_add_expr_to_block (&loop->code[n], tmp);
    4687              : 
    4688              :       /* Add the exit label.  */
    4689       165681 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4690       165681 :       gfc_add_expr_to_block (&loop->code[n], tmp);
    4691              :     }
    4692              : 
    4693       165761 : }
    4694              : 
    4695              : 
    4696              : /* Finishes and generates the loops for a scalarized expression.  */
    4697              : 
    4698              : void
    4699       126820 : gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
    4700              : {
    4701       126820 :   int dim;
    4702       126820 :   int n;
    4703       126820 :   gfc_ss *ss;
    4704       126820 :   stmtblock_t *pblock;
    4705       126820 :   tree tmp;
    4706              : 
    4707       126820 :   pblock = body;
    4708              :   /* Generate the loops.  */
    4709       282744 :   for (dim = 0; dim < loop->dimen; dim++)
    4710              :     {
    4711       155924 :       n = loop->order[dim];
    4712       155924 :       gfc_trans_scalarized_loop_end (loop, n, pblock);
    4713       155924 :       loop->loopvar[n] = NULL_TREE;
    4714       155924 :       pblock = &loop->code[n];
    4715              :     }
    4716              : 
    4717       126820 :   tmp = gfc_finish_block (pblock);
    4718       126820 :   gfc_add_expr_to_block (&loop->pre, tmp);
    4719              : 
    4720              :   /* Clear all the used flags.  */
    4721       369702 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4722       242882 :     if (ss->parent == NULL)
    4723       238132 :       ss->info->useflags = 0;
    4724       126820 : }
    4725              : 
    4726              : 
    4727              : /* Finish the main body of a scalarized expression, and start the secondary
    4728              :    copying body.  */
    4729              : 
    4730              : void
    4731         8240 : gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
    4732              : {
    4733         8240 :   int dim;
    4734         8240 :   int n;
    4735         8240 :   stmtblock_t *pblock;
    4736         8240 :   gfc_ss *ss;
    4737              : 
    4738         8240 :   pblock = body;
    4739              :   /* We finish as many loops as are used by the temporary.  */
    4740         9837 :   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         8240 :   n = loop->order[loop->temp_dim - 1];
    4750         8240 :   gfc_trans_scalarized_loop_end (loop, n, pblock);
    4751              : 
    4752              :   /* Restore the initial offsets.  */
    4753        23631 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4754              :     {
    4755        15391 :       gfc_ss_type ss_type;
    4756        15391 :       gfc_ss_info *ss_info;
    4757              : 
    4758        15391 :       ss_info = ss->info;
    4759              : 
    4760        15391 :       if ((ss_info->useflags & 2) == 0)
    4761         4553 :         continue;
    4762              : 
    4763        10838 :       ss_type = ss_info->type;
    4764        10992 :       if (ss_type != GFC_SS_SECTION
    4765              :           && ss_type != GFC_SS_FUNCTION
    4766        10838 :           && ss_type != GFC_SS_CONSTRUCTOR
    4767        10838 :           && ss_type != GFC_SS_COMPONENT)
    4768          154 :         continue;
    4769              : 
    4770        10684 :       ss_info->data.array.offset = ss_info->data.array.saved_offset;
    4771              :     }
    4772              : 
    4773              :   /* Restart all the inner loops we just finished.  */
    4774         9837 :   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         8240 :   gfc_start_block (body);
    4787         8240 : }
    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       527731 : evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
    4799              :                 tree desc, int dim, bool lbound, bool deferred, bool save_value)
    4800              : {
    4801       527731 :   gfc_se se;
    4802       527731 :   gfc_expr * input_val = values[dim];
    4803       527731 :   tree *output = &bounds[dim];
    4804              : 
    4805       527731 :   if (input_val)
    4806              :     {
    4807              :       /* Specified section bound.  */
    4808        48330 :       gfc_init_se (&se, NULL);
    4809        48330 :       gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
    4810        48330 :       gfc_add_block_to_block (block, &se.pre);
    4811        48330 :       *output = se.expr;
    4812              :     }
    4813       479401 :   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       195362 :       *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
    4820              :                                                          gfc_rank_cst[dim]):
    4821        65234 :                          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       520467 :       *output = lbound ? gfc_conv_array_lbound (desc, dim) :
    4828       171194 :                          gfc_conv_array_ubound (desc, dim);
    4829              :     }
    4830       527731 :   if (save_value)
    4831       508321 :     *output = gfc_evaluate_now (*output, block);
    4832       527731 : }
    4833              : 
    4834              : 
    4835              : /* Calculate the lower bound of an array section.  */
    4836              : 
    4837              : static void
    4838       264476 : gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
    4839              : {
    4840       264476 :   gfc_expr *stride = NULL;
    4841       264476 :   tree desc;
    4842       264476 :   gfc_se se;
    4843       264476 :   gfc_array_info *info;
    4844       264476 :   gfc_array_ref *ar;
    4845              : 
    4846       264476 :   gcc_assert (ss->info->type == GFC_SS_SECTION);
    4847              : 
    4848       264476 :   info = &ss->info->data.array;
    4849       264476 :   ar = &info->ref->u.ar;
    4850              : 
    4851       264476 :   if (ar->dimen_type[dim] == DIMEN_VECTOR)
    4852              :     {
    4853              :       /* We use a zero-based index to access the vector.  */
    4854          986 :       info->start[dim] = gfc_index_zero_node;
    4855          986 :       info->end[dim] = NULL;
    4856          986 :       info->stride[dim] = gfc_index_one_node;
    4857          986 :       return;
    4858              :     }
    4859              : 
    4860       263490 :   gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
    4861              :               || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
    4862       263490 :   desc = info->descriptor;
    4863       263490 :   stride = ar->stride[dim];
    4864       263490 :   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       263490 :   evaluate_bound (block, info->start, ar->start, desc, dim, true,
    4869       263490 :                   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       263490 :   evaluate_bound (block, info->end, ar->end, desc, dim, false,
    4875       263490 :                   ar->as->type == AS_DEFERRED, save_value);
    4876              : 
    4877              : 
    4878              :   /* Calculate the stride.  */
    4879       263490 :   if (stride == NULL)
    4880       250542 :     info->stride[dim] = gfc_index_one_node;
    4881              :   else
    4882              :     {
    4883        12948 :       gfc_init_se (&se, NULL);
    4884        12948 :       gfc_conv_expr_type (&se, stride, gfc_array_index_type);
    4885        12948 :       gfc_add_block_to_block (block, &se.pre);
    4886        12948 :       tree value = se.expr;
    4887        12948 :       if (save_value)
    4888        12948 :         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        24078 : add_check_section_in_array_bounds (stmtblock_t *inner, gfc_ss_info *ss_info,
    4900              :                                    int dim)
    4901              : {
    4902        24078 :   gfc_expr *expr = ss_info->expr;
    4903        24078 :   locus *expr_loc = &expr->where;
    4904        24078 :   const char *expr_name = expr->symtree->name;
    4905              : 
    4906        24078 :   gfc_array_info *info = &ss_info->data.array;
    4907              : 
    4908        24078 :   bool check_upper;
    4909        24078 :   if (dim == info->ref->u.ar.dimen - 1
    4910        20451 :       && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    4911              :     check_upper = false;
    4912              :   else
    4913        23782 :     check_upper = true;
    4914              : 
    4915              :   /* Zero stride is not allowed.  */
    4916        24078 :   tree tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    4917              :                               info->stride[dim], gfc_index_zero_node);
    4918        24078 :   char * msg = xasprintf ("Zero stride is not allowed, for dimension %d "
    4919              :                           "of array '%s'", dim + 1, expr_name);
    4920        24078 :   gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg);
    4921        24078 :   free (msg);
    4922              : 
    4923        24078 :   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        24078 :   tree lbound = gfc_conv_array_lbound (desc, dim);
    4929        24078 :   tree end = info->end[dim];
    4930        24078 :   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        24078 :   tree stride_pos = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4935              :                                      info->stride[dim], gfc_index_zero_node);
    4936        24078 :   tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    4937              :                          info->start[dim], end);
    4938        24078 :   stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    4939              :                                 logical_type_node, stride_pos, tmp);
    4940              : 
    4941        24078 :   tree stride_neg = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4942              :                                      info->stride[dim], gfc_index_zero_node);
    4943        24078 :   tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    4944              :                          info->start[dim], end);
    4945        24078 :   stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    4946              :                                 logical_type_node, stride_neg, tmp);
    4947        24078 :   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        24078 :   if (check_upper)
    4956              :     {
    4957        23782 :       tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4958              :                              info->start[dim], lbound);
    4959        23782 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4960              :                              non_zerosized, tmp);
    4961        23782 :       tree tmp2 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4962              :                                    info->start[dim], ubound);
    4963        23782 :       tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4964              :                               non_zerosized, tmp2);
    4965        23782 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
    4966              :                        "expected range (%%ld:%%ld)", dim + 1, expr_name);
    4967        23782 :       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        23782 :       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        23782 :       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        24078 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    4996              :                          end, info->start[dim]);
    4997        24078 :   tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR, gfc_array_index_type,
    4998              :                          tmp, info->stride[dim]);
    4999        24078 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5000              :                          end, tmp);
    5001        24078 :   tree tmp2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    5002              :                                tmp, lbound);
    5003        24078 :   tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    5004              :                           non_zerosized, tmp2);
    5005        24078 :   if (check_upper)
    5006              :     {
    5007        23782 :       tree tmp3 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    5008              :                                    tmp, ubound);
    5009        23782 :       tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    5010              :                               non_zerosized, tmp3);
    5011        23782 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
    5012              :                        "expected range (%%ld:%%ld)", dim + 1, expr_name);
    5013        23782 :       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        23782 :       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        23782 :       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        24078 : }
    5033              : 
    5034              : 
    5035              : /* Tells whether we need to generate bounds checking code for the array
    5036              :    associated with SS.  */
    5037              : 
    5038              : bool
    5039        25045 : bounds_check_needed (gfc_ss *ss)
    5040              : {
    5041              :   /* Catch allocatable lhs in f2003.  */
    5042        25045 :   if (flag_realloc_lhs && ss->no_bounds_check)
    5043              :     return false;
    5044              : 
    5045        24768 :   gfc_ss_info *ss_info = ss->info;
    5046        24768 :   if (ss_info->type == GFC_SS_SECTION)
    5047              :     return true;
    5048              : 
    5049         4126 :   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       189256 : gfc_conv_ss_startstride (gfc_loopinfo * loop)
    5070              : {
    5071       189256 :   int n;
    5072       189256 :   tree tmp;
    5073       189256 :   gfc_ss *ss;
    5074              : 
    5075       189256 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    5076              : 
    5077       189256 :   loop->dimen = 0;
    5078              :   /* Determine the rank of the loop.  */
    5079       210166 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5080              :     {
    5081       210166 :       switch (ss->info->type)
    5082              :         {
    5083       177845 :         case GFC_SS_SECTION:
    5084       177845 :         case GFC_SS_CONSTRUCTOR:
    5085       177845 :         case GFC_SS_FUNCTION:
    5086       177845 :         case GFC_SS_COMPONENT:
    5087       177845 :           loop->dimen = ss->dimen;
    5088       177845 :           goto done;
    5089              : 
    5090              :         /* As usual, lbound and ubound are exceptions!.  */
    5091        11411 :         case GFC_SS_INTRINSIC:
    5092        11411 :           switch (ss->info->expr->value.function.isym->id)
    5093              :             {
    5094        11411 :             case GFC_ISYM_LBOUND:
    5095        11411 :             case GFC_ISYM_UBOUND:
    5096        11411 :             case GFC_ISYM_COSHAPE:
    5097        11411 :             case GFC_ISYM_LCOBOUND:
    5098        11411 :             case GFC_ISYM_UCOBOUND:
    5099        11411 :             case GFC_ISYM_MAXLOC:
    5100        11411 :             case GFC_ISYM_MINLOC:
    5101        11411 :             case GFC_ISYM_SHAPE:
    5102        11411 :             case GFC_ISYM_THIS_IMAGE:
    5103        11411 :               loop->dimen = ss->dimen;
    5104        11411 :               goto done;
    5105              : 
    5106              :             default:
    5107              :               break;
    5108              :             }
    5109              : 
    5110        20910 :         default:
    5111        20910 :           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       189256 : done:
    5120              :   /* Loop over all the SS in the chain.  */
    5121       491680 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5122              :     {
    5123       302424 :       gfc_ss_info *ss_info;
    5124       302424 :       gfc_array_info *info;
    5125       302424 :       gfc_expr *expr;
    5126              : 
    5127       302424 :       ss_info = ss->info;
    5128       302424 :       expr = ss_info->expr;
    5129       302424 :       info = &ss_info->data.array;
    5130              : 
    5131       302424 :       if (expr && expr->shape && !info->shape)
    5132       174602 :         info->shape = expr->shape;
    5133              : 
    5134       302424 :       switch (ss_info->type)
    5135              :         {
    5136       191725 :         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       191725 :           if (ss->parent == NULL)
    5140       187089 :             gfc_conv_ss_descriptor (&outer_loop->pre, ss,
    5141       187089 :                                     !loop->array_parameter);
    5142              : 
    5143       455373 :           for (n = 0; n < ss->dimen; n++)
    5144       263648 :             gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
    5145              :           break;
    5146              : 
    5147        11704 :         case GFC_SS_INTRINSIC:
    5148        11704 :           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         8028 :             case GFC_ISYM_SHAPE:
    5188         8028 :               {
    5189         8028 :                 gfc_expr *arg;
    5190              : 
    5191         8028 :                 arg = expr->value.function.actual->expr;
    5192         8028 :                 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       133967 :           for (n = 0; n < ss->dimen; n++)
    5237              :             {
    5238        72197 :               int dim = ss->dim[n];
    5239              : 
    5240        72197 :               info->start[dim]  = gfc_index_zero_node;
    5241        72197 :               if (ss_info->type != GFC_SS_FUNCTION)
    5242        57426 :                 info->end[dim]    = gfc_index_zero_node;
    5243        72197 :               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       189256 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    5254              :     {
    5255        16945 :       stmtblock_t block;
    5256        16945 :       tree size[GFC_MAX_DIMENSIONS];
    5257        16945 :       tree tmp3;
    5258        16945 :       gfc_array_info *info;
    5259        16945 :       char *msg;
    5260        16945 :       int dim;
    5261              : 
    5262        16945 :       gfc_start_block (&block);
    5263              : 
    5264        54257 :       for (n = 0; n < loop->dimen; n++)
    5265        20367 :         size[n] = NULL_TREE;
    5266              : 
    5267              :       /* If there is a constructor involved, derive size[] from its shape.  */
    5268        39164 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5269              :         {
    5270        24699 :           gfc_ss_info *ss_info;
    5271              : 
    5272        24699 :           ss_info = ss->info;
    5273        24699 :           info = &ss_info->data.array;
    5274              : 
    5275        24699 :           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        41990 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5291              :         {
    5292        25045 :           stmtblock_t inner;
    5293        25045 :           gfc_ss_info *ss_info;
    5294        25045 :           gfc_expr *expr;
    5295        25045 :           locus *expr_loc;
    5296        25045 :           const char *expr_name;
    5297        25045 :           char *ref_name = NULL;
    5298              : 
    5299        25045 :           if (!bounds_check_needed (ss))
    5300         4369 :             continue;
    5301              : 
    5302        20676 :           ss_info = ss->info;
    5303        20676 :           expr = ss_info->expr;
    5304        20676 :           expr_loc = &expr->where;
    5305        20676 :           if (expr->ref)
    5306        20642 :             expr_name = ref_name = abridged_ref_name (expr, NULL);
    5307              :           else
    5308           34 :             expr_name = expr->symtree->name;
    5309              : 
    5310        20676 :           gfc_start_block (&inner);
    5311              : 
    5312              :           /* TODO: range checking for mapped dimensions.  */
    5313        20676 :           info = &ss_info->data.array;
    5314              : 
    5315              :           /* This code only checks ranges.  Elemental and vector
    5316              :              dimensions are checked later.  */
    5317        65478 :           for (n = 0; n < loop->dimen; n++)
    5318              :             {
    5319        24126 :               dim = ss->dim[n];
    5320        24126 :               if (ss_info->type == GFC_SS_SECTION)
    5321              :                 {
    5322        24092 :                   if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
    5323           14 :                     continue;
    5324              : 
    5325        24078 :                   add_check_section_in_array_bounds (&inner, ss_info, dim);
    5326              :                 }
    5327              : 
    5328              :               /* Check the section sizes match.  */
    5329        24112 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5330              :                                      gfc_array_index_type, info->end[dim],
    5331              :                                      info->start[dim]);
    5332        24112 :               tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
    5333              :                                      gfc_array_index_type, tmp,
    5334              :                                      info->stride[dim]);
    5335        24112 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5336              :                                      gfc_array_index_type,
    5337              :                                      gfc_index_one_node, tmp);
    5338        24112 :               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        24112 :               if (size[n])
    5344              :                 {
    5345         7193 :                   tmp3 = fold_build2_loc (input_location, NE_EXPR,
    5346              :                                           logical_type_node, tmp, size[n]);
    5347         7193 :                   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         7193 :                     msg = xasprintf ("Array bound mismatch for dimension %d "
    5353              :                                      "of array '%s' (%%ld/%%ld)",
    5354              :                                      dim + 1, expr_name);
    5355              : 
    5356         7193 :                   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         7193 :                   free (msg);
    5362              :                 }
    5363              :               else
    5364        16919 :                 size[n] = gfc_evaluate_now (tmp, &inner);
    5365              :             }
    5366              : 
    5367        20676 :           tmp = gfc_finish_block (&inner);
    5368              : 
    5369              :           /* For optional arguments, only check bounds if the argument is
    5370              :              present.  */
    5371        20676 :           if ((expr->symtree->n.sym->attr.optional
    5372        20368 :                || 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        20676 :           gfc_add_expr_to_block (&block, tmp);
    5379              : 
    5380        20676 :           free (ref_name);
    5381              :         }
    5382              : 
    5383        16945 :       tmp = gfc_finish_block (&block);
    5384        16945 :       gfc_add_expr_to_block (&outer_loop->pre, tmp);
    5385              :     }
    5386              : 
    5387       192620 :   for (loop = loop->nested; loop; loop = loop->next)
    5388         3364 :     gfc_conv_ss_startstride (loop);
    5389       189256 : }
    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        14230 : 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        14230 :   if (!gfc_compare_types (&lsym->ts, &rsym->ts)
    5402        14230 :       && !(((lsym->ts.type == BT_COMPLEX && rsym->ts.type == BT_REAL)
    5403         5187 :             || (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         9056 :   if ((lsym_pointer && (rsym_pointer || rsym_target))
    5410         8847 :       || (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         8933 :   if (lsym_target && rsym_target
    5417           14 :       && ((lsym->attr.dummy && !lsym->attr.contiguous
    5418            0 :            && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
    5419           14 :           || (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        11794 : gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
    5434              : {
    5435        11794 :   gfc_ref *lref;
    5436        11794 :   gfc_ref *rref;
    5437        11794 :   gfc_expr *lexpr, *rexpr;
    5438        11794 :   gfc_symbol *lsym;
    5439        11794 :   gfc_symbol *rsym;
    5440        11794 :   bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
    5441              : 
    5442        11794 :   lexpr = lss->info->expr;
    5443        11794 :   rexpr = rss->info->expr;
    5444              : 
    5445        11794 :   lsym = lexpr->symtree->n.sym;
    5446        11794 :   rsym = rexpr->symtree->n.sym;
    5447              : 
    5448        11794 :   lsym_pointer = lsym->attr.pointer;
    5449        11794 :   lsym_target = lsym->attr.target;
    5450        11794 :   rsym_pointer = rsym->attr.pointer;
    5451        11794 :   rsym_target = rsym->attr.target;
    5452              : 
    5453        11794 :   if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
    5454              :                            rsym_pointer, rsym_target))
    5455              :     return 1;
    5456              : 
    5457        11703 :   if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
    5458        10190 :       && 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         3070 :   for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
    5465              :     {
    5466         1103 :       if (lref->type != REF_COMPONENT)
    5467          107 :         continue;
    5468              : 
    5469          996 :       lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
    5470          996 :       lsym_target  = lsym_target  || lref->u.c.sym->attr.target;
    5471              : 
    5472          996 :       if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
    5473              :                                rsym_pointer, rsym_target))
    5474              :         return 1;
    5475              : 
    5476          996 :       if ((lsym_pointer && (rsym_pointer || rsym_target))
    5477          981 :           || (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         1504 :       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         1967 :   lsym_pointer = lsym->attr.pointer;
    5515         1967 :   lsym_target = lsym->attr.target;
    5516              : 
    5517         2922 :   for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
    5518              :     {
    5519         1126 :       if (rref->type != REF_COMPONENT)
    5520              :         break;
    5521              : 
    5522          961 :       rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
    5523          961 :       rsym_target  = lsym_target  || rref->u.c.sym->attr.target;
    5524              : 
    5525          961 :       if (symbols_could_alias (rref->u.c.sym, lsym,
    5526              :                                lsym_pointer, lsym_target,
    5527              :                                rsym_pointer, rsym_target))
    5528              :         return 1;
    5529              : 
    5530          961 :       if ((lsym_pointer && (rsym_pointer || rsym_target))
    5531          943 :           || (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        39457 : gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
    5548              :                                gfc_ss * rss)
    5549              : {
    5550        39457 :   gfc_ss *ss;
    5551        39457 :   gfc_ref *lref;
    5552        39457 :   gfc_ref *rref;
    5553        39457 :   gfc_ss_info *ss_info;
    5554        39457 :   gfc_expr *dest_expr;
    5555        39457 :   gfc_expr *ss_expr;
    5556        39457 :   int nDepend = 0;
    5557        39457 :   int i, j;
    5558              : 
    5559        39457 :   loop->temp_ss = NULL;
    5560        39457 :   dest_expr = dest->info->expr;
    5561              : 
    5562        84863 :   for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
    5563              :     {
    5564        46606 :       ss_info = ss->info;
    5565        46606 :       ss_expr = ss_info->expr;
    5566              : 
    5567        46606 :       if (ss_info->array_outer_dependency)
    5568              :         {
    5569              :           nDepend = 1;
    5570              :           break;
    5571              :         }
    5572              : 
    5573        46483 :       if (ss_info->type != GFC_SS_SECTION)
    5574              :         {
    5575        31864 :           if (flag_realloc_lhs
    5576        30767 :               && dest_expr != ss_expr
    5577        30767 :               && gfc_is_reallocatable_lhs (dest_expr)
    5578        39135 :               && ss_expr->rank)
    5579         3537 :             nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
    5580              : 
    5581              :           /* Check for cases like   c(:)(1:2) = c(2)(2:3)  */
    5582        31864 :           if (!nDepend && dest_expr->rank > 0
    5583        31333 :               && dest_expr->ts.type == BT_CHARACTER
    5584         4892 :               && ss_expr->expr_type == EXPR_VARIABLE)
    5585              : 
    5586          165 :             nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
    5587              : 
    5588        31864 :           if (ss_info->type == GFC_SS_REFERENCE
    5589        31864 :               && gfc_check_dependency (dest_expr, ss_expr, false))
    5590          206 :             ss_info->data.scalar.needs_temporary = 1;
    5591              : 
    5592        31864 :           if (nDepend)
    5593              :             break;
    5594              :           else
    5595        31321 :             continue;
    5596              :         }
    5597              : 
    5598        14619 :       if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
    5599              :         {
    5600        11794 :           if (gfc_could_be_alias (dest, ss)
    5601        11794 :               || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
    5602              :             {
    5603              :               nDepend = 1;
    5604              :               break;
    5605              :             }
    5606              :         }
    5607              :       else
    5608              :         {
    5609         2825 :           lref = dest_expr->ref;
    5610         2825 :           rref = ss_expr->ref;
    5611              : 
    5612         2825 :           nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
    5613              : 
    5614         2825 :           if (nDepend == 1)
    5615              :             break;
    5616              : 
    5617         5638 :           for (i = 0; i < dest->dimen; i++)
    5618         7630 :             for (j = 0; j < ss->dimen; j++)
    5619         4528 :               if (i != j
    5620         1363 :                   && 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          832 : temporary:
    5666              : 
    5667        39457 :   if (nDepend == 1)
    5668              :     {
    5669         1200 :       tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
    5670         1200 :       if (GFC_ARRAY_TYPE_P (base_type)
    5671         1200 :           || GFC_DESCRIPTOR_TYPE_P (base_type))
    5672            0 :         base_type = gfc_get_element_type (base_type);
    5673         1200 :       loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
    5674              :                                        loop->dimen);
    5675         1200 :       gfc_add_ss_to_loop (loop, loop->temp_ss);
    5676              :     }
    5677              :   else
    5678        38257 :     loop->temp_ss = NULL;
    5679        39457 : }
    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       189256 : set_loop_bounds (gfc_loopinfo *loop)
    5688              : {
    5689       189256 :   int n, dim, spec_dim;
    5690       189256 :   gfc_array_info *info;
    5691       189256 :   gfc_array_info *specinfo;
    5692       189256 :   gfc_ss *ss;
    5693       189256 :   tree tmp;
    5694       189256 :   gfc_ss **loopspec;
    5695       189256 :   bool dynamic[GFC_MAX_DIMENSIONS];
    5696       189256 :   mpz_t *cshape;
    5697       189256 :   mpz_t i;
    5698       189256 :   bool nonoptional_arr;
    5699              : 
    5700       189256 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    5701              : 
    5702       189256 :   loopspec = loop->specloop;
    5703              : 
    5704       189256 :   mpz_init (i);
    5705       633884 :   for (n = 0; n < loop->dimen; n++)
    5706              :     {
    5707       255372 :       loopspec[n] = NULL;
    5708       255372 :       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       255372 :       nonoptional_arr = false;
    5715              : 
    5716       298054 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5717       298034 :         if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
    5718       261997 :             && 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       668503 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5727              :         {
    5728       413131 :           gfc_ss_type ss_type;
    5729              : 
    5730       413131 :           ss_type = ss->info->type;
    5731       484978 :           if (ss_type == GFC_SS_SCALAR
    5732       413131 :               || ss_type == GFC_SS_TEMP
    5733       350614 :               || ss_type == GFC_SS_REFERENCE
    5734       341567 :               || (ss->info->can_be_null_ref && nonoptional_arr))
    5735        71847 :             continue;
    5736              : 
    5737       341284 :           info = &ss->info->data.array;
    5738       341284 :           dim = ss->dim[n];
    5739              : 
    5740       341284 :           if (loopspec[n] != NULL)
    5741              :             {
    5742        85912 :               specinfo = &loopspec[n]->info->data.array;
    5743        85912 :               spec_dim = loopspec[n]->dim[n];
    5744              :             }
    5745              :           else
    5746              :             {
    5747              :               /* Silence uninitialized warnings.  */
    5748              :               specinfo = NULL;
    5749              :               spec_dim = 0;
    5750              :             }
    5751              : 
    5752       341284 :           if (info->shape)
    5753              :             {
    5754              :               /* The frontend has worked out the size for us.  */
    5755       229821 :               if (!loopspec[n]
    5756        60806 :                   || !specinfo->shape
    5757       277420 :                   || !integer_zerop (specinfo->start[spec_dim]))
    5758              :                 /* Prefer zero-based descriptors if possible.  */
    5759       212484 :                 loopspec[n] = ss;
    5760       229821 :               continue;
    5761              :             }
    5762              : 
    5763       111463 :           if (ss_type == GFC_SS_CONSTRUCTOR)
    5764              :             {
    5765         1512 :               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         1512 :               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         1512 :               base = ss->info->expr->value.constructor;
    5776         1512 :               dynamic[n] = gfc_get_array_constructor_size (&i, base);
    5777         1512 :               if (!dynamic[n] || !loopspec[n])
    5778         1283 :                 loopspec[n] = ss;
    5779         1512 :               continue;
    5780         1512 :             }
    5781              : 
    5782              :           /* Avoid using an allocatable lhs in an assignment, since
    5783              :              there might be a reallocation coming.  */
    5784       109951 :           if (loopspec[n] && ss->is_alloc_lhs)
    5785         9705 :             continue;
    5786              : 
    5787       100246 :           if (!loopspec[n])
    5788        85074 :             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        15172 :           else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
    5797          289 :             loopspec[n] = ss;
    5798        14883 :           else if (integer_onep (info->stride[dim])
    5799        14883 :                    && !integer_onep (specinfo->stride[spec_dim]))
    5800          126 :             loopspec[n] = ss;
    5801        14757 :           else if (INTEGER_CST_P (info->stride[dim])
    5802        14533 :                    && !INTEGER_CST_P (specinfo->stride[spec_dim]))
    5803            0 :             loopspec[n] = ss;
    5804        14757 :           else if (INTEGER_CST_P (info->start[dim])
    5805         4576 :                    && !INTEGER_CST_P (specinfo->start[spec_dim])
    5806          856 :                    && integer_onep (info->stride[dim])
    5807          428 :                       == integer_onep (specinfo->stride[spec_dim])
    5808        14757 :                    && INTEGER_CST_P (info->stride[dim])
    5809          401 :                       == INTEGER_CST_P (specinfo->stride[spec_dim]))
    5810          401 :             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       255372 :       gcc_assert (loopspec[n]);
    5820              : 
    5821       255372 :       info = &loopspec[n]->info->data.array;
    5822       255372 :       dim = loopspec[n]->dim[n];
    5823              : 
    5824              :       /* Set the extents of this range.  */
    5825       255372 :       cshape = info->shape;
    5826       255372 :       if (cshape && INTEGER_CST_P (info->start[dim])
    5827       182017 :           && INTEGER_CST_P (info->stride[dim]))
    5828              :         {
    5829       182017 :           loop->from[n] = info->start[dim];
    5830       182017 :           mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
    5831       182017 :           mpz_sub_ui (i, i, 1);
    5832              :           /* To = from + (size - 1) * stride.  */
    5833       182017 :           tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
    5834       182017 :           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       182017 :           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        73355 :           loop->from[n] = info->start[dim];
    5845        73355 :           switch (loopspec[n]->info->type)
    5846              :             {
    5847          893 :             case GFC_SS_CONSTRUCTOR:
    5848              :               /* The upper bound is calculated when we expand the
    5849              :                  constructor.  */
    5850          893 :               gcc_assert (loop->to[n] == NULL_TREE);
    5851              :               break;
    5852              : 
    5853        66638 :             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        66638 :               loop->to[n] = info->end[dim];
    5857        66638 :               break;
    5858              : 
    5859         5045 :             case GFC_SS_FUNCTION:
    5860              :               /* The loop bound will be set when we generate the call.  */
    5861         5045 :               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       255372 :       if (integer_onep (info->stride[dim]))
    5899       244394 :         info->delta[dim] = gfc_index_zero_node;
    5900              :       else
    5901              :         {
    5902              :           /* Set the delta for this section.  */
    5903        10978 :           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        10978 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5909              :                                  gfc_array_index_type, loop->to[n],
    5910              :                                  loop->from[n]);
    5911        10978 :           tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
    5912              :                                  gfc_array_index_type, tmp, info->stride[dim]);
    5913        10978 :           tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    5914              :                                  tmp, build_int_cst (gfc_array_index_type, -1));
    5915        10978 :           loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5916              :           /* Make the loop variable start at 0.  */
    5917        10978 :           loop->from[n] = gfc_index_zero_node;
    5918              :         }
    5919              :     }
    5920       189256 :   mpz_clear (i);
    5921              : 
    5922       192620 :   for (loop = loop->nested; loop; loop = loop->next)
    5923         3364 :     set_loop_bounds (loop);
    5924       189256 : }
    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       189256 : late_set_loop_bounds (gfc_loopinfo *loop)
    5932              : {
    5933       189256 :   int n, dim;
    5934       189256 :   gfc_array_info *info;
    5935       189256 :   gfc_ss **loopspec;
    5936              : 
    5937       189256 :   loopspec = loop->specloop;
    5938              : 
    5939       444628 :   for (n = 0; n < loop->dimen; n++)
    5940              :     {
    5941              :       /* Set the extents of this range.  */
    5942       255372 :       if (loop->from[n] == NULL_TREE
    5943       255372 :           || loop->to[n] == NULL_TREE)
    5944              :         {
    5945              :           /* We should have found the scalarization loop specifier.  If not,
    5946              :              that's bad news.  */
    5947          455 :           gcc_assert (loopspec[n]);
    5948              : 
    5949          455 :           info = &loopspec[n]->info->data.array;
    5950          455 :           dim = loopspec[n]->dim[n];
    5951              : 
    5952          455 :           if (loopspec[n]->info->type == GFC_SS_FUNCTION
    5953          455 :               && info->start[dim]
    5954          455 :               && info->end[dim])
    5955              :             {
    5956          153 :               loop->from[n] = info->start[dim];
    5957          153 :               loop->to[n] = info->end[dim];
    5958              :             }
    5959              :         }
    5960              :     }
    5961              : 
    5962       192620 :   for (loop = loop->nested; loop; loop = loop->next)
    5963         3364 :     late_set_loop_bounds (loop);
    5964       189256 : }
    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       185892 : gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
    5974              : {
    5975       185892 :   gfc_ss *tmp_ss;
    5976       185892 :   tree tmp;
    5977              : 
    5978       185892 :   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       185892 :   gfc_add_loop_ss_code (loop, loop->ss, false, where);
    5984              : 
    5985       185892 :   late_set_loop_bounds (loop);
    5986              : 
    5987       185892 :   tmp_ss = loop->temp_ss;
    5988              :   /* If we want a temporary then create it.  */
    5989       185892 :   if (tmp_ss != NULL)
    5990              :     {
    5991        11857 :       gfc_ss_info *tmp_ss_info;
    5992              : 
    5993        11857 :       tmp_ss_info = tmp_ss->info;
    5994        11857 :       gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
    5995        11857 :       gcc_assert (loop->parent == NULL);
    5996              : 
    5997              :       /* Make absolutely sure that this is a complete type.  */
    5998        11857 :       if (tmp_ss_info->string_length)
    5999         2821 :         tmp_ss_info->data.temp.type
    6000         2821 :                 = gfc_get_character_type_len_for_eltype
    6001         2821 :                         (TREE_TYPE (tmp_ss_info->data.temp.type),
    6002              :                          tmp_ss_info->string_length);
    6003              : 
    6004        11857 :       tmp = tmp_ss_info->data.temp.type;
    6005        11857 :       memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
    6006        11857 :       tmp_ss_info->type = GFC_SS_SECTION;
    6007              : 
    6008        11857 :       gcc_assert (tmp_ss->dimen != 0);
    6009              : 
    6010        11857 :       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       185892 :   if (!loop->array_parameter)
    6017       116824 :     gfc_set_delta (loop);
    6018       185892 : }
    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       120655 : gfc_set_delta (gfc_loopinfo *loop)
    6027              : {
    6028       120655 :   gfc_ss *ss, **loopspec;
    6029       120655 :   gfc_array_info *info;
    6030       120655 :   tree tmp;
    6031       120655 :   int n, dim;
    6032              : 
    6033       120655 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    6034              : 
    6035       120655 :   loopspec = loop->specloop;
    6036              : 
    6037              :   /* Calculate the translation from loop variables to array indices.  */
    6038       365141 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    6039              :     {
    6040       244486 :       gfc_ss_type ss_type;
    6041              : 
    6042       244486 :       ss_type = ss->info->type;
    6043        62356 :       if (!(ss_type == GFC_SS_SECTION
    6044       244486 :             || ss_type == GFC_SS_COMPONENT
    6045        99290 :             || ss_type == GFC_SS_CONSTRUCTOR
    6046              :             || (ss_type == GFC_SS_FUNCTION
    6047         8390 :                 && gfc_is_class_array_function (ss->info->expr))))
    6048        62204 :         continue;
    6049              : 
    6050       182282 :       info = &ss->info->data.array;
    6051              : 
    6052       409182 :       for (n = 0; n < ss->dimen; n++)
    6053              :         {
    6054              :           /* If we are specifying the range the delta is already set.  */
    6055       226900 :           if (loopspec[n] != ss)
    6056              :             {
    6057       118010 :               dim = ss->dim[n];
    6058              : 
    6059              :               /* Calculate the offset relative to the loop variable.
    6060              :                  First multiply by the stride.  */
    6061       118010 :               tmp = loop->from[n];
    6062       118010 :               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       118010 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    6069              :                                      gfc_array_index_type,
    6070              :                                      info->start[dim], tmp);
    6071              : 
    6072       118010 :               if (ss->is_alloc_lhs)
    6073         9705 :                 info->delta[dim] = tmp;
    6074              :               else
    6075       108305 :                 info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
    6076              :             }
    6077              :         }
    6078              :     }
    6079              : 
    6080       124107 :   for (loop = loop->nested; loop; loop = loop->next)
    6081         3452 :     gfc_set_delta (loop);
    6082       120655 : }
    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        23711 : gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
    6092              : {
    6093        23711 :   tree res;
    6094        23711 :   tree cond;
    6095              : 
    6096              :   /* Calculate (ubound - lbound + 1).  */
    6097        23711 :   res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6098              :                          ubound, lbound);
    6099        23711 :   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        23711 :   cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
    6104              :                           gfc_index_zero_node);
    6105        23711 :   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        23711 :   if (or_expr)
    6110        18212 :     *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    6111              :                                 logical_type_node, *or_expr, cond);
    6112              : 
    6113        23711 :   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        12578 : 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        12578 :   tree type;
    6157        12578 :   tree tmp;
    6158        12578 :   tree size;
    6159        12578 :   tree offset;
    6160        12578 :   tree stride;
    6161        12578 :   tree or_expr;
    6162        12578 :   tree thencase;
    6163        12578 :   tree elsecase;
    6164        12578 :   tree cond;
    6165        12578 :   tree var;
    6166        12578 :   stmtblock_t thenblock;
    6167        12578 :   stmtblock_t elseblock;
    6168        12578 :   gfc_expr *ubound;
    6169        12578 :   gfc_se se;
    6170        12578 :   int n;
    6171              : 
    6172        12578 :   if (expr->ts.type == BT_CLASS
    6173         1794 :       && expr3_desc != NULL_TREE
    6174        12964 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
    6175          386 :     type = TREE_TYPE (expr3_desc);
    6176              :   else
    6177        12192 :     type = TREE_TYPE (descriptor);
    6178              : 
    6179              : 
    6180        12578 :   stride = gfc_index_one_node;
    6181        12578 :   offset = gfc_index_zero_node;
    6182              : 
    6183              :   /* Set the dtype before the alloc, because registration of coarrays needs
    6184              :      it initialized.  */
    6185        12578 :   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        12200 :   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        12033 :   else if (expr3_desc && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
    6216          976 :     gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6217              :                                    gfc_conv_descriptor_dtype_get (expr3_desc));
    6218        11057 :   else if (expr->ts.type == BT_CLASS && !explicit_ts
    6219         1408 :            && expr3 && expr3->ts.type != BT_CLASS
    6220          361 :            && expr3_elem_size != NULL_TREE && expr3_desc == NULL_TREE)
    6221              :     {
    6222          361 :       gfc_conv_descriptor_dtype_set (pblock, descriptor, gfc_get_dtype (type));
    6223          361 :       gfc_conv_descriptor_elem_len_set (pblock, descriptor, expr3_elem_size);
    6224              :     }
    6225              :   else
    6226        10696 :     gfc_conv_descriptor_dtype_set (pblock, descriptor, gfc_get_dtype (type));
    6227              : 
    6228        12578 :   or_expr = logical_false_node;
    6229              : 
    6230        30790 :   for (n = 0; n < rank; n++)
    6231              :     {
    6232        18212 :       tree conv_lbound;
    6233        18212 :       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        18212 :       ubound = upper[n];
    6240              : 
    6241              :       /* Set lower bound.  */
    6242        18212 :       gfc_init_se (&se, NULL);
    6243        18212 :       if (expr3_desc != NULL_TREE)
    6244              :         {
    6245         1519 :           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          919 :             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        16693 :       else if (lower == NULL)
    6256        13473 :         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        18212 :       gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
    6272              :                                       gfc_rank_cst[n], se.expr);
    6273        18212 :       conv_lbound = se.expr;
    6274              : 
    6275              :       /* Work out the offset for this component.  */
    6276        18212 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6277              :                              se.expr, stride);
    6278        18212 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    6279              :                                 gfc_array_index_type, offset, tmp);
    6280              : 
    6281              :       /* Set upper bound.  */
    6282        18212 :       gfc_init_se (&se, NULL);
    6283        18212 :       if (expr3_desc != NULL_TREE)
    6284              :         {
    6285         1519 :           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          919 :               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          919 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    6299              :                                      gfc_array_index_type, tmp,
    6300              :                                      gfc_index_one_node);
    6301          919 :               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        16693 :           gcc_assert (ubound);
    6310        16693 :           gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
    6311        16693 :           gfc_add_block_to_block (pblock, &se.pre);
    6312        16693 :           if (ubound->expr_type == EXPR_FUNCTION)
    6313          781 :             se.expr = gfc_evaluate_now (se.expr, pblock);
    6314              :         }
    6315        18212 :       gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
    6316              :                                       gfc_rank_cst[n], se.expr);
    6317        18212 :       conv_ubound = se.expr;
    6318              : 
    6319              :       /* Store the stride.  */
    6320        18212 :       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        18212 :       size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
    6325        18212 :       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        18212 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6333              :                              gfc_array_index_type,
    6334        18212 :                              fold_convert (gfc_array_index_type,
    6335              :                                            TYPE_MAX_VALUE (gfc_array_index_type)),
    6336              :                                            size);
    6337        18212 :       cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
    6338              :                                             logical_type_node, tmp, stride),
    6339              :                            PRED_FORTRAN_OVERFLOW);
    6340        18212 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6341              :                              integer_one_node, integer_zero_node);
    6342        18212 :       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        18212 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6347              :                              integer_zero_node, tmp);
    6348        18212 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    6349              :                              *overflow, tmp);
    6350        18212 :       *overflow = gfc_evaluate_now (tmp, pblock);
    6351              : 
    6352              :       /* Multiply the stride by the number of elements in this dimension.  */
    6353        18212 :       stride = fold_build2_loc (input_location, MULT_EXPR,
    6354              :                                 gfc_array_index_type, stride, size);
    6355        18212 :       stride = gfc_evaluate_now (stride, pblock);
    6356              :     }
    6357              : 
    6358        13303 :   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        12578 :   if (expr3_elem_size != NULL_TREE)
    6400         3229 :     tmp = expr3_elem_size;
    6401         9349 :   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         9349 :     tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    6422              : 
    6423              :   /* Convert to size_t.  */
    6424        12578 :   *element_size = fold_convert (size_type_node, tmp);
    6425              : 
    6426        12578 :   if (rank == 0)
    6427              :     return *element_size;
    6428              : 
    6429        12325 :   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        12325 :   tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6435              :                          size_type_node,
    6436        12325 :                          TYPE_MAX_VALUE (size_type_node), *element_size);
    6437        12325 :   cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
    6438              :                                         logical_type_node, tmp, stride),
    6439              :                        PRED_FORTRAN_OVERFLOW);
    6440        12325 :   tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6441              :                          integer_one_node, integer_zero_node);
    6442        12325 :   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        12325 :   tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6447              :                          integer_zero_node, tmp);
    6448        12325 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    6449              :                          *overflow, tmp);
    6450        12325 :   *overflow = gfc_evaluate_now (tmp, pblock);
    6451              : 
    6452        12325 :   size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6453              :                           stride, *element_size);
    6454              : 
    6455        12325 :   if (poffset != NULL)
    6456              :     {
    6457        12325 :       offset = gfc_evaluate_now (offset, pblock);
    6458        12325 :       *poffset = offset;
    6459              :     }
    6460              : 
    6461        12325 :   if (integer_zerop (or_expr))
    6462              :     return size;
    6463         3700 :   if (integer_onep (or_expr))
    6464          605 :     return build_int_cst (size_type_node, 0);
    6465              : 
    6466         3095 :   var = gfc_create_var (TREE_TYPE (size), "size");
    6467         3095 :   gfc_start_block (&thenblock);
    6468         3095 :   gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
    6469         3095 :   thencase = gfc_finish_block (&thenblock);
    6470              : 
    6471         3095 :   gfc_start_block (&elseblock);
    6472         3095 :   gfc_add_modify (&elseblock, var, size);
    6473         3095 :   elsecase = gfc_finish_block (&elseblock);
    6474              : 
    6475         3095 :   tmp = gfc_evaluate_now (or_expr, pblock);
    6476         3095 :   tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
    6477         3095 :   gfc_add_expr_to_block (pblock, tmp);
    6478              : 
    6479         3095 :   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        18055 : retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
    6488              : {
    6489        18055 :   gfc_ref *ref, *prev_ref;
    6490              : 
    6491        18055 :   ref = *ref_in;
    6492              :   /* Prevent warnings for uninitialized variables.  */
    6493        18055 :   prev_ref = *prev_ref_in;
    6494        25744 :   while (ref && ref->next != NULL)
    6495              :     {
    6496         7689 :       gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
    6497              :                   || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
    6498         7689 :       prev_ref = ref;
    6499         7689 :       ref = ref->next;
    6500              :     }
    6501              : 
    6502        18055 :   if (ref == NULL || ref->type != REF_ARRAY)
    6503              :     return false;
    6504              : 
    6505        12578 :   *ref_in = ref;
    6506        12578 :   *prev_ref_in = prev_ref;
    6507        12578 :   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        18055 : 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        18055 :   tree tmp;
    6521        18055 :   tree pointer;
    6522        18055 :   tree offset = NULL_TREE;
    6523        18055 :   tree token = NULL_TREE;
    6524        18055 :   tree size;
    6525        18055 :   tree msg;
    6526        18055 :   tree error = NULL_TREE;
    6527        18055 :   tree overflow; /* Boolean storing whether size calculation overflows.  */
    6528        18055 :   tree var_overflow = NULL_TREE;
    6529        18055 :   tree cond;
    6530        18055 :   tree set_descriptor;
    6531        18055 :   tree not_prev_allocated = NULL_TREE;
    6532        18055 :   tree element_size = NULL_TREE;
    6533        18055 :   stmtblock_t set_descriptor_block;
    6534        18055 :   stmtblock_t elseblock;
    6535        18055 :   gfc_expr **lower;
    6536        18055 :   gfc_expr **upper;
    6537        18055 :   gfc_ref *ref, *prev_ref = NULL, *coref;
    6538        18055 :   bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
    6539              :       non_ulimate_coarray_ptr_comp;
    6540        18055 :   tree omp_cond = NULL_TREE, omp_alt_alloc = NULL_TREE;
    6541              : 
    6542        18055 :   ref = expr->ref;
    6543              : 
    6544              :   /* Find the last reference in the chain.  */
    6545        18055 :   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        12578 :   if (!prev_ref)
    6551              :     {
    6552         8483 :       allocatable = expr->symtree->n.sym->attr.allocatable;
    6553         8483 :       dimension = expr->symtree->n.sym->attr.dimension;
    6554         8483 :       non_ulimate_coarray_ptr_comp = false;
    6555              :     }
    6556              :   else
    6557              :     {
    6558         4095 :       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         8190 :       non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
    6564         4095 :               && !prev_ref->u.c.component->attr.codimension);
    6565         4095 :       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        12578 :   coarray = false;
    6572        30360 :   for (coref = expr->ref; coref; coref = coref->next)
    6573        18507 :     if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
    6574              :       {
    6575              :         coarray = true;
    6576              :         break;
    6577              :       }
    6578              : 
    6579        12578 :   if (!dimension)
    6580          253 :     gcc_assert (coarray);
    6581              : 
    6582        12578 :   gfc_array_ref *ar;
    6583        12578 :   if (ref->u.ar.type == AR_FULL && expr3 != NULL)
    6584              :     {
    6585              :       /* F08:C633: Array shape from expr3.  */
    6586         1261 :       ar = gfc_find_array_ref (expr3, true);
    6587         1261 :       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        11317 :     ar = &ref->u.ar;
    6599              : 
    6600              :   /* Figure out the size of the array.  */
    6601        12578 :   switch (ar->type)
    6602              :     {
    6603         9641 :     case AR_ELEMENT:
    6604         9641 :       if (!coarray)
    6605              :         {
    6606         8968 :           lower = NULL;
    6607         8968 :           upper = ar->start;
    6608         8968 :           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         1212 :     case AR_FULL:
    6618         1212 :       gcc_assert (ar->as->type == AS_EXPLICIT
    6619              :                   || alloc_w_e3_arr_spec);
    6620              : 
    6621         1212 :       lower = ar->as->lower;
    6622         1212 :       upper = ar->as->upper;
    6623         1212 :       break;
    6624              : 
    6625            0 :     default:
    6626            0 :       gcc_unreachable ();
    6627        12578 :       break;
    6628              :     }
    6629              : 
    6630        12578 :   overflow = integer_zero_node;
    6631              : 
    6632        12578 :   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        12578 :   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        24620 :   size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
    6645        11317 :                                                            : 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        12578 :   if (dimension)
    6654              :     {
    6655        12325 :       var_overflow = gfc_create_var (integer_type_node, "overflow");
    6656        12325 :       gfc_add_modify (&se->pre, var_overflow, overflow);
    6657              : 
    6658        12325 :       if (status == NULL_TREE)
    6659              :         {
    6660              :           /* Generate the block of code handling overflow.  */
    6661        12084 :           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        12084 :           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        12578 :   if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
    6682            0 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    6683              : 
    6684        12578 :   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        12113 :       pointer = gfc_conv_descriptor_data_get (se->expr);
    6694        12113 :       if (omp_alloc)
    6695           33 :         omp_cond = boolean_true_node;
    6696              :     }
    6697        12578 :   STRIP_NOPS (pointer);
    6698              : 
    6699        12578 :   if (allocatable)
    6700              :     {
    6701        10367 :       not_prev_allocated = gfc_create_var (logical_type_node,
    6702              :                                            "not_prev_allocated");
    6703        10367 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
    6704              :                              logical_type_node, pointer,
    6705        10367 :                              build_int_cst (TREE_TYPE (pointer), 0));
    6706              : 
    6707        10367 :       gfc_add_modify (&se->pre, not_prev_allocated, tmp);
    6708              :     }
    6709              : 
    6710        12578 :   gfc_start_block (&elseblock);
    6711              : 
    6712        12578 :   tree succ_add_expr = NULL_TREE;
    6713        12578 :   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        12578 :   if (allocatable)
    6760        11017 :     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        12578 :   if (dimension && !(allocatable && status))
    6777              :     {
    6778        12102 :       cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
    6779              :                            logical_type_node, var_overflow, integer_zero_node),
    6780              :                            PRED_FORTRAN_OVERFLOW);
    6781        12102 :       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        12578 :   gfc_add_expr_to_block (&se->pre, tmp);
    6788              : 
    6789              :   /* Update the array descriptor with the offset and the span.  */
    6790        12578 :   if (dimension)
    6791              :     {
    6792        12325 :       gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
    6793        12325 :       tmp = fold_convert (gfc_array_index_type, element_size);
    6794        12325 :       gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
    6795              :     }
    6796              : 
    6797        12578 :   set_descriptor = gfc_finish_block (&set_descriptor_block);
    6798        12578 :   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        12315 :       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         8174 : gfc_conv_array_initializer (tree type, gfc_expr * expr)
    6826              : {
    6827         8174 :   gfc_constructor *c;
    6828         8174 :   tree tmp;
    6829         8174 :   gfc_se se;
    6830         8174 :   tree index, range;
    6831         8174 :   vec<constructor_elt, va_gc> *v = NULL;
    6832              : 
    6833         8174 :   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         8174 :     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         8174 :   if (expr->expr_type != EXPR_CONSTANT
    6843         7672 :       && expr->expr_type != EXPR_STRUCTURE
    6844         6778 :       && 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         8170 :   switch (expr->expr_type)
    6853              :     {
    6854         1396 :     case EXPR_CONSTANT:
    6855         1396 :     case EXPR_STRUCTURE:
    6856              :       /* A single scalar or derived type value.  Create an array with all
    6857              :          elements equal to that value.  */
    6858         1396 :       gfc_init_se (&se, NULL);
    6859              : 
    6860         1396 :       if (expr->expr_type == EXPR_CONSTANT)
    6861          502 :         gfc_conv_constant (&se, expr);
    6862              :       else
    6863          894 :         gfc_conv_structure (&se, expr, 1);
    6864              : 
    6865         2792 :       if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
    6866         1396 :                            TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
    6867              :         break;
    6868         2768 :       else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
    6869         1384 :                                    TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
    6870          179 :         range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
    6871              :       else
    6872         2410 :         range = build2 (RANGE_EXPR, gfc_array_index_type,
    6873         1205 :                         TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
    6874         1205 :                         TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
    6875         1384 :       CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
    6876         1384 :       break;
    6877              : 
    6878         6774 :     case EXPR_ARRAY:
    6879              :       /* Create a vector of all the elements.  */
    6880         6774 :       for (c = gfc_constructor_first (expr->value.constructor);
    6881       165539 :            c && c->expr; c = gfc_constructor_next (c))
    6882              :         {
    6883       158765 :           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       158765 :           index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
    6895              : 
    6896       158765 :           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       158765 :           gfc_init_se (&se, NULL);
    6920       158765 :           switch (c->expr->expr_type)
    6921              :             {
    6922       157240 :             case EXPR_CONSTANT:
    6923       157240 :               gfc_conv_constant (&se, c->expr);
    6924              : 
    6925              :               /* See gfortran.dg/charlen_15.f90 for instance.  */
    6926       157240 :               if (TREE_CODE (se.expr) == STRING_CST
    6927         5568 :                   && TREE_CODE (type) == ARRAY_TYPE)
    6928              :                 {
    6929              :                   tree atype = type;
    6930        11136 :                   while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
    6931         5568 :                     atype = TREE_TYPE (atype);
    6932         5568 :                   gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
    6933              :                                        == INTEGER_TYPE);
    6934         5568 :                   gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
    6935              :                                        == TREE_TYPE (atype));
    6936         5568 :                   if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
    6937         5568 :                       > 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         5568 :                   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       158765 :           if (range == NULL_TREE)
    6962       158638 :             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       158892 :               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         8170 :   tmp = build_constructor (type, v);
    6981         8170 :   TREE_CONSTANT (tmp) = 1;
    6982         8170 :   return tmp;
    6983              : }
    6984              : 
    6985              : 
    6986              : /* Generate code to evaluate non-constant coarray cobounds.  */
    6987              : 
    6988              : void
    6989        23582 : gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
    6990              :                           const gfc_symbol *sym)
    6991              : {
    6992        23582 :   int dim;
    6993        23582 :   tree ubound;
    6994        23582 :   tree lbound;
    6995        23582 :   gfc_se se;
    6996        23582 :   gfc_array_spec *as;
    6997              : 
    6998        23582 :   as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    6999              : 
    7000        26022 :   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         2440 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    7008         2440 :       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         2440 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    7017         2440 :       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        23582 : }
    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        15678 : gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
    7034              :                         stmtblock_t * pblock)
    7035              : {
    7036        15678 :   gfc_array_spec *as;
    7037        15678 :   tree size;
    7038        15678 :   tree stride;
    7039        15678 :   tree offset;
    7040        15678 :   tree ubound;
    7041        15678 :   tree lbound;
    7042        15678 :   tree tmp;
    7043        15678 :   gfc_se se;
    7044              : 
    7045        15678 :   int dim;
    7046              : 
    7047        15678 :   as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    7048              : 
    7049        15678 :   size = gfc_index_one_node;
    7050        15678 :   offset = gfc_index_zero_node;
    7051        15678 :   stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
    7052        15678 :   if (stride && VAR_P (stride))
    7053          124 :     gfc_add_modify (pblock, stride, gfc_index_one_node);
    7054        33189 :   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        17511 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    7062        17511 :       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        17511 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    7071        17511 :       if (as->upper[dim] && !INTEGER_CST_P (ubound))
    7072              :         {
    7073        10869 :           gfc_init_se (&se, NULL);
    7074        10869 :           gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
    7075        10869 :           gfc_add_block_to_block (pblock, &se.pre);
    7076        10869 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7077        10869 :           gfc_add_modify (pblock, ubound, se.expr);
    7078              :         }
    7079              :       /* The offset of this dimension.  offset = offset - lbound * stride.  */
    7080        17511 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7081              :                              lbound, size);
    7082        17511 :       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        17511 :       if (dim + 1 < as->rank)
    7087         3468 :         stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
    7088              :       else
    7089        14043 :         stride = GFC_TYPE_ARRAY_SIZE (type);
    7090              : 
    7091        17511 :       if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
    7092              :         {
    7093              :           /* Calculate stride = size * (ubound + 1 - lbound).  */
    7094        11060 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7095              :                                  gfc_array_index_type,
    7096              :                                  gfc_index_one_node, lbound);
    7097        11060 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7098              :                                  gfc_array_index_type, ubound, tmp);
    7099        11060 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    7100              :                                  gfc_array_index_type, size, tmp);
    7101        11060 :           if (stride)
    7102        11060 :             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        11060 :           tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    7109              :                                  stride, gfc_index_zero_node);
    7110        11060 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    7111              :                                  gfc_array_index_type, tmp,
    7112              :                                  stride, gfc_index_zero_node);
    7113        11060 :           gfc_add_modify (pblock, stride, tmp);
    7114              :         }
    7115              : 
    7116        17511 :       size = stride;
    7117              :     }
    7118              : 
    7119        15678 :   gfc_trans_array_cobounds (type, pblock, sym);
    7120        15678 :   gfc_trans_vla_type_sizes (sym, pblock);
    7121              : 
    7122        15678 :   *poffset = offset;
    7123        15678 :   return size;
    7124              : }
    7125              : 
    7126              : 
    7127              : /* Generate code to initialize/allocate an array variable.  */
    7128              : 
    7129              : void
    7130        32715 : gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
    7131              :                                  gfc_wrapped_block * block)
    7132              : {
    7133        32715 :   stmtblock_t init;
    7134        32715 :   tree type;
    7135        32715 :   tree tmp = NULL_TREE;
    7136        32715 :   tree size;
    7137        32715 :   tree offset;
    7138        32715 :   tree space;
    7139        32715 :   tree inittree;
    7140        32715 :   bool onstack;
    7141        32715 :   bool back;
    7142              : 
    7143        32715 :   gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
    7144              : 
    7145              :   /* Do nothing for USEd variables.  */
    7146        32715 :   if (sym->attr.use_assoc)
    7147        26371 :     return;
    7148              : 
    7149        32666 :   type = TREE_TYPE (decl);
    7150        32666 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7151        32666 :   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        32666 :   back = sym->fn_result_dep;
    7158              : 
    7159        32666 :   gfc_init_block (&init);
    7160              : 
    7161              :   /* Evaluate character string length.  */
    7162        32666 :   if (sym->ts.type == BT_CHARACTER
    7163         3170 :       && 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        32464 :   if (onstack)
    7185              :     {
    7186        26182 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
    7187              :                             back);
    7188        26182 :       return;
    7189              :     }
    7190              : 
    7191         6484 :   type = TREE_TYPE (type);
    7192              : 
    7193         6484 :   gcc_assert (!sym->attr.use_assoc);
    7194         6484 :   gcc_assert (!sym->module);
    7195              : 
    7196         6484 :   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         6484 :   size = gfc_trans_array_bounds (type, sym, &offset, &init);
    7201              : 
    7202              :   /* Don't actually allocate space for Cray Pointees.  */
    7203         6484 :   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         6344 :   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         6337 :   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         6319 :       tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    7239         6319 :       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         6319 :       tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
    7244         6319 :       gfc_add_modify (&init, decl, tmp);
    7245              : 
    7246              :       /* Free the temporary.  */
    7247         6319 :       tmp = gfc_call_free (decl);
    7248         6319 :       space = NULL_TREE;
    7249              :     }
    7250              : 
    7251              :   /* Set offset of the array.  */
    7252         6344 :   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         6344 :   gcc_assert (!sym->value);
    7257              : 
    7258         6344 :   inittree = gfc_finish_block (&init);
    7259              : 
    7260         6344 :   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         6344 :   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         8940 : gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
    7286              : {
    7287         8940 :   tree parm;
    7288         8940 :   tree type;
    7289         8940 :   tree offset;
    7290         8940 :   tree tmp;
    7291         8940 :   tree stmt;
    7292         8940 :   stmtblock_t init;
    7293              : 
    7294         8940 :   location_t loc = input_location;
    7295         8940 :   input_location = gfc_get_location (&sym->declared_at);
    7296              : 
    7297              :   /* Descriptor type.  */
    7298         8940 :   parm = sym->backend_decl;
    7299         8940 :   type = TREE_TYPE (parm);
    7300         8940 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7301              : 
    7302         8940 :   gfc_start_block (&init);
    7303              : 
    7304         8940 :   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         8940 :   gfc_trans_array_bounds (type, sym, &offset, &init);
    7310              : 
    7311              :   /* Set the offset.  */
    7312         8940 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7313         1214 :     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         8940 :   if (TREE_CODE (parm) != PARM_DECL)
    7317              :     {
    7318          618 :       tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
    7319          618 :       if (sym->ts.type == BT_CLASS)
    7320              :         {
    7321          243 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    7322          243 :           tmp = gfc_class_data_get (tmp);
    7323          243 :           tmp = gfc_conv_descriptor_data_get (tmp);
    7324              :         }
    7325          618 :       tmp = convert (TREE_TYPE (parm), tmp);
    7326          618 :       gfc_add_modify (&init, parm, tmp);
    7327              :     }
    7328         8940 :   stmt = gfc_finish_block (&init);
    7329              : 
    7330         8940 :   input_location = loc;
    7331              : 
    7332              :   /* Add the initialization code to the start of the function.  */
    7333              : 
    7334         8940 :   if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
    7335         8940 :       || sym->attr.optional
    7336         8446 :       || sym->attr.not_always_present)
    7337              :     {
    7338          554 :       tree nullify;
    7339          554 :       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          449 :         nullify = build_empty_stmt (input_location);
    7344          554 :       tmp = gfc_conv_expr_present (sym, true);
    7345          554 :       stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
    7346              :     }
    7347              : 
    7348         8940 :   gfc_add_init_cleanup (block, stmt, NULL_TREE);
    7349         8940 : }
    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        15073 : gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
    7365              :                             gfc_wrapped_block * block)
    7366              : {
    7367        15073 :   tree size;
    7368        15073 :   tree type;
    7369        15073 :   tree offset;
    7370        15073 :   stmtblock_t init;
    7371        15073 :   tree stmtInit, stmtCleanup;
    7372        15073 :   tree lbound;
    7373        15073 :   tree ubound;
    7374        15073 :   tree dubound;
    7375        15073 :   tree dlbound;
    7376        15073 :   tree dumdesc;
    7377        15073 :   tree tmp;
    7378        15073 :   tree stride, stride2;
    7379        15073 :   tree stmt_packed;
    7380        15073 :   tree stmt_unpacked;
    7381        15073 :   tree partial;
    7382        15073 :   gfc_se se;
    7383        15073 :   int n;
    7384        15073 :   int checkparm;
    7385        15073 :   int no_repack;
    7386        15073 :   bool optional_arg;
    7387        15073 :   gfc_array_spec *as;
    7388        15073 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    7389              : 
    7390              :   /* Do nothing for pointer and allocatable arrays.  */
    7391        15073 :   if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    7392        14976 :       || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
    7393        14976 :       || sym->attr.allocatable
    7394        14870 :       || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
    7395         7596 :     return;
    7396              : 
    7397          911 :   if ((!is_classarray
    7398          911 :        || (is_classarray && CLASS_DATA (sym)->as->type == AS_EXPLICIT))
    7399        14143 :       && sym->attr.dummy && !sym->attr.elemental && gfc_is_nodesc_array (sym))
    7400              :     {
    7401         7393 :       gfc_trans_g77_array (sym, block);
    7402         7393 :       return;
    7403              :     }
    7404              : 
    7405         7477 :   location_t loc = input_location;
    7406         7477 :   input_location = gfc_get_location (&sym->declared_at);
    7407              : 
    7408              :   /* Descriptor type.  */
    7409         7477 :   type = TREE_TYPE (tmpdesc);
    7410         7477 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7411         7477 :   dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7412         7477 :   if (is_classarray)
    7413              :     /* For a class array the dummy array descriptor is in the _class
    7414              :        component.  */
    7415          746 :     dumdesc = gfc_class_data_get (dumdesc);
    7416              :   else
    7417         6731 :     dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
    7418         7477 :   as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    7419         7477 :   gfc_start_block (&init);
    7420              : 
    7421         7477 :   if (sym->ts.type == BT_CHARACTER
    7422          822 :       && VAR_P (sym->ts.u.cl->backend_decl))
    7423           87 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7424              : 
    7425              :   /* TODO: Fix the exclusion of class arrays from extent checking.  */
    7426         1096 :   checkparm = (as->type == AS_EXPLICIT && !is_classarray
    7427         8554 :                && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
    7428              : 
    7429         7477 :   no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
    7430         7476 :                 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
    7431              : 
    7432         7477 :   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         7477 :   if (no_repack)
    7450              :     {
    7451              :       /* Set the first stride.  */
    7452         7475 :       stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
    7453         7475 :       stride = gfc_evaluate_now (stride, &init);
    7454              : 
    7455         7475 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7456              :                              stride, gfc_index_zero_node);
    7457         7475 :       tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    7458              :                              tmp, gfc_index_one_node, stride);
    7459         7475 :       stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
    7460         7475 :       gfc_add_modify (&init, stride, tmp);
    7461              : 
    7462              :       /* Allow the user to disable array repacking.  */
    7463         7475 :       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         7477 :   if (no_repack || partial != NULL_TREE)
    7487         7476 :     stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
    7488              :   else
    7489              :     stmt_packed = NULL_TREE;
    7490              : 
    7491              :   /* Assign the data pointer.  */
    7492         7477 :   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         7476 :     tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
    7500         7477 :   gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
    7501              : 
    7502         7477 :   offset = gfc_index_zero_node;
    7503         7477 :   size = gfc_index_one_node;
    7504              : 
    7505              :   /* Evaluate the bounds of the array.  */
    7506        17397 :   for (n = 0; n < as->rank; n++)
    7507              :     {
    7508         9920 :       if (checkparm || !as->upper[n])
    7509              :         {
    7510              :           /* Get the bounds of the actual parameter.  */
    7511         8589 :           dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
    7512         8589 :           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         9920 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
    7521         9920 :       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         9920 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
    7531              :       /* Set the desired upper bound.  */
    7532         9920 :       if (as->upper[n])
    7533              :         {
    7534              :           /* We know what we want the upper bound to be.  */
    7535         1389 :           if (!INTEGER_CST_P (ubound))
    7536              :             {
    7537          639 :               gfc_init_se (&se, NULL);
    7538          639 :               gfc_conv_expr_type (&se, as->upper[n],
    7539              :                                   gfc_array_index_type);
    7540          639 :               gfc_add_block_to_block (&init, &se.pre);
    7541          639 :               gfc_add_modify (&init, ubound, se.expr);
    7542              :             }
    7543              : 
    7544              :           /* Check the sizes match.  */
    7545         1389 :           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         8531 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7581              :                                  gfc_array_index_type, dubound, dlbound);
    7582         8531 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7583              :                                  gfc_array_index_type, tmp, lbound);
    7584         8531 :           gfc_add_modify (&init, ubound, tmp);
    7585              :         }
    7586              :       /* The offset of this dimension.  offset = offset - lbound * stride.  */
    7587         9920 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7588              :                              lbound, stride);
    7589         9920 :       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         9920 :       if (n + 1 < as->rank)
    7594              :         {
    7595         2443 :           stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
    7596              : 
    7597         2443 :           if (no_repack || partial != NULL_TREE)
    7598         2442 :             stmt_unpacked =
    7599         2442 :               gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
    7600              : 
    7601              :           /* Figure out the stride if not a known constant.  */
    7602         2443 :           if (!INTEGER_CST_P (stride))
    7603              :             {
    7604         2442 :               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         2442 :               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         2442 :                 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
    7626         2442 :               gfc_add_modify (&init, stride, tmp);
    7627              :             }
    7628              :         }
    7629              :       else
    7630              :         {
    7631         7477 :           stride = GFC_TYPE_ARRAY_SIZE (type);
    7632              : 
    7633         7477 :           if (stride && !INTEGER_CST_P (stride))
    7634              :             {
    7635              :               /* Calculate size = stride * (ubound + 1 - lbound).  */
    7636         7476 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7637              :                                      gfc_array_index_type,
    7638              :                                      gfc_index_one_node, lbound);
    7639         7476 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7640              :                                      gfc_array_index_type,
    7641              :                                      ubound, tmp);
    7642        22428 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    7643              :                                      gfc_array_index_type,
    7644         7476 :                                      GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
    7645         7476 :               gfc_add_modify (&init, stride, tmp);
    7646              :             }
    7647              :         }
    7648              :     }
    7649              : 
    7650         7477 :   gfc_trans_array_cobounds (type, &init, sym);
    7651              : 
    7652              :   /* Set the offset.  */
    7653         7477 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7654         7475 :     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         7477 :   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         7477 :   if (tree span = GFC_DECL_GET_SPAN (tmpdesc))
    7697          222 :     gfc_add_modify (&init, span, gfc_conv_descriptor_span_get (dumdesc));
    7698              : 
    7699         7477 :   gfc_trans_vla_type_sizes (sym, &init);
    7700              : 
    7701         7477 :   stmtInit = gfc_finish_block (&init);
    7702              : 
    7703              :   /* Only do the entry/initialization code if the arg is present.  */
    7704         7477 :   dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7705         7477 :   optional_arg = (sym->attr.optional
    7706         7477 :                   || (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         7477 :   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         7477 :   gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
    7763              : 
    7764         7477 :   input_location = loc;
    7765              : }
    7766              : 
    7767              : 
    7768              : /* Calculate the overall offset, including subreferences.  */
    7769              : void
    7770        61700 : gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
    7771              :                         bool subref, gfc_expr *expr)
    7772              : {
    7773        61700 :   tree tmp;
    7774        61700 :   tree field;
    7775        61700 :   tree stride;
    7776        61700 :   tree index;
    7777        61700 :   gfc_ref *ref;
    7778        61700 :   gfc_se start;
    7779        61700 :   int n;
    7780              : 
    7781              :   /* If offset is NULL and this is not a subreferenced array, there is
    7782              :      nothing to do.  */
    7783        61700 :   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        60769 :   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        60769 :   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        60769 :   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        60769 :   if (expr->expr_type == EXPR_VARIABLE
    7905        53250 :       && expr->symtree->n.sym->attr.dummy
    7906         6554 :       && expr->symtree->n.sym->attr.optional
    7907        61761 :       && !is_CFI_desc (NULL, expr))
    7908         1624 :     offset = build3_loc (input_location, COND_EXPR, TREE_TYPE (offset),
    7909          812 :                          gfc_conv_expr_present (expr->symtree->n.sym), offset,
    7910          812 :                          fold_convert (TREE_TYPE (offset), gfc_index_zero_node));
    7911              : 
    7912        60769 :   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         1572 : get_array_charlen (gfc_expr *expr, gfc_se *se)
    7924              : {
    7925         1572 :   gfc_interface_mapping mapping;
    7926         1572 :   gfc_formal_arglist *formal;
    7927         1572 :   gfc_actual_arglist *arg;
    7928         1572 :   gfc_se tse;
    7929         1572 :   gfc_expr *e;
    7930              : 
    7931         1572 :   if (expr->ts.u.cl->length
    7932         1572 :         && gfc_is_constant_expr (expr->ts.u.cl->length))
    7933              :     {
    7934         1225 :       if (!expr->ts.u.cl->backend_decl)
    7935          459 :         gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    7936         1357 :       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       181874 :   for (n = 0; n < ss->dimen; n++)
    8068        90867 :     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       148732 : is_explicit_coarray (gfc_expr *expr)
    8146              : {
    8147       148732 :   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       165745 : gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
    8185              : {
    8186       165745 :   gfc_ss *ss;
    8187       165745 :   gfc_ss_type ss_type;
    8188       165745 :   gfc_ss_info *ss_info;
    8189       165745 :   gfc_loopinfo loop;
    8190       165745 :   gfc_array_info *info;
    8191       165745 :   int need_tmp;
    8192       165745 :   int n;
    8193       165745 :   tree tmp;
    8194       165745 :   tree desc;
    8195       165745 :   stmtblock_t block;
    8196       165745 :   tree start;
    8197       165745 :   int full;
    8198       165745 :   bool subref_array_target = false;
    8199       165745 :   bool deferred_array_component = false;
    8200       165745 :   bool substr = false;
    8201       165745 :   gfc_expr *arg, *ss_expr;
    8202              : 
    8203       165745 :   if (se->want_coarray || expr->rank == 0)
    8204         1657 :     ss = walk_coarray (expr);
    8205              :   else
    8206       164088 :     ss = gfc_walk_expr (expr);
    8207              : 
    8208       165745 :   gcc_assert (ss != NULL);
    8209       165745 :   gcc_assert (ss != gfc_ss_terminator);
    8210              : 
    8211       165745 :   ss_info = ss->info;
    8212       165745 :   ss_type = ss_info->type;
    8213       165745 :   ss_expr = ss_info->expr;
    8214              : 
    8215              :   /* Special case: TRANSPOSE which needs no temporary.  */
    8216       171354 :   while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
    8217       170940 :          && (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          444 :       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
    8222          444 :       expr = expr->value.function.actual->expr;
    8223              :     }
    8224              : 
    8225       165745 :   if (!se->direct_byref)
    8226       318295 :     se->unlimited_polymorphic = UNLIMITED_POLY (expr);
    8227              : 
    8228              :   /* Special case things we know we can pass easily.  */
    8229       165745 :   switch (expr->expr_type)
    8230              :     {
    8231       149035 :     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       149035 :       gcc_assert (ss_type == GFC_SS_SECTION);
    8236       149035 :       gcc_assert (ss_expr == expr);
    8237       149035 :       info = &ss_info->data.array;
    8238              : 
    8239              :       /* Get the descriptor for the array.  */
    8240       149035 :       gfc_conv_ss_descriptor (&se->pre, ss, 0);
    8241       149035 :       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       149035 :       if (expr->ts.type == BT_CHARACTER
    8247        20419 :           && expr->ts.deferred
    8248         2878 :           && TREE_CODE (desc) == COMPONENT_REF)
    8249       149035 :         deferred_array_component = true;
    8250              : 
    8251       149035 :       substr = info->ref && info->ref->next
    8252       150196 :                && info->ref->next->type == REF_SUBSTRING;
    8253              : 
    8254       149035 :       subref_array_target = (is_subref_array (expr)
    8255       149035 :                              && (se->direct_byref
    8256         3768 :                                  || se->force_no_tmp
    8257         3073 :                                  || expr->ts.type == BT_CHARACTER));
    8258       149035 :       need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
    8259       149035 :                   && !subref_array_target);
    8260              : 
    8261       149035 :       if (se->force_tmp)
    8262              :         need_tmp = 1;
    8263       148834 :       else if (se->force_no_tmp)
    8264              :         need_tmp = 0;
    8265              : 
    8266       142129 :       if (need_tmp)
    8267              :         full = 0;
    8268       148732 :       else if (is_explicit_coarray (expr))
    8269              :         full = 0;
    8270       147855 :       else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
    8271              :         {
    8272              :           /* Create a new descriptor if the array doesn't have one.  */
    8273              :           full = 0;
    8274              :         }
    8275        97126 :       else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
    8276              :         full = 1;
    8277         8190 :       else if (se->direct_byref)
    8278              :         full = 0;
    8279         7827 :       else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
    8280              :         full = 1;
    8281         7617 :       else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
    8282              :         full = 0;
    8283              :       else
    8284         3675 :         full = gfc_full_array_ref_p (info->ref, NULL);
    8285              : 
    8286              :       /* A subobject of the array elements is described by a new descriptor,
    8287              :          whose element type is that of the subobject and whose span is the
    8288              :          element size of the array.  */
    8289       149035 :       if (subref_array_target && !se->direct_byref
    8290         1446 :           && info->ref && info->ref->next)
    8291              :         full = 0;
    8292              : 
    8293       238052 :       if (full && !transposed_dims (ss))
    8294              :         {
    8295        89336 :           if (se->direct_byref && !se->byref_noassign)
    8296              :             {
    8297         1108 :               struct lang_type *lhs_ls
    8298         1108 :                 = TYPE_LANG_SPECIFIC (TREE_TYPE (se->expr)),
    8299         1108 :                 *rhs_ls = TYPE_LANG_SPECIFIC (TREE_TYPE (desc));
    8300              :               /* When only the array_kind differs, do a view_convert.  */
    8301         1516 :               tmp = lhs_ls && rhs_ls && lhs_ls->rank == rhs_ls->rank
    8302         1108 :                         && lhs_ls->akind != rhs_ls->akind
    8303         1516 :                       ? build1 (VIEW_CONVERT_EXPR, TREE_TYPE (se->expr), desc)
    8304              :                       : desc;
    8305              :               /* Copy the descriptor for pointer assignments.  */
    8306         1108 :               gfc_add_modify (&se->pre, se->expr, tmp);
    8307              : 
    8308              :               /* Add any offsets from subreferences.  */
    8309         1108 :               gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
    8310              :                                       subref_array_target, expr);
    8311              : 
    8312              :               /* ....and set the span field.  */
    8313         1108 :               if (ss_info->expr->ts.type == BT_CHARACTER)
    8314          147 :                 tmp = gfc_conv_descriptor_span_get (desc);
    8315              :               else
    8316          961 :                 tmp = gfc_get_array_span (desc, expr);
    8317         1108 :               gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
    8318         1108 :             }
    8319        88228 :           else if (se->want_pointer)
    8320              :             {
    8321              :               /* We pass full arrays directly.  This means that pointers and
    8322              :                  allocatable arrays should also work.  */
    8323        14296 :               se->expr = gfc_build_addr_expr (NULL_TREE, desc);
    8324              :             }
    8325              :           else
    8326              :             {
    8327        73932 :               se->expr = desc;
    8328              :             }
    8329              : 
    8330        89336 :           if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
    8331         8516 :             se->string_length = gfc_get_expr_charlen (expr);
    8332              :           /* The ss_info string length is returned set to the value of the
    8333              :              hidden string length component.  */
    8334        80551 :           else if (deferred_array_component)
    8335          269 :             se->string_length = ss_info->string_length;
    8336              : 
    8337        89336 :           se->class_container = ss_info->class_container;
    8338              : 
    8339        89336 :           gfc_free_ss_chain (ss);
    8340       178828 :           return;
    8341              :         }
    8342              :       break;
    8343              : 
    8344         5165 :     case EXPR_FUNCTION:
    8345              :       /* A transformational function return value will be a temporary
    8346              :          array descriptor.  We still need to go through the scalarizer
    8347              :          to create the descriptor.  Elemental functions are handled as
    8348              :          arbitrary expressions, i.e. copy to a temporary.  */
    8349              : 
    8350         5165 :       if (se->direct_byref)
    8351              :         {
    8352          156 :           gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
    8353              : 
    8354              :           /* For pointer assignments pass the descriptor directly.  */
    8355          156 :           if (se->ss == NULL)
    8356          156 :             se->ss = ss;
    8357              :           else
    8358            0 :             gcc_assert (se->ss == ss);
    8359              : 
    8360          156 :           se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    8361          156 :           gfc_conv_expr (se, expr);
    8362              : 
    8363          156 :           gfc_free_ss_chain (ss);
    8364          156 :           return;
    8365              :         }
    8366              : 
    8367         5009 :       if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
    8368              :         {
    8369         3325 :           if (ss_expr != expr)
    8370              :             /* Elemental function.  */
    8371         2576 :             gcc_assert ((expr->value.function.esym != NULL
    8372              :                          && expr->value.function.esym->attr.elemental)
    8373              :                         || (expr->value.function.isym != NULL
    8374              :                             && expr->value.function.isym->elemental)
    8375              :                         || (gfc_expr_attr (expr).proc_pointer
    8376              :                             && gfc_expr_attr (expr).elemental)
    8377              :                         || gfc_inline_intrinsic_function_p (expr));
    8378              : 
    8379         3325 :           need_tmp = 1;
    8380         3325 :           if (expr->ts.type == BT_CHARACTER
    8381           35 :                 && expr->ts.u.cl->length
    8382           29 :                 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    8383           13 :             get_array_charlen (expr, se);
    8384              : 
    8385              :           info = NULL;
    8386              :         }
    8387              :       else
    8388              :         {
    8389              :           /* Transformational function.  */
    8390         1684 :           info = &ss_info->data.array;
    8391         1684 :           need_tmp = 0;
    8392              :         }
    8393              :       break;
    8394              : 
    8395        10817 :     case EXPR_ARRAY:
    8396              :       /* Constant array constructors don't need a temporary.  */
    8397        10817 :       if (ss_type == GFC_SS_CONSTRUCTOR
    8398        10817 :           && expr->ts.type != BT_CHARACTER
    8399        20375 :           && gfc_constant_array_constructor_p (expr->value.constructor))
    8400              :         {
    8401         7506 :           need_tmp = 0;
    8402         7506 :           info = &ss_info->data.array;
    8403              :         }
    8404              :       else
    8405              :         {
    8406              :           need_tmp = 1;
    8407              :           info = NULL;
    8408              :         }
    8409              :       break;
    8410              : 
    8411              :     default:
    8412              :       /* Something complicated.  Copy it into a temporary.  */
    8413              :       need_tmp = 1;
    8414              :       info = NULL;
    8415              :       break;
    8416              :     }
    8417              : 
    8418              :   /* If we are creating a temporary, we don't need to bother about aliases
    8419              :      anymore.  */
    8420        68902 :   if (need_tmp)
    8421         7667 :     se->force_tmp = 0;
    8422              : 
    8423        76253 :   gfc_init_loopinfo (&loop);
    8424              : 
    8425              :   /* Associate the SS with the loop.  */
    8426        76253 :   gfc_add_ss_to_loop (&loop, ss);
    8427              : 
    8428              :   /* Tell the scalarizer not to bother creating loop variables, etc.  */
    8429        76253 :   if (!need_tmp)
    8430        68586 :     loop.array_parameter = 1;
    8431              :   else
    8432              :     /* The right-hand side of a pointer assignment mustn't use a temporary.  */
    8433         7667 :     gcc_assert (!se->direct_byref);
    8434              : 
    8435              :   /* Do we need bounds checking or not?  */
    8436        76253 :   ss->no_bounds_check = expr->no_bounds_check;
    8437              : 
    8438              :   /* Setup the scalarizing loops and bounds.  */
    8439        76253 :   gfc_conv_ss_startstride (&loop);
    8440              : 
    8441              :   /* Add bounds-checking for elemental dimensions.  */
    8442        76253 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check)
    8443         6688 :     array_bound_check_elemental (&outermost_loop (&loop)->pre, ss, expr);
    8444              : 
    8445        76253 :   if (need_tmp)
    8446              :     {
    8447         7667 :       if (expr->ts.type == BT_CHARACTER
    8448         1487 :           && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
    8449         1379 :         get_array_charlen (expr, se);
    8450              : 
    8451              :       /* Tell the scalarizer to make a temporary.  */
    8452         7667 :       loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
    8453         7667 :                                       ((expr->ts.type == BT_CHARACTER)
    8454         1487 :                                        ? expr->ts.u.cl->backend_decl
    8455              :                                        : NULL),
    8456              :                                       loop.dimen);
    8457              : 
    8458         7667 :       se->string_length = loop.temp_ss->info->string_length;
    8459         7667 :       gcc_assert (loop.temp_ss->dimen == loop.dimen);
    8460         7667 :       gfc_add_ss_to_loop (&loop, loop.temp_ss);
    8461              :     }
    8462              : 
    8463        76253 :   gfc_conv_loop_setup (&loop, & expr->where);
    8464              : 
    8465        76253 :   if (need_tmp)
    8466              :     {
    8467              :       /* Copy into a temporary and pass that.  We don't need to copy the data
    8468              :          back because expressions and vector subscripts must be INTENT_IN.  */
    8469              :       /* TODO: Optimize passing function return values.  */
    8470         7667 :       gfc_se lse;
    8471         7667 :       gfc_se rse;
    8472         7667 :       bool deep_copy;
    8473              : 
    8474              :       /* Start the copying loops.  */
    8475         7667 :       gfc_mark_ss_chain_used (loop.temp_ss, 1);
    8476         7667 :       gfc_mark_ss_chain_used (ss, 1);
    8477         7667 :       gfc_start_scalarized_body (&loop, &block);
    8478              : 
    8479              :       /* Copy each data element.  */
    8480         7667 :       gfc_init_se (&lse, NULL);
    8481         7667 :       gfc_copy_loopinfo_to_se (&lse, &loop);
    8482         7667 :       gfc_init_se (&rse, NULL);
    8483         7667 :       gfc_copy_loopinfo_to_se (&rse, &loop);
    8484              : 
    8485         7667 :       lse.ss = loop.temp_ss;
    8486         7667 :       rse.ss = ss;
    8487              : 
    8488         7667 :       gfc_conv_tmp_array_ref (&lse);
    8489         7667 :       if (expr->ts.type == BT_CHARACTER)
    8490              :         {
    8491         1487 :           gfc_conv_expr (&rse, expr);
    8492         1487 :           if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
    8493         1057 :             rse.expr = build_fold_indirect_ref_loc (input_location,
    8494              :                                                 rse.expr);
    8495              :         }
    8496              :       else
    8497         6180 :         gfc_conv_expr_val (&rse, expr);
    8498              : 
    8499         7667 :       gfc_add_block_to_block (&block, &rse.pre);
    8500         7667 :       gfc_add_block_to_block (&block, &lse.pre);
    8501              : 
    8502         7667 :       lse.string_length = rse.string_length;
    8503              : 
    8504              :       /* A parenthesized constructor is copied like the constructor.  */
    8505         7667 :       gfc_expr *src = expr;
    8506         7667 :       if (src->expr_type == EXPR_OP
    8507          728 :           && src->value.op.op == INTRINSIC_PARENTHESES
    8508          258 :           && src->value.op.op1->expr_type == EXPR_ARRAY)
    8509         7667 :         src = src->value.op.op1;
    8510        15334 :       deep_copy = !se->data_not_needed
    8511         7667 :                   && (src->expr_type == EXPR_VARIABLE
    8512         7111 :                       || src->expr_type == EXPR_ARRAY);
    8513         7667 :       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
    8514              :                                      deep_copy, false);
    8515         7667 :       gfc_add_expr_to_block (&block, tmp);
    8516              : 
    8517              :       /* Finish the copying loops.  */
    8518         7667 :       gfc_trans_scalarizing_loops (&loop, &block);
    8519              : 
    8520         7667 :       desc = loop.temp_ss->info->data.array.descriptor;
    8521              : 
    8522              :       /* The _copy of a CLASS element is deep, so finalize the temporary
    8523              :          through the vtable to free the components of the copies.  */
    8524           60 :       tree ctmp = expr->ts.type == BT_CLASS && expr->expr_type == EXPR_OP
    8525         7697 :                   ? gfc_get_class_from_expr (desc) : NULL_TREE;
    8526           30 :       if (ctmp)
    8527              :         {
    8528           30 :           tree final_fn, has_final, elsize;
    8529              : 
    8530           30 :           final_fn = gfc_evaluate_now (gfc_class_vtab_final_get (ctmp),
    8531              :                                        &se->post);
    8532           30 :           has_final = fold_build2_loc (input_location, NE_EXPR,
    8533              :                                        logical_type_node, final_fn,
    8534           30 :                                        fold_convert (TREE_TYPE (final_fn),
    8535              :                                                      null_pointer_node));
    8536           30 :           elsize = gfc_resize_class_size_with_len (&se->post, ctmp,
    8537              :                                                    gfc_class_vtab_size_get (ctmp));
    8538              :           /* The finalization wrapper takes the rank from the descriptor.  */
    8539           30 :           gfc_conv_descriptor_rank_set (&se->post, desc, loop.dimen);
    8540           30 :           tmp = build_call_expr_loc (input_location,
    8541              :                                      build_fold_indirect_ref_loc (input_location,
    8542              :                                                                   final_fn),
    8543              :                                      3, gfc_build_addr_expr (NULL_TREE, desc),
    8544              :                                      fold_convert (gfc_charlen_type_node,
    8545              :                                                    elsize),
    8546              :                                      boolean_false_node);
    8547           30 :           tmp = build3_v (COND_EXPR, has_final, tmp,
    8548              :                           build_empty_stmt (input_location));
    8549           30 :           gfc_add_expr_to_block (&se->post, tmp);
    8550              :         }
    8551              :     }
    8552        70270 :   else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
    8553              :     {
    8554         1671 :       desc = info->descriptor;
    8555         1671 :       se->string_length = ss_info->string_length;
    8556              :     }
    8557              :   else
    8558              :     {
    8559              :       /* We pass sections without copying to a temporary.  Make a new
    8560              :          descriptor and point it at the section we want.  The loop variable
    8561              :          limits will be the limits of the section.
    8562              :          A function may decide to repack the array to speed up access, but
    8563              :          we're not bothered about that here.  */
    8564        66915 :       int dim, ndim, codim;
    8565        66915 :       tree parm;
    8566        66915 :       tree parmtype;
    8567        66915 :       tree dtype;
    8568        66915 :       tree stride;
    8569        66915 :       tree from;
    8570        66915 :       tree to;
    8571        66915 :       tree base;
    8572        66915 :       tree offset;
    8573              : 
    8574        66915 :       ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
    8575              : 
    8576        66915 :       if (se->want_coarray)
    8577              :         {
    8578          751 :           gfc_array_ref *ar = &info->ref->u.ar;
    8579              : 
    8580          751 :           codim = expr->corank;
    8581         1579 :           for (n = 0; n < codim - 1; n++)
    8582              :             {
    8583              :               /* Make sure we are not lost somehow.  */
    8584          828 :               gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
    8585              : 
    8586              :               /* Make sure the call to gfc_conv_section_startstride won't
    8587              :                  generate unnecessary code to calculate stride.  */
    8588          828 :               gcc_assert (ar->stride[n + ndim] == NULL);
    8589              : 
    8590          828 :               gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
    8591          828 :               loop.from[n + loop.dimen] = info->start[n + ndim];
    8592          828 :               loop.to[n + loop.dimen]   = info->end[n + ndim];
    8593              :             }
    8594              : 
    8595          751 :           gcc_assert (n == codim - 1);
    8596          751 :           evaluate_bound (&loop.pre, info->start, ar->start,
    8597              :                           info->descriptor, n + ndim, true,
    8598          751 :                           ar->as->type == AS_DEFERRED, true);
    8599          751 :           loop.from[n + loop.dimen] = info->start[n + ndim];
    8600              :         }
    8601              :       else
    8602              :         codim = 0;
    8603              : 
    8604              :       /* Set the string_length for a character array.  */
    8605        66915 :       if (expr->ts.type == BT_CHARACTER)
    8606              :         {
    8607        11590 :           if (deferred_array_component && !substr)
    8608           37 :             se->string_length = ss_info->string_length;
    8609              :           else
    8610        11553 :             se->string_length =  gfc_get_expr_charlen (expr);
    8611              : 
    8612        11590 :           if (VAR_P (se->string_length)
    8613          990 :               && expr->ts.u.cl->backend_decl == se->string_length)
    8614          984 :             tmp = ss_info->string_length;
    8615              :           else
    8616              :             tmp = se->string_length;
    8617              : 
    8618        11590 :           if (expr->ts.deferred && expr->ts.u.cl->backend_decl
    8619          205 :               && VAR_P (expr->ts.u.cl->backend_decl))
    8620          150 :             gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
    8621              :           else
    8622        11440 :             expr->ts.u.cl->backend_decl = tmp;
    8623              :         }
    8624              : 
    8625              :       /* If we have an array section, are assigning  or passing an array
    8626              :          section argument make sure that the lower bound is 1.  References
    8627              :          to the full array should otherwise keep the original bounds.  */
    8628        66915 :       if (!info->ref || info->ref->u.ar.type != AR_FULL)
    8629        85274 :         for (dim = 0; dim < loop.dimen; dim++)
    8630        51725 :           if (!integer_onep (loop.from[dim]))
    8631              :             {
    8632        28063 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8633              :                                      gfc_array_index_type, gfc_index_one_node,
    8634              :                                      loop.from[dim]);
    8635        28063 :               loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
    8636              :                                               gfc_array_index_type,
    8637              :                                               loop.to[dim], tmp);
    8638        28063 :               loop.from[dim] = gfc_index_one_node;
    8639              :             }
    8640              : 
    8641        66915 :       desc = info->descriptor;
    8642        66915 :       if (se->direct_byref && !se->byref_noassign)
    8643              :         {
    8644              :           /* For pointer assignments we fill in the destination.  */
    8645         2839 :           parm = se->expr;
    8646         2839 :           parmtype = TREE_TYPE (parm);
    8647              :         }
    8648              :       else
    8649              :         {
    8650              :           /* Otherwise make a new one.  The element type is that of the
    8651              :              subobject for a subreference of the array.  */
    8652        64076 :           if (expr->ts.type == BT_CHARACTER
    8653        53180 :               || (subref_array_target && !se->direct_byref))
    8654              :             {
    8655        11112 :               gfc_typespec *ts = &expr->ts;
    8656        11112 :               if (ts->type == BT_CLASS
    8657           18 :                   && ts->u.derived->attr.is_class)
    8658           18 :                 ts = &ts->u.derived->components->ts;
    8659        11112 :               parmtype = gfc_typenode_for_spec (ts);
    8660        11112 :             }
    8661              :           else
    8662        52964 :             parmtype = gfc_get_element_type (TREE_TYPE (desc));
    8663              : 
    8664        64076 :           parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
    8665              :                                                 loop.from, loop.to, 0,
    8666              :                                                 GFC_ARRAY_UNKNOWN, false);
    8667        64076 :           parm = gfc_create_var (parmtype, "parm");
    8668              : 
    8669              :           /* When expression is a class object, then add the class' handle to
    8670              :              the parm_decl.  */
    8671        64076 :           if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
    8672              :             {
    8673         1300 :               gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
    8674         1300 :               gfc_se classse;
    8675              : 
    8676              :               /* class_expr can be NULL, when no _class ref is in expr.
    8677              :                  We must not fix this here with a gfc_fix_class_ref ().  */
    8678         1300 :               if (class_expr)
    8679              :                 {
    8680         1284 :                   gfc_init_se (&classse, NULL);
    8681         1284 :                   gfc_conv_expr (&classse, class_expr);
    8682         1284 :                   gfc_free_expr (class_expr);
    8683              : 
    8684         1284 :                   gcc_assert (classse.pre.head == NULL_TREE
    8685              :                               && classse.post.head == NULL_TREE);
    8686         1284 :                   gfc_allocate_lang_decl (parm);
    8687         1284 :                   GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
    8688              :                 }
    8689              :             }
    8690              :         }
    8691              : 
    8692        66915 :       if (expr->ts.type == BT_CHARACTER
    8693        66915 :           && VAR_P (TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)))))
    8694              :         {
    8695            0 :           tree elem_len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)));
    8696            0 :           gfc_add_modify (&loop.pre, elem_len,
    8697            0 :                           fold_convert (TREE_TYPE (elem_len),
    8698              :                           gfc_get_array_span (desc, expr)));
    8699              :         }
    8700              : 
    8701              :       /* Set the span field.  */
    8702        66915 :       tmp = NULL_TREE;
    8703        66915 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    8704         7822 :         tmp = gfc_conv_descriptor_span_get (desc);
    8705              :       else
    8706        59093 :         tmp = gfc_get_array_span (desc, expr);
    8707        66915 :       if (tmp)
    8708        66847 :         gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
    8709              : 
    8710              :       /* The following can be somewhat confusing.  We have two
    8711              :          descriptors, a new one and the original array.
    8712              :          {parm, parmtype, dim} refer to the new one.
    8713              :          {desc, type, n, loop} refer to the original, which maybe
    8714              :          a descriptorless array.
    8715              :          The bounds of the scalarization are the bounds of the section.
    8716              :          We don't have to worry about numeric overflows when calculating
    8717              :          the offsets because all elements are within the array data.  */
    8718              : 
    8719              :       /* Set the dtype.  */
    8720        66915 :       if (se->unlimited_polymorphic)
    8721          703 :         dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
    8722        66212 :       else if (expr->ts.type == BT_ASSUMED)
    8723              :         {
    8724          127 :           tree tmp2 = desc;
    8725          127 :           if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
    8726          127 :             tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
    8727          127 :           if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
    8728          127 :             tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
    8729          127 :           dtype = gfc_conv_descriptor_dtype_get (tmp2);
    8730              :         }
    8731              :       else
    8732        66085 :         dtype = gfc_get_dtype (parmtype);
    8733        66915 :       gfc_conv_descriptor_dtype_set (&loop.pre, parm, dtype);
    8734              : 
    8735              :       /* The 1st element in the section.  */
    8736        66915 :       base = gfc_index_zero_node;
    8737        66915 :       if (expr->ts.type == BT_CHARACTER && expr->rank == 0 && codim)
    8738            6 :         base = gfc_index_one_node;
    8739              : 
    8740              :       /* The offset from the 1st element in the section.  */
    8741        66915 :       offset = gfc_index_zero_node;
    8742              : 
    8743       171210 :       for (n = 0; n < ndim; n++)
    8744              :         {
    8745       104295 :           stride = gfc_conv_array_stride (desc, n);
    8746              : 
    8747              :           /* Work out the 1st element in the section.  */
    8748       104295 :           if (info->ref
    8749        96331 :               && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
    8750              :             {
    8751         1329 :               gcc_assert (info->subscript[n]
    8752              :                           && info->subscript[n]->info->type == GFC_SS_SCALAR);
    8753         1329 :               start = info->subscript[n]->info->data.scalar.value;
    8754              :             }
    8755              :           else
    8756              :             {
    8757              :               /* Evaluate and remember the start of the section.  */
    8758       102966 :               start = info->start[n];
    8759       102966 :               stride = gfc_evaluate_now (stride, &loop.pre);
    8760              :             }
    8761              : 
    8762       104295 :           tmp = gfc_conv_array_lbound (desc, n);
    8763       104295 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
    8764              :                                  start, tmp);
    8765       104295 :           tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    8766              :                                  tmp, stride);
    8767       104295 :           base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    8768              :                                     base, tmp);
    8769              : 
    8770       104295 :           if (info->ref
    8771        96331 :               && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
    8772              :             {
    8773              :               /* For elemental dimensions, we only need the 1st
    8774              :                  element in the section.  */
    8775         1329 :               continue;
    8776              :             }
    8777              : 
    8778              :           /* Vector subscripts need copying and are handled elsewhere.  */
    8779       102966 :           if (info->ref)
    8780        95002 :             gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
    8781              : 
    8782              :           /* look for the corresponding scalarizer dimension: dim.  */
    8783       153974 :           for (dim = 0; dim < ndim; dim++)
    8784       153974 :             if (ss->dim[dim] == n)
    8785              :               break;
    8786              : 
    8787              :           /* loop exited early: the DIM being looked for has been found.  */
    8788       102966 :           gcc_assert (dim < ndim);
    8789              : 
    8790              :           /* Set the new lower bound.  */
    8791       102966 :           from = loop.from[dim];
    8792       102966 :           to = loop.to[dim];
    8793              : 
    8794       102966 :           gfc_conv_descriptor_lbound_set (&loop.pre, parm,
    8795              :                                           gfc_rank_cst[dim], from);
    8796              : 
    8797              :           /* Set the new upper bound.  */
    8798       102966 :           gfc_conv_descriptor_ubound_set (&loop.pre, parm,
    8799              :                                           gfc_rank_cst[dim], to);
    8800              : 
    8801              :           /* Multiply the stride by the section stride to get the
    8802              :              total stride.  */
    8803       102966 :           stride = fold_build2_loc (input_location, MULT_EXPR,
    8804              :                                     gfc_array_index_type,
    8805              :                                     stride, info->stride[n]);
    8806              : 
    8807       102966 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8808       102966 :                                  TREE_TYPE (offset), stride, from);
    8809       102966 :           offset = fold_build2_loc (input_location, MINUS_EXPR,
    8810       102966 :                                    TREE_TYPE (offset), offset, tmp);
    8811              : 
    8812              :           /* Store the new stride.  */
    8813       102966 :           gfc_conv_descriptor_stride_set (&loop.pre, parm,
    8814              :                                           gfc_rank_cst[dim], stride);
    8815              :         }
    8816              : 
    8817              :       /* For deferred-length character we need to take the dynamic length
    8818              :          into account for the dataptr offset.  */
    8819        66915 :       if (expr->ts.type == BT_CHARACTER
    8820        11590 :           && expr->ts.deferred
    8821          211 :           && expr->ts.u.cl->backend_decl
    8822          211 :           && VAR_P (expr->ts.u.cl->backend_decl))
    8823              :         {
    8824          150 :           tree base_type = TREE_TYPE (base);
    8825          150 :           base = fold_build2_loc (input_location, MULT_EXPR, base_type, base,
    8826              :                                   fold_convert (base_type,
    8827              :                                                 expr->ts.u.cl->backend_decl));
    8828              :         }
    8829              : 
    8830        68494 :       for (n = loop.dimen; n < loop.dimen + codim; n++)
    8831              :         {
    8832         1579 :           from = loop.from[n];
    8833         1579 :           to = loop.to[n];
    8834         1579 :           gfc_conv_descriptor_lbound_set (&loop.pre, parm,
    8835              :                                           gfc_rank_cst[n], from);
    8836         1579 :           if (n < loop.dimen + codim - 1)
    8837          828 :             gfc_conv_descriptor_ubound_set (&loop.pre, parm,
    8838              :                                             gfc_rank_cst[n], to);
    8839              :         }
    8840              : 
    8841        66915 :       if (se->data_not_needed)
    8842         6323 :         gfc_conv_descriptor_data_set (&loop.pre, parm,
    8843              :                                       gfc_index_zero_node);
    8844              :       else
    8845              :         /* Point the data pointer at the 1st element in the section.  */
    8846        60592 :         gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
    8847              :                                 subref_array_target, expr);
    8848              : 
    8849        66915 :       gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
    8850              : 
    8851        66915 :       if (flag_coarray == GFC_FCOARRAY_LIB && expr->corank)
    8852              :         {
    8853          448 :           tmp = INDIRECT_REF_P (desc) ? TREE_OPERAND (desc, 0) : desc;
    8854          448 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8855              :             {
    8856           24 :               tmp = gfc_conv_descriptor_token (tmp);
    8857              :             }
    8858          424 :           else if (DECL_P (tmp) && DECL_LANG_SPECIFIC (tmp)
    8859          504 :                    && GFC_DECL_TOKEN (tmp) != NULL_TREE)
    8860           64 :             tmp = GFC_DECL_TOKEN (tmp);
    8861              :           else
    8862              :             {
    8863          360 :               tmp = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (tmp));
    8864              :             }
    8865              : 
    8866          448 :           gfc_conv_descriptor_token_set (&loop.pre, parm, tmp);
    8867              :         }
    8868              :       desc = parm;
    8869              :     }
    8870              : 
    8871              :   /* For class arrays add the class tree into the saved descriptor to
    8872              :      enable getting of _vptr and the like.  */
    8873        76253 :   if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
    8874        58644 :       && IS_CLASS_ARRAY (expr->symtree->n.sym))
    8875              :     {
    8876         1230 :       gfc_allocate_lang_decl (desc);
    8877         1230 :       GFC_DECL_SAVED_DESCRIPTOR (desc) =
    8878         1230 :           DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
    8879         1132 :             GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
    8880              :           : expr->symtree->n.sym->backend_decl;
    8881              :     }
    8882        75023 :   else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
    8883        10817 :            && IS_CLASS_ARRAY (expr))
    8884              :     {
    8885           12 :       tree vtype;
    8886           12 :       gfc_allocate_lang_decl (desc);
    8887           12 :       tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
    8888           12 :       GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
    8889           12 :       vtype = gfc_class_vptr_get (tmp);
    8890           12 :       gfc_add_modify (&se->pre, vtype,
    8891           12 :                       gfc_build_addr_expr (TREE_TYPE (vtype),
    8892           12 :                                       gfc_find_vtab (&expr->ts)->backend_decl));
    8893              :     }
    8894        76253 :   if (!se->direct_byref || se->byref_noassign)
    8895              :     {
    8896              :       /* Get a pointer to the new descriptor.  */
    8897        73414 :       if (se->want_pointer)
    8898        41102 :         se->expr = gfc_build_addr_expr (NULL_TREE, desc);
    8899              :       else
    8900        32312 :         se->expr = desc;
    8901              :     }
    8902              : 
    8903        76253 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    8904        76253 :   gfc_add_block_to_block (&se->post, &loop.post);
    8905              : 
    8906              :   /* Cleanup the scalarizer.  */
    8907        76253 :   gfc_cleanup_loop (&loop);
    8908              : }
    8909              : 
    8910              : 
    8911              : /* Calculate the array size (number of elements); if dim != NULL_TREE,
    8912              :    return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P).
    8913              :    If !expr && descriptor array, the rank is taken from the descriptor.  */
    8914              : tree
    8915        16152 : gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
    8916              : {
    8917        16152 :   if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
    8918              :     {
    8919           40 :       gcc_assert (dim == NULL_TREE);
    8920           40 :       return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
    8921              :     }
    8922        16112 :   tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
    8923        16112 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
    8924        16112 :   enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc));
    8925        16112 :   if (expr == NULL || expr->rank < 0)
    8926         3772 :     rank = gfc_conv_descriptor_rank_get (desc);
    8927              :   else
    8928        12340 :     rank = gfc_rank_cst[expr->rank];
    8929              : 
    8930        16112 :   if (dim || (expr && expr->rank == 1))
    8931              :     {
    8932         4880 :       if (dim)
    8933         9564 :         dim = fold_convert_loc (input_location, gfc_array_dim_rank_type, dim);
    8934              :       else
    8935         4880 :         dim = gfc_rank_cst[0];
    8936        14444 :       tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
    8937        14444 :       tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
    8938              : 
    8939        14444 :       size = fold_build2_loc (input_location, MINUS_EXPR,
    8940              :                               gfc_array_index_type, ubound, lbound);
    8941        14444 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    8942              :                               size, gfc_index_one_node);
    8943              :       /* if (!allocatable && !pointer && assumed rank)
    8944              :            size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
    8945              :          else
    8946              :            size = max (0, size);  */
    8947        14444 :       size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    8948              :                               size, gfc_index_zero_node);
    8949        14444 :       if (akind == GFC_ARRAY_ASSUMED_RANK_CONT
    8950        14444 :           || akind == GFC_ARRAY_ASSUMED_RANK)
    8951              :         {
    8952         3073 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8953              :                                  gfc_array_dim_rank_type, rank,
    8954              :                                  gfc_rank_cst[1]);
    8955         3073 :           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8956              :                                   dim, tmp);
    8957         3073 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8958              :                                  gfc_conv_descriptor_ubound_get (desc, dim),
    8959              :                                  build_int_cst (gfc_array_index_type, -1));
    8960         3073 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
    8961              :                                   cond, tmp);
    8962         3073 :           tmp = build_int_cst (gfc_array_index_type, -1);
    8963         3073 :           size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    8964              :                              cond, tmp, size);
    8965              :         }
    8966              :       return size;
    8967              :     }
    8968              : 
    8969              :   /* size = 1. */
    8970         1668 :   size = gfc_create_var (gfc_array_index_type, "size");
    8971         1668 :   gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
    8972         1668 :   tree extent = gfc_create_var (gfc_array_index_type, "extent");
    8973              : 
    8974         1668 :   stmtblock_t cond_block, loop_body;
    8975         1668 :   gfc_init_block (&cond_block);
    8976         1668 :   gfc_init_block (&loop_body);
    8977              : 
    8978              :   /* Loop: for (i = 0; i < rank; ++i).  */
    8979         1668 :   tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    8980              :   /* Loop body.  */
    8981              :   /* #if (assumed-rank + !allocatable && !pointer)
    8982              :        if (idx + 1 == rank && dim[idx].ubound == -1)
    8983              :          extent = -1;
    8984              :        else
    8985              :      #endif
    8986              :          extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
    8987              :          if (extent < 0)
    8988              :            extent = 0
    8989              :       size *= extent.  */
    8990         1668 :   cond = NULL_TREE;
    8991         1668 :   if (akind == GFC_ARRAY_ASSUMED_RANK_CONT || akind == GFC_ARRAY_ASSUMED_RANK)
    8992              :     {
    8993          471 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8994              :                              gfc_array_dim_rank_type, idx, gfc_rank_cst[1]);
    8995          471 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8996              :                               tmp, rank);
    8997          471 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8998              :                              gfc_conv_descriptor_ubound_get (desc, idx),
    8999              :                              build_int_cst (gfc_array_index_type, -1));
    9000          471 :       cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
    9001              :                               cond, tmp);
    9002              :     }
    9003         1668 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9004              :                          gfc_conv_descriptor_ubound_get (desc, idx),
    9005              :                          gfc_conv_descriptor_lbound_get (desc, idx));
    9006         1668 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9007              :                          tmp, gfc_index_one_node);
    9008         1668 :   gfc_add_modify (&cond_block, extent, tmp);
    9009         1668 :   tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
    9010              :                          extent, gfc_index_zero_node);
    9011         1668 :   tmp = build3_v (COND_EXPR, tmp,
    9012              :                   fold_build2_loc (input_location, MODIFY_EXPR,
    9013              :                                    gfc_array_index_type,
    9014              :                                    extent, gfc_index_zero_node),
    9015              :                   build_empty_stmt (input_location));
    9016         1668 :   gfc_add_expr_to_block (&cond_block, tmp);
    9017         1668 :   tmp = gfc_finish_block (&cond_block);
    9018         1668 :   if (cond)
    9019          471 :     tmp = build3_v (COND_EXPR, cond,
    9020              :                     fold_build2_loc (input_location, MODIFY_EXPR,
    9021              :                                      gfc_array_index_type, extent,
    9022              :                                      build_int_cst (gfc_array_index_type, -1)),
    9023              :                     tmp);
    9024         1668 :    gfc_add_expr_to_block (&loop_body, tmp);
    9025              :    /* size *= extent.  */
    9026         1668 :    gfc_add_modify (&loop_body, size,
    9027              :                    fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9028              :                                     size, extent));
    9029              :   /* Generate loop. */
    9030         3336 :   gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
    9031         1668 :                        build_int_cst (TREE_TYPE (idx), 1),
    9032              :                        gfc_finish_block (&loop_body));
    9033         1668 :   return size;
    9034              : }
    9035              : 
    9036              : /* Helper function for gfc_conv_array_parameter if array size needs to be
    9037              :    computed.  */
    9038              : 
    9039              : static void
    9040          152 : array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
    9041              : {
    9042          152 :   tree elem;
    9043          152 :   *size = gfc_tree_array_size (block, desc, expr, NULL);
    9044          152 :   elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
    9045          152 :   *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9046              :                            *size, fold_convert (gfc_array_index_type, elem));
    9047          152 : }
    9048              : 
    9049              : /* Helper function - return true if the argument is a pointer.  */
    9050              : 
    9051              : static bool
    9052          719 : is_pointer (gfc_expr *e)
    9053              : {
    9054          719 :   gfc_symbol *sym;
    9055              : 
    9056          719 :   if (e->expr_type != EXPR_VARIABLE ||  e->symtree == NULL)
    9057              :     return false;
    9058              : 
    9059          719 :   sym = e->symtree->n.sym;
    9060          719 :   if (sym == NULL)
    9061              :     return false;
    9062              : 
    9063          719 :   return sym->attr.pointer || sym->attr.proc_pointer;
    9064              : }
    9065              : 
    9066              : /* Assumed-rank actual argument: the caller only allocates storage for dtype
    9067              :    rank dimensions. Copying GFC_MAX_DIMENSIONS dim entries would read past the
    9068              :    physical end of the descriptor. Copy the header fields explicitly and use a
    9069              :    runtime-sized memcpy for the dim[] entries.  */
    9070              : void
    9071           78 : gfc_resize_assumed_rank_dim_field (gfc_se *se, stmtblock_t *block, tree desc)
    9072              : {
    9073           78 :   tree rank, dim_field, dim_size, copy_size, dst_ptr, src_ptr;
    9074              : 
    9075           78 :   gfc_conv_descriptor_data_set (block, desc,
    9076              :                                 gfc_conv_descriptor_data_get (se->expr));
    9077           78 :   gfc_conv_descriptor_offset_set (block, desc,
    9078              :                                   gfc_conv_descriptor_offset_get (se->expr));
    9079           78 :   gfc_conv_descriptor_dtype_set (block, desc,
    9080              :                                  gfc_conv_descriptor_dtype_get (se->expr));
    9081           78 :   rank = fold_convert (size_type_node, gfc_conv_descriptor_rank_get (se->expr));
    9082           78 :   dim_field = gfc_get_descriptor_dimension (se->expr);
    9083           78 :   dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dim_field)));
    9084           78 :   copy_size = fold_build2_loc (input_location, MULT_EXPR,
    9085              :                                size_type_node, rank, dim_size);
    9086           78 :   dst_ptr = gfc_build_addr_expr (pvoid_type_node,
    9087              :                                  gfc_get_descriptor_dimension (desc));
    9088           78 :   src_ptr = gfc_build_addr_expr (pvoid_type_node, dim_field);
    9089           78 :   gfc_add_expr_to_block (block, build_call_expr_loc (input_location,
    9090              :                          builtin_decl_explicit (BUILT_IN_MEMCPY),
    9091              :                          3, dst_ptr, src_ptr, copy_size));
    9092           78 : }
    9093              : 
    9094              : /* Convert an array for passing as an actual parameter.  */
    9095              : 
    9096              : void
    9097        67330 : gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
    9098              :                           const gfc_symbol *fsym, const char *proc_name,
    9099              :                           tree *size, tree *lbshift, tree *packed)
    9100              : {
    9101        67330 :   tree ptr;
    9102        67330 :   tree desc;
    9103        67330 :   tree tmp = NULL_TREE;
    9104        67330 :   tree stmt;
    9105        67330 :   tree parent = DECL_CONTEXT (current_function_decl);
    9106        67330 :   tree ctree;
    9107        67330 :   tree pack_attr = NULL_TREE; /* Set when packing class arrays.  */
    9108        67330 :   bool full_array_var;
    9109        67330 :   bool this_array_result;
    9110        67330 :   bool contiguous;
    9111        67330 :   bool no_pack;
    9112        67330 :   bool array_constructor;
    9113        67330 :   bool good_allocatable;
    9114        67330 :   bool ultimate_ptr_comp;
    9115        67330 :   bool ultimate_alloc_comp;
    9116        67330 :   bool readonly;
    9117        67330 :   gfc_symbol *sym;
    9118        67330 :   stmtblock_t block;
    9119        67330 :   gfc_ref *ref;
    9120              : 
    9121        67330 :   ultimate_ptr_comp = false;
    9122        67330 :   ultimate_alloc_comp = false;
    9123              : 
    9124        68305 :   for (ref = expr->ref; ref; ref = ref->next)
    9125              :     {
    9126        56596 :       if (ref->next == NULL)
    9127              :         break;
    9128              : 
    9129          975 :       if (ref->type == REF_COMPONENT)
    9130              :         {
    9131          745 :           ultimate_ptr_comp = ref->u.c.component->attr.pointer;
    9132          745 :           ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
    9133              :         }
    9134              :     }
    9135              : 
    9136        67330 :   full_array_var = false;
    9137        67330 :   contiguous = false;
    9138              : 
    9139        67330 :   if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
    9140        55486 :     full_array_var = gfc_full_array_ref_p (ref, &contiguous);
    9141              : 
    9142        55486 :   sym = full_array_var ? expr->symtree->n.sym : NULL;
    9143              : 
    9144              :   /* The symbol should have an array specification.  */
    9145        64138 :   gcc_assert (!sym || sym->as || ref->u.ar.as);
    9146              : 
    9147        67330 :   if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
    9148              :     {
    9149          708 :       if (expr->ts.u.cl->length_from_typespec && expr->ts.u.cl->length)
    9150              :         {
    9151              :           /* The constructor has an explicit character type-spec length
    9152              :              so convert it directly.  */
    9153          126 :           gfc_se cse;
    9154          126 :           gfc_init_se (&cse, NULL);
    9155          126 :           gfc_conv_expr_type (&cse, expr->ts.u.cl->length,
    9156              :                               gfc_charlen_type_node);
    9157          126 :           gfc_add_block_to_block (&se->pre, &cse.pre);
    9158          126 :           tmp = cse.expr;
    9159          126 :         }
    9160              :       else
    9161          582 :         get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
    9162              : 
    9163          708 :       expr->ts.u.cl->backend_decl = tmp;
    9164          708 :       se->string_length = tmp;
    9165              :     }
    9166              : 
    9167              :   /* Is this the result of the enclosing procedure?  */
    9168        67330 :   this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
    9169           58 :   if (this_array_result
    9170           58 :         && (sym->backend_decl != current_function_decl)
    9171            0 :         && (sym->backend_decl != parent))
    9172        67330 :     this_array_result = false;
    9173              : 
    9174              :   /* Passing an optional dummy argument as actual to an optional dummy?  */
    9175        67330 :   bool pass_optional;
    9176        67330 :   pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
    9177              : 
    9178              :   /* Passing address of the array if it is not pointer or assumed-shape.  */
    9179        67330 :   if (full_array_var && g77 && !this_array_result
    9180        16395 :       && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    9181              :     {
    9182        12613 :       tmp = gfc_get_symbol_decl (sym);
    9183              : 
    9184        12613 :       if (sym->ts.type == BT_CHARACTER)
    9185         2821 :         se->string_length = sym->ts.u.cl->backend_decl;
    9186              : 
    9187        12613 :       if (!sym->attr.pointer
    9188        12122 :           && sym->as
    9189        12122 :           && sym->as->type != AS_ASSUMED_SHAPE
    9190        11871 :           && sym->as->type != AS_DEFERRED
    9191        10375 :           && sym->as->type != AS_ASSUMED_RANK
    9192        10299 :           && !sym->attr.allocatable)
    9193              :         {
    9194              :           /* Some variables are declared directly, others are declared as
    9195              :              pointers and allocated on the heap.  */
    9196         9793 :           if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
    9197         2518 :             se->expr = tmp;
    9198              :           else
    9199         7275 :             se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    9200         9793 :           if (size)
    9201           40 :             array_parameter_size (&se->pre, tmp, expr, size);
    9202        17299 :           return;
    9203              :         }
    9204              : 
    9205         2820 :       if (sym->attr.allocatable)
    9206              :         {
    9207         1882 :           if (sym->attr.dummy || sym->attr.result)
    9208              :             {
    9209         1176 :               gfc_conv_expr_descriptor (se, expr);
    9210         1176 :               tmp = se->expr;
    9211              :             }
    9212         1882 :           if (size)
    9213           14 :             array_parameter_size (&se->pre, tmp, expr, size);
    9214         1882 :           se->expr = gfc_conv_array_data (tmp);
    9215         1882 :           if (pass_optional)
    9216              :             {
    9217           18 :               tree cond = gfc_conv_expr_present (sym);
    9218           36 :               se->expr = build3_loc (input_location, COND_EXPR,
    9219           18 :                                      TREE_TYPE (se->expr), cond, se->expr,
    9220           18 :                                      fold_convert (TREE_TYPE (se->expr),
    9221              :                                                    null_pointer_node));
    9222              :             }
    9223              :           return;
    9224              :         }
    9225              :     }
    9226              : 
    9227              :   /* A convenient reduction in scope.  */
    9228        55655 :   contiguous = g77 && !this_array_result && contiguous;
    9229              : 
    9230              :   /* There is no need to pack and unpack the array, if it is contiguous
    9231              :      and not a deferred- or assumed-shape array, or if it is simply
    9232              :      contiguous.  */
    9233        55655 :   no_pack = false;
    9234              :   // clang-format off
    9235        55655 :   if (sym)
    9236              :     {
    9237        40752 :       symbol_attribute *attr = &(IS_CLASS_ARRAY (sym)
    9238              :                                  ? CLASS_DATA (sym)->attr : sym->attr);
    9239        40752 :       gfc_array_spec *as = IS_CLASS_ARRAY (sym)
    9240        40752 :                            ? CLASS_DATA (sym)->as : sym->as;
    9241        40752 :       no_pack = (as
    9242        40485 :                  && !attr->pointer
    9243        37201 :                  && as->type != AS_DEFERRED
    9244        27464 :                  && as->type != AS_ASSUMED_RANK
    9245        64846 :                  && as->type != AS_ASSUMED_SHAPE);
    9246              :     }
    9247        55655 :   if (ref && ref->u.ar.as)
    9248        43800 :     no_pack = no_pack
    9249        43800 :               || (ref->u.ar.as->type != AS_DEFERRED
    9250              :                   && ref->u.ar.as->type != AS_ASSUMED_RANK
    9251              :                   && ref->u.ar.as->type != AS_ASSUMED_SHAPE);
    9252       111310 :   no_pack = contiguous
    9253        55655 :             && (no_pack || gfc_is_simply_contiguous (expr, false, true));
    9254              :   // clang-format on
    9255              : 
    9256              :   /* If we have an EXPR_OP or a function returning an explicit-shaped
    9257              :      or allocatable array, an array temporary will be generated which
    9258              :      does not need to be packed / unpacked if passed to an
    9259              :      explicit-shape dummy array.  */
    9260              : 
    9261        55655 :   if (g77)
    9262              :     {
    9263         6667 :       if (expr->expr_type == EXPR_OP)
    9264              :         no_pack = 1;
    9265         6590 :       else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
    9266              :         {
    9267           34 :           gfc_symbol *result = expr->value.function.esym->result;
    9268           34 :           if (result->attr.dimension
    9269           34 :               && (result->as->type == AS_EXPLICIT
    9270            7 :                   || result->attr.allocatable
    9271            0 :                   || result->attr.contiguous))
    9272        55655 :             no_pack = 1;
    9273              :         }
    9274              :     }
    9275              : 
    9276              :   /* Array constructors are always contiguous and do not need packing.  */
    9277        55655 :   array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
    9278              : 
    9279              :   /* Same is true of contiguous sections from allocatable variables.  */
    9280       111310 :   good_allocatable = contiguous
    9281         4857 :                        && expr->symtree
    9282        60512 :                        && expr->symtree->n.sym->attr.allocatable;
    9283              : 
    9284              :   /* Or ultimate allocatable components.  */
    9285        55655 :   ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
    9286              : 
    9287        55655 :   if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
    9288              :     {
    9289         5237 :       gfc_conv_expr_descriptor (se, expr);
    9290              :       /* Deallocate the allocatable components of structures that are
    9291              :          not variable.  */
    9292         5237 :       if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    9293         3685 :            && expr->ts.u.derived->attr.alloc_comp
    9294         2250 :            && expr->expr_type != EXPR_VARIABLE)
    9295              :         {
    9296            2 :           tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
    9297              : 
    9298              :           /* The components shall be deallocated before their containing entity.  */
    9299            2 :           gfc_prepend_expr_to_block (&se->post, tmp);
    9300              :         }
    9301         5237 :       if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
    9302          309 :         se->string_length = expr->ts.u.cl->backend_decl;
    9303         5237 :       if (size)
    9304           68 :         array_parameter_size (&se->pre, se->expr, expr, size);
    9305         5237 :       se->expr = gfc_conv_array_data (se->expr);
    9306         5237 :       return;
    9307              :     }
    9308              : 
    9309        50418 :   if (fsym && fsym->ts.type == BT_CLASS)
    9310              :     {
    9311         1291 :       gcc_assert (se->expr);
    9312              :       ctree = se->expr;
    9313              :     }
    9314              :   else
    9315              :     ctree = NULL_TREE;
    9316              : 
    9317        50418 :   if (this_array_result)
    9318              :     {
    9319              :       /* Result of the enclosing function.  */
    9320           58 :       gfc_conv_expr_descriptor (se, expr);
    9321           58 :       if (size)
    9322            0 :         array_parameter_size (&se->pre, se->expr, expr, size);
    9323           58 :       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    9324              : 
    9325           18 :       if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
    9326           76 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
    9327           18 :         se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
    9328              :                                                                  se->expr));
    9329              : 
    9330              :       return;
    9331              :     }
    9332              :   else
    9333              :     {
    9334              :       /* Every other type of array.  */
    9335        50360 :       se->want_pointer = (ctree) ? 0 : 1;
    9336        50360 :       se->want_coarray = expr->corank;
    9337        50360 :       gfc_conv_expr_descriptor (se, expr);
    9338              : 
    9339        50360 :       if (size)
    9340           30 :         array_parameter_size (&se->pre,
    9341              :                               build_fold_indirect_ref_loc (input_location,
    9342              :                                                             se->expr),
    9343              :                               expr, size);
    9344        50360 :       if (ctree)
    9345              :         {
    9346         1291 :           stmtblock_t block;
    9347              : 
    9348         1291 :           gfc_init_block (&block);
    9349         1291 :           if (lbshift && *lbshift)
    9350              :             {
    9351              :               /* Apply a shift of the lbound when supplied.  */
    9352           98 :               for (int dim = 0; dim < expr->rank; ++dim)
    9353           49 :                 gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
    9354              :                                                   *lbshift);
    9355              :             }
    9356         1291 :           tmp = gfc_class_data_get (ctree);
    9357         1291 :           if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
    9358           84 :               && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
    9359              :             {
    9360           36 :               tree arr = gfc_create_var (TREE_TYPE (tmp), "parm");
    9361           36 :               gfc_conv_descriptor_data_set (&block, arr,
    9362              :                                             gfc_conv_descriptor_data_get (
    9363              :                                               se->expr));
    9364           36 :               gfc_conv_descriptor_lbound_set (&block, arr, gfc_index_zero_node,
    9365              :                                               gfc_index_zero_node);
    9366           36 :               gfc_conv_descriptor_ubound_set (
    9367              :                 &block, arr, gfc_index_zero_node,
    9368              :                 gfc_conv_descriptor_size (se->expr, expr->rank));
    9369           36 :               gfc_conv_descriptor_stride_set (
    9370              :                 &block, arr, gfc_index_zero_node,
    9371              :                 gfc_conv_descriptor_stride_get (se->expr, gfc_index_zero_node));
    9372           36 :               tree dtype_val = gfc_conv_descriptor_dtype_get (se->expr);
    9373           36 :               gfc_conv_descriptor_dtype_set (&block, arr, dtype_val);
    9374           36 :               gfc_conv_descriptor_rank_set (&block, arr, 1);
    9375           36 :               gfc_conv_descriptor_span_set (&block, arr,
    9376              :                                             gfc_conv_descriptor_span_get (arr));
    9377           36 :               gfc_conv_descriptor_offset_set (&block, arr, gfc_index_zero_node);
    9378           36 :               se->expr = arr;
    9379              :             }
    9380         1291 :           if (expr->rank == -1)
    9381           78 :             gfc_resize_assumed_rank_dim_field (se, &block, tmp);
    9382         1213 :           else if (CLASS_DATA (fsym)->as->rank == -1)
    9383          397 :             gfc_class_array_data_assign (&block, tmp, se->expr, false);
    9384              :           else
    9385          816 :             gfc_class_array_data_assign (&block, tmp, se->expr, true);
    9386              : 
    9387              :           /* Handle optional.  */
    9388         1291 :           if (fsym && fsym->attr.optional && sym && sym->attr.optional)
    9389          348 :             tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
    9390              :                             gfc_finish_block (&block),
    9391              :                             build_empty_stmt (input_location));
    9392              :           else
    9393          943 :             tmp = gfc_finish_block (&block);
    9394              : 
    9395         1291 :           gfc_add_expr_to_block (&se->pre, tmp);
    9396              :         }
    9397        49069 :       else if (pass_optional && full_array_var && sym->as && sym->as->rank != 0)
    9398              :         {
    9399              :           /* Perform calculation of bounds and strides of optional array dummy
    9400              :              only if the argument is present.  */
    9401          219 :           tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
    9402              :                           gfc_finish_block (&se->pre),
    9403              :                           build_empty_stmt (input_location));
    9404          219 :           gfc_add_expr_to_block (&se->pre, tmp);
    9405              :         }
    9406              :     }
    9407              : 
    9408              :   /* Deallocate the allocatable components of structures that are
    9409              :      not variable, for descriptorless arguments.
    9410              :      Arguments with a descriptor are handled in gfc_conv_procedure_call.  */
    9411        50360 :   if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    9412           76 :           && expr->ts.u.derived->attr.alloc_comp
    9413           18 :           && expr->expr_type != EXPR_VARIABLE)
    9414              :     {
    9415            0 :       tmp = build_fold_indirect_ref_loc (input_location, se->expr);
    9416            0 :       tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
    9417              : 
    9418              :       /* The components shall be deallocated before their containing entity.  */
    9419            0 :       gfc_prepend_expr_to_block (&se->post, tmp);
    9420              :     }
    9421              : 
    9422        48948 :   if (g77 || (fsym && fsym->attr.contiguous
    9423         1581 :               && !gfc_is_simply_contiguous (expr, false, true)))
    9424              :     {
    9425         1592 :       tree origptr = NULL_TREE, packedptr = NULL_TREE;
    9426              : 
    9427         1592 :       desc = se->expr;
    9428              : 
    9429              :       /* For contiguous arrays, save the original value of the descriptor.  */
    9430         1592 :       if (!g77 && !ctree)
    9431              :         {
    9432           84 :           origptr = gfc_create_var (pvoid_type_node, "origptr");
    9433           84 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9434           84 :           tmp = gfc_conv_array_data (tmp);
    9435          168 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    9436           84 :                                  TREE_TYPE (origptr), origptr,
    9437           84 :                                  fold_convert (TREE_TYPE (origptr), tmp));
    9438           84 :           gfc_add_expr_to_block (&se->pre, tmp);
    9439              :         }
    9440              : 
    9441              :       /* Repack the array.  */
    9442         1592 :       if (warn_array_temporaries)
    9443              :         {
    9444           28 :           if (fsym)
    9445           18 :             gfc_warning (OPT_Warray_temporaries,
    9446              :                          "Creating array temporary at %L for argument %qs",
    9447           18 :                          &expr->where, fsym->name);
    9448              :           else
    9449           10 :             gfc_warning (OPT_Warray_temporaries,
    9450              :                          "Creating array temporary at %L", &expr->where);
    9451              :         }
    9452              : 
    9453              :       /* When optimizing, we can use gfc_conv_subref_array_arg for
    9454              :          making the packing and unpacking operation visible to the
    9455              :          optimizers.  */
    9456              : 
    9457         1412 :       if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
    9458          719 :           && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
    9459          349 :           && !(expr->symtree->n.sym->as
    9460          332 :                && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
    9461         1941 :           && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
    9462              :         {
    9463          329 :           gfc_conv_subref_array_arg (se, expr, g77,
    9464          153 :                                      fsym ? fsym->attr.intent : INTENT_INOUT,
    9465              :                                      false, fsym, proc_name, sym, true);
    9466          329 :           return;
    9467              :         }
    9468              : 
    9469         1263 :       if (ctree)
    9470              :         {
    9471           96 :           packedptr
    9472           96 :             = gfc_build_addr_expr (NULL_TREE, gfc_create_var (TREE_TYPE (ctree),
    9473              :                                                               "packed"));
    9474           96 :           if (fsym)
    9475              :             {
    9476           96 :               int pack_mask = 0;
    9477              : 
    9478              :               /* Set bit 0 to the mask, when this is an unlimited_poly
    9479              :                  class.  */
    9480           96 :               if (CLASS_DATA (fsym)->ts.u.derived->attr.unlimited_polymorphic)
    9481           36 :                 pack_mask = 1 << 0;
    9482           96 :               pack_attr = build_int_cst (integer_type_node, pack_mask);
    9483              :             }
    9484              :           else
    9485            0 :             pack_attr = integer_zero_node;
    9486              : 
    9487           96 :           gfc_add_expr_to_block (
    9488              :             &se->pre,
    9489              :             build_call_expr_loc (input_location, gfor_fndecl_in_pack_class, 4,
    9490              :                                  packedptr,
    9491              :                                  gfc_build_addr_expr (NULL_TREE, ctree),
    9492           96 :                                  size_in_bytes (TREE_TYPE (ctree)), pack_attr));
    9493           96 :           ptr = gfc_conv_array_data (gfc_class_data_get (packedptr));
    9494           96 :           se->expr = packedptr;
    9495           96 :           if (packed)
    9496           96 :             *packed = packedptr;
    9497              :         }
    9498              :       else
    9499              :         {
    9500         1167 :           ptr = build_call_expr_loc (input_location, gfor_fndecl_in_pack, 1,
    9501              :                                      desc);
    9502              : 
    9503         1167 :           if (fsym && fsym->attr.optional && sym && sym->attr.optional)
    9504              :             {
    9505           11 :               tmp = gfc_conv_expr_present (sym);
    9506           22 :               ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
    9507           11 :                                 tmp, fold_convert (TREE_TYPE (se->expr), ptr),
    9508           11 :                                 fold_convert (TREE_TYPE (se->expr),
    9509              :                                               null_pointer_node));
    9510              :             }
    9511              : 
    9512         1167 :           ptr = gfc_evaluate_now (ptr, &se->pre);
    9513              :         }
    9514              : 
    9515              :       /* Use the packed data for the actual argument, except for contiguous arrays,
    9516              :          where the descriptor's data component is set.  */
    9517         1263 :       if (g77)
    9518         1083 :         se->expr = ptr;
    9519              :       else
    9520              :         {
    9521          180 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9522              : 
    9523          180 :           if (!ctree)
    9524              :             {
    9525              :               /* The original descriptor may have transposed dims so we
    9526              :                  can't reuse it directly; we have to create a new one.  */
    9527           84 :               tree old_field;
    9528           84 :               tree old_desc = tmp;
    9529           84 :               tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
    9530              : 
    9531           84 :               old_field = gfc_conv_descriptor_dtype_get (old_desc);
    9532           84 :               gfc_conv_descriptor_dtype_set (&se->pre, new_desc, old_field);
    9533              : 
    9534           84 :               if (expr->rank == -1)
    9535              :                 {
    9536           12 :                   tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    9537           12 :                   tree stride = gfc_create_var (gfc_array_index_type, "stride");
    9538           12 :                   stmtblock_t loop_body;
    9539              : 
    9540           12 :                   gfc_conv_descriptor_offset_set (&se->pre, new_desc,
    9541              :                                                   gfc_index_zero_node);
    9542           12 :                   gfc_conv_descriptor_span_set (&se->pre, new_desc,
    9543              :                                                 gfc_conv_descriptor_span_get
    9544              :                                                 (old_desc));
    9545           12 :                   gfc_add_modify (&se->pre, stride, gfc_index_one_node);
    9546              : 
    9547           12 :                   gfc_init_block (&loop_body);
    9548              : 
    9549           12 :                   old_field = gfc_conv_descriptor_lbound_get (old_desc, idx);
    9550           12 :                   gfc_conv_descriptor_lbound_set (&loop_body, new_desc, idx,
    9551              :                                                   old_field);
    9552              : 
    9553           12 :                   old_field = gfc_conv_descriptor_ubound_get (old_desc, idx);
    9554           12 :                   gfc_conv_descriptor_ubound_set (&loop_body, new_desc, idx,
    9555              :                                                   old_field);
    9556              : 
    9557           12 :                   gfc_conv_descriptor_stride_set (&loop_body, new_desc, idx,
    9558              :                                                   stride);
    9559              : 
    9560           12 :                   tree offset = fold_build2_loc (input_location, MULT_EXPR,
    9561              :                                                  gfc_array_index_type, stride,
    9562              :                                                  gfc_conv_descriptor_lbound_get
    9563              :                                                  (new_desc, idx));
    9564           12 :                   offset = fold_build2_loc (input_location, MINUS_EXPR,
    9565              :                                             gfc_array_index_type,
    9566              :                                             gfc_conv_descriptor_offset_get
    9567              :                                             (new_desc), offset);
    9568           12 :                   gfc_conv_descriptor_offset_set (&loop_body, new_desc, offset);
    9569              : 
    9570           12 :                   tree extent = gfc_conv_array_extent_dim
    9571           12 :                                 (gfc_conv_descriptor_lbound_get (new_desc, idx),
    9572              :                                  gfc_conv_descriptor_ubound_get (new_desc, idx),
    9573              :                                  NULL);
    9574           12 :                   extent = fold_build2_loc (input_location, MULT_EXPR,
    9575              :                                             gfc_array_index_type, stride,
    9576              :                                             extent);
    9577           12 :                   gfc_add_modify (&loop_body, stride, extent);
    9578              : 
    9579           36 :                   gfc_simple_for_loop (&se->pre, idx,
    9580           12 :                                        build_int_cst (TREE_TYPE (idx), 0),
    9581              :                                        gfc_conv_descriptor_rank_get (old_desc),
    9582              :                                        LT_EXPR,
    9583           12 :                                        build_int_cst (TREE_TYPE (idx), 1),
    9584              :                                        gfc_finish_block (&loop_body));
    9585              :                 }
    9586              :               else
    9587              :                 {
    9588           72 :                   tree offset = gfc_index_zero_node;
    9589              : 
    9590           72 :                   tree stride = gfc_index_one_node;
    9591              : 
    9592          102 :                   for (int i = 0; i < expr->rank; i++)
    9593              :                     {
    9594          102 :                       tree dim = gfc_rank_cst[i];
    9595              : 
    9596          102 :                       tree lbound = gfc_conv_descriptor_lbound_get (old_desc,
    9597              :                                                                     dim);
    9598          102 :                       lbound = gfc_evaluate_now (lbound, &se->pre);
    9599          102 :                       gfc_conv_descriptor_lbound_set (&se->pre, new_desc, dim,
    9600              :                                                       lbound);
    9601              : 
    9602          102 :                       tree ubound = gfc_conv_descriptor_ubound_get (old_desc,
    9603              :                                                                     dim);
    9604          102 :                       ubound = gfc_evaluate_now (ubound, &se->pre);
    9605          102 :                       gfc_conv_descriptor_ubound_set (&se->pre, new_desc, dim,
    9606              :                                                       ubound);
    9607              : 
    9608          102 :                       gfc_conv_descriptor_stride_set (&se->pre, new_desc, dim,
    9609              :                                                       stride);
    9610              : 
    9611          102 :                       tree tmp = fold_build2_loc (input_location, MULT_EXPR,
    9612              :                                                   gfc_array_index_type,
    9613              :                                                   stride, lbound);
    9614          102 :                       offset = fold_build2_loc (input_location, MINUS_EXPR,
    9615              :                                                 gfc_array_index_type,
    9616              :                                                 offset, tmp);
    9617          102 :                       offset = gfc_evaluate_now (offset, &se->pre);
    9618              : 
    9619              :                       /* Now calculate the stride for next dimension, unless the
    9620              :                          current dimension is the last one.  */
    9621          102 :                       if (i == expr->rank - 1)
    9622              :                         break;
    9623              : 
    9624           30 :                       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9625              :                                              gfc_array_index_type,
    9626              :                                              lbound, gfc_index_one_node);
    9627           30 :                       tree extent = fold_build2_loc (input_location, MINUS_EXPR,
    9628              :                                                      gfc_array_index_type,
    9629              :                                                      ubound, tmp);
    9630           30 :                       stride = fold_build2_loc (input_location, MULT_EXPR,
    9631              :                                                 gfc_array_index_type,
    9632              :                                                 stride, extent);
    9633           30 :                       stride = gfc_evaluate_now (stride, &se->pre);
    9634              :                     }
    9635              : 
    9636           72 :                   gfc_conv_descriptor_offset_set (&se->pre, new_desc, offset);
    9637              :                 }
    9638              : 
    9639           84 :               if (flag_coarray == GFC_FCOARRAY_LIB
    9640            0 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
    9641           84 :                   && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
    9642              :                      == GFC_ARRAY_ALLOCATABLE)
    9643              :                 {
    9644            0 :                   old_field = gfc_conv_descriptor_token (old_desc);
    9645            0 :                   gfc_conv_descriptor_token_set (&se->pre, new_desc,
    9646              :                                                  old_field);
    9647              :                 }
    9648              : 
    9649           84 :               gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
    9650           84 :               se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
    9651              :             }
    9652              :         }
    9653              : 
    9654         1263 :       if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
    9655              :         {
    9656            8 :           char * msg;
    9657              : 
    9658            8 :           if (fsym && proc_name)
    9659            8 :             msg = xasprintf ("An array temporary was created for argument "
    9660            8 :                              "'%s' of procedure '%s'", fsym->name, proc_name);
    9661              :           else
    9662            0 :             msg = xasprintf ("An array temporary was created");
    9663              : 
    9664            8 :           tmp = build_fold_indirect_ref_loc (input_location,
    9665              :                                          desc);
    9666            8 :           tmp = gfc_conv_array_data (tmp);
    9667            8 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9668            8 :                                  fold_convert (TREE_TYPE (tmp), ptr), tmp);
    9669              : 
    9670            8 :           if (pass_optional)
    9671            6 :             tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9672              :                                    logical_type_node,
    9673              :                                    gfc_conv_expr_present (sym), tmp);
    9674              : 
    9675            8 :           gfc_trans_runtime_check (false, true, tmp, &se->pre,
    9676              :                                    &expr->where, msg);
    9677            8 :           free (msg);
    9678              :         }
    9679              : 
    9680         1263 :       gfc_start_block (&block);
    9681              : 
    9682              :       /* Copy the data back.  If input expr is read-only, e.g. a PARAMETER
    9683              :          array, copying back modified values is undefined behavior.  */
    9684         2526 :       readonly = (expr->expr_type == EXPR_VARIABLE
    9685          858 :                   && expr->symtree
    9686         2121 :                   && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
    9687              : 
    9688         1263 :       if ((fsym == NULL || fsym->attr.intent != INTENT_IN) && !readonly)
    9689              :         {
    9690         1106 :           if (ctree)
    9691              :             {
    9692           66 :               tmp = gfc_build_addr_expr (NULL_TREE, ctree);
    9693           66 :               tmp = build_call_expr_loc (input_location,
    9694              :                                          gfor_fndecl_in_unpack_class, 4, tmp,
    9695              :                                          packedptr,
    9696           66 :                                          size_in_bytes (TREE_TYPE (ctree)),
    9697              :                                          pack_attr);
    9698              :             }
    9699              :           else
    9700         1040 :             tmp = build_call_expr_loc (input_location, gfor_fndecl_in_unpack, 2,
    9701              :                                        desc, ptr);
    9702         1106 :           gfc_add_expr_to_block (&block, tmp);
    9703              :         }
    9704          157 :       else if (ctree && fsym->attr.intent == INTENT_IN)
    9705              :         {
    9706              :           /* Need to free the memory for class arrays, that got packed.  */
    9707           30 :           gfc_add_expr_to_block (&block, gfc_call_free (ptr));
    9708              :         }
    9709              : 
    9710              :       /* Free the temporary.  */
    9711         1136 :       if (!ctree)
    9712         1167 :         gfc_add_expr_to_block (&block, gfc_call_free (ptr));
    9713              : 
    9714         1263 :       stmt = gfc_finish_block (&block);
    9715              : 
    9716         1263 :       gfc_init_block (&block);
    9717              :       /* Only if it was repacked.  This code needs to be executed before the
    9718              :          loop cleanup code.  */
    9719         1263 :       tmp = (ctree) ? desc : build_fold_indirect_ref_loc (input_location, desc);
    9720         1263 :       tmp = gfc_conv_array_data (tmp);
    9721         1263 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9722         1263 :                              fold_convert (TREE_TYPE (tmp), ptr), tmp);
    9723              : 
    9724         1263 :       if (pass_optional)
    9725           11 :         tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9726              :                                logical_type_node,
    9727              :                                gfc_conv_expr_present (sym), tmp);
    9728              : 
    9729         1263 :       tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
    9730              : 
    9731         1263 :       gfc_add_expr_to_block (&block, tmp);
    9732         1263 :       gfc_add_block_to_block (&block, &se->post);
    9733              : 
    9734         1263 :       gfc_init_block (&se->post);
    9735              : 
    9736              :       /* Reset the descriptor pointer.  */
    9737         1263 :       if (!g77 && !ctree)
    9738              :         {
    9739           84 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9740           84 :           gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
    9741              :         }
    9742              : 
    9743         1263 :       gfc_add_block_to_block (&se->post, &block);
    9744              :     }
    9745              : }
    9746              : 
    9747              : 
    9748              : /* This helper function calculates the size in words of a full array.  */
    9749              : 
    9750              : tree
    9751        21966 : gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
    9752              : {
    9753        21966 :   tree idx;
    9754        21966 :   tree nelems;
    9755        21966 :   tree tmp;
    9756        21966 :   if (rank < 0)
    9757            0 :     idx = gfc_conv_descriptor_rank_get (decl);
    9758              :   else
    9759        21966 :     idx = gfc_rank_cst[rank - 1];
    9760        21966 :   nelems = gfc_conv_descriptor_ubound_get (decl, idx);
    9761        21966 :   tmp = gfc_conv_descriptor_lbound_get (decl, idx);
    9762        21966 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9763              :                          nelems, tmp);
    9764        21966 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9765              :                          tmp, gfc_index_one_node);
    9766        21966 :   tmp = gfc_evaluate_now (tmp, block);
    9767              : 
    9768        21966 :   nelems = gfc_conv_descriptor_stride_get (decl, idx);
    9769        21966 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9770              :                          nelems, tmp);
    9771        21966 :   return gfc_evaluate_now (tmp, block);
    9772              : }
    9773              : 
    9774              : 
    9775              : /* Allocate dest to the same size as src, and copy src -> dest.
    9776              :    If no_malloc is set, only the copy is done.  */
    9777              : 
    9778              : static tree
    9779        10875 : duplicate_allocatable (tree dest, tree src, tree type, int rank,
    9780              :                        bool no_malloc, bool no_memcpy, tree str_sz,
    9781              :                        tree add_when_allocated)
    9782              : {
    9783        10875 :   tree tmp;
    9784        10875 :   tree eltype;
    9785        10875 :   tree size;
    9786        10875 :   tree nelems;
    9787        10875 :   tree null_cond;
    9788        10875 :   tree null_data;
    9789        10875 :   stmtblock_t block;
    9790              : 
    9791              :   /* If the source is null, set the destination to null.  Then,
    9792              :      allocate memory to the destination.  */
    9793        10875 :   gfc_init_block (&block);
    9794              : 
    9795        10875 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
    9796              :     {
    9797         2604 :       gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
    9798         2604 :       null_data = gfc_finish_block (&block);
    9799              : 
    9800         2604 :       gfc_init_block (&block);
    9801         2604 :       eltype = TREE_TYPE (type);
    9802         2604 :       if (str_sz != NULL_TREE)
    9803              :         size = str_sz;
    9804              :       else
    9805         2230 :         size = TYPE_SIZE_UNIT (eltype);
    9806              : 
    9807         2604 :       if (!no_malloc)
    9808              :         {
    9809         2604 :           tmp = gfc_call_malloc (&block, type, size);
    9810         2604 :           gfc_add_modify (&block, dest, fold_convert (type, tmp));
    9811              :         }
    9812              : 
    9813         2604 :       if (!no_memcpy)
    9814              :         {
    9815         1809 :           tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9816         1809 :           tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
    9817              :                                      fold_convert (size_type_node, size));
    9818         1809 :           gfc_add_expr_to_block (&block, tmp);
    9819              :         }
    9820              :     }
    9821              :   else
    9822              :     {
    9823         8271 :       gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9824         8271 :       null_data = gfc_finish_block (&block);
    9825              : 
    9826         8271 :       gfc_init_block (&block);
    9827         8271 :       if (rank)
    9828         8256 :         nelems = gfc_full_array_size (&block, src, rank);
    9829              :       else
    9830           15 :         nelems = gfc_index_one_node;
    9831              : 
    9832              :       /* If type is not the array type, then it is the element type.  */
    9833         8271 :       if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
    9834         8241 :         eltype = gfc_get_element_type (type);
    9835              :       else
    9836              :         eltype = type;
    9837              : 
    9838         8271 :       if (str_sz != NULL_TREE)
    9839           43 :         tmp = fold_convert (gfc_array_index_type, str_sz);
    9840              :       else
    9841         8228 :         tmp = fold_convert (gfc_array_index_type,
    9842              :                             TYPE_SIZE_UNIT (eltype));
    9843              : 
    9844         8271 :       tmp = gfc_evaluate_now (tmp, &block);
    9845         8271 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9846              :                               nelems, tmp);
    9847         8271 :       if (!no_malloc)
    9848              :         {
    9849         8173 :           tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
    9850         8173 :           tmp = gfc_call_malloc (&block, tmp, size);
    9851         8173 :           gfc_conv_descriptor_data_set (&block, dest, tmp);
    9852              :         }
    9853              : 
    9854              :       /* We know the temporary and the value will be the same length,
    9855              :          so can use memcpy.  */
    9856         8271 :       if (!no_memcpy)
    9857              :         {
    9858         6910 :           tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9859         6910 :           tmp = build_call_expr_loc (input_location, tmp, 3,
    9860              :                                      gfc_conv_descriptor_data_get (dest),
    9861              :                                      gfc_conv_descriptor_data_get (src),
    9862              :                                      fold_convert (size_type_node, size));
    9863         6910 :           gfc_add_expr_to_block (&block, tmp);
    9864              :         }
    9865              :     }
    9866              : 
    9867        10875 :   gfc_add_expr_to_block (&block, add_when_allocated);
    9868        10875 :   tmp = gfc_finish_block (&block);
    9869              : 
    9870              :   /* Null the destination if the source is null; otherwise do
    9871              :      the allocate and copy.  */
    9872        10875 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
    9873              :     null_cond = src;
    9874              :   else
    9875         8271 :     null_cond = gfc_conv_descriptor_data_get (src);
    9876              : 
    9877        10875 :   null_cond = convert (pvoid_type_node, null_cond);
    9878        10875 :   null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9879              :                                null_cond, null_pointer_node);
    9880        10875 :   return build3_v (COND_EXPR, null_cond, tmp, null_data);
    9881              : }
    9882              : 
    9883              : 
    9884              : /* Allocate dest to the same size as src, and copy data src -> dest.  */
    9885              : 
    9886              : tree
    9887         7800 : gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
    9888              :                            tree add_when_allocated)
    9889              : {
    9890         7800 :   return duplicate_allocatable (dest, src, type, rank, false, false,
    9891         7800 :                                 NULL_TREE, add_when_allocated);
    9892              : }
    9893              : 
    9894              : 
    9895              : /* Copy data src -> dest.  */
    9896              : 
    9897              : tree
    9898           98 : gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
    9899              : {
    9900           98 :   return duplicate_allocatable (dest, src, type, rank, true, false,
    9901           98 :                                 NULL_TREE, NULL_TREE);
    9902              : }
    9903              : 
    9904              : /* Allocate dest to the same size as src, but don't copy anything.  */
    9905              : 
    9906              : tree
    9907         2156 : gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
    9908              : {
    9909         2156 :   return duplicate_allocatable (dest, src, type, rank, false, true,
    9910         2156 :                                 NULL_TREE, NULL_TREE);
    9911              : }
    9912              : 
    9913              : static tree
    9914           64 : duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
    9915              :                                int rank, tree add_when_allocated)
    9916              : {
    9917           64 :   tree tmp;
    9918           64 :   tree size;
    9919           64 :   tree nelems;
    9920           64 :   tree null_cond;
    9921           64 :   tree null_data;
    9922           64 :   stmtblock_t block, globalblock;
    9923              : 
    9924              :   /* If the source is null, set the destination to null.  Then,
    9925              :      allocate memory to the destination.  */
    9926           64 :   gfc_init_block (&block);
    9927           64 :   gfc_init_block (&globalblock);
    9928              : 
    9929           64 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
    9930              :     {
    9931           18 :       gfc_se se;
    9932           18 :       symbol_attribute attr;
    9933           18 :       tree dummy_desc;
    9934              : 
    9935           18 :       gfc_init_se (&se, NULL);
    9936           18 :       gfc_clear_attr (&attr);
    9937           18 :       attr.allocatable = 1;
    9938           18 :       dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
    9939           18 :       gfc_add_block_to_block (&globalblock, &se.pre);
    9940           18 :       size = TYPE_SIZE_UNIT (TREE_TYPE (type));
    9941              : 
    9942           18 :       gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
    9943           18 :       gfc_allocate_using_caf_lib (&block, dummy_desc, size,
    9944              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9945              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9946              :                                   GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
    9947           18 :       gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
    9948           18 :       null_data = gfc_finish_block (&block);
    9949              : 
    9950           18 :       gfc_init_block (&block);
    9951              : 
    9952           18 :       gfc_allocate_using_caf_lib (&block, dummy_desc,
    9953              :                                   fold_convert (size_type_node, size),
    9954              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9955              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9956              :                                   GFC_CAF_COARRAY_ALLOC);
    9957           18 :       gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
    9958              : 
    9959           18 :       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9960           18 :       tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
    9961              :                                  fold_convert (size_type_node, size));
    9962           18 :       gfc_add_expr_to_block (&block, tmp);
    9963              :     }
    9964              :   else
    9965              :     {
    9966              :       /* Set the rank or uninitialized memory access may be reported.  */
    9967           46 :       gfc_conv_descriptor_rank_set (&globalblock, dest, rank);
    9968              : 
    9969           46 :       if (rank)
    9970           46 :         nelems = gfc_full_array_size (&globalblock, src, rank);
    9971              :       else
    9972            0 :         nelems = integer_one_node;
    9973              : 
    9974           46 :       tmp = fold_convert (size_type_node,
    9975              :                           TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    9976           46 :       size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    9977              :                               fold_convert (size_type_node, nelems), tmp);
    9978              : 
    9979           46 :       gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9980           46 :       gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
    9981              :                                                               size),
    9982              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9983              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9984              :                                   GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
    9985           46 :       null_data = gfc_finish_block (&block);
    9986              : 
    9987           46 :       gfc_init_block (&block);
    9988           46 :       gfc_allocate_using_caf_lib (&block, dest,
    9989              :                                   fold_convert (size_type_node, size),
    9990              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9991              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9992              :                                   GFC_CAF_COARRAY_ALLOC);
    9993              : 
    9994           46 :       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9995           46 :       tmp = build_call_expr_loc (input_location, tmp, 3,
    9996              :                                  gfc_conv_descriptor_data_get (dest),
    9997              :                                  gfc_conv_descriptor_data_get (src),
    9998              :                                  fold_convert (size_type_node, size));
    9999           46 :       gfc_add_expr_to_block (&block, tmp);
   10000              :     }
   10001           64 :   gfc_add_expr_to_block (&block, add_when_allocated);
   10002           64 :   tmp = gfc_finish_block (&block);
   10003              : 
   10004              :   /* Null the destination if the source is null; otherwise do
   10005              :      the register and copy.  */
   10006           64 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
   10007              :     null_cond = src;
   10008              :   else
   10009           46 :     null_cond = gfc_conv_descriptor_data_get (src);
   10010              : 
   10011           64 :   null_cond = convert (pvoid_type_node, null_cond);
   10012           64 :   null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10013              :                                null_cond, null_pointer_node);
   10014           64 :   gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
   10015              :                                                  null_data));
   10016           64 :   return gfc_finish_block (&globalblock);
   10017              : }
   10018              : 
   10019              : 
   10020              : /* Helper function to abstract whether coarray processing is enabled.  */
   10021              : 
   10022              : static bool
   10023         4221 : caf_enabled (int caf_mode)
   10024              : {
   10025         4221 :   return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
   10026         4221 :       == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
   10027              : }
   10028              : 
   10029              : 
   10030              : /* Helper function to abstract whether coarray processing is enabled
   10031              :    and we are in a derived type coarray.  */
   10032              : 
   10033              : static bool
   10034        13771 : caf_in_coarray (int caf_mode)
   10035              : {
   10036        13771 :   static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   10037              :                          | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
   10038        13771 :   return (caf_mode & pat) == pat;
   10039              : }
   10040              : 
   10041              : 
   10042              : /* Helper function to abstract whether coarray is to deallocate only.  */
   10043              : 
   10044              : bool
   10045          427 : gfc_caf_is_dealloc_only (int caf_mode)
   10046              : {
   10047          427 :   return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
   10048          427 :       == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
   10049              : }
   10050              : 
   10051              : 
   10052              : /* Recursively traverse an object of derived type, generating code to
   10053              :    deallocate, nullify or copy allocatable components.  This is the work horse
   10054              :    function for the functions named in this enum.  */
   10055              : 
   10056              : enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
   10057              :       COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
   10058              :       ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
   10059              :       CHECK_PDT_INTENT_OUT_DUMMY, BCAST_ALLOC_COMP};
   10060              : 
   10061              : static gfc_actual_arglist *pdt_param_list;
   10062              : static bool generating_copy_helper;
   10063              : static hash_set<gfc_symbol *> seen_derived_types;
   10064              : 
   10065              : /* Forward declaration of structure_alloc_comps for wrapper generator.  */
   10066              : static tree structure_alloc_comps (gfc_symbol *, tree, tree, int, int, int,
   10067              :                                     gfc_co_subroutines_args *, bool);
   10068              : 
   10069              : /* Generate a wrapper function that performs element-wise deep copy for
   10070              :    recursive allocatable array components. This wrapper is passed as a
   10071              :    function pointer to the runtime helper _gfortran_cfi_deep_copy_array,
   10072              :    allowing recursion to happen at runtime instead of compile time.  */
   10073              : 
   10074              : static tree
   10075          487 : get_copy_helper_function_type (void)
   10076              : {
   10077          487 :   static tree fn_type = NULL_TREE;
   10078          487 :   if (fn_type == NULL_TREE)
   10079           99 :     fn_type = build_function_type_list (void_type_node,
   10080              :                                         pvoid_type_node,
   10081              :                                         pvoid_type_node,
   10082              :                                         NULL_TREE);
   10083          487 :   return fn_type;
   10084              : }
   10085              : 
   10086              : static tree
   10087         1688 : get_copy_helper_pointer_type (void)
   10088              : {
   10089         1688 :   static tree ptr_type = NULL_TREE;
   10090         1688 :   if (ptr_type == NULL_TREE)
   10091           99 :     ptr_type = build_pointer_type (get_copy_helper_function_type ());
   10092         1688 :   return ptr_type;
   10093              : }
   10094              : 
   10095              : static tree
   10096          388 : generate_element_copy_wrapper (gfc_symbol *der_type, tree comp_type,
   10097              :                                 int purpose, int caf_mode)
   10098              : {
   10099          388 :   tree fndecl, fntype, result_decl;
   10100          388 :   tree dest_parm, src_parm, dest_typed, src_typed;
   10101          388 :   tree der_type_ptr;
   10102          388 :   stmtblock_t block;
   10103          388 :   tree decls;
   10104          388 :   tree body;
   10105              : 
   10106          388 :   fntype = get_copy_helper_function_type ();
   10107              : 
   10108          388 :   fndecl = build_decl (input_location, FUNCTION_DECL,
   10109              :                        create_tmp_var_name ("copy_element"),
   10110              :                        fntype);
   10111              : 
   10112          388 :   TREE_STATIC (fndecl) = 1;
   10113          388 :   TREE_USED (fndecl) = 1;
   10114          388 :   DECL_ARTIFICIAL (fndecl) = 1;
   10115          388 :   DECL_IGNORED_P (fndecl) = 0;
   10116          388 :   TREE_PUBLIC (fndecl) = 0;
   10117          388 :   DECL_UNINLINABLE (fndecl) = 1;
   10118          388 :   DECL_EXTERNAL (fndecl) = 0;
   10119          388 :   DECL_CONTEXT (fndecl) = NULL_TREE;
   10120          388 :   DECL_INITIAL (fndecl) = make_node (BLOCK);
   10121          388 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
   10122              : 
   10123          388 :   result_decl = build_decl (input_location, RESULT_DECL, NULL_TREE,
   10124              :                             void_type_node);
   10125          388 :   DECL_ARTIFICIAL (result_decl) = 1;
   10126          388 :   DECL_IGNORED_P (result_decl) = 1;
   10127          388 :   DECL_CONTEXT (result_decl) = fndecl;
   10128          388 :   DECL_RESULT (fndecl) = result_decl;
   10129              : 
   10130          388 :   dest_parm = build_decl (input_location, PARM_DECL,
   10131              :                           get_identifier ("dest"), pvoid_type_node);
   10132          388 :   src_parm = build_decl (input_location, PARM_DECL,
   10133              :                          get_identifier ("src"), pvoid_type_node);
   10134              : 
   10135          388 :   DECL_ARTIFICIAL (dest_parm) = 1;
   10136          388 :   DECL_ARTIFICIAL (src_parm) = 1;
   10137          388 :   DECL_ARG_TYPE (dest_parm) = pvoid_type_node;
   10138          388 :   DECL_ARG_TYPE (src_parm) = pvoid_type_node;
   10139          388 :   DECL_CONTEXT (dest_parm) = fndecl;
   10140          388 :   DECL_CONTEXT (src_parm) = fndecl;
   10141              : 
   10142          388 :   DECL_ARGUMENTS (fndecl) = dest_parm;
   10143          388 :   TREE_CHAIN (dest_parm) = src_parm;
   10144              : 
   10145          388 :   push_struct_function (fndecl);
   10146          388 :   cfun->function_end_locus = input_location;
   10147              : 
   10148          388 :   pushlevel ();
   10149          388 :   gfc_init_block (&block);
   10150              : 
   10151          388 :   bool saved_generating = generating_copy_helper;
   10152          388 :   generating_copy_helper = true;
   10153              : 
   10154              :   /* When generating a wrapper, we need a fresh type tracking state to
   10155              :      avoid inheriting the parent context's seen_derived_types, which would
   10156              :      cause infinite recursion when the wrapper tries to handle the same
   10157              :      recursive type.  Save elements, clear the set, generate wrapper, then
   10158              :      restore elements.  */
   10159          388 :   vec<gfc_symbol *> saved_symbols = vNULL;
   10160          388 :   for (hash_set<gfc_symbol *>::iterator it = seen_derived_types.begin ();
   10161          930 :        it != seen_derived_types.end (); ++it)
   10162          542 :     saved_symbols.safe_push (*it);
   10163          388 :   seen_derived_types.empty ();
   10164              : 
   10165          388 :   der_type_ptr = build_pointer_type (comp_type);
   10166          388 :   dest_typed = fold_convert (der_type_ptr, dest_parm);
   10167          388 :   src_typed = fold_convert (der_type_ptr, src_parm);
   10168              : 
   10169          388 :   dest_typed = build_fold_indirect_ref (dest_typed);
   10170          388 :   src_typed = build_fold_indirect_ref (src_typed);
   10171              : 
   10172          388 :   body = structure_alloc_comps (der_type, src_typed, dest_typed,
   10173              :                                 0, purpose, caf_mode, NULL, false);
   10174          388 :   gfc_add_expr_to_block (&block, body);
   10175              : 
   10176              :   /* Restore saved symbols.  */
   10177          388 :   seen_derived_types.empty ();
   10178          930 :   for (unsigned i = 0; i < saved_symbols.length (); i++)
   10179          542 :     seen_derived_types.add (saved_symbols[i]);
   10180          388 :   saved_symbols.release ();
   10181          388 :   generating_copy_helper = saved_generating;
   10182              : 
   10183          388 :   body = gfc_finish_block (&block);
   10184          388 :   decls = getdecls ();
   10185              : 
   10186          388 :   poplevel (1, 1);
   10187              : 
   10188          776 :   DECL_SAVED_TREE (fndecl)
   10189          388 :     = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR,
   10190          388 :                        void_type_node, decls, body, DECL_INITIAL (fndecl));
   10191              : 
   10192          388 :   pop_cfun ();
   10193              : 
   10194              :   /* Use finalize_function with no_collect=true to skip the ggc_collect
   10195              :      call that add_new_function would trigger.  This function is called
   10196              :      during tree lowering of structure_alloc_comps where caller stack
   10197              :      frames hold locally-computed tree nodes (COMPONENT_REFs etc.) that
   10198              :      are not yet attached to any GC root.  A collection at this point
   10199              :      would free those nodes and cause segfaults.  PR124235.  */
   10200          388 :   cgraph_node::finalize_function (fndecl, true);
   10201              : 
   10202          388 :   return build1 (ADDR_EXPR, get_copy_helper_pointer_type (), fndecl);
   10203              : }
   10204              : 
   10205              : static tree
   10206        29084 : structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
   10207              :                        int rank, int purpose, int caf_mode,
   10208              :                        gfc_co_subroutines_args *args,
   10209              :                        bool no_finalization = false)
   10210              : {
   10211        29084 :   gfc_component *c;
   10212        29084 :   gfc_loopinfo loop;
   10213        29084 :   stmtblock_t fnblock;
   10214        29084 :   stmtblock_t loopbody;
   10215        29084 :   stmtblock_t tmpblock;
   10216        29084 :   tree decl_type;
   10217        29084 :   tree tmp;
   10218        29084 :   tree comp;
   10219        29084 :   tree dcmp;
   10220        29084 :   tree nelems;
   10221        29084 :   tree index;
   10222        29084 :   tree var;
   10223        29084 :   tree cdecl;
   10224        29084 :   tree ctype;
   10225        29084 :   tree vref, dref;
   10226        29084 :   tree null_cond = NULL_TREE;
   10227        29084 :   tree add_when_allocated;
   10228        29084 :   tree dealloc_fndecl;
   10229        29084 :   tree caf_token;
   10230        29084 :   gfc_symbol *vtab;
   10231        29084 :   int caf_dereg_mode;
   10232        29084 :   symbol_attribute *attr;
   10233        29084 :   bool deallocate_called;
   10234        29084 :   bool intent_out = purpose == CHECK_PDT_INTENT_OUT_DUMMY;
   10235              : 
   10236        29084 :   gfc_init_block (&fnblock);
   10237              : 
   10238        29084 :   decl_type = TREE_TYPE (decl);
   10239              : 
   10240        29084 :   if ((POINTER_TYPE_P (decl_type))
   10241              :         || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
   10242              :     {
   10243         1899 :       decl = build_fold_indirect_ref_loc (input_location, decl);
   10244              :       /* Deref dest in sync with decl, but only when it is not NULL.  */
   10245         1899 :       if (dest)
   10246          124 :         dest = build_fold_indirect_ref_loc (input_location, dest);
   10247              : 
   10248              :       /* Update the decl_type because it got dereferenced.  */
   10249         1899 :       decl_type = TREE_TYPE (decl);
   10250              :     }
   10251              : 
   10252              :   /* If this is an array of derived types with allocatable components
   10253              :      build a loop and recursively call this function.  */
   10254        29084 :   if (TREE_CODE (decl_type) == ARRAY_TYPE
   10255        29084 :       || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
   10256              :     {
   10257         5140 :       tmp = gfc_conv_array_data (decl);
   10258         5140 :       var = build_fold_indirect_ref_loc (input_location, tmp);
   10259              : 
   10260              :       /* Get the number of elements - 1 and set the counter.  */
   10261         5140 :       if (GFC_DESCRIPTOR_TYPE_P (decl_type))
   10262              :         {
   10263              :           /* Use the descriptor for an allocatable array.  Since this
   10264              :              is a full array reference, we only need the descriptor
   10265              :              information from dimension = rank.  */
   10266         3692 :           tmp = gfc_full_array_size (&fnblock, decl, rank);
   10267         3692 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
   10268              :                                  gfc_array_index_type, tmp,
   10269              :                                  gfc_index_one_node);
   10270              : 
   10271         3692 :           null_cond = gfc_conv_descriptor_data_get (decl);
   10272         3692 :           null_cond = fold_build2_loc (input_location, NE_EXPR,
   10273              :                                        logical_type_node, null_cond,
   10274         3692 :                                        build_int_cst (TREE_TYPE (null_cond), 0));
   10275              :         }
   10276              :       else
   10277              :         {
   10278              :           /*  Otherwise use the TYPE_DOMAIN information.  */
   10279         1448 :           tmp = array_type_nelts_minus_one (decl_type);
   10280         1448 :           tmp = fold_convert (gfc_array_index_type, tmp);
   10281              :         }
   10282              : 
   10283              :       /* Remember that this is, in fact, the no. of elements - 1.  */
   10284         5140 :       nelems = gfc_evaluate_now (tmp, &fnblock);
   10285         5140 :       index = gfc_create_var (gfc_array_index_type, "S");
   10286              : 
   10287              :       /* Build the body of the loop.  */
   10288         5140 :       gfc_init_block (&loopbody);
   10289              : 
   10290         5140 :       vref = gfc_build_array_ref (var, index, NULL);
   10291              : 
   10292         5140 :       if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
   10293              :         {
   10294         1131 :           tmp = build_fold_indirect_ref_loc (input_location,
   10295              :                                              gfc_conv_array_data (dest));
   10296         1131 :           dref = gfc_build_array_ref (tmp, index, NULL);
   10297         1131 :           tmp = structure_alloc_comps (der_type, vref, dref, rank,
   10298              :                                        COPY_ALLOC_COMP, caf_mode, args,
   10299              :                                        no_finalization);
   10300              :         }
   10301              :       else
   10302         4009 :         tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
   10303              :                                      caf_mode, args, no_finalization);
   10304              : 
   10305         5140 :       gfc_add_expr_to_block (&loopbody, tmp);
   10306              : 
   10307              :       /* Build the loop and return.  */
   10308         5140 :       gfc_init_loopinfo (&loop);
   10309         5140 :       loop.dimen = 1;
   10310         5140 :       loop.from[0] = gfc_index_zero_node;
   10311         5140 :       loop.loopvar[0] = index;
   10312         5140 :       loop.to[0] = nelems;
   10313         5140 :       gfc_trans_scalarizing_loops (&loop, &loopbody);
   10314         5140 :       gfc_add_block_to_block (&fnblock, &loop.pre);
   10315              : 
   10316         5140 :       tmp = gfc_finish_block (&fnblock);
   10317              :       /* When copying allocateable components, the above implements the
   10318              :          deep copy.  Nevertheless is a deep copy only allowed, when the current
   10319              :          component is allocated, for which code will be generated in
   10320              :          gfc_duplicate_allocatable (), where the deep copy code is just added
   10321              :          into the if's body, by adding tmp (the deep copy code) as last
   10322              :          argument to gfc_duplicate_allocatable ().  */
   10323         5140 :       if (purpose == COPY_ALLOC_COMP && caf_mode == 0
   10324         5140 :           && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
   10325          846 :         tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
   10326              :                                          tmp);
   10327         4294 :       else if (null_cond != NULL_TREE)
   10328         2846 :         tmp = build3_v (COND_EXPR, null_cond, tmp,
   10329              :                         build_empty_stmt (input_location));
   10330              : 
   10331         5140 :       return tmp;
   10332              :     }
   10333              : 
   10334        23944 :   if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
   10335              :     {
   10336          947 :       tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   10337              :                                    DEALLOCATE_PDT_COMP, 0, args,
   10338              :                                    no_finalization);
   10339          947 :       gfc_add_expr_to_block (&fnblock, tmp);
   10340              :     }
   10341        22997 :   else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
   10342              :     {
   10343          144 :       tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   10344              :                                    NULLIFY_ALLOC_COMP, 0, args,
   10345              :                                    no_finalization);
   10346          144 :       gfc_add_expr_to_block (&fnblock, tmp);
   10347              :     }
   10348              : 
   10349              :   /* Still having a descriptor array of rank == 0 here, indicates an
   10350              :      allocatable coarrays.  Dereference it correctly.  */
   10351        23944 :   if (GFC_DESCRIPTOR_TYPE_P (decl_type))
   10352              :     {
   10353           23 :       decl = build_fold_indirect_ref (gfc_conv_array_data (decl));
   10354              :     }
   10355              :   /* Otherwise, act on the components or recursively call self to
   10356              :      act on a chain of components.  */
   10357        23944 :   seen_derived_types.add (der_type);
   10358        70718 :   for (c = der_type->components; c; c = c->next)
   10359              :     {
   10360        46774 :       bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
   10361        46774 :                                   || c->ts.type == BT_CLASS)
   10362        46774 :                                     && c->ts.u.derived->attr.alloc_comp;
   10363        46774 :       bool same_type
   10364              :         = (c->ts.type == BT_DERIVED
   10365        10495 :            && seen_derived_types.contains (c->ts.u.derived))
   10366        54309 :           || (c->ts.type == BT_CLASS
   10367         2598 :               && seen_derived_types.contains (CLASS_DATA (c)->ts.u.derived));
   10368        46774 :       bool inside_wrapper = generating_copy_helper;
   10369              : 
   10370        46774 :       bool is_pdt_type = IS_PDT (c);
   10371        46774 :       tree strlen = NULL_TREE;
   10372              : 
   10373        46774 :       cdecl = c->backend_decl;
   10374        46774 :       ctype = TREE_TYPE (cdecl);
   10375              : 
   10376        46774 :       switch (purpose)
   10377              :         {
   10378              : 
   10379            3 :         case BCAST_ALLOC_COMP:
   10380              : 
   10381            3 :           tree ubound;
   10382            3 :           tree cdesc;
   10383            3 :           stmtblock_t derived_type_block;
   10384              : 
   10385            3 :           gfc_init_block (&tmpblock);
   10386              : 
   10387            3 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10388              :                                   decl, cdecl, NULL_TREE);
   10389              : 
   10390              :           /* Shortcut to get the attributes of the component.  */
   10391            3 :           if (c->ts.type == BT_CLASS)
   10392              :             {
   10393            0 :               attr = &CLASS_DATA (c)->attr;
   10394            0 :               if (attr->class_pointer)
   10395            0 :                 continue;
   10396              :             }
   10397              :           else
   10398              :             {
   10399            3 :               attr = &c->attr;
   10400            3 :               if (attr->pointer)
   10401            0 :                 continue;
   10402              :             }
   10403              : 
   10404              :           /* Do not broadcast a caf_token.  These are local to the image.  */
   10405            3 :           if (attr->caf_token)
   10406            1 :             continue;
   10407              : 
   10408            2 :           add_when_allocated = NULL_TREE;
   10409            2 :           if (cmp_has_alloc_comps
   10410            0 :               && !c->attr.pointer && !c->attr.proc_pointer)
   10411              :             {
   10412            0 :               if (c->ts.type == BT_CLASS)
   10413              :                 {
   10414            0 :                   rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
   10415            0 :                   add_when_allocated
   10416            0 :                       = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
   10417              :                                                comp, NULL_TREE, rank, purpose,
   10418              :                                                caf_mode, args, no_finalization);
   10419              :                 }
   10420              :               else
   10421              :                 {
   10422            0 :                   rank = c->as ? c->as->rank : 0;
   10423            0 :                   add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10424              :                                                               comp, NULL_TREE,
   10425              :                                                               rank, purpose,
   10426              :                                                               caf_mode, args,
   10427              :                                                               no_finalization);
   10428              :                 }
   10429              :             }
   10430              : 
   10431            2 :           gfc_init_block (&derived_type_block);
   10432            2 :           if (add_when_allocated)
   10433            0 :             gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
   10434            2 :           tmp = gfc_finish_block (&derived_type_block);
   10435            2 :           gfc_add_expr_to_block (&tmpblock, tmp);
   10436              : 
   10437              :           /* Convert the component into a rank 1 descriptor type.  */
   10438            2 :           if (attr->dimension)
   10439              :             {
   10440            0 :               tmp = gfc_get_element_type (TREE_TYPE (comp));
   10441            0 :               if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10442            0 :                 ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
   10443              :               else
   10444            0 :                 ubound = gfc_full_array_size (&tmpblock, comp,
   10445            0 :                                               c->ts.type == BT_CLASS
   10446            0 :                                               ? CLASS_DATA (c)->as->rank
   10447            0 :                                               : c->as->rank);
   10448              :             }
   10449              :           else
   10450              :             {
   10451            2 :               tmp = TREE_TYPE (comp);
   10452            2 :               ubound = build_int_cst (gfc_array_index_type, 1);
   10453              :             }
   10454              : 
   10455              :           /* Treat strings like arrays.  Or the other way around, do not
   10456              :            * generate an additional array layer for scalar components.  */
   10457            2 :           if (attr->dimension || c->ts.type == BT_CHARACTER)
   10458              :             {
   10459            0 :               cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
   10460              :                                                  &ubound, 1,
   10461              :                                                  GFC_ARRAY_ALLOCATABLE, false);
   10462              : 
   10463            0 :               cdesc = gfc_create_var (cdesc, "cdesc");
   10464            0 :               DECL_ARTIFICIAL (cdesc) = 1;
   10465              : 
   10466            0 :               gfc_conv_descriptor_dtype_set (&tmpblock, cdesc,
   10467              :                                              gfc_get_dtype_rank_type (1, tmp));
   10468            0 :               gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
   10469              :                                               gfc_index_zero_node,
   10470              :                                               gfc_index_one_node);
   10471            0 :               gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
   10472              :                                               gfc_index_zero_node,
   10473              :                                               gfc_index_one_node);
   10474            0 :               gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
   10475              :                                               gfc_index_zero_node, ubound);
   10476              :             }
   10477              :           else
   10478              :             /* Prevent warning.  */
   10479              :             cdesc = NULL_TREE;
   10480              : 
   10481            2 :           if (attr->dimension)
   10482              :             {
   10483            0 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10484            0 :                 comp = gfc_conv_descriptor_data_get (comp);
   10485              :               else
   10486            0 :                 comp = gfc_build_addr_expr (NULL_TREE, comp);
   10487              :             }
   10488              :           else
   10489              :             {
   10490            2 :               gfc_se se;
   10491              : 
   10492            2 :               gfc_init_se (&se, NULL);
   10493              : 
   10494            2 :               comp = gfc_conv_scalar_to_descriptor (&se, comp,
   10495            2 :                                                      c->ts.type == BT_CLASS
   10496            2 :                                                      ? CLASS_DATA (c)->attr
   10497              :                                                      : c->attr);
   10498            2 :               if (c->ts.type == BT_CHARACTER)
   10499            0 :                 comp = gfc_build_addr_expr (NULL_TREE, comp);
   10500            2 :               gfc_add_block_to_block (&tmpblock, &se.pre);
   10501              :             }
   10502              : 
   10503            2 :           if (attr->dimension || c->ts.type == BT_CHARACTER)
   10504            0 :             gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
   10505              :           else
   10506            2 :             cdesc = comp;
   10507              : 
   10508            2 :           tree fndecl;
   10509              : 
   10510            2 :           fndecl = build_call_expr_loc (input_location,
   10511              :                                         gfor_fndecl_co_broadcast, 5,
   10512              :                                         gfc_build_addr_expr (pvoid_type_node,cdesc),
   10513              :                                         args->image_index,
   10514              :                                         null_pointer_node, null_pointer_node,
   10515              :                                         null_pointer_node);
   10516              : 
   10517            2 :           gfc_add_expr_to_block (&tmpblock, fndecl);
   10518            2 :           gfc_add_block_to_block (&fnblock, &tmpblock);
   10519              : 
   10520        36448 :           break;
   10521              : 
   10522        16539 :         case DEALLOCATE_ALLOC_COMP:
   10523              : 
   10524        16539 :           gfc_init_block (&tmpblock);
   10525              : 
   10526        16539 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10527              :                                   decl, cdecl, NULL_TREE);
   10528              : 
   10529              :           /* Shortcut to get the attributes of the component.  */
   10530        16539 :           if (c->ts.type == BT_CLASS)
   10531              :             {
   10532         1136 :               attr = &CLASS_DATA (c)->attr;
   10533         1136 :               if (attr->class_pointer || c->attr.proc_pointer)
   10534           18 :                 continue;
   10535              :             }
   10536              :           else
   10537              :             {
   10538        15403 :               attr = &c->attr;
   10539        15403 :               if (attr->pointer || attr->proc_pointer)
   10540          161 :                 continue;
   10541              :             }
   10542              : 
   10543        16360 :           if (!no_finalization && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
   10544         9719 :              || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer)))
   10545              :             /* Call the finalizer, which will free the memory and nullify the
   10546              :                pointer of an array.  */
   10547         4146 :             deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
   10548              :                                                          caf_enabled (caf_mode))
   10549         4146 :                 && attr->dimension;
   10550              :           else
   10551              :             deallocate_called = false;
   10552              : 
   10553              :           /* Add the _class ref for classes.  */
   10554        16360 :           if (c->ts.type == BT_CLASS && attr->allocatable)
   10555         1118 :             comp = gfc_class_data_get (comp);
   10556              : 
   10557        16360 :           add_when_allocated = NULL_TREE;
   10558        16360 :           if (cmp_has_alloc_comps
   10559         3549 :               && !c->attr.pointer && !c->attr.proc_pointer
   10560              :               && !same_type
   10561         3549 :               && !deallocate_called)
   10562              :             {
   10563              :               /* Add checked deallocation of the components.  This code is
   10564              :                  obviously added because the finalizer is not trusted to free
   10565              :                  all memory.  */
   10566         2185 :               if (c->ts.type == BT_CLASS)
   10567              :                 {
   10568          242 :                   rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
   10569          242 :                   add_when_allocated
   10570          242 :                       = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
   10571              :                                                comp, NULL_TREE, rank, purpose,
   10572              :                                                caf_mode, args, no_finalization);
   10573              :                 }
   10574              :               else
   10575              :                 {
   10576         1943 :                   rank = c->as ? c->as->rank : 0;
   10577         1943 :                   add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10578              :                                                               comp, NULL_TREE,
   10579              :                                                               rank, purpose,
   10580              :                                                               caf_mode, args,
   10581              :                                                               no_finalization);
   10582              :                 }
   10583              :             }
   10584              : 
   10585        10301 :           if (attr->allocatable && !same_type
   10586        25572 :               && (!attr->codimension || caf_enabled (caf_mode)))
   10587              :             {
   10588              :               /* Handle all types of components besides components of the
   10589              :                  same_type as the current one, because those would create an
   10590              :                  endless loop.  */
   10591           51 :               caf_dereg_mode = (caf_in_coarray (caf_mode)
   10592           58 :                                 && (attr->dimension || c->caf_token))
   10593         9148 :                                     || attr->codimension
   10594         9283 :                                  ? (gfc_caf_is_dealloc_only (caf_mode)
   10595              :                                       ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
   10596              :                                       : GFC_CAF_COARRAY_DEREGISTER)
   10597              :                                  : GFC_CAF_COARRAY_NOCOARRAY;
   10598              : 
   10599         9205 :               caf_token = NULL_TREE;
   10600              :               /* Coarray components are handled directly by
   10601              :                  deallocate_with_status.  */
   10602         9205 :               if (!attr->codimension
   10603         9184 :                   && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
   10604              :                 {
   10605           57 :                   if (c->caf_token)
   10606           19 :                     caf_token
   10607           19 :                       = fold_build3_loc (input_location, COMPONENT_REF,
   10608           19 :                                          TREE_TYPE (gfc_comp_caf_token (c)),
   10609              :                                          decl, gfc_comp_caf_token (c),
   10610              :                                          NULL_TREE);
   10611           38 :                   else if (attr->dimension && !attr->proc_pointer)
   10612           38 :                     caf_token = gfc_conv_descriptor_token (comp);
   10613              :                 }
   10614              : 
   10615         9205 :               tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
   10616              :                                                 NULL_TREE, NULL_TREE, true,
   10617              :                                                 NULL, caf_dereg_mode, NULL_TREE,
   10618              :                                                 add_when_allocated, caf_token);
   10619              : 
   10620         9205 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10621              :             }
   10622         7155 :           else if (attr->allocatable && !attr->codimension
   10623         1089 :                    && !deallocate_called)
   10624              :             {
   10625              :               /* Case of recursive allocatable derived types.  */
   10626         1089 :               tree is_allocated;
   10627         1089 :               tree ubound;
   10628         1089 :               tree cdesc;
   10629         1089 :               stmtblock_t dealloc_block;
   10630              : 
   10631         1089 :               gfc_init_block (&dealloc_block);
   10632         1089 :               if (add_when_allocated)
   10633            0 :                 gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
   10634              : 
   10635              :               /* Convert the component into a rank 1 descriptor type.  */
   10636         1089 :               if (attr->dimension)
   10637              :                 {
   10638          327 :                   tmp = gfc_get_element_type (TREE_TYPE (comp));
   10639          327 :                   ubound = gfc_full_array_size (&dealloc_block, comp,
   10640          327 :                                                 c->ts.type == BT_CLASS
   10641            0 :                                                 ? CLASS_DATA (c)->as->rank
   10642          327 :                                                 : c->as->rank);
   10643              :                 }
   10644              :               else
   10645              :                 {
   10646          762 :                   tmp = TREE_TYPE (comp);
   10647          762 :                   ubound = build_int_cst (gfc_array_index_type, 1);
   10648              :                 }
   10649              : 
   10650         1089 :               cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
   10651              :                                                  &ubound, 1,
   10652              :                                                  GFC_ARRAY_ALLOCATABLE, false);
   10653              : 
   10654         1089 :               cdesc = gfc_create_var (cdesc, "cdesc");
   10655         1089 :               DECL_ARTIFICIAL (cdesc) = 1;
   10656              : 
   10657         1089 :               gfc_conv_descriptor_dtype_set (&dealloc_block, cdesc,
   10658              :                                              gfc_get_dtype_rank_type (1, tmp));
   10659         1089 :               gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
   10660              :                                               gfc_index_zero_node,
   10661              :                                               gfc_index_one_node);
   10662         1089 :               gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
   10663              :                                               gfc_index_zero_node,
   10664              :                                               gfc_index_one_node);
   10665         1089 :               gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
   10666              :                                               gfc_index_zero_node, ubound);
   10667              : 
   10668         1089 :               if (attr->dimension)
   10669          327 :                 comp = gfc_conv_descriptor_data_get (comp);
   10670              : 
   10671         1089 :               gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
   10672              : 
   10673              :               /* Now call the deallocator.  */
   10674         1089 :               vtab = gfc_find_vtab (&c->ts);
   10675         1089 :               if (vtab->backend_decl == NULL)
   10676           53 :                 gfc_get_symbol_decl (vtab);
   10677         1089 :               tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
   10678         1089 :               dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
   10679         1089 :               dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
   10680              :                                                             dealloc_fndecl);
   10681         1089 :               tmp = build_int_cst (TREE_TYPE (comp), 0);
   10682         1089 :               is_allocated = fold_build2_loc (input_location, NE_EXPR,
   10683              :                                               logical_type_node, tmp,
   10684              :                                               comp);
   10685         1089 :               cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
   10686              : 
   10687         1089 :               tmp = build_call_expr_loc (input_location,
   10688              :                                          dealloc_fndecl, 1,
   10689              :                                          cdesc);
   10690         1089 :               gfc_add_expr_to_block (&dealloc_block, tmp);
   10691              : 
   10692         1089 :               tmp = gfc_finish_block (&dealloc_block);
   10693              : 
   10694         1089 :               tmp = fold_build3_loc (input_location, COND_EXPR,
   10695              :                                      void_type_node, is_allocated, tmp,
   10696              :                                      build_empty_stmt (input_location));
   10697              : 
   10698         1089 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10699         1089 :             }
   10700         6066 :           else if (add_when_allocated)
   10701         1151 :             gfc_add_expr_to_block (&tmpblock, add_when_allocated);
   10702              : 
   10703         1118 :           if (c->ts.type == BT_CLASS && attr->allocatable
   10704        17478 :               && (!attr->codimension || !caf_enabled (caf_mode)))
   10705              :             {
   10706              :               /* Finally, reset the vptr to the declared type vtable and, if
   10707              :                  necessary reset the _len field.
   10708              : 
   10709              :                  First recover the reference to the component and obtain
   10710              :                  the vptr.  */
   10711         1103 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10712              :                                       decl, cdecl, NULL_TREE);
   10713         1103 :               tmp = gfc_class_vptr_get (comp);
   10714              : 
   10715         1103 :               if (UNLIMITED_POLY (c))
   10716              :                 {
   10717              :                   /* Both vptr and _len field should be nulled.  */
   10718          231 :                   gfc_add_modify (&tmpblock, tmp,
   10719          231 :                                   build_int_cst (TREE_TYPE (tmp), 0));
   10720          231 :                   tmp = gfc_class_len_get (comp);
   10721          231 :                   gfc_add_modify (&tmpblock, tmp,
   10722          231 :                                   build_int_cst (TREE_TYPE (tmp), 0));
   10723              :                 }
   10724              :               else
   10725              :                 {
   10726              :                   /* Build the vtable address and set the vptr with it.  */
   10727          872 :                   gfc_reset_vptr (&tmpblock, nullptr, tmp, c->ts.u.derived);
   10728              :                 }
   10729              :             }
   10730              : 
   10731              :           /* Now add the deallocation of this component.  */
   10732        16360 :           gfc_add_block_to_block (&fnblock, &tmpblock);
   10733        16360 :           break;
   10734              : 
   10735         6410 :         case NULLIFY_ALLOC_COMP:
   10736              :           /* Nullify
   10737              :              - allocatable components (regular or in class)
   10738              :              - components that have allocatable components
   10739              :              - pointer components when in a coarray.
   10740              :              Skip everything else especially proc_pointers, which may come
   10741              :              coupled with the regular pointer attribute.  */
   10742         8644 :           if (c->attr.proc_pointer
   10743         6410 :               || !(c->attr.allocatable || (c->ts.type == BT_CLASS
   10744          560 :                                            && CLASS_DATA (c)->attr.allocatable)
   10745         2867 :                    || (cmp_has_alloc_comps
   10746          550 :                        && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
   10747           18 :                            || (c->ts.type == BT_CLASS
   10748           12 :                                && !CLASS_DATA (c)->attr.class_pointer)))
   10749         2335 :                    || (caf_in_coarray (caf_mode) && c->attr.pointer)))
   10750         2234 :             continue;
   10751              : 
   10752              :           /* Process class components first, because they always have the
   10753              :              pointer-attribute set which would be caught wrong else.  */
   10754         4176 :           if (c->ts.type == BT_CLASS
   10755          541 :               && (CLASS_DATA (c)->attr.allocatable
   10756            0 :                   || CLASS_DATA (c)->attr.class_pointer))
   10757              :             {
   10758          541 :               tree class_ref;
   10759              : 
   10760              :               /* Allocatable CLASS components.  */
   10761          541 :               class_ref = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10762              :                                            decl, cdecl, NULL_TREE);
   10763              : 
   10764          541 :               comp = gfc_class_data_get (class_ref);
   10765          541 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10766          317 :                 gfc_conv_descriptor_data_set (&fnblock, comp,
   10767              :                                               null_pointer_node);
   10768              :               else
   10769              :                 {
   10770          224 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10771              :                                          void_type_node, comp,
   10772          224 :                                          build_int_cst (TREE_TYPE (comp), 0));
   10773          224 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10774              :                 }
   10775              : 
   10776              :               /* The dynamic type of a disassociated pointer or unallocated
   10777              :                  allocatable variable is its declared type. An unlimited
   10778              :                  polymorphic entity has no declared type.  */
   10779          541 :               gfc_reset_vptr (&fnblock, nullptr, class_ref, c->ts.u.derived);
   10780              : 
   10781          541 :               cmp_has_alloc_comps = false;
   10782          541 :             }
   10783              :           /* Coarrays need the component to be nulled before the api-call
   10784              :              is made.  */
   10785         3635 :           else if (c->attr.pointer || c->attr.allocatable)
   10786              :             {
   10787         3103 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10788              :                                       decl, cdecl, NULL_TREE);
   10789         3103 :               if (c->attr.dimension || c->attr.codimension)
   10790         2219 :                 gfc_conv_descriptor_data_set (&fnblock, comp,
   10791              :                                               null_pointer_node);
   10792              :               else
   10793          884 :                 gfc_add_modify (&fnblock, comp,
   10794          884 :                                 build_int_cst (TREE_TYPE (comp), 0));
   10795         3103 :               if (!c->attr.pdt_string && gfc_deferred_strlen (c, &comp))
   10796              :                 {
   10797          323 :                   comp = fold_build3_loc (input_location, COMPONENT_REF,
   10798          323 :                                           TREE_TYPE (comp),
   10799              :                                           decl, comp, NULL_TREE);
   10800          646 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10801          323 :                                          TREE_TYPE (comp), comp,
   10802          323 :                                          build_int_cst (TREE_TYPE (comp), 0));
   10803          323 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10804              :                 }
   10805              :               cmp_has_alloc_comps = false;
   10806              :             }
   10807              : 
   10808         4176 :           if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
   10809              :             {
   10810              :               /* Register a component of a derived type coarray with the
   10811              :                  coarray library.  Do not register ultimate component
   10812              :                  coarrays here.  They are treated like regular coarrays and
   10813              :                  are either allocated on all images or on none.  */
   10814          132 :               tree token;
   10815              : 
   10816          132 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10817              :                                       decl, cdecl, NULL_TREE);
   10818          132 :               if (c->attr.dimension)
   10819              :                 {
   10820              :                   /* Set the dtype, because caf_register needs it.  */
   10821          104 :                   tree dtype_val = gfc_get_dtype (TREE_TYPE (comp));
   10822          104 :                   gfc_conv_descriptor_dtype_set (&fnblock, comp, dtype_val);
   10823          104 :                   tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10824              :                                          decl, cdecl, NULL_TREE);
   10825          104 :                   token = gfc_conv_descriptor_token (tmp);
   10826              :                 }
   10827              :               else
   10828              :                 {
   10829           28 :                   gfc_se se;
   10830              : 
   10831           28 :                   gfc_init_se (&se, NULL);
   10832           56 :                   token = fold_build3_loc (input_location, COMPONENT_REF,
   10833              :                                            pvoid_type_node, decl,
   10834           28 :                                            gfc_comp_caf_token (c), NULL_TREE);
   10835           28 :                   comp = gfc_conv_scalar_to_descriptor (&se, comp,
   10836           28 :                                                         c->ts.type == BT_CLASS
   10837           28 :                                                         ? CLASS_DATA (c)->attr
   10838              :                                                         : c->attr);
   10839           28 :                   gfc_add_block_to_block (&fnblock, &se.pre);
   10840              :                 }
   10841              : 
   10842          132 :               gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
   10843              :                                           gfc_build_addr_expr (NULL_TREE,
   10844              :                                                                token),
   10845              :                                           NULL_TREE, NULL_TREE, NULL_TREE,
   10846              :                                           GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
   10847              :             }
   10848              : 
   10849         4176 :           if (cmp_has_alloc_comps)
   10850              :             {
   10851          532 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10852              :                                       decl, cdecl, NULL_TREE);
   10853          532 :               rank = c->as ? c->as->rank : 0;
   10854          532 :               tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
   10855              :                                            rank, purpose, caf_mode, args,
   10856              :                                            no_finalization);
   10857          532 :               gfc_add_expr_to_block (&fnblock, tmp);
   10858              :             }
   10859              :           break;
   10860              : 
   10861           30 :         case REASSIGN_CAF_COMP:
   10862           30 :           if (caf_enabled (caf_mode)
   10863           30 :               && (c->attr.codimension
   10864           23 :                   || (c->ts.type == BT_CLASS
   10865            2 :                       && (CLASS_DATA (c)->attr.coarray_comp
   10866            2 :                           || caf_in_coarray (caf_mode)))
   10867           21 :                   || (c->ts.type == BT_DERIVED
   10868            7 :                       && (c->ts.u.derived->attr.coarray_comp
   10869            6 :                           || caf_in_coarray (caf_mode))))
   10870           46 :               && !same_type)
   10871              :             {
   10872           14 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10873              :                                       decl, cdecl, NULL_TREE);
   10874           14 :               dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10875              :                                       dest, cdecl, NULL_TREE);
   10876              : 
   10877           14 :               if (c->attr.codimension)
   10878              :                 {
   10879            7 :                   if (c->ts.type == BT_CLASS)
   10880              :                     {
   10881            0 :                       comp = gfc_class_data_get (comp);
   10882            0 :                       dcmp = gfc_class_data_get (dcmp);
   10883              :                     }
   10884            7 :                   gfc_conv_descriptor_data_set (&fnblock, dcmp,
   10885              :                                            gfc_conv_descriptor_data_get (comp));
   10886              :                 }
   10887              :               else
   10888              :                 {
   10889            7 :                   tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
   10890              :                                                rank, purpose, caf_mode
   10891              :                                                | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
   10892              :                                                args, no_finalization);
   10893            7 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10894              :                 }
   10895              :             }
   10896              :           break;
   10897              : 
   10898        13851 :         case COPY_ALLOC_COMP:
   10899        13851 :           if (c->attr.pointer || c->attr.proc_pointer)
   10900          153 :             continue;
   10901              : 
   10902              :           /* We need source and destination components.  */
   10903        13698 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
   10904              :                                   cdecl, NULL_TREE);
   10905        13698 :           dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
   10906              :                                   cdecl, NULL_TREE);
   10907        13698 :           dcmp = fold_convert (TREE_TYPE (comp), dcmp);
   10908              : 
   10909        13698 :           if (IS_PDT (c) && !c->attr.allocatable)
   10910              :             {
   10911          135 :               tmp = gfc_copy_alloc_comp (c->ts.u.derived, comp, dcmp,
   10912              :                                          0, 0);
   10913          135 :               gfc_add_expr_to_block (&fnblock, tmp);
   10914          135 :               continue;
   10915              :             }
   10916              : 
   10917        13563 :           if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
   10918              :             {
   10919          860 :               tree ftn_tree;
   10920          860 :               tree size;
   10921          860 :               tree dst_data;
   10922          860 :               tree src_data;
   10923          860 :               tree null_data;
   10924              : 
   10925          860 :               dst_data = gfc_class_data_get (dcmp);
   10926          860 :               src_data = gfc_class_data_get (comp);
   10927          860 :               size = fold_convert (size_type_node,
   10928              :                                    gfc_class_vtab_size_get (comp));
   10929              : 
   10930          860 :               if (CLASS_DATA (c)->attr.dimension)
   10931              :                 {
   10932          864 :                   nelems = gfc_conv_descriptor_size (src_data,
   10933          432 :                                                      CLASS_DATA (c)->as->rank);
   10934          432 :                   size = fold_build2_loc (input_location, MULT_EXPR,
   10935              :                                           size_type_node, size,
   10936              :                                           fold_convert (size_type_node,
   10937              :                                                         nelems));
   10938              :                 }
   10939              :               else
   10940          428 :                 nelems = build_int_cst (size_type_node, 1);
   10941              : 
   10942          860 :               if (CLASS_DATA (c)->attr.dimension
   10943          428 :                   || CLASS_DATA (c)->attr.codimension)
   10944              :                 {
   10945          440 :                   src_data = gfc_conv_descriptor_data_get (src_data);
   10946          440 :                   dst_data = gfc_conv_descriptor_data_get (dst_data);
   10947              :                 }
   10948              : 
   10949          860 :               gfc_init_block (&tmpblock);
   10950              : 
   10951          860 :               gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
   10952              :                               gfc_class_vptr_get (comp));
   10953              : 
   10954              :               /* Copy the unlimited '_len' field. If it is greater than zero
   10955              :                  (ie. a character(_len)), multiply it by size and use this
   10956              :                  for the malloc call.  */
   10957          860 :               if (UNLIMITED_POLY (c))
   10958              :                 {
   10959          158 :                   gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
   10960              :                                   gfc_class_len_get (comp));
   10961          158 :                   size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
   10962              :                 }
   10963              : 
   10964              :               /* Coarray component have to have the same allocation status and
   10965              :                  shape/type-parameter/effective-type on the LHS and RHS of an
   10966              :                  intrinsic assignment. Hence, we did not deallocated them - and
   10967              :                  do not allocate them here.  */
   10968          860 :               if (!CLASS_DATA (c)->attr.codimension)
   10969              :                 {
   10970          845 :                   ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
   10971          845 :                   tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
   10972          845 :                   gfc_add_modify (&tmpblock, dst_data,
   10973          845 :                                   fold_convert (TREE_TYPE (dst_data), tmp));
   10974              :                 }
   10975              : 
   10976         1705 :               tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
   10977          860 :                                              UNLIMITED_POLY (c));
   10978          860 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10979          860 :               tmp = gfc_finish_block (&tmpblock);
   10980              : 
   10981          860 :               gfc_init_block (&tmpblock);
   10982          860 :               gfc_add_modify (&tmpblock, dst_data,
   10983          860 :                               fold_convert (TREE_TYPE (dst_data),
   10984              :                                             null_pointer_node));
   10985          860 :               null_data = gfc_finish_block (&tmpblock);
   10986              : 
   10987          860 :               null_cond = fold_build2_loc (input_location, NE_EXPR,
   10988              :                                            logical_type_node, src_data,
   10989              :                                            null_pointer_node);
   10990              : 
   10991          860 :               gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
   10992              :                                                          tmp, null_data));
   10993          860 :               continue;
   10994          860 :             }
   10995              : 
   10996              :           /* To implement guarded deep copy, i.e., deep copy only allocatable
   10997              :              components that are really allocated, the deep copy code has to
   10998              :              be generated first and then added to the if-block in
   10999              :              gfc_duplicate_allocatable ().  */
   11000        12703 :           if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
   11001              :             {
   11002         1929 :               rank = c->as ? c->as->rank : 0;
   11003         1929 :               tmp = fold_convert (TREE_TYPE (dcmp), comp);
   11004         1929 :               gfc_add_modify (&fnblock, dcmp, tmp);
   11005         1929 :               add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   11006              :                                                           comp, dcmp,
   11007              :                                                           rank, purpose,
   11008              :                                                           caf_mode, args,
   11009              :                                                           no_finalization);
   11010              :             }
   11011              :           else
   11012              :             add_when_allocated = NULL_TREE;
   11013              : 
   11014        12703 :           if (gfc_deferred_strlen (c, &tmp))
   11015              :             {
   11016          417 :               tree len, size;
   11017          417 :               len = tmp;
   11018          417 :               tmp = fold_build3_loc (input_location, COMPONENT_REF,
   11019          417 :                                      TREE_TYPE (len),
   11020              :                                      decl, len, NULL_TREE);
   11021          417 :               len = fold_build3_loc (input_location, COMPONENT_REF,
   11022          417 :                                      TREE_TYPE (len),
   11023              :                                      dest, len, NULL_TREE);
   11024          417 :               tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   11025          417 :                                      TREE_TYPE (len), len, tmp);
   11026          417 :               gfc_add_expr_to_block (&fnblock, tmp);
   11027          417 :               size = size_of_string_in_bytes (c->ts.kind, len);
   11028              :               /* This component cannot have allocatable components,
   11029              :                  therefore add_when_allocated of duplicate_allocatable ()
   11030              :                  is always NULL.  */
   11031          417 :               rank = c->as ? c->as->rank : 0;
   11032          417 :               tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
   11033              :                                            false, false, size, NULL_TREE);
   11034          417 :               gfc_add_expr_to_block (&fnblock, tmp);
   11035              :             }
   11036        12286 :           else if (c->attr.pdt_array
   11037          404 :                    && !c->attr.allocatable && !c->attr.pointer)
   11038              :             {
   11039          404 :               tmp = duplicate_allocatable (dcmp, comp, ctype,
   11040          404 :                                            c->as ? c->as->rank : 0,
   11041              :                                            false, false, NULL_TREE, NULL_TREE);
   11042          404 :               gfc_add_expr_to_block (&fnblock, tmp);
   11043              :             }
   11044              :       /* Special case: recursive allocatable array components require
   11045              :          runtime helpers to avoid compile-time infinite recursion.  Generate
   11046              :          a call to _gfortran_cfi_deep_copy_array with an element copy
   11047              :          wrapper.  When inside a wrapper, reuse current_function_decl.  */
   11048         6945 :       else if (c->attr.allocatable && cmp_has_alloc_comps && same_type
   11049         1300 :                && purpose == COPY_ALLOC_COMP && !c->attr.proc_pointer
   11050         1300 :                && !c->attr.codimension && !caf_in_coarray (caf_mode)
   11051        13182 :                && c->ts.type == BT_DERIVED && c->ts.u.derived != NULL)
   11052              :             {
   11053         1300 :               tree copy_wrapper, call, dest_addr, src_addr, elem_type;
   11054         1300 :               tree helper_ptr_type;
   11055         1300 :               tree alloc_expr;
   11056         1300 :               int comp_rank;
   11057              : 
   11058              :               /* Get the element type from ctype (already the component
   11059              :                  type).  For arrays we need the element type, not the array
   11060              :                  type.  */
   11061         1300 :               elem_type = ctype;
   11062         1300 :               if (GFC_DESCRIPTOR_TYPE_P (ctype))
   11063          930 :                 elem_type = gfc_get_element_type (ctype);
   11064          370 :               else if (TREE_CODE (ctype) == ARRAY_TYPE)
   11065            0 :                 elem_type = TREE_TYPE (ctype);
   11066          370 :               else if (!c->as)
   11067          370 :                 elem_type = TREE_TYPE (TREE_TYPE (comp));
   11068              : 
   11069         1300 :               helper_ptr_type = get_copy_helper_pointer_type ();
   11070              : 
   11071         1300 :               comp_rank = c->as ? c->as->rank : 0;
   11072         1300 :               alloc_expr = gfc_duplicate_allocatable_nocopy (dcmp, comp, ctype,
   11073              :                                                              comp_rank);
   11074         1300 :               gfc_add_expr_to_block (&fnblock, alloc_expr);
   11075              : 
   11076              :               /* Generate or reuse the element copy helper.  Inside an
   11077              :                  existing helper we can reuse the current function to
   11078              :                  prevent recursive generation.  */
   11079         1300 :               if (inside_wrapper)
   11080          912 :                 copy_wrapper
   11081          912 :                   = gfc_build_addr_expr (NULL_TREE, current_function_decl);
   11082              :               else
   11083          388 :                 copy_wrapper
   11084          388 :                   = generate_element_copy_wrapper (c->ts.u.derived, elem_type,
   11085              :                                                    purpose, caf_mode);
   11086         1300 :               copy_wrapper = fold_convert (helper_ptr_type, copy_wrapper);
   11087              : 
   11088         1300 :               if (c->as)
   11089              :                 {
   11090              :                   /* Build addresses of descriptors.  */
   11091          930 :                   dest_addr = gfc_build_addr_expr (pvoid_type_node, dcmp);
   11092          930 :                   src_addr = gfc_build_addr_expr (pvoid_type_node, comp);
   11093              :                 }
   11094              :               else
   11095              :                 {
   11096              :                   /* For scalars, create separate descriptors for source and
   11097              :                      dest, then pass their addresses.  */
   11098          370 :                   gfc_se se;
   11099          370 :                   gfc_init_se (&se, NULL);
   11100          370 :                   tmp = gfc_conv_scalar_to_descriptor (&se, dcmp, c->attr);
   11101          370 :                   dest_addr = gfc_build_addr_expr (pvoid_type_node, tmp);
   11102          370 :                   tmp = gfc_conv_scalar_to_descriptor (&se, comp, c->attr);
   11103          370 :                   src_addr = gfc_build_addr_expr (pvoid_type_node, tmp);
   11104          370 :                   gfc_add_block_to_block (&fnblock, &se.pre);
   11105              :                 }
   11106              : 
   11107              :               /* Build call: _gfortran_cfi_deep_copy_array (&dcmp, &comp, wrapper).  */
   11108         1300 :               call = build_call_expr_loc (input_location,
   11109              :                                           gfor_fndecl_cfi_deep_copy_array, 3,
   11110              :                                           dest_addr, src_addr,
   11111              :                                           copy_wrapper);
   11112              : 
   11113         1300 :               gfc_add_expr_to_block (&fnblock, call);
   11114              :             }
   11115              :           /* For allocatable arrays with nested allocatable components,
   11116              :              add_when_allocated already includes gfc_duplicate_allocatable
   11117              :              (from the recursive structure_alloc_comps call at line 10290-10293),
   11118              :              so we must not call it again here.  PR121628 added an
   11119              :              add_when_allocated != NULL clause that was redundant for scalars
   11120              :              (already handled by !c->as) and wrong for arrays (double alloc).  */
   11121         5645 :           else if (c->attr.allocatable && !c->attr.proc_pointer
   11122        16227 :                    && (!cmp_has_alloc_comps
   11123          775 :                        || !c->as
   11124          661 :                        || c->attr.codimension
   11125          658 :                        || caf_in_coarray (caf_mode)))
   11126              :             {
   11127         4993 :               rank = c->as ? c->as->rank : 0;
   11128         4993 :               if (c->attr.codimension)
   11129           20 :                 tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
   11130         4973 :               else if (flag_coarray == GFC_FCOARRAY_LIB
   11131         4973 :                        && caf_in_coarray (caf_mode))
   11132              :                 {
   11133           64 :                   tree dst_tok;
   11134           64 :                   if (c->as)
   11135           46 :                     dst_tok = gfc_conv_descriptor_token (dcmp);
   11136              :                   else
   11137              :                     {
   11138           18 :                       dst_tok
   11139           18 :                         = fold_build3_loc (input_location, COMPONENT_REF,
   11140              :                                            pvoid_type_node, dest,
   11141           18 :                                            gfc_comp_caf_token (c), NULL_TREE);
   11142              :                     }
   11143           64 :                   tmp
   11144           64 :                     = duplicate_allocatable_coarray (dcmp, dst_tok, comp, ctype,
   11145              :                                                      rank, add_when_allocated);
   11146              :                 }
   11147              :               else
   11148         4909 :                 tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
   11149              :                                                  add_when_allocated);
   11150         4993 :               gfc_add_expr_to_block (&fnblock, tmp);
   11151              :             }
   11152              :           else
   11153         5589 :             if (cmp_has_alloc_comps || is_pdt_type)
   11154         1962 :               gfc_add_expr_to_block (&fnblock, add_when_allocated);
   11155              : 
   11156              :           break;
   11157              : 
   11158         3654 :         case ALLOCATE_PDT_COMP:
   11159              : 
   11160         3654 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11161              :                                   decl, cdecl, NULL_TREE);
   11162              : 
   11163              :           /* Set the PDT KIND and LEN fields.  */
   11164         3654 :           if (c->attr.pdt_kind || c->attr.pdt_len)
   11165              :             {
   11166         1581 :               gfc_se tse;
   11167         1581 :               gfc_expr *c_expr = NULL;
   11168         1581 :               gfc_actual_arglist *param = pdt_param_list;
   11169         1581 :               gfc_init_se (&tse, NULL);
   11170         5859 :               for (; param; param = param->next)
   11171         2697 :                 if (param->name && !strcmp (c->name, param->name))
   11172         1581 :                   c_expr = param->expr;
   11173              : 
   11174         1581 :               if (!c_expr)
   11175           18 :                 c_expr = c->initializer;
   11176              : 
   11177           18 :               if (c_expr)
   11178              :                 {
   11179         1563 :                   gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11180         1563 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11181         1563 :                   gfc_add_modify (&fnblock, comp, tse.expr);
   11182         1563 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11183              :                 }
   11184         1581 :             }
   11185         2073 :           else if (c->initializer && !c->attr.pdt_string && !c->attr.pdt_array
   11186          297 :                    && !c->as && !IS_PDT (c))   /* Take care of arrays.  */
   11187              :             {
   11188          115 :               gfc_se tse;
   11189          115 :               gfc_expr *c_expr;
   11190          115 :               gfc_init_se (&tse, NULL);
   11191          115 :               c_expr = c->initializer;
   11192          115 :               gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11193          115 :               gfc_add_block_to_block (&fnblock, &tse.pre);
   11194          115 :               gfc_add_modify (&fnblock, comp, tse.expr);
   11195          115 :               gfc_add_block_to_block (&fnblock, &tse.post);
   11196              :             }
   11197              : 
   11198         3654 :           if (c->attr.pdt_string)
   11199              :             {
   11200          180 :               gfc_se tse;
   11201          180 :               gfc_init_se (&tse, NULL);
   11202          180 :               gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
   11203              :               /* Convert the parameterized string length to its value. The
   11204              :                  string length is stored in a hidden field in the same way as
   11205              :                  deferred string lengths.  */
   11206          180 :               gfc_insert_parameter_exprs (e, pdt_param_list);
   11207          180 :               if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
   11208              :                 {
   11209          180 :                   gfc_conv_expr_type (&tse, e,
   11210          180 :                                       TREE_TYPE (strlen));
   11211          180 :                   strlen = fold_build3_loc (input_location, COMPONENT_REF,
   11212          180 :                                             TREE_TYPE (strlen),
   11213              :                                             decl, strlen, NULL_TREE);
   11214          180 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11215          180 :                   gfc_add_modify (&fnblock, strlen, tse.expr);
   11216          180 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11217          180 :                   c->ts.u.cl->backend_decl = strlen;
   11218              :                 }
   11219          180 :               gfc_free_expr (e);
   11220              : 
   11221              :               /* Scalar parameterized strings can be allocated now.  */
   11222          180 :               if (!c->as)
   11223              :                 {
   11224          120 :                   tmp = fold_convert (gfc_array_index_type, strlen);
   11225          120 :                   tmp = size_of_string_in_bytes (c->ts.kind, tmp);
   11226          120 :                   tmp = gfc_evaluate_now (tmp, &fnblock);
   11227          120 :                   tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
   11228          120 :                   gfc_add_modify (&fnblock, comp, tmp);
   11229              :                 }
   11230              :             }
   11231              : 
   11232              :           /* Allocate parameterized arrays of parameterized derived types.  */
   11233         3654 :           if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
   11234         2869 :               && !(IS_PDT (c) || IS_CLASS_PDT (c)))
   11235         2601 :             continue;
   11236              : 
   11237         1053 :           if (c->ts.type == BT_CLASS)
   11238            0 :             comp = gfc_class_data_get (comp);
   11239              : 
   11240         1053 :           if (c->attr.pdt_array
   11241         1053 :               || (c->attr.pdt_string
   11242            0 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp))))
   11243              :             {
   11244          785 :               gfc_se tse;
   11245          785 :               int i;
   11246          785 :               tree size = gfc_index_one_node;
   11247          785 :               tree offset = gfc_index_zero_node;
   11248          785 :               tree lower, upper;
   11249          785 :               gfc_expr *e;
   11250              : 
   11251              :               /* This chunk takes the expressions for 'lower' and 'upper'
   11252              :                  in the arrayspec and substitutes in the expressions for
   11253              :                  the parameters from 'pdt_param_list'. The descriptor
   11254              :                  fields can then be filled from the values so obtained.  */
   11255          785 :               gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
   11256         1702 :               for (i = 0; i < c->as->rank; i++)
   11257              :                 {
   11258          917 :                   gfc_init_se (&tse, NULL);
   11259          917 :                   e = gfc_copy_expr (c->as->lower[i]);
   11260          917 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11261          917 :                   gfc_conv_expr_type (&tse, e, gfc_array_index_type);
   11262          917 :                   gfc_free_expr (e);
   11263          917 :                   lower = tse.expr;
   11264          917 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11265          917 :                   gfc_conv_descriptor_lbound_set (&fnblock, comp,
   11266              :                                                   gfc_rank_cst[i],
   11267              :                                                   lower);
   11268          917 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11269          917 :                   e = gfc_copy_expr (c->as->upper[i]);
   11270          917 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11271          917 :                   gfc_conv_expr_type (&tse, e, gfc_array_index_type);
   11272          917 :                   gfc_free_expr (e);
   11273          917 :                   upper = tse.expr;
   11274          917 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11275          917 :                   gfc_conv_descriptor_ubound_set (&fnblock, comp,
   11276              :                                                   gfc_rank_cst[i],
   11277              :                                                   upper);
   11278          917 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11279          917 :                   gfc_conv_descriptor_stride_set (&fnblock, comp,
   11280              :                                                   gfc_rank_cst[i],
   11281              :                                                   size);
   11282          917 :                   size = gfc_evaluate_now (size, &fnblock);
   11283          917 :                   tmp = fold_build2_loc (input_location,
   11284              :                                          MULT_EXPR,
   11285              :                                          gfc_array_index_type,
   11286              :                                          lower, size);
   11287          917 :                   offset = fold_build2_loc (input_location,
   11288              :                                             MINUS_EXPR,
   11289              :                                             gfc_array_index_type,
   11290              :                                             offset, tmp);
   11291          917 :                   offset = gfc_evaluate_now (offset, &fnblock);
   11292          917 :                   tmp = fold_build2_loc (input_location, MINUS_EXPR,
   11293              :                                          gfc_array_index_type,
   11294              :                                          upper, lower);
   11295          917 :                   tmp = fold_build2_loc (input_location, PLUS_EXPR,
   11296              :                                          gfc_array_index_type,
   11297              :                                          tmp, gfc_index_one_node);
   11298          917 :                   size = fold_build2_loc (input_location, MULT_EXPR,
   11299              :                                           gfc_array_index_type, size, tmp);
   11300              :                 }
   11301          785 :               gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
   11302          785 :               if (c->ts.type == BT_CLASS)
   11303              :                 {
   11304            0 :                   tmp = gfc_get_vptr_from_expr (comp);
   11305            0 :                   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
   11306            0 :                     tmp = build_fold_indirect_ref_loc (input_location, tmp);
   11307            0 :                   tmp = gfc_vptr_size_get (tmp);
   11308              :                 }
   11309          785 :               else if (strlen != NULL_TREE)
   11310           54 :                 tmp = strlen;
   11311              :               else
   11312          731 :                 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
   11313          785 :               tmp = fold_convert (gfc_array_index_type, tmp);
   11314          785 :               size = fold_build2_loc (input_location, MULT_EXPR,
   11315              :                                       gfc_array_index_type, size, tmp);
   11316          785 :               size = gfc_evaluate_now (size, &fnblock);
   11317          785 :               tmp = gfc_call_malloc (&fnblock, NULL, size);
   11318          785 :               gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
   11319          785 :               gfc_conv_descriptor_dtype_set (&fnblock, comp,
   11320              :                                              gfc_get_dtype (ctype));
   11321          785 :               if (strlen != NULL_TREE)
   11322              :                 {
   11323           54 :                   tmp = gfc_conv_descriptor_elem_len_get (comp);
   11324           54 :                   gfc_add_modify (&fnblock, tmp, fold_convert (TREE_TYPE (tmp), strlen));
   11325           54 :                   gfc_conv_descriptor_span_set (&fnblock, comp, strlen);
   11326              :                 }
   11327              : 
   11328          785 :               if (c->initializer && c->initializer->rank)
   11329              :                 {
   11330            0 :                   gfc_init_se (&tse, NULL);
   11331            0 :                   e = gfc_copy_expr (c->initializer);
   11332            0 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11333            0 :                   gfc_conv_expr_descriptor (&tse, e);
   11334            0 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11335            0 :                   gfc_free_expr (e);
   11336            0 :                   tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
   11337            0 :                   tmp = build_call_expr_loc (input_location, tmp, 3,
   11338              :                                      gfc_conv_descriptor_data_get (comp),
   11339              :                                      gfc_conv_descriptor_data_get (tse.expr),
   11340              :                                      fold_convert (size_type_node, size));
   11341            0 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11342            0 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11343              :                 }
   11344              :             }
   11345              : 
   11346              :           /* Recurse in to PDT components.  */
   11347         1053 :           if ((IS_PDT (c) || IS_CLASS_PDT (c))
   11348          282 :                && !(c->attr.pointer || c->attr.allocatable))
   11349              :             {
   11350          194 :               gfc_actual_arglist *tail = c->param_list, *parms;
   11351          194 :               bool ext;
   11352              : 
   11353          194 :               ext = gfc_get_derived_super_type (der_type) == c->ts.u.derived;
   11354              : 
   11355          496 :               for (; tail; tail = tail->next)
   11356          326 :                 if (tail->spec_type != SPEC_EXPLICIT && tail->expr)
   11357              :                   break;
   11358              : 
   11359          194 :               if (tail && ext)
   11360              :                 /* 'der_type' extends the type of this component but has no
   11361              :                    type parameters of its own and so some of the components
   11362              :                    might be assumed.  The pdt_param_list will have what is
   11363              :                    needed.  */
   11364           24 :                 parms = pdt_param_list;
   11365              :               else
   11366              :                 /* Otherwise, use the component's type parameters.  */
   11367              :                 {
   11368          170 :                   tail = parms = c->param_list;
   11369          460 :                   for (; tail; tail = tail->next)
   11370          290 :                     if (tail->expr)
   11371          266 :                       gfc_insert_parameter_exprs (tail->expr, pdt_param_list);
   11372              :                 }
   11373              : 
   11374          194 :               tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
   11375          194 :                                            c->as ? c->as->rank : 0,
   11376              :                                            parms);
   11377          194 :               gfc_add_expr_to_block (&fnblock, tmp);
   11378              :             }
   11379              : 
   11380              :           break;
   11381              : 
   11382         5789 :         case DEALLOCATE_PDT_COMP:
   11383              :           /* Deallocate array or parameterized string length components
   11384              :              of parameterized derived types.  */
   11385         5789 :           if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
   11386         4515 :               && !c->attr.pdt_string
   11387         4371 :               && !(IS_PDT (c) || IS_CLASS_PDT (c)))
   11388         3841 :             continue;
   11389              : 
   11390         1948 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11391              :                                   decl, cdecl, NULL_TREE);
   11392         1948 :           if (c->ts.type == BT_CLASS)
   11393            0 :             comp = gfc_class_data_get (comp);
   11394              : 
   11395              :           /* Recurse in to PDT components.  */
   11396         1948 :           if ((IS_PDT (c) || IS_CLASS_PDT (c))
   11397          568 :               && (!c->attr.pointer && !c->attr.allocatable))
   11398              :             {
   11399          377 :               tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
   11400          377 :                                              c->as ? c->as->rank : 0);
   11401          377 :               gfc_add_expr_to_block (&fnblock, tmp);
   11402              :             }
   11403              : 
   11404         1948 :           if (c->attr.pdt_array || c->attr.pdt_string)
   11405              :             {
   11406         1418 :               tmp = comp;
   11407         1418 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   11408         1280 :                 tmp = gfc_conv_descriptor_data_get (comp);
   11409         1418 :               null_cond = fold_build2_loc (input_location, NE_EXPR,
   11410              :                                            logical_type_node, tmp,
   11411         1418 :                                            build_int_cst (TREE_TYPE (tmp), 0));
   11412         1418 :               if (flag_openmp_allocators)
   11413              :                 {
   11414            0 :                   tree cd, t;
   11415            0 :                   if (c->attr.pdt_array)
   11416              :                     {
   11417            0 :                       tree version_val = gfc_conv_descriptor_version_get (comp);
   11418            0 :                       cd = fold_build2_loc (input_location, EQ_EXPR,
   11419              :                                             boolean_type_node, version_val,
   11420              :                                             integer_one_node);
   11421              :                     }
   11422              :                   else
   11423            0 :                     cd = gfc_omp_call_is_alloc (tmp);
   11424            0 :                   t = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
   11425            0 :                   t = build_call_expr_loc (input_location, t, 1, tmp);
   11426              : 
   11427            0 :                   stmtblock_t tblock;
   11428            0 :                   gfc_init_block (&tblock);
   11429            0 :                   gfc_add_expr_to_block (&tblock, t);
   11430            0 :                   if (c->attr.pdt_array)
   11431            0 :                     gfc_conv_descriptor_version_set (&tblock, comp,
   11432              :                                                      integer_zero_node);
   11433            0 :                   tmp = build3_loc (input_location, COND_EXPR, void_type_node,
   11434              :                                     cd, gfc_finish_block (&tblock),
   11435              :                                     gfc_call_free (tmp));
   11436              :                 }
   11437              :               else
   11438         1418 :                 tmp = gfc_call_free (tmp);
   11439         1418 :               tmp = build3_v (COND_EXPR, null_cond, tmp,
   11440              :                               build_empty_stmt (input_location));
   11441         1418 :               gfc_add_expr_to_block (&fnblock, tmp);
   11442              : 
   11443         1418 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   11444         1280 :                 gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
   11445              :               else
   11446              :                 {
   11447          138 :                   tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
   11448          138 :                   gfc_add_modify (&fnblock, comp, tmp);
   11449              :                 }
   11450              :             }
   11451              : 
   11452              :           break;
   11453              : 
   11454          498 :         case CHECK_PDT_DUMMY:
   11455          498 :         case CHECK_PDT_INTENT_OUT_DUMMY:
   11456              : 
   11457          498 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11458              :                                   decl, cdecl, NULL_TREE);
   11459          498 :           if (c->ts.type == BT_CLASS)
   11460            0 :             comp = gfc_class_data_get (comp);
   11461              : 
   11462              :           /* Recurse in to PDT components.  */
   11463          498 :           if (((c->ts.type == BT_DERIVED
   11464           42 :                 && !c->attr.allocatable && !c->attr.pointer)
   11465          456 :                || (c->ts.type == BT_CLASS
   11466            0 :                    && !CLASS_DATA (c)->attr.allocatable
   11467            0 :                    && !CLASS_DATA (c)->attr.pointer))
   11468           42 :               && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
   11469              :             {
   11470           12 :               tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
   11471           12 :                                          c->as ? c->as->rank : 0,
   11472              :                                          pdt_param_list, intent_out);
   11473           12 :               gfc_add_expr_to_block (&fnblock, tmp);
   11474              :             }
   11475              : 
   11476          498 :           if (!c->attr.pdt_len)
   11477              :             {
   11478          414 :               gfc_se tse;
   11479          738 :               if (!intent_out
   11480          120 :                   || c->attr.codimension
   11481          120 :                   || c->attr.pointer
   11482          120 :                   || IS_PDT (c) || IS_CLASS_PDT (c)
   11483          120 :                   || c->attr.pdt_comp || c->attr.pdt_kind
   11484          120 :                   || !c->initializer
   11485          504 :                   || !gfc_is_constant_expr (c->initializer)) // Just in case!
   11486          324 :                 continue;
   11487              : 
   11488              :               /* F2023 19.6.5(24) "Invocation of a procedure that has a
   11489              :                  nonpointer, nonallocatable INTENT (OUT) dummy argument causes
   11490              :                  all nonpointer default-initialized subcomponents of the dummy
   11491              :                  argument to become defined."  */
   11492           90 :               if (c->initializer->expr_type == EXPR_CONSTANT)
   11493              :                 {
   11494           30 :                   gfc_init_se (&tse, NULL);
   11495           30 :                   gfc_conv_expr (&tse, c->initializer);
   11496           30 :                   gfc_add_modify (&fnblock, comp, tse.expr);
   11497              :                 }
   11498           60 :               else if (c->initializer->expr_type == EXPR_STRUCTURE)
   11499              :                 {
   11500           30 :                   tmp = gfc_trans_structure_assign (comp, c->initializer,
   11501              :                                                     true, false);
   11502           30 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11503              :                 }
   11504           30 :               else if (c->initializer->expr_type == EXPR_ARRAY)
   11505              :                 {
   11506           30 :                   tree type = TREE_TYPE (TREE_TYPE (comp));
   11507           30 :                   tree len = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
   11508           30 :                   len = fold_build2_loc (input_location, MULT_EXPR,
   11509           30 :                                          TREE_TYPE (len), len,
   11510           30 :                                          TYPE_SIZE_UNIT (type));
   11511              : 
   11512           30 :                   tmp = gfc_build_constant_array_constructor (c->initializer, type);
   11513           30 :                   tmp = gfc_build_memcpy_call (gfc_conv_array_data (comp), tmp, len);
   11514           30 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11515              :                 }
   11516              :               else
   11517            0 :                 continue;
   11518              :             }
   11519              :           else
   11520              :             {
   11521           84 :               gfc_se tse;
   11522           84 :               gfc_expr *c_expr = NULL;
   11523           84 :               gfc_actual_arglist *param = pdt_param_list;
   11524           84 :               gfc_init_se (&tse, NULL);
   11525          300 :               for (; param; param = param->next)
   11526          132 :                 if (!strcmp (c->name, param->name)
   11527           84 :                     && param->spec_type == SPEC_EXPLICIT)
   11528           54 :                   c_expr = param->expr;
   11529              : 
   11530           84 :               if (c_expr)
   11531              :                 {
   11532           54 :                   tree error, cond, cname;
   11533           54 :                   gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11534           54 :                   cond = fold_build2_loc (input_location, NE_EXPR,
   11535              :                                           logical_type_node,
   11536              :                                           comp, tse.expr);
   11537           54 :                   cname = gfc_build_cstring_const (c->name);
   11538           54 :                   cname = gfc_build_addr_expr (pchar_type_node, cname);
   11539           54 :                   error = gfc_trans_runtime_error (true, NULL,
   11540              :                                                    "The value of the PDT LEN "
   11541              :                                                    "parameter '%s' does not "
   11542              :                                                    "agree with that in the "
   11543              :                                                    "dummy declaration",
   11544              :                                                    cname);
   11545           54 :                   tmp = fold_build3_loc (input_location, COND_EXPR,
   11546              :                                          void_type_node, cond, error,
   11547              :                                          build_empty_stmt (input_location));
   11548           54 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11549              :                 }
   11550              :             }
   11551              :           break;
   11552              : 
   11553            0 :         default:
   11554            0 :           gcc_unreachable ();
   11555        10328 :           break;
   11556              :         }
   11557              :     }
   11558        23944 :   seen_derived_types.remove (der_type);
   11559              : 
   11560        23944 :   return gfc_finish_block (&fnblock);
   11561              : }
   11562              : 
   11563              : /* Recursively traverse an object of derived type, generating code to
   11564              :    nullify allocatable components.  */
   11565              : 
   11566              : tree
   11567         3234 : gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
   11568              :                         int caf_mode)
   11569              : {
   11570         3234 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11571              :                                 NULLIFY_ALLOC_COMP,
   11572              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
   11573         3234 :                                 NULL);
   11574              : }
   11575              : 
   11576              : 
   11577              : /* Recursively traverse an object of derived type, generating code to
   11578              :    deallocate allocatable components.  */
   11579              : 
   11580              : tree
   11581         3247 : gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
   11582              :                            int caf_mode, bool no_finalization)
   11583              : {
   11584         3247 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11585              :                                 DEALLOCATE_ALLOC_COMP,
   11586              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
   11587         3247 :                                 NULL, no_finalization);
   11588              : }
   11589              : 
   11590              : tree
   11591            1 : gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
   11592              :                       tree image_index, tree stat, tree errmsg,
   11593              :                       tree errmsg_len)
   11594              : {
   11595            1 :   tree tmp, array;
   11596            1 :   gfc_se argse;
   11597            1 :   stmtblock_t block, post_block;
   11598            1 :   gfc_co_subroutines_args args;
   11599              : 
   11600            1 :   args.image_index = image_index;
   11601            1 :   args.stat = stat;
   11602            1 :   args.errmsg = errmsg;
   11603            1 :   args.errmsg_len = errmsg_len;
   11604              : 
   11605            1 :   if (rank == 0)
   11606              :     {
   11607            1 :       gfc_start_block (&block);
   11608            1 :       gfc_init_block (&post_block);
   11609            1 :       gfc_init_se (&argse, NULL);
   11610            1 :       gfc_conv_expr (&argse, expr);
   11611            1 :       gfc_add_block_to_block (&block, &argse.pre);
   11612            1 :       gfc_add_block_to_block (&post_block, &argse.post);
   11613            1 :       array = argse.expr;
   11614              :     }
   11615              :   else
   11616              :     {
   11617            0 :       gfc_init_se (&argse, NULL);
   11618            0 :       argse.want_pointer = 1;
   11619            0 :       gfc_conv_expr_descriptor (&argse, expr);
   11620            0 :       array = argse.expr;
   11621              :     }
   11622              : 
   11623            1 :   tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
   11624              :                                BCAST_ALLOC_COMP,
   11625              :                                GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
   11626              :                                &args);
   11627            1 :   return tmp;
   11628              : }
   11629              : 
   11630              : /* Recursively traverse an object of derived type, generating code to
   11631              :    deallocate allocatable components.  But do not deallocate coarrays.
   11632              :    To be used for intrinsic assignment, which may not change the allocation
   11633              :    status of coarrays.  */
   11634              : 
   11635              : tree
   11636         3695 : gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank,
   11637              :                                   bool no_finalization)
   11638              : {
   11639         3695 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11640              :                                 DEALLOCATE_ALLOC_COMP, 0, NULL,
   11641         3695 :                                 no_finalization);
   11642              : }
   11643              : 
   11644              : 
   11645              : tree
   11646            5 : gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
   11647              : {
   11648            5 :   return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
   11649              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
   11650            5 :                                 NULL);
   11651              : }
   11652              : 
   11653              : 
   11654              : /* Recursively traverse an object of derived type, generating code to
   11655              :    copy it and its allocatable components.  */
   11656              : 
   11657              : tree
   11658         5020 : gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
   11659              :                      int caf_mode)
   11660              : {
   11661         5020 :   return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
   11662         5020 :                                 caf_mode, NULL);
   11663              : }
   11664              : 
   11665              : 
   11666              : /* Recursively traverse an object of derived type, generating code to
   11667              :    copy it and its allocatable components, while suppressing any
   11668              :    finalization that might occur.  This is used in the finalization of
   11669              :    function results.  */
   11670              : 
   11671              : tree
   11672           38 : gfc_copy_alloc_comp_no_fini (gfc_symbol * der_type, tree decl, tree dest,
   11673              :                              int rank, int caf_mode)
   11674              : {
   11675           38 :   return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
   11676           38 :                                 caf_mode, NULL, true);
   11677              : }
   11678              : 
   11679              : 
   11680              : /* Recursively traverse an object of derived type, generating code to
   11681              :    copy only its allocatable components.  */
   11682              : 
   11683              : tree
   11684            0 : gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
   11685              : {
   11686            0 :   return structure_alloc_comps (der_type, decl, dest, rank,
   11687            0 :                                 COPY_ONLY_ALLOC_COMP, 0, NULL);
   11688              : }
   11689              : 
   11690              : 
   11691              : /* Recursively traverse an object of parameterized derived type, generating
   11692              :    code to allocate parameterized components.  */
   11693              : 
   11694              : tree
   11695         1245 : gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
   11696              :                        gfc_actual_arglist *param_list)
   11697              : {
   11698         1245 :   tree res;
   11699         1245 :   gfc_actual_arglist *old_param_list = pdt_param_list;
   11700         1245 :   pdt_param_list = param_list;
   11701         1245 :   res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11702              :                                ALLOCATE_PDT_COMP, 0, NULL);
   11703         1245 :   pdt_param_list = old_param_list;
   11704         1245 :   return res;
   11705              : }
   11706              : 
   11707              : /* Recursively traverse an object of parameterized derived type, generating
   11708              :    code to deallocate parameterized components.  */
   11709              : 
   11710              : tree
   11711         2030 : gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
   11712              : {
   11713              :   /* A type without parameterized components causes gimplifier problems.  */
   11714         2030 :   if (!has_parameterized_comps (der_type))
   11715              :     return NULL_TREE;
   11716              : 
   11717         1213 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11718         1213 :                                 DEALLOCATE_PDT_COMP, 0, NULL);
   11719              : }
   11720              : 
   11721              : 
   11722              : /* Recursively traverse a dummy of parameterized derived type to check the
   11723              :    values of LEN parameters.  */
   11724              : 
   11725              : tree
   11726          114 : gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
   11727              :                      gfc_actual_arglist *param_list, bool intent_out)
   11728              : {
   11729          114 :   tree res;
   11730          114 :   gfc_actual_arglist *old_param_list = pdt_param_list;
   11731          114 :   pdt_param_list = param_list;
   11732          198 :   res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11733              :                                intent_out ? CHECK_PDT_INTENT_OUT_DUMMY :
   11734              :                                             CHECK_PDT_DUMMY,
   11735              :                                0, NULL);
   11736          114 :   pdt_param_list = old_param_list;
   11737          114 :   return res;
   11738              : }
   11739              : 
   11740              : 
   11741              : /* Returns the value of LBOUND for an expression.  This could be broken out
   11742              :    from gfc_conv_intrinsic_bound but this seemed to be simpler.  This is
   11743              :    called by gfc_alloc_allocatable_for_assignment.  */
   11744              : static tree
   11745         1090 : get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
   11746              : {
   11747         1090 :   tree lbound;
   11748         1090 :   tree ubound;
   11749         1090 :   tree stride;
   11750         1090 :   tree cond, cond1, cond3, cond4;
   11751         1090 :   tree tmp;
   11752         1090 :   gfc_ref *ref;
   11753              : 
   11754         1090 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   11755              :     {
   11756          508 :       tmp = gfc_rank_cst[dim];
   11757          508 :       lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
   11758          508 :       ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
   11759          508 :       stride = gfc_conv_descriptor_stride_get (desc, tmp);
   11760          508 :       cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11761              :                                ubound, lbound);
   11762          508 :       cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11763              :                                stride, gfc_index_zero_node);
   11764          508 :       cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   11765              :                                logical_type_node, cond3, cond1);
   11766          508 :       cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   11767              :                                stride, gfc_index_zero_node);
   11768          508 :       if (assumed_size)
   11769            0 :         cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11770              :                                 tmp, build_int_cst (gfc_array_index_type,
   11771            0 :                                                     expr->rank - 1));
   11772              :       else
   11773          508 :         cond = logical_false_node;
   11774              : 
   11775          508 :       cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11776              :                                logical_type_node, cond3, cond4);
   11777          508 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11778              :                               logical_type_node, cond, cond1);
   11779              : 
   11780          508 :       return fold_build3_loc (input_location, COND_EXPR,
   11781              :                               gfc_array_index_type, cond,
   11782          508 :                               lbound, gfc_index_one_node);
   11783              :     }
   11784              : 
   11785          582 :   if (expr->expr_type == EXPR_FUNCTION)
   11786              :     {
   11787              :       /* A conversion function, so use the argument.  */
   11788            7 :       gcc_assert (expr->value.function.isym
   11789              :                   && expr->value.function.isym->conversion);
   11790            7 :       expr = expr->value.function.actual->expr;
   11791              :     }
   11792              : 
   11793          582 :   if (expr->expr_type == EXPR_VARIABLE)
   11794              :     {
   11795          582 :       tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
   11796         1508 :       for (ref = expr->ref; ref; ref = ref->next)
   11797              :         {
   11798          926 :           if (ref->type == REF_COMPONENT
   11799          295 :                 && ref->u.c.component->as
   11800          246 :                 && ref->next
   11801          246 :                 && ref->next->u.ar.type == AR_FULL)
   11802          204 :             tmp = TREE_TYPE (ref->u.c.component->backend_decl);
   11803              :         }
   11804          582 :       return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
   11805              :     }
   11806              : 
   11807            0 :   return gfc_index_one_node;
   11808              : }
   11809              : 
   11810              : 
   11811              : /* Returns true if an expression represents an lhs that can be reallocated
   11812              :    on assignment.  */
   11813              : 
   11814              : bool
   11815       646962 : gfc_is_reallocatable_lhs (gfc_expr *expr)
   11816              : {
   11817       646962 :   gfc_ref * ref;
   11818       646962 :   gfc_symbol *sym;
   11819              : 
   11820       646962 :   if (!flag_realloc_lhs)
   11821              :     return false;
   11822              : 
   11823       646462 :   if (!expr->ref)
   11824              :     return false;
   11825              : 
   11826       218724 :   sym = expr->symtree->n.sym;
   11827              : 
   11828       218724 :   if (sym->attr.associate_var && !expr->ref)
   11829              :     return false;
   11830              : 
   11831              :   /* An allocatable class variable with no reference.  */
   11832       218724 :   if (sym->ts.type == BT_CLASS
   11833         6853 :       && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
   11834         6693 :       && CLASS_DATA (sym)->attr.allocatable
   11835              :       && expr->ref
   11836         4137 :       && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
   11837          703 :            && expr->ref->next == NULL)
   11838         3507 :           || (expr->ref->type == REF_COMPONENT
   11839         3154 :               && strcmp (expr->ref->u.c.component->name, "_data") == 0
   11840         2195 :               && (expr->ref->next == NULL
   11841         2195 :                   || (expr->ref->next->type == REF_ARRAY
   11842         2195 :                       && expr->ref->next->u.ar.type == AR_FULL
   11843         1749 :                       && expr->ref->next->next == NULL)))))
   11844              :     return true;
   11845              : 
   11846              :   /* An allocatable variable.  */
   11847       216485 :   if (sym->attr.allocatable
   11848        47113 :       && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
   11849              :       && expr->ref
   11850        47113 :       && expr->ref->type == REF_ARRAY
   11851        45492 :       && expr->ref->u.ar.type == AR_FULL)
   11852              :     return true;
   11853              : 
   11854              :   /* All that can be left are allocatable components.  */
   11855       188381 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
   11856              :     return false;
   11857              : 
   11858              :   /* Find a component ref followed by an array reference.  */
   11859        95419 :   for (ref = expr->ref; ref; ref = ref->next)
   11860        67073 :     if (ref->next
   11861        38727 :           && ref->type == REF_COMPONENT
   11862        22366 :           && ref->next->type == REF_ARRAY
   11863        18409 :           && !ref->next->next)
   11864              :       break;
   11865              : 
   11866        42252 :   if (!ref)
   11867              :     return false;
   11868              : 
   11869              :   /* Return true if valid reallocatable lhs.  */
   11870        13906 :   if (ref->u.c.component->attr.allocatable
   11871         6618 :         && ref->next->u.ar.type == AR_FULL)
   11872         4968 :     return true;
   11873              : 
   11874              :   return false;
   11875              : }
   11876              : 
   11877              : 
   11878              : static tree
   11879           56 : concat_str_length (gfc_expr* expr)
   11880              : {
   11881           56 :   tree type;
   11882           56 :   tree len1;
   11883           56 :   tree len2;
   11884           56 :   gfc_se se;
   11885              : 
   11886           56 :   type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
   11887           56 :   len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   11888           56 :   if (len1 == NULL_TREE)
   11889              :     {
   11890           56 :       if (expr->value.op.op1->expr_type == EXPR_OP)
   11891           31 :         len1 = concat_str_length (expr->value.op.op1);
   11892           25 :       else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
   11893           25 :         len1 = build_int_cst (gfc_charlen_type_node,
   11894           25 :                         expr->value.op.op1->value.character.length);
   11895            0 :       else if (expr->value.op.op1->ts.u.cl->length)
   11896              :         {
   11897            0 :           gfc_init_se (&se, NULL);
   11898            0 :           gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
   11899            0 :           len1 = se.expr;
   11900              :         }
   11901              :       else
   11902              :         {
   11903              :           /* Last resort!  */
   11904            0 :           gfc_init_se (&se, NULL);
   11905            0 :           se.want_pointer = 1;
   11906            0 :           se.descriptor_only = 1;
   11907            0 :           gfc_conv_expr (&se, expr->value.op.op1);
   11908            0 :           len1 = se.string_length;
   11909              :         }
   11910              :     }
   11911              : 
   11912           56 :   type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
   11913           56 :   len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   11914           56 :   if (len2 == NULL_TREE)
   11915              :     {
   11916           31 :       if (expr->value.op.op2->expr_type == EXPR_OP)
   11917            0 :         len2 = concat_str_length (expr->value.op.op2);
   11918           31 :       else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
   11919           25 :         len2 = build_int_cst (gfc_charlen_type_node,
   11920           25 :                         expr->value.op.op2->value.character.length);
   11921            6 :       else if (expr->value.op.op2->ts.u.cl->length)
   11922              :         {
   11923            6 :           gfc_init_se (&se, NULL);
   11924            6 :           gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
   11925            6 :           len2 = se.expr;
   11926              :         }
   11927              :       else
   11928              :         {
   11929              :           /* Last resort!  */
   11930            0 :           gfc_init_se (&se, NULL);
   11931            0 :           se.want_pointer = 1;
   11932            0 :           se.descriptor_only = 1;
   11933            0 :           gfc_conv_expr (&se, expr->value.op.op2);
   11934            0 :           len2 = se.string_length;
   11935              :         }
   11936              :     }
   11937              : 
   11938           56 :   gcc_assert(len1 && len2);
   11939           56 :   len1 = fold_convert (gfc_charlen_type_node, len1);
   11940           56 :   len2 = fold_convert (gfc_charlen_type_node, len2);
   11941              : 
   11942           56 :   return fold_build2_loc (input_location, PLUS_EXPR,
   11943           56 :                           gfc_charlen_type_node, len1, len2);
   11944              : }
   11945              : 
   11946              : 
   11947              : /* Among the scalarization chain of LOOP, find the element associated with an
   11948              :    allocatable array on the lhs of an assignment and evaluate its fields
   11949              :    (bounds, offset, etc) to new variables, putting the new code in BLOCK.  This
   11950              :    function is to be called after putting the reallocation code in BLOCK and
   11951              :    before the beginning of the scalarization loop body.
   11952              : 
   11953              :    The fields to be saved are expected to hold on entry to the function
   11954              :    expressions referencing the array descriptor.  Especially the expressions
   11955              :    shouldn't be already temporary variable references as the value saved before
   11956              :    reallocation would be incorrect after reallocation.
   11957              :    At the end of the function, the expressions have been replaced with variable
   11958              :    references.  */
   11959              : 
   11960              : static void
   11961         6795 : update_reallocated_descriptor (stmtblock_t *block, gfc_loopinfo *loop)
   11962              : {
   11963        23654 :   for (gfc_ss *s = loop->ss; s != gfc_ss_terminator; s = s->loop_chain)
   11964              :     {
   11965        16859 :       if (!s->is_alloc_lhs)
   11966        10064 :         continue;
   11967              : 
   11968         6795 :       gcc_assert (s->info->type == GFC_SS_SECTION);
   11969         6795 :       gfc_array_info *info = &s->info->data.array;
   11970              : 
   11971              : #define SAVE_VALUE(value) \
   11972              :               do \
   11973              :                 { \
   11974              :                   value = gfc_evaluate_now (value, block); \
   11975              :                 } \
   11976              :               while (0)
   11977              : 
   11978         6795 :       if (save_descriptor_data (info->descriptor, info->data))
   11979         5955 :         SAVE_VALUE (info->data);
   11980         6795 :       SAVE_VALUE (info->offset);
   11981         6795 :       info->saved_offset = info->offset;
   11982        16802 :       for (int i = 0; i < s->dimen; i++)
   11983              :         {
   11984        10007 :           int dim = s->dim[i];
   11985        10007 :           SAVE_VALUE (info->start[dim]);
   11986        10007 :           SAVE_VALUE (info->end[dim]);
   11987        10007 :           SAVE_VALUE (info->stride[dim]);
   11988        10007 :           SAVE_VALUE (info->delta[dim]);
   11989              :         }
   11990              : 
   11991              : #undef SAVE_VALUE
   11992              :     }
   11993         6795 : }
   11994              : 
   11995              : 
   11996              : /* Allocate the lhs of an assignment to an allocatable array, otherwise
   11997              :    reallocate it.  */
   11998              : 
   11999              : tree
   12000         6795 : gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
   12001              :                                       gfc_expr *expr1,
   12002              :                                       gfc_expr *expr2)
   12003              : {
   12004         6795 :   stmtblock_t realloc_block;
   12005         6795 :   stmtblock_t alloc_block;
   12006         6795 :   stmtblock_t fblock;
   12007         6795 :   stmtblock_t loop_pre_block;
   12008         6795 :   gfc_ref *ref;
   12009         6795 :   gfc_ss *rss;
   12010         6795 :   gfc_ss *lss;
   12011         6795 :   gfc_array_info *linfo;
   12012         6795 :   tree realloc_expr;
   12013         6795 :   tree alloc_expr;
   12014         6795 :   tree size1;
   12015         6795 :   tree size2;
   12016         6795 :   tree elemsize1;
   12017         6795 :   tree elemsize2;
   12018         6795 :   tree array1;
   12019         6795 :   tree cond_null;
   12020         6795 :   tree cond;
   12021         6795 :   tree tmp;
   12022         6795 :   tree tmp2;
   12023         6795 :   tree lbound;
   12024         6795 :   tree ubound;
   12025         6795 :   tree desc;
   12026         6795 :   tree old_desc;
   12027         6795 :   tree desc2;
   12028         6795 :   tree offset;
   12029         6795 :   tree jump_label1;
   12030         6795 :   tree jump_label2;
   12031         6795 :   tree lbd;
   12032         6795 :   tree class_expr2 = NULL_TREE;
   12033         6795 :   int n;
   12034         6795 :   gfc_array_spec * as;
   12035         6795 :   bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
   12036         6795 :                   && gfc_caf_attr (expr1, true).codimension);
   12037         6795 :   tree token;
   12038         6795 :   gfc_se caf_se;
   12039              : 
   12040              :   /* x = f(...) with x allocatable.  In this case, expr1 is the rhs.
   12041              :      Find the lhs expression in the loop chain and set expr1 and
   12042              :      expr2 accordingly.  */
   12043         6795 :   if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
   12044              :     {
   12045          203 :       expr2 = expr1;
   12046              :       /* Find the ss for the lhs.  */
   12047          203 :       lss = loop->ss;
   12048          406 :       for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
   12049          406 :         if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
   12050              :           break;
   12051          203 :       if (lss == gfc_ss_terminator)
   12052              :         return NULL_TREE;
   12053          203 :       expr1 = lss->info->expr;
   12054              :     }
   12055              : 
   12056              :   /* Bail out if this is not a valid allocate on assignment.  */
   12057         6795 :   if (!gfc_is_reallocatable_lhs (expr1)
   12058         6795 :         || (expr2 && !expr2->rank))
   12059              :     return NULL_TREE;
   12060              : 
   12061              :   /* Find the ss for the lhs.  */
   12062         6795 :   lss = loop->ss;
   12063        16859 :   for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
   12064        16859 :     if (lss->info->expr == expr1)
   12065              :       break;
   12066              : 
   12067         6795 :   if (lss == gfc_ss_terminator)
   12068              :     return NULL_TREE;
   12069              : 
   12070         6795 :   linfo = &lss->info->data.array;
   12071              : 
   12072              :   /* Find an ss for the rhs. For operator expressions, we see the
   12073              :      ss's for the operands. Any one of these will do.  */
   12074         6795 :   rss = loop->ss;
   12075         7400 :   for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
   12076         7400 :     if (rss->info->expr != expr1 && rss != loop->temp_ss)
   12077              :       break;
   12078              : 
   12079         6795 :   if (expr2 && rss == gfc_ss_terminator)
   12080              :     return NULL_TREE;
   12081              : 
   12082              :   /* Ensure that the string length from the current scope is used.  */
   12083         6795 :   if (expr2->ts.type == BT_CHARACTER
   12084         1013 :       && expr2->expr_type == EXPR_FUNCTION
   12085          130 :       && !expr2->value.function.isym)
   12086           21 :     expr2->ts.u.cl->backend_decl = rss->info->string_length;
   12087              : 
   12088              :   /* Since the lhs is allocatable, this must be a descriptor type.
   12089              :      Get the data and array size.  */
   12090         6795 :   desc = linfo->descriptor;
   12091         6795 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
   12092         6795 :   array1 = gfc_conv_descriptor_data_get (desc);
   12093              : 
   12094              :   /* If the data is null, set the descriptor bounds and offset.  This suppresses
   12095              :      the maybe used uninitialized warning.  Note that the always false variable
   12096              :      prevents this block from ever being executed, and makes sure that the
   12097              :      optimizers are able to remove it.  Component references are not subject to
   12098              :      the warnings, so we don't uselessly complicate the generated code for them.
   12099              :      */
   12100        12100 :   for (ref = expr1->ref; ref; ref = ref->next)
   12101         7032 :     if (ref->type == REF_COMPONENT)
   12102              :       break;
   12103              : 
   12104         6795 :   if (!ref)
   12105              :     {
   12106         5068 :       stmtblock_t unalloc_init_block;
   12107         5068 :       gfc_init_block (&unalloc_init_block);
   12108         5068 :       tree guard = gfc_create_var (logical_type_node, "unallocated_init_guard");
   12109         5068 :       gfc_add_modify (&unalloc_init_block, guard, logical_false_node);
   12110              : 
   12111         5068 :       gfc_start_block (&loop_pre_block);
   12112        18059 :       for (n = 0; n < expr1->rank; n++)
   12113              :         {
   12114         7923 :           gfc_conv_descriptor_lbound_set (&loop_pre_block, desc,
   12115              :                                           gfc_rank_cst[n],
   12116              :                                           gfc_index_one_node);
   12117         7923 :           gfc_conv_descriptor_ubound_set (&loop_pre_block, desc,
   12118              :                                           gfc_rank_cst[n],
   12119              :                                           gfc_index_zero_node);
   12120         7923 :           gfc_conv_descriptor_stride_set (&loop_pre_block, desc,
   12121              :                                           gfc_rank_cst[n],
   12122              :                                           gfc_index_zero_node);
   12123              :         }
   12124              : 
   12125         5068 :       gfc_conv_descriptor_offset_set (&loop_pre_block, desc,
   12126              :                                       gfc_index_zero_node);
   12127              : 
   12128         5068 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
   12129              :                              logical_type_node, array1,
   12130         5068 :                              build_int_cst (TREE_TYPE (array1), 0));
   12131         5068 :       tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
   12132              :                              logical_type_node, tmp, guard);
   12133         5068 :       tmp = build3_v (COND_EXPR, tmp,
   12134              :                       gfc_finish_block (&loop_pre_block),
   12135              :                       build_empty_stmt (input_location));
   12136         5068 :       gfc_prepend_expr_to_block (&loop->pre, tmp);
   12137         5068 :       gfc_prepend_expr_to_block (&loop->pre,
   12138              :                                  gfc_finish_block (&unalloc_init_block));
   12139              :     }
   12140              : 
   12141         6795 :   gfc_start_block (&fblock);
   12142              : 
   12143         6795 :   if (expr2)
   12144         6795 :     desc2 = rss->info->data.array.descriptor;
   12145              :   else
   12146              :     desc2 = NULL_TREE;
   12147              : 
   12148              :   /* Get the old lhs element size for deferred character and class expr1.  */
   12149         6795 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12150              :     {
   12151          687 :       if (expr1->ts.u.cl->backend_decl
   12152          687 :           && VAR_P (expr1->ts.u.cl->backend_decl))
   12153              :         elemsize1 = expr1->ts.u.cl->backend_decl;
   12154              :       else
   12155           70 :         elemsize1 = lss->info->string_length;
   12156          687 :       tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
   12157         1374 :       elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
   12158          687 :                                    TREE_TYPE (elemsize1), elemsize1,
   12159          687 :                                    fold_convert (TREE_TYPE (elemsize1), unit_size));
   12160              : 
   12161          687 :     }
   12162         6108 :   else if (expr1->ts.type == BT_CLASS)
   12163              :     {
   12164              :       /* Unfortunately, the lhs vptr is set too early in many cases.
   12165              :          Play it safe by using the descriptor element length.  */
   12166          669 :       tmp = gfc_conv_descriptor_elem_len_get (desc);
   12167          669 :       elemsize1 = fold_convert (gfc_array_index_type, tmp);
   12168              :     }
   12169              :   else
   12170              :     elemsize1 = NULL_TREE;
   12171         1356 :   if (elemsize1 != NULL_TREE)
   12172         1356 :     elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
   12173              : 
   12174              :   /* Get the new lhs size in bytes.  */
   12175         6795 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12176              :     {
   12177          687 :       if (expr2->ts.deferred)
   12178              :         {
   12179          183 :           if (expr2->ts.u.cl->backend_decl
   12180          183 :               && VAR_P (expr2->ts.u.cl->backend_decl))
   12181              :             tmp = expr2->ts.u.cl->backend_decl;
   12182              :           else
   12183            0 :             tmp = rss->info->string_length;
   12184              :         }
   12185              :       else
   12186              :         {
   12187          504 :           tmp = expr2->ts.u.cl->backend_decl;
   12188          504 :           if (!tmp && expr2->expr_type == EXPR_OP
   12189           25 :               && expr2->value.op.op == INTRINSIC_CONCAT)
   12190              :             {
   12191           25 :               tmp = concat_str_length (expr2);
   12192           25 :               expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
   12193              :             }
   12194           12 :           else if (!tmp && expr2->ts.u.cl->length)
   12195              :             {
   12196           12 :               gfc_se tmpse;
   12197           12 :               gfc_init_se (&tmpse, NULL);
   12198           12 :               gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
   12199              :                                   gfc_charlen_type_node);
   12200           12 :               tmp = tmpse.expr;
   12201           12 :               expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
   12202              :             }
   12203          504 :           tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
   12204              :         }
   12205              : 
   12206          687 :       if (expr1->ts.u.cl->backend_decl
   12207          687 :           && VAR_P (expr1->ts.u.cl->backend_decl))
   12208          617 :         gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
   12209              :       else
   12210           70 :         gfc_add_modify (&fblock, lss->info->string_length, tmp);
   12211              : 
   12212          687 :       if (expr1->ts.kind > 1)
   12213           12 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
   12214            6 :                                TREE_TYPE (tmp),
   12215            6 :                                tmp, build_int_cst (TREE_TYPE (tmp),
   12216            6 :                                                    expr1->ts.kind));
   12217              :     }
   12218         6108 :   else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
   12219              :     {
   12220          277 :       tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
   12221          277 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12222              :                              fold_convert (gfc_array_index_type, tmp),
   12223          277 :                              expr1->ts.u.cl->backend_decl);
   12224              :     }
   12225         5831 :   else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
   12226          164 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12227         5667 :   else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
   12228              :     {
   12229          298 :       tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
   12230          298 :       if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
   12231           54 :         tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
   12232              : 
   12233           61 :       if (tmp != NULL_TREE)
   12234          291 :         tmp = gfc_class_vtab_size_get (tmp);
   12235              :       else
   12236            7 :         tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
   12237              :     }
   12238              :   else
   12239         5369 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12240         6795 :   elemsize2 = fold_convert (gfc_array_index_type, tmp);
   12241         6795 :   elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
   12242              : 
   12243              :   /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
   12244              :      deallocated if expr is an array of different shape or any of the
   12245              :      corresponding length type parameter values of variable and expr
   12246              :      differ."  This assures F95 compatibility.  */
   12247         6795 :   jump_label1 = gfc_build_label_decl (NULL_TREE);
   12248         6795 :   jump_label2 = gfc_build_label_decl (NULL_TREE);
   12249              : 
   12250              :   /* Allocate if data is NULL.  */
   12251         6795 :   cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   12252         6795 :                          array1, build_int_cst (TREE_TYPE (array1), 0));
   12253         6795 :   cond_null= gfc_evaluate_now (cond_null, &fblock);
   12254              : 
   12255         6795 :   tmp = build3_v (COND_EXPR, cond_null,
   12256              :                   build1_v (GOTO_EXPR, jump_label1),
   12257              :                   build_empty_stmt (input_location));
   12258         6795 :   gfc_add_expr_to_block (&fblock, tmp);
   12259              : 
   12260              :   /* Get arrayspec if expr is a full array.  */
   12261         6795 :   if (expr2 && expr2->expr_type == EXPR_FUNCTION
   12262         2820 :         && expr2->value.function.isym
   12263         2301 :         && expr2->value.function.isym->conversion)
   12264              :     {
   12265              :       /* For conversion functions, take the arg.  */
   12266          245 :       gfc_expr *arg = expr2->value.function.actual->expr;
   12267          245 :       as = gfc_get_full_arrayspec_from_expr (arg);
   12268          245 :     }
   12269              :   else if (expr2)
   12270         6550 :     as = gfc_get_full_arrayspec_from_expr (expr2);
   12271              :   else
   12272              :     as = NULL;
   12273              : 
   12274              :   /* If the lhs shape is not the same as the rhs jump to setting the
   12275              :      bounds and doing the reallocation.......  */
   12276        16802 :   for (n = 0; n < expr1->rank; n++)
   12277              :     {
   12278              :       /* Check the shape.  */
   12279        10007 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12280        10007 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
   12281        10007 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12282              :                              gfc_array_index_type,
   12283              :                              loop->to[n], loop->from[n]);
   12284        10007 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12285              :                              gfc_array_index_type,
   12286              :                              tmp, lbound);
   12287        10007 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12288              :                              gfc_array_index_type,
   12289              :                              tmp, ubound);
   12290        10007 :       cond = fold_build2_loc (input_location, NE_EXPR,
   12291              :                               logical_type_node,
   12292              :                               tmp, gfc_index_zero_node);
   12293        10007 :       tmp = build3_v (COND_EXPR, cond,
   12294              :                       build1_v (GOTO_EXPR, jump_label1),
   12295              :                       build_empty_stmt (input_location));
   12296        10007 :       gfc_add_expr_to_block (&fblock, tmp);
   12297              :     }
   12298              : 
   12299              :   /* ...else if the element lengths are not the same also go to
   12300              :      setting the bounds and doing the reallocation.... */
   12301         6795 :   if (elemsize1 != NULL_TREE)
   12302              :     {
   12303         1356 :       cond = fold_build2_loc (input_location, NE_EXPR,
   12304              :                               logical_type_node,
   12305              :                               elemsize1, elemsize2);
   12306         1356 :       tmp = build3_v (COND_EXPR, cond,
   12307              :                       build1_v (GOTO_EXPR, jump_label1),
   12308              :                       build_empty_stmt (input_location));
   12309         1356 :       gfc_add_expr_to_block (&fblock, tmp);
   12310              :     }
   12311              : 
   12312              :   /* ....else jump past the (re)alloc code.  */
   12313         6795 :   tmp = build1_v (GOTO_EXPR, jump_label2);
   12314         6795 :   gfc_add_expr_to_block (&fblock, tmp);
   12315              : 
   12316              :   /* Add the label to start automatic (re)allocation.  */
   12317         6795 :   tmp = build1_v (LABEL_EXPR, jump_label1);
   12318         6795 :   gfc_add_expr_to_block (&fblock, tmp);
   12319              : 
   12320              :   /* Get the rhs size and fix it.  */
   12321         6795 :   size2 = gfc_index_one_node;
   12322        16802 :   for (n = 0; n < expr2->rank; n++)
   12323              :     {
   12324        10007 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12325              :                              gfc_array_index_type,
   12326              :                              loop->to[n], loop->from[n]);
   12327        10007 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12328              :                              gfc_array_index_type,
   12329              :                              tmp, gfc_index_one_node);
   12330        10007 :       size2 = fold_build2_loc (input_location, MULT_EXPR,
   12331              :                                gfc_array_index_type,
   12332              :                                tmp, size2);
   12333              :     }
   12334         6795 :   size2 = gfc_evaluate_now (size2, &fblock);
   12335              : 
   12336              :   /* Deallocation of allocatable components will have to occur on
   12337              :      reallocation.  Fix the old descriptor now.  */
   12338         6795 :   if ((expr1->ts.type == BT_DERIVED)
   12339          442 :         && expr1->ts.u.derived->attr.alloc_comp)
   12340          200 :     old_desc = gfc_evaluate_now (desc, &fblock);
   12341              :   else
   12342              :     old_desc = NULL_TREE;
   12343              : 
   12344              :   /* Now modify the lhs descriptor and the associated scalarizer
   12345              :      variables. F2003 7.4.1.3: "If variable is or becomes an
   12346              :      unallocated allocatable variable, then it is allocated with each
   12347              :      deferred type parameter equal to the corresponding type parameters
   12348              :      of expr , with the shape of expr , and with each lower bound equal
   12349              :      to the corresponding element of LBOUND(expr)."
   12350              :      Reuse size1 to keep a dimension-by-dimension track of the
   12351              :      stride of the new array.  */
   12352         6795 :   size1 = gfc_index_one_node;
   12353         6795 :   offset = gfc_index_zero_node;
   12354              : 
   12355        16802 :   for (n = 0; n < expr2->rank; n++)
   12356              :     {
   12357        10007 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12358              :                              gfc_array_index_type,
   12359              :                              loop->to[n], loop->from[n]);
   12360        10007 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12361              :                              gfc_array_index_type,
   12362              :                              tmp, gfc_index_one_node);
   12363              : 
   12364        10007 :       lbound = gfc_index_one_node;
   12365        10007 :       ubound = tmp;
   12366              : 
   12367        10007 :       if (as)
   12368              :         {
   12369         2180 :           lbd = get_std_lbound (expr2, desc2, n,
   12370         1090 :                                 as->type == AS_ASSUMED_SIZE);
   12371         1090 :           ubound = fold_build2_loc (input_location,
   12372              :                                     MINUS_EXPR,
   12373              :                                     gfc_array_index_type,
   12374              :                                     ubound, lbound);
   12375         1090 :           ubound = fold_build2_loc (input_location,
   12376              :                                     PLUS_EXPR,
   12377              :                                     gfc_array_index_type,
   12378              :                                     ubound, lbd);
   12379         1090 :           lbound = lbd;
   12380              :         }
   12381              : 
   12382        10007 :       gfc_conv_descriptor_lbound_set (&fblock, desc,
   12383              :                                       gfc_rank_cst[n],
   12384              :                                       lbound);
   12385        10007 :       gfc_conv_descriptor_ubound_set (&fblock, desc,
   12386              :                                       gfc_rank_cst[n],
   12387              :                                       ubound);
   12388        10007 :       gfc_conv_descriptor_stride_set (&fblock, desc,
   12389              :                                       gfc_rank_cst[n],
   12390              :                                       size1);
   12391        10007 :       lbound = gfc_conv_descriptor_lbound_get (desc,
   12392              :                                                gfc_rank_cst[n]);
   12393        10007 :       tmp2 = fold_build2_loc (input_location, MULT_EXPR,
   12394              :                               gfc_array_index_type,
   12395              :                               lbound, size1);
   12396        10007 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
   12397              :                                 gfc_array_index_type,
   12398              :                                 offset, tmp2);
   12399        10007 :       size1 = fold_build2_loc (input_location, MULT_EXPR,
   12400              :                                gfc_array_index_type,
   12401              :                                tmp, size1);
   12402              :     }
   12403              : 
   12404              :   /* Set the lhs descriptor and scalarizer offsets.  For rank > 1,
   12405              :      the array offset is saved and the info.offset is used for a
   12406              :      running offset.  Use the saved_offset instead.  */
   12407         6795 :   gfc_conv_descriptor_offset_set (&fblock, desc, offset);
   12408              : 
   12409              :   /* Take into account _len of unlimited polymorphic entities, so that span
   12410              :      for array descriptors and allocation sizes are computed correctly.  */
   12411         6795 :   if (UNLIMITED_POLY (expr2))
   12412              :     {
   12413          110 :       tree len = gfc_class_len_get (TREE_OPERAND (desc2, 0));
   12414          110 :       len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12415              :                              fold_convert (size_type_node, len),
   12416              :                              size_one_node);
   12417          110 :       elemsize2 = fold_build2_loc (input_location, MULT_EXPR,
   12418              :                                    gfc_array_index_type, elemsize2,
   12419              :                                    fold_convert (gfc_array_index_type, len));
   12420              :     }
   12421              : 
   12422         6795 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   12423         6795 :     gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
   12424              : 
   12425         6795 :   size2 = fold_build2_loc (input_location, MULT_EXPR,
   12426              :                            gfc_array_index_type,
   12427              :                            elemsize2, size2);
   12428         6795 :   size2 = fold_convert (size_type_node, size2);
   12429         6795 :   size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12430              :                            size2, size_one_node);
   12431         6795 :   size2 = gfc_evaluate_now (size2, &fblock);
   12432              : 
   12433              :   /* For deferred character length, the 'size' field of the dtype might
   12434              :      have changed so set the dtype.  */
   12435         6795 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
   12436         6795 :       && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12437              :     {
   12438          687 :       tree type;
   12439          687 :       if (expr2->ts.u.cl->backend_decl)
   12440          687 :         type = gfc_typenode_for_spec (&expr2->ts);
   12441              :       else
   12442            0 :         type = gfc_typenode_for_spec (&expr1->ts);
   12443              : 
   12444          687 :       gfc_conv_descriptor_dtype_set (&fblock, desc,
   12445              :                                      gfc_get_dtype_rank_type (expr1->rank,
   12446              :                                                               type));
   12447              :     }
   12448         6108 :   else if (expr1->ts.type == BT_CLASS)
   12449              :     {
   12450          669 :       tree type;
   12451              : 
   12452          669 :       if (expr2->ts.type != BT_CLASS)
   12453          371 :         type = gfc_typenode_for_spec (&expr2->ts);
   12454              :       else
   12455          298 :         type = gfc_get_character_type_len (1, elemsize2);
   12456              : 
   12457          669 :       gfc_conv_descriptor_dtype_set (&fblock, desc,
   12458              :                                      gfc_get_dtype_rank_type (expr2->rank,
   12459              :                                                               type));
   12460              : 
   12461              :       /* Set the _len field as well...  */
   12462          669 :       if (UNLIMITED_POLY (expr1))
   12463              :         {
   12464          274 :           tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
   12465          274 :           if (expr2->ts.type == BT_CHARACTER)
   12466           49 :             gfc_add_modify (&fblock, tmp,
   12467           49 :                             fold_convert (TREE_TYPE (tmp),
   12468              :                                           TYPE_SIZE_UNIT (type)));
   12469          225 :           else if (UNLIMITED_POLY (expr2))
   12470          110 :             gfc_add_modify (&fblock, tmp,
   12471          110 :                             gfc_class_len_get (TREE_OPERAND (desc2, 0)));
   12472              :           else
   12473          115 :             gfc_add_modify (&fblock, tmp,
   12474          115 :                             build_int_cst (TREE_TYPE (tmp), 0));
   12475              :         }
   12476              :       /* ...and the vptr.  */
   12477          669 :       tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
   12478          669 :       if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
   12479          291 :           && TREE_CODE (desc2) == COMPONENT_REF)
   12480              :         {
   12481          237 :           tmp2 = gfc_get_class_from_expr (desc2);
   12482          237 :           tmp2 = gfc_class_vptr_get (tmp2);
   12483              :         }
   12484          432 :       else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
   12485           54 :         tmp2 = gfc_class_vptr_get (class_expr2);
   12486              :       else
   12487              :         {
   12488          378 :           tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
   12489          378 :           tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
   12490              :         }
   12491              : 
   12492          669 :       gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
   12493              :     }
   12494         5439 :   else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   12495           39 :     gfc_conv_descriptor_dtype_set (&fblock, desc,
   12496           39 :                                    gfc_get_dtype (TREE_TYPE (desc)));
   12497              : 
   12498              :   /* Realloc expression.  Note that the scalarizer uses desc.data
   12499              :      in the array reference - (*desc.data)[<element>].  */
   12500         6795 :   gfc_init_block (&realloc_block);
   12501         6795 :   gfc_init_se (&caf_se, NULL);
   12502              : 
   12503         6795 :   if (coarray)
   12504              :     {
   12505           39 :       token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
   12506           39 :       if (token == NULL_TREE)
   12507              :         {
   12508            9 :           tmp = gfc_get_tree_for_caf_expr (expr1);
   12509            9 :           if (POINTER_TYPE_P (TREE_TYPE (tmp)))
   12510            6 :             tmp = build_fold_indirect_ref (tmp);
   12511            9 :           gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
   12512              :                                     expr1);
   12513            9 :           token = gfc_build_addr_expr (NULL_TREE, token);
   12514              :         }
   12515              : 
   12516           39 :       gfc_add_block_to_block (&realloc_block, &caf_se.pre);
   12517              :     }
   12518         6795 :   if ((expr1->ts.type == BT_DERIVED)
   12519          442 :         && expr1->ts.u.derived->attr.alloc_comp)
   12520              :     {
   12521          200 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
   12522              :                                               expr1->rank, true);
   12523          200 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12524              :     }
   12525              : 
   12526         6795 :   if (!coarray)
   12527              :     {
   12528         6756 :       tmp = build_call_expr_loc (input_location,
   12529              :                                  builtin_decl_explicit (BUILT_IN_REALLOC), 2,
   12530              :                                  fold_convert (pvoid_type_node, array1),
   12531              :                                  size2);
   12532         6756 :       if (flag_openmp_allocators)
   12533              :         {
   12534            2 :           tree cond, omp_tmp;
   12535            2 :           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
   12536              :                                   gfc_conv_descriptor_version_get (desc),
   12537              :                                   integer_one_node);
   12538            2 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
   12539            2 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
   12540              :                                  fold_convert (pvoid_type_node, array1), size2,
   12541              :                                  build_zero_cst (ptr_type_node),
   12542              :                                  build_zero_cst (ptr_type_node));
   12543            2 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
   12544              :                             omp_tmp, tmp);
   12545              :         }
   12546              : 
   12547         6756 :       gfc_conv_descriptor_data_set (&realloc_block, desc, tmp);
   12548              :     }
   12549              :   else
   12550              :     {
   12551           39 :       tmp = build_call_expr_loc (input_location,
   12552              :                                  gfor_fndecl_caf_deregister, 5, token,
   12553              :                                  build_int_cst (integer_type_node,
   12554              :                                                GFC_CAF_COARRAY_DEALLOCATE_ONLY),
   12555              :                                  null_pointer_node, null_pointer_node,
   12556              :                                  integer_zero_node);
   12557           39 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12558           39 :       tmp = build_call_expr_loc (input_location,
   12559              :                                  gfor_fndecl_caf_register,
   12560              :                                  7, size2,
   12561              :                                  build_int_cst (integer_type_node,
   12562              :                                            GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
   12563              :                                  token, gfc_build_addr_expr (NULL_TREE, desc),
   12564              :                                  null_pointer_node, null_pointer_node,
   12565              :                                  integer_zero_node);
   12566           39 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12567              :     }
   12568              : 
   12569         6795 :   if ((expr1->ts.type == BT_DERIVED)
   12570          442 :         && expr1->ts.u.derived->attr.alloc_comp)
   12571              :     {
   12572          200 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
   12573              :                                     expr1->rank);
   12574          200 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12575              :     }
   12576              : 
   12577         6795 :   gfc_add_block_to_block (&realloc_block, &caf_se.post);
   12578         6795 :   realloc_expr = gfc_finish_block (&realloc_block);
   12579              : 
   12580              :   /* Malloc expression.  */
   12581         6795 :   gfc_init_block (&alloc_block);
   12582         6795 :   if (!coarray)
   12583              :     {
   12584         6756 :       tmp = build_call_expr_loc (input_location,
   12585              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   12586              :                                  1, size2);
   12587         6756 :       gfc_conv_descriptor_data_set (&alloc_block,
   12588              :                                     desc, tmp);
   12589              :     }
   12590              :   else
   12591              :     {
   12592           39 :       tmp = build_call_expr_loc (input_location,
   12593              :                                  gfor_fndecl_caf_register,
   12594              :                                  7, size2,
   12595              :                                  build_int_cst (integer_type_node,
   12596              :                                                 GFC_CAF_COARRAY_ALLOC),
   12597              :                                  token, gfc_build_addr_expr (NULL_TREE, desc),
   12598              :                                  null_pointer_node, null_pointer_node,
   12599              :                                  integer_zero_node);
   12600           39 :       gfc_add_expr_to_block (&alloc_block, tmp);
   12601              :     }
   12602              : 
   12603              : 
   12604              :   /* We already set the dtype in the case of deferred character
   12605              :      length arrays and class lvalues.  */
   12606         6795 :   if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
   12607         6795 :         && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12608         6108 :             || coarray))
   12609        12864 :       && expr1->ts.type != BT_CLASS)
   12610         5400 :     gfc_conv_descriptor_dtype_set (&alloc_block, desc,
   12611         5400 :                                    gfc_get_dtype (TREE_TYPE (desc)));
   12612              : 
   12613         6795 :   if ((expr1->ts.type == BT_DERIVED)
   12614          442 :         && expr1->ts.u.derived->attr.alloc_comp)
   12615              :     {
   12616          200 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
   12617              :                                     expr1->rank);
   12618          200 :       gfc_add_expr_to_block (&alloc_block, tmp);
   12619              :     }
   12620         6795 :   alloc_expr = gfc_finish_block (&alloc_block);
   12621              : 
   12622              :   /* Malloc if not allocated; realloc otherwise.  */
   12623         6795 :   tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
   12624         6795 :   gfc_add_expr_to_block (&fblock, tmp);
   12625              : 
   12626              :   /* Add the label for same shape lhs and rhs.  */
   12627         6795 :   tmp = build1_v (LABEL_EXPR, jump_label2);
   12628         6795 :   gfc_add_expr_to_block (&fblock, tmp);
   12629              : 
   12630         6795 :   tree realloc_code = gfc_finish_block (&fblock);
   12631              : 
   12632         6795 :   stmtblock_t result_block;
   12633         6795 :   gfc_init_block (&result_block);
   12634         6795 :   gfc_add_expr_to_block (&result_block, realloc_code);
   12635         6795 :   update_reallocated_descriptor (&result_block, loop);
   12636              : 
   12637         6795 :   return gfc_finish_block (&result_block);
   12638              : }
   12639              : 
   12640              : 
   12641              : /* Initialize class descriptor's TKR information.  */
   12642              : 
   12643              : void
   12644         3169 : gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
   12645              : {
   12646         3169 :   tree type, etype;
   12647         3169 :   tree descriptor;
   12648         3169 :   stmtblock_t init;
   12649         3169 :   int rank;
   12650              : 
   12651              :   /* Make sure the frontend gets these right.  */
   12652         3169 :   gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   12653              :               && (CLASS_DATA (sym)->attr.class_pointer
   12654              :                   || CLASS_DATA (sym)->attr.allocatable));
   12655              : 
   12656         3169 :   gcc_assert (VAR_P (sym->backend_decl)
   12657              :               || TREE_CODE (sym->backend_decl) == PARM_DECL);
   12658              : 
   12659         3169 :   if (sym->attr.dummy)
   12660         1539 :     return;
   12661              : 
   12662         3169 :   descriptor = gfc_class_data_get (sym->backend_decl);
   12663         3169 :   type = TREE_TYPE (descriptor);
   12664              : 
   12665         3169 :   if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
   12666              :     return;
   12667              : 
   12668         1630 :   location_t loc = input_location;
   12669         1630 :   input_location = gfc_get_location (&sym->declared_at);
   12670         1630 :   gfc_init_block (&init);
   12671              : 
   12672         1630 :   rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
   12673         1630 :   gcc_assert (rank>=0);
   12674         1630 :   etype = gfc_get_element_type (type);
   12675         1630 :   gfc_conv_descriptor_dtype_set (&init, descriptor,
   12676              :                                  gfc_get_dtype_rank_type (rank, etype));
   12677              : 
   12678         1630 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12679         1630 :   input_location = loc;
   12680              : }
   12681              : 
   12682              : 
   12683              : /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
   12684              :    Do likewise, recursively if necessary, with the allocatable components of
   12685              :    derived types.  This function is also called for assumed-rank arrays, which
   12686              :    are always dummy arguments.  */
   12687              : 
   12688              : void
   12689        18776 : gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
   12690              : {
   12691        18776 :   tree type;
   12692        18776 :   tree tmp;
   12693        18776 :   tree descriptor;
   12694        18776 :   stmtblock_t init;
   12695        18776 :   stmtblock_t cleanup;
   12696        18776 :   int rank;
   12697        18776 :   bool sym_has_alloc_comp, has_finalizer;
   12698              : 
   12699        37552 :   sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
   12700        11187 :                         || sym->ts.type == BT_CLASS)
   12701        18776 :                           && sym->ts.u.derived->attr.alloc_comp;
   12702        18776 :   has_finalizer = gfc_may_be_finalized (sym->ts);
   12703              : 
   12704              :   /* Make sure the frontend gets these right.  */
   12705        18776 :   gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
   12706              :               || has_finalizer
   12707              :               || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
   12708              : 
   12709        18776 :   location_t loc = input_location;
   12710        18776 :   input_location = gfc_get_location (&sym->declared_at);
   12711        18776 :   gfc_init_block (&init);
   12712              : 
   12713        18776 :   gcc_assert (VAR_P (sym->backend_decl)
   12714              :               || TREE_CODE (sym->backend_decl) == PARM_DECL);
   12715              : 
   12716        18776 :   if (sym->ts.type == BT_CHARACTER
   12717         1420 :       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
   12718              :     {
   12719          836 :       if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
   12720              :         {
   12721          625 :           tree len_expr = sym->ts.u.cl->backend_decl;
   12722          625 :           tree init_val = build_zero_cst (TREE_TYPE (len_expr));
   12723          625 :           if (VAR_P (len_expr)
   12724          625 :               && sym->attr.save
   12725          680 :               && !DECL_INITIAL (len_expr))
   12726           55 :             DECL_INITIAL (len_expr) = init_val;
   12727              :           else
   12728          570 :             gfc_add_modify (&init, len_expr, init_val);
   12729              :         }
   12730          836 :       gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
   12731          836 :       gfc_trans_vla_type_sizes (sym, &init);
   12732              : 
   12733              :       /* Presence check of optional deferred-length character dummy.  */
   12734          836 :       if (sym->ts.deferred && sym->attr.dummy && sym->attr.optional)
   12735              :         {
   12736           43 :           tmp = gfc_finish_block (&init);
   12737           43 :           tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
   12738              :                           tmp, build_empty_stmt (input_location));
   12739           43 :           gfc_add_expr_to_block (&init, tmp);
   12740              :         }
   12741              :     }
   12742              : 
   12743              :   /* Dummy, use associated and result variables don't need anything special.  */
   12744        18776 :   if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
   12745              :     {
   12746          972 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12747          972 :       input_location = loc;
   12748         1248 :       return;
   12749              :     }
   12750              : 
   12751        17804 :   descriptor = sym->backend_decl;
   12752              : 
   12753              :   /* Although static, derived types with default initializers and
   12754              :      allocatable components must not be nulled wholesale; instead they
   12755              :      are treated component by component.  */
   12756        17804 :   if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
   12757              :     {
   12758              :       /* SAVEd variables are not freed on exit.  */
   12759          276 :       gfc_trans_static_array_pointer (sym);
   12760              : 
   12761          276 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12762          276 :       input_location = loc;
   12763          276 :       return;
   12764              :     }
   12765              : 
   12766              :   /* Get the descriptor type.  */
   12767        17528 :   type = TREE_TYPE (sym->backend_decl);
   12768              : 
   12769        17528 :   if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
   12770         5872 :       && !(sym->attr.pointer || sym->attr.allocatable))
   12771              :     {
   12772         3024 :       if (!sym->attr.save
   12773         2615 :           && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
   12774              :         {
   12775         2615 :           if (sym->value == NULL
   12776         2615 :               || !gfc_has_default_initializer (sym->ts.u.derived))
   12777              :             {
   12778         2147 :               rank = sym->as ? sym->as->rank : 0;
   12779         2147 :               tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
   12780              :                                             descriptor, rank);
   12781         2147 :               gfc_add_expr_to_block (&init, tmp);
   12782              :             }
   12783              :           else
   12784          468 :             gfc_init_default_dt (sym, &init, false);
   12785              :         }
   12786              :     }
   12787        14504 :   else if (!GFC_DESCRIPTOR_TYPE_P (type))
   12788              :     {
   12789              :       /* If the backend_decl is not a descriptor, we must have a pointer
   12790              :          to one.  */
   12791         2254 :       descriptor = build_fold_indirect_ref_loc (input_location,
   12792              :                                                 sym->backend_decl);
   12793         2254 :       type = TREE_TYPE (descriptor);
   12794              :     }
   12795              : 
   12796              :   /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
   12797              :      pointers when -fcheck=pointer is specified.  */
   12798        17528 :   if (GFC_DESCRIPTOR_TYPE_P (type)
   12799        17528 :       && (sym->attr.allocatable || sym->attr.pointer))
   12800              :     {
   12801        12250 :       if (flag_coarray == GFC_FCOARRAY_LIB
   12802          445 :           && sym->attr.codimension
   12803          271 :           && !sym->attr.save)
   12804              :         {
   12805              :           /* Declare the variable static so its array descriptor stays present
   12806              :              after leaving the scope.  It may still be accessed through another
   12807              :              image.  This may happen, for example, with the caf_mpi
   12808              :              implementation.  */
   12809          271 :           TREE_STATIC (descriptor) = 1;
   12810              :         }
   12811        12250 :       gfc_init_descriptor_variable (&init, sym, descriptor);
   12812              :     }
   12813              : 
   12814        17528 :   input_location = loc;
   12815        17528 :   gfc_init_block (&cleanup);
   12816              : 
   12817              :   /* Allocatable arrays need to be freed when they go out of scope.
   12818              :      The allocatable components of pointers must not be touched.  */
   12819        17528 :   if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
   12820          628 :       && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
   12821          333 :       && !sym->ns->proc_name->attr.is_main_program)
   12822              :     {
   12823          294 :       gfc_expr *e;
   12824          294 :       sym->attr.referenced = 1;
   12825          294 :       e = gfc_lval_expr_from_sym (sym);
   12826          294 :       gfc_add_finalizer_call (&cleanup, e);
   12827          294 :       gfc_free_expr (e);
   12828          294 :     }
   12829        17234 :   else if ((!sym->attr.allocatable || !has_finalizer)
   12830        17110 :       && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
   12831         5291 :       && !sym->attr.pointer && !sym->attr.save
   12832         2602 :       && !(sym->attr.artificial && sym->name[0] == '_')
   12833         2547 :       && !sym->ns->proc_name->attr.is_main_program)
   12834              :     {
   12835          700 :       int rank;
   12836          700 :       rank = sym->as ? sym->as->rank : 0;
   12837          700 :       tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank,
   12838          700 :                                        (sym->attr.codimension
   12839            3 :                                         && flag_coarray == GFC_FCOARRAY_LIB)
   12840              :                                        ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY
   12841              :                                        : 0);
   12842          700 :       gfc_add_expr_to_block (&cleanup, tmp);
   12843              :     }
   12844              : 
   12845        17528 :   if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
   12846         8927 :       && !sym->attr.save && !sym->attr.result
   12847         8920 :       && !sym->ns->proc_name->attr.is_main_program)
   12848              :     {
   12849         4578 :       gfc_expr *e;
   12850         4578 :       e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
   12851         9156 :       tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
   12852              :                                         NULL_TREE, NULL_TREE, true, e,
   12853         4578 :                                         sym->attr.codimension
   12854              :                                         ? GFC_CAF_COARRAY_DEREGISTER
   12855              :                                         : GFC_CAF_COARRAY_NOCOARRAY,
   12856              :                                         NULL_TREE, gfc_finish_block (&cleanup));
   12857         4578 :       if (e)
   12858           45 :         gfc_free_expr (e);
   12859         4578 :       gfc_init_block (&cleanup);
   12860         4578 :       gfc_add_expr_to_block (&cleanup, tmp);
   12861              :     }
   12862              : 
   12863        17528 :   gfc_add_init_cleanup (block, gfc_finish_block (&init),
   12864              :                         gfc_finish_block (&cleanup));
   12865              : }
   12866              : 
   12867              : /************ Expression Walking Functions ******************/
   12868              : 
   12869              : /* Walk a variable reference.
   12870              : 
   12871              :    Possible extension - multiple component subscripts.
   12872              :     x(:,:) = foo%a(:)%b(:)
   12873              :    Transforms to
   12874              :     forall (i=..., j=...)
   12875              :       x(i,j) = foo%a(j)%b(i)
   12876              :     end forall
   12877              :    This adds a fair amount of complexity because you need to deal with more
   12878              :    than one ref.  Maybe handle in a similar manner to vector subscripts.
   12879              :    Maybe not worth the effort.  */
   12880              : 
   12881              : 
   12882              : static gfc_ss *
   12883       704967 : gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
   12884              : {
   12885       704967 :   gfc_ref *ref;
   12886              : 
   12887       704967 :   gfc_fix_class_refs (expr);
   12888              : 
   12889       826881 :   for (ref = expr->ref; ref; ref = ref->next)
   12890       461723 :     if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
   12891              :       break;
   12892              : 
   12893       704967 :   return gfc_walk_array_ref (ss, expr, ref);
   12894              : }
   12895              : 
   12896              : gfc_ss *
   12897       705448 : gfc_walk_array_ref (gfc_ss *ss, gfc_expr *expr, gfc_ref *ref, bool array_only)
   12898              : {
   12899       705448 :   gfc_array_ref *ar;
   12900       705448 :   gfc_ss *newss;
   12901       705448 :   int n;
   12902              : 
   12903      1056049 :   for (; ref; ref = ref->next)
   12904              :     {
   12905       350601 :       if (ref->type == REF_SUBSTRING)
   12906              :         {
   12907         1320 :           ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
   12908         1320 :           if (ref->u.ss.end)
   12909         1294 :             ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
   12910              :         }
   12911              : 
   12912              :       /* We're only interested in array sections from now on.  */
   12913       350601 :       if (ref->type != REF_ARRAY
   12914       340692 :           || (array_only && ref->u.ar.as && ref->u.ar.as->rank == 0))
   12915        10024 :         continue;
   12916              : 
   12917       340577 :       ar = &ref->u.ar;
   12918              : 
   12919       340577 :       switch (ar->type)
   12920              :         {
   12921          326 :         case AR_ELEMENT:
   12922          699 :           for (n = ar->dimen - 1; n >= 0; n--)
   12923          373 :             ss = gfc_get_scalar_ss (ss, ar->start[n]);
   12924              :           break;
   12925              : 
   12926       283171 :         case AR_FULL:
   12927              :           /* Assumed shape arrays from interface mapping need this fix.  */
   12928       283171 :           if (!ar->as && expr->symtree->n.sym->as)
   12929              :             {
   12930            6 :               ar->as = gfc_get_array_spec();
   12931            6 :               *ar->as = *expr->symtree->n.sym->as;
   12932              :             }
   12933       283171 :           newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
   12934       283171 :           newss->info->data.array.ref = ref;
   12935              : 
   12936              :           /* Make sure array is the same as array(:,:), this way
   12937              :              we don't need to special case all the time.  */
   12938       283171 :           ar->dimen = ar->as->rank;
   12939       648212 :           for (n = 0; n < ar->dimen; n++)
   12940              :             {
   12941       365041 :               ar->dimen_type[n] = DIMEN_RANGE;
   12942              : 
   12943       365041 :               gcc_assert (ar->start[n] == NULL);
   12944       365041 :               gcc_assert (ar->end[n] == NULL);
   12945       365041 :               gcc_assert (ar->stride[n] == NULL);
   12946              :             }
   12947              :           ss = newss;
   12948              :           break;
   12949              : 
   12950        57080 :         case AR_SECTION:
   12951        57080 :           newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
   12952        57080 :           newss->info->data.array.ref = ref;
   12953              : 
   12954              :           /* We add SS chains for all the subscripts in the section.  */
   12955       146705 :           for (n = 0; n < ar->dimen; n++)
   12956              :             {
   12957        89625 :               gfc_ss *indexss;
   12958              : 
   12959        89625 :               switch (ar->dimen_type[n])
   12960              :                 {
   12961         6931 :                 case DIMEN_ELEMENT:
   12962              :                   /* Add SS for elemental (scalar) subscripts.  */
   12963         6931 :                   gcc_assert (ar->start[n]);
   12964         6931 :                   indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
   12965         6931 :                   indexss->loop_chain = gfc_ss_terminator;
   12966         6931 :                   newss->info->data.array.subscript[n] = indexss;
   12967         6931 :                   break;
   12968              : 
   12969        81648 :                 case DIMEN_RANGE:
   12970              :                   /* We don't add anything for sections, just remember this
   12971              :                      dimension for later.  */
   12972        81648 :                   newss->dim[newss->dimen] = n;
   12973        81648 :                   newss->dimen++;
   12974        81648 :                   break;
   12975              : 
   12976         1046 :                 case DIMEN_VECTOR:
   12977              :                   /* Create a GFC_SS_VECTOR index in which we can store
   12978              :                      the vector's descriptor.  */
   12979         1046 :                   indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
   12980              :                                               1, GFC_SS_VECTOR);
   12981         1046 :                   indexss->loop_chain = gfc_ss_terminator;
   12982         1046 :                   newss->info->data.array.subscript[n] = indexss;
   12983         1046 :                   newss->dim[newss->dimen] = n;
   12984         1046 :                   newss->dimen++;
   12985         1046 :                   break;
   12986              : 
   12987            0 :                 default:
   12988              :                   /* We should know what sort of section it is by now.  */
   12989            0 :                   gcc_unreachable ();
   12990              :                 }
   12991              :             }
   12992              :           /* We should have at least one non-elemental dimension,
   12993              :              unless we are creating a descriptor for a (scalar) coarray.  */
   12994        57080 :           gcc_assert (newss->dimen > 0
   12995              :                       || newss->info->data.array.ref->u.ar.as->corank > 0);
   12996              :           ss = newss;
   12997              :           break;
   12998              : 
   12999            0 :         default:
   13000              :           /* We should know what sort of section it is by now.  */
   13001            0 :           gcc_unreachable ();
   13002              :         }
   13003              : 
   13004              :     }
   13005       705448 :   return ss;
   13006              : }
   13007              : 
   13008              : 
   13009              : /* Walk an expression operator. If only one operand of a binary expression is
   13010              :    scalar, we must also add the scalar term to the SS chain.  */
   13011              : 
   13012              : static gfc_ss *
   13013        59123 : gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
   13014              : {
   13015        59123 :   gfc_ss *head;
   13016        59123 :   gfc_ss *head2;
   13017              : 
   13018        59123 :   head = gfc_walk_subexpr (ss, expr->value.op.op1);
   13019        59123 :   if (expr->value.op.op2 == NULL)
   13020              :     head2 = head;
   13021              :   else
   13022        56420 :     head2 = gfc_walk_subexpr (head, expr->value.op.op2);
   13023              : 
   13024              :   /* All operands are scalar.  Pass back and let the caller deal with it.  */
   13025        59123 :   if (head2 == ss)
   13026              :     return head2;
   13027              : 
   13028              :   /* All operands require scalarization.  */
   13029        53221 :   if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
   13030              :     return head2;
   13031              : 
   13032              :   /* One of the operands needs scalarization, the other is scalar.
   13033              :      Create a gfc_ss for the scalar expression.  */
   13034        19706 :   if (head == ss)
   13035              :     {
   13036              :       /* First operand is scalar.  We build the chain in reverse order, so
   13037              :          add the scalar SS after the second operand.  */
   13038              :       head = head2;
   13039         2304 :       while (head && head->next != ss)
   13040              :         head = head->next;
   13041              :       /* Check we haven't somehow broken the chain.  */
   13042         2061 :       gcc_assert (head);
   13043         2061 :       head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
   13044              :     }
   13045              :   else                          /* head2 == head */
   13046              :     {
   13047        17645 :       gcc_assert (head2 == head);
   13048              :       /* Second operand is scalar.  */
   13049        17645 :       head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
   13050              :     }
   13051              : 
   13052              :   return head2;
   13053              : }
   13054              : 
   13055              : static gfc_ss *
   13056           36 : gfc_walk_conditional_expr (gfc_ss *ss, gfc_expr *expr)
   13057              : {
   13058           36 :   gfc_ss *head;
   13059              : 
   13060           36 :   head = gfc_walk_subexpr (ss, expr->value.conditional.true_expr);
   13061           36 :   head = gfc_walk_subexpr (head, expr->value.conditional.false_expr);
   13062           36 :   return head;
   13063              : }
   13064              : 
   13065              : /* Reverse a SS chain.  */
   13066              : 
   13067              : gfc_ss *
   13068       883859 : gfc_reverse_ss (gfc_ss * ss)
   13069              : {
   13070       883859 :   gfc_ss *next;
   13071       883859 :   gfc_ss *head;
   13072              : 
   13073       883859 :   gcc_assert (ss != NULL);
   13074              : 
   13075              :   head = gfc_ss_terminator;
   13076      1335856 :   while (ss != gfc_ss_terminator)
   13077              :     {
   13078       451997 :       next = ss->next;
   13079              :       /* Check we didn't somehow break the chain.  */
   13080       451997 :       gcc_assert (next != NULL);
   13081       451997 :       ss->next = head;
   13082       451997 :       head = ss;
   13083       451997 :       ss = next;
   13084              :     }
   13085              : 
   13086       883859 :   return (head);
   13087              : }
   13088              : 
   13089              : 
   13090              : /* Given an expression referring to a procedure, return the symbol of its
   13091              :    interface.  We can't get the procedure symbol directly as we have to handle
   13092              :    the case of (deferred) type-bound procedures.  */
   13093              : 
   13094              : gfc_symbol *
   13095          161 : gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
   13096              : {
   13097          161 :   gfc_symbol *sym;
   13098          161 :   gfc_ref *ref;
   13099              : 
   13100          161 :   if (procedure_ref == NULL)
   13101              :     return NULL;
   13102              : 
   13103              :   /* Normal procedure case.  */
   13104          161 :   if (procedure_ref->expr_type == EXPR_FUNCTION
   13105          161 :       && procedure_ref->value.function.esym)
   13106              :     sym = procedure_ref->value.function.esym;
   13107              :   else
   13108           24 :     sym = procedure_ref->symtree->n.sym;
   13109              : 
   13110              :   /* Typebound procedure case.  */
   13111          209 :   for (ref = procedure_ref->ref; ref; ref = ref->next)
   13112              :     {
   13113           48 :       if (ref->type == REF_COMPONENT
   13114           48 :           && ref->u.c.component->attr.proc_pointer)
   13115           24 :         sym = ref->u.c.component->ts.interface;
   13116              :       else
   13117              :         sym = NULL;
   13118              :     }
   13119              : 
   13120              :   return sym;
   13121              : }
   13122              : 
   13123              : 
   13124              : /* Given an expression referring to an intrinsic function call,
   13125              :    return the intrinsic symbol.  */
   13126              : 
   13127              : gfc_intrinsic_sym *
   13128         7982 : gfc_get_intrinsic_for_expr (gfc_expr *call)
   13129              : {
   13130         7982 :   if (call == NULL)
   13131              :     return NULL;
   13132              : 
   13133              :   /* Normal procedure case.  */
   13134         2378 :   if (call->expr_type == EXPR_FUNCTION)
   13135         2272 :     return call->value.function.isym;
   13136              :   else
   13137              :     return NULL;
   13138              : }
   13139              : 
   13140              : 
   13141              : /* Indicates whether an argument to an intrinsic function should be used in
   13142              :    scalarization.  It is usually the case, except for some intrinsics
   13143              :    requiring the value to be constant, and using the value at compile time only.
   13144              :    As the value is not used at runtime in those cases, we don’t produce code
   13145              :    for it, and it should not be visible to the scalarizer.
   13146              :    FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
   13147              :    argument being examined in that call, and ARG_NUM the index number
   13148              :    of ACTUAL_ARG in the list of arguments.
   13149              :    The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
   13150              :    identified using the name in ACTUAL_ARG if it is present (that is: if it’s
   13151              :    a keyword argument), otherwise using ARG_NUM.  */
   13152              : 
   13153              : static bool
   13154        38189 : arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
   13155              :                                  gfc_dummy_arg *dummy_arg)
   13156              : {
   13157        38189 :   if (function != NULL && dummy_arg != NULL)
   13158              :     {
   13159        12539 :       switch (function->id)
   13160              :         {
   13161          241 :           case GFC_ISYM_INDEX:
   13162          241 :           case GFC_ISYM_LEN_TRIM:
   13163          241 :           case GFC_ISYM_MASKL:
   13164          241 :           case GFC_ISYM_MASKR:
   13165          241 :           case GFC_ISYM_SCAN:
   13166          241 :           case GFC_ISYM_VERIFY:
   13167          241 :             if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
   13168           33 :               return false;
   13169              :           /* Fallthrough.  */
   13170              : 
   13171              :           default:
   13172              :             break;
   13173              :         }
   13174              :     }
   13175              : 
   13176              :   return true;
   13177              : }
   13178              : 
   13179              : 
   13180              : /* Walk the arguments of an elemental function.
   13181              :    PROC_EXPR is used to check whether an argument is permitted to be absent.  If
   13182              :    it is NULL, we don't do the check and the argument is assumed to be present.
   13183              : */
   13184              : 
   13185              : gfc_ss *
   13186        27152 : gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
   13187              :                                   gfc_intrinsic_sym *intrinsic_sym,
   13188              :                                   gfc_ss_type type)
   13189              : {
   13190        27152 :   int scalar;
   13191        27152 :   gfc_ss *head;
   13192        27152 :   gfc_ss *tail;
   13193        27152 :   gfc_ss *newss;
   13194              : 
   13195        27152 :   head = gfc_ss_terminator;
   13196        27152 :   tail = NULL;
   13197              : 
   13198        27152 :   scalar = 1;
   13199        66811 :   for (; arg; arg = arg->next)
   13200              :     {
   13201        39659 :       gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
   13202        41162 :       if (!arg->expr
   13203        38339 :           || arg->expr->expr_type == EXPR_NULL
   13204        77848 :           || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
   13205         1503 :         continue;
   13206              : 
   13207        38156 :       newss = gfc_walk_subexpr (head, arg->expr);
   13208        38156 :       if (newss == head)
   13209              :         {
   13210              :           /* Scalar argument.  */
   13211        18619 :           gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
   13212        18619 :           newss = gfc_get_scalar_ss (head, arg->expr);
   13213        18619 :           newss->info->type = type;
   13214        18619 :           if (dummy_arg)
   13215        15469 :             newss->info->data.scalar.dummy_arg = dummy_arg;
   13216              :         }
   13217              :       else
   13218              :         scalar = 0;
   13219              : 
   13220        35006 :       if (dummy_arg != NULL
   13221        26522 :           && gfc_dummy_arg_is_optional (*dummy_arg)
   13222         2544 :           && arg->expr->expr_type == EXPR_VARIABLE
   13223        36716 :           && (gfc_expr_attr (arg->expr).optional
   13224         1199 :               || gfc_expr_attr (arg->expr).allocatable
   13225        38156 :               || gfc_expr_attr (arg->expr).pointer))
   13226         1023 :         newss->info->can_be_null_ref = true;
   13227              : 
   13228        38156 :       head = newss;
   13229        38156 :       if (!tail)
   13230              :         {
   13231              :           tail = head;
   13232        33888 :           while (tail->next != gfc_ss_terminator)
   13233              :             tail = tail->next;
   13234              :         }
   13235              :     }
   13236              : 
   13237        27152 :   if (scalar)
   13238              :     {
   13239              :       /* If all the arguments are scalar we don't need the argument SS.  */
   13240        10399 :       gfc_free_ss_chain (head);
   13241              :       /* Pass it back.  */
   13242        10399 :       return ss;
   13243              :     }
   13244              : 
   13245              :   /* Add it onto the existing chain.  */
   13246        16753 :   tail->next = ss;
   13247        16753 :   return head;
   13248              : }
   13249              : 
   13250              : 
   13251              : /* Walk a function call.  Scalar functions are passed back, and taken out of
   13252              :    scalarization loops.  For elemental functions we walk their arguments.
   13253              :    The result of functions returning arrays is stored in a temporary outside
   13254              :    the loop, so that the function is only called once.  Hence we do not need
   13255              :    to walk their arguments.  */
   13256              : 
   13257              : static gfc_ss *
   13258        64360 : gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
   13259              : {
   13260        64360 :   gfc_intrinsic_sym *isym;
   13261        64360 :   gfc_symbol *sym;
   13262        64360 :   gfc_component *comp = NULL;
   13263              : 
   13264        64360 :   isym = expr->value.function.isym;
   13265              : 
   13266              :   /* Handle intrinsic functions separately.  */
   13267        64360 :   if (isym)
   13268        56278 :     return gfc_walk_intrinsic_function (ss, expr, isym);
   13269              : 
   13270         8082 :   sym = expr->value.function.esym;
   13271         8082 :   if (!sym)
   13272          552 :     sym = expr->symtree->n.sym;
   13273              : 
   13274         8082 :   if (gfc_is_class_array_function (expr))
   13275          234 :     return gfc_get_array_ss (ss, expr,
   13276          234 :                              CLASS_DATA (expr->value.function.esym->result)->as->rank,
   13277          234 :                              GFC_SS_FUNCTION);
   13278              : 
   13279              :   /* A function that returns arrays.  */
   13280         7848 :   comp = gfc_get_proc_ptr_comp (expr);
   13281         7444 :   if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
   13282         7848 :       || (comp && comp->attr.dimension))
   13283         3016 :     return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
   13284              : 
   13285              :   /* Walk the parameters of an elemental function.  For now we always pass
   13286              :      by reference.  */
   13287         4832 :   if (sym->attr.elemental || (comp && comp->attr.elemental))
   13288              :     {
   13289         2236 :       gfc_ss *old_ss = ss;
   13290              : 
   13291         2236 :       ss = gfc_walk_elemental_function_args (old_ss,
   13292              :                                              expr->value.function.actual,
   13293              :                                              gfc_get_intrinsic_for_expr (expr),
   13294              :                                              GFC_SS_REFERENCE);
   13295         2236 :       if (ss != old_ss
   13296         1200 :           && (comp
   13297         1133 :               || sym->attr.proc_pointer
   13298         1133 :               || sym->attr.if_source != IFSRC_DECL
   13299         1011 :               || sym->attr.array_outer_dependency))
   13300          237 :         ss->info->array_outer_dependency = 1;
   13301              :     }
   13302              : 
   13303              :   /* Scalar functions are OK as these are evaluated outside the scalarization
   13304              :      loop.  Pass back and let the caller deal with it.  */
   13305              :   return ss;
   13306              : }
   13307              : 
   13308              : 
   13309              : /* An array temporary is constructed for array constructors.  */
   13310              : 
   13311              : static gfc_ss *
   13312        52260 : gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
   13313              : {
   13314            0 :   return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
   13315              : }
   13316              : 
   13317              : 
   13318              : /* Walk an expression.  Add walked expressions to the head of the SS chain.
   13319              :    A wholly scalar expression will not be added.  */
   13320              : 
   13321              : gfc_ss *
   13322      1041716 : gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
   13323              : {
   13324      1041716 :   gfc_ss *head;
   13325              : 
   13326      1041716 :   switch (expr->expr_type)
   13327              :     {
   13328       704967 :     case EXPR_VARIABLE:
   13329       704967 :       head = gfc_walk_variable_expr (ss, expr);
   13330       704967 :       return head;
   13331              : 
   13332        59123 :     case EXPR_OP:
   13333        59123 :       head = gfc_walk_op_expr (ss, expr);
   13334        59123 :       return head;
   13335              : 
   13336           36 :     case EXPR_CONDITIONAL:
   13337           36 :       head = gfc_walk_conditional_expr (ss, expr);
   13338           36 :       return head;
   13339              : 
   13340        64360 :     case EXPR_FUNCTION:
   13341        64360 :       head = gfc_walk_function_expr (ss, expr);
   13342        64360 :       return head;
   13343              : 
   13344              :     case EXPR_CONSTANT:
   13345              :     case EXPR_NULL:
   13346              :     case EXPR_STRUCTURE:
   13347              :       /* Pass back and let the caller deal with it.  */
   13348              :       break;
   13349              : 
   13350        52260 :     case EXPR_ARRAY:
   13351        52260 :       head = gfc_walk_array_constructor (ss, expr);
   13352        52260 :       return head;
   13353              : 
   13354              :     case EXPR_SUBSTRING:
   13355              :       /* Pass back and let the caller deal with it.  */
   13356              :       break;
   13357              : 
   13358            0 :     default:
   13359            0 :       gfc_internal_error ("bad expression type during walk (%d)",
   13360              :                       expr->expr_type);
   13361              :     }
   13362              :   return ss;
   13363              : }
   13364              : 
   13365              : 
   13366              : /* Entry point for expression walking.
   13367              :    A return value equal to the passed chain means this is
   13368              :    a scalar expression.  It is up to the caller to take whatever action is
   13369              :    necessary to translate these.  */
   13370              : 
   13371              : gfc_ss *
   13372       880916 : gfc_walk_expr (gfc_expr * expr)
   13373              : {
   13374       880916 :   gfc_ss *res;
   13375              : 
   13376       880916 :   res = gfc_walk_subexpr (gfc_ss_terminator, expr);
   13377       880916 :   return gfc_reverse_ss (res);
   13378              : }
        

Generated by: LCOV version 2.5.0-beta

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