LCOV - code coverage report
Current view: top level - gcc/fortran - trans-array.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.2 % 6186 5948
Test Date: 2026-10-03 16:17:38 Functions: 99.4 % 175 174
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        60203 : gfc_array_dataptr_type (tree desc)
     109              : {
     110        60203 :   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       176371 : gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
     226              : {
     227       414262 :   for (; ss != gfc_ss_terminator; ss = ss->next)
     228       237891 :     ss->info->useflags = flags;
     229       176371 : }
     230              : 
     231              : 
     232              : /* Free a gfc_ss chain.  */
     233              : 
     234              : void
     235       185075 : gfc_free_ss_chain (gfc_ss * ss)
     236              : {
     237       185075 :   gfc_ss *next;
     238              : 
     239       378368 :   while (ss != gfc_ss_terminator)
     240              :     {
     241       193293 :       gcc_assert (ss != NULL);
     242       193293 :       next = ss->next;
     243       193293 :       gfc_free_ss (ss);
     244       193293 :       ss = next;
     245              :     }
     246       185075 : }
     247              : 
     248              : 
     249              : static void
     250       502122 : free_ss_info (gfc_ss_info *ss_info)
     251              : {
     252       502122 :   int n;
     253              : 
     254       502122 :   ss_info->refcount--;
     255       502122 :   if (ss_info->refcount > 0)
     256              :     return;
     257              : 
     258       497375 :   gcc_assert (ss_info->refcount == 0);
     259              : 
     260       497375 :   switch (ss_info->type)
     261              :     {
     262              :     case GFC_SS_SECTION:
     263      5532640 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
     264      5186850 :         if (ss_info->data.array.subscript[n])
     265         7840 :           gfc_free_ss_chain (ss_info->data.array.subscript[n]);
     266              :       break;
     267              : 
     268              :     default:
     269              :       break;
     270              :     }
     271              : 
     272       497375 :   free (ss_info);
     273              : }
     274              : 
     275              : 
     276              : /* Free a SS.  */
     277              : 
     278              : void
     279       502122 : gfc_free_ss (gfc_ss * ss)
     280              : {
     281       502122 :   free_ss_info (ss->info);
     282       502122 :   free (ss);
     283       502122 : }
     284              : 
     285              : 
     286              : /* Creates and initializes an array type gfc_ss struct.  */
     287              : 
     288              : gfc_ss *
     289       420507 : gfc_get_array_ss (gfc_ss *next, gfc_expr *expr, int dimen, gfc_ss_type type)
     290              : {
     291       420507 :   gfc_ss *ss;
     292       420507 :   gfc_ss_info *ss_info;
     293       420507 :   int i;
     294              : 
     295       420507 :   ss_info = gfc_get_ss_info ();
     296       420507 :   ss_info->refcount++;
     297       420507 :   ss_info->type = type;
     298       420507 :   ss_info->expr = expr;
     299              : 
     300       420507 :   ss = gfc_get_ss ();
     301       420507 :   ss->info = ss_info;
     302       420507 :   ss->next = next;
     303       420507 :   ss->dimen = dimen;
     304       884804 :   for (i = 0; i < ss->dimen; i++)
     305       464297 :     ss->dim[i] = i;
     306              : 
     307       420507 :   return ss;
     308              : }
     309              : 
     310              : 
     311              : /* Creates and initializes a temporary type gfc_ss struct.  */
     312              : 
     313              : gfc_ss *
     314        11667 : gfc_get_temp_ss (tree type, tree string_length, int dimen)
     315              : {
     316        11667 :   gfc_ss *ss;
     317        11667 :   gfc_ss_info *ss_info;
     318        11667 :   int i;
     319              : 
     320        11667 :   ss_info = gfc_get_ss_info ();
     321        11667 :   ss_info->refcount++;
     322        11667 :   ss_info->type = GFC_SS_TEMP;
     323        11667 :   ss_info->string_length = string_length;
     324        11667 :   ss_info->data.temp.type = type;
     325              : 
     326        11667 :   ss = gfc_get_ss ();
     327        11667 :   ss->info = ss_info;
     328        11667 :   ss->next = gfc_ss_terminator;
     329        11667 :   ss->dimen = dimen;
     330        26067 :   for (i = 0; i < ss->dimen; i++)
     331        14400 :     ss->dim[i] = i;
     332              : 
     333        11667 :   return ss;
     334              : }
     335              : 
     336              : 
     337              : /* Creates and initializes a scalar type gfc_ss struct.  */
     338              : 
     339              : gfc_ss *
     340        67320 : gfc_get_scalar_ss (gfc_ss *next, gfc_expr *expr)
     341              : {
     342        67320 :   gfc_ss *ss;
     343        67320 :   gfc_ss_info *ss_info;
     344              : 
     345        67320 :   ss_info = gfc_get_ss_info ();
     346        67320 :   ss_info->refcount++;
     347        67320 :   ss_info->type = GFC_SS_SCALAR;
     348        67320 :   ss_info->expr = expr;
     349              : 
     350        67320 :   ss = gfc_get_ss ();
     351        67320 :   ss->info = ss_info;
     352        67320 :   ss->next = next;
     353              : 
     354        67320 :   return ss;
     355              : }
     356              : 
     357              : 
     358              : /* Free all the SS associated with a loop.  */
     359              : 
     360              : void
     361       186473 : gfc_cleanup_loop (gfc_loopinfo * loop)
     362              : {
     363       186473 :   gfc_loopinfo *loop_next, **ploop;
     364       186473 :   gfc_ss *ss;
     365       186473 :   gfc_ss *next;
     366              : 
     367       186473 :   ss = loop->ss;
     368       494923 :   while (ss != gfc_ss_terminator)
     369              :     {
     370       308450 :       gcc_assert (ss != NULL);
     371       308450 :       next = ss->loop_chain;
     372       308450 :       gfc_free_ss (ss);
     373       308450 :       ss = next;
     374              :     }
     375              : 
     376              :   /* Remove reference to self in the parent loop.  */
     377       186473 :   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       189837 :   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       186473 : }
     393              : 
     394              : 
     395              : static void
     396       253632 : set_ss_loop (gfc_ss *ss, gfc_loopinfo *loop)
     397              : {
     398       253632 :   int n;
     399              : 
     400       571269 :   for (; ss != gfc_ss_terminator; ss = ss->next)
     401              :     {
     402       317637 :       ss->loop = loop;
     403              : 
     404       317637 :       if (ss->info->type == GFC_SS_SCALAR
     405              :           || ss->info->type == GFC_SS_REFERENCE
     406       268434 :           || ss->info->type == GFC_SS_TEMP)
     407        60870 :         continue;
     408              : 
     409      4108272 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
     410      3851505 :         if (ss->info->data.array.subscript[n] != NULL)
     411         7569 :           set_ss_loop (ss->info->data.array.subscript[n], loop);
     412              :     }
     413       253632 : }
     414              : 
     415              : 
     416              : /* Associate a SS chain with a loop.  */
     417              : 
     418              : void
     419       246063 : gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
     420              : {
     421       246063 :   gfc_ss *ss;
     422       246063 :   gfc_loopinfo *nested_loop;
     423              : 
     424       246063 :   if (head == gfc_ss_terminator)
     425              :     return;
     426              : 
     427       246063 :   set_ss_loop (head, loop);
     428              : 
     429       246063 :   ss = head;
     430       802194 :   for (; ss && ss != gfc_ss_terminator; ss = ss->next)
     431              :     {
     432       310068 :       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       310068 :       if (ss->next == gfc_ss_terminator)
     453       246063 :         ss->loop_chain = loop->ss;
     454              :       else
     455              :         ss->loop_chain = ss->next;
     456              :     }
     457       246063 :   gcc_assert (ss == gfc_ss_terminator);
     458       246063 :   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       548477 : is_pointer_array (tree expr)
     467              : {
     468       548477 :   if (expr == NULL_TREE
     469       548477 :       || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr))
     470       673302 :       || GFC_CLASS_TYPE_P (TREE_TYPE (expr)))
     471              :     return false;
     472              : 
     473       124825 :   if (VAR_P (expr)
     474       124825 :       && GFC_DECL_PTR_ARRAY_P (expr))
     475              :     return true;
     476              : 
     477       117916 :   if (TREE_CODE (expr) == PARM_DECL
     478       117916 :       && GFC_DECL_PTR_ARRAY_P (expr))
     479              :     return true;
     480              : 
     481       117916 :   if (INDIRECT_REF_P (expr)
     482       117916 :       && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 0)))
     483              :     return true;
     484              : 
     485              :   /* The field declaration is marked as a pointer array.  */
     486       115365 :   if (TREE_CODE (expr) == COMPONENT_REF
     487       115365 :       && GFC_DECL_PTR_ARRAY_P (TREE_OPERAND (expr, 1)))
     488         3903 :     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       548477 : is_span_addressed_array (tree expr)
     501              : {
     502       548477 :   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        13363 :       if (TREE_CODE (expr) == COMPONENT_REF
     507         3903 :           && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
     508        13577 :           && TYPE_LANG_SPECIFIC (TREE_TYPE (expr)))
     509              :         {
     510          214 :           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        13363 :       return true;
     526              :     }
     527              : 
     528       535114 :   if (VAR_P (expr)
     529       467253 :       && GFC_DECL_PTR_ARRAY_P (expr)
     530          246 :       && !GFC_DECL_CLASS (expr)
     531          246 :       && GFC_ARRAY_TYPE_P (TREE_TYPE (expr))
     532          246 :       && DECL_LANG_SPECIFIC (expr)
     533       535360 :       && GFC_DECL_SAVED_DESCRIPTOR (expr))
     534          246 :     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        13115 : gfc_span_folds_into_stride (gfc_symbol *sym)
     547              : {
     548        13115 :   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         6269 :   if (sym->ts.type != BT_INTEGER
     554         4772 :       && sym->ts.type != BT_REAL
     555         1308 :       && sym->ts.type != BT_LOGICAL)
     556              :     return false;
     557              : 
     558              :   /* An assumed rank dummy has no strides to fold the spacing into.  */
     559         4961 :   if (!sym->as || sym->as->type != AS_ASSUMED_SHAPE || sym->as->rank < 1)
     560              :     return false;
     561              : 
     562         4827 :   tree etype = gfc_typenode_for_spec (&sym->ts);
     563         4827 :   tree size = etype ? TYPE_SIZE_UNIT (etype) : NULL_TREE;
     564              : 
     565         4827 :   return (size
     566         4827 :           && tree_fits_uhwi_p (size)
     567         9654 :           && 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       592359 : gfc_is_span_addressed_dummy (gfc_symbol *sym)
     578              : {
     579       592359 :   return gfc_dummy_requires_direct_arg (sym)
     580       592359 :          && !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           12 : gfc_conv_span_is_elem_len (gfc_se *se, gfc_expr *arg)
     590              : {
     591           12 :   gfc_se argse;
     592           12 :   gfc_ss *ss;
     593              : 
     594           12 :   if (arg->ts.type == BT_CLASS)
     595            0 :     gfc_add_class_array_ref (arg);
     596              : 
     597           12 :   ss = gfc_walk_expr (arg);
     598           12 :   gcc_assert (ss != gfc_ss_terminator);
     599              : 
     600           12 :   gfc_init_se (&argse, NULL);
     601           12 :   argse.data_not_needed = 1;
     602           12 :   gfc_conv_expr_descriptor (&argse, arg);
     603           12 :   gfc_add_block_to_block (&se->pre, &argse.pre);
     604           12 :   gfc_add_block_to_block (&se->post, &argse.post);
     605           12 :   gfc_free_ss_chain (ss);
     606              : 
     607           12 :   tree desc = gfc_evaluate_now (argse.expr, &se->pre);
     608           12 :   tree span = gfc_conv_descriptor_span_get (desc);
     609           12 :   tree elem_len = fold_convert (TREE_TYPE (span),
     610              :                                 gfc_conv_descriptor_elem_len_get (desc));
     611           12 :   se->expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
     612              :                               span, elem_len);
     613           12 : }
     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       379111 : get_CFI_desc (gfc_symbol *sym, gfc_expr *expr,
     624              :               tree *desc, gfc_array_ref *ar)
     625              : {
     626       379111 :   tree tmp;
     627              : 
     628       379111 :   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         1149 : 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         1149 :   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         1149 :   if (unlimited)
     663         1058 :     return gfc_resize_class_size_with_len (NULL,
     664          529 :                                            TREE_OPERAND (vptr, 0),
     665          529 :                                            gfc_vptr_size_get (vptr));
     666              :   else
     667          620 :     return gfc_vptr_size_get (vptr);
     668              : }
     669              : 
     670              : 
     671              : /* Return the span of an array.  */
     672              : 
     673              : tree
     674        59502 : gfc_get_array_span (tree desc, gfc_expr *expr)
     675              : {
     676        59502 :   tree tmp;
     677        59502 :   gfc_symbol *sym = (expr && expr->expr_type == EXPR_VARIABLE) ?
     678        52156 :                     expr->symtree->n.sym : NULL;
     679              : 
     680        59502 :   if (tree span = GFC_DECL_GET_SPAN (desc))
     681              :     /* A span addressed dummy loaded its span on entry.  */
     682              :     tmp = span;
     683        59358 :   else if (is_span_addressed_array (desc)
     684        59358 :            || (get_CFI_desc (NULL, expr, &desc, NULL)
     685         1332 :                && (POINTER_TYPE_P (TREE_TYPE (desc))
     686          666 :                    ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
     687            0 :                    : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
     688              :     /* This will have the span field set.  */
     689          632 :     tmp = gfc_conv_descriptor_span_get (gfc_get_span_descriptor (desc));
     690        58726 :   else if (expr->ts.type == BT_ASSUMED)
     691              :     {
     692          127 :       if (DECL_LANG_SPECIFIC (desc) && GFC_DECL_SAVED_DESCRIPTOR (desc))
     693          127 :         desc = GFC_DECL_SAVED_DESCRIPTOR (desc);
     694          127 :       if (POINTER_TYPE_P (TREE_TYPE (desc)))
     695          127 :         desc = build_fold_indirect_ref_loc (input_location, desc);
     696          127 :       tmp = gfc_conv_descriptor_span_get (desc);
     697              :     }
     698        58599 :   else if (TREE_CODE (desc) == COMPONENT_REF
     699          556 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
     700        58704 :            && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (desc, 0))))
     701              :     /* The descriptor is the _data field of a class object.  */
     702           32 :     tmp = class_array_element_size (TREE_OPERAND (desc, 0),
     703           32 :                                     UNLIMITED_POLY (expr));
     704        58567 :   else if (sym && sym->ts.type == BT_CLASS
     705         1173 :            && expr->ref->type == REF_COMPONENT
     706         1173 :            && expr->ref->next->type == REF_ARRAY
     707         1173 :            && expr->ref->next->next == NULL
     708         1155 :            && CLASS_DATA (sym)->attr.dimension)
     709              :     /* Having escaped the above, this can only be a class array dummy.  */
     710         1117 :     tmp = class_array_element_size (sym->backend_decl,
     711         1117 :                                     UNLIMITED_POLY (sym));
     712              :   else
     713              :     {
     714              :       /* If none of the fancy stuff works, the span is the element
     715              :          size of the array. Attempt to deal with unbounded character
     716              :          types if possible. Otherwise, return NULL_TREE.  */
     717        57450 :       tmp = gfc_get_element_type (TREE_TYPE (desc));
     718        57450 :       if (tmp && TREE_CODE (tmp) == ARRAY_TYPE && TYPE_STRING_FLAG (tmp))
     719              :         {
     720        11011 :           gcc_assert (expr->ts.type == BT_CHARACTER);
     721              : 
     722        11011 :           tmp = gfc_get_character_len_in_bytes (tmp);
     723              : 
     724        11011 :           if (tmp == NULL_TREE || integer_zerop (tmp))
     725              :             {
     726           68 :               tree bs;
     727              : 
     728           68 :               tmp = gfc_get_expr_charlen (expr);
     729           68 :               tmp = fold_convert (gfc_array_index_type, tmp);
     730           68 :               bs = build_int_cst (gfc_array_index_type, expr->ts.kind);
     731           68 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
     732              :                                      gfc_array_index_type, tmp, bs);
     733              :             }
     734              : 
     735        21954 :           tmp = (tmp && !integer_zerop (tmp))
     736        21954 :             ? (fold_convert (gfc_array_index_type, tmp)) : (NULL_TREE);
     737              :         }
     738              :       else
     739        46439 :         tmp = fold_convert (gfc_array_index_type,
     740              :                             size_in_bytes (tmp));
     741              :     }
     742        59502 :   return tmp;
     743              : }
     744              : 
     745              : 
     746              : /* Generate an initializer for a static pointer or allocatable array.  */
     747              : 
     748              : void
     749          276 : gfc_trans_static_array_pointer (gfc_symbol * sym)
     750              : {
     751          276 :   tree type;
     752              : 
     753          276 :   gcc_assert (TREE_STATIC (sym->backend_decl));
     754              :   /* Just zero the data member.  */
     755          276 :   type = TREE_TYPE (sym->backend_decl);
     756          276 :   DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
     757          276 : }
     758              : 
     759              : 
     760              : /* If the bounds of SE's loop have not yet been set, see if they can be
     761              :    determined from array spec AS, which is the array spec of a called
     762              :    function.  MAPPING maps the callee's dummy arguments to the values
     763              :    that the caller is passing.  Add any initialization and finalization
     764              :    code to SE.  */
     765              : 
     766              : void
     767         8761 : gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
     768              :                                      gfc_se * se, gfc_array_spec * as)
     769              : {
     770         8761 :   int n, dim, total_dim;
     771         8761 :   gfc_se tmpse;
     772         8761 :   gfc_ss *ss;
     773         8761 :   tree lower;
     774         8761 :   tree upper;
     775         8761 :   tree tmp;
     776              : 
     777         8761 :   total_dim = 0;
     778              : 
     779         8761 :   if (!as || as->type != AS_EXPLICIT)
     780         7600 :     return;
     781              : 
     782         2347 :   for (ss = se->ss; ss; ss = ss->parent)
     783              :     {
     784         1186 :       total_dim += ss->loop->dimen;
     785         2727 :       for (n = 0; n < ss->loop->dimen; n++)
     786              :         {
     787              :           /* The bound is known, nothing to do.  */
     788         1541 :           if (ss->loop->to[n] != NULL_TREE)
     789          485 :             continue;
     790              : 
     791         1056 :           dim = ss->dim[n];
     792         1056 :           gcc_assert (dim < as->rank);
     793         1056 :           gcc_assert (ss->loop->dimen <= as->rank);
     794              : 
     795              :           /* Evaluate the lower bound.  */
     796         1056 :           gfc_init_se (&tmpse, NULL);
     797         1056 :           gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
     798         1056 :           gfc_add_block_to_block (&se->pre, &tmpse.pre);
     799         1056 :           gfc_add_block_to_block (&se->post, &tmpse.post);
     800         1056 :           lower = fold_convert (gfc_array_index_type, tmpse.expr);
     801              : 
     802              :           /* ...and the upper bound.  */
     803         1056 :           gfc_init_se (&tmpse, NULL);
     804         1056 :           gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
     805         1056 :           gfc_add_block_to_block (&se->pre, &tmpse.pre);
     806         1056 :           gfc_add_block_to_block (&se->post, &tmpse.post);
     807         1056 :           upper = fold_convert (gfc_array_index_type, tmpse.expr);
     808              : 
     809              :           /* Set the upper bound of the loop to UPPER - LOWER.  */
     810         1056 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
     811              :                                  gfc_array_index_type, upper, lower);
     812         1056 :           tmp = gfc_evaluate_now (tmp, &se->pre);
     813         1056 :           ss->loop->to[n] = tmp;
     814              :         }
     815              :     }
     816              : 
     817         1161 :   gcc_assert (total_dim == as->rank);
     818              : }
     819              : 
     820              : 
     821              : /* Generate code to allocate an array temporary, or create a variable to
     822              :    hold the data.  If size is NULL, zero the descriptor so that the
     823              :    callee will allocate the array.  If DEALLOC is true, also generate code to
     824              :    free the array afterwards.
     825              : 
     826              :    If INITIAL is not NULL, it is packed using internal_pack and the result used
     827              :    as data instead of allocating a fresh, uninitialized area of memory.
     828              : 
     829              :    Initialization code is added to PRE and finalization code to POST.
     830              :    DYNAMIC is true if the caller may want to extend the array later
     831              :    using realloc.  This prevents us from putting the array on the stack.  */
     832              : 
     833              : static void
     834        28401 : gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
     835              :                                   gfc_array_info * info, tree size, tree nelem,
     836              :                                   tree initial, bool dynamic, bool dealloc)
     837              : {
     838        28401 :   tree tmp;
     839        28401 :   tree desc;
     840        28401 :   bool onstack;
     841              : 
     842        28401 :   desc = info->descriptor;
     843        28401 :   info->offset = gfc_index_zero_node;
     844        28401 :   if (size == NULL_TREE || (dynamic && integer_zerop (size)))
     845              :     {
     846              :       /* A callee allocated array.  */
     847         2883 :       gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
     848         2883 :       onstack = false;
     849              :     }
     850              :   else
     851              :     {
     852              :       /* Allocate the temporary.  */
     853        51036 :       onstack = !dynamic && initial == NULL_TREE
     854        25518 :                          && (flag_stack_arrays
     855        25133 :                              || gfc_can_put_var_on_stack (size));
     856              : 
     857         5204 :       if (onstack)
     858              :         {
     859              :           /* Make a temporary variable to hold the data.  */
     860        20314 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (nelem),
     861              :                                  nelem, gfc_index_one_node);
     862        20314 :           tmp = gfc_evaluate_now (tmp, pre);
     863        20314 :           tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
     864              :                                   tmp);
     865        20314 :           tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
     866              :                                   tmp);
     867        20314 :           tmp = gfc_create_var (tmp, "A");
     868              :           /* If we're here only because of -fstack-arrays we have to
     869              :              emit a DECL_EXPR to make the gimplifier emit alloca calls.  */
     870        20314 :           if (!gfc_can_put_var_on_stack (size))
     871           17 :             gfc_add_expr_to_block (pre,
     872              :                                    fold_build1_loc (input_location,
     873           17 :                                                     DECL_EXPR, TREE_TYPE (tmp),
     874              :                                                     tmp));
     875        20314 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
     876        20314 :           gfc_conv_descriptor_data_set (pre, desc, tmp);
     877              :         }
     878              :       else
     879              :         {
     880              :           /* Allocate memory to hold the data or call internal_pack.  */
     881         5204 :           if (initial == NULL_TREE)
     882              :             {
     883         5061 :               tmp = gfc_call_malloc (pre, NULL, size);
     884         5061 :               tmp = gfc_evaluate_now (tmp, pre);
     885              :             }
     886              :           else
     887              :             {
     888          143 :               tree packed;
     889          143 :               tree source_data;
     890          143 :               tree was_packed;
     891          143 :               stmtblock_t do_copying;
     892              : 
     893          143 :               tmp = TREE_TYPE (initial); /* Pointer to descriptor.  */
     894          143 :               gcc_assert (TREE_CODE (tmp) == POINTER_TYPE);
     895          143 :               tmp = TREE_TYPE (tmp); /* The descriptor itself.  */
     896          143 :               tmp = gfc_get_element_type (tmp);
     897          143 :               packed = gfc_create_var (build_pointer_type (tmp), "data");
     898              : 
     899          143 :               tmp = build_call_expr_loc (input_location,
     900              :                                      gfor_fndecl_in_pack, 1, initial);
     901          143 :               tmp = fold_convert (TREE_TYPE (packed), tmp);
     902          143 :               gfc_add_modify (pre, packed, tmp);
     903              : 
     904          143 :               tmp = build_fold_indirect_ref_loc (input_location,
     905              :                                              initial);
     906          143 :               source_data = gfc_conv_descriptor_data_get (tmp);
     907              : 
     908              :               /* internal_pack may return source->data without any allocation
     909              :                  or copying if it is already packed.  If that's the case, we
     910              :                  need to allocate and copy manually.  */
     911              : 
     912          143 :               gfc_start_block (&do_copying);
     913          143 :               tmp = gfc_call_malloc (&do_copying, NULL, size);
     914          143 :               tmp = fold_convert (TREE_TYPE (packed), tmp);
     915          143 :               gfc_add_modify (&do_copying, packed, tmp);
     916          143 :               tmp = gfc_build_memcpy_call (packed, source_data, size);
     917          143 :               gfc_add_expr_to_block (&do_copying, tmp);
     918              : 
     919          143 :               was_packed = fold_build2_loc (input_location, EQ_EXPR,
     920              :                                             logical_type_node, packed,
     921              :                                             source_data);
     922          143 :               tmp = gfc_finish_block (&do_copying);
     923          143 :               tmp = build3_v (COND_EXPR, was_packed, tmp,
     924              :                               build_empty_stmt (input_location));
     925          143 :               gfc_add_expr_to_block (pre, tmp);
     926              : 
     927          143 :               tmp = fold_convert (pvoid_type_node, packed);
     928              :             }
     929              : 
     930         5204 :           gfc_conv_descriptor_data_set (pre, desc, tmp);
     931              :         }
     932              :     }
     933        28401 :   info->data = gfc_conv_descriptor_data_get (desc);
     934              : 
     935              :   /* The offset is zero because we create temporaries with a zero
     936              :      lower bound.  */
     937        28401 :   gfc_conv_descriptor_offset_set (pre, desc, gfc_index_zero_node);
     938              : 
     939        28401 :   if (dealloc && !onstack)
     940              :     {
     941              :       /* Free the temporary.  */
     942         7837 :       tmp = gfc_conv_descriptor_data_get (desc);
     943         7837 :       tmp = gfc_call_free (tmp);
     944         7837 :       gfc_add_expr_to_block (post, tmp);
     945              :     }
     946        28401 : }
     947              : 
     948              : 
     949              : /* Get the scalarizer array dimension corresponding to actual array dimension
     950              :    given by ARRAY_DIM.
     951              : 
     952              :    For example, if SS represents the array ref a(1,:,:,1), it is a
     953              :    bidimensional scalarizer array, and the result would be 0 for ARRAY_DIM=1,
     954              :    and 1 for ARRAY_DIM=2.
     955              :    If SS represents transpose(a(:,1,1,:)), it is again a bidimensional
     956              :    scalarizer array, and the result would be 1 for ARRAY_DIM=0 and 0 for
     957              :    ARRAY_DIM=3.
     958              :    If SS represents sum(a(:,:,:,1), dim=1), it is a 2+1-dimensional scalarizer
     959              :    array.  If called on the inner ss, the result would be respectively 0,1,2 for
     960              :    ARRAY_DIM=0,1,2.  If called on the outer ss, the result would be 0,1
     961              :    for ARRAY_DIM=1,2.  */
     962              : 
     963              : static int
     964       265368 : get_scalarizer_dim_for_array_dim (gfc_ss *ss, int array_dim)
     965              : {
     966       265368 :   int array_ref_dim;
     967       265368 :   int n;
     968              : 
     969       265368 :   array_ref_dim = 0;
     970              : 
     971       536869 :   for (; ss; ss = ss->parent)
     972       696997 :     for (n = 0; n < ss->dimen; n++)
     973       425496 :       if (ss->dim[n] < array_dim)
     974        77172 :         array_ref_dim++;
     975              : 
     976       265368 :   return array_ref_dim;
     977              : }
     978              : 
     979              : 
     980              : static gfc_ss *
     981       224304 : innermost_ss (gfc_ss *ss)
     982              : {
     983       413731 :   while (ss->nested_ss != NULL)
     984              :     ss = ss->nested_ss;
     985              : 
     986       405523 :   return ss;
     987              : }
     988              : 
     989              : 
     990              : 
     991              : /* Get the array reference dimension corresponding to the given loop dimension.
     992              :    It is different from the true array dimension given by the dim array in
     993              :    the case of a partial array reference (i.e. a(:,:,1,:) for example)
     994              :    It is different from the loop dimension in the case of a transposed array.
     995              :    */
     996              : 
     997              : static int
     998       224304 : get_array_ref_dim_for_loop_dim (gfc_ss *ss, int loop_dim)
     999              : {
    1000       224304 :   return get_scalarizer_dim_for_array_dim (innermost_ss (ss),
    1001       224304 :                                            ss->dim[loop_dim]);
    1002              : }
    1003              : 
    1004              : 
    1005              : /* Use the information in the ss to obtain the required information about
    1006              :    the type and size of an array temporary, when the lhs in an assignment
    1007              :    is a class expression.  */
    1008              : 
    1009              : static tree
    1010          327 : get_class_info_from_ss (stmtblock_t * pre, gfc_ss *ss, tree *eltype,
    1011              :                         gfc_ss **fcnss)
    1012              : {
    1013          327 :   gfc_ss *loop_ss = ss->loop->ss;
    1014          327 :   gfc_ss *lhs_ss;
    1015          327 :   gfc_ss *rhs_ss;
    1016          327 :   gfc_ss *fcn_ss = NULL;
    1017          327 :   tree tmp;
    1018          327 :   tree tmp2;
    1019          327 :   tree vptr;
    1020          327 :   tree class_expr = NULL_TREE;
    1021          327 :   tree lhs_class_expr = NULL_TREE;
    1022          327 :   bool unlimited_rhs = false;
    1023          327 :   bool unlimited_lhs = false;
    1024          327 :   bool rhs_function = false;
    1025          327 :   bool unlimited_arg1 = false;
    1026          327 :   gfc_symbol *vtab;
    1027          327 :   tree cntnr = NULL_TREE;
    1028              : 
    1029              :   /* The second element in the loop chain contains the source for the
    1030              :      class temporary created in gfc_trans_create_temp_array.  */
    1031          327 :   rhs_ss = loop_ss->loop_chain;
    1032              : 
    1033          327 :   if (rhs_ss != gfc_ss_terminator
    1034          303 :       && rhs_ss->info
    1035          303 :       && rhs_ss->info->expr
    1036          303 :       && rhs_ss->info->expr->ts.type == BT_CLASS
    1037          182 :       && rhs_ss->info->data.array.descriptor)
    1038              :     {
    1039          170 :       if (rhs_ss->info->expr->expr_type != EXPR_VARIABLE)
    1040           56 :         class_expr
    1041           56 :           = gfc_get_class_from_expr (rhs_ss->info->data.array.descriptor);
    1042              :       else
    1043          114 :         class_expr = gfc_get_class_from_gfc_expr (rhs_ss->info->expr);
    1044          170 :       unlimited_rhs = UNLIMITED_POLY (rhs_ss->info->expr);
    1045          170 :       if (rhs_ss->info->expr->expr_type == EXPR_FUNCTION)
    1046              :         rhs_function = true;
    1047              :     }
    1048              : 
    1049              :   /* Usually, ss points to the function. When the function call is an actual
    1050              :      argument, it is instead rhs_ss because the ss chain is shifted by one.  */
    1051          327 :   *fcnss = fcn_ss = rhs_function ? rhs_ss : ss;
    1052              : 
    1053              :   /* If this is a transformational function with a class result, the info
    1054              :      class_container field points to the class container of arg1.  */
    1055          327 :   if (class_expr != NULL_TREE
    1056          151 :       && fcn_ss->info && fcn_ss->info->expr
    1057           91 :       && fcn_ss->info->expr->expr_type == EXPR_FUNCTION
    1058           91 :       && fcn_ss->info->expr->value.function.isym
    1059           60 :       && fcn_ss->info->expr->value.function.isym->transformational)
    1060              :     {
    1061           60 :       cntnr = ss->info->class_container;
    1062           60 :       unlimited_arg1
    1063           60 :            = UNLIMITED_POLY (fcn_ss->info->expr->value.function.actual->expr);
    1064              :     }
    1065              : 
    1066              :   /* For an assignment the lhs is the next element in the loop chain.
    1067              :      If we have a class rhs, this had better be a class variable
    1068              :      expression!  Otherwise, the class container from arg1 can be used
    1069              :      to set the vptr and len fields of the result class container.  */
    1070          327 :   lhs_ss = rhs_ss->loop_chain;
    1071          327 :   if (lhs_ss && lhs_ss != gfc_ss_terminator
    1072          225 :       && lhs_ss->info && lhs_ss->info->expr
    1073          225 :       && lhs_ss->info->expr->expr_type ==EXPR_VARIABLE
    1074          225 :       && lhs_ss->info->expr->ts.type == BT_CLASS)
    1075              :     {
    1076          225 :       tmp = lhs_ss->info->data.array.descriptor;
    1077          225 :       unlimited_lhs = UNLIMITED_POLY (rhs_ss->info->expr);
    1078              :     }
    1079          102 :   else if (cntnr != NULL_TREE)
    1080              :     {
    1081           54 :       tmp = gfc_class_vptr_get (class_expr);
    1082           54 :       gfc_add_modify (pre, tmp, fold_convert (TREE_TYPE (tmp),
    1083              :                                               gfc_class_vptr_get (cntnr)));
    1084           54 :       if (unlimited_rhs)
    1085              :         {
    1086            6 :           tmp = gfc_class_len_get (class_expr);
    1087            6 :           if (unlimited_arg1)
    1088            6 :             gfc_add_modify (pre, tmp, gfc_class_len_get (cntnr));
    1089              :         }
    1090              :       tmp = NULL_TREE;
    1091              :     }
    1092              :   else
    1093              :     tmp = NULL_TREE;
    1094              : 
    1095              :   /* Get the lhs class expression.  */
    1096          231 :   if (tmp != NULL_TREE && lhs_ss->loop_chain == gfc_ss_terminator)
    1097          213 :     lhs_class_expr = gfc_get_class_from_expr (tmp);
    1098              :   else
    1099              :     return class_expr;
    1100              : 
    1101          213 :   gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (lhs_class_expr)));
    1102              : 
    1103              :   /* Set the lhs vptr and, if necessary, the _len field.  */
    1104          213 :   if (class_expr)
    1105              :     {
    1106              :       /* Both lhs and rhs are class expressions.  */
    1107           79 :       tmp = gfc_class_vptr_get (lhs_class_expr);
    1108          158 :       gfc_add_modify (pre, tmp,
    1109           79 :                       fold_convert (TREE_TYPE (tmp),
    1110              :                                     gfc_class_vptr_get (class_expr)));
    1111           79 :       if (unlimited_lhs)
    1112              :         {
    1113           31 :           gcc_assert (unlimited_rhs);
    1114           31 :           tmp = gfc_class_len_get (lhs_class_expr);
    1115           31 :           tmp2 = gfc_class_len_get (class_expr);
    1116           31 :           gfc_add_modify (pre, tmp, tmp2);
    1117              :         }
    1118              :     }
    1119          134 :   else if (rhs_ss->info->data.array.descriptor)
    1120              :    {
    1121              :       /* lhs is class and rhs is intrinsic or derived type.  */
    1122          128 :       *eltype = TREE_TYPE (rhs_ss->info->data.array.descriptor);
    1123          128 :       *eltype = gfc_get_element_type (*eltype);
    1124          128 :       vtab = gfc_find_vtab (&rhs_ss->info->expr->ts);
    1125          128 :       vptr = vtab->backend_decl;
    1126          128 :       if (vptr == NULL_TREE)
    1127           24 :         vptr = gfc_get_symbol_decl (vtab);
    1128          128 :       vptr = gfc_build_addr_expr (NULL_TREE, vptr);
    1129          128 :       tmp = gfc_class_vptr_get (lhs_class_expr);
    1130          128 :       gfc_add_modify (pre, tmp,
    1131          128 :                       fold_convert (TREE_TYPE (tmp), vptr));
    1132              : 
    1133          128 :       if (unlimited_lhs)
    1134              :         {
    1135            0 :           tmp = gfc_class_len_get (lhs_class_expr);
    1136            0 :           if (rhs_ss->info
    1137            0 :               && rhs_ss->info->expr
    1138            0 :               && rhs_ss->info->expr->ts.type == BT_CHARACTER)
    1139            0 :             tmp2 = build_int_cst (TREE_TYPE (tmp),
    1140            0 :                                   rhs_ss->info->expr->ts.kind);
    1141              :           else
    1142            0 :             tmp2 = build_int_cst (TREE_TYPE (tmp), 0);
    1143            0 :           gfc_add_modify (pre, tmp, tmp2);
    1144              :         }
    1145              :     }
    1146              : 
    1147              :   return class_expr;
    1148              : }
    1149              : 
    1150              : 
    1151              : 
    1152              : /* Generate code to create and initialize the descriptor for a temporary
    1153              :    array.  This is used for both temporaries needed by the scalarizer, and
    1154              :    functions returning arrays.  Adjusts the loop variables to be
    1155              :    zero-based, and calculates the loop bounds for callee allocated arrays.
    1156              :    Allocate the array unless it's callee allocated (we have a callee
    1157              :    allocated array if 'callee_alloc' is true, or if loop->to[n] is
    1158              :    NULL_TREE for any n).  Also fills in the descriptor, data and offset
    1159              :    fields of info if known.  Returns the size of the array, or NULL for a
    1160              :    callee allocated array.
    1161              : 
    1162              :    'eltype' == NULL signals that the temporary should be a class object.
    1163              :    The 'initial' expression is used to obtain the size of the dynamic
    1164              :    type; otherwise the allocation and initialization proceeds as for any
    1165              :    other expression
    1166              : 
    1167              :    PRE, POST, INITIAL, DYNAMIC and DEALLOC are as for
    1168              :    gfc_trans_allocate_array_storage.  */
    1169              : 
    1170              : tree
    1171        28401 : gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, gfc_ss * ss,
    1172              :                              tree eltype, tree initial, bool dynamic,
    1173              :                              bool dealloc, bool callee_alloc, locus * where)
    1174              : {
    1175        28401 :   gfc_loopinfo *loop;
    1176        28401 :   gfc_ss *s;
    1177        28401 :   gfc_array_info *info;
    1178        28401 :   tree from[GFC_MAX_DIMENSIONS], to[GFC_MAX_DIMENSIONS];
    1179        28401 :   tree type;
    1180        28401 :   tree desc;
    1181        28401 :   tree tmp;
    1182        28401 :   tree size;
    1183        28401 :   tree nelem;
    1184        28401 :   tree cond;
    1185        28401 :   tree or_expr;
    1186        28401 :   tree elemsize;
    1187        28401 :   tree class_expr = NULL_TREE;
    1188        28401 :   gfc_ss *fcn_ss = NULL;
    1189        28401 :   int n, dim, tmp_dim;
    1190        28401 :   int total_dim = 0;
    1191              : 
    1192              :   /* This signals a class array for which we need the size of the
    1193              :      dynamic type.  Generate an eltype and then the class expression.  */
    1194        28401 :   if (eltype == NULL_TREE && initial)
    1195              :     {
    1196            0 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (initial)));
    1197            0 :       class_expr = build_fold_indirect_ref_loc (input_location, initial);
    1198              :       /* Obtain the structure (class) expression.  */
    1199            0 :       class_expr = gfc_get_class_from_expr (class_expr);
    1200            0 :       gcc_assert (class_expr);
    1201              :     }
    1202              : 
    1203              :   /* Otherwise, some expressions, such as class functions, arising from
    1204              :      dependency checking in assignments come here with class element type.
    1205              :      The descriptor can be obtained from the ss->info and then converted
    1206              :      to the class object.  */
    1207        28401 :   if (class_expr == NULL_TREE && GFC_CLASS_TYPE_P (eltype))
    1208          327 :     class_expr = get_class_info_from_ss (pre, ss, &eltype, &fcn_ss);
    1209              : 
    1210              :   /* If the dynamic type is not available, use the declared type.  */
    1211        28401 :   if (eltype && GFC_CLASS_TYPE_P (eltype))
    1212          199 :     eltype = gfc_get_element_type (TREE_TYPE (TYPE_FIELDS (eltype)));
    1213              : 
    1214        28401 :   if (class_expr == NULL_TREE)
    1215        28250 :     elemsize = fold_convert (gfc_array_index_type,
    1216              :                              TYPE_SIZE_UNIT (eltype));
    1217              :   else
    1218              :     {
    1219              :       /* Unlimited polymorphic entities are initialised with NULL vptr. They
    1220              :          can be tested for by checking if the len field is present. If so
    1221              :          test the vptr before using the vtable size.  */
    1222          151 :       tmp = gfc_class_vptr_get (class_expr);
    1223          151 :       tmp = fold_build2_loc (input_location, NE_EXPR,
    1224              :                              logical_type_node,
    1225          151 :                              tmp, build_int_cst (TREE_TYPE (tmp), 0));
    1226          151 :       elemsize = fold_build3_loc (input_location, COND_EXPR,
    1227              :                                   gfc_array_index_type,
    1228              :                                   tmp,
    1229              :                                   gfc_class_vtab_size_get (class_expr),
    1230              :                                   gfc_index_zero_node);
    1231          151 :       elemsize = gfc_evaluate_now (elemsize, pre);
    1232          151 :       elemsize = gfc_resize_class_size_with_len (pre, class_expr, elemsize);
    1233              :       /* Casting the data as a character of the dynamic length ensures that
    1234              :          assignment of elements works when needed.  */
    1235          151 :       eltype = gfc_get_character_type_len (1, elemsize);
    1236              :     }
    1237              : 
    1238        28401 :   memset (from, 0, sizeof (from));
    1239        28401 :   memset (to, 0, sizeof (to));
    1240              : 
    1241        28401 :   info = &ss->info->data.array;
    1242              : 
    1243        28401 :   gcc_assert (ss->dimen > 0);
    1244        28401 :   gcc_assert (ss->loop->dimen == ss->dimen);
    1245              : 
    1246        28401 :   if (warn_array_temporaries && where)
    1247          207 :     gfc_warning (OPT_Warray_temporaries,
    1248              :                  "Creating array temporary at %L", where);
    1249              : 
    1250              :   /* Set the lower bound to zero.  */
    1251        56837 :   for (s = ss; s; s = s->parent)
    1252              :     {
    1253        28436 :       loop = s->loop;
    1254              : 
    1255        28436 :       total_dim += loop->dimen;
    1256        66100 :       for (n = 0; n < loop->dimen; n++)
    1257              :         {
    1258        37664 :           dim = s->dim[n];
    1259              : 
    1260              :           /* Callee allocated arrays may not have a known bound yet.  */
    1261        37664 :           if (loop->to[n])
    1262        34269 :             loop->to[n] = gfc_evaluate_now (
    1263              :                         fold_build2_loc (input_location, MINUS_EXPR,
    1264              :                                          gfc_array_index_type,
    1265              :                                          loop->to[n], loop->from[n]),
    1266              :                         pre);
    1267        37664 :           loop->from[n] = gfc_index_zero_node;
    1268              : 
    1269              :           /* We have just changed the loop bounds, we must clear the
    1270              :              corresponding specloop, so that delta calculation is not skipped
    1271              :              later in gfc_set_delta.  */
    1272        37664 :           loop->specloop[n] = NULL;
    1273              : 
    1274              :           /* We are constructing the temporary's descriptor based on the loop
    1275              :              dimensions.  As the dimensions may be accessed in arbitrary order
    1276              :              (think of transpose) the size taken from the n'th loop may not map
    1277              :              to the n'th dimension of the array.  We need to reconstruct loop
    1278              :              infos in the right order before using it to set the descriptor
    1279              :              bounds.  */
    1280        37664 :           tmp_dim = get_scalarizer_dim_for_array_dim (ss, dim);
    1281        37664 :           from[tmp_dim] = loop->from[n];
    1282        37664 :           to[tmp_dim] = loop->to[n];
    1283              : 
    1284        37664 :           info->delta[dim] = gfc_index_zero_node;
    1285        37664 :           info->start[dim] = gfc_index_zero_node;
    1286        37664 :           info->end[dim] = gfc_index_zero_node;
    1287        37664 :           info->stride[dim] = gfc_index_one_node;
    1288              :         }
    1289              :     }
    1290              : 
    1291              :   /* Initialize the descriptor.  */
    1292        28401 :   type =
    1293        28401 :     gfc_get_array_type_bounds (eltype, total_dim, 0, from, to, 1,
    1294              :                                GFC_ARRAY_UNKNOWN, true);
    1295        28401 :   desc = gfc_create_var (type, "atmp");
    1296        28401 :   GFC_DECL_PACKED_ARRAY (desc) = 1;
    1297              : 
    1298              :   /* Emit a DECL_EXPR for the variable sized array type in
    1299              :      GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
    1300              :      sizes works correctly.  */
    1301        28401 :   tree arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (type));
    1302        28401 :   if (! TYPE_NAME (arraytype))
    1303        28401 :     TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
    1304              :                                         NULL_TREE, arraytype);
    1305        28401 :   gfc_add_expr_to_block (pre, build1 (DECL_EXPR,
    1306        28401 :                                       arraytype, TYPE_NAME (arraytype)));
    1307              : 
    1308        28401 :   if (fcn_ss && fcn_ss->info && fcn_ss->info->class_container)
    1309              :     {
    1310           90 :       suppress_warning (desc);
    1311           90 :       TREE_USED (desc) = 0;
    1312              :     }
    1313              : 
    1314        28401 :   if (class_expr != NULL_TREE
    1315        28250 :       || (fcn_ss && fcn_ss->info && fcn_ss->info->class_container))
    1316              :     {
    1317          181 :       tree class_data;
    1318          181 :       tree dtype;
    1319          181 :       gfc_expr *expr1 = fcn_ss ? fcn_ss->info->expr : NULL;
    1320          181 :       bool rank_changer;
    1321              : 
    1322              :       /* Pick out these transformational functions because they change the rank
    1323              :          or shape of the first argument. This requires that the class type be
    1324              :          changed, the dtype updated and the correct rank used.  */
    1325          121 :       rank_changer = expr1 && expr1->expr_type == EXPR_FUNCTION
    1326          121 :                      && expr1->value.function.isym
    1327          271 :                      && (expr1->value.function.isym->id == GFC_ISYM_RESHAPE
    1328              :                          || expr1->value.function.isym->id == GFC_ISYM_SPREAD
    1329              :                          || expr1->value.function.isym->id == GFC_ISYM_PACK
    1330              :                          || expr1->value.function.isym->id == GFC_ISYM_UNPACK);
    1331              : 
    1332              :       /* Create a class temporary for the result using the lhs class object.  */
    1333          181 :       if (class_expr != NULL_TREE && !rank_changer)
    1334              :         {
    1335          103 :           tmp = gfc_create_var (TREE_TYPE (class_expr), "ctmp");
    1336          103 :           gfc_add_modify (pre, tmp, class_expr);
    1337              :         }
    1338              :       else
    1339              :         {
    1340           78 :           tree vptr;
    1341           78 :           class_expr = fcn_ss->info->class_container;
    1342           78 :           gcc_assert (expr1);
    1343              : 
    1344              :           /* Build a new class container using the arg1 class object. The class
    1345              :              typespec must be rebuilt because the rank might have changed.  */
    1346           78 :           gfc_typespec ts = CLASS_DATA (expr1)->ts;
    1347           78 :           symbol_attribute attr = CLASS_DATA (expr1)->attr;
    1348           78 :           gfc_change_class (&ts, &attr, NULL, expr1->rank, 0);
    1349           78 :           tmp = gfc_create_var (gfc_typenode_for_spec (&ts), "ctmp");
    1350           78 :           fcn_ss->info->class_container = tmp;
    1351              : 
    1352              :           /* Set the vptr and obtain the element size.  */
    1353           78 :           vptr = gfc_class_vptr_get (tmp);
    1354          156 :           gfc_add_modify (pre, vptr,
    1355           78 :                           fold_convert (TREE_TYPE (vptr),
    1356              :                                         gfc_class_vptr_get (class_expr)));
    1357           78 :           elemsize = gfc_class_vtab_size_get (class_expr);
    1358              : 
    1359              :           /* Set the _len field, if necessary.  */
    1360           78 :           if (UNLIMITED_POLY (expr1))
    1361              :             {
    1362           18 :               gfc_add_modify (pre, gfc_class_len_get (tmp),
    1363              :                               gfc_class_len_get (class_expr));
    1364           18 :               elemsize = gfc_resize_class_size_with_len (pre, class_expr,
    1365              :                                                          elemsize);
    1366              :             }
    1367              : 
    1368           78 :           elemsize = gfc_evaluate_now (elemsize, pre);
    1369              :         }
    1370              : 
    1371          181 :       class_data = gfc_class_data_get (tmp);
    1372              : 
    1373          181 :       if (rank_changer)
    1374              :         {
    1375              :           /* Take the dtype from the class expression.  */
    1376           72 :           tree class_descr = gfc_class_data_get (class_expr);
    1377           72 :           dtype = gfc_conv_descriptor_dtype_get (class_descr);
    1378           72 :           gfc_conv_descriptor_dtype_set (pre, desc, dtype);
    1379              : 
    1380              :           /* These transformational functions change the rank.  */
    1381           72 :           gfc_conv_descriptor_rank_set (pre, desc, ss->loop->dimen);
    1382           72 :           fcn_ss->info->class_container = NULL_TREE;
    1383              :         }
    1384              : 
    1385              :       /* Assign the new descriptor to the _data field. This allows the
    1386              :          vptr _copy to be used for scalarized assignment since the class
    1387              :          temporary can be found from the descriptor.  */
    1388          181 :       tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1389          181 :                              TREE_TYPE (desc), desc);
    1390          181 :       gfc_add_modify (pre, class_data, tmp);
    1391              : 
    1392              :       /* Point desc to the class _data field.  */
    1393          181 :       desc = class_data;
    1394          181 :     }
    1395              :   else
    1396              :     {
    1397              :       /* Fill in the array dtype.  */
    1398        28220 :       gfc_conv_descriptor_dtype_set (pre, desc,
    1399        28220 :                                      gfc_get_dtype (TREE_TYPE (desc)));
    1400              :     }
    1401              : 
    1402        28401 :   info->descriptor = desc;
    1403        28401 :   size = gfc_index_one_node;
    1404              : 
    1405              :   /*
    1406              :      Fill in the bounds and stride.  This is a packed array, so:
    1407              : 
    1408              :      size = 1;
    1409              :      for (n = 0; n < rank; n++)
    1410              :        {
    1411              :          stride[n] = size
    1412              :          delta = ubound[n] + 1 - lbound[n];
    1413              :          size = size * delta;
    1414              :        }
    1415              :      size = size * sizeof(element);
    1416              :   */
    1417              : 
    1418        28401 :   or_expr = NULL_TREE;
    1419              : 
    1420              :   /* If there is at least one null loop->to[n], it is a callee allocated
    1421              :      array.  */
    1422        62670 :   for (n = 0; n < total_dim; n++)
    1423        36316 :     if (to[n] == NULL_TREE)
    1424              :       {
    1425              :         size = NULL_TREE;
    1426              :         break;
    1427              :       }
    1428              : 
    1429        28401 :   if (size == NULL_TREE)
    1430         4104 :     for (s = ss; s; s = s->parent)
    1431         5457 :       for (n = 0; n < s->loop->dimen; n++)
    1432              :         {
    1433         3400 :           dim = get_scalarizer_dim_for_array_dim (ss, s->dim[n]);
    1434              : 
    1435              :           /* For a callee allocated array express the loop bounds in terms
    1436              :              of the descriptor fields.  */
    1437         3400 :           tmp = fold_build2_loc (input_location,
    1438              :                 MINUS_EXPR, gfc_array_index_type,
    1439              :                 gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]),
    1440              :                 gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]));
    1441         3400 :           s->loop->to[n] = tmp;
    1442              :         }
    1443              :   else
    1444              :     {
    1445        60618 :       for (n = 0; n < total_dim; n++)
    1446              :         {
    1447              :           /* Store the stride and bound components in the descriptor.  */
    1448        34264 :           gfc_conv_descriptor_stride_set (pre, desc, gfc_rank_cst[n], size);
    1449              : 
    1450        34264 :           gfc_conv_descriptor_lbound_set (pre, desc, gfc_rank_cst[n],
    1451              :                                           gfc_index_zero_node);
    1452              : 
    1453        34264 :           gfc_conv_descriptor_ubound_set (pre, desc, gfc_rank_cst[n], to[n]);
    1454              : 
    1455        34264 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    1456              :                                  gfc_array_index_type,
    1457              :                                  to[n], gfc_index_one_node);
    1458              : 
    1459              :           /* Check whether the size for this dimension is negative.  */
    1460        34264 :           cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    1461              :                                   tmp, gfc_index_zero_node);
    1462        34264 :           cond = gfc_evaluate_now (cond, pre);
    1463              : 
    1464        34264 :           if (n == 0)
    1465              :             or_expr = cond;
    1466              :           else
    1467         7910 :             or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    1468              :                                        logical_type_node, or_expr, cond);
    1469              : 
    1470        34264 :           size = fold_build2_loc (input_location, MULT_EXPR,
    1471              :                                   gfc_array_index_type, size, tmp);
    1472        34264 :           size = gfc_evaluate_now (size, pre);
    1473              :         }
    1474              :     }
    1475              : 
    1476              :   /* Get the size of the array.  */
    1477        28401 :   if (size && !callee_alloc)
    1478              :     {
    1479              :       /* If or_expr is true, then the extent in at least one
    1480              :          dimension is zero and the size is set to zero.  */
    1481        26164 :       size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    1482              :                               or_expr, gfc_index_zero_node, size);
    1483              : 
    1484        26164 :       nelem = size;
    1485        26164 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    1486              :                               size, elemsize);
    1487              :     }
    1488              :   else
    1489              :     {
    1490              :       nelem = size;
    1491              :       size = NULL_TREE;
    1492              :     }
    1493              : 
    1494              :   /* Set the span.  */
    1495        28401 :   tmp = fold_convert (gfc_array_index_type, elemsize);
    1496        28401 :   gfc_conv_descriptor_span_set (pre, desc, tmp);
    1497              : 
    1498        28401 :   gfc_trans_allocate_array_storage (pre, post, info, size, nelem, initial,
    1499              :                                     dynamic, dealloc);
    1500              : 
    1501        56837 :   while (ss->parent)
    1502              :     ss = ss->parent;
    1503              : 
    1504        28401 :   if (ss->dimen > ss->loop->temp_dim)
    1505        24534 :     ss->loop->temp_dim = ss->dimen;
    1506              : 
    1507        28401 :   return size;
    1508              : }
    1509              : 
    1510              : 
    1511              : /* Return the number of iterations in a loop that starts at START,
    1512              :    ends at END, and has step STEP.  */
    1513              : 
    1514              : static tree
    1515         1078 : gfc_get_iteration_count (tree start, tree end, tree step)
    1516              : {
    1517         1078 :   tree tmp;
    1518         1078 :   tree type;
    1519              : 
    1520         1078 :   type = TREE_TYPE (step);
    1521         1078 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, type, end, start);
    1522         1078 :   tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, tmp, step);
    1523         1078 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp,
    1524              :                          build_int_cst (type, 1));
    1525         1078 :   tmp = fold_build2_loc (input_location, MAX_EXPR, type, tmp,
    1526              :                          build_int_cst (type, 0));
    1527         1078 :   return fold_convert (gfc_array_index_type, tmp);
    1528              : }
    1529              : 
    1530              : 
    1531              : /* Return true if the bounds of iterator I can only be determined
    1532              :    at run time.  */
    1533              : 
    1534              : static inline bool
    1535         2363 : gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
    1536              : {
    1537         2363 :   return (i->start->expr_type != EXPR_CONSTANT
    1538         1945 :           || i->end->expr_type != EXPR_CONSTANT
    1539         2536 :           || i->step->expr_type != EXPR_CONSTANT);
    1540              : }
    1541              : 
    1542              : 
    1543              : /* Split the size of constructor element EXPR into the sum of two terms,
    1544              :    one of which can be determined at compile time and one of which must
    1545              :    be calculated at run time.  Set *SIZE to the former and return true
    1546              :    if the latter might be nonzero.  */
    1547              : 
    1548              : static bool
    1549         3290 : gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
    1550              : {
    1551         3290 :   if (expr->expr_type == EXPR_ARRAY)
    1552          685 :     return gfc_get_array_constructor_size (size, expr->value.constructor);
    1553         2605 :   else if (expr->rank > 0)
    1554              :     {
    1555              :       /* Calculate everything at run time.  */
    1556         1031 :       mpz_set_ui (*size, 0);
    1557         1031 :       return true;
    1558              :     }
    1559              :   else
    1560              :     {
    1561              :       /* A single element.  */
    1562         1574 :       mpz_set_ui (*size, 1);
    1563         1574 :       return false;
    1564              :     }
    1565              : }
    1566              : 
    1567              : 
    1568              : /* Like gfc_get_array_constructor_element_size, but applied to the whole
    1569              :    of array constructor C.  */
    1570              : 
    1571              : static bool
    1572         3030 : gfc_get_array_constructor_size (mpz_t * size, gfc_constructor_base base)
    1573              : {
    1574         3030 :   gfc_constructor *c;
    1575         3030 :   gfc_iterator *i;
    1576         3030 :   mpz_t val;
    1577         3030 :   mpz_t len;
    1578         3030 :   bool dynamic;
    1579              : 
    1580         3030 :   mpz_set_ui (*size, 0);
    1581         3030 :   mpz_init (len);
    1582         3030 :   mpz_init (val);
    1583              : 
    1584         3030 :   dynamic = false;
    1585         7408 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1586              :     {
    1587         4378 :       i = c->iterator;
    1588         4378 :       if (i && gfc_iterator_has_dynamic_bounds (i))
    1589              :         dynamic = true;
    1590              :       else
    1591              :         {
    1592         2739 :           dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
    1593         2739 :           if (i)
    1594              :             {
    1595              :               /* Multiply the static part of the element size by the
    1596              :                  number of iterations.  */
    1597          128 :               mpz_sub (val, i->end->value.integer, i->start->value.integer);
    1598          128 :               mpz_fdiv_q (val, val, i->step->value.integer);
    1599          128 :               mpz_add_ui (val, val, 1);
    1600          128 :               if (mpz_sgn (val) > 0)
    1601           92 :                 mpz_mul (len, len, val);
    1602              :               else
    1603           36 :                 mpz_set_ui (len, 0);
    1604              :             }
    1605         2739 :           mpz_add (*size, *size, len);
    1606              :         }
    1607              :     }
    1608         3030 :   mpz_clear (len);
    1609         3030 :   mpz_clear (val);
    1610         3030 :   return dynamic;
    1611              : }
    1612              : 
    1613              : 
    1614              : /* Make sure offset is a variable.  */
    1615              : 
    1616              : static void
    1617         3329 : gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
    1618              :                          tree * offsetvar)
    1619              : {
    1620              :   /* We should have already created the offset variable.  We cannot
    1621              :      create it here because we may be in an inner scope.  */
    1622         3329 :   gcc_assert (*offsetvar != NULL_TREE);
    1623         3329 :   gfc_add_modify (pblock, *offsetvar, *poffset);
    1624         3329 :   *poffset = *offsetvar;
    1625         3329 :   TREE_USED (*offsetvar) = 1;
    1626         3329 : }
    1627              : 
    1628              : 
    1629              : /* Variables needed for bounds-checking.  */
    1630              : static bool first_len;
    1631              : static tree first_len_val;
    1632              : static bool typespec_chararray_ctor;
    1633              : 
    1634              : /* Return true if DER has any CLASS allocatable component.  Such components
    1635              :    are initialised by VIEW_CONVERT in structure constructors (a bitwise copy
    1636              :    of the class descriptor), so their _data pointer may refer to a non-heap
    1637              :    object and must not be passed to gfc_deallocate_alloc_comp_no_caf.  */
    1638              : 
    1639              : static bool
    1640         4728 : has_class_alloc_comp (gfc_symbol *der)
    1641              : {
    1642        12703 :   for (gfc_component *c = der->components; c; c = c->next)
    1643         8041 :     if (c->ts.type == BT_CLASS && !c->attr.class_pointer)
    1644              :       return true;
    1645              :   return false;
    1646              : }
    1647              : 
    1648              : static void
    1649        12908 : gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
    1650              :                               tree offset, gfc_se * se, gfc_expr * expr)
    1651              : {
    1652        12908 :   tree tmp, offset_eval;
    1653              : 
    1654        12908 :   gfc_conv_expr (se, expr);
    1655              : 
    1656              :   /* Store the value.  */
    1657        12908 :   tmp = build_fold_indirect_ref_loc (input_location,
    1658              :                                  gfc_conv_descriptor_data_get (desc));
    1659              : 
    1660              :   /* The offset may change, so get its value now and use that to free memory.  */
    1661        12908 :   offset_eval = gfc_evaluate_now (offset, &se->pre);
    1662        12908 :   tmp = gfc_build_array_ref (tmp, offset_eval, NULL);
    1663              : 
    1664        12908 :   if (expr->ts.type == BT_DERIVED
    1665         4637 :       && (expr->expr_type == EXPR_FUNCTION
    1666         4553 :           || (expr->expr_type == EXPR_STRUCTURE
    1667         3949 :               && !has_class_alloc_comp (expr->ts.u.derived)))
    1668        16899 :       && expr->ts.u.derived->attr.alloc_comp)
    1669          800 :     gfc_add_expr_to_block (&se->finalblock,
    1670              :                            gfc_deallocate_alloc_comp_no_caf (expr->ts.u.derived,
    1671              :                                                              tmp, expr->rank,
    1672              :                                                              true));
    1673              : 
    1674        12908 :   if (expr->ts.type == BT_CHARACTER)
    1675              :     {
    1676         2154 :       int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
    1677         2154 :       tree esize;
    1678              : 
    1679         2154 :       esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
    1680         2154 :       esize = fold_convert (gfc_charlen_type_node, esize);
    1681         4308 :       esize = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    1682         2154 :                                TREE_TYPE (esize), esize,
    1683         2154 :                                build_int_cst (TREE_TYPE (esize),
    1684         2154 :                                           gfc_character_kinds[i].bit_size / 8));
    1685              : 
    1686         2154 :       gfc_conv_string_parameter (se);
    1687         2154 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    1688              :         {
    1689              :           /* The temporary is an array of pointers.  */
    1690            6 :           se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1691            6 :           gfc_add_modify (&se->pre, tmp, se->expr);
    1692              :         }
    1693              :       else
    1694              :         {
    1695              :           /* The temporary is an array of string values.  */
    1696         2148 :           tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
    1697              :           /* We know the temporary and the value will be the same length,
    1698              :              so can use memcpy.  */
    1699         2148 :           gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
    1700              :                                  se->string_length, se->expr, expr->ts.kind);
    1701              :         }
    1702         2154 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !typespec_chararray_ctor)
    1703              :         {
    1704          310 :           if (first_len)
    1705              :             {
    1706          130 :               gfc_add_modify (&se->pre, first_len_val,
    1707          130 :                               fold_convert (TREE_TYPE (first_len_val),
    1708              :                                             se->string_length));
    1709          130 :               first_len = false;
    1710              :             }
    1711              :           else
    1712              :             {
    1713              :               /* Verify that all constructor elements are of the same
    1714              :                  length.  */
    1715          180 :               tree rhs = fold_convert (TREE_TYPE (first_len_val),
    1716              :                                        se->string_length);
    1717          180 :               tree cond = fold_build2_loc (input_location, NE_EXPR,
    1718              :                                            logical_type_node, first_len_val,
    1719              :                                            rhs);
    1720          180 :               gfc_trans_runtime_check
    1721          180 :                 (true, false, cond, &se->pre, &expr->where,
    1722              :                  "Different CHARACTER lengths (%ld/%ld) in array constructor",
    1723              :                  fold_convert (long_integer_type_node, first_len_val),
    1724              :                  fold_convert (long_integer_type_node, se->string_length));
    1725              :             }
    1726              :         }
    1727              :     }
    1728        10754 :   else if (GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
    1729        10754 :            && !GFC_CLASS_TYPE_P (gfc_get_element_type (TREE_TYPE (desc))))
    1730              :     {
    1731              :       /* Assignment of a CLASS array constructor to a derived type array.  */
    1732           24 :       if (expr->expr_type == EXPR_FUNCTION)
    1733           18 :         se->expr = gfc_evaluate_now (se->expr, pblock);
    1734           24 :       se->expr = gfc_class_data_get (se->expr);
    1735           24 :       se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    1736           24 :       se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1737           24 :       gfc_add_modify (&se->pre, tmp, se->expr);
    1738              :     }
    1739              :   else
    1740              :     {
    1741              :       /* TODO: Should the frontend already have done this conversion?  */
    1742        10730 :       se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
    1743        10730 :       gfc_add_modify (&se->pre, tmp, se->expr);
    1744              :     }
    1745              : 
    1746        12908 :   gfc_add_block_to_block (pblock, &se->pre);
    1747        12908 :   gfc_add_block_to_block (pblock, &se->post);
    1748        12908 : }
    1749              : 
    1750              : 
    1751              : /* Add the contents of an array to the constructor.  DYNAMIC is as for
    1752              :    gfc_trans_array_constructor_value.  */
    1753              : 
    1754              : static void
    1755         1141 : gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
    1756              :                                       tree type ATTRIBUTE_UNUSED,
    1757              :                                       tree desc, gfc_expr * expr,
    1758              :                                       tree * poffset, tree * offsetvar,
    1759              :                                       bool dynamic)
    1760              : {
    1761         1141 :   gfc_se se;
    1762         1141 :   gfc_ss *ss;
    1763         1141 :   gfc_loopinfo loop;
    1764         1141 :   stmtblock_t body;
    1765         1141 :   tree tmp;
    1766         1141 :   tree size;
    1767         1141 :   int n;
    1768              : 
    1769              :   /* We need this to be a variable so we can increment it.  */
    1770         1141 :   gfc_put_offset_into_var (pblock, poffset, offsetvar);
    1771              : 
    1772         1141 :   gfc_init_se (&se, NULL);
    1773              : 
    1774              :   /* Walk the array expression.  */
    1775         1141 :   ss = gfc_walk_expr (expr);
    1776         1141 :   gcc_assert (ss != gfc_ss_terminator);
    1777              : 
    1778              :   /* Initialize the scalarizer.  */
    1779         1141 :   gfc_init_loopinfo (&loop);
    1780         1141 :   gfc_add_ss_to_loop (&loop, ss);
    1781              : 
    1782              :   /* Initialize the loop.  */
    1783         1141 :   gfc_conv_ss_startstride (&loop);
    1784         1141 :   gfc_conv_loop_setup (&loop, &expr->where);
    1785              : 
    1786              :   /* Make sure the constructed array has room for the new data.  */
    1787         1141 :   if (dynamic)
    1788              :     {
    1789              :       /* Set SIZE to the total number of elements in the subarray.  */
    1790          515 :       size = gfc_index_one_node;
    1791         1042 :       for (n = 0; n < loop.dimen; n++)
    1792              :         {
    1793          527 :           tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
    1794              :                                          gfc_index_one_node);
    1795          527 :           size = fold_build2_loc (input_location, MULT_EXPR,
    1796              :                                   gfc_array_index_type, size, tmp);
    1797              :         }
    1798              : 
    1799              :       /* Grow the constructed array by SIZE elements.  */
    1800          515 :       gfc_grow_array (&loop.pre, desc, size);
    1801              :     }
    1802              : 
    1803              :   /* Make the loop body.  */
    1804         1141 :   gfc_mark_ss_chain_used (ss, 1);
    1805         1141 :   gfc_start_scalarized_body (&loop, &body);
    1806         1141 :   gfc_copy_loopinfo_to_se (&se, &loop);
    1807         1141 :   se.ss = ss;
    1808              : 
    1809         1141 :   gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
    1810         1141 :   gcc_assert (se.ss == gfc_ss_terminator);
    1811              : 
    1812              :   /* Increment the offset.  */
    1813         1141 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    1814              :                          *poffset, gfc_index_one_node);
    1815         1141 :   gfc_add_modify (&body, *poffset, tmp);
    1816              : 
    1817              :   /* Finish the loop.  */
    1818         1141 :   gfc_trans_scalarizing_loops (&loop, &body);
    1819         1141 :   gfc_add_block_to_block (&loop.pre, &loop.post);
    1820         1141 :   tmp = gfc_finish_block (&loop.pre);
    1821         1141 :   gfc_add_expr_to_block (pblock, tmp);
    1822              : 
    1823         1141 :   gfc_cleanup_loop (&loop);
    1824         1141 : }
    1825              : 
    1826              : 
    1827              : /* Return true if every leaf element of an array constructor is a function
    1828              :    reference returning derived type DER, which has allocatable components.
    1829              :    Such results are moved (shallow-copied) into the constructor temporary, so
    1830              :    the temporary owns their allocatable components and they can all be freed
    1831              :    in a single sweep over the whole temporary.  Returns false as soon as an
    1832              :    element is anything else - notably a variable, whose allocatable components
    1833              :    are aliased rather than owned by the temporary and must not be freed.  */
    1834              : 
    1835              : static bool
    1836          521 : gfc_constructor_is_owned_alloc_comp (gfc_constructor_base base,
    1837              :                                      gfc_symbol *der)
    1838              : {
    1839          521 :   gfc_constructor *c;
    1840              : 
    1841          521 :   if (base == NULL)
    1842              :     return false;
    1843              : 
    1844         1369 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1845              :     {
    1846         1065 :       gfc_expr *e = c->expr;
    1847         1065 :       if (e->expr_type == EXPR_ARRAY)
    1848              :         {
    1849           54 :           if (!gfc_constructor_is_owned_alloc_comp (e->value.constructor, der))
    1850              :             return false;
    1851              :         }
    1852         1011 :       else if (!(e->ts.type == BT_DERIVED
    1853         1011 :                  && (e->expr_type == EXPR_FUNCTION
    1854          972 :                      || (e->expr_type == EXPR_STRUCTURE
    1855          779 :                          && !has_class_alloc_comp (e->ts.u.derived)))
    1856          794 :                  && e->ts.u.derived == der))
    1857              :         return false;
    1858              :     }
    1859              :   return true;
    1860              : }
    1861              : 
    1862              : 
    1863              : /* Assign the values to the elements of an array constructor.  DYNAMIC
    1864              :    is true if descriptor DESC only contains enough data for the static
    1865              :    size calculated by gfc_get_array_constructor_size.  When true, memory
    1866              :    for the dynamic parts must be allocated using realloc.  OWNED_SWEEP is
    1867              :    true when the caller will free the allocatable components of every
    1868              :    constructor element in one sweep over the whole temporary; in that case
    1869              :    the per-element finalization built here is suppressed to avoid a double
    1870              :    free.  */
    1871              : 
    1872              : static void
    1873         8449 : gfc_trans_array_constructor_value (stmtblock_t * pblock,
    1874              :                                    stmtblock_t * finalblock,
    1875              :                                    tree type, tree desc,
    1876              :                                    gfc_constructor_base base, tree * poffset,
    1877              :                                    tree * offsetvar, bool dynamic,
    1878              :                                    bool owned_sweep)
    1879              : {
    1880         8449 :   tree tmp;
    1881         8449 :   tree start = NULL_TREE;
    1882         8449 :   tree end = NULL_TREE;
    1883         8449 :   tree step = NULL_TREE;
    1884         8449 :   stmtblock_t body;
    1885         8449 :   gfc_se se;
    1886         8449 :   mpz_t size;
    1887         8449 :   gfc_constructor *c;
    1888         8449 :   gfc_typespec ts;
    1889         8449 :   int ctr = 0;
    1890              : 
    1891         8449 :   tree shadow_loopvar = NULL_TREE;
    1892         8449 :   gfc_saved_var saved_loopvar;
    1893              : 
    1894         8449 :   ts.type = BT_UNKNOWN;
    1895         8449 :   mpz_init (size);
    1896        22973 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1897              :     {
    1898        14524 :       ctr++;
    1899              :       /* If this is an iterator or an array, the offset must be a variable.  */
    1900        14524 :       if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
    1901         2188 :         gfc_put_offset_into_var (pblock, poffset, offsetvar);
    1902              : 
    1903              :       /* Shadowing the iterator avoids changing its value and saves us from
    1904              :          keeping track of it. Further, it makes sure that there's always a
    1905              :          backend-decl for the symbol, even if there wasn't one before,
    1906              :          e.g. in the case of an iterator that appears in a specification
    1907              :          expression in an interface mapping.  */
    1908        14524 :       if (c->iterator)
    1909              :         {
    1910         1489 :           gfc_symbol *sym;
    1911         1489 :           tree type;
    1912              : 
    1913              :           /* Evaluate loop bounds before substituting the loop variable
    1914              :              in case they depend on it.  Such a case is invalid, but it is
    1915              :              not more expensive to do the right thing here.
    1916              :              See PR 44354.  */
    1917         1489 :           gfc_init_se (&se, NULL);
    1918         1489 :           gfc_conv_expr_val (&se, c->iterator->start);
    1919         1489 :           gfc_add_block_to_block (pblock, &se.pre);
    1920         1489 :           start = gfc_evaluate_now (se.expr, pblock);
    1921              : 
    1922         1489 :           gfc_init_se (&se, NULL);
    1923         1489 :           gfc_conv_expr_val (&se, c->iterator->end);
    1924         1489 :           gfc_add_block_to_block (pblock, &se.pre);
    1925         1489 :           end = gfc_evaluate_now (se.expr, pblock);
    1926              : 
    1927         1489 :           gfc_init_se (&se, NULL);
    1928         1489 :           gfc_conv_expr_val (&se, c->iterator->step);
    1929         1489 :           gfc_add_block_to_block (pblock, &se.pre);
    1930         1489 :           step = gfc_evaluate_now (se.expr, pblock);
    1931              : 
    1932         1489 :           sym = c->iterator->var->symtree->n.sym;
    1933         1489 :           type = gfc_typenode_for_spec (&sym->ts);
    1934              : 
    1935         1489 :           shadow_loopvar = gfc_create_var (type, "shadow_loopvar");
    1936         1489 :           gfc_shadow_sym (sym, shadow_loopvar, &saved_loopvar);
    1937              :         }
    1938              : 
    1939        14524 :       gfc_start_block (&body);
    1940              : 
    1941        14524 :       if (c->expr->expr_type == EXPR_ARRAY)
    1942              :         {
    1943              :           /* Array constructors can be nested.  */
    1944         1511 :           gfc_trans_array_constructor_value (&body, finalblock, type,
    1945              :                                              desc, c->expr->value.constructor,
    1946              :                                              poffset, offsetvar, dynamic,
    1947              :                                              owned_sweep);
    1948              :         }
    1949        13013 :       else if (c->expr->rank > 0)
    1950              :         {
    1951         1141 :           gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
    1952              :                                                 poffset, offsetvar, dynamic);
    1953              :         }
    1954              :       else
    1955              :         {
    1956              :           /* This code really upsets the gimplifier so don't bother for now.  */
    1957              :           gfc_constructor *p;
    1958              :           HOST_WIDE_INT n;
    1959              :           HOST_WIDE_INT size;
    1960              : 
    1961              :           p = c;
    1962              :           n = 0;
    1963        13680 :           while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
    1964              :             {
    1965         1808 :               p = gfc_constructor_next (p);
    1966         1808 :               n++;
    1967              :             }
    1968              :           /* Constructor with few constant elements, or element size not
    1969              :              known at compile time (e.g. deferred-length character).  */
    1970        11872 :           if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type)))
    1971              :             {
    1972              :               /* Scalar values.  */
    1973        11767 :               gfc_init_se (&se, NULL);
    1974        11767 :               if (IS_PDT (c->expr) && c->expr->expr_type == EXPR_STRUCTURE)
    1975          276 :                 c->expr->must_finalize = 1;
    1976              : 
    1977        11767 :               gfc_trans_array_ctor_element (&body, desc, *poffset,
    1978              :                                             &se, c->expr);
    1979              : 
    1980        11767 :               *poffset = fold_build2_loc (input_location, PLUS_EXPR,
    1981              :                                           gfc_array_index_type,
    1982              :                                           *poffset, gfc_index_one_node);
    1983              :               /* Unless the whole temporary is being swept by the caller, add
    1984              :                  the per-element finalization.  The sweep is used when every
    1985              :                  element is an owned function result, which is the only way to
    1986              :                  correctly free elements produced inside an implied-do loop.  */
    1987        11767 :               if (finalblock && !owned_sweep)
    1988          496 :                 gfc_add_block_to_block (finalblock, &se.finalblock);
    1989              :             }
    1990              :           else
    1991              :             {
    1992              :               /* Collect multiple scalar constants into a constructor.  */
    1993          105 :               vec<constructor_elt, va_gc> *v = NULL;
    1994          105 :               tree init;
    1995          105 :               tree bound;
    1996          105 :               tree tmptype;
    1997          105 :               HOST_WIDE_INT idx = 0;
    1998              : 
    1999          105 :               p = c;
    2000              :               /* Count the number of consecutive scalar constants.  */
    2001          837 :               while (p && !(p->iterator
    2002          745 :                             || p->expr->expr_type != EXPR_CONSTANT))
    2003              :                 {
    2004          732 :                   gfc_init_se (&se, NULL);
    2005          732 :                   gfc_conv_constant (&se, p->expr);
    2006              : 
    2007          732 :                   if (c->expr->ts.type != BT_CHARACTER)
    2008          660 :                     se.expr = fold_convert (type, se.expr);
    2009              :                   /* For constant character array constructors we build
    2010              :                      an array of pointers.  */
    2011           72 :                   else if (POINTER_TYPE_P (type))
    2012            0 :                     se.expr = gfc_build_addr_expr
    2013            0 :                                 (gfc_get_pchar_type (p->expr->ts.kind),
    2014              :                                  se.expr);
    2015              : 
    2016          732 :                   CONSTRUCTOR_APPEND_ELT (v,
    2017              :                                           build_int_cst (gfc_array_index_type,
    2018              :                                                          idx++),
    2019              :                                           se.expr);
    2020          732 :                   c = p;
    2021          732 :                   p = gfc_constructor_next (p);
    2022              :                 }
    2023              : 
    2024          105 :               bound = size_int (n - 1);
    2025              :               /* Create an array type to hold them.  */
    2026          105 :               tmptype = build_range_type (gfc_array_index_type,
    2027              :                                           gfc_index_zero_node, bound);
    2028          105 :               tmptype = build_array_type (type, tmptype);
    2029              : 
    2030          105 :               init = build_constructor (tmptype, v);
    2031          105 :               TREE_CONSTANT (init) = 1;
    2032          105 :               TREE_STATIC (init) = 1;
    2033              :               /* Create a static variable to hold the data.  */
    2034          105 :               tmp = gfc_create_var (tmptype, "data");
    2035          105 :               TREE_STATIC (tmp) = 1;
    2036          105 :               TREE_CONSTANT (tmp) = 1;
    2037          105 :               TREE_READONLY (tmp) = 1;
    2038          105 :               DECL_INITIAL (tmp) = init;
    2039          105 :               init = tmp;
    2040              : 
    2041              :               /* Use BUILTIN_MEMCPY to assign the values.  */
    2042          105 :               tmp = gfc_conv_descriptor_data_get (desc);
    2043          105 :               tmp = build_fold_indirect_ref_loc (input_location,
    2044              :                                              tmp);
    2045          105 :               tmp = gfc_build_array_ref (tmp, *poffset, NULL);
    2046          105 :               tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    2047          105 :               init = gfc_build_addr_expr (NULL_TREE, init);
    2048              : 
    2049          105 :               size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
    2050          105 :               bound = build_int_cst (size_type_node, n * size);
    2051          105 :               tmp = build_call_expr_loc (input_location,
    2052              :                                          builtin_decl_explicit (BUILT_IN_MEMCPY),
    2053              :                                          3, tmp, init, bound);
    2054          105 :               gfc_add_expr_to_block (&body, tmp);
    2055              : 
    2056          105 :               *poffset = fold_build2_loc (input_location, PLUS_EXPR,
    2057              :                                       gfc_array_index_type, *poffset,
    2058          105 :                                       build_int_cst (gfc_array_index_type, n));
    2059              :             }
    2060        11872 :           if (!INTEGER_CST_P (*poffset))
    2061              :             {
    2062         1791 :               gfc_add_modify (&body, *offsetvar, *poffset);
    2063         1791 :               *poffset = *offsetvar;
    2064              :             }
    2065              : 
    2066        11872 :           if (!c->iterator)
    2067        11872 :             ts = c->expr->ts;
    2068              :         }
    2069              : 
    2070              :       /* The frontend should already have done any expansions
    2071              :          at compile-time.  */
    2072        14524 :       if (!c->iterator)
    2073              :         {
    2074              :           /* Pass the code as is.  */
    2075        13035 :           tmp = gfc_finish_block (&body);
    2076        13035 :           gfc_add_expr_to_block (pblock, tmp);
    2077              :         }
    2078              :       else
    2079              :         {
    2080              :           /* Build the implied do-loop.  */
    2081         1489 :           stmtblock_t implied_do_block;
    2082         1489 :           tree cond;
    2083         1489 :           tree exit_label;
    2084         1489 :           tree loopbody;
    2085         1489 :           tree tmp2;
    2086              : 
    2087         1489 :           loopbody = gfc_finish_block (&body);
    2088              : 
    2089              :           /* Create a new block that holds the implied-do loop. A temporary
    2090              :              loop-variable is used.  */
    2091         1489 :           gfc_start_block(&implied_do_block);
    2092              : 
    2093              :           /* Initialize the loop.  */
    2094         1489 :           gfc_add_modify (&implied_do_block, shadow_loopvar, start);
    2095              : 
    2096              :           /* If this array expands dynamically, and the number of iterations
    2097              :              is not constant, we won't have allocated space for the static
    2098              :              part of C->EXPR's size.  Do that now.  */
    2099         1489 :           if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
    2100              :             {
    2101              :               /* Get the number of iterations.  */
    2102          551 :               tmp = gfc_get_iteration_count (shadow_loopvar, end, step);
    2103              : 
    2104              :               /* Get the static part of C->EXPR's size.  */
    2105          551 :               gfc_get_array_constructor_element_size (&size, c->expr);
    2106          551 :               tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
    2107              : 
    2108              :               /* Grow the array by TMP * TMP2 elements.  */
    2109          551 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    2110              :                                      gfc_array_index_type, tmp, tmp2);
    2111          551 :               gfc_grow_array (&implied_do_block, desc, tmp);
    2112              :             }
    2113              : 
    2114              :           /* Generate the loop body.  */
    2115         1489 :           exit_label = gfc_build_label_decl (NULL_TREE);
    2116         1489 :           gfc_start_block (&body);
    2117              : 
    2118              :           /* Generate the exit condition.  Depending on the sign of
    2119              :              the step variable we have to generate the correct
    2120              :              comparison.  */
    2121         1489 :           tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2122         1489 :                                  step, build_int_cst (TREE_TYPE (step), 0));
    2123         1489 :           cond = fold_build3_loc (input_location, COND_EXPR,
    2124              :                       logical_type_node, tmp,
    2125              :                       fold_build2_loc (input_location, GT_EXPR,
    2126              :                                        logical_type_node, shadow_loopvar, end),
    2127              :                       fold_build2_loc (input_location, LT_EXPR,
    2128              :                                        logical_type_node, shadow_loopvar, end));
    2129         1489 :           tmp = build1_v (GOTO_EXPR, exit_label);
    2130         1489 :           TREE_USED (exit_label) = 1;
    2131         1489 :           tmp = build3_v (COND_EXPR, cond, tmp,
    2132              :                           build_empty_stmt (input_location));
    2133         1489 :           gfc_add_expr_to_block (&body, tmp);
    2134              : 
    2135              :           /* The main loop body.  */
    2136         1489 :           gfc_add_expr_to_block (&body, loopbody);
    2137              : 
    2138              :           /* Increase loop variable by step.  */
    2139         1489 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2140         1489 :                                  TREE_TYPE (shadow_loopvar), shadow_loopvar,
    2141              :                                  step);
    2142         1489 :           gfc_add_modify (&body, shadow_loopvar, tmp);
    2143              : 
    2144              :           /* Finish the loop.  */
    2145         1489 :           tmp = gfc_finish_block (&body);
    2146         1489 :           tmp = build1_v (LOOP_EXPR, tmp);
    2147         1489 :           gfc_add_expr_to_block (&implied_do_block, tmp);
    2148              : 
    2149              :           /* Add the exit label.  */
    2150         1489 :           tmp = build1_v (LABEL_EXPR, exit_label);
    2151         1489 :           gfc_add_expr_to_block (&implied_do_block, tmp);
    2152              : 
    2153              :           /* Finish the implied-do loop.  */
    2154         1489 :           tmp = gfc_finish_block(&implied_do_block);
    2155         1489 :           gfc_add_expr_to_block(pblock, tmp);
    2156              : 
    2157         1489 :           gfc_restore_sym (c->iterator->var->symtree->n.sym, &saved_loopvar);
    2158              :         }
    2159              :     }
    2160              : 
    2161              :   /* F2008 4.5.6.3 para 5: If an executable construct references a structure
    2162              :      constructor or array constructor, the entity created by the constructor is
    2163              :      finalized after execution of the innermost executable construct containing
    2164              :      the reference. This, in fact, was later deleted by the Combined Technical
    2165              :      Corrigenda 1 TO 4 for fortran 2008 (f08/0011).
    2166              : 
    2167              :      Transmit finalization of this constructor through 'finalblock'. */
    2168         8449 :   if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
    2169         8449 :       && !(gfc_option.allow_std & GFC_STD_GNU)
    2170           70 :       && finalblock != NULL
    2171           24 :       && gfc_may_be_finalized (ts)
    2172           18 :       && ctr > 0 && desc != NULL_TREE
    2173         8467 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    2174              :     {
    2175           18 :       symbol_attribute attr;
    2176           18 :       gfc_se fse;
    2177           18 :       locus loc;
    2178           18 :       gfc_locus_from_location (&loc, input_location);
    2179           18 :       gfc_warning (0, "The structure constructor at %L has been"
    2180              :                          " finalized. This feature was removed by f08/0011."
    2181              :                          " Use -std=f2018 or -std=gnu to eliminate the"
    2182              :                          " finalization.", &loc);
    2183           18 :       attr.pointer = attr.allocatable = 0;
    2184           18 :       gfc_init_se (&fse, NULL);
    2185           18 :       fse.expr = desc;
    2186           18 :       gfc_finalize_tree_expr (&fse, ts.u.derived, attr, 1);
    2187           18 :       gfc_add_block_to_block (finalblock, &fse.pre);
    2188           18 :       gfc_add_block_to_block (finalblock, &fse.finalblock);
    2189           18 :       gfc_add_block_to_block (finalblock, &fse.post);
    2190              :     }
    2191              : 
    2192         8449 :   mpz_clear (size);
    2193         8449 : }
    2194              : 
    2195              : 
    2196              : /* The array constructor code can create a string length with an operand
    2197              :    in the form of a temporary variable.  This variable will retain its
    2198              :    context (current_function_decl).  If we store this length tree in a
    2199              :    gfc_charlen structure which is shared by a variable in another
    2200              :    context, the resulting gfc_charlen structure with a variable in a
    2201              :    different context, we could trip the assertion in expand_expr_real_1
    2202              :    when it sees that a variable has been created in one context and
    2203              :    referenced in another.
    2204              : 
    2205              :    If this might be the case, we create a new gfc_charlen structure and
    2206              :    link it into the current namespace.  */
    2207              : 
    2208              : static void
    2209         8497 : store_backend_decl (gfc_charlen **clp, tree len, bool force_new_cl)
    2210              : {
    2211         8497 :   if (force_new_cl)
    2212              :     {
    2213         8469 :       gfc_charlen *new_cl = gfc_new_charlen (gfc_current_ns, *clp);
    2214         8469 :       *clp = new_cl;
    2215              :     }
    2216         8497 :   (*clp)->backend_decl = len;
    2217         8497 : }
    2218              : 
    2219              : /* A catch-all to obtain the string length for anything that is not
    2220              :    a substring of non-constant length, a constant, array or variable.  */
    2221              : 
    2222              : static void
    2223          312 : get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
    2224              : {
    2225          312 :   gfc_se se;
    2226              : 
    2227              :   /* Don't bother if we already know the length is a constant.  */
    2228          312 :   if (*len && INTEGER_CST_P (*len))
    2229           52 :     return;
    2230              : 
    2231          260 :   if (!e->ref && e->ts.u.cl && e->ts.u.cl->length
    2232           35 :         && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    2233              :     {
    2234              :       /* This is easy.  */
    2235            1 :       gfc_conv_const_charlen (e->ts.u.cl);
    2236            1 :       *len = e->ts.u.cl->backend_decl;
    2237              :     }
    2238              :   else
    2239              :     {
    2240              :       /* Otherwise, be brutal even if inefficient.  */
    2241          259 :       gfc_init_se (&se, NULL);
    2242              : 
    2243              :       /* No function call, in case of side effects.  */
    2244          259 :       se.no_function_call = 1;
    2245          259 :       if (e->rank == 0)
    2246          140 :         gfc_conv_expr (&se, e);
    2247              :       else
    2248          119 :         gfc_conv_expr_descriptor (&se, e);
    2249              : 
    2250              :       /* Fix the value.  */
    2251          259 :       *len = gfc_evaluate_now (se.string_length, &se.pre);
    2252              : 
    2253          259 :       gfc_add_block_to_block (block, &se.pre);
    2254          259 :       gfc_add_block_to_block (block, &se.post);
    2255              : 
    2256          259 :       store_backend_decl (&e->ts.u.cl, *len, true);
    2257              :     }
    2258              : }
    2259              : 
    2260              : 
    2261              : /* Figure out the string length of a variable reference expression.
    2262              :    Used by get_array_ctor_strlen.  */
    2263              : 
    2264              : static void
    2265          882 : get_array_ctor_var_strlen (stmtblock_t *block, gfc_expr * expr, tree * len)
    2266              : {
    2267          882 :   gfc_ref *ref;
    2268          882 :   gfc_typespec *ts;
    2269          882 :   mpz_t char_len;
    2270          882 :   gfc_se se;
    2271              : 
    2272              :   /* Don't bother if we already know the length is a constant.  */
    2273          882 :   if (*len && INTEGER_CST_P (*len))
    2274          551 :     return;
    2275              : 
    2276          420 :   ts = &expr->symtree->n.sym->ts;
    2277          651 :   for (ref = expr->ref; ref; ref = ref->next)
    2278              :     {
    2279          320 :       switch (ref->type)
    2280              :         {
    2281          186 :         case REF_ARRAY:
    2282              :           /* Array references don't change the string length.  */
    2283          186 :           if (ts->deferred)
    2284          112 :             get_array_ctor_all_strlen (block, expr, len);
    2285              :           break;
    2286              : 
    2287           45 :         case REF_COMPONENT:
    2288              :           /* Use the length of the component.  */
    2289           45 :           ts = &ref->u.c.component->ts;
    2290           45 :           break;
    2291              : 
    2292           89 :         case REF_SUBSTRING:
    2293           89 :           if (ref->u.ss.end == NULL
    2294           77 :               || ref->u.ss.start->expr_type != EXPR_CONSTANT
    2295           58 :               || ref->u.ss.end->expr_type != EXPR_CONSTANT)
    2296              :             {
    2297              :               /* Note that this might evaluate expr.  */
    2298           64 :               get_array_ctor_all_strlen (block, expr, len);
    2299           64 :               return;
    2300              :             }
    2301           25 :           mpz_init_set_ui (char_len, 1);
    2302           25 :           mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
    2303           25 :           mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
    2304           25 :           *len = gfc_conv_mpz_to_tree_type (char_len, gfc_charlen_type_node);
    2305           25 :           mpz_clear (char_len);
    2306           25 :           return;
    2307              : 
    2308              :         case REF_INQUIRY:
    2309              :           break;
    2310              : 
    2311            0 :         default:
    2312            0 :          gcc_unreachable ();
    2313              :         }
    2314              :     }
    2315              : 
    2316              :   /* A last ditch attempt that is sometimes needed for deferred characters.  */
    2317          331 :   if (!ts->u.cl->backend_decl)
    2318              :     {
    2319            7 :       gfc_init_se (&se, NULL);
    2320            7 :       if (expr->rank)
    2321            0 :         gfc_conv_expr_descriptor (&se, expr);
    2322              :       else
    2323            7 :         gfc_conv_expr (&se, expr);
    2324            7 :       gcc_assert (se.string_length != NULL_TREE);
    2325            7 :       gfc_add_block_to_block (block, &se.pre);
    2326            7 :       ts->u.cl->backend_decl = se.string_length;
    2327              :     }
    2328              : 
    2329          331 :   *len = ts->u.cl->backend_decl;
    2330              : }
    2331              : 
    2332              : 
    2333              : /* Figure out the string length of a character array constructor.
    2334              :    If len is NULL, don't calculate the length; this happens for recursive calls
    2335              :    when a sub-array-constructor is an element but not at the first position,
    2336              :    so when we're not interested in the length.
    2337              :    Returns TRUE if all elements are character constants.  */
    2338              : 
    2339              : bool
    2340         8837 : get_array_ctor_strlen (stmtblock_t *block, gfc_constructor_base base, tree * len)
    2341              : {
    2342         8837 :   gfc_constructor *c;
    2343         8837 :   bool is_const;
    2344              : 
    2345         8837 :   is_const = true;
    2346              : 
    2347         8837 :   if (gfc_constructor_first (base) == NULL)
    2348              :     {
    2349          273 :       if (len)
    2350          273 :         *len = build_int_cstu (gfc_charlen_type_node, 0);
    2351              :       return is_const;
    2352              :     }
    2353              : 
    2354              :   /* Loop over all constructor elements to find out is_const, but in len we
    2355              :      want to store the length of the first, not the last, element.  We can
    2356              :      of course exit the loop as soon as is_const is found to be false.  */
    2357         8564 :   for (c = gfc_constructor_first (base);
    2358        46869 :        c && is_const; c = gfc_constructor_next (c))
    2359              :     {
    2360        38305 :       switch (c->expr->expr_type)
    2361              :         {
    2362        37184 :         case EXPR_CONSTANT:
    2363        37184 :           if (len && !(*len && INTEGER_CST_P (*len)))
    2364          386 :             *len = build_int_cstu (gfc_charlen_type_node,
    2365          386 :                                    c->expr->value.character.length);
    2366              :           break;
    2367              : 
    2368           43 :         case EXPR_ARRAY:
    2369           43 :           if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
    2370        38305 :             is_const = false;
    2371              :           break;
    2372              : 
    2373          942 :         case EXPR_VARIABLE:
    2374          942 :           is_const = false;
    2375          942 :           if (len)
    2376          882 :             get_array_ctor_var_strlen (block, c->expr, len);
    2377              :           break;
    2378              : 
    2379          136 :         default:
    2380          136 :           is_const = false;
    2381          136 :           if (len)
    2382          136 :             get_array_ctor_all_strlen (block, c->expr, len);
    2383              :           break;
    2384              :         }
    2385              : 
    2386              :       /* After the first iteration, we don't want the length modified.  */
    2387        38305 :       len = NULL;
    2388              :     }
    2389              : 
    2390              :   return is_const;
    2391              : }
    2392              : 
    2393              : /* Check whether the array constructor C consists entirely of constant
    2394              :    elements, and if so returns the number of those elements, otherwise
    2395              :    return zero.  Note, an empty or NULL array constructor returns zero.  */
    2396              : 
    2397              : unsigned HOST_WIDE_INT
    2398        60739 : gfc_constant_array_constructor_p (gfc_constructor_base base)
    2399              : {
    2400        60739 :   unsigned HOST_WIDE_INT nelem = 0;
    2401              : 
    2402        60739 :   gfc_constructor *c = gfc_constructor_first (base);
    2403       547857 :   while (c)
    2404              :     {
    2405       433681 :       if (c->iterator
    2406       432076 :           || c->expr->rank > 0
    2407       431266 :           || c->expr->expr_type != EXPR_CONSTANT)
    2408              :         return 0;
    2409       426379 :       c = gfc_constructor_next (c);
    2410       426379 :       nelem++;
    2411              :     }
    2412              :   return nelem;
    2413              : }
    2414              : 
    2415              : 
    2416              : /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
    2417              :    and the tree type of it's elements, TYPE, return a static constant
    2418              :    variable that is compile-time initialized.  */
    2419              : 
    2420              : tree
    2421        42796 : gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
    2422              : {
    2423        42796 :   tree tmptype, init, tmp;
    2424        42796 :   HOST_WIDE_INT nelem;
    2425        42796 :   gfc_constructor *c;
    2426        42796 :   gfc_array_spec as;
    2427        42796 :   gfc_se se;
    2428        42796 :   int i;
    2429        42796 :   vec<constructor_elt, va_gc> *v = NULL;
    2430              : 
    2431              :   /* First traverse the constructor list, converting the constants
    2432              :      to tree to build an initializer.  */
    2433        42796 :   nelem = 0;
    2434        42796 :   c = gfc_constructor_first (expr->value.constructor);
    2435       428231 :   while (c)
    2436              :     {
    2437       342639 :       gfc_init_se (&se, NULL);
    2438       342639 :       gfc_conv_constant (&se, c->expr);
    2439       342639 :       if (c->expr->ts.type != BT_CHARACTER)
    2440       306375 :         se.expr = fold_convert (type, se.expr);
    2441        36264 :       else if (POINTER_TYPE_P (type))
    2442        36264 :         se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
    2443              :                                        se.expr);
    2444       342639 :       CONSTRUCTOR_APPEND_ELT (v, build_int_cst (gfc_array_index_type, nelem),
    2445              :                               se.expr);
    2446       342639 :       c = gfc_constructor_next (c);
    2447       342639 :       nelem++;
    2448              :     }
    2449              : 
    2450              :   /* Next determine the tree type for the array.  We use the gfortran
    2451              :      front-end's gfc_get_nodesc_array_type in order to create a suitable
    2452              :      GFC_ARRAY_TYPE_P that may be used by the scalarizer.  */
    2453              : 
    2454        42796 :   memset (&as, 0, sizeof (gfc_array_spec));
    2455              : 
    2456        42796 :   as.rank = expr->rank;
    2457        42796 :   as.type = AS_EXPLICIT;
    2458        42796 :   if (!expr->shape)
    2459              :     {
    2460            4 :       as.lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
    2461            4 :       as.upper[0] = gfc_get_int_expr (gfc_default_integer_kind,
    2462              :                                       NULL, nelem - 1);
    2463              :     }
    2464              :   else
    2465        92299 :     for (i = 0; i < expr->rank; i++)
    2466              :       {
    2467        49507 :         int tmp = (int) mpz_get_si (expr->shape[i]);
    2468        49507 :         as.lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
    2469        49507 :         as.upper[i] = gfc_get_int_expr (gfc_default_integer_kind,
    2470        49507 :                                         NULL, tmp - 1);
    2471              :       }
    2472              : 
    2473        42796 :   tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
    2474              : 
    2475              :   /* as is not needed anymore.  */
    2476       135103 :   for (i = 0; i < as.rank + as.corank; i++)
    2477              :     {
    2478        49511 :       gfc_free_expr (as.lower[i]);
    2479        49511 :       gfc_free_expr (as.upper[i]);
    2480              :     }
    2481              : 
    2482        42796 :   init = build_constructor (tmptype, v);
    2483              : 
    2484        42796 :   TREE_CONSTANT (init) = 1;
    2485        42796 :   TREE_STATIC (init) = 1;
    2486              : 
    2487        42796 :   tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
    2488              :                     tmptype);
    2489        42796 :   DECL_ARTIFICIAL (tmp) = 1;
    2490        42796 :   DECL_IGNORED_P (tmp) = 1;
    2491        42796 :   TREE_STATIC (tmp) = 1;
    2492        42796 :   TREE_CONSTANT (tmp) = 1;
    2493        42796 :   TREE_READONLY (tmp) = 1;
    2494        42796 :   DECL_INITIAL (tmp) = init;
    2495        42796 :   pushdecl (tmp);
    2496              : 
    2497        42796 :   return tmp;
    2498              : }
    2499              : 
    2500              : 
    2501              : /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
    2502              :    This mostly initializes the scalarizer state info structure with the
    2503              :    appropriate values to directly use the array created by the function
    2504              :    gfc_build_constant_array_constructor.  */
    2505              : 
    2506              : static void
    2507        36861 : trans_constant_array_constructor (gfc_ss * ss, tree type)
    2508              : {
    2509        36861 :   gfc_array_info *info;
    2510        36861 :   tree tmp;
    2511        36861 :   int i;
    2512              : 
    2513        36861 :   tmp = gfc_build_constant_array_constructor (ss->info->expr, type);
    2514              : 
    2515        36861 :   info = &ss->info->data.array;
    2516              : 
    2517        36861 :   info->descriptor = tmp;
    2518        36861 :   info->data = gfc_build_addr_expr (NULL_TREE, tmp);
    2519        36861 :   info->offset = gfc_index_zero_node;
    2520              : 
    2521        77565 :   for (i = 0; i < ss->dimen; i++)
    2522              :     {
    2523        40704 :       info->delta[i] = gfc_index_zero_node;
    2524        40704 :       info->start[i] = gfc_index_zero_node;
    2525        40704 :       info->end[i] = gfc_index_zero_node;
    2526        40704 :       info->stride[i] = gfc_index_one_node;
    2527              :     }
    2528        36861 : }
    2529              : 
    2530              : 
    2531              : static int
    2532        36868 : get_rank (gfc_loopinfo *loop)
    2533              : {
    2534        36868 :   int rank;
    2535              : 
    2536        36868 :   rank = 0;
    2537       158442 :   for (; loop; loop = loop->parent)
    2538        79227 :     rank += loop->dimen;
    2539              : 
    2540        42347 :   return rank;
    2541              : }
    2542              : 
    2543              : 
    2544              : /* Helper routine of gfc_trans_array_constructor to determine if the
    2545              :    bounds of the loop specified by LOOP are constant and simple enough
    2546              :    to use with trans_constant_array_constructor.  Returns the
    2547              :    iteration count of the loop if suitable, and NULL_TREE otherwise.  */
    2548              : 
    2549              : static tree
    2550        36868 : constant_array_constructor_loop_size (gfc_loopinfo * l)
    2551              : {
    2552        36868 :   gfc_loopinfo *loop;
    2553        36868 :   tree size = gfc_index_one_node;
    2554        36868 :   tree tmp;
    2555        36868 :   int i, total_dim;
    2556              : 
    2557        36868 :   total_dim = get_rank (l);
    2558              : 
    2559        73736 :   for (loop = l; loop; loop = loop->parent)
    2560              :     {
    2561        77591 :       for (i = 0; i < loop->dimen; i++)
    2562              :         {
    2563              :           /* If the bounds aren't constant, return NULL_TREE.  */
    2564        40723 :           if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
    2565              :             return NULL_TREE;
    2566        40717 :           if (!integer_zerop (loop->from[i]))
    2567              :             {
    2568              :               /* Only allow nonzero "from" in one-dimensional arrays.  */
    2569            0 :               if (total_dim != 1)
    2570              :                 return NULL_TREE;
    2571            0 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2572              :                                      gfc_array_index_type,
    2573              :                                      loop->to[i], loop->from[i]);
    2574              :             }
    2575              :           else
    2576        40717 :             tmp = loop->to[i];
    2577        40717 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    2578              :                                  gfc_array_index_type, tmp, gfc_index_one_node);
    2579        40717 :           size = fold_build2_loc (input_location, MULT_EXPR,
    2580              :                                   gfc_array_index_type, size, tmp);
    2581              :         }
    2582              :     }
    2583              : 
    2584              :   return size;
    2585              : }
    2586              : 
    2587              : 
    2588              : static tree *
    2589        43799 : get_loop_upper_bound_for_array (gfc_ss *array, int array_dim)
    2590              : {
    2591        43799 :   gfc_ss *ss;
    2592        43799 :   int n;
    2593              : 
    2594        43799 :   gcc_assert (array->nested_ss == NULL);
    2595              : 
    2596        43799 :   for (ss = array; ss; ss = ss->parent)
    2597        43799 :     for (n = 0; n < ss->loop->dimen; n++)
    2598        43799 :       if (array_dim == get_array_ref_dim_for_loop_dim (ss, n))
    2599        43799 :         return &(ss->loop->to[n]);
    2600              : 
    2601            0 :   gcc_unreachable ();
    2602              : }
    2603              : 
    2604              : 
    2605              : static gfc_loopinfo *
    2606       720185 : outermost_loop (gfc_loopinfo * loop)
    2607              : {
    2608       939316 :   while (loop->parent != NULL)
    2609              :     loop = loop->parent;
    2610              : 
    2611       726873 :   return loop;
    2612              : }
    2613              : 
    2614              : 
    2615              : /* Array constructors are handled by constructing a temporary, then using that
    2616              :    within the scalarization loop.  This is not optimal, but seems by far the
    2617              :    simplest method.  */
    2618              : 
    2619              : static void
    2620        43799 : trans_array_constructor (gfc_ss * ss, locus * where)
    2621              : {
    2622        43799 :   gfc_constructor_base c;
    2623        43799 :   tree offset;
    2624        43799 :   tree offsetvar;
    2625        43799 :   tree desc;
    2626        43799 :   tree type;
    2627        43799 :   tree tmp;
    2628        43799 :   tree *loop_ubound0;
    2629        43799 :   bool dynamic;
    2630        43799 :   bool old_first_len, old_typespec_chararray_ctor;
    2631        43799 :   tree old_first_len_val;
    2632        43799 :   gfc_loopinfo *loop, *outer_loop;
    2633        43799 :   gfc_ss_info *ss_info;
    2634        43799 :   gfc_expr *expr;
    2635        43799 :   gfc_ss *s;
    2636        43799 :   tree neg_len;
    2637        43799 :   char *msg;
    2638        43799 :   stmtblock_t finalblock;
    2639        43799 :   bool finalize_required;
    2640        43799 :   bool owned_sweep = false;
    2641              : 
    2642              :   /* Save the old values for nested checking.  */
    2643        43799 :   old_first_len = first_len;
    2644        43799 :   old_first_len_val = first_len_val;
    2645        43799 :   old_typespec_chararray_ctor = typespec_chararray_ctor;
    2646              : 
    2647        43799 :   loop = ss->loop;
    2648        43799 :   outer_loop = outermost_loop (loop);
    2649        43799 :   ss_info = ss->info;
    2650        43799 :   expr = ss_info->expr;
    2651              : 
    2652              :   /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
    2653              :      typespec was given for the array constructor.  */
    2654        87598 :   typespec_chararray_ctor = (expr->ts.type == BT_CHARACTER
    2655         8238 :                              && expr->ts.u.cl
    2656        52037 :                              && expr->ts.u.cl->length_from_typespec);
    2657              : 
    2658        43799 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2659         2542 :       && expr->ts.type == BT_CHARACTER && !typespec_chararray_ctor)
    2660              :     {
    2661         1468 :       first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
    2662         1468 :       first_len = true;
    2663              :     }
    2664              : 
    2665        43799 :   gcc_assert (ss->dimen == ss->loop->dimen);
    2666              : 
    2667        43799 :   c = expr->value.constructor;
    2668        43799 :   if (expr->ts.type == BT_CHARACTER)
    2669              :     {
    2670         8238 :       bool const_string;
    2671         8238 :       bool force_new_cl = false;
    2672              : 
    2673              :       /* get_array_ctor_strlen walks the elements of the constructor, if a
    2674              :          typespec was given, we already know the string length and want the one
    2675              :          specified there.  */
    2676         8238 :       if (typespec_chararray_ctor && expr->ts.u.cl->length
    2677          520 :           && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    2678              :         {
    2679           28 :           gfc_se length_se;
    2680              : 
    2681           28 :           const_string = false;
    2682           28 :           gfc_init_se (&length_se, NULL);
    2683           28 :           gfc_conv_expr_type (&length_se, expr->ts.u.cl->length,
    2684              :                               gfc_charlen_type_node);
    2685           28 :           ss_info->string_length = length_se.expr;
    2686              : 
    2687              :           /* Check if the character length is negative.  If it is, then
    2688              :              set LEN = 0.  */
    2689           28 :           neg_len = fold_build2_loc (input_location, LT_EXPR,
    2690              :                                      logical_type_node, ss_info->string_length,
    2691           28 :                                      build_zero_cst (TREE_TYPE
    2692              :                                                      (ss_info->string_length)));
    2693              :           /* Print a warning if bounds checking is enabled.  */
    2694           28 :           if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2695              :             {
    2696           18 :               msg = xasprintf ("Negative character length treated as LEN = 0");
    2697           18 :               gfc_trans_runtime_check (false, true, neg_len, &length_se.pre,
    2698              :                                        where, msg);
    2699           18 :               free (msg);
    2700              :             }
    2701              : 
    2702           28 :           ss_info->string_length
    2703           28 :             = fold_build3_loc (input_location, COND_EXPR,
    2704              :                                gfc_charlen_type_node, neg_len,
    2705              :                                build_zero_cst
    2706           28 :                                (TREE_TYPE (ss_info->string_length)),
    2707              :                                ss_info->string_length);
    2708           28 :           ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
    2709              :                                                      &length_se.pre);
    2710           28 :           gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
    2711           28 :           gfc_add_block_to_block (&outer_loop->post, &length_se.post);
    2712           28 :         }
    2713              :       else
    2714              :         {
    2715         8210 :           const_string = get_array_ctor_strlen (&outer_loop->pre, c,
    2716              :                                                 &ss_info->string_length);
    2717         8210 :           force_new_cl = true;
    2718              : 
    2719              :           /* Initialize "len" with string length for bounds checking.  */
    2720         8210 :           if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2721         1486 :               && !typespec_chararray_ctor
    2722         1468 :               && ss_info->string_length)
    2723              :             {
    2724         1468 :               gfc_se length_se;
    2725              : 
    2726         1468 :               gfc_init_se (&length_se, NULL);
    2727         1468 :               gfc_add_modify (&length_se.pre, first_len_val,
    2728         1468 :                               fold_convert (TREE_TYPE (first_len_val),
    2729              :                                             ss_info->string_length));
    2730         1468 :               ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
    2731              :                                                          &length_se.pre);
    2732         1468 :               gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
    2733         1468 :               gfc_add_block_to_block (&outer_loop->post, &length_se.post);
    2734              :             }
    2735              :         }
    2736              : 
    2737              :       /* Complex character array constructors should have been taken care of
    2738              :          and not end up here.  */
    2739         8238 :       gcc_assert (ss_info->string_length);
    2740              : 
    2741         8238 :       store_backend_decl (&expr->ts.u.cl, ss_info->string_length, force_new_cl);
    2742              : 
    2743         8238 :       type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
    2744         8238 :       if (const_string)
    2745         7280 :         type = build_pointer_type (type);
    2746              :     }
    2747              :   else
    2748        35586 :     type = gfc_typenode_for_spec (expr->ts.type == BT_CLASS
    2749           25 :                                   ? &CLASS_DATA (expr)->ts : &expr->ts);
    2750              : 
    2751              :   /* See if the constructor determines the loop bounds.  */
    2752        43799 :   dynamic = false;
    2753              : 
    2754        43799 :   loop_ubound0 = get_loop_upper_bound_for_array (ss, 0);
    2755              : 
    2756        86146 :   if (expr->shape && get_rank (loop) > 1 && *loop_ubound0 == NULL_TREE)
    2757              :     {
    2758              :       /* We have a multidimensional parameter.  */
    2759            0 :       for (s = ss; s; s = s->parent)
    2760              :         {
    2761              :           int n;
    2762            0 :           for (n = 0; n < s->loop->dimen; n++)
    2763              :             {
    2764            0 :               s->loop->from[n] = gfc_index_zero_node;
    2765            0 :               s->loop->to[n] = gfc_conv_mpz_to_tree (expr->shape[s->dim[n]],
    2766              :                                                      gfc_index_integer_kind);
    2767            0 :               s->loop->to[n] = fold_build2_loc (input_location, MINUS_EXPR,
    2768              :                                                 gfc_array_index_type,
    2769            0 :                                                 s->loop->to[n],
    2770              :                                                 gfc_index_one_node);
    2771              :             }
    2772              :         }
    2773              :     }
    2774              : 
    2775        43799 :   if (*loop_ubound0 == NULL_TREE)
    2776              :     {
    2777          893 :       mpz_t size;
    2778              : 
    2779              :       /* We should have a 1-dimensional, zero-based loop.  */
    2780          893 :       gcc_assert (loop->parent == NULL && loop->nested == NULL);
    2781          893 :       gcc_assert (loop->dimen == 1);
    2782          893 :       gcc_assert (integer_zerop (loop->from[0]));
    2783              : 
    2784              :       /* Split the constructor size into a static part and a dynamic part.
    2785              :          Allocate the static size up-front and record whether the dynamic
    2786              :          size might be nonzero.  */
    2787          893 :       mpz_init (size);
    2788          893 :       dynamic = gfc_get_array_constructor_size (&size, c);
    2789          893 :       mpz_sub_ui (size, size, 1);
    2790          893 :       loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
    2791          893 :       mpz_clear (size);
    2792              :     }
    2793              : 
    2794              :   /* Special case constant array constructors.  */
    2795          893 :   if (!dynamic)
    2796              :     {
    2797        42931 :       unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
    2798        42931 :       if (nelem > 0)
    2799              :         {
    2800        36868 :           tree size = constant_array_constructor_loop_size (loop);
    2801        36862 :           if (size && compare_tree_int (size, nelem) == 0
    2802        73730 :               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
    2803              :             {
    2804        36861 :               trans_constant_array_constructor (ss, type);
    2805        36861 :               goto finish;
    2806              :             }
    2807              :         }
    2808              :     }
    2809              : 
    2810         6938 :   gfc_trans_create_temp_array (&outer_loop->pre, &outer_loop->post, ss, type,
    2811              :                                NULL_TREE, dynamic, true, false, where);
    2812              : 
    2813         6938 :   desc = ss_info->data.array.descriptor;
    2814         6938 :   offset = gfc_index_zero_node;
    2815         6938 :   offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
    2816         6938 :   suppress_warning (offsetvar);
    2817         6938 :   TREE_USED (offsetvar) = 0;
    2818              : 
    2819         6938 :   gfc_init_block (&finalblock);
    2820         6938 :   finalize_required = expr->must_finalize;
    2821         6938 :   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
    2822              :     finalize_required = true;
    2823              : 
    2824         6938 :   if (IS_PDT (expr))
    2825              :    finalize_required = true;
    2826              : 
    2827              :   /* If every element of the constructor is a function result with allocatable
    2828              :      components, those components are owned by the temporary and are freed in a
    2829              :      single sweep over the whole array below.  This is the only way to free the
    2830              :      elements produced inside an implied-do loop, where a single compile-time
    2831              :      element stands for many runtime elements.  */
    2832        13803 :   owned_sweep = finalize_required
    2833          552 :     && expr->ts.type == BT_DERIVED
    2834          552 :     && expr->ts.u.derived->attr.alloc_comp
    2835         7332 :     && gfc_constructor_is_owned_alloc_comp (c, expr->ts.u.derived);
    2836              : 
    2837         6938 :   gfc_trans_array_constructor_value (&outer_loop->pre,
    2838              :                                      finalize_required ? &finalblock : NULL,
    2839              :                                      type, desc, c, &offset, &offsetvar,
    2840              :                                      dynamic, owned_sweep);
    2841              : 
    2842         6938 :   if (owned_sweep)
    2843          250 :     gfc_add_expr_to_block (&finalblock,
    2844          250 :                            gfc_deallocate_alloc_comp_no_caf (expr->ts.u.derived,
    2845              :                                                              desc, 1, true));
    2846              : 
    2847              :   /* If the array grows dynamically, the upper bound of the loop variable
    2848              :      is determined by the array's final upper bound.  */
    2849         6938 :   if (dynamic)
    2850              :     {
    2851          868 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2852              :                              gfc_array_index_type,
    2853              :                              offsetvar, gfc_index_one_node);
    2854          868 :       tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
    2855          868 :       if (*loop_ubound0 && VAR_P (*loop_ubound0))
    2856            0 :         gfc_add_modify (&outer_loop->pre, *loop_ubound0, tmp);
    2857              :       else
    2858          868 :         *loop_ubound0 = tmp;
    2859              :     }
    2860              : 
    2861         6938 :   if (TREE_USED (offsetvar))
    2862         2188 :     pushdecl (offsetvar);
    2863              :   else
    2864         4750 :     gcc_assert (INTEGER_CST_P (offset));
    2865              : 
    2866              : #if 0
    2867              :   /* Disable bound checking for now because it's probably broken.  */
    2868              :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2869              :     {
    2870              :       gcc_unreachable ();
    2871              :     }
    2872              : #endif
    2873              : 
    2874         4750 : finish:
    2875              :   /* Restore old values of globals.  */
    2876        43799 :   first_len = old_first_len;
    2877        43799 :   first_len_val = old_first_len_val;
    2878        43799 :   typespec_chararray_ctor = old_typespec_chararray_ctor;
    2879              : 
    2880              :   /* F2008 4.5.6.3 para 5: If an executable construct references a structure
    2881              :      constructor or array constructor, the entity created by the constructor is
    2882              :      finalized after execution of the innermost executable construct containing
    2883              :      the reference.  */
    2884        43799 :   if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    2885         1764 :        && finalblock.head != NULL_TREE)
    2886          322 :     gfc_prepend_expr_to_block (&loop->post, finalblock.head);
    2887        43799 : }
    2888              : 
    2889              : 
    2890              : /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
    2891              :    called after evaluating all of INFO's vector dimensions.  Go through
    2892              :    each such vector dimension and see if we can now fill in any missing
    2893              :    loop bounds.  */
    2894              : 
    2895              : static void
    2896       184528 : set_vector_loop_bounds (gfc_ss * ss)
    2897              : {
    2898       184528 :   gfc_loopinfo *loop, *outer_loop;
    2899       184528 :   gfc_array_info *info;
    2900       184528 :   gfc_se se;
    2901       184528 :   tree tmp;
    2902       184528 :   tree desc;
    2903       184528 :   tree zero;
    2904       184528 :   int n;
    2905       184528 :   int dim;
    2906              : 
    2907       184528 :   outer_loop = outermost_loop (ss->loop);
    2908              : 
    2909       184528 :   info = &ss->info->data.array;
    2910              : 
    2911       373692 :   for (; ss; ss = ss->parent)
    2912              :     {
    2913       189164 :       loop = ss->loop;
    2914              : 
    2915       449908 :       for (n = 0; n < loop->dimen; n++)
    2916              :         {
    2917       260744 :           dim = ss->dim[n];
    2918       260744 :           if (info->ref->u.ar.dimen_type[dim] != DIMEN_VECTOR
    2919          986 :               || loop->to[n] != NULL)
    2920       260564 :             continue;
    2921              : 
    2922              :           /* Loop variable N indexes vector dimension DIM, and we don't
    2923              :              yet know the upper bound of loop variable N.  Set it to the
    2924              :              difference between the vector's upper and lower bounds.  */
    2925          180 :           gcc_assert (loop->from[n] == gfc_index_zero_node);
    2926          180 :           gcc_assert (info->subscript[dim]
    2927              :                       && info->subscript[dim]->info->type == GFC_SS_VECTOR);
    2928              : 
    2929          180 :           gfc_init_se (&se, NULL);
    2930          180 :           desc = info->subscript[dim]->info->data.array.descriptor;
    2931          180 :           zero = gfc_rank_cst[0];
    2932          180 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2933              :                              gfc_array_index_type,
    2934              :                              gfc_conv_descriptor_ubound_get (desc, zero),
    2935              :                              gfc_conv_descriptor_lbound_get (desc, zero));
    2936          180 :           tmp = gfc_evaluate_now (tmp, &outer_loop->pre);
    2937          180 :           loop->to[n] = tmp;
    2938              :         }
    2939              :     }
    2940       184528 : }
    2941              : 
    2942              : 
    2943              : /* Tells whether a scalar argument to an elemental procedure is saved out
    2944              :    of a scalarization loop as a value or as a reference.  */
    2945              : 
    2946              : bool
    2947        46083 : gfc_scalar_elemental_arg_saved_as_reference (gfc_ss_info * ss_info)
    2948              : {
    2949        46083 :   if (ss_info->type != GFC_SS_REFERENCE)
    2950              :     return false;
    2951              : 
    2952        10294 :   if (ss_info->data.scalar.needs_temporary)
    2953              :     return false;
    2954              : 
    2955              :   /* If the actual argument can be absent (in other words, it can
    2956              :      be a NULL reference), don't try to evaluate it; pass instead
    2957              :      the reference directly.  */
    2958         9918 :   if (ss_info->can_be_null_ref)
    2959              :     return true;
    2960              : 
    2961              :   /* If the expression is of polymorphic type, it's actual size is not known,
    2962              :      so we avoid copying it anywhere.  */
    2963         9242 :   if (ss_info->data.scalar.dummy_arg
    2964         1402 :       && gfc_dummy_arg_get_typespec (*ss_info->data.scalar.dummy_arg).type
    2965              :          == BT_CLASS
    2966         9366 :       && ss_info->expr->ts.type == BT_CLASS)
    2967              :     return true;
    2968              : 
    2969              :   /* If the expression is a data reference of aggregate type,
    2970              :      and the data reference is not used on the left hand side,
    2971              :      avoid a copy by saving a reference to the content.  */
    2972         9218 :   if (!ss_info->data.scalar.needs_temporary
    2973         9218 :       && (ss_info->expr->ts.type == BT_DERIVED
    2974         8230 :           || ss_info->expr->ts.type == BT_CLASS)
    2975        10254 :       && gfc_expr_is_variable (ss_info->expr))
    2976              :     return true;
    2977              : 
    2978              :   /* Otherwise the expression is evaluated to a temporary variable before the
    2979              :      scalarization loop.  */
    2980              :   return false;
    2981              : }
    2982              : 
    2983              : 
    2984              : /* Add the pre and post chains for all the scalar expressions in a SS chain
    2985              :    to loop.  This is called after the loop parameters have been calculated,
    2986              :    but before the actual scalarizing loops.  */
    2987              : 
    2988              : static void
    2989       194098 : gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
    2990              :                       locus * where)
    2991              : {
    2992       194098 :   gfc_loopinfo *nested_loop, *outer_loop;
    2993       194098 :   gfc_se se;
    2994       194098 :   gfc_ss_info *ss_info;
    2995       194098 :   gfc_array_info *info;
    2996       194098 :   gfc_expr *expr;
    2997       194098 :   int n;
    2998              : 
    2999              :   /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
    3000              :      arguments could get evaluated multiple times.  */
    3001       194098 :   if (ss->is_alloc_lhs)
    3002          203 :     return;
    3003              : 
    3004       511068 :   outer_loop = outermost_loop (loop);
    3005              : 
    3006              :   /* TODO: This can generate bad code if there are ordering dependencies,
    3007              :      e.g., a callee allocated function and an unknown size constructor.  */
    3008              :   gcc_assert (ss != NULL);
    3009              : 
    3010       511068 :   for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
    3011              :     {
    3012       317173 :       gcc_assert (ss);
    3013              : 
    3014              :       /* Cross loop arrays are handled from within the most nested loop.  */
    3015       317173 :       if (ss->nested_ss != NULL)
    3016         4740 :         continue;
    3017              : 
    3018       312433 :       ss_info = ss->info;
    3019       312433 :       expr = ss_info->expr;
    3020       312433 :       info = &ss_info->data.array;
    3021              : 
    3022       312433 :       switch (ss_info->type)
    3023              :         {
    3024        43993 :         case GFC_SS_SCALAR:
    3025              :           /* Scalar expression.  Evaluate this now.  This includes elemental
    3026              :              dimension indices, but not array section bounds.  */
    3027        43993 :           gfc_init_se (&se, NULL);
    3028        43993 :           gfc_conv_expr (&se, expr);
    3029        43993 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3030              : 
    3031        43993 :           if (expr->ts.type != BT_CHARACTER
    3032        43993 :               && !gfc_is_alloc_class_scalar_function (expr))
    3033              :             {
    3034              :               /* Move the evaluation of scalar expressions outside the
    3035              :                  scalarization loop, except for WHERE assignments.  */
    3036        39967 :               if (subscript)
    3037         6545 :                 se.expr = convert(gfc_array_index_type, se.expr);
    3038        39967 :               if (!ss_info->where)
    3039        39553 :                 se.expr = gfc_evaluate_now (se.expr, &outer_loop->pre);
    3040        39967 :               gfc_add_block_to_block (&outer_loop->pre, &se.post);
    3041              :             }
    3042              :           else
    3043         4026 :             gfc_add_block_to_block (&outer_loop->post, &se.post);
    3044              : 
    3045        43993 :           ss_info->data.scalar.value = se.expr;
    3046        43993 :           ss_info->string_length = se.string_length;
    3047        43993 :           break;
    3048              : 
    3049         5147 :         case GFC_SS_REFERENCE:
    3050              :           /* Scalar argument to elemental procedure.  */
    3051         5147 :           gfc_init_se (&se, NULL);
    3052         5147 :           if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
    3053          844 :             gfc_conv_expr_reference (&se, expr);
    3054              :           else
    3055              :             {
    3056              :               /* Evaluate the argument outside the loop and pass
    3057              :                  a reference to the value.  */
    3058         4303 :               gfc_conv_expr (&se, expr);
    3059              :             }
    3060              : 
    3061              :           /* Ensure that a pointer to the string is stored.  */
    3062         5147 :           if (expr->ts.type == BT_CHARACTER)
    3063          174 :             gfc_conv_string_parameter (&se);
    3064              : 
    3065         5147 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3066         5147 :           gfc_add_block_to_block (&outer_loop->post, &se.post);
    3067         5147 :           if (gfc_is_class_scalar_expr (expr))
    3068              :             /* This is necessary because the dynamic type will always be
    3069              :                large than the declared type.  In consequence, assigning
    3070              :                the value to a temporary could segfault.
    3071              :                OOP-TODO: see if this is generally correct or is the value
    3072              :                has to be written to an allocated temporary, whose address
    3073              :                is passed via ss_info.  */
    3074           48 :             ss_info->data.scalar.value = se.expr;
    3075              :           else
    3076         5099 :             ss_info->data.scalar.value = gfc_evaluate_now (se.expr,
    3077              :                                                            &outer_loop->pre);
    3078              : 
    3079         5147 :           ss_info->string_length = se.string_length;
    3080         5147 :           break;
    3081              : 
    3082              :         case GFC_SS_SECTION:
    3083              :           /* Add the expressions for scalar and vector subscripts.  */
    3084      2952448 :           for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    3085      2767920 :             if (info->subscript[n])
    3086         7531 :               gfc_add_loop_ss_code (loop, info->subscript[n], true, where);
    3087              : 
    3088       184528 :           set_vector_loop_bounds (ss);
    3089       184528 :           break;
    3090              : 
    3091          986 :         case GFC_SS_VECTOR:
    3092              :           /* Get the vector's descriptor and store it in SS.  */
    3093          986 :           gfc_init_se (&se, NULL);
    3094          986 :           gfc_conv_expr_descriptor (&se, expr);
    3095          986 :           gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3096          986 :           gfc_add_block_to_block (&outer_loop->post, &se.post);
    3097          986 :           info->descriptor = se.expr;
    3098          986 :           break;
    3099              : 
    3100        11676 :         case GFC_SS_INTRINSIC:
    3101        11676 :           gfc_add_intrinsic_ss_code (loop, ss);
    3102        11676 :           break;
    3103              : 
    3104         9600 :         case GFC_SS_FUNCTION:
    3105         9600 :           {
    3106              :             /* Array function return value.  We call the function and save its
    3107              :                result in a temporary for use inside the loop.  */
    3108         9600 :             gfc_init_se (&se, NULL);
    3109         9600 :             se.loop = loop;
    3110         9600 :             se.ss = ss;
    3111         9600 :             bool class_func = gfc_is_class_array_function (expr);
    3112         9600 :             if (class_func)
    3113          183 :               expr->must_finalize = 1;
    3114         9600 :             gfc_conv_expr (&se, expr);
    3115         9600 :             gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3116         9600 :             if (class_func
    3117          183 :                 && se.expr
    3118         9783 :                 && GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
    3119              :               {
    3120          183 :                 tree tmp = gfc_class_data_get (se.expr);
    3121          183 :                 info->descriptor = tmp;
    3122          183 :                 info->data = gfc_conv_descriptor_data_get (tmp);
    3123          183 :                 info->offset = gfc_conv_descriptor_offset_get (tmp);
    3124          366 :                 for (gfc_ss *s = ss; s; s = s->parent)
    3125          378 :                   for (int n = 0; n < s->dimen; n++)
    3126              :                     {
    3127          195 :                       int dim = s->dim[n];
    3128          195 :                       tree tree_dim = gfc_rank_cst[dim];
    3129              : 
    3130          195 :                       tree start;
    3131          195 :                       start = gfc_conv_descriptor_lbound_get (tmp, tree_dim);
    3132          195 :                       start = gfc_evaluate_now (start, &outer_loop->pre);
    3133          195 :                       info->start[dim] = start;
    3134              : 
    3135          195 :                       tree end;
    3136          195 :                       end = gfc_conv_descriptor_ubound_get (tmp, tree_dim);
    3137          195 :                       end = gfc_evaluate_now (end, &outer_loop->pre);
    3138          195 :                       info->end[dim] = end;
    3139              : 
    3140          195 :                       tree stride;
    3141          195 :                       stride = gfc_conv_descriptor_stride_get (tmp, tree_dim);
    3142          195 :                       stride = gfc_evaluate_now (stride, &outer_loop->pre);
    3143          195 :                       info->stride[dim] = stride;
    3144              :                     }
    3145              :               }
    3146         9600 :             gfc_add_block_to_block (&outer_loop->post, &se.post);
    3147         9600 :             gfc_add_block_to_block (&outer_loop->post, &se.finalblock);
    3148         9600 :             ss_info->string_length = se.string_length;
    3149              :           }
    3150         9600 :           break;
    3151              : 
    3152        43799 :         case GFC_SS_CONSTRUCTOR:
    3153        43799 :           if (expr->ts.type == BT_CHARACTER
    3154         8238 :               && ss_info->string_length == NULL
    3155         8238 :               && expr->ts.u.cl
    3156         8238 :               && expr->ts.u.cl->length
    3157         7894 :               && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    3158              :             {
    3159         7836 :               gfc_init_se (&se, NULL);
    3160         7836 :               gfc_conv_expr_type (&se, expr->ts.u.cl->length,
    3161              :                                   gfc_charlen_type_node);
    3162         7836 :               ss_info->string_length = se.expr;
    3163         7836 :               gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    3164         7836 :               gfc_add_block_to_block (&outer_loop->post, &se.post);
    3165              :             }
    3166        43799 :           trans_array_constructor (ss, where);
    3167        43799 :           break;
    3168              : 
    3169              :         case GFC_SS_TEMP:
    3170              :         case GFC_SS_COMPONENT:
    3171              :           /* Do nothing.  These are handled elsewhere.  */
    3172              :           break;
    3173              : 
    3174            0 :         default:
    3175            0 :           gcc_unreachable ();
    3176              :         }
    3177              :     }
    3178              : 
    3179       193895 :   if (!subscript)
    3180       189728 :     for (nested_loop = loop->nested; nested_loop;
    3181         3364 :          nested_loop = nested_loop->next)
    3182         3364 :       gfc_add_loop_ss_code (nested_loop, nested_loop->ss, subscript, where);
    3183              : }
    3184              : 
    3185              : 
    3186              : /* Given an array descriptor expression DESCR and its data pointer DATA, decide
    3187              :    whether to either save the data pointer to a variable and use the variable or
    3188              :    use the data pointer expression directly without any intermediary variable.
    3189              :    */
    3190              : 
    3191              : static bool
    3192       131605 : save_descriptor_data (tree descr, tree data)
    3193              : {
    3194       131605 :   return !(DECL_P (data)
    3195       120109 :            || (TREE_CODE (data) == ADDR_EXPR
    3196        70772 :                && DECL_P (TREE_OPERAND (data, 0)))
    3197        52474 :            || (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (descr))
    3198        48897 :                && TREE_CODE (descr) == COMPONENT_REF
    3199        11657 :                && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (descr, 0)))));
    3200              : }
    3201              : 
    3202              : 
    3203              : /* Type of the DATA argument passed to walk_tree by substitute_subexpr_in_expr
    3204              :    and used by maybe_substitute_expr.  */
    3205              : 
    3206              : typedef struct
    3207              : {
    3208              :   tree target, repl;
    3209              : }
    3210              : substitute_t;
    3211              : 
    3212              : 
    3213              : /* Check if the expression in *TP is equal to the substitution target provided
    3214              :    in DATA->TARGET and replace it with DATA->REPL in that case.   This is a
    3215              :    callback function for use with walk_tree.  */
    3216              : 
    3217              : static tree
    3218        22659 : maybe_substitute_expr (tree *tp, int *walk_subtree, void *data)
    3219              : {
    3220        22659 :   substitute_t *subst = (substitute_t *) data;
    3221        22659 :   if (*tp == subst->target)
    3222              :     {
    3223         4312 :       *tp = subst->repl;
    3224         4312 :       *walk_subtree = 0;
    3225              :     }
    3226              : 
    3227        22659 :   return NULL_TREE;
    3228              : }
    3229              : 
    3230              : 
    3231              : /* Substitute in EXPR any occurrence of TARGET with REPLACEMENT.  */
    3232              : 
    3233              : static void
    3234         3969 : substitute_subexpr_in_expr (tree target, tree replacement, tree expr)
    3235              : {
    3236         3969 :   substitute_t subst;
    3237         3969 :   subst.target = target;
    3238         3969 :   subst.repl = replacement;
    3239              : 
    3240         3969 :   walk_tree (&expr, maybe_substitute_expr, &subst, nullptr);
    3241         3969 : }
    3242              : 
    3243              : 
    3244              : /* Save REF to a fresh variable in all of REPLACEMENT_ROOTS, appending extra
    3245              :    code to CODE.  Before returning, add REF to REPLACEMENT_ROOTS and clear
    3246              :    REF.  */
    3247              : 
    3248              : static void
    3249         3791 : save_ref (tree &code, tree &ref, vec<tree> &replacement_roots)
    3250              : {
    3251         3791 :   stmtblock_t tmp_block;
    3252         3791 :   gfc_init_block (&tmp_block);
    3253         3791 :   tree var = gfc_evaluate_now (ref, &tmp_block);
    3254         3791 :   gfc_add_expr_to_block (&tmp_block, code);
    3255         3791 :   code = gfc_finish_block (&tmp_block);
    3256              : 
    3257         3791 :   unsigned i;
    3258         3791 :   tree repl_root;
    3259         7760 :   FOR_EACH_VEC_ELT (replacement_roots, i, repl_root)
    3260         3969 :     substitute_subexpr_in_expr (ref, var, repl_root);
    3261              : 
    3262         3791 :   replacement_roots.safe_push (ref);
    3263         3791 :   ref = NULL_TREE;
    3264         3791 : }
    3265              : 
    3266              : 
    3267              : /* If REF isn't shared with code in PREVIOUS_CODE, replace it with a fresh
    3268              :    variable in all of REPLACEMENT_ROOTS, appending extra code to CODE.  */
    3269              : 
    3270              : static void
    3271         3863 : maybe_save_ref (tree &code, tree &ref, vec<tree> &replacement_roots,
    3272              :                 stmtblock_t *previous_code)
    3273              : {
    3274         3863 :   if (find_tree (previous_code->head, ref))
    3275              :     return;
    3276              : 
    3277         3791 :   save_ref (code, ref, replacement_roots);
    3278              : }
    3279              : 
    3280              : 
    3281              : /* Save the descriptor reference VALUE to storage pointed by DESC_PTR.  Before
    3282              :    that, try to create fresh variables to factor subexpressions of VALUE, if
    3283              :    those subexpressions aren't shared with code in PRELIMINARY_CODE.  Add any
    3284              :    necessary additional code (initialization of variables typically) to BLOCK.
    3285              : 
    3286              :    The candidate references to factoring are dereferenced pointers because they
    3287              :    are cheap to copy and array descriptors because they are often the base of
    3288              :    multiple subreferences.  */
    3289              : 
    3290              : static void
    3291       331414 : set_factored_descriptor_value (tree *desc_ptr, tree value, stmtblock_t *block,
    3292              :                                stmtblock_t *preliminary_code)
    3293              : {
    3294              :   /* As the reference is processed from outer to inner, variable definitions
    3295              :      will be generated in reversed order, so can't be put directly in BLOCK.
    3296              :      We use temporary blocks instead, which we save in ACCUMULATED_CODE, and
    3297              :      only append to BLOCK at the end.  */
    3298       331414 :   tree accumulated_code = NULL_TREE;
    3299              : 
    3300              :   /* The current candidate to factoring.  */
    3301       331414 :   tree saveable_ref = NULL_TREE;
    3302              : 
    3303              :   /* The root expressions in which we look for subexpressions to replace with
    3304              :      variables.  */
    3305       331414 :   auto_vec<tree> replacement_roots;
    3306       331414 :   replacement_roots.safe_push (value);
    3307              : 
    3308       331414 :   tree data_ref = value;
    3309       331414 :   tree next_ref = NULL_TREE;
    3310              : 
    3311              :   /* If the candidate reference is not followed by a subreference, it can't be
    3312              :      saved to a variable as it may be reallocatable, and we have to keep the
    3313              :      parent reference to be able to store the new pointer value in case of
    3314              :      reallocation.  */
    3315       331414 :   bool maybe_reallocatable = true;
    3316              : 
    3317       553586 :   while (true)
    3318              :     {
    3319       442500 :       if (!maybe_reallocatable
    3320       442500 :           && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (data_ref)))
    3321         2476 :         saveable_ref = data_ref;
    3322              : 
    3323       442500 :       if (TREE_CODE (data_ref) == INDIRECT_REF)
    3324              :         {
    3325        59688 :           next_ref = TREE_OPERAND (data_ref, 0);
    3326              : 
    3327        59688 :           if (!maybe_reallocatable)
    3328              :             {
    3329        15173 :               if (saveable_ref != NULL_TREE && saveable_ref != data_ref)
    3330              :                 {
    3331              :                   /* A reference worth saving has been seen, and now the pointer
    3332              :                      to the current reference is also worth saving.  If the
    3333              :                      previous reference to save wasn't the current one, do save
    3334              :                      it now.  Otherwise drop it as we prefer saving the
    3335              :                      pointer.  */
    3336         1893 :                   maybe_save_ref (accumulated_code, saveable_ref,
    3337              :                                   replacement_roots, preliminary_code);
    3338              :                 }
    3339              : 
    3340              :               /* Don't evaluate the pointer to a variable yet; do it only if the
    3341              :                  variable would be significantly more simple than the reference
    3342              :                  it replaces.  That is if the reference contains anything
    3343              :                  different from NOPs, COMPONENTs and DECLs.  */
    3344        15173 :               saveable_ref = next_ref;
    3345              :             }
    3346              :         }
    3347       382812 :       else if (TREE_CODE (data_ref) == COMPONENT_REF)
    3348              :         {
    3349        42087 :           maybe_reallocatable = false;
    3350        42087 :           next_ref = TREE_OPERAND (data_ref, 0);
    3351              :         }
    3352       340725 :       else if (TREE_CODE (data_ref) == NOP_EXPR)
    3353         3743 :         next_ref = TREE_OPERAND (data_ref, 0);
    3354              :       else
    3355              :         {
    3356       336982 :           if (DECL_P (data_ref))
    3357              :             break;
    3358              : 
    3359         7216 :           if (TREE_CODE (data_ref) == ARRAY_REF)
    3360              :             {
    3361         5568 :               maybe_reallocatable = false;
    3362         5568 :               next_ref = TREE_OPERAND (data_ref, 0);
    3363              :             }
    3364              : 
    3365         7216 :           if (saveable_ref != NULL_TREE)
    3366              :             /* We have seen a reference worth saving.  Do it now.  */
    3367         1970 :             maybe_save_ref (accumulated_code, saveable_ref, replacement_roots,
    3368              :                             preliminary_code);
    3369              : 
    3370         7216 :           if (TREE_CODE (data_ref) != ARRAY_REF)
    3371              :             break;
    3372              :         }
    3373              : 
    3374       111086 :       data_ref = next_ref;
    3375              :     }
    3376              : 
    3377       331414 :   *desc_ptr = value;
    3378       331414 :   gfc_add_expr_to_block (block, accumulated_code);
    3379       331414 : }
    3380              : 
    3381              : 
    3382              : /* Translate expressions for the descriptor and data pointer of a SS.  */
    3383              : /*GCC ARRAYS*/
    3384              : 
    3385              : static void
    3386       331414 : gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
    3387              : {
    3388       331414 :   gfc_se se;
    3389       331414 :   gfc_ss_info *ss_info;
    3390       331414 :   gfc_array_info *info;
    3391       331414 :   tree tmp;
    3392              : 
    3393       331414 :   ss_info = ss->info;
    3394       331414 :   info = &ss_info->data.array;
    3395              : 
    3396              :   /* Get the descriptor for the array to be scalarized.  */
    3397       331414 :   gcc_assert (ss_info->expr->expr_type == EXPR_VARIABLE);
    3398       331414 :   gfc_init_se (&se, NULL);
    3399       331414 :   se.descriptor_only = 1;
    3400       331414 :   gfc_conv_expr_lhs (&se, ss_info->expr);
    3401       331414 :   stmtblock_t tmp_block;
    3402       331414 :   gfc_init_block (&tmp_block);
    3403       331414 :   set_factored_descriptor_value (&info->descriptor, se.expr, &tmp_block,
    3404              :                                  &se.pre);
    3405       331414 :   gfc_add_block_to_block (block, &se.pre);
    3406       331414 :   gfc_add_block_to_block (block, &tmp_block);
    3407       331414 :   ss_info->string_length = se.string_length;
    3408       331414 :   ss_info->class_container = se.class_container;
    3409              : 
    3410       331414 :   if (base)
    3411              :     {
    3412       124823 :       if (ss_info->expr->ts.type == BT_CHARACTER && !ss_info->expr->ts.deferred
    3413        22940 :           && ss_info->expr->ts.u.cl->length == NULL)
    3414              :         {
    3415              :           /* Emit a DECL_EXPR for the variable sized array type in
    3416              :              GFC_TYPE_ARRAY_DATAPTR_TYPE so the gimplification of its type
    3417              :              sizes works correctly.  */
    3418         1127 :           tree arraytype = TREE_TYPE (
    3419              :                 GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (info->descriptor)));
    3420         1127 :           if (! TYPE_NAME (arraytype))
    3421          911 :             TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
    3422              :                                                 NULL_TREE, arraytype);
    3423         1127 :           gfc_add_expr_to_block (block, build1 (DECL_EXPR, arraytype,
    3424         1127 :                                                 TYPE_NAME (arraytype)));
    3425              :         }
    3426              :       /* Also the data pointer.  */
    3427       124823 :       tmp = gfc_conv_array_data (se.expr);
    3428              :       /* If this is a variable or address or a class array, use it directly.
    3429              :          Otherwise we must evaluate it now to avoid breaking dependency
    3430              :          analysis by pulling the expressions for elemental array indices
    3431              :          inside the loop.  */
    3432       124823 :       if (save_descriptor_data (se.expr, tmp) && !ss->is_alloc_lhs)
    3433        36844 :         tmp = gfc_evaluate_now (tmp, block);
    3434       124823 :       info->data = tmp;
    3435              : 
    3436       124823 :       tmp = gfc_conv_array_offset (se.expr);
    3437       124823 :       if (!ss->is_alloc_lhs)
    3438       118244 :         tmp = gfc_evaluate_now (tmp, block);
    3439       124823 :       info->offset = tmp;
    3440              : 
    3441              :       /* Make absolutely sure that the saved_offset is indeed saved
    3442              :          so that the variable is still accessible after the loops
    3443              :          are translated.  */
    3444       124823 :       info->saved_offset = info->offset;
    3445              :     }
    3446       331414 : }
    3447              : 
    3448              : 
    3449              : /* Initialize a gfc_loopinfo structure.  */
    3450              : 
    3451              : void
    3452       193590 : gfc_init_loopinfo (gfc_loopinfo * loop)
    3453              : {
    3454       193590 :   int n;
    3455              : 
    3456       193590 :   memset (loop, 0, sizeof (gfc_loopinfo));
    3457       193590 :   gfc_init_block (&loop->pre);
    3458       193590 :   gfc_init_block (&loop->post);
    3459              : 
    3460              :   /* Initially scalarize in order and default to no loop reversal.  */
    3461      3291030 :   for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
    3462              :     {
    3463      2903850 :       loop->order[n] = n;
    3464      2903850 :       loop->reverse[n] = GFC_INHIBIT_REVERSE;
    3465              :     }
    3466              : 
    3467       193590 :   loop->ss = gfc_ss_terminator;
    3468       193590 : }
    3469              : 
    3470              : 
    3471              : /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
    3472              :    chain.  */
    3473              : 
    3474              : void
    3475       193266 : gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
    3476              : {
    3477       193266 :   se->loop = loop;
    3478       193266 : }
    3479              : 
    3480              : 
    3481              : /* Return an expression for the data pointer of an array.  */
    3482              : 
    3483              : tree
    3484       340470 : gfc_conv_array_data (tree descriptor)
    3485              : {
    3486       340470 :   tree type;
    3487              : 
    3488       340470 :   type = TREE_TYPE (descriptor);
    3489       340470 :   if (GFC_ARRAY_TYPE_P (type))
    3490              :     {
    3491       238701 :       if (TREE_CODE (type) == POINTER_TYPE)
    3492              :         return descriptor;
    3493              :       else
    3494              :         {
    3495              :           /* Descriptorless arrays.  */
    3496       177808 :           return gfc_build_addr_expr (NULL_TREE, descriptor);
    3497              :         }
    3498              :     }
    3499              :   else
    3500       101769 :     return gfc_conv_descriptor_data_get (descriptor);
    3501              : }
    3502              : 
    3503              : 
    3504              : /* Return an expression for the base offset of an array.  */
    3505              : 
    3506              : tree
    3507       253175 : gfc_conv_array_offset (tree descriptor)
    3508              : {
    3509       253175 :   tree type;
    3510              : 
    3511       253175 :   type = TREE_TYPE (descriptor);
    3512       253175 :   if (GFC_ARRAY_TYPE_P (type))
    3513       180650 :     return GFC_TYPE_ARRAY_OFFSET (type);
    3514              :   else
    3515        72525 :     return gfc_conv_descriptor_offset_get (descriptor);
    3516              : }
    3517              : 
    3518              : 
    3519              : /* Get an expression for the array stride.  */
    3520              : 
    3521              : tree
    3522       503395 : gfc_conv_array_stride (tree descriptor, int dim)
    3523              : {
    3524       503395 :   tree tmp;
    3525       503395 :   tree type;
    3526              : 
    3527       503395 :   type = TREE_TYPE (descriptor);
    3528              : 
    3529              :   /* For descriptorless arrays use the array size.  */
    3530       503395 :   tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
    3531       503395 :   if (tmp != NULL_TREE)
    3532              :     return tmp;
    3533              : 
    3534       115490 :   tmp = gfc_conv_descriptor_stride_get (descriptor, gfc_rank_cst[dim]);
    3535       115490 :   return tmp;
    3536              : }
    3537              : 
    3538              : 
    3539              : /* Like gfc_conv_array_stride, but for the lower bound.  */
    3540              : 
    3541              : tree
    3542       322645 : gfc_conv_array_lbound (tree descriptor, int dim)
    3543              : {
    3544       322645 :   tree tmp;
    3545       322645 :   tree type;
    3546              : 
    3547       322645 :   type = TREE_TYPE (descriptor);
    3548              : 
    3549       322645 :   tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
    3550       322645 :   if (tmp != NULL_TREE)
    3551              :     return tmp;
    3552              : 
    3553        18781 :   tmp = gfc_conv_descriptor_lbound_get (descriptor, gfc_rank_cst[dim]);
    3554        18781 :   return tmp;
    3555              : }
    3556              : 
    3557              : 
    3558              : /* Like gfc_conv_array_stride, but for the upper bound.  */
    3559              : 
    3560              : tree
    3561       209020 : gfc_conv_array_ubound (tree descriptor, int dim)
    3562              : {
    3563       209020 :   tree tmp;
    3564       209020 :   tree type;
    3565              : 
    3566       209020 :   type = TREE_TYPE (descriptor);
    3567              : 
    3568       209020 :   tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
    3569       209020 :   if (tmp != NULL_TREE)
    3570              :     return tmp;
    3571              : 
    3572              :   /* This should only ever happen when passing an assumed shape array
    3573              :      as an actual parameter.  The value will never be used.  */
    3574         8099 :   if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
    3575          554 :     return gfc_index_zero_node;
    3576              : 
    3577         7545 :   tmp = gfc_conv_descriptor_ubound_get (descriptor, gfc_rank_cst[dim]);
    3578         7545 :   return tmp;
    3579              : }
    3580              : 
    3581              : 
    3582              : /* Generate abridged name of a part-ref for use in bounds-check message.
    3583              :    Cases:
    3584              :    (1) for an ordinary array variable x return "x"
    3585              :    (2) for z a DT scalar and array component x (at level 1) return "z%%x"
    3586              :    (3) for z a DT scalar and array component x (at level > 1) or
    3587              :        for z a DT array and array x (at any number of levels): "z...%%x"
    3588              :  */
    3589              : 
    3590              : static char *
    3591        36604 : abridged_ref_name (gfc_expr * expr, gfc_array_ref * ar)
    3592              : {
    3593        36604 :   gfc_ref *ref;
    3594        36604 :   gfc_symbol *sym;
    3595        36604 :   char *ref_name = NULL;
    3596        36604 :   const char *comp_name = NULL;
    3597        36604 :   int len_sym, last_len = 0, level = 0;
    3598        36604 :   bool sym_is_array;
    3599              : 
    3600        36604 :   gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->ref != NULL);
    3601              : 
    3602        36604 :   sym = expr->symtree->n.sym;
    3603        72821 :   sym_is_array = (sym->ts.type != BT_CLASS
    3604        36604 :                   ? sym->as != NULL
    3605          387 :                   : IS_CLASS_ARRAY (sym));
    3606        36604 :   len_sym = strlen (sym->name);
    3607              : 
    3608              :   /* Scan ref chain to get name of the array component (when ar != NULL) or
    3609              :      array section, determine depth and remember its component name.  */
    3610        52135 :   for (ref = expr->ref; ref; ref = ref->next)
    3611              :     {
    3612        38053 :       if (ref->type == REF_COMPONENT
    3613         1048 :           && strcmp (ref->u.c.component->name, "_data") != 0)
    3614              :         {
    3615          918 :           level++;
    3616          918 :           comp_name = ref->u.c.component->name;
    3617          918 :           continue;
    3618              :         }
    3619              : 
    3620        37135 :       if (ref->type != REF_ARRAY)
    3621          150 :         continue;
    3622              : 
    3623        36985 :       if (ar)
    3624              :         {
    3625        15971 :           if (&ref->u.ar == ar)
    3626              :             break;
    3627              :         }
    3628        21014 :       else if (ref->u.ar.type == AR_SECTION)
    3629              :         break;
    3630              :     }
    3631              : 
    3632        36604 :   if (level > 0)
    3633          800 :     last_len = strlen (comp_name);
    3634              : 
    3635              :   /* Provide a buffer sufficiently large to hold "x...%%z".  */
    3636        36604 :   ref_name = XNEWVEC (char, len_sym + last_len + 6);
    3637        36604 :   strcpy (ref_name, sym->name);
    3638              : 
    3639        36604 :   if (level == 1 && !sym_is_array)
    3640              :     {
    3641          442 :       strcat (ref_name, "%%");
    3642          442 :       strcat (ref_name, comp_name);
    3643              :     }
    3644        36162 :   else if (level > 0)
    3645              :     {
    3646          358 :       strcat (ref_name, "...%%");
    3647          358 :       strcat (ref_name, comp_name);
    3648              :     }
    3649              : 
    3650        36604 :   return ref_name;
    3651              : }
    3652              : 
    3653              : 
    3654              : /* Generate code to perform an array index bound check.  */
    3655              : 
    3656              : static tree
    3657         5774 : trans_array_bound_check (stmtblock_t *block, gfc_ss *ss, tree index, int n,
    3658              :                          locus * where, bool check_upper,
    3659              :                          const char *compname = NULL)
    3660              : {
    3661         5774 :   tree fault;
    3662         5774 :   tree tmp_lo, tmp_up;
    3663         5774 :   tree descriptor;
    3664         5774 :   char *msg;
    3665         5774 :   char *ref_name = NULL;
    3666         5774 :   const char * name = NULL;
    3667         5774 :   gfc_expr *expr;
    3668              : 
    3669         5774 :   if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
    3670              :     return index;
    3671              : 
    3672          252 :   descriptor = ss->info->data.array.descriptor;
    3673              : 
    3674          252 :   index = gfc_evaluate_now (index, block);
    3675              : 
    3676              :   /* We find a name for the error message.  */
    3677          252 :   name = ss->info->expr->symtree->n.sym->name;
    3678          252 :   gcc_assert (name != NULL);
    3679              : 
    3680              :   /* When we have a component ref, get name of the array section.
    3681              :      Note that there can only be one part ref.  */
    3682          252 :   expr = ss->info->expr;
    3683          252 :   if (expr->ref && !compname)
    3684          160 :     name = ref_name = abridged_ref_name (expr, NULL);
    3685              : 
    3686          252 :   if (VAR_P (descriptor))
    3687          162 :     name = IDENTIFIER_POINTER (DECL_NAME (descriptor));
    3688              : 
    3689              :   /* Use given (array component) name.  */
    3690          252 :   if (compname)
    3691           92 :     name = compname;
    3692              : 
    3693              :   /* If upper bound is present, include both bounds in the error message.  */
    3694          252 :   if (check_upper)
    3695              :     {
    3696          225 :       tmp_lo = gfc_conv_array_lbound (descriptor, n);
    3697          225 :       tmp_up = gfc_conv_array_ubound (descriptor, n);
    3698              : 
    3699          225 :       if (name)
    3700          225 :         msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    3701              :                          "outside of expected range (%%ld:%%ld)", n+1, name);
    3702              :       else
    3703            0 :         msg = xasprintf ("Index '%%ld' of dimension %d "
    3704              :                          "outside of expected range (%%ld:%%ld)", n+1);
    3705              : 
    3706          225 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3707              :                                index, tmp_lo);
    3708          225 :       gfc_trans_runtime_check (true, false, fault, block, where, msg,
    3709              :                                fold_convert (long_integer_type_node, index),
    3710              :                                fold_convert (long_integer_type_node, tmp_lo),
    3711              :                                fold_convert (long_integer_type_node, tmp_up));
    3712          225 :       fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3713              :                                index, tmp_up);
    3714          225 :       gfc_trans_runtime_check (true, false, fault, block, where, msg,
    3715              :                                fold_convert (long_integer_type_node, index),
    3716              :                                fold_convert (long_integer_type_node, tmp_lo),
    3717              :                                fold_convert (long_integer_type_node, tmp_up));
    3718          225 :       free (msg);
    3719              :     }
    3720              :   else
    3721              :     {
    3722           27 :       tmp_lo = gfc_conv_array_lbound (descriptor, n);
    3723              : 
    3724           27 :       if (name)
    3725           27 :         msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    3726              :                          "below lower bound of %%ld", n+1, name);
    3727              :       else
    3728            0 :         msg = xasprintf ("Index '%%ld' of dimension %d "
    3729              :                          "below lower bound of %%ld", n+1);
    3730              : 
    3731           27 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    3732              :                                index, tmp_lo);
    3733           27 :       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           27 :       free (msg);
    3737              :     }
    3738              : 
    3739          252 :   free (ref_name);
    3740          252 :   return index;
    3741              : }
    3742              : 
    3743              : 
    3744              : /* Helper functions to detect impure functions in an expression.  */
    3745              : 
    3746              : static const char *impure_name = NULL;
    3747              : static bool
    3748          108 : expr_contains_impure_fcn (gfc_expr *e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    3749              :          int* g ATTRIBUTE_UNUSED)
    3750              : {
    3751          108 :   if (e && e->expr_type == EXPR_FUNCTION
    3752            6 :       && !gfc_pure_function (e, &impure_name)
    3753          111 :       && !gfc_implicit_pure_function (e))
    3754            3 :     return true;
    3755              : 
    3756              :   return false;
    3757              : }
    3758              : 
    3759              : static bool
    3760           92 : gfc_expr_contains_impure_fcn (gfc_expr *e)
    3761              : {
    3762           92 :   impure_name = NULL;
    3763           92 :   return gfc_traverse_expr (e, NULL, &expr_contains_impure_fcn, 0);
    3764              : }
    3765              : 
    3766              : 
    3767              : /* Generate code for bounds checking for elemental dimensions.  */
    3768              : 
    3769              : static void
    3770         6688 : array_bound_check_elemental (stmtblock_t *block, gfc_ss * ss, gfc_expr * expr)
    3771              : {
    3772         6688 :   gfc_array_ref *ar;
    3773         6688 :   gfc_ref *ref;
    3774         6688 :   char *var_name = NULL;
    3775         6688 :   int dim;
    3776              : 
    3777         6688 :   if (expr->expr_type == EXPR_VARIABLE)
    3778              :     {
    3779        12533 :       for (ref = expr->ref; ref; ref = ref->next)
    3780              :         {
    3781         6303 :           if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    3782              :             {
    3783         3953 :               ar = &ref->u.ar;
    3784         3953 :               var_name = abridged_ref_name (expr, ar);
    3785        12111 :               for (dim = 0; dim < ar->dimen; dim++)
    3786              :                 {
    3787         4205 :                   if (ar->dimen_type[dim] == DIMEN_ELEMENT)
    3788              :                     {
    3789           92 :                       if (gfc_expr_contains_impure_fcn (ar->start[dim]))
    3790            3 :                         gfc_warning_now (0, "Bounds checking of the elemental "
    3791              :                                          "index at %L will cause two calls to "
    3792              :                                          "%qs, which is not declared to be "
    3793              :                                          "PURE or is not implicitly pure.",
    3794            3 :                                          &ar->start[dim]->where, impure_name);
    3795           92 :                       gfc_se indexse;
    3796           92 :                       gfc_init_se (&indexse, NULL);
    3797           92 :                       gfc_conv_expr_type (&indexse, ar->start[dim],
    3798              :                                           gfc_array_index_type);
    3799           92 :                       gfc_add_block_to_block (block, &indexse.pre);
    3800           92 :                       trans_array_bound_check (block, ss, indexse.expr, dim,
    3801              :                                                &ar->where,
    3802           92 :                                                ar->as->type != AS_ASSUMED_SIZE
    3803            0 :                                                || dim < ar->dimen - 1,
    3804              :                                                var_name);
    3805              :                     }
    3806              :                 }
    3807         3953 :               free (var_name);
    3808              :             }
    3809              :         }
    3810              :     }
    3811         6688 : }
    3812              : 
    3813              : 
    3814              : /* Return the offset for an index.  Performs bound checking for elemental
    3815              :    dimensions.  Single element references are processed separately.
    3816              :    DIM is the array dimension, I is the loop dimension.  */
    3817              : 
    3818              : static tree
    3819       256724 : conv_array_index_offset (gfc_se * se, gfc_ss * ss, int dim, int i,
    3820              :                          gfc_array_ref * ar, tree stride)
    3821              : {
    3822       256724 :   gfc_array_info *info;
    3823       256724 :   tree index;
    3824       256724 :   tree desc;
    3825       256724 :   tree data;
    3826              : 
    3827       256724 :   info = &ss->info->data.array;
    3828              : 
    3829              :   /* Get the index into the array for this dimension.  */
    3830       256724 :   if (ar)
    3831              :     {
    3832       182404 :       gcc_assert (ar->type != AR_ELEMENT);
    3833       182404 :       switch (ar->dimen_type[dim])
    3834              :         {
    3835            0 :         case DIMEN_THIS_IMAGE:
    3836            0 :           gcc_unreachable ();
    3837         4699 :           break;
    3838         4699 :         case DIMEN_ELEMENT:
    3839              :           /* Elemental dimension.  */
    3840         4699 :           gcc_assert (info->subscript[dim]
    3841              :                       && info->subscript[dim]->info->type == GFC_SS_SCALAR);
    3842              :           /* We've already translated this value outside the loop.  */
    3843         4699 :           index = info->subscript[dim]->info->data.scalar.value;
    3844              : 
    3845         9474 :           index = trans_array_bound_check (&se->pre, ss, index, dim, &ar->where,
    3846         4699 :                                            ar->as->type != AS_ASSUMED_SIZE
    3847           76 :                                            || dim < ar->dimen - 1);
    3848         4699 :           break;
    3849              : 
    3850          983 :         case DIMEN_VECTOR:
    3851          983 :           gcc_assert (info && se->loop);
    3852          983 :           gcc_assert (info->subscript[dim]
    3853              :                       && info->subscript[dim]->info->type == GFC_SS_VECTOR);
    3854          983 :           desc = info->subscript[dim]->info->data.array.descriptor;
    3855              : 
    3856              :           /* Get a zero-based index into the vector.  */
    3857          983 :           index = fold_build2_loc (input_location, MINUS_EXPR,
    3858              :                                    gfc_array_index_type,
    3859              :                                    se->loop->loopvar[i], se->loop->from[i]);
    3860              : 
    3861              :           /* Multiply the index by the stride.  */
    3862          983 :           index = fold_build2_loc (input_location, MULT_EXPR,
    3863              :                                    gfc_array_index_type,
    3864              :                                    index, gfc_conv_array_stride (desc, 0));
    3865              : 
    3866              :           /* Read the vector to get an index into info->descriptor.  */
    3867          983 :           data = build_fold_indirect_ref_loc (input_location,
    3868              :                                           gfc_conv_array_data (desc));
    3869          983 :           index = gfc_build_array_ref (data, index, NULL);
    3870          983 :           index = gfc_evaluate_now (index, &se->pre);
    3871          983 :           index = fold_convert (gfc_array_index_type, index);
    3872              : 
    3873              :           /* Do any bounds checking on the final info->descriptor index.  */
    3874         1972 :           index = trans_array_bound_check (&se->pre, ss, index, dim, &ar->where,
    3875          983 :                                            ar->as->type != AS_ASSUMED_SIZE
    3876            6 :                                            || dim < ar->dimen - 1);
    3877          983 :           break;
    3878              : 
    3879       176722 :         case DIMEN_RANGE:
    3880              :           /* Scalarized dimension.  */
    3881       176722 :           gcc_assert (info && se->loop);
    3882              : 
    3883              :           /* Multiply the loop variable by the stride and delta.  */
    3884       176722 :           index = se->loop->loopvar[i];
    3885       176722 :           if (!integer_onep (info->stride[dim]))
    3886         6990 :             index = fold_build2_loc (input_location, MULT_EXPR,
    3887              :                                      gfc_array_index_type, index,
    3888              :                                      info->stride[dim]);
    3889       176722 :           if (!integer_zerop (info->delta[dim]))
    3890        68178 :             index = fold_build2_loc (input_location, PLUS_EXPR,
    3891              :                                      gfc_array_index_type, index,
    3892              :                                      info->delta[dim]);
    3893              :           break;
    3894              : 
    3895            0 :         default:
    3896            0 :           gcc_unreachable ();
    3897              :         }
    3898              :     }
    3899              :   else
    3900              :     {
    3901              :       /* Temporary array or derived type component.  */
    3902        74320 :       gcc_assert (se->loop);
    3903        74320 :       index = se->loop->loopvar[se->loop->order[i]];
    3904              : 
    3905              :       /* Pointer functions can have stride[0] different from unity.
    3906              :          Use the stride returned by the function call and stored in
    3907              :          the descriptor for the temporary.  */
    3908        74320 :       if (se->ss && se->ss->info->type == GFC_SS_FUNCTION
    3909         8056 :           && se->ss->info->expr
    3910         8056 :           && se->ss->info->expr->symtree
    3911         8056 :           && se->ss->info->expr->symtree->n.sym->result
    3912         7616 :           && se->ss->info->expr->symtree->n.sym->result->attr.pointer)
    3913          144 :         stride = gfc_conv_descriptor_stride_get (info->descriptor,
    3914              :                                                  gfc_rank_cst[dim]);
    3915              : 
    3916        74320 :       if (info->delta[dim] && !integer_zerop (info->delta[dim]))
    3917          804 :         index = fold_build2_loc (input_location, PLUS_EXPR,
    3918              :                                  gfc_array_index_type, index, info->delta[dim]);
    3919              :     }
    3920              : 
    3921              :   /* Multiply by the stride.  */
    3922       256724 :   if (stride != NULL && !integer_onep (stride))
    3923        78197 :     index = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    3924              :                              index, stride);
    3925              : 
    3926       256724 :   return index;
    3927              : }
    3928              : 
    3929              : 
    3930              : /* Build a scalarized array reference using the vptr 'size'.  */
    3931              : 
    3932              : static bool
    3933       196991 : build_class_array_ref (gfc_se *se, tree base, tree index)
    3934              : {
    3935       196991 :   tree size;
    3936       196991 :   tree decl = NULL_TREE;
    3937       196991 :   tree tmp;
    3938       196991 :   gfc_expr *expr = se->ss->info->expr;
    3939       196991 :   gfc_expr *class_expr;
    3940       196991 :   gfc_typespec *ts;
    3941       196991 :   gfc_symbol *sym;
    3942              : 
    3943       196991 :   tmp = !VAR_P (base) ? gfc_get_class_from_expr (base) : NULL_TREE;
    3944              : 
    3945        92395 :   if (tmp != NULL_TREE)
    3946              :     decl = tmp;
    3947              :   else
    3948              :     {
    3949              :       /* The base expression does not contain a class component, either
    3950              :          because it is a temporary array or array descriptor.  Class
    3951              :          array functions are correctly resolved above.  */
    3952       193552 :       if (!expr
    3953       193552 :           || (expr->ts.type != BT_CLASS
    3954       179440 :               && !gfc_is_class_array_ref (expr, NULL)))
    3955              :         return false;
    3956              : 
    3957              :       /* Obtain the expression for the class entity or component that is
    3958              :          followed by an array reference, which is not an element, so that
    3959              :          the span of the array can be obtained.  */
    3960          483 :       class_expr = gfc_find_and_cut_at_last_class_ref (expr, false, &ts);
    3961              : 
    3962          483 :       if (!ts)
    3963              :         return false;
    3964              : 
    3965          458 :       sym = (!class_expr && expr) ? expr->symtree->n.sym : NULL;
    3966            0 :       if (sym && sym->attr.function
    3967            0 :           && sym == sym->result
    3968            0 :           && sym->backend_decl == current_function_decl)
    3969              :         /* The temporary is the data field of the class data component
    3970              :            of the current function.  */
    3971            0 :         decl = gfc_get_fake_result_decl (sym, 0);
    3972          458 :       else if (sym)
    3973              :         {
    3974            0 :           if (decl == NULL_TREE)
    3975            0 :             decl = expr->symtree->n.sym->backend_decl;
    3976              :           /* For class arrays the tree containing the class is stored in
    3977              :              GFC_DECL_SAVED_DESCRIPTOR of the sym's backend_decl.
    3978              :              For all others it's sym's backend_decl directly.  */
    3979            0 :           if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
    3980            0 :             decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    3981              :         }
    3982              :       else
    3983          458 :         decl = gfc_get_class_from_gfc_expr (class_expr);
    3984              : 
    3985          458 :       if (POINTER_TYPE_P (TREE_TYPE (decl)))
    3986            0 :         decl = build_fold_indirect_ref_loc (input_location, decl);
    3987              : 
    3988          458 :       if (!GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
    3989              :         return false;
    3990              :     }
    3991              : 
    3992         3897 :   se->class_vptr = gfc_evaluate_now (gfc_class_vptr_get (decl), &se->pre);
    3993              : 
    3994         3897 :   size = gfc_class_vtab_size_get (decl);
    3995              :   /* For unlimited polymorphic entities then _len component needs to be
    3996              :      multiplied with the size.  */
    3997         3897 :   size = gfc_resize_class_size_with_len (&se->pre, decl, size);
    3998         3897 :   size = fold_convert (TREE_TYPE (index), size);
    3999              : 
    4000              :   /* Return the element in the se expression.  */
    4001         3897 :   se->expr = gfc_build_spanned_array_ref (base, index, size);
    4002         3897 :   return true;
    4003              : }
    4004              : 
    4005              : 
    4006              : /* Indicates that the tree EXPR is a reference to an array that can’t
    4007              :    have any negative stride.  */
    4008              : 
    4009              : static bool
    4010       318796 : non_negative_strides_array_p (tree expr)
    4011              : {
    4012       332747 :   if (expr == NULL_TREE)
    4013              :     return false;
    4014              : 
    4015       332747 :   tree type = TREE_TYPE (expr);
    4016       332747 :   if (POINTER_TYPE_P (type))
    4017        75064 :     type = TREE_TYPE (type);
    4018              : 
    4019       332747 :   if (TYPE_LANG_SPECIFIC (type))
    4020              :     {
    4021       332747 :       gfc_array_kind array_kind = GFC_TYPE_ARRAY_AKIND (type);
    4022              : 
    4023       332747 :       if (array_kind == GFC_ARRAY_ALLOCATABLE
    4024       332747 :           || array_kind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
    4025              :         return true;
    4026              :     }
    4027              : 
    4028              :   /* An array with descriptor can have negative strides.
    4029              :      We try to be conservative and return false by default here
    4030              :      if we don’t recognize a contiguous array instead of
    4031              :      returning false if we can identify a non-contiguous one.  */
    4032       274850 :   if (!GFC_ARRAY_TYPE_P (type))
    4033              :     return false;
    4034              : 
    4035              :   /* If the array was originally a dummy with a descriptor, strides can be
    4036              :      negative.  */
    4037       239852 :   if (DECL_P (expr)
    4038       230811 :       && DECL_LANG_SPECIFIC (expr)
    4039        48714 :       && GFC_DECL_SAVED_DESCRIPTOR (expr)
    4040       253822 :       && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
    4041        13951 :     return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
    4042              : 
    4043              :   return true;
    4044              : }
    4045              : 
    4046              : 
    4047              : /* Build a scalarized reference to an array.  */
    4048              : 
    4049              : static void
    4050       196991 : gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar,
    4051              :                                bool tmp_array = false)
    4052              : {
    4053       196991 :   gfc_array_info *info;
    4054       196991 :   tree decl = NULL_TREE;
    4055       196991 :   tree index;
    4056       196991 :   tree base;
    4057       196991 :   gfc_ss *ss;
    4058       196991 :   gfc_expr *expr;
    4059       196991 :   int n;
    4060              : 
    4061       196991 :   ss = se->ss;
    4062       196991 :   expr = ss->info->expr;
    4063       196991 :   info = &ss->info->data.array;
    4064       196991 :   if (ar)
    4065       134805 :     n = se->loop->order[0];
    4066              :   else
    4067              :     n = 0;
    4068              : 
    4069       196991 :   index = conv_array_index_offset (se, ss, ss->dim[n], n, ar, info->stride0);
    4070              :   /* Add the offset for this dimension to the stored offset for all other
    4071              :      dimensions.  */
    4072       196991 :   if (info->offset && !integer_zerop (info->offset))
    4073       144487 :     index = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4074              :                              index, info->offset);
    4075              : 
    4076       196991 :   base = build_fold_indirect_ref_loc (input_location, info->data);
    4077              : 
    4078              :   /* Use the vptr 'size' field to access the element of a class array.  */
    4079       196991 :   if (build_class_array_ref (se, base, index))
    4080         3897 :     return;
    4081              : 
    4082       193094 :   if (get_CFI_desc (NULL, expr, &decl, ar))
    4083          442 :     decl = build_fold_indirect_ref_loc (input_location, decl);
    4084              : 
    4085              :   /* A pointer array component can be detected from its field decl. Fix
    4086              :      the descriptor, mark the resulting variable decl and pass it to
    4087              :      gfc_build_array_ref.  */
    4088       193094 :   if (is_span_addressed_array (info->descriptor)
    4089       193094 :       || (expr && ((expr->ts.deferred && info->descriptor
    4090         2842 :                     && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (info->descriptor)))
    4091       193094 :                    || (expr && gfc_expr_attr (expr).pdt_string))))
    4092              :     {
    4093         9348 :       if (TREE_CODE (info->descriptor) == COMPONENT_REF)
    4094         1642 :         decl = info->descriptor;
    4095         7706 :       else if (INDIRECT_REF_P (info->descriptor))
    4096         1485 :         decl = TREE_OPERAND (info->descriptor, 0);
    4097              : 
    4098         9348 :       if (decl == NULL_TREE)
    4099         6221 :         decl = info->descriptor;
    4100              :     }
    4101              : 
    4102       193094 :   bool non_negative_stride = tmp_array
    4103       193094 :                              || non_negative_strides_array_p (info->descriptor);
    4104       193094 :   se->expr = gfc_build_array_ref (base, index, decl,
    4105              :                                   non_negative_stride);
    4106              : }
    4107              : 
    4108              : 
    4109              : /* Translate access of temporary array.  */
    4110              : 
    4111              : void
    4112        62186 : gfc_conv_tmp_array_ref (gfc_se * se)
    4113              : {
    4114        62186 :   se->string_length = se->ss->info->string_length;
    4115        62186 :   gfc_conv_scalarized_array_ref (se, NULL, true);
    4116        62186 :   gfc_advance_se_ss_chain (se);
    4117        62186 : }
    4118              : 
    4119              : /* Add T to the offset pair *OFFSET, *CST_OFFSET.  */
    4120              : 
    4121              : static void
    4122       282018 : add_to_offset (tree *cst_offset, tree *offset, tree t)
    4123              : {
    4124       282018 :   if (TREE_CODE (t) == INTEGER_CST)
    4125       141932 :     *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
    4126              :   else
    4127              :     {
    4128       140086 :       if (!integer_zerop (*offset))
    4129        48969 :         *offset = fold_build2_loc (input_location, PLUS_EXPR,
    4130              :                                    gfc_array_index_type, *offset, t);
    4131              :       else
    4132        91117 :         *offset = t;
    4133              :     }
    4134       282018 : }
    4135              : 
    4136              : 
    4137              : static tree
    4138       187494 : build_array_ref (tree desc, tree offset, tree decl, tree vptr)
    4139              : {
    4140       187494 :   tree tmp;
    4141       187494 :   tree type;
    4142       187494 :   tree cdesc;
    4143              : 
    4144              :   /* For class arrays the class declaration is stored in the saved
    4145              :      descriptor.  */
    4146       187494 :   if (INDIRECT_REF_P (desc)
    4147         7374 :       && DECL_LANG_SPECIFIC (TREE_OPERAND (desc, 0))
    4148       189840 :       && GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)))
    4149          911 :     cdesc = gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (
    4150              :                                   TREE_OPERAND (desc, 0)));
    4151              :   else
    4152              :     cdesc = desc;
    4153              : 
    4154              :   /* Class container types do not always have the GFC_CLASS_TYPE_P
    4155              :      but the canonical type does.  */
    4156       187494 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (cdesc))
    4157       187494 :       && TREE_CODE (cdesc) == COMPONENT_REF)
    4158              :     {
    4159        11782 :       type = TREE_TYPE (TREE_OPERAND (cdesc, 0));
    4160        11782 :       if (TYPE_CANONICAL (type)
    4161        11782 :           && GFC_CLASS_TYPE_P (TYPE_CANONICAL (type)))
    4162              :         {
    4163         3643 :           vptr = gfc_class_vptr_get (TREE_OPERAND (cdesc, 0));
    4164              :           /* Pass the class container as decl so that gfc_build_array_ref can
    4165              :              correct the element size for an unlimited polymorphic character
    4166              :              payload (the _len field), which the vptr size alone omits.  Only do
    4167              :              this for a genuine array element reference; a scalar coarray has
    4168              :              nothing to span-correct and gfc_build_array_ref asserts decl is null
    4169              :              for it.  */
    4170         3643 :           if (decl == NULL_TREE
    4171         3643 :               && GFC_TYPE_ARRAY_RANK (TREE_TYPE (cdesc)) > 0)
    4172         3366 :             decl = TREE_OPERAND (cdesc, 0);
    4173              :         }
    4174              :     }
    4175              : 
    4176       187494 :   if (decl == NULL_TREE
    4177       187494 :       && is_span_addressed_array (desc))
    4178              :     decl = desc;
    4179              : 
    4180       187494 :   tmp = gfc_conv_array_data (desc);
    4181       187494 :   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    4182       187494 :   tmp = gfc_build_array_ref (tmp, offset, decl,
    4183              :                              non_negative_strides_array_p (desc),
    4184              :                              vptr);
    4185       187494 :   return tmp;
    4186              : }
    4187              : 
    4188              : 
    4189              : /* Build an array reference.  se->expr already holds the array descriptor.
    4190              :    This should be either a variable, indirect variable reference or component
    4191              :    reference.  For arrays which do not have a descriptor, se->expr will be
    4192              :    the data pointer.
    4193              :    a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
    4194              : 
    4195              : void
    4196       266639 : gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_expr *expr,
    4197              :                     locus * where)
    4198              : {
    4199       266639 :   int n;
    4200       266639 :   tree offset, cst_offset;
    4201       266639 :   tree tmp;
    4202       266639 :   tree stride;
    4203       266639 :   tree decl = NULL_TREE;
    4204       266639 :   gfc_se indexse;
    4205       266639 :   gfc_se tmpse;
    4206       266639 :   gfc_symbol * sym = expr->symtree->n.sym;
    4207       266639 :   char *var_name = NULL;
    4208              : 
    4209       266639 :   if (ar->stat)
    4210              :     {
    4211            3 :       gfc_se statse;
    4212              : 
    4213            3 :       gfc_init_se (&statse, NULL);
    4214            3 :       gfc_conv_expr_lhs (&statse, ar->stat);
    4215            3 :       gfc_add_block_to_block (&se->pre, &statse.pre);
    4216            3 :       gfc_add_modify (&se->pre, statse.expr, integer_zero_node);
    4217              :     }
    4218       266639 :   if (ar->dimen == 0)
    4219              :     {
    4220         4543 :       gcc_assert (ar->codimen || sym->attr.select_rank_temporary
    4221              :                   || (ar->as && ar->as->corank));
    4222              : 
    4223         4543 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
    4224          993 :         se->expr = build_fold_indirect_ref (gfc_conv_array_data (se->expr));
    4225              :       else
    4226              :         {
    4227         3550 :           if (GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr))
    4228         3550 :               && TREE_CODE (TREE_TYPE (se->expr)) == POINTER_TYPE)
    4229         2602 :             se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    4230              : 
    4231              :           /* Use the actual tree type and not the wrapped coarray.  */
    4232         3550 :           if (!se->want_pointer)
    4233         2581 :             se->expr = fold_convert (TYPE_MAIN_VARIANT (TREE_TYPE (se->expr)),
    4234              :                                      se->expr);
    4235              :         }
    4236              : 
    4237       139348 :       return;
    4238              :     }
    4239              : 
    4240              :   /* Handle scalarized references separately.  */
    4241       262096 :   if (ar->type != AR_ELEMENT)
    4242              :     {
    4243       134805 :       gfc_conv_scalarized_array_ref (se, ar);
    4244       134805 :       gfc_advance_se_ss_chain (se);
    4245       134805 :       return;
    4246              :     }
    4247              : 
    4248       127291 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    4249        11849 :     var_name = abridged_ref_name (expr, ar);
    4250              : 
    4251       127291 :   decl = se->expr;
    4252       127291 :   if (UNLIMITED_POLY(sym)
    4253          104 :       && IS_CLASS_ARRAY (sym)
    4254          103 :       && sym->attr.dummy
    4255           60 :       && ar->as->type != AS_DEFERRED)
    4256           48 :     decl = sym->backend_decl;
    4257              : 
    4258       127291 :   cst_offset = offset = gfc_index_zero_node;
    4259       127291 :   add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
    4260              : 
    4261              :   /* Calculate the offsets from all the dimensions.  Make sure to associate
    4262              :      the final offset so that we form a chain of loop invariant summands.  */
    4263       282018 :   for (n = ar->dimen - 1; n >= 0; n--)
    4264              :     {
    4265              :       /* Calculate the index for this dimension.  */
    4266       154727 :       gfc_init_se (&indexse, se);
    4267       154727 :       gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
    4268       154727 :       gfc_add_block_to_block (&se->pre, &indexse.pre);
    4269              : 
    4270       154727 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && ! expr->no_bounds_check)
    4271              :         {
    4272              :           /* Check array bounds.  */
    4273        15389 :           tree cond;
    4274        15389 :           char *msg;
    4275              : 
    4276              :           /* Evaluate the indexse.expr only once.  */
    4277        15389 :           indexse.expr = save_expr (indexse.expr);
    4278              : 
    4279              :           /* Lower bound.  */
    4280        15389 :           tmp = gfc_conv_array_lbound (decl, n);
    4281        15389 :           if (sym->attr.temporary)
    4282              :             {
    4283           18 :               gfc_init_se (&tmpse, se);
    4284           18 :               gfc_conv_expr_type (&tmpse, ar->as->lower[n],
    4285              :                                   gfc_array_index_type);
    4286           18 :               gfc_add_block_to_block (&se->pre, &tmpse.pre);
    4287           18 :               tmp = tmpse.expr;
    4288              :             }
    4289              : 
    4290        15389 :           cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4291              :                                   indexse.expr, tmp);
    4292        15389 :           msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    4293              :                            "below lower bound of %%ld", n+1, var_name);
    4294        15389 :           gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
    4295              :                                    fold_convert (long_integer_type_node,
    4296              :                                                  indexse.expr),
    4297              :                                    fold_convert (long_integer_type_node, tmp));
    4298        15389 :           free (msg);
    4299              : 
    4300              :           /* Upper bound, but not for the last dimension of assumed-size
    4301              :              arrays.  */
    4302        15389 :           if (n < ar->dimen - 1 || ar->as->type != AS_ASSUMED_SIZE)
    4303              :             {
    4304        13656 :               tmp = gfc_conv_array_ubound (decl, n);
    4305        13656 :               if (sym->attr.temporary)
    4306              :                 {
    4307           18 :                   gfc_init_se (&tmpse, se);
    4308           18 :                   gfc_conv_expr_type (&tmpse, ar->as->upper[n],
    4309              :                                       gfc_array_index_type);
    4310           18 :                   gfc_add_block_to_block (&se->pre, &tmpse.pre);
    4311           18 :                   tmp = tmpse.expr;
    4312              :                 }
    4313              : 
    4314        13656 :               cond = fold_build2_loc (input_location, GT_EXPR,
    4315              :                                       logical_type_node, indexse.expr, tmp);
    4316        13656 :               msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' "
    4317              :                                "above upper bound of %%ld", n+1, var_name);
    4318        13656 :               gfc_trans_runtime_check (true, false, cond, &se->pre, where, msg,
    4319              :                                    fold_convert (long_integer_type_node,
    4320              :                                                  indexse.expr),
    4321              :                                    fold_convert (long_integer_type_node, tmp));
    4322        13656 :               free (msg);
    4323              :             }
    4324              :         }
    4325              : 
    4326              :       /* Multiply the index by the stride.  */
    4327       154727 :       stride = gfc_conv_array_stride (decl, n);
    4328       154727 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    4329              :                              indexse.expr, stride);
    4330              : 
    4331              :       /* And add it to the total.  */
    4332       154727 :       add_to_offset (&cst_offset, &offset, tmp);
    4333              :     }
    4334              : 
    4335       127291 :   if (!integer_zerop (cst_offset))
    4336        67644 :     offset = fold_build2_loc (input_location, PLUS_EXPR,
    4337              :                               gfc_array_index_type, offset, cst_offset);
    4338              : 
    4339              :   /* A pointer array component can be detected from its field decl. Fix
    4340              :      the descriptor, mark the resulting variable decl and pass it to
    4341              :      build_array_ref.  */
    4342       127291 :   decl = NULL_TREE;
    4343       127291 :   if (get_CFI_desc (sym, expr, &decl, ar))
    4344         3589 :     decl = build_fold_indirect_ref_loc (input_location, decl);
    4345       126148 :   if (!expr->ts.deferred && !sym->attr.codimension
    4346       251214 :       && is_span_addressed_array (se->expr))
    4347              :     {
    4348         5373 :       if (INDIRECT_REF_P (se->expr))
    4349          990 :         decl = TREE_OPERAND (se->expr, 0);
    4350              :       else
    4351         4383 :         decl = se->expr;
    4352              :     }
    4353       121918 :   else if (expr->ts.deferred
    4354       120775 :            || (sym->ts.type == BT_CHARACTER
    4355        15395 :                && sym->attr.select_type_temporary)
    4356       240983 :            || (expr->ts.type == BT_CHARACTER
    4357        15826 :                && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr))
    4358         5374 :                && gfc_expr_attr (expr).pdt_string))
    4359              :     {
    4360         2961 :       decl = se->expr;
    4361         2961 :       if (INDIRECT_REF_P (decl))
    4362           20 :         decl = TREE_OPERAND (decl, 0);
    4363              :     }
    4364       118957 :   else if (sym->ts.type == BT_CLASS)
    4365              :     {
    4366         2167 :       if (UNLIMITED_POLY (sym))
    4367              :         {
    4368          103 :           gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
    4369          103 :           gfc_init_se (&tmpse, NULL);
    4370          103 :           gfc_conv_expr (&tmpse, class_expr);
    4371          103 :           if (!se->class_vptr)
    4372          103 :             se->class_vptr = gfc_class_vptr_get (tmpse.expr);
    4373          103 :           gfc_free_expr (class_expr);
    4374          103 :           decl = tmpse.expr;
    4375          103 :         }
    4376              :       else
    4377         2064 :         decl = NULL_TREE;
    4378              :     }
    4379              : 
    4380       127291 :   free (var_name);
    4381       127291 :   se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
    4382              : }
    4383              : 
    4384              : 
    4385              : /* Add the offset corresponding to array's ARRAY_DIM dimension and loop's
    4386              :    LOOP_DIM dimension (if any) to array's offset.  */
    4387              : 
    4388              : static void
    4389        59733 : add_array_offset (stmtblock_t *pblock, gfc_loopinfo *loop, gfc_ss *ss,
    4390              :                   gfc_array_ref *ar, int array_dim, int loop_dim)
    4391              : {
    4392        59733 :   gfc_se se;
    4393        59733 :   gfc_array_info *info;
    4394        59733 :   tree stride, index;
    4395              : 
    4396        59733 :   info = &ss->info->data.array;
    4397              : 
    4398        59733 :   gfc_init_se (&se, NULL);
    4399        59733 :   se.loop = loop;
    4400        59733 :   se.expr = info->descriptor;
    4401        59733 :   stride = gfc_conv_array_stride (info->descriptor, array_dim);
    4402        59733 :   index = conv_array_index_offset (&se, ss, array_dim, loop_dim, ar, stride);
    4403        59733 :   gfc_add_block_to_block (pblock, &se.pre);
    4404              : 
    4405        59733 :   info->offset = fold_build2_loc (input_location, PLUS_EXPR,
    4406              :                                   gfc_array_index_type,
    4407              :                                   info->offset, index);
    4408        59733 :   info->offset = gfc_evaluate_now (info->offset, pblock);
    4409        59733 : }
    4410              : 
    4411              : 
    4412              : /* Generate the code to be executed immediately before entering a
    4413              :    scalarization loop.  */
    4414              : 
    4415              : static void
    4416       148551 : gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
    4417              :                          stmtblock_t * pblock)
    4418              : {
    4419       148551 :   tree stride;
    4420       148551 :   gfc_ss_info *ss_info;
    4421       148551 :   gfc_array_info *info;
    4422       148551 :   gfc_ss_type ss_type;
    4423       148551 :   gfc_ss *ss, *pss;
    4424       148551 :   gfc_loopinfo *ploop;
    4425       148551 :   gfc_array_ref *ar;
    4426              : 
    4427              :   /* This code will be executed before entering the scalarization loop
    4428              :      for this dimension.  */
    4429       452690 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4430              :     {
    4431       304139 :       ss_info = ss->info;
    4432              : 
    4433       304139 :       if ((ss_info->useflags & flag) == 0)
    4434         1476 :         continue;
    4435              : 
    4436       302663 :       ss_type = ss_info->type;
    4437       369073 :       if (ss_type != GFC_SS_SECTION
    4438              :           && ss_type != GFC_SS_FUNCTION
    4439       302663 :           && ss_type != GFC_SS_CONSTRUCTOR
    4440       302663 :           && ss_type != GFC_SS_COMPONENT)
    4441        66410 :         continue;
    4442              : 
    4443       236253 :       info = &ss_info->data.array;
    4444              : 
    4445       236253 :       gcc_assert (dim < ss->dimen);
    4446       236253 :       gcc_assert (ss->dimen == loop->dimen);
    4447              : 
    4448       236253 :       if (info->ref)
    4449       166554 :         ar = &info->ref->u.ar;
    4450              :       else
    4451              :         ar = NULL;
    4452              : 
    4453       236253 :       if (dim == loop->dimen - 1 && loop->parent != NULL)
    4454              :         {
    4455              :           /* If we are in the outermost dimension of this loop, the previous
    4456              :              dimension shall be in the parent loop.  */
    4457         4687 :           gcc_assert (ss->parent != NULL);
    4458              : 
    4459         4687 :           pss = ss->parent;
    4460         4687 :           ploop = loop->parent;
    4461              : 
    4462              :           /* ss and ss->parent are about the same array.  */
    4463         4687 :           gcc_assert (ss_info == pss->info);
    4464              :         }
    4465              :       else
    4466              :         {
    4467              :           ploop = loop;
    4468              :           pss = ss;
    4469              :         }
    4470              : 
    4471       236253 :       if (dim == loop->dimen - 1 && loop->parent == NULL)
    4472              :         {
    4473       181219 :           gcc_assert (0 == ploop->order[0]);
    4474              : 
    4475       362438 :           stride = gfc_conv_array_stride (info->descriptor,
    4476       181219 :                                           innermost_ss (ss)->dim[0]);
    4477              : 
    4478              :           /* Calculate the stride of the innermost loop.  Hopefully this will
    4479              :              allow the backend optimizers to do their stuff more effectively.
    4480              :            */
    4481       181219 :           info->stride0 = gfc_evaluate_now (stride, pblock);
    4482              : 
    4483              :           /* For the outermost loop calculate the offset due to any
    4484              :              elemental dimensions.  It will have been initialized with the
    4485              :              base offset of the array.  */
    4486       181219 :           if (info->ref)
    4487              :             {
    4488       292533 :               for (int i = 0; i < ar->dimen; i++)
    4489              :                 {
    4490       168879 :                   if (ar->dimen_type[i] != DIMEN_ELEMENT)
    4491       164180 :                     continue;
    4492              : 
    4493         4699 :                   add_array_offset (pblock, loop, ss, ar, i, /* unused */ -1);
    4494              :                 }
    4495              :             }
    4496              :         }
    4497              :       else
    4498              :         {
    4499        55034 :           int i;
    4500              : 
    4501        55034 :           if (dim == loop->dimen - 1)
    4502              :             i = 0;
    4503              :           else
    4504        50347 :             i = dim + 1;
    4505              : 
    4506              :           /* For the time being, there is no loop reordering.  */
    4507        55034 :           gcc_assert (i == ploop->order[i]);
    4508        55034 :           i = ploop->order[i];
    4509              : 
    4510              :           /* Add the offset for the previous loop dimension.  */
    4511        55034 :           add_array_offset (pblock, ploop, ss, ar, pss->dim[i], i);
    4512              :         }
    4513              : 
    4514              :       /* Remember this offset for the second loop.  */
    4515       236253 :       if (dim == loop->temp_dim - 1 && loop->parent == NULL)
    4516        55141 :         info->saved_offset = info->offset;
    4517              :     }
    4518       148551 : }
    4519              : 
    4520              : 
    4521              : /* Start a scalarized expression.  Creates a scope and declares loop
    4522              :    variables.  */
    4523              : 
    4524              : void
    4525       118055 : gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
    4526              : {
    4527       118055 :   int dim;
    4528       118055 :   int n;
    4529       118055 :   int flags;
    4530              : 
    4531       118055 :   gcc_assert (!loop->array_parameter);
    4532              : 
    4533       265026 :   for (dim = loop->dimen - 1; dim >= 0; dim--)
    4534              :     {
    4535       146971 :       n = loop->order[dim];
    4536              : 
    4537       146971 :       gfc_start_block (&loop->code[n]);
    4538              : 
    4539              :       /* Create the loop variable.  */
    4540       146971 :       loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
    4541              : 
    4542       146971 :       if (dim < loop->temp_dim)
    4543              :         flags = 3;
    4544              :       else
    4545       100690 :         flags = 1;
    4546              :       /* Calculate values that will be constant within this loop.  */
    4547       146971 :       gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
    4548              :     }
    4549       118055 :   gfc_start_block (pbody);
    4550       118055 : }
    4551              : 
    4552              : 
    4553              : /* Generates the actual loop code for a scalarization loop.  */
    4554              : 
    4555              : static void
    4556       163196 : gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
    4557              :                                stmtblock_t * pbody)
    4558              : {
    4559       163196 :   stmtblock_t block;
    4560       163196 :   tree cond;
    4561       163196 :   tree tmp;
    4562       163196 :   tree loopbody;
    4563       163196 :   tree exit_label;
    4564       163196 :   tree stmt;
    4565       163196 :   tree init;
    4566       163196 :   tree incr;
    4567              : 
    4568       163196 :   if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS
    4569              :                       | OMPWS_SCALARIZER_BODY))
    4570              :       == (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_WS)
    4571          108 :       && n == loop->dimen - 1)
    4572              :     {
    4573              :       /* We create an OMP_FOR construct for the outermost scalarized loop.  */
    4574           80 :       init = make_tree_vec (1);
    4575           80 :       cond = make_tree_vec (1);
    4576           80 :       incr = make_tree_vec (1);
    4577              : 
    4578              :       /* Cycle statement is implemented with a goto.  Exit statement must not
    4579              :          be present for this loop.  */
    4580           80 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4581           80 :       TREE_USED (exit_label) = 1;
    4582              : 
    4583              :       /* Label for cycle statements (if needed).  */
    4584           80 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4585           80 :       gfc_add_expr_to_block (pbody, tmp);
    4586              : 
    4587           80 :       stmt = make_node (OMP_FOR);
    4588              : 
    4589           80 :       TREE_TYPE (stmt) = void_type_node;
    4590           80 :       OMP_FOR_BODY (stmt) = loopbody = gfc_finish_block (pbody);
    4591              : 
    4592           80 :       OMP_FOR_CLAUSES (stmt) = build_omp_clause (input_location,
    4593              :                                                  OMP_CLAUSE_SCHEDULE);
    4594           80 :       OMP_CLAUSE_SCHEDULE_KIND (OMP_FOR_CLAUSES (stmt))
    4595           80 :         = OMP_CLAUSE_SCHEDULE_STATIC;
    4596           80 :       if (ompws_flags & OMPWS_NOWAIT)
    4597           33 :         OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (stmt))
    4598           66 :           = build_omp_clause (input_location, OMP_CLAUSE_NOWAIT);
    4599              : 
    4600              :       /* Initialize the loopvar.  */
    4601           80 :       TREE_VEC_ELT (init, 0) = build2_v (MODIFY_EXPR, loop->loopvar[n],
    4602              :                                          loop->from[n]);
    4603           80 :       OMP_FOR_INIT (stmt) = init;
    4604              :       /* The exit condition.  */
    4605           80 :       TREE_VEC_ELT (cond, 0) = build2_loc (input_location, LE_EXPR,
    4606              :                                            logical_type_node,
    4607              :                                            loop->loopvar[n], loop->to[n]);
    4608           80 :       SET_EXPR_LOCATION (TREE_VEC_ELT (cond, 0), input_location);
    4609           80 :       OMP_FOR_COND (stmt) = cond;
    4610              :       /* Increment the loopvar.  */
    4611           80 :       tmp = build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    4612              :                         loop->loopvar[n], gfc_index_one_node);
    4613           80 :       TREE_VEC_ELT (incr, 0) = fold_build2_loc (input_location, MODIFY_EXPR,
    4614              :           void_type_node, loop->loopvar[n], tmp);
    4615           80 :       OMP_FOR_INCR (stmt) = incr;
    4616              : 
    4617           80 :       ompws_flags &= ~OMPWS_CURR_SINGLEUNIT;
    4618           80 :       gfc_add_expr_to_block (&loop->code[n], stmt);
    4619              :     }
    4620              :   else
    4621              :     {
    4622       326232 :       bool reverse_loop = (loop->reverse[n] == GFC_REVERSE_SET)
    4623       163116 :                              && (loop->temp_ss == NULL);
    4624              : 
    4625       163116 :       loopbody = gfc_finish_block (pbody);
    4626              : 
    4627       163116 :       if (reverse_loop)
    4628          204 :         std::swap (loop->from[n], loop->to[n]);
    4629              : 
    4630              :       /* Initialize the loopvar.  */
    4631       163116 :       if (loop->loopvar[n] != loop->from[n])
    4632       162295 :         gfc_add_modify (&loop->code[n], loop->loopvar[n], loop->from[n]);
    4633              : 
    4634       163116 :       exit_label = gfc_build_label_decl (NULL_TREE);
    4635              : 
    4636              :       /* Generate the loop body.  */
    4637       163116 :       gfc_init_block (&block);
    4638              : 
    4639              :       /* The exit condition.  */
    4640       326028 :       cond = fold_build2_loc (input_location, reverse_loop ? LT_EXPR : GT_EXPR,
    4641              :                           logical_type_node, loop->loopvar[n], loop->to[n]);
    4642       163116 :       tmp = build1_v (GOTO_EXPR, exit_label);
    4643       163116 :       TREE_USED (exit_label) = 1;
    4644       163116 :       tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
    4645       163116 :       gfc_add_expr_to_block (&block, tmp);
    4646              : 
    4647              :       /* The main body.  */
    4648       163116 :       gfc_add_expr_to_block (&block, loopbody);
    4649              : 
    4650              :       /* Increment the loopvar.  */
    4651       326028 :       tmp = fold_build2_loc (input_location,
    4652              :                              reverse_loop ? MINUS_EXPR : PLUS_EXPR,
    4653              :                              gfc_array_index_type, loop->loopvar[n],
    4654              :                              gfc_index_one_node);
    4655              : 
    4656       163116 :       gfc_add_modify (&block, loop->loopvar[n], tmp);
    4657              : 
    4658              :       /* Build the loop.  */
    4659       163116 :       tmp = gfc_finish_block (&block);
    4660       163116 :       tmp = build1_v (LOOP_EXPR, tmp);
    4661       163116 :       gfc_add_expr_to_block (&loop->code[n], tmp);
    4662              : 
    4663              :       /* Add the exit label.  */
    4664       163116 :       tmp = build1_v (LABEL_EXPR, exit_label);
    4665       163116 :       gfc_add_expr_to_block (&loop->code[n], tmp);
    4666              :     }
    4667              : 
    4668       163196 : }
    4669              : 
    4670              : 
    4671              : /* Finishes and generates the loops for a scalarized expression.  */
    4672              : 
    4673              : void
    4674       124492 : gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
    4675              : {
    4676       124492 :   int dim;
    4677       124492 :   int n;
    4678       124492 :   gfc_ss *ss;
    4679       124492 :   stmtblock_t *pblock;
    4680       124492 :   tree tmp;
    4681              : 
    4682       124492 :   pblock = body;
    4683              :   /* Generate the loops.  */
    4684       277891 :   for (dim = 0; dim < loop->dimen; dim++)
    4685              :     {
    4686       153399 :       n = loop->order[dim];
    4687       153399 :       gfc_trans_scalarized_loop_end (loop, n, pblock);
    4688       153399 :       loop->loopvar[n] = NULL_TREE;
    4689       153399 :       pblock = &loop->code[n];
    4690              :     }
    4691              : 
    4692       124492 :   tmp = gfc_finish_block (pblock);
    4693       124492 :   gfc_add_expr_to_block (&loop->pre, tmp);
    4694              : 
    4695              :   /* Clear all the used flags.  */
    4696       363946 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4697       239454 :     if (ss->parent == NULL)
    4698       234704 :       ss->info->useflags = 0;
    4699       124492 : }
    4700              : 
    4701              : 
    4702              : /* Finish the main body of a scalarized expression, and start the secondary
    4703              :    copying body.  */
    4704              : 
    4705              : void
    4706         8217 : gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
    4707              : {
    4708         8217 :   int dim;
    4709         8217 :   int n;
    4710         8217 :   stmtblock_t *pblock;
    4711         8217 :   gfc_ss *ss;
    4712              : 
    4713         8217 :   pblock = body;
    4714              :   /* We finish as many loops as are used by the temporary.  */
    4715         9797 :   for (dim = 0; dim < loop->temp_dim - 1; dim++)
    4716              :     {
    4717         1580 :       n = loop->order[dim];
    4718         1580 :       gfc_trans_scalarized_loop_end (loop, n, pblock);
    4719         1580 :       loop->loopvar[n] = NULL_TREE;
    4720         1580 :       pblock = &loop->code[n];
    4721              :     }
    4722              : 
    4723              :   /* We don't want to finish the outermost loop entirely.  */
    4724         8217 :   n = loop->order[loop->temp_dim - 1];
    4725         8217 :   gfc_trans_scalarized_loop_end (loop, n, pblock);
    4726              : 
    4727              :   /* Restore the initial offsets.  */
    4728        23555 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    4729              :     {
    4730        15338 :       gfc_ss_type ss_type;
    4731        15338 :       gfc_ss_info *ss_info;
    4732              : 
    4733        15338 :       ss_info = ss->info;
    4734              : 
    4735        15338 :       if ((ss_info->useflags & 2) == 0)
    4736         4546 :         continue;
    4737              : 
    4738        10792 :       ss_type = ss_info->type;
    4739        10946 :       if (ss_type != GFC_SS_SECTION
    4740              :           && ss_type != GFC_SS_FUNCTION
    4741        10792 :           && ss_type != GFC_SS_CONSTRUCTOR
    4742        10792 :           && ss_type != GFC_SS_COMPONENT)
    4743          154 :         continue;
    4744              : 
    4745        10638 :       ss_info->data.array.offset = ss_info->data.array.saved_offset;
    4746              :     }
    4747              : 
    4748              :   /* Restart all the inner loops we just finished.  */
    4749         9797 :   for (dim = loop->temp_dim - 2; dim >= 0; dim--)
    4750              :     {
    4751         1580 :       n = loop->order[dim];
    4752              : 
    4753         1580 :       gfc_start_block (&loop->code[n]);
    4754              : 
    4755         1580 :       loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
    4756              : 
    4757         1580 :       gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
    4758              :     }
    4759              : 
    4760              :   /* Start a block for the secondary copying code.  */
    4761         8217 :   gfc_start_block (body);
    4762         8217 : }
    4763              : 
    4764              : 
    4765              : /* Precalculate (either lower or upper) bound of an array section.
    4766              :      BLOCK: Block in which the (pre)calculation code will go.
    4767              :      BOUNDS[DIM]: Where the bound value will be stored once evaluated.
    4768              :      VALUES[DIM]: Specified bound (NULL <=> unspecified).
    4769              :      DESC: Array descriptor from which the bound will be picked if unspecified
    4770              :        (either lower or upper bound according to LBOUND).  */
    4771              : 
    4772              : static void
    4773       522527 : evaluate_bound (stmtblock_t *block, tree *bounds, gfc_expr ** values,
    4774              :                 tree desc, int dim, bool lbound, bool deferred, bool save_value)
    4775              : {
    4776       522527 :   gfc_se se;
    4777       522527 :   gfc_expr * input_val = values[dim];
    4778       522527 :   tree *output = &bounds[dim];
    4779              : 
    4780       522527 :   if (input_val)
    4781              :     {
    4782              :       /* Specified section bound.  */
    4783        48194 :       gfc_init_se (&se, NULL);
    4784        48194 :       gfc_conv_expr_type (&se, input_val, gfc_array_index_type);
    4785        48194 :       gfc_add_block_to_block (block, &se.pre);
    4786        48194 :       *output = se.expr;
    4787              :     }
    4788       474333 :   else if (deferred && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    4789              :     {
    4790              :       /* The gfc_conv_array_lbound () routine returns a constant zero for
    4791              :          deferred length arrays, which in the scalarizer wreaks havoc, when
    4792              :          copying to a (newly allocated) one-based array.
    4793              :          Keep returning the actual result in sync for both bounds.  */
    4794       193358 :       *output = lbound ? gfc_conv_descriptor_lbound_get (desc,
    4795              :                                                          gfc_rank_cst[dim]):
    4796        64566 :                          gfc_conv_descriptor_ubound_get (desc,
    4797              :                                                          gfc_rank_cst[dim]);
    4798              :     }
    4799              :   else
    4800              :     {
    4801              :       /* No specific bound specified so use the bound of the array.  */
    4802       514869 :       *output = lbound ? gfc_conv_array_lbound (desc, dim) :
    4803       169328 :                          gfc_conv_array_ubound (desc, dim);
    4804              :     }
    4805       522527 :   if (save_value)
    4806       503145 :     *output = gfc_evaluate_now (*output, block);
    4807       522527 : }
    4808              : 
    4809              : 
    4810              : /* Calculate the lower bound of an array section.  */
    4811              : 
    4812              : static void
    4813       261874 : gfc_conv_section_startstride (stmtblock_t * block, gfc_ss * ss, int dim)
    4814              : {
    4815       261874 :   gfc_expr *stride = NULL;
    4816       261874 :   tree desc;
    4817       261874 :   gfc_se se;
    4818       261874 :   gfc_array_info *info;
    4819       261874 :   gfc_array_ref *ar;
    4820              : 
    4821       261874 :   gcc_assert (ss->info->type == GFC_SS_SECTION);
    4822              : 
    4823       261874 :   info = &ss->info->data.array;
    4824       261874 :   ar = &info->ref->u.ar;
    4825              : 
    4826       261874 :   if (ar->dimen_type[dim] == DIMEN_VECTOR)
    4827              :     {
    4828              :       /* We use a zero-based index to access the vector.  */
    4829          986 :       info->start[dim] = gfc_index_zero_node;
    4830          986 :       info->end[dim] = NULL;
    4831          986 :       info->stride[dim] = gfc_index_one_node;
    4832          986 :       return;
    4833              :     }
    4834              : 
    4835       260888 :   gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE
    4836              :               || ar->dimen_type[dim] == DIMEN_THIS_IMAGE);
    4837       260888 :   desc = info->descriptor;
    4838       260888 :   stride = ar->stride[dim];
    4839       260888 :   bool save_value = !ss->is_alloc_lhs;
    4840              : 
    4841              :   /* Calculate the start of the range.  For vector subscripts this will
    4842              :      be the range of the vector.  */
    4843       260888 :   evaluate_bound (block, info->start, ar->start, desc, dim, true,
    4844       260888 :                   ar->as->type == AS_DEFERRED, save_value);
    4845              : 
    4846              :   /* Similarly calculate the end.  Although this is not used in the
    4847              :      scalarizer, it is needed when checking bounds and where the end
    4848              :      is an expression with side-effects.  */
    4849       260888 :   evaluate_bound (block, info->end, ar->end, desc, dim, false,
    4850       260888 :                   ar->as->type == AS_DEFERRED, save_value);
    4851              : 
    4852              : 
    4853              :   /* Calculate the stride.  */
    4854       260888 :   if (stride == NULL)
    4855       247958 :     info->stride[dim] = gfc_index_one_node;
    4856              :   else
    4857              :     {
    4858        12930 :       gfc_init_se (&se, NULL);
    4859        12930 :       gfc_conv_expr_type (&se, stride, gfc_array_index_type);
    4860        12930 :       gfc_add_block_to_block (block, &se.pre);
    4861        12930 :       tree value = se.expr;
    4862        12930 :       if (save_value)
    4863        12930 :         info->stride[dim] = gfc_evaluate_now (value, block);
    4864              :       else
    4865            0 :         info->stride[dim] = value;
    4866              :     }
    4867              : }
    4868              : 
    4869              : 
    4870              : /* Generate in INNER the bounds checking code along the dimension DIM for
    4871              :    the array associated with SS_INFO.  */
    4872              : 
    4873              : static void
    4874        24078 : add_check_section_in_array_bounds (stmtblock_t *inner, gfc_ss_info *ss_info,
    4875              :                                    int dim)
    4876              : {
    4877        24078 :   gfc_expr *expr = ss_info->expr;
    4878        24078 :   locus *expr_loc = &expr->where;
    4879        24078 :   const char *expr_name = expr->symtree->name;
    4880              : 
    4881        24078 :   gfc_array_info *info = &ss_info->data.array;
    4882              : 
    4883        24078 :   bool check_upper;
    4884        24078 :   if (dim == info->ref->u.ar.dimen - 1
    4885        20451 :       && info->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    4886              :     check_upper = false;
    4887              :   else
    4888        23782 :     check_upper = true;
    4889              : 
    4890              :   /* Zero stride is not allowed.  */
    4891        24078 :   tree tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    4892              :                               info->stride[dim], gfc_index_zero_node);
    4893        24078 :   char * msg = xasprintf ("Zero stride is not allowed, for dimension %d "
    4894              :                           "of array '%s'", dim + 1, expr_name);
    4895        24078 :   gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg);
    4896        24078 :   free (msg);
    4897              : 
    4898        24078 :   tree desc = info->descriptor;
    4899              : 
    4900              :   /* This is the run-time equivalent of resolve.cc's
    4901              :      check_dimension.  The logical is more readable there
    4902              :      than it is here, with all the trees.  */
    4903        24078 :   tree lbound = gfc_conv_array_lbound (desc, dim);
    4904        24078 :   tree end = info->end[dim];
    4905        24078 :   tree ubound = check_upper ? gfc_conv_array_ubound (desc, dim) : NULL_TREE;
    4906              : 
    4907              :   /* non_zerosized is true when the selected range is not
    4908              :      empty.  */
    4909        24078 :   tree stride_pos = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4910              :                                      info->stride[dim], gfc_index_zero_node);
    4911        24078 :   tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
    4912              :                          info->start[dim], end);
    4913        24078 :   stride_pos = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    4914              :                                 logical_type_node, stride_pos, tmp);
    4915              : 
    4916        24078 :   tree stride_neg = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4917              :                                      info->stride[dim], gfc_index_zero_node);
    4918        24078 :   tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    4919              :                          info->start[dim], end);
    4920        24078 :   stride_neg = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    4921              :                                 logical_type_node, stride_neg, tmp);
    4922        24078 :   tree non_zerosized = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    4923              :                                         logical_type_node, stride_pos,
    4924              :                                         stride_neg);
    4925              : 
    4926              :   /* Check the start of the range against the lower and upper
    4927              :      bounds of the array, if the range is not empty.
    4928              :      If upper bound is present, include both bounds in the
    4929              :      error message.  */
    4930        24078 :   if (check_upper)
    4931              :     {
    4932        23782 :       tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4933              :                              info->start[dim], lbound);
    4934        23782 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4935              :                              non_zerosized, tmp);
    4936        23782 :       tree tmp2 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4937              :                                    info->start[dim], ubound);
    4938        23782 :       tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4939              :                               non_zerosized, tmp2);
    4940        23782 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
    4941              :                        "expected range (%%ld:%%ld)", dim + 1, expr_name);
    4942        23782 :       gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
    4943              :           fold_convert (long_integer_type_node, info->start[dim]),
    4944              :           fold_convert (long_integer_type_node, lbound),
    4945              :           fold_convert (long_integer_type_node, ubound));
    4946        23782 :       gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
    4947              :           fold_convert (long_integer_type_node, info->start[dim]),
    4948              :           fold_convert (long_integer_type_node, lbound),
    4949              :           fold_convert (long_integer_type_node, ubound));
    4950        23782 :       free (msg);
    4951              :     }
    4952              :   else
    4953              :     {
    4954          296 :       tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4955              :                              info->start[dim], lbound);
    4956          296 :       tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4957              :                              non_zerosized, tmp);
    4958          296 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
    4959              :                        "lower bound of %%ld", dim + 1, expr_name);
    4960          296 :       gfc_trans_runtime_check (true, false, tmp, inner, expr_loc, msg,
    4961              :           fold_convert (long_integer_type_node, info->start[dim]),
    4962              :           fold_convert (long_integer_type_node, lbound));
    4963          296 :       free (msg);
    4964              :     }
    4965              : 
    4966              :   /* Compute the last element of the range, which is not
    4967              :      necessarily "end" (think 0:5:3, which doesn't contain 5)
    4968              :      and check it against both lower and upper bounds.  */
    4969              : 
    4970        24078 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    4971              :                          end, info->start[dim]);
    4972        24078 :   tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR, gfc_array_index_type,
    4973              :                          tmp, info->stride[dim]);
    4974        24078 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    4975              :                          end, tmp);
    4976        24078 :   tree tmp2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    4977              :                                tmp, lbound);
    4978        24078 :   tmp2 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4979              :                           non_zerosized, tmp2);
    4980        24078 :   if (check_upper)
    4981              :     {
    4982        23782 :       tree tmp3 = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    4983              :                                    tmp, ubound);
    4984        23782 :       tmp3 = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    4985              :                               non_zerosized, tmp3);
    4986        23782 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' outside of "
    4987              :                        "expected range (%%ld:%%ld)", dim + 1, expr_name);
    4988        23782 :       gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
    4989              :           fold_convert (long_integer_type_node, tmp),
    4990              :           fold_convert (long_integer_type_node, ubound),
    4991              :           fold_convert (long_integer_type_node, lbound));
    4992        23782 :       gfc_trans_runtime_check (true, false, tmp3, inner, expr_loc, msg,
    4993              :           fold_convert (long_integer_type_node, tmp),
    4994              :           fold_convert (long_integer_type_node, ubound),
    4995              :           fold_convert (long_integer_type_node, lbound));
    4996        23782 :       free (msg);
    4997              :     }
    4998              :   else
    4999              :     {
    5000          296 :       msg = xasprintf ("Index '%%ld' of dimension %d of array '%s' below "
    5001              :                        "lower bound of %%ld", dim + 1, expr_name);
    5002          296 :       gfc_trans_runtime_check (true, false, tmp2, inner, expr_loc, msg,
    5003              :           fold_convert (long_integer_type_node, tmp),
    5004              :           fold_convert (long_integer_type_node, lbound));
    5005          296 :       free (msg);
    5006              :     }
    5007        24078 : }
    5008              : 
    5009              : 
    5010              : /* Tells whether we need to generate bounds checking code for the array
    5011              :    associated with SS.  */
    5012              : 
    5013              : bool
    5014        25045 : bounds_check_needed (gfc_ss *ss)
    5015              : {
    5016              :   /* Catch allocatable lhs in f2003.  */
    5017        25045 :   if (flag_realloc_lhs && ss->no_bounds_check)
    5018              :     return false;
    5019              : 
    5020        24768 :   gfc_ss_info *ss_info = ss->info;
    5021        24768 :   if (ss_info->type == GFC_SS_SECTION)
    5022              :     return true;
    5023              : 
    5024         4126 :   if (!(ss_info->type == GFC_SS_INTRINSIC
    5025          227 :         && ss_info->expr
    5026          227 :         && ss_info->expr->expr_type == EXPR_FUNCTION))
    5027              :     return false;
    5028              : 
    5029          227 :   gfc_intrinsic_sym *isym = ss_info->expr->value.function.isym;
    5030          227 :   if (!(isym
    5031          227 :         && (isym->id == GFC_ISYM_MAXLOC
    5032          203 :             || isym->id == GFC_ISYM_MINLOC)))
    5033              :     return false;
    5034              : 
    5035           34 :   return gfc_inline_intrinsic_function_p (ss_info->expr);
    5036              : }
    5037              : 
    5038              : 
    5039              : /* Calculates the range start and stride for a SS chain.  Also gets the
    5040              :    descriptor and data pointer.  The range of vector subscripts is the size
    5041              :    of the vector.  Array bounds are also checked.  */
    5042              : 
    5043              : void
    5044       186567 : gfc_conv_ss_startstride (gfc_loopinfo * loop)
    5045              : {
    5046       186567 :   int n;
    5047       186567 :   tree tmp;
    5048       186567 :   gfc_ss *ss;
    5049              : 
    5050       186567 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    5051              : 
    5052       186567 :   loop->dimen = 0;
    5053              :   /* Determine the rank of the loop.  */
    5054       207039 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5055              :     {
    5056       207039 :       switch (ss->info->type)
    5057              :         {
    5058       175184 :         case GFC_SS_SECTION:
    5059       175184 :         case GFC_SS_CONSTRUCTOR:
    5060       175184 :         case GFC_SS_FUNCTION:
    5061       175184 :         case GFC_SS_COMPONENT:
    5062       175184 :           loop->dimen = ss->dimen;
    5063       175184 :           goto done;
    5064              : 
    5065              :         /* As usual, lbound and ubound are exceptions!.  */
    5066        11383 :         case GFC_SS_INTRINSIC:
    5067        11383 :           switch (ss->info->expr->value.function.isym->id)
    5068              :             {
    5069        11383 :             case GFC_ISYM_LBOUND:
    5070        11383 :             case GFC_ISYM_UBOUND:
    5071        11383 :             case GFC_ISYM_COSHAPE:
    5072        11383 :             case GFC_ISYM_LCOBOUND:
    5073        11383 :             case GFC_ISYM_UCOBOUND:
    5074        11383 :             case GFC_ISYM_MAXLOC:
    5075        11383 :             case GFC_ISYM_MINLOC:
    5076        11383 :             case GFC_ISYM_SHAPE:
    5077        11383 :             case GFC_ISYM_THIS_IMAGE:
    5078        11383 :               loop->dimen = ss->dimen;
    5079        11383 :               goto done;
    5080              : 
    5081              :             default:
    5082              :               break;
    5083              :             }
    5084              : 
    5085        20472 :         default:
    5086        20472 :           break;
    5087              :         }
    5088              :     }
    5089              : 
    5090              :   /* We should have determined the rank of the expression by now.  If
    5091              :      not, that's bad news.  */
    5092            0 :   gcc_unreachable ();
    5093              : 
    5094       186567 : done:
    5095              :   /* Loop over all the SS in the chain.  */
    5096       484948 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5097              :     {
    5098       298381 :       gfc_ss_info *ss_info;
    5099       298381 :       gfc_array_info *info;
    5100       298381 :       gfc_expr *expr;
    5101              : 
    5102       298381 :       ss_info = ss->info;
    5103       298381 :       expr = ss_info->expr;
    5104       298381 :       info = &ss_info->data.array;
    5105              : 
    5106       298381 :       if (expr && expr->shape && !info->shape)
    5107       172999 :         info->shape = expr->shape;
    5108              : 
    5109       298381 :       switch (ss_info->type)
    5110              :         {
    5111       189367 :         case GFC_SS_SECTION:
    5112              :           /* Get the descriptor for the array.  If it is a cross loops array,
    5113              :              we got the descriptor already in the outermost loop.  */
    5114       189367 :           if (ss->parent == NULL)
    5115       184731 :             gfc_conv_ss_descriptor (&outer_loop->pre, ss,
    5116       184731 :                                     !loop->array_parameter);
    5117              : 
    5118       450413 :           for (n = 0; n < ss->dimen; n++)
    5119       261046 :             gfc_conv_section_startstride (&outer_loop->pre, ss, ss->dim[n]);
    5120              :           break;
    5121              : 
    5122        11676 :         case GFC_SS_INTRINSIC:
    5123        11676 :           switch (expr->value.function.isym->id)
    5124              :             {
    5125         3281 :             case GFC_ISYM_MINLOC:
    5126         3281 :             case GFC_ISYM_MAXLOC:
    5127         3281 :               {
    5128         3281 :                 gfc_se se;
    5129         3281 :                 gfc_init_se (&se, nullptr);
    5130         3281 :                 se.loop = loop;
    5131         3281 :                 se.ss = ss;
    5132         3281 :                 gfc_conv_intrinsic_function (&se, expr);
    5133         3281 :                 gfc_add_block_to_block (&outer_loop->pre, &se.pre);
    5134         3281 :                 gfc_add_block_to_block (&outer_loop->post, &se.post);
    5135              : 
    5136         3281 :                 info->descriptor = se.expr;
    5137              : 
    5138         3281 :                 info->data = gfc_conv_array_data (info->descriptor);
    5139         3281 :                 info->data = gfc_evaluate_now (info->data, &outer_loop->pre);
    5140              : 
    5141         3281 :                 gfc_expr *array = expr->value.function.actual->expr;
    5142         3281 :                 tree rank = build_int_cst (gfc_array_index_type, array->rank);
    5143              : 
    5144         3281 :                 tree tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5145              :                                             gfc_array_index_type, rank,
    5146              :                                             gfc_index_one_node);
    5147              : 
    5148         3281 :                 info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5149         3281 :                 info->start[0] = gfc_index_zero_node;
    5150         3281 :                 info->stride[0] = gfc_index_one_node;
    5151         3281 :                 info->offset = gfc_index_zero_node;
    5152         3281 :                 continue;
    5153         3281 :               }
    5154              : 
    5155              :             /* Fall through to supply start and stride.  */
    5156         3004 :             case GFC_ISYM_LBOUND:
    5157         3004 :             case GFC_ISYM_UBOUND:
    5158              :               /* This is the variant without DIM=...  */
    5159         3004 :               gcc_assert (expr->value.function.actual->next->expr == NULL);
    5160              :               /* Fall through.  */
    5161              : 
    5162         8016 :             case GFC_ISYM_SHAPE:
    5163         8016 :               {
    5164         8016 :                 gfc_expr *arg;
    5165              : 
    5166         8016 :                 arg = expr->value.function.actual->expr;
    5167         8016 :                 if (arg->rank == -1)
    5168              :                   {
    5169         1175 :                     gfc_se se;
    5170         1175 :                     tree rank, tmp;
    5171              : 
    5172              :                     /* The rank (hence the return value's shape) is unknown,
    5173              :                        we have to retrieve it.  */
    5174         1175 :                     gfc_init_se (&se, NULL);
    5175         1175 :                     se.descriptor_only = 1;
    5176         1175 :                     gfc_conv_expr (&se, arg);
    5177              :                     /* This is a bare variable, so there is no preliminary
    5178              :                        or cleanup code unless -std=f202y and bounds checking
    5179              :                        is on.  */
    5180         1175 :                     if (!((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    5181            0 :                           && (gfc_option.allow_std & GFC_STD_F202Y)))
    5182         1175 :                       gcc_assert (se.pre.head == NULL_TREE
    5183              :                                   && se.post.head == NULL_TREE);
    5184         1175 :                     rank = gfc_conv_descriptor_rank_get (se.expr);
    5185         1175 :                     tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5186              :                                            gfc_array_index_type,
    5187              :                                            fold_convert (gfc_array_index_type,
    5188              :                                                          rank),
    5189              :                                            gfc_index_one_node);
    5190         1175 :                     info->end[0] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5191         1175 :                     info->start[0] = gfc_index_zero_node;
    5192         1175 :                     info->stride[0] = gfc_index_one_node;
    5193         1175 :                     continue;
    5194         1175 :                   }
    5195              :                   /* Otherwise fall through GFC_SS_FUNCTION.  */
    5196              :                   gcc_fallthrough ();
    5197              :               }
    5198              :             case GFC_ISYM_COSHAPE:
    5199              :             case GFC_ISYM_LCOBOUND:
    5200              :             case GFC_ISYM_UCOBOUND:
    5201              :             case GFC_ISYM_THIS_IMAGE:
    5202              :               break;
    5203              : 
    5204            0 :             default:
    5205            0 :               continue;
    5206            0 :             }
    5207              : 
    5208              :           /* FALLTHRU */
    5209              :         case GFC_SS_CONSTRUCTOR:
    5210              :         case GFC_SS_FUNCTION:
    5211       132104 :           for (n = 0; n < ss->dimen; n++)
    5212              :             {
    5213        71241 :               int dim = ss->dim[n];
    5214              : 
    5215        71241 :               info->start[dim]  = gfc_index_zero_node;
    5216        71241 :               if (ss_info->type != GFC_SS_FUNCTION)
    5217        56748 :                 info->end[dim]    = gfc_index_zero_node;
    5218        71241 :               info->stride[dim] = gfc_index_one_node;
    5219              :             }
    5220              :           break;
    5221              : 
    5222              :         default:
    5223              :           break;
    5224              :         }
    5225              :     }
    5226              : 
    5227              :   /* The rest is just runtime bounds checking.  */
    5228       186567 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    5229              :     {
    5230        16945 :       stmtblock_t block;
    5231        16945 :       tree size[GFC_MAX_DIMENSIONS];
    5232        16945 :       tree tmp3;
    5233        16945 :       gfc_array_info *info;
    5234        16945 :       char *msg;
    5235        16945 :       int dim;
    5236              : 
    5237        16945 :       gfc_start_block (&block);
    5238              : 
    5239        54257 :       for (n = 0; n < loop->dimen; n++)
    5240        20367 :         size[n] = NULL_TREE;
    5241              : 
    5242              :       /* If there is a constructor involved, derive size[] from its shape.  */
    5243        39164 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5244              :         {
    5245        24699 :           gfc_ss_info *ss_info;
    5246              : 
    5247        24699 :           ss_info = ss->info;
    5248        24699 :           info = &ss_info->data.array;
    5249              : 
    5250        24699 :           if (ss_info->type == GFC_SS_CONSTRUCTOR && info->shape)
    5251              :             {
    5252         5224 :               for (n = 0; n < loop->dimen; n++)
    5253              :                 {
    5254         2744 :                   if (size[n] == NULL)
    5255              :                     {
    5256         2744 :                       gcc_assert (info->shape[n]);
    5257         2744 :                       size[n] = gfc_conv_mpz_to_tree (info->shape[n],
    5258              :                                                       gfc_index_integer_kind);
    5259              :                     }
    5260              :                 }
    5261              :               break;
    5262              :             }
    5263              :         }
    5264              : 
    5265        41990 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5266              :         {
    5267        25045 :           stmtblock_t inner;
    5268        25045 :           gfc_ss_info *ss_info;
    5269        25045 :           gfc_expr *expr;
    5270        25045 :           locus *expr_loc;
    5271        25045 :           const char *expr_name;
    5272        25045 :           char *ref_name = NULL;
    5273              : 
    5274        25045 :           if (!bounds_check_needed (ss))
    5275         4369 :             continue;
    5276              : 
    5277        20676 :           ss_info = ss->info;
    5278        20676 :           expr = ss_info->expr;
    5279        20676 :           expr_loc = &expr->where;
    5280        20676 :           if (expr->ref)
    5281        20642 :             expr_name = ref_name = abridged_ref_name (expr, NULL);
    5282              :           else
    5283           34 :             expr_name = expr->symtree->name;
    5284              : 
    5285        20676 :           gfc_start_block (&inner);
    5286              : 
    5287              :           /* TODO: range checking for mapped dimensions.  */
    5288        20676 :           info = &ss_info->data.array;
    5289              : 
    5290              :           /* This code only checks ranges.  Elemental and vector
    5291              :              dimensions are checked later.  */
    5292        65478 :           for (n = 0; n < loop->dimen; n++)
    5293              :             {
    5294        24126 :               dim = ss->dim[n];
    5295        24126 :               if (ss_info->type == GFC_SS_SECTION)
    5296              :                 {
    5297        24092 :                   if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
    5298           14 :                     continue;
    5299              : 
    5300        24078 :                   add_check_section_in_array_bounds (&inner, ss_info, dim);
    5301              :                 }
    5302              : 
    5303              :               /* Check the section sizes match.  */
    5304        24112 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5305              :                                      gfc_array_index_type, info->end[dim],
    5306              :                                      info->start[dim]);
    5307        24112 :               tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
    5308              :                                      gfc_array_index_type, tmp,
    5309              :                                      info->stride[dim]);
    5310        24112 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5311              :                                      gfc_array_index_type,
    5312              :                                      gfc_index_one_node, tmp);
    5313        24112 :               tmp = fold_build2_loc (input_location, MAX_EXPR,
    5314              :                                      gfc_array_index_type, tmp,
    5315              :                                      build_int_cst (gfc_array_index_type, 0));
    5316              :               /* We remember the size of the first section, and check all the
    5317              :                  others against this.  */
    5318        24112 :               if (size[n])
    5319              :                 {
    5320         7193 :                   tmp3 = fold_build2_loc (input_location, NE_EXPR,
    5321              :                                           logical_type_node, tmp, size[n]);
    5322         7193 :                   if (ss_info->type == GFC_SS_INTRINSIC)
    5323            0 :                     msg = xasprintf ("Extent mismatch for dimension %d of the "
    5324              :                                      "result of intrinsic '%s' (%%ld/%%ld)",
    5325              :                                      dim + 1, expr_name);
    5326              :                   else
    5327         7193 :                     msg = xasprintf ("Array bound mismatch for dimension %d "
    5328              :                                      "of array '%s' (%%ld/%%ld)",
    5329              :                                      dim + 1, expr_name);
    5330              : 
    5331         7193 :                   gfc_trans_runtime_check (true, false, tmp3, &inner,
    5332              :                                            expr_loc, msg,
    5333              :                         fold_convert (long_integer_type_node, tmp),
    5334              :                         fold_convert (long_integer_type_node, size[n]));
    5335              : 
    5336         7193 :                   free (msg);
    5337              :                 }
    5338              :               else
    5339        16919 :                 size[n] = gfc_evaluate_now (tmp, &inner);
    5340              :             }
    5341              : 
    5342        20676 :           tmp = gfc_finish_block (&inner);
    5343              : 
    5344              :           /* For optional arguments, only check bounds if the argument is
    5345              :              present.  */
    5346        20676 :           if ((expr->symtree->n.sym->attr.optional
    5347        20368 :                || expr->symtree->n.sym->attr.not_always_present)
    5348          308 :               && expr->symtree->n.sym->attr.dummy)
    5349          307 :             tmp = build3_v (COND_EXPR,
    5350              :                             gfc_conv_expr_present (expr->symtree->n.sym),
    5351              :                             tmp, build_empty_stmt (input_location));
    5352              : 
    5353        20676 :           gfc_add_expr_to_block (&block, tmp);
    5354              : 
    5355        20676 :           free (ref_name);
    5356              :         }
    5357              : 
    5358        16945 :       tmp = gfc_finish_block (&block);
    5359        16945 :       gfc_add_expr_to_block (&outer_loop->pre, tmp);
    5360              :     }
    5361              : 
    5362       189931 :   for (loop = loop->nested; loop; loop = loop->next)
    5363         3364 :     gfc_conv_ss_startstride (loop);
    5364       186567 : }
    5365              : 
    5366              : /* Return true if both symbols could refer to the same data object.  Does
    5367              :    not take account of aliasing due to equivalence statements.  */
    5368              : 
    5369              : static bool
    5370        14026 : symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym, bool lsym_pointer,
    5371              :                      bool lsym_target, bool rsym_pointer, bool rsym_target)
    5372              : {
    5373              :   /* Aliasing isn't possible if the symbols have different base types,
    5374              :      except for complex types where an inquiry reference (%RE, %IM) could
    5375              :      alias with a real type with the same kind parameter.  */
    5376        14026 :   if (!gfc_compare_types (&lsym->ts, &rsym->ts)
    5377        14026 :       && !(((lsym->ts.type == BT_COMPLEX && rsym->ts.type == BT_REAL)
    5378         5073 :             || (lsym->ts.type == BT_REAL && rsym->ts.type == BT_COMPLEX))
    5379           76 :            && lsym->ts.kind == rsym->ts.kind))
    5380              :     return false;
    5381              : 
    5382              :   /* Pointers can point to other pointers and target objects.  */
    5383              : 
    5384         8966 :   if ((lsym_pointer && (rsym_pointer || rsym_target))
    5385         8757 :       || (rsym_pointer && (lsym_pointer || lsym_target)))
    5386              :     return true;
    5387              : 
    5388              :   /* Special case: Argument association, cf. F90 12.4.1.6, F2003 12.4.1.7
    5389              :      and F2008 12.5.2.13 items 3b and 4b. The pointer case (a) is already
    5390              :      checked above.  */
    5391         8843 :   if (lsym_target && rsym_target
    5392           14 :       && ((lsym->attr.dummy && !lsym->attr.contiguous
    5393            0 :            && (!lsym->attr.dimension || lsym->as->type == AS_ASSUMED_SHAPE))
    5394           14 :           || (rsym->attr.dummy && !rsym->attr.contiguous
    5395            6 :               && (!rsym->attr.dimension
    5396            6 :                   || rsym->as->type == AS_ASSUMED_SHAPE))))
    5397            6 :     return true;
    5398              : 
    5399              :   return false;
    5400              : }
    5401              : 
    5402              : 
    5403              : /* Return true if the two SS could be aliased, i.e. both point to the same data
    5404              :    object.  */
    5405              : /* TODO: resolve aliases based on frontend expressions.  */
    5406              : 
    5407              : static int
    5408        11704 : gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
    5409              : {
    5410        11704 :   gfc_ref *lref;
    5411        11704 :   gfc_ref *rref;
    5412        11704 :   gfc_expr *lexpr, *rexpr;
    5413        11704 :   gfc_symbol *lsym;
    5414        11704 :   gfc_symbol *rsym;
    5415        11704 :   bool lsym_pointer, lsym_target, rsym_pointer, rsym_target;
    5416              : 
    5417        11704 :   lexpr = lss->info->expr;
    5418        11704 :   rexpr = rss->info->expr;
    5419              : 
    5420        11704 :   lsym = lexpr->symtree->n.sym;
    5421        11704 :   rsym = rexpr->symtree->n.sym;
    5422              : 
    5423        11704 :   lsym_pointer = lsym->attr.pointer;
    5424        11704 :   lsym_target = lsym->attr.target;
    5425        11704 :   rsym_pointer = rsym->attr.pointer;
    5426        11704 :   rsym_target = rsym->attr.target;
    5427              : 
    5428        11704 :   if (symbols_could_alias (lsym, rsym, lsym_pointer, lsym_target,
    5429              :                            rsym_pointer, rsym_target))
    5430              :     return 1;
    5431              : 
    5432        11613 :   if (rsym->ts.type != BT_DERIVED && rsym->ts.type != BT_CLASS
    5433        10184 :       && lsym->ts.type != BT_DERIVED && lsym->ts.type != BT_CLASS)
    5434              :     return 0;
    5435              : 
    5436              :   /* For derived types we must check all the component types.  We can ignore
    5437              :      array references as these will have the same base type as the previous
    5438              :      component ref.  */
    5439         2962 :   for (lref = lexpr->ref; lref != lss->info->data.array.ref; lref = lref->next)
    5440              :     {
    5441         1085 :       if (lref->type != REF_COMPONENT)
    5442          107 :         continue;
    5443              : 
    5444          978 :       lsym_pointer = lsym_pointer || lref->u.c.sym->attr.pointer;
    5445          978 :       lsym_target  = lsym_target  || lref->u.c.sym->attr.target;
    5446              : 
    5447          978 :       if (symbols_could_alias (lref->u.c.sym, rsym, lsym_pointer, lsym_target,
    5448              :                                rsym_pointer, rsym_target))
    5449              :         return 1;
    5450              : 
    5451          978 :       if ((lsym_pointer && (rsym_pointer || rsym_target))
    5452          963 :           || (rsym_pointer && (lsym_pointer || lsym_target)))
    5453              :         {
    5454            6 :           if (gfc_compare_types (&lref->u.c.component->ts,
    5455              :                                  &rsym->ts))
    5456              :             return 1;
    5457              :         }
    5458              : 
    5459         1468 :       for (rref = rexpr->ref; rref != rss->info->data.array.ref;
    5460          496 :            rref = rref->next)
    5461              :         {
    5462          497 :           if (rref->type != REF_COMPONENT)
    5463           36 :             continue;
    5464              : 
    5465          461 :           rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
    5466          461 :           rsym_target  = lsym_target  || rref->u.c.sym->attr.target;
    5467              : 
    5468          461 :           if (symbols_could_alias (lref->u.c.sym, rref->u.c.sym,
    5469              :                                    lsym_pointer, lsym_target,
    5470              :                                    rsym_pointer, rsym_target))
    5471              :             return 1;
    5472              : 
    5473          460 :           if ((lsym_pointer && (rsym_pointer || rsym_target))
    5474          456 :               || (rsym_pointer && (lsym_pointer || lsym_target)))
    5475              :             {
    5476            0 :               if (gfc_compare_types (&lref->u.c.component->ts,
    5477            0 :                                      &rref->u.c.sym->ts))
    5478              :                 return 1;
    5479            0 :               if (gfc_compare_types (&lref->u.c.sym->ts,
    5480            0 :                                      &rref->u.c.component->ts))
    5481              :                 return 1;
    5482            0 :               if (gfc_compare_types (&lref->u.c.component->ts,
    5483            0 :                                      &rref->u.c.component->ts))
    5484              :                 return 1;
    5485              :             }
    5486              :         }
    5487              :     }
    5488              : 
    5489         1877 :   lsym_pointer = lsym->attr.pointer;
    5490         1877 :   lsym_target = lsym->attr.target;
    5491              : 
    5492         2754 :   for (rref = rexpr->ref; rref != rss->info->data.array.ref; rref = rref->next)
    5493              :     {
    5494         1030 :       if (rref->type != REF_COMPONENT)
    5495              :         break;
    5496              : 
    5497          883 :       rsym_pointer = rsym_pointer || rref->u.c.sym->attr.pointer;
    5498          883 :       rsym_target  = lsym_target  || rref->u.c.sym->attr.target;
    5499              : 
    5500          883 :       if (symbols_could_alias (rref->u.c.sym, lsym,
    5501              :                                lsym_pointer, lsym_target,
    5502              :                                rsym_pointer, rsym_target))
    5503              :         return 1;
    5504              : 
    5505          883 :       if ((lsym_pointer && (rsym_pointer || rsym_target))
    5506          865 :           || (rsym_pointer && (lsym_pointer || lsym_target)))
    5507              :         {
    5508            6 :           if (gfc_compare_types (&lsym->ts, &rref->u.c.component->ts))
    5509              :             return 1;
    5510              :         }
    5511              :     }
    5512              : 
    5513              :   return 0;
    5514              : }
    5515              : 
    5516              : 
    5517              : /* Resolve array data dependencies.  Creates a temporary if required.  */
    5518              : /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
    5519              :    dependency.cc.  */
    5520              : 
    5521              : void
    5522        38850 : gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
    5523              :                                gfc_ss * rss)
    5524              : {
    5525        38850 :   gfc_ss *ss;
    5526        38850 :   gfc_ref *lref;
    5527        38850 :   gfc_ref *rref;
    5528        38850 :   gfc_ss_info *ss_info;
    5529        38850 :   gfc_expr *dest_expr;
    5530        38850 :   gfc_expr *ss_expr;
    5531        38850 :   int nDepend = 0;
    5532        38850 :   int i, j;
    5533              : 
    5534        38850 :   loop->temp_ss = NULL;
    5535        38850 :   dest_expr = dest->info->expr;
    5536              : 
    5537        83614 :   for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
    5538              :     {
    5539        45957 :       ss_info = ss->info;
    5540        45957 :       ss_expr = ss_info->expr;
    5541              : 
    5542        45957 :       if (ss_info->array_outer_dependency)
    5543              :         {
    5544              :           nDepend = 1;
    5545              :           break;
    5546              :         }
    5547              : 
    5548        45840 :       if (ss_info->type != GFC_SS_SECTION)
    5549              :         {
    5550        31323 :           if (flag_realloc_lhs
    5551        30265 :               && dest_expr != ss_expr
    5552        30265 :               && gfc_is_reallocatable_lhs (dest_expr)
    5553        38563 :               && ss_expr->rank)
    5554         3524 :             nDepend = gfc_check_dependency (dest_expr, ss_expr, true);
    5555              : 
    5556              :           /* Check for cases like   c(:)(1:2) = c(2)(2:3)  */
    5557        31323 :           if (!nDepend && dest_expr->rank > 0
    5558        30793 :               && dest_expr->ts.type == BT_CHARACTER
    5559         4832 :               && ss_expr->expr_type == EXPR_VARIABLE)
    5560              : 
    5561          165 :             nDepend = gfc_check_dependency (dest_expr, ss_expr, false);
    5562              : 
    5563        31323 :           if (ss_info->type == GFC_SS_REFERENCE
    5564        31323 :               && gfc_check_dependency (dest_expr, ss_expr, false))
    5565          188 :             ss_info->data.scalar.needs_temporary = 1;
    5566              : 
    5567        31323 :           if (nDepend)
    5568              :             break;
    5569              :           else
    5570        30781 :             continue;
    5571              :         }
    5572              : 
    5573        14517 :       if (dest_expr->symtree->n.sym != ss_expr->symtree->n.sym)
    5574              :         {
    5575        11704 :           if (gfc_could_be_alias (dest, ss)
    5576        11704 :               || gfc_are_equivalenced_arrays (dest_expr, ss_expr))
    5577              :             {
    5578              :               nDepend = 1;
    5579              :               break;
    5580              :             }
    5581              :         }
    5582              :       else
    5583              :         {
    5584         2813 :           lref = dest_expr->ref;
    5585         2813 :           rref = ss_expr->ref;
    5586              : 
    5587         2813 :           nDepend = gfc_dep_resolver (lref, rref, &loop->reverse[0]);
    5588              : 
    5589         2813 :           if (nDepend == 1)
    5590              :             break;
    5591              : 
    5592         5614 :           for (i = 0; i < dest->dimen; i++)
    5593         7606 :             for (j = 0; j < ss->dimen; j++)
    5594         4516 :               if (i != j
    5595         1363 :                   && dest->dim[i] == ss->dim[j])
    5596              :                 {
    5597              :                   /* If we don't access array elements in the same order,
    5598              :                      there is a dependency.  */
    5599           63 :                   nDepend = 1;
    5600           63 :                   goto temporary;
    5601              :                 }
    5602              : #if 0
    5603              :           /* TODO : loop shifting.  */
    5604              :           if (nDepend == 1)
    5605              :             {
    5606              :               /* Mark the dimensions for LOOP SHIFTING */
    5607              :               for (n = 0; n < loop->dimen; n++)
    5608              :                 {
    5609              :                   int dim = dest->data.info.dim[n];
    5610              : 
    5611              :                   if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
    5612              :                     depends[n] = 2;
    5613              :                   else if (! gfc_is_same_range (&lref->u.ar,
    5614              :                                                 &rref->u.ar, dim, 0))
    5615              :                     depends[n] = 1;
    5616              :                  }
    5617              : 
    5618              :               /* Put all the dimensions with dependencies in the
    5619              :                  innermost loops.  */
    5620              :               dim = 0;
    5621              :               for (n = 0; n < loop->dimen; n++)
    5622              :                 {
    5623              :                   gcc_assert (loop->order[n] == n);
    5624              :                   if (depends[n])
    5625              :                   loop->order[dim++] = n;
    5626              :                 }
    5627              :               for (n = 0; n < loop->dimen; n++)
    5628              :                 {
    5629              :                   if (! depends[n])
    5630              :                   loop->order[dim++] = n;
    5631              :                 }
    5632              : 
    5633              :               gcc_assert (dim == loop->dimen);
    5634              :               break;
    5635              :             }
    5636              : #endif
    5637              :         }
    5638              :     }
    5639              : 
    5640          831 : temporary:
    5641              : 
    5642        38850 :   if (nDepend == 1)
    5643              :     {
    5644         1193 :       tree base_type = gfc_typenode_for_spec (&dest_expr->ts);
    5645         1193 :       if (GFC_ARRAY_TYPE_P (base_type)
    5646         1193 :           || GFC_DESCRIPTOR_TYPE_P (base_type))
    5647            0 :         base_type = gfc_get_element_type (base_type);
    5648         1193 :       loop->temp_ss = gfc_get_temp_ss (base_type, dest->info->string_length,
    5649              :                                        loop->dimen);
    5650         1193 :       gfc_add_ss_to_loop (loop, loop->temp_ss);
    5651              :     }
    5652              :   else
    5653        37657 :     loop->temp_ss = NULL;
    5654        38850 : }
    5655              : 
    5656              : 
    5657              : /* Browse through each array's information from the scalarizer and set the loop
    5658              :    bounds according to the "best" one (per dimension), i.e. the one which
    5659              :    provides the most information (constant bounds, shape, etc.).  */
    5660              : 
    5661              : static void
    5662       186567 : set_loop_bounds (gfc_loopinfo *loop)
    5663              : {
    5664       186567 :   int n, dim, spec_dim;
    5665       186567 :   gfc_array_info *info;
    5666       186567 :   gfc_array_info *specinfo;
    5667       186567 :   gfc_ss *ss;
    5668       186567 :   tree tmp;
    5669       186567 :   gfc_ss **loopspec;
    5670       186567 :   bool dynamic[GFC_MAX_DIMENSIONS];
    5671       186567 :   mpz_t *cshape;
    5672       186567 :   mpz_t i;
    5673       186567 :   bool nonoptional_arr;
    5674              : 
    5675       186567 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    5676              : 
    5677       186567 :   loopspec = loop->specloop;
    5678              : 
    5679       186567 :   mpz_init (i);
    5680       625571 :   for (n = 0; n < loop->dimen; n++)
    5681              :     {
    5682       252437 :       loopspec[n] = NULL;
    5683       252437 :       dynamic[n] = false;
    5684              : 
    5685              :       /* If there are both optional and nonoptional array arguments, scalarize
    5686              :          over the nonoptional; otherwise, it does not matter as then all
    5687              :          (optional) arrays have to be present per F2008, 125.2.12p3(6).  */
    5688              : 
    5689       252437 :       nonoptional_arr = false;
    5690              : 
    5691       294424 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5692       294404 :         if (ss->info->type != GFC_SS_SCALAR && ss->info->type != GFC_SS_TEMP
    5693       259008 :             && ss->info->type != GFC_SS_REFERENCE && !ss->info->can_be_null_ref)
    5694              :           {
    5695              :             nonoptional_arr = true;
    5696              :             break;
    5697              :           }
    5698              : 
    5699              :       /* We use one SS term, and use that to determine the bounds of the
    5700              :          loop for this dimension.  We try to pick the simplest term.  */
    5701       660957 :       for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    5702              :         {
    5703       408520 :           gfc_ss_type ss_type;
    5704              : 
    5705       408520 :           ss_type = ss->info->type;
    5706       479486 :           if (ss_type == GFC_SS_SCALAR
    5707       408520 :               || ss_type == GFC_SS_TEMP
    5708       346830 :               || ss_type == GFC_SS_REFERENCE
    5709       337837 :               || (ss->info->can_be_null_ref && nonoptional_arr))
    5710        70966 :             continue;
    5711              : 
    5712       337554 :           info = &ss->info->data.array;
    5713       337554 :           dim = ss->dim[n];
    5714              : 
    5715       337554 :           if (loopspec[n] != NULL)
    5716              :             {
    5717        85117 :               specinfo = &loopspec[n]->info->data.array;
    5718        85117 :               spec_dim = loopspec[n]->dim[n];
    5719              :             }
    5720              :           else
    5721              :             {
    5722              :               /* Silence uninitialized warnings.  */
    5723              :               specinfo = NULL;
    5724              :               spec_dim = 0;
    5725              :             }
    5726              : 
    5727       337554 :           if (info->shape)
    5728              :             {
    5729              :               /* The frontend has worked out the size for us.  */
    5730       227881 :               if (!loopspec[n]
    5731        60198 :                   || !specinfo->shape
    5732       275058 :                   || !integer_zerop (specinfo->start[spec_dim]))
    5733              :                 /* Prefer zero-based descriptors if possible.  */
    5734       210694 :                 loopspec[n] = ss;
    5735       227881 :               continue;
    5736              :             }
    5737              : 
    5738       109673 :           if (ss_type == GFC_SS_CONSTRUCTOR)
    5739              :             {
    5740         1452 :               gfc_constructor_base base;
    5741              :               /* An unknown size constructor will always be rank one.
    5742              :                  Higher rank constructors will either have known shape,
    5743              :                  or still be wrapped in a call to reshape.  */
    5744         1452 :               gcc_assert (loop->dimen == 1);
    5745              : 
    5746              :               /* Always prefer to use the constructor bounds if the size
    5747              :                  can be determined at compile time.  Prefer not to otherwise,
    5748              :                  since the general case involves realloc, and it's better to
    5749              :                  avoid that overhead if possible.  */
    5750         1452 :               base = ss->info->expr->value.constructor;
    5751         1452 :               dynamic[n] = gfc_get_array_constructor_size (&i, base);
    5752         1452 :               if (!dynamic[n] || !loopspec[n])
    5753         1229 :                 loopspec[n] = ss;
    5754         1452 :               continue;
    5755         1452 :             }
    5756              : 
    5757              :           /* Avoid using an allocatable lhs in an assignment, since
    5758              :              there might be a reallocation coming.  */
    5759       108221 :           if (loopspec[n] && ss->is_alloc_lhs)
    5760         9691 :             continue;
    5761              : 
    5762        98530 :           if (!loopspec[n])
    5763        83525 :             loopspec[n] = ss;
    5764              :           /* Criteria for choosing a loop specifier (most important first):
    5765              :              doesn't need realloc
    5766              :              stride of one
    5767              :              known stride
    5768              :              known lower bound
    5769              :              known upper bound
    5770              :            */
    5771        15005 :           else if (loopspec[n]->info->type == GFC_SS_CONSTRUCTOR && dynamic[n])
    5772          235 :             loopspec[n] = ss;
    5773        14770 :           else if (integer_onep (info->stride[dim])
    5774        14770 :                    && !integer_onep (specinfo->stride[spec_dim]))
    5775          120 :             loopspec[n] = ss;
    5776        14650 :           else if (INTEGER_CST_P (info->stride[dim])
    5777        14426 :                    && !INTEGER_CST_P (specinfo->stride[spec_dim]))
    5778            0 :             loopspec[n] = ss;
    5779        14650 :           else if (INTEGER_CST_P (info->start[dim])
    5780         4511 :                    && !INTEGER_CST_P (specinfo->start[spec_dim])
    5781          856 :                    && integer_onep (info->stride[dim])
    5782          428 :                       == integer_onep (specinfo->stride[spec_dim])
    5783        14650 :                    && INTEGER_CST_P (info->stride[dim])
    5784          401 :                       == INTEGER_CST_P (specinfo->stride[spec_dim]))
    5785          401 :             loopspec[n] = ss;
    5786              :           /* We don't work out the upper bound.
    5787              :              else if (INTEGER_CST_P (info->finish[n])
    5788              :              && ! INTEGER_CST_P (specinfo->finish[n]))
    5789              :              loopspec[n] = ss; */
    5790              :         }
    5791              : 
    5792              :       /* We should have found the scalarization loop specifier.  If not,
    5793              :          that's bad news.  */
    5794       252437 :       gcc_assert (loopspec[n]);
    5795              : 
    5796       252437 :       info = &loopspec[n]->info->data.array;
    5797       252437 :       dim = loopspec[n]->dim[n];
    5798              : 
    5799              :       /* Set the extents of this range.  */
    5800       252437 :       cshape = info->shape;
    5801       252437 :       if (cshape && INTEGER_CST_P (info->start[dim])
    5802       180505 :           && INTEGER_CST_P (info->stride[dim]))
    5803              :         {
    5804       180505 :           loop->from[n] = info->start[dim];
    5805       180505 :           mpz_set (i, cshape[get_array_ref_dim_for_loop_dim (loopspec[n], n)]);
    5806       180505 :           mpz_sub_ui (i, i, 1);
    5807              :           /* To = from + (size - 1) * stride.  */
    5808       180505 :           tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
    5809       180505 :           if (!integer_onep (info->stride[dim]))
    5810         8803 :             tmp = fold_build2_loc (input_location, MULT_EXPR,
    5811              :                                    gfc_array_index_type, tmp,
    5812              :                                    info->stride[dim]);
    5813       180505 :           loop->to[n] = fold_build2_loc (input_location, PLUS_EXPR,
    5814              :                                          gfc_array_index_type,
    5815              :                                          loop->from[n], tmp);
    5816              :         }
    5817              :       else
    5818              :         {
    5819        71932 :           loop->from[n] = info->start[dim];
    5820        71932 :           switch (loopspec[n]->info->type)
    5821              :             {
    5822          893 :             case GFC_SS_CONSTRUCTOR:
    5823              :               /* The upper bound is calculated when we expand the
    5824              :                  constructor.  */
    5825          893 :               gcc_assert (loop->to[n] == NULL_TREE);
    5826              :               break;
    5827              : 
    5828        65383 :             case GFC_SS_SECTION:
    5829              :               /* Use the end expression if it exists and is not constant,
    5830              :                  so that it is only evaluated once.  */
    5831        65383 :               loop->to[n] = info->end[dim];
    5832        65383 :               break;
    5833              : 
    5834         4877 :             case GFC_SS_FUNCTION:
    5835              :               /* The loop bound will be set when we generate the call.  */
    5836         4877 :               gcc_assert (loop->to[n] == NULL_TREE);
    5837              :               break;
    5838              : 
    5839          767 :             case GFC_SS_INTRINSIC:
    5840          767 :               {
    5841          767 :                 gfc_expr *expr = loopspec[n]->info->expr;
    5842              : 
    5843              :                 /* The {l,u}bound of an assumed rank.  */
    5844          767 :                 if (expr->value.function.isym->id == GFC_ISYM_SHAPE)
    5845          255 :                   gcc_assert (expr->value.function.actual->expr->rank == -1);
    5846              :                 else
    5847          512 :                   gcc_assert ((expr->value.function.isym->id == GFC_ISYM_LBOUND
    5848              :                                || expr->value.function.isym->id == GFC_ISYM_UBOUND)
    5849              :                               && expr->value.function.actual->next->expr == NULL
    5850              :                               && expr->value.function.actual->expr->rank == -1);
    5851              : 
    5852          767 :                 loop->to[n] = info->end[dim];
    5853          767 :                 break;
    5854              :               }
    5855              : 
    5856           12 :             case GFC_SS_COMPONENT:
    5857           12 :               {
    5858           12 :                 if (info->end[dim] != NULL_TREE)
    5859              :                   {
    5860           12 :                     loop->to[n] = info->end[dim];
    5861           12 :                     break;
    5862              :                   }
    5863              :                 else
    5864            0 :                   gcc_unreachable ();
    5865              :               }
    5866              : 
    5867            0 :             default:
    5868            0 :               gcc_unreachable ();
    5869              :             }
    5870              :         }
    5871              : 
    5872              :       /* Transform everything so we have a simple incrementing variable.  */
    5873       252437 :       if (integer_onep (info->stride[dim]))
    5874       241465 :         info->delta[dim] = gfc_index_zero_node;
    5875              :       else
    5876              :         {
    5877              :           /* Set the delta for this section.  */
    5878        10972 :           info->delta[dim] = gfc_evaluate_now (loop->from[n], &outer_loop->pre);
    5879              :           /* Number of iterations is (end - start + step) / step.
    5880              :              with start = 0, this simplifies to
    5881              :              last = end / step;
    5882              :              for (i = 0; i<=last; i++){...};  */
    5883        10972 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5884              :                                  gfc_array_index_type, loop->to[n],
    5885              :                                  loop->from[n]);
    5886        10972 :           tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR,
    5887              :                                  gfc_array_index_type, tmp, info->stride[dim]);
    5888        10972 :           tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    5889              :                                  tmp, build_int_cst (gfc_array_index_type, -1));
    5890        10972 :           loop->to[n] = gfc_evaluate_now (tmp, &outer_loop->pre);
    5891              :           /* Make the loop variable start at 0.  */
    5892        10972 :           loop->from[n] = gfc_index_zero_node;
    5893              :         }
    5894              :     }
    5895       186567 :   mpz_clear (i);
    5896              : 
    5897       189931 :   for (loop = loop->nested; loop; loop = loop->next)
    5898         3364 :     set_loop_bounds (loop);
    5899       186567 : }
    5900              : 
    5901              : 
    5902              : /* Last attempt to set the loop bounds, in case they depend on an allocatable
    5903              :    function result.  */
    5904              : 
    5905              : static void
    5906       186567 : late_set_loop_bounds (gfc_loopinfo *loop)
    5907              : {
    5908       186567 :   int n, dim;
    5909       186567 :   gfc_array_info *info;
    5910       186567 :   gfc_ss **loopspec;
    5911              : 
    5912       186567 :   loopspec = loop->specloop;
    5913              : 
    5914       439004 :   for (n = 0; n < loop->dimen; n++)
    5915              :     {
    5916              :       /* Set the extents of this range.  */
    5917       252437 :       if (loop->from[n] == NULL_TREE
    5918       252437 :           || loop->to[n] == NULL_TREE)
    5919              :         {
    5920              :           /* We should have found the scalarization loop specifier.  If not,
    5921              :              that's bad news.  */
    5922          455 :           gcc_assert (loopspec[n]);
    5923              : 
    5924          455 :           info = &loopspec[n]->info->data.array;
    5925          455 :           dim = loopspec[n]->dim[n];
    5926              : 
    5927          455 :           if (loopspec[n]->info->type == GFC_SS_FUNCTION
    5928          455 :               && info->start[dim]
    5929          455 :               && info->end[dim])
    5930              :             {
    5931          153 :               loop->from[n] = info->start[dim];
    5932          153 :               loop->to[n] = info->end[dim];
    5933              :             }
    5934              :         }
    5935              :     }
    5936              : 
    5937       189931 :   for (loop = loop->nested; loop; loop = loop->next)
    5938         3364 :     late_set_loop_bounds (loop);
    5939       186567 : }
    5940              : 
    5941              : 
    5942              : /* Initialize the scalarization loop.  Creates the loop variables.  Determines
    5943              :    the range of the loop variables.  Creates a temporary if required.
    5944              :    Also generates code for scalar expressions which have been
    5945              :    moved outside the loop.  */
    5946              : 
    5947              : void
    5948       183203 : gfc_conv_loop_setup (gfc_loopinfo * loop, locus * where)
    5949              : {
    5950       183203 :   gfc_ss *tmp_ss;
    5951       183203 :   tree tmp;
    5952              : 
    5953       183203 :   set_loop_bounds (loop);
    5954              : 
    5955              :   /* Add all the scalar code that can be taken out of the loops.
    5956              :      This may include calculating the loop bounds, so do it before
    5957              :      allocating the temporary.  */
    5958       183203 :   gfc_add_loop_ss_code (loop, loop->ss, false, where);
    5959              : 
    5960       183203 :   late_set_loop_bounds (loop);
    5961              : 
    5962       183203 :   tmp_ss = loop->temp_ss;
    5963              :   /* If we want a temporary then create it.  */
    5964       183203 :   if (tmp_ss != NULL)
    5965              :     {
    5966        11667 :       gfc_ss_info *tmp_ss_info;
    5967              : 
    5968        11667 :       tmp_ss_info = tmp_ss->info;
    5969        11667 :       gcc_assert (tmp_ss_info->type == GFC_SS_TEMP);
    5970        11667 :       gcc_assert (loop->parent == NULL);
    5971              : 
    5972              :       /* Make absolutely sure that this is a complete type.  */
    5973        11667 :       if (tmp_ss_info->string_length)
    5974         2791 :         tmp_ss_info->data.temp.type
    5975         2791 :                 = gfc_get_character_type_len_for_eltype
    5976         2791 :                         (TREE_TYPE (tmp_ss_info->data.temp.type),
    5977              :                          tmp_ss_info->string_length);
    5978              : 
    5979        11667 :       tmp = tmp_ss_info->data.temp.type;
    5980        11667 :       memset (&tmp_ss_info->data.array, 0, sizeof (gfc_array_info));
    5981        11667 :       tmp_ss_info->type = GFC_SS_SECTION;
    5982              : 
    5983        11667 :       gcc_assert (tmp_ss->dimen != 0);
    5984              : 
    5985        11667 :       gfc_trans_create_temp_array (&loop->pre, &loop->post, tmp_ss, tmp,
    5986              :                                    NULL_TREE, false, true, false, where);
    5987              :     }
    5988              : 
    5989              :   /* For array parameters we don't have loop variables, so don't calculate the
    5990              :      translations.  */
    5991       183203 :   if (!loop->array_parameter)
    5992       114893 :     gfc_set_delta (loop);
    5993       183203 : }
    5994              : 
    5995              : 
    5996              : /* Calculates how to transform from loop variables to array indices for each
    5997              :    array: once loop bounds are chosen, sets the difference (DELTA field) between
    5998              :    loop bounds and array reference bounds, for each array info.  */
    5999              : 
    6000              : void
    6001       118724 : gfc_set_delta (gfc_loopinfo *loop)
    6002              : {
    6003       118724 :   gfc_ss *ss, **loopspec;
    6004       118724 :   gfc_array_info *info;
    6005       118724 :   tree tmp;
    6006       118724 :   int n, dim;
    6007              : 
    6008       118724 :   gfc_loopinfo * const outer_loop = outermost_loop (loop);
    6009              : 
    6010       118724 :   loopspec = loop->specloop;
    6011              : 
    6012              :   /* Calculate the translation from loop variables to array indices.  */
    6013       359770 :   for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
    6014              :     {
    6015       241046 :       gfc_ss_type ss_type;
    6016              : 
    6017       241046 :       ss_type = ss->info->type;
    6018        61642 :       if (!(ss_type == GFC_SS_SECTION
    6019       241046 :             || ss_type == GFC_SS_COMPONENT
    6020        98101 :             || ss_type == GFC_SS_CONSTRUCTOR
    6021              :             || (ss_type == GFC_SS_FUNCTION
    6022         8292 :                 && gfc_is_class_array_function (ss->info->expr))))
    6023        61490 :         continue;
    6024              : 
    6025       179556 :       info = &ss->info->data.array;
    6026              : 
    6027       403461 :       for (n = 0; n < ss->dimen; n++)
    6028              :         {
    6029              :           /* If we are specifying the range the delta is already set.  */
    6030       223905 :           if (loopspec[n] != ss)
    6031              :             {
    6032       116676 :               dim = ss->dim[n];
    6033              : 
    6034              :               /* Calculate the offset relative to the loop variable.
    6035              :                  First multiply by the stride.  */
    6036       116676 :               tmp = loop->from[n];
    6037       116676 :               if (!integer_onep (info->stride[dim]))
    6038         3132 :                 tmp = fold_build2_loc (input_location, MULT_EXPR,
    6039              :                                        gfc_array_index_type,
    6040              :                                        tmp, info->stride[dim]);
    6041              : 
    6042              :               /* Then subtract this from our starting value.  */
    6043       116676 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    6044              :                                      gfc_array_index_type,
    6045              :                                      info->start[dim], tmp);
    6046              : 
    6047       116676 :               if (ss->is_alloc_lhs)
    6048         9691 :                 info->delta[dim] = tmp;
    6049              :               else
    6050       106985 :                 info->delta[dim] = gfc_evaluate_now (tmp, &outer_loop->pre);
    6051              :             }
    6052              :         }
    6053              :     }
    6054              : 
    6055       122176 :   for (loop = loop->nested; loop; loop = loop->next)
    6056         3452 :     gfc_set_delta (loop);
    6057       118724 : }
    6058              : 
    6059              : 
    6060              : /* Calculate the size of a given array dimension from the bounds.  This
    6061              :    is simply (ubound - lbound + 1) if this expression is positive
    6062              :    or 0 if it is negative (pick either one if it is zero).  Optionally
    6063              :    (if or_expr is present) OR the (expression != 0) condition to it.  */
    6064              : 
    6065              : tree
    6066        23469 : gfc_conv_array_extent_dim (tree lbound, tree ubound, tree* or_expr)
    6067              : {
    6068        23469 :   tree res;
    6069        23469 :   tree cond;
    6070              : 
    6071              :   /* Calculate (ubound - lbound + 1).  */
    6072        23469 :   res = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6073              :                          ubound, lbound);
    6074        23469 :   res = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, res,
    6075              :                          gfc_index_one_node);
    6076              : 
    6077              :   /* Check whether the size for this dimension is negative.  */
    6078        23469 :   cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, res,
    6079              :                           gfc_index_zero_node);
    6080        23469 :   res = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
    6081              :                          gfc_index_zero_node, res);
    6082              : 
    6083              :   /* Build OR expression.  */
    6084        23469 :   if (or_expr)
    6085        18042 :     *or_expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    6086              :                                 logical_type_node, *or_expr, cond);
    6087              : 
    6088        23469 :   return res;
    6089              : }
    6090              : 
    6091              : 
    6092              : /* Fills in an array descriptor, and returns the size of the array.
    6093              :    The size will be a simple_val, ie a variable or a constant.  Also
    6094              :    calculates the offset of the base.  The pointer argument overflow,
    6095              :    which should be of integer type, will increase in value if overflow
    6096              :    occurs during the size calculation.  Returns the size of the array.
    6097              :    {
    6098              :     stride = 1;
    6099              :     offset = 0;
    6100              :     for (n = 0; n < rank; n++)
    6101              :       {
    6102              :         a.lbound[n] = specified_lower_bound;
    6103              :         offset = offset + a.lbond[n] * stride;
    6104              :         size = 1 - lbound;
    6105              :         a.ubound[n] = specified_upper_bound;
    6106              :         a.stride[n] = stride;
    6107              :         size = size >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
    6108              :         overflow += size == 0 ? 0: (MAX/size < stride ? 1: 0);
    6109              :         stride = stride * size;
    6110              :       }
    6111              :     for (n = rank; n < rank+corank; n++)
    6112              :       (Set lcobound/ucobound as above.)
    6113              :     element_size = sizeof (array element);
    6114              :     if (!rank)
    6115              :       return element_size
    6116              :     stride = (size_t) stride;
    6117              :     overflow += element_size == 0 ? 0: (MAX/element_size < stride ? 1: 0);
    6118              :     stride = stride * element_size;
    6119              :     return (stride);
    6120              :    }  */
    6121              : /*GCC ARRAYS*/
    6122              : 
    6123              : static tree
    6124        12394 : gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset,
    6125              :                      gfc_expr ** lower, gfc_expr ** upper, stmtblock_t * pblock,
    6126              :                      stmtblock_t * descriptor_block, tree * overflow,
    6127              :                      tree expr3_elem_size, gfc_expr *expr3, tree expr3_desc,
    6128              :                      bool e3_has_nodescriptor, gfc_expr *expr,
    6129              :                      tree *element_size, bool explicit_ts)
    6130              : {
    6131        12394 :   tree type;
    6132        12394 :   tree tmp;
    6133        12394 :   tree size;
    6134        12394 :   tree offset;
    6135        12394 :   tree stride;
    6136        12394 :   tree or_expr;
    6137        12394 :   tree thencase;
    6138        12394 :   tree elsecase;
    6139        12394 :   tree cond;
    6140        12394 :   tree var;
    6141        12394 :   stmtblock_t thenblock;
    6142        12394 :   stmtblock_t elseblock;
    6143        12394 :   gfc_expr *ubound;
    6144        12394 :   gfc_se se;
    6145        12394 :   int n;
    6146              : 
    6147        12394 :   if (expr->ts.type == BT_CLASS
    6148         1710 :       && expr3_desc != NULL_TREE
    6149        12756 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
    6150          362 :     type = TREE_TYPE (expr3_desc);
    6151              :   else
    6152        12032 :     type = TREE_TYPE (descriptor);
    6153              : 
    6154              : 
    6155        12394 :   stride = gfc_index_one_node;
    6156        12394 :   offset = gfc_index_zero_node;
    6157              : 
    6158              :   /* Set the dtype before the alloc, because registration of coarrays needs
    6159              :      it initialized.  */
    6160        12394 :   if (expr->ts.type == BT_CHARACTER
    6161         1103 :       && expr->ts.deferred
    6162          563 :       && VAR_P (expr->ts.u.cl->backend_decl))
    6163              :     {
    6164          378 :       type = gfc_typenode_for_spec (&expr->ts);
    6165          378 :       gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6166              :                                      gfc_get_dtype_rank_type (rank, type));
    6167              :     }
    6168        12016 :   else if (expr->ts.type == BT_CHARACTER
    6169          725 :            && expr->ts.deferred
    6170          185 :            && TREE_CODE (descriptor) == COMPONENT_REF)
    6171              :     {
    6172              :       /* Deferred character components have their string length tucked away
    6173              :          in a hidden field of the derived type. Obtain that and use it to
    6174              :          set the dtype. The charlen backend decl is zero because the field
    6175              :          type is zero length.  */
    6176          167 :       gfc_ref *ref;
    6177          167 :       tmp = NULL_TREE;
    6178          167 :       for (ref = expr->ref; ref; ref = ref->next)
    6179          167 :         if (ref->type == REF_COMPONENT
    6180          167 :             && gfc_deferred_strlen (ref->u.c.component, &tmp))
    6181              :           break;
    6182          167 :       gcc_assert (tmp != NULL_TREE);
    6183          167 :       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (tmp),
    6184          167 :                              TREE_OPERAND (descriptor, 0), tmp, NULL_TREE);
    6185          167 :       tmp = fold_convert (gfc_charlen_type_node, tmp);
    6186          167 :       type = gfc_get_character_type_len (expr->ts.kind, tmp);
    6187          167 :       gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6188              :                                      gfc_get_dtype_rank_type (rank, type));
    6189          167 :     }
    6190        11849 :   else if (expr3_desc && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (expr3_desc)))
    6191          952 :     gfc_conv_descriptor_dtype_set (pblock, descriptor,
    6192              :                                    gfc_conv_descriptor_dtype_get (expr3_desc));
    6193        10897 :   else if (expr->ts.type == BT_CLASS && !explicit_ts
    6194         1348 :            && expr3 && expr3->ts.type != BT_CLASS
    6195          355 :            && expr3_elem_size != NULL_TREE && expr3_desc == NULL_TREE)
    6196              :     {
    6197          355 :       gfc_conv_descriptor_dtype_set (pblock, descriptor, gfc_get_dtype (type));
    6198          355 :       gfc_conv_descriptor_elem_len_set (pblock, descriptor, expr3_elem_size);
    6199              :     }
    6200              :   else
    6201        10542 :     gfc_conv_descriptor_dtype_set (pblock, descriptor, gfc_get_dtype (type));
    6202              : 
    6203        12394 :   or_expr = logical_false_node;
    6204              : 
    6205        30436 :   for (n = 0; n < rank; n++)
    6206              :     {
    6207        18042 :       tree conv_lbound;
    6208        18042 :       tree conv_ubound;
    6209              : 
    6210              :       /* We have 3 possibilities for determining the size of the array:
    6211              :          lower == NULL    => lbound = 1, ubound = upper[n]
    6212              :          upper[n] = NULL  => lbound = 1, ubound = lower[n]
    6213              :          upper[n] != NULL => lbound = lower[n], ubound = upper[n]  */
    6214        18042 :       ubound = upper[n];
    6215              : 
    6216              :       /* Set lower bound.  */
    6217        18042 :       gfc_init_se (&se, NULL);
    6218        18042 :       if (expr3_desc != NULL_TREE)
    6219              :         {
    6220         1495 :           if (e3_has_nodescriptor)
    6221              :             /* The lbound of nondescriptor arrays like array constructors,
    6222              :                nonallocatable/nonpointer function results/variables,
    6223              :                start at zero, but when allocating it, the standard expects
    6224              :                the array to start at one.  */
    6225          967 :             se.expr = gfc_index_one_node;
    6226              :           else
    6227          528 :             se.expr = gfc_conv_descriptor_lbound_get (expr3_desc,
    6228              :                                                       gfc_rank_cst[n]);
    6229              :         }
    6230        16547 :       else if (lower == NULL)
    6231        13350 :         se.expr = gfc_index_one_node;
    6232              :       else
    6233              :         {
    6234         3197 :           gcc_assert (lower[n]);
    6235         3197 :           if (ubound)
    6236              :             {
    6237         2457 :               gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
    6238         2457 :               gfc_add_block_to_block (pblock, &se.pre);
    6239              :             }
    6240              :           else
    6241              :             {
    6242          740 :               se.expr = gfc_index_one_node;
    6243          740 :               ubound = lower[n];
    6244              :             }
    6245              :         }
    6246        18042 :       gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
    6247              :                                       gfc_rank_cst[n], se.expr);
    6248        18042 :       conv_lbound = se.expr;
    6249              : 
    6250              :       /* Work out the offset for this component.  */
    6251        18042 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6252              :                              se.expr, stride);
    6253        18042 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    6254              :                                 gfc_array_index_type, offset, tmp);
    6255              : 
    6256              :       /* Set upper bound.  */
    6257        18042 :       gfc_init_se (&se, NULL);
    6258        18042 :       if (expr3_desc != NULL_TREE)
    6259              :         {
    6260         1495 :           if (e3_has_nodescriptor)
    6261              :             {
    6262              :               /* The lbound of nondescriptor arrays like array constructors,
    6263              :                  nonallocatable/nonpointer function results/variables,
    6264              :                  start at zero, but when allocating it, the standard expects
    6265              :                  the array to start at one.  Therefore fix the upper bound to be
    6266              :                  (desc.ubound - desc.lbound) + 1.  */
    6267          967 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    6268              :                                      gfc_array_index_type,
    6269              :                                      gfc_conv_descriptor_ubound_get (
    6270              :                                        expr3_desc, gfc_rank_cst[n]),
    6271              :                                      gfc_conv_descriptor_lbound_get (
    6272              :                                        expr3_desc, gfc_rank_cst[n]));
    6273          967 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    6274              :                                      gfc_array_index_type, tmp,
    6275              :                                      gfc_index_one_node);
    6276          967 :               se.expr = gfc_evaluate_now (tmp, pblock);
    6277              :             }
    6278              :           else
    6279          528 :             se.expr = gfc_conv_descriptor_ubound_get (expr3_desc,
    6280              :                                                       gfc_rank_cst[n]);
    6281              :         }
    6282              :       else
    6283              :         {
    6284        16547 :           gcc_assert (ubound);
    6285        16547 :           gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
    6286        16547 :           gfc_add_block_to_block (pblock, &se.pre);
    6287        16547 :           if (ubound->expr_type == EXPR_FUNCTION)
    6288          781 :             se.expr = gfc_evaluate_now (se.expr, pblock);
    6289              :         }
    6290        18042 :       gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
    6291              :                                       gfc_rank_cst[n], se.expr);
    6292        18042 :       conv_ubound = se.expr;
    6293              : 
    6294              :       /* Store the stride.  */
    6295        18042 :       gfc_conv_descriptor_stride_set (descriptor_block, descriptor,
    6296              :                                       gfc_rank_cst[n], stride);
    6297              : 
    6298              :       /* Calculate size and check whether extent is negative.  */
    6299        18042 :       size = gfc_conv_array_extent_dim (conv_lbound, conv_ubound, &or_expr);
    6300        18042 :       size = gfc_evaluate_now (size, pblock);
    6301              : 
    6302              :       /* Check whether multiplying the stride by the number of
    6303              :          elements in this dimension would overflow. We must also check
    6304              :          whether the current dimension has zero size in order to avoid
    6305              :          division by zero.
    6306              :       */
    6307        18042 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6308              :                              gfc_array_index_type,
    6309        18042 :                              fold_convert (gfc_array_index_type,
    6310              :                                            TYPE_MAX_VALUE (gfc_array_index_type)),
    6311              :                                            size);
    6312        18042 :       cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
    6313              :                                             logical_type_node, tmp, stride),
    6314              :                            PRED_FORTRAN_OVERFLOW);
    6315        18042 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6316              :                              integer_one_node, integer_zero_node);
    6317        18042 :       cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
    6318              :                                             logical_type_node, size,
    6319              :                                             gfc_index_zero_node),
    6320              :                            PRED_FORTRAN_SIZE_ZERO);
    6321        18042 :       tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6322              :                              integer_zero_node, tmp);
    6323        18042 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    6324              :                              *overflow, tmp);
    6325        18042 :       *overflow = gfc_evaluate_now (tmp, pblock);
    6326              : 
    6327              :       /* Multiply the stride by the number of elements in this dimension.  */
    6328        18042 :       stride = fold_build2_loc (input_location, MULT_EXPR,
    6329              :                                 gfc_array_index_type, stride, size);
    6330        18042 :       stride = gfc_evaluate_now (stride, pblock);
    6331              :     }
    6332              : 
    6333        13082 :   for (n = rank; n < rank + corank; n++)
    6334              :     {
    6335          688 :       ubound = upper[n];
    6336              : 
    6337              :       /* Set lower bound.  */
    6338          688 :       gfc_init_se (&se, NULL);
    6339          688 :       if (lower == NULL || lower[n] == NULL)
    6340              :         {
    6341          407 :           gcc_assert (n == rank + corank - 1);
    6342          407 :           se.expr = gfc_index_one_node;
    6343              :         }
    6344              :       else
    6345              :         {
    6346          281 :           if (ubound || n == rank + corank - 1)
    6347              :             {
    6348          184 :               gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
    6349          184 :               gfc_add_block_to_block (pblock, &se.pre);
    6350              :             }
    6351              :           else
    6352              :             {
    6353           97 :               se.expr = gfc_index_one_node;
    6354           97 :               ubound = lower[n];
    6355              :             }
    6356              :         }
    6357          688 :       gfc_conv_descriptor_lbound_set (descriptor_block, descriptor,
    6358              :                                       gfc_rank_cst[n], se.expr);
    6359              : 
    6360          688 :       if (n < rank + corank - 1)
    6361              :         {
    6362          181 :           gfc_init_se (&se, NULL);
    6363          181 :           gcc_assert (ubound);
    6364          181 :           gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
    6365          181 :           gfc_add_block_to_block (pblock, &se.pre);
    6366          181 :           gfc_conv_descriptor_ubound_set (descriptor_block, descriptor,
    6367              :                                           gfc_rank_cst[n], se.expr);
    6368              :         }
    6369              :     }
    6370              : 
    6371              :   /* The stride is the number of elements in the array, so multiply by the
    6372              :      size of an element to get the total size.  Obviously, if there is a
    6373              :      SOURCE expression (expr3) we must use its element size.  */
    6374        12394 :   if (expr3_elem_size != NULL_TREE)
    6375         3145 :     tmp = expr3_elem_size;
    6376         9249 :   else if (expr3 != NULL)
    6377              :     {
    6378            0 :       if (expr3->ts.type == BT_CLASS)
    6379              :         {
    6380            0 :           gfc_se se_sz;
    6381            0 :           gfc_expr *sz = gfc_copy_expr (expr3);
    6382            0 :           gfc_add_vptr_component (sz);
    6383            0 :           gfc_add_size_component (sz);
    6384            0 :           gfc_init_se (&se_sz, NULL);
    6385            0 :           gfc_conv_expr (&se_sz, sz);
    6386            0 :           gfc_free_expr (sz);
    6387            0 :           tmp = se_sz.expr;
    6388              :         }
    6389              :       else
    6390              :         {
    6391            0 :           tmp = gfc_typenode_for_spec (&expr3->ts);
    6392            0 :           tmp = TYPE_SIZE_UNIT (tmp);
    6393              :         }
    6394              :     }
    6395              :   else
    6396         9249 :     tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    6397              : 
    6398              :   /* Convert to size_t.  */
    6399        12394 :   *element_size = fold_convert (size_type_node, tmp);
    6400              : 
    6401        12394 :   if (rank == 0)
    6402              :     return *element_size;
    6403              : 
    6404        12161 :   stride = fold_convert (size_type_node, stride);
    6405              : 
    6406              :   /* First check for overflow. Since an array of type character can
    6407              :      have zero element_size, we must check for that before
    6408              :      dividing.  */
    6409        12161 :   tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6410              :                          size_type_node,
    6411        12161 :                          TYPE_MAX_VALUE (size_type_node), *element_size);
    6412        12161 :   cond = gfc_unlikely (fold_build2_loc (input_location, LT_EXPR,
    6413              :                                         logical_type_node, tmp, stride),
    6414              :                        PRED_FORTRAN_OVERFLOW);
    6415        12161 :   tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6416              :                          integer_one_node, integer_zero_node);
    6417        12161 :   cond = gfc_unlikely (fold_build2_loc (input_location, EQ_EXPR,
    6418              :                                         logical_type_node, *element_size,
    6419              :                                         build_int_cst (size_type_node, 0)),
    6420              :                        PRED_FORTRAN_SIZE_ZERO);
    6421        12161 :   tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node, cond,
    6422              :                          integer_zero_node, tmp);
    6423        12161 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
    6424              :                          *overflow, tmp);
    6425        12161 :   *overflow = gfc_evaluate_now (tmp, pblock);
    6426              : 
    6427        12161 :   size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6428              :                           stride, *element_size);
    6429              : 
    6430        12161 :   if (poffset != NULL)
    6431              :     {
    6432        12161 :       offset = gfc_evaluate_now (offset, pblock);
    6433        12161 :       *poffset = offset;
    6434              :     }
    6435              : 
    6436        12161 :   if (integer_zerop (or_expr))
    6437              :     return size;
    6438         3667 :   if (integer_onep (or_expr))
    6439          605 :     return build_int_cst (size_type_node, 0);
    6440              : 
    6441         3062 :   var = gfc_create_var (TREE_TYPE (size), "size");
    6442         3062 :   gfc_start_block (&thenblock);
    6443         3062 :   gfc_add_modify (&thenblock, var, build_int_cst (size_type_node, 0));
    6444         3062 :   thencase = gfc_finish_block (&thenblock);
    6445              : 
    6446         3062 :   gfc_start_block (&elseblock);
    6447         3062 :   gfc_add_modify (&elseblock, var, size);
    6448         3062 :   elsecase = gfc_finish_block (&elseblock);
    6449              : 
    6450         3062 :   tmp = gfc_evaluate_now (or_expr, pblock);
    6451         3062 :   tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
    6452         3062 :   gfc_add_expr_to_block (pblock, tmp);
    6453              : 
    6454         3062 :   return var;
    6455              : }
    6456              : 
    6457              : 
    6458              : /* Retrieve the last ref from the chain.  This routine is specific to
    6459              :    gfc_array_allocate ()'s needs.  */
    6460              : 
    6461              : bool
    6462        18891 : retrieve_last_ref (gfc_ref **ref_in, gfc_ref **prev_ref_in)
    6463              : {
    6464        18891 :   gfc_ref *ref, *prev_ref;
    6465              : 
    6466        18891 :   ref = *ref_in;
    6467              :   /* Prevent warnings for uninitialized variables.  */
    6468        18891 :   prev_ref = *prev_ref_in;
    6469        26281 :   while (ref && ref->next != NULL)
    6470              :     {
    6471         7390 :       gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT
    6472              :                   || (ref->u.ar.dimen == 0 && ref->u.ar.codimen > 0));
    6473         7390 :       prev_ref = ref;
    6474         7390 :       ref = ref->next;
    6475              :     }
    6476              : 
    6477        18891 :   if (ref == NULL || ref->type != REF_ARRAY)
    6478              :     return false;
    6479              : 
    6480        13631 :   *ref_in = ref;
    6481        13631 :   *prev_ref_in = prev_ref;
    6482        13631 :   return true;
    6483              : }
    6484              : 
    6485              : /* Initializes the descriptor and generates a call to _gfor_allocate.  Does
    6486              :    the work for an ALLOCATE statement.  */
    6487              : /*GCC ARRAYS*/
    6488              : 
    6489              : bool
    6490        17654 : gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
    6491              :                     tree errlen, tree label_finish, tree expr3_elem_size,
    6492              :                     gfc_expr *expr3, tree e3_arr_desc, bool e3_has_nodescriptor,
    6493              :                     gfc_omp_namelist *omp_alloc, bool explicit_ts)
    6494              : {
    6495        17654 :   tree tmp;
    6496        17654 :   tree pointer;
    6497        17654 :   tree offset = NULL_TREE;
    6498        17654 :   tree token = NULL_TREE;
    6499        17654 :   tree size;
    6500        17654 :   tree msg;
    6501        17654 :   tree error = NULL_TREE;
    6502        17654 :   tree overflow; /* Boolean storing whether size calculation overflows.  */
    6503        17654 :   tree var_overflow = NULL_TREE;
    6504        17654 :   tree cond;
    6505        17654 :   tree set_descriptor;
    6506        17654 :   tree not_prev_allocated = NULL_TREE;
    6507        17654 :   tree element_size = NULL_TREE;
    6508        17654 :   stmtblock_t set_descriptor_block;
    6509        17654 :   stmtblock_t elseblock;
    6510        17654 :   gfc_expr **lower;
    6511        17654 :   gfc_expr **upper;
    6512        17654 :   gfc_ref *ref, *prev_ref = NULL, *coref;
    6513        17654 :   bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
    6514              :       non_ulimate_coarray_ptr_comp;
    6515        17654 :   tree omp_cond = NULL_TREE, omp_alt_alloc = NULL_TREE;
    6516              : 
    6517        17654 :   ref = expr->ref;
    6518              : 
    6519              :   /* Find the last reference in the chain.  */
    6520        17654 :   if (!retrieve_last_ref (&ref, &prev_ref))
    6521              :     return false;
    6522              : 
    6523              :   /* Take the allocatable and coarray properties solely from the expr-ref's
    6524              :      attributes and not from source=-expression.  */
    6525        12394 :   if (!prev_ref)
    6526              :     {
    6527         8413 :       allocatable = expr->symtree->n.sym->attr.allocatable;
    6528         8413 :       dimension = expr->symtree->n.sym->attr.dimension;
    6529         8413 :       non_ulimate_coarray_ptr_comp = false;
    6530              :     }
    6531              :   else
    6532              :     {
    6533         3981 :       allocatable = prev_ref->u.c.component->attr.allocatable;
    6534              :       /* Pointer components in coarrayed derived types must be treated
    6535              :          specially in that they are registered without a check if the are
    6536              :          already associated.  This does not hold for ultimate coarray
    6537              :          pointers.  */
    6538         7962 :       non_ulimate_coarray_ptr_comp = (prev_ref->u.c.component->attr.pointer
    6539         3981 :               && !prev_ref->u.c.component->attr.codimension);
    6540         3981 :       dimension = prev_ref->u.c.component->attr.dimension;
    6541              :     }
    6542              : 
    6543              :   /* For allocatable/pointer arrays in derived types, one of the refs has to be
    6544              :      a coarray.  In this case it does not matter whether we are on this_image
    6545              :      or not.  */
    6546        12394 :   coarray = false;
    6547        29771 :   for (coref = expr->ref; coref; coref = coref->next)
    6548        18059 :     if (coref->type == REF_ARRAY && coref->u.ar.codimen > 0)
    6549              :       {
    6550              :         coarray = true;
    6551              :         break;
    6552              :       }
    6553              : 
    6554        12394 :   if (!dimension)
    6555          233 :     gcc_assert (coarray);
    6556              : 
    6557        12394 :   if (ref->u.ar.type == AR_FULL && expr3 != NULL)
    6558              :     {
    6559         1237 :       gfc_ref *old_ref = ref;
    6560              :       /* F08:C633: Array shape from expr3.  */
    6561         1237 :       ref = expr3->ref;
    6562              : 
    6563              :       /* Find the last reference in the chain.  */
    6564         1237 :       if (!retrieve_last_ref (&ref, &prev_ref))
    6565              :         {
    6566            0 :           if (expr3->expr_type == EXPR_FUNCTION
    6567            0 :               && gfc_expr_attr (expr3).dimension)
    6568            0 :             ref = old_ref;
    6569              :           else
    6570            0 :             return false;
    6571              :         }
    6572              :       alloc_w_e3_arr_spec = true;
    6573              :     }
    6574              : 
    6575              :   /* Figure out the size of the array.  */
    6576        12394 :   switch (ref->u.ar.type)
    6577              :     {
    6578         9481 :     case AR_ELEMENT:
    6579         9481 :       if (!coarray)
    6580              :         {
    6581         8851 :           lower = NULL;
    6582         8851 :           upper = ref->u.ar.start;
    6583         8851 :           break;
    6584              :         }
    6585              :       /* Fall through.  */
    6586              : 
    6587         2337 :     case AR_SECTION:
    6588         2337 :       lower = ref->u.ar.start;
    6589         2337 :       upper = ref->u.ar.end;
    6590         2337 :       break;
    6591              : 
    6592         1206 :     case AR_FULL:
    6593         1206 :       gcc_assert (ref->u.ar.as->type == AS_EXPLICIT
    6594              :                   || alloc_w_e3_arr_spec);
    6595              : 
    6596         1206 :       lower = ref->u.ar.as->lower;
    6597         1206 :       upper = ref->u.ar.as->upper;
    6598         1206 :       break;
    6599              : 
    6600            0 :     default:
    6601            0 :       gcc_unreachable ();
    6602        12394 :       break;
    6603              :     }
    6604              : 
    6605        12394 :   overflow = integer_zero_node;
    6606              : 
    6607        12394 :   if (expr->ts.type == BT_CHARACTER
    6608         1103 :       && TREE_CODE (se->string_length) == COMPONENT_REF
    6609          167 :       && expr->ts.u.cl->backend_decl != se->string_length
    6610          167 :       && VAR_P (expr->ts.u.cl->backend_decl))
    6611            0 :     gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    6612            0 :                     fold_convert (TREE_TYPE (expr->ts.u.cl->backend_decl),
    6613              :                                   se->string_length));
    6614              : 
    6615        12394 :   gfc_init_block (&set_descriptor_block);
    6616              :   /* Take the corank only from the actual ref and not from the coref.  The
    6617              :      later will mislead the generation of the array dimensions for allocatable/
    6618              :      pointer components in derived types.  */
    6619        24233 :   size = gfc_array_init_size (se->expr, alloc_w_e3_arr_spec ? expr->rank
    6620        11157 :                                                            : ref->u.ar.as->rank,
    6621          682 :                               coarray ? ref->u.ar.as->corank : 0,
    6622              :                               &offset, lower, upper,
    6623              :                               &se->pre, &set_descriptor_block, &overflow,
    6624              :                               expr3_elem_size, expr3, e3_arr_desc,
    6625              :                               e3_has_nodescriptor, expr, &element_size,
    6626              :                               explicit_ts);
    6627              : 
    6628        12394 :   if (dimension)
    6629              :     {
    6630        12161 :       var_overflow = gfc_create_var (integer_type_node, "overflow");
    6631        12161 :       gfc_add_modify (&se->pre, var_overflow, overflow);
    6632              : 
    6633        12161 :       if (status == NULL_TREE)
    6634              :         {
    6635              :           /* Generate the block of code handling overflow.  */
    6636        11939 :           msg = gfc_build_addr_expr (pchar_type_node,
    6637              :                     gfc_build_localized_cstring_const
    6638              :                         ("Integer overflow when calculating the amount of "
    6639              :                          "memory to allocate"));
    6640        11939 :           error = build_call_expr_loc (input_location,
    6641              :                                        gfor_fndecl_runtime_error, 1, msg);
    6642              :         }
    6643              :       else
    6644              :         {
    6645          222 :           tree status_type = TREE_TYPE (status);
    6646          222 :           stmtblock_t set_status_block;
    6647              : 
    6648          222 :           gfc_start_block (&set_status_block);
    6649          222 :           gfc_add_modify (&set_status_block, status,
    6650              :                           build_int_cst (status_type, LIBERROR_ALLOCATION));
    6651          222 :           error = gfc_finish_block (&set_status_block);
    6652              :         }
    6653              :     }
    6654              : 
    6655              :   /* Allocate memory to store the data.  */
    6656        12394 :   if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
    6657            0 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    6658              : 
    6659        12394 :   if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
    6660              :     {
    6661          437 :       pointer = non_ulimate_coarray_ptr_comp ? se->expr
    6662          365 :                                       : gfc_conv_descriptor_data_get (se->expr);
    6663          437 :       token = gfc_conv_descriptor_token (se->expr);
    6664          437 :       token = gfc_build_addr_expr (NULL_TREE, token);
    6665              :     }
    6666              :   else
    6667              :     {
    6668        11957 :       pointer = gfc_conv_descriptor_data_get (se->expr);
    6669        11957 :       if (omp_alloc)
    6670           33 :         omp_cond = boolean_true_node;
    6671              :     }
    6672        12394 :   STRIP_NOPS (pointer);
    6673              : 
    6674        12394 :   if (allocatable)
    6675              :     {
    6676        10192 :       not_prev_allocated = gfc_create_var (logical_type_node,
    6677              :                                            "not_prev_allocated");
    6678        10192 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
    6679              :                              logical_type_node, pointer,
    6680        10192 :                              build_int_cst (TREE_TYPE (pointer), 0));
    6681              : 
    6682        10192 :       gfc_add_modify (&se->pre, not_prev_allocated, tmp);
    6683              :     }
    6684              : 
    6685        12394 :   gfc_start_block (&elseblock);
    6686              : 
    6687        12394 :   tree succ_add_expr = NULL_TREE;
    6688        12394 :   if (omp_cond)
    6689              :     {
    6690           33 :       tree align, alloc, sz;
    6691           33 :       gfc_se se2;
    6692           33 :       if (omp_alloc->u2.allocator)
    6693              :         {
    6694           10 :           gfc_init_se (&se2, NULL);
    6695           10 :           gfc_conv_expr (&se2, omp_alloc->u2.allocator);
    6696           10 :           gfc_add_block_to_block (&elseblock, &se2.pre);
    6697           10 :           alloc = gfc_evaluate_now (se2.expr, &elseblock);
    6698           10 :           gfc_add_block_to_block (&elseblock, &se2.post);
    6699              :         }
    6700              :       else
    6701           23 :         alloc = build_zero_cst (ptr_type_node);
    6702           33 :       tmp = TREE_TYPE (TREE_TYPE (pointer));
    6703           33 :       if (tmp == void_type_node)
    6704           33 :         tmp = gfc_typenode_for_spec (&expr->ts, 0);
    6705           33 :       if (omp_alloc->u.align)
    6706              :         {
    6707           17 :           gfc_init_se (&se2, NULL);
    6708           17 :           gfc_conv_expr (&se2, omp_alloc->u.align);
    6709           17 :           gcc_assert (CONSTANT_CLASS_P (se2.expr)
    6710              :                       && se2.pre.head == NULL
    6711              :                       && se2.post.head == NULL);
    6712           17 :           align = build_int_cst (size_type_node,
    6713           17 :                                  MAX (tree_to_uhwi (se2.expr),
    6714              :                                       TYPE_ALIGN_UNIT (tmp)));
    6715              :         }
    6716              :       else
    6717           16 :         align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
    6718           33 :       sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
    6719              :                             fold_convert (size_type_node, size),
    6720              :                             build_int_cst (size_type_node, 1));
    6721           33 :       omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
    6722           33 :       DECL_ATTRIBUTES (omp_alt_alloc)
    6723           33 :         = tree_cons (get_identifier ("omp allocator"),
    6724              :                      build_tree_list (NULL_TREE, alloc),
    6725           33 :                      DECL_ATTRIBUTES (omp_alt_alloc));
    6726           33 :       omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
    6727           33 :       stmtblock_t tmp_block;
    6728           33 :       gfc_init_block (&tmp_block);
    6729           33 :       gfc_conv_descriptor_version_set (&tmp_block, se->expr, integer_one_node);
    6730           33 :       succ_add_expr = gfc_finish_block (&tmp_block);
    6731              :     }
    6732              : 
    6733              :   /* The allocatable variant takes the old pointer as first argument.  */
    6734        12394 :   if (allocatable)
    6735        10799 :     gfc_allocate_allocatable (&elseblock, pointer, size, token,
    6736              :                               status, errmsg, errlen, label_finish, expr,
    6737          607 :                               coref != NULL ? coref->u.ar.as->corank : 0,
    6738              :                               omp_cond, omp_alt_alloc, succ_add_expr);
    6739         2202 :   else if (non_ulimate_coarray_ptr_comp && token)
    6740              :     /* The token is set only for GFC_FCOARRAY_LIB mode.  */
    6741           72 :     gfc_allocate_using_caf_lib (&elseblock, pointer, size, token, status,
    6742              :                                 errmsg, errlen,
    6743              :                                 GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY);
    6744              :   else
    6745         2130 :     gfc_allocate_using_malloc (&elseblock, pointer, size, status,
    6746              :                                omp_cond, omp_alt_alloc, succ_add_expr);
    6747              : 
    6748        12394 :   if (dimension)
    6749              :     {
    6750        12161 :       cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
    6751              :                            logical_type_node, var_overflow, integer_zero_node),
    6752              :                            PRED_FORTRAN_OVERFLOW);
    6753        12161 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6754              :                              error, gfc_finish_block (&elseblock));
    6755              :     }
    6756              :   else
    6757          233 :     tmp = gfc_finish_block (&elseblock);
    6758              : 
    6759        12394 :   gfc_add_expr_to_block (&se->pre, tmp);
    6760              : 
    6761              :   /* Update the array descriptor with the offset and the span.  */
    6762        12394 :   if (dimension)
    6763              :     {
    6764        12161 :       gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
    6765        12161 :       tmp = fold_convert (gfc_array_index_type, element_size);
    6766        12161 :       gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
    6767              :     }
    6768              : 
    6769        12394 :   set_descriptor = gfc_finish_block (&set_descriptor_block);
    6770        12394 :   if (status != NULL_TREE)
    6771              :     {
    6772          238 :       cond = fold_build2_loc (input_location, EQ_EXPR,
    6773              :                           logical_type_node, status,
    6774          238 :                           build_int_cst (TREE_TYPE (status), 0));
    6775              : 
    6776          238 :       if (not_prev_allocated != NULL_TREE)
    6777          222 :         cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    6778              :                                 logical_type_node, cond, not_prev_allocated);
    6779              : 
    6780          238 :       gfc_add_expr_to_block (&se->pre,
    6781              :                  fold_build3_loc (input_location, COND_EXPR, void_type_node,
    6782              :                                   cond,
    6783              :                                   set_descriptor,
    6784              :                                   build_empty_stmt (input_location)));
    6785              :     }
    6786              :   else
    6787        12156 :       gfc_add_expr_to_block (&se->pre, set_descriptor);
    6788              : 
    6789              :   return true;
    6790              : }
    6791              : 
    6792              : 
    6793              : /* Create an array constructor from an initialization expression.
    6794              :    We assume the frontend already did any expansions and conversions.  */
    6795              : 
    6796              : tree
    6797         7845 : gfc_conv_array_initializer (tree type, gfc_expr * expr)
    6798              : {
    6799         7845 :   gfc_constructor *c;
    6800         7845 :   tree tmp;
    6801         7845 :   gfc_se se;
    6802         7845 :   tree index, range;
    6803         7845 :   vec<constructor_elt, va_gc> *v = NULL;
    6804              : 
    6805         7845 :   if (expr->expr_type == EXPR_VARIABLE
    6806            1 :       && expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    6807            1 :       && expr->symtree->n.sym->value
    6808            1 :       && !expr->ref)
    6809         7845 :     expr = expr->symtree->n.sym->value;
    6810              : 
    6811              :   /* After parameter substitution the expression should be a constant, array
    6812              :      constructor, structure constructor, or NULL.  Anything else is invalid
    6813              :      and must not ICE later in lowering.  */
    6814         7845 :   if (expr->expr_type != EXPR_CONSTANT
    6815         7445 :       && expr->expr_type != EXPR_STRUCTURE
    6816         6655 :       && expr->expr_type != EXPR_ARRAY
    6817            4 :       && expr->expr_type != EXPR_NULL)
    6818              :     {
    6819            4 :       gfc_error ("Array initializer at %L does not reduce to a constant "
    6820              :                  "expression", &expr->where);
    6821            4 :       return build_constructor (type, NULL);
    6822              :     }
    6823              : 
    6824         7841 :   switch (expr->expr_type)
    6825              :     {
    6826         1190 :     case EXPR_CONSTANT:
    6827         1190 :     case EXPR_STRUCTURE:
    6828              :       /* A single scalar or derived type value.  Create an array with all
    6829              :          elements equal to that value.  */
    6830         1190 :       gfc_init_se (&se, NULL);
    6831              : 
    6832         1190 :       if (expr->expr_type == EXPR_CONSTANT)
    6833          400 :         gfc_conv_constant (&se, expr);
    6834              :       else
    6835          790 :         gfc_conv_structure (&se, expr, 1);
    6836              : 
    6837         2380 :       if (tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
    6838         1190 :                            TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
    6839              :         break;
    6840         2356 :       else if (tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
    6841         1178 :                                    TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
    6842          167 :         range = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
    6843              :       else
    6844         2022 :         range = build2 (RANGE_EXPR, gfc_array_index_type,
    6845         1011 :                         TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
    6846         1011 :                         TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
    6847         1178 :       CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
    6848         1178 :       break;
    6849              : 
    6850         6651 :     case EXPR_ARRAY:
    6851              :       /* Create a vector of all the elements.  */
    6852         6651 :       for (c = gfc_constructor_first (expr->value.constructor);
    6853       164952 :            c && c->expr; c = gfc_constructor_next (c))
    6854              :         {
    6855       158301 :           if (c->iterator)
    6856              :             {
    6857              :               /* Problems occur when we get something like
    6858              :                  integer :: a(lots) = (/(i, i=1, lots)/)  */
    6859            0 :               gfc_fatal_error ("The number of elements in the array "
    6860              :                                "constructor at %L requires an increase of "
    6861              :                                "the allowed %d upper limit. See "
    6862              :                                "%<-fmax-array-constructor%> option",
    6863              :                                &expr->where, flag_max_array_constructor);
    6864              :               return NULL_TREE;
    6865              :             }
    6866       158301 :           index = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
    6867              : 
    6868       158301 :           if (mpz_cmp_si (c->repeat, 1) > 0)
    6869              :             {
    6870          127 :               tree tmp1, tmp2;
    6871          127 :               mpz_t maxval;
    6872              : 
    6873          127 :               mpz_init (maxval);
    6874          127 :               mpz_add (maxval, c->offset, c->repeat);
    6875          127 :               mpz_sub_ui (maxval, maxval, 1);
    6876          127 :               tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
    6877          127 :               if (mpz_cmp_si (c->offset, 0) != 0)
    6878              :                 {
    6879           27 :                   mpz_add_ui (maxval, c->offset, 1);
    6880           27 :                   tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
    6881              :                 }
    6882              :               else
    6883          100 :                 tmp1 = gfc_conv_mpz_to_tree (c->offset, gfc_index_integer_kind);
    6884              : 
    6885          127 :               range = fold_build2 (RANGE_EXPR, gfc_array_index_type, tmp1, tmp2);
    6886          127 :               mpz_clear (maxval);
    6887              :             }
    6888              :           else
    6889              :             range = NULL;
    6890              : 
    6891       158301 :           gfc_init_se (&se, NULL);
    6892       158301 :           switch (c->expr->expr_type)
    6893              :             {
    6894       156818 :             case EXPR_CONSTANT:
    6895       156818 :               gfc_conv_constant (&se, c->expr);
    6896              : 
    6897              :               /* See gfortran.dg/charlen_15.f90 for instance.  */
    6898       156818 :               if (TREE_CODE (se.expr) == STRING_CST
    6899         5320 :                   && TREE_CODE (type) == ARRAY_TYPE)
    6900              :                 {
    6901              :                   tree atype = type;
    6902        10640 :                   while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE)
    6903         5320 :                     atype = TREE_TYPE (atype);
    6904         5320 :                   gcc_checking_assert (TREE_CODE (TREE_TYPE (atype))
    6905              :                                        == INTEGER_TYPE);
    6906         5320 :                   gcc_checking_assert (TREE_TYPE (TREE_TYPE (se.expr))
    6907              :                                        == TREE_TYPE (atype));
    6908         5320 :                   if (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr)))
    6909         5320 :                       > tree_to_uhwi (TYPE_SIZE_UNIT (atype)))
    6910              :                     {
    6911            0 :                       unsigned HOST_WIDE_INT size
    6912            0 :                         = tree_to_uhwi (TYPE_SIZE_UNIT (atype));
    6913            0 :                       const char *p = TREE_STRING_POINTER (se.expr);
    6914              : 
    6915            0 :                       se.expr = build_string (size, p);
    6916              :                     }
    6917         5320 :                   TREE_TYPE (se.expr) = atype;
    6918              :                 }
    6919              :               break;
    6920              : 
    6921         1483 :             case EXPR_STRUCTURE:
    6922         1483 :               gfc_conv_structure (&se, c->expr, 1);
    6923         1483 :               break;
    6924              : 
    6925            0 :             default:
    6926              :               /* Catch those occasional beasts that do not simplify
    6927              :                  for one reason or another, assuming that if they are
    6928              :                  standard defying the frontend will catch them.  */
    6929            0 :               gfc_conv_expr (&se, c->expr);
    6930            0 :               break;
    6931              :             }
    6932              : 
    6933       158301 :           if (range == NULL_TREE)
    6934       158174 :             CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
    6935              :           else
    6936              :             {
    6937          127 :               if (!integer_zerop (index))
    6938           27 :                 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
    6939       158428 :               CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
    6940              :             }
    6941              :         }
    6942              :       break;
    6943              : 
    6944            0 :     case EXPR_NULL:
    6945            0 :       return gfc_build_null_descriptor (type);
    6946              : 
    6947              :     default:
    6948              :       gcc_unreachable ();
    6949              :     }
    6950              : 
    6951              :   /* Create a constructor from the list of elements.  */
    6952         7841 :   tmp = build_constructor (type, v);
    6953         7841 :   TREE_CONSTANT (tmp) = 1;
    6954         7841 :   return tmp;
    6955              : }
    6956              : 
    6957              : 
    6958              : /* Generate code to evaluate non-constant coarray cobounds.  */
    6959              : 
    6960              : void
    6961        21673 : gfc_trans_array_cobounds (tree type, stmtblock_t * pblock,
    6962              :                           const gfc_symbol *sym)
    6963              : {
    6964        21673 :   int dim;
    6965        21673 :   tree ubound;
    6966        21673 :   tree lbound;
    6967        21673 :   gfc_se se;
    6968        21673 :   gfc_array_spec *as;
    6969              : 
    6970        21673 :   as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    6971              : 
    6972        22667 :   for (dim = as->rank; dim < as->rank + as->corank; dim++)
    6973              :     {
    6974              :       /* Evaluate non-constant array bound expressions.
    6975              :          F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
    6976              :          references a function, the result is finalized before execution of the
    6977              :          executable constructs in the scoping unit.
    6978              :          Adding the finalblocks enables this.  */
    6979          994 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    6980          994 :       if (as->lower[dim] && !INTEGER_CST_P (lbound))
    6981              :         {
    6982          114 :           gfc_init_se (&se, NULL);
    6983          114 :           gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
    6984          114 :           gfc_add_block_to_block (pblock, &se.pre);
    6985          114 :           gfc_add_block_to_block (pblock, &se.finalblock);
    6986          114 :           gfc_add_modify (pblock, lbound, se.expr);
    6987              :         }
    6988          994 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    6989          994 :       if (as->upper[dim] && !INTEGER_CST_P (ubound))
    6990              :         {
    6991           60 :           gfc_init_se (&se, NULL);
    6992           60 :           gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
    6993           60 :           gfc_add_block_to_block (pblock, &se.pre);
    6994           60 :           gfc_add_block_to_block (pblock, &se.finalblock);
    6995           60 :           gfc_add_modify (pblock, ubound, se.expr);
    6996              :         }
    6997              :     }
    6998        21673 : }
    6999              : 
    7000              : 
    7001              : /* Generate code to evaluate non-constant array bounds.  Sets *poffset and
    7002              :    returns the size (in elements) of the array.  */
    7003              : 
    7004              : tree
    7005        13972 : gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
    7006              :                         stmtblock_t * pblock)
    7007              : {
    7008        13972 :   gfc_array_spec *as;
    7009        13972 :   tree size;
    7010        13972 :   tree stride;
    7011        13972 :   tree offset;
    7012        13972 :   tree ubound;
    7013        13972 :   tree lbound;
    7014        13972 :   tree tmp;
    7015        13972 :   gfc_se se;
    7016              : 
    7017        13972 :   int dim;
    7018              : 
    7019        13972 :   as = IS_CLASS_COARRAY_OR_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    7020              : 
    7021        13972 :   size = gfc_index_one_node;
    7022        13972 :   offset = gfc_index_zero_node;
    7023        13972 :   stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
    7024        13972 :   if (stride && VAR_P (stride))
    7025          124 :     gfc_add_modify (pblock, stride, gfc_index_one_node);
    7026        31213 :   for (dim = 0; dim < as->rank; dim++)
    7027              :     {
    7028              :       /* Evaluate non-constant array bound expressions.
    7029              :          F2008 4.5.6.3 para 6: If a specification expression in a scoping unit
    7030              :          references a function, the result is finalized before execution of the
    7031              :          executable constructs in the scoping unit.
    7032              :          Adding the finalblocks enables this.  */
    7033        17241 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    7034        17241 :       if (as->lower[dim] && !INTEGER_CST_P (lbound))
    7035              :         {
    7036          475 :           gfc_init_se (&se, NULL);
    7037          475 :           gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
    7038          475 :           gfc_add_block_to_block (pblock, &se.pre);
    7039          475 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7040          475 :           gfc_add_modify (pblock, lbound, se.expr);
    7041              :         }
    7042        17241 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    7043        17241 :       if (as->upper[dim] && !INTEGER_CST_P (ubound))
    7044              :         {
    7045        10619 :           gfc_init_se (&se, NULL);
    7046        10619 :           gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
    7047        10619 :           gfc_add_block_to_block (pblock, &se.pre);
    7048        10619 :           gfc_add_block_to_block (pblock, &se.finalblock);
    7049        10619 :           gfc_add_modify (pblock, ubound, se.expr);
    7050              :         }
    7051              :       /* The offset of this dimension.  offset = offset - lbound * stride.  */
    7052        17241 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7053              :                              lbound, size);
    7054        17241 :       offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    7055              :                                 offset, tmp);
    7056              : 
    7057              :       /* The size of this dimension, and the stride of the next.  */
    7058        17241 :       if (dim + 1 < as->rank)
    7059         3468 :         stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
    7060              :       else
    7061        13773 :         stride = GFC_TYPE_ARRAY_SIZE (type);
    7062              : 
    7063        17241 :       if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
    7064              :         {
    7065              :           /* Calculate stride = size * (ubound + 1 - lbound).  */
    7066        10810 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7067              :                                  gfc_array_index_type,
    7068              :                                  gfc_index_one_node, lbound);
    7069        10810 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7070              :                                  gfc_array_index_type, ubound, tmp);
    7071        10810 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    7072              :                                  gfc_array_index_type, size, tmp);
    7073        10810 :           if (stride)
    7074        10810 :             gfc_add_modify (pblock, stride, tmp);
    7075              :           else
    7076            0 :             stride = gfc_evaluate_now (tmp, pblock);
    7077              : 
    7078              :           /* Make sure that negative size arrays are translated
    7079              :              to being zero size.  */
    7080        10810 :           tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    7081              :                                  stride, gfc_index_zero_node);
    7082        10810 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    7083              :                                  gfc_array_index_type, tmp,
    7084              :                                  stride, gfc_index_zero_node);
    7085        10810 :           gfc_add_modify (pblock, stride, tmp);
    7086              :         }
    7087              : 
    7088        17241 :       size = stride;
    7089              :     }
    7090              : 
    7091        13972 :   gfc_trans_array_cobounds (type, pblock, sym);
    7092        13972 :   gfc_trans_vla_type_sizes (sym, pblock);
    7093              : 
    7094        13972 :   *poffset = offset;
    7095        13972 :   return size;
    7096              : }
    7097              : 
    7098              : 
    7099              : /* Generate code to initialize/allocate an array variable.  */
    7100              : 
    7101              : void
    7102        32189 : gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
    7103              :                                  gfc_wrapped_block * block)
    7104              : {
    7105        32189 :   stmtblock_t init;
    7106        32189 :   tree type;
    7107        32189 :   tree tmp = NULL_TREE;
    7108        32189 :   tree size;
    7109        32189 :   tree offset;
    7110        32189 :   tree space;
    7111        32189 :   tree inittree;
    7112        32189 :   bool onstack;
    7113        32189 :   bool back;
    7114              : 
    7115        32189 :   gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
    7116              : 
    7117              :   /* Do nothing for USEd variables.  */
    7118        32189 :   if (sym->attr.use_assoc)
    7119        26089 :     return;
    7120              : 
    7121        32140 :   type = TREE_TYPE (decl);
    7122        32140 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7123        32140 :   onstack = TREE_CODE (type) != POINTER_TYPE;
    7124              : 
    7125              :   /* In the case of non-dummy symbols with dependencies on an old-fashioned
    7126              :      function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
    7127              :      must be called with the last, optional argument false so that the alloc-
    7128              :      ation occurs after the processing of the result.  */
    7129        32140 :   back = sym->fn_result_dep;
    7130              : 
    7131        32140 :   gfc_init_block (&init);
    7132              : 
    7133              :   /* Evaluate character string length.  */
    7134        32140 :   if (sym->ts.type == BT_CHARACTER
    7135         3092 :       && onstack && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
    7136              :     {
    7137           49 :       gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7138              : 
    7139           49 :       gfc_trans_vla_type_sizes (sym, &init);
    7140              : 
    7141              :       /* Emit a DECL_EXPR for this variable, which will cause the
    7142              :          gimplifier to allocate storage, and all that good stuff.  */
    7143           49 :       tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
    7144           49 :       gfc_add_expr_to_block (&init, tmp);
    7145           49 :       if (sym->attr.omp_allocate)
    7146              :         {
    7147              :           /* Save location of size calculation to ensure GOMP_alloc is placed
    7148              :              after it.  */
    7149            0 :           tree omp_alloc = lookup_attribute ("omp allocate",
    7150            0 :                                              DECL_ATTRIBUTES (decl));
    7151            0 :           TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
    7152            0 :             = build_tree_list (NULL_TREE, tsi_stmt (tsi_last (init.head)));
    7153              :         }
    7154              :     }
    7155              : 
    7156        31938 :   if (onstack)
    7157              :     {
    7158        25900 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
    7159              :                             back);
    7160        25900 :       return;
    7161              :     }
    7162              : 
    7163         6240 :   type = TREE_TYPE (type);
    7164              : 
    7165         6240 :   gcc_assert (!sym->attr.use_assoc);
    7166         6240 :   gcc_assert (!sym->module);
    7167              : 
    7168         6240 :   if (sym->ts.type == BT_CHARACTER
    7169          202 :       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
    7170           94 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7171              : 
    7172         6240 :   size = gfc_trans_array_bounds (type, sym, &offset, &init);
    7173              : 
    7174              :   /* Don't actually allocate space for Cray Pointees.  */
    7175         6240 :   if (sym->attr.cray_pointee)
    7176              :     {
    7177          140 :       if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7178           49 :         gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7179              : 
    7180          140 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
    7181          140 :       return;
    7182              :     }
    7183         6100 :   if (sym->attr.omp_allocate)
    7184              :     {
    7185              :       /* The size is the number of elements in the array, so multiply by the
    7186              :          size of an element to get the total size.  */
    7187            7 :       tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    7188            7 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7189              :                               size, fold_convert (gfc_array_index_type, tmp));
    7190            7 :       size = gfc_evaluate_now (size, &init);
    7191              : 
    7192            7 :       tree omp_alloc = lookup_attribute ("omp allocate",
    7193            7 :                                          DECL_ATTRIBUTES (decl));
    7194            7 :       TREE_CHAIN (TREE_CHAIN (TREE_VALUE (omp_alloc)))
    7195            7 :         = build_tree_list (size, NULL_TREE);
    7196            7 :       space = NULL_TREE;
    7197              :     }
    7198         6093 :   else if (flag_stack_arrays)
    7199              :     {
    7200           18 :       gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
    7201           18 :       space = build_decl (gfc_get_location (&sym->declared_at),
    7202              :                           VAR_DECL, create_tmp_var_name ("A"),
    7203           18 :                           TREE_TYPE (TREE_TYPE (decl)));
    7204           18 :       gfc_trans_vla_type_sizes (sym, &init);
    7205              :     }
    7206              :   else
    7207              :     {
    7208              :       /* The size is the number of elements in the array, so multiply by the
    7209              :          size of an element to get the total size.  */
    7210         6075 :       tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
    7211         6075 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7212              :                               size, fold_convert (gfc_array_index_type, tmp));
    7213              : 
    7214              :       /* Allocate memory to hold the data.  */
    7215         6075 :       tmp = gfc_call_malloc (&init, TREE_TYPE (decl), size);
    7216         6075 :       gfc_add_modify (&init, decl, tmp);
    7217              : 
    7218              :       /* Free the temporary.  */
    7219         6075 :       tmp = gfc_call_free (decl);
    7220         6075 :       space = NULL_TREE;
    7221              :     }
    7222              : 
    7223              :   /* Set offset of the array.  */
    7224         6100 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7225          388 :     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7226              : 
    7227              :   /* Automatic arrays should not have initializers.  */
    7228         6100 :   gcc_assert (!sym->value);
    7229              : 
    7230         6100 :   inittree = gfc_finish_block (&init);
    7231              : 
    7232         6100 :   if (space)
    7233              :     {
    7234           18 :       tree addr;
    7235           18 :       pushdecl (space);
    7236              : 
    7237              :       /* Don't create new scope, emit the DECL_EXPR in exactly the scope
    7238              :          where also space is located.  */
    7239           18 :       gfc_init_block (&init);
    7240           18 :       tmp = fold_build1_loc (input_location, DECL_EXPR,
    7241           18 :                              TREE_TYPE (space), space);
    7242           18 :       gfc_add_expr_to_block (&init, tmp);
    7243           18 :       addr = fold_build1_loc (gfc_get_location (&sym->declared_at),
    7244           18 :                               ADDR_EXPR, TREE_TYPE (decl), space);
    7245           18 :       gfc_add_modify (&init, decl, addr);
    7246           18 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE,
    7247              :                             back);
    7248           18 :       tmp = NULL_TREE;
    7249              :     }
    7250         6100 :   gfc_add_init_cleanup (block, inittree, tmp, back);
    7251              : }
    7252              : 
    7253              : 
    7254              : /* Generate entry and exit code for g77 calling convention arrays.  */
    7255              : 
    7256              : void
    7257         7478 : gfc_trans_g77_array (gfc_symbol * sym, gfc_wrapped_block * block)
    7258              : {
    7259         7478 :   tree parm;
    7260         7478 :   tree type;
    7261         7478 :   tree offset;
    7262         7478 :   tree tmp;
    7263         7478 :   tree stmt;
    7264         7478 :   stmtblock_t init;
    7265              : 
    7266         7478 :   location_t loc = input_location;
    7267         7478 :   input_location = gfc_get_location (&sym->declared_at);
    7268              : 
    7269              :   /* Descriptor type.  */
    7270         7478 :   parm = sym->backend_decl;
    7271         7478 :   type = TREE_TYPE (parm);
    7272         7478 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7273              : 
    7274         7478 :   gfc_start_block (&init);
    7275              : 
    7276         7478 :   if (sym->ts.type == BT_CHARACTER
    7277          746 :       && VAR_P (sym->ts.u.cl->backend_decl))
    7278           85 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7279              : 
    7280              :   /* Evaluate the bounds of the array.  */
    7281         7478 :   gfc_trans_array_bounds (type, sym, &offset, &init);
    7282              : 
    7283              :   /* Set the offset.  */
    7284         7478 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7285         1214 :     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7286              : 
    7287              :   /* Set the pointer itself if we aren't using the parameter directly.  */
    7288         7478 :   if (TREE_CODE (parm) != PARM_DECL)
    7289              :     {
    7290          618 :       tmp = GFC_DECL_SAVED_DESCRIPTOR (parm);
    7291          618 :       if (sym->ts.type == BT_CLASS)
    7292              :         {
    7293          243 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    7294          243 :           tmp = gfc_class_data_get (tmp);
    7295          243 :           tmp = gfc_conv_descriptor_data_get (tmp);
    7296              :         }
    7297          618 :       tmp = convert (TREE_TYPE (parm), tmp);
    7298          618 :       gfc_add_modify (&init, parm, tmp);
    7299              :     }
    7300         7478 :   stmt = gfc_finish_block (&init);
    7301              : 
    7302         7478 :   input_location = loc;
    7303              : 
    7304              :   /* Add the initialization code to the start of the function.  */
    7305              : 
    7306         7478 :   if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
    7307         7478 :       || sym->attr.optional
    7308         6984 :       || sym->attr.not_always_present)
    7309              :     {
    7310          554 :       tree nullify;
    7311          554 :       if (TREE_CODE (parm) != PARM_DECL)
    7312          105 :         nullify = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7313              :                                    parm, null_pointer_node);
    7314              :       else
    7315          449 :         nullify = build_empty_stmt (input_location);
    7316          554 :       tmp = gfc_conv_expr_present (sym, true);
    7317          554 :       stmt = build3_v (COND_EXPR, tmp, stmt, nullify);
    7318              :     }
    7319              : 
    7320         7478 :   gfc_add_init_cleanup (block, stmt, NULL_TREE);
    7321         7478 : }
    7322              : 
    7323              : 
    7324              : /* Modify the descriptor of an array parameter so that it has the
    7325              :    correct lower bound.  Also move the upper bound accordingly.
    7326              :    If the array is not packed, it will be copied into a temporary.
    7327              :    For each dimension we set the new lower and upper bounds.  Then we copy the
    7328              :    stride and calculate the offset for this dimension.  We also work out
    7329              :    what the stride of a packed array would be, and see it the two match.
    7330              :    If the array need repacking, we set the stride to the values we just
    7331              :    calculated, recalculate the offset and copy the array data.
    7332              :    Code is also added to copy the data back at the end of the function.
    7333              :    */
    7334              : 
    7335              : void
    7336        13426 : gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
    7337              :                             gfc_wrapped_block * block)
    7338              : {
    7339        13426 :   tree size;
    7340        13426 :   tree type;
    7341        13426 :   tree offset;
    7342        13426 :   stmtblock_t init;
    7343        13426 :   tree stmtInit, stmtCleanup;
    7344        13426 :   tree lbound;
    7345        13426 :   tree ubound;
    7346        13426 :   tree dubound;
    7347        13426 :   tree dlbound;
    7348        13426 :   tree dumdesc;
    7349        13426 :   tree tmp;
    7350        13426 :   tree stride, stride2;
    7351        13426 :   tree stmt_packed;
    7352        13426 :   tree stmt_unpacked;
    7353        13426 :   tree partial;
    7354        13426 :   gfc_se se;
    7355        13426 :   int n;
    7356        13426 :   int checkparm;
    7357        13426 :   int no_repack;
    7358        13426 :   bool optional_arg;
    7359        13426 :   gfc_array_spec *as;
    7360        13426 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    7361              : 
    7362              :   /* Do nothing for pointer and allocatable arrays.  */
    7363        13426 :   if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    7364        13329 :       || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
    7365        13329 :       || sym->attr.allocatable
    7366        13223 :       || (is_classarray && CLASS_DATA (sym)->attr.allocatable))
    7367         6142 :     return;
    7368              : 
    7369          886 :   if ((!is_classarray
    7370          886 :        || (is_classarray && CLASS_DATA (sym)->as->type == AS_EXPLICIT))
    7371        12521 :       && sym->attr.dummy && !sym->attr.elemental && gfc_is_nodesc_array (sym))
    7372              :     {
    7373         5939 :       gfc_trans_g77_array (sym, block);
    7374         5939 :       return;
    7375              :     }
    7376              : 
    7377         7284 :   location_t loc = input_location;
    7378         7284 :   input_location = gfc_get_location (&sym->declared_at);
    7379              : 
    7380              :   /* Descriptor type.  */
    7381         7284 :   type = TREE_TYPE (tmpdesc);
    7382         7284 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    7383         7284 :   dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7384         7284 :   if (is_classarray)
    7385              :     /* For a class array the dummy array descriptor is in the _class
    7386              :        component.  */
    7387          721 :     dumdesc = gfc_class_data_get (dumdesc);
    7388              :   else
    7389         6563 :     dumdesc = build_fold_indirect_ref_loc (input_location, dumdesc);
    7390         7284 :   as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    7391         7284 :   gfc_start_block (&init);
    7392              : 
    7393         7284 :   if (sym->ts.type == BT_CHARACTER
    7394          810 :       && VAR_P (sym->ts.u.cl->backend_decl))
    7395           87 :     gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    7396              : 
    7397              :   /* TODO: Fix the exclusion of class arrays from extent checking.  */
    7398         1084 :   checkparm = (as->type == AS_EXPLICIT && !is_classarray
    7399         8349 :                && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS));
    7400              : 
    7401         7284 :   no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
    7402         7283 :                 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
    7403              : 
    7404         7284 :   if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
    7405              :     {
    7406              :       /* For non-constant shape arrays we only check if the first dimension
    7407              :          is contiguous.  Repacking higher dimensions wouldn't gain us
    7408              :          anything as we still don't know the array stride.  */
    7409            1 :       partial = gfc_create_var (logical_type_node, "partial");
    7410            1 :       TREE_USED (partial) = 1;
    7411            1 :       tmp = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
    7412            1 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
    7413              :                              gfc_index_one_node);
    7414            1 :       gfc_add_modify (&init, partial, tmp);
    7415              :     }
    7416              :   else
    7417              :     partial = NULL_TREE;
    7418              : 
    7419              :   /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
    7420              :      here, however I think it does the right thing.  */
    7421         7284 :   if (no_repack)
    7422              :     {
    7423              :       /* Set the first stride.  */
    7424         7282 :       stride = gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[0]);
    7425         7282 :       stride = gfc_evaluate_now (stride, &init);
    7426              : 
    7427         7282 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7428              :                              stride, gfc_index_zero_node);
    7429         7282 :       tmp = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    7430              :                              tmp, gfc_index_one_node, stride);
    7431         7282 :       stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
    7432         7282 :       gfc_add_modify (&init, stride, tmp);
    7433              : 
    7434              :       /* Allow the user to disable array repacking.  */
    7435         7282 :       stmt_unpacked = NULL_TREE;
    7436              :     }
    7437              :   else
    7438              :     {
    7439            2 :       gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
    7440              :       /* A library call to repack the array if necessary.  */
    7441            2 :       tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7442            2 :       stmt_unpacked = build_call_expr_loc (input_location,
    7443              :                                        gfor_fndecl_in_pack, 1, tmp);
    7444              : 
    7445            2 :       stride = gfc_index_one_node;
    7446              : 
    7447            2 :       if (warn_array_temporaries)
    7448              :         {
    7449            1 :           locus where;
    7450            1 :           gfc_locus_from_location (&where, loc);
    7451            1 :           gfc_warning (OPT_Warray_temporaries,
    7452              :                      "Creating array temporary at %L", &where);
    7453              :         }
    7454              :     }
    7455              : 
    7456              :   /* This is for the case where the array data is used directly without
    7457              :      calling the repack function.  */
    7458         7284 :   if (no_repack || partial != NULL_TREE)
    7459         7283 :     stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
    7460              :   else
    7461              :     stmt_packed = NULL_TREE;
    7462              : 
    7463              :   /* Assign the data pointer.  */
    7464         7284 :   if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
    7465              :     {
    7466              :       /* Don't repack unknown shape arrays when the first stride is 1.  */
    7467            1 :       tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (stmt_packed),
    7468              :                              partial, stmt_packed, stmt_unpacked);
    7469              :     }
    7470              :   else
    7471         7283 :     tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
    7472         7284 :   gfc_add_modify (&init, tmpdesc, fold_convert (type, tmp));
    7473              : 
    7474         7284 :   offset = gfc_index_zero_node;
    7475         7284 :   size = gfc_index_one_node;
    7476              : 
    7477              :   /* Evaluate the bounds of the array.  */
    7478        17004 :   for (n = 0; n < as->rank; n++)
    7479              :     {
    7480         9720 :       if (checkparm || !as->upper[n])
    7481              :         {
    7482              :           /* Get the bounds of the actual parameter.  */
    7483         8401 :           dubound = gfc_conv_descriptor_ubound_get (dumdesc, gfc_rank_cst[n]);
    7484         8401 :           dlbound = gfc_conv_descriptor_lbound_get (dumdesc, gfc_rank_cst[n]);
    7485              :         }
    7486              :       else
    7487              :         {
    7488              :           dubound = NULL_TREE;
    7489              :           dlbound = NULL_TREE;
    7490              :         }
    7491              : 
    7492         9720 :       lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
    7493         9720 :       if (!INTEGER_CST_P (lbound))
    7494              :         {
    7495           46 :           gfc_init_se (&se, NULL);
    7496           46 :           gfc_conv_expr_type (&se, as->lower[n],
    7497              :                               gfc_array_index_type);
    7498           46 :           gfc_add_block_to_block (&init, &se.pre);
    7499           46 :           gfc_add_modify (&init, lbound, se.expr);
    7500              :         }
    7501              : 
    7502         9720 :       ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
    7503              :       /* Set the desired upper bound.  */
    7504         9720 :       if (as->upper[n])
    7505              :         {
    7506              :           /* We know what we want the upper bound to be.  */
    7507         1377 :           if (!INTEGER_CST_P (ubound))
    7508              :             {
    7509          639 :               gfc_init_se (&se, NULL);
    7510          639 :               gfc_conv_expr_type (&se, as->upper[n],
    7511              :                                   gfc_array_index_type);
    7512          639 :               gfc_add_block_to_block (&init, &se.pre);
    7513          639 :               gfc_add_modify (&init, ubound, se.expr);
    7514              :             }
    7515              : 
    7516              :           /* Check the sizes match.  */
    7517         1377 :           if (checkparm)
    7518              :             {
    7519              :               /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)).  */
    7520           58 :               char * msg;
    7521           58 :               tree temp;
    7522           58 :               locus where;
    7523              : 
    7524           58 :               gfc_locus_from_location (&where, loc);
    7525           58 :               temp = fold_build2_loc (input_location, MINUS_EXPR,
    7526              :                                       gfc_array_index_type, ubound, lbound);
    7527           58 :               temp = fold_build2_loc (input_location, PLUS_EXPR,
    7528              :                                       gfc_array_index_type,
    7529              :                                       gfc_index_one_node, temp);
    7530           58 :               stride2 = fold_build2_loc (input_location, MINUS_EXPR,
    7531              :                                          gfc_array_index_type, dubound,
    7532              :                                          dlbound);
    7533           58 :               stride2 = fold_build2_loc (input_location, PLUS_EXPR,
    7534              :                                          gfc_array_index_type,
    7535              :                                          gfc_index_one_node, stride2);
    7536           58 :               tmp = fold_build2_loc (input_location, NE_EXPR,
    7537              :                                      gfc_array_index_type, temp, stride2);
    7538           58 :               msg = xasprintf ("Dimension %d of array '%s' has extent "
    7539              :                                "%%ld instead of %%ld", n+1, sym->name);
    7540              : 
    7541           58 :               gfc_trans_runtime_check (true, false, tmp, &init, &where, msg,
    7542              :                         fold_convert (long_integer_type_node, temp),
    7543              :                         fold_convert (long_integer_type_node, stride2));
    7544              : 
    7545           58 :               free (msg);
    7546              :             }
    7547              :         }
    7548              :       else
    7549              :         {
    7550              :           /* For assumed shape arrays move the upper bound by the same amount
    7551              :              as the lower bound.  */
    7552         8343 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7553              :                                  gfc_array_index_type, dubound, dlbound);
    7554         8343 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7555              :                                  gfc_array_index_type, tmp, lbound);
    7556         8343 :           gfc_add_modify (&init, ubound, tmp);
    7557              :         }
    7558              :       /* The offset of this dimension.  offset = offset - lbound * stride.  */
    7559         9720 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7560              :                              lbound, stride);
    7561         9720 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    7562              :                                 gfc_array_index_type, offset, tmp);
    7563              : 
    7564              :       /* The size of this dimension, and the stride of the next.  */
    7565         9720 :       if (n + 1 < as->rank)
    7566              :         {
    7567         2436 :           stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
    7568              : 
    7569         2436 :           if (no_repack || partial != NULL_TREE)
    7570         2435 :             stmt_unpacked =
    7571         2435 :               gfc_conv_descriptor_stride_get (dumdesc, gfc_rank_cst[n+1]);
    7572              : 
    7573              :           /* Figure out the stride if not a known constant.  */
    7574         2436 :           if (!INTEGER_CST_P (stride))
    7575              :             {
    7576         2435 :               if (no_repack)
    7577              :                 stmt_packed = NULL_TREE;
    7578              :               else
    7579              :                 {
    7580              :                   /* Calculate stride = size * (ubound + 1 - lbound).  */
    7581            0 :                   tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7582              :                                          gfc_array_index_type,
    7583              :                                          gfc_index_one_node, lbound);
    7584            0 :                   tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7585              :                                          gfc_array_index_type, ubound, tmp);
    7586            0 :                   size = fold_build2_loc (input_location, MULT_EXPR,
    7587              :                                           gfc_array_index_type, size, tmp);
    7588            0 :                   stmt_packed = size;
    7589              :                 }
    7590              : 
    7591              :               /* Assign the stride.  */
    7592         2435 :               if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
    7593            0 :                 tmp = fold_build3_loc (input_location, COND_EXPR,
    7594              :                                        gfc_array_index_type, partial,
    7595              :                                        stmt_unpacked, stmt_packed);
    7596              :               else
    7597         2435 :                 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
    7598         2435 :               gfc_add_modify (&init, stride, tmp);
    7599              :             }
    7600              :         }
    7601              :       else
    7602              :         {
    7603         7284 :           stride = GFC_TYPE_ARRAY_SIZE (type);
    7604              : 
    7605         7284 :           if (stride && !INTEGER_CST_P (stride))
    7606              :             {
    7607              :               /* Calculate size = stride * (ubound + 1 - lbound).  */
    7608         7283 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    7609              :                                      gfc_array_index_type,
    7610              :                                      gfc_index_one_node, lbound);
    7611         7283 :               tmp = fold_build2_loc (input_location, PLUS_EXPR,
    7612              :                                      gfc_array_index_type,
    7613              :                                      ubound, tmp);
    7614        21849 :               tmp = fold_build2_loc (input_location, MULT_EXPR,
    7615              :                                      gfc_array_index_type,
    7616         7283 :                                      GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
    7617         7283 :               gfc_add_modify (&init, stride, tmp);
    7618              :             }
    7619              :         }
    7620              :     }
    7621              : 
    7622         7284 :   gfc_trans_array_cobounds (type, &init, sym);
    7623              : 
    7624              :   /* Set the offset.  */
    7625         7284 :   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7626         7282 :     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7627              : 
    7628              :   /* Fold the element spacing of the actual argument into the strides and the
    7629              :      offset, so that the elements are addressed by the constant element length
    7630              :      rather than by a span loaded from the descriptor.  */
    7631         7284 :   if (DECL_LANG_SPECIFIC (tmpdesc) && GFC_DECL_SPAN_NORMALIZED (tmpdesc))
    7632              :     {
    7633          445 :       tree element = fold_convert (gfc_array_index_type,
    7634              :                                    TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    7635          445 :       tree span = gfc_evaluate_now (gfc_conv_descriptor_span_get (dumdesc),
    7636              :                                     &init);
    7637          445 :       tree unit = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7638          445 :                                    span, element);
    7639          445 :       tree factor = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    7640          445 :                                      gfc_array_index_type, span, element);
    7641          445 :       factor = gfc_evaluate_now (factor, &init);
    7642              : 
    7643         1385 :       auto scale = [&] (tree var)
    7644              :         {
    7645          940 :           tree scaled = fold_build2_loc (input_location, MULT_EXPR,
    7646              :                                          gfc_array_index_type, var, factor);
    7647          940 :           scaled = fold_build3_loc (input_location, COND_EXPR,
    7648              :                                     gfc_array_index_type, unit, var, scaled);
    7649          940 :           gfc_add_modify (&init, var, scaled);
    7650         1385 :         };
    7651              : 
    7652              :       /* A span addressed dummy is never repacked, so its strides and its
    7653              :          offset are all variables loaded from the descriptor.  */
    7654          940 :       for (n = 0; n < as->rank; n++)
    7655              :         {
    7656          495 :           gcc_assert (VAR_P (GFC_TYPE_ARRAY_STRIDE (type, n)));
    7657          495 :           scale (GFC_TYPE_ARRAY_STRIDE (type, n));
    7658              :         }
    7659              : 
    7660          445 :       gcc_assert (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)));
    7661          445 :       scale (GFC_TYPE_ARRAY_OFFSET (type));
    7662              :     }
    7663              : 
    7664              :   /* Load the span once here, like the bounds above, so that element
    7665              :      addressing does not reload it from the descriptor.  The descriptor
    7666              :      itself is not available in an outlined region, such as an OpenMP
    7667              :      target region, whereas this local variable is.  */
    7668         7284 :   if (tree span = GFC_DECL_GET_SPAN (tmpdesc))
    7669          156 :     gfc_add_modify (&init, span, gfc_conv_descriptor_span_get (dumdesc));
    7670              : 
    7671         7284 :   gfc_trans_vla_type_sizes (sym, &init);
    7672              : 
    7673         7284 :   stmtInit = gfc_finish_block (&init);
    7674              : 
    7675              :   /* Only do the entry/initialization code if the arg is present.  */
    7676         7284 :   dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
    7677         7284 :   optional_arg = (sym->attr.optional
    7678         7284 :                   || (sym->ns->proc_name->attr.entry_master
    7679           79 :                       && sym->attr.dummy));
    7680              :   if (optional_arg)
    7681              :     {
    7682          777 :       tree zero_init = fold_convert (TREE_TYPE (tmpdesc), null_pointer_node);
    7683          777 :       zero_init = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7684              :                                    tmpdesc, zero_init);
    7685          777 :       tmp = gfc_conv_expr_present (sym, true);
    7686          777 :       stmtInit = build3_v (COND_EXPR, tmp, stmtInit, zero_init);
    7687              :     }
    7688              : 
    7689              :   /* Cleanup code.  */
    7690         7284 :   if (no_repack)
    7691              :     stmtCleanup = NULL_TREE;
    7692              :   else
    7693              :     {
    7694            2 :       stmtblock_t cleanup;
    7695            2 :       gfc_start_block (&cleanup);
    7696              : 
    7697            2 :       if (sym->attr.intent != INTENT_IN)
    7698              :         {
    7699              :           /* Copy the data back.  */
    7700            2 :           tmp = build_call_expr_loc (input_location,
    7701              :                                  gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
    7702            2 :           gfc_add_expr_to_block (&cleanup, tmp);
    7703              :         }
    7704              : 
    7705              :       /* Free the temporary.  */
    7706            2 :       tmp = gfc_call_free (tmpdesc);
    7707            2 :       gfc_add_expr_to_block (&cleanup, tmp);
    7708              : 
    7709            2 :       stmtCleanup = gfc_finish_block (&cleanup);
    7710              : 
    7711              :       /* Only do the cleanup if the array was repacked.  */
    7712            2 :       if (is_classarray)
    7713              :         /* For a class array the dummy array descriptor is in the _class
    7714              :            component.  */
    7715            1 :         tmp = gfc_class_data_get (dumdesc);
    7716              :       else
    7717            1 :         tmp = build_fold_indirect_ref_loc (input_location, dumdesc);
    7718            2 :       tmp = gfc_conv_descriptor_data_get (tmp);
    7719            2 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    7720              :                              tmp, tmpdesc);
    7721            2 :       stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
    7722              :                               build_empty_stmt (input_location));
    7723              : 
    7724            2 :       if (optional_arg)
    7725              :         {
    7726            0 :           tmp = gfc_conv_expr_present (sym);
    7727            0 :           stmtCleanup = build3_v (COND_EXPR, tmp, stmtCleanup,
    7728              :                                   build_empty_stmt (input_location));
    7729              :         }
    7730              :     }
    7731              : 
    7732              :   /* We don't need to free any memory allocated by internal_pack as it will
    7733              :      be freed at the end of the function by pop_context.  */
    7734         7284 :   gfc_add_init_cleanup (block, stmtInit, stmtCleanup);
    7735              : 
    7736         7284 :   input_location = loc;
    7737              : }
    7738              : 
    7739              : 
    7740              : /* Calculate the overall offset, including subreferences.  */
    7741              : void
    7742        61140 : gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
    7743              :                         bool subref, gfc_expr *expr)
    7744              : {
    7745        61140 :   tree tmp;
    7746        61140 :   tree field;
    7747        61140 :   tree stride;
    7748        61140 :   tree index;
    7749        61140 :   gfc_ref *ref;
    7750        61140 :   gfc_se start;
    7751        61140 :   int n;
    7752              : 
    7753              :   /* If offset is NULL and this is not a subreferenced array, there is
    7754              :      nothing to do.  */
    7755        61140 :   if (offset == NULL_TREE)
    7756              :     {
    7757         1108 :       if (subref)
    7758          171 :         offset = gfc_index_zero_node;
    7759              :       else
    7760          937 :         return;
    7761              :     }
    7762              : 
    7763              :   /* An array whose elements are spaced by the span needs pointer arithmetic
    7764              :      to reference an element.  */
    7765        60203 :   tmp = build_array_ref (desc, offset, NULL, NULL);
    7766              : 
    7767              :   /* Offset the data pointer for pointer assignments from arrays with
    7768              :      subreferences; e.g. my_integer => my_type(:)%integer_component.  */
    7769        60203 :   if (subref)
    7770              :     {
    7771              :       /* Go past the array reference.  */
    7772         1182 :       for (ref = expr->ref; ref; ref = ref->next)
    7773         1182 :         if (ref->type == REF_ARRAY &&
    7774         1053 :               ref->u.ar.type != AR_ELEMENT)
    7775              :           {
    7776         1029 :             ref = ref->next;
    7777         1029 :             break;
    7778              :           }
    7779              : 
    7780              :       /* Calculate the offset for each subsequent subreference.  */
    7781         1914 :       for (; ref; ref = ref->next)
    7782              :         {
    7783          885 :           switch (ref->type)
    7784              :             {
    7785          487 :             case REF_COMPONENT:
    7786          487 :               field = ref->u.c.component->backend_decl;
    7787          487 :               gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
    7788          974 :               tmp = fold_build3_loc (input_location, COMPONENT_REF,
    7789          487 :                                      TREE_TYPE (field),
    7790              :                                      tmp, field, NULL_TREE);
    7791          487 :               break;
    7792              : 
    7793          314 :             case REF_SUBSTRING:
    7794          314 :               gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
    7795          314 :               gfc_init_se (&start, NULL);
    7796          314 :               gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
    7797          314 :               gfc_add_block_to_block (block, &start.pre);
    7798          314 :               tmp = gfc_build_array_ref (tmp, start.expr, NULL);
    7799          314 :               break;
    7800              : 
    7801           24 :             case REF_ARRAY:
    7802           24 :               gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
    7803              :                             && ref->u.ar.type == AR_ELEMENT);
    7804              : 
    7805              :               /* TODO - Add bounds checking.  */
    7806           24 :               stride = gfc_index_one_node;
    7807           24 :               index = gfc_index_zero_node;
    7808           55 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7809              :                 {
    7810           31 :                   tree itmp;
    7811           31 :                   tree jtmp;
    7812              : 
    7813              :                   /* Update the index.  */
    7814           31 :                   gfc_init_se (&start, NULL);
    7815           31 :                   gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
    7816           31 :                   itmp = gfc_evaluate_now (start.expr, block);
    7817           31 :                   gfc_init_se (&start, NULL);
    7818           31 :                   gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
    7819           31 :                   jtmp = gfc_evaluate_now (start.expr, block);
    7820           31 :                   itmp = fold_build2_loc (input_location, MINUS_EXPR,
    7821              :                                           gfc_array_index_type, itmp, jtmp);
    7822           31 :                   itmp = fold_build2_loc (input_location, MULT_EXPR,
    7823              :                                           gfc_array_index_type, itmp, stride);
    7824           31 :                   index = fold_build2_loc (input_location, PLUS_EXPR,
    7825              :                                           gfc_array_index_type, itmp, index);
    7826           31 :                   index = gfc_evaluate_now (index, block);
    7827              : 
    7828              :                   /* Update the stride.  */
    7829           31 :                   gfc_init_se (&start, NULL);
    7830           31 :                   gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
    7831           31 :                   itmp =  fold_build2_loc (input_location, MINUS_EXPR,
    7832              :                                            gfc_array_index_type, start.expr,
    7833              :                                            jtmp);
    7834           31 :                   itmp =  fold_build2_loc (input_location, PLUS_EXPR,
    7835              :                                            gfc_array_index_type,
    7836              :                                            gfc_index_one_node, itmp);
    7837           31 :                   stride =  fold_build2_loc (input_location, MULT_EXPR,
    7838              :                                              gfc_array_index_type, stride, itmp);
    7839           31 :                   stride = gfc_evaluate_now (stride, block);
    7840              :                 }
    7841              : 
    7842              :               /* Apply the index to obtain the array element.  */
    7843           24 :               tmp = gfc_build_array_ref (tmp, index, NULL);
    7844           24 :               break;
    7845              : 
    7846           60 :             case REF_INQUIRY:
    7847           60 :               switch (ref->u.i)
    7848              :                 {
    7849           54 :                 case INQUIRY_RE:
    7850          108 :                   tmp = fold_build1_loc (input_location, REALPART_EXPR,
    7851           54 :                                          TREE_TYPE (TREE_TYPE (tmp)), tmp);
    7852           54 :                   break;
    7853              : 
    7854            6 :                 case INQUIRY_IM:
    7855           12 :                   tmp = fold_build1_loc (input_location, IMAGPART_EXPR,
    7856            6 :                                          TREE_TYPE (TREE_TYPE (tmp)), tmp);
    7857            6 :                   break;
    7858              : 
    7859              :                 default:
    7860              :                   break;
    7861              :                 }
    7862              :               break;
    7863              : 
    7864            0 :             default:
    7865            0 :               gcc_unreachable ();
    7866          885 :               break;
    7867              :             }
    7868              :         }
    7869              :     }
    7870              : 
    7871              :   /* Set the target data pointer.  */
    7872        60203 :   offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
    7873              : 
    7874              :   /* Check for optional dummy argument being present.  Arguments of BIND(C)
    7875              :      procedures are excepted here since they are handled differently.  */
    7876        60203 :   if (expr->expr_type == EXPR_VARIABLE
    7877        52844 :       && expr->symtree->n.sym->attr.dummy
    7878         6533 :       && expr->symtree->n.sym->attr.optional
    7879        61195 :       && !is_CFI_desc (NULL, expr))
    7880         1624 :     offset = build3_loc (input_location, COND_EXPR, TREE_TYPE (offset),
    7881          812 :                          gfc_conv_expr_present (expr->symtree->n.sym), offset,
    7882          812 :                          fold_convert (TREE_TYPE (offset), gfc_index_zero_node));
    7883              : 
    7884        60203 :   gfc_conv_descriptor_data_set (block, parm, offset);
    7885              : }
    7886              : 
    7887              : 
    7888              : /* gfc_conv_expr_descriptor needs the string length an expression
    7889              :    so that the size of the temporary can be obtained.  This is done
    7890              :    by adding up the string lengths of all the elements in the
    7891              :    expression.  Function with non-constant expressions have their
    7892              :    string lengths mapped onto the actual arguments using the
    7893              :    interface mapping machinery in trans-expr.cc.  */
    7894              : static void
    7895         1584 : get_array_charlen (gfc_expr *expr, gfc_se *se)
    7896              : {
    7897         1584 :   gfc_interface_mapping mapping;
    7898         1584 :   gfc_formal_arglist *formal;
    7899         1584 :   gfc_actual_arglist *arg;
    7900         1584 :   gfc_se tse;
    7901         1584 :   gfc_expr *e;
    7902              : 
    7903         1584 :   if (expr->ts.u.cl->length
    7904         1584 :         && gfc_is_constant_expr (expr->ts.u.cl->length))
    7905              :     {
    7906         1237 :       if (!expr->ts.u.cl->backend_decl)
    7907          471 :         gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    7908         1369 :       return;
    7909              :     }
    7910              : 
    7911          347 :   switch (expr->expr_type)
    7912              :     {
    7913          130 :     case EXPR_ARRAY:
    7914              : 
    7915              :       /* This is somewhat brutal. The expression for the first
    7916              :          element of the array is evaluated and assigned to a
    7917              :          new string length for the original expression.  */
    7918          130 :       e = gfc_constructor_first (expr->value.constructor)->expr;
    7919              : 
    7920          130 :       gfc_init_se (&tse, NULL);
    7921              : 
    7922              :       /* Avoid evaluating trailing array references since all we need is
    7923              :          the string length.  */
    7924          130 :       if (e->rank)
    7925           38 :         tse.descriptor_only = 1;
    7926          130 :       if (e->rank && e->expr_type != EXPR_VARIABLE)
    7927            1 :         gfc_conv_expr_descriptor (&tse, e);
    7928              :       else
    7929          129 :         gfc_conv_expr (&tse, e);
    7930              : 
    7931          130 :       gfc_add_block_to_block (&se->pre, &tse.pre);
    7932          130 :       gfc_add_block_to_block (&se->post, &tse.post);
    7933              : 
    7934          130 :       if (!expr->ts.u.cl->backend_decl || !VAR_P (expr->ts.u.cl->backend_decl))
    7935              :         {
    7936           87 :           expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7937           87 :           expr->ts.u.cl->backend_decl =
    7938           87 :                         gfc_create_var (gfc_charlen_type_node, "sln");
    7939              :         }
    7940              : 
    7941          130 :       gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    7942              :                       tse.string_length);
    7943              : 
    7944              :       /* Make sure that deferred length components point to the hidden
    7945              :          string_length component.  */
    7946          130 :       if (TREE_CODE (tse.expr) == COMPONENT_REF
    7947           25 :           && TREE_CODE (tse.string_length) == COMPONENT_REF
    7948          149 :           && TREE_OPERAND (tse.expr, 0) == TREE_OPERAND (tse.string_length, 0))
    7949           19 :         e->ts.u.cl->backend_decl = expr->ts.u.cl->backend_decl;
    7950              : 
    7951              :       return;
    7952              : 
    7953           91 :     case EXPR_OP:
    7954           91 :       get_array_charlen (expr->value.op.op1, se);
    7955              : 
    7956              :       /* For parentheses the expression ts.u.cl should be identical.  */
    7957           91 :       if (expr->value.op.op == INTRINSIC_PARENTHESES)
    7958              :         {
    7959            2 :           if (expr->value.op.op1->ts.u.cl != expr->ts.u.cl)
    7960            2 :             expr->ts.u.cl->backend_decl
    7961            2 :                         = expr->value.op.op1->ts.u.cl->backend_decl;
    7962              :           return;
    7963              :         }
    7964              : 
    7965          178 :       expr->ts.u.cl->backend_decl =
    7966           89 :                 gfc_create_var (gfc_charlen_type_node, "sln");
    7967              : 
    7968           89 :       if (expr->value.op.op2)
    7969              :         {
    7970           89 :           get_array_charlen (expr->value.op.op2, se);
    7971              : 
    7972           89 :           gcc_assert (expr->value.op.op == INTRINSIC_CONCAT);
    7973              : 
    7974              :           /* Add the string lengths and assign them to the expression
    7975              :              string length backend declaration.  */
    7976           89 :           gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    7977              :                           fold_build2_loc (input_location, PLUS_EXPR,
    7978              :                                 gfc_charlen_type_node,
    7979           89 :                                 expr->value.op.op1->ts.u.cl->backend_decl,
    7980           89 :                                 expr->value.op.op2->ts.u.cl->backend_decl));
    7981              :         }
    7982              :       else
    7983            0 :         gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl,
    7984            0 :                         expr->value.op.op1->ts.u.cl->backend_decl);
    7985              :       break;
    7986              : 
    7987           44 :     case EXPR_FUNCTION:
    7988           44 :       if (expr->value.function.esym == NULL
    7989           37 :             || expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    7990              :         {
    7991            7 :           gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    7992            7 :           break;
    7993              :         }
    7994              : 
    7995              :       /* Map expressions involving the dummy arguments onto the actual
    7996              :          argument expressions.  */
    7997           37 :       gfc_init_interface_mapping (&mapping);
    7998           37 :       formal = gfc_sym_get_dummy_args (expr->symtree->n.sym);
    7999           37 :       arg = expr->value.function.actual;
    8000              : 
    8001              :       /* Set se = NULL in the calls to the interface mapping, to suppress any
    8002              :          backend stuff.  */
    8003          113 :       for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
    8004              :         {
    8005           38 :           if (!arg->expr)
    8006            0 :             continue;
    8007           38 :           if (formal->sym)
    8008           38 :           gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
    8009              :         }
    8010              : 
    8011           37 :       gfc_init_se (&tse, NULL);
    8012              : 
    8013              :       /* Build the expression for the character length and convert it.  */
    8014           37 :       gfc_apply_interface_mapping (&mapping, &tse, expr->ts.u.cl->length);
    8015              : 
    8016           37 :       gfc_add_block_to_block (&se->pre, &tse.pre);
    8017           37 :       gfc_add_block_to_block (&se->post, &tse.post);
    8018           37 :       tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
    8019           74 :       tse.expr = fold_build2_loc (input_location, MAX_EXPR,
    8020           37 :                                   TREE_TYPE (tse.expr), tse.expr,
    8021           37 :                                   build_zero_cst (TREE_TYPE (tse.expr)));
    8022           37 :       expr->ts.u.cl->backend_decl = tse.expr;
    8023           37 :       gfc_free_interface_mapping (&mapping);
    8024           37 :       break;
    8025              : 
    8026           82 :     default:
    8027           82 :       gfc_conv_string_length (expr->ts.u.cl, expr, &se->pre);
    8028           82 :       break;
    8029              :     }
    8030              : }
    8031              : 
    8032              : 
    8033              : /* Helper function to check dimensions.  */
    8034              : static bool
    8035            0 : transposed_dims (gfc_ss *ss)
    8036              : {
    8037            0 :   int n;
    8038              : 
    8039       178714 :   for (n = 0; n < ss->dimen; n++)
    8040        89767 :     if (ss->dim[n] != n)
    8041              :       return true;
    8042              :   return false;
    8043              : }
    8044              : 
    8045              : 
    8046              : /* Convert the last ref of a scalar coarray from an AR_ELEMENT to an
    8047              :    AR_FULL, suitable for the scalarizer.  */
    8048              : 
    8049              : static gfc_ss *
    8050         1657 : walk_coarray (gfc_expr *e)
    8051              : {
    8052         1657 :   gfc_ss *ss;
    8053              : 
    8054         1657 :   ss = gfc_walk_expr (e);
    8055              : 
    8056              :   /* Fix scalar coarray.  */
    8057         1657 :   if (ss == gfc_ss_terminator)
    8058              :     {
    8059          481 :       gfc_ref *ref;
    8060              : 
    8061          481 :       ref = e->ref;
    8062          659 :       while (ref)
    8063              :         {
    8064          659 :           if (ref->type == REF_ARRAY
    8065          481 :               && ref->u.ar.codimen > 0)
    8066              :             break;
    8067              : 
    8068          178 :           ref = ref->next;
    8069              :         }
    8070              : 
    8071          481 :       gcc_assert (ref != NULL);
    8072          481 :       if (ref->u.ar.type == AR_ELEMENT)
    8073          463 :         ref->u.ar.type = AR_SECTION;
    8074          481 :       ss = gfc_reverse_ss (gfc_walk_array_ref (ss, e, ref, false));
    8075              :     }
    8076              : 
    8077         1657 :   return ss;
    8078              : }
    8079              : 
    8080              : gfc_array_spec *
    8081         2327 : get_coarray_as (const gfc_expr *e)
    8082              : {
    8083         2327 :   gfc_array_spec *as;
    8084         2327 :   gfc_symbol *sym = e->symtree->n.sym;
    8085         2327 :   gfc_component *comp;
    8086              : 
    8087         2327 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.codimension)
    8088          631 :     as = CLASS_DATA (sym)->as;
    8089         1696 :   else if (sym->attr.codimension)
    8090         1636 :     as = sym->as;
    8091              :   else
    8092              :     as = nullptr;
    8093              : 
    8094         5387 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
    8095              :     {
    8096         3060 :       switch (ref->type)
    8097              :         {
    8098          733 :         case REF_COMPONENT:
    8099          733 :           comp = ref->u.c.component;
    8100          733 :           if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.codimension)
    8101           18 :             as = CLASS_DATA (comp)->as;
    8102          715 :           else if (comp->ts.type != BT_CLASS && comp->attr.codimension)
    8103          691 :             as = comp->as;
    8104              :           break;
    8105              : 
    8106              :         case REF_ARRAY:
    8107              :         case REF_SUBSTRING:
    8108              :         case REF_INQUIRY:
    8109              :           break;
    8110              :         }
    8111              :     }
    8112              : 
    8113         2327 :   return as;
    8114              : }
    8115              : 
    8116              : bool
    8117       146398 : is_explicit_coarray (gfc_expr *expr)
    8118              : {
    8119       146398 :   if (!gfc_is_coarray (expr))
    8120              :     return false;
    8121              : 
    8122         2327 :   gfc_array_spec *cas = get_coarray_as (expr);
    8123         2327 :   return cas && cas->cotype == AS_EXPLICIT;
    8124              : }
    8125              : 
    8126              : /* Convert an array for passing as an actual argument.  Expressions and
    8127              :    vector subscripts are evaluated and stored in a temporary, which is then
    8128              :    passed.  For whole arrays the descriptor is passed.  For array sections
    8129              :    a modified copy of the descriptor is passed, but using the original data.
    8130              : 
    8131              :    This function is also used for array pointer assignments, and there
    8132              :    are three cases:
    8133              : 
    8134              :      - se->want_pointer && !se->direct_byref
    8135              :          EXPR is an actual argument.  On exit, se->expr contains a
    8136              :          pointer to the array descriptor.
    8137              : 
    8138              :      - !se->want_pointer && !se->direct_byref
    8139              :          EXPR is an actual argument to an intrinsic function or the
    8140              :          left-hand side of a pointer assignment.  On exit, se->expr
    8141              :          contains the descriptor for EXPR.
    8142              : 
    8143              :      - !se->want_pointer && se->direct_byref
    8144              :          EXPR is the right-hand side of a pointer assignment and
    8145              :          se->expr is the descriptor for the previously-evaluated
    8146              :          left-hand side.  The function creates an assignment from
    8147              :          EXPR to se->expr.
    8148              : 
    8149              : 
    8150              :    The se->force_tmp flag disables the non-copying descriptor optimization
    8151              :    that is used for transpose. It may be used in cases where there is an
    8152              :    alias between the transpose argument and another argument in the same
    8153              :    function call.  */
    8154              : 
    8155              : void
    8156       163065 : gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
    8157              : {
    8158       163065 :   gfc_ss *ss;
    8159       163065 :   gfc_ss_type ss_type;
    8160       163065 :   gfc_ss_info *ss_info;
    8161       163065 :   gfc_loopinfo loop;
    8162       163065 :   gfc_array_info *info;
    8163       163065 :   int need_tmp;
    8164       163065 :   int n;
    8165       163065 :   tree tmp;
    8166       163065 :   tree desc;
    8167       163065 :   stmtblock_t block;
    8168       163065 :   tree start;
    8169       163065 :   int full;
    8170       163065 :   bool subref_array_target = false;
    8171       163065 :   bool deferred_array_component = false;
    8172       163065 :   bool substr = false;
    8173       163065 :   gfc_expr *arg, *ss_expr;
    8174              : 
    8175       163065 :   if (se->want_coarray || expr->rank == 0)
    8176         1657 :     ss = walk_coarray (expr);
    8177              :   else
    8178       161408 :     ss = gfc_walk_expr (expr);
    8179              : 
    8180       163065 :   gcc_assert (ss != NULL);
    8181       163065 :   gcc_assert (ss != gfc_ss_terminator);
    8182              : 
    8183       163065 :   ss_info = ss->info;
    8184       163065 :   ss_type = ss_info->type;
    8185       163065 :   ss_expr = ss_info->expr;
    8186              : 
    8187              :   /* Special case: TRANSPOSE which needs no temporary.  */
    8188       168482 :   while (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym
    8189       168248 :          && (arg = gfc_get_noncopying_intrinsic_argument (expr)) != NULL)
    8190              :     {
    8191              :       /* This is a call to transpose which has already been handled by the
    8192              :          scalarizer, so that we just need to get its argument's descriptor.  */
    8193          444 :       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
    8194          444 :       expr = expr->value.function.actual->expr;
    8195              :     }
    8196              : 
    8197       163065 :   if (!se->direct_byref)
    8198       313321 :     se->unlimited_polymorphic = UNLIMITED_POLY (expr);
    8199              : 
    8200              :   /* Special case things we know we can pass easily.  */
    8201       163065 :   switch (expr->expr_type)
    8202              :     {
    8203       146683 :     case EXPR_VARIABLE:
    8204              :       /* If we have a linear array section, we can pass it directly.
    8205              :          Otherwise we need to copy it into a temporary.  */
    8206              : 
    8207       146683 :       gcc_assert (ss_type == GFC_SS_SECTION);
    8208       146683 :       gcc_assert (ss_expr == expr);
    8209       146683 :       info = &ss_info->data.array;
    8210              : 
    8211              :       /* Get the descriptor for the array.  */
    8212       146683 :       gfc_conv_ss_descriptor (&se->pre, ss, 0);
    8213       146683 :       desc = info->descriptor;
    8214              : 
    8215              :       /* The charlen backend decl for deferred character components cannot
    8216              :          be used because it is fixed at zero.  Instead, the hidden string
    8217              :          length component is used.  */
    8218       146683 :       if (expr->ts.type == BT_CHARACTER
    8219        20287 :           && expr->ts.deferred
    8220         2836 :           && TREE_CODE (desc) == COMPONENT_REF)
    8221       146683 :         deferred_array_component = true;
    8222              : 
    8223       146683 :       substr = info->ref && info->ref->next
    8224       147673 :                && info->ref->next->type == REF_SUBSTRING;
    8225              : 
    8226       146683 :       subref_array_target = (is_subref_array (expr)
    8227       146683 :                              && (se->direct_byref
    8228         3095 :                                  || se->force_no_tmp
    8229         2475 :                                  || expr->ts.type == BT_CHARACTER));
    8230       146683 :       need_tmp = (gfc_ref_needs_temporary_p (expr->ref)
    8231       146683 :                   && !subref_array_target);
    8232              : 
    8233       146683 :       if (se->force_tmp)
    8234              :         need_tmp = 1;
    8235       146500 :       else if (se->force_no_tmp)
    8236              :         need_tmp = 0;
    8237              : 
    8238       139864 :       if (need_tmp)
    8239              :         full = 0;
    8240       146398 :       else if (is_explicit_coarray (expr))
    8241              :         full = 0;
    8242       145521 :       else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
    8243              :         {
    8244              :           /* Create a new descriptor if the array doesn't have one.  */
    8245              :           full = 0;
    8246              :         }
    8247        95184 :       else if (info->ref->u.ar.type == AR_FULL || se->descriptor_only)
    8248              :         full = 1;
    8249         8164 :       else if (se->direct_byref)
    8250              :         full = 0;
    8251         7801 :       else if (info->ref->u.ar.dimen == 0 && !info->ref->next)
    8252              :         full = 1;
    8253         7591 :       else if (info->ref->u.ar.type == AR_SECTION && se->want_pointer)
    8254              :         full = 0;
    8255              :       else
    8256         3651 :         full = gfc_full_array_ref_p (info->ref, NULL);
    8257              : 
    8258              :       /* A subobject of the array elements is described by a new descriptor,
    8259              :          whose element type is that of the subobject and whose span is the
    8260              :          element size of the array.  */
    8261       146683 :       if (subref_array_target && !se->direct_byref
    8262         1323 :           && info->ref && info->ref->next)
    8263              :         full = 0;
    8264              : 
    8265       233844 :       if (full && !transposed_dims (ss))
    8266              :         {
    8267        87438 :           if (se->direct_byref && !se->byref_noassign)
    8268              :             {
    8269         1108 :               struct lang_type *lhs_ls
    8270         1108 :                 = TYPE_LANG_SPECIFIC (TREE_TYPE (se->expr)),
    8271         1108 :                 *rhs_ls = TYPE_LANG_SPECIFIC (TREE_TYPE (desc));
    8272              :               /* When only the array_kind differs, do a view_convert.  */
    8273         1516 :               tmp = lhs_ls && rhs_ls && lhs_ls->rank == rhs_ls->rank
    8274         1108 :                         && lhs_ls->akind != rhs_ls->akind
    8275         1516 :                       ? build1 (VIEW_CONVERT_EXPR, TREE_TYPE (se->expr), desc)
    8276              :                       : desc;
    8277              :               /* Copy the descriptor for pointer assignments.  */
    8278         1108 :               gfc_add_modify (&se->pre, se->expr, tmp);
    8279              : 
    8280              :               /* Add any offsets from subreferences.  */
    8281         1108 :               gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
    8282              :                                       subref_array_target, expr);
    8283              : 
    8284              :               /* ....and set the span field.  */
    8285         1108 :               if (ss_info->expr->ts.type == BT_CHARACTER)
    8286          147 :                 tmp = gfc_conv_descriptor_span_get (desc);
    8287              :               else
    8288          961 :                 tmp = gfc_get_array_span (desc, expr);
    8289         1108 :               gfc_conv_descriptor_span_set (&se->pre, se->expr, tmp);
    8290         1108 :             }
    8291        86330 :           else if (se->want_pointer)
    8292              :             {
    8293              :               /* We pass full arrays directly.  This means that pointers and
    8294              :                  allocatable arrays should also work.  */
    8295        14201 :               se->expr = gfc_build_addr_expr (NULL_TREE, desc);
    8296              :             }
    8297              :           else
    8298              :             {
    8299        72129 :               se->expr = desc;
    8300              :             }
    8301              : 
    8302        87438 :           if (expr->ts.type == BT_CHARACTER && !deferred_array_component)
    8303         8426 :             se->string_length = gfc_get_expr_charlen (expr);
    8304              :           /* The ss_info string length is returned set to the value of the
    8305              :              hidden string length component.  */
    8306        78743 :           else if (deferred_array_component)
    8307          269 :             se->string_length = ss_info->string_length;
    8308              : 
    8309        87438 :           se->class_container = ss_info->class_container;
    8310              : 
    8311        87438 :           gfc_free_ss_chain (ss);
    8312       175002 :           return;
    8313              :         }
    8314              :       break;
    8315              : 
    8316         4973 :     case EXPR_FUNCTION:
    8317              :       /* A transformational function return value will be a temporary
    8318              :          array descriptor.  We still need to go through the scalarizer
    8319              :          to create the descriptor.  Elemental functions are handled as
    8320              :          arbitrary expressions, i.e. copy to a temporary.  */
    8321              : 
    8322         4973 :       if (se->direct_byref)
    8323              :         {
    8324          126 :           gcc_assert (ss_type == GFC_SS_FUNCTION && ss_expr == expr);
    8325              : 
    8326              :           /* For pointer assignments pass the descriptor directly.  */
    8327          126 :           if (se->ss == NULL)
    8328          126 :             se->ss = ss;
    8329              :           else
    8330            0 :             gcc_assert (se->ss == ss);
    8331              : 
    8332          126 :           se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    8333          126 :           gfc_conv_expr (se, expr);
    8334              : 
    8335          126 :           gfc_free_ss_chain (ss);
    8336          126 :           return;
    8337              :         }
    8338              : 
    8339         4847 :       if (ss_expr != expr || ss_type != GFC_SS_FUNCTION)
    8340              :         {
    8341         3325 :           if (ss_expr != expr)
    8342              :             /* Elemental function.  */
    8343         2576 :             gcc_assert ((expr->value.function.esym != NULL
    8344              :                          && expr->value.function.esym->attr.elemental)
    8345              :                         || (expr->value.function.isym != NULL
    8346              :                             && expr->value.function.isym->elemental)
    8347              :                         || (gfc_expr_attr (expr).proc_pointer
    8348              :                             && gfc_expr_attr (expr).elemental)
    8349              :                         || gfc_inline_intrinsic_function_p (expr));
    8350              : 
    8351         3325 :           need_tmp = 1;
    8352         3325 :           if (expr->ts.type == BT_CHARACTER
    8353           35 :                 && expr->ts.u.cl->length
    8354           29 :                 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    8355           13 :             get_array_charlen (expr, se);
    8356              : 
    8357              :           info = NULL;
    8358              :         }
    8359              :       else
    8360              :         {
    8361              :           /* Transformational function.  */
    8362         1522 :           info = &ss_info->data.array;
    8363         1522 :           need_tmp = 0;
    8364              :         }
    8365              :       break;
    8366              : 
    8367        10651 :     case EXPR_ARRAY:
    8368              :       /* Constant array constructors don't need a temporary.  */
    8369        10651 :       if (ss_type == GFC_SS_CONSTRUCTOR
    8370        10651 :           && expr->ts.type != BT_CHARACTER
    8371        20043 :           && gfc_constant_array_constructor_p (expr->value.constructor))
    8372              :         {
    8373         7346 :           need_tmp = 0;
    8374         7346 :           info = &ss_info->data.array;
    8375              :         }
    8376              :       else
    8377              :         {
    8378              :           need_tmp = 1;
    8379              :           info = NULL;
    8380              :         }
    8381              :       break;
    8382              : 
    8383              :     default:
    8384              :       /* Something complicated.  Copy it into a temporary.  */
    8385              :       need_tmp = 1;
    8386              :       info = NULL;
    8387              :       break;
    8388              :     }
    8389              : 
    8390              :   /* If we are creating a temporary, we don't need to bother about aliases
    8391              :      anymore.  */
    8392        68126 :   if (need_tmp)
    8393         7673 :     se->force_tmp = 0;
    8394              : 
    8395        75501 :   gfc_init_loopinfo (&loop);
    8396              : 
    8397              :   /* Associate the SS with the loop.  */
    8398        75501 :   gfc_add_ss_to_loop (&loop, ss);
    8399              : 
    8400              :   /* Tell the scalarizer not to bother creating loop variables, etc.  */
    8401        75501 :   if (!need_tmp)
    8402        67828 :     loop.array_parameter = 1;
    8403              :   else
    8404              :     /* The right-hand side of a pointer assignment mustn't use a temporary.  */
    8405         7673 :     gcc_assert (!se->direct_byref);
    8406              : 
    8407              :   /* Do we need bounds checking or not?  */
    8408        75501 :   ss->no_bounds_check = expr->no_bounds_check;
    8409              : 
    8410              :   /* Setup the scalarizing loops and bounds.  */
    8411        75501 :   gfc_conv_ss_startstride (&loop);
    8412              : 
    8413              :   /* Add bounds-checking for elemental dimensions.  */
    8414        75501 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check)
    8415         6688 :     array_bound_check_elemental (&outermost_loop (&loop)->pre, ss, expr);
    8416              : 
    8417        75501 :   if (need_tmp)
    8418              :     {
    8419         7673 :       if (expr->ts.type == BT_CHARACTER
    8420         1499 :           && (!expr->ts.u.cl->backend_decl || expr->expr_type == EXPR_ARRAY))
    8421         1391 :         get_array_charlen (expr, se);
    8422              : 
    8423              :       /* Tell the scalarizer to make a temporary.  */
    8424         7673 :       loop.temp_ss = gfc_get_temp_ss (gfc_typenode_for_spec (&expr->ts),
    8425         7673 :                                       ((expr->ts.type == BT_CHARACTER)
    8426         1499 :                                        ? expr->ts.u.cl->backend_decl
    8427              :                                        : NULL),
    8428              :                                       loop.dimen);
    8429              : 
    8430         7673 :       se->string_length = loop.temp_ss->info->string_length;
    8431         7673 :       gcc_assert (loop.temp_ss->dimen == loop.dimen);
    8432         7673 :       gfc_add_ss_to_loop (&loop, loop.temp_ss);
    8433              :     }
    8434              : 
    8435        75501 :   gfc_conv_loop_setup (&loop, & expr->where);
    8436              : 
    8437        75501 :   if (need_tmp)
    8438              :     {
    8439              :       /* Copy into a temporary and pass that.  We don't need to copy the data
    8440              :          back because expressions and vector subscripts must be INTENT_IN.  */
    8441              :       /* TODO: Optimize passing function return values.  */
    8442         7673 :       gfc_se lse;
    8443         7673 :       gfc_se rse;
    8444         7673 :       bool deep_copy;
    8445              : 
    8446              :       /* Start the copying loops.  */
    8447         7673 :       gfc_mark_ss_chain_used (loop.temp_ss, 1);
    8448         7673 :       gfc_mark_ss_chain_used (ss, 1);
    8449         7673 :       gfc_start_scalarized_body (&loop, &block);
    8450              : 
    8451              :       /* Copy each data element.  */
    8452         7673 :       gfc_init_se (&lse, NULL);
    8453         7673 :       gfc_copy_loopinfo_to_se (&lse, &loop);
    8454         7673 :       gfc_init_se (&rse, NULL);
    8455         7673 :       gfc_copy_loopinfo_to_se (&rse, &loop);
    8456              : 
    8457         7673 :       lse.ss = loop.temp_ss;
    8458         7673 :       rse.ss = ss;
    8459              : 
    8460         7673 :       gfc_conv_tmp_array_ref (&lse);
    8461         7673 :       if (expr->ts.type == BT_CHARACTER)
    8462              :         {
    8463         1499 :           gfc_conv_expr (&rse, expr);
    8464         1499 :           if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
    8465         1069 :             rse.expr = build_fold_indirect_ref_loc (input_location,
    8466              :                                                 rse.expr);
    8467              :         }
    8468              :       else
    8469         6174 :         gfc_conv_expr_val (&rse, expr);
    8470              : 
    8471         7673 :       gfc_add_block_to_block (&block, &rse.pre);
    8472         7673 :       gfc_add_block_to_block (&block, &lse.pre);
    8473              : 
    8474         7673 :       lse.string_length = rse.string_length;
    8475              : 
    8476        15346 :       deep_copy = !se->data_not_needed
    8477         7673 :                   && (expr->expr_type == EXPR_VARIABLE
    8478         7135 :                       || expr->expr_type == EXPR_ARRAY);
    8479         7673 :       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
    8480              :                                      deep_copy, false);
    8481         7673 :       gfc_add_expr_to_block (&block, tmp);
    8482              : 
    8483              :       /* Finish the copying loops.  */
    8484         7673 :       gfc_trans_scalarizing_loops (&loop, &block);
    8485              : 
    8486         7673 :       desc = loop.temp_ss->info->data.array.descriptor;
    8487              :     }
    8488        69350 :   else if (expr->expr_type == EXPR_FUNCTION && !transposed_dims (ss))
    8489              :     {
    8490         1509 :       desc = info->descriptor;
    8491         1509 :       se->string_length = ss_info->string_length;
    8492              :     }
    8493              :   else
    8494              :     {
    8495              :       /* We pass sections without copying to a temporary.  Make a new
    8496              :          descriptor and point it at the section we want.  The loop variable
    8497              :          limits will be the limits of the section.
    8498              :          A function may decide to repack the array to speed up access, but
    8499              :          we're not bothered about that here.  */
    8500        66319 :       int dim, ndim, codim;
    8501        66319 :       tree parm;
    8502        66319 :       tree parmtype;
    8503        66319 :       tree dtype;
    8504        66319 :       tree stride;
    8505        66319 :       tree from;
    8506        66319 :       tree to;
    8507        66319 :       tree base;
    8508        66319 :       tree offset;
    8509              : 
    8510        66319 :       ndim = info->ref ? info->ref->u.ar.dimen : ss->dimen;
    8511              : 
    8512        66319 :       if (se->want_coarray)
    8513              :         {
    8514          751 :           gfc_array_ref *ar = &info->ref->u.ar;
    8515              : 
    8516          751 :           codim = expr->corank;
    8517         1579 :           for (n = 0; n < codim - 1; n++)
    8518              :             {
    8519              :               /* Make sure we are not lost somehow.  */
    8520          828 :               gcc_assert (ar->dimen_type[n + ndim] == DIMEN_THIS_IMAGE);
    8521              : 
    8522              :               /* Make sure the call to gfc_conv_section_startstride won't
    8523              :                  generate unnecessary code to calculate stride.  */
    8524          828 :               gcc_assert (ar->stride[n + ndim] == NULL);
    8525              : 
    8526          828 :               gfc_conv_section_startstride (&loop.pre, ss, n + ndim);
    8527          828 :               loop.from[n + loop.dimen] = info->start[n + ndim];
    8528          828 :               loop.to[n + loop.dimen]   = info->end[n + ndim];
    8529              :             }
    8530              : 
    8531          751 :           gcc_assert (n == codim - 1);
    8532          751 :           evaluate_bound (&loop.pre, info->start, ar->start,
    8533              :                           info->descriptor, n + ndim, true,
    8534          751 :                           ar->as->type == AS_DEFERRED, true);
    8535          751 :           loop.from[n + loop.dimen] = info->start[n + ndim];
    8536              :         }
    8537              :       else
    8538              :         codim = 0;
    8539              : 
    8540              :       /* Set the string_length for a character array.  */
    8541        66319 :       if (expr->ts.type == BT_CHARACTER)
    8542              :         {
    8543        11548 :           if (deferred_array_component && !substr)
    8544           37 :             se->string_length = ss_info->string_length;
    8545              :           else
    8546        11511 :             se->string_length =  gfc_get_expr_charlen (expr);
    8547              : 
    8548        11548 :           if (VAR_P (se->string_length)
    8549          990 :               && expr->ts.u.cl->backend_decl == se->string_length)
    8550          984 :             tmp = ss_info->string_length;
    8551              :           else
    8552              :             tmp = se->string_length;
    8553              : 
    8554        11548 :           if (expr->ts.deferred && expr->ts.u.cl->backend_decl
    8555          205 :               && VAR_P (expr->ts.u.cl->backend_decl))
    8556          150 :             gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp);
    8557              :           else
    8558        11398 :             expr->ts.u.cl->backend_decl = tmp;
    8559              :         }
    8560              : 
    8561              :       /* If we have an array section, are assigning  or passing an array
    8562              :          section argument make sure that the lower bound is 1.  References
    8563              :          to the full array should otherwise keep the original bounds.  */
    8564        66319 :       if (!info->ref || info->ref->u.ar.type != AR_FULL)
    8565        84680 :         for (dim = 0; dim < loop.dimen; dim++)
    8566        51411 :           if (!integer_onep (loop.from[dim]))
    8567              :             {
    8568        27873 :               tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8569              :                                      gfc_array_index_type, gfc_index_one_node,
    8570              :                                      loop.from[dim]);
    8571        27873 :               loop.to[dim] = fold_build2_loc (input_location, PLUS_EXPR,
    8572              :                                               gfc_array_index_type,
    8573              :                                               loop.to[dim], tmp);
    8574        27873 :               loop.from[dim] = gfc_index_one_node;
    8575              :             }
    8576              : 
    8577        66319 :       desc = info->descriptor;
    8578        66319 :       if (se->direct_byref && !se->byref_noassign)
    8579              :         {
    8580              :           /* For pointer assignments we fill in the destination.  */
    8581         2730 :           parm = se->expr;
    8582         2730 :           parmtype = TREE_TYPE (parm);
    8583              :         }
    8584              :       else
    8585              :         {
    8586              :           /* Otherwise make a new one.  The element type is that of the
    8587              :              subobject for a subreference of the array.  */
    8588        63589 :           if (expr->ts.type == BT_CHARACTER
    8589        52705 :               || (subref_array_target && !se->direct_byref))
    8590        11046 :             parmtype = gfc_typenode_for_spec (&expr->ts);
    8591              :           else
    8592        52543 :             parmtype = gfc_get_element_type (TREE_TYPE (desc));
    8593              : 
    8594        63589 :           parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, codim,
    8595              :                                                 loop.from, loop.to, 0,
    8596              :                                                 GFC_ARRAY_UNKNOWN, false);
    8597        63589 :           parm = gfc_create_var (parmtype, "parm");
    8598              : 
    8599              :           /* When expression is a class object, then add the class' handle to
    8600              :              the parm_decl.  */
    8601        63589 :           if (expr->ts.type == BT_CLASS && expr->expr_type == EXPR_VARIABLE)
    8602              :             {
    8603         1262 :               gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (expr);
    8604         1262 :               gfc_se classse;
    8605              : 
    8606              :               /* class_expr can be NULL, when no _class ref is in expr.
    8607              :                  We must not fix this here with a gfc_fix_class_ref ().  */
    8608         1262 :               if (class_expr)
    8609              :                 {
    8610         1252 :                   gfc_init_se (&classse, NULL);
    8611         1252 :                   gfc_conv_expr (&classse, class_expr);
    8612         1252 :                   gfc_free_expr (class_expr);
    8613              : 
    8614         1252 :                   gcc_assert (classse.pre.head == NULL_TREE
    8615              :                               && classse.post.head == NULL_TREE);
    8616         1252 :                   gfc_allocate_lang_decl (parm);
    8617         1252 :                   GFC_DECL_SAVED_DESCRIPTOR (parm) = classse.expr;
    8618              :                 }
    8619              :             }
    8620              :         }
    8621              : 
    8622        66319 :       if (expr->ts.type == BT_CHARACTER
    8623        66319 :           && VAR_P (TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)))))
    8624              :         {
    8625            0 :           tree elem_len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (parm)));
    8626            0 :           gfc_add_modify (&loop.pre, elem_len,
    8627            0 :                           fold_convert (TREE_TYPE (elem_len),
    8628              :                           gfc_get_array_span (desc, expr)));
    8629              :         }
    8630              : 
    8631              :       /* Set the span field.  */
    8632        66319 :       tmp = NULL_TREE;
    8633        66319 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    8634         7778 :         tmp = gfc_conv_descriptor_span_get (desc);
    8635              :       else
    8636        58541 :         tmp = gfc_get_array_span (desc, expr);
    8637        66319 :       if (tmp)
    8638        66251 :         gfc_conv_descriptor_span_set (&loop.pre, parm, tmp);
    8639              : 
    8640              :       /* The following can be somewhat confusing.  We have two
    8641              :          descriptors, a new one and the original array.
    8642              :          {parm, parmtype, dim} refer to the new one.
    8643              :          {desc, type, n, loop} refer to the original, which maybe
    8644              :          a descriptorless array.
    8645              :          The bounds of the scalarization are the bounds of the section.
    8646              :          We don't have to worry about numeric overflows when calculating
    8647              :          the offsets because all elements are within the array data.  */
    8648              : 
    8649              :       /* Set the dtype.  */
    8650        66319 :       if (se->unlimited_polymorphic)
    8651          679 :         dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
    8652        65640 :       else if (expr->ts.type == BT_ASSUMED)
    8653              :         {
    8654          127 :           tree tmp2 = desc;
    8655          127 :           if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR (tmp2))
    8656          127 :             tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
    8657          127 :           if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
    8658          127 :             tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
    8659          127 :           dtype = gfc_conv_descriptor_dtype_get (tmp2);
    8660              :         }
    8661              :       else
    8662        65513 :         dtype = gfc_get_dtype (parmtype);
    8663        66319 :       gfc_conv_descriptor_dtype_set (&loop.pre, parm, dtype);
    8664              : 
    8665              :       /* The 1st element in the section.  */
    8666        66319 :       base = gfc_index_zero_node;
    8667        66319 :       if (expr->ts.type == BT_CHARACTER && expr->rank == 0 && codim)
    8668            6 :         base = gfc_index_one_node;
    8669              : 
    8670              :       /* The offset from the 1st element in the section.  */
    8671        66319 :       offset = gfc_index_zero_node;
    8672              : 
    8673       169921 :       for (n = 0; n < ndim; n++)
    8674              :         {
    8675       103602 :           stride = gfc_conv_array_stride (desc, n);
    8676              : 
    8677              :           /* Work out the 1st element in the section.  */
    8678       103602 :           if (info->ref
    8679        95798 :               && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
    8680              :             {
    8681         1275 :               gcc_assert (info->subscript[n]
    8682              :                           && info->subscript[n]->info->type == GFC_SS_SCALAR);
    8683         1275 :               start = info->subscript[n]->info->data.scalar.value;
    8684              :             }
    8685              :           else
    8686              :             {
    8687              :               /* Evaluate and remember the start of the section.  */
    8688       102327 :               start = info->start[n];
    8689       102327 :               stride = gfc_evaluate_now (stride, &loop.pre);
    8690              :             }
    8691              : 
    8692       103602 :           tmp = gfc_conv_array_lbound (desc, n);
    8693       103602 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
    8694              :                                  start, tmp);
    8695       103602 :           tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    8696              :                                  tmp, stride);
    8697       103602 :           base = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    8698              :                                     base, tmp);
    8699              : 
    8700       103602 :           if (info->ref
    8701        95798 :               && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
    8702              :             {
    8703              :               /* For elemental dimensions, we only need the 1st
    8704              :                  element in the section.  */
    8705         1275 :               continue;
    8706              :             }
    8707              : 
    8708              :           /* Vector subscripts need copying and are handled elsewhere.  */
    8709       102327 :           if (info->ref)
    8710        94523 :             gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
    8711              : 
    8712              :           /* look for the corresponding scalarizer dimension: dim.  */
    8713       153280 :           for (dim = 0; dim < ndim; dim++)
    8714       153280 :             if (ss->dim[dim] == n)
    8715              :               break;
    8716              : 
    8717              :           /* loop exited early: the DIM being looked for has been found.  */
    8718       102327 :           gcc_assert (dim < ndim);
    8719              : 
    8720              :           /* Set the new lower bound.  */
    8721       102327 :           from = loop.from[dim];
    8722       102327 :           to = loop.to[dim];
    8723              : 
    8724       102327 :           gfc_conv_descriptor_lbound_set (&loop.pre, parm,
    8725              :                                           gfc_rank_cst[dim], from);
    8726              : 
    8727              :           /* Set the new upper bound.  */
    8728       102327 :           gfc_conv_descriptor_ubound_set (&loop.pre, parm,
    8729              :                                           gfc_rank_cst[dim], to);
    8730              : 
    8731              :           /* Multiply the stride by the section stride to get the
    8732              :              total stride.  */
    8733       102327 :           stride = fold_build2_loc (input_location, MULT_EXPR,
    8734              :                                     gfc_array_index_type,
    8735              :                                     stride, info->stride[n]);
    8736              : 
    8737       102327 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    8738       102327 :                                  TREE_TYPE (offset), stride, from);
    8739       102327 :           offset = fold_build2_loc (input_location, MINUS_EXPR,
    8740       102327 :                                    TREE_TYPE (offset), offset, tmp);
    8741              : 
    8742              :           /* Store the new stride.  */
    8743       102327 :           gfc_conv_descriptor_stride_set (&loop.pre, parm,
    8744              :                                           gfc_rank_cst[dim], stride);
    8745              :         }
    8746              : 
    8747              :       /* For deferred-length character we need to take the dynamic length
    8748              :          into account for the dataptr offset.  */
    8749        66319 :       if (expr->ts.type == BT_CHARACTER
    8750        11548 :           && expr->ts.deferred
    8751          211 :           && expr->ts.u.cl->backend_decl
    8752          211 :           && VAR_P (expr->ts.u.cl->backend_decl))
    8753              :         {
    8754          150 :           tree base_type = TREE_TYPE (base);
    8755          150 :           base = fold_build2_loc (input_location, MULT_EXPR, base_type, base,
    8756              :                                   fold_convert (base_type,
    8757              :                                                 expr->ts.u.cl->backend_decl));
    8758              :         }
    8759              : 
    8760        67898 :       for (n = loop.dimen; n < loop.dimen + codim; n++)
    8761              :         {
    8762         1579 :           from = loop.from[n];
    8763         1579 :           to = loop.to[n];
    8764         1579 :           gfc_conv_descriptor_lbound_set (&loop.pre, parm,
    8765              :                                           gfc_rank_cst[n], from);
    8766         1579 :           if (n < loop.dimen + codim - 1)
    8767          828 :             gfc_conv_descriptor_ubound_set (&loop.pre, parm,
    8768              :                                             gfc_rank_cst[n], to);
    8769              :         }
    8770              : 
    8771        66319 :       if (se->data_not_needed)
    8772         6287 :         gfc_conv_descriptor_data_set (&loop.pre, parm,
    8773              :                                       gfc_index_zero_node);
    8774              :       else
    8775              :         /* Point the data pointer at the 1st element in the section.  */
    8776        60032 :         gfc_get_dataptr_offset (&loop.pre, parm, desc, base,
    8777              :                                 subref_array_target, expr);
    8778              : 
    8779        66319 :       gfc_conv_descriptor_offset_set (&loop.pre, parm, offset);
    8780              : 
    8781        66319 :       if (flag_coarray == GFC_FCOARRAY_LIB && expr->corank)
    8782              :         {
    8783          448 :           tmp = INDIRECT_REF_P (desc) ? TREE_OPERAND (desc, 0) : desc;
    8784          448 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8785              :             {
    8786           24 :               tmp = gfc_conv_descriptor_token (tmp);
    8787              :             }
    8788          424 :           else if (DECL_P (tmp) && DECL_LANG_SPECIFIC (tmp)
    8789          504 :                    && GFC_DECL_TOKEN (tmp) != NULL_TREE)
    8790           64 :             tmp = GFC_DECL_TOKEN (tmp);
    8791              :           else
    8792              :             {
    8793          360 :               tmp = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (tmp));
    8794              :             }
    8795              : 
    8796          448 :           gfc_conv_descriptor_token_set (&loop.pre, parm, tmp);
    8797              :         }
    8798              :       desc = parm;
    8799              :     }
    8800              : 
    8801              :   /* For class arrays add the class tree into the saved descriptor to
    8802              :      enable getting of _vptr and the like.  */
    8803        75501 :   if (expr->expr_type == EXPR_VARIABLE && VAR_P (desc)
    8804        58311 :       && IS_CLASS_ARRAY (expr->symtree->n.sym))
    8805              :     {
    8806         1222 :       gfc_allocate_lang_decl (desc);
    8807         1222 :       GFC_DECL_SAVED_DESCRIPTOR (desc) =
    8808         1222 :           DECL_LANG_SPECIFIC (expr->symtree->n.sym->backend_decl) ?
    8809         1130 :             GFC_DECL_SAVED_DESCRIPTOR (expr->symtree->n.sym->backend_decl)
    8810              :           : expr->symtree->n.sym->backend_decl;
    8811              :     }
    8812        74279 :   else if (expr->expr_type == EXPR_ARRAY && VAR_P (desc)
    8813        10651 :            && IS_CLASS_ARRAY (expr))
    8814              :     {
    8815           12 :       tree vtype;
    8816           12 :       gfc_allocate_lang_decl (desc);
    8817           12 :       tmp = gfc_create_var (expr->ts.u.derived->backend_decl, "class");
    8818           12 :       GFC_DECL_SAVED_DESCRIPTOR (desc) = tmp;
    8819           12 :       vtype = gfc_class_vptr_get (tmp);
    8820           12 :       gfc_add_modify (&se->pre, vtype,
    8821           12 :                       gfc_build_addr_expr (TREE_TYPE (vtype),
    8822           12 :                                       gfc_find_vtab (&expr->ts)->backend_decl));
    8823              :     }
    8824        75501 :   if (!se->direct_byref || se->byref_noassign)
    8825              :     {
    8826              :       /* Get a pointer to the new descriptor.  */
    8827        72771 :       if (se->want_pointer)
    8828        40868 :         se->expr = gfc_build_addr_expr (NULL_TREE, desc);
    8829              :       else
    8830        31903 :         se->expr = desc;
    8831              :     }
    8832              : 
    8833        75501 :   gfc_add_block_to_block (&se->pre, &loop.pre);
    8834        75501 :   gfc_add_block_to_block (&se->post, &loop.post);
    8835              : 
    8836              :   /* Cleanup the scalarizer.  */
    8837        75501 :   gfc_cleanup_loop (&loop);
    8838              : }
    8839              : 
    8840              : 
    8841              : /* Calculate the array size (number of elements); if dim != NULL_TREE,
    8842              :    return size for that dim (dim=0..rank-1; only for GFC_DESCRIPTOR_TYPE_P).
    8843              :    If !expr && descriptor array, the rank is taken from the descriptor.  */
    8844              : tree
    8845        15840 : gfc_tree_array_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree dim)
    8846              : {
    8847        15840 :   if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
    8848              :     {
    8849           40 :       gcc_assert (dim == NULL_TREE);
    8850           40 :       return GFC_TYPE_ARRAY_SIZE (TREE_TYPE (desc));
    8851              :     }
    8852        15800 :   tree size, tmp, rank = NULL_TREE, cond = NULL_TREE;
    8853        15800 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
    8854        15800 :   enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc));
    8855        15800 :   if (expr == NULL || expr->rank < 0)
    8856         3647 :     rank = gfc_conv_descriptor_rank_get (desc);
    8857              :   else
    8858        12153 :     rank = gfc_rank_cst[expr->rank];
    8859              : 
    8860        15800 :   if (dim || (expr && expr->rank == 1))
    8861              :     {
    8862         4765 :       if (dim)
    8863         9367 :         dim = fold_convert_loc (input_location, gfc_array_dim_rank_type, dim);
    8864              :       else
    8865         4765 :         dim = gfc_rank_cst[0];
    8866        14132 :       tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
    8867        14132 :       tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
    8868              : 
    8869        14132 :       size = fold_build2_loc (input_location, MINUS_EXPR,
    8870              :                               gfc_array_index_type, ubound, lbound);
    8871        14132 :       size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    8872              :                               size, gfc_index_one_node);
    8873              :       /* if (!allocatable && !pointer && assumed rank)
    8874              :            size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
    8875              :          else
    8876              :            size = max (0, size);  */
    8877        14132 :       size = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    8878              :                               size, gfc_index_zero_node);
    8879        14132 :       if (akind == GFC_ARRAY_ASSUMED_RANK_CONT
    8880        14132 :           || akind == GFC_ARRAY_ASSUMED_RANK)
    8881              :         {
    8882         2948 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8883              :                                  gfc_array_dim_rank_type, rank,
    8884              :                                  gfc_rank_cst[1]);
    8885         2948 :           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8886              :                                   dim, tmp);
    8887         2948 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8888              :                                  gfc_conv_descriptor_ubound_get (desc, dim),
    8889              :                                  build_int_cst (gfc_array_index_type, -1));
    8890         2948 :           cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
    8891              :                                   cond, tmp);
    8892         2948 :           tmp = build_int_cst (gfc_array_index_type, -1);
    8893         2948 :           size = build3_loc (input_location, COND_EXPR, gfc_array_index_type,
    8894              :                              cond, tmp, size);
    8895              :         }
    8896              :       return size;
    8897              :     }
    8898              : 
    8899              :   /* size = 1. */
    8900         1668 :   size = gfc_create_var (gfc_array_index_type, "size");
    8901         1668 :   gfc_add_modify (block, size, build_int_cst (TREE_TYPE (size), 1));
    8902         1668 :   tree extent = gfc_create_var (gfc_array_index_type, "extent");
    8903              : 
    8904         1668 :   stmtblock_t cond_block, loop_body;
    8905         1668 :   gfc_init_block (&cond_block);
    8906         1668 :   gfc_init_block (&loop_body);
    8907              : 
    8908              :   /* Loop: for (i = 0; i < rank; ++i).  */
    8909         1668 :   tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    8910              :   /* Loop body.  */
    8911              :   /* #if (assumed-rank + !allocatable && !pointer)
    8912              :        if (idx + 1 == rank && dim[idx].ubound == -1)
    8913              :          extent = -1;
    8914              :        else
    8915              :      #endif
    8916              :          extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
    8917              :          if (extent < 0)
    8918              :            extent = 0
    8919              :       size *= extent.  */
    8920         1668 :   cond = NULL_TREE;
    8921         1668 :   if (akind == GFC_ARRAY_ASSUMED_RANK_CONT || akind == GFC_ARRAY_ASSUMED_RANK)
    8922              :     {
    8923          471 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
    8924              :                              gfc_array_dim_rank_type, idx, gfc_rank_cst[1]);
    8925          471 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8926              :                               tmp, rank);
    8927          471 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    8928              :                              gfc_conv_descriptor_ubound_get (desc, idx),
    8929              :                              build_int_cst (gfc_array_index_type, -1));
    8930          471 :       cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
    8931              :                               cond, tmp);
    8932              :     }
    8933         1668 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8934              :                          gfc_conv_descriptor_ubound_get (desc, idx),
    8935              :                          gfc_conv_descriptor_lbound_get (desc, idx));
    8936         1668 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    8937              :                          tmp, gfc_index_one_node);
    8938         1668 :   gfc_add_modify (&cond_block, extent, tmp);
    8939         1668 :   tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
    8940              :                          extent, gfc_index_zero_node);
    8941         1668 :   tmp = build3_v (COND_EXPR, tmp,
    8942              :                   fold_build2_loc (input_location, MODIFY_EXPR,
    8943              :                                    gfc_array_index_type,
    8944              :                                    extent, gfc_index_zero_node),
    8945              :                   build_empty_stmt (input_location));
    8946         1668 :   gfc_add_expr_to_block (&cond_block, tmp);
    8947         1668 :   tmp = gfc_finish_block (&cond_block);
    8948         1668 :   if (cond)
    8949          471 :     tmp = build3_v (COND_EXPR, cond,
    8950              :                     fold_build2_loc (input_location, MODIFY_EXPR,
    8951              :                                      gfc_array_index_type, extent,
    8952              :                                      build_int_cst (gfc_array_index_type, -1)),
    8953              :                     tmp);
    8954         1668 :    gfc_add_expr_to_block (&loop_body, tmp);
    8955              :    /* size *= extent.  */
    8956         1668 :    gfc_add_modify (&loop_body, size,
    8957              :                    fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    8958              :                                     size, extent));
    8959              :   /* Generate loop. */
    8960         3336 :   gfc_simple_for_loop (block, idx, build_int_cst (TREE_TYPE (idx), 0), rank, LT_EXPR,
    8961         1668 :                        build_int_cst (TREE_TYPE (idx), 1),
    8962              :                        gfc_finish_block (&loop_body));
    8963         1668 :   return size;
    8964              : }
    8965              : 
    8966              : /* Helper function for gfc_conv_array_parameter if array size needs to be
    8967              :    computed.  */
    8968              : 
    8969              : static void
    8970          142 : array_parameter_size (stmtblock_t *block, tree desc, gfc_expr *expr, tree *size)
    8971              : {
    8972          142 :   tree elem;
    8973          142 :   *size = gfc_tree_array_size (block, desc, expr, NULL);
    8974          142 :   elem = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
    8975          142 :   *size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    8976              :                            *size, fold_convert (gfc_array_index_type, elem));
    8977          142 : }
    8978              : 
    8979              : /* Helper function - return true if the argument is a pointer.  */
    8980              : 
    8981              : static bool
    8982          720 : is_pointer (gfc_expr *e)
    8983              : {
    8984          720 :   gfc_symbol *sym;
    8985              : 
    8986          720 :   if (e->expr_type != EXPR_VARIABLE ||  e->symtree == NULL)
    8987              :     return false;
    8988              : 
    8989          720 :   sym = e->symtree->n.sym;
    8990          720 :   if (sym == NULL)
    8991              :     return false;
    8992              : 
    8993          720 :   return sym->attr.pointer || sym->attr.proc_pointer;
    8994              : }
    8995              : 
    8996              : /* Assumed-rank actual argument: the caller only allocates storage for dtype
    8997              :    rank dimensions. Copying GFC_MAX_DIMENSIONS dim entries would read past the
    8998              :    physical end of the descriptor. Copy the header fields explicitly and use a
    8999              :    runtime-sized memcpy for the dim[] entries.  */
    9000              : void
    9001           78 : gfc_resize_assumed_rank_dim_field (gfc_se *se, stmtblock_t *block, tree desc)
    9002              : {
    9003           78 :   tree rank, dim_field, dim_size, copy_size, dst_ptr, src_ptr;
    9004              : 
    9005           78 :   gfc_conv_descriptor_data_set (block, desc,
    9006              :                                 gfc_conv_descriptor_data_get (se->expr));
    9007           78 :   gfc_conv_descriptor_offset_set (block, desc,
    9008              :                                   gfc_conv_descriptor_offset_get (se->expr));
    9009           78 :   gfc_conv_descriptor_dtype_set (block, desc,
    9010              :                                  gfc_conv_descriptor_dtype_get (se->expr));
    9011           78 :   rank = fold_convert (size_type_node, gfc_conv_descriptor_rank_get (se->expr));
    9012           78 :   dim_field = gfc_get_descriptor_dimension (se->expr);
    9013           78 :   dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dim_field)));
    9014           78 :   copy_size = fold_build2_loc (input_location, MULT_EXPR,
    9015              :                                size_type_node, rank, dim_size);
    9016           78 :   dst_ptr = gfc_build_addr_expr (pvoid_type_node,
    9017              :                                  gfc_get_descriptor_dimension (desc));
    9018           78 :   src_ptr = gfc_build_addr_expr (pvoid_type_node, dim_field);
    9019           78 :   gfc_add_expr_to_block (block, build_call_expr_loc (input_location,
    9020              :                          builtin_decl_explicit (BUILT_IN_MEMCPY),
    9021              :                          3, dst_ptr, src_ptr, copy_size));
    9022           78 : }
    9023              : 
    9024              : /* Convert an array for passing as an actual parameter.  */
    9025              : 
    9026              : void
    9027        66938 : gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
    9028              :                           const gfc_symbol *fsym, const char *proc_name,
    9029              :                           tree *size, tree *lbshift, tree *packed)
    9030              : {
    9031        66938 :   tree ptr;
    9032        66938 :   tree desc;
    9033        66938 :   tree tmp = NULL_TREE;
    9034        66938 :   tree stmt;
    9035        66938 :   tree parent = DECL_CONTEXT (current_function_decl);
    9036        66938 :   tree ctree;
    9037        66938 :   tree pack_attr = NULL_TREE; /* Set when packing class arrays.  */
    9038        66938 :   bool full_array_var;
    9039        66938 :   bool this_array_result;
    9040        66938 :   bool contiguous;
    9041        66938 :   bool no_pack;
    9042        66938 :   bool array_constructor;
    9043        66938 :   bool good_allocatable;
    9044        66938 :   bool ultimate_ptr_comp;
    9045        66938 :   bool ultimate_alloc_comp;
    9046        66938 :   bool readonly;
    9047        66938 :   gfc_symbol *sym;
    9048        66938 :   stmtblock_t block;
    9049        66938 :   gfc_ref *ref;
    9050              : 
    9051        66938 :   ultimate_ptr_comp = false;
    9052        66938 :   ultimate_alloc_comp = false;
    9053              : 
    9054        67869 :   for (ref = expr->ref; ref; ref = ref->next)
    9055              :     {
    9056        56241 :       if (ref->next == NULL)
    9057              :         break;
    9058              : 
    9059          931 :       if (ref->type == REF_COMPONENT)
    9060              :         {
    9061          739 :           ultimate_ptr_comp = ref->u.c.component->attr.pointer;
    9062          739 :           ultimate_alloc_comp = ref->u.c.component->attr.allocatable;
    9063              :         }
    9064              :     }
    9065              : 
    9066        66938 :   full_array_var = false;
    9067        66938 :   contiguous = false;
    9068              : 
    9069        66938 :   if (expr->expr_type == EXPR_VARIABLE && ref && !ultimate_ptr_comp)
    9070        55179 :     full_array_var = gfc_full_array_ref_p (ref, &contiguous);
    9071              : 
    9072        55179 :   sym = full_array_var ? expr->symtree->n.sym : NULL;
    9073              : 
    9074              :   /* The symbol should have an array specification.  */
    9075        63792 :   gcc_assert (!sym || sym->as || ref->u.ar.as);
    9076              : 
    9077        66938 :   if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
    9078              :     {
    9079          708 :       if (expr->ts.u.cl->length_from_typespec && expr->ts.u.cl->length)
    9080              :         {
    9081              :           /* The constructor has an explicit character type-spec length
    9082              :              so convert it directly.  */
    9083          126 :           gfc_se cse;
    9084          126 :           gfc_init_se (&cse, NULL);
    9085          126 :           gfc_conv_expr_type (&cse, expr->ts.u.cl->length,
    9086              :                               gfc_charlen_type_node);
    9087          126 :           gfc_add_block_to_block (&se->pre, &cse.pre);
    9088          126 :           tmp = cse.expr;
    9089          126 :         }
    9090              :       else
    9091          582 :         get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
    9092              : 
    9093          708 :       expr->ts.u.cl->backend_decl = tmp;
    9094          708 :       se->string_length = tmp;
    9095              :     }
    9096              : 
    9097              :   /* Is this the result of the enclosing procedure?  */
    9098        66938 :   this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
    9099           58 :   if (this_array_result
    9100           58 :         && (sym->backend_decl != current_function_decl)
    9101            0 :         && (sym->backend_decl != parent))
    9102        66938 :     this_array_result = false;
    9103              : 
    9104              :   /* Passing an optional dummy argument as actual to an optional dummy?  */
    9105        66938 :   bool pass_optional;
    9106        66938 :   pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
    9107              : 
    9108              :   /* Passing address of the array if it is not pointer or assumed-shape.  */
    9109        66938 :   if (full_array_var && g77 && !this_array_result
    9110        16274 :       && sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    9111              :     {
    9112        12613 :       tmp = gfc_get_symbol_decl (sym);
    9113              : 
    9114        12613 :       if (sym->ts.type == BT_CHARACTER)
    9115         2821 :         se->string_length = sym->ts.u.cl->backend_decl;
    9116              : 
    9117        12613 :       if (!sym->attr.pointer
    9118        12122 :           && sym->as
    9119        12122 :           && sym->as->type != AS_ASSUMED_SHAPE
    9120        11871 :           && sym->as->type != AS_DEFERRED
    9121        10375 :           && sym->as->type != AS_ASSUMED_RANK
    9122        10299 :           && !sym->attr.allocatable)
    9123              :         {
    9124              :           /* Some variables are declared directly, others are declared as
    9125              :              pointers and allocated on the heap.  */
    9126         9793 :           if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
    9127         2518 :             se->expr = tmp;
    9128              :           else
    9129         7275 :             se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    9130         9793 :           if (size)
    9131           40 :             array_parameter_size (&se->pre, tmp, expr, size);
    9132        17169 :           return;
    9133              :         }
    9134              : 
    9135         2820 :       if (sym->attr.allocatable)
    9136              :         {
    9137         1882 :           if (sym->attr.dummy || sym->attr.result)
    9138              :             {
    9139         1176 :               gfc_conv_expr_descriptor (se, expr);
    9140         1176 :               tmp = se->expr;
    9141              :             }
    9142         1882 :           if (size)
    9143           14 :             array_parameter_size (&se->pre, tmp, expr, size);
    9144         1882 :           se->expr = gfc_conv_array_data (tmp);
    9145         1882 :           if (pass_optional)
    9146              :             {
    9147           18 :               tree cond = gfc_conv_expr_present (sym);
    9148           36 :               se->expr = build3_loc (input_location, COND_EXPR,
    9149           18 :                                      TREE_TYPE (se->expr), cond, se->expr,
    9150           18 :                                      fold_convert (TREE_TYPE (se->expr),
    9151              :                                                    null_pointer_node));
    9152              :             }
    9153              :           return;
    9154              :         }
    9155              :     }
    9156              : 
    9157              :   /* A convenient reduction in scope.  */
    9158        55263 :   contiguous = g77 && !this_array_result && contiguous;
    9159              : 
    9160              :   /* There is no need to pack and unpack the array, if it is contiguous
    9161              :      and not a deferred- or assumed-shape array, or if it is simply
    9162              :      contiguous.  */
    9163        55263 :   no_pack = false;
    9164              :   // clang-format off
    9165        55263 :   if (sym)
    9166              :     {
    9167        40487 :       symbol_attribute *attr = &(IS_CLASS_ARRAY (sym)
    9168              :                                  ? CLASS_DATA (sym)->attr : sym->attr);
    9169        40487 :       gfc_array_spec *as = IS_CLASS_ARRAY (sym)
    9170        40487 :                            ? CLASS_DATA (sym)->as : sym->as;
    9171        40487 :       no_pack = (as
    9172        40197 :                  && !attr->pointer
    9173        36907 :                  && as->type != AS_DEFERRED
    9174        27178 :                  && as->type != AS_ASSUMED_RANK
    9175        64439 :                  && as->type != AS_ASSUMED_SHAPE);
    9176              :     }
    9177        55263 :   if (ref && ref->u.ar.as)
    9178        43525 :     no_pack = no_pack
    9179        43525 :               || (ref->u.ar.as->type != AS_DEFERRED
    9180              :                   && ref->u.ar.as->type != AS_ASSUMED_RANK
    9181              :                   && ref->u.ar.as->type != AS_ASSUMED_SHAPE);
    9182       110526 :   no_pack = contiguous
    9183        55263 :             && (no_pack || gfc_is_simply_contiguous (expr, false, true));
    9184              :   // clang-format on
    9185              : 
    9186              :   /* If we have an EXPR_OP or a function returning an explicit-shaped
    9187              :      or allocatable array, an array temporary will be generated which
    9188              :      does not need to be packed / unpacked if passed to an
    9189              :      explicit-shape dummy array.  */
    9190              : 
    9191        55263 :   if (g77)
    9192              :     {
    9193         6545 :       if (expr->expr_type == EXPR_OP)
    9194              :         no_pack = 1;
    9195         6468 :       else if (expr->expr_type == EXPR_FUNCTION && expr->value.function.esym)
    9196              :         {
    9197           41 :           gfc_symbol *result = expr->value.function.esym->result;
    9198           41 :           if (result->attr.dimension
    9199           41 :               && (result->as->type == AS_EXPLICIT
    9200           14 :                   || result->attr.allocatable
    9201            7 :                   || result->attr.contiguous))
    9202        55263 :             no_pack = 1;
    9203              :         }
    9204              :     }
    9205              : 
    9206              :   /* Array constructors are always contiguous and do not need packing.  */
    9207        55263 :   array_constructor = g77 && !this_array_result && expr->expr_type == EXPR_ARRAY;
    9208              : 
    9209              :   /* Same is true of contiguous sections from allocatable variables.  */
    9210       110526 :   good_allocatable = contiguous
    9211         4736 :                        && expr->symtree
    9212        59999 :                        && expr->symtree->n.sym->attr.allocatable;
    9213              : 
    9214              :   /* Or ultimate allocatable components.  */
    9215        55263 :   ultimate_alloc_comp = contiguous && ultimate_alloc_comp;
    9216              : 
    9217        55263 :   if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp)
    9218              :     {
    9219         5107 :       gfc_conv_expr_descriptor (se, expr);
    9220              :       /* Deallocate the allocatable components of structures that are
    9221              :          not variable.  */
    9222         5107 :       if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    9223         3564 :            && expr->ts.u.derived->attr.alloc_comp
    9224         2143 :            && expr->expr_type != EXPR_VARIABLE)
    9225              :         {
    9226            2 :           tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank);
    9227              : 
    9228              :           /* The components shall be deallocated before their containing entity.  */
    9229            2 :           gfc_prepend_expr_to_block (&se->post, tmp);
    9230              :         }
    9231         5107 :       if (expr->ts.type == BT_CHARACTER && expr->expr_type != EXPR_FUNCTION)
    9232          309 :         se->string_length = expr->ts.u.cl->backend_decl;
    9233         5107 :       if (size)
    9234           58 :         array_parameter_size (&se->pre, se->expr, expr, size);
    9235         5107 :       se->expr = gfc_conv_array_data (se->expr);
    9236         5107 :       return;
    9237              :     }
    9238              : 
    9239        50156 :   if (fsym && fsym->ts.type == BT_CLASS)
    9240              :     {
    9241         1260 :       gcc_assert (se->expr);
    9242              :       ctree = se->expr;
    9243              :     }
    9244              :   else
    9245              :     ctree = NULL_TREE;
    9246              : 
    9247        50156 :   if (this_array_result)
    9248              :     {
    9249              :       /* Result of the enclosing function.  */
    9250           58 :       gfc_conv_expr_descriptor (se, expr);
    9251           58 :       if (size)
    9252            0 :         array_parameter_size (&se->pre, se->expr, expr, size);
    9253           58 :       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    9254              : 
    9255           18 :       if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
    9256           76 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
    9257           18 :         se->expr = gfc_conv_array_data (build_fold_indirect_ref_loc (input_location,
    9258              :                                                                  se->expr));
    9259              : 
    9260              :       return;
    9261              :     }
    9262              :   else
    9263              :     {
    9264              :       /* Every other type of array.  */
    9265        50098 :       se->want_pointer = (ctree) ? 0 : 1;
    9266        50098 :       se->want_coarray = expr->corank;
    9267        50098 :       gfc_conv_expr_descriptor (se, expr);
    9268              : 
    9269        50098 :       if (size)
    9270           30 :         array_parameter_size (&se->pre,
    9271              :                               build_fold_indirect_ref_loc (input_location,
    9272              :                                                             se->expr),
    9273              :                               expr, size);
    9274        50098 :       if (ctree)
    9275              :         {
    9276         1260 :           stmtblock_t block;
    9277              : 
    9278         1260 :           gfc_init_block (&block);
    9279         1260 :           if (lbshift && *lbshift)
    9280              :             {
    9281              :               /* Apply a shift of the lbound when supplied.  */
    9282           98 :               for (int dim = 0; dim < expr->rank; ++dim)
    9283           49 :                 gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
    9284              :                                                   *lbshift);
    9285              :             }
    9286         1260 :           tmp = gfc_class_data_get (ctree);
    9287         1260 :           if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
    9288           84 :               && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
    9289              :             {
    9290           36 :               tree arr = gfc_create_var (TREE_TYPE (tmp), "parm");
    9291           36 :               gfc_conv_descriptor_data_set (&block, arr,
    9292              :                                             gfc_conv_descriptor_data_get (
    9293              :                                               se->expr));
    9294           36 :               gfc_conv_descriptor_lbound_set (&block, arr, gfc_index_zero_node,
    9295              :                                               gfc_index_zero_node);
    9296           36 :               gfc_conv_descriptor_ubound_set (
    9297              :                 &block, arr, gfc_index_zero_node,
    9298              :                 gfc_conv_descriptor_size (se->expr, expr->rank));
    9299           36 :               gfc_conv_descriptor_stride_set (
    9300              :                 &block, arr, gfc_index_zero_node,
    9301              :                 gfc_conv_descriptor_stride_get (se->expr, gfc_index_zero_node));
    9302           36 :               tree dtype_val = gfc_conv_descriptor_dtype_get (se->expr);
    9303           36 :               gfc_conv_descriptor_dtype_set (&block, arr, dtype_val);
    9304           36 :               gfc_conv_descriptor_rank_set (&block, arr, 1);
    9305           36 :               gfc_conv_descriptor_span_set (&block, arr,
    9306              :                                             gfc_conv_descriptor_span_get (arr));
    9307           36 :               gfc_conv_descriptor_offset_set (&block, arr, gfc_index_zero_node);
    9308           36 :               se->expr = arr;
    9309              :             }
    9310         1260 :           if (expr->rank == -1)
    9311           78 :             gfc_resize_assumed_rank_dim_field (se, &block, tmp);
    9312         1182 :           else if (CLASS_DATA (fsym)->as->rank == -1)
    9313          397 :             gfc_class_array_data_assign (&block, tmp, se->expr, false);
    9314              :           else
    9315          785 :             gfc_class_array_data_assign (&block, tmp, se->expr, true);
    9316              : 
    9317              :           /* Handle optional.  */
    9318         1260 :           if (fsym && fsym->attr.optional && sym && sym->attr.optional)
    9319          348 :             tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
    9320              :                             gfc_finish_block (&block),
    9321              :                             build_empty_stmt (input_location));
    9322              :           else
    9323          912 :             tmp = gfc_finish_block (&block);
    9324              : 
    9325         1260 :           gfc_add_expr_to_block (&se->pre, tmp);
    9326              :         }
    9327        48838 :       else if (pass_optional && full_array_var && sym->as && sym->as->rank != 0)
    9328              :         {
    9329              :           /* Perform calculation of bounds and strides of optional array dummy
    9330              :              only if the argument is present.  */
    9331          219 :           tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
    9332              :                           gfc_finish_block (&se->pre),
    9333              :                           build_empty_stmt (input_location));
    9334          219 :           gfc_add_expr_to_block (&se->pre, tmp);
    9335              :         }
    9336              :     }
    9337              : 
    9338              :   /* Deallocate the allocatable components of structures that are
    9339              :      not variable, for descriptorless arguments.
    9340              :      Arguments with a descriptor are handled in gfc_conv_procedure_call.  */
    9341        50098 :   if (g77 && (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS)
    9342           78 :           && expr->ts.u.derived->attr.alloc_comp
    9343           18 :           && expr->expr_type != EXPR_VARIABLE)
    9344              :     {
    9345            0 :       tmp = build_fold_indirect_ref_loc (input_location, se->expr);
    9346            0 :       tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank);
    9347              : 
    9348              :       /* The components shall be deallocated before their containing entity.  */
    9349            0 :       gfc_prepend_expr_to_block (&se->post, tmp);
    9350              :     }
    9351              : 
    9352        48678 :   if (g77 || (fsym && fsym->attr.contiguous
    9353         1585 :               && !gfc_is_simply_contiguous (expr, false, true)))
    9354              :     {
    9355         1600 :       tree origptr = NULL_TREE, packedptr = NULL_TREE;
    9356              : 
    9357         1600 :       desc = se->expr;
    9358              : 
    9359              :       /* For contiguous arrays, save the original value of the descriptor.  */
    9360         1600 :       if (!g77 && !ctree)
    9361              :         {
    9362           84 :           origptr = gfc_create_var (pvoid_type_node, "origptr");
    9363           84 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9364           84 :           tmp = gfc_conv_array_data (tmp);
    9365          168 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    9366           84 :                                  TREE_TYPE (origptr), origptr,
    9367           84 :                                  fold_convert (TREE_TYPE (origptr), tmp));
    9368           84 :           gfc_add_expr_to_block (&se->pre, tmp);
    9369              :         }
    9370              : 
    9371              :       /* Repack the array.  */
    9372         1600 :       if (warn_array_temporaries)
    9373              :         {
    9374           28 :           if (fsym)
    9375           18 :             gfc_warning (OPT_Warray_temporaries,
    9376              :                          "Creating array temporary at %L for argument %qs",
    9377           18 :                          &expr->where, fsym->name);
    9378              :           else
    9379           10 :             gfc_warning (OPT_Warray_temporaries,
    9380              :                          "Creating array temporary at %L", &expr->where);
    9381              :         }
    9382              : 
    9383              :       /* When optimizing, we can use gfc_conv_subref_array_arg for
    9384              :          making the packing and unpacking operation visible to the
    9385              :          optimizers.  */
    9386              : 
    9387         1420 :       if (g77 && flag_inline_arg_packing && expr->expr_type == EXPR_VARIABLE
    9388          720 :           && !is_pointer (expr) && ! gfc_has_dimen_vector_ref (expr)
    9389          350 :           && !(expr->symtree->n.sym->as
    9390          332 :                && expr->symtree->n.sym->as->type == AS_ASSUMED_RANK)
    9391         1950 :           && (fsym == NULL || fsym->ts.type != BT_ASSUMED))
    9392              :         {
    9393          329 :           gfc_conv_subref_array_arg (se, expr, g77,
    9394          153 :                                      fsym ? fsym->attr.intent : INTENT_INOUT,
    9395              :                                      false, fsym, proc_name, sym, true);
    9396          329 :           return;
    9397              :         }
    9398              : 
    9399         1271 :       if (ctree)
    9400              :         {
    9401           96 :           packedptr
    9402           96 :             = gfc_build_addr_expr (NULL_TREE, gfc_create_var (TREE_TYPE (ctree),
    9403              :                                                               "packed"));
    9404           96 :           if (fsym)
    9405              :             {
    9406           96 :               int pack_mask = 0;
    9407              : 
    9408              :               /* Set bit 0 to the mask, when this is an unlimited_poly
    9409              :                  class.  */
    9410           96 :               if (CLASS_DATA (fsym)->ts.u.derived->attr.unlimited_polymorphic)
    9411           36 :                 pack_mask = 1 << 0;
    9412           96 :               pack_attr = build_int_cst (integer_type_node, pack_mask);
    9413              :             }
    9414              :           else
    9415            0 :             pack_attr = integer_zero_node;
    9416              : 
    9417           96 :           gfc_add_expr_to_block (
    9418              :             &se->pre,
    9419              :             build_call_expr_loc (input_location, gfor_fndecl_in_pack_class, 4,
    9420              :                                  packedptr,
    9421              :                                  gfc_build_addr_expr (NULL_TREE, ctree),
    9422           96 :                                  size_in_bytes (TREE_TYPE (ctree)), pack_attr));
    9423           96 :           ptr = gfc_conv_array_data (gfc_class_data_get (packedptr));
    9424           96 :           se->expr = packedptr;
    9425           96 :           if (packed)
    9426           96 :             *packed = packedptr;
    9427              :         }
    9428              :       else
    9429              :         {
    9430         1175 :           ptr = build_call_expr_loc (input_location, gfor_fndecl_in_pack, 1,
    9431              :                                      desc);
    9432              : 
    9433         1175 :           if (fsym && fsym->attr.optional && sym && sym->attr.optional)
    9434              :             {
    9435           11 :               tmp = gfc_conv_expr_present (sym);
    9436           22 :               ptr = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
    9437           11 :                                 tmp, fold_convert (TREE_TYPE (se->expr), ptr),
    9438           11 :                                 fold_convert (TREE_TYPE (se->expr),
    9439              :                                               null_pointer_node));
    9440              :             }
    9441              : 
    9442         1175 :           ptr = gfc_evaluate_now (ptr, &se->pre);
    9443              :         }
    9444              : 
    9445              :       /* Use the packed data for the actual argument, except for contiguous arrays,
    9446              :          where the descriptor's data component is set.  */
    9447         1271 :       if (g77)
    9448         1091 :         se->expr = ptr;
    9449              :       else
    9450              :         {
    9451          180 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9452              : 
    9453          180 :           if (!ctree)
    9454              :             {
    9455              :               /* The original descriptor may have transposed dims so we
    9456              :                  can't reuse it directly; we have to create a new one.  */
    9457           84 :               tree old_field;
    9458           84 :               tree old_desc = tmp;
    9459           84 :               tree new_desc = gfc_create_var (TREE_TYPE (old_desc), "arg_desc");
    9460              : 
    9461           84 :               old_field = gfc_conv_descriptor_dtype_get (old_desc);
    9462           84 :               gfc_conv_descriptor_dtype_set (&se->pre, new_desc, old_field);
    9463              : 
    9464           84 :               if (expr->rank == -1)
    9465              :                 {
    9466           12 :                   tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    9467           12 :                   tree stride = gfc_create_var (gfc_array_index_type, "stride");
    9468           12 :                   stmtblock_t loop_body;
    9469              : 
    9470           12 :                   gfc_conv_descriptor_offset_set (&se->pre, new_desc,
    9471              :                                                   gfc_index_zero_node);
    9472           12 :                   gfc_conv_descriptor_span_set (&se->pre, new_desc,
    9473              :                                                 gfc_conv_descriptor_span_get
    9474              :                                                 (old_desc));
    9475           12 :                   gfc_add_modify (&se->pre, stride, gfc_index_one_node);
    9476              : 
    9477           12 :                   gfc_init_block (&loop_body);
    9478              : 
    9479           12 :                   old_field = gfc_conv_descriptor_lbound_get (old_desc, idx);
    9480           12 :                   gfc_conv_descriptor_lbound_set (&loop_body, new_desc, idx,
    9481              :                                                   old_field);
    9482              : 
    9483           12 :                   old_field = gfc_conv_descriptor_ubound_get (old_desc, idx);
    9484           12 :                   gfc_conv_descriptor_ubound_set (&loop_body, new_desc, idx,
    9485              :                                                   old_field);
    9486              : 
    9487           12 :                   gfc_conv_descriptor_stride_set (&loop_body, new_desc, idx,
    9488              :                                                   stride);
    9489              : 
    9490           12 :                   tree offset = fold_build2_loc (input_location, MULT_EXPR,
    9491              :                                                  gfc_array_index_type, stride,
    9492              :                                                  gfc_conv_descriptor_lbound_get
    9493              :                                                  (new_desc, idx));
    9494           12 :                   offset = fold_build2_loc (input_location, MINUS_EXPR,
    9495              :                                             gfc_array_index_type,
    9496              :                                             gfc_conv_descriptor_offset_get
    9497              :                                             (new_desc), offset);
    9498           12 :                   gfc_conv_descriptor_offset_set (&loop_body, new_desc, offset);
    9499              : 
    9500           12 :                   tree extent = gfc_conv_array_extent_dim
    9501           12 :                                 (gfc_conv_descriptor_lbound_get (new_desc, idx),
    9502              :                                  gfc_conv_descriptor_ubound_get (new_desc, idx),
    9503              :                                  NULL);
    9504           12 :                   extent = fold_build2_loc (input_location, MULT_EXPR,
    9505              :                                             gfc_array_index_type, stride,
    9506              :                                             extent);
    9507           12 :                   gfc_add_modify (&loop_body, stride, extent);
    9508              : 
    9509           36 :                   gfc_simple_for_loop (&se->pre, idx,
    9510           12 :                                        build_int_cst (TREE_TYPE (idx), 0),
    9511              :                                        gfc_conv_descriptor_rank_get (old_desc),
    9512              :                                        LT_EXPR,
    9513           12 :                                        build_int_cst (TREE_TYPE (idx), 1),
    9514              :                                        gfc_finish_block (&loop_body));
    9515              :                 }
    9516              :               else
    9517              :                 {
    9518           72 :                   tree offset = gfc_index_zero_node;
    9519              : 
    9520           72 :                   tree stride = gfc_index_one_node;
    9521              : 
    9522          102 :                   for (int i = 0; i < expr->rank; i++)
    9523              :                     {
    9524          102 :                       tree dim = gfc_rank_cst[i];
    9525              : 
    9526          102 :                       tree lbound = gfc_conv_descriptor_lbound_get (old_desc,
    9527              :                                                                     dim);
    9528          102 :                       lbound = gfc_evaluate_now (lbound, &se->pre);
    9529          102 :                       gfc_conv_descriptor_lbound_set (&se->pre, new_desc, dim,
    9530              :                                                       lbound);
    9531              : 
    9532          102 :                       tree ubound = gfc_conv_descriptor_ubound_get (old_desc,
    9533              :                                                                     dim);
    9534          102 :                       ubound = gfc_evaluate_now (ubound, &se->pre);
    9535          102 :                       gfc_conv_descriptor_ubound_set (&se->pre, new_desc, dim,
    9536              :                                                       ubound);
    9537              : 
    9538          102 :                       gfc_conv_descriptor_stride_set (&se->pre, new_desc, dim,
    9539              :                                                       stride);
    9540              : 
    9541          102 :                       tree tmp = fold_build2_loc (input_location, MULT_EXPR,
    9542              :                                                   gfc_array_index_type,
    9543              :                                                   stride, lbound);
    9544          102 :                       offset = fold_build2_loc (input_location, MINUS_EXPR,
    9545              :                                                 gfc_array_index_type,
    9546              :                                                 offset, tmp);
    9547          102 :                       offset = gfc_evaluate_now (offset, &se->pre);
    9548              : 
    9549              :                       /* Now calculate the stride for next dimension, unless the
    9550              :                          current dimension is the last one.  */
    9551          102 :                       if (i == expr->rank - 1)
    9552              :                         break;
    9553              : 
    9554           30 :                       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9555              :                                              gfc_array_index_type,
    9556              :                                              lbound, gfc_index_one_node);
    9557           30 :                       tree extent = fold_build2_loc (input_location, MINUS_EXPR,
    9558              :                                                      gfc_array_index_type,
    9559              :                                                      ubound, tmp);
    9560           30 :                       stride = fold_build2_loc (input_location, MULT_EXPR,
    9561              :                                                 gfc_array_index_type,
    9562              :                                                 stride, extent);
    9563           30 :                       stride = gfc_evaluate_now (stride, &se->pre);
    9564              :                     }
    9565              : 
    9566           72 :                   gfc_conv_descriptor_offset_set (&se->pre, new_desc, offset);
    9567              :                 }
    9568              : 
    9569           84 :               if (flag_coarray == GFC_FCOARRAY_LIB
    9570            0 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (old_desc))
    9571           84 :                   && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (old_desc))
    9572              :                      == GFC_ARRAY_ALLOCATABLE)
    9573              :                 {
    9574            0 :                   old_field = gfc_conv_descriptor_token (old_desc);
    9575            0 :                   gfc_conv_descriptor_token_set (&se->pre, new_desc,
    9576              :                                                  old_field);
    9577              :                 }
    9578              : 
    9579           84 :               gfc_conv_descriptor_data_set (&se->pre, new_desc, ptr);
    9580           84 :               se->expr = gfc_build_addr_expr (NULL_TREE, new_desc);
    9581              :             }
    9582              :         }
    9583              : 
    9584         1271 :       if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
    9585              :         {
    9586            8 :           char * msg;
    9587              : 
    9588            8 :           if (fsym && proc_name)
    9589            8 :             msg = xasprintf ("An array temporary was created for argument "
    9590            8 :                              "'%s' of procedure '%s'", fsym->name, proc_name);
    9591              :           else
    9592            0 :             msg = xasprintf ("An array temporary was created");
    9593              : 
    9594            8 :           tmp = build_fold_indirect_ref_loc (input_location,
    9595              :                                          desc);
    9596            8 :           tmp = gfc_conv_array_data (tmp);
    9597            8 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9598            8 :                                  fold_convert (TREE_TYPE (tmp), ptr), tmp);
    9599              : 
    9600            8 :           if (pass_optional)
    9601            6 :             tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9602              :                                    logical_type_node,
    9603              :                                    gfc_conv_expr_present (sym), tmp);
    9604              : 
    9605            8 :           gfc_trans_runtime_check (false, true, tmp, &se->pre,
    9606              :                                    &expr->where, msg);
    9607            8 :           free (msg);
    9608              :         }
    9609              : 
    9610         1271 :       gfc_start_block (&block);
    9611              : 
    9612              :       /* Copy the data back.  If input expr is read-only, e.g. a PARAMETER
    9613              :          array, copying back modified values is undefined behavior.  */
    9614         2542 :       readonly = (expr->expr_type == EXPR_VARIABLE
    9615          860 :                   && expr->symtree
    9616         2131 :                   && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
    9617              : 
    9618         1271 :       if ((fsym == NULL || fsym->attr.intent != INTENT_IN) && !readonly)
    9619              :         {
    9620         1114 :           if (ctree)
    9621              :             {
    9622           66 :               tmp = gfc_build_addr_expr (NULL_TREE, ctree);
    9623           66 :               tmp = build_call_expr_loc (input_location,
    9624              :                                          gfor_fndecl_in_unpack_class, 4, tmp,
    9625              :                                          packedptr,
    9626           66 :                                          size_in_bytes (TREE_TYPE (ctree)),
    9627              :                                          pack_attr);
    9628              :             }
    9629              :           else
    9630         1048 :             tmp = build_call_expr_loc (input_location, gfor_fndecl_in_unpack, 2,
    9631              :                                        desc, ptr);
    9632         1114 :           gfc_add_expr_to_block (&block, tmp);
    9633              :         }
    9634          157 :       else if (ctree && fsym->attr.intent == INTENT_IN)
    9635              :         {
    9636              :           /* Need to free the memory for class arrays, that got packed.  */
    9637           30 :           gfc_add_expr_to_block (&block, gfc_call_free (ptr));
    9638              :         }
    9639              : 
    9640              :       /* Free the temporary.  */
    9641         1144 :       if (!ctree)
    9642         1175 :         gfc_add_expr_to_block (&block, gfc_call_free (ptr));
    9643              : 
    9644         1271 :       stmt = gfc_finish_block (&block);
    9645              : 
    9646         1271 :       gfc_init_block (&block);
    9647              :       /* Only if it was repacked.  This code needs to be executed before the
    9648              :          loop cleanup code.  */
    9649         1271 :       tmp = (ctree) ? desc : build_fold_indirect_ref_loc (input_location, desc);
    9650         1271 :       tmp = gfc_conv_array_data (tmp);
    9651         1271 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9652         1271 :                              fold_convert (TREE_TYPE (tmp), ptr), tmp);
    9653              : 
    9654         1271 :       if (pass_optional)
    9655           11 :         tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    9656              :                                logical_type_node,
    9657              :                                gfc_conv_expr_present (sym), tmp);
    9658              : 
    9659         1271 :       tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt (input_location));
    9660              : 
    9661         1271 :       gfc_add_expr_to_block (&block, tmp);
    9662         1271 :       gfc_add_block_to_block (&block, &se->post);
    9663              : 
    9664         1271 :       gfc_init_block (&se->post);
    9665              : 
    9666              :       /* Reset the descriptor pointer.  */
    9667         1271 :       if (!g77 && !ctree)
    9668              :         {
    9669           84 :           tmp = build_fold_indirect_ref_loc (input_location, desc);
    9670           84 :           gfc_conv_descriptor_data_set (&se->post, tmp, origptr);
    9671              :         }
    9672              : 
    9673         1271 :       gfc_add_block_to_block (&se->post, &block);
    9674              :     }
    9675              : }
    9676              : 
    9677              : 
    9678              : /* This helper function calculates the size in words of a full array.  */
    9679              : 
    9680              : tree
    9681        21439 : gfc_full_array_size (stmtblock_t *block, tree decl, int rank)
    9682              : {
    9683        21439 :   tree idx;
    9684        21439 :   tree nelems;
    9685        21439 :   tree tmp;
    9686        21439 :   if (rank < 0)
    9687            0 :     idx = gfc_conv_descriptor_rank_get (decl);
    9688              :   else
    9689        21439 :     idx = gfc_rank_cst[rank - 1];
    9690        21439 :   nelems = gfc_conv_descriptor_ubound_get (decl, idx);
    9691        21439 :   tmp = gfc_conv_descriptor_lbound_get (decl, idx);
    9692        21439 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9693              :                          nelems, tmp);
    9694        21439 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9695              :                          tmp, gfc_index_one_node);
    9696        21439 :   tmp = gfc_evaluate_now (tmp, block);
    9697              : 
    9698        21439 :   nelems = gfc_conv_descriptor_stride_get (decl, idx);
    9699        21439 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9700              :                          nelems, tmp);
    9701        21439 :   return gfc_evaluate_now (tmp, block);
    9702              : }
    9703              : 
    9704              : 
    9705              : /* Allocate dest to the same size as src, and copy src -> dest.
    9706              :    If no_malloc is set, only the copy is done.  */
    9707              : 
    9708              : static tree
    9709        10362 : duplicate_allocatable (tree dest, tree src, tree type, int rank,
    9710              :                        bool no_malloc, bool no_memcpy, tree str_sz,
    9711              :                        tree add_when_allocated)
    9712              : {
    9713        10362 :   tree tmp;
    9714        10362 :   tree eltype;
    9715        10362 :   tree size;
    9716        10362 :   tree nelems;
    9717        10362 :   tree null_cond;
    9718        10362 :   tree null_data;
    9719        10362 :   stmtblock_t block;
    9720              : 
    9721              :   /* If the source is null, set the destination to null.  Then,
    9722              :      allocate memory to the destination.  */
    9723        10362 :   gfc_init_block (&block);
    9724              : 
    9725        10362 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
    9726              :     {
    9727         2513 :       gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
    9728         2513 :       null_data = gfc_finish_block (&block);
    9729              : 
    9730         2513 :       gfc_init_block (&block);
    9731         2513 :       eltype = TREE_TYPE (type);
    9732         2513 :       if (str_sz != NULL_TREE)
    9733              :         size = str_sz;
    9734              :       else
    9735         2133 :         size = TYPE_SIZE_UNIT (eltype);
    9736              : 
    9737         2513 :       if (!no_malloc)
    9738              :         {
    9739         2513 :           tmp = gfc_call_malloc (&block, type, size);
    9740         2513 :           gfc_add_modify (&block, dest, fold_convert (type, tmp));
    9741              :         }
    9742              : 
    9743         2513 :       if (!no_memcpy)
    9744              :         {
    9745         1730 :           tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9746         1730 :           tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
    9747              :                                      fold_convert (size_type_node, size));
    9748         1730 :           gfc_add_expr_to_block (&block, tmp);
    9749              :         }
    9750              :     }
    9751              :   else
    9752              :     {
    9753         7849 :       gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9754         7849 :       null_data = gfc_finish_block (&block);
    9755              : 
    9756         7849 :       gfc_init_block (&block);
    9757         7849 :       if (rank)
    9758         7834 :         nelems = gfc_full_array_size (&block, src, rank);
    9759              :       else
    9760           15 :         nelems = gfc_index_one_node;
    9761              : 
    9762              :       /* If type is not the array type, then it is the element type.  */
    9763         7849 :       if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
    9764         7819 :         eltype = gfc_get_element_type (type);
    9765              :       else
    9766              :         eltype = type;
    9767              : 
    9768         7849 :       if (str_sz != NULL_TREE)
    9769           43 :         tmp = fold_convert (gfc_array_index_type, str_sz);
    9770              :       else
    9771         7806 :         tmp = fold_convert (gfc_array_index_type,
    9772              :                             TYPE_SIZE_UNIT (eltype));
    9773              : 
    9774         7849 :       tmp = gfc_evaluate_now (tmp, &block);
    9775         7849 :       size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9776              :                               nelems, tmp);
    9777         7849 :       if (!no_malloc)
    9778              :         {
    9779         7781 :           tmp = TREE_TYPE (gfc_conv_descriptor_data_get (src));
    9780         7781 :           tmp = gfc_call_malloc (&block, tmp, size);
    9781         7781 :           gfc_conv_descriptor_data_set (&block, dest, tmp);
    9782              :         }
    9783              : 
    9784              :       /* We know the temporary and the value will be the same length,
    9785              :          so can use memcpy.  */
    9786         7849 :       if (!no_memcpy)
    9787              :         {
    9788         6488 :           tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9789         6488 :           tmp = build_call_expr_loc (input_location, tmp, 3,
    9790              :                                      gfc_conv_descriptor_data_get (dest),
    9791              :                                      gfc_conv_descriptor_data_get (src),
    9792              :                                      fold_convert (size_type_node, size));
    9793         6488 :           gfc_add_expr_to_block (&block, tmp);
    9794              :         }
    9795              :     }
    9796              : 
    9797        10362 :   gfc_add_expr_to_block (&block, add_when_allocated);
    9798        10362 :   tmp = gfc_finish_block (&block);
    9799              : 
    9800              :   /* Null the destination if the source is null; otherwise do
    9801              :      the allocate and copy.  */
    9802        10362 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
    9803              :     null_cond = src;
    9804              :   else
    9805         7849 :     null_cond = gfc_conv_descriptor_data_get (src);
    9806              : 
    9807        10362 :   null_cond = convert (pvoid_type_node, null_cond);
    9808        10362 :   null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9809              :                                null_cond, null_pointer_node);
    9810        10362 :   return build3_v (COND_EXPR, null_cond, tmp, null_data);
    9811              : }
    9812              : 
    9813              : 
    9814              : /* Allocate dest to the same size as src, and copy data src -> dest.  */
    9815              : 
    9816              : tree
    9817         7551 : gfc_duplicate_allocatable (tree dest, tree src, tree type, int rank,
    9818              :                            tree add_when_allocated)
    9819              : {
    9820         7551 :   return duplicate_allocatable (dest, src, type, rank, false, false,
    9821         7551 :                                 NULL_TREE, add_when_allocated);
    9822              : }
    9823              : 
    9824              : 
    9825              : /* Copy data src -> dest.  */
    9826              : 
    9827              : tree
    9828           68 : gfc_copy_allocatable_data (tree dest, tree src, tree type, int rank)
    9829              : {
    9830           68 :   return duplicate_allocatable (dest, src, type, rank, true, false,
    9831           68 :                                 NULL_TREE, NULL_TREE);
    9832              : }
    9833              : 
    9834              : /* Allocate dest to the same size as src, but don't copy anything.  */
    9835              : 
    9836              : tree
    9837         2144 : gfc_duplicate_allocatable_nocopy (tree dest, tree src, tree type, int rank)
    9838              : {
    9839         2144 :   return duplicate_allocatable (dest, src, type, rank, false, true,
    9840         2144 :                                 NULL_TREE, NULL_TREE);
    9841              : }
    9842              : 
    9843              : static tree
    9844           62 : duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
    9845              :                                int rank, tree add_when_allocated)
    9846              : {
    9847           62 :   tree tmp;
    9848           62 :   tree size;
    9849           62 :   tree nelems;
    9850           62 :   tree null_cond;
    9851           62 :   tree null_data;
    9852           62 :   stmtblock_t block, globalblock;
    9853              : 
    9854              :   /* If the source is null, set the destination to null.  Then,
    9855              :      allocate memory to the destination.  */
    9856           62 :   gfc_init_block (&block);
    9857           62 :   gfc_init_block (&globalblock);
    9858              : 
    9859           62 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
    9860              :     {
    9861           18 :       gfc_se se;
    9862           18 :       symbol_attribute attr;
    9863           18 :       tree dummy_desc;
    9864              : 
    9865           18 :       gfc_init_se (&se, NULL);
    9866           18 :       gfc_clear_attr (&attr);
    9867           18 :       attr.allocatable = 1;
    9868           18 :       dummy_desc = gfc_conv_scalar_to_descriptor (&se, dest, attr);
    9869           18 :       gfc_add_block_to_block (&globalblock, &se.pre);
    9870           18 :       size = TYPE_SIZE_UNIT (TREE_TYPE (type));
    9871              : 
    9872           18 :       gfc_add_modify (&block, dest, fold_convert (type, null_pointer_node));
    9873           18 :       gfc_allocate_using_caf_lib (&block, dummy_desc, size,
    9874              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9875              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9876              :                                   GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
    9877           18 :       gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
    9878           18 :       null_data = gfc_finish_block (&block);
    9879              : 
    9880           18 :       gfc_init_block (&block);
    9881              : 
    9882           18 :       gfc_allocate_using_caf_lib (&block, dummy_desc,
    9883              :                                   fold_convert (size_type_node, size),
    9884              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9885              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9886              :                                   GFC_CAF_COARRAY_ALLOC);
    9887           18 :       gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
    9888              : 
    9889           18 :       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9890           18 :       tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
    9891              :                                  fold_convert (size_type_node, size));
    9892           18 :       gfc_add_expr_to_block (&block, tmp);
    9893              :     }
    9894              :   else
    9895              :     {
    9896              :       /* Set the rank or uninitialized memory access may be reported.  */
    9897           44 :       gfc_conv_descriptor_rank_set (&globalblock, dest, rank);
    9898              : 
    9899           44 :       if (rank)
    9900           44 :         nelems = gfc_full_array_size (&globalblock, src, rank);
    9901              :       else
    9902            0 :         nelems = integer_one_node;
    9903              : 
    9904           44 :       tmp = fold_convert (size_type_node,
    9905              :                           TYPE_SIZE_UNIT (gfc_get_element_type (type)));
    9906           44 :       size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    9907              :                               fold_convert (size_type_node, nelems), tmp);
    9908              : 
    9909           44 :       gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9910           44 :       gfc_allocate_using_caf_lib (&block, dest, fold_convert (size_type_node,
    9911              :                                                               size),
    9912              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9913              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9914              :                                   GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
    9915           44 :       null_data = gfc_finish_block (&block);
    9916              : 
    9917           44 :       gfc_init_block (&block);
    9918           44 :       gfc_allocate_using_caf_lib (&block, dest,
    9919              :                                   fold_convert (size_type_node, size),
    9920              :                                   gfc_build_addr_expr (NULL_TREE, dest_tok),
    9921              :                                   NULL_TREE, NULL_TREE, NULL_TREE,
    9922              :                                   GFC_CAF_COARRAY_ALLOC);
    9923              : 
    9924           44 :       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
    9925           44 :       tmp = build_call_expr_loc (input_location, tmp, 3,
    9926              :                                  gfc_conv_descriptor_data_get (dest),
    9927              :                                  gfc_conv_descriptor_data_get (src),
    9928              :                                  fold_convert (size_type_node, size));
    9929           44 :       gfc_add_expr_to_block (&block, tmp);
    9930              :     }
    9931           62 :   gfc_add_expr_to_block (&block, add_when_allocated);
    9932           62 :   tmp = gfc_finish_block (&block);
    9933              : 
    9934              :   /* Null the destination if the source is null; otherwise do
    9935              :      the register and copy.  */
    9936           62 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (src)))
    9937              :     null_cond = src;
    9938              :   else
    9939           44 :     null_cond = gfc_conv_descriptor_data_get (src);
    9940              : 
    9941           62 :   null_cond = convert (pvoid_type_node, null_cond);
    9942           62 :   null_cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    9943              :                                null_cond, null_pointer_node);
    9944           62 :   gfc_add_expr_to_block (&globalblock, build3_v (COND_EXPR, null_cond, tmp,
    9945              :                                                  null_data));
    9946           62 :   return gfc_finish_block (&globalblock);
    9947              : }
    9948              : 
    9949              : 
    9950              : /* Helper function to abstract whether coarray processing is enabled.  */
    9951              : 
    9952              : static bool
    9953         4257 : caf_enabled (int caf_mode)
    9954              : {
    9955         4257 :   return (caf_mode & GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY)
    9956         4257 :       == GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY;
    9957              : }
    9958              : 
    9959              : 
    9960              : /* Helper function to abstract whether coarray processing is enabled
    9961              :    and we are in a derived type coarray.  */
    9962              : 
    9963              : static bool
    9964        13552 : caf_in_coarray (int caf_mode)
    9965              : {
    9966        13552 :   static const int pat = GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
    9967              :                          | GFC_STRUCTURE_CAF_MODE_IN_COARRAY;
    9968        13552 :   return (caf_mode & pat) == pat;
    9969              : }
    9970              : 
    9971              : 
    9972              : /* Helper function to abstract whether coarray is to deallocate only.  */
    9973              : 
    9974              : bool
    9975          403 : gfc_caf_is_dealloc_only (int caf_mode)
    9976              : {
    9977          403 :   return (caf_mode & GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY)
    9978          403 :       == GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY;
    9979              : }
    9980              : 
    9981              : 
    9982              : /* Recursively traverse an object of derived type, generating code to
    9983              :    deallocate, nullify or copy allocatable components.  This is the work horse
    9984              :    function for the functions named in this enum.  */
    9985              : 
    9986              : enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP,
    9987              :       COPY_ALLOC_COMP, COPY_ONLY_ALLOC_COMP, REASSIGN_CAF_COMP,
    9988              :       ALLOCATE_PDT_COMP, DEALLOCATE_PDT_COMP, CHECK_PDT_DUMMY,
    9989              :       BCAST_ALLOC_COMP};
    9990              : 
    9991              : static gfc_actual_arglist *pdt_param_list;
    9992              : static bool generating_copy_helper;
    9993              : static hash_set<gfc_symbol *> seen_derived_types;
    9994              : 
    9995              : /* Forward declaration of structure_alloc_comps for wrapper generator.  */
    9996              : static tree structure_alloc_comps (gfc_symbol *, tree, tree, int, int, int,
    9997              :                                     gfc_co_subroutines_args *, bool);
    9998              : 
    9999              : /* Generate a wrapper function that performs element-wise deep copy for
   10000              :    recursive allocatable array components. This wrapper is passed as a
   10001              :    function pointer to the runtime helper _gfortran_cfi_deep_copy_array,
   10002              :    allowing recursion to happen at runtime instead of compile time.  */
   10003              : 
   10004              : static tree
   10005          475 : get_copy_helper_function_type (void)
   10006              : {
   10007          475 :   static tree fn_type = NULL_TREE;
   10008          475 :   if (fn_type == NULL_TREE)
   10009           93 :     fn_type = build_function_type_list (void_type_node,
   10010              :                                         pvoid_type_node,
   10011              :                                         pvoid_type_node,
   10012              :                                         NULL_TREE);
   10013          475 :   return fn_type;
   10014              : }
   10015              : 
   10016              : static tree
   10017         1670 : get_copy_helper_pointer_type (void)
   10018              : {
   10019         1670 :   static tree ptr_type = NULL_TREE;
   10020         1670 :   if (ptr_type == NULL_TREE)
   10021           93 :     ptr_type = build_pointer_type (get_copy_helper_function_type ());
   10022         1670 :   return ptr_type;
   10023              : }
   10024              : 
   10025              : static tree
   10026          382 : generate_element_copy_wrapper (gfc_symbol *der_type, tree comp_type,
   10027              :                                 int purpose, int caf_mode)
   10028              : {
   10029          382 :   tree fndecl, fntype, result_decl;
   10030          382 :   tree dest_parm, src_parm, dest_typed, src_typed;
   10031          382 :   tree der_type_ptr;
   10032          382 :   stmtblock_t block;
   10033          382 :   tree decls;
   10034          382 :   tree body;
   10035              : 
   10036          382 :   fntype = get_copy_helper_function_type ();
   10037              : 
   10038          382 :   fndecl = build_decl (input_location, FUNCTION_DECL,
   10039              :                        create_tmp_var_name ("copy_element"),
   10040              :                        fntype);
   10041              : 
   10042          382 :   TREE_STATIC (fndecl) = 1;
   10043          382 :   TREE_USED (fndecl) = 1;
   10044          382 :   DECL_ARTIFICIAL (fndecl) = 1;
   10045          382 :   DECL_IGNORED_P (fndecl) = 0;
   10046          382 :   TREE_PUBLIC (fndecl) = 0;
   10047          382 :   DECL_UNINLINABLE (fndecl) = 1;
   10048          382 :   DECL_EXTERNAL (fndecl) = 0;
   10049          382 :   DECL_CONTEXT (fndecl) = NULL_TREE;
   10050          382 :   DECL_INITIAL (fndecl) = make_node (BLOCK);
   10051          382 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
   10052              : 
   10053          382 :   result_decl = build_decl (input_location, RESULT_DECL, NULL_TREE,
   10054              :                             void_type_node);
   10055          382 :   DECL_ARTIFICIAL (result_decl) = 1;
   10056          382 :   DECL_IGNORED_P (result_decl) = 1;
   10057          382 :   DECL_CONTEXT (result_decl) = fndecl;
   10058          382 :   DECL_RESULT (fndecl) = result_decl;
   10059              : 
   10060          382 :   dest_parm = build_decl (input_location, PARM_DECL,
   10061              :                           get_identifier ("dest"), pvoid_type_node);
   10062          382 :   src_parm = build_decl (input_location, PARM_DECL,
   10063              :                          get_identifier ("src"), pvoid_type_node);
   10064              : 
   10065          382 :   DECL_ARTIFICIAL (dest_parm) = 1;
   10066          382 :   DECL_ARTIFICIAL (src_parm) = 1;
   10067          382 :   DECL_ARG_TYPE (dest_parm) = pvoid_type_node;
   10068          382 :   DECL_ARG_TYPE (src_parm) = pvoid_type_node;
   10069          382 :   DECL_CONTEXT (dest_parm) = fndecl;
   10070          382 :   DECL_CONTEXT (src_parm) = fndecl;
   10071              : 
   10072          382 :   DECL_ARGUMENTS (fndecl) = dest_parm;
   10073          382 :   TREE_CHAIN (dest_parm) = src_parm;
   10074              : 
   10075          382 :   push_struct_function (fndecl);
   10076          382 :   cfun->function_end_locus = input_location;
   10077              : 
   10078          382 :   pushlevel ();
   10079          382 :   gfc_init_block (&block);
   10080              : 
   10081          382 :   bool saved_generating = generating_copy_helper;
   10082          382 :   generating_copy_helper = true;
   10083              : 
   10084              :   /* When generating a wrapper, we need a fresh type tracking state to
   10085              :      avoid inheriting the parent context's seen_derived_types, which would
   10086              :      cause infinite recursion when the wrapper tries to handle the same
   10087              :      recursive type.  Save elements, clear the set, generate wrapper, then
   10088              :      restore elements.  */
   10089          382 :   vec<gfc_symbol *> saved_symbols = vNULL;
   10090          382 :   for (hash_set<gfc_symbol *>::iterator it = seen_derived_types.begin ();
   10091          918 :        it != seen_derived_types.end (); ++it)
   10092          536 :     saved_symbols.safe_push (*it);
   10093          382 :   seen_derived_types.empty ();
   10094              : 
   10095          382 :   der_type_ptr = build_pointer_type (comp_type);
   10096          382 :   dest_typed = fold_convert (der_type_ptr, dest_parm);
   10097          382 :   src_typed = fold_convert (der_type_ptr, src_parm);
   10098              : 
   10099          382 :   dest_typed = build_fold_indirect_ref (dest_typed);
   10100          382 :   src_typed = build_fold_indirect_ref (src_typed);
   10101              : 
   10102          382 :   body = structure_alloc_comps (der_type, src_typed, dest_typed,
   10103              :                                 0, purpose, caf_mode, NULL, false);
   10104          382 :   gfc_add_expr_to_block (&block, body);
   10105              : 
   10106              :   /* Restore saved symbols.  */
   10107          382 :   seen_derived_types.empty ();
   10108          918 :   for (unsigned i = 0; i < saved_symbols.length (); i++)
   10109          536 :     seen_derived_types.add (saved_symbols[i]);
   10110          382 :   saved_symbols.release ();
   10111          382 :   generating_copy_helper = saved_generating;
   10112              : 
   10113          382 :   body = gfc_finish_block (&block);
   10114          382 :   decls = getdecls ();
   10115              : 
   10116          382 :   poplevel (1, 1);
   10117              : 
   10118          764 :   DECL_SAVED_TREE (fndecl)
   10119          382 :     = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR,
   10120          382 :                        void_type_node, decls, body, DECL_INITIAL (fndecl));
   10121              : 
   10122          382 :   pop_cfun ();
   10123              : 
   10124              :   /* Use finalize_function with no_collect=true to skip the ggc_collect
   10125              :      call that add_new_function would trigger.  This function is called
   10126              :      during tree lowering of structure_alloc_comps where caller stack
   10127              :      frames hold locally-computed tree nodes (COMPONENT_REFs etc.) that
   10128              :      are not yet attached to any GC root.  A collection at this point
   10129              :      would free those nodes and cause segfaults.  PR124235.  */
   10130          382 :   cgraph_node::finalize_function (fndecl, true);
   10131              : 
   10132          382 :   return build1 (ADDR_EXPR, get_copy_helper_pointer_type (), fndecl);
   10133              : }
   10134              : 
   10135              : static tree
   10136        26786 : structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
   10137              :                        int rank, int purpose, int caf_mode,
   10138              :                        gfc_co_subroutines_args *args,
   10139              :                        bool no_finalization = false)
   10140              : {
   10141        26786 :   gfc_component *c;
   10142        26786 :   gfc_loopinfo loop;
   10143        26786 :   stmtblock_t fnblock;
   10144        26786 :   stmtblock_t loopbody;
   10145        26786 :   stmtblock_t tmpblock;
   10146        26786 :   tree decl_type;
   10147        26786 :   tree tmp;
   10148        26786 :   tree comp;
   10149        26786 :   tree dcmp;
   10150        26786 :   tree nelems;
   10151        26786 :   tree index;
   10152        26786 :   tree var;
   10153        26786 :   tree cdecl;
   10154        26786 :   tree ctype;
   10155        26786 :   tree vref, dref;
   10156        26786 :   tree null_cond = NULL_TREE;
   10157        26786 :   tree add_when_allocated;
   10158        26786 :   tree dealloc_fndecl;
   10159        26786 :   tree caf_token;
   10160        26786 :   gfc_symbol *vtab;
   10161        26786 :   int caf_dereg_mode;
   10162        26786 :   symbol_attribute *attr;
   10163        26786 :   bool deallocate_called;
   10164              : 
   10165        26786 :   gfc_init_block (&fnblock);
   10166              : 
   10167        26786 :   decl_type = TREE_TYPE (decl);
   10168              : 
   10169        26786 :   if ((POINTER_TYPE_P (decl_type))
   10170              :         || (TREE_CODE (decl_type) == REFERENCE_TYPE && rank == 0))
   10171              :     {
   10172         1637 :       decl = build_fold_indirect_ref_loc (input_location, decl);
   10173              :       /* Deref dest in sync with decl, but only when it is not NULL.  */
   10174         1637 :       if (dest)
   10175          124 :         dest = build_fold_indirect_ref_loc (input_location, dest);
   10176              : 
   10177              :       /* Update the decl_type because it got dereferenced.  */
   10178         1637 :       decl_type = TREE_TYPE (decl);
   10179              :     }
   10180              : 
   10181              :   /* If this is an array of derived types with allocatable components
   10182              :      build a loop and recursively call this function.  */
   10183        26786 :   if (TREE_CODE (decl_type) == ARRAY_TYPE
   10184        26786 :       || (GFC_DESCRIPTOR_TYPE_P (decl_type) && rank != 0))
   10185              :     {
   10186         4793 :       tmp = gfc_conv_array_data (decl);
   10187         4793 :       var = build_fold_indirect_ref_loc (input_location, tmp);
   10188              : 
   10189              :       /* Get the number of elements - 1 and set the counter.  */
   10190         4793 :       if (GFC_DESCRIPTOR_TYPE_P (decl_type))
   10191              :         {
   10192              :           /* Use the descriptor for an allocatable array.  Since this
   10193              :              is a full array reference, we only need the descriptor
   10194              :              information from dimension = rank.  */
   10195         3499 :           tmp = gfc_full_array_size (&fnblock, decl, rank);
   10196         3499 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
   10197              :                                  gfc_array_index_type, tmp,
   10198              :                                  gfc_index_one_node);
   10199              : 
   10200         3499 :           null_cond = gfc_conv_descriptor_data_get (decl);
   10201         3499 :           null_cond = fold_build2_loc (input_location, NE_EXPR,
   10202              :                                        logical_type_node, null_cond,
   10203         3499 :                                        build_int_cst (TREE_TYPE (null_cond), 0));
   10204              :         }
   10205              :       else
   10206              :         {
   10207              :           /*  Otherwise use the TYPE_DOMAIN information.  */
   10208         1294 :           tmp = array_type_nelts_minus_one (decl_type);
   10209         1294 :           tmp = fold_convert (gfc_array_index_type, tmp);
   10210              :         }
   10211              : 
   10212              :       /* Remember that this is, in fact, the no. of elements - 1.  */
   10213         4793 :       nelems = gfc_evaluate_now (tmp, &fnblock);
   10214         4793 :       index = gfc_create_var (gfc_array_index_type, "S");
   10215              : 
   10216              :       /* Build the body of the loop.  */
   10217         4793 :       gfc_init_block (&loopbody);
   10218              : 
   10219         4793 :       vref = gfc_build_array_ref (var, index, NULL);
   10220              : 
   10221         4793 :       if (purpose == COPY_ALLOC_COMP || purpose == COPY_ONLY_ALLOC_COMP)
   10222              :         {
   10223         1011 :           tmp = build_fold_indirect_ref_loc (input_location,
   10224              :                                              gfc_conv_array_data (dest));
   10225         1011 :           dref = gfc_build_array_ref (tmp, index, NULL);
   10226         1011 :           tmp = structure_alloc_comps (der_type, vref, dref, rank,
   10227              :                                        COPY_ALLOC_COMP, caf_mode, args,
   10228              :                                        no_finalization);
   10229              :         }
   10230              :       else
   10231         3782 :         tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose,
   10232              :                                      caf_mode, args, no_finalization);
   10233              : 
   10234         4793 :       gfc_add_expr_to_block (&loopbody, tmp);
   10235              : 
   10236              :       /* Build the loop and return.  */
   10237         4793 :       gfc_init_loopinfo (&loop);
   10238         4793 :       loop.dimen = 1;
   10239         4793 :       loop.from[0] = gfc_index_zero_node;
   10240         4793 :       loop.loopvar[0] = index;
   10241         4793 :       loop.to[0] = nelems;
   10242         4793 :       gfc_trans_scalarizing_loops (&loop, &loopbody);
   10243         4793 :       gfc_add_block_to_block (&fnblock, &loop.pre);
   10244              : 
   10245         4793 :       tmp = gfc_finish_block (&fnblock);
   10246              :       /* When copying allocateable components, the above implements the
   10247              :          deep copy.  Nevertheless is a deep copy only allowed, when the current
   10248              :          component is allocated, for which code will be generated in
   10249              :          gfc_duplicate_allocatable (), where the deep copy code is just added
   10250              :          into the if's body, by adding tmp (the deep copy code) as last
   10251              :          argument to gfc_duplicate_allocatable ().  */
   10252         4793 :       if (purpose == COPY_ALLOC_COMP && caf_mode == 0
   10253         4793 :           && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (dest)))
   10254          758 :         tmp = gfc_duplicate_allocatable (dest, decl, decl_type, rank,
   10255              :                                          tmp);
   10256         4035 :       else if (null_cond != NULL_TREE)
   10257         2741 :         tmp = build3_v (COND_EXPR, null_cond, tmp,
   10258              :                         build_empty_stmt (input_location));
   10259              : 
   10260         4793 :       return tmp;
   10261              :     }
   10262              : 
   10263        21993 :   if (purpose == DEALLOCATE_ALLOC_COMP && der_type->attr.pdt_type)
   10264              :     {
   10265          863 :       tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   10266              :                                    DEALLOCATE_PDT_COMP, 0, args,
   10267              :                                    no_finalization);
   10268          863 :       gfc_add_expr_to_block (&fnblock, tmp);
   10269              :     }
   10270        21130 :   else if (purpose == ALLOCATE_PDT_COMP && der_type->attr.alloc_comp)
   10271              :     {
   10272          125 :       tmp = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   10273              :                                    NULLIFY_ALLOC_COMP, 0, args,
   10274              :                                    no_finalization);
   10275          125 :       gfc_add_expr_to_block (&fnblock, tmp);
   10276              :     }
   10277              : 
   10278              :   /* Still having a descriptor array of rank == 0 here, indicates an
   10279              :      allocatable coarrays.  Dereference it correctly.  */
   10280        21993 :   if (GFC_DESCRIPTOR_TYPE_P (decl_type))
   10281              :     {
   10282            5 :       decl = build_fold_indirect_ref (gfc_conv_array_data (decl));
   10283              :     }
   10284              :   /* Otherwise, act on the components or recursively call self to
   10285              :      act on a chain of components.  */
   10286        21993 :   seen_derived_types.add (der_type);
   10287        63925 :   for (c = der_type->components; c; c = c->next)
   10288              :     {
   10289        41932 :       bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED
   10290        41932 :                                   || c->ts.type == BT_CLASS)
   10291        41932 :                                     && c->ts.u.derived->attr.alloc_comp;
   10292        41932 :       bool same_type
   10293              :         = (c->ts.type == BT_DERIVED
   10294        10235 :            && seen_derived_types.contains (c->ts.u.derived))
   10295        49145 :           || (c->ts.type == BT_CLASS
   10296         2374 :               && seen_derived_types.contains (CLASS_DATA (c)->ts.u.derived));
   10297        41932 :       bool inside_wrapper = generating_copy_helper;
   10298              : 
   10299        41932 :       bool is_pdt_type = IS_PDT (c);
   10300        41932 :       tree strlen = NULL_TREE;
   10301              : 
   10302        41932 :       cdecl = c->backend_decl;
   10303        41932 :       ctype = TREE_TYPE (cdecl);
   10304              : 
   10305        41932 :       switch (purpose)
   10306              :         {
   10307              : 
   10308            3 :         case BCAST_ALLOC_COMP:
   10309              : 
   10310            3 :           tree ubound;
   10311            3 :           tree cdesc;
   10312            3 :           stmtblock_t derived_type_block;
   10313              : 
   10314            3 :           gfc_init_block (&tmpblock);
   10315              : 
   10316            3 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10317              :                                   decl, cdecl, NULL_TREE);
   10318              : 
   10319              :           /* Shortcut to get the attributes of the component.  */
   10320            3 :           if (c->ts.type == BT_CLASS)
   10321              :             {
   10322            0 :               attr = &CLASS_DATA (c)->attr;
   10323            0 :               if (attr->class_pointer)
   10324            0 :                 continue;
   10325              :             }
   10326              :           else
   10327              :             {
   10328            3 :               attr = &c->attr;
   10329            3 :               if (attr->pointer)
   10330            0 :                 continue;
   10331              :             }
   10332              : 
   10333              :           /* Do not broadcast a caf_token.  These are local to the image.  */
   10334            3 :           if (attr->caf_token)
   10335            1 :             continue;
   10336              : 
   10337            2 :           add_when_allocated = NULL_TREE;
   10338            2 :           if (cmp_has_alloc_comps
   10339            0 :               && !c->attr.pointer && !c->attr.proc_pointer)
   10340              :             {
   10341            0 :               if (c->ts.type == BT_CLASS)
   10342              :                 {
   10343            0 :                   rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
   10344            0 :                   add_when_allocated
   10345            0 :                       = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
   10346              :                                                comp, NULL_TREE, rank, purpose,
   10347              :                                                caf_mode, args, no_finalization);
   10348              :                 }
   10349              :               else
   10350              :                 {
   10351            0 :                   rank = c->as ? c->as->rank : 0;
   10352            0 :                   add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10353              :                                                               comp, NULL_TREE,
   10354              :                                                               rank, purpose,
   10355              :                                                               caf_mode, args,
   10356              :                                                               no_finalization);
   10357              :                 }
   10358              :             }
   10359              : 
   10360            2 :           gfc_init_block (&derived_type_block);
   10361            2 :           if (add_when_allocated)
   10362            0 :             gfc_add_expr_to_block (&derived_type_block, add_when_allocated);
   10363            2 :           tmp = gfc_finish_block (&derived_type_block);
   10364            2 :           gfc_add_expr_to_block (&tmpblock, tmp);
   10365              : 
   10366              :           /* Convert the component into a rank 1 descriptor type.  */
   10367            2 :           if (attr->dimension)
   10368              :             {
   10369            0 :               tmp = gfc_get_element_type (TREE_TYPE (comp));
   10370            0 :               if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10371            0 :                 ubound = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (comp));
   10372              :               else
   10373            0 :                 ubound = gfc_full_array_size (&tmpblock, comp,
   10374            0 :                                               c->ts.type == BT_CLASS
   10375            0 :                                               ? CLASS_DATA (c)->as->rank
   10376            0 :                                               : c->as->rank);
   10377              :             }
   10378              :           else
   10379              :             {
   10380            2 :               tmp = TREE_TYPE (comp);
   10381            2 :               ubound = build_int_cst (gfc_array_index_type, 1);
   10382              :             }
   10383              : 
   10384              :           /* Treat strings like arrays.  Or the other way around, do not
   10385              :            * generate an additional array layer for scalar components.  */
   10386            2 :           if (attr->dimension || c->ts.type == BT_CHARACTER)
   10387              :             {
   10388            0 :               cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
   10389              :                                                  &ubound, 1,
   10390              :                                                  GFC_ARRAY_ALLOCATABLE, false);
   10391              : 
   10392            0 :               cdesc = gfc_create_var (cdesc, "cdesc");
   10393            0 :               DECL_ARTIFICIAL (cdesc) = 1;
   10394              : 
   10395            0 :               gfc_conv_descriptor_dtype_set (&tmpblock, cdesc,
   10396              :                                              gfc_get_dtype_rank_type (1, tmp));
   10397            0 :               gfc_conv_descriptor_lbound_set (&tmpblock, cdesc,
   10398              :                                               gfc_index_zero_node,
   10399              :                                               gfc_index_one_node);
   10400            0 :               gfc_conv_descriptor_stride_set (&tmpblock, cdesc,
   10401              :                                               gfc_index_zero_node,
   10402              :                                               gfc_index_one_node);
   10403            0 :               gfc_conv_descriptor_ubound_set (&tmpblock, cdesc,
   10404              :                                               gfc_index_zero_node, ubound);
   10405              :             }
   10406              :           else
   10407              :             /* Prevent warning.  */
   10408              :             cdesc = NULL_TREE;
   10409              : 
   10410            2 :           if (attr->dimension)
   10411              :             {
   10412            0 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10413            0 :                 comp = gfc_conv_descriptor_data_get (comp);
   10414              :               else
   10415            0 :                 comp = gfc_build_addr_expr (NULL_TREE, comp);
   10416              :             }
   10417              :           else
   10418              :             {
   10419            2 :               gfc_se se;
   10420              : 
   10421            2 :               gfc_init_se (&se, NULL);
   10422              : 
   10423            2 :               comp = gfc_conv_scalar_to_descriptor (&se, comp,
   10424            2 :                                                      c->ts.type == BT_CLASS
   10425            2 :                                                      ? CLASS_DATA (c)->attr
   10426              :                                                      : c->attr);
   10427            2 :               if (c->ts.type == BT_CHARACTER)
   10428            0 :                 comp = gfc_build_addr_expr (NULL_TREE, comp);
   10429            2 :               gfc_add_block_to_block (&tmpblock, &se.pre);
   10430              :             }
   10431              : 
   10432            2 :           if (attr->dimension || c->ts.type == BT_CHARACTER)
   10433            0 :             gfc_conv_descriptor_data_set (&tmpblock, cdesc, comp);
   10434              :           else
   10435            2 :             cdesc = comp;
   10436              : 
   10437            2 :           tree fndecl;
   10438              : 
   10439            2 :           fndecl = build_call_expr_loc (input_location,
   10440              :                                         gfor_fndecl_co_broadcast, 5,
   10441              :                                         gfc_build_addr_expr (pvoid_type_node,cdesc),
   10442              :                                         args->image_index,
   10443              :                                         null_pointer_node, null_pointer_node,
   10444              :                                         null_pointer_node);
   10445              : 
   10446            2 :           gfc_add_expr_to_block (&tmpblock, fndecl);
   10447            2 :           gfc_add_block_to_block (&fnblock, &tmpblock);
   10448              : 
   10449        33762 :           break;
   10450              : 
   10451        16265 :         case DEALLOCATE_ALLOC_COMP:
   10452              : 
   10453        16265 :           gfc_init_block (&tmpblock);
   10454              : 
   10455        16265 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10456              :                                   decl, cdecl, NULL_TREE);
   10457              : 
   10458              :           /* Shortcut to get the attributes of the component.  */
   10459        16265 :           if (c->ts.type == BT_CLASS)
   10460              :             {
   10461         1070 :               attr = &CLASS_DATA (c)->attr;
   10462         1070 :               if (attr->class_pointer || c->attr.proc_pointer)
   10463           18 :                 continue;
   10464              :             }
   10465              :           else
   10466              :             {
   10467        15195 :               attr = &c->attr;
   10468        15195 :               if (attr->pointer || attr->proc_pointer)
   10469          143 :                 continue;
   10470              :             }
   10471              : 
   10472        16104 :           if (!no_finalization && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
   10473         9397 :              || (c->ts.type == BT_CLASS && !CLASS_DATA (c)->attr.class_pointer)))
   10474              :             /* Call the finalizer, which will free the memory and nullify the
   10475              :                pointer of an array.  */
   10476         4182 :             deallocate_called = gfc_add_comp_finalizer_call (&tmpblock, comp, c,
   10477              :                                                          caf_enabled (caf_mode))
   10478         4182 :                 && attr->dimension;
   10479              :           else
   10480              :             deallocate_called = false;
   10481              : 
   10482              :           /* Add the _class ref for classes.  */
   10483        16104 :           if (c->ts.type == BT_CLASS && attr->allocatable)
   10484         1052 :             comp = gfc_class_data_get (comp);
   10485              : 
   10486        16104 :           add_when_allocated = NULL_TREE;
   10487        16104 :           if (cmp_has_alloc_comps
   10488         3639 :               && !c->attr.pointer && !c->attr.proc_pointer
   10489              :               && !same_type
   10490         3639 :               && !deallocate_called)
   10491              :             {
   10492              :               /* Add checked deallocation of the components.  This code is
   10493              :                  obviously added because the finalizer is not trusted to free
   10494              :                  all memory.  */
   10495         2197 :               if (c->ts.type == BT_CLASS)
   10496              :                 {
   10497          242 :                   rank = CLASS_DATA (c)->as ? CLASS_DATA (c)->as->rank : 0;
   10498          242 :                   add_when_allocated
   10499          242 :                       = structure_alloc_comps (CLASS_DATA (c)->ts.u.derived,
   10500              :                                                comp, NULL_TREE, rank, purpose,
   10501              :                                                caf_mode, args, no_finalization);
   10502              :                 }
   10503              :               else
   10504              :                 {
   10505         1955 :                   rank = c->as ? c->as->rank : 0;
   10506         1955 :                   add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10507              :                                                               comp, NULL_TREE,
   10508              :                                                               rank, purpose,
   10509              :                                                               caf_mode, args,
   10510              :                                                               no_finalization);
   10511              :                 }
   10512              :             }
   10513              : 
   10514        10314 :           if (attr->allocatable && !same_type
   10515        25251 :               && (!attr->codimension || caf_enabled (caf_mode)))
   10516              :             {
   10517              :               /* Handle all types of components besides components of the
   10518              :                  same_type as the current one, because those would create an
   10519              :                  endless loop.  */
   10520           51 :               caf_dereg_mode = (caf_in_coarray (caf_mode)
   10521           58 :                                 && (attr->dimension || c->caf_token))
   10522         9083 :                                     || attr->codimension
   10523         9218 :                                  ? (gfc_caf_is_dealloc_only (caf_mode)
   10524              :                                       ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
   10525              :                                       : GFC_CAF_COARRAY_DEREGISTER)
   10526              :                                  : GFC_CAF_COARRAY_NOCOARRAY;
   10527              : 
   10528         9140 :               caf_token = NULL_TREE;
   10529              :               /* Coarray components are handled directly by
   10530              :                  deallocate_with_status.  */
   10531         9140 :               if (!attr->codimension
   10532         9119 :                   && caf_dereg_mode != GFC_CAF_COARRAY_NOCOARRAY)
   10533              :                 {
   10534           57 :                   if (c->caf_token)
   10535           19 :                     caf_token
   10536           19 :                       = fold_build3_loc (input_location, COMPONENT_REF,
   10537           19 :                                          TREE_TYPE (gfc_comp_caf_token (c)),
   10538              :                                          decl, gfc_comp_caf_token (c),
   10539              :                                          NULL_TREE);
   10540           38 :                   else if (attr->dimension && !attr->proc_pointer)
   10541           38 :                     caf_token = gfc_conv_descriptor_token (comp);
   10542              :                 }
   10543              : 
   10544         9140 :               tmp = gfc_deallocate_with_status (comp, NULL_TREE, NULL_TREE,
   10545              :                                                 NULL_TREE, NULL_TREE, true,
   10546              :                                                 NULL, caf_dereg_mode, NULL_TREE,
   10547              :                                                 add_when_allocated, caf_token);
   10548              : 
   10549         9140 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10550              :             }
   10551         6964 :           else if (attr->allocatable && !attr->codimension
   10552         1167 :                    && !deallocate_called)
   10553              :             {
   10554              :               /* Case of recursive allocatable derived types.  */
   10555         1167 :               tree is_allocated;
   10556         1167 :               tree ubound;
   10557         1167 :               tree cdesc;
   10558         1167 :               stmtblock_t dealloc_block;
   10559              : 
   10560         1167 :               gfc_init_block (&dealloc_block);
   10561         1167 :               if (add_when_allocated)
   10562            0 :                 gfc_add_expr_to_block (&dealloc_block, add_when_allocated);
   10563              : 
   10564              :               /* Convert the component into a rank 1 descriptor type.  */
   10565         1167 :               if (attr->dimension)
   10566              :                 {
   10567          417 :                   tmp = gfc_get_element_type (TREE_TYPE (comp));
   10568          417 :                   ubound = gfc_full_array_size (&dealloc_block, comp,
   10569          417 :                                                 c->ts.type == BT_CLASS
   10570            0 :                                                 ? CLASS_DATA (c)->as->rank
   10571          417 :                                                 : c->as->rank);
   10572              :                 }
   10573              :               else
   10574              :                 {
   10575          750 :                   tmp = TREE_TYPE (comp);
   10576          750 :                   ubound = build_int_cst (gfc_array_index_type, 1);
   10577              :                 }
   10578              : 
   10579         1167 :               cdesc = gfc_get_array_type_bounds (tmp, 1, 0, &gfc_index_one_node,
   10580              :                                                  &ubound, 1,
   10581              :                                                  GFC_ARRAY_ALLOCATABLE, false);
   10582              : 
   10583         1167 :               cdesc = gfc_create_var (cdesc, "cdesc");
   10584         1167 :               DECL_ARTIFICIAL (cdesc) = 1;
   10585              : 
   10586         1167 :               gfc_conv_descriptor_dtype_set (&dealloc_block, cdesc,
   10587              :                                              gfc_get_dtype_rank_type (1, tmp));
   10588         1167 :               gfc_conv_descriptor_lbound_set (&dealloc_block, cdesc,
   10589              :                                               gfc_index_zero_node,
   10590              :                                               gfc_index_one_node);
   10591         1167 :               gfc_conv_descriptor_stride_set (&dealloc_block, cdesc,
   10592              :                                               gfc_index_zero_node,
   10593              :                                               gfc_index_one_node);
   10594         1167 :               gfc_conv_descriptor_ubound_set (&dealloc_block, cdesc,
   10595              :                                               gfc_index_zero_node, ubound);
   10596              : 
   10597         1167 :               if (attr->dimension)
   10598          417 :                 comp = gfc_conv_descriptor_data_get (comp);
   10599              : 
   10600         1167 :               gfc_conv_descriptor_data_set (&dealloc_block, cdesc, comp);
   10601              : 
   10602              :               /* Now call the deallocator.  */
   10603         1167 :               vtab = gfc_find_vtab (&c->ts);
   10604         1167 :               if (vtab->backend_decl == NULL)
   10605           47 :                 gfc_get_symbol_decl (vtab);
   10606         1167 :               tmp = gfc_build_addr_expr (NULL_TREE, vtab->backend_decl);
   10607         1167 :               dealloc_fndecl = gfc_vptr_deallocate_get (tmp);
   10608         1167 :               dealloc_fndecl = build_fold_indirect_ref_loc (input_location,
   10609              :                                                             dealloc_fndecl);
   10610         1167 :               tmp = build_int_cst (TREE_TYPE (comp), 0);
   10611         1167 :               is_allocated = fold_build2_loc (input_location, NE_EXPR,
   10612              :                                               logical_type_node, tmp,
   10613              :                                               comp);
   10614         1167 :               cdesc = gfc_build_addr_expr (NULL_TREE, cdesc);
   10615              : 
   10616         1167 :               tmp = build_call_expr_loc (input_location,
   10617              :                                          dealloc_fndecl, 1,
   10618              :                                          cdesc);
   10619         1167 :               gfc_add_expr_to_block (&dealloc_block, tmp);
   10620              : 
   10621         1167 :               tmp = gfc_finish_block (&dealloc_block);
   10622              : 
   10623         1167 :               tmp = fold_build3_loc (input_location, COND_EXPR,
   10624              :                                      void_type_node, is_allocated, tmp,
   10625              :                                      build_empty_stmt (input_location));
   10626              : 
   10627         1167 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10628         1167 :             }
   10629         5797 :           else if (add_when_allocated)
   10630         1159 :             gfc_add_expr_to_block (&tmpblock, add_when_allocated);
   10631              : 
   10632         1052 :           if (c->ts.type == BT_CLASS && attr->allocatable
   10633        17156 :               && (!attr->codimension || !caf_enabled (caf_mode)))
   10634              :             {
   10635              :               /* Finally, reset the vptr to the declared type vtable and, if
   10636              :                  necessary reset the _len field.
   10637              : 
   10638              :                  First recover the reference to the component and obtain
   10639              :                  the vptr.  */
   10640         1037 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10641              :                                       decl, cdecl, NULL_TREE);
   10642         1037 :               tmp = gfc_class_vptr_get (comp);
   10643              : 
   10644         1037 :               if (UNLIMITED_POLY (c))
   10645              :                 {
   10646              :                   /* Both vptr and _len field should be nulled.  */
   10647          231 :                   gfc_add_modify (&tmpblock, tmp,
   10648          231 :                                   build_int_cst (TREE_TYPE (tmp), 0));
   10649          231 :                   tmp = gfc_class_len_get (comp);
   10650          231 :                   gfc_add_modify (&tmpblock, tmp,
   10651          231 :                                   build_int_cst (TREE_TYPE (tmp), 0));
   10652              :                 }
   10653              :               else
   10654              :                 {
   10655              :                   /* Build the vtable address and set the vptr with it.  */
   10656          806 :                   gfc_reset_vptr (&tmpblock, nullptr, tmp, c->ts.u.derived);
   10657              :                 }
   10658              :             }
   10659              : 
   10660              :           /* Now add the deallocation of this component.  */
   10661        16104 :           gfc_add_block_to_block (&fnblock, &tmpblock);
   10662        16104 :           break;
   10663              : 
   10664         6239 :         case NULLIFY_ALLOC_COMP:
   10665              :           /* Nullify
   10666              :              - allocatable components (regular or in class)
   10667              :              - components that have allocatable components
   10668              :              - pointer components when in a coarray.
   10669              :              Skip everything else especially proc_pointers, which may come
   10670              :              coupled with the regular pointer attribute.  */
   10671         8393 :           if (c->attr.proc_pointer
   10672         6239 :               || !(c->attr.allocatable || (c->ts.type == BT_CLASS
   10673          494 :                                            && CLASS_DATA (c)->attr.allocatable)
   10674         2775 :                    || (cmp_has_alloc_comps
   10675          538 :                        && ((c->ts.type == BT_DERIVED && !c->attr.pointer)
   10676           18 :                            || (c->ts.type == BT_CLASS
   10677           12 :                                && !CLASS_DATA (c)->attr.class_pointer)))
   10678         2255 :                    || (caf_in_coarray (caf_mode) && c->attr.pointer)))
   10679         2154 :             continue;
   10680              : 
   10681              :           /* Process class components first, because they always have the
   10682              :              pointer-attribute set which would be caught wrong else.  */
   10683         4085 :           if (c->ts.type == BT_CLASS
   10684          481 :               && (CLASS_DATA (c)->attr.allocatable
   10685            0 :                   || CLASS_DATA (c)->attr.class_pointer))
   10686              :             {
   10687          481 :               tree class_ref;
   10688              : 
   10689              :               /* Allocatable CLASS components.  */
   10690          481 :               class_ref = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10691              :                                            decl, cdecl, NULL_TREE);
   10692              : 
   10693          481 :               comp = gfc_class_data_get (class_ref);
   10694          481 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   10695          269 :                 gfc_conv_descriptor_data_set (&fnblock, comp,
   10696              :                                               null_pointer_node);
   10697              :               else
   10698              :                 {
   10699          212 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10700              :                                          void_type_node, comp,
   10701          212 :                                          build_int_cst (TREE_TYPE (comp), 0));
   10702          212 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10703              :                 }
   10704              : 
   10705              :               /* The dynamic type of a disassociated pointer or unallocated
   10706              :                  allocatable variable is its declared type. An unlimited
   10707              :                  polymorphic entity has no declared type.  */
   10708          481 :               gfc_reset_vptr (&fnblock, nullptr, class_ref, c->ts.u.derived);
   10709              : 
   10710          481 :               cmp_has_alloc_comps = false;
   10711          481 :             }
   10712              :           /* Coarrays need the component to be nulled before the api-call
   10713              :              is made.  */
   10714         3604 :           else if (c->attr.pointer || c->attr.allocatable)
   10715              :             {
   10716         3084 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10717              :                                       decl, cdecl, NULL_TREE);
   10718         3084 :               if (c->attr.dimension || c->attr.codimension)
   10719         2205 :                 gfc_conv_descriptor_data_set (&fnblock, comp,
   10720              :                                               null_pointer_node);
   10721              :               else
   10722          879 :                 gfc_add_modify (&fnblock, comp,
   10723          879 :                                 build_int_cst (TREE_TYPE (comp), 0));
   10724         3084 :               if (!c->attr.pdt_string && gfc_deferred_strlen (c, &comp))
   10725              :                 {
   10726          323 :                   comp = fold_build3_loc (input_location, COMPONENT_REF,
   10727          323 :                                           TREE_TYPE (comp),
   10728              :                                           decl, comp, NULL_TREE);
   10729          646 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10730          323 :                                          TREE_TYPE (comp), comp,
   10731          323 :                                          build_int_cst (TREE_TYPE (comp), 0));
   10732          323 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10733              :                 }
   10734              :               cmp_has_alloc_comps = false;
   10735              :             }
   10736              : 
   10737         4085 :           if (flag_coarray == GFC_FCOARRAY_LIB && caf_in_coarray (caf_mode))
   10738              :             {
   10739              :               /* Register a component of a derived type coarray with the
   10740              :                  coarray library.  Do not register ultimate component
   10741              :                  coarrays here.  They are treated like regular coarrays and
   10742              :                  are either allocated on all images or on none.  */
   10743          132 :               tree token;
   10744              : 
   10745          132 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10746              :                                       decl, cdecl, NULL_TREE);
   10747          132 :               if (c->attr.dimension)
   10748              :                 {
   10749              :                   /* Set the dtype, because caf_register needs it.  */
   10750          104 :                   tree dtype_val = gfc_get_dtype (TREE_TYPE (comp));
   10751          104 :                   gfc_conv_descriptor_dtype_set (&fnblock, comp, dtype_val);
   10752          104 :                   tmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10753              :                                          decl, cdecl, NULL_TREE);
   10754          104 :                   token = gfc_conv_descriptor_token (tmp);
   10755              :                 }
   10756              :               else
   10757              :                 {
   10758           28 :                   gfc_se se;
   10759              : 
   10760           28 :                   gfc_init_se (&se, NULL);
   10761           56 :                   token = fold_build3_loc (input_location, COMPONENT_REF,
   10762              :                                            pvoid_type_node, decl,
   10763           28 :                                            gfc_comp_caf_token (c), NULL_TREE);
   10764           28 :                   comp = gfc_conv_scalar_to_descriptor (&se, comp,
   10765           28 :                                                         c->ts.type == BT_CLASS
   10766           28 :                                                         ? CLASS_DATA (c)->attr
   10767              :                                                         : c->attr);
   10768           28 :                   gfc_add_block_to_block (&fnblock, &se.pre);
   10769              :                 }
   10770              : 
   10771          132 :               gfc_allocate_using_caf_lib (&fnblock, comp, size_zero_node,
   10772              :                                           gfc_build_addr_expr (NULL_TREE,
   10773              :                                                                token),
   10774              :                                           NULL_TREE, NULL_TREE, NULL_TREE,
   10775              :                                           GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
   10776              :             }
   10777              : 
   10778         4085 :           if (cmp_has_alloc_comps)
   10779              :             {
   10780          520 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10781              :                                       decl, cdecl, NULL_TREE);
   10782          520 :               rank = c->as ? c->as->rank : 0;
   10783          520 :               tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE,
   10784              :                                            rank, purpose, caf_mode, args,
   10785              :                                            no_finalization);
   10786          520 :               gfc_add_expr_to_block (&fnblock, tmp);
   10787              :             }
   10788              :           break;
   10789              : 
   10790           30 :         case REASSIGN_CAF_COMP:
   10791           30 :           if (caf_enabled (caf_mode)
   10792           30 :               && (c->attr.codimension
   10793           23 :                   || (c->ts.type == BT_CLASS
   10794            2 :                       && (CLASS_DATA (c)->attr.coarray_comp
   10795            2 :                           || caf_in_coarray (caf_mode)))
   10796           21 :                   || (c->ts.type == BT_DERIVED
   10797            7 :                       && (c->ts.u.derived->attr.coarray_comp
   10798            6 :                           || caf_in_coarray (caf_mode))))
   10799           46 :               && !same_type)
   10800              :             {
   10801           14 :               comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10802              :                                       decl, cdecl, NULL_TREE);
   10803           14 :               dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   10804              :                                       dest, cdecl, NULL_TREE);
   10805              : 
   10806           14 :               if (c->attr.codimension)
   10807              :                 {
   10808            7 :                   if (c->ts.type == BT_CLASS)
   10809              :                     {
   10810            0 :                       comp = gfc_class_data_get (comp);
   10811            0 :                       dcmp = gfc_class_data_get (dcmp);
   10812              :                     }
   10813            7 :                   gfc_conv_descriptor_data_set (&fnblock, dcmp,
   10814              :                                            gfc_conv_descriptor_data_get (comp));
   10815              :                 }
   10816              :               else
   10817              :                 {
   10818            7 :                   tmp = structure_alloc_comps (c->ts.u.derived, comp, dcmp,
   10819              :                                                rank, purpose, caf_mode
   10820              :                                                | GFC_STRUCTURE_CAF_MODE_IN_COARRAY,
   10821              :                                                args, no_finalization);
   10822            7 :                   gfc_add_expr_to_block (&fnblock, tmp);
   10823              :                 }
   10824              :             }
   10825              :           break;
   10826              : 
   10827        12798 :         case COPY_ALLOC_COMP:
   10828        12798 :           if (c->attr.pointer || c->attr.proc_pointer)
   10829          153 :             continue;
   10830              : 
   10831              :           /* We need source and destination components.  */
   10832        12645 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl,
   10833              :                                   cdecl, NULL_TREE);
   10834        12645 :           dcmp = fold_build3_loc (input_location, COMPONENT_REF, ctype, dest,
   10835              :                                   cdecl, NULL_TREE);
   10836        12645 :           dcmp = fold_convert (TREE_TYPE (comp), dcmp);
   10837              : 
   10838        12645 :           if (IS_PDT (c) && !c->attr.allocatable)
   10839              :             {
   10840          117 :               tmp = gfc_copy_alloc_comp (c->ts.u.derived, comp, dcmp,
   10841              :                                          0, 0);
   10842          117 :               gfc_add_expr_to_block (&fnblock, tmp);
   10843          117 :               continue;
   10844              :             }
   10845              : 
   10846        12528 :           if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
   10847              :             {
   10848          780 :               tree ftn_tree;
   10849          780 :               tree size;
   10850          780 :               tree dst_data;
   10851          780 :               tree src_data;
   10852          780 :               tree null_data;
   10853              : 
   10854          780 :               dst_data = gfc_class_data_get (dcmp);
   10855          780 :               src_data = gfc_class_data_get (comp);
   10856          780 :               size = fold_convert (size_type_node,
   10857              :                                    gfc_class_vtab_size_get (comp));
   10858              : 
   10859          780 :               if (CLASS_DATA (c)->attr.dimension)
   10860              :                 {
   10861          752 :                   nelems = gfc_conv_descriptor_size (src_data,
   10862          376 :                                                      CLASS_DATA (c)->as->rank);
   10863          376 :                   size = fold_build2_loc (input_location, MULT_EXPR,
   10864              :                                           size_type_node, size,
   10865              :                                           fold_convert (size_type_node,
   10866              :                                                         nelems));
   10867              :                 }
   10868              :               else
   10869          404 :                 nelems = build_int_cst (size_type_node, 1);
   10870              : 
   10871          780 :               if (CLASS_DATA (c)->attr.dimension
   10872          404 :                   || CLASS_DATA (c)->attr.codimension)
   10873              :                 {
   10874          384 :                   src_data = gfc_conv_descriptor_data_get (src_data);
   10875          384 :                   dst_data = gfc_conv_descriptor_data_get (dst_data);
   10876              :                 }
   10877              : 
   10878          780 :               gfc_init_block (&tmpblock);
   10879              : 
   10880          780 :               gfc_add_modify (&tmpblock, gfc_class_vptr_get (dcmp),
   10881              :                               gfc_class_vptr_get (comp));
   10882              : 
   10883              :               /* Copy the unlimited '_len' field. If it is greater than zero
   10884              :                  (ie. a character(_len)), multiply it by size and use this
   10885              :                  for the malloc call.  */
   10886          780 :               if (UNLIMITED_POLY (c))
   10887              :                 {
   10888          158 :                   gfc_add_modify (&tmpblock, gfc_class_len_get (dcmp),
   10889              :                                   gfc_class_len_get (comp));
   10890          158 :                   size = gfc_resize_class_size_with_len (&tmpblock, comp, size);
   10891              :                 }
   10892              : 
   10893              :               /* Coarray component have to have the same allocation status and
   10894              :                  shape/type-parameter/effective-type on the LHS and RHS of an
   10895              :                  intrinsic assignment. Hence, we did not deallocated them - and
   10896              :                  do not allocate them here.  */
   10897          780 :               if (!CLASS_DATA (c)->attr.codimension)
   10898              :                 {
   10899          765 :                   ftn_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
   10900          765 :                   tmp = build_call_expr_loc (input_location, ftn_tree, 1, size);
   10901          765 :                   gfc_add_modify (&tmpblock, dst_data,
   10902          765 :                                   fold_convert (TREE_TYPE (dst_data), tmp));
   10903              :                 }
   10904              : 
   10905         1545 :               tmp = gfc_copy_class_to_class (comp, dcmp, nelems,
   10906          780 :                                              UNLIMITED_POLY (c));
   10907          780 :               gfc_add_expr_to_block (&tmpblock, tmp);
   10908          780 :               tmp = gfc_finish_block (&tmpblock);
   10909              : 
   10910          780 :               gfc_init_block (&tmpblock);
   10911          780 :               gfc_add_modify (&tmpblock, dst_data,
   10912          780 :                               fold_convert (TREE_TYPE (dst_data),
   10913              :                                             null_pointer_node));
   10914          780 :               null_data = gfc_finish_block (&tmpblock);
   10915              : 
   10916          780 :               null_cond = fold_build2_loc (input_location, NE_EXPR,
   10917              :                                            logical_type_node, src_data,
   10918              :                                            null_pointer_node);
   10919              : 
   10920          780 :               gfc_add_expr_to_block (&fnblock, build3_v (COND_EXPR, null_cond,
   10921              :                                                          tmp, null_data));
   10922          780 :               continue;
   10923          780 :             }
   10924              : 
   10925              :           /* To implement guarded deep copy, i.e., deep copy only allocatable
   10926              :              components that are really allocated, the deep copy code has to
   10927              :              be generated first and then added to the if-block in
   10928              :              gfc_duplicate_allocatable ().  */
   10929        11748 :           if (cmp_has_alloc_comps && !c->attr.proc_pointer && !same_type)
   10930              :             {
   10931         1832 :               rank = c->as ? c->as->rank : 0;
   10932         1832 :               tmp = fold_convert (TREE_TYPE (dcmp), comp);
   10933         1832 :               gfc_add_modify (&fnblock, dcmp, tmp);
   10934         1832 :               add_when_allocated = structure_alloc_comps (c->ts.u.derived,
   10935              :                                                           comp, dcmp,
   10936              :                                                           rank, purpose,
   10937              :                                                           caf_mode, args,
   10938              :                                                           no_finalization);
   10939              :             }
   10940              :           else
   10941              :             add_when_allocated = NULL_TREE;
   10942              : 
   10943        11748 :           if (gfc_deferred_strlen (c, &tmp))
   10944              :             {
   10945          423 :               tree len, size;
   10946          423 :               len = tmp;
   10947          423 :               tmp = fold_build3_loc (input_location, COMPONENT_REF,
   10948          423 :                                      TREE_TYPE (len),
   10949              :                                      decl, len, NULL_TREE);
   10950          423 :               len = fold_build3_loc (input_location, COMPONENT_REF,
   10951          423 :                                      TREE_TYPE (len),
   10952              :                                      dest, len, NULL_TREE);
   10953          423 :               tmp = fold_build2_loc (input_location, MODIFY_EXPR,
   10954          423 :                                      TREE_TYPE (len), len, tmp);
   10955          423 :               gfc_add_expr_to_block (&fnblock, tmp);
   10956          423 :               size = size_of_string_in_bytes (c->ts.kind, len);
   10957              :               /* This component cannot have allocatable components,
   10958              :                  therefore add_when_allocated of duplicate_allocatable ()
   10959              :                  is always NULL.  */
   10960          423 :               rank = c->as ? c->as->rank : 0;
   10961          423 :               tmp = duplicate_allocatable (dcmp, comp, ctype, rank,
   10962              :                                            false, false, size, NULL_TREE);
   10963          423 :               gfc_add_expr_to_block (&fnblock, tmp);
   10964              :             }
   10965        11325 :           else if (c->attr.pdt_array
   10966          176 :                    && !c->attr.allocatable && !c->attr.pointer)
   10967              :             {
   10968          176 :               tmp = duplicate_allocatable (dcmp, comp, ctype,
   10969          176 :                                            c->as ? c->as->rank : 0,
   10970              :                                            false, false, NULL_TREE, NULL_TREE);
   10971          176 :               gfc_add_expr_to_block (&fnblock, tmp);
   10972              :             }
   10973              :       /* Special case: recursive allocatable array components require
   10974              :          runtime helpers to avoid compile-time infinite recursion.  Generate
   10975              :          a call to _gfortran_cfi_deep_copy_array with an element copy
   10976              :          wrapper.  When inside a wrapper, reuse current_function_decl.  */
   10977         6790 :       else if (c->attr.allocatable && cmp_has_alloc_comps && same_type
   10978         1288 :                && purpose == COPY_ALLOC_COMP && !c->attr.proc_pointer
   10979         1288 :                && !c->attr.codimension && !caf_in_coarray (caf_mode)
   10980        12437 :                && c->ts.type == BT_DERIVED && c->ts.u.derived != NULL)
   10981              :             {
   10982         1288 :               tree copy_wrapper, call, dest_addr, src_addr, elem_type;
   10983         1288 :               tree helper_ptr_type;
   10984         1288 :               tree alloc_expr;
   10985         1288 :               int comp_rank;
   10986              : 
   10987              :               /* Get the element type from ctype (already the component
   10988              :                  type).  For arrays we need the element type, not the array
   10989              :                  type.  */
   10990         1288 :               elem_type = ctype;
   10991         1288 :               if (GFC_DESCRIPTOR_TYPE_P (ctype))
   10992          930 :                 elem_type = gfc_get_element_type (ctype);
   10993          358 :               else if (TREE_CODE (ctype) == ARRAY_TYPE)
   10994            0 :                 elem_type = TREE_TYPE (ctype);
   10995          358 :               else if (!c->as)
   10996          358 :                 elem_type = TREE_TYPE (TREE_TYPE (comp));
   10997              : 
   10998         1288 :               helper_ptr_type = get_copy_helper_pointer_type ();
   10999              : 
   11000         1288 :               comp_rank = c->as ? c->as->rank : 0;
   11001         1288 :               alloc_expr = gfc_duplicate_allocatable_nocopy (dcmp, comp, ctype,
   11002              :                                                              comp_rank);
   11003         1288 :               gfc_add_expr_to_block (&fnblock, alloc_expr);
   11004              : 
   11005              :               /* Generate or reuse the element copy helper.  Inside an
   11006              :                  existing helper we can reuse the current function to
   11007              :                  prevent recursive generation.  */
   11008         1288 :               if (inside_wrapper)
   11009          906 :                 copy_wrapper
   11010          906 :                   = gfc_build_addr_expr (NULL_TREE, current_function_decl);
   11011              :               else
   11012          382 :                 copy_wrapper
   11013          382 :                   = generate_element_copy_wrapper (c->ts.u.derived, elem_type,
   11014              :                                                    purpose, caf_mode);
   11015         1288 :               copy_wrapper = fold_convert (helper_ptr_type, copy_wrapper);
   11016              : 
   11017         1288 :               if (c->as)
   11018              :                 {
   11019              :                   /* Build addresses of descriptors.  */
   11020          930 :                   dest_addr = gfc_build_addr_expr (pvoid_type_node, dcmp);
   11021          930 :                   src_addr = gfc_build_addr_expr (pvoid_type_node, comp);
   11022              :                 }
   11023              :               else
   11024              :                 {
   11025              :                   /* For scalars, create separate descriptors for source and
   11026              :                      dest, then pass their addresses.  */
   11027          358 :                   gfc_se se;
   11028          358 :                   gfc_init_se (&se, NULL);
   11029          358 :                   tmp = gfc_conv_scalar_to_descriptor (&se, dcmp, c->attr);
   11030          358 :                   dest_addr = gfc_build_addr_expr (pvoid_type_node, tmp);
   11031          358 :                   tmp = gfc_conv_scalar_to_descriptor (&se, comp, c->attr);
   11032          358 :                   src_addr = gfc_build_addr_expr (pvoid_type_node, tmp);
   11033          358 :                   gfc_add_block_to_block (&fnblock, &se.pre);
   11034              :                 }
   11035              : 
   11036              :               /* Build call: _gfortran_cfi_deep_copy_array (&dcmp, &comp, wrapper).  */
   11037         1288 :               call = build_call_expr_loc (input_location,
   11038              :                                           gfor_fndecl_cfi_deep_copy_array, 3,
   11039              :                                           dest_addr, src_addr,
   11040              :                                           copy_wrapper);
   11041              : 
   11042         1288 :               gfc_add_expr_to_block (&fnblock, call);
   11043              :             }
   11044              :           /* For allocatable arrays with nested allocatable components,
   11045              :              add_when_allocated already includes gfc_duplicate_allocatable
   11046              :              (from the recursive structure_alloc_comps call at line 10290-10293),
   11047              :              so we must not call it again here.  PR121628 added an
   11048              :              add_when_allocated != NULL clause that was redundant for scalars
   11049              :              (already handled by !c->as) and wrong for arrays (double alloc).  */
   11050         5502 :           else if (c->attr.allocatable && !c->attr.proc_pointer
   11051        15363 :                    && (!cmp_has_alloc_comps
   11052          717 :                        || !c->as
   11053          603 :                        || c->attr.codimension
   11054          600 :                        || caf_in_coarray (caf_mode)))
   11055              :             {
   11056         4908 :               rank = c->as ? c->as->rank : 0;
   11057         4908 :               if (c->attr.codimension)
   11058           20 :                 tmp = gfc_copy_allocatable_data (dcmp, comp, ctype, rank);
   11059         4888 :               else if (flag_coarray == GFC_FCOARRAY_LIB
   11060         4888 :                        && caf_in_coarray (caf_mode))
   11061              :                 {
   11062           62 :                   tree dst_tok;
   11063           62 :                   if (c->as)
   11064           44 :                     dst_tok = gfc_conv_descriptor_token (dcmp);
   11065              :                   else
   11066              :                     {
   11067           18 :                       dst_tok
   11068           18 :                         = fold_build3_loc (input_location, COMPONENT_REF,
   11069              :                                            pvoid_type_node, dest,
   11070           18 :                                            gfc_comp_caf_token (c), NULL_TREE);
   11071              :                     }
   11072           62 :                   tmp
   11073           62 :                     = duplicate_allocatable_coarray (dcmp, dst_tok, comp, ctype,
   11074              :                                                      rank, add_when_allocated);
   11075              :                 }
   11076              :               else
   11077         4826 :                 tmp = gfc_duplicate_allocatable (dcmp, comp, ctype, rank,
   11078              :                                                  add_when_allocated);
   11079         4908 :               gfc_add_expr_to_block (&fnblock, tmp);
   11080              :             }
   11081              :           else
   11082         4953 :             if (cmp_has_alloc_comps || is_pdt_type)
   11083         1865 :               gfc_add_expr_to_block (&fnblock, add_when_allocated);
   11084              : 
   11085              :           break;
   11086              : 
   11087         2404 :         case ALLOCATE_PDT_COMP:
   11088              : 
   11089         2404 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11090              :                                   decl, cdecl, NULL_TREE);
   11091              : 
   11092              :           /* Set the PDT KIND and LEN fields.  */
   11093         2404 :           if (c->attr.pdt_kind || c->attr.pdt_len)
   11094              :             {
   11095         1015 :               gfc_se tse;
   11096         1015 :               gfc_expr *c_expr = NULL;
   11097         1015 :               gfc_actual_arglist *param = pdt_param_list;
   11098         1015 :               gfc_init_se (&tse, NULL);
   11099         3813 :               for (; param; param = param->next)
   11100         1783 :                 if (param->name && !strcmp (c->name, param->name))
   11101         1003 :                   c_expr = param->expr;
   11102              : 
   11103         1015 :               if (!c_expr)
   11104           30 :                 c_expr = c->initializer;
   11105              : 
   11106           30 :               if (c_expr)
   11107              :                 {
   11108          997 :                   gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11109          997 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11110          997 :                   gfc_add_modify (&fnblock, comp, tse.expr);
   11111          997 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11112              :                 }
   11113         1015 :             }
   11114         1389 :           else if (c->initializer && !c->attr.pdt_string && !c->attr.pdt_array
   11115          195 :                    && !c->as && !IS_PDT (c))   /* Take care of arrays.  */
   11116              :             {
   11117           49 :               gfc_se tse;
   11118           49 :               gfc_expr *c_expr;
   11119           49 :               gfc_init_se (&tse, NULL);
   11120           49 :               c_expr = c->initializer;
   11121           49 :               gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11122           49 :               gfc_add_block_to_block (&fnblock, &tse.pre);
   11123           49 :               gfc_add_modify (&fnblock, comp, tse.expr);
   11124           49 :               gfc_add_block_to_block (&fnblock, &tse.post);
   11125              :             }
   11126              : 
   11127         2404 :           if (c->attr.pdt_string)
   11128              :             {
   11129          150 :               gfc_se tse;
   11130          150 :               gfc_init_se (&tse, NULL);
   11131          150 :               gfc_expr *e = gfc_copy_expr (c->ts.u.cl->length);
   11132              :               /* Convert the parameterized string length to its value. The
   11133              :                  string length is stored in a hidden field in the same way as
   11134              :                  deferred string lengths.  */
   11135          150 :               gfc_insert_parameter_exprs (e, pdt_param_list);
   11136          150 :               if (gfc_deferred_strlen (c, &strlen) && strlen != NULL_TREE)
   11137              :                 {
   11138          150 :                   gfc_conv_expr_type (&tse, e,
   11139          150 :                                       TREE_TYPE (strlen));
   11140          150 :                   strlen = fold_build3_loc (input_location, COMPONENT_REF,
   11141          150 :                                             TREE_TYPE (strlen),
   11142              :                                             decl, strlen, NULL_TREE);
   11143          150 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11144          150 :                   gfc_add_modify (&fnblock, strlen, tse.expr);
   11145          150 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11146          150 :                   c->ts.u.cl->backend_decl = strlen;
   11147              :                 }
   11148          150 :               gfc_free_expr (e);
   11149              : 
   11150              :               /* Scalar parameterized strings can be allocated now.  */
   11151          150 :               if (!c->as)
   11152              :                 {
   11153           90 :                   tmp = fold_convert (gfc_array_index_type, strlen);
   11154           90 :                   tmp = size_of_string_in_bytes (c->ts.kind, tmp);
   11155           90 :                   tmp = gfc_evaluate_now (tmp, &fnblock);
   11156           90 :                   tmp = gfc_call_malloc (&fnblock, TREE_TYPE (comp), tmp);
   11157           90 :                   gfc_add_modify (&fnblock, comp, tmp);
   11158              :                 }
   11159              :             }
   11160              : 
   11161              :           /* Allocate parameterized arrays of parameterized derived types.  */
   11162         2404 :           if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
   11163         2069 :               && !(IS_PDT (c) || IS_CLASS_PDT (c)))
   11164         1853 :             continue;
   11165              : 
   11166          551 :           if (c->ts.type == BT_CLASS)
   11167            0 :             comp = gfc_class_data_get (comp);
   11168              : 
   11169          551 :           if (c->attr.pdt_array
   11170          551 :               || (c->attr.pdt_string
   11171            0 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp))))
   11172              :             {
   11173          335 :               gfc_se tse;
   11174          335 :               int i;
   11175          335 :               tree size = gfc_index_one_node;
   11176          335 :               tree offset = gfc_index_zero_node;
   11177          335 :               tree lower, upper;
   11178          335 :               gfc_expr *e;
   11179              : 
   11180              :               /* This chunk takes the expressions for 'lower' and 'upper'
   11181              :                  in the arrayspec and substitutes in the expressions for
   11182              :                  the parameters from 'pdt_param_list'. The descriptor
   11183              :                  fields can then be filled from the values so obtained.  */
   11184          335 :               gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)));
   11185          772 :               for (i = 0; i < c->as->rank; i++)
   11186              :                 {
   11187          437 :                   gfc_init_se (&tse, NULL);
   11188          437 :                   e = gfc_copy_expr (c->as->lower[i]);
   11189          437 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11190          437 :                   gfc_conv_expr_type (&tse, e, gfc_array_index_type);
   11191          437 :                   gfc_free_expr (e);
   11192          437 :                   lower = tse.expr;
   11193          437 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11194          437 :                   gfc_conv_descriptor_lbound_set (&fnblock, comp,
   11195              :                                                   gfc_rank_cst[i],
   11196              :                                                   lower);
   11197          437 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11198          437 :                   e = gfc_copy_expr (c->as->upper[i]);
   11199          437 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11200          437 :                   gfc_conv_expr_type (&tse, e, gfc_array_index_type);
   11201          437 :                   gfc_free_expr (e);
   11202          437 :                   upper = tse.expr;
   11203          437 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11204          437 :                   gfc_conv_descriptor_ubound_set (&fnblock, comp,
   11205              :                                                   gfc_rank_cst[i],
   11206              :                                                   upper);
   11207          437 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11208          437 :                   gfc_conv_descriptor_stride_set (&fnblock, comp,
   11209              :                                                   gfc_rank_cst[i],
   11210              :                                                   size);
   11211          437 :                   size = gfc_evaluate_now (size, &fnblock);
   11212          437 :                   offset = fold_build2_loc (input_location,
   11213              :                                             MINUS_EXPR,
   11214              :                                             gfc_array_index_type,
   11215              :                                             offset, size);
   11216          437 :                   offset = gfc_evaluate_now (offset, &fnblock);
   11217          437 :                   tmp = fold_build2_loc (input_location, MINUS_EXPR,
   11218              :                                          gfc_array_index_type,
   11219              :                                          upper, lower);
   11220          437 :                   tmp = fold_build2_loc (input_location, PLUS_EXPR,
   11221              :                                          gfc_array_index_type,
   11222              :                                          tmp, gfc_index_one_node);
   11223          437 :                   size = fold_build2_loc (input_location, MULT_EXPR,
   11224              :                                           gfc_array_index_type, size, tmp);
   11225              :                 }
   11226          335 :               gfc_conv_descriptor_offset_set (&fnblock, comp, offset);
   11227          335 :               if (c->ts.type == BT_CLASS)
   11228              :                 {
   11229            0 :                   tmp = gfc_get_vptr_from_expr (comp);
   11230            0 :                   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
   11231            0 :                     tmp = build_fold_indirect_ref_loc (input_location, tmp);
   11232            0 :                   tmp = gfc_vptr_size_get (tmp);
   11233              :                 }
   11234          335 :               else if (strlen != NULL_TREE)
   11235           54 :                 tmp = strlen;
   11236              :               else
   11237          281 :                 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (ctype));
   11238          335 :               tmp = fold_convert (gfc_array_index_type, tmp);
   11239          335 :               size = fold_build2_loc (input_location, MULT_EXPR,
   11240              :                                       gfc_array_index_type, size, tmp);
   11241          335 :               size = gfc_evaluate_now (size, &fnblock);
   11242          335 :               tmp = gfc_call_malloc (&fnblock, NULL, size);
   11243          335 :               gfc_conv_descriptor_data_set (&fnblock, comp, tmp);
   11244          335 :               gfc_conv_descriptor_dtype_set (&fnblock, comp,
   11245              :                                              gfc_get_dtype (ctype));
   11246          335 :               if (strlen != NULL_TREE)
   11247              :                 {
   11248           54 :                   tmp = gfc_conv_descriptor_elem_len_get (comp);
   11249           54 :                   gfc_add_modify (&fnblock, tmp, fold_convert (TREE_TYPE (tmp), strlen));
   11250           54 :                   gfc_conv_descriptor_span_set (&fnblock, comp, strlen);
   11251              :                 }
   11252              : 
   11253          335 :               if (c->initializer && c->initializer->rank)
   11254              :                 {
   11255            0 :                   gfc_init_se (&tse, NULL);
   11256            0 :                   e = gfc_copy_expr (c->initializer);
   11257            0 :                   gfc_insert_parameter_exprs (e, pdt_param_list);
   11258            0 :                   gfc_conv_expr_descriptor (&tse, e);
   11259            0 :                   gfc_add_block_to_block (&fnblock, &tse.pre);
   11260            0 :                   gfc_free_expr (e);
   11261            0 :                   tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
   11262            0 :                   tmp = build_call_expr_loc (input_location, tmp, 3,
   11263              :                                      gfc_conv_descriptor_data_get (comp),
   11264              :                                      gfc_conv_descriptor_data_get (tse.expr),
   11265              :                                      fold_convert (size_type_node, size));
   11266            0 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11267            0 :                   gfc_add_block_to_block (&fnblock, &tse.post);
   11268              :                 }
   11269              :             }
   11270              : 
   11271              :           /* Recurse in to PDT components.  */
   11272          551 :           if ((IS_PDT (c) || IS_CLASS_PDT (c))
   11273          230 :               && !(c->attr.pointer || c->attr.allocatable))
   11274              :             {
   11275          140 :               gfc_actual_arglist *tail = c->param_list;
   11276              : 
   11277          382 :               for (; tail; tail = tail->next)
   11278          242 :                 if (tail->expr)
   11279          218 :                   gfc_insert_parameter_exprs (tail->expr, pdt_param_list);
   11280              : 
   11281          140 :               tmp = gfc_allocate_pdt_comp (c->ts.u.derived, comp,
   11282          140 :                                            c->as ? c->as->rank : 0,
   11283          140 :                                            c->param_list);
   11284          140 :               gfc_add_expr_to_block (&fnblock, tmp);
   11285              :             }
   11286              : 
   11287              :           break;
   11288              : 
   11289         3857 :         case DEALLOCATE_PDT_COMP:
   11290              :           /* Deallocate array or parameterized string length components
   11291              :              of parameterized derived types.  */
   11292         3857 :           if (!(c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
   11293         3261 :               && !c->attr.pdt_string
   11294         3147 :               && !(IS_PDT (c) || IS_CLASS_PDT (c)))
   11295         2665 :             continue;
   11296              : 
   11297         1192 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11298              :                                   decl, cdecl, NULL_TREE);
   11299         1192 :           if (c->ts.type == BT_CLASS)
   11300            0 :             comp = gfc_class_data_get (comp);
   11301              : 
   11302              :           /* Recurse in to PDT components.  */
   11303         1192 :           if ((IS_PDT (c) || IS_CLASS_PDT (c))
   11304          520 :               && (!c->attr.pointer && !c->attr.allocatable))
   11305              :             {
   11306          347 :               tmp = gfc_deallocate_pdt_comp (c->ts.u.derived, comp,
   11307          347 :                                              c->as ? c->as->rank : 0);
   11308          347 :               gfc_add_expr_to_block (&fnblock, tmp);
   11309              :             }
   11310              : 
   11311         1192 :           if (c->attr.pdt_array || c->attr.pdt_string)
   11312              :             {
   11313          710 :               tmp = comp;
   11314          710 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   11315          602 :                 tmp = gfc_conv_descriptor_data_get (comp);
   11316          710 :               null_cond = fold_build2_loc (input_location, NE_EXPR,
   11317              :                                            logical_type_node, tmp,
   11318          710 :                                            build_int_cst (TREE_TYPE (tmp), 0));
   11319          710 :               if (flag_openmp_allocators)
   11320              :                 {
   11321            0 :                   tree cd, t;
   11322            0 :                   if (c->attr.pdt_array)
   11323              :                     {
   11324            0 :                       tree version_val = gfc_conv_descriptor_version_get (comp);
   11325            0 :                       cd = fold_build2_loc (input_location, EQ_EXPR,
   11326              :                                             boolean_type_node, version_val,
   11327              :                                             integer_one_node);
   11328              :                     }
   11329              :                   else
   11330            0 :                     cd = gfc_omp_call_is_alloc (tmp);
   11331            0 :                   t = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
   11332            0 :                   t = build_call_expr_loc (input_location, t, 1, tmp);
   11333              : 
   11334            0 :                   stmtblock_t tblock;
   11335            0 :                   gfc_init_block (&tblock);
   11336            0 :                   gfc_add_expr_to_block (&tblock, t);
   11337            0 :                   if (c->attr.pdt_array)
   11338            0 :                     gfc_conv_descriptor_version_set (&tblock, comp,
   11339              :                                                      integer_zero_node);
   11340            0 :                   tmp = build3_loc (input_location, COND_EXPR, void_type_node,
   11341              :                                     cd, gfc_finish_block (&tblock),
   11342              :                                     gfc_call_free (tmp));
   11343              :                 }
   11344              :               else
   11345          710 :                 tmp = gfc_call_free (tmp);
   11346          710 :               tmp = build3_v (COND_EXPR, null_cond, tmp,
   11347              :                               build_empty_stmt (input_location));
   11348          710 :               gfc_add_expr_to_block (&fnblock, tmp);
   11349              : 
   11350          710 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (comp)))
   11351          602 :                 gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
   11352              :               else
   11353              :                 {
   11354          108 :                   tmp = fold_convert (TREE_TYPE (comp), null_pointer_node);
   11355          108 :                   gfc_add_modify (&fnblock, comp, tmp);
   11356              :                 }
   11357              :             }
   11358              : 
   11359              :           break;
   11360              : 
   11361          336 :         case CHECK_PDT_DUMMY:
   11362              : 
   11363          336 :           comp = fold_build3_loc (input_location, COMPONENT_REF, ctype,
   11364              :                                   decl, cdecl, NULL_TREE);
   11365          336 :           if (c->ts.type == BT_CLASS)
   11366            0 :             comp = gfc_class_data_get (comp);
   11367              : 
   11368              :           /* Recurse in to PDT components.  */
   11369          336 :           if (((c->ts.type == BT_DERIVED
   11370           14 :                 && !c->attr.allocatable && !c->attr.pointer)
   11371          324 :                || (c->ts.type == BT_CLASS
   11372            0 :                    && !CLASS_DATA (c)->attr.allocatable
   11373            0 :                    && !CLASS_DATA (c)->attr.pointer))
   11374           12 :               && c->ts.u.derived && c->ts.u.derived->attr.pdt_type)
   11375              :             {
   11376           12 :               tmp = gfc_check_pdt_dummy (c->ts.u.derived, comp,
   11377           12 :                                          c->as ? c->as->rank : 0,
   11378              :                                          pdt_param_list);
   11379           12 :               gfc_add_expr_to_block (&fnblock, tmp);
   11380              :             }
   11381              : 
   11382          336 :           if (!c->attr.pdt_len)
   11383          288 :             continue;
   11384              :           else
   11385              :             {
   11386           48 :               gfc_se tse;
   11387           48 :               gfc_expr *c_expr = NULL;
   11388           48 :               gfc_actual_arglist *param = pdt_param_list;
   11389              : 
   11390           48 :               gfc_init_se (&tse, NULL);
   11391          186 :               for (; param; param = param->next)
   11392           90 :                 if (!strcmp (c->name, param->name)
   11393           48 :                     && param->spec_type == SPEC_EXPLICIT)
   11394           30 :                   c_expr = param->expr;
   11395              : 
   11396           48 :               if (c_expr)
   11397              :                 {
   11398           30 :                   tree error, cond, cname;
   11399           30 :                   gfc_conv_expr_type (&tse, c_expr, TREE_TYPE (comp));
   11400           30 :                   cond = fold_build2_loc (input_location, NE_EXPR,
   11401              :                                           logical_type_node,
   11402              :                                           comp, tse.expr);
   11403           30 :                   cname = gfc_build_cstring_const (c->name);
   11404           30 :                   cname = gfc_build_addr_expr (pchar_type_node, cname);
   11405           30 :                   error = gfc_trans_runtime_error (true, NULL,
   11406              :                                                    "The value of the PDT LEN "
   11407              :                                                    "parameter '%s' does not "
   11408              :                                                    "agree with that in the "
   11409              :                                                    "dummy declaration",
   11410              :                                                    cname);
   11411           30 :                   tmp = fold_build3_loc (input_location, COND_EXPR,
   11412              :                                          void_type_node, cond, error,
   11413              :                                          build_empty_stmt (input_location));
   11414           30 :                   gfc_add_expr_to_block (&fnblock, tmp);
   11415              :                 }
   11416              :             }
   11417           48 :           break;
   11418              : 
   11419            0 :         default:
   11420            0 :           gcc_unreachable ();
   11421         8172 :           break;
   11422              :         }
   11423              :     }
   11424        21993 :   seen_derived_types.remove (der_type);
   11425              : 
   11426        21993 :   return gfc_finish_block (&fnblock);
   11427              : }
   11428              : 
   11429              : /* Recursively traverse an object of derived type, generating code to
   11430              :    nullify allocatable components.  */
   11431              : 
   11432              : tree
   11433         3168 : gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
   11434              :                         int caf_mode)
   11435              : {
   11436         3168 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11437              :                                 NULLIFY_ALLOC_COMP,
   11438              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
   11439         3168 :                                 NULL);
   11440              : }
   11441              : 
   11442              : 
   11443              : /* Recursively traverse an object of derived type, generating code to
   11444              :    deallocate allocatable components.  */
   11445              : 
   11446              : tree
   11447         3182 : gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank,
   11448              :                            int caf_mode, bool no_finalization)
   11449              : {
   11450         3182 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11451              :                                 DEALLOCATE_ALLOC_COMP,
   11452              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY | caf_mode,
   11453         3182 :                                 NULL, no_finalization);
   11454              : }
   11455              : 
   11456              : tree
   11457            1 : gfc_bcast_alloc_comp (gfc_symbol *derived, gfc_expr *expr, int rank,
   11458              :                       tree image_index, tree stat, tree errmsg,
   11459              :                       tree errmsg_len)
   11460              : {
   11461            1 :   tree tmp, array;
   11462            1 :   gfc_se argse;
   11463            1 :   stmtblock_t block, post_block;
   11464            1 :   gfc_co_subroutines_args args;
   11465              : 
   11466            1 :   args.image_index = image_index;
   11467            1 :   args.stat = stat;
   11468            1 :   args.errmsg = errmsg;
   11469            1 :   args.errmsg_len = errmsg_len;
   11470              : 
   11471            1 :   if (rank == 0)
   11472              :     {
   11473            1 :       gfc_start_block (&block);
   11474            1 :       gfc_init_block (&post_block);
   11475            1 :       gfc_init_se (&argse, NULL);
   11476            1 :       gfc_conv_expr (&argse, expr);
   11477            1 :       gfc_add_block_to_block (&block, &argse.pre);
   11478            1 :       gfc_add_block_to_block (&post_block, &argse.post);
   11479            1 :       array = argse.expr;
   11480              :     }
   11481              :   else
   11482              :     {
   11483            0 :       gfc_init_se (&argse, NULL);
   11484            0 :       argse.want_pointer = 1;
   11485            0 :       gfc_conv_expr_descriptor (&argse, expr);
   11486            0 :       array = argse.expr;
   11487              :     }
   11488              : 
   11489            1 :   tmp = structure_alloc_comps (derived, array, NULL_TREE, rank,
   11490              :                                BCAST_ALLOC_COMP,
   11491              :                                GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
   11492              :                                &args);
   11493            1 :   return tmp;
   11494              : }
   11495              : 
   11496              : /* Recursively traverse an object of derived type, generating code to
   11497              :    deallocate allocatable components.  But do not deallocate coarrays.
   11498              :    To be used for intrinsic assignment, which may not change the allocation
   11499              :    status of coarrays.  */
   11500              : 
   11501              : tree
   11502         3551 : gfc_deallocate_alloc_comp_no_caf (gfc_symbol * der_type, tree decl, int rank,
   11503              :                                   bool no_finalization)
   11504              : {
   11505         3551 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11506              :                                 DEALLOCATE_ALLOC_COMP, 0, NULL,
   11507         3551 :                                 no_finalization);
   11508              : }
   11509              : 
   11510              : 
   11511              : tree
   11512            5 : gfc_reassign_alloc_comp_caf (gfc_symbol *der_type, tree decl, tree dest)
   11513              : {
   11514            5 :   return structure_alloc_comps (der_type, decl, dest, 0, REASSIGN_CAF_COMP,
   11515              :                                 GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY,
   11516            5 :                                 NULL);
   11517              : }
   11518              : 
   11519              : 
   11520              : /* Recursively traverse an object of derived type, generating code to
   11521              :    copy it and its allocatable components.  */
   11522              : 
   11523              : tree
   11524         4634 : gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank,
   11525              :                      int caf_mode)
   11526              : {
   11527         4634 :   return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
   11528         4634 :                                 caf_mode, NULL);
   11529              : }
   11530              : 
   11531              : 
   11532              : /* Recursively traverse an object of derived type, generating code to
   11533              :    copy it and its allocatable components, while suppressing any
   11534              :    finalization that might occur.  This is used in the finalization of
   11535              :    function results.  */
   11536              : 
   11537              : tree
   11538           38 : gfc_copy_alloc_comp_no_fini (gfc_symbol * der_type, tree decl, tree dest,
   11539              :                              int rank, int caf_mode)
   11540              : {
   11541           38 :   return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP,
   11542           38 :                                 caf_mode, NULL, true);
   11543              : }
   11544              : 
   11545              : 
   11546              : /* Recursively traverse an object of derived type, generating code to
   11547              :    copy only its allocatable components.  */
   11548              : 
   11549              : tree
   11550            0 : gfc_copy_only_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
   11551              : {
   11552            0 :   return structure_alloc_comps (der_type, decl, dest, rank,
   11553            0 :                                 COPY_ONLY_ALLOC_COMP, 0, NULL);
   11554              : }
   11555              : 
   11556              : 
   11557              : /* Recursively traverse an object of parameterized derived type, generating
   11558              :    code to allocate parameterized components.  */
   11559              : 
   11560              : tree
   11561          795 : gfc_allocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank,
   11562              :                        gfc_actual_arglist *param_list)
   11563              : {
   11564          795 :   tree res;
   11565          795 :   gfc_actual_arglist *old_param_list = pdt_param_list;
   11566          795 :   pdt_param_list = param_list;
   11567          795 :   res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11568              :                                ALLOCATE_PDT_COMP, 0, NULL);
   11569          795 :   pdt_param_list = old_param_list;
   11570          795 :   return res;
   11571              : }
   11572              : 
   11573              : /* Recursively traverse an object of parameterized derived type, generating
   11574              :    code to deallocate parameterized components.  */
   11575              : 
   11576              : tree
   11577         1430 : gfc_deallocate_pdt_comp (gfc_symbol * der_type, tree decl, int rank)
   11578              : {
   11579              :   /* A type without parameterized components causes gimplifier problems.  */
   11580         1430 :   if (!has_parameterized_comps (der_type))
   11581              :     return NULL_TREE;
   11582              : 
   11583          613 :   return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11584          613 :                                 DEALLOCATE_PDT_COMP, 0, NULL);
   11585              : }
   11586              : 
   11587              : 
   11588              : /* Recursively traverse a dummy of parameterized derived type to check the
   11589              :    values of LEN parameters.  */
   11590              : 
   11591              : tree
   11592           80 : gfc_check_pdt_dummy (gfc_symbol * der_type, tree decl, int rank,
   11593              :                      gfc_actual_arglist *param_list)
   11594              : {
   11595           80 :   tree res;
   11596           80 :   gfc_actual_arglist *old_param_list = pdt_param_list;
   11597           80 :   pdt_param_list = param_list;
   11598           80 :   res = structure_alloc_comps (der_type, decl, NULL_TREE, rank,
   11599              :                                CHECK_PDT_DUMMY, 0, NULL);
   11600           80 :   pdt_param_list = old_param_list;
   11601           80 :   return res;
   11602              : }
   11603              : 
   11604              : 
   11605              : /* Returns the value of LBOUND for an expression.  This could be broken out
   11606              :    from gfc_conv_intrinsic_bound but this seemed to be simpler.  This is
   11607              :    called by gfc_alloc_allocatable_for_assignment.  */
   11608              : static tree
   11609         1090 : get_std_lbound (gfc_expr *expr, tree desc, int dim, bool assumed_size)
   11610              : {
   11611         1090 :   tree lbound;
   11612         1090 :   tree ubound;
   11613         1090 :   tree stride;
   11614         1090 :   tree cond, cond1, cond3, cond4;
   11615         1090 :   tree tmp;
   11616         1090 :   gfc_ref *ref;
   11617              : 
   11618         1090 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   11619              :     {
   11620          508 :       tmp = gfc_rank_cst[dim];
   11621          508 :       lbound = gfc_conv_descriptor_lbound_get (desc, tmp);
   11622          508 :       ubound = gfc_conv_descriptor_ubound_get (desc, tmp);
   11623          508 :       stride = gfc_conv_descriptor_stride_get (desc, tmp);
   11624          508 :       cond1 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11625              :                                ubound, lbound);
   11626          508 :       cond3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
   11627              :                                stride, gfc_index_zero_node);
   11628          508 :       cond3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
   11629              :                                logical_type_node, cond3, cond1);
   11630          508 :       cond4 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   11631              :                                stride, gfc_index_zero_node);
   11632          508 :       if (assumed_size)
   11633            0 :         cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11634              :                                 tmp, build_int_cst (gfc_array_index_type,
   11635            0 :                                                     expr->rank - 1));
   11636              :       else
   11637          508 :         cond = logical_false_node;
   11638              : 
   11639          508 :       cond1 = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11640              :                                logical_type_node, cond3, cond4);
   11641          508 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11642              :                               logical_type_node, cond, cond1);
   11643              : 
   11644          508 :       return fold_build3_loc (input_location, COND_EXPR,
   11645              :                               gfc_array_index_type, cond,
   11646          508 :                               lbound, gfc_index_one_node);
   11647              :     }
   11648              : 
   11649          582 :   if (expr->expr_type == EXPR_FUNCTION)
   11650              :     {
   11651              :       /* A conversion function, so use the argument.  */
   11652            7 :       gcc_assert (expr->value.function.isym
   11653              :                   && expr->value.function.isym->conversion);
   11654            7 :       expr = expr->value.function.actual->expr;
   11655              :     }
   11656              : 
   11657          582 :   if (expr->expr_type == EXPR_VARIABLE)
   11658              :     {
   11659          582 :       tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
   11660         1508 :       for (ref = expr->ref; ref; ref = ref->next)
   11661              :         {
   11662          926 :           if (ref->type == REF_COMPONENT
   11663          295 :                 && ref->u.c.component->as
   11664          246 :                 && ref->next
   11665          246 :                 && ref->next->u.ar.type == AR_FULL)
   11666          204 :             tmp = TREE_TYPE (ref->u.c.component->backend_decl);
   11667              :         }
   11668          582 :       return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
   11669              :     }
   11670              : 
   11671            0 :   return gfc_index_one_node;
   11672              : }
   11673              : 
   11674              : 
   11675              : /* Returns true if an expression represents an lhs that can be reallocated
   11676              :    on assignment.  */
   11677              : 
   11678              : bool
   11679       641174 : gfc_is_reallocatable_lhs (gfc_expr *expr)
   11680              : {
   11681       641174 :   gfc_ref * ref;
   11682       641174 :   gfc_symbol *sym;
   11683              : 
   11684       641174 :   if (!flag_realloc_lhs)
   11685              :     return false;
   11686              : 
   11687       640674 :   if (!expr->ref)
   11688              :     return false;
   11689              : 
   11690       216275 :   sym = expr->symtree->n.sym;
   11691              : 
   11692       216275 :   if (sym->attr.associate_var && !expr->ref)
   11693              :     return false;
   11694              : 
   11695              :   /* An allocatable class variable with no reference.  */
   11696       216275 :   if (sym->ts.type == BT_CLASS
   11697         6721 :       && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
   11698         6561 :       && CLASS_DATA (sym)->attr.allocatable
   11699              :       && expr->ref
   11700         4023 :       && ((expr->ref->type == REF_ARRAY && expr->ref->u.ar.type == AR_FULL
   11701          703 :            && expr->ref->next == NULL)
   11702         3393 :           || (expr->ref->type == REF_COMPONENT
   11703         3052 :               && strcmp (expr->ref->u.c.component->name, "_data") == 0
   11704         2171 :               && (expr->ref->next == NULL
   11705         2171 :                   || (expr->ref->next->type == REF_ARRAY
   11706         2171 :                       && expr->ref->next->u.ar.type == AR_FULL
   11707         1749 :                       && expr->ref->next->next == NULL)))))
   11708              :     return true;
   11709              : 
   11710              :   /* An allocatable variable.  */
   11711       214036 :   if (sym->attr.allocatable
   11712        46812 :       && (!sym->attr.associate_var || sym->attr.select_rank_temporary)
   11713              :       && expr->ref
   11714        46812 :       && expr->ref->type == REF_ARRAY
   11715        45317 :       && expr->ref->u.ar.type == AR_FULL)
   11716              :     return true;
   11717              : 
   11718              :   /* All that can be left are allocatable components.  */
   11719       186020 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
   11720              :     return false;
   11721              : 
   11722              :   /* Find a component ref followed by an array reference.  */
   11723        92459 :   for (ref = expr->ref; ref; ref = ref->next)
   11724        64780 :     if (ref->next
   11725        37101 :           && ref->type == REF_COMPONENT
   11726        21160 :           && ref->next->type == REF_ARRAY
   11727        17437 :           && !ref->next->next)
   11728              :       break;
   11729              : 
   11730        40811 :   if (!ref)
   11731              :     return false;
   11732              : 
   11733              :   /* Return true if valid reallocatable lhs.  */
   11734        13132 :   if (ref->u.c.component->attr.allocatable
   11735         6600 :         && ref->next->u.ar.type == AR_FULL)
   11736         4950 :     return true;
   11737              : 
   11738              :   return false;
   11739              : }
   11740              : 
   11741              : 
   11742              : static tree
   11743           56 : concat_str_length (gfc_expr* expr)
   11744              : {
   11745           56 :   tree type;
   11746           56 :   tree len1;
   11747           56 :   tree len2;
   11748           56 :   gfc_se se;
   11749              : 
   11750           56 :   type = gfc_typenode_for_spec (&expr->value.op.op1->ts);
   11751           56 :   len1 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   11752           56 :   if (len1 == NULL_TREE)
   11753              :     {
   11754           56 :       if (expr->value.op.op1->expr_type == EXPR_OP)
   11755           31 :         len1 = concat_str_length (expr->value.op.op1);
   11756           25 :       else if (expr->value.op.op1->expr_type == EXPR_CONSTANT)
   11757           25 :         len1 = build_int_cst (gfc_charlen_type_node,
   11758           25 :                         expr->value.op.op1->value.character.length);
   11759            0 :       else if (expr->value.op.op1->ts.u.cl->length)
   11760              :         {
   11761            0 :           gfc_init_se (&se, NULL);
   11762            0 :           gfc_conv_expr (&se, expr->value.op.op1->ts.u.cl->length);
   11763            0 :           len1 = se.expr;
   11764              :         }
   11765              :       else
   11766              :         {
   11767              :           /* Last resort!  */
   11768            0 :           gfc_init_se (&se, NULL);
   11769            0 :           se.want_pointer = 1;
   11770            0 :           se.descriptor_only = 1;
   11771            0 :           gfc_conv_expr (&se, expr->value.op.op1);
   11772            0 :           len1 = se.string_length;
   11773              :         }
   11774              :     }
   11775              : 
   11776           56 :   type = gfc_typenode_for_spec (&expr->value.op.op2->ts);
   11777           56 :   len2 = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
   11778           56 :   if (len2 == NULL_TREE)
   11779              :     {
   11780           31 :       if (expr->value.op.op2->expr_type == EXPR_OP)
   11781            0 :         len2 = concat_str_length (expr->value.op.op2);
   11782           31 :       else if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
   11783           25 :         len2 = build_int_cst (gfc_charlen_type_node,
   11784           25 :                         expr->value.op.op2->value.character.length);
   11785            6 :       else if (expr->value.op.op2->ts.u.cl->length)
   11786              :         {
   11787            6 :           gfc_init_se (&se, NULL);
   11788            6 :           gfc_conv_expr (&se, expr->value.op.op2->ts.u.cl->length);
   11789            6 :           len2 = se.expr;
   11790              :         }
   11791              :       else
   11792              :         {
   11793              :           /* Last resort!  */
   11794            0 :           gfc_init_se (&se, NULL);
   11795            0 :           se.want_pointer = 1;
   11796            0 :           se.descriptor_only = 1;
   11797            0 :           gfc_conv_expr (&se, expr->value.op.op2);
   11798            0 :           len2 = se.string_length;
   11799              :         }
   11800              :     }
   11801              : 
   11802           56 :   gcc_assert(len1 && len2);
   11803           56 :   len1 = fold_convert (gfc_charlen_type_node, len1);
   11804           56 :   len2 = fold_convert (gfc_charlen_type_node, len2);
   11805              : 
   11806           56 :   return fold_build2_loc (input_location, PLUS_EXPR,
   11807           56 :                           gfc_charlen_type_node, len1, len2);
   11808              : }
   11809              : 
   11810              : 
   11811              : /* Among the scalarization chain of LOOP, find the element associated with an
   11812              :    allocatable array on the lhs of an assignment and evaluate its fields
   11813              :    (bounds, offset, etc) to new variables, putting the new code in BLOCK.  This
   11814              :    function is to be called after putting the reallocation code in BLOCK and
   11815              :    before the beginning of the scalarization loop body.
   11816              : 
   11817              :    The fields to be saved are expected to hold on entry to the function
   11818              :    expressions referencing the array descriptor.  Especially the expressions
   11819              :    shouldn't be already temporary variable references as the value saved before
   11820              :    reallocation would be incorrect after reallocation.
   11821              :    At the end of the function, the expressions have been replaced with variable
   11822              :    references.  */
   11823              : 
   11824              : static void
   11825         6782 : update_reallocated_descriptor (stmtblock_t *block, gfc_loopinfo *loop)
   11826              : {
   11827        23614 :   for (gfc_ss *s = loop->ss; s != gfc_ss_terminator; s = s->loop_chain)
   11828              :     {
   11829        16832 :       if (!s->is_alloc_lhs)
   11830        10050 :         continue;
   11831              : 
   11832         6782 :       gcc_assert (s->info->type == GFC_SS_SECTION);
   11833         6782 :       gfc_array_info *info = &s->info->data.array;
   11834              : 
   11835              : #define SAVE_VALUE(value) \
   11836              :               do \
   11837              :                 { \
   11838              :                   value = gfc_evaluate_now (value, block); \
   11839              :                 } \
   11840              :               while (0)
   11841              : 
   11842         6782 :       if (save_descriptor_data (info->descriptor, info->data))
   11843         5942 :         SAVE_VALUE (info->data);
   11844         6782 :       SAVE_VALUE (info->offset);
   11845         6782 :       info->saved_offset = info->offset;
   11846        16775 :       for (int i = 0; i < s->dimen; i++)
   11847              :         {
   11848         9993 :           int dim = s->dim[i];
   11849         9993 :           SAVE_VALUE (info->start[dim]);
   11850         9993 :           SAVE_VALUE (info->end[dim]);
   11851         9993 :           SAVE_VALUE (info->stride[dim]);
   11852         9993 :           SAVE_VALUE (info->delta[dim]);
   11853              :         }
   11854              : 
   11855              : #undef SAVE_VALUE
   11856              :     }
   11857         6782 : }
   11858              : 
   11859              : 
   11860              : /* Allocate the lhs of an assignment to an allocatable array, otherwise
   11861              :    reallocate it.  */
   11862              : 
   11863              : tree
   11864         6782 : gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
   11865              :                                       gfc_expr *expr1,
   11866              :                                       gfc_expr *expr2)
   11867              : {
   11868         6782 :   stmtblock_t realloc_block;
   11869         6782 :   stmtblock_t alloc_block;
   11870         6782 :   stmtblock_t fblock;
   11871         6782 :   stmtblock_t loop_pre_block;
   11872         6782 :   gfc_ref *ref;
   11873         6782 :   gfc_ss *rss;
   11874         6782 :   gfc_ss *lss;
   11875         6782 :   gfc_array_info *linfo;
   11876         6782 :   tree realloc_expr;
   11877         6782 :   tree alloc_expr;
   11878         6782 :   tree size1;
   11879         6782 :   tree size2;
   11880         6782 :   tree elemsize1;
   11881         6782 :   tree elemsize2;
   11882         6782 :   tree array1;
   11883         6782 :   tree cond_null;
   11884         6782 :   tree cond;
   11885         6782 :   tree tmp;
   11886         6782 :   tree tmp2;
   11887         6782 :   tree lbound;
   11888         6782 :   tree ubound;
   11889         6782 :   tree desc;
   11890         6782 :   tree old_desc;
   11891         6782 :   tree desc2;
   11892         6782 :   tree offset;
   11893         6782 :   tree jump_label1;
   11894         6782 :   tree jump_label2;
   11895         6782 :   tree lbd;
   11896         6782 :   tree class_expr2 = NULL_TREE;
   11897         6782 :   int n;
   11898         6782 :   gfc_array_spec * as;
   11899         6782 :   bool coarray = (flag_coarray == GFC_FCOARRAY_LIB
   11900         6782 :                   && gfc_caf_attr (expr1, true).codimension);
   11901         6782 :   tree token;
   11902         6782 :   gfc_se caf_se;
   11903              : 
   11904              :   /* x = f(...) with x allocatable.  In this case, expr1 is the rhs.
   11905              :      Find the lhs expression in the loop chain and set expr1 and
   11906              :      expr2 accordingly.  */
   11907         6782 :   if (expr1->expr_type == EXPR_FUNCTION && expr2 == NULL)
   11908              :     {
   11909          203 :       expr2 = expr1;
   11910              :       /* Find the ss for the lhs.  */
   11911          203 :       lss = loop->ss;
   11912          406 :       for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
   11913          406 :         if (lss->info->expr && lss->info->expr->expr_type == EXPR_VARIABLE)
   11914              :           break;
   11915          203 :       if (lss == gfc_ss_terminator)
   11916              :         return NULL_TREE;
   11917          203 :       expr1 = lss->info->expr;
   11918              :     }
   11919              : 
   11920              :   /* Bail out if this is not a valid allocate on assignment.  */
   11921         6782 :   if (!gfc_is_reallocatable_lhs (expr1)
   11922         6782 :         || (expr2 && !expr2->rank))
   11923              :     return NULL_TREE;
   11924              : 
   11925              :   /* Find the ss for the lhs.  */
   11926         6782 :   lss = loop->ss;
   11927        16832 :   for (; lss && lss != gfc_ss_terminator; lss = lss->loop_chain)
   11928        16832 :     if (lss->info->expr == expr1)
   11929              :       break;
   11930              : 
   11931         6782 :   if (lss == gfc_ss_terminator)
   11932              :     return NULL_TREE;
   11933              : 
   11934         6782 :   linfo = &lss->info->data.array;
   11935              : 
   11936              :   /* Find an ss for the rhs. For operator expressions, we see the
   11937              :      ss's for the operands. Any one of these will do.  */
   11938         6782 :   rss = loop->ss;
   11939         7386 :   for (; rss && rss != gfc_ss_terminator; rss = rss->loop_chain)
   11940         7386 :     if (rss->info->expr != expr1 && rss != loop->temp_ss)
   11941              :       break;
   11942              : 
   11943         6782 :   if (expr2 && rss == gfc_ss_terminator)
   11944              :     return NULL_TREE;
   11945              : 
   11946              :   /* Ensure that the string length from the current scope is used.  */
   11947         6782 :   if (expr2->ts.type == BT_CHARACTER
   11948         1013 :       && expr2->expr_type == EXPR_FUNCTION
   11949          130 :       && !expr2->value.function.isym)
   11950           21 :     expr2->ts.u.cl->backend_decl = rss->info->string_length;
   11951              : 
   11952              :   /* Since the lhs is allocatable, this must be a descriptor type.
   11953              :      Get the data and array size.  */
   11954         6782 :   desc = linfo->descriptor;
   11955         6782 :   gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)));
   11956         6782 :   array1 = gfc_conv_descriptor_data_get (desc);
   11957              : 
   11958              :   /* If the data is null, set the descriptor bounds and offset.  This suppresses
   11959              :      the maybe used uninitialized warning.  Note that the always false variable
   11960              :      prevents this block from ever being executed, and makes sure that the
   11961              :      optimizers are able to remove it.  Component references are not subject to
   11962              :      the warnings, so we don't uselessly complicate the generated code for them.
   11963              :      */
   11964        12074 :   for (ref = expr1->ref; ref; ref = ref->next)
   11965         7019 :     if (ref->type == REF_COMPONENT)
   11966              :       break;
   11967              : 
   11968         6782 :   if (!ref)
   11969              :     {
   11970         5055 :       stmtblock_t unalloc_init_block;
   11971         5055 :       gfc_init_block (&unalloc_init_block);
   11972         5055 :       tree guard = gfc_create_var (logical_type_node, "unallocated_init_guard");
   11973         5055 :       gfc_add_modify (&unalloc_init_block, guard, logical_false_node);
   11974              : 
   11975         5055 :       gfc_start_block (&loop_pre_block);
   11976        18019 :       for (n = 0; n < expr1->rank; n++)
   11977              :         {
   11978         7909 :           gfc_conv_descriptor_lbound_set (&loop_pre_block, desc,
   11979              :                                           gfc_rank_cst[n],
   11980              :                                           gfc_index_one_node);
   11981         7909 :           gfc_conv_descriptor_ubound_set (&loop_pre_block, desc,
   11982              :                                           gfc_rank_cst[n],
   11983              :                                           gfc_index_zero_node);
   11984         7909 :           gfc_conv_descriptor_stride_set (&loop_pre_block, desc,
   11985              :                                           gfc_rank_cst[n],
   11986              :                                           gfc_index_zero_node);
   11987              :         }
   11988              : 
   11989         5055 :       gfc_conv_descriptor_offset_set (&loop_pre_block, desc,
   11990              :                                       gfc_index_zero_node);
   11991              : 
   11992         5055 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
   11993              :                              logical_type_node, array1,
   11994         5055 :                              build_int_cst (TREE_TYPE (array1), 0));
   11995         5055 :       tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
   11996              :                              logical_type_node, tmp, guard);
   11997         5055 :       tmp = build3_v (COND_EXPR, tmp,
   11998              :                       gfc_finish_block (&loop_pre_block),
   11999              :                       build_empty_stmt (input_location));
   12000         5055 :       gfc_prepend_expr_to_block (&loop->pre, tmp);
   12001         5055 :       gfc_prepend_expr_to_block (&loop->pre,
   12002              :                                  gfc_finish_block (&unalloc_init_block));
   12003              :     }
   12004              : 
   12005         6782 :   gfc_start_block (&fblock);
   12006              : 
   12007         6782 :   if (expr2)
   12008         6782 :     desc2 = rss->info->data.array.descriptor;
   12009              :   else
   12010              :     desc2 = NULL_TREE;
   12011              : 
   12012              :   /* Get the old lhs element size for deferred character and class expr1.  */
   12013         6782 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12014              :     {
   12015          687 :       if (expr1->ts.u.cl->backend_decl
   12016          687 :           && VAR_P (expr1->ts.u.cl->backend_decl))
   12017              :         elemsize1 = expr1->ts.u.cl->backend_decl;
   12018              :       else
   12019           70 :         elemsize1 = lss->info->string_length;
   12020          687 :       tree unit_size = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
   12021         1374 :       elemsize1 = fold_build2_loc (input_location, MULT_EXPR,
   12022          687 :                                    TREE_TYPE (elemsize1), elemsize1,
   12023          687 :                                    fold_convert (TREE_TYPE (elemsize1), unit_size));
   12024              : 
   12025          687 :     }
   12026         6095 :   else if (expr1->ts.type == BT_CLASS)
   12027              :     {
   12028              :       /* Unfortunately, the lhs vptr is set too early in many cases.
   12029              :          Play it safe by using the descriptor element length.  */
   12030          669 :       tmp = gfc_conv_descriptor_elem_len_get (desc);
   12031          669 :       elemsize1 = fold_convert (gfc_array_index_type, tmp);
   12032              :     }
   12033              :   else
   12034              :     elemsize1 = NULL_TREE;
   12035         1356 :   if (elemsize1 != NULL_TREE)
   12036         1356 :     elemsize1 = gfc_evaluate_now (elemsize1, &fblock);
   12037              : 
   12038              :   /* Get the new lhs size in bytes.  */
   12039         6782 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12040              :     {
   12041          687 :       if (expr2->ts.deferred)
   12042              :         {
   12043          183 :           if (expr2->ts.u.cl->backend_decl
   12044          183 :               && VAR_P (expr2->ts.u.cl->backend_decl))
   12045              :             tmp = expr2->ts.u.cl->backend_decl;
   12046              :           else
   12047            0 :             tmp = rss->info->string_length;
   12048              :         }
   12049              :       else
   12050              :         {
   12051          504 :           tmp = expr2->ts.u.cl->backend_decl;
   12052          504 :           if (!tmp && expr2->expr_type == EXPR_OP
   12053           25 :               && expr2->value.op.op == INTRINSIC_CONCAT)
   12054              :             {
   12055           25 :               tmp = concat_str_length (expr2);
   12056           25 :               expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
   12057              :             }
   12058           12 :           else if (!tmp && expr2->ts.u.cl->length)
   12059              :             {
   12060           12 :               gfc_se tmpse;
   12061           12 :               gfc_init_se (&tmpse, NULL);
   12062           12 :               gfc_conv_expr_type (&tmpse, expr2->ts.u.cl->length,
   12063              :                                   gfc_charlen_type_node);
   12064           12 :               tmp = tmpse.expr;
   12065           12 :               expr2->ts.u.cl->backend_decl = gfc_evaluate_now (tmp, &fblock);
   12066              :             }
   12067          504 :           tmp = fold_convert (TREE_TYPE (expr1->ts.u.cl->backend_decl), tmp);
   12068              :         }
   12069              : 
   12070          687 :       if (expr1->ts.u.cl->backend_decl
   12071          687 :           && VAR_P (expr1->ts.u.cl->backend_decl))
   12072          617 :         gfc_add_modify (&fblock, expr1->ts.u.cl->backend_decl, tmp);
   12073              :       else
   12074           70 :         gfc_add_modify (&fblock, lss->info->string_length, tmp);
   12075              : 
   12076          687 :       if (expr1->ts.kind > 1)
   12077           12 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
   12078            6 :                                TREE_TYPE (tmp),
   12079            6 :                                tmp, build_int_cst (TREE_TYPE (tmp),
   12080            6 :                                                    expr1->ts.kind));
   12081              :     }
   12082         6095 :   else if (expr1->ts.type == BT_CHARACTER && expr1->ts.u.cl->backend_decl)
   12083              :     {
   12084          277 :       tmp = TYPE_SIZE_UNIT (TREE_TYPE (gfc_typenode_for_spec (&expr1->ts)));
   12085          277 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12086              :                              fold_convert (gfc_array_index_type, tmp),
   12087          277 :                              expr1->ts.u.cl->backend_decl);
   12088              :     }
   12089         5818 :   else if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
   12090          164 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12091         5654 :   else if (expr1->ts.type == BT_CLASS && expr2->ts.type == BT_CLASS)
   12092              :     {
   12093          298 :       tmp = expr2->rank ? gfc_get_class_from_expr (desc2) : NULL_TREE;
   12094          298 :       if (tmp == NULL_TREE && expr2->expr_type == EXPR_VARIABLE)
   12095           54 :         tmp = class_expr2 = gfc_get_class_from_gfc_expr (expr2);
   12096              : 
   12097           61 :       if (tmp != NULL_TREE)
   12098          291 :         tmp = gfc_class_vtab_size_get (tmp);
   12099              :       else
   12100            7 :         tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
   12101              :     }
   12102              :   else
   12103         5356 :     tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12104         6782 :   elemsize2 = fold_convert (gfc_array_index_type, tmp);
   12105         6782 :   elemsize2 = gfc_evaluate_now (elemsize2, &fblock);
   12106              : 
   12107              :   /* 7.4.1.3 "If variable is an allocated allocatable variable, it is
   12108              :      deallocated if expr is an array of different shape or any of the
   12109              :      corresponding length type parameter values of variable and expr
   12110              :      differ."  This assures F95 compatibility.  */
   12111         6782 :   jump_label1 = gfc_build_label_decl (NULL_TREE);
   12112         6782 :   jump_label2 = gfc_build_label_decl (NULL_TREE);
   12113              : 
   12114              :   /* Allocate if data is NULL.  */
   12115         6782 :   cond_null = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   12116         6782 :                          array1, build_int_cst (TREE_TYPE (array1), 0));
   12117         6782 :   cond_null= gfc_evaluate_now (cond_null, &fblock);
   12118              : 
   12119         6782 :   tmp = build3_v (COND_EXPR, cond_null,
   12120              :                   build1_v (GOTO_EXPR, jump_label1),
   12121              :                   build_empty_stmt (input_location));
   12122         6782 :   gfc_add_expr_to_block (&fblock, tmp);
   12123              : 
   12124              :   /* Get arrayspec if expr is a full array.  */
   12125         6782 :   if (expr2 && expr2->expr_type == EXPR_FUNCTION
   12126         2814 :         && expr2->value.function.isym
   12127         2295 :         && expr2->value.function.isym->conversion)
   12128              :     {
   12129              :       /* For conversion functions, take the arg.  */
   12130          245 :       gfc_expr *arg = expr2->value.function.actual->expr;
   12131          245 :       as = gfc_get_full_arrayspec_from_expr (arg);
   12132          245 :     }
   12133              :   else if (expr2)
   12134         6537 :     as = gfc_get_full_arrayspec_from_expr (expr2);
   12135              :   else
   12136              :     as = NULL;
   12137              : 
   12138              :   /* If the lhs shape is not the same as the rhs jump to setting the
   12139              :      bounds and doing the reallocation.......  */
   12140        16775 :   for (n = 0; n < expr1->rank; n++)
   12141              :     {
   12142              :       /* Check the shape.  */
   12143         9993 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12144         9993 :       ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
   12145         9993 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12146              :                              gfc_array_index_type,
   12147              :                              loop->to[n], loop->from[n]);
   12148         9993 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12149              :                              gfc_array_index_type,
   12150              :                              tmp, lbound);
   12151         9993 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12152              :                              gfc_array_index_type,
   12153              :                              tmp, ubound);
   12154         9993 :       cond = fold_build2_loc (input_location, NE_EXPR,
   12155              :                               logical_type_node,
   12156              :                               tmp, gfc_index_zero_node);
   12157         9993 :       tmp = build3_v (COND_EXPR, cond,
   12158              :                       build1_v (GOTO_EXPR, jump_label1),
   12159              :                       build_empty_stmt (input_location));
   12160         9993 :       gfc_add_expr_to_block (&fblock, tmp);
   12161              :     }
   12162              : 
   12163              :   /* ...else if the element lengths are not the same also go to
   12164              :      setting the bounds and doing the reallocation.... */
   12165         6782 :   if (elemsize1 != NULL_TREE)
   12166              :     {
   12167         1356 :       cond = fold_build2_loc (input_location, NE_EXPR,
   12168              :                               logical_type_node,
   12169              :                               elemsize1, elemsize2);
   12170         1356 :       tmp = build3_v (COND_EXPR, cond,
   12171              :                       build1_v (GOTO_EXPR, jump_label1),
   12172              :                       build_empty_stmt (input_location));
   12173         1356 :       gfc_add_expr_to_block (&fblock, tmp);
   12174              :     }
   12175              : 
   12176              :   /* ....else jump past the (re)alloc code.  */
   12177         6782 :   tmp = build1_v (GOTO_EXPR, jump_label2);
   12178         6782 :   gfc_add_expr_to_block (&fblock, tmp);
   12179              : 
   12180              :   /* Add the label to start automatic (re)allocation.  */
   12181         6782 :   tmp = build1_v (LABEL_EXPR, jump_label1);
   12182         6782 :   gfc_add_expr_to_block (&fblock, tmp);
   12183              : 
   12184              :   /* Get the rhs size and fix it.  */
   12185         6782 :   size2 = gfc_index_one_node;
   12186        16775 :   for (n = 0; n < expr2->rank; n++)
   12187              :     {
   12188         9993 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12189              :                              gfc_array_index_type,
   12190              :                              loop->to[n], loop->from[n]);
   12191         9993 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12192              :                              gfc_array_index_type,
   12193              :                              tmp, gfc_index_one_node);
   12194         9993 :       size2 = fold_build2_loc (input_location, MULT_EXPR,
   12195              :                                gfc_array_index_type,
   12196              :                                tmp, size2);
   12197              :     }
   12198         6782 :   size2 = gfc_evaluate_now (size2, &fblock);
   12199              : 
   12200              :   /* Deallocation of allocatable components will have to occur on
   12201              :      reallocation.  Fix the old descriptor now.  */
   12202         6782 :   if ((expr1->ts.type == BT_DERIVED)
   12203          441 :         && expr1->ts.u.derived->attr.alloc_comp)
   12204          200 :     old_desc = gfc_evaluate_now (desc, &fblock);
   12205              :   else
   12206              :     old_desc = NULL_TREE;
   12207              : 
   12208              :   /* Now modify the lhs descriptor and the associated scalarizer
   12209              :      variables. F2003 7.4.1.3: "If variable is or becomes an
   12210              :      unallocated allocatable variable, then it is allocated with each
   12211              :      deferred type parameter equal to the corresponding type parameters
   12212              :      of expr , with the shape of expr , and with each lower bound equal
   12213              :      to the corresponding element of LBOUND(expr)."
   12214              :      Reuse size1 to keep a dimension-by-dimension track of the
   12215              :      stride of the new array.  */
   12216         6782 :   size1 = gfc_index_one_node;
   12217         6782 :   offset = gfc_index_zero_node;
   12218              : 
   12219        16775 :   for (n = 0; n < expr2->rank; n++)
   12220              :     {
   12221         9993 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12222              :                              gfc_array_index_type,
   12223              :                              loop->to[n], loop->from[n]);
   12224         9993 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12225              :                              gfc_array_index_type,
   12226              :                              tmp, gfc_index_one_node);
   12227              : 
   12228         9993 :       lbound = gfc_index_one_node;
   12229         9993 :       ubound = tmp;
   12230              : 
   12231         9993 :       if (as)
   12232              :         {
   12233         2180 :           lbd = get_std_lbound (expr2, desc2, n,
   12234         1090 :                                 as->type == AS_ASSUMED_SIZE);
   12235         1090 :           ubound = fold_build2_loc (input_location,
   12236              :                                     MINUS_EXPR,
   12237              :                                     gfc_array_index_type,
   12238              :                                     ubound, lbound);
   12239         1090 :           ubound = fold_build2_loc (input_location,
   12240              :                                     PLUS_EXPR,
   12241              :                                     gfc_array_index_type,
   12242              :                                     ubound, lbd);
   12243         1090 :           lbound = lbd;
   12244              :         }
   12245              : 
   12246         9993 :       gfc_conv_descriptor_lbound_set (&fblock, desc,
   12247              :                                       gfc_rank_cst[n],
   12248              :                                       lbound);
   12249         9993 :       gfc_conv_descriptor_ubound_set (&fblock, desc,
   12250              :                                       gfc_rank_cst[n],
   12251              :                                       ubound);
   12252         9993 :       gfc_conv_descriptor_stride_set (&fblock, desc,
   12253              :                                       gfc_rank_cst[n],
   12254              :                                       size1);
   12255         9993 :       lbound = gfc_conv_descriptor_lbound_get (desc,
   12256              :                                                gfc_rank_cst[n]);
   12257         9993 :       tmp2 = fold_build2_loc (input_location, MULT_EXPR,
   12258              :                               gfc_array_index_type,
   12259              :                               lbound, size1);
   12260         9993 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
   12261              :                                 gfc_array_index_type,
   12262              :                                 offset, tmp2);
   12263         9993 :       size1 = fold_build2_loc (input_location, MULT_EXPR,
   12264              :                                gfc_array_index_type,
   12265              :                                tmp, size1);
   12266              :     }
   12267              : 
   12268              :   /* Set the lhs descriptor and scalarizer offsets.  For rank > 1,
   12269              :      the array offset is saved and the info.offset is used for a
   12270              :      running offset.  Use the saved_offset instead.  */
   12271         6782 :   gfc_conv_descriptor_offset_set (&fblock, desc, offset);
   12272              : 
   12273              :   /* Take into account _len of unlimited polymorphic entities, so that span
   12274              :      for array descriptors and allocation sizes are computed correctly.  */
   12275         6782 :   if (UNLIMITED_POLY (expr2))
   12276              :     {
   12277          110 :       tree len = gfc_class_len_get (TREE_OPERAND (desc2, 0));
   12278          110 :       len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12279              :                              fold_convert (size_type_node, len),
   12280              :                              size_one_node);
   12281          110 :       elemsize2 = fold_build2_loc (input_location, MULT_EXPR,
   12282              :                                    gfc_array_index_type, elemsize2,
   12283              :                                    fold_convert (gfc_array_index_type, len));
   12284              :     }
   12285              : 
   12286         6782 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   12287         6782 :     gfc_conv_descriptor_span_set (&fblock, desc, elemsize2);
   12288              : 
   12289         6782 :   size2 = fold_build2_loc (input_location, MULT_EXPR,
   12290              :                            gfc_array_index_type,
   12291              :                            elemsize2, size2);
   12292         6782 :   size2 = fold_convert (size_type_node, size2);
   12293         6782 :   size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12294              :                            size2, size_one_node);
   12295         6782 :   size2 = gfc_evaluate_now (size2, &fblock);
   12296              : 
   12297              :   /* For deferred character length, the 'size' field of the dtype might
   12298              :      have changed so set the dtype.  */
   12299         6782 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
   12300         6782 :       && expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12301              :     {
   12302          687 :       tree type;
   12303          687 :       if (expr2->ts.u.cl->backend_decl)
   12304          687 :         type = gfc_typenode_for_spec (&expr2->ts);
   12305              :       else
   12306            0 :         type = gfc_typenode_for_spec (&expr1->ts);
   12307              : 
   12308          687 :       gfc_conv_descriptor_dtype_set (&fblock, desc,
   12309              :                                      gfc_get_dtype_rank_type (expr1->rank,
   12310              :                                                               type));
   12311              :     }
   12312         6095 :   else if (expr1->ts.type == BT_CLASS)
   12313              :     {
   12314          669 :       tree type;
   12315              : 
   12316          669 :       if (expr2->ts.type != BT_CLASS)
   12317          371 :         type = gfc_typenode_for_spec (&expr2->ts);
   12318              :       else
   12319          298 :         type = gfc_get_character_type_len (1, elemsize2);
   12320              : 
   12321          669 :       gfc_conv_descriptor_dtype_set (&fblock, desc,
   12322              :                                      gfc_get_dtype_rank_type (expr2->rank,
   12323              :                                                               type));
   12324              : 
   12325              :       /* Set the _len field as well...  */
   12326          669 :       if (UNLIMITED_POLY (expr1))
   12327              :         {
   12328          274 :           tmp = gfc_class_len_get (TREE_OPERAND (desc, 0));
   12329          274 :           if (expr2->ts.type == BT_CHARACTER)
   12330           49 :             gfc_add_modify (&fblock, tmp,
   12331           49 :                             fold_convert (TREE_TYPE (tmp),
   12332              :                                           TYPE_SIZE_UNIT (type)));
   12333          225 :           else if (UNLIMITED_POLY (expr2))
   12334          110 :             gfc_add_modify (&fblock, tmp,
   12335          110 :                             gfc_class_len_get (TREE_OPERAND (desc2, 0)));
   12336              :           else
   12337          115 :             gfc_add_modify (&fblock, tmp,
   12338          115 :                             build_int_cst (TREE_TYPE (tmp), 0));
   12339              :         }
   12340              :       /* ...and the vptr.  */
   12341          669 :       tmp = gfc_class_vptr_get (TREE_OPERAND (desc, 0));
   12342          669 :       if (expr2->ts.type == BT_CLASS && !VAR_P (desc2)
   12343          291 :           && TREE_CODE (desc2) == COMPONENT_REF)
   12344              :         {
   12345          237 :           tmp2 = gfc_get_class_from_expr (desc2);
   12346          237 :           tmp2 = gfc_class_vptr_get (tmp2);
   12347              :         }
   12348          432 :       else if (expr2->ts.type == BT_CLASS && class_expr2 != NULL_TREE)
   12349           54 :         tmp2 = gfc_class_vptr_get (class_expr2);
   12350              :       else
   12351              :         {
   12352          378 :           tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
   12353          378 :           tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
   12354              :         }
   12355              : 
   12356          669 :       gfc_add_modify (&fblock, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
   12357              :     }
   12358         5426 :   else if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
   12359           39 :     gfc_conv_descriptor_dtype_set (&fblock, desc,
   12360           39 :                                    gfc_get_dtype (TREE_TYPE (desc)));
   12361              : 
   12362              :   /* Realloc expression.  Note that the scalarizer uses desc.data
   12363              :      in the array reference - (*desc.data)[<element>].  */
   12364         6782 :   gfc_init_block (&realloc_block);
   12365         6782 :   gfc_init_se (&caf_se, NULL);
   12366              : 
   12367         6782 :   if (coarray)
   12368              :     {
   12369           39 :       token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&caf_se, expr1);
   12370           39 :       if (token == NULL_TREE)
   12371              :         {
   12372            9 :           tmp = gfc_get_tree_for_caf_expr (expr1);
   12373            9 :           if (POINTER_TYPE_P (TREE_TYPE (tmp)))
   12374            6 :             tmp = build_fold_indirect_ref (tmp);
   12375            9 :           gfc_get_caf_token_offset (&caf_se, &token, NULL, tmp, NULL_TREE,
   12376              :                                     expr1);
   12377            9 :           token = gfc_build_addr_expr (NULL_TREE, token);
   12378              :         }
   12379              : 
   12380           39 :       gfc_add_block_to_block (&realloc_block, &caf_se.pre);
   12381              :     }
   12382         6782 :   if ((expr1->ts.type == BT_DERIVED)
   12383          441 :         && expr1->ts.u.derived->attr.alloc_comp)
   12384              :     {
   12385          200 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, old_desc,
   12386              :                                               expr1->rank, true);
   12387          200 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12388              :     }
   12389              : 
   12390         6782 :   if (!coarray)
   12391              :     {
   12392         6743 :       tmp = build_call_expr_loc (input_location,
   12393              :                                  builtin_decl_explicit (BUILT_IN_REALLOC), 2,
   12394              :                                  fold_convert (pvoid_type_node, array1),
   12395              :                                  size2);
   12396         6743 :       if (flag_openmp_allocators)
   12397              :         {
   12398            2 :           tree cond, omp_tmp;
   12399            2 :           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
   12400              :                                   gfc_conv_descriptor_version_get (desc),
   12401              :                                   integer_one_node);
   12402            2 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
   12403            2 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
   12404              :                                  fold_convert (pvoid_type_node, array1), size2,
   12405              :                                  build_zero_cst (ptr_type_node),
   12406              :                                  build_zero_cst (ptr_type_node));
   12407            2 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
   12408              :                             omp_tmp, tmp);
   12409              :         }
   12410              : 
   12411         6743 :       gfc_conv_descriptor_data_set (&realloc_block, desc, tmp);
   12412              :     }
   12413              :   else
   12414              :     {
   12415           39 :       tmp = build_call_expr_loc (input_location,
   12416              :                                  gfor_fndecl_caf_deregister, 5, token,
   12417              :                                  build_int_cst (integer_type_node,
   12418              :                                                GFC_CAF_COARRAY_DEALLOCATE_ONLY),
   12419              :                                  null_pointer_node, null_pointer_node,
   12420              :                                  integer_zero_node);
   12421           39 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12422           39 :       tmp = build_call_expr_loc (input_location,
   12423              :                                  gfor_fndecl_caf_register,
   12424              :                                  7, size2,
   12425              :                                  build_int_cst (integer_type_node,
   12426              :                                            GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY),
   12427              :                                  token, gfc_build_addr_expr (NULL_TREE, desc),
   12428              :                                  null_pointer_node, null_pointer_node,
   12429              :                                  integer_zero_node);
   12430           39 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12431              :     }
   12432              : 
   12433         6782 :   if ((expr1->ts.type == BT_DERIVED)
   12434          441 :         && expr1->ts.u.derived->attr.alloc_comp)
   12435              :     {
   12436          200 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
   12437              :                                     expr1->rank);
   12438          200 :       gfc_add_expr_to_block (&realloc_block, tmp);
   12439              :     }
   12440              : 
   12441         6782 :   gfc_add_block_to_block (&realloc_block, &caf_se.post);
   12442         6782 :   realloc_expr = gfc_finish_block (&realloc_block);
   12443              : 
   12444              :   /* Malloc expression.  */
   12445         6782 :   gfc_init_block (&alloc_block);
   12446         6782 :   if (!coarray)
   12447              :     {
   12448         6743 :       tmp = build_call_expr_loc (input_location,
   12449              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   12450              :                                  1, size2);
   12451         6743 :       gfc_conv_descriptor_data_set (&alloc_block,
   12452              :                                     desc, tmp);
   12453              :     }
   12454              :   else
   12455              :     {
   12456           39 :       tmp = build_call_expr_loc (input_location,
   12457              :                                  gfor_fndecl_caf_register,
   12458              :                                  7, size2,
   12459              :                                  build_int_cst (integer_type_node,
   12460              :                                                 GFC_CAF_COARRAY_ALLOC),
   12461              :                                  token, gfc_build_addr_expr (NULL_TREE, desc),
   12462              :                                  null_pointer_node, null_pointer_node,
   12463              :                                  integer_zero_node);
   12464           39 :       gfc_add_expr_to_block (&alloc_block, tmp);
   12465              :     }
   12466              : 
   12467              : 
   12468              :   /* We already set the dtype in the case of deferred character
   12469              :      length arrays and class lvalues.  */
   12470         6782 :   if (!(GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc))
   12471         6782 :         && ((expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12472         6095 :             || coarray))
   12473        12838 :       && expr1->ts.type != BT_CLASS)
   12474         5387 :     gfc_conv_descriptor_dtype_set (&alloc_block, desc,
   12475         5387 :                                    gfc_get_dtype (TREE_TYPE (desc)));
   12476              : 
   12477         6782 :   if ((expr1->ts.type == BT_DERIVED)
   12478          441 :         && expr1->ts.u.derived->attr.alloc_comp)
   12479              :     {
   12480          200 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, desc,
   12481              :                                     expr1->rank);
   12482          200 :       gfc_add_expr_to_block (&alloc_block, tmp);
   12483              :     }
   12484         6782 :   alloc_expr = gfc_finish_block (&alloc_block);
   12485              : 
   12486              :   /* Malloc if not allocated; realloc otherwise.  */
   12487         6782 :   tmp = build3_v (COND_EXPR, cond_null, alloc_expr, realloc_expr);
   12488         6782 :   gfc_add_expr_to_block (&fblock, tmp);
   12489              : 
   12490              :   /* Add the label for same shape lhs and rhs.  */
   12491         6782 :   tmp = build1_v (LABEL_EXPR, jump_label2);
   12492         6782 :   gfc_add_expr_to_block (&fblock, tmp);
   12493              : 
   12494         6782 :   tree realloc_code = gfc_finish_block (&fblock);
   12495              : 
   12496         6782 :   stmtblock_t result_block;
   12497         6782 :   gfc_init_block (&result_block);
   12498         6782 :   gfc_add_expr_to_block (&result_block, realloc_code);
   12499         6782 :   update_reallocated_descriptor (&result_block, loop);
   12500              : 
   12501         6782 :   return gfc_finish_block (&result_block);
   12502              : }
   12503              : 
   12504              : 
   12505              : /* Initialize class descriptor's TKR information.  */
   12506              : 
   12507              : void
   12508         3070 : gfc_trans_class_array (gfc_symbol * sym, gfc_wrapped_block * block)
   12509              : {
   12510         3070 :   tree type, etype;
   12511         3070 :   tree descriptor;
   12512         3070 :   stmtblock_t init;
   12513         3070 :   int rank;
   12514              : 
   12515              :   /* Make sure the frontend gets these right.  */
   12516         3070 :   gcc_assert (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   12517              :               && (CLASS_DATA (sym)->attr.class_pointer
   12518              :                   || CLASS_DATA (sym)->attr.allocatable));
   12519              : 
   12520         3070 :   gcc_assert (VAR_P (sym->backend_decl)
   12521              :               || TREE_CODE (sym->backend_decl) == PARM_DECL);
   12522              : 
   12523         3070 :   if (sym->attr.dummy)
   12524         1496 :     return;
   12525              : 
   12526         3070 :   descriptor = gfc_class_data_get (sym->backend_decl);
   12527         3070 :   type = TREE_TYPE (descriptor);
   12528              : 
   12529         3070 :   if (type == NULL || !GFC_DESCRIPTOR_TYPE_P (type))
   12530              :     return;
   12531              : 
   12532         1574 :   location_t loc = input_location;
   12533         1574 :   input_location = gfc_get_location (&sym->declared_at);
   12534         1574 :   gfc_init_block (&init);
   12535              : 
   12536         1574 :   rank = CLASS_DATA (sym)->as ? (CLASS_DATA (sym)->as->rank) : (0);
   12537         1574 :   gcc_assert (rank>=0);
   12538         1574 :   etype = gfc_get_element_type (type);
   12539         1574 :   gfc_conv_descriptor_dtype_set (&init, descriptor,
   12540              :                                  gfc_get_dtype_rank_type (rank, etype));
   12541              : 
   12542         1574 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12543         1574 :   input_location = loc;
   12544              : }
   12545              : 
   12546              : 
   12547              : /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
   12548              :    Do likewise, recursively if necessary, with the allocatable components of
   12549              :    derived types.  This function is also called for assumed-rank arrays, which
   12550              :    are always dummy arguments.  */
   12551              : 
   12552              : void
   12553        18353 : gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
   12554              : {
   12555        18353 :   tree type;
   12556        18353 :   tree tmp;
   12557        18353 :   tree descriptor;
   12558        18353 :   stmtblock_t init;
   12559        18353 :   stmtblock_t cleanup;
   12560        18353 :   int rank;
   12561        18353 :   bool sym_has_alloc_comp, has_finalizer;
   12562              : 
   12563        36706 :   sym_has_alloc_comp = (sym->ts.type == BT_DERIVED
   12564        11013 :                         || sym->ts.type == BT_CLASS)
   12565        18353 :                           && sym->ts.u.derived->attr.alloc_comp;
   12566        18353 :   has_finalizer = gfc_may_be_finalized (sym->ts);
   12567              : 
   12568              :   /* Make sure the frontend gets these right.  */
   12569        18353 :   gcc_assert (sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp
   12570              :               || has_finalizer
   12571              :               || (sym->as->type == AS_ASSUMED_RANK && sym->attr.dummy));
   12572              : 
   12573        18353 :   location_t loc = input_location;
   12574        18353 :   input_location = gfc_get_location (&sym->declared_at);
   12575        18353 :   gfc_init_block (&init);
   12576              : 
   12577        18353 :   gcc_assert (VAR_P (sym->backend_decl)
   12578              :               || TREE_CODE (sym->backend_decl) == PARM_DECL);
   12579              : 
   12580        18353 :   if (sym->ts.type == BT_CHARACTER
   12581         1408 :       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
   12582              :     {
   12583          824 :       if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
   12584              :         {
   12585          619 :           tree len_expr = sym->ts.u.cl->backend_decl;
   12586          619 :           tree init_val = build_zero_cst (TREE_TYPE (len_expr));
   12587          619 :           if (VAR_P (len_expr)
   12588          619 :               && sym->attr.save
   12589          674 :               && !DECL_INITIAL (len_expr))
   12590           55 :             DECL_INITIAL (len_expr) = init_val;
   12591              :           else
   12592          564 :             gfc_add_modify (&init, len_expr, init_val);
   12593              :         }
   12594          824 :       gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
   12595          824 :       gfc_trans_vla_type_sizes (sym, &init);
   12596              : 
   12597              :       /* Presence check of optional deferred-length character dummy.  */
   12598          824 :       if (sym->ts.deferred && sym->attr.dummy && sym->attr.optional)
   12599              :         {
   12600           43 :           tmp = gfc_finish_block (&init);
   12601           43 :           tmp = build3_v (COND_EXPR, gfc_conv_expr_present (sym),
   12602              :                           tmp, build_empty_stmt (input_location));
   12603           43 :           gfc_add_expr_to_block (&init, tmp);
   12604              :         }
   12605              :     }
   12606              : 
   12607              :   /* Dummy, use associated and result variables don't need anything special.  */
   12608        18353 :   if (sym->attr.dummy || sym->attr.use_assoc || sym->attr.result)
   12609              :     {
   12610          912 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12611          912 :       input_location = loc;
   12612         1188 :       return;
   12613              :     }
   12614              : 
   12615        17441 :   descriptor = sym->backend_decl;
   12616              : 
   12617              :   /* Although static, derived types with default initializers and
   12618              :      allocatable components must not be nulled wholesale; instead they
   12619              :      are treated component by component.  */
   12620        17441 :   if (TREE_STATIC (descriptor) && !sym_has_alloc_comp && !has_finalizer)
   12621              :     {
   12622              :       /* SAVEd variables are not freed on exit.  */
   12623          276 :       gfc_trans_static_array_pointer (sym);
   12624              : 
   12625          276 :       gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
   12626          276 :       input_location = loc;
   12627          276 :       return;
   12628              :     }
   12629              : 
   12630              :   /* Get the descriptor type.  */
   12631        17165 :   type = TREE_TYPE (sym->backend_decl);
   12632              : 
   12633        17165 :   if ((sym_has_alloc_comp || (has_finalizer && sym->ts.type != BT_CLASS))
   12634         5690 :       && !(sym->attr.pointer || sym->attr.allocatable))
   12635              :     {
   12636         2958 :       if (!sym->attr.save
   12637         2555 :           && !(TREE_STATIC (sym->backend_decl) && sym->attr.is_main_program))
   12638              :         {
   12639         2555 :           if (sym->value == NULL
   12640         2555 :               || !gfc_has_default_initializer (sym->ts.u.derived))
   12641              :             {
   12642         2117 :               rank = sym->as ? sym->as->rank : 0;
   12643         2117 :               tmp = gfc_nullify_alloc_comp (sym->ts.u.derived,
   12644              :                                             descriptor, rank);
   12645         2117 :               gfc_add_expr_to_block (&init, tmp);
   12646              :             }
   12647              :           else
   12648          438 :             gfc_init_default_dt (sym, &init, false);
   12649              :         }
   12650              :     }
   12651        14207 :   else if (!GFC_DESCRIPTOR_TYPE_P (type))
   12652              :     {
   12653              :       /* If the backend_decl is not a descriptor, we must have a pointer
   12654              :          to one.  */
   12655         2147 :       descriptor = build_fold_indirect_ref_loc (input_location,
   12656              :                                                 sym->backend_decl);
   12657         2147 :       type = TREE_TYPE (descriptor);
   12658              :     }
   12659              : 
   12660              :   /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
   12661              :      pointers when -fcheck=pointer is specified.  */
   12662        17165 :   if (GFC_DESCRIPTOR_TYPE_P (type)
   12663        17165 :       && (sym->attr.allocatable || sym->attr.pointer))
   12664              :     {
   12665        12060 :       if (flag_coarray == GFC_FCOARRAY_LIB
   12666          349 :           && sym->attr.codimension
   12667          177 :           && !sym->attr.save)
   12668              :         {
   12669              :           /* Declare the variable static so its array descriptor stays present
   12670              :              after leaving the scope.  It may still be accessed through another
   12671              :              image.  This may happen, for example, with the caf_mpi
   12672              :              implementation.  */
   12673          177 :           TREE_STATIC (descriptor) = 1;
   12674              :         }
   12675        12060 :       gfc_init_descriptor_variable (&init, sym, descriptor);
   12676              :     }
   12677              : 
   12678        17165 :   input_location = loc;
   12679        17165 :   gfc_init_block (&cleanup);
   12680              : 
   12681              :   /* Allocatable arrays need to be freed when they go out of scope.
   12682              :      The allocatable components of pointers must not be touched.  */
   12683        17165 :   if (!sym->attr.allocatable && has_finalizer && sym->ts.type != BT_CLASS
   12684          604 :       && !sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
   12685          315 :       && !sym->ns->proc_name->attr.is_main_program)
   12686              :     {
   12687          276 :       gfc_expr *e;
   12688          276 :       sym->attr.referenced = 1;
   12689          276 :       e = gfc_lval_expr_from_sym (sym);
   12690          276 :       gfc_add_finalizer_call (&cleanup, e);
   12691          276 :       gfc_free_expr (e);
   12692          276 :     }
   12693        16889 :   else if ((!sym->attr.allocatable || !has_finalizer)
   12694        16765 :       && sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
   12695         5133 :       && !sym->attr.pointer && !sym->attr.save
   12696         2557 :       && !(sym->attr.artificial && sym->name[0] == '_')
   12697         2502 :       && !sym->ns->proc_name->attr.is_main_program)
   12698              :     {
   12699          688 :       int rank;
   12700          688 :       rank = sym->as ? sym->as->rank : 0;
   12701          688 :       tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank,
   12702          688 :                                        (sym->attr.codimension
   12703            3 :                                         && flag_coarray == GFC_FCOARRAY_LIB)
   12704              :                                        ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY
   12705              :                                        : 0);
   12706          688 :       gfc_add_expr_to_block (&cleanup, tmp);
   12707              :     }
   12708              : 
   12709        17165 :   if (sym->attr.allocatable && (sym->attr.dimension || sym->attr.codimension)
   12710         8757 :       && !sym->attr.save && !sym->attr.result
   12711         8750 :       && !sym->ns->proc_name->attr.is_main_program)
   12712              :     {
   12713         4572 :       gfc_expr *e;
   12714         4572 :       e = has_finalizer ? gfc_lval_expr_from_sym (sym) : NULL;
   12715         9144 :       tmp = gfc_deallocate_with_status (sym->backend_decl, NULL_TREE, NULL_TREE,
   12716              :                                         NULL_TREE, NULL_TREE, true, e,
   12717         4572 :                                         sym->attr.codimension
   12718              :                                         ? GFC_CAF_COARRAY_DEREGISTER
   12719              :                                         : GFC_CAF_COARRAY_NOCOARRAY,
   12720              :                                         NULL_TREE, gfc_finish_block (&cleanup));
   12721         4572 :       if (e)
   12722           45 :         gfc_free_expr (e);
   12723         4572 :       gfc_init_block (&cleanup);
   12724         4572 :       gfc_add_expr_to_block (&cleanup, tmp);
   12725              :     }
   12726              : 
   12727        17165 :   gfc_add_init_cleanup (block, gfc_finish_block (&init),
   12728              :                         gfc_finish_block (&cleanup));
   12729              : }
   12730              : 
   12731              : /************ Expression Walking Functions ******************/
   12732              : 
   12733              : /* Walk a variable reference.
   12734              : 
   12735              :    Possible extension - multiple component subscripts.
   12736              :     x(:,:) = foo%a(:)%b(:)
   12737              :    Transforms to
   12738              :     forall (i=..., j=...)
   12739              :       x(i,j) = foo%a(j)%b(i)
   12740              :     end forall
   12741              :    This adds a fair amount of complexity because you need to deal with more
   12742              :    than one ref.  Maybe handle in a similar manner to vector subscripts.
   12743              :    Maybe not worth the effort.  */
   12744              : 
   12745              : 
   12746              : static gfc_ss *
   12747       696208 : gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
   12748              : {
   12749       696208 :   gfc_ref *ref;
   12750              : 
   12751       696208 :   gfc_fix_class_refs (expr);
   12752              : 
   12753       815261 :   for (ref = expr->ref; ref; ref = ref->next)
   12754       454112 :     if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
   12755              :       break;
   12756              : 
   12757       696208 :   return gfc_walk_array_ref (ss, expr, ref);
   12758              : }
   12759              : 
   12760              : gfc_ss *
   12761       696689 : gfc_walk_array_ref (gfc_ss *ss, gfc_expr *expr, gfc_ref *ref, bool array_only)
   12762              : {
   12763       696689 :   gfc_array_ref *ar;
   12764       696689 :   gfc_ss *newss;
   12765       696689 :   int n;
   12766              : 
   12767      1042038 :   for (; ref; ref = ref->next)
   12768              :     {
   12769       345349 :       if (ref->type == REF_SUBSTRING)
   12770              :         {
   12771         1308 :           ss = gfc_get_scalar_ss (ss, ref->u.ss.start);
   12772         1308 :           if (ref->u.ss.end)
   12773         1282 :             ss = gfc_get_scalar_ss (ss, ref->u.ss.end);
   12774              :         }
   12775              : 
   12776              :       /* We're only interested in array sections from now on.  */
   12777       345349 :       if (ref->type != REF_ARRAY
   12778       335950 :           || (array_only && ref->u.ar.as && ref->u.ar.as->rank == 0))
   12779         9514 :         continue;
   12780              : 
   12781       335835 :       ar = &ref->u.ar;
   12782              : 
   12783       335835 :       switch (ar->type)
   12784              :         {
   12785          326 :         case AR_ELEMENT:
   12786          699 :           for (n = ar->dimen - 1; n >= 0; n--)
   12787          373 :             ss = gfc_get_scalar_ss (ss, ar->start[n]);
   12788              :           break;
   12789              : 
   12790       278635 :         case AR_FULL:
   12791              :           /* Assumed shape arrays from interface mapping need this fix.  */
   12792       278635 :           if (!ar->as && expr->symtree->n.sym->as)
   12793              :             {
   12794            6 :               ar->as = gfc_get_array_spec();
   12795            6 :               *ar->as = *expr->symtree->n.sym->as;
   12796              :             }
   12797       278635 :           newss = gfc_get_array_ss (ss, expr, ar->as->rank, GFC_SS_SECTION);
   12798       278635 :           newss->info->data.array.ref = ref;
   12799              : 
   12800              :           /* Make sure array is the same as array(:,:), this way
   12801              :              we don't need to special case all the time.  */
   12802       278635 :           ar->dimen = ar->as->rank;
   12803       639879 :           for (n = 0; n < ar->dimen; n++)
   12804              :             {
   12805       361244 :               ar->dimen_type[n] = DIMEN_RANGE;
   12806              : 
   12807       361244 :               gcc_assert (ar->start[n] == NULL);
   12808       361244 :               gcc_assert (ar->end[n] == NULL);
   12809       361244 :               gcc_assert (ar->stride[n] == NULL);
   12810              :             }
   12811              :           ss = newss;
   12812              :           break;
   12813              : 
   12814        56874 :         case AR_SECTION:
   12815        56874 :           newss = gfc_get_array_ss (ss, expr, 0, GFC_SS_SECTION);
   12816        56874 :           newss->info->data.array.ref = ref;
   12817              : 
   12818              :           /* We add SS chains for all the subscripts in the section.  */
   12819       146187 :           for (n = 0; n < ar->dimen; n++)
   12820              :             {
   12821        89313 :               gfc_ss *indexss;
   12822              : 
   12823        89313 :               switch (ar->dimen_type[n])
   12824              :                 {
   12825         6871 :                 case DIMEN_ELEMENT:
   12826              :                   /* Add SS for elemental (scalar) subscripts.  */
   12827         6871 :                   gcc_assert (ar->start[n]);
   12828         6871 :                   indexss = gfc_get_scalar_ss (gfc_ss_terminator, ar->start[n]);
   12829         6871 :                   indexss->loop_chain = gfc_ss_terminator;
   12830         6871 :                   newss->info->data.array.subscript[n] = indexss;
   12831         6871 :                   break;
   12832              : 
   12833        81396 :                 case DIMEN_RANGE:
   12834              :                   /* We don't add anything for sections, just remember this
   12835              :                      dimension for later.  */
   12836        81396 :                   newss->dim[newss->dimen] = n;
   12837        81396 :                   newss->dimen++;
   12838        81396 :                   break;
   12839              : 
   12840         1046 :                 case DIMEN_VECTOR:
   12841              :                   /* Create a GFC_SS_VECTOR index in which we can store
   12842              :                      the vector's descriptor.  */
   12843         1046 :                   indexss = gfc_get_array_ss (gfc_ss_terminator, ar->start[n],
   12844              :                                               1, GFC_SS_VECTOR);
   12845         1046 :                   indexss->loop_chain = gfc_ss_terminator;
   12846         1046 :                   newss->info->data.array.subscript[n] = indexss;
   12847         1046 :                   newss->dim[newss->dimen] = n;
   12848         1046 :                   newss->dimen++;
   12849         1046 :                   break;
   12850              : 
   12851            0 :                 default:
   12852              :                   /* We should know what sort of section it is by now.  */
   12853            0 :                   gcc_unreachable ();
   12854              :                 }
   12855              :             }
   12856              :           /* We should have at least one non-elemental dimension,
   12857              :              unless we are creating a descriptor for a (scalar) coarray.  */
   12858        56874 :           gcc_assert (newss->dimen > 0
   12859              :                       || newss->info->data.array.ref->u.ar.as->corank > 0);
   12860              :           ss = newss;
   12861              :           break;
   12862              : 
   12863            0 :         default:
   12864              :           /* We should know what sort of section it is by now.  */
   12865            0 :           gcc_unreachable ();
   12866              :         }
   12867              : 
   12868              :     }
   12869       696689 :   return ss;
   12870              : }
   12871              : 
   12872              : 
   12873              : /* Walk an expression operator. If only one operand of a binary expression is
   12874              :    scalar, we must also add the scalar term to the SS chain.  */
   12875              : 
   12876              : static gfc_ss *
   12877        58505 : gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
   12878              : {
   12879        58505 :   gfc_ss *head;
   12880        58505 :   gfc_ss *head2;
   12881              : 
   12882        58505 :   head = gfc_walk_subexpr (ss, expr->value.op.op1);
   12883        58505 :   if (expr->value.op.op2 == NULL)
   12884              :     head2 = head;
   12885              :   else
   12886        55823 :     head2 = gfc_walk_subexpr (head, expr->value.op.op2);
   12887              : 
   12888              :   /* All operands are scalar.  Pass back and let the caller deal with it.  */
   12889        58505 :   if (head2 == ss)
   12890              :     return head2;
   12891              : 
   12892              :   /* All operands require scalarization.  */
   12893        52699 :   if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
   12894              :     return head2;
   12895              : 
   12896              :   /* One of the operands needs scalarization, the other is scalar.
   12897              :      Create a gfc_ss for the scalar expression.  */
   12898        19526 :   if (head == ss)
   12899              :     {
   12900              :       /* First operand is scalar.  We build the chain in reverse order, so
   12901              :          add the scalar SS after the second operand.  */
   12902              :       head = head2;
   12903         2280 :       while (head && head->next != ss)
   12904              :         head = head->next;
   12905              :       /* Check we haven't somehow broken the chain.  */
   12906         2037 :       gcc_assert (head);
   12907         2037 :       head->next = gfc_get_scalar_ss (ss, expr->value.op.op1);
   12908              :     }
   12909              :   else                          /* head2 == head */
   12910              :     {
   12911        17489 :       gcc_assert (head2 == head);
   12912              :       /* Second operand is scalar.  */
   12913        17489 :       head2 = gfc_get_scalar_ss (head2, expr->value.op.op2);
   12914              :     }
   12915              : 
   12916              :   return head2;
   12917              : }
   12918              : 
   12919              : static gfc_ss *
   12920           36 : gfc_walk_conditional_expr (gfc_ss *ss, gfc_expr *expr)
   12921              : {
   12922           36 :   gfc_ss *head;
   12923              : 
   12924           36 :   head = gfc_walk_subexpr (ss, expr->value.conditional.true_expr);
   12925           36 :   head = gfc_walk_subexpr (head, expr->value.conditional.false_expr);
   12926           36 :   return head;
   12927              : }
   12928              : 
   12929              : /* Reverse a SS chain.  */
   12930              : 
   12931              : gfc_ss *
   12932       873693 : gfc_reverse_ss (gfc_ss * ss)
   12933              : {
   12934       873693 :   gfc_ss *next;
   12935       873693 :   gfc_ss *head;
   12936              : 
   12937       873693 :   gcc_assert (ss != NULL);
   12938              : 
   12939              :   head = gfc_ss_terminator;
   12940      1319569 :   while (ss != gfc_ss_terminator)
   12941              :     {
   12942       445876 :       next = ss->next;
   12943              :       /* Check we didn't somehow break the chain.  */
   12944       445876 :       gcc_assert (next != NULL);
   12945       445876 :       ss->next = head;
   12946       445876 :       head = ss;
   12947       445876 :       ss = next;
   12948              :     }
   12949              : 
   12950       873693 :   return (head);
   12951              : }
   12952              : 
   12953              : 
   12954              : /* Given an expression referring to a procedure, return the symbol of its
   12955              :    interface.  We can't get the procedure symbol directly as we have to handle
   12956              :    the case of (deferred) type-bound procedures.  */
   12957              : 
   12958              : gfc_symbol *
   12959          161 : gfc_get_proc_ifc_for_expr (gfc_expr *procedure_ref)
   12960              : {
   12961          161 :   gfc_symbol *sym;
   12962          161 :   gfc_ref *ref;
   12963              : 
   12964          161 :   if (procedure_ref == NULL)
   12965              :     return NULL;
   12966              : 
   12967              :   /* Normal procedure case.  */
   12968          161 :   if (procedure_ref->expr_type == EXPR_FUNCTION
   12969          161 :       && procedure_ref->value.function.esym)
   12970              :     sym = procedure_ref->value.function.esym;
   12971              :   else
   12972           24 :     sym = procedure_ref->symtree->n.sym;
   12973              : 
   12974              :   /* Typebound procedure case.  */
   12975          209 :   for (ref = procedure_ref->ref; ref; ref = ref->next)
   12976              :     {
   12977           48 :       if (ref->type == REF_COMPONENT
   12978           48 :           && ref->u.c.component->attr.proc_pointer)
   12979           24 :         sym = ref->u.c.component->ts.interface;
   12980              :       else
   12981              :         sym = NULL;
   12982              :     }
   12983              : 
   12984              :   return sym;
   12985              : }
   12986              : 
   12987              : 
   12988              : /* Given an expression referring to an intrinsic function call,
   12989              :    return the intrinsic symbol.  */
   12990              : 
   12991              : gfc_intrinsic_sym *
   12992         7970 : gfc_get_intrinsic_for_expr (gfc_expr *call)
   12993              : {
   12994         7970 :   if (call == NULL)
   12995              :     return NULL;
   12996              : 
   12997              :   /* Normal procedure case.  */
   12998         2372 :   if (call->expr_type == EXPR_FUNCTION)
   12999         2266 :     return call->value.function.isym;
   13000              :   else
   13001              :     return NULL;
   13002              : }
   13003              : 
   13004              : 
   13005              : /* Indicates whether an argument to an intrinsic function should be used in
   13006              :    scalarization.  It is usually the case, except for some intrinsics
   13007              :    requiring the value to be constant, and using the value at compile time only.
   13008              :    As the value is not used at runtime in those cases, we don’t produce code
   13009              :    for it, and it should not be visible to the scalarizer.
   13010              :    FUNCTION is the intrinsic function being called, ACTUAL_ARG is the actual
   13011              :    argument being examined in that call, and ARG_NUM the index number
   13012              :    of ACTUAL_ARG in the list of arguments.
   13013              :    The intrinsic procedure’s dummy argument associated with ACTUAL_ARG is
   13014              :    identified using the name in ACTUAL_ARG if it is present (that is: if it’s
   13015              :    a keyword argument), otherwise using ARG_NUM.  */
   13016              : 
   13017              : static bool
   13018        38087 : arg_evaluated_for_scalarization (gfc_intrinsic_sym *function,
   13019              :                                  gfc_dummy_arg *dummy_arg)
   13020              : {
   13021        38087 :   if (function != NULL && dummy_arg != NULL)
   13022              :     {
   13023        12473 :       switch (function->id)
   13024              :         {
   13025          241 :           case GFC_ISYM_INDEX:
   13026          241 :           case GFC_ISYM_LEN_TRIM:
   13027          241 :           case GFC_ISYM_MASKL:
   13028          241 :           case GFC_ISYM_MASKR:
   13029          241 :           case GFC_ISYM_SCAN:
   13030          241 :           case GFC_ISYM_VERIFY:
   13031          241 :             if (strcmp ("kind", gfc_dummy_arg_get_name (*dummy_arg)) == 0)
   13032           33 :               return false;
   13033              :           /* Fallthrough.  */
   13034              : 
   13035              :           default:
   13036              :             break;
   13037              :         }
   13038              :     }
   13039              : 
   13040              :   return true;
   13041              : }
   13042              : 
   13043              : 
   13044              : /* Walk the arguments of an elemental function.
   13045              :    PROC_EXPR is used to check whether an argument is permitted to be absent.  If
   13046              :    it is NULL, we don't do the check and the argument is assumed to be present.
   13047              : */
   13048              : 
   13049              : gfc_ss *
   13050        27062 : gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
   13051              :                                   gfc_intrinsic_sym *intrinsic_sym,
   13052              :                                   gfc_ss_type type)
   13053              : {
   13054        27062 :   int scalar;
   13055        27062 :   gfc_ss *head;
   13056        27062 :   gfc_ss *tail;
   13057        27062 :   gfc_ss *newss;
   13058              : 
   13059        27062 :   head = gfc_ss_terminator;
   13060        27062 :   tail = NULL;
   13061              : 
   13062        27062 :   scalar = 1;
   13063        66613 :   for (; arg; arg = arg->next)
   13064              :     {
   13065        39551 :       gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
   13066        41048 :       if (!arg->expr
   13067        38237 :           || arg->expr->expr_type == EXPR_NULL
   13068        77638 :           || !arg_evaluated_for_scalarization (intrinsic_sym, dummy_arg))
   13069         1497 :         continue;
   13070              : 
   13071        38054 :       newss = gfc_walk_subexpr (head, arg->expr);
   13072        38054 :       if (newss == head)
   13073              :         {
   13074              :           /* Scalar argument.  */
   13075        18595 :           gcc_assert (type == GFC_SS_SCALAR || type == GFC_SS_REFERENCE);
   13076        18595 :           newss = gfc_get_scalar_ss (head, arg->expr);
   13077        18595 :           newss->info->type = type;
   13078        18595 :           if (dummy_arg)
   13079        15463 :             newss->info->data.scalar.dummy_arg = dummy_arg;
   13080              :         }
   13081              :       else
   13082              :         scalar = 0;
   13083              : 
   13084        34922 :       if (dummy_arg != NULL
   13085        26444 :           && gfc_dummy_arg_is_optional (*dummy_arg)
   13086         2544 :           && arg->expr->expr_type == EXPR_VARIABLE
   13087        36632 :           && (gfc_expr_attr (arg->expr).optional
   13088         1199 :               || gfc_expr_attr (arg->expr).allocatable
   13089        38054 :               || gfc_expr_attr (arg->expr).pointer))
   13090         1023 :         newss->info->can_be_null_ref = true;
   13091              : 
   13092        38054 :       head = newss;
   13093        38054 :       if (!tail)
   13094              :         {
   13095              :           tail = head;
   13096        33780 :           while (tail->next != gfc_ss_terminator)
   13097              :             tail = tail->next;
   13098              :         }
   13099              :     }
   13100              : 
   13101        27062 :   if (scalar)
   13102              :     {
   13103              :       /* If all the arguments are scalar we don't need the argument SS.  */
   13104        10375 :       gfc_free_ss_chain (head);
   13105              :       /* Pass it back.  */
   13106        10375 :       return ss;
   13107              :     }
   13108              : 
   13109              :   /* Add it onto the existing chain.  */
   13110        16687 :   tail->next = ss;
   13111        16687 :   return head;
   13112              : }
   13113              : 
   13114              : 
   13115              : /* Walk a function call.  Scalar functions are passed back, and taken out of
   13116              :    scalarization loops.  For elemental functions we walk their arguments.
   13117              :    The result of functions returning arrays is stored in a temporary outside
   13118              :    the loop, so that the function is only called once.  Hence we do not need
   13119              :    to walk their arguments.  */
   13120              : 
   13121              : static gfc_ss *
   13122        63784 : gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
   13123              : {
   13124        63784 :   gfc_intrinsic_sym *isym;
   13125        63784 :   gfc_symbol *sym;
   13126        63784 :   gfc_component *comp = NULL;
   13127              : 
   13128        63784 :   isym = expr->value.function.isym;
   13129              : 
   13130              :   /* Handle intrinsic functions separately.  */
   13131        63784 :   if (isym)
   13132        56044 :     return gfc_walk_intrinsic_function (ss, expr, isym);
   13133              : 
   13134         7740 :   sym = expr->value.function.esym;
   13135         7740 :   if (!sym)
   13136          546 :     sym = expr->symtree->n.sym;
   13137              : 
   13138         7740 :   if (gfc_is_class_array_function (expr))
   13139          234 :     return gfc_get_array_ss (ss, expr,
   13140          234 :                              CLASS_DATA (expr->value.function.esym->result)->as->rank,
   13141          234 :                              GFC_SS_FUNCTION);
   13142              : 
   13143              :   /* A function that returns arrays.  */
   13144         7506 :   comp = gfc_get_proc_ptr_comp (expr);
   13145         7108 :   if ((!comp && gfc_return_by_reference (sym) && sym->result->attr.dimension)
   13146         7506 :       || (comp && comp->attr.dimension))
   13147         2680 :     return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
   13148              : 
   13149              :   /* Walk the parameters of an elemental function.  For now we always pass
   13150              :      by reference.  */
   13151         4826 :   if (sym->attr.elemental || (comp && comp->attr.elemental))
   13152              :     {
   13153         2230 :       gfc_ss *old_ss = ss;
   13154              : 
   13155         2230 :       ss = gfc_walk_elemental_function_args (old_ss,
   13156              :                                              expr->value.function.actual,
   13157              :                                              gfc_get_intrinsic_for_expr (expr),
   13158              :                                              GFC_SS_REFERENCE);
   13159         2230 :       if (ss != old_ss
   13160         1194 :           && (comp
   13161         1133 :               || sym->attr.proc_pointer
   13162         1133 :               || sym->attr.if_source != IFSRC_DECL
   13163         1011 :               || sym->attr.array_outer_dependency))
   13164          231 :         ss->info->array_outer_dependency = 1;
   13165              :     }
   13166              : 
   13167              :   /* Scalar functions are OK as these are evaluated outside the scalarization
   13168              :      loop.  Pass back and let the caller deal with it.  */
   13169              :   return ss;
   13170              : }
   13171              : 
   13172              : 
   13173              : /* An array temporary is constructed for array constructors.  */
   13174              : 
   13175              : static gfc_ss *
   13176        51559 : gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
   13177              : {
   13178            0 :   return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_CONSTRUCTOR);
   13179              : }
   13180              : 
   13181              : 
   13182              : /* Walk an expression.  Add walked expressions to the head of the SS chain.
   13183              :    A wholly scalar expression will not be added.  */
   13184              : 
   13185              : gfc_ss *
   13186      1030239 : gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
   13187              : {
   13188      1030239 :   gfc_ss *head;
   13189              : 
   13190      1030239 :   switch (expr->expr_type)
   13191              :     {
   13192       696208 :     case EXPR_VARIABLE:
   13193       696208 :       head = gfc_walk_variable_expr (ss, expr);
   13194       696208 :       return head;
   13195              : 
   13196        58505 :     case EXPR_OP:
   13197        58505 :       head = gfc_walk_op_expr (ss, expr);
   13198        58505 :       return head;
   13199              : 
   13200           36 :     case EXPR_CONDITIONAL:
   13201           36 :       head = gfc_walk_conditional_expr (ss, expr);
   13202           36 :       return head;
   13203              : 
   13204        63784 :     case EXPR_FUNCTION:
   13205        63784 :       head = gfc_walk_function_expr (ss, expr);
   13206        63784 :       return head;
   13207              : 
   13208              :     case EXPR_CONSTANT:
   13209              :     case EXPR_NULL:
   13210              :     case EXPR_STRUCTURE:
   13211              :       /* Pass back and let the caller deal with it.  */
   13212              :       break;
   13213              : 
   13214        51559 :     case EXPR_ARRAY:
   13215        51559 :       head = gfc_walk_array_constructor (ss, expr);
   13216        51559 :       return head;
   13217              : 
   13218              :     case EXPR_SUBSTRING:
   13219              :       /* Pass back and let the caller deal with it.  */
   13220              :       break;
   13221              : 
   13222            0 :     default:
   13223            0 :       gfc_internal_error ("bad expression type during walk (%d)",
   13224              :                       expr->expr_type);
   13225              :     }
   13226              :   return ss;
   13227              : }
   13228              : 
   13229              : 
   13230              : /* Entry point for expression walking.
   13231              :    A return value equal to the passed chain means this is
   13232              :    a scalar expression.  It is up to the caller to take whatever action is
   13233              :    necessary to translate these.  */
   13234              : 
   13235              : gfc_ss *
   13236       870756 : gfc_walk_expr (gfc_expr * expr)
   13237              : {
   13238       870756 :   gfc_ss *res;
   13239              : 
   13240       870756 :   res = gfc_walk_subexpr (gfc_ss_terminator, expr);
   13241       870756 :   return gfc_reverse_ss (res);
   13242              : }
        

Generated by: LCOV version 2.4-beta

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